( suggestionText: string, context: REPLHookContext, setAppState: (f: (prev: AppState) => AppState) => void, isPipelined = false, cacheSafeParams?: CacheSafeParams, )
| 400 | } |
| 401 | |
| 402 | export async function startSpeculation( |
| 403 | suggestionText: string, |
| 404 | context: REPLHookContext, |
| 405 | setAppState: (f: (prev: AppState) => AppState) => void, |
| 406 | isPipelined = false, |
| 407 | cacheSafeParams?: CacheSafeParams, |
| 408 | ): Promise<void> { |
| 409 | if (!isSpeculationEnabled()) return |
| 410 | |
| 411 | // Abort any existing speculation before starting a new one |
| 412 | abortSpeculation(setAppState) |
| 413 | |
| 414 | const id = randomUUID().slice(0, 8) |
| 415 | |
| 416 | const abortController = createChildAbortController( |
| 417 | context.toolUseContext.abortController, |
| 418 | ) |
| 419 | |
| 420 | if (abortController.signal.aborted) return |
| 421 | |
| 422 | const startTime = Date.now() |
| 423 | const messagesRef = { current: [] as Message[] } |
| 424 | const writtenPathsRef = { current: new Set<string>() } |
| 425 | const overlayPath = getOverlayPath(id) |
| 426 | const cwd = getCwdState() |
| 427 | |
| 428 | try { |
| 429 | await mkdir(overlayPath, { recursive: true }) |
| 430 | } catch { |
| 431 | logForDebugging('[Speculation] Failed to create overlay directory') |
| 432 | return |
| 433 | } |
| 434 | |
| 435 | const contextRef = { current: context } |
| 436 | |
| 437 | setAppState(prev => ({ |
| 438 | ...prev, |
| 439 | speculation: { |
| 440 | status: 'active', |
| 441 | id, |
| 442 | abort: () => abortController.abort(), |
| 443 | startTime, |
| 444 | messagesRef, |
| 445 | writtenPathsRef, |
| 446 | boundary: null, |
| 447 | suggestionLength: suggestionText.length, |
| 448 | toolUseCount: 0, |
| 449 | isPipelined, |
| 450 | contextRef, |
| 451 | }, |
| 452 | })) |
| 453 | |
| 454 | logForDebugging(`[Speculation] Starting speculation ${id}`) |
| 455 | |
| 456 | try { |
| 457 | const result = await runForkedAgent({ |
| 458 | promptMessages: [createUserMessage({ content: suggestionText })], |
| 459 | cacheSafeParams: cacheSafeParams ?? createCacheSafeParams(context), |
no test coverage detected