@@ -5,7 +5,6 @@ import { fileURLToPath } from "url"
55import { createElement } from "react"
66import pWaitFor from "p-wait-for"
77
8- import type { HistoryItem } from "@roo-code/types"
98import { setLogger } from "@roo-code/vscode-shim"
109
1110import {
@@ -23,8 +22,8 @@ import { JsonEventEmitter } from "@/agent/json-event-emitter.js"
2322
2423import { createClient } from "@/lib/sdk/index.js"
2524import { loadToken , loadSettings } from "@/lib/storage/index.js"
25+ import { readWorkspaceTaskSessions , resolveWorkspaceResumeSessionId } from "@/lib/task-history/index.js"
2626import { isRecord } from "@/lib/utils/guards.js"
27- import { arePathsEqual } from "@/lib/utils/path.js"
2827import { getEnvVarName , getApiKeyFromEnv } from "@/lib/utils/provider.js"
2928import { runOnboarding } from "@/lib/utils/onboarding.js"
3029import { getDefaultExtensionPath } from "@/lib/utils/extension.js"
@@ -105,38 +104,6 @@ async function warmRooModels(host: ExtensionHost): Promise<void> {
105104 } )
106105}
107106
108- function extractTaskHistoryFromMessage ( message : unknown ) : HistoryItem [ ] | undefined {
109- if ( ! isRecord ( message ) ) {
110- return undefined
111- }
112-
113- if ( message . type === "state" ) {
114- const state = isRecord ( message . state ) ? message . state : undefined
115- if ( Array . isArray ( state ?. taskHistory ) ) {
116- return state . taskHistory as HistoryItem [ ]
117- }
118- }
119-
120- if ( message . type === "taskHistoryUpdated" && Array . isArray ( message . taskHistory ) ) {
121- return message . taskHistory as HistoryItem [ ]
122- }
123-
124- return undefined
125- }
126-
127- function getMostRecentTaskIdInWorkspace ( taskHistory : HistoryItem [ ] , workspacePath : string ) : string | undefined {
128- const workspaceTasks = taskHistory . filter (
129- ( item ) => typeof item . workspace === "string" && arePathsEqual ( item . workspace , workspacePath ) ,
130- )
131-
132- if ( workspaceTasks . length === 0 ) {
133- return undefined
134- }
135-
136- const sorted = [ ...workspaceTasks ] . sort ( ( a , b ) => b . ts - a . ts )
137- return sorted [ 0 ] ?. id
138- }
139-
140107export async function run ( promptArg : string | undefined , flagOptions : FlagOptions ) {
141108 setLogger ( {
142109 info : ( ) => { } ,
@@ -360,6 +327,18 @@ export async function run(promptArg: string | undefined, flagOptions: FlagOption
360327 }
361328
362329 const useStdinPromptStream = flagOptions . stdinPromptStream
330+ let resolvedResumeSessionId : string | undefined
331+
332+ if ( isResumeRequested ) {
333+ const workspaceSessions = await readWorkspaceTaskSessions ( effectiveWorkspacePath )
334+ try {
335+ resolvedResumeSessionId = resolveWorkspaceResumeSessionId ( workspaceSessions , requestedSessionId )
336+ } catch ( error ) {
337+ const message = error instanceof Error ? error . message : String ( error )
338+ console . error ( `[CLI] Error: ${ message } ` )
339+ process . exit ( 1 )
340+ }
341+ }
363342
364343 if ( ! isTuiEnabled ) {
365344 if ( ! prompt && ! useStdinPromptStream && ! isResumeRequested ) {
@@ -394,8 +373,8 @@ export async function run(promptArg: string | undefined, flagOptions: FlagOption
394373 createElement ( App , {
395374 ...extensionHostOptions ,
396375 initialPrompt : prompt ,
397- initialSessionId : requestedSessionId ,
398- continueSession : shouldContinueSession ,
376+ initialSessionId : resolvedResumeSessionId ,
377+ continueSession : false ,
399378 version : VERSION ,
400379 createExtensionHost : ( opts : ExtensionHostOptions ) => new ExtensionHost ( opts ) ,
401380 } ) ,
@@ -422,16 +401,6 @@ export async function run(promptArg: string | undefined, flagOptions: FlagOption
422401 let keepAliveInterval : NodeJS . Timeout | undefined
423402 let isShuttingDown = false
424403 let hostDisposed = false
425- let taskHistorySnapshot : HistoryItem [ ] = [ ]
426-
427- const onExtensionMessage = ( message : unknown ) => {
428- const taskHistory = extractTaskHistoryFromMessage ( message )
429- if ( taskHistory ) {
430- taskHistorySnapshot = taskHistory
431- }
432- }
433-
434- host . on ( "extensionWebviewMessage" , onExtensionMessage )
435404
436405 const jsonEmitter = useJsonOutput
437406 ? new JsonEventEmitter ( {
@@ -497,7 +466,6 @@ export async function run(promptArg: string | undefined, flagOptions: FlagOption
497466 }
498467
499468 hostDisposed = true
500- host . off ( "extensionWebviewMessage" , onExtensionMessage )
501469 jsonEmitter ?. detach ( )
502470 await host . dispose ( )
503471 }
@@ -594,22 +562,7 @@ export async function run(promptArg: string | undefined, flagOptions: FlagOption
594562 }
595563
596564 if ( isResumeRequested ) {
597- const resolvedSessionId =
598- requestedSessionId ||
599- getMostRecentTaskIdInWorkspace ( taskHistorySnapshot , effectiveWorkspacePath )
600-
601- if ( requestedSessionId && taskHistorySnapshot . length > 0 ) {
602- const hasRequestedTask = taskHistorySnapshot . some ( ( item ) => item . id === requestedSessionId )
603- if ( ! hasRequestedTask ) {
604- throw new Error ( `Session not found in task history: ${ requestedSessionId } ` )
605- }
606- }
607-
608- if ( ! resolvedSessionId ) {
609- throw new Error ( "No previous tasks found to continue in this workspace." )
610- }
611-
612- await bootstrapResumeForStdinStream ( host , resolvedSessionId )
565+ await bootstrapResumeForStdinStream ( host , resolvedResumeSessionId ! )
613566 }
614567
615568 await runStdinStreamMode ( {
@@ -621,22 +574,7 @@ export async function run(promptArg: string | undefined, flagOptions: FlagOption
621574 } )
622575 } else {
623576 if ( isResumeRequested ) {
624- const resolvedSessionId =
625- requestedSessionId ||
626- getMostRecentTaskIdInWorkspace ( taskHistorySnapshot , effectiveWorkspacePath )
627-
628- if ( requestedSessionId && taskHistorySnapshot . length > 0 ) {
629- const hasRequestedTask = taskHistorySnapshot . some ( ( item ) => item . id === requestedSessionId )
630- if ( ! hasRequestedTask ) {
631- throw new Error ( `Session not found in task history: ${ requestedSessionId } ` )
632- }
633- }
634-
635- if ( ! resolvedSessionId ) {
636- throw new Error ( "No previous tasks found to continue in this workspace." )
637- }
638-
639- await host . resumeTask ( resolvedSessionId )
577+ await host . resumeTask ( resolvedResumeSessionId ! )
640578 } else {
641579 await host . runTask ( prompt ! )
642580 }
0 commit comments