Skip to content

Commit eb610fe

Browse files
committed
parse the logs
1 parent 3f44ede commit eb610fe

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

apps/array/src/main/preload.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { AgentEvent } from "@posthog/agent";
12
import { contextBridge, type IpcRendererEvent, ipcRenderer } from "electron";
23
import type { CloudRegion, OAuthTokenResponse } from "../shared/types/oauth";
34
import type {
@@ -38,7 +39,7 @@ contextBridge.exposeInMainWorld("electronAPI", {
3839
ipcRenderer.invoke("store-api-key", apiKey),
3940
retrieveApiKey: (encryptedKey: string): Promise<string | null> =>
4041
ipcRenderer.invoke("retrieve-api-key", encryptedKey),
41-
fetchS3Logs: (logUrl: string): Promise<string> =>
42+
fetchS3Logs: (logUrl: string): Promise<AgentEvent[]> =>
4243
ipcRenderer.invoke("fetch-s3-logs", logUrl),
4344
// OAuth API
4445
oauthStartFlow: (

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

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { type AgentEvent, parseAgentEvents } from "@posthog/agent";
12
import { type IpcMainInvokeEvent, ipcMain, safeStorage } from "electron";
23
import { logger } from "../lib/logger";
34

@@ -34,10 +35,13 @@ export function registerPosthogIpc(): void {
3435
},
3536
);
3637

37-
// Fetch S3 logs
38+
// Fetch and parse S3 logs
3839
ipcMain.handle(
3940
"fetch-s3-logs",
40-
async (_event: IpcMainInvokeEvent, logUrl: string): Promise<string> => {
41+
async (
42+
_event: IpcMainInvokeEvent,
43+
logUrl: string,
44+
): Promise<AgentEvent[]> => {
4145
try {
4246
log.debug("Fetching S3 logs from:", logUrl);
4347
const response = await fetch(logUrl);
@@ -49,8 +53,24 @@ export function registerPosthogIpc(): void {
4953
}
5054

5155
const content = await response.text();
52-
log.debug("S3 logs fetched:", content);
53-
return content;
56+
57+
if (!content.trim()) {
58+
return [];
59+
}
60+
61+
const rawEntries = content
62+
.trim()
63+
.split("\n")
64+
.map((line) => {
65+
try {
66+
return JSON.parse(line);
67+
} catch {
68+
return null;
69+
}
70+
})
71+
.filter(Boolean);
72+
73+
return parseAgentEvents(rawEntries);
5474
} catch (error) {
5575
log.error("Failed to fetch S3 logs:", error);
5676
throw error;

apps/array/src/renderer/features/task-detail/stores/taskExecutionStore.ts

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useAuthStore } from "@features/auth/stores/authStore";
22
import { usePanelLayoutStore } from "@features/panels/store/panelLayoutStore";
33
import { useSettingsStore } from "@features/settings/stores/settingsStore";
4-
import { type AgentEvent, parseAgentEvents } from "@posthog/agent";
4+
import type { AgentEvent } from "@posthog/agent";
55
import { track } from "@renderer/lib/analytics";
66
import { logger } from "@renderer/lib/logger";
77
import { queryClient } from "@renderer/lib/queryClient";
@@ -83,30 +83,12 @@ const toClarifyingQuestions = (
8383
};
8484

8585
/**
86-
* Fetch logs from S3 log URL via main process to avoid CORS issues
87-
* S3 stores AgentEvent objects as newline-delimited JSON
86+
* Fetch logs from S3 log URL via main process
87+
* Main process handles parsing and validation
8888
*/
8989
async function fetchLogsFromS3Url(logUrl: string): Promise<AgentEvent[]> {
9090
try {
91-
const content = await window.electronAPI?.fetchS3Logs(logUrl);
92-
93-
if (!content || !content.trim()) {
94-
return [];
95-
}
96-
97-
const rawEntries = content
98-
.trim()
99-
.split("\n")
100-
.map((line: string) => {
101-
try {
102-
return JSON.parse(line);
103-
} catch {
104-
return null;
105-
}
106-
})
107-
.filter(Boolean);
108-
109-
return parseAgentEvents(rawEntries);
91+
return (await window.electronAPI?.fetchS3Logs(logUrl)) ?? [];
11092
} catch (err) {
11193
log.warn("Failed to fetch task logs from S3", err);
11294
return [];
@@ -385,9 +367,9 @@ export const useTaskExecutionStore = create<TaskExecutionStore>()(
385367
}
386368
};
387369

388-
// Poll immediately, then every 2 seconds
370+
// Poll immediately, then every 500ms
389371
void poll();
390-
const poller = setInterval(() => void poll(), 2000);
372+
const poller = setInterval(() => void poll(), 500);
391373
store.updateTaskState(taskId, { logPoller: poller });
392374
},
393375

apps/array/src/renderer/types/electron.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ declare global {
1818
interface IElectronAPI {
1919
storeApiKey: (apiKey: string) => Promise<string>;
2020
retrieveApiKey: (encryptedKey: string) => Promise<string | null>;
21-
fetchS3Logs: (logUrl: string) => Promise<string>;
21+
fetchS3Logs: (logUrl: string) => Promise<AgentEvent[]>;
2222
// OAuth API
2323
oauthStartFlow: (region: CloudRegion) => Promise<{
2424
success: boolean;

0 commit comments

Comments
 (0)