-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathagent.ts
More file actions
208 lines (187 loc) · 6.39 KB
/
Copy pathagent.ts
File metadata and controls
208 lines (187 loc) · 6.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { buildPrOutput, mergePrUrls, readPrUrls } from "@posthog/shared";
import {
createAcpConnection,
type InProcessAcpConnection,
} from "./adapters/acp-connection";
import type { GatewayEnv } from "./adapters/claude/session/options";
import {
DEFAULT_CODEX_MODEL,
DEFAULT_GATEWAY_MODEL,
fetchModelsList,
isBlockedModelId,
type ModelInfo,
} from "./gateway-models";
import { PostHogAPIClient, type TaskRunUpdate } from "./posthog-api";
import { SessionLogWriter } from "./session-log-writer";
import type { AgentConfig, TaskExecutionOptions } from "./types";
import { Logger } from "./utils/logger";
export class Agent {
private posthogAPI?: PostHogAPIClient;
private logger: Logger;
private acpConnection?: InProcessAcpConnection;
private taskRunId?: string;
private sessionLogWriter?: SessionLogWriter;
private posthogApiConfig?: AgentConfig["posthog"];
private enricherEnabled: boolean;
constructor(config: AgentConfig) {
this.logger = new Logger({
debug: config.debug || false,
prefix: "[PostHog Agent]",
onLog: config.onLog,
});
if (config.posthog) {
this.posthogAPI = new PostHogAPIClient(config.posthog);
this.posthogApiConfig = config.posthog;
}
this.enricherEnabled = config.enricher?.enabled !== false;
if (config.posthog && !config.skipLogPersistence) {
this.sessionLogWriter = new SessionLogWriter({
posthogAPI: this.posthogAPI,
logger: this.logger.child("SessionLogWriter"),
localCachePath: config.localCachePath,
});
if (config.localCachePath) {
SessionLogWriter.cleanupOldSessions(config.localCachePath).catch(
() => {},
);
}
}
}
private async _resolveGatewayConfig(overrideUrl?: string): Promise<{
gatewayUrl: string;
apiKey: string;
} | null> {
if (!this.posthogAPI) {
return null;
}
try {
const gatewayUrl = overrideUrl ?? this.posthogAPI.getLlmGatewayUrl();
const apiKey = await this.posthogAPI.getApiKey();
return { gatewayUrl, apiKey };
} catch (error) {
this.logger.error("Failed to resolve LLM gateway config", error);
throw error;
}
}
async run(
taskId: string,
taskRunId: string,
options: TaskExecutionOptions = {},
): Promise<InProcessAcpConnection> {
const gatewayConfig = await this._resolveGatewayConfig(options.gatewayUrl);
this.taskRunId = taskRunId;
let codexModels: ModelInfo[] | undefined;
let sanitizedModel =
options.model && !isBlockedModelId(options.model)
? options.model
: undefined;
if (options.adapter === "codex" && gatewayConfig) {
const models = await fetchModelsList({
gatewayUrl: gatewayConfig.gatewayUrl,
authToken: gatewayConfig.apiKey,
});
const gatewayCodexModels = models.filter((model) => {
if (isBlockedModelId(model.id)) return false;
if (model.owned_by) {
return model.owned_by === "openai";
}
return model.id.startsWith("gpt-") || model.id.startsWith("openai/");
});
const allowedModelIds = gatewayCodexModels
.filter((model) => model.allowed)
.map((model) => model.id);
if (gatewayCodexModels.length > 0) {
codexModels = gatewayCodexModels;
}
if (!sanitizedModel || !allowedModelIds.includes(sanitizedModel)) {
sanitizedModel = allowedModelIds.includes(DEFAULT_CODEX_MODEL)
? DEFAULT_CODEX_MODEL
: (allowedModelIds[0] ?? sanitizedModel);
}
}
if (!sanitizedModel && options.adapter !== "codex") {
sanitizedModel = DEFAULT_GATEWAY_MODEL;
}
const claudeGatewayEnv: GatewayEnv | undefined =
options.adapter !== "codex" && gatewayConfig
? {
anthropicBaseUrl: gatewayConfig.gatewayUrl,
anthropicAuthToken: gatewayConfig.apiKey,
openaiBaseUrl: `${gatewayConfig.gatewayUrl}/v1`,
openaiApiKey: gatewayConfig.apiKey,
}
: undefined;
this.acpConnection = createAcpConnection({
adapter: options.adapter,
logWriter: this.sessionLogWriter,
taskRunId,
taskId,
deviceType: "local",
logger: this.logger,
processCallbacks: options.processCallbacks,
onStructuredOutput: options.onStructuredOutput,
codexModels,
posthogApiConfig: this.posthogApiConfig,
enricherEnabled: this.enricherEnabled,
claudeGatewayEnv,
codexOptions:
options.adapter === "codex" && gatewayConfig
? {
cwd: options.repositoryPath,
apiBaseUrl: `${gatewayConfig.gatewayUrl}/v1`,
apiKey: gatewayConfig.apiKey,
binaryPath: options.codexBinaryPath,
codexHome: options.codexHome,
model: sanitizedModel,
reasoningEffort: options.reasoningEffort,
developerInstructions: options.developerInstructions,
additionalDirectories: options.additionalDirectories,
}
: undefined,
});
return this.acpConnection;
}
async attachPullRequestToTask(
taskId: string,
prUrl: string,
branchName?: string,
): Promise<void> {
this.logger.info("Attaching PR to task run", { taskId, prUrl, branchName });
if (!this.posthogAPI || !this.taskRunId) {
const error = new Error(
"PostHog API not configured or no active run. Cannot attach PR to task.",
);
this.logger.error("PostHog API not configured", error);
throw error;
}
const freshOutput = await this.posthogAPI
.getTaskRun(taskId, this.taskRunId)
.then((run) => run.output)
.catch(() => null);
const urls = mergePrUrls(readPrUrls(freshOutput), [prUrl]);
const updates: TaskRunUpdate = {
output: buildPrOutput(freshOutput, urls),
};
if (branchName) {
updates.branch = branchName;
}
await this.posthogAPI.updateTaskRun(taskId, this.taskRunId, updates);
this.logger.debug("PR attached to task run", {
taskId,
taskRunId: this.taskRunId,
prUrl,
});
}
getPosthogAPI(): PostHogAPIClient | undefined {
return this.posthogAPI;
}
async flushAllLogs(): Promise<void> {
await this.sessionLogWriter?.flushAll();
}
async cleanup(): Promise<void> {
if (this.sessionLogWriter && this.taskRunId) {
await this.sessionLogWriter.flush(this.taskRunId, { coalesce: true });
}
await this.acpConnection?.cleanup();
}
}