|
| 1 | +import { type OrchestrationV2ProviderTurn, type RunAttemptId } from "@t3tools/contracts"; |
| 2 | +import { |
| 3 | + deriveLatestThreadRun, |
| 4 | + deriveThreadRuntime, |
| 5 | +} from "@t3tools/client-runtime/state/thread-execution"; |
| 6 | +import { derivePendingBackgroundWork } from "@t3tools/shared/orchestrationV2PendingBackgroundWork"; |
| 7 | +import { |
| 8 | + latestUnheldRun, |
| 9 | + usageLimitRunPresentedAsLatest, |
| 10 | +} from "@t3tools/shared/orchestrationV2ThreadError"; |
1 | 11 | import * as Option from "effect/Option"; |
2 | 12 | import type { SetStateAction } from "react"; |
3 | 13 | import { applyClaudePromptEffortPrefix, resolvePromptInjectedEffort } from "@t3tools/shared/model"; |
4 | 14 | import { getProviderModelCapabilities } from "../providerModels"; |
5 | 15 | import { sanitizeThreadErrorMessage } from "~/rpc/transportError"; |
6 | | -import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell"; |
| 16 | +import type { |
| 17 | + EnvironmentThread, |
| 18 | + EnvironmentThreadShell, |
| 19 | +} from "@t3tools/client-runtime/state/shell"; |
7 | 20 | import { |
8 | 21 | ANTIGRAVITY_DEFAULT_MODEL, |
9 | 22 | PROVIDER_SEND_TURN_MAX_ATTACHMENTS, |
@@ -92,6 +105,8 @@ import { |
92 | 105 | type ProviderInstanceEntry, |
93 | 106 | } from "../providerInstances"; |
94 | 107 |
|
| 108 | +type ThreadProjection = EnvironmentThread["projection"]; |
| 109 | + |
95 | 110 | export const LAST_INVOKED_SCRIPT_BY_PROJECT_KEY = "t3code:last-invoked-script-by-project"; |
96 | 111 | export const MAX_HIDDEN_MOUNTED_TERMINAL_THREADS = 10; |
97 | 112 |
|
@@ -1610,3 +1625,136 @@ export function sendTitleFallbackLabels(snapshot: ComposerSendSnapshot) { |
1610 | 1625 | previewAnnotation ? previewAnnotationContextLabel(previewAnnotation) : undefined, |
1611 | 1626 | ]; |
1612 | 1627 | } |
| 1628 | + |
| 1629 | +/** A thread shell built from its loaded detail, for direct links that hydrate the detail first. */ |
| 1630 | +export function presentThreadShellFromProjection( |
| 1631 | + environmentId: EnvironmentId, |
| 1632 | + threadId: ThreadId, |
| 1633 | + projection: ThreadProjection, |
| 1634 | +) { |
| 1635 | + const latestRun = deriveLatestThreadRun(projection); |
| 1636 | + const runtime = deriveThreadRuntime(projection); |
| 1637 | + const pendingRuntimeRequest = |
| 1638 | + projection.runtimeRequests.findLast((request) => request.status === "pending") ?? null; |
| 1639 | + // Direct links can hydrate detail before the thread enters the shell index. |
| 1640 | + return { |
| 1641 | + ...presentThreadShell(environmentId, { |
| 1642 | + ...projection.thread, |
| 1643 | + latestRunId: latestRun?.runId ?? null, |
| 1644 | + activeRunId: runtime?.activeRunId ?? null, |
| 1645 | + status: runtime?.status ?? "idle", |
| 1646 | + pendingRuntimeRequest, |
| 1647 | + latestVisibleMessage: null, |
| 1648 | + latestUserMessageAt: |
| 1649 | + projection.messages.findLast((message) => message.role === "user")?.updatedAt ?? null, |
| 1650 | + hasActionableProposedPlan: projection.plans.some( |
| 1651 | + (plan) => plan.kind === "proposed_plan" && plan.status === "active", |
| 1652 | + ), |
| 1653 | + providerInstanceHistory: [ |
| 1654 | + ...new Set( |
| 1655 | + projection.providerThreads |
| 1656 | + .filter((thread) => thread.appThreadId === threadId && thread.ownerNodeId === null) |
| 1657 | + .map((thread) => thread.providerInstanceId), |
| 1658 | + ), |
| 1659 | + ], |
| 1660 | + goal: |
| 1661 | + projection.providerThreads.find( |
| 1662 | + (thread) => thread.id === projection.thread.activeProviderThreadId, |
| 1663 | + )?.goal ?? null, |
| 1664 | + itemCount: projection.turnItems.length, |
| 1665 | + visibleItemCount: projection.visibleTurnItems.length, |
| 1666 | + updatedAt: projection.updatedAt, |
| 1667 | + }), |
| 1668 | + latestRun, |
| 1669 | + runtime, |
| 1670 | + }; |
| 1671 | +} |
| 1672 | + |
| 1673 | +/** Background work the provider still runs after the latest run ended. */ |
| 1674 | +export function deriveThreadPendingBackgroundTasks(projection: ThreadProjection | null) { |
| 1675 | + if (projection === null) return []; |
| 1676 | + const sessionError = |
| 1677 | + projection.providerSessions.findLast( |
| 1678 | + (session) => session.providerInstanceId === projection.thread.providerInstanceId, |
| 1679 | + )?.lastError ?? null; |
| 1680 | + const latestRun = |
| 1681 | + usageLimitRunPresentedAsLatest(projection.runs, projection.turnItems, sessionError) ?? |
| 1682 | + latestUnheldRun(projection.runs); |
| 1683 | + return [ |
| 1684 | + ...derivePendingBackgroundWork({ |
| 1685 | + latestRun, |
| 1686 | + providerThreads: projection.providerThreads, |
| 1687 | + turnItems: projection.turnItems, |
| 1688 | + activeProviderThreadId: projection.thread.activeProviderThreadId, |
| 1689 | + runs: projection.runs, |
| 1690 | + pullRequests: projection.thread.pullRequests, |
| 1691 | + }), |
| 1692 | + ]; |
| 1693 | +} |
| 1694 | + |
| 1695 | +/** |
| 1696 | + * Run ordinals the conversation can be reverted to: those whose provider turn belongs to the |
| 1697 | + * active provider thread, plus 0 for the thread start. Empty without a live provider session |
| 1698 | + * on the thread's selected instance. |
| 1699 | + */ |
| 1700 | +export function deriveAvailableRevertTurnCounts(projection: ThreadProjection | null) { |
| 1701 | + const available = new Set<number>(); |
| 1702 | + const providerThread = projection?.providerThreads.find( |
| 1703 | + (candidate) => candidate.id === projection.thread.activeProviderThreadId, |
| 1704 | + ); |
| 1705 | + if ( |
| 1706 | + !providerThread?.providerSessionId || |
| 1707 | + providerThread.providerInstanceId !== projection?.thread.modelSelection.instanceId |
| 1708 | + ) |
| 1709 | + return available; |
| 1710 | + available.add(0); |
| 1711 | + const attemptsById = new Map(projection.attempts.map((attempt) => [attempt.id, attempt])); |
| 1712 | + const providerTurnsById = new Map< |
| 1713 | + OrchestrationV2ProviderTurn["id"], |
| 1714 | + OrchestrationV2ProviderTurn |
| 1715 | + >(); |
| 1716 | + const latestTurnByAttempt = new Map<RunAttemptId, OrchestrationV2ProviderTurn>(); |
| 1717 | + for (const turn of projection.providerTurns) { |
| 1718 | + providerTurnsById.set(turn.id, turn); |
| 1719 | + if (turn.runAttemptId === null) continue; |
| 1720 | + const latest = latestTurnByAttempt.get(turn.runAttemptId); |
| 1721 | + if (latest === undefined || turn.ordinal > latest.ordinal) { |
| 1722 | + latestTurnByAttempt.set(turn.runAttemptId, turn); |
| 1723 | + } |
| 1724 | + } |
| 1725 | + for (const run of projection.runs) { |
| 1726 | + if (run.activeAttemptId === null) continue; |
| 1727 | + const attempt = attemptsById.get(run.activeAttemptId); |
| 1728 | + const providerTurn = |
| 1729 | + latestTurnByAttempt.get(run.activeAttemptId) ?? |
| 1730 | + (attempt?.providerTurnId == null ? undefined : providerTurnsById.get(attempt.providerTurnId)); |
| 1731 | + // A ready file checkpoint alone cannot restore the provider conversation. |
| 1732 | + if (providerTurn?.providerThreadId === providerThread.id) available.add(run.ordinal); |
| 1733 | + } |
| 1734 | + return available; |
| 1735 | +} |
| 1736 | + |
| 1737 | +/** |
| 1738 | + * Changes when the agent may have changed the workspace, so file views refresh. File tools are |
| 1739 | + * explicit; completed commands are included because a shell command can mutate the workspace |
| 1740 | + * without reporting the paths it touched. |
| 1741 | + */ |
| 1742 | +export function deriveWorkspaceMutationId( |
| 1743 | + visibleTurnItems: ReadonlyArray<OrchestrationV2ProjectedTurnItem>, |
| 1744 | + latestCheckpointCompletedAt: string | null, |
| 1745 | +) { |
| 1746 | + let itemId: string | null = null; |
| 1747 | + for (let index = visibleTurnItems.length - 1; index >= 0; index -= 1) { |
| 1748 | + const item = visibleTurnItems[index]?.item; |
| 1749 | + if (!item) continue; |
| 1750 | + if (item.type !== "command_execution" && item.type !== "file_change") continue; |
| 1751 | + if (item.status === "pending" || item.status === "running" || item.status === "waiting") { |
| 1752 | + continue; |
| 1753 | + } |
| 1754 | + itemId = item.id; |
| 1755 | + break; |
| 1756 | + } |
| 1757 | + return itemId === null && latestCheckpointCompletedAt === null |
| 1758 | + ? null |
| 1759 | + : JSON.stringify([itemId, latestCheckpointCompletedAt]); |
| 1760 | +} |
0 commit comments