@unscientificjszhai/howto 1.0.2 → 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 +88 -29
- package/{README.CN.md → README.zh-hans-cn.md} +84 -25
- package/THIRD_PARTY_NOTICES.md +1 -0
- package/dist/ai/chatgpt-models.js +148 -0
- package/dist/ai/chatgpt.js +495 -0
- package/dist/ai/command-schema.js +8 -0
- 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 +10 -3
- package/dist/errors.js +4 -0
- package/dist/index.js +11 -4
- package/dist/init/ChatGptInitializationStep.js +223 -0
- package/dist/init/InitializationApp.js +71 -37
- package/dist/init/index.js +24 -5
- package/dist/init/state.js +14 -3
- package/dist/prompt.js +36 -6
- package/dist/shell/command-analysis.js +24 -4
- package/dist/ui/App.js +99 -10
- package/dist/ui/ConfirmView.js +6 -39
- package/dist/ui/InteractiveSessionProvider.js +7 -8
- package/dist/ui/ResolvePlaceholdersView.js +6 -1
- package/dist/ui/UsageApprovalView.js +320 -0
- package/dist/ui/interactive-session.js +180 -39
- 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 +6 -1
- package/dist/ui/use-paste-aware-input.js +8 -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/usage-response.js +39 -0
- package/package.json +21 -19
|
@@ -118,6 +118,19 @@ export function parseShellCommand(command) {
|
|
|
118
118
|
commands.push({ words });
|
|
119
119
|
return commands.length > 0 ? { kind: "parsed", commands } : unsupported("missing-executable");
|
|
120
120
|
}
|
|
121
|
+
/** 仅解析 argv 字面量;保留 lexer 的重定向信息,不能复用命令解析的过滤结果。 */
|
|
122
|
+
export function parseLiteralArguments(text) {
|
|
123
|
+
const lexed = lexShellCommand(text, true);
|
|
124
|
+
if (lexed.kind === "unsupported") {
|
|
125
|
+
throw new Error("Usage lookup arguments must contain only literal arguments");
|
|
126
|
+
}
|
|
127
|
+
return lexed.tokens.map((token) => {
|
|
128
|
+
if (token.kind !== "word" || token.word.hasExpansion) {
|
|
129
|
+
throw new Error("Usage lookup arguments must contain only literal arguments");
|
|
130
|
+
}
|
|
131
|
+
return token.word.value;
|
|
132
|
+
});
|
|
133
|
+
}
|
|
121
134
|
export function resolveCommandPrefix(words) {
|
|
122
135
|
let index = 0;
|
|
123
136
|
while (words[index]?.assignmentOperator === "=")
|
|
@@ -218,7 +231,7 @@ function skipWrapperOptions(words, start, wrapper) {
|
|
|
218
231
|
}
|
|
219
232
|
return index;
|
|
220
233
|
}
|
|
221
|
-
function lexShellCommand(command) {
|
|
234
|
+
function lexShellCommand(command, literalsOnly = false) {
|
|
222
235
|
const tokens = [];
|
|
223
236
|
let index = 0;
|
|
224
237
|
while (index < command.length) {
|
|
@@ -228,10 +241,14 @@ function lexShellCommand(command) {
|
|
|
228
241
|
continue;
|
|
229
242
|
}
|
|
230
243
|
if (char === "\\" && command[index + 1] === "\n") {
|
|
244
|
+
if (literalsOnly)
|
|
245
|
+
return unsupported("syntax");
|
|
231
246
|
index += 2;
|
|
232
247
|
continue;
|
|
233
248
|
}
|
|
234
249
|
if (char === "#") {
|
|
250
|
+
if (literalsOnly)
|
|
251
|
+
return unsupported("syntax");
|
|
235
252
|
while (index < command.length && command[index] !== "\n")
|
|
236
253
|
index += 1;
|
|
237
254
|
continue;
|
|
@@ -264,7 +281,7 @@ function lexShellCommand(command) {
|
|
|
264
281
|
}
|
|
265
282
|
continue;
|
|
266
283
|
}
|
|
267
|
-
const word = readWord(command, index);
|
|
284
|
+
const word = readWord(command, index, literalsOnly);
|
|
268
285
|
if (word.kind === "unsupported")
|
|
269
286
|
return word;
|
|
270
287
|
const rawWord = command.slice(index, word.word.end);
|
|
@@ -280,7 +297,7 @@ function lexShellCommand(command) {
|
|
|
280
297
|
}
|
|
281
298
|
return { kind: "parsed", tokens };
|
|
282
299
|
}
|
|
283
|
-
function readWord(command, start) {
|
|
300
|
+
function readWord(command, start, literalsOnly = false) {
|
|
284
301
|
let index = start;
|
|
285
302
|
let value = "";
|
|
286
303
|
let quote;
|
|
@@ -302,6 +319,8 @@ function readWord(command, start) {
|
|
|
302
319
|
if (next === undefined)
|
|
303
320
|
return unsupported("syntax");
|
|
304
321
|
if (next === "\n") {
|
|
322
|
+
if (literalsOnly)
|
|
323
|
+
return unsupported("syntax");
|
|
305
324
|
index += 2;
|
|
306
325
|
continue;
|
|
307
326
|
}
|
|
@@ -351,7 +370,8 @@ function readWord(command, start) {
|
|
|
351
370
|
if ("(){}".includes(char))
|
|
352
371
|
return unsupported("syntax");
|
|
353
372
|
if ("*?[".includes(char) ||
|
|
354
|
-
(char === "~" &&
|
|
373
|
+
(char === "~" &&
|
|
374
|
+
(value === "" || (literalsOnly && (value.endsWith("=") || value.endsWith(":"))))) ||
|
|
355
375
|
(char === "=" && value === "" && assignmentEligible))
|
|
356
376
|
hasExpansion = true;
|
|
357
377
|
if (char === "=" && assignmentEligible) {
|
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";
|
|
@@ -12,10 +12,17 @@ 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();
|
|
@@ -39,19 +65,75 @@ export const App = ({ provider, request, onSuccess, onError }) => {
|
|
|
39
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) {
|
|
@@ -99,6 +182,7 @@ 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
188
|
setDanger(resolveCommandDanger(resolved.command, selectedCandidate));
|
|
@@ -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
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,12 +10,14 @@ 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
22
|
const inputReady = usePasteAwareInput({
|
|
22
23
|
onInput: (input, key) => {
|
|
@@ -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) {
|
|
@@ -85,25 +71,6 @@ export const ConfirmView = ({ command, danger, onConfirm, onCancel, isDone = fal
|
|
|
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;
|