-
Notifications
You must be signed in to change notification settings - Fork 881
feat(site): add download logs option #13466
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very handy!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good so far! I feel like there might be some ways to tighten up useAgentLogs
, but I'll come back to this tomorrow morning, when I'm not so tired
site/src/pages/WorkspacePage/WorkspaceActions/DownloadLogsDialog.tsx
Outdated
Show resolved
Hide resolved
@BrunoQuaresma What do you think of this? type UseAgentLogsOptions = Readonly<{
workspaceId: string;
agentId: string;
agentLifeCycleState: WorkspaceAgentLifecycle;
enabled?: boolean;
}>;
export function useAgentLogs(
options: UseAgentLogsOptions,
): readonly WorkspaceAgentLog[] | undefined {
const { workspaceId, agentId, agentLifeCycleState, enabled = true } = options;
const queryClient = useQueryClient();
const queryOptions = agentLogs(workspaceId, agentId);
const { data } = useQuery({
...queryOptions,
enabled,
select: (logs) => {
return {
logs,
lastLogId: logs.at(-1)?.id ?? 0,
};
},
});
const socketRef = useRef<WebSocket | null>(null);
const lastInitializedAgentIdRef = useRef<string | null>(null);
const addLogs = useEffectEvent((newLogs: WorkspaceAgentLog[]) => {
queryClient.setQueryData(
queryOptions.queryKey,
(oldLogs: WorkspaceAgentLog[] = []) => [...oldLogs, ...newLogs],
);
});
useEffect(() => {
const isSameAgentId = agentId === lastInitializedAgentIdRef.current;
if (!isSameAgentId) {
socketRef.current?.close();
}
const cannotCreateSocket =
agentLifeCycleState !== "starting" || data === undefined;
if (cannotCreateSocket) {
return;
}
const socket = watchWorkspaceAgentLogs(agentId, {
after: data.lastLogId,
onMessage: (newLogs) => {
// Prevent new logs getting added when a connection is not open
if (socket.readyState === WebSocket.OPEN) {
addLogs(newLogs);;
}
},
onError: (error) => {
// Might be able to put more precise error handling here?
console.error(error);
},
});
socketRef.current = socket;
lastInitializedAgentIdRef.current = agentId;
}, [addLogs, agentId, agentLifeCycleState, data]);
// The above effect is likely going to run a lot because we don't know when or
// how agentLifeCycleState will change over time (it's a union of nine
// values). The only way to ensure that we only close when we unmount is by
// putting the logic into a separate effect with an empty dependency array
useEffect(() => {
const closeSocketOnUnmount = () => socketRef.current?.close();
return closeSocketOnUnmount;
}, []);
return data?.logs;
} |
@BrunoQuaresma Realized one other thing: if the query for the agent logs hook is only able to run once, is it a problem if the query doesn't come back in the starting state at first? I don't know enough about lifecycle states, and I don't know if there's a flow chart for how the states can transition into each other anywhere |
Do you have a use case that could contextualize this better? |
Looking at the type definition for My main worry was, if a workspace is already in the |
@Parkreiner I think I maybe answered this question here #13466 (comment) |
@Parkreiner I had to roll back to the previous |
@BrunoQuaresma Ah, right – forgot that we are mutating the cache, so the data is potentially going to be changing a lot |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! And glad you caught that infinite loop issue
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me!
Close #12485
Screen.Recording.2024-06-03.at.15.31.41.mov