Open History to see the last commit
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 /** The scoped route thread key, which stays the same when a draft is promoted. */
19 threadKey: string;
20 activeThread: Thread | undefined;
21 activeLatestRun: Thread["latestRun"] | null;
22 latestUserMessageId: MessageId | null;
23 phase: SessionPhase;
24 activePendingApproval: RuntimeRequestId | null;
25 activePendingUserInput: RuntimeRequestId | null;
26 threadError: string | null | undefined;
27}) {
28 const [localDispatch, setLocalDispatch] = useState<LocalDispatchSnapshot | null>(null);
29 const [localDispatchThreadKey, setLocalDispatchThreadKey] = useState(input.threadKey);
30 if (localDispatchThreadKey !== input.threadKey) {
31 setLocalDispatchThreadKey(input.threadKey);
32 setLocalDispatch(null);
33 }
34
35 const resetLocalDispatch = () => {
36 setLocalDispatch(null);
37 };
38
39 const serverAcknowledgedLocalDispatch = hasServerAcknowledgedLocalDispatch({
40 localDispatch,
41 phase: input.phase,
42 latestRun: input.activeLatestRun,
43 latestUserMessageId: input.latestUserMessageId,
44 runtime: input.activeThread?.runtime ?? null,
45 hasPendingApproval: input.activePendingApproval !== null,
46 hasPendingUserInput: input.activePendingUserInput !== null,
47 threadError: input.threadError,
48 });
49 const activeLocalDispatch = serverAcknowledgedLocalDispatch ? null : localDispatch;
50 const beginLocalDispatch = (options?: {
51 preparingWorktree?: boolean;
52 submissionIntent?: ComposerSubmissionIntent;
53 providerInstanceId?: ProviderInstanceId;
54 }) => {
55 const preparingWorktree = Boolean(options?.preparingWorktree);
56 setLocalDispatch((current) => {
57 const active = serverAcknowledgedLocalDispatch ? null : current;
58 if (active) {
59 const submissionIntent = options?.submissionIntent ?? active.submissionIntent;
60 const providerInstanceId = options?.providerInstanceId ?? active.providerInstanceId;
61 return active.preparingWorktree === preparingWorktree &&
62 active.submissionIntent === submissionIntent &&
63 active.providerInstanceId === providerInstanceId
64 ? active
65 : { ...active, preparingWorktree, submissionIntent, providerInstanceId };
66 }
67 return createLocalDispatchSnapshot(input.activeThread, {
68 ...options,
69 environmentId: input.environmentId,
70 latestUserMessageId: input.latestUserMessageId,
71 });
72 });
73 };
74
75 return {
76 beginLocalDispatch,
77 resetLocalDispatch,
78 localDispatchStartedAt: activeLocalDispatch?.startedAt ?? null,
79 isPreparingWorktree: activeLocalDispatch?.preparingWorktree ?? false,
80 isSendBusy: activeLocalDispatch !== null,
81 localDispatchProviderInstanceId: activeLocalDispatch?.providerInstanceId ?? null,
82 backgroundSubmissionPending: activeLocalDispatch?.submissionIntent === "background",
83 };
84}