@@ -20,16 +20,13 @@ type Command<Input, E> = (request: {
2020 readonly input : Input ;
2121} ) => Promise < AtomCommandResult < unknown , E > > ;
2222
23- /** Resolves to the first failed command's result, or the last successful one. */
24- export async function resumeThread < E > ( input : {
23+ interface ThreadContinuationInput < E > {
2524 readonly thread : {
2625 readonly environmentId : EnvironmentId ;
2726 readonly id : ThreadId ;
2827 readonly runtimeMode : RuntimeMode ;
2928 readonly interactionMode : ProviderInteractionMode ;
3029 } ;
31- readonly resumableRunId : RunId | null ;
32- readonly queueHeld : boolean ;
3330 readonly runtimeMode : RuntimeMode ;
3431 readonly interactionMode : ProviderInteractionMode ;
3532 readonly modelSelection : ModelSelection | null ;
@@ -38,46 +35,70 @@ export async function resumeThread<E>(input: {
3835 readonly setRuntimeMode : Command < SetThreadRuntimeModeInput , E > ;
3936 readonly setInteractionMode : Command < SetThreadInteractionModeInput , E > ;
4037 readonly startTurn : Command < StartThreadTurnInput , E > ;
41- readonly resumeQueue : Command < ResumeThreadQueueInput , E > ;
42- } ) : Promise < AtomCommandResult < unknown , E > > {
38+ }
39+
40+ export async function continueThread < E > (
41+ input : ThreadContinuationInput < E > & {
42+ readonly continuation :
43+ | { readonly kind : "goal" }
44+ | { readonly kind : "run" ; readonly runId : RunId } ;
45+ } ,
46+ ) : Promise < AtomCommandResult < unknown , E > > {
4347 const { thread, runtimeMode, interactionMode, createdAt } = input ;
4448 const environmentId = thread . environmentId ;
45- // The server holds the queue behind a stopped or usage-limited run until that run is continued.
46- if ( input . resumableRunId !== null ) {
47- // A turn on an existing thread runs with the thread's saved modes, not the turn's.
48- if ( runtimeMode !== thread . runtimeMode ) {
49- const saved = await input . setRuntimeMode ( {
50- environmentId,
51- input : { threadId : thread . id , runtimeMode, createdAt } ,
52- } ) ;
53- if ( saved . _tag === "Failure" ) return saved ;
54- }
55- if ( interactionMode !== thread . interactionMode ) {
56- const saved = await input . setInteractionMode ( {
57- environmentId,
58- input : { threadId : thread . id , interactionMode, createdAt } ,
59- } ) ;
60- if ( saved . _tag === "Failure" ) return saved ;
61- }
62- const continued = await input . startTurn ( {
49+ if ( runtimeMode !== thread . runtimeMode ) {
50+ const saved = await input . setRuntimeMode ( {
51+ environmentId,
52+ input : { threadId : thread . id , runtimeMode, createdAt } ,
53+ } ) ;
54+ if ( saved . _tag === "Failure" ) return saved ;
55+ }
56+ if ( interactionMode !== thread . interactionMode ) {
57+ const saved = await input . setInteractionMode ( {
6358 environmentId,
64- input : {
65- creationSource : "mobile" ,
66- threadId : thread . id ,
67- manualContinuationOfRunId : input . resumableRunId ,
68- ...( input . modelSelection ? { modelSelection : input . modelSelection } : { } ) ,
69- message : {
70- messageId : input . messageId ,
71- role : "user" ,
72- text : "Continue where you left off." ,
73- attachments : [ ] ,
74- } ,
75- runtimeMode,
76- interactionMode,
77- dispatchMode : "start" ,
59+ input : { threadId : thread . id , interactionMode, createdAt } ,
60+ } ) ;
61+ if ( saved . _tag === "Failure" ) return saved ;
62+ }
63+ return input . startTurn ( {
64+ environmentId,
65+ input : {
66+ creationSource : "mobile" ,
67+ threadId : thread . id ,
68+ ...( input . continuation . kind === "run"
69+ ? { manualContinuationOfRunId : input . continuation . runId }
70+ : { } ) ,
71+ ...( input . modelSelection ? { modelSelection : input . modelSelection } : { } ) ,
72+ message : {
73+ messageId : input . messageId ,
74+ role : "user" ,
75+ text : input . continuation . kind === "goal" ? "/goal resume" : "Continue where you left off." ,
76+ attachments : [ ] ,
7877 } ,
78+ runtimeMode,
79+ interactionMode,
80+ dispatchMode : input . continuation . kind === "goal" ? "queue" : "start" ,
81+ createdAt,
82+ } ,
83+ } ) ;
84+ }
85+
86+ export async function resumeThread < E > (
87+ input : ThreadContinuationInput < E > & {
88+ readonly resumableRunId : RunId | null ;
89+ readonly queueHeld : boolean ;
90+ readonly resumeQueue : Command < ResumeThreadQueueInput , E > ;
91+ } ,
92+ ) : Promise < AtomCommandResult < unknown , E > > {
93+ if ( input . resumableRunId !== null ) {
94+ const continued = await continueThread ( {
95+ ...input ,
96+ continuation : { kind : "run" , runId : input . resumableRunId } ,
7997 } ) ;
8098 if ( continued . _tag === "Failure" || ! input . queueHeld ) return continued ;
8199 }
82- return input . resumeQueue ( { environmentId, input : { threadId : thread . id } } ) ;
100+ return input . resumeQueue ( {
101+ environmentId : input . thread . environmentId ,
102+ input : { threadId : input . thread . id } ,
103+ } ) ;
83104}
0 commit comments