@posthog/agent 2.1.48 → 2.1.53

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/types.d.ts CHANGED
@@ -103,6 +103,8 @@ interface AgentConfig {
103
103
  otelTransport?: OtelTransportConfig;
104
104
  /** Skip session log persistence (e.g. for preview sessions with no real task) */
105
105
  skipLogPersistence?: boolean;
106
+ /** Local cache path for instant log loading (e.g., ~/.twig) */
107
+ localCachePath?: string;
106
108
  debug?: boolean;
107
109
  onLog?: OnLogCallback;
108
110
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@posthog/agent",
3
- "version": "2.1.48",
3
+ "version": "2.1.53",
4
4
  "repository": "https://github.com/PostHog/twig",
5
5
  "description": "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
6
6
  "exports": {
package/src/agent.ts CHANGED
@@ -36,6 +36,7 @@ export class Agent {
36
36
  this.sessionLogWriter = new SessionLogWriter({
37
37
  posthogAPI: this.posthogAPI,
38
38
  logger: this.logger.child("SessionLogWriter"),
39
+ localCachePath: config.localCachePath,
39
40
  });
40
41
  }
41
42
  }
@@ -1,3 +1,5 @@
1
+ import fs from "node:fs";
2
+ import path from "node:path";
1
3
  import type { SessionContext } from "./otel-log-writer.js";
2
4
  import type { PostHogAPIClient } from "./posthog-api.js";
3
5
  import type { StoredNotification } from "./types.js";
@@ -8,6 +10,8 @@ export interface SessionLogWriterOptions {
8
10
  posthogAPI?: PostHogAPIClient;
9
11
  /** Logger instance */
10
12
  logger?: Logger;
13
+ /** Local cache path for instant log loading (e.g., ~/.twig) */
14
+ localCachePath?: string;
11
15
  }
12
16
 
13
17
  interface ChunkBuffer {
@@ -34,9 +38,11 @@ export class SessionLogWriter {
34
38
  private sessions: Map<string, SessionState> = new Map();
35
39
  private messageCounts: Map<string, number> = new Map();
36
40
  private logger: Logger;
41
+ private localCachePath?: string;
37
42
 
38
43
  constructor(options: SessionLogWriterOptions = {}) {
39
44
  this.posthogAPI = options.posthogAPI;
45
+ this.localCachePath = options.localCachePath;
40
46
  this.logger =
41
47
  options.logger ??
42
48
  new Logger({ debug: false, prefix: "[SessionLogWriter]" });
@@ -71,7 +77,24 @@ export class SessionLogWriter {
71
77
  taskId: context.taskId,
72
78
  });
73
79
  this.sessions.set(sessionId, { context });
80
+
74
81
  this.lastFlushAttemptTime.set(sessionId, Date.now());
82
+
83
+ if (this.localCachePath) {
84
+ const sessionDir = path.join(
85
+ this.localCachePath,
86
+ "sessions",
87
+ context.runId,
88
+ );
89
+ try {
90
+ fs.mkdirSync(sessionDir, { recursive: true });
91
+ } catch (error) {
92
+ this.logger.warn("Failed to create local cache directory", {
93
+ sessionDir,
94
+ error,
95
+ });
96
+ }
97
+ }
75
98
  }
76
99
 
77
100
  isRegistered(sessionId: string): boolean {
@@ -120,6 +143,8 @@ export class SessionLogWriter {
120
143
  notification: message,
121
144
  };
122
145
 
146
+ this.writeToLocalCache(sessionId, entry);
147
+
123
148
  if (this.posthogAPI) {
124
149
  const pending = this.pendingEntries.get(sessionId) ?? [];
125
150
  pending.push(entry);
@@ -236,6 +261,8 @@ export class SessionLogWriter {
236
261
  },
237
262
  };
238
263
 
264
+ this.writeToLocalCache(sessionId, entry);
265
+
239
266
  if (this.posthogAPI) {
240
267
  const pending = this.pendingEntries.get(sessionId) ?? [];
241
268
  pending.push(entry);
@@ -269,4 +296,27 @@ export class SessionLogWriter {
269
296
  const timeout = setTimeout(() => this.flush(sessionId), delay);
270
297
  this.flushTimeouts.set(sessionId, timeout);
271
298
  }
299
+
300
+ private writeToLocalCache(
301
+ sessionId: string,
302
+ entry: StoredNotification,
303
+ ): void {
304
+ if (!this.localCachePath) return;
305
+
306
+ const session = this.sessions.get(sessionId);
307
+ if (!session) return;
308
+
309
+ const logPath = path.join(
310
+ this.localCachePath,
311
+ "sessions",
312
+ session.context.runId,
313
+ "logs.ndjson",
314
+ );
315
+
316
+ try {
317
+ fs.appendFileSync(logPath, `${JSON.stringify(entry)}\n`);
318
+ } catch (error) {
319
+ this.logger.warn("Failed to write to local cache", { logPath, error });
320
+ }
321
+ }
272
322
  }
package/src/types.ts CHANGED
@@ -143,6 +143,8 @@ export interface AgentConfig {
143
143
  otelTransport?: OtelTransportConfig;
144
144
  /** Skip session log persistence (e.g. for preview sessions with no real task) */
145
145
  skipLogPersistence?: boolean;
146
+ /** Local cache path for instant log loading (e.g., ~/.twig) */
147
+ localCachePath?: string;
146
148
  debug?: boolean;
147
149
  onLog?: OnLogCallback;
148
150
  }