refresh views after assistant changes
This commit is contained in:
1 parent
8dd1d70900
commit
c81a3b9432
14 files changed
+343
-7
No files matched your search
@@ -0,0 +1,46 @@
|
||||
import type { ThreadMessage } from "@assistant-ui/react";
|
||||
|
||||
const resultHasError = (result: unknown) => typeof result === "object" && result !== null && "error" in result;
|
||||
|
||||
export const getSuccessfulApprovedAssistantMutationIds = (messages: readonly ThreadMessage[]) =>
|
||||
messages.flatMap((message) =>
|
||||
message.role === "assistant"
|
||||
? message.content.flatMap((part) =>
|
||||
part.type === "tool-call" &&
|
||||
part.approval?.approved === true &&
|
||||
part.result !== undefined &&
|
||||
part.isError !== true &&
|
||||
!resultHasError(part.result)
|
||||
? [part.toolCallId]
|
||||
: [],
|
||||
)
|
||||
: [],
|
||||
);
|
||||
|
||||
export interface AssistantMutationRefreshState {
|
||||
conversationId: string | null;
|
||||
toolCallIds: Set<string>;
|
||||
}
|
||||
|
||||
export const updateAssistantMutationRefreshState = (
|
||||
state: AssistantMutationRefreshState,
|
||||
conversationId: string,
|
||||
successfulMutationIds: readonly string[],
|
||||
) => {
|
||||
if (state.conversationId !== conversationId) {
|
||||
return {
|
||||
state: { conversationId, toolCallIds: new Set(successfulMutationIds) },
|
||||
shouldRefresh: false,
|
||||
};
|
||||
}
|
||||
|
||||
const toolCallIds = new Set(state.toolCallIds);
|
||||
let shouldRefresh = false;
|
||||
for (const toolCallId of successfulMutationIds) {
|
||||
if (toolCallIds.has(toolCallId)) continue;
|
||||
toolCallIds.add(toolCallId);
|
||||
shouldRefresh = true;
|
||||
}
|
||||
|
||||
return { state: { conversationId, toolCallIds }, shouldRefresh };
|
||||
};
|
||||
Reference in new issue
Block a user