MCPcopy Create free account
hub / github.com/tiann/hapi / ensureThreadForGoal

Method ensureThreadForGoal

cli/src/codex/codexRemoteLauncher.ts:2731–2793  ·  view source on GitHub ↗
(mode: EnhancedMode)

Source from the content-addressed store, hash-verified

2729 };
2730
2731 const ensureThreadForGoal = async (mode: EnhancedMode): Promise<string | null> => {
2732 if (this.currentThreadId && this.currentThreadId !== invalidThreadId) {
2733 hasThread = true;
2734 return this.currentThreadId;
2735 }
2736
2737 const resumeCandidate = session.sessionId && session.sessionId !== invalidThreadId
2738 ? session.sessionId
2739 : null;
2740 if (resumeCandidate) {
2741 const threadParams = buildThreadStartParams({
2742 cwd: session.path,
2743 mode,
2744 mcpServers,
2745 cliOverrides: session.codexCliOverrides
2746 });
2747 try {
2748 const resumeResponse = await appServerClient.resumeThread({
2749 threadId: resumeCandidate,
2750 ...threadParams
2751 }, {
2752 signal: this.abortController.signal
2753 });
2754 const resumeRecord = asRecord(resumeResponse);
2755 const resumeThread = resumeRecord ? asRecord(resumeRecord.thread) : null;
2756 const threadId = asString(resumeThread?.id) ?? resumeCandidate;
2757 applyResolvedModel(resumeRecord?.model);
2758 this.currentThreadId = threadId;
2759 session.onSessionFound(threadId);
2760 hasThread = true;
2761 return threadId;
2762 } catch (error) {
2763 logger.warn(`[Codex] Failed to resume app-server thread ${resumeCandidate} for /goal`, error);
2764 sendVisibleStatus(`Goal failed: Codex conversation ${resumeCandidate} could not be resumed`);
2765 return null;
2766 }
2767 }
2768
2769 if (!hasThread) {
2770 const threadParams = buildThreadStartParams({
2771 cwd: session.path,
2772 mode,
2773 mcpServers,
2774 cliOverrides: session.codexCliOverrides
2775 });
2776 const threadResponse = await appServerClient.startThread(threadParams, {
2777 signal: this.abortController.signal
2778 });
2779 const threadRecord = asRecord(threadResponse);
2780 const thread = threadRecord ? asRecord(threadRecord.thread) : null;
2781 const threadId = asString(thread?.id);
2782 applyResolvedModel(threadRecord?.model);
2783 if (!threadId) {
2784 throw new Error('app-server thread/start did not return thread.id');
2785 }
2786 this.currentThreadId = threadId;
2787 session.onSessionFound(threadId);
2788 hasThread = true;

Callers

nothing calls this directly

Calls 6

buildThreadStartParamsFunction · 0.90
warnMethod · 0.80
asRecordFunction · 0.70
asStringFunction · 0.50
resumeThreadMethod · 0.45
startThreadMethod · 0.45

Tested by

no test coverage detected