@posthog/agent 2.3.104 → 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 +39 -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 +36 -19
- package/dist/server/agent-server.js.map +1 -1
- package/dist/server/bin.cjs +36 -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/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 [];
|
|
@@ -5036,13 +5053,13 @@ var Agent = class {
|
|
|
5036
5053
|
}
|
|
5037
5054
|
}
|
|
5038
5055
|
}
|
|
5039
|
-
_configureLlmGateway(overrideUrl) {
|
|
5056
|
+
async _configureLlmGateway(overrideUrl) {
|
|
5040
5057
|
if (!this.posthogAPI) {
|
|
5041
5058
|
return null;
|
|
5042
5059
|
}
|
|
5043
5060
|
try {
|
|
5044
5061
|
const gatewayUrl = overrideUrl ?? this.posthogAPI.getLlmGatewayUrl();
|
|
5045
|
-
const apiKey = this.posthogAPI.getApiKey();
|
|
5062
|
+
const apiKey = await this.posthogAPI.getApiKey();
|
|
5046
5063
|
process.env.OPENAI_BASE_URL = `${gatewayUrl}/v1`;
|
|
5047
5064
|
process.env.OPENAI_API_KEY = apiKey;
|
|
5048
5065
|
process.env.ANTHROPIC_BASE_URL = gatewayUrl;
|
|
@@ -5054,7 +5071,7 @@ var Agent = class {
|
|
|
5054
5071
|
}
|
|
5055
5072
|
}
|
|
5056
5073
|
async run(taskId, taskRunId, options = {}) {
|
|
5057
|
-
const gatewayConfig = this._configureLlmGateway(options.gatewayUrl);
|
|
5074
|
+
const gatewayConfig = await this._configureLlmGateway(options.gatewayUrl);
|
|
5058
5075
|
this.logger.info("Configured LLM gateway", {
|
|
5059
5076
|
adapter: options.adapter
|
|
5060
5077
|
});
|