- Add thread-panel presentation logic for inline vs popover layouts - Move thread details controls into the chat header/sidebar flow - Add shared wait-for-atom helper and workspace display name parsing Co-authored-by: codex <[email protected]>
542 lines
18 KiB
TypeScript
542 lines
18 KiB
TypeScript
import {
|
|
type EnvironmentId,
|
|
isProviderDriverKind,
|
|
ProjectId,
|
|
type ModelSelection,
|
|
type ProviderDriverKind,
|
|
type ServerProvider,
|
|
type ScopedProjectRef,
|
|
type ScopedThreadRef,
|
|
type ThreadId,
|
|
type RunId,
|
|
} from "@t3tools/contracts";
|
|
import * as DateTime from "effect/DateTime";
|
|
import { presentThread } from "@t3tools/client-runtime/state/shell";
|
|
import { type ChatMessage, type SessionPhase, type Thread } from "../types";
|
|
import { type ComposerImageAttachment, type DraftThreadState } from "../composerDraftStore";
|
|
import * as Schema from "effect/Schema";
|
|
import { appAtomRegistry } from "../rpc/atomRegistry";
|
|
import { environmentThreadDetails } from "../state/threads";
|
|
import { waitForAtomValue } from "../state/waitForAtomValue";
|
|
import {
|
|
filterTerminalContextsWithText,
|
|
stripInlineTerminalContextPlaceholders,
|
|
type TerminalContextDraft,
|
|
} from "../lib/terminalContext";
|
|
import type { DraftThreadEnvMode } from "../composerDraftStore";
|
|
|
|
export const LAST_INVOKED_SCRIPT_BY_PROJECT_KEY = "t3code:last-invoked-script-by-project";
|
|
export const MAX_HIDDEN_MOUNTED_TERMINAL_THREADS = 10;
|
|
export const MAX_HIDDEN_MOUNTED_PREVIEW_THREADS = 3;
|
|
|
|
export const LastInvokedScriptByProjectSchema = Schema.Record(ProjectId, Schema.String);
|
|
|
|
export function startNewThreadForProject(
|
|
projectRef: ScopedProjectRef | null,
|
|
handleNewThread: (projectRef: ScopedProjectRef) => Promise<void>,
|
|
): boolean {
|
|
if (projectRef === null) return false;
|
|
void handleNewThread(projectRef);
|
|
|
|
return true;
|
|
}
|
|
|
|
export function resolveThreadMetadataUpdateForNextTurn(input: {
|
|
currentModelSelection: ModelSelection;
|
|
nextModelSelection?: ModelSelection;
|
|
currentBranch: string | null;
|
|
nextBranch?: string;
|
|
}): {
|
|
modelSelection?: ModelSelection;
|
|
branch?: string;
|
|
worktreePath?: null;
|
|
} | null {
|
|
const nextModelSelection = input.nextModelSelection;
|
|
const modelSelectionChanged =
|
|
nextModelSelection !== undefined &&
|
|
(nextModelSelection.model !== input.currentModelSelection.model ||
|
|
nextModelSelection.instanceId !== input.currentModelSelection.instanceId ||
|
|
JSON.stringify(nextModelSelection.options ?? null) !==
|
|
JSON.stringify(input.currentModelSelection.options ?? null));
|
|
const branchChanged = input.nextBranch !== undefined && input.nextBranch !== input.currentBranch;
|
|
if (!modelSelectionChanged && !branchChanged) {
|
|
return null;
|
|
}
|
|
return {
|
|
...(modelSelectionChanged ? { modelSelection: nextModelSelection } : {}),
|
|
...(branchChanged ? { branch: input.nextBranch, worktreePath: null } : {}),
|
|
};
|
|
}
|
|
|
|
export function buildLocalDraftThread(
|
|
threadId: ThreadId,
|
|
draftThread: DraftThreadState,
|
|
fallbackModelSelection: ModelSelection,
|
|
): Thread {
|
|
const timestamp = DateTime.makeUnsafe(draftThread.createdAt);
|
|
return presentThread(draftThread.environmentId, {
|
|
thread: {
|
|
id: threadId,
|
|
projectId: draftThread.projectId,
|
|
title: "New thread",
|
|
providerInstanceId: fallbackModelSelection.instanceId,
|
|
modelSelection: fallbackModelSelection,
|
|
runtimeMode: draftThread.runtimeMode,
|
|
interactionMode: draftThread.interactionMode,
|
|
branch: draftThread.branch,
|
|
worktreePath: draftThread.worktreePath,
|
|
activeProviderThreadId: null,
|
|
lineage: { rootThreadId: threadId, parentThreadId: null, relationshipToParent: null },
|
|
forkedFrom: null,
|
|
createdBy: "user",
|
|
creationSource: "web",
|
|
createdAt: timestamp,
|
|
updatedAt: timestamp,
|
|
archivedAt: null,
|
|
deletedAt: null,
|
|
},
|
|
runs: [],
|
|
attempts: [],
|
|
nodes: [],
|
|
subagents: [],
|
|
providerSessions: [],
|
|
providerThreads: [],
|
|
providerTurns: [],
|
|
runtimeRequests: [],
|
|
messages: [],
|
|
plans: [],
|
|
turnItems: [],
|
|
checkpointScopes: [],
|
|
checkpoints: [],
|
|
contextHandoffs: [],
|
|
contextTransfers: [],
|
|
visibleTurnItems: [],
|
|
updatedAt: timestamp,
|
|
});
|
|
}
|
|
|
|
export function buildLoadingThreadFromShell(shell: ThreadShell): Thread {
|
|
return {
|
|
...shell,
|
|
messages: [],
|
|
proposedPlans: [],
|
|
activities: [],
|
|
checkpoints: [],
|
|
deletedAt: null,
|
|
};
|
|
}
|
|
|
|
export function shouldWriteThreadErrorToCurrentServerThread(input: {
|
|
activeServerThread:
|
|
| {
|
|
environmentId: EnvironmentId;
|
|
id: ThreadId;
|
|
}
|
|
| null
|
|
| undefined;
|
|
routeThreadRef: ScopedThreadRef;
|
|
targetThreadId: ThreadId;
|
|
}): boolean {
|
|
return Boolean(
|
|
input.activeServerThread &&
|
|
input.targetThreadId === input.routeThreadRef.threadId &&
|
|
input.activeServerThread.environmentId === input.routeThreadRef.environmentId &&
|
|
input.activeServerThread.id === input.targetThreadId,
|
|
);
|
|
}
|
|
|
|
export function buildThreadTurnInterruptInput(thread: Pick<Thread, "id" | "session">): {
|
|
threadId: ThreadId;
|
|
turnId?: TurnId;
|
|
} {
|
|
const runningTurnId = thread.session?.status === "running" ? thread.session.activeTurnId : null;
|
|
return {
|
|
threadId: thread.id,
|
|
...(runningTurnId !== null ? { turnId: runningTurnId } : {}),
|
|
};
|
|
}
|
|
|
|
export function reconcileMountedTerminalThreadIds(input: {
|
|
currentThreadIds: ReadonlyArray<string>;
|
|
openThreadIds: ReadonlyArray<string>;
|
|
activeThreadId: string | null;
|
|
activeThreadTerminalOpen: boolean;
|
|
maxHiddenThreadCount?: number;
|
|
}): string[] {
|
|
return reconcileRetainedMountedThreadIds({
|
|
currentThreadIds: input.currentThreadIds,
|
|
openThreadIds: input.openThreadIds,
|
|
activeThreadId: input.activeThreadId,
|
|
activeThreadOpen: input.activeThreadTerminalOpen,
|
|
maxHiddenThreadCount: input.maxHiddenThreadCount ?? MAX_HIDDEN_MOUNTED_TERMINAL_THREADS,
|
|
});
|
|
}
|
|
|
|
export function reconcileRetainedMountedThreadIds(input: {
|
|
currentThreadIds: ReadonlyArray<string>;
|
|
openThreadIds: ReadonlyArray<string>;
|
|
activeThreadId: string | null;
|
|
activeThreadOpen: boolean;
|
|
maxHiddenThreadCount: number;
|
|
retainInactiveActiveThread?: boolean;
|
|
}): string[] {
|
|
const openThreadIdSet = new Set(input.openThreadIds);
|
|
const hiddenThreadIds = input.currentThreadIds.filter(
|
|
(threadId) =>
|
|
(threadId !== input.activeThreadId || input.retainInactiveActiveThread === true) &&
|
|
openThreadIdSet.has(threadId),
|
|
);
|
|
const maxHiddenThreadCount = Math.max(0, input.maxHiddenThreadCount);
|
|
const nextThreadIds =
|
|
hiddenThreadIds.length > maxHiddenThreadCount
|
|
? hiddenThreadIds.slice(-maxHiddenThreadCount)
|
|
: hiddenThreadIds;
|
|
|
|
if (
|
|
input.activeThreadId &&
|
|
input.activeThreadOpen &&
|
|
!nextThreadIds.includes(input.activeThreadId)
|
|
) {
|
|
nextThreadIds.push(input.activeThreadId);
|
|
}
|
|
|
|
return nextThreadIds;
|
|
}
|
|
|
|
export function revokeBlobPreviewUrl(previewUrl: string | undefined): void {
|
|
if (!previewUrl || typeof URL === "undefined" || !previewUrl.startsWith("blob:")) {
|
|
return;
|
|
}
|
|
URL.revokeObjectURL(previewUrl);
|
|
}
|
|
|
|
export function revokeUserMessagePreviewUrls(message: ChatMessage): void {
|
|
if (message.role !== "user" || !message.attachments) {
|
|
return;
|
|
}
|
|
for (const attachment of message.attachments) {
|
|
if (attachment.type !== "image") {
|
|
continue;
|
|
}
|
|
revokeBlobPreviewUrl(attachment.previewUrl);
|
|
}
|
|
}
|
|
|
|
export function collectUserMessageBlobPreviewUrls(message: ChatMessage): string[] {
|
|
if (message.role !== "user" || !message.attachments) {
|
|
return [];
|
|
}
|
|
const previewUrls: string[] = [];
|
|
for (const attachment of message.attachments) {
|
|
if (attachment.type !== "image") continue;
|
|
if (!attachment.previewUrl || !attachment.previewUrl.startsWith("blob:")) continue;
|
|
previewUrls.push(attachment.previewUrl);
|
|
}
|
|
return previewUrls;
|
|
}
|
|
|
|
export interface PullRequestDialogState {
|
|
initialReference: string | null;
|
|
key: number;
|
|
}
|
|
|
|
export function readFileAsDataUrl(file: File): Promise<string> {
|
|
return new Promise((resolve, reject) => {
|
|
const reader = new FileReader();
|
|
reader.addEventListener("load", () => {
|
|
if (typeof reader.result === "string") {
|
|
resolve(reader.result);
|
|
return;
|
|
}
|
|
reject(new Error("Could not read image data."));
|
|
});
|
|
reader.addEventListener("error", () => {
|
|
reject(reader.error ?? new Error("Failed to read image."));
|
|
});
|
|
reader.readAsDataURL(file);
|
|
});
|
|
}
|
|
|
|
export function resolveSendEnvMode(input: {
|
|
requestedEnvMode: DraftThreadEnvMode;
|
|
isGitRepo: boolean;
|
|
}): DraftThreadEnvMode {
|
|
return input.isGitRepo ? input.requestedEnvMode : "local";
|
|
}
|
|
|
|
export function cloneComposerImageForRetry(
|
|
image: ComposerImageAttachment,
|
|
): ComposerImageAttachment {
|
|
if (typeof URL === "undefined" || !image.previewUrl.startsWith("blob:")) {
|
|
return image;
|
|
}
|
|
try {
|
|
return {
|
|
...image,
|
|
previewUrl: URL.createObjectURL(image.file),
|
|
};
|
|
} catch {
|
|
return image;
|
|
}
|
|
}
|
|
|
|
export function deriveComposerSendState(options: {
|
|
prompt: string;
|
|
imageCount: number;
|
|
terminalContexts: ReadonlyArray<TerminalContextDraft>;
|
|
/**
|
|
* Optional element-pick attachment count. Element contexts contribute to
|
|
* "sendable content" exactly like images and (text-bearing) terminal
|
|
* contexts do: a prompt of just element chips is still a valid send.
|
|
*/
|
|
elementContextCount?: number;
|
|
}): {
|
|
trimmedPrompt: string;
|
|
sendableTerminalContexts: TerminalContextDraft[];
|
|
expiredTerminalContextCount: number;
|
|
hasSendableContent: boolean;
|
|
} {
|
|
const trimmedPrompt = stripInlineTerminalContextPlaceholders(options.prompt).trim();
|
|
const sendableTerminalContexts = filterTerminalContextsWithText(options.terminalContexts);
|
|
const expiredTerminalContextCount =
|
|
options.terminalContexts.length - sendableTerminalContexts.length;
|
|
const elementContextCount = options.elementContextCount ?? 0;
|
|
return {
|
|
trimmedPrompt,
|
|
sendableTerminalContexts,
|
|
expiredTerminalContextCount,
|
|
hasSendableContent:
|
|
trimmedPrompt.length > 0 ||
|
|
options.imageCount > 0 ||
|
|
sendableTerminalContexts.length > 0 ||
|
|
elementContextCount > 0,
|
|
};
|
|
}
|
|
|
|
export function buildExpiredTerminalContextToastCopy(
|
|
expiredTerminalContextCount: number,
|
|
variant: "omitted" | "empty",
|
|
): { title: string; description: string } {
|
|
const count = Math.max(1, Math.floor(expiredTerminalContextCount));
|
|
const noun = count === 1 ? "Expired terminal context" : "Expired terminal contexts";
|
|
if (variant === "empty") {
|
|
return {
|
|
title: `${noun} won't be sent`,
|
|
description: "Remove it or re-add it to include terminal output.",
|
|
};
|
|
}
|
|
return {
|
|
title: `${noun} omitted from message`,
|
|
description: "Re-add it if you want that terminal output included.",
|
|
};
|
|
}
|
|
|
|
export function branchMismatchKey(
|
|
threadId: string | null,
|
|
mismatch: { threadBranch: string; currentBranch: string } | null,
|
|
): string | null {
|
|
if (!threadId || !mismatch) {
|
|
return null;
|
|
}
|
|
return `${threadId}:${mismatch.threadBranch}:${mismatch.currentBranch}`;
|
|
}
|
|
|
|
// The mismatch banner only matters when the user is about to send: passive
|
|
// reading of an old thread carries no risk (the branch picker tint already
|
|
// covers ambient awareness). Draft content is the intent signal — composer
|
|
// focus is useless here because ChatView autofocuses the composer on every
|
|
// thread open. `wasShownForCurrentMismatch` keeps the banner mounted once
|
|
// revealed so it doesn't flicker away when the draft is cleared.
|
|
export function shouldShowBranchMismatchBanner(input: {
|
|
hasMismatch: boolean;
|
|
isDismissed: boolean;
|
|
composerHasContent: boolean;
|
|
wasShownForCurrentMismatch: boolean;
|
|
}): boolean {
|
|
if (!input.hasMismatch || input.isDismissed) {
|
|
return false;
|
|
}
|
|
return input.composerHasContent || input.wasShownForCurrentMismatch;
|
|
}
|
|
|
|
// Session-scoped (module-level so it survives ChatView remounts, e.g. route
|
|
// changes). Durable cross-device dismissal is planned as a server-side ack.
|
|
const sessionDismissedBranchMismatchKeys = new Set<string>();
|
|
|
|
export function dismissBranchMismatchForSession(key: string): void {
|
|
sessionDismissedBranchMismatchKeys.add(key);
|
|
}
|
|
|
|
export function isBranchMismatchDismissedForSession(key: string | null): boolean {
|
|
return key !== null && sessionDismissedBranchMismatchKeys.has(key);
|
|
}
|
|
|
|
export function threadHasStarted(thread: Thread | null | undefined): boolean {
|
|
return Boolean(
|
|
thread && (thread.latestRun !== null || thread.messages.length > 0 || thread.runtime !== null),
|
|
);
|
|
}
|
|
|
|
// `threadProvider` is the open branded driver kind carried by the session.
|
|
// Unknown driver kinds degrade to `null` (i.e. "unlocked"), which is the safe
|
|
// rollback / fork behavior — the routing layer is the right place to surface
|
|
// "driver not installed" errors, not the lock state.
|
|
//
|
|
// `selectedProvider` takes the same open-string shape because the composer
|
|
// now tracks the picker selection as a `ProviderInstanceId` (e.g.
|
|
// `codex_personal`). Custom instance ids that don't directly match a
|
|
// registered driver resolve to `null` here, which matches the existing
|
|
// "unknown driver -> unlocked" semantics. Callers that want the lock to track
|
|
// a custom instance's underlying driver kind should resolve the instance id
|
|
// upstream and pass the correlated kind.
|
|
export function deriveLockedProvider(input: {
|
|
thread: Thread | null | undefined;
|
|
selectedProvider: string | null;
|
|
threadProvider: string | null;
|
|
}): ProviderDriverKind | null {
|
|
if (!threadHasStarted(input.thread)) {
|
|
return null;
|
|
}
|
|
const sessionProvider = input.thread?.runtime?.providerName ?? null;
|
|
if (sessionProvider && isProviderDriverKind(sessionProvider)) {
|
|
return sessionProvider;
|
|
}
|
|
const narrowedThreadProvider =
|
|
input.threadProvider && isProviderDriverKind(input.threadProvider)
|
|
? input.threadProvider
|
|
: null;
|
|
const narrowedSelectedProvider =
|
|
input.selectedProvider && isProviderDriverKind(input.selectedProvider)
|
|
? input.selectedProvider
|
|
: null;
|
|
return narrowedThreadProvider ?? narrowedSelectedProvider ?? null;
|
|
}
|
|
|
|
export function getStartedThreadModelChangeBlockReason(input: {
|
|
providers: ReadonlyArray<Pick<ServerProvider, "instanceId" | "requiresNewThreadForModelChange">>;
|
|
hasStartedSession: boolean;
|
|
currentModelSelection: ModelSelection;
|
|
currentProviderInstanceId?: ModelSelection["instanceId"] | null | undefined;
|
|
nextModelSelection: ModelSelection;
|
|
}): { title: string; description: string } | null {
|
|
if (!input.hasStartedSession) {
|
|
return null;
|
|
}
|
|
const currentModelSelection = {
|
|
...input.currentModelSelection,
|
|
instanceId: input.currentProviderInstanceId ?? input.currentModelSelection.instanceId,
|
|
};
|
|
if (
|
|
currentModelSelection.instanceId === input.nextModelSelection.instanceId &&
|
|
currentModelSelection.model === input.nextModelSelection.model
|
|
) {
|
|
return null;
|
|
}
|
|
const currentProvider = input.providers.find(
|
|
(snapshot) => snapshot.instanceId === currentModelSelection.instanceId,
|
|
);
|
|
const nextProvider = input.providers.find(
|
|
(snapshot) => snapshot.instanceId === input.nextModelSelection.instanceId,
|
|
);
|
|
if (
|
|
currentProvider?.requiresNewThreadForModelChange !== true &&
|
|
nextProvider?.requiresNewThreadForModelChange !== true
|
|
) {
|
|
return null;
|
|
}
|
|
return {
|
|
title: "Start a new chat to change models",
|
|
description: "This provider does not allow switching models after a conversation has started.",
|
|
};
|
|
}
|
|
|
|
export async function waitForStartedServerThread(
|
|
threadRef: ScopedThreadRef,
|
|
timeoutMs = 1_000,
|
|
): Promise<boolean> {
|
|
const threadAtom = environmentThreadDetails.detailAtom(threadRef);
|
|
return waitForAtomValue({
|
|
registry: appAtomRegistry,
|
|
atom: threadAtom,
|
|
predicate: threadHasStarted,
|
|
timeoutMs,
|
|
});
|
|
}
|
|
|
|
export interface LocalDispatchSnapshot {
|
|
startedAt: string;
|
|
preparingWorktree: boolean;
|
|
latestRunId: RunId | null;
|
|
latestRunRequestedAt: string | null;
|
|
latestRunStartedAt: string | null;
|
|
latestRunCompletedAt: string | null;
|
|
runtimeStatus: NonNullable<Thread["runtime"]>["status"] | null;
|
|
runtimeUpdatedAt: string | null;
|
|
}
|
|
|
|
export function createLocalDispatchSnapshot(
|
|
activeThread: Thread | undefined,
|
|
options?: { preparingWorktree?: boolean },
|
|
): LocalDispatchSnapshot {
|
|
const latestRun = activeThread?.latestRun ?? null;
|
|
const runtime = activeThread?.runtime ?? null;
|
|
return {
|
|
startedAt: new Date().toISOString(),
|
|
preparingWorktree: Boolean(options?.preparingWorktree),
|
|
latestRunId: latestRun?.runId ?? null,
|
|
latestRunRequestedAt: latestRun?.requestedAt ?? null,
|
|
latestRunStartedAt: latestRun?.startedAt ?? null,
|
|
latestRunCompletedAt: latestRun?.completedAt ?? null,
|
|
runtimeStatus: runtime?.status ?? null,
|
|
runtimeUpdatedAt: runtime?.updatedAt ?? null,
|
|
};
|
|
}
|
|
|
|
export function hasServerAcknowledgedLocalDispatch(input: {
|
|
localDispatch: LocalDispatchSnapshot | null;
|
|
phase: SessionPhase;
|
|
latestRun: Thread["latestRun"] | null;
|
|
runtime: Thread["runtime"] | null;
|
|
hasPendingApproval: boolean;
|
|
hasPendingUserInput: boolean;
|
|
threadError: string | null | undefined;
|
|
}): boolean {
|
|
if (!input.localDispatch) {
|
|
return false;
|
|
}
|
|
if (input.hasPendingApproval || input.hasPendingUserInput || Boolean(input.threadError)) {
|
|
return true;
|
|
}
|
|
|
|
const latestRun = input.latestRun ?? null;
|
|
const runtime = input.runtime ?? null;
|
|
const latestRunChanged =
|
|
input.localDispatch.latestRunId !== (latestRun?.runId ?? null) ||
|
|
input.localDispatch.latestRunRequestedAt !== (latestRun?.requestedAt ?? null) ||
|
|
input.localDispatch.latestRunStartedAt !== (latestRun?.startedAt ?? null) ||
|
|
input.localDispatch.latestRunCompletedAt !== (latestRun?.completedAt ?? null);
|
|
|
|
if (input.phase === "running") {
|
|
if (!latestRunChanged) {
|
|
return false;
|
|
}
|
|
if (latestRun?.startedAt === null || latestRun === null) {
|
|
return false;
|
|
}
|
|
if (
|
|
runtime?.activeRunId !== null &&
|
|
runtime?.activeRunId !== undefined &&
|
|
latestRun?.runId !== runtime.activeRunId
|
|
) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
return (
|
|
latestRunChanged ||
|
|
input.localDispatch.runtimeStatus !== (runtime?.status ?? null) ||
|
|
input.localDispatch.runtimeUpdatedAt !== (runtime?.updatedAt ?? null)
|
|
);
|
|
}
|