- Replace model-only fields with provider/model selections across orchestration - Add projection schema and migration updates for provider-backed snapshots - Update server and web tests to use the new selection shape
122 lines
4.1 KiB
TypeScript
122 lines
4.1 KiB
TypeScript
import { DEFAULT_RUNTIME_MODE, type ProjectId, ThreadId } from "@t3tools/contracts";
|
|
import { useNavigate, useParams } from "@tanstack/react-router";
|
|
import { useCallback } from "react";
|
|
import {
|
|
type DraftThreadEnvMode,
|
|
type DraftThreadState,
|
|
useComposerDraftStore,
|
|
} from "../composerDraftStore";
|
|
import { newThreadId } from "../lib/utils";
|
|
import { useStore } from "../store";
|
|
|
|
export function useHandleNewThread() {
|
|
const projects = useStore((store) => store.projects);
|
|
const threads = useStore((store) => store.threads);
|
|
const stickyModelSelection = useComposerDraftStore((store) => store.stickyModelSelection);
|
|
const navigate = useNavigate();
|
|
const routeThreadId = useParams({
|
|
strict: false,
|
|
select: (params) => (params.threadId ? ThreadId.makeUnsafe(params.threadId) : null),
|
|
});
|
|
const activeDraftThread = useComposerDraftStore((store) =>
|
|
routeThreadId ? (store.draftThreadsByThreadId[routeThreadId] ?? null) : null,
|
|
);
|
|
|
|
const activeThread = routeThreadId
|
|
? threads.find((thread) => thread.id === routeThreadId)
|
|
: undefined;
|
|
|
|
const handleNewThread = useCallback(
|
|
(
|
|
projectId: ProjectId,
|
|
options?: {
|
|
branch?: string | null;
|
|
worktreePath?: string | null;
|
|
envMode?: DraftThreadEnvMode;
|
|
},
|
|
): Promise<void> => {
|
|
const {
|
|
clearProjectDraftThreadId,
|
|
getDraftThread,
|
|
getDraftThreadByProjectId,
|
|
setModelSelection,
|
|
setDraftThreadContext,
|
|
setProjectDraftThreadId,
|
|
} = useComposerDraftStore.getState();
|
|
const hasBranchOption = options?.branch !== undefined;
|
|
const hasWorktreePathOption = options?.worktreePath !== undefined;
|
|
const hasEnvModeOption = options?.envMode !== undefined;
|
|
const storedDraftThread = getDraftThreadByProjectId(projectId);
|
|
const latestActiveDraftThread: DraftThreadState | null = routeThreadId
|
|
? getDraftThread(routeThreadId)
|
|
: null;
|
|
if (storedDraftThread) {
|
|
return (async () => {
|
|
if (hasBranchOption || hasWorktreePathOption || hasEnvModeOption) {
|
|
setDraftThreadContext(storedDraftThread.threadId, {
|
|
...(hasBranchOption ? { branch: options?.branch ?? null } : {}),
|
|
...(hasWorktreePathOption ? { worktreePath: options?.worktreePath ?? null } : {}),
|
|
...(hasEnvModeOption ? { envMode: options?.envMode } : {}),
|
|
});
|
|
}
|
|
setProjectDraftThreadId(projectId, storedDraftThread.threadId);
|
|
if (routeThreadId === storedDraftThread.threadId) {
|
|
return;
|
|
}
|
|
await navigate({
|
|
to: "/$threadId",
|
|
params: { threadId: storedDraftThread.threadId },
|
|
});
|
|
})();
|
|
}
|
|
|
|
clearProjectDraftThreadId(projectId);
|
|
|
|
if (
|
|
latestActiveDraftThread &&
|
|
routeThreadId &&
|
|
latestActiveDraftThread.projectId === projectId
|
|
) {
|
|
if (hasBranchOption || hasWorktreePathOption || hasEnvModeOption) {
|
|
setDraftThreadContext(routeThreadId, {
|
|
...(hasBranchOption ? { branch: options?.branch ?? null } : {}),
|
|
...(hasWorktreePathOption ? { worktreePath: options?.worktreePath ?? null } : {}),
|
|
...(hasEnvModeOption ? { envMode: options?.envMode } : {}),
|
|
});
|
|
}
|
|
setProjectDraftThreadId(projectId, routeThreadId);
|
|
return Promise.resolve();
|
|
}
|
|
|
|
const threadId = newThreadId();
|
|
const createdAt = new Date().toISOString();
|
|
return (async () => {
|
|
setProjectDraftThreadId(projectId, threadId, {
|
|
createdAt,
|
|
branch: options?.branch ?? null,
|
|
worktreePath: options?.worktreePath ?? null,
|
|
envMode: options?.envMode ?? "local",
|
|
runtimeMode: DEFAULT_RUNTIME_MODE,
|
|
});
|
|
if (stickyModelSelection) {
|
|
setModelSelection(threadId, stickyModelSelection);
|
|
}
|
|
|
|
await navigate({
|
|
to: "/$threadId",
|
|
params: { threadId },
|
|
});
|
|
})();
|
|
},
|
|
[navigate, routeThreadId, stickyModelSelection],
|
|
);
|
|
|
|
return {
|
|
activeDraftThread,
|
|
activeThread,
|
|
handleNewThread,
|
|
projects,
|
|
routeThreadId,
|
|
};
|
|
}
|