@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.
@@ -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
@@ -1,4 +1,4 @@
1
- export { b as Agent } from './agent-DcBmoTR4.js';
1
+ export { b as Agent } from './agent-9gv5HohC.js';
2
2
  import './types.js';
3
3
  import '@agentclientprotocol/sdk';
4
4
  import './logger-DDBiMOOD.js';
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.48",
279
+ version: "2.1.53",
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: {
@@ -690,8 +690,8 @@ var ToolContentBuilder = class {
690
690
  this.items.push({ type: "content", content: image(data, mimeType, uri) });
691
691
  return this;
692
692
  }
693
- diff(path4, oldText, newText) {
694
- this.items.push({ type: "diff", path: path4, oldText, newText });
693
+ diff(path5, oldText, newText) {
694
+ this.items.push({ type: "diff", path: path5, oldText, newText });
695
695
  return this;
696
696
  }
697
697
  build() {
@@ -881,13 +881,13 @@ function toolInfoFromToolUse(toolUse, cachedFileContent, logger = new Logger({ d
881
881
  locations: []
882
882
  };
883
883
  case "Edit": {
884
- const path4 = input?.file_path ? String(input.file_path) : void 0;
884
+ const path5 = input?.file_path ? String(input.file_path) : void 0;
885
885
  let oldText = input?.old_string ? String(input.old_string) : null;
886
886
  let newText = input?.new_string ? String(input.new_string) : "";
887
887
  let affectedLines = [];
888
- if (path4 && oldText) {
888
+ if (path5 && oldText) {
889
889
  try {
890
- const oldContent = cachedFileContent[path4] || "";
890
+ const oldContent = cachedFileContent[path5] || "";
891
891
  const newContent = replaceAndCalculateLocation(oldContent, [
892
892
  {
893
893
  oldText,
@@ -903,17 +903,17 @@ function toolInfoFromToolUse(toolUse, cachedFileContent, logger = new Logger({ d
903
903
  }
904
904
  }
905
905
  return {
906
- title: path4 ? `Edit \`${path4}\`` : "Edit",
906
+ title: path5 ? `Edit \`${path5}\`` : "Edit",
907
907
  kind: "edit",
908
- content: input && path4 ? [
908
+ content: input && path5 ? [
909
909
  {
910
910
  type: "diff",
911
- path: path4,
911
+ path: path5,
912
912
  oldText,
913
913
  newText
914
914
  }
915
915
  ] : [],
916
- locations: path4 ? affectedLines.length > 0 ? affectedLines.map((line) => ({ line, path: path4 })) : [{ path: path4 }] : []
916
+ locations: path5 ? affectedLines.length > 0 ? affectedLines.map((line) => ({ line, path: path5 })) : [{ path: path5 }] : []
917
917
  };
918
918
  }
919
919
  case "Write": {
@@ -3407,6 +3407,8 @@ var PostHogAPIClient = class {
3407
3407
  };
3408
3408
 
3409
3409
  // src/session-log-writer.ts
3410
+ import fs3 from "fs";
3411
+ import path4 from "path";
3410
3412
  var SessionLogWriter = class _SessionLogWriter {
3411
3413
  static FLUSH_DEBOUNCE_MS = 500;
3412
3414
  static FLUSH_MAX_INTERVAL_MS = 5e3;
@@ -3420,8 +3422,10 @@ var SessionLogWriter = class _SessionLogWriter {
3420
3422
  sessions = /* @__PURE__ */ new Map();
3421
3423
  messageCounts = /* @__PURE__ */ new Map();
3422
3424
  logger;
3425
+ localCachePath;
3423
3426
  constructor(options = {}) {
3424
3427
  this.posthogAPI = options.posthogAPI;
3428
+ this.localCachePath = options.localCachePath;
3425
3429
  this.logger = options.logger ?? new Logger({ debug: false, prefix: "[SessionLogWriter]" });
3426
3430
  }
3427
3431
  async flushAll() {
@@ -3451,6 +3455,21 @@ var SessionLogWriter = class _SessionLogWriter {
3451
3455
  });
3452
3456
  this.sessions.set(sessionId, { context });
3453
3457
  this.lastFlushAttemptTime.set(sessionId, Date.now());
3458
+ if (this.localCachePath) {
3459
+ const sessionDir = path4.join(
3460
+ this.localCachePath,
3461
+ "sessions",
3462
+ context.runId
3463
+ );
3464
+ try {
3465
+ fs3.mkdirSync(sessionDir, { recursive: true });
3466
+ } catch (error) {
3467
+ this.logger.warn("Failed to create local cache directory", {
3468
+ sessionDir,
3469
+ error
3470
+ });
3471
+ }
3472
+ }
3454
3473
  }
3455
3474
  isRegistered(sessionId) {
3456
3475
  return this.sessions.has(sessionId);
@@ -3488,6 +3507,7 @@ var SessionLogWriter = class _SessionLogWriter {
3488
3507
  timestamp,
3489
3508
  notification: message
3490
3509
  };
3510
+ this.writeToLocalCache(sessionId, entry);
3491
3511
  if (this.posthogAPI) {
3492
3512
  const pending = this.pendingEntries.get(sessionId) ?? [];
3493
3513
  pending.push(entry);
@@ -3588,6 +3608,7 @@ var SessionLogWriter = class _SessionLogWriter {
3588
3608
  }
3589
3609
  }
3590
3610
  };
3611
+ this.writeToLocalCache(sessionId, entry);
3591
3612
  if (this.posthogAPI) {
3592
3613
  const pending = this.pendingEntries.get(sessionId) ?? [];
3593
3614
  pending.push(entry);
@@ -3615,6 +3636,23 @@ var SessionLogWriter = class _SessionLogWriter {
3615
3636
  const timeout = setTimeout(() => this.flush(sessionId), delay);
3616
3637
  this.flushTimeouts.set(sessionId, timeout);
3617
3638
  }
3639
+ writeToLocalCache(sessionId, entry) {
3640
+ if (!this.localCachePath) return;
3641
+ const session = this.sessions.get(sessionId);
3642
+ if (!session) return;
3643
+ const logPath = path4.join(
3644
+ this.localCachePath,
3645
+ "sessions",
3646
+ session.context.runId,
3647
+ "logs.ndjson"
3648
+ );
3649
+ try {
3650
+ fs3.appendFileSync(logPath, `${JSON.stringify(entry)}
3651
+ `);
3652
+ } catch (error) {
3653
+ this.logger.warn("Failed to write to local cache", { logPath, error });
3654
+ }
3655
+ }
3618
3656
  };
3619
3657
 
3620
3658
  // src/agent.ts
@@ -3638,7 +3676,8 @@ var Agent = class {
3638
3676
  if (config.posthog && !config.skipLogPersistence) {
3639
3677
  this.sessionLogWriter = new SessionLogWriter({
3640
3678
  posthogAPI: this.posthogAPI,
3641
- logger: this.logger.child("SessionLogWriter")
3679
+ logger: this.logger.child("SessionLogWriter"),
3680
+ localCachePath: config.localCachePath
3642
3681
  });
3643
3682
  }
3644
3683
  }