@posthog/agent 2.1.2 → 2.1.8
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-kRbaLUfe.d.ts → agent-LrKyX9KN.d.ts} +1 -4
- package/dist/agent.d.ts +1 -1
- package/dist/agent.js +3 -86
- package/dist/agent.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +58 -86
- package/dist/index.js.map +1 -1
- package/dist/server/agent-server.js +4 -82
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +4 -79
- package/dist/server/bin.cjs.map +1 -1
- package/dist/types.d.ts +2 -0
- package/package.json +1 -1
- package/src/agent.ts +1 -12
- package/src/server/agent-server.ts +2 -6
- package/src/session-log-writer.test.ts +72 -101
- package/src/session-log-writer.ts +3 -34
- package/src/types.ts +2 -0
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { A as AcpConnection, a as AcpConnectionConfig, b as Agent, c as AgentAdapter, C as CodexProcessOptions, I as InProcessAcpConnection, O as OtelLogConfig, d as OtelLogWriter, S as SessionContext, e as SessionLogWriter, f as SessionLogWriterOptions, g as createAcpConnection } from './agent-
|
|
1
|
+
export { A as AcpConnection, a as AcpConnectionConfig, b as Agent, c as AgentAdapter, C as CodexProcessOptions, I as InProcessAcpConnection, O as OtelLogConfig, d as OtelLogWriter, S as SessionContext, e as SessionLogWriter, f as SessionLogWriterOptions, g as createAcpConnection } from './agent-LrKyX9KN.js';
|
|
2
2
|
import { McpServerConfig } from '@anthropic-ai/claude-agent-sdk';
|
|
3
3
|
import { L as Logger } from './logger-DDBiMOOD.js';
|
|
4
4
|
export { a as LoggerConfig } from './logger-DDBiMOOD.js';
|
package/dist/index.js
CHANGED
|
@@ -1174,7 +1174,7 @@ import { v7 as uuidv7 } from "uuid";
|
|
|
1174
1174
|
// package.json
|
|
1175
1175
|
var package_default = {
|
|
1176
1176
|
name: "@posthog/agent",
|
|
1177
|
-
version: "2.1.
|
|
1177
|
+
version: "2.1.8",
|
|
1178
1178
|
repository: "https://github.com/PostHog/twig",
|
|
1179
1179
|
description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
|
|
1180
1180
|
exports: {
|
|
@@ -4277,72 +4277,15 @@ var PostHogAPIClient = class {
|
|
|
4277
4277
|
}
|
|
4278
4278
|
};
|
|
4279
4279
|
|
|
4280
|
-
// src/otel-log-writer.ts
|
|
4281
|
-
import { SeverityNumber } from "@opentelemetry/api-logs";
|
|
4282
|
-
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
4283
|
-
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
4284
|
-
import {
|
|
4285
|
-
BatchLogRecordProcessor,
|
|
4286
|
-
LoggerProvider
|
|
4287
|
-
} from "@opentelemetry/sdk-logs";
|
|
4288
|
-
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
|
|
4289
|
-
var OtelLogWriter = class {
|
|
4290
|
-
loggerProvider;
|
|
4291
|
-
logger;
|
|
4292
|
-
constructor(config, sessionContext, _debugLogger) {
|
|
4293
|
-
const logsPath = config.logsPath ?? "/i/v1/agent-logs";
|
|
4294
|
-
const exporter = new OTLPLogExporter({
|
|
4295
|
-
url: `${config.posthogHost}${logsPath}`,
|
|
4296
|
-
headers: { Authorization: `Bearer ${config.apiKey}` }
|
|
4297
|
-
});
|
|
4298
|
-
const processor = new BatchLogRecordProcessor(exporter, {
|
|
4299
|
-
scheduledDelayMillis: config.flushIntervalMs ?? 500
|
|
4300
|
-
});
|
|
4301
|
-
this.loggerProvider = new LoggerProvider({
|
|
4302
|
-
resource: resourceFromAttributes({
|
|
4303
|
-
[ATTR_SERVICE_NAME]: "twig-agent",
|
|
4304
|
-
run_id: sessionContext.runId,
|
|
4305
|
-
task_id: sessionContext.taskId,
|
|
4306
|
-
device_type: sessionContext.deviceType ?? "local"
|
|
4307
|
-
}),
|
|
4308
|
-
processors: [processor]
|
|
4309
|
-
});
|
|
4310
|
-
this.logger = this.loggerProvider.getLogger("agent-session");
|
|
4311
|
-
}
|
|
4312
|
-
/**
|
|
4313
|
-
* Emit an agent event to PostHog Logs via OTEL.
|
|
4314
|
-
*/
|
|
4315
|
-
emit(entry) {
|
|
4316
|
-
const { notification } = entry;
|
|
4317
|
-
const eventType = notification.notification.method;
|
|
4318
|
-
this.logger.emit({
|
|
4319
|
-
severityNumber: SeverityNumber.INFO,
|
|
4320
|
-
severityText: "INFO",
|
|
4321
|
-
body: JSON.stringify(notification),
|
|
4322
|
-
attributes: {
|
|
4323
|
-
event_type: eventType
|
|
4324
|
-
}
|
|
4325
|
-
});
|
|
4326
|
-
}
|
|
4327
|
-
async flush() {
|
|
4328
|
-
await this.loggerProvider.forceFlush();
|
|
4329
|
-
}
|
|
4330
|
-
async shutdown() {
|
|
4331
|
-
await this.loggerProvider.shutdown();
|
|
4332
|
-
}
|
|
4333
|
-
};
|
|
4334
|
-
|
|
4335
4280
|
// src/session-log-writer.ts
|
|
4336
4281
|
var SessionLogWriter = class {
|
|
4337
4282
|
posthogAPI;
|
|
4338
|
-
otelConfig;
|
|
4339
4283
|
pendingEntries = /* @__PURE__ */ new Map();
|
|
4340
4284
|
flushTimeouts = /* @__PURE__ */ new Map();
|
|
4341
4285
|
sessions = /* @__PURE__ */ new Map();
|
|
4342
4286
|
logger;
|
|
4343
4287
|
constructor(options = {}) {
|
|
4344
4288
|
this.posthogAPI = options.posthogAPI;
|
|
4345
|
-
this.otelConfig = options.otelConfig;
|
|
4346
4289
|
this.logger = options.logger ?? new Logger({ debug: false, prefix: "[SessionLogWriter]" });
|
|
4347
4290
|
}
|
|
4348
4291
|
async flushAll() {
|
|
@@ -4356,15 +4299,7 @@ var SessionLogWriter = class {
|
|
|
4356
4299
|
if (this.sessions.has(sessionId)) {
|
|
4357
4300
|
return;
|
|
4358
4301
|
}
|
|
4359
|
-
|
|
4360
|
-
if (this.otelConfig) {
|
|
4361
|
-
otelWriter = new OtelLogWriter(
|
|
4362
|
-
this.otelConfig,
|
|
4363
|
-
context,
|
|
4364
|
-
this.logger.child(`OtelWriter:${sessionId}`)
|
|
4365
|
-
);
|
|
4366
|
-
}
|
|
4367
|
-
this.sessions.set(sessionId, { context, otelWriter });
|
|
4302
|
+
this.sessions.set(sessionId, { context });
|
|
4368
4303
|
}
|
|
4369
4304
|
isRegistered(sessionId) {
|
|
4370
4305
|
return this.sessions.has(sessionId);
|
|
@@ -4394,9 +4329,6 @@ var SessionLogWriter = class {
|
|
|
4394
4329
|
timestamp,
|
|
4395
4330
|
notification: message
|
|
4396
4331
|
};
|
|
4397
|
-
if (session.otelWriter) {
|
|
4398
|
-
session.otelWriter.emit({ notification: entry });
|
|
4399
|
-
}
|
|
4400
4332
|
if (this.posthogAPI) {
|
|
4401
4333
|
const pending = this.pendingEntries.get(sessionId) ?? [];
|
|
4402
4334
|
pending.push(entry);
|
|
@@ -4414,9 +4346,6 @@ var SessionLogWriter = class {
|
|
|
4414
4346
|
const session = this.sessions.get(sessionId);
|
|
4415
4347
|
if (!session) return;
|
|
4416
4348
|
this.emitCoalescedMessage(sessionId, session);
|
|
4417
|
-
if (session.otelWriter) {
|
|
4418
|
-
await session.otelWriter.flush();
|
|
4419
|
-
}
|
|
4420
4349
|
const pending = this.pendingEntries.get(sessionId);
|
|
4421
4350
|
if (!this.posthogAPI || !pending?.length) return;
|
|
4422
4351
|
this.pendingEntries.delete(sessionId);
|
|
@@ -4468,9 +4397,6 @@ var SessionLogWriter = class {
|
|
|
4468
4397
|
}
|
|
4469
4398
|
}
|
|
4470
4399
|
};
|
|
4471
|
-
if (session.otelWriter) {
|
|
4472
|
-
session.otelWriter.emit({ notification: entry });
|
|
4473
|
-
}
|
|
4474
4400
|
if (this.posthogAPI) {
|
|
4475
4401
|
const pending = this.pendingEntries.get(sessionId) ?? [];
|
|
4476
4402
|
pending.push(entry);
|
|
@@ -4504,16 +4430,7 @@ var Agent = class {
|
|
|
4504
4430
|
if (config.posthog) {
|
|
4505
4431
|
this.posthogAPI = new PostHogAPIClient(config.posthog);
|
|
4506
4432
|
}
|
|
4507
|
-
if (config.
|
|
4508
|
-
this.sessionLogWriter = new SessionLogWriter({
|
|
4509
|
-
otelConfig: {
|
|
4510
|
-
posthogHost: config.otelTransport.host,
|
|
4511
|
-
apiKey: config.otelTransport.apiKey,
|
|
4512
|
-
logsPath: config.otelTransport.logsPath
|
|
4513
|
-
},
|
|
4514
|
-
logger: this.logger.child("SessionLogWriter")
|
|
4515
|
-
});
|
|
4516
|
-
} else if (config.posthog) {
|
|
4433
|
+
if (config.posthog && !config.skipLogPersistence) {
|
|
4517
4434
|
this.sessionLogWriter = new SessionLogWriter({
|
|
4518
4435
|
posthogAPI: this.posthogAPI,
|
|
4519
4436
|
logger: this.logger.child("SessionLogWriter")
|
|
@@ -4615,6 +4532,61 @@ var Agent = class {
|
|
|
4615
4532
|
}
|
|
4616
4533
|
};
|
|
4617
4534
|
|
|
4535
|
+
// src/otel-log-writer.ts
|
|
4536
|
+
import { SeverityNumber } from "@opentelemetry/api-logs";
|
|
4537
|
+
import { OTLPLogExporter } from "@opentelemetry/exporter-logs-otlp-http";
|
|
4538
|
+
import { resourceFromAttributes } from "@opentelemetry/resources";
|
|
4539
|
+
import {
|
|
4540
|
+
BatchLogRecordProcessor,
|
|
4541
|
+
LoggerProvider
|
|
4542
|
+
} from "@opentelemetry/sdk-logs";
|
|
4543
|
+
import { ATTR_SERVICE_NAME } from "@opentelemetry/semantic-conventions";
|
|
4544
|
+
var OtelLogWriter = class {
|
|
4545
|
+
loggerProvider;
|
|
4546
|
+
logger;
|
|
4547
|
+
constructor(config, sessionContext, _debugLogger) {
|
|
4548
|
+
const logsPath = config.logsPath ?? "/i/v1/agent-logs";
|
|
4549
|
+
const exporter = new OTLPLogExporter({
|
|
4550
|
+
url: `${config.posthogHost}${logsPath}`,
|
|
4551
|
+
headers: { Authorization: `Bearer ${config.apiKey}` }
|
|
4552
|
+
});
|
|
4553
|
+
const processor = new BatchLogRecordProcessor(exporter, {
|
|
4554
|
+
scheduledDelayMillis: config.flushIntervalMs ?? 500
|
|
4555
|
+
});
|
|
4556
|
+
this.loggerProvider = new LoggerProvider({
|
|
4557
|
+
resource: resourceFromAttributes({
|
|
4558
|
+
[ATTR_SERVICE_NAME]: "twig-agent",
|
|
4559
|
+
run_id: sessionContext.runId,
|
|
4560
|
+
task_id: sessionContext.taskId,
|
|
4561
|
+
device_type: sessionContext.deviceType ?? "local"
|
|
4562
|
+
}),
|
|
4563
|
+
processors: [processor]
|
|
4564
|
+
});
|
|
4565
|
+
this.logger = this.loggerProvider.getLogger("agent-session");
|
|
4566
|
+
}
|
|
4567
|
+
/**
|
|
4568
|
+
* Emit an agent event to PostHog Logs via OTEL.
|
|
4569
|
+
*/
|
|
4570
|
+
emit(entry) {
|
|
4571
|
+
const { notification } = entry;
|
|
4572
|
+
const eventType = notification.notification.method;
|
|
4573
|
+
this.logger.emit({
|
|
4574
|
+
severityNumber: SeverityNumber.INFO,
|
|
4575
|
+
severityText: "INFO",
|
|
4576
|
+
body: JSON.stringify(notification),
|
|
4577
|
+
attributes: {
|
|
4578
|
+
event_type: eventType
|
|
4579
|
+
}
|
|
4580
|
+
});
|
|
4581
|
+
}
|
|
4582
|
+
async flush() {
|
|
4583
|
+
await this.loggerProvider.forceFlush();
|
|
4584
|
+
}
|
|
4585
|
+
async shutdown() {
|
|
4586
|
+
await this.loggerProvider.shutdown();
|
|
4587
|
+
}
|
|
4588
|
+
};
|
|
4589
|
+
|
|
4618
4590
|
// ../shared/dist/index.js
|
|
4619
4591
|
var consoleLogger = {
|
|
4620
4592
|
info: (_message, _data) => {
|