minimal-agent 0.18.21 → 0.18.22
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 +16 -3
- package/package.json +1 -1
- package/plugins/workflow-runner/plugin.js +1 -0
- package/plugins/workflow-runner/plugin.ts +1 -0
- package/plugins/workflow-runner/src/index.js +1 -0
- package/plugins/workflow-runner/src/index.ts +2 -0
- package/plugins/workflow-runner/src/runner.js +6 -1
- package/plugins/workflow-runner/src/runner.ts +5 -1
- package/plugins/workflow-runner/src/types.ts +1 -0
- package/plugins/workflow-runner/test/childWorkflow.test.ts +19 -0
- package/src/advisor/README.md +7 -3
- package/src/advisor/advisorNudge.js +3 -3
- package/src/advisor/advisorNudge.ts +3 -3
- package/src/advisor/deliveryReview.js +9 -2
- package/src/advisor/deliveryReview.ts +10 -2
- package/src/cli/args.js +1 -0
- package/src/cli/args.ts +1 -0
- package/src/cli/headlessTurn.js +143 -0
- package/src/cli/headlessTurn.ts +187 -0
- package/src/cli/lifecycle.js +52 -0
- package/src/cli/lifecycle.ts +59 -0
- package/src/cli/print.js +91 -240
- package/src/cli/print.ts +99 -301
- package/src/cli/quickModelSetup.js +3 -0
- package/src/cli/quickModelSetup.tsx +3 -0
- package/src/cli/streamJson.js +244 -334
- package/src/cli/streamJson.ts +248 -416
- package/src/cli/wizards/providerPresets.js +25 -14
- package/src/cli/wizards/providerPresets.ts +25 -14
- package/src/cli/wizards/providerWizard.js +29 -11
- package/src/cli/wizards/providerWizard.tsx +29 -11
- package/src/config/configDiagnostics.js +7 -3
- package/src/config/configDiagnostics.ts +6 -3
- package/src/context/compact.js +40 -10
- package/src/context/compact.ts +51 -12
- package/src/context/compactResources.js +39 -0
- package/src/context/compactResources.ts +35 -0
- package/src/context/manualCompact.js +20 -0
- package/src/context/manualCompact.ts +28 -0
- package/src/llm/client.js +4 -0
- package/src/llm/client.ts +4 -0
- package/src/llm/messageProvenance.js +1 -1
- package/src/llm/messageProvenance.ts +1 -1
- package/src/loop/deliveryIntent.js +3 -2
- package/src/loop/deliveryIntent.ts +4 -2
- package/src/loop/messages.js +1 -0
- package/src/loop/messages.ts +1 -0
- package/src/loop/options.ts +2 -0
- package/src/loop.js +4 -2
- package/src/loop.ts +4 -2
- package/src/main.js +40 -45
- package/src/main.tsx +33 -52
- package/src/plugins/artifactDelivery.js +76 -0
- package/src/plugins/artifactDelivery.ts +76 -0
- package/src/plugins/pluginApi.ts +2 -0
- package/src/plugins/pluginRunner.js +2 -0
- package/src/plugins/pluginRunner.ts +3 -0
- package/src/prompts/delegation.js +1 -1
- package/src/prompts/delegation.ts +1 -1
- package/src/prompts/policyRegistry.js +7 -6
- package/src/prompts/policyRegistry.ts +7 -6
- package/src/prompts/promptAssembly.js +2 -2
- package/src/prompts/promptAssembly.ts +2 -2
- package/src/runtime/requestIntent.js +86 -0
- package/src/runtime/requestIntent.ts +66 -0
- package/src/runtime/systemEvents.js +10 -1
- package/src/runtime/systemEvents.ts +9 -1
- package/src/session/commandTranscript.js +76 -0
- package/src/session/commandTranscript.ts +77 -0
- package/src/session/runTurn.js +24 -3
- package/src/session/runTurn.ts +24 -3
- package/src/tools/advisor/advisor.js +1 -1
- package/src/tools/advisor/advisor.ts +1 -1
- package/src/tools/advisor/advisorCoordination.js +3 -2
- package/src/tools/advisor/advisorCoordination.ts +3 -2
- package/src/tools/advisor/advisorDescription.js +1 -1
- package/src/tools/advisor/advisorDescription.ts +1 -1
- package/src/tools/agent/README.md +2 -0
- package/src/tools/agent/agent.js +3 -0
- package/src/tools/agent/agent.ts +3 -0
- package/src/tools/agent/agentHandoff.js +2 -1
- package/src/tools/agent/agentHandoff.ts +2 -1
- package/src/tools/agent/agentTaskService.js +26 -5
- package/src/tools/agent/agentTaskService.ts +23 -5
- package/src/tools/agent/agentTypes.js +1 -1
- package/src/tools/agent/agentTypes.ts +1 -1
- package/src/tools/applypatch/applyPatch.js +39 -13
- package/src/tools/applypatch/applyPatch.ts +35 -15
- package/src/tools/bash/bash.js +3 -0
- package/src/tools/bash/bash.ts +3 -0
- package/src/tools/download/download.js +147 -11
- package/src/tools/download/download.ts +193 -17
- package/src/tools/edit/edit.js +4 -4
- package/src/tools/edit/edit.ts +4 -4
- package/src/tools/edit/multi-edit.js +2 -2
- package/src/tools/edit/multi-edit.ts +2 -2
- package/src/tools/powershell/powershell.js +2 -0
- package/src/tools/powershell/powershell.ts +2 -0
- package/src/tools/shared/fileUtils.js +16 -6
- package/src/tools/shared/fileUtils.ts +15 -6
- package/src/tools/tasks/backgroundTaskService.js +108 -12
- package/src/tools/tasks/backgroundTaskService.ts +106 -14
- package/src/tools/tasks/taskOutput.js +15 -5
- package/src/tools/tasks/taskOutput.ts +13 -5
- package/src/tools/tasks/taskStop.js +2 -2
- package/src/tools/tasks/taskStop.ts +2 -2
- package/src/tools/toolPromptGuides.js +3 -3
- package/src/tools/toolPromptGuides.ts +3 -3
- package/src/tools/webfetch/webfetch.js +1 -1
- package/src/tools/webfetch/webfetch.ts +1 -1
- package/src/types.ts +4 -0
- package/src/ui/InputBox.js +2 -2
- package/src/ui/InputBox.tsx +2 -2
- package/src/ui/MessageList.js +2 -0
- package/src/ui/MessageList.tsx +2 -0
- package/src/ui/README.md +8 -2
- package/src/ui/activityPresentation.js +22 -2
- package/src/ui/activityPresentation.ts +21 -2
- package/src/ui/backgroundWake.js +20 -4
- package/src/ui/backgroundWake.ts +19 -4
- package/src/ui/commandTranscript.js +2 -69
- package/src/ui/commandTranscript.ts +2 -71
- package/src/ui/hooks/useChat.js +41 -25
- package/src/ui/hooks/useChat.ts +30 -23
- package/src/web/protocol.ts +3 -2
- package/src/cli/wizards/advisorWizard.js +0 -152
- package/src/cli/wizards/advisorWizard.tsx +0 -203
package/src/ui/hooks/useChat.ts
CHANGED
|
@@ -40,7 +40,8 @@ import {
|
|
|
40
40
|
createComputerUseHostGate,
|
|
41
41
|
} from '../../cli/computerUse.js';
|
|
42
42
|
import { resolveLocalBrowserRun } from '../../cli/browser.js';
|
|
43
|
-
import {
|
|
43
|
+
import { manualCompactNotice } from '../../context/compact.js';
|
|
44
|
+
import { compactSession } from '../../context/manualCompact.js';
|
|
44
45
|
import { acquireContextLock } from '../../context/contextLock.js';
|
|
45
46
|
import {
|
|
46
47
|
clearContext,
|
|
@@ -192,7 +193,7 @@ export interface UseChatResult {
|
|
|
192
193
|
/** 清空对话上下文(保留 system prompt);isLoading 时无效 */
|
|
193
194
|
clearHistory: () => Promise<void>;
|
|
194
195
|
/** 手动压缩对话上下文(不看阈值);isLoading 时无效 */
|
|
195
|
-
compactNow: () => Promise<void>;
|
|
196
|
+
compactNow: (instructions?: string) => Promise<void>;
|
|
196
197
|
/**
|
|
197
198
|
* `/advisor ask` 手动咨询顾问模型。与工具路径共用过程显示(LiveArea 进度行)
|
|
198
199
|
* 并可被 ESC 中断;isLoading 时拒绝执行并返回带原因的失败结果。
|
|
@@ -731,8 +732,7 @@ export function useChat(args: {
|
|
|
731
732
|
if (!isWake) backgroundWake.notifyUserInput();
|
|
732
733
|
|
|
733
734
|
if (isLoadingRef.current) {
|
|
734
|
-
//
|
|
735
|
-
// 而且没必要——回合在跑时 loop 自己会在模型边界取走完成通知。
|
|
735
|
+
// 系统接续只留在后台信箱中;绝不进入会打断真人回合的输入队列。
|
|
736
736
|
if (isWake) return;
|
|
737
737
|
inputQueue.enqueue(trimmed, 'next');
|
|
738
738
|
const running = Array.from(runningToolsRef.current.values());
|
|
@@ -791,6 +791,7 @@ export function useChat(args: {
|
|
|
791
791
|
);
|
|
792
792
|
releaseContextLock = await acquireContextLock(activeFile, ac.signal);
|
|
793
793
|
if (isWake && !backgroundWake.isPending(options!.identity!, options!.continuation!.taskIds)) return;
|
|
794
|
+
if (isWake) backgroundWake.begin(options!.identity!, options!.continuation!.taskIds);
|
|
794
795
|
// Gateway/Cron may have written this cwd since the TUI rendered its last frame.
|
|
795
796
|
// Reload after acquiring the cross-process lock so this turn extends the newest history.
|
|
796
797
|
const latest = await loadContext(activeFile);
|
|
@@ -970,6 +971,7 @@ export function useChat(args: {
|
|
|
970
971
|
userMessageId,
|
|
971
972
|
chat: args.chat,
|
|
972
973
|
backgroundContinuation: options?.continuation,
|
|
974
|
+
deferBackgroundNotifications: true,
|
|
973
975
|
provider: turnProvider,
|
|
974
976
|
history: historyRef.current,
|
|
975
977
|
signal: ac.signal,
|
|
@@ -1092,6 +1094,8 @@ export function useChat(args: {
|
|
|
1092
1094
|
// 中断/失败后不再自动唤醒,等真人再说话:用户刚按下 ESC,紧接着自己
|
|
1093
1095
|
// 又跑起来是敌意行为。排队里有真人输入时,它的 notifyUserInput 会把
|
|
1094
1096
|
// 这里刚安排上的唤醒撤掉(防抖窗口远长于一个微任务)。
|
|
1097
|
+
if (isWake) backgroundWake.finish(options!.identity!, options!.continuation!.taskIds,
|
|
1098
|
+
!ac.signal.aborted && turnOutcome === 'completed');
|
|
1095
1099
|
finishHostOperation(
|
|
1096
1100
|
ac.signal.aborted ? 'interrupted' : turnOutcome,
|
|
1097
1101
|
);
|
|
@@ -1272,7 +1276,7 @@ export function useChat(args: {
|
|
|
1272
1276
|
*
|
|
1273
1277
|
* ✅ 关键改进:压缩失败时不保存 history(可能包含损坏数据)
|
|
1274
1278
|
*/
|
|
1275
|
-
const compactNow = useCallback(async () => {
|
|
1279
|
+
const compactNow = useCallback(async (customInstructions?: string) => {
|
|
1276
1280
|
if (isLoadingRef.current) return;
|
|
1277
1281
|
setIsLoading(true);
|
|
1278
1282
|
// 手动压缩仍然把 progressStage 切到 compacting,给 LiveArea 显示用
|
|
@@ -1280,6 +1284,8 @@ export function useChat(args: {
|
|
|
1280
1284
|
setError(null);
|
|
1281
1285
|
// 压缩前清掉任何待 flush 的 streaming buffer(避免压缩中 timer 触发干扰)
|
|
1282
1286
|
cancelAllThrottles();
|
|
1287
|
+
const ac = new AbortController();
|
|
1288
|
+
abortRef.current = ac;
|
|
1283
1289
|
let success = false;
|
|
1284
1290
|
try {
|
|
1285
1291
|
await initialSessionReadyRef.current;
|
|
@@ -1287,40 +1293,41 @@ export function useChat(args: {
|
|
|
1287
1293
|
const workspace = await workspaceIndexService
|
|
1288
1294
|
.findByPath(cwd)
|
|
1289
1295
|
.catch(() => null);
|
|
1290
|
-
const r = await
|
|
1291
|
-
historyRef.current,
|
|
1296
|
+
const r = await compactSession({
|
|
1297
|
+
history: historyRef.current,
|
|
1298
|
+
sessionFile: sessionFile(),
|
|
1292
1299
|
// ⚠️ 用 providerRef.current 而非 args.provider:后者是 useChat 挂载时的
|
|
1293
1300
|
// 初始 prop,全程冻结不变;/model 切换只更新 providerRef,若这里继续用
|
|
1294
1301
|
// args.provider,切换模型后手动 /compact 仍会按旧模型的 contextWindow/
|
|
1295
1302
|
// compactRatio 算阈值和输出预算。
|
|
1296
|
-
providerRef.current,
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
createPromptCacheContext(
|
|
1303
|
+
provider: providerRef.current,
|
|
1304
|
+
signal: ac.signal,
|
|
1305
|
+
customInstructions,
|
|
1306
|
+
promptCache: createPromptCacheContext(
|
|
1300
1307
|
`workspace:${workspace?.id ?? cwd}:session:${activeSessionIdRef.current}`,
|
|
1301
1308
|
),
|
|
1302
|
-
);
|
|
1309
|
+
});
|
|
1303
1310
|
historyRef.current.length = 0;
|
|
1304
1311
|
historyRef.current.push(...r.messages);
|
|
1305
1312
|
// 压缩后 MessageList 要 remount,丢掉旧 <Static> 缓存
|
|
1306
1313
|
setCompactGeneration((g) => g + 1);
|
|
1307
1314
|
bump();
|
|
1315
|
+
setProgressNotice(manualCompactNotice(r));
|
|
1308
1316
|
success = true; // ✅ 标记成功
|
|
1317
|
+
// The transaction is already committed; a presentation callback cannot
|
|
1318
|
+
// turn this into a claim that the original context was retained.
|
|
1319
|
+
if (!r.unchangedReason) {
|
|
1320
|
+
try { args.onCompactDone?.(); }
|
|
1321
|
+
catch { setProgressNotice(`${manualCompactNotice(r)} 界面通知未完成,请重新打开会话。`); }
|
|
1322
|
+
}
|
|
1309
1323
|
} catch (e) {
|
|
1310
|
-
|
|
1324
|
+
if (ac.signal.aborted) setProgressNotice('压缩已取消,原上下文保持不变。');
|
|
1325
|
+
else setError(`手动压缩失败,原上下文保持不变:${(e as Error).message}`);
|
|
1311
1326
|
// ❌ 不修改 historyRef.current,保持原样(不保存可能损坏的数据)
|
|
1312
1327
|
} finally {
|
|
1313
1328
|
setProgressStage('idle');
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
if (success) {
|
|
1317
|
-
await saveContext(historyRef.current, sessionFile());
|
|
1318
|
-
args.onCompactDone?.();
|
|
1319
|
-
}
|
|
1320
|
-
} catch (error) {
|
|
1321
|
-
success = false;
|
|
1322
|
-
setError(`压缩后保存失败:${(error as Error).message}`);
|
|
1323
|
-
} finally { finishHostOperation(success ? 'completed' : 'failed'); }
|
|
1329
|
+
if (abortRef.current === ac) abortRef.current = null;
|
|
1330
|
+
finishHostOperation(success ? 'completed' : ac.signal.aborted ? 'interrupted' : 'failed');
|
|
1324
1331
|
}
|
|
1325
1332
|
}, [
|
|
1326
1333
|
args.onCompactDone,
|
package/src/web/protocol.ts
CHANGED
|
@@ -1915,7 +1915,7 @@ export type ClientFrame =
|
|
|
1915
1915
|
modelId?: string;
|
|
1916
1916
|
}
|
|
1917
1917
|
| { t: 'plain/clear'; sessionId: string }
|
|
1918
|
-
| { t: 'plain/compact'; sessionId: string; requestId?: string }
|
|
1918
|
+
| { t: 'plain/compact'; sessionId: string; requestId?: string; customInstructions?: string }
|
|
1919
1919
|
| { t: 'plain/rename'; sessionId: string; title: string }
|
|
1920
1920
|
| { t: 'plain/delete'; sessionId: string }
|
|
1921
1921
|
| { t: 'resource/files/list'; dir: string; sessionId: string }
|
|
@@ -2207,7 +2207,7 @@ export type ClientFrame =
|
|
|
2207
2207
|
| { t: 'abort' }
|
|
2208
2208
|
| { t: 'compact/abort'; requestId: string }
|
|
2209
2209
|
| { t: 'new'; dir: string; sessionId?: string }
|
|
2210
|
-
| { t: 'compact'; dir: string; sessionId?: string; requestId?: string }
|
|
2210
|
+
| { t: 'compact'; dir: string; sessionId?: string; requestId?: string; customInstructions?: string }
|
|
2211
2211
|
| { t: 'listDirs' }
|
|
2212
2212
|
/** 探测局域网内的 minimal-server(UDP 只能在本机 Runtime 里发,浏览器上下文发不了)。 */
|
|
2213
2213
|
| { t: 'server/discover' }
|
|
@@ -2812,6 +2812,7 @@ export type ServerFrame =
|
|
|
2812
2812
|
t: 'conversation/view/opened';
|
|
2813
2813
|
requestId: string;
|
|
2814
2814
|
conversationKey: string;
|
|
2815
|
+
session?: SessionSummary;
|
|
2815
2816
|
historyRevision: string;
|
|
2816
2817
|
messages: Message[];
|
|
2817
2818
|
deliveries?: ConversationDelivery[];
|
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { jsx as _jsx } from "react/jsx-runtime";
|
|
2
|
-
/**
|
|
3
|
-
* ============================================================
|
|
4
|
-
* src/cli/wizards/advisorWizard.tsx —— 顾问模型子向导
|
|
5
|
-
* ------------------------------------------------------------
|
|
6
|
-
* v4 模型列表 schema 下,顾问不再自带一整套独立凭据——默认从已配置的模型列表
|
|
7
|
-
* 选一个复用。但顾问常常需要一个跟基座完全不同的 provider(对比不同模型的
|
|
8
|
-
* 第二意见才有意义),而 CLI 的「配置基座模型」向导只管理基座这一个槽位、
|
|
9
|
-
* 没有别的入口能新增模型——所以这里的模型选择字段里多一项「+ 新建模型」,
|
|
10
|
-
* 选它会先走一小段收集新模型字段的表单,新建成功后再回落到常规的顾问行为
|
|
11
|
-
* 字段(启用/调用模式/预算/超时/敏感共享)。三阶段状态机,全部落盘只发生在
|
|
12
|
-
* 最后一步提交时(ESC 在任何阶段取消都不留半成品模型)。
|
|
13
|
-
* ============================================================
|
|
14
|
-
*/
|
|
15
|
-
import { useState } from 'react';
|
|
16
|
-
import { configService } from '../../config/configService.js';
|
|
17
|
-
import { DEFAULT_ADVISOR_CONFIG } from '../../config/configDefaults.js';
|
|
18
|
-
import { CONFIG_LIMITS, normalizeLimited } from '../../config/numericLimits.js';
|
|
19
|
-
import { FieldForm } from './fieldForm.js';
|
|
20
|
-
const NEW_MODEL_OPTION = '+ 新建模型(作为顾问专用,可与基座用不同 provider)';
|
|
21
|
-
function parsePositiveInt(value, fallback) {
|
|
22
|
-
const parsed = Number.parseInt(String(value ?? ''), 10);
|
|
23
|
-
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
24
|
-
}
|
|
25
|
-
export function AdvisorWizard({ existing, models, onDone, onCancel }) {
|
|
26
|
-
const enabledModels = models.filter((m) => m.enabled);
|
|
27
|
-
const labelFor = (m, index) => `${index + 1}. ${m.displayName || m.model}`;
|
|
28
|
-
const existingOptions = enabledModels.map(labelFor);
|
|
29
|
-
const labelToId = new Map(enabledModels.map((m, i) => [labelFor(m, i), m.id]));
|
|
30
|
-
const currentLabel = (() => {
|
|
31
|
-
const index = enabledModels.findIndex((m) => m.id === existing?.modelId);
|
|
32
|
-
return index >= 0 ? labelFor(enabledModels[index], index) : undefined;
|
|
33
|
-
})();
|
|
34
|
-
const [phase, setPhase] = useState('model-choice');
|
|
35
|
-
const [chosenModelId, setChosenModelId] = useState(null);
|
|
36
|
-
const [newModelDraft, setNewModelDraft] = useState(null);
|
|
37
|
-
if (phase === 'model-choice') {
|
|
38
|
-
return (_jsx(FieldForm, { title: "\u914D\u7F6E\u987E\u95EE\u6A21\u578B\uFF08Advisor \u5DE5\u5177\uFF09", fields: [
|
|
39
|
-
{
|
|
40
|
-
key: 'modelLabel',
|
|
41
|
-
label: '使用哪个模型',
|
|
42
|
-
type: 'select',
|
|
43
|
-
options: [...existingOptions, NEW_MODEL_OPTION],
|
|
44
|
-
default: currentLabel ?? (existingOptions[0] ?? NEW_MODEL_OPTION),
|
|
45
|
-
hint: enabledModels.length === 0
|
|
46
|
-
? '还没有已配置的模型,只能新建'
|
|
47
|
-
: '可以直接复用基座模型,也可以新建一个专属 provider',
|
|
48
|
-
},
|
|
49
|
-
], onSubmit: async (values) => {
|
|
50
|
-
const label = String(values.modelLabel ?? '');
|
|
51
|
-
if (label === NEW_MODEL_OPTION) {
|
|
52
|
-
setPhase('new-model');
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
const id = labelToId.get(label);
|
|
56
|
-
if (!id)
|
|
57
|
-
throw new Error('请选择一个模型');
|
|
58
|
-
setChosenModelId(id);
|
|
59
|
-
setPhase('advisor-fields');
|
|
60
|
-
}, onCancel: onCancel }));
|
|
61
|
-
}
|
|
62
|
-
if (phase === 'new-model') {
|
|
63
|
-
return (_jsx(FieldForm, { title: "\u65B0\u5EFA\u987E\u95EE\u4E13\u7528\u6A21\u578B\uFF08\u56FA\u5B9A OpenAI \u517C\u5BB9\u683C\u5F0F\uFF1BAnthropic \u683C\u5F0F\u8BF7\u7528 Desktop \u6DFB\u52A0\uFF09", fields: [
|
|
64
|
-
{ key: 'baseURL', label: 'API 完整端点地址(含 /chat/completions 后缀)', type: 'text', default: '' },
|
|
65
|
-
{ key: 'apiKey', label: 'API Key', type: 'password', default: '' },
|
|
66
|
-
{ key: 'model', label: 'Model', type: 'text', default: '' },
|
|
67
|
-
{ key: 'contextWindow', label: 'Context Window(token 数)', type: 'text', default: '1000000', optional: true },
|
|
68
|
-
], onSubmit: async (values) => {
|
|
69
|
-
const baseURL = String(values.baseURL ?? '').trim();
|
|
70
|
-
const apiKey = String(values.apiKey ?? '').trim();
|
|
71
|
-
const model = String(values.model ?? '').trim();
|
|
72
|
-
if (!baseURL || !apiKey || !model)
|
|
73
|
-
throw new Error('Base URL / API Key / Model 均不能为空');
|
|
74
|
-
setNewModelDraft({
|
|
75
|
-
baseURL,
|
|
76
|
-
apiKey,
|
|
77
|
-
model,
|
|
78
|
-
contextWindow: parsePositiveInt(values.contextWindow, 1_000_000),
|
|
79
|
-
supportsVision: true,
|
|
80
|
-
});
|
|
81
|
-
setPhase('advisor-fields');
|
|
82
|
-
}, onCancel: onCancel }));
|
|
83
|
-
}
|
|
84
|
-
// phase === 'advisor-fields'
|
|
85
|
-
return (_jsx(FieldForm, { title: "\u914D\u7F6E\u987E\u95EE\u6A21\u578B \u00B7 \u884C\u4E3A\u8BBE\u7F6E", fields: [
|
|
86
|
-
{ key: 'enabled', label: '是否启用', type: 'boolean', default: existing?.enabled ?? true },
|
|
87
|
-
{
|
|
88
|
-
key: 'mode',
|
|
89
|
-
label: '调用模式',
|
|
90
|
-
type: 'select',
|
|
91
|
-
options: ['auto', 'manual'],
|
|
92
|
-
default: existing?.mode ?? 'auto',
|
|
93
|
-
hint: 'auto=主循环自主判断时机调用 · manual=仅 /advisor ask 手动触发',
|
|
94
|
-
},
|
|
95
|
-
{
|
|
96
|
-
key: 'enforcement',
|
|
97
|
-
label: '补问方式',
|
|
98
|
-
type: 'select',
|
|
99
|
-
options: ['remind', 'followup'],
|
|
100
|
-
default: existing?.enforcement ?? 'remind',
|
|
101
|
-
hint: 'remind=只提醒 · followup=实质回答未复核时再发一次请求,让模型自己补问顾问(弱基座模型用)',
|
|
102
|
-
},
|
|
103
|
-
{
|
|
104
|
-
key: 'timeoutSeconds',
|
|
105
|
-
label: '最长等待(秒)',
|
|
106
|
-
type: 'text',
|
|
107
|
-
default: String(Math.round((existing?.timeoutMs ?? DEFAULT_ADVISOR_CONFIG.timeoutMs) / 1000)),
|
|
108
|
-
optional: true,
|
|
109
|
-
},
|
|
110
|
-
], onSubmit: async (values) => {
|
|
111
|
-
// 只在这最后一步才真正落盘——ESC 在任何前置阶段取消都不留半成品模型。
|
|
112
|
-
// newModelDraft 在 add 成功后立即清空 + 把新 id 记入 chosenModelId:若紧接着
|
|
113
|
-
// 的顾问 patch 失败,FieldForm 在同一 phase 内回到第 1 项重试会重新调这个
|
|
114
|
-
// onSubmit,若不清空 newModelDraft 会重新执行一次 add,产生重复模型。
|
|
115
|
-
let modelId = chosenModelId;
|
|
116
|
-
if (!modelId && newModelDraft) {
|
|
117
|
-
const beforeIds = new Set(models.map((m) => m.id));
|
|
118
|
-
const saved = await configService.savePatch({
|
|
119
|
-
models: [{
|
|
120
|
-
op: 'add',
|
|
121
|
-
model: {
|
|
122
|
-
apiFormat: 'openai',
|
|
123
|
-
baseURL: newModelDraft.baseURL,
|
|
124
|
-
apiKey: newModelDraft.apiKey,
|
|
125
|
-
model: newModelDraft.model,
|
|
126
|
-
contextWindow: newModelDraft.contextWindow,
|
|
127
|
-
supportsVision: newModelDraft.supportsVision,
|
|
128
|
-
promptCache: { mode: 'auto' },
|
|
129
|
-
enabled: true,
|
|
130
|
-
},
|
|
131
|
-
}],
|
|
132
|
-
});
|
|
133
|
-
modelId = saved.config?.models.find((m) => !beforeIds.has(m.id))?.id ?? null;
|
|
134
|
-
if (modelId) {
|
|
135
|
-
setChosenModelId(modelId);
|
|
136
|
-
setNewModelDraft(null);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
if (!modelId)
|
|
140
|
-
throw new Error('未能确定要使用的模型');
|
|
141
|
-
await configService.savePatch({
|
|
142
|
-
advisor: {
|
|
143
|
-
enabled: values.enabled,
|
|
144
|
-
mode: values.mode === 'manual' ? 'manual' : 'auto',
|
|
145
|
-
enforcement: values.enforcement === 'followup' ? 'followup' : 'remind',
|
|
146
|
-
modelId,
|
|
147
|
-
timeoutMs: normalizeLimited(parsePositiveInt(values.timeoutSeconds, DEFAULT_ADVISOR_CONFIG.timeoutMs / 1000) * 1000, CONFIG_LIMITS.advisorTimeoutMs, DEFAULT_ADVISOR_CONFIG.timeoutMs),
|
|
148
|
-
},
|
|
149
|
-
});
|
|
150
|
-
onDone();
|
|
151
|
-
}, onCancel: onCancel }));
|
|
152
|
-
}
|
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ============================================================
|
|
3
|
-
* src/cli/wizards/advisorWizard.tsx —— 顾问模型子向导
|
|
4
|
-
* ------------------------------------------------------------
|
|
5
|
-
* v4 模型列表 schema 下,顾问不再自带一整套独立凭据——默认从已配置的模型列表
|
|
6
|
-
* 选一个复用。但顾问常常需要一个跟基座完全不同的 provider(对比不同模型的
|
|
7
|
-
* 第二意见才有意义),而 CLI 的「配置基座模型」向导只管理基座这一个槽位、
|
|
8
|
-
* 没有别的入口能新增模型——所以这里的模型选择字段里多一项「+ 新建模型」,
|
|
9
|
-
* 选它会先走一小段收集新模型字段的表单,新建成功后再回落到常规的顾问行为
|
|
10
|
-
* 字段(启用/调用模式/预算/超时/敏感共享)。三阶段状态机,全部落盘只发生在
|
|
11
|
-
* 最后一步提交时(ESC 在任何阶段取消都不留半成品模型)。
|
|
12
|
-
* ============================================================
|
|
13
|
-
*/
|
|
14
|
-
|
|
15
|
-
import React, { useState } from 'react';
|
|
16
|
-
|
|
17
|
-
import type { ModelConfig } from '../../config/configFile.js';
|
|
18
|
-
import { configService } from '../../config/configService.js';
|
|
19
|
-
import { DEFAULT_ADVISOR_CONFIG } from '../../config/configDefaults.js';
|
|
20
|
-
import { CONFIG_LIMITS, normalizeLimited } from '../../config/numericLimits.js';
|
|
21
|
-
import { FieldForm, type FieldValues } from './fieldForm.js';
|
|
22
|
-
|
|
23
|
-
type ResolvedAdvisor = Awaited<ReturnType<typeof configService.advisor>>;
|
|
24
|
-
|
|
25
|
-
interface Props {
|
|
26
|
-
existing?: ResolvedAdvisor | null;
|
|
27
|
-
/** 已配置的模型列表(供选择顾问引用哪一个,或对照展示);由 configMenu.tsx 一并拉取传入。 */
|
|
28
|
-
models: ModelConfig[];
|
|
29
|
-
onDone: () => void;
|
|
30
|
-
onCancel: () => void;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
const NEW_MODEL_OPTION = '+ 新建模型(作为顾问专用,可与基座用不同 provider)';
|
|
34
|
-
|
|
35
|
-
function parsePositiveInt(value: unknown, fallback: number): number {
|
|
36
|
-
const parsed = Number.parseInt(String(value ?? ''), 10);
|
|
37
|
-
return Number.isFinite(parsed) && parsed > 0 ? parsed : fallback;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
interface NewModelDraft {
|
|
41
|
-
baseURL: string;
|
|
42
|
-
apiKey: string;
|
|
43
|
-
model: string;
|
|
44
|
-
contextWindow: number;
|
|
45
|
-
supportsVision: boolean;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
type Phase = 'model-choice' | 'new-model' | 'advisor-fields';
|
|
49
|
-
|
|
50
|
-
export function AdvisorWizard({ existing, models, onDone, onCancel }: Props): JSX.Element {
|
|
51
|
-
const enabledModels = models.filter((m) => m.enabled);
|
|
52
|
-
const labelFor = (m: ModelConfig, index: number) => `${index + 1}. ${m.displayName || m.model}`;
|
|
53
|
-
const existingOptions = enabledModels.map(labelFor);
|
|
54
|
-
const labelToId = new Map(enabledModels.map((m, i) => [labelFor(m, i), m.id]));
|
|
55
|
-
const currentLabel = (() => {
|
|
56
|
-
const index = enabledModels.findIndex((m) => m.id === existing?.modelId);
|
|
57
|
-
return index >= 0 ? labelFor(enabledModels[index]!, index) : undefined;
|
|
58
|
-
})();
|
|
59
|
-
|
|
60
|
-
const [phase, setPhase] = useState<Phase>('model-choice');
|
|
61
|
-
const [chosenModelId, setChosenModelId] = useState<string | null>(null);
|
|
62
|
-
const [newModelDraft, setNewModelDraft] = useState<NewModelDraft | null>(null);
|
|
63
|
-
|
|
64
|
-
if (phase === 'model-choice') {
|
|
65
|
-
return (
|
|
66
|
-
<FieldForm
|
|
67
|
-
title="配置顾问模型(Advisor 工具)"
|
|
68
|
-
fields={[
|
|
69
|
-
{
|
|
70
|
-
key: 'modelLabel',
|
|
71
|
-
label: '使用哪个模型',
|
|
72
|
-
type: 'select',
|
|
73
|
-
options: [...existingOptions, NEW_MODEL_OPTION],
|
|
74
|
-
default: currentLabel ?? (existingOptions[0] ?? NEW_MODEL_OPTION),
|
|
75
|
-
hint: enabledModels.length === 0
|
|
76
|
-
? '还没有已配置的模型,只能新建'
|
|
77
|
-
: '可以直接复用基座模型,也可以新建一个专属 provider',
|
|
78
|
-
},
|
|
79
|
-
]}
|
|
80
|
-
onSubmit={async (values) => {
|
|
81
|
-
const label = String(values.modelLabel ?? '');
|
|
82
|
-
if (label === NEW_MODEL_OPTION) {
|
|
83
|
-
setPhase('new-model');
|
|
84
|
-
return;
|
|
85
|
-
}
|
|
86
|
-
const id = labelToId.get(label);
|
|
87
|
-
if (!id) throw new Error('请选择一个模型');
|
|
88
|
-
setChosenModelId(id);
|
|
89
|
-
setPhase('advisor-fields');
|
|
90
|
-
}}
|
|
91
|
-
onCancel={onCancel}
|
|
92
|
-
/>
|
|
93
|
-
);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (phase === 'new-model') {
|
|
97
|
-
return (
|
|
98
|
-
<FieldForm
|
|
99
|
-
title="新建顾问专用模型(固定 OpenAI 兼容格式;Anthropic 格式请用 Desktop 添加)"
|
|
100
|
-
fields={[
|
|
101
|
-
{ key: 'baseURL', label: 'API 完整端点地址(含 /chat/completions 后缀)', type: 'text', default: '' },
|
|
102
|
-
{ key: 'apiKey', label: 'API Key', type: 'password', default: '' },
|
|
103
|
-
{ key: 'model', label: 'Model', type: 'text', default: '' },
|
|
104
|
-
{ key: 'contextWindow', label: 'Context Window(token 数)', type: 'text', default: '1000000', optional: true },
|
|
105
|
-
]}
|
|
106
|
-
onSubmit={async (values: FieldValues) => {
|
|
107
|
-
const baseURL = String(values.baseURL ?? '').trim();
|
|
108
|
-
const apiKey = String(values.apiKey ?? '').trim();
|
|
109
|
-
const model = String(values.model ?? '').trim();
|
|
110
|
-
if (!baseURL || !apiKey || !model) throw new Error('Base URL / API Key / Model 均不能为空');
|
|
111
|
-
setNewModelDraft({
|
|
112
|
-
baseURL,
|
|
113
|
-
apiKey,
|
|
114
|
-
model,
|
|
115
|
-
contextWindow: parsePositiveInt(values.contextWindow, 1_000_000),
|
|
116
|
-
supportsVision: true,
|
|
117
|
-
});
|
|
118
|
-
setPhase('advisor-fields');
|
|
119
|
-
}}
|
|
120
|
-
onCancel={onCancel}
|
|
121
|
-
/>
|
|
122
|
-
);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
// phase === 'advisor-fields'
|
|
126
|
-
return (
|
|
127
|
-
<FieldForm
|
|
128
|
-
title="配置顾问模型 · 行为设置"
|
|
129
|
-
fields={[
|
|
130
|
-
{ key: 'enabled', label: '是否启用', type: 'boolean', default: existing?.enabled ?? true },
|
|
131
|
-
{
|
|
132
|
-
key: 'mode',
|
|
133
|
-
label: '调用模式',
|
|
134
|
-
type: 'select',
|
|
135
|
-
options: ['auto', 'manual'],
|
|
136
|
-
default: existing?.mode ?? 'auto',
|
|
137
|
-
hint: 'auto=主循环自主判断时机调用 · manual=仅 /advisor ask 手动触发',
|
|
138
|
-
},
|
|
139
|
-
{
|
|
140
|
-
key: 'enforcement',
|
|
141
|
-
label: '补问方式',
|
|
142
|
-
type: 'select',
|
|
143
|
-
options: ['remind', 'followup'],
|
|
144
|
-
default: existing?.enforcement ?? 'remind',
|
|
145
|
-
hint: 'remind=只提醒 · followup=实质回答未复核时再发一次请求,让模型自己补问顾问(弱基座模型用)',
|
|
146
|
-
},
|
|
147
|
-
{
|
|
148
|
-
key: 'timeoutSeconds',
|
|
149
|
-
label: '最长等待(秒)',
|
|
150
|
-
type: 'text',
|
|
151
|
-
default: String(Math.round((existing?.timeoutMs ?? DEFAULT_ADVISOR_CONFIG.timeoutMs) / 1000)),
|
|
152
|
-
optional: true,
|
|
153
|
-
},
|
|
154
|
-
]}
|
|
155
|
-
onSubmit={async (values) => {
|
|
156
|
-
// 只在这最后一步才真正落盘——ESC 在任何前置阶段取消都不留半成品模型。
|
|
157
|
-
// newModelDraft 在 add 成功后立即清空 + 把新 id 记入 chosenModelId:若紧接着
|
|
158
|
-
// 的顾问 patch 失败,FieldForm 在同一 phase 内回到第 1 项重试会重新调这个
|
|
159
|
-
// onSubmit,若不清空 newModelDraft 会重新执行一次 add,产生重复模型。
|
|
160
|
-
let modelId = chosenModelId;
|
|
161
|
-
if (!modelId && newModelDraft) {
|
|
162
|
-
const beforeIds = new Set(models.map((m) => m.id));
|
|
163
|
-
const saved = await configService.savePatch({
|
|
164
|
-
models: [{
|
|
165
|
-
op: 'add',
|
|
166
|
-
model: {
|
|
167
|
-
apiFormat: 'openai',
|
|
168
|
-
baseURL: newModelDraft.baseURL,
|
|
169
|
-
apiKey: newModelDraft.apiKey,
|
|
170
|
-
model: newModelDraft.model,
|
|
171
|
-
contextWindow: newModelDraft.contextWindow,
|
|
172
|
-
supportsVision: newModelDraft.supportsVision,
|
|
173
|
-
promptCache: { mode: 'auto' },
|
|
174
|
-
enabled: true,
|
|
175
|
-
},
|
|
176
|
-
}],
|
|
177
|
-
});
|
|
178
|
-
modelId = saved.config?.models.find((m) => !beforeIds.has(m.id))?.id ?? null;
|
|
179
|
-
if (modelId) {
|
|
180
|
-
setChosenModelId(modelId);
|
|
181
|
-
setNewModelDraft(null);
|
|
182
|
-
}
|
|
183
|
-
}
|
|
184
|
-
if (!modelId) throw new Error('未能确定要使用的模型');
|
|
185
|
-
await configService.savePatch({
|
|
186
|
-
advisor: {
|
|
187
|
-
enabled: values.enabled as boolean,
|
|
188
|
-
mode: values.mode === 'manual' ? 'manual' : 'auto',
|
|
189
|
-
enforcement: values.enforcement === 'followup' ? 'followup' : 'remind',
|
|
190
|
-
modelId,
|
|
191
|
-
timeoutMs: normalizeLimited(
|
|
192
|
-
parsePositiveInt(values.timeoutSeconds, DEFAULT_ADVISOR_CONFIG.timeoutMs / 1000) * 1000,
|
|
193
|
-
CONFIG_LIMITS.advisorTimeoutMs,
|
|
194
|
-
DEFAULT_ADVISOR_CONFIG.timeoutMs,
|
|
195
|
-
),
|
|
196
|
-
},
|
|
197
|
-
});
|
|
198
|
-
onDone();
|
|
199
|
-
}}
|
|
200
|
-
onCancel={onCancel}
|
|
201
|
-
/>
|
|
202
|
-
);
|
|
203
|
-
}
|