Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

useLocalDispatchState.ts

77 lines2.9 KB

1import {
2 type EnvironmentId,
3 type MessageId,
4 type ProviderInstanceId,
5 type RuntimeRequestId,
6} from "@t3tools/contracts";
7import { useState } from "react";
8import { type ComposerSubmissionIntent } from "../../composer-logic";
9import { type SessionPhase, type Thread } from "../../types";
10import {
11 createLocalDispatchSnapshot,
12 hasServerAcknowledgedLocalDispatch,
13 type LocalDispatchSnapshot,
14} from "../ChatView.logic";
15​
16export function useLocalDispatchState(input: {
17 environmentId: EnvironmentId;
18 activeThread: Thread | undefined;
19 activeLatestRun: Thread["latestRun"] | null;
20 latestUserMessageId: MessageId | null;
21 phase: SessionPhase;
22 activePendingApproval: RuntimeRequestId | null;
23 activePendingUserInput: RuntimeRequestId | null;
24 threadError: string | null | undefined;
25}) {
26 const [localDispatch, setLocalDispatch] = useState<LocalDispatchSnapshot | null>(null);
27​
28 const resetLocalDispatch = () => {
29 setLocalDispatch(null);
30 };
31​
32 const serverAcknowledgedLocalDispatch = hasServerAcknowledgedLocalDispatch({
33 localDispatch,
34 phase: input.phase,
35 latestRun: input.activeLatestRun,
36 latestUserMessageId: input.latestUserMessageId,
37 runtime: input.activeThread?.runtime ?? null,
38 hasPendingApproval: input.activePendingApproval !== null,
39 hasPendingUserInput: input.activePendingUserInput !== null,
40 threadError: input.threadError,
41 });
42 const activeLocalDispatch = serverAcknowledgedLocalDispatch ? null : localDispatch;
43 const beginLocalDispatch = (options?: {
44 preparingWorktree?: boolean;
45 submissionIntent?: ComposerSubmissionIntent;
46 providerInstanceId?: ProviderInstanceId;
47 }) => {
48 const preparingWorktree = Boolean(options?.preparingWorktree);
49 setLocalDispatch((current) => {
50 const active = serverAcknowledgedLocalDispatch ? null : current;
51 if (active) {
52 const submissionIntent = options?.submissionIntent ?? active.submissionIntent;
53 const providerInstanceId = options?.providerInstanceId ?? active.providerInstanceId;
54 return active.preparingWorktree === preparingWorktree &&
55 active.submissionIntent === submissionIntent &&
56 active.providerInstanceId === providerInstanceId
57 ? active
58 : { ...active, preparingWorktree, submissionIntent, providerInstanceId };
59 }
60 return createLocalDispatchSnapshot(input.activeThread, {
61 ...options,
62 environmentId: input.environmentId,
63 latestUserMessageId: input.latestUserMessageId,
64 });
65 });
66 };
67​
68 return {
69 beginLocalDispatch,
70 resetLocalDispatch,
71 localDispatchStartedAt: activeLocalDispatch?.startedAt ?? null,
72 isPreparingWorktree: activeLocalDispatch?.preparingWorktree ?? false,
73 isSendBusy: activeLocalDispatch !== null,
74 localDispatchProviderInstanceId: activeLocalDispatch?.providerInstanceId ?? null,
75 backgroundSubmissionPending: activeLocalDispatch?.submissionIntent === "background",
76 };