Skip to content

Commit 13eb030

Browse files
committed
ditch seperate log entries, everything is an AgentEvent
1 parent 5a85190 commit 13eb030

File tree

13 files changed

+477
-646
lines changed

13 files changed

+477
-646
lines changed

apps/array/src/api/posthogClient.ts

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import type { AgentEvent } from "@posthog/agent";
12
import { logger } from "@renderer/lib/logger";
2-
import type { LogEntry, Task, TaskRun } from "@shared/types";
3+
import type { Task, TaskRun } from "@shared/types";
34
import { buildApiFetcher } from "./fetcher";
45
import { createApiClient, type Schemas } from "./generated";
56

@@ -171,7 +172,7 @@ export class PostHogAPIClient {
171172
return data.results ?? data ?? [];
172173
}
173174

174-
async getTaskRun(taskId: string, runId: string) {
175+
async getTaskRun(taskId: string, runId: string): Promise<TaskRun> {
175176
const teamId = await this.getTeamId();
176177
const url = new URL(
177178
`${this.api.baseUrl}/api/projects/${teamId}/tasks/${taskId}/runs/${runId}/`,
@@ -229,7 +230,31 @@ export class PostHogAPIClient {
229230
return data as unknown as TaskRun;
230231
}
231232

232-
async getTaskLogs(taskId: string): Promise<LogEntry[]> {
233+
/**
234+
* Append events to a task run's S3 log file
235+
*/
236+
async appendTaskRunLog(
237+
taskId: string,
238+
runId: string,
239+
entries: AgentEvent[],
240+
): Promise<void> {
241+
const teamId = await this.getTeamId();
242+
const url = `${this.api.baseUrl}/api/projects/${teamId}/tasks/${taskId}/runs/${runId}/append_log/`;
243+
const response = await this.api.fetcher.fetch({
244+
method: "post",
245+
url: new URL(url),
246+
path: url,
247+
overrides: {
248+
body: JSON.stringify({ entries }),
249+
headers: { "Content-Type": "application/json" },
250+
},
251+
});
252+
if (!response.ok) {
253+
throw new Error(`Failed to append log: ${response.statusText}`);
254+
}
255+
}
256+
257+
async getTaskLogs(taskId: string): Promise<AgentEvent[]> {
233258
try {
234259
const task = (await this.getTask(taskId)) as unknown as Task;
235260
const logUrl = task?.latest_run?.log_url;
@@ -255,7 +280,7 @@ export class PostHogAPIClient {
255280
return content
256281
.trim()
257282
.split("\n")
258-
.map((line) => JSON.parse(line) as LogEntry);
283+
.map((line) => JSON.parse(line) as AgentEvent);
259284
} catch (err) {
260285
log.warn("Failed to fetch task logs from latest run", err);
261286
return [];

apps/array/src/main/lib/logger.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
import { app } from "electron";
12
import log from "electron-log/main";
23

34
// Initialize IPC transport to forward main process logs to renderer dev tools
45
log.initialize();
56

6-
log.transports.file.level = "info";
7-
log.transports.console.level = "info";
7+
// Set levels - use debug in dev (check NODE_ENV since app.isPackaged may not be ready)
8+
const isDev = process.env.NODE_ENV === "development" || !app.isPackaged;
9+
const level = isDev ? "debug" : "info";
10+
log.transports.file.level = level;
11+
log.transports.console.level = level;
12+
// IPC transport needs level set separately
13+
log.transports.ipc.level = level;
814

915
export const logger = {
1016
info: (message: string, ...args: unknown[]) => log.info(message, ...args),

apps/array/src/main/services/agent.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const log = logger.scope("agent");
1616
const onAgentLog: OnLogCallback = (level, scope, message, data) => {
1717
const scopedLog = logger.scope(scope);
1818
if (data !== undefined) {
19-
scopedLog[level](message, data);
19+
scopedLog[level as keyof typeof scopedLog](message, data);
2020
} else {
2121
scopedLog[level](message);
2222
}
@@ -88,7 +88,7 @@ export function registerAgentIpc(
8888
permissionMode,
8989
autoProgress,
9090
model,
91-
runMode,
91+
runMode: _runMode, // TODO: Add support for cloud runs
9292
createPR,
9393
}: AgentStartParams,
9494
): Promise<{ taskId: string; channel: string }> => {
@@ -158,7 +158,7 @@ export function registerAgentIpc(
158158
posthogApiKey: apiKey,
159159
posthogApiUrl: apiHost,
160160
posthogProjectId: projectId,
161-
debug: true,
161+
debug: !app.isPackaged,
162162
onLog: onAgentLog,
163163
});
164164

@@ -188,6 +188,12 @@ export function registerAgentIpc(
188188
// Store the current run ID
189189
controllerEntry.currentRunId = latestRun.id;
190190

191+
log.debug("Task progress poll", {
192+
runId: latestRun.id,
193+
status: latestRun.status,
194+
hasLogUrl: !!latestRun.log_url,
195+
});
196+
191197
emitToRenderer({
192198
type: "progress",
193199
ts: Date.now(),
@@ -252,7 +258,7 @@ export function registerAgentIpc(
252258
await agent.runTask(posthogTaskId, taskRunId, {
253259
repositoryPath: repoPath,
254260
permissionMode: resolvedPermission,
255-
isCloudMode: runMode === "cloud",
261+
isCloudMode: false,
256262
autoProgress: autoProgress ?? true,
257263
createPR: createPR ?? true,
258264
queryOverrides: {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { BaseLogEntry } from "@features/logs/components/BaseLogEntry";
2+
import type { AgentEvent } from "@posthog/agent";
3+
import { Code } from "@radix-ui/themes";
4+
5+
interface ConsoleViewProps {
6+
event: Extract<AgentEvent, { type: "console" }>;
7+
}
8+
9+
const LEVEL_COLORS = {
10+
debug: "gray",
11+
info: "blue",
12+
warn: "yellow",
13+
error: "red",
14+
} as const;
15+
16+
export function ConsoleView({ event }: ConsoleViewProps) {
17+
const color = LEVEL_COLORS[event.level] || "gray";
18+
19+
return (
20+
<BaseLogEntry ts={event.ts}>
21+
<Code size="2" color={color} variant="ghost">
22+
[{event.level}] {event.message}
23+
</Code>
24+
</BaseLogEntry>
25+
);
26+
}

apps/array/src/renderer/features/logs/components/LogEventRenderer.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ArtifactView } from "@features/logs/components/ArtifactView";
22
import { BaseLogEntry } from "@features/logs/components/BaseLogEntry";
3+
import { ConsoleView } from "@features/logs/components/ConsoleView";
34
import { DoneView } from "@features/logs/components/DoneView";
45
import { ErrorView } from "@features/logs/components/ErrorView";
56
import { InitView } from "@features/logs/components/InitView";
@@ -28,6 +29,7 @@ const EVENT_COMPONENT_MAP: Record<
2829
artifact: ArtifactView,
2930
metric: MetricEventView,
3031
progress: ProgressView,
32+
console: ConsoleView,
3133
};
3234

3335
const SKIP_EVENTS = [

0 commit comments

Comments
 (0)