aegiscode 3.1.8 → 3.1.10
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 +23 -15
- package/dist/main.js +549 -552
- package/package.json +4 -2
- package/src/agent/Agent.ts +903 -0
- package/src/agent/SimpleAgent.ts +48 -0
- package/src/agent/index.ts +54 -0
- package/src/agent/orchestrator/AppBuilder.ts +443 -0
- package/src/agent/orchestrator/CouncilAgent.ts +310 -0
- package/src/agent/orchestrator/DiscussionRoom.ts +462 -0
- package/src/agent/orchestrator/OrchestratorAgent.ts +637 -0
- package/src/agent/orchestrator/index.ts +38 -0
- package/src/agent/orchestrator/utils.ts +397 -0
- package/src/agent/pricing.ts +115 -0
- package/src/agent/router.ts +74 -0
- package/src/agent/routerStats.ts +121 -0
- package/src/agent/types.ts +318 -0
- package/src/auth/login.ts +383 -0
- package/src/cli/config.ts +189 -0
- package/src/cli/index.ts +17 -0
- package/src/cli/middleware.ts +119 -0
- package/src/cli/types.ts +75 -0
- package/src/config/ConfigManager.ts +587 -0
- package/src/config/index.ts +7 -0
- package/src/config/types.ts +584 -0
- package/src/context/CompactionService.ts +300 -0
- package/src/context/ContextManager.ts +450 -0
- package/src/context/FileAnalyzer.ts +267 -0
- package/src/context/TokenCounter.ts +265 -0
- package/src/context/index.ts +27 -0
- package/src/context/storage/CacheStore.ts +176 -0
- package/src/context/storage/JSONLStore.ts +201 -0
- package/src/context/storage/MemoryStore.ts +205 -0
- package/src/context/storage/PersistentStore.ts +327 -0
- package/src/context/storage/index.ts +9 -0
- package/src/context/storage/pathUtils.ts +114 -0
- package/src/context/test.ts +309 -0
- package/src/context/types.ts +268 -0
- package/src/hooks/HookExecutor.ts +434 -0
- package/src/hooks/HookManager.ts +596 -0
- package/src/hooks/HookService.ts +269 -0
- package/src/hooks/Matcher.ts +157 -0
- package/src/hooks/index.ts +63 -0
- package/src/hooks/types.ts +424 -0
- package/src/main.tsx +596 -0
- package/src/mcp/HealthMonitor.ts +150 -0
- package/src/mcp/McpClient.ts +491 -0
- package/src/mcp/McpRegistry.ts +321 -0
- package/src/mcp/createMcpTool.ts +251 -0
- package/src/mcp/index.ts +15 -0
- package/src/mcp/server.ts +334 -0
- package/src/mcp/test-server.ts +88 -0
- package/src/mcp/test.ts +372 -0
- package/src/mcp/types.ts +247 -0
- package/src/memory/AgentMemoryBus.ts +432 -0
- package/src/memory/CloudSync.ts +99 -0
- package/src/memory/DriveSync.ts +106 -0
- package/src/memory/SharedMemory.ts +951 -0
- package/src/memory/index.ts +14 -0
- package/src/memory/machineFingerprint.ts +40 -0
- package/src/orchestrator/SubAgentMetadata.ts +136 -0
- package/src/prompts/builder.ts +213 -0
- package/src/prompts/default.ts +144 -0
- package/src/prompts/index.ts +16 -0
- package/src/prompts/plan.ts +64 -0
- package/src/prompts/test.ts +78 -0
- package/src/services/AnthropicChatService.ts +341 -0
- package/src/services/ChatService.ts +347 -0
- package/src/services/ClaudeCliChatService.ts +256 -0
- package/src/services/CloudSync.ts +168 -0
- package/src/services/CostLedger.ts +211 -0
- package/src/services/Heartbeat.ts +135 -0
- package/src/services/LearningCollector.ts +291 -0
- package/src/services/OllamaInstaller.ts +342 -0
- package/src/services/VersionChecker.ts +445 -0
- package/src/services/index.ts +57 -0
- package/src/services/streaming/OpenAIEventAdapter.ts +126 -0
- package/src/services/streaming/RenderingProfile.ts +90 -0
- package/src/services/streaming/StreamEventParser.ts +181 -0
- package/src/services/streaming/ThrottledRenderer.ts +139 -0
- package/src/services/streaming/TranscriptBuffer.ts +574 -0
- package/src/services/streaming/eventStatusMap.ts +52 -0
- package/src/services/streaming/index.ts +46 -0
- package/src/services/streaming/renderFormatting.ts +79 -0
- package/src/services/streaming/types.ts +234 -0
- package/src/skills/SkillLoader.ts +126 -0
- package/src/skills/SkillRegistry.ts +366 -0
- package/src/skills/index.ts +48 -0
- package/src/skills/types.ts +146 -0
- package/src/slash-commands/billing.ts +70 -0
- package/src/slash-commands/build.ts +413 -0
- package/src/slash-commands/builtinCommands.ts +2733 -0
- package/src/slash-commands/clone.ts +242 -0
- package/src/slash-commands/council.ts +125 -0
- package/src/slash-commands/custom/CustomCommandExecutor.ts +143 -0
- package/src/slash-commands/custom/CustomCommandLoader.ts +251 -0
- package/src/slash-commands/custom/CustomCommandRegistry.ts +144 -0
- package/src/slash-commands/custom/index.ts +7 -0
- package/src/slash-commands/debate.ts +254 -0
- package/src/slash-commands/gmail.ts +105 -0
- package/src/slash-commands/index.ts +388 -0
- package/src/slash-commands/mcpCommand.ts +205 -0
- package/src/slash-commands/types.ts +201 -0
- package/src/store/index.ts +76 -0
- package/src/store/selectors.ts +246 -0
- package/src/store/slices/appSlice.ts +205 -0
- package/src/store/slices/commandSlice.ts +115 -0
- package/src/store/slices/configSlice.ts +46 -0
- package/src/store/slices/focusSlice.ts +64 -0
- package/src/store/slices/index.ts +9 -0
- package/src/store/slices/sessionSlice.ts +424 -0
- package/src/store/streaming-buffer.ts +425 -0
- package/src/store/test.ts +296 -0
- package/src/store/types.ts +274 -0
- package/src/store/vanilla.ts +186 -0
- package/src/tools/builtin/bash.ts +236 -0
- package/src/tools/builtin/council.ts +105 -0
- package/src/tools/builtin/edit.ts +213 -0
- package/src/tools/builtin/glob.ts +136 -0
- package/src/tools/builtin/grep.ts +263 -0
- package/src/tools/builtin/index.ts +61 -0
- package/src/tools/builtin/memory.ts +66 -0
- package/src/tools/builtin/read.ts +168 -0
- package/src/tools/builtin/skill.ts +97 -0
- package/src/tools/builtin/snapshot.ts +40 -0
- package/src/tools/builtin/task.ts +106 -0
- package/src/tools/builtin/write.ts +134 -0
- package/src/tools/createTool.ts +221 -0
- package/src/tools/execution/ExecutionPipeline.ts +263 -0
- package/src/tools/execution/index.ts +40 -0
- package/src/tools/execution/stages/CacheStage.ts +131 -0
- package/src/tools/execution/stages/ConfirmationStage.ts +203 -0
- package/src/tools/execution/stages/DiscoveryStage.ts +46 -0
- package/src/tools/execution/stages/ExecutionStage.ts +48 -0
- package/src/tools/execution/stages/FormattingStage.ts +44 -0
- package/src/tools/execution/stages/HookStage.ts +72 -0
- package/src/tools/execution/stages/PermissionStage.ts +287 -0
- package/src/tools/execution/stages/PostHookStage.ts +71 -0
- package/src/tools/execution/stages/index.ts +12 -0
- package/src/tools/execution/test.ts +266 -0
- package/src/tools/execution/types.ts +273 -0
- package/src/tools/index.ts +81 -0
- package/src/tools/registry.ts +304 -0
- package/src/tools/schemas.ts +109 -0
- package/src/tools/test.ts +220 -0
- package/src/tools/types.ts +175 -0
- package/src/tools/validation/PermissionChecker.ts +242 -0
- package/src/tools/validation/SensitiveFileDetector.ts +210 -0
- package/src/tools/validation/index.ts +11 -0
- package/src/ui/App.tsx +166 -0
- package/src/ui/components/AegisInterface.tsx +484 -0
- package/src/ui/components/common/ChatSearch.tsx +150 -0
- package/src/ui/components/common/ErrorBoundary.tsx +82 -0
- package/src/ui/components/common/ExitMessage.tsx +120 -0
- package/src/ui/components/common/LoadingIndicator.tsx +49 -0
- package/src/ui/components/common/index.ts +6 -0
- package/src/ui/components/dialog/ConfirmationPrompt.tsx +208 -0
- package/src/ui/components/dialog/InteractiveSelector.tsx +149 -0
- package/src/ui/components/dialog/SetupWizard.tsx +297 -0
- package/src/ui/components/dialog/UpdatePrompt.tsx +155 -0
- package/src/ui/components/dialog/index.ts +8 -0
- package/src/ui/components/index.ts +28 -0
- package/src/ui/components/input/CommandSuggestions.tsx +139 -0
- package/src/ui/components/input/CustomTextInput.tsx +220 -0
- package/src/ui/components/input/InputArea.tsx +361 -0
- package/src/ui/components/input/PromptSuggestions.tsx +66 -0
- package/src/ui/components/input/index.ts +6 -0
- package/src/ui/components/layout/ChatStatusBar.tsx +90 -0
- package/src/ui/components/layout/ContextBar.tsx +79 -0
- package/src/ui/components/layout/MessageArea.tsx +96 -0
- package/src/ui/components/layout/MessageList.tsx +647 -0
- package/src/ui/components/layout/MessageSeparator.tsx +26 -0
- package/src/ui/components/layout/WelcomeMessage.tsx +93 -0
- package/src/ui/components/layout/index.ts +7 -0
- package/src/ui/components/markdown/CodeHighlighter.tsx +292 -0
- package/src/ui/components/markdown/MessageRenderer.tsx +1211 -0
- package/src/ui/components/markdown/index.ts +8 -0
- package/src/ui/components/markdown/parser.ts +336 -0
- package/src/ui/components/markdown/types.ts +66 -0
- package/src/ui/focus/FocusManager.ts +137 -0
- package/src/ui/focus/index.ts +13 -0
- package/src/ui/focus/types.ts +54 -0
- package/src/ui/focus/useFocus.ts +75 -0
- package/src/ui/hooks/index.ts +11 -0
- package/src/ui/hooks/useAgent.ts +284 -0
- package/src/ui/hooks/useCommandHistory.ts +87 -0
- package/src/ui/hooks/useCommandProcessor.ts +443 -0
- package/src/ui/hooks/useConfirmation.ts +99 -0
- package/src/ui/hooks/useCtrlCHandler.ts +100 -0
- package/src/ui/hooks/useInputBuffer.ts +122 -0
- package/src/ui/hooks/useTerminalSize.ts +68 -0
- package/src/ui/hooks/useTerminalWidth.ts +5 -0
- package/src/ui/hooks/useWindowedList.ts +118 -0
- package/src/ui/render-debugger.ts +621 -0
- package/src/ui/test.ts +189 -0
- package/src/ui/themes/ThemeManager.ts +332 -0
- package/src/ui/themes/aegisTheme.ts +87 -0
- package/src/ui/themes/darkTheme.ts +87 -0
- package/src/ui/themes/defaultTheme.ts +85 -0
- package/src/ui/themes/index.ts +10 -0
- package/src/ui/themes/lightTheme.ts +89 -0
- package/src/ui/themes/popularThemes.ts +187 -0
- package/src/ui/themes/types.ts +130 -0
- package/src/utils/clipboard.ts +48 -0
- package/src/utils/debug.ts +43 -0
- package/src/utils/environment.ts +68 -0
- package/src/utils/index.ts +10 -0
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-router learning loop — v1.
|
|
3
|
+
*
|
|
4
|
+
* The only outcome signal available without new UI is whether the user
|
|
5
|
+
* aborted (Escape/Ctrl+C) an auto-routed response. That's a noisy proxy for
|
|
6
|
+
* "this model didn't deliver" (people also abort for unrelated reasons —
|
|
7
|
+
* wrong question, impatience), but it's real, already-wired data instead of
|
|
8
|
+
* a guessed-at shape. Tracked as a Beta(success, failure) per tier+model and
|
|
9
|
+
* sampled via Thompson sampling, so the fallback list self-corrects toward
|
|
10
|
+
* whichever configured model actually finishes tasks for that tier, instead
|
|
11
|
+
* of always picking the same hardcoded first candidate.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
import * as fs from 'node:fs';
|
|
15
|
+
import * as path from 'node:path';
|
|
16
|
+
import * as os from 'node:os';
|
|
17
|
+
import type { ComplexityTier } from './router.js';
|
|
18
|
+
|
|
19
|
+
type ModelStats = { success: number; failure: number };
|
|
20
|
+
type RouterStats = Partial<Record<ComplexityTier, Record<string, ModelStats>>>;
|
|
21
|
+
|
|
22
|
+
const STATS_PATH = path.join(os.homedir(), '.aegiscode', 'router-stats.json');
|
|
23
|
+
|
|
24
|
+
let cache: RouterStats | null = null;
|
|
25
|
+
|
|
26
|
+
function load(): RouterStats {
|
|
27
|
+
if (cache) return cache;
|
|
28
|
+
try {
|
|
29
|
+
cache = JSON.parse(fs.readFileSync(STATS_PATH, 'utf8'));
|
|
30
|
+
} catch {
|
|
31
|
+
cache = {};
|
|
32
|
+
}
|
|
33
|
+
return cache!;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function save(stats: RouterStats): void {
|
|
37
|
+
cache = stats;
|
|
38
|
+
try {
|
|
39
|
+
fs.mkdirSync(path.dirname(STATS_PATH), { recursive: true });
|
|
40
|
+
fs.writeFileSync(STATS_PATH, JSON.stringify(stats, null, 2));
|
|
41
|
+
} catch { /* non-fatal — learning is best-effort */ }
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** Record whether an auto-routed turn completed normally or was aborted/errored. */
|
|
45
|
+
export function recordRouterOutcome(tier: ComplexityTier, modelId: string, success: boolean): void {
|
|
46
|
+
const stats = load();
|
|
47
|
+
const tierStats = stats[tier] || (stats[tier] = {});
|
|
48
|
+
const modelStats = tierStats[modelId] || (tierStats[modelId] = { success: 0, failure: 0 });
|
|
49
|
+
if (success) modelStats.success += 1;
|
|
50
|
+
else modelStats.failure += 1;
|
|
51
|
+
save(stats);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function getRouterStats(): RouterStats {
|
|
55
|
+
return load();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** Marsaglia-Tsang gamma sampler — shape > 0, scale 1. */
|
|
59
|
+
function sampleGamma(shape: number): number {
|
|
60
|
+
if (shape < 1) {
|
|
61
|
+
// Boost via Johnk's trick: Gamma(shape) = Gamma(shape+1) * U^(1/shape)
|
|
62
|
+
const u = Math.random();
|
|
63
|
+
return sampleGamma(shape + 1) * Math.pow(u, 1 / shape);
|
|
64
|
+
}
|
|
65
|
+
const d = shape - 1 / 3;
|
|
66
|
+
const c = 1 / Math.sqrt(9 * d);
|
|
67
|
+
for (;;) {
|
|
68
|
+
let x: number;
|
|
69
|
+
let v: number;
|
|
70
|
+
do {
|
|
71
|
+
// Box-Muller for a standard normal sample
|
|
72
|
+
const u1 = Math.random() || Number.EPSILON;
|
|
73
|
+
const u2 = Math.random();
|
|
74
|
+
x = Math.sqrt(-2 * Math.log(u1)) * Math.cos(2 * Math.PI * u2);
|
|
75
|
+
v = 1 + c * x;
|
|
76
|
+
} while (v <= 0);
|
|
77
|
+
v = v * v * v;
|
|
78
|
+
const u = Math.random();
|
|
79
|
+
if (u < 1 - 0.0331 * (x * x) * (x * x)) return d * v;
|
|
80
|
+
if (Math.log(u) < 0.5 * x * x + d * (1 - v + Math.log(v))) return d * v;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function sampleBeta(alpha: number, beta: number): number {
|
|
85
|
+
const ga = sampleGamma(alpha);
|
|
86
|
+
const gb = sampleGamma(beta);
|
|
87
|
+
return ga / (ga + gb);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Thompson sampling pick among candidate model ids for this tier, in
|
|
92
|
+
* cost-ascending order (cheapest first). A flat Beta(1,1) prior would make
|
|
93
|
+
* brand-new, zero-data routing an even coin flip across candidates —
|
|
94
|
+
* defeating the point of a cost-ordered fallback list before any evidence
|
|
95
|
+
* exists. Instead each candidate's prior is biased by its cost rank (a
|
|
96
|
+
* "virtual success" head start that shrinks toward the back of the list),
|
|
97
|
+
* so with no data it deterministically prefers the cheapest candidate, and
|
|
98
|
+
* only drifts toward a pricier one once real failures (aborted responses)
|
|
99
|
+
* accumulate enough to outweigh that head start.
|
|
100
|
+
*/
|
|
101
|
+
export function pickByOutcomes(tier: ComplexityTier, candidateIdsCostOrder: string[]): string | undefined {
|
|
102
|
+
const n = candidateIdsCostOrder.length;
|
|
103
|
+
if (n === 0) return undefined;
|
|
104
|
+
if (n === 1) return candidateIdsCostOrder[0];
|
|
105
|
+
|
|
106
|
+
const stats = load();
|
|
107
|
+
const tierStats = stats[tier] || {};
|
|
108
|
+
|
|
109
|
+
let best: string | undefined;
|
|
110
|
+
let bestScore = -Infinity;
|
|
111
|
+
candidateIdsCostOrder.forEach((id, i) => {
|
|
112
|
+
const priorBonus = (n - 1 - i) * 3; // cheapest gets the biggest head start
|
|
113
|
+
const s = tierStats[id];
|
|
114
|
+
const score = sampleBeta(1 + priorBonus + (s?.success ?? 0), 1 + (s?.failure ?? 0));
|
|
115
|
+
if (score > bestScore) {
|
|
116
|
+
bestScore = score;
|
|
117
|
+
best = id;
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
return best;
|
|
121
|
+
}
|
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent 类型定义
|
|
3
|
+
*
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
// ========== 消息类
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
*/
|
|
12
|
+
export type MessageRole = 'system' | 'user' | 'assistant' | 'tool';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
*
|
|
16
|
+
*/
|
|
17
|
+
export interface ToolCall {
|
|
18
|
+
id: string;
|
|
19
|
+
type: 'function';
|
|
20
|
+
function: {
|
|
21
|
+
name: string;
|
|
22
|
+
arguments: string; // JSON 字符
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
*
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
export interface Message {
|
|
31
|
+
role: MessageRole;
|
|
32
|
+
content: string;
|
|
33
|
+
|
|
34
|
+
/** DeepSeek R1 等推理模型产生的思维链内容 */
|
|
35
|
+
reasoning_content?: string;
|
|
36
|
+
|
|
37
|
+
/** assistant 消息专用:发起的工具调用列表 */
|
|
38
|
+
tool_calls?: ToolCall[];
|
|
39
|
+
|
|
40
|
+
/** tool 消息专用:关联的调用 ID */
|
|
41
|
+
tool_call_id?: string;
|
|
42
|
+
|
|
43
|
+
/** tool 消息专用:工具名称 */
|
|
44
|
+
name?: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// ========== 上下文类
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
export type PermissionMode = 'default' | 'autoEdit' | 'yolo' | 'plan';
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
*
|
|
56
|
+
*/
|
|
57
|
+
export interface ConfirmationDetails {
|
|
58
|
+
title: string;
|
|
59
|
+
message: string;
|
|
60
|
+
details?: string;
|
|
61
|
+
risks?: string[];
|
|
62
|
+
affectedFiles?: string[];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
*
|
|
67
|
+
*/
|
|
68
|
+
export interface ConfirmationResponse {
|
|
69
|
+
approved: boolean;
|
|
70
|
+
reason?: string;
|
|
71
|
+
scope?: 'once' | 'session';
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
*
|
|
76
|
+
*/
|
|
77
|
+
export interface ConfirmationHandler {
|
|
78
|
+
requestConfirmation(details: ConfirmationDetails): Promise<ConfirmationResponse>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
*
|
|
83
|
+
*
|
|
84
|
+
* Agent 是无状态的,所有状态通过 context 传入
|
|
85
|
+
*/
|
|
86
|
+
export interface ChatContext {
|
|
87
|
+
/** 会话 ID */
|
|
88
|
+
sessionId: string;
|
|
89
|
+
|
|
90
|
+
/** 消息历史 */
|
|
91
|
+
messages: Message[];
|
|
92
|
+
|
|
93
|
+
/** 权限模式 */
|
|
94
|
+
permissionMode?: PermissionMode;
|
|
95
|
+
|
|
96
|
+
/** 中断信号 */
|
|
97
|
+
signal?: AbortSignal;
|
|
98
|
+
|
|
99
|
+
/** 确认处理器 */
|
|
100
|
+
confirmationHandler?: ConfirmationHandler;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ========== 循环选项与结
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
*
|
|
107
|
+
*/
|
|
108
|
+
export interface LoopOptions {
|
|
109
|
+
/** 最大轮次 */
|
|
110
|
+
maxTurns?: number;
|
|
111
|
+
|
|
112
|
+
/** 中断信号 */
|
|
113
|
+
signal?: AbortSignal;
|
|
114
|
+
|
|
115
|
+
/** 轮次开始回调 */
|
|
116
|
+
onTurnStart?: (info: { turn: number; maxTurns: number }) => void;
|
|
117
|
+
|
|
118
|
+
/** 内容回调(完整内容,用于非流式场景) */
|
|
119
|
+
onContent?: (content: string) => void;
|
|
120
|
+
|
|
121
|
+
/** Unified Anthropic-format stream event (preferred over individual delta callbacks) */
|
|
122
|
+
onStreamEvent?: (event: import('../services/streaming/types.js').AnthropicStreamEvent) => void;
|
|
123
|
+
|
|
124
|
+
/** 内容增量回调(流式) */
|
|
125
|
+
onContentDelta?: (delta: string) => void;
|
|
126
|
+
|
|
127
|
+
/** 思考内容回调(完整内容) */
|
|
128
|
+
onThinking?: (content: string) => void;
|
|
129
|
+
|
|
130
|
+
/** 思考内容增量回调(流式) */
|
|
131
|
+
onThinkingDelta?: (delta: string) => void;
|
|
132
|
+
|
|
133
|
+
/** 工具调用开始回调 */
|
|
134
|
+
onToolCallStart?: (toolCall: Partial<ToolCall>) => void;
|
|
135
|
+
|
|
136
|
+
/** 工具调用参数增量回调(流式) */
|
|
137
|
+
onToolCallDelta?: (toolCallId: string, argumentsDelta: string) => void;
|
|
138
|
+
|
|
139
|
+
/** 工具结果回调 */
|
|
140
|
+
onToolResult?: (toolCall: ToolCall, result: ToolResult) => void;
|
|
141
|
+
|
|
142
|
+
/** 轮次上限回调 */
|
|
143
|
+
onTurnLimitReached?: (info: { turnsCount: number }) => Promise<{ continue: boolean }>;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
*
|
|
148
|
+
*/
|
|
149
|
+
export type LoopErrorType =
|
|
150
|
+
| 'aborted'
|
|
151
|
+
| 'max_turns_exceeded'
|
|
152
|
+
| 'chat_disabled'
|
|
153
|
+
| 'initialization_failed'
|
|
154
|
+
| 'llm_error'
|
|
155
|
+
| 'tool_error';
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
*
|
|
159
|
+
*/
|
|
160
|
+
export interface LoopError {
|
|
161
|
+
type: LoopErrorType;
|
|
162
|
+
message?: string;
|
|
163
|
+
cause?: Error;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
*
|
|
168
|
+
*/
|
|
169
|
+
export interface LoopResult {
|
|
170
|
+
success: boolean;
|
|
171
|
+
finalMessage?: string;
|
|
172
|
+
error?: LoopError;
|
|
173
|
+
metadata?: {
|
|
174
|
+
turnsCount: number;
|
|
175
|
+
toolCallsCount: number;
|
|
176
|
+
totalTokens?: number;
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// ========== 工具类
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
*
|
|
184
|
+
*/
|
|
185
|
+
export interface ToolResult {
|
|
186
|
+
success: boolean;
|
|
187
|
+
|
|
188
|
+
/** 显示给用户的内容 */
|
|
189
|
+
displayContent?: string;
|
|
190
|
+
|
|
191
|
+
/** 发送给 LLM 的内容 */
|
|
192
|
+
llmContent?: string;
|
|
193
|
+
|
|
194
|
+
/** 错误信息 */
|
|
195
|
+
error?: string;
|
|
196
|
+
|
|
197
|
+
/** 元数据 */
|
|
198
|
+
metadata?: Record<string, unknown>;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
*
|
|
203
|
+
*/
|
|
204
|
+
export interface ToolDefinition {
|
|
205
|
+
type: 'function';
|
|
206
|
+
function: {
|
|
207
|
+
name: string;
|
|
208
|
+
description: string;
|
|
209
|
+
parameters: {
|
|
210
|
+
type: 'object';
|
|
211
|
+
properties: Record<string, {
|
|
212
|
+
type: string;
|
|
213
|
+
description: string;
|
|
214
|
+
enum?: string[];
|
|
215
|
+
}>;
|
|
216
|
+
required?: string[];
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
// ========== Agent 配
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* Agent 配置
|
|
225
|
+
*/
|
|
226
|
+
export interface AgentConfig {
|
|
227
|
+
/** API Key */
|
|
228
|
+
apiKey: string;
|
|
229
|
+
|
|
230
|
+
/** API Base URL */
|
|
231
|
+
baseURL?: string;
|
|
232
|
+
|
|
233
|
+
/** 模型名称 */
|
|
234
|
+
model?: string;
|
|
235
|
+
|
|
236
|
+
/** 最大轮次 (-1 表示无限) */
|
|
237
|
+
maxTurns?: number;
|
|
238
|
+
|
|
239
|
+
/** 最大上下文 Token */
|
|
240
|
+
maxContextTokens?: number;
|
|
241
|
+
|
|
242
|
+
/** 最大输出 Token */
|
|
243
|
+
maxOutputTokens?: number;
|
|
244
|
+
|
|
245
|
+
/** 系统提示词 */
|
|
246
|
+
systemPrompt?: string;
|
|
247
|
+
|
|
248
|
+
/** Request timeout in ms (default 60000) */
|
|
249
|
+
timeout?: number;
|
|
250
|
+
|
|
251
|
+
/** Show confirmation prompts before sensitive tool calls (default true) */
|
|
252
|
+
requireConfirmation?: boolean;
|
|
253
|
+
|
|
254
|
+
/** aegiscode's own permission mode; only consulted for OAuth (claude CLI) transport. */
|
|
255
|
+
permissionMode?: string;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Agent 创建选项
|
|
260
|
+
*/
|
|
261
|
+
export interface AgentOptions {
|
|
262
|
+
/** 最大轮次 */
|
|
263
|
+
maxTurns?: number;
|
|
264
|
+
|
|
265
|
+
/** 工具白名单 */
|
|
266
|
+
toolWhitelist?: string[];
|
|
267
|
+
|
|
268
|
+
/** 工具黑名单 */
|
|
269
|
+
toolBlacklist?: string[];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// ========== ChatService 类
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* ChatService 响应
|
|
276
|
+
*/
|
|
277
|
+
export interface ChatResponse {
|
|
278
|
+
content: string;
|
|
279
|
+
reasoningContent?: string;
|
|
280
|
+
toolCalls?: ToolCall[];
|
|
281
|
+
usage?: {
|
|
282
|
+
promptTokens: number;
|
|
283
|
+
completionTokens: number;
|
|
284
|
+
totalTokens: number;
|
|
285
|
+
/** Tokens served from the prompt cache (~0.1x cost) — native Anthropic transport only. */
|
|
286
|
+
cacheReadTokens?: number;
|
|
287
|
+
/** Tokens written to the prompt cache this request (~1.25x cost) — native Anthropic transport only. */
|
|
288
|
+
cacheCreationTokens?: number;
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
*
|
|
294
|
+
*/
|
|
295
|
+
export interface StreamCallbacks {
|
|
296
|
+
/** Unified Anthropic-format stream event (preferred over the individual callbacks below) */
|
|
297
|
+
onStreamEvent?: (event: import('../services/streaming/types.js').AnthropicStreamEvent) => void;
|
|
298
|
+
/** 内容增量回调 */
|
|
299
|
+
onContentDelta?: (delta: string) => void;
|
|
300
|
+
/** 思考内容增量回调 */
|
|
301
|
+
onThinkingDelta?: (delta: string) => void;
|
|
302
|
+
/** 工具调用开始回调 */
|
|
303
|
+
onToolCallStart?: (toolCall: Partial<ToolCall>) => void;
|
|
304
|
+
/** 工具调用参数增量回调 */
|
|
305
|
+
onToolCallDelta?: (toolCallId: string, argumentsDelta: string) => void;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* ChatService 接口
|
|
310
|
+
*/
|
|
311
|
+
export interface IChatService {
|
|
312
|
+
chat(
|
|
313
|
+
messages: Message[],
|
|
314
|
+
tools?: ToolDefinition[],
|
|
315
|
+
signal?: AbortSignal,
|
|
316
|
+
streamCallbacks?: StreamCallbacks
|
|
317
|
+
): Promise<ChatResponse>;
|
|
318
|
+
}
|