wave-code 0.13.6 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/BackgroundTaskManager.d.ts.map +1 -1
- package/dist/components/BackgroundTaskManager.js +36 -25
- package/dist/components/ChatInterface.d.ts.map +1 -1
- package/dist/components/ChatInterface.js +1 -1
- package/dist/components/ConfirmationSelector.d.ts +0 -1
- package/dist/components/ConfirmationSelector.d.ts.map +1 -1
- package/dist/components/ConfirmationSelector.js +111 -259
- package/dist/components/InputBox.d.ts.map +1 -1
- package/dist/components/InputBox.js +2 -2
- package/dist/components/MarketplaceAddForm.d.ts.map +1 -1
- package/dist/components/MarketplaceAddForm.js +48 -17
- package/dist/components/MarketplaceDetail.d.ts.map +1 -1
- package/dist/components/MarketplaceDetail.js +2 -1
- package/dist/components/MarketplaceList.d.ts.map +1 -1
- package/dist/components/MarketplaceList.js +2 -1
- package/dist/components/McpManager.d.ts.map +1 -1
- package/dist/components/McpManager.js +22 -27
- package/dist/components/MessageList.d.ts.map +1 -1
- package/dist/components/MessageList.js +4 -6
- package/dist/components/PluginDetail.d.ts.map +1 -1
- package/dist/components/PluginDetail.js +22 -22
- package/dist/components/PluginManagerShell.d.ts +1 -0
- package/dist/components/PluginManagerShell.d.ts.map +1 -1
- package/dist/components/PluginManagerShell.js +2 -2
- package/dist/components/PluginManagerTypes.d.ts +2 -2
- package/dist/components/PluginManagerTypes.d.ts.map +1 -1
- package/dist/components/TaskNotificationMessage.js +1 -1
- package/dist/contexts/useChat.d.ts +1 -0
- package/dist/contexts/useChat.d.ts.map +1 -1
- package/dist/contexts/useChat.js +128 -102
- package/dist/hooks/usePluginManager.d.ts +3 -1
- package/dist/hooks/usePluginManager.d.ts.map +1 -1
- package/dist/hooks/usePluginManager.js +16 -7
- package/dist/managers/inputHandlers.d.ts.map +1 -1
- package/dist/managers/inputHandlers.js +6 -9
- package/dist/managers/inputReducer.d.ts +6 -0
- package/dist/managers/inputReducer.d.ts.map +1 -1
- package/dist/managers/inputReducer.js +15 -0
- package/dist/reducers/backgroundTaskManagerReducer.d.ts +43 -0
- package/dist/reducers/backgroundTaskManagerReducer.d.ts.map +1 -0
- package/dist/reducers/backgroundTaskManagerReducer.js +23 -0
- package/dist/reducers/confirmationReducer.d.ts +26 -0
- package/dist/reducers/confirmationReducer.d.ts.map +1 -0
- package/dist/reducers/confirmationReducer.js +45 -0
- package/dist/reducers/marketplaceAddFormReducer.d.ts +24 -0
- package/dist/reducers/marketplaceAddFormReducer.d.ts.map +1 -0
- package/dist/reducers/marketplaceAddFormReducer.js +18 -0
- package/dist/reducers/mcpManagerReducer.d.ts +16 -0
- package/dist/reducers/mcpManagerReducer.d.ts.map +1 -0
- package/dist/reducers/mcpManagerReducer.js +15 -0
- package/dist/reducers/pluginDetailReducer.d.ts +25 -0
- package/dist/reducers/pluginDetailReducer.d.ts.map +1 -0
- package/dist/reducers/pluginDetailReducer.js +38 -0
- package/dist/reducers/questionReducer.d.ts +69 -0
- package/dist/reducers/questionReducer.d.ts.map +1 -0
- package/dist/reducers/questionReducer.js +195 -0
- package/package.json +2 -2
- package/src/components/BackgroundTaskManager.tsx +32 -34
- package/src/components/ChatInterface.tsx +0 -1
- package/src/components/ConfirmationSelector.tsx +122 -304
- package/src/components/InputBox.tsx +7 -1
- package/src/components/MarketplaceAddForm.tsx +76 -22
- package/src/components/MarketplaceDetail.tsx +4 -0
- package/src/components/MarketplaceList.tsx +4 -0
- package/src/components/McpManager.tsx +30 -34
- package/src/components/MessageList.tsx +10 -14
- package/src/components/PluginDetail.tsx +25 -33
- package/src/components/PluginManagerShell.tsx +3 -2
- package/src/components/PluginManagerTypes.ts +8 -2
- package/src/components/TaskNotificationMessage.tsx +1 -1
- package/src/contexts/useChat.tsx +57 -24
- package/src/hooks/usePluginManager.ts +18 -7
- package/src/managers/inputHandlers.ts +6 -8
- package/src/managers/inputReducer.ts +19 -0
- package/src/reducers/backgroundTaskManagerReducer.ts +60 -0
- package/src/reducers/confirmationReducer.ts +74 -0
- package/src/reducers/marketplaceAddFormReducer.ts +35 -0
- package/src/reducers/mcpManagerReducer.ts +31 -0
- package/src/reducers/pluginDetailReducer.ts +58 -0
- package/src/reducers/questionReducer.ts +251 -0
|
@@ -11,7 +11,9 @@ import {
|
|
|
11
11
|
PluginManagerContextType,
|
|
12
12
|
} from "../components/PluginManagerTypes.js";
|
|
13
13
|
|
|
14
|
-
export function usePluginManager(
|
|
14
|
+
export function usePluginManager(options?: {
|
|
15
|
+
onPluginInstalled?: () => void;
|
|
16
|
+
}): PluginManagerContextType {
|
|
15
17
|
const [state, setState] = useState<PluginManagerState>({
|
|
16
18
|
currentView: "DISCOVER",
|
|
17
19
|
selectedId: null,
|
|
@@ -141,16 +143,20 @@ export function usePluginManager(): PluginManagerContextType {
|
|
|
141
143
|
}, []);
|
|
142
144
|
|
|
143
145
|
const addMarketplace = useCallback(
|
|
144
|
-
async (source: string) => {
|
|
146
|
+
async (source: string, scope: "user" | "project" | "local" = "user") => {
|
|
145
147
|
clearPluginFeedback();
|
|
146
148
|
setState((prev: PluginManagerState) => ({
|
|
147
149
|
...prev,
|
|
148
150
|
isLoading: true,
|
|
149
151
|
}));
|
|
150
152
|
try {
|
|
151
|
-
await pluginCore.addMarketplace(source);
|
|
153
|
+
await pluginCore.addMarketplace(source, scope);
|
|
152
154
|
await refresh();
|
|
153
|
-
setSuccessMessage(`Marketplace added successfully`);
|
|
155
|
+
setSuccessMessage(`Marketplace added successfully (${scope} scope)`);
|
|
156
|
+
setState((prev: PluginManagerState) => ({
|
|
157
|
+
...prev,
|
|
158
|
+
currentView: "MARKETPLACES",
|
|
159
|
+
}));
|
|
154
160
|
} catch (error) {
|
|
155
161
|
setState((prev: PluginManagerState) => ({
|
|
156
162
|
...prev,
|
|
@@ -163,16 +169,20 @@ export function usePluginManager(): PluginManagerContextType {
|
|
|
163
169
|
);
|
|
164
170
|
|
|
165
171
|
const removeMarketplace = useCallback(
|
|
166
|
-
async (name: string) => {
|
|
172
|
+
async (name: string, scope?: "user" | "project" | "local") => {
|
|
167
173
|
clearPluginFeedback();
|
|
168
174
|
setState((prev: PluginManagerState) => ({
|
|
169
175
|
...prev,
|
|
170
176
|
isLoading: true,
|
|
171
177
|
}));
|
|
172
178
|
try {
|
|
173
|
-
await pluginCore.removeMarketplace(name);
|
|
179
|
+
await pluginCore.removeMarketplace(name, scope);
|
|
174
180
|
await refresh();
|
|
175
181
|
setSuccessMessage(`Marketplace '${name}' removed successfully`);
|
|
182
|
+
setState((prev: PluginManagerState) => ({
|
|
183
|
+
...prev,
|
|
184
|
+
currentView: "MARKETPLACES",
|
|
185
|
+
}));
|
|
176
186
|
} catch (error) {
|
|
177
187
|
setState((prev: PluginManagerState) => ({
|
|
178
188
|
...prev,
|
|
@@ -222,6 +232,7 @@ export function usePluginManager(): PluginManagerContextType {
|
|
|
222
232
|
await pluginCore.installPlugin(pluginId, scope);
|
|
223
233
|
await refresh();
|
|
224
234
|
setSuccessMessage(`Plugin '${name}' installed successfully`);
|
|
235
|
+
options?.onPluginInstalled?.();
|
|
225
236
|
} catch (error) {
|
|
226
237
|
setState((prev: PluginManagerState) => ({
|
|
227
238
|
...prev,
|
|
@@ -230,7 +241,7 @@ export function usePluginManager(): PluginManagerContextType {
|
|
|
230
241
|
}));
|
|
231
242
|
}
|
|
232
243
|
},
|
|
233
|
-
[pluginCore, refresh, clearPluginFeedback, setSuccessMessage],
|
|
244
|
+
[pluginCore, refresh, clearPluginFeedback, setSuccessMessage, options],
|
|
234
245
|
);
|
|
235
246
|
|
|
236
247
|
const uninstallPlugin = useCallback(
|
|
@@ -297,14 +297,12 @@ export const handlePasteInput = (
|
|
|
297
297
|
inputString.includes("\r");
|
|
298
298
|
|
|
299
299
|
if (isPasteOperation) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
dispatch({ type: "APPEND_PASTE_BUFFER", payload: inputString });
|
|
307
|
-
}
|
|
300
|
+
// Dispatch a single action type; the reducer determines start vs append
|
|
301
|
+
// by checking pasteBuffer, avoiding stale state issues
|
|
302
|
+
dispatch({
|
|
303
|
+
type: "APPEND_PASTE_CHUNK",
|
|
304
|
+
payload: { chunk: inputString, cursorPosition: state.cursorPosition },
|
|
305
|
+
});
|
|
308
306
|
} else {
|
|
309
307
|
let char = inputString;
|
|
310
308
|
if (char === "!" && state.cursorPosition === 0) {
|
|
@@ -173,6 +173,10 @@ export type InputAction =
|
|
|
173
173
|
| { type: "CLEAR_INPUT" }
|
|
174
174
|
| { type: "START_PASTE"; payload: { buffer: string; cursorPosition: number } }
|
|
175
175
|
| { type: "APPEND_PASTE_BUFFER"; payload: string }
|
|
176
|
+
| {
|
|
177
|
+
type: "APPEND_PASTE_CHUNK";
|
|
178
|
+
payload: { chunk: string; cursorPosition: number };
|
|
179
|
+
}
|
|
176
180
|
| { type: "END_PASTE" }
|
|
177
181
|
| {
|
|
178
182
|
type: "ADD_IMAGE_AND_INSERT_PLACEHOLDER";
|
|
@@ -407,6 +411,21 @@ export function inputReducer(
|
|
|
407
411
|
cursorPosition: 0,
|
|
408
412
|
historyIndex: -1,
|
|
409
413
|
};
|
|
414
|
+
case "APPEND_PASTE_CHUNK": {
|
|
415
|
+
// The reducer determines if this is a new paste or a continuation
|
|
416
|
+
// by checking if pasteBuffer is already set. This avoids the
|
|
417
|
+
// handler needing to track isPasting state, which can be stale
|
|
418
|
+
// when multiple dispatches fire before React state updates.
|
|
419
|
+
const isNewPaste = !state.pasteBuffer;
|
|
420
|
+
return {
|
|
421
|
+
...state,
|
|
422
|
+
isPasting: true,
|
|
423
|
+
pasteBuffer: state.pasteBuffer + action.payload.chunk,
|
|
424
|
+
initialPasteCursorPosition: isNewPaste
|
|
425
|
+
? action.payload.cursorPosition
|
|
426
|
+
: state.initialPasteCursorPosition,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
410
429
|
case "START_PASTE":
|
|
411
430
|
return {
|
|
412
431
|
...state,
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
export interface Task {
|
|
2
|
+
id: string;
|
|
3
|
+
type: string;
|
|
4
|
+
description?: string;
|
|
5
|
+
status: "running" | "completed" | "failed" | "killed";
|
|
6
|
+
startTime: number;
|
|
7
|
+
exitCode?: number;
|
|
8
|
+
runtime?: number;
|
|
9
|
+
outputPath?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface DetailOutput {
|
|
13
|
+
stdout: string;
|
|
14
|
+
stderr: string;
|
|
15
|
+
status: string;
|
|
16
|
+
outputPath?: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface BackgroundTaskManagerState {
|
|
20
|
+
tasks: Task[];
|
|
21
|
+
selectedIndex: number;
|
|
22
|
+
viewMode: "list" | "detail";
|
|
23
|
+
detailTaskId: string | null;
|
|
24
|
+
detailOutput: DetailOutput | null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type BackgroundTaskManagerAction =
|
|
28
|
+
| { type: "SET_TASKS"; tasks: Task[] }
|
|
29
|
+
| { type: "SELECT_INDEX"; index: number }
|
|
30
|
+
| { type: "SET_VIEW_MODE"; mode: "list" | "detail" }
|
|
31
|
+
| { type: "SET_DETAIL_TASK_ID"; id: string | null }
|
|
32
|
+
| { type: "SET_DETAIL_OUTPUT"; output: DetailOutput | null }
|
|
33
|
+
| { type: "RESET_DETAIL" };
|
|
34
|
+
|
|
35
|
+
export function backgroundTaskManagerReducer(
|
|
36
|
+
state: BackgroundTaskManagerState,
|
|
37
|
+
action: BackgroundTaskManagerAction,
|
|
38
|
+
): BackgroundTaskManagerState {
|
|
39
|
+
switch (action.type) {
|
|
40
|
+
case "SET_TASKS":
|
|
41
|
+
return { ...state, tasks: action.tasks };
|
|
42
|
+
case "SELECT_INDEX":
|
|
43
|
+
return { ...state, selectedIndex: action.index };
|
|
44
|
+
case "SET_VIEW_MODE":
|
|
45
|
+
return { ...state, viewMode: action.mode };
|
|
46
|
+
case "SET_DETAIL_TASK_ID":
|
|
47
|
+
return { ...state, detailTaskId: action.id };
|
|
48
|
+
case "SET_DETAIL_OUTPUT":
|
|
49
|
+
return { ...state, detailOutput: action.output };
|
|
50
|
+
case "RESET_DETAIL":
|
|
51
|
+
return {
|
|
52
|
+
...state,
|
|
53
|
+
viewMode: "list",
|
|
54
|
+
detailTaskId: null,
|
|
55
|
+
detailOutput: null,
|
|
56
|
+
};
|
|
57
|
+
default:
|
|
58
|
+
return state;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import type { PermissionDecision } from "wave-agent-sdk";
|
|
2
|
+
|
|
3
|
+
export interface ConfirmationState {
|
|
4
|
+
selectedOption: "clear" | "auto" | "allow" | "alternative";
|
|
5
|
+
alternativeText: string;
|
|
6
|
+
alternativeCursorPosition: number;
|
|
7
|
+
hasUserInput: boolean;
|
|
8
|
+
decision: PermissionDecision | null;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export type ConfirmationAction =
|
|
12
|
+
| { type: "SELECT_OPTION"; option: ConfirmationState["selectedOption"] }
|
|
13
|
+
| { type: "INSERT_TEXT"; text: string }
|
|
14
|
+
| { type: "BACKSPACE" }
|
|
15
|
+
| { type: "MOVE_CURSOR_LEFT" }
|
|
16
|
+
| { type: "MOVE_CURSOR_RIGHT" }
|
|
17
|
+
| { type: "CONFIRM"; decision: PermissionDecision };
|
|
18
|
+
|
|
19
|
+
export function confirmationReducer(
|
|
20
|
+
state: ConfirmationState,
|
|
21
|
+
action: ConfirmationAction,
|
|
22
|
+
): ConfirmationState {
|
|
23
|
+
switch (action.type) {
|
|
24
|
+
case "SELECT_OPTION":
|
|
25
|
+
return { ...state, selectedOption: action.option };
|
|
26
|
+
case "INSERT_TEXT": {
|
|
27
|
+
const nextText =
|
|
28
|
+
state.alternativeText.slice(0, state.alternativeCursorPosition) +
|
|
29
|
+
action.text +
|
|
30
|
+
state.alternativeText.slice(state.alternativeCursorPosition);
|
|
31
|
+
return {
|
|
32
|
+
...state,
|
|
33
|
+
selectedOption: "alternative",
|
|
34
|
+
alternativeText: nextText,
|
|
35
|
+
alternativeCursorPosition:
|
|
36
|
+
state.alternativeCursorPosition + action.text.length,
|
|
37
|
+
hasUserInput: true,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
case "BACKSPACE": {
|
|
41
|
+
if (state.alternativeCursorPosition <= 0) return state;
|
|
42
|
+
const nextText =
|
|
43
|
+
state.alternativeText.slice(0, state.alternativeCursorPosition - 1) +
|
|
44
|
+
state.alternativeText.slice(state.alternativeCursorPosition);
|
|
45
|
+
return {
|
|
46
|
+
...state,
|
|
47
|
+
selectedOption: "alternative",
|
|
48
|
+
alternativeText: nextText,
|
|
49
|
+
alternativeCursorPosition: state.alternativeCursorPosition - 1,
|
|
50
|
+
hasUserInput: nextText.length > 0,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
case "MOVE_CURSOR_LEFT":
|
|
54
|
+
return {
|
|
55
|
+
...state,
|
|
56
|
+
alternativeCursorPosition: Math.max(
|
|
57
|
+
0,
|
|
58
|
+
state.alternativeCursorPosition - 1,
|
|
59
|
+
),
|
|
60
|
+
};
|
|
61
|
+
case "MOVE_CURSOR_RIGHT":
|
|
62
|
+
return {
|
|
63
|
+
...state,
|
|
64
|
+
alternativeCursorPosition: Math.min(
|
|
65
|
+
state.alternativeText.length,
|
|
66
|
+
state.alternativeCursorPosition + 1,
|
|
67
|
+
),
|
|
68
|
+
};
|
|
69
|
+
case "CONFIRM":
|
|
70
|
+
return { ...state, decision: action.decision };
|
|
71
|
+
default:
|
|
72
|
+
return state;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface MarketplaceAddFormState {
|
|
2
|
+
source: string;
|
|
3
|
+
scopeIndex: number;
|
|
4
|
+
step: "source" | "scope";
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export type MarketplaceAddFormAction =
|
|
8
|
+
| { type: "SET_SOURCE"; source: string }
|
|
9
|
+
| { type: "SET_SCOPE_INDEX"; index: number }
|
|
10
|
+
| { type: "SET_STEP"; step: "source" | "scope" }
|
|
11
|
+
| { type: "INSERT_CHAR"; text: string }
|
|
12
|
+
| { type: "DELETE_CHAR" }
|
|
13
|
+
| { type: "BACK_TO_SOURCE" };
|
|
14
|
+
|
|
15
|
+
export function marketplaceAddFormReducer(
|
|
16
|
+
state: MarketplaceAddFormState,
|
|
17
|
+
action: MarketplaceAddFormAction,
|
|
18
|
+
): MarketplaceAddFormState {
|
|
19
|
+
switch (action.type) {
|
|
20
|
+
case "SET_SOURCE":
|
|
21
|
+
return { ...state, source: action.source };
|
|
22
|
+
case "SET_SCOPE_INDEX":
|
|
23
|
+
return { ...state, scopeIndex: action.index };
|
|
24
|
+
case "SET_STEP":
|
|
25
|
+
return { ...state, step: action.step };
|
|
26
|
+
case "INSERT_CHAR":
|
|
27
|
+
return { ...state, source: state.source + action.text };
|
|
28
|
+
case "DELETE_CHAR":
|
|
29
|
+
return { ...state, source: state.source.slice(0, -1) };
|
|
30
|
+
case "BACK_TO_SOURCE":
|
|
31
|
+
return { ...state, step: "source" };
|
|
32
|
+
default:
|
|
33
|
+
return state;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export interface McpManagerState {
|
|
2
|
+
selectedIndex: number;
|
|
3
|
+
viewMode: "list" | "detail";
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type McpManagerAction =
|
|
7
|
+
| { type: "MOVE_UP"; serverCount: number }
|
|
8
|
+
| { type: "MOVE_DOWN"; serverCount: number }
|
|
9
|
+
| { type: "SET_VIEW_MODE"; viewMode: "list" | "detail" };
|
|
10
|
+
|
|
11
|
+
export function mcpManagerReducer(
|
|
12
|
+
state: McpManagerState,
|
|
13
|
+
action: McpManagerAction,
|
|
14
|
+
): McpManagerState {
|
|
15
|
+
switch (action.type) {
|
|
16
|
+
case "MOVE_UP":
|
|
17
|
+
return { ...state, selectedIndex: Math.max(0, state.selectedIndex - 1) };
|
|
18
|
+
case "MOVE_DOWN":
|
|
19
|
+
return {
|
|
20
|
+
...state,
|
|
21
|
+
selectedIndex: Math.min(
|
|
22
|
+
action.serverCount - 1,
|
|
23
|
+
state.selectedIndex + 1,
|
|
24
|
+
),
|
|
25
|
+
};
|
|
26
|
+
case "SET_VIEW_MODE":
|
|
27
|
+
return { ...state, viewMode: action.viewMode };
|
|
28
|
+
default:
|
|
29
|
+
return state;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export interface PluginDetailState {
|
|
2
|
+
selectedScopeIndex: number;
|
|
3
|
+
selectedActionIndex: number;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export type PluginDetailAction =
|
|
7
|
+
| { type: "SELECT_SCOPE_INDEX"; index: number }
|
|
8
|
+
| { type: "SELECT_ACTION_INDEX"; index: number }
|
|
9
|
+
| { type: "MOVE_SCOPE_UP"; maxIndex: number }
|
|
10
|
+
| { type: "MOVE_SCOPE_DOWN"; maxIndex: number }
|
|
11
|
+
| { type: "MOVE_ACTION_UP"; maxIndex: number }
|
|
12
|
+
| { type: "MOVE_ACTION_DOWN"; maxIndex: number };
|
|
13
|
+
|
|
14
|
+
export function pluginDetailReducer(
|
|
15
|
+
state: PluginDetailState,
|
|
16
|
+
action: PluginDetailAction,
|
|
17
|
+
): PluginDetailState {
|
|
18
|
+
switch (action.type) {
|
|
19
|
+
case "SELECT_SCOPE_INDEX":
|
|
20
|
+
return { ...state, selectedScopeIndex: action.index };
|
|
21
|
+
case "SELECT_ACTION_INDEX":
|
|
22
|
+
return { ...state, selectedActionIndex: action.index };
|
|
23
|
+
case "MOVE_SCOPE_UP":
|
|
24
|
+
return {
|
|
25
|
+
...state,
|
|
26
|
+
selectedScopeIndex:
|
|
27
|
+
state.selectedScopeIndex > 0
|
|
28
|
+
? state.selectedScopeIndex - 1
|
|
29
|
+
: action.maxIndex,
|
|
30
|
+
};
|
|
31
|
+
case "MOVE_SCOPE_DOWN":
|
|
32
|
+
return {
|
|
33
|
+
...state,
|
|
34
|
+
selectedScopeIndex:
|
|
35
|
+
state.selectedScopeIndex < action.maxIndex
|
|
36
|
+
? state.selectedScopeIndex + 1
|
|
37
|
+
: 0,
|
|
38
|
+
};
|
|
39
|
+
case "MOVE_ACTION_UP":
|
|
40
|
+
return {
|
|
41
|
+
...state,
|
|
42
|
+
selectedActionIndex:
|
|
43
|
+
state.selectedActionIndex > 0
|
|
44
|
+
? state.selectedActionIndex - 1
|
|
45
|
+
: action.maxIndex,
|
|
46
|
+
};
|
|
47
|
+
case "MOVE_ACTION_DOWN":
|
|
48
|
+
return {
|
|
49
|
+
...state,
|
|
50
|
+
selectedActionIndex:
|
|
51
|
+
state.selectedActionIndex < action.maxIndex
|
|
52
|
+
? state.selectedActionIndex + 1
|
|
53
|
+
: 0,
|
|
54
|
+
};
|
|
55
|
+
default:
|
|
56
|
+
return state;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
import type { PermissionDecision } from "wave-agent-sdk";
|
|
2
|
+
|
|
3
|
+
export interface QuestionSavedState {
|
|
4
|
+
selectedOptionIndex: number;
|
|
5
|
+
selectedOptionIndices: Set<number>;
|
|
6
|
+
otherText: string;
|
|
7
|
+
otherCursorPosition: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface QuestionState {
|
|
11
|
+
currentQuestionIndex: number;
|
|
12
|
+
selectedOptionIndex: number;
|
|
13
|
+
selectedOptionIndices: Set<number>;
|
|
14
|
+
userAnswers: Record<string, string>;
|
|
15
|
+
otherText: string;
|
|
16
|
+
otherCursorPosition: number;
|
|
17
|
+
savedStates: Record<number, QuestionSavedState>;
|
|
18
|
+
decision: PermissionDecision | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export type QuestionAction =
|
|
22
|
+
| { type: "SELECT_OPTION"; index: number }
|
|
23
|
+
| { type: "MOVE_OPTION_UP"; maxIndex: number }
|
|
24
|
+
| { type: "MOVE_OPTION_DOWN"; maxIndex: number }
|
|
25
|
+
| {
|
|
26
|
+
type: "TOGGLE_MULTI_SELECT";
|
|
27
|
+
optionsLength: number;
|
|
28
|
+
}
|
|
29
|
+
| { type: "CYCLE_QUESTION"; shift: boolean; questionCount: number }
|
|
30
|
+
| { type: "INSERT_OTHER"; text: string; optionsLength: number }
|
|
31
|
+
| { type: "DELETE_OTHER"; optionsLength: number }
|
|
32
|
+
| { type: "MOVE_OTHER_LEFT"; optionsLength: number }
|
|
33
|
+
| { type: "MOVE_OTHER_RIGHT"; optionsLength: number }
|
|
34
|
+
| {
|
|
35
|
+
type: "CONFIRM_ANSWER";
|
|
36
|
+
currentQuestion: {
|
|
37
|
+
question: string;
|
|
38
|
+
options: Array<{ label: string }>;
|
|
39
|
+
multiSelect?: boolean;
|
|
40
|
+
};
|
|
41
|
+
options: Array<{ label: string }>;
|
|
42
|
+
isMultiSelect: boolean;
|
|
43
|
+
questions: Array<{
|
|
44
|
+
question: string;
|
|
45
|
+
options: Array<{ label: string }>;
|
|
46
|
+
multiSelect?: boolean;
|
|
47
|
+
}>;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
function buildAnswerFromSavedState(
|
|
51
|
+
q: {
|
|
52
|
+
question: string;
|
|
53
|
+
options: Array<{ label: string }>;
|
|
54
|
+
multiSelect?: boolean;
|
|
55
|
+
},
|
|
56
|
+
s: QuestionSavedState,
|
|
57
|
+
): string {
|
|
58
|
+
const opts = [...q.options, { label: "Other" }];
|
|
59
|
+
let a = "";
|
|
60
|
+
if (q.multiSelect) {
|
|
61
|
+
const selectedLabels = Array.from(s.selectedOptionIndices)
|
|
62
|
+
.filter((i) => i < q.options.length)
|
|
63
|
+
.map((i) => q.options[i].label);
|
|
64
|
+
const isOtherChecked = s.selectedOptionIndices.has(opts.length - 1);
|
|
65
|
+
if (isOtherChecked && s.otherText.trim()) {
|
|
66
|
+
selectedLabels.push(s.otherText.trim());
|
|
67
|
+
}
|
|
68
|
+
a = selectedLabels.join(", ");
|
|
69
|
+
} else {
|
|
70
|
+
if (s.selectedOptionIndex === opts.length - 1) {
|
|
71
|
+
a = s.otherText.trim();
|
|
72
|
+
} else {
|
|
73
|
+
a = opts[s.selectedOptionIndex].label;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return a;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function questionReducer(
|
|
80
|
+
state: QuestionState,
|
|
81
|
+
action: QuestionAction,
|
|
82
|
+
): QuestionState {
|
|
83
|
+
switch (action.type) {
|
|
84
|
+
case "SELECT_OPTION":
|
|
85
|
+
return { ...state, selectedOptionIndex: action.index };
|
|
86
|
+
case "MOVE_OPTION_UP":
|
|
87
|
+
return {
|
|
88
|
+
...state,
|
|
89
|
+
selectedOptionIndex: Math.max(0, state.selectedOptionIndex - 1),
|
|
90
|
+
};
|
|
91
|
+
case "MOVE_OPTION_DOWN":
|
|
92
|
+
return {
|
|
93
|
+
...state,
|
|
94
|
+
selectedOptionIndex: Math.min(
|
|
95
|
+
action.maxIndex,
|
|
96
|
+
state.selectedOptionIndex + 1,
|
|
97
|
+
),
|
|
98
|
+
};
|
|
99
|
+
case "TOGGLE_MULTI_SELECT": {
|
|
100
|
+
const nextIndices = new Set(state.selectedOptionIndices);
|
|
101
|
+
if (nextIndices.has(state.selectedOptionIndex))
|
|
102
|
+
nextIndices.delete(state.selectedOptionIndex);
|
|
103
|
+
else nextIndices.add(state.selectedOptionIndex);
|
|
104
|
+
return { ...state, selectedOptionIndices: nextIndices };
|
|
105
|
+
}
|
|
106
|
+
case "CYCLE_QUESTION": {
|
|
107
|
+
const direction = action.shift ? -1 : 1;
|
|
108
|
+
let nextIndex = state.currentQuestionIndex + direction;
|
|
109
|
+
if (nextIndex < 0) nextIndex = action.questionCount - 1;
|
|
110
|
+
if (nextIndex >= action.questionCount) nextIndex = 0;
|
|
111
|
+
if (nextIndex === state.currentQuestionIndex) return state;
|
|
112
|
+
const savedStates = {
|
|
113
|
+
...state.savedStates,
|
|
114
|
+
[state.currentQuestionIndex]: {
|
|
115
|
+
selectedOptionIndex: state.selectedOptionIndex,
|
|
116
|
+
selectedOptionIndices: state.selectedOptionIndices,
|
|
117
|
+
otherText: state.otherText,
|
|
118
|
+
otherCursorPosition: state.otherCursorPosition,
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
const nextState = savedStates[nextIndex] || {
|
|
122
|
+
selectedOptionIndex: 0,
|
|
123
|
+
selectedOptionIndices: new Set<number>(),
|
|
124
|
+
otherText: "",
|
|
125
|
+
otherCursorPosition: 0,
|
|
126
|
+
};
|
|
127
|
+
return {
|
|
128
|
+
...state,
|
|
129
|
+
currentQuestionIndex: nextIndex,
|
|
130
|
+
...nextState,
|
|
131
|
+
savedStates,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
case "INSERT_OTHER": {
|
|
135
|
+
if (state.selectedOptionIndex !== action.optionsLength - 1) return state;
|
|
136
|
+
const newText =
|
|
137
|
+
state.otherText.slice(0, state.otherCursorPosition) +
|
|
138
|
+
action.text +
|
|
139
|
+
state.otherText.slice(state.otherCursorPosition);
|
|
140
|
+
return {
|
|
141
|
+
...state,
|
|
142
|
+
otherText: newText,
|
|
143
|
+
otherCursorPosition: state.otherCursorPosition + action.text.length,
|
|
144
|
+
};
|
|
145
|
+
}
|
|
146
|
+
case "DELETE_OTHER":
|
|
147
|
+
if (state.selectedOptionIndex !== action.optionsLength - 1) return state;
|
|
148
|
+
if (state.otherCursorPosition > 0) {
|
|
149
|
+
return {
|
|
150
|
+
...state,
|
|
151
|
+
otherText:
|
|
152
|
+
state.otherText.slice(0, state.otherCursorPosition - 1) +
|
|
153
|
+
state.otherText.slice(state.otherCursorPosition),
|
|
154
|
+
otherCursorPosition: state.otherCursorPosition - 1,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
return state;
|
|
158
|
+
case "MOVE_OTHER_LEFT":
|
|
159
|
+
if (state.selectedOptionIndex !== action.optionsLength - 1) return state;
|
|
160
|
+
return {
|
|
161
|
+
...state,
|
|
162
|
+
otherCursorPosition: Math.max(0, state.otherCursorPosition - 1),
|
|
163
|
+
};
|
|
164
|
+
case "MOVE_OTHER_RIGHT":
|
|
165
|
+
if (state.selectedOptionIndex !== action.optionsLength - 1) return state;
|
|
166
|
+
return {
|
|
167
|
+
...state,
|
|
168
|
+
otherCursorPosition: Math.min(
|
|
169
|
+
state.otherText.length,
|
|
170
|
+
state.otherCursorPosition + 1,
|
|
171
|
+
),
|
|
172
|
+
};
|
|
173
|
+
case "CONFIRM_ANSWER": {
|
|
174
|
+
const { currentQuestion: cq, options, isMultiSelect, questions } = action;
|
|
175
|
+
const isOtherFocused = state.selectedOptionIndex === options.length - 1;
|
|
176
|
+
let answer = "";
|
|
177
|
+
if (isMultiSelect) {
|
|
178
|
+
const selectedLabels = Array.from(state.selectedOptionIndices)
|
|
179
|
+
.filter((i) => i < cq.options.length)
|
|
180
|
+
.map((i) => cq.options[i].label);
|
|
181
|
+
const isOtherChecked = state.selectedOptionIndices.has(
|
|
182
|
+
options.length - 1,
|
|
183
|
+
);
|
|
184
|
+
if (isOtherChecked && state.otherText.trim()) {
|
|
185
|
+
selectedLabels.push(state.otherText.trim());
|
|
186
|
+
}
|
|
187
|
+
answer = selectedLabels.join(", ");
|
|
188
|
+
} else {
|
|
189
|
+
if (isOtherFocused) {
|
|
190
|
+
answer = state.otherText.trim();
|
|
191
|
+
} else {
|
|
192
|
+
answer = options[state.selectedOptionIndex].label;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
if (!answer) return state;
|
|
196
|
+
|
|
197
|
+
const newAnswers = {
|
|
198
|
+
...state.userAnswers,
|
|
199
|
+
[cq.question]: answer,
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
if (state.currentQuestionIndex < questions.length - 1) {
|
|
203
|
+
const nextIndex = state.currentQuestionIndex + 1;
|
|
204
|
+
const savedStates = {
|
|
205
|
+
...state.savedStates,
|
|
206
|
+
[state.currentQuestionIndex]: {
|
|
207
|
+
selectedOptionIndex: state.selectedOptionIndex,
|
|
208
|
+
selectedOptionIndices: state.selectedOptionIndices,
|
|
209
|
+
otherText: state.otherText,
|
|
210
|
+
otherCursorPosition: state.otherCursorPosition,
|
|
211
|
+
},
|
|
212
|
+
};
|
|
213
|
+
const nextState = savedStates[nextIndex] || {
|
|
214
|
+
selectedOptionIndex: 0,
|
|
215
|
+
selectedOptionIndices: new Set<number>(),
|
|
216
|
+
otherText: "",
|
|
217
|
+
otherCursorPosition: 0,
|
|
218
|
+
};
|
|
219
|
+
return {
|
|
220
|
+
...state,
|
|
221
|
+
currentQuestionIndex: nextIndex,
|
|
222
|
+
...nextState,
|
|
223
|
+
userAnswers: newAnswers,
|
|
224
|
+
savedStates,
|
|
225
|
+
};
|
|
226
|
+
} else {
|
|
227
|
+
const finalAnswers = { ...newAnswers };
|
|
228
|
+
for (const [idxStr, s] of Object.entries(state.savedStates)) {
|
|
229
|
+
const idx = parseInt(idxStr);
|
|
230
|
+
const q = questions[idx];
|
|
231
|
+
if (q && !finalAnswers[q.question]) {
|
|
232
|
+
const a = buildAnswerFromSavedState(q, s);
|
|
233
|
+
if (a) finalAnswers[q.question] = a;
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
const allAnswered = questions.every((q) => finalAnswers[q.question]);
|
|
237
|
+
if (!allAnswered) return state;
|
|
238
|
+
return {
|
|
239
|
+
...state,
|
|
240
|
+
userAnswers: finalAnswers,
|
|
241
|
+
decision: {
|
|
242
|
+
behavior: "allow",
|
|
243
|
+
message: JSON.stringify(finalAnswers),
|
|
244
|
+
},
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
default:
|
|
249
|
+
return state;
|
|
250
|
+
}
|
|
251
|
+
}
|