@posthog/agent 2.3.93 → 2.3.110
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 +52 -22
- package/dist/agent.js.map +1 -1
- package/dist/posthog-api.d.ts +6 -2
- package/dist/posthog-api.js +36 -19
- package/dist/posthog-api.js.map +1 -1
- package/dist/server/agent-server.js +49 -19
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +49 -19
- package/dist/server/bin.cjs.map +1 -1
- package/dist/types.d.ts +2 -1
- package/package.json +1 -1
- package/src/agent.ts +4 -4
- package/src/posthog-api.test.ts +48 -0
- package/src/posthog-api.ts +54 -20
- package/src/session-log-writer.test.ts +87 -0
- package/src/session-log-writer.ts +14 -0
- package/src/types.ts +2 -1
package/dist/agent.js
CHANGED
|
@@ -371,7 +371,7 @@ import { v7 as uuidv7 } from "uuid";
|
|
|
371
371
|
// package.json
|
|
372
372
|
var package_default = {
|
|
373
373
|
name: "@posthog/agent",
|
|
374
|
-
version: "2.3.
|
|
374
|
+
version: "2.3.110",
|
|
375
375
|
repository: "https://github.com/PostHog/code",
|
|
376
376
|
description: "TypeScript agent framework wrapping Claude Agent SDK with Git-based task execution for PostHog",
|
|
377
377
|
exports: {
|
|
@@ -4530,22 +4530,41 @@ var PostHogAPIClient = class {
|
|
|
4530
4530
|
const host = this.config.apiUrl.endsWith("/") ? this.config.apiUrl.slice(0, -1) : this.config.apiUrl;
|
|
4531
4531
|
return host;
|
|
4532
4532
|
}
|
|
4533
|
-
|
|
4534
|
-
return
|
|
4535
|
-
Authorization: `Bearer ${this.config.getApiKey()}`,
|
|
4536
|
-
"Content-Type": "application/json",
|
|
4537
|
-
"User-Agent": this.config.userAgent ?? DEFAULT_USER_AGENT
|
|
4538
|
-
};
|
|
4533
|
+
isAuthFailure(status) {
|
|
4534
|
+
return status === 401 || status === 403;
|
|
4539
4535
|
}
|
|
4540
|
-
async
|
|
4536
|
+
async resolveApiKey(forceRefresh = false) {
|
|
4537
|
+
if (forceRefresh && this.config.refreshApiKey) {
|
|
4538
|
+
return this.config.refreshApiKey();
|
|
4539
|
+
}
|
|
4540
|
+
return this.config.getApiKey();
|
|
4541
|
+
}
|
|
4542
|
+
async buildHeaders(options, forceRefresh = false) {
|
|
4543
|
+
const headers = new Headers(options.headers);
|
|
4544
|
+
headers.set(
|
|
4545
|
+
"Authorization",
|
|
4546
|
+
`Bearer ${await this.resolveApiKey(forceRefresh)}`
|
|
4547
|
+
);
|
|
4548
|
+
headers.set("Content-Type", "application/json");
|
|
4549
|
+
headers.set("User-Agent", this.config.userAgent ?? DEFAULT_USER_AGENT);
|
|
4550
|
+
return headers;
|
|
4551
|
+
}
|
|
4552
|
+
async performRequest(endpoint, options, forceRefresh = false) {
|
|
4541
4553
|
const url = `${this.baseUrl}${endpoint}`;
|
|
4542
|
-
|
|
4554
|
+
return fetch(url, {
|
|
4543
4555
|
...options,
|
|
4544
|
-
headers:
|
|
4545
|
-
...this.headers,
|
|
4546
|
-
...options.headers
|
|
4547
|
-
}
|
|
4556
|
+
headers: await this.buildHeaders(options, forceRefresh)
|
|
4548
4557
|
});
|
|
4558
|
+
}
|
|
4559
|
+
async performRequestWithRetry(endpoint, options = {}) {
|
|
4560
|
+
let response = await this.performRequest(endpoint, options);
|
|
4561
|
+
if (!response.ok && this.isAuthFailure(response.status)) {
|
|
4562
|
+
response = await this.performRequest(endpoint, options, true);
|
|
4563
|
+
}
|
|
4564
|
+
return response;
|
|
4565
|
+
}
|
|
4566
|
+
async apiRequest(endpoint, options = {}) {
|
|
4567
|
+
const response = await this.performRequestWithRetry(endpoint, options);
|
|
4549
4568
|
if (!response.ok) {
|
|
4550
4569
|
let errorMessage;
|
|
4551
4570
|
try {
|
|
@@ -4561,8 +4580,8 @@ var PostHogAPIClient = class {
|
|
|
4561
4580
|
getTeamId() {
|
|
4562
4581
|
return this.config.projectId;
|
|
4563
4582
|
}
|
|
4564
|
-
getApiKey() {
|
|
4565
|
-
return this.
|
|
4583
|
+
async getApiKey(forceRefresh = false) {
|
|
4584
|
+
return this.resolveApiKey(forceRefresh);
|
|
4566
4585
|
}
|
|
4567
4586
|
getLlmGatewayUrl() {
|
|
4568
4587
|
return getLlmGatewayUrl(this.baseUrl);
|
|
@@ -4662,11 +4681,9 @@ var PostHogAPIClient = class {
|
|
|
4662
4681
|
*/
|
|
4663
4682
|
async fetchTaskRunLogs(taskRun) {
|
|
4664
4683
|
const teamId = this.getTeamId();
|
|
4684
|
+
const endpoint = `/api/projects/${teamId}/tasks/${taskRun.task}/runs/${taskRun.id}/logs`;
|
|
4665
4685
|
try {
|
|
4666
|
-
const response = await
|
|
4667
|
-
`${this.baseUrl}/api/projects/${teamId}/tasks/${taskRun.task}/runs/${taskRun.id}/logs`,
|
|
4668
|
-
{ headers: this.headers }
|
|
4669
|
-
);
|
|
4686
|
+
const response = await this.performRequestWithRetry(endpoint);
|
|
4670
4687
|
if (!response.ok) {
|
|
4671
4688
|
if (response.status === 404) {
|
|
4672
4689
|
return [];
|
|
@@ -4704,6 +4721,7 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
4704
4721
|
lastFlushAttemptTime = /* @__PURE__ */ new Map();
|
|
4705
4722
|
retryCounts = /* @__PURE__ */ new Map();
|
|
4706
4723
|
sessions = /* @__PURE__ */ new Map();
|
|
4724
|
+
flushQueues = /* @__PURE__ */ new Map();
|
|
4707
4725
|
logger;
|
|
4708
4726
|
localCachePath;
|
|
4709
4727
|
constructor(options = {}) {
|
|
@@ -4797,6 +4815,18 @@ var SessionLogWriter = class _SessionLogWriter {
|
|
|
4797
4815
|
}
|
|
4798
4816
|
}
|
|
4799
4817
|
async flush(sessionId) {
|
|
4818
|
+
const prev = this.flushQueues.get(sessionId) ?? Promise.resolve();
|
|
4819
|
+
const next = prev.catch(() => {
|
|
4820
|
+
}).then(() => this._doFlush(sessionId));
|
|
4821
|
+
this.flushQueues.set(sessionId, next);
|
|
4822
|
+
next.finally(() => {
|
|
4823
|
+
if (this.flushQueues.get(sessionId) === next) {
|
|
4824
|
+
this.flushQueues.delete(sessionId);
|
|
4825
|
+
}
|
|
4826
|
+
});
|
|
4827
|
+
return next;
|
|
4828
|
+
}
|
|
4829
|
+
async _doFlush(sessionId) {
|
|
4800
4830
|
const session = this.sessions.get(sessionId);
|
|
4801
4831
|
if (!session) {
|
|
4802
4832
|
this.logger.warn("flush: no session found", { sessionId });
|
|
@@ -5023,13 +5053,13 @@ var Agent = class {
|
|
|
5023
5053
|
}
|
|
5024
5054
|
}
|
|
5025
5055
|
}
|
|
5026
|
-
_configureLlmGateway(overrideUrl) {
|
|
5056
|
+
async _configureLlmGateway(overrideUrl) {
|
|
5027
5057
|
if (!this.posthogAPI) {
|
|
5028
5058
|
return null;
|
|
5029
5059
|
}
|
|
5030
5060
|
try {
|
|
5031
5061
|
const gatewayUrl = overrideUrl ?? this.posthogAPI.getLlmGatewayUrl();
|
|
5032
|
-
const apiKey = this.posthogAPI.getApiKey();
|
|
5062
|
+
const apiKey = await this.posthogAPI.getApiKey();
|
|
5033
5063
|
process.env.OPENAI_BASE_URL = `${gatewayUrl}/v1`;
|
|
5034
5064
|
process.env.OPENAI_API_KEY = apiKey;
|
|
5035
5065
|
process.env.ANTHROPIC_BASE_URL = gatewayUrl;
|
|
@@ -5041,7 +5071,7 @@ var Agent = class {
|
|
|
5041
5071
|
}
|
|
5042
5072
|
}
|
|
5043
5073
|
async run(taskId, taskRunId, options = {}) {
|
|
5044
|
-
const gatewayConfig = this._configureLlmGateway(options.gatewayUrl);
|
|
5074
|
+
const gatewayConfig = await this._configureLlmGateway(options.gatewayUrl);
|
|
5045
5075
|
this.logger.info("Configured LLM gateway", {
|
|
5046
5076
|
adapter: options.adapter
|
|
5047
5077
|
});
|