11import * as Dialog from "../../components/ui/Dialog" ;
22import type { NavigationState , PartialState } from "@react-navigation/native" ;
3- import { EnvironmentId , ThreadId } from "@t3tools/contracts" ;
4- import { parseThreadLinkPrompt } from "@t3tools/shared/promptLinks" ;
3+ import { EnvironmentId , ThreadId , type ScopedThreadRef } from "@t3tools/contracts" ;
4+ import { parseThreadLinkPrompt , type LinkedPrompt } from "@t3tools/shared/promptLinks" ;
55
6+ import { findStackRoute } from "../../lib/appLinking" ;
7+ import { rootNavigationRef } from "../../lib/rootNavigation" ;
68import { scopedThreadKey } from "../../lib/scopedEntities" ;
9+ import { appAtomRegistry } from "../../state/atom-registry" ;
10+ import { environmentThreadShells } from "../../state/threads" ;
711import { applyLinkedPromptToThreadDraft } from "./prompt-link-actions" ;
8- import { promptLinkNotice , promptLinkSearchFromParams } from "./prompt-link-model" ;
12+ import {
13+ promptLinkNotice ,
14+ promptLinkSearchFromParams ,
15+ stepPendingPromptLink ,
16+ } from "./prompt-link-model" ;
917
1018const THREAD_LINK_KEYS = [ "prompt" ] ;
1119
20+ type PendingThreadLink = { readonly ref : ScopedThreadRef ; readonly prompt : LinkedPrompt } ;
21+
1222function firstString ( value : unknown ) : string | null {
1323 const first = Array . isArray ( value ) ? value [ 0 ] : value ;
1424 return typeof first === "string" && first . length > 0 ? first : null ;
1525}
1626
27+ let pendingLink : PendingThreadLink | null = null ;
28+ let stopWaitingForThread : ( ( ) => void ) | null = null ;
29+
1730/**
18- * `threads/<environment>/<thread>?prompt=…` fills that thread's composer draft.
19- * The link applies the prompt as it arrives and drops the param, so the thread
20- * screen never sees it and a later visit cannot apply it again.
31+ * `threads/<environment>/<thread>?prompt=…` fills that thread's composer draft. The param is
32+ * dropped as the link arrives, so the thread screen never sees it and a later visit cannot apply
33+ * it again. The draft is written only once the thread's screen shows a thread that exists: a
34+ * screen for an unknown thread replaces itself with a new-thread draft, which drops the link.
2135 */
2236export function takeThreadPromptLink (
2337 state : PartialState < NavigationState > | undefined ,
@@ -41,12 +55,51 @@ export function takeThreadPromptLink(
4155 Dialog . alert ( "Could not open link" , parsed . error ) ;
4256 return nextState ;
4357 }
58+ dropPendingLink ( ) ;
4459 if ( parsed . value !== null ) {
60+ pendingLink = {
61+ ref : { environmentId : EnvironmentId . make ( environmentId ) , threadId : ThreadId . make ( threadId ) } ,
62+ prompt : parsed . value ,
63+ } ;
64+ }
65+ return nextState ;
66+ }
67+
68+ /** Wired to the navigation container's state changes. */
69+ export function resolvePendingThreadPromptLink ( state : NavigationState | undefined ) : void {
70+ if ( pendingLink === null ) return ;
71+ const { ref } = pendingLink ;
72+ // Every thread the screen can show gets a shell: for a subagent thread the environment's list
73+ // leaves out, the screen mounts its family, which adds it.
74+ const shellAtom = environmentThreadShells . threadShellAtom ( ref ) ;
75+ const step = stepPendingPromptLink ( pendingLink , {
76+ screenOnStack :
77+ findStackRoute (
78+ state ,
79+ "Thread" ,
80+ ( params ) =>
81+ firstString ( params ?. environmentId ) === ref . environmentId &&
82+ firstString ( params ?. threadId ) === ref . threadId ,
83+ ) !== undefined ,
84+ ready : appAtomRegistry . get ( shellAtom ) !== null ,
85+ } ) ;
86+ if ( step . pending === null ) dropPendingLink ( ) ;
87+ if ( step . resolve !== null ) {
88+ const { prompt } = step . resolve ;
4589 void applyLinkedPromptToThreadDraft (
46- scopedThreadKey ( EnvironmentId . make ( environmentId ) , ThreadId . make ( threadId ) ) ,
47- parsed . value ,
48- promptLinkNotice ( { hiddenCharacters : parsed . value . hiddenCharacters } ) ,
90+ scopedThreadKey ( ref . environmentId , ref . threadId ) ,
91+ prompt ,
92+ promptLinkNotice ( { hiddenCharacters : prompt . hiddenCharacters } ) ,
4993 ) ;
94+ } else if ( step . pending !== null ) {
95+ stopWaitingForThread ??= appAtomRegistry . subscribe ( shellAtom , ( shell ) => {
96+ if ( shell !== null ) resolvePendingThreadPromptLink ( rootNavigationRef . getRootState ( ) ) ;
97+ } ) ;
5098 }
51- return nextState ;
99+ }
100+
101+ function dropPendingLink ( ) : void {
102+ pendingLink = null ;
103+ stopWaitingForThread ?.( ) ;
104+ stopWaitingForThread = null ;
52105}
0 commit comments