|
4 | 4 | } from "@t3tools/client-runtime/state/shell"; |
5 | 5 | import type { LocalThreadMessage } from "../lib/threadActivity"; |
6 | 6 | import { DEFAULT_PROVIDER_INTERACTION_MODE, DEFAULT_RUNTIME_MODE } from "@t3tools/contracts"; |
| 7 | +import { threadKey } from "@t3tools/client-runtime/state/entities"; |
7 | 8 | import { Atom } from "effect/reactivity"; |
8 | 9 | import * as DateTime from "effect/DateTime"; |
9 | 10 |
|
@@ -133,6 +134,49 @@ export function pendingThreadCreationMessage(message: QueuedThreadMessage): Loca |
133 | 134 | }; |
134 | 135 | } |
135 | 136 |
|
| 137 | +/** |
| 138 | + * Threads this client is launching read as preparing from Send until their first run lands. |
| 139 | + * The server commits the thread before its run, so the list would otherwise place it twice. |
| 140 | + */ |
| 141 | +export function withLaunchingThreads(input: { |
| 142 | + readonly threads: ReadonlyArray<EnvironmentThreadShell>; |
| 143 | + readonly launches: ReadonlyArray<QueuedThreadMessage>; |
| 144 | + readonly hiddenThreadKeys: ReadonlySet<string>; |
| 145 | +}): ReadonlyArray<EnvironmentThreadShell> { |
| 146 | + if (input.launches.length === 0) return input.threads; |
| 147 | + const launchByKey = new Map<string, QueuedThreadMessage>(); |
| 148 | + for (const message of input.launches) { |
| 149 | + if (!message.creation || input.hiddenThreadKeys.has(threadKey(message))) continue; |
| 150 | + launchByKey.set(scopedThreadKey(message.environmentId, message.threadId), message); |
| 151 | + } |
| 152 | + if (launchByKey.size === 0) return input.threads; |
| 153 | + const threads = input.threads.map((thread) => { |
| 154 | + const key = scopedThreadKey(thread.environmentId, thread.id); |
| 155 | + const launch = launchByKey.get(key); |
| 156 | + if (!launch) return thread; |
| 157 | + launchByKey.delete(key); |
| 158 | + return thread.latestRun === null && thread.runtime === null |
| 159 | + ? { ...thread, runtime: launchingRuntime(thread) } |
| 160 | + : thread; |
| 161 | + }); |
| 162 | + for (const launch of launchByKey.values()) { |
| 163 | + const shell = pendingThreadCreationShell(launch); |
| 164 | + if (shell) threads.push({ ...shell, runtime: launchingRuntime(shell) }); |
| 165 | + } |
| 166 | + return threads; |
| 167 | +} |
| 168 | + |
| 169 | +function launchingRuntime(thread: EnvironmentThreadShell): EnvironmentThreadShell["runtime"] { |
| 170 | + return { |
| 171 | + status: "preparing", |
| 172 | + activeRunId: null, |
| 173 | + providerInstanceId: thread.providerInstanceId, |
| 174 | + providerName: null, |
| 175 | + lastError: null, |
| 176 | + updatedAt: thread.updatedAt, |
| 177 | + }; |
| 178 | +} |
| 179 | + |
136 | 180 | export function pendingThreadCreationShell( |
137 | 181 | message: QueuedThreadMessage, |
138 | 182 | ): EnvironmentThreadShell | null { |
|
0 commit comments