-
Notifications
You must be signed in to change notification settings - Fork 0
fix(git): materialize draft session for generate #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -29,6 +29,7 @@ import { markPendingUserSendAnimation } from "@/lib/userSendAnimation" | |||||||||||||||||||||||||||||||
| import { flattenAssistantTextParts } from "@/lib/messages/messageText" | ||||||||||||||||||||||||||||||||
| import { composeForkSessionMessage } from "@/lib/messages/executionMeta" | ||||||||||||||||||||||||||||||||
| import { waitForPendingDraftWorktreeRequest } from "@/lib/worktrees/pendingDraftWorktree" | ||||||||||||||||||||||||||||||||
| import { waitForWorktreeBootstrap } from "@/lib/worktrees/worktreeBootstrap" | ||||||||||||||||||||||||||||||||
| import { resolveProjectForSessionDirectory } from "@/lib/projectResolution" | ||||||||||||||||||||||||||||||||
| import { | ||||||||||||||||||||||||||||||||
| getSyncSessions, | ||||||||||||||||||||||||||||||||
|
|
@@ -401,6 +402,84 @@ const writeRuntimeSessionMemory = (key: string, patch: Partial<RuntimeSessionMem | |||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| type MaterializedDraftSession = { | ||||||||||||||||||||||||||||||||
| sessionId: string | ||||||||||||||||||||||||||||||||
| directory: string | null | ||||||||||||||||||||||||||||||||
| agent?: string | ||||||||||||||||||||||||||||||||
| syntheticParts?: SyntheticContextPart[] | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| export async function materializeOpenDraftSession(selection: { | ||||||||||||||||||||||||||||||||
| providerID: string | ||||||||||||||||||||||||||||||||
| modelID: string | ||||||||||||||||||||||||||||||||
| agent?: string | ||||||||||||||||||||||||||||||||
| variant?: string | ||||||||||||||||||||||||||||||||
| }): Promise<MaterializedDraftSession | null> { | ||||||||||||||||||||||||||||||||
| const store = useSessionUIStore.getState() | ||||||||||||||||||||||||||||||||
| const draft = store.newSessionDraft | ||||||||||||||||||||||||||||||||
| if (!draft?.open) return null | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const trimmedAgent = typeof selection.agent === "string" && selection.agent.trim().length > 0 | ||||||||||||||||||||||||||||||||
| ? selection.agent.trim() | ||||||||||||||||||||||||||||||||
| : undefined | ||||||||||||||||||||||||||||||||
| const draftTargetFolderId = draft.targetFolderId | ||||||||||||||||||||||||||||||||
| let draftDirectoryOverride = draft.bootstrapPendingDirectory ?? draft.directoryOverride ?? null | ||||||||||||||||||||||||||||||||
| const draftProjectId = draft.selectedProjectId ?? null | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (draft.pendingWorktreeRequestId) { | ||||||||||||||||||||||||||||||||
| draftDirectoryOverride = await waitForPendingDraftWorktreeRequest(draft.pendingWorktreeRequestId) | ||||||||||||||||||||||||||||||||
| store.resolvePendingDraftWorktreeTarget(draft.pendingWorktreeRequestId, draftDirectoryOverride) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (draftDirectoryOverride) { | ||||||||||||||||||||||||||||||||
| await waitForWorktreeBootstrap(draftDirectoryOverride) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const created = await store.createSession(draft.title, draftDirectoryOverride, draft.parentID ?? null) | ||||||||||||||||||||||||||||||||
| if (!created?.id) throw new Error("Failed to create session") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| persistDraftTarget({ | ||||||||||||||||||||||||||||||||
| projectId: draftProjectId, | ||||||||||||||||||||||||||||||||
| directory: normalizePath(draftDirectoryOverride ?? created.directory ?? null), | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const draftSyntheticParts = draft.syntheticParts | ||||||||||||||||||||||||||||||||
| const createdDirectory = normalizePath(draftDirectoryOverride ?? created.directory ?? null) | ||||||||||||||||||||||||||||||||
|
Comment on lines
+441
to
+447
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of accessing
Suggested change
|
||||||||||||||||||||||||||||||||
| const configState = useConfigStore.getState() | ||||||||||||||||||||||||||||||||
| void activateConfigForDirectory(createdDirectory).catch((error) => { | ||||||||||||||||||||||||||||||||
| console.warn("Failed to activate directory after creating session:", error) | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const effectiveDraftAgent = trimmedAgent ?? configState.currentAgentName | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| useSelectionStore.getState().saveSessionModelSelection(created.id, selection.providerID, selection.modelID) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (effectiveDraftAgent) { | ||||||||||||||||||||||||||||||||
| useSelectionStore.getState().saveSessionAgentSelection(created.id, effectiveDraftAgent) | ||||||||||||||||||||||||||||||||
| useSelectionStore.getState().saveAgentModelForSession(created.id, effectiveDraftAgent, selection.providerID, selection.modelID) | ||||||||||||||||||||||||||||||||
| useSelectionStore.getState().saveAgentModelVariantForSession(created.id, effectiveDraftAgent, selection.providerID, selection.modelID, selection.variant) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| store.initializeNewOpenChamberSession(created.id, configState.agents ?? []) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| store.closeNewSessionDraft() | ||||||||||||||||||||||||||||||||
| store.setCurrentSession(created.id, createdDirectory) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (draftTargetFolderId) { | ||||||||||||||||||||||||||||||||
| const scopeKey = draftDirectoryOverride || created.directory || null | ||||||||||||||||||||||||||||||||
| if (scopeKey) { | ||||||||||||||||||||||||||||||||
| useSessionFoldersStore.getState().addSessionToFolder(scopeKey, draftTargetFolderId, created.id) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+468
to
+473
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can simplify this block by reusing the already resolved and normalized if (draftTargetFolderId && createdDirectory) {
useSessionFoldersStore.getState().addSessionToFolder(createdDirectory, draftTargetFolderId, created.id)
} |
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| return { | ||||||||||||||||||||||||||||||||
| sessionId: created.id, | ||||||||||||||||||||||||||||||||
| directory: createdDirectory, | ||||||||||||||||||||||||||||||||
| agent: effectiveDraftAgent, | ||||||||||||||||||||||||||||||||
| syntheticParts: draftSyntheticParts, | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
| // Store | ||||||||||||||||||||||||||||||||
| // --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||
|
|
@@ -913,59 +992,21 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({ | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // ---- New session from draft ---- | ||||||||||||||||||||||||||||||||
| if (!options?.sessionId && draft?.open) { | ||||||||||||||||||||||||||||||||
| const draftTargetFolderId = draft.targetFolderId | ||||||||||||||||||||||||||||||||
| let draftDirectoryOverride = draft.bootstrapPendingDirectory ?? draft.directoryOverride ?? null | ||||||||||||||||||||||||||||||||
| const draftProjectId = draft.selectedProjectId ?? null | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (draft.pendingWorktreeRequestId) { | ||||||||||||||||||||||||||||||||
| draftDirectoryOverride = await waitForPendingDraftWorktreeRequest(draft.pendingWorktreeRequestId) | ||||||||||||||||||||||||||||||||
| get().resolvePendingDraftWorktreeTarget(draft.pendingWorktreeRequestId, draftDirectoryOverride) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const created = await get().createSession(draft.title, draftDirectoryOverride, draft.parentID ?? null) | ||||||||||||||||||||||||||||||||
| if (!created?.id) throw new Error("Failed to create session") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| persistDraftTarget({ | ||||||||||||||||||||||||||||||||
| projectId: draftProjectId, | ||||||||||||||||||||||||||||||||
| directory: normalizePath(draftDirectoryOverride ?? created.directory ?? null), | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const draftSyntheticParts = draft.syntheticParts | ||||||||||||||||||||||||||||||||
| const createdDirectory = normalizePath(draftDirectoryOverride ?? created.directory ?? null) | ||||||||||||||||||||||||||||||||
| const configState = useConfigStore.getState() | ||||||||||||||||||||||||||||||||
| void activateConfigForDirectory(createdDirectory).catch((error) => { | ||||||||||||||||||||||||||||||||
| console.warn("Failed to activate directory after creating session:", error) | ||||||||||||||||||||||||||||||||
| const createdDraftSession = await materializeOpenDraftSession({ | ||||||||||||||||||||||||||||||||
| providerID, | ||||||||||||||||||||||||||||||||
| modelID, | ||||||||||||||||||||||||||||||||
| agent: trimmedAgent, | ||||||||||||||||||||||||||||||||
| variant, | ||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||
| if (!createdDraftSession) throw new Error("Failed to create session") | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const effectiveDraftAgent = trimmedAgent ?? configState.currentAgentName | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| useSelectionStore.getState().saveSessionModelSelection(created.id, providerID, modelID) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (effectiveDraftAgent) { | ||||||||||||||||||||||||||||||||
| useSelectionStore.getState().saveSessionAgentSelection(created.id, effectiveDraftAgent) | ||||||||||||||||||||||||||||||||
| useSelectionStore.getState().saveAgentModelForSession(created.id, effectiveDraftAgent, providerID, modelID) | ||||||||||||||||||||||||||||||||
| useSelectionStore.getState().saveAgentModelVariantForSession(created.id, effectiveDraftAgent, providerID, modelID, variant) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| get().initializeNewOpenChamberSession(created.id, configState.agents ?? []) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| get().closeNewSessionDraft() | ||||||||||||||||||||||||||||||||
| get().setCurrentSession(created.id, createdDirectory) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (draftTargetFolderId) { | ||||||||||||||||||||||||||||||||
| const scopeKey = draftDirectoryOverride || created.directory || null | ||||||||||||||||||||||||||||||||
| if (scopeKey) { | ||||||||||||||||||||||||||||||||
| useSessionFoldersStore.getState().addSessionToFolder(scopeKey, draftTargetFolderId, created.id) | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const mergedAdditionalParts = draftSyntheticParts?.length | ||||||||||||||||||||||||||||||||
| ? [...(additionalParts || []), ...draftSyntheticParts] | ||||||||||||||||||||||||||||||||
| const mergedAdditionalParts = createdDraftSession.syntheticParts?.length | ||||||||||||||||||||||||||||||||
| ? [...(additionalParts || []), ...createdDraftSession.syntheticParts] | ||||||||||||||||||||||||||||||||
| : additionalParts | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| notifyMessageSent(created.id) | ||||||||||||||||||||||||||||||||
| notifyMessageSent(createdDraftSession.sessionId) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| markPendingUserSendAnimation(created.id) | ||||||||||||||||||||||||||||||||
| markPendingUserSendAnimation(createdDraftSession.sessionId) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| const files = attachments?.map((a) => ({ | ||||||||||||||||||||||||||||||||
| type: "file" as const, | ||||||||||||||||||||||||||||||||
|
|
@@ -975,12 +1016,12 @@ export const useSessionUIStore = create<SessionUIState>()((set, get) => ({ | |||||||||||||||||||||||||||||||
| })) | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| await routeMessage({ | ||||||||||||||||||||||||||||||||
| sessionId: created.id, | ||||||||||||||||||||||||||||||||
| directory: createdDirectory, | ||||||||||||||||||||||||||||||||
| sessionId: createdDraftSession.sessionId, | ||||||||||||||||||||||||||||||||
| directory: createdDraftSession.directory, | ||||||||||||||||||||||||||||||||
| content, | ||||||||||||||||||||||||||||||||
| providerID, | ||||||||||||||||||||||||||||||||
| modelID, | ||||||||||||||||||||||||||||||||
| agent: effectiveDraftAgent, | ||||||||||||||||||||||||||||||||
| agent: createdDraftSession.agent, | ||||||||||||||||||||||||||||||||
| agentMentionName, | ||||||||||||||||||||||||||||||||
| variant, | ||||||||||||||||||||||||||||||||
| inputMode, | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better type safety and consistency, use optional chaining on
lastChoice.modelIDas well. This prevents potential static analysis or TypeScript compiler warnings under strict null-checking configurations.