@posthog/agent 2.1.48 → 2.1.59
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-DcBmoTR4.d.ts → agent-9gv5HohC.d.ts} +4 -0
- package/dist/agent.d.ts +1 -1
- package/dist/agent.js +52 -12
- package/dist/agent.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +122 -82
- package/dist/index.js.map +1 -1
- package/dist/server/agent-server.js +120 -81
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +127 -88
- package/dist/server/bin.cjs.map +1 -1
- package/dist/types.d.ts +2 -0
- package/package.json +3 -2
- package/src/agent.ts +1 -0
- package/src/session-log-writer.ts +50 -0
- package/src/types.ts +2 -0
|
@@ -44,6 +44,8 @@ interface SessionLogWriterOptions {
|
|
|
44
44
|
posthogAPI?: PostHogAPIClient;
|
|
45
45
|
/** Logger instance */
|
|
46
46
|
logger?: Logger;
|
|
47
|
+
/** Local cache path for instant log loading (e.g., ~/.twig) */
|
|
48
|
+
localCachePath?: string;
|
|
47
49
|
}
|
|
48
50
|
declare class SessionLogWriter {
|
|
49
51
|
private static readonly FLUSH_DEBOUNCE_MS;
|
|
@@ -58,6 +60,7 @@ declare class SessionLogWriter {
|
|
|
58
60
|
private sessions;
|
|
59
61
|
private messageCounts;
|
|
60
62
|
private logger;
|
|
63
|
+
private localCachePath?;
|
|
61
64
|
constructor(options?: SessionLogWriterOptions);
|
|
62
65
|
flushAll(): Promise<void>;
|
|
63
66
|
register(sessionId: string, context: SessionContext): void;
|
|
@@ -68,6 +71,7 @@ declare class SessionLogWriter {
|
|
|
68
71
|
private extractChunkText;
|
|
69
72
|
private emitCoalescedMessage;
|
|
70
73
|
private scheduleFlush;
|
|
74
|
+
private writeToLocalCache;
|
|
71
75
|
}
|
|
72
76
|
|
|
73
77
|
type StreamPair = {
|
package/dist/agent.d.ts
CHANGED
package/dist/agent.js
CHANGED
|
@@ -276,7 +276,7 @@ import { v7 as uuidv7 } from "uuid";
|
|
|
276
276
|
// package.json
|
|
277
277
|
var package_default = {
|
|
278
278
|
name: "@posthog/agent",
|
|
279
|
-
version: "2.1.
|
|
279
|
+
version: "2.1.59",
|
|
280
280
|
repository: "https://github.com/PostHog/twig",
|
|
281
281
|
description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
|
|
282
282
|
exports: {
|
|
@@ -341,7 +341,8 @@ var package_default = {
|
|
|
341
341
|
test: "vitest run",
|
|
342
342
|
"test:watch": "vitest",
|
|
343
343
|
typecheck: "pnpm exec tsc --noEmit",
|
|
344
|
-
prepublishOnly: "pnpm run build"
|
|
344
|
+
prepublishOnly: "pnpm run build",
|
|
345
|
+
clean: "rm -rf dist .turbo"
|
|
345
346
|
},
|
|
346
347
|
engines: {
|
|
347
348
|
node: ">=20.0.0"
|
|
@@ -690,8 +691,8 @@ var ToolContentBuilder = class {
|
|
|
690
691
|
this.items.push({ type: "content", content: image(data, mimeType, uri) });
|
|
691
692
|
return this;
|
|
692
693
|
}
|
|
693
|
-
diff(
|
|
694
|
-
this.items.push({ type: "diff", path:
|
|
694
|
+
diff(path5, oldText, newText) {
|
|
695
|
+
this.items.push({ type: "diff", path: path5, oldText, newText });
|
|
695
696
|
return this;
|
|
696
697
|
}
|
|
697
698
|
build() {
|
|
@@ -881,13 +882,13 @@ function toolInfoFromToolUse(toolUse, cachedFileContent, logger = new Logger({ d
|
|
|
881
882
|
locations: []
|
|
882
883
|
};
|
|
883
884
|
case "Edit": {
|
|
884
|
-
const
|
|
885
|
+
const path5 = input?.file_path ? String(input.file_path) : void 0;
|
|
885
886
|
let oldText = input?.old_string ? String(input.old_string) : null;
|
|
886
887
|
let newText = input?.new_string ? String(input.new_string) : "";
|
|
887
888
|
let affectedLines = [];
|
|
888
|
-
if (
|
|
889
|
+
if (path5 && oldText) {
|
|
889
890
|
try {
|
|
890
|
-
const oldContent = cachedFileContent[
|
|
891
|
+
const oldContent = cachedFileContent[path5] || "";
|
|
891
892
|
const newContent = replaceAndCalculateLocation(oldContent, [
|
|
892
893
|
{
|
|
893
894
|
oldText,
|
|
@@ -903,17 +904,17 @@ function toolInfoFromToolUse(toolUse, cachedFileContent, logger = new Logger({ d
|
|
|
903
904
|
}
|
|
904
905
|
}
|
|
905
906
|
return {
|
|
906
|
-
title:
|
|
907
|
+
title: path5 ? `Edit \`${path5}\`` : "Edit",
|
|
907
908
|
kind: "edit",
|
|
908
|
-
content: input &&
|
|
909
|
+
content: input && path5 ? [
|
|
909
910
|
{
|
|
910
911
|
type: "diff",
|
|
911
|
-
path:
|
|
912
|
+
path: path5,
|
|
912
913
|
oldText,
|
|
913
914
|
newText
|
|
914
915
|
}
|
|
915
916
|
] : [],
|
|
916
|
-
locations:
|
|
917
|
+
locations: path5 ? affectedLines.length > 0 ? affectedLines.map((line) => ({ line, path: path5 })) : [{ path: path5 }] : []
|
|
917
918
|
};
|
|
918
919
|
}
|
|
919
920
|
case "Write": {
|
|
@@ -3407,6 +3408,8 @@ var PostHogAPIClient = class {
|
|
|
3407
3408
|
};
|
|
3408
3409
|
|
|
3409
3410
|
// src/session-log-writer.ts
|
|
3411
|
+
import fs3 from "fs";
|
|
3412
|
+
import path4 from "path";
|
|
3410
3413
|
var SessionLogWriter = class _SessionLogWriter {
|
|
3411
3414
|
static FLUSH_DEBOUNCE_MS = 500;
|
|
3412
3415
|
static FLUSH_MAX_INTERVAL_MS = 5e3;
|
|
@@ -3420,8 +3423,10 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
3420
3423
|
sessions = /* @__PURE__ */ new Map();
|
|
3421
3424
|
messageCounts = /* @__PURE__ */ new Map();
|
|
3422
3425
|
logger;
|
|
3426
|
+
localCachePath;
|
|
3423
3427
|
constructor(options = {}) {
|
|
3424
3428
|
this.posthogAPI = options.posthogAPI;
|
|
3429
|
+
this.localCachePath = options.localCachePath;
|
|
3425
3430
|
this.logger = options.logger ?? new Logger({ debug: false, prefix: "[SessionLogWriter]" });
|
|
3426
3431
|
}
|
|
3427
3432
|
async flushAll() {
|
|
@@ -3451,6 +3456,21 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
3451
3456
|
});
|
|
3452
3457
|
this.sessions.set(sessionId, { context });
|
|
3453
3458
|
this.lastFlushAttemptTime.set(sessionId, Date.now());
|
|
3459
|
+
if (this.localCachePath) {
|
|
3460
|
+
const sessionDir = path4.join(
|
|
3461
|
+
this.localCachePath,
|
|
3462
|
+
"sessions",
|
|
3463
|
+
context.runId
|
|
3464
|
+
);
|
|
3465
|
+
try {
|
|
3466
|
+
fs3.mkdirSync(sessionDir, { recursive: true });
|
|
3467
|
+
} catch (error) {
|
|
3468
|
+
this.logger.warn("Failed to create local cache directory", {
|
|
3469
|
+
sessionDir,
|
|
3470
|
+
error
|
|
3471
|
+
});
|
|
3472
|
+
}
|
|
3473
|
+
}
|
|
3454
3474
|
}
|
|
3455
3475
|
isRegistered(sessionId) {
|
|
3456
3476
|
return this.sessions.has(sessionId);
|
|
@@ -3488,6 +3508,7 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
3488
3508
|
timestamp,
|
|
3489
3509
|
notification: message
|
|
3490
3510
|
};
|
|
3511
|
+
this.writeToLocalCache(sessionId, entry);
|
|
3491
3512
|
if (this.posthogAPI) {
|
|
3492
3513
|
const pending = this.pendingEntries.get(sessionId) ?? [];
|
|
3493
3514
|
pending.push(entry);
|
|
@@ -3588,6 +3609,7 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
3588
3609
|
}
|
|
3589
3610
|
}
|
|
3590
3611
|
};
|
|
3612
|
+
this.writeToLocalCache(sessionId, entry);
|
|
3591
3613
|
if (this.posthogAPI) {
|
|
3592
3614
|
const pending = this.pendingEntries.get(sessionId) ?? [];
|
|
3593
3615
|
pending.push(entry);
|
|
@@ -3615,6 +3637,23 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
3615
3637
|
const timeout = setTimeout(() => this.flush(sessionId), delay);
|
|
3616
3638
|
this.flushTimeouts.set(sessionId, timeout);
|
|
3617
3639
|
}
|
|
3640
|
+
writeToLocalCache(sessionId, entry) {
|
|
3641
|
+
if (!this.localCachePath) return;
|
|
3642
|
+
const session = this.sessions.get(sessionId);
|
|
3643
|
+
if (!session) return;
|
|
3644
|
+
const logPath = path4.join(
|
|
3645
|
+
this.localCachePath,
|
|
3646
|
+
"sessions",
|
|
3647
|
+
session.context.runId,
|
|
3648
|
+
"logs.ndjson"
|
|
3649
|
+
);
|
|
3650
|
+
try {
|
|
3651
|
+
fs3.appendFileSync(logPath, `${JSON.stringify(entry)}
|
|
3652
|
+
`);
|
|
3653
|
+
} catch (error) {
|
|
3654
|
+
this.logger.warn("Failed to write to local cache", { logPath, error });
|
|
3655
|
+
}
|
|
3656
|
+
}
|
|
3618
3657
|
};
|
|
3619
3658
|
|
|
3620
3659
|
// src/agent.ts
|
|
@@ -3638,7 +3677,8 @@ var Agent = class {
|
|
|
3638
3677
|
if (config.posthog && !config.skipLogPersistence) {
|
|
3639
3678
|
this.sessionLogWriter = new SessionLogWriter({
|
|
3640
3679
|
posthogAPI: this.posthogAPI,
|
|
3641
|
-
logger: this.logger.child("SessionLogWriter")
|
|
3680
|
+
logger: this.logger.child("SessionLogWriter"),
|
|
3681
|
+
localCachePath: config.localCachePath
|
|
3642
3682
|
});
|
|
3643
3683
|
}
|
|
3644
3684
|
}
|