@unscientificjszhai/howto 1.0.1 → 1.1.0
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/README.md +127 -31
- package/README.zh-hans-cn.md +331 -0
- package/THIRD_PARTY_NOTICES.md +2 -0
- package/dist/ai/chatgpt-models.js +148 -0
- package/dist/ai/chatgpt.js +495 -0
- package/dist/ai/command-schema.js +15 -1
- package/dist/ai/errors.js +6 -0
- package/dist/ai/gemini.js +117 -4
- package/dist/ai/index.js +4 -1
- package/dist/ai/openai.js +105 -2
- package/dist/ai/usage-session.js +144 -0
- package/dist/auth/chatgpt/commands.js +90 -0
- package/dist/auth/chatgpt/errors.js +85 -0
- package/dist/auth/chatgpt/http.js +97 -0
- package/dist/auth/chatgpt/index.js +229 -0
- package/dist/auth/chatgpt/instructions.js +22 -0
- package/dist/auth/chatgpt/oauth.js +320 -0
- package/dist/auth/chatgpt/storage.js +366 -0
- package/dist/auth/chatgpt/types.js +1 -0
- package/dist/cli.js +77 -16
- package/dist/config-file.js +2 -0
- package/dist/config.js +11 -4
- package/dist/errors.js +6 -1
- package/dist/execute.js +4 -13
- package/dist/index.js +32 -12
- package/dist/init/ChatGptInitializationStep.js +223 -0
- package/dist/init/InitializationApp.js +74 -38
- package/dist/init/index.js +24 -5
- package/dist/init/state.js +14 -3
- package/dist/prompt.js +56 -10
- package/dist/safety/command-risk.js +11 -0
- package/dist/safety/dangerous-command.js +209 -0
- package/dist/shell/command-analysis.js +25 -5
- package/dist/shell/execution-environment.js +25 -0
- package/dist/shell/runtime-environment.js +61 -0
- package/dist/ui/App.js +104 -15
- package/dist/ui/ConfirmView.js +8 -41
- package/dist/ui/InteractiveSessionProvider.js +7 -8
- package/dist/ui/ResolvePlaceholdersView.js +8 -3
- package/dist/ui/SelectCommandView.js +2 -2
- package/dist/ui/UsageApprovalView.js +320 -0
- package/dist/ui/interactive-session.js +243 -33
- package/dist/ui/keyboard-framing.js +102 -0
- package/dist/ui/paste-framing.js +69 -32
- package/dist/ui/resize-safe-output.js +2 -2
- package/dist/ui/run-interactive-command.js +2 -2
- package/dist/ui/text-input.js +4 -1
- package/dist/ui/use-keyboard-input.js +17 -2
- package/dist/ui/use-paste-aware-input.js +12 -3
- package/dist/usage/arguments.js +61 -0
- package/dist/usage/generate.js +156 -0
- package/dist/usage/output.js +216 -0
- package/dist/usage/risk.js +17 -0
- package/dist/usage/runner.js +238 -0
- package/dist/usage/types.js +1 -0
- package/dist/validation/ai-response.js +24 -3
- package/dist/validation/command-tool.js +7 -6
- package/dist/validation/usage-response.js +39 -0
- package/package.json +31 -17
- package/README.CN.md +0 -235
package/dist/ui/App.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import React, { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
-
import { Box, Text, usePaste, useWindowSize } from "ink";
|
|
1
|
+
import React, { useCallback, useEffect, useRef, useState, useLayoutEffect } from "react";
|
|
2
|
+
import { Box, Text, usePaste, useStderr, useWindowSize } from "ink";
|
|
3
3
|
import { useKeyboardInput } from "./use-keyboard-input.js";
|
|
4
4
|
import { useInteractiveSession } from "./InteractiveSessionProvider.js";
|
|
5
5
|
import { LoadingView } from "./LoadingView.js";
|
|
@@ -7,15 +7,22 @@ import { SelectCommandView } from "./SelectCommandView.js";
|
|
|
7
7
|
import { ResolvePlaceholdersView } from "./ResolvePlaceholdersView.js";
|
|
8
8
|
import { ConfirmView } from "./ConfirmView.js";
|
|
9
9
|
import { generateValidatedCommandCandidates } from "../validation/generated-commands.js";
|
|
10
|
-
import {
|
|
10
|
+
import { resolveCommandDanger } from "../safety/command-risk.js";
|
|
11
11
|
import { resolveCandidatePlaceholders } from "./placeholder-logic.js";
|
|
12
12
|
import { InteractionCancelledError } from "./tty.js";
|
|
13
13
|
import { usePhysicalStdoutRows } from "./resize-safe-output.js";
|
|
14
14
|
import { getUserVisibleErrorMessage } from "../errors.js";
|
|
15
|
-
|
|
15
|
+
import { generateWithUsageLookup } from "../usage/generate.js";
|
|
16
|
+
import { formatUsageCommand } from "../usage/arguments.js";
|
|
17
|
+
import { safeUsageDisplay, UsageApprovalView } from "./UsageApprovalView.js";
|
|
18
|
+
import { toSingleLinePreview } from "./single-line-preview.js";
|
|
19
|
+
export const App = ({ provider, request, onSuccess, onError, knownCredentials = [], }) => {
|
|
16
20
|
const session = useInteractiveSession();
|
|
17
21
|
const rows = usePhysicalStdoutRows();
|
|
18
22
|
const { columns } = useWindowSize();
|
|
23
|
+
const { write: writeStderr } = useStderr();
|
|
24
|
+
const writeStderrRef = useRef(writeStderr);
|
|
25
|
+
writeStderrRef.current = writeStderr;
|
|
19
26
|
const [status, setStatus] = useState("loading");
|
|
20
27
|
const [candidates, setCandidates] = useState([]);
|
|
21
28
|
const [selectedCandidate, setSelectedCandidate] = useState(null);
|
|
@@ -23,11 +30,30 @@ export const App = ({ provider, request, onSuccess, onError }) => {
|
|
|
23
30
|
const [finalCommand, setFinalCommand] = useState("");
|
|
24
31
|
const [danger, setDanger] = useState(undefined);
|
|
25
32
|
const [errorMessage, setErrorMessage] = useState("");
|
|
33
|
+
const [usageApproval, setUsageApproval] = useState(null);
|
|
34
|
+
const [runningQuery, setRunningQuery] = useState("");
|
|
35
|
+
const approvalSequence = useRef(0);
|
|
36
|
+
const pendingApproval = useRef(null);
|
|
37
|
+
const onErrorRef = useRef(onError);
|
|
38
|
+
const credentialsRef = useRef(knownCredentials);
|
|
39
|
+
onErrorRef.current = onError;
|
|
40
|
+
credentialsRef.current = knownCredentials;
|
|
26
41
|
const activeRequestControllerRef = useRef(null);
|
|
27
42
|
const cancellationReportedRef = useRef(false);
|
|
28
43
|
const frameRows = Math.max(0, rows - 1);
|
|
29
44
|
const isFrameVisible = frameRows > 0;
|
|
30
|
-
usePaste(() => {
|
|
45
|
+
usePaste(() => {
|
|
46
|
+
if (status === "resolving" || status === "usage-approval" || status === "confirming")
|
|
47
|
+
return;
|
|
48
|
+
if (session.isInputCurrent())
|
|
49
|
+
session.reportIgnoredPaste();
|
|
50
|
+
else
|
|
51
|
+
session.reportStalePaste();
|
|
52
|
+
}, { isActive: isFrameVisible });
|
|
53
|
+
useLayoutEffect(() => {
|
|
54
|
+
if (status !== "done" && status !== "error" && status !== "usage-approval")
|
|
55
|
+
return session.beginView();
|
|
56
|
+
}, [session, status]);
|
|
31
57
|
const handleCancel = useCallback(() => {
|
|
32
58
|
session.suspendViewInput();
|
|
33
59
|
activeRequestControllerRef.current?.abort();
|
|
@@ -36,22 +62,78 @@ export const App = ({ provider, request, onSuccess, onError }) => {
|
|
|
36
62
|
cancellationReportedRef.current = true;
|
|
37
63
|
onError(new InteractionCancelledError());
|
|
38
64
|
}, [onError, session]);
|
|
39
|
-
useKeyboardInput((input, key) => {
|
|
65
|
+
const inputReady = useKeyboardInput((input, key) => {
|
|
40
66
|
if (frameRows === 0)
|
|
41
67
|
return;
|
|
42
|
-
if (status !== "loading")
|
|
68
|
+
if (status !== "loading" && status !== "usage-running")
|
|
43
69
|
return;
|
|
44
70
|
if (key.ctrl && input === "c") {
|
|
45
71
|
handleCancel();
|
|
46
72
|
}
|
|
47
73
|
});
|
|
48
74
|
useEffect(() => {
|
|
49
|
-
if (status !== "loading")
|
|
50
|
-
return;
|
|
51
75
|
const controller = new AbortController();
|
|
52
76
|
let isCurrent = true;
|
|
53
77
|
activeRequestControllerRef.current = controller;
|
|
54
|
-
|
|
78
|
+
const approve = (approval, signal) => {
|
|
79
|
+
if (signal.aborted || session.getExecutionPolicy() !== "execute") {
|
|
80
|
+
return Promise.resolve({ kind: "reject" });
|
|
81
|
+
}
|
|
82
|
+
return new Promise((resolve) => {
|
|
83
|
+
let settled = false;
|
|
84
|
+
const finish = (decision) => {
|
|
85
|
+
if (settled)
|
|
86
|
+
return;
|
|
87
|
+
settled = true;
|
|
88
|
+
signal.removeEventListener("abort", reject);
|
|
89
|
+
unsubscribe();
|
|
90
|
+
pendingApproval.current = null;
|
|
91
|
+
session.suspendViewInput();
|
|
92
|
+
if (isCurrent && !controller.signal.aborted) {
|
|
93
|
+
setUsageApproval(null);
|
|
94
|
+
setStatus("loading");
|
|
95
|
+
}
|
|
96
|
+
resolve(decision);
|
|
97
|
+
};
|
|
98
|
+
const reject = () => finish({ kind: "reject" });
|
|
99
|
+
const unsubscribe = session.subscribeExecutionPolicy(() => {
|
|
100
|
+
if (session.getExecutionPolicy() !== "execute")
|
|
101
|
+
reject();
|
|
102
|
+
});
|
|
103
|
+
signal.addEventListener("abort", reject, { once: true });
|
|
104
|
+
pendingApproval.current = finish;
|
|
105
|
+
session.suspendViewInput();
|
|
106
|
+
if (settled)
|
|
107
|
+
return;
|
|
108
|
+
approvalSequence.current++;
|
|
109
|
+
setUsageApproval(approval);
|
|
110
|
+
setStatus("usage-approval");
|
|
111
|
+
});
|
|
112
|
+
};
|
|
113
|
+
const generation = request.useCommand !== undefined && provider.createUsageSession !== undefined
|
|
114
|
+
? generateWithUsageLookup(provider, request, {
|
|
115
|
+
approve,
|
|
116
|
+
getExecutionPolicy: session.getExecutionPolicy,
|
|
117
|
+
checkExecutionPolicy: session.checkExecutionPolicy,
|
|
118
|
+
subscribeExecutionPolicy: session.subscribeExecutionPolicy,
|
|
119
|
+
knownCredentials: credentialsRef.current,
|
|
120
|
+
onQueryRunning: (command) => {
|
|
121
|
+
if (!isCurrent || controller.signal.aborted)
|
|
122
|
+
return;
|
|
123
|
+
session.suspendViewInput();
|
|
124
|
+
setRunningQuery(command === null
|
|
125
|
+
? ""
|
|
126
|
+
: safeUsageDisplay(formatUsageCommand(command.executable, command.args), command.knownCredentials));
|
|
127
|
+
setStatus(command === null ? "loading" : "usage-running");
|
|
128
|
+
},
|
|
129
|
+
onCapabilityFallback: (message) => {
|
|
130
|
+
// Ink 的输出入口先清理当前帧再恢复,完整提示在紧凑终端中仍进入可查看的滚动历史。
|
|
131
|
+
if (isCurrent && !controller.signal.aborted)
|
|
132
|
+
writeStderrRef.current(`${message}\n`);
|
|
133
|
+
},
|
|
134
|
+
}, controller.signal)
|
|
135
|
+
: generateValidatedCommandCandidates(provider, request, controller.signal);
|
|
136
|
+
void generation
|
|
55
137
|
.then((generatedCandidates) => {
|
|
56
138
|
if (!isCurrent || controller.signal.aborted)
|
|
57
139
|
return;
|
|
@@ -64,7 +146,7 @@ export const App = ({ provider, request, onSuccess, onError }) => {
|
|
|
64
146
|
const appError = normalizeError(error);
|
|
65
147
|
setErrorMessage(getUserVisibleErrorMessage(appError));
|
|
66
148
|
setStatus("error");
|
|
67
|
-
|
|
149
|
+
onErrorRef.current(appError);
|
|
68
150
|
});
|
|
69
151
|
return () => {
|
|
70
152
|
isCurrent = false;
|
|
@@ -73,8 +155,9 @@ export const App = ({ provider, request, onSuccess, onError }) => {
|
|
|
73
155
|
activeRequestControllerRef.current = null;
|
|
74
156
|
}
|
|
75
157
|
};
|
|
76
|
-
}, [
|
|
158
|
+
}, [provider, request, session]);
|
|
77
159
|
const handleSelect = (candidate) => {
|
|
160
|
+
session.suspendViewInput();
|
|
78
161
|
setSelectedCandidate(candidate);
|
|
79
162
|
setResolvedValues(new Map());
|
|
80
163
|
if (candidate.placeholders.length > 0) {
|
|
@@ -85,7 +168,7 @@ export const App = ({ provider, request, onSuccess, onError }) => {
|
|
|
85
168
|
const resolved = resolveCandidatePlaceholders(candidate, new Map());
|
|
86
169
|
setFinalCommand(resolved.command);
|
|
87
170
|
setResolvedValues(resolved.values);
|
|
88
|
-
setDanger(
|
|
171
|
+
setDanger(resolveCommandDanger(resolved.command, candidate));
|
|
89
172
|
setStatus("confirming");
|
|
90
173
|
}
|
|
91
174
|
catch (error) {
|
|
@@ -99,9 +182,10 @@ export const App = ({ provider, request, onSuccess, onError }) => {
|
|
|
99
182
|
const handleResolve = (resolved) => {
|
|
100
183
|
if (!selectedCandidate)
|
|
101
184
|
return;
|
|
185
|
+
session.suspendViewInput();
|
|
102
186
|
setResolvedValues(resolved.values);
|
|
103
187
|
setFinalCommand(resolved.command);
|
|
104
|
-
setDanger(
|
|
188
|
+
setDanger(resolveCommandDanger(resolved.command, selectedCandidate));
|
|
105
189
|
setStatus("confirming");
|
|
106
190
|
};
|
|
107
191
|
useEffect(() => {
|
|
@@ -114,10 +198,15 @@ export const App = ({ provider, request, onSuccess, onError }) => {
|
|
|
114
198
|
setStatus("done");
|
|
115
199
|
};
|
|
116
200
|
const handleBackToSelection = () => {
|
|
201
|
+
session.suspendViewInput();
|
|
117
202
|
setStatus("selecting");
|
|
118
203
|
};
|
|
119
|
-
return (React.createElement(Box, { flexDirection: "column", display: isFrameVisible ? "flex" : "none", maxHeight: frameRows, overflowY: "hidden" },
|
|
204
|
+
return (React.createElement(Box, { flexDirection: "column", display: isFrameVisible && inputReady ? "flex" : "none", maxHeight: frameRows, overflowY: "hidden" },
|
|
120
205
|
status === "loading" && React.createElement(LoadingView, null),
|
|
206
|
+
status === "usage-approval" && usageApproval && (React.createElement(UsageApprovalView, { key: approvalSequence.current, request: usageApproval, knownCredentials: knownCredentials, availableRows: frameRows, availableColumns: columns, onApprove: (args) => pendingApproval.current?.({ kind: "approve", args }), onReject: () => pendingApproval.current?.({ kind: "reject" }), onCancel: handleCancel })),
|
|
207
|
+
status === "usage-running" && (React.createElement(Text, { wrap: "truncate-middle" },
|
|
208
|
+
"\u67E5\u8BE2\u8FD0\u884C\u4E2D\uFF08Ctrl+C\u53D6\u6D88\uFF09: ",
|
|
209
|
+
toSingleLinePreview(runningQuery))),
|
|
121
210
|
status === "selecting" && (React.createElement(SelectCommandView, { candidates: candidates, availableRows: frameRows, isInputActive: isFrameVisible, onSelect: handleSelect, onCancel: handleCancel })),
|
|
122
211
|
status === "resolving" && selectedCandidate && (React.createElement(ResolvePlaceholdersView, { candidate: selectedCandidate, availableRows: frameRows, availableColumns: columns, isInputActive: isFrameVisible, onResolve: handleResolve, onBack: handleBackToSelection, onCancel: handleCancel })),
|
|
123
212
|
(status === "confirming" || status === "done") && selectedCandidate && (React.createElement(ConfirmView, { candidate: selectedCandidate, command: finalCommand, resolvedValues: resolvedValues, danger: danger, availableRows: frameRows, availableColumns: columns, isInputActive: isFrameVisible, onConfirm: handleConfirm, onCancel: handleCancel, isDone: status === "done" })),
|
package/dist/ui/ConfirmView.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import React, { useRef, useState } from "react";
|
|
2
2
|
import { Box, Text, useStdout } from "ink";
|
|
3
|
-
import { hasUnsafeTerminalControlCharacters } from "../terminal-text.js";
|
|
4
3
|
import { deleteLastGrapheme, isTextInputEvent } from "./text-input.js";
|
|
5
4
|
import { toSingleLinePreview, toTailPreview } from "./single-line-preview.js";
|
|
6
|
-
import {
|
|
5
|
+
import { useOptionalInteractiveSession } from "./InteractiveSessionProvider.js";
|
|
7
6
|
import { usePasteAwareInput } from "./use-paste-aware-input.js";
|
|
8
7
|
const DANGER_CONFIRMATION_PHRASE = "EXECUTE";
|
|
9
8
|
const DANGER_CONFIRMATION_SUFFIX = "+Enter; Esc/Ctrl+C |> ";
|
|
@@ -11,14 +10,16 @@ export function isDangerConfirmationInput(input) {
|
|
|
11
10
|
return input.toUpperCase() === DANGER_CONFIRMATION_PHRASE;
|
|
12
11
|
}
|
|
13
12
|
export const ConfirmView = ({ command, danger, onConfirm, onCancel, isDone = false, isInputActive = true, availableRows = 3, availableColumns, }) => {
|
|
14
|
-
const
|
|
13
|
+
const session = useOptionalInteractiveSession();
|
|
15
14
|
const [buffer, setBuffer] = useState("");
|
|
16
15
|
const bufferRef = useRef(buffer);
|
|
17
16
|
const finishedRef = useRef(false);
|
|
18
17
|
const { stdout } = useStdout();
|
|
19
|
-
const stdoutColumns = typeof stdout.columns === "number" && stdout.columns > 0
|
|
18
|
+
const stdoutColumns = "columns" in stdout && typeof stdout.columns === "number" && stdout.columns > 0
|
|
19
|
+
? stdout.columns
|
|
20
|
+
: 80;
|
|
20
21
|
const columns = availableColumns ?? stdoutColumns;
|
|
21
|
-
usePasteAwareInput({
|
|
22
|
+
const inputReady = usePasteAwareInput({
|
|
22
23
|
onInput: (input, key) => {
|
|
23
24
|
if (finishedRef.current || !isInputActive || availableRows <= 0 || isDone)
|
|
24
25
|
return;
|
|
@@ -32,13 +33,6 @@ export const ConfirmView = ({ command, danger, onConfirm, onCancel, isDone = fal
|
|
|
32
33
|
onCancel();
|
|
33
34
|
return;
|
|
34
35
|
}
|
|
35
|
-
if (getPolicy() === "print") {
|
|
36
|
-
if (key.return) {
|
|
37
|
-
finishedRef.current = true;
|
|
38
|
-
onConfirm();
|
|
39
|
-
}
|
|
40
|
-
return;
|
|
41
|
-
}
|
|
42
36
|
if (danger) {
|
|
43
37
|
if (key.return) {
|
|
44
38
|
finishedRef.current = true;
|
|
@@ -63,15 +57,7 @@ export const ConfirmView = ({ command, danger, onConfirm, onCancel, isDone = fal
|
|
|
63
57
|
onConfirm();
|
|
64
58
|
}
|
|
65
59
|
},
|
|
66
|
-
onPaste: (
|
|
67
|
-
if (finishedRef.current ||
|
|
68
|
-
getPolicy() === "print" ||
|
|
69
|
-
danger === undefined ||
|
|
70
|
-
hasUnsafeTerminalControlCharacters(input)) {
|
|
71
|
-
return;
|
|
72
|
-
}
|
|
73
|
-
updateBuffer((previous) => previous + input);
|
|
74
|
-
},
|
|
60
|
+
onPaste: () => session?.reportIgnoredPaste(),
|
|
75
61
|
isPasteActive: isInputActive && availableRows > 0 && !isDone,
|
|
76
62
|
});
|
|
77
63
|
function updateBuffer(update) {
|
|
@@ -79,31 +65,12 @@ export const ConfirmView = ({ command, danger, onConfirm, onCancel, isDone = fal
|
|
|
79
65
|
bufferRef.current = nextBuffer;
|
|
80
66
|
setBuffer(nextBuffer);
|
|
81
67
|
}
|
|
82
|
-
if (availableRows <= 0) {
|
|
68
|
+
if (!inputReady || availableRows <= 0) {
|
|
83
69
|
return null;
|
|
84
70
|
}
|
|
85
71
|
if (isDone) {
|
|
86
72
|
return React.createElement(CommandPreview, { command: command });
|
|
87
73
|
}
|
|
88
|
-
if (policy === "print") {
|
|
89
|
-
if (availableRows === 1) {
|
|
90
|
-
return (React.createElement(Box, { height: 1, maxHeight: 1, overflowX: "hidden", overflowY: "hidden" },
|
|
91
|
-
React.createElement(CommandPreview, { command: command, prefix: "\u4EC5\u8F93\u51FA:", prefixWidth: 7 }),
|
|
92
|
-
React.createElement(Box, { width: 8, flexShrink: 0 },
|
|
93
|
-
React.createElement(Text, null, " Ent Esc"))));
|
|
94
|
-
}
|
|
95
|
-
return (React.createElement(Box, { flexDirection: "column" },
|
|
96
|
-
danger && availableRows >= 3 && (React.createElement(Text, { color: "red", wrap: "truncate-middle" },
|
|
97
|
-
"Risk: ",
|
|
98
|
-
toSingleLinePreview(danger.reason),
|
|
99
|
-
" [",
|
|
100
|
-
toSingleLinePreview(danger.rule),
|
|
101
|
-
"]")),
|
|
102
|
-
React.createElement(Text, { wrap: "truncate-middle" },
|
|
103
|
-
"\u4EC5\u8F93\u51FA: ",
|
|
104
|
-
toSingleLinePreview(command)),
|
|
105
|
-
React.createElement(Text, { wrap: "truncate-end" }, "Enter\u8F93\u51FA Esc\u53D6\u6D88")));
|
|
106
|
-
}
|
|
107
74
|
if (!danger && availableRows === 1) {
|
|
108
75
|
return (React.createElement(Box, { height: 1, maxHeight: 1, overflowX: "hidden", overflowY: "hidden" },
|
|
109
76
|
React.createElement(CommandPreview, { command: command }),
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
-
import React, { createContext, useContext, useEffect,
|
|
1
|
+
import React, { createContext, useContext, useEffect, useLayoutEffect } from "react";
|
|
2
|
+
import { useStderr } from "ink";
|
|
2
3
|
const SessionContext = createContext(undefined);
|
|
3
4
|
export const InteractiveSessionProvider = ({ session, children }) => {
|
|
4
|
-
|
|
5
|
+
const { write } = useStderr();
|
|
6
|
+
useLayoutEffect(() => session.beginView(), [session]);
|
|
7
|
+
useEffect(() => session.subscribeInputNotice((message) => write(`提示:${message}\n`)), [session, write]);
|
|
5
8
|
return React.createElement(SessionContext.Provider, { value: session }, children);
|
|
6
9
|
};
|
|
7
10
|
export function useInteractiveSession() {
|
|
@@ -10,10 +13,6 @@ export function useInteractiveSession() {
|
|
|
10
13
|
throw new Error("交互视图必须使用受管理的终端会话。");
|
|
11
14
|
return session;
|
|
12
15
|
}
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
export function useExecutionPolicy() {
|
|
16
|
-
const session = useContext(SessionContext);
|
|
17
|
-
const policy = useSyncExternalStore(session?.subscribeExecutionPolicy ?? subscribeWithoutSession, session?.getExecutionPolicy ?? keyboardPolicy, keyboardPolicy);
|
|
18
|
-
return { policy, getPolicy: session?.getExecutionPolicy ?? keyboardPolicy };
|
|
16
|
+
export function useOptionalInteractiveSession() {
|
|
17
|
+
return useContext(SessionContext);
|
|
19
18
|
}
|
|
@@ -1,15 +1,18 @@
|
|
|
1
|
-
import React, { useRef, useState } from "react";
|
|
1
|
+
import React, { useRef, useState, useLayoutEffect } from "react";
|
|
2
2
|
import { Box, Text } from "ink";
|
|
3
3
|
import { hasUnsafeTerminalControlCharacters } from "../terminal-text.js";
|
|
4
4
|
import { SelectedCommandDisplay } from "./SelectedCommandDisplay.js";
|
|
5
5
|
import { toSingleLinePreview, toTailPreview } from "./single-line-preview.js";
|
|
6
6
|
import { isTextInputEvent } from "./text-input.js";
|
|
7
7
|
import { usePasteAwareInput } from "./use-paste-aware-input.js";
|
|
8
|
+
import { useOptionalInteractiveSession } from "./InteractiveSessionProvider.js";
|
|
8
9
|
import { applyPlaceholderResolutionInput, createPlaceholderResolution, getPlaceholderResolutionView, } from "./placeholder-logic.js";
|
|
9
10
|
export const ResolvePlaceholdersView = ({ candidate, availableRows, availableColumns, isInputActive = true, onResolve, onBack, onCancel, }) => {
|
|
11
|
+
const session = useOptionalInteractiveSession();
|
|
10
12
|
const [resolutionState, setResolutionState] = useState(() => createPlaceholderResolution(candidate));
|
|
11
13
|
const resolutionStateRef = useRef(resolutionState);
|
|
12
14
|
const finishedRef = useRef(false);
|
|
15
|
+
useLayoutEffect(() => session?.beginView(), [session, resolutionState.activeIndex]);
|
|
13
16
|
const { currentPlaceholder, currentBuffer, resolvedValues } = getPlaceholderResolutionView(resolutionState);
|
|
14
17
|
const handleResolutionInput = (resolutionInput) => {
|
|
15
18
|
if (finishedRef.current)
|
|
@@ -17,6 +20,8 @@ export const ResolvePlaceholdersView = ({ candidate, availableRows, availableCol
|
|
|
17
20
|
const transition = applyPlaceholderResolutionInput(resolutionStateRef.current, resolutionInput);
|
|
18
21
|
switch (transition.type) {
|
|
19
22
|
case "editing":
|
|
23
|
+
if (transition.state.activeIndex !== resolutionStateRef.current.activeIndex)
|
|
24
|
+
session?.suspendViewInput();
|
|
20
25
|
resolutionStateRef.current = transition.state;
|
|
21
26
|
setResolutionState(transition.state);
|
|
22
27
|
return;
|
|
@@ -30,7 +35,7 @@ export const ResolvePlaceholdersView = ({ candidate, availableRows, availableCol
|
|
|
30
35
|
return;
|
|
31
36
|
}
|
|
32
37
|
};
|
|
33
|
-
usePasteAwareInput({
|
|
38
|
+
const inputReady = usePasteAwareInput({
|
|
34
39
|
onInput: (input, key) => {
|
|
35
40
|
if (finishedRef.current || !isInputActive || availableRows <= 0)
|
|
36
41
|
return;
|
|
@@ -52,7 +57,7 @@ export const ResolvePlaceholdersView = ({ candidate, availableRows, availableCol
|
|
|
52
57
|
},
|
|
53
58
|
isPasteActive: isInputActive && availableRows > 0,
|
|
54
59
|
});
|
|
55
|
-
if (availableRows <= 0) {
|
|
60
|
+
if (!inputReady || availableRows <= 0) {
|
|
56
61
|
return null;
|
|
57
62
|
}
|
|
58
63
|
if (availableRows === 1) {
|
|
@@ -11,7 +11,7 @@ export const SelectCommandView = ({ candidates, availableRows, isInputActive = t
|
|
|
11
11
|
activeIndexRef.current = nextIndex;
|
|
12
12
|
setActiveIndex(nextIndex);
|
|
13
13
|
}
|
|
14
|
-
useKeyboardInput((input, key) => {
|
|
14
|
+
const inputReady = useKeyboardInput((input, key) => {
|
|
15
15
|
if (finishedRef.current || !isInputActive || availableRows <= 0)
|
|
16
16
|
return;
|
|
17
17
|
if (key.escape || (key.ctrl && input === "c")) {
|
|
@@ -30,7 +30,7 @@ export const SelectCommandView = ({ candidates, availableRows, isInputActive = t
|
|
|
30
30
|
onSelect(candidates[activeIndexRef.current]);
|
|
31
31
|
}
|
|
32
32
|
});
|
|
33
|
-
if (availableRows <= 0) {
|
|
33
|
+
if (!inputReady || availableRows <= 0) {
|
|
34
34
|
return null;
|
|
35
35
|
}
|
|
36
36
|
const currentCandidate = candidates[activeIndex];
|