@posthog/agent 2.3.110 → 2.3.116
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 +46 -13
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +46 -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 +13 -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.116",
|
|
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) {
|
|
@@ -12168,7 +12192,9 @@ You MUST NOT create a new branch, close the existing PR, or create a new PR.`
|
|
|
12168
12192
|
}
|
|
12169
12193
|
let assistantMessage;
|
|
12170
12194
|
try {
|
|
12171
|
-
await this.session.logWriter.flush(this.session.payload.run_id
|
|
12195
|
+
await this.session.logWriter.flush(this.session.payload.run_id, {
|
|
12196
|
+
coalesce: true
|
|
12197
|
+
});
|
|
12172
12198
|
assistantMessage = this.session.logWriter.getFullAgentResponse(
|
|
12173
12199
|
this.session.payload.run_id
|
|
12174
12200
|
);
|
|
@@ -12341,6 +12367,9 @@ You MUST NOT create a new branch, close the existing PR, or create a new PR.`
|
|
|
12341
12367
|
}
|
|
12342
12368
|
});
|
|
12343
12369
|
this.logger.info("Session initialized successfully");
|
|
12370
|
+
this.logger.info(
|
|
12371
|
+
`Agent version: ${this.config.version ?? package_default.version}`
|
|
12372
|
+
);
|
|
12344
12373
|
this.posthogAPI.updateTaskRun(payload.task_id, payload.run_id, {
|
|
12345
12374
|
status: "in_progress"
|
|
12346
12375
|
}).catch(
|
|
@@ -12618,7 +12647,9 @@ Important:
|
|
|
12618
12647
|
async signalTaskComplete(payload, stopReason) {
|
|
12619
12648
|
if (this.session?.payload.run_id === payload.run_id) {
|
|
12620
12649
|
try {
|
|
12621
|
-
await this.session.logWriter.flush(payload.run_id
|
|
12650
|
+
await this.session.logWriter.flush(payload.run_id, {
|
|
12651
|
+
coalesce: true
|
|
12652
|
+
});
|
|
12622
12653
|
} catch (error) {
|
|
12623
12654
|
this.logger.warn("Failed to flush session logs before completion", {
|
|
12624
12655
|
taskId: payload.task_id,
|
|
@@ -12727,7 +12758,7 @@ Important:
|
|
|
12727
12758
|
return;
|
|
12728
12759
|
}
|
|
12729
12760
|
try {
|
|
12730
|
-
await this.session.logWriter.flush(payload.run_id);
|
|
12761
|
+
await this.session.logWriter.flush(payload.run_id, { coalesce: true });
|
|
12731
12762
|
} catch (error) {
|
|
12732
12763
|
this.logger.warn("Failed to flush logs before Slack relay", {
|
|
12733
12764
|
taskId: payload.task_id,
|
|
@@ -12876,7 +12907,9 @@ Important:
|
|
|
12876
12907
|
this.logger.error("Failed to capture final tree state", error);
|
|
12877
12908
|
}
|
|
12878
12909
|
try {
|
|
12879
|
-
await this.session.logWriter.flush(this.session.payload.run_id
|
|
12910
|
+
await this.session.logWriter.flush(this.session.payload.run_id, {
|
|
12911
|
+
coalesce: true
|
|
12912
|
+
});
|
|
12880
12913
|
} catch (error) {
|
|
12881
12914
|
this.logger.error("Failed to flush session logs", error);
|
|
12882
12915
|
}
|