Repository navigation
Expand file tree
/
Copy pathnewSessionForm.ts
More file actions
205 lines (193 loc) · 6.94 KB
/
Copy pathnewSessionForm.ts
File metadata and controls
205 lines (193 loc) · 6.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import type { Dispatch, RefObject, SetStateAction } from "react";
import {
isMobileAuthOwnerCurrent,
type MobileAuthOwnerGeneration,
} from "@/auth/authOwnerGeneration";
import type { RemoteSerializedAttachment } from "@/session/types";
import {
DEFAULT_NEW_SESSION_DRAFT,
type NewSessionDraft,
type NewSessionRemoteAgentPick,
} from "./newSession";
import { isRemoteTaskSuggestionId } from "./remoteTaskSuggestionsModel";
import { appendComposerPluginTrigger } from "./composerPlugins";
import {
composerDocumentProjectedText,
textComposerDocument,
} from "./composerDocument";
export function readNewSessionRouteString(value: unknown): string | null {
if (typeof value === "string" && value.length > 0) return value;
if (
Array.isArray(value) &&
typeof value[0] === "string" &&
value[0].length > 0
)
return value[0];
return null;
}
export function createInitialNewSessionDraft(input: {
initialWorkingDir: string | null;
visualInitialDraft: string | null;
routeDraft: unknown;
suggestion: unknown;
t: (key: string) => string;
}): NewSessionDraft {
return {
...DEFAULT_NEW_SESSION_DRAFT,
firstMessage:
input.visualInitialDraft ??
readNewSessionRouteString(input.routeDraft) ??
(isRemoteTaskSuggestionId(input.suggestion)
? input.t(
`devices.list.taskSuggestions.items.${input.suggestion}.prompt`,
)
: DEFAULT_NEW_SESSION_DRAFT.firstMessage),
workspaceKind: input.initialWorkingDir ? "project" : "dialogue",
workingDir: input.initialWorkingDir ?? "",
};
}
export type NewSessionSelection = { start: number; end: number };
export type NewSessionRemoteAgentChoice = {
controlledDeviceId: string;
pick: NewSessionRemoteAgentPick;
restored?: true;
} | null;
export interface NewSessionDraftRestoreTarget {
userTouchedWorkspaceRef: RefObject<boolean>;
userTouchedRuntimeRef: RefObject<boolean>;
appliedPermissionMemoryRef: RefObject<boolean>;
runtimeActionSeqRef: RefObject<number>;
firstMessageRef: RefObject<string>;
firstMessageSelectionRef: RefObject<NewSessionSelection>;
attachmentsRef: RefObject<RemoteSerializedAttachment[]>;
prePlanPermissionModeRef: RefObject<string | null>;
setRemoteAgentChoice: (choice: NewSessionRemoteAgentChoice) => void;
setFirstMessageSelection: (selection: NewSessionSelection) => void;
setAttachments: (files: RemoteSerializedAttachment[]) => void;
setPlanModeDraftOn: (enabled: boolean) => void;
setDraft: (draft: NewSessionDraft) => void;
}
/** Restore refs and visible state together, before composer actions can read the draft. */
export function restoreNewSessionDraft(
target: NewSessionDraftRestoreTarget,
saved: NewSessionDraft,
files: RemoteSerializedAttachment[],
plan?: { enabled: boolean; restorePermissionMode: string | null },
controlledDeviceId?: string,
): void {
const { agentDeviceId, ...recovered } = saved;
target.setRemoteAgentChoice(
agentDeviceId && controlledDeviceId
? {
controlledDeviceId,
pick: {
deviceId: agentDeviceId,
agentKind: saved.agentKind,
model: saved.model,
providerId: saved.providerId,
effort: saved.effort,
fastMode: saved.fastMode,
},
}
: null,
);
target.userTouchedWorkspaceRef.current = true;
target.userTouchedRuntimeRef.current = true;
target.appliedPermissionMemoryRef.current = true;
target.runtimeActionSeqRef.current += 1;
target.firstMessageRef.current = recovered.firstMessage;
const selection = {
start: recovered.firstMessage.length,
end: recovered.firstMessage.length,
};
target.firstMessageSelectionRef.current = selection;
target.setFirstMessageSelection(selection);
target.attachmentsRef.current = files;
target.setAttachments(files);
if (plan) {
target.setPlanModeDraftOn(plan.enabled);
target.prePlanPermissionModeRef.current = plan.restorePermissionMode;
}
target.setDraft(recovered);
}
/** A share claimed by another screen must not suppress this form's draft recovery. */
export function markNewSessionIncomingShare(
batch: { destination?: string } | null | undefined,
received: RefObject<boolean>,
): void {
if (batch?.destination === "new") received.current = true;
}
/** Both normal and goal creation keep the same account-generation boundary. */
export function captureNewSessionOwner(
owner: MobileAuthOwnerGeneration,
accountId: string,
) {
return {
worktreeAccountId: owner.accountId,
isCurrentOwner: () =>
owner.accountId === accountId && isMobileAuthOwnerCurrent(owner),
};
}
export interface NewSessionDraftEditTarget {
firstMessageRef: RefObject<string>;
creatingRef: RefObject<boolean>;
editRecoveredDraft: (patch: { firstMessage: string }) => unknown;
voiceDictionaryLearningTrackerRef: RefObject<{
inspectDraft(text: string): void;
} | null>;
setDraft: Dispatch<SetStateAction<NewSessionDraft>>;
}
/** Text input, system shares and plugin insertion all keep durable edits and refs in sync. */
export function patchNewSessionDraft(
target: NewSessionDraftEditTarget,
patch: Partial<NewSessionDraft>,
): void {
if (patch.firstMessage !== undefined) {
target.firstMessageRef.current = patch.firstMessage;
if (!target.creatingRef.current)
target.editRecoveredDraft({ firstMessage: patch.firstMessage });
target.voiceDictionaryLearningTrackerRef.current?.inspectDraft(
patch.firstMessage,
);
}
target.setDraft((current) => ({ ...current, ...patch }));
}
export function setNewSessionFirstMessage(
target: NewSessionDraftEditTarget,
next: SetStateAction<string>,
): void {
const value =
typeof next === "function" ? next(target.firstMessageRef.current) : next;
// Keep the ordering of the text-input path: publish the draft before dictionary inspection.
target.firstMessageRef.current = value;
if (!target.creatingRef.current)
target.editRecoveredDraft({ firstMessage: value });
target.setDraft((current) => ({ ...current, firstMessage: value }));
target.voiceDictionaryLearningTrackerRef.current?.inspectDraft(value);
}
export function appendNewSessionSharedText(
target: NewSessionDraftEditTarget,
text: string,
): void {
setNewSessionFirstMessage(target, (current) =>
[current, text].filter(Boolean).join("\n\n"),
);
}
export function openNewSessionPluginPalette(target: {
firstMessageRef: RefObject<string>;
patchDraft: (patch: Partial<NewSessionDraft>) => void;
firstMessageSelectionRef: RefObject<NewSessionSelection>;
setFirstMessageSelection: (selection: NewSessionSelection) => void;
focusComposer: () => void;
}): void {
const next = composerDocumentProjectedText(
appendComposerPluginTrigger(
textComposerDocument(target.firstMessageRef.current),
),
);
target.patchDraft({ firstMessage: next });
const selection = { start: next.length, end: next.length };
target.firstMessageSelectionRef.current = selection;
target.setFirstMessageSelection(selection);
target.focusComposer();
}