minimal-agent 0.18.15 → 0.18.16
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/package.json +1 -1
- package/src/cli/configMenu.js +19 -2
- package/src/cli/configMenu.tsx +25 -2
- package/src/cli/wizards/networkProxyWizard.js +69 -11
- package/src/cli/wizards/networkProxyWizard.tsx +148 -26
- package/src/llm/client.js +5 -2
- package/src/llm/client.ts +5 -2
- package/src/llm/gatewayClient.js +2 -1
- package/src/llm/gatewayClient.ts +2 -1
- package/src/main.js +9 -0
- package/src/main.tsx +9 -0
- package/src/network/httpFetch.js +30 -0
- package/src/network/httpFetch.ts +42 -0
- package/src/network/proxy.js +38 -3
- package/src/network/proxy.ts +48 -3
- package/src/network/proxyProbe.js +193 -0
- package/src/network/proxyProbe.ts +244 -0
- package/src/network/proxyRuntime.js +110 -0
- package/src/network/proxyRuntime.ts +117 -0
- package/src/ui/App.js +49 -1
- package/src/ui/App.tsx +53 -1
- package/src/ui/configRefresh.js +34 -0
- package/src/ui/configRefresh.ts +62 -0
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ============================================================
|
|
3
|
+
* src/network/proxyRuntime.ts —— 代理运行时环境检查与按需部署
|
|
4
|
+
* ------------------------------------------------------------
|
|
5
|
+
* Node 24+ 内置 fetch 的代理门控(NODE_USE_ENV_PROXY + HTTPS_PROXY)只在
|
|
6
|
+
* 进程启动的瞬间读取一次,应用运行后再改环境变量不生效。因此在 /config 中
|
|
7
|
+
* 开启代理后,已存在进程的「全局 fetch 链路」(浏览器守护进程、插件/技能
|
|
8
|
+
* 子进程等)无法中途切换——唯一可靠的部署方式是带着正确的环境变量重新
|
|
9
|
+
* 拉起本进程一次。
|
|
10
|
+
*
|
|
11
|
+
* 注意:基座模型请求走 src/network/httpFetch.ts(undici 同实例 dispatcher),
|
|
12
|
+
* 不依赖本模块,/config 保存后立即生效、无需重启。本模块只负责让其余网络栈
|
|
13
|
+
* 与模型链路保持一致。
|
|
14
|
+
* ============================================================
|
|
15
|
+
*/
|
|
16
|
+
import { spawn } from 'node:child_process';
|
|
17
|
+
|
|
18
|
+
import type { NetworkProxyConfig } from '../config/configFile.js';
|
|
19
|
+
import { BOOT_PROXY_ENV, proxyUrl } from './proxy.js';
|
|
20
|
+
|
|
21
|
+
/** 防止异常情况下反复重拉进程的哨兵环境变量。 */
|
|
22
|
+
const RELAUNCH_SENTINEL = 'MINIMAL_AGENT_PROXY_RELAUNCHED';
|
|
23
|
+
const LOOPBACK_BYPASS = 'localhost,127.0.0.1,::1';
|
|
24
|
+
|
|
25
|
+
function nodeMajor(): number {
|
|
26
|
+
return Number(process.versions.node?.split('.')[0] ?? 0) || 0;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* 当前进程的启动环境是否已经能让 Node 内置网络栈走配置中的代理。
|
|
31
|
+
* - 代理关闭:视为就绪(本就不需要代理环境);
|
|
32
|
+
* - Node 23 及以下:包级 setGlobalDispatcher 对内置 fetch 仍有效,视为就绪;
|
|
33
|
+
* - Node 24+:必须启动时就带上门控与匹配的代理 URL。
|
|
34
|
+
*/
|
|
35
|
+
export function nativeProxyRuntimeReady(config?: NetworkProxyConfig): boolean {
|
|
36
|
+
if (!config?.enabled) return true;
|
|
37
|
+
if (nodeMajor() < 24) return true;
|
|
38
|
+
// host 非法(只可能来自 env 覆盖,落盘配置已经向导校验)时不重拉:
|
|
39
|
+
// applyNetworkProxy 同样会忽略它走直连,两条链路保持一致,且不炸启动。
|
|
40
|
+
let url: string;
|
|
41
|
+
try {
|
|
42
|
+
url = proxyUrl(config);
|
|
43
|
+
} catch {
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
return (
|
|
47
|
+
BOOT_PROXY_ENV.useEnvProxy &&
|
|
48
|
+
(BOOT_PROXY_ENV.httpsProxy === url || BOOT_PROXY_ENV.httpProxy === url)
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 需要时带着代理环境变量重新拉起当前进程。
|
|
54
|
+
*
|
|
55
|
+
* @returns relaunched=true 表示新进程已接管;调用方必须立即停止后续动作
|
|
56
|
+
* (当前进程会在子进程退出时以相同退出码结束)。
|
|
57
|
+
*/
|
|
58
|
+
export async function relaunchWithProxyEnv(
|
|
59
|
+
config: NetworkProxyConfig,
|
|
60
|
+
): Promise<{ relaunched: boolean; reason?: string }> {
|
|
61
|
+
if (nativeProxyRuntimeReady(config)) return { relaunched: false };
|
|
62
|
+
if (process.env[RELAUNCH_SENTINEL] === '1') {
|
|
63
|
+
// 已重拉过仍不匹配:放弃自动部署(模型链路有 httpFetch 兜底,不影响对话)。
|
|
64
|
+
return { relaunched: false, reason: 'sentinel-present' };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const url = proxyUrl(config);
|
|
68
|
+
const noProxy = [LOOPBACK_BYPASS, process.env.NO_PROXY, process.env.no_proxy]
|
|
69
|
+
.filter((value) => typeof value === 'string' && value.trim())
|
|
70
|
+
.join(',');
|
|
71
|
+
const child = spawn(process.execPath, process.argv.slice(1), {
|
|
72
|
+
stdio: 'inherit',
|
|
73
|
+
env: {
|
|
74
|
+
...process.env,
|
|
75
|
+
NODE_USE_ENV_PROXY: '1',
|
|
76
|
+
HTTP_PROXY: url,
|
|
77
|
+
HTTPS_PROXY: url,
|
|
78
|
+
ALL_PROXY: url,
|
|
79
|
+
http_proxy: url,
|
|
80
|
+
https_proxy: url,
|
|
81
|
+
all_proxy: url,
|
|
82
|
+
NO_PROXY: noProxy,
|
|
83
|
+
no_proxy: noProxy,
|
|
84
|
+
[RELAUNCH_SENTINEL]: '1',
|
|
85
|
+
},
|
|
86
|
+
});
|
|
87
|
+
const forwardSignal = (signal: NodeJS.Signals): void => {
|
|
88
|
+
try {
|
|
89
|
+
child.kill(signal);
|
|
90
|
+
} catch {
|
|
91
|
+
// 子进程可能已退出,忽略转发失败。
|
|
92
|
+
}
|
|
93
|
+
};
|
|
94
|
+
process.on('SIGINT', forwardSignal);
|
|
95
|
+
process.on('SIGTERM', forwardSignal);
|
|
96
|
+
|
|
97
|
+
return new Promise((resolve) => {
|
|
98
|
+
child.once('error', () => resolve({ relaunched: false, reason: 'spawn-failed' }));
|
|
99
|
+
child.once('exit', (code, signal) => {
|
|
100
|
+
if (signal) process.kill(process.pid, signal);
|
|
101
|
+
else process.exit(code ?? 0);
|
|
102
|
+
});
|
|
103
|
+
resolve({ relaunched: true });
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* 启动引导:配置启用代理但启动环境未部署时,重拉一次进程。
|
|
109
|
+
* @returns true 表示已重拉,调用方应当即 return,不再继续初始化。
|
|
110
|
+
*/
|
|
111
|
+
export async function bootstrapProxyRuntime(
|
|
112
|
+
config?: NetworkProxyConfig,
|
|
113
|
+
): Promise<boolean> {
|
|
114
|
+
if (!config?.enabled) return false;
|
|
115
|
+
const result = await relaunchWithProxyEnv(config);
|
|
116
|
+
return result.relaunched;
|
|
117
|
+
}
|
package/src/ui/App.js
CHANGED
|
@@ -34,6 +34,7 @@ import { LiveArea } from './LiveArea.js';
|
|
|
34
34
|
import { MessageList } from './MessageList.js';
|
|
35
35
|
import { ModelPicker } from './ModelPicker.js';
|
|
36
36
|
import { effortChoicesText, parseEffortCommand } from './effortCommand.js';
|
|
37
|
+
import { carrySessionEffort, modelIdToReload, snapshotForConfig, } from './configRefresh.js';
|
|
37
38
|
import { QueuedMessagesBanner } from './QueuedMessagesBanner.js';
|
|
38
39
|
import { SessionPicker } from './SessionPicker.js';
|
|
39
40
|
import { StatusLine } from './StatusLine.js';
|
|
@@ -162,6 +163,8 @@ export function App({ provider, initialHistory }) {
|
|
|
162
163
|
const [workspacePicker, setWorkspacePicker] = React.useState(null);
|
|
163
164
|
// /config —— 硬编码菜单覆盖层,纯本地 UI 态(与 sessionPicker 同构)。
|
|
164
165
|
const [configMenuOpen, setConfigMenuOpen] = React.useState(false);
|
|
166
|
+
// 打开 /config 时会话跑在哪条模型配置上;关闭时据此让运行中的 provider 跟上配置
|
|
167
|
+
const configOpenRef = React.useRef(null);
|
|
165
168
|
// /model —— 基座模型选择器;/advisor select —— 顾问模型选择器
|
|
166
169
|
const [baseModelPicker, setBaseModelPicker] = React.useState(null);
|
|
167
170
|
const [advisorModelPicker, setAdvisorModelPicker] = React.useState(null);
|
|
@@ -271,6 +274,49 @@ export function App({ provider, initialHistory }) {
|
|
|
271
274
|
chat.ephemeralLine,
|
|
272
275
|
compactBeforeShrinkingContext,
|
|
273
276
|
]);
|
|
277
|
+
/**
|
|
278
|
+
* /config 关闭后:状态栏的模型名/上下文大小/压缩阈值都读 providerRef,不刷新就一直是
|
|
279
|
+
* 打开前的旧值(把上下文改到 512K,状态栏仍是 204.8K)。重新解析会话所用的那条配置,
|
|
280
|
+
* 上下文变小时先用旧 provider 压缩一次(与 /model 同规则),再切换。
|
|
281
|
+
*/
|
|
282
|
+
const handleConfigDone = React.useCallback(async () => {
|
|
283
|
+
setConfigMenuOpen(false);
|
|
284
|
+
const open = configOpenRef.current;
|
|
285
|
+
configOpenRef.current = null;
|
|
286
|
+
if (!open)
|
|
287
|
+
return;
|
|
288
|
+
const { config } = await configService.getSnapshot();
|
|
289
|
+
const targetId = modelIdToReload(open, config);
|
|
290
|
+
if (!targetId)
|
|
291
|
+
return;
|
|
292
|
+
const resolved = await configService.providerForModel(targetId);
|
|
293
|
+
if (!resolved)
|
|
294
|
+
return;
|
|
295
|
+
const current = chat.getCurrentProvider();
|
|
296
|
+
const next = carrySessionEffort(current, open, resolved);
|
|
297
|
+
const unchanged = JSON.stringify(current) === JSON.stringify(next);
|
|
298
|
+
if (unchanged)
|
|
299
|
+
return;
|
|
300
|
+
if (config?.baseModelId !== open.baseModelId) {
|
|
301
|
+
await updateSessionMetadata(getWorkingDir(), chat.activeSessionId, {
|
|
302
|
+
modelId: null,
|
|
303
|
+
preferredModel: { source: 'local', modelId: targetId },
|
|
304
|
+
effectiveModel: { source: 'local', modelId: targetId },
|
|
305
|
+
fallbackReason: undefined,
|
|
306
|
+
restoreWhenAvailable: false,
|
|
307
|
+
});
|
|
308
|
+
}
|
|
309
|
+
await compactBeforeShrinkingContext(next);
|
|
310
|
+
chat.setProvider(next);
|
|
311
|
+
forceRerender((v) => v + 1);
|
|
312
|
+
chat.ephemeralLine(`[/config] 当前 Session 已采用新配置:${next.name}(上下文 ${next.contextWindow}),下一轮对话生效。`);
|
|
313
|
+
}, [
|
|
314
|
+
chat.getCurrentProvider,
|
|
315
|
+
chat.setProvider,
|
|
316
|
+
chat.activeSessionId,
|
|
317
|
+
chat.ephemeralLine,
|
|
318
|
+
compactBeforeShrinkingContext,
|
|
319
|
+
]);
|
|
274
320
|
React.useEffect(() => {
|
|
275
321
|
let cancelled = false;
|
|
276
322
|
void listSessionSummaries(getWorkingDir())
|
|
@@ -962,6 +1008,8 @@ export function App({ provider, initialHistory }) {
|
|
|
962
1008
|
// 精确匹配,不接受多余文字(与 /advisor 等硬拦截命令同规则)。仅交互式 TUI
|
|
963
1009
|
// 支持;-p 非交互模式没有 Ink 树可渲染菜单,此命令在那里就是普通聊天文本。
|
|
964
1010
|
if (trimmed === '/config') {
|
|
1011
|
+
const { config } = await configService.getSnapshot();
|
|
1012
|
+
configOpenRef.current = snapshotForConfig(chat.getCurrentProvider(), config);
|
|
965
1013
|
setConfigMenuOpen(true);
|
|
966
1014
|
return;
|
|
967
1015
|
}
|
|
@@ -999,7 +1047,7 @@ export function App({ provider, initialHistory }) {
|
|
|
999
1047
|
drafts.current.set(draftKey, { current: { text: '', cursor: 0 } });
|
|
1000
1048
|
const advisorLabel = !advisorStatus.configured ? '未配置' : !advisorStatus.enabled ? '已关闭' : advisorStatus.mode === 'manual' ? '仅手动' : '自动启用';
|
|
1001
1049
|
const bannerText = `minimal-agent · ${currentProvider.name}/${currentProvider.model} · /model 切模型 · /effort 思考强度 · /computer 控制桌面 · /copy 复制回复 · /paste-image 添加图片 · /new 清空 · /compact 压缩 · Ctrl+C 中断`;
|
|
1002
|
-
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(MessageList, { history: chat.history, compactGeneration: chat.compactGeneration, bannerText: `${bannerText} · 顾问:${advisorLabel}(/advisor status)`, terminalRows: terminalRows }), _jsxs(Box, { flexDirection: "column", width: Math.max(1, terminalCols - 1), children: [!configMenuOpen && !baseModelPicker && !advisorModelPicker && !workspacePicker && !sessionPicker && !chat.pendingAsk && _jsx(LiveArea, { progressStage: chat.progressStage, reasoningPreview: chat.reasoningPreview, streamingText: chat.streamingText, runningTools: chat.runningTools, pluginProgress: chat.pluginProgress, transientError: chat.transientError, progressNotice: chat.progressNotice, terminalCols: terminalCols, terminalRows: terminalRows, reservedRows: chromeRows + 3 }), _jsxs(Box, { flexDirection: "column", ref: chromeRef, children: [_jsx(QueuedMessagesBanner, { items: chat.queuedItems }), chat.pendingComputerIntervention && (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: theme.warning, paddingX: 1, marginBottom: 1, children: [_jsxs(Text, { color: theme.warning, children: ["\u9700\u8981\u4EBA\u5DE5\u64CD\u4F5C\uFF1A", chat.pendingComputerIntervention.sanitizedSummary] }), _jsx(Text, { dimColor: true, children: "\u8BF7\u76F4\u63A5\u5728\u76EE\u6807\u5E94\u7528\u4E2D\u5904\u7406\uFF1B\u654F\u611F\u5185\u5BB9\u4E0D\u4F1A\u8FDB\u5165\u7EC8\u7AEF\u6216\u6A21\u578B\u3002\u5B8C\u6210\u540E\u8F93\u5165 /computer resume\uFF0C\u53D6\u6D88\u8F93\u5165 /computer cancel\u3002" })] })), chat.error && (_jsx(Box, { marginBottom: 1, children: _jsxs(Text, { color: theme.error, wrap: "truncate-end", children: ["\u26A0 ", terminalText(chat.error)] }) })), chat.interrupted && (_jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { color: theme.warning, children: "\u26A1 \u64CD\u4F5C\u88AB\u7528\u6237\u4E2D\u65AD\uFF0C\u7B49\u5F85\u65B0\u7684\u4EFB\u52A1\u8F93\u5165..." }), _jsxs(Text, { dimColor: true, children: [' ', "\u8C03\u6574\u4E0A\u4E00\u6B21\u8BA1\u5212\uFF1A\u76F4\u63A5\u8BF4\u8C03\u6574\u5185\u5BB9 / \u91CD\u65B0\u8DD1 /workflow plan \u76EE\u6807 / \u65B0\u4EFB\u52A1\u76F4\u63A5\u8BF4"] })] })), configMenuOpen ? (_jsx(ConfigMenu, { mode: "reconfigure", onDone: () =>
|
|
1050
|
+
return (_jsxs(Box, { flexDirection: "column", children: [_jsx(MessageList, { history: chat.history, compactGeneration: chat.compactGeneration, bannerText: `${bannerText} · 顾问:${advisorLabel}(/advisor status)`, terminalRows: terminalRows }), _jsxs(Box, { flexDirection: "column", width: Math.max(1, terminalCols - 1), children: [!configMenuOpen && !baseModelPicker && !advisorModelPicker && !workspacePicker && !sessionPicker && !chat.pendingAsk && _jsx(LiveArea, { progressStage: chat.progressStage, reasoningPreview: chat.reasoningPreview, streamingText: chat.streamingText, runningTools: chat.runningTools, pluginProgress: chat.pluginProgress, transientError: chat.transientError, progressNotice: chat.progressNotice, terminalCols: terminalCols, terminalRows: terminalRows, reservedRows: chromeRows + 3 }), _jsxs(Box, { flexDirection: "column", ref: chromeRef, children: [_jsx(QueuedMessagesBanner, { items: chat.queuedItems }), chat.pendingComputerIntervention && (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: theme.warning, paddingX: 1, marginBottom: 1, children: [_jsxs(Text, { color: theme.warning, children: ["\u9700\u8981\u4EBA\u5DE5\u64CD\u4F5C\uFF1A", chat.pendingComputerIntervention.sanitizedSummary] }), _jsx(Text, { dimColor: true, children: "\u8BF7\u76F4\u63A5\u5728\u76EE\u6807\u5E94\u7528\u4E2D\u5904\u7406\uFF1B\u654F\u611F\u5185\u5BB9\u4E0D\u4F1A\u8FDB\u5165\u7EC8\u7AEF\u6216\u6A21\u578B\u3002\u5B8C\u6210\u540E\u8F93\u5165 /computer resume\uFF0C\u53D6\u6D88\u8F93\u5165 /computer cancel\u3002" })] })), chat.error && (_jsx(Box, { marginBottom: 1, children: _jsxs(Text, { color: theme.error, wrap: "truncate-end", children: ["\u26A0 ", terminalText(chat.error)] }) })), chat.interrupted && (_jsxs(Box, { marginBottom: 1, children: [_jsx(Text, { color: theme.warning, children: "\u26A1 \u64CD\u4F5C\u88AB\u7528\u6237\u4E2D\u65AD\uFF0C\u7B49\u5F85\u65B0\u7684\u4EFB\u52A1\u8F93\u5165..." }), _jsxs(Text, { dimColor: true, children: [' ', "\u8C03\u6574\u4E0A\u4E00\u6B21\u8BA1\u5212\uFF1A\u76F4\u63A5\u8BF4\u8C03\u6574\u5185\u5BB9 / \u91CD\u65B0\u8DD1 /workflow plan \u76EE\u6807 / \u65B0\u4EFB\u52A1\u76F4\u63A5\u8BF4"] })] })), configMenuOpen ? (_jsx(ConfigMenu, { mode: "reconfigure", onDone: () => void handleConfigDone() })) : baseModelPicker ? (_jsx(ModelPicker, { models: baseModelPicker.models, currentModelId: baseModelPicker.currentId, title: "\u9009\u62E9\u57FA\u5EA7\u6A21\u578B", onSelect: handleBaseModelSelect, onCancel: () => setBaseModelPicker(null) })) : advisorModelPicker ? (_jsx(ModelPicker, { models: advisorModelPicker.models, currentModelId: advisorModelPicker.currentId, title: "\u9009\u62E9\u987E\u95EE\u6A21\u578B", nullOptionLabel: "\u25CB \u4E0D\u4F7F\u7528\u987E\u95EE\u6A21\u578B", onSelect: handleAdvisorModelSelect, onCancel: () => setAdvisorModelPicker(null) })) : workspacePicker ? (_jsx(WorkspacePicker, { workspaces: workspacePicker, currentPath: getWorkingDir(), currentSessionId: chat.activeSessionId, onSelect: handleWorkspaceSelect, onCancel: () => setWorkspacePicker(null) })) : sessionPicker ? (_jsx(SessionPicker, { sessions: sessionPicker, activeSessionId: chat.activeSessionId, onSelect: handleSessionSelect, onCreate: handleSessionCreate, onCancel: () => setSessionPicker(null) })) : chat.pendingAsk ? (_jsx(AskQuestion, { questions: chat.pendingAsk, source: chat.pendingAskSource, queuedCount: chat.pendingAskQueueCount, onAnswer: chat.answerAsk, onCancel: chat.cancelAsk })) : (
|
|
1003
1051
|
/* handleSubmit 包装做 slash 命令拦截,非命令透传 chat.submit */
|
|
1004
1052
|
_jsx(InputBox, { draft: drafts.current.get(draftKey), onHeight: setInputRows, onSubmit: handleSubmit, disabled: chat.isLoading && !chat.pendingComputerIntervention, onAbort: chat.abort, onClear: handleClear, onCompact: chat.compactNow, onWorkspaces: openWorkspacePicker }, draftKey)), _jsx(StatusLine, { provider: currentProvider, history: chat.history, pluginProgress: chat.pluginProgress, contextUsage: chat.contextUsage })] })] })] }));
|
|
1005
1053
|
}
|
package/src/ui/App.tsx
CHANGED
|
@@ -57,6 +57,12 @@ import { LiveArea } from './LiveArea.js';
|
|
|
57
57
|
import { MessageList } from './MessageList.js';
|
|
58
58
|
import { ModelPicker } from './ModelPicker.js';
|
|
59
59
|
import { effortChoicesText, parseEffortCommand } from './effortCommand.js';
|
|
60
|
+
import {
|
|
61
|
+
carrySessionEffort,
|
|
62
|
+
modelIdToReload,
|
|
63
|
+
snapshotForConfig,
|
|
64
|
+
type ConfigOpenSnapshot,
|
|
65
|
+
} from './configRefresh.js';
|
|
60
66
|
import type { PickerModel } from './ModelPicker.js';
|
|
61
67
|
import { QueuedMessagesBanner } from './QueuedMessagesBanner.js';
|
|
62
68
|
import { SessionPicker } from './SessionPicker.js';
|
|
@@ -212,6 +218,8 @@ export function App({ provider, initialHistory }: Props): JSX.Element {
|
|
|
212
218
|
|
|
213
219
|
// /config —— 硬编码菜单覆盖层,纯本地 UI 态(与 sessionPicker 同构)。
|
|
214
220
|
const [configMenuOpen, setConfigMenuOpen] = React.useState(false);
|
|
221
|
+
// 打开 /config 时会话跑在哪条模型配置上;关闭时据此让运行中的 provider 跟上配置
|
|
222
|
+
const configOpenRef = React.useRef<ConfigOpenSnapshot | null>(null);
|
|
215
223
|
|
|
216
224
|
// /model —— 基座模型选择器;/advisor select —— 顾问模型选择器
|
|
217
225
|
const [baseModelPicker, setBaseModelPicker] = React.useState<{
|
|
@@ -357,6 +365,48 @@ export function App({ provider, initialHistory }: Props): JSX.Element {
|
|
|
357
365
|
],
|
|
358
366
|
);
|
|
359
367
|
|
|
368
|
+
/**
|
|
369
|
+
* /config 关闭后:状态栏的模型名/上下文大小/压缩阈值都读 providerRef,不刷新就一直是
|
|
370
|
+
* 打开前的旧值(把上下文改到 512K,状态栏仍是 204.8K)。重新解析会话所用的那条配置,
|
|
371
|
+
* 上下文变小时先用旧 provider 压缩一次(与 /model 同规则),再切换。
|
|
372
|
+
*/
|
|
373
|
+
const handleConfigDone = React.useCallback(async () => {
|
|
374
|
+
setConfigMenuOpen(false);
|
|
375
|
+
const open = configOpenRef.current;
|
|
376
|
+
configOpenRef.current = null;
|
|
377
|
+
if (!open) return;
|
|
378
|
+
const { config } = await configService.getSnapshot();
|
|
379
|
+
const targetId = modelIdToReload(open, config);
|
|
380
|
+
if (!targetId) return;
|
|
381
|
+
const resolved = await configService.providerForModel(targetId);
|
|
382
|
+
if (!resolved) return;
|
|
383
|
+
const current = chat.getCurrentProvider();
|
|
384
|
+
const next = carrySessionEffort(current, open, resolved);
|
|
385
|
+
const unchanged = JSON.stringify(current) === JSON.stringify(next);
|
|
386
|
+
if (unchanged) return;
|
|
387
|
+
if (config?.baseModelId !== open.baseModelId) {
|
|
388
|
+
await updateSessionMetadata(getWorkingDir(), chat.activeSessionId, {
|
|
389
|
+
modelId: null,
|
|
390
|
+
preferredModel: { source: 'local', modelId: targetId },
|
|
391
|
+
effectiveModel: { source: 'local', modelId: targetId },
|
|
392
|
+
fallbackReason: undefined,
|
|
393
|
+
restoreWhenAvailable: false,
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
await compactBeforeShrinkingContext(next);
|
|
397
|
+
chat.setProvider(next);
|
|
398
|
+
forceRerender((v) => v + 1);
|
|
399
|
+
chat.ephemeralLine(
|
|
400
|
+
`[/config] 当前 Session 已采用新配置:${next.name}(上下文 ${next.contextWindow}),下一轮对话生效。`,
|
|
401
|
+
);
|
|
402
|
+
}, [
|
|
403
|
+
chat.getCurrentProvider,
|
|
404
|
+
chat.setProvider,
|
|
405
|
+
chat.activeSessionId,
|
|
406
|
+
chat.ephemeralLine,
|
|
407
|
+
compactBeforeShrinkingContext,
|
|
408
|
+
]);
|
|
409
|
+
|
|
360
410
|
React.useEffect(() => {
|
|
361
411
|
let cancelled = false;
|
|
362
412
|
void listSessionSummaries(getWorkingDir())
|
|
@@ -1218,6 +1268,8 @@ export function App({ provider, initialHistory }: Props): JSX.Element {
|
|
|
1218
1268
|
// 精确匹配,不接受多余文字(与 /advisor 等硬拦截命令同规则)。仅交互式 TUI
|
|
1219
1269
|
// 支持;-p 非交互模式没有 Ink 树可渲染菜单,此命令在那里就是普通聊天文本。
|
|
1220
1270
|
if (trimmed === '/config') {
|
|
1271
|
+
const { config } = await configService.getSnapshot();
|
|
1272
|
+
configOpenRef.current = snapshotForConfig(chat.getCurrentProvider(), config);
|
|
1221
1273
|
setConfigMenuOpen(true);
|
|
1222
1274
|
return;
|
|
1223
1275
|
}
|
|
@@ -1342,7 +1394,7 @@ export function App({ provider, initialHistory }: Props): JSX.Element {
|
|
|
1342
1394
|
{configMenuOpen ? (
|
|
1343
1395
|
<ConfigMenu
|
|
1344
1396
|
mode="reconfigure"
|
|
1345
|
-
onDone={() =>
|
|
1397
|
+
onDone={() => void handleConfigDone()}
|
|
1346
1398
|
/>
|
|
1347
1399
|
) : baseModelPicker ? (
|
|
1348
1400
|
<ModelPicker
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export function snapshotForConfig(current, config) {
|
|
2
|
+
const entry = current.source === 'gateway'
|
|
3
|
+
? undefined
|
|
4
|
+
: config?.models.find((m) => m.baseURL === current.baseURL && m.model === current.model);
|
|
5
|
+
return {
|
|
6
|
+
modelId: entry?.id ?? null,
|
|
7
|
+
baseModelId: config?.baseModelId ?? null,
|
|
8
|
+
...(entry?.reasoningEffort ? { configEffort: entry.reasoningEffort } : {}),
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
/** 关闭 /config 后应重新解析的模型 id;null = 不动当前 provider。 */
|
|
12
|
+
export function modelIdToReload(open, config) {
|
|
13
|
+
if (!config)
|
|
14
|
+
return null;
|
|
15
|
+
// 用户在 /config 里换了基座:会话跟随(这是他刚做的明确选择)。
|
|
16
|
+
if (config.baseModelId !== open.baseModelId)
|
|
17
|
+
return config.baseModelId ?? null;
|
|
18
|
+
if (!open.modelId)
|
|
19
|
+
return null;
|
|
20
|
+
if (config.models.some((m) => m.id === open.modelId))
|
|
21
|
+
return open.modelId;
|
|
22
|
+
return config.baseModelId ?? null; // 会话用的模型被删了 → 退回基座
|
|
23
|
+
}
|
|
24
|
+
/** 配置里的推理强度没动过时,保留会话里 /effort 设的值;改了就以配置为准。 */
|
|
25
|
+
export function carrySessionEffort(current, open, next) {
|
|
26
|
+
const configUnchanged = open.configEffort === next.reasoningEffort;
|
|
27
|
+
const sessionDiffers = current.reasoningEffort !== open.configEffort;
|
|
28
|
+
if (!configUnchanged || !sessionDiffers)
|
|
29
|
+
return next;
|
|
30
|
+
const { reasoningEffort: _dropped, ...rest } = next;
|
|
31
|
+
return (current.reasoningEffort
|
|
32
|
+
? { ...rest, reasoningEffort: current.reasoningEffort }
|
|
33
|
+
: rest);
|
|
34
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* /config 关闭后让正在运行的会话跟上配置(纯逻辑,便于测试)。
|
|
3
|
+
*
|
|
4
|
+
* 状态栏的模型名、上下文大小、压缩阈值都读 useChat 的 providerRef,而它只在启动和
|
|
5
|
+
* /model、/effort 时更新——在 /config 里改了上下文窗口,运行中的会话仍用旧值。
|
|
6
|
+
* 流程:打开 /config 时 snapshotForConfig 记下"会话跑在哪条模型配置上",关闭时
|
|
7
|
+
* modelIdToReload 决定重新解析哪条,carrySessionEffort 保住 /effort 的会话级覆盖。
|
|
8
|
+
*/
|
|
9
|
+
import type { MinimalAgentConfig } from '../config/configFile.js';
|
|
10
|
+
import type { ReasoningEffort } from '../config/reasoningEffort.js';
|
|
11
|
+
import type { Provider } from '../types.js';
|
|
12
|
+
|
|
13
|
+
export interface ConfigOpenSnapshot {
|
|
14
|
+
/** 会话当前所用的本机模型配置 id;在线模型或匹配不上时为 null。 */
|
|
15
|
+
modelId: string | null;
|
|
16
|
+
baseModelId: string | null;
|
|
17
|
+
/** 该配置条目里写的推理强度(用来区分「会话级 /effort 覆盖」与「配置值」)。 */
|
|
18
|
+
configEffort?: ReasoningEffort;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function snapshotForConfig(
|
|
22
|
+
current: Provider,
|
|
23
|
+
config: MinimalAgentConfig | null,
|
|
24
|
+
): ConfigOpenSnapshot {
|
|
25
|
+
const entry =
|
|
26
|
+
current.source === 'gateway'
|
|
27
|
+
? undefined
|
|
28
|
+
: config?.models.find((m) => m.baseURL === current.baseURL && m.model === current.model);
|
|
29
|
+
return {
|
|
30
|
+
modelId: entry?.id ?? null,
|
|
31
|
+
baseModelId: config?.baseModelId ?? null,
|
|
32
|
+
...(entry?.reasoningEffort ? { configEffort: entry.reasoningEffort } : {}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** 关闭 /config 后应重新解析的模型 id;null = 不动当前 provider。 */
|
|
37
|
+
export function modelIdToReload(
|
|
38
|
+
open: ConfigOpenSnapshot,
|
|
39
|
+
config: MinimalAgentConfig | null,
|
|
40
|
+
): string | null {
|
|
41
|
+
if (!config) return null;
|
|
42
|
+
// 用户在 /config 里换了基座:会话跟随(这是他刚做的明确选择)。
|
|
43
|
+
if (config.baseModelId !== open.baseModelId) return config.baseModelId ?? null;
|
|
44
|
+
if (!open.modelId) return null;
|
|
45
|
+
if (config.models.some((m) => m.id === open.modelId)) return open.modelId;
|
|
46
|
+
return config.baseModelId ?? null; // 会话用的模型被删了 → 退回基座
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/** 配置里的推理强度没动过时,保留会话里 /effort 设的值;改了就以配置为准。 */
|
|
50
|
+
export function carrySessionEffort(
|
|
51
|
+
current: Provider,
|
|
52
|
+
open: ConfigOpenSnapshot,
|
|
53
|
+
next: Provider,
|
|
54
|
+
): Provider {
|
|
55
|
+
const configUnchanged = open.configEffort === next.reasoningEffort;
|
|
56
|
+
const sessionDiffers = current.reasoningEffort !== open.configEffort;
|
|
57
|
+
if (!configUnchanged || !sessionDiffers) return next;
|
|
58
|
+
const { reasoningEffort: _dropped, ...rest } = next;
|
|
59
|
+
return (current.reasoningEffort
|
|
60
|
+
? { ...rest, reasoningEffort: current.reasoningEffort }
|
|
61
|
+
: rest) as Provider;
|
|
62
|
+
}
|