@posthog/agent 2.3.110 → 2.3.125
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/agent.js +34 -10
- package/dist/agent.js.map +1 -1
- package/dist/posthog-api.js +1 -1
- package/dist/posthog-api.js.map +1 -1
- package/dist/server/agent-server.js +49 -13
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +49 -13
- package/dist/server/bin.cjs.map +1 -1
- package/package.json +1 -1
- package/src/agent.ts +1 -1
- package/src/server/agent-server.ts +17 -4
- package/src/session-log-writer.test.ts +216 -0
- package/src/session-log-writer.ts +48 -11
package/dist/server/bin.cjs
CHANGED
|
@@ -904,7 +904,7 @@ var import_hono = require("hono");
|
|
|
904
904
|
// package.json
|
|
905
905
|
var package_default = {
|
|
906
906
|
name: "@posthog/agent",
|
|
907
|
-
version: "2.3.
|
|
907
|
+
version: "2.3.125",
|
|
908
908
|
repository: "https://github.com/PostHog/code",
|
|
909
909
|
description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
|
|
910
910
|
exports: {
|
|
@@ -11357,9 +11357,9 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
11357
11357
|
this.logger = options.logger ?? new Logger({ debug: false, prefix: "[SessionLogWriter]" });
|
|
11358
11358
|
}
|
|
11359
11359
|
async flushAll() {
|
|
11360
|
-
const sessionIds = [...this.sessions.keys()];
|
|
11361
11360
|
const flushPromises = [];
|
|
11362
|
-
for (const sessionId of
|
|
11361
|
+
for (const [sessionId, session] of this.sessions) {
|
|
11362
|
+
this.emitCoalescedMessage(sessionId, session);
|
|
11363
11363
|
flushPromises.push(this.flush(sessionId));
|
|
11364
11364
|
}
|
|
11365
11365
|
await Promise.all(flushPromises);
|
|
@@ -11415,7 +11415,11 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
11415
11415
|
}
|
|
11416
11416
|
return;
|
|
11417
11417
|
}
|
|
11418
|
-
this.
|
|
11418
|
+
if (this.isDirectAgentMessage(message) && session.chunkBuffer) {
|
|
11419
|
+
session.chunkBuffer = void 0;
|
|
11420
|
+
} else {
|
|
11421
|
+
this.emitCoalescedMessage(sessionId, session);
|
|
11422
|
+
}
|
|
11419
11423
|
const nonChunkAgentText = this.extractAgentMessageText(message);
|
|
11420
11424
|
if (nonChunkAgentText) {
|
|
11421
11425
|
session.lastAgentMessage = nonChunkAgentText;
|
|
@@ -11441,7 +11445,13 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
11441
11445
|
});
|
|
11442
11446
|
}
|
|
11443
11447
|
}
|
|
11444
|
-
async flush(sessionId) {
|
|
11448
|
+
async flush(sessionId, { coalesce = false } = {}) {
|
|
11449
|
+
if (coalesce) {
|
|
11450
|
+
const session = this.sessions.get(sessionId);
|
|
11451
|
+
if (session) {
|
|
11452
|
+
this.emitCoalescedMessage(sessionId, session);
|
|
11453
|
+
}
|
|
11454
|
+
}
|
|
11445
11455
|
const prev = this.flushQueues.get(sessionId) ?? Promise.resolve();
|
|
11446
11456
|
const next = prev.catch(() => {
|
|
11447
11457
|
}).then(() => this._doFlush(sessionId));
|
|
@@ -11459,7 +11469,6 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
11459
11469
|
this.logger.warn("flush: no session found", { sessionId });
|
|
11460
11470
|
return;
|
|
11461
11471
|
}
|
|
11462
|
-
this.emitCoalescedMessage(sessionId, session);
|
|
11463
11472
|
const pending = this.pendingEntries.get(sessionId);
|
|
11464
11473
|
if (!this.posthogAPI || !pending?.length) {
|
|
11465
11474
|
return;
|
|
@@ -11508,11 +11517,17 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
11508
11517
|
}
|
|
11509
11518
|
}
|
|
11510
11519
|
}
|
|
11511
|
-
|
|
11512
|
-
if (message.method !== "session/update") return
|
|
11520
|
+
getSessionUpdateType(message) {
|
|
11521
|
+
if (message.method !== "session/update") return void 0;
|
|
11513
11522
|
const params = message.params;
|
|
11514
11523
|
const update = params?.update;
|
|
11515
|
-
return update?.sessionUpdate
|
|
11524
|
+
return update?.sessionUpdate;
|
|
11525
|
+
}
|
|
11526
|
+
isDirectAgentMessage(message) {
|
|
11527
|
+
return this.getSessionUpdateType(message) === "agent_message";
|
|
11528
|
+
}
|
|
11529
|
+
isAgentMessageChunk(message) {
|
|
11530
|
+
return this.getSessionUpdateType(message) === "agent_message_chunk";
|
|
11516
11531
|
}
|
|
11517
11532
|
extractChunkText(message) {
|
|
11518
11533
|
const params = message.params;
|
|
@@ -11557,6 +11572,15 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
11557
11572
|
getFullAgentResponse(sessionId) {
|
|
11558
11573
|
const session = this.sessions.get(sessionId);
|
|
11559
11574
|
if (!session || session.currentTurnMessages.length === 0) return void 0;
|
|
11575
|
+
if (session.chunkBuffer) {
|
|
11576
|
+
this.logger.warn(
|
|
11577
|
+
"getFullAgentResponse called with non-empty chunk buffer",
|
|
11578
|
+
{
|
|
11579
|
+
sessionId,
|
|
11580
|
+
bufferedLength: session.chunkBuffer.text.length
|
|
11581
|
+
}
|
|
11582
|
+
);
|
|
11583
|
+
}
|
|
11560
11584
|
return session.currentTurnMessages.join("\n\n");
|
|
11561
11585
|
}
|
|
11562
11586
|
resetTurnMessages(sessionId) {
|
|
@@ -12160,6 +12184,9 @@ You MUST NOT create a new branch, close the existing PR, or create a new PR.`
|
|
|
12160
12184
|
}
|
|
12161
12185
|
}
|
|
12162
12186
|
});
|
|
12187
|
+
this.logger.info("User message completed", {
|
|
12188
|
+
stopReason: result.stopReason
|
|
12189
|
+
});
|
|
12163
12190
|
this.broadcastTurnComplete(result.stopReason);
|
|
12164
12191
|
if (result.stopReason === "end_turn") {
|
|
12165
12192
|
this.relayAgentResponse(this.session.payload).catch(
|
|
@@ -12168,7 +12195,9 @@ You MUST NOT create a new branch, close the existing PR, or create a new PR.`
|
|
|
12168
12195
|
}
|
|
12169
12196
|
let assistantMessage;
|
|
12170
12197
|
try {
|
|
12171
|
-
await this.session.logWriter.flush(this.session.payload.run_id
|
|
12198
|
+
await this.session.logWriter.flush(this.session.payload.run_id, {
|
|
12199
|
+
coalesce: true
|
|
12200
|
+
});
|
|
12172
12201
|
assistantMessage = this.session.logWriter.getFullAgentResponse(
|
|
12173
12202
|
this.session.payload.run_id
|
|
12174
12203
|
);
|
|
@@ -12341,6 +12370,9 @@ You MUST NOT create a new branch, close the existing PR, or create a new PR.`
|
|
|
12341
12370
|
}
|
|
12342
12371
|
});
|
|
12343
12372
|
this.logger.info("Session initialized successfully");
|
|
12373
|
+
this.logger.info(
|
|
12374
|
+
`Agent version: ${this.config.version ?? package_default.version}`
|
|
12375
|
+
);
|
|
12344
12376
|
this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
|
|
12345
12377
|
status: "in_progress"
|
|
12346
12378
|
}).catch(
|
|
@@ -12618,7 +12650,9 @@ Important:
|
|
|
12618
12650
|
async signalTaskComplete(payload, stopReason) {
|
|
12619
12651
|
if (this.session?.payload.run_id === payload.run_id) {
|
|
12620
12652
|
try {
|
|
12621
|
-
await this.session.logWriter.flush(payload.run_id
|
|
12653
|
+
await this.session.logWriter.flush(payload.run_id, {
|
|
12654
|
+
coalesce: true
|
|
12655
|
+
});
|
|
12622
12656
|
} catch (error) {
|
|
12623
12657
|
this.logger.warn("Failed to flush session logs before completion", {
|
|
12624
12658
|
taskId: payload.task_id,
|
|
@@ -12727,7 +12761,7 @@ Important:
|
|
|
12727
12761
|
return;
|
|
12728
12762
|
}
|
|
12729
12763
|
try {
|
|
12730
|
-
await this.session.logWriter.flush(payload.run_id);
|
|
12764
|
+
await this.session.logWriter.flush(payload.run_id, { coalesce: true });
|
|
12731
12765
|
} catch (error) {
|
|
12732
12766
|
this.logger.warn("Failed to flush logs before Slack relay", {
|
|
12733
12767
|
taskId: payload.task_id,
|
|
@@ -12876,7 +12910,9 @@ Important:
|
|
|
12876
12910
|
this.logger.error("Failed to capture final tree state", error);
|
|
12877
12911
|
}
|
|
12878
12912
|
try {
|
|
12879
|
-
await this.session.logWriter.flush(this.session.payload.run_id
|
|
12913
|
+
await this.session.logWriter.flush(this.session.payload.run_id, {
|
|
12914
|
+
coalesce: true
|
|
12915
|
+
});
|
|
12880
12916
|
} catch (error) {
|
|
12881
12917
|
this.logger.error("Failed to flush session logs", error);
|
|
12882
12918
|
}
|