-
Notifications
You must be signed in to change notification settings - Fork 103
Expand file tree
/
Copy pathThinkingContext.tsx
More file actions
198 lines (170 loc) · 7.12 KB
/
ThinkingContext.tsx
File metadata and controls
198 lines (170 loc) · 7.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import type { ReactNode } from "react";
import React, { createContext, useContext, useEffect, useMemo, useCallback } from "react";
import { THINKING_LEVEL_OFF, type ThinkingLevel } from "@/common/types/thinking";
import {
readPersistedState,
updatePersistedState,
usePersistedState,
} from "@/browser/hooks/usePersistedState";
import {
getAgentIdKey,
getModelKey,
getProjectScopeId,
getThinkingLevelByModelKey,
getThinkingLevelKey,
getWorkspaceAISettingsByAgentKey,
GLOBAL_SCOPE_ID,
} from "@/common/constants/storage";
import { getDefaultModel } from "@/browser/hooks/useModelsFromSettings";
import { normalizeToCanonical } from "@/common/utils/ai/models";
import { enforceThinkingPolicy, getThinkingPolicyForModel } from "@/common/utils/thinking/policy";
import { useAPI } from "@/browser/contexts/API";
import {
clearPendingWorkspaceAiSettings,
getWorkspaceAiSettingsFromMetadata,
markPendingWorkspaceAiSettings,
} from "@/browser/utils/workspaceAiSettingsSync";
import { useOptionalWorkspaceContext } from "@/browser/contexts/WorkspaceContext";
import { KEYBINDS, matchesKeybind } from "@/browser/utils/ui/keybinds";
import { WORKSPACE_DEFAULTS } from "@/constants/workspaceDefaults";
interface ThinkingContextType {
thinkingLevel: ThinkingLevel;
setThinkingLevel: (level: ThinkingLevel) => void;
}
const ThinkingContext = createContext<ThinkingContextType | undefined>(undefined);
interface ThinkingProviderProps {
workspaceId?: string; // Workspace-scoped storage (highest priority)
projectPath?: string; // Project-scoped storage (fallback if no workspaceId)
children: ReactNode;
}
function getScopeId(workspaceId: string | undefined, projectPath: string | undefined): string {
return workspaceId ?? (projectPath ? getProjectScopeId(projectPath) : GLOBAL_SCOPE_ID);
}
function getCanonicalModelForScope(scopeId: string, fallbackModel: string): string {
const rawModel = readPersistedState<string>(getModelKey(scopeId), fallbackModel);
return normalizeToCanonical(rawModel || fallbackModel);
}
export const ThinkingProvider: React.FC<ThinkingProviderProps> = (props) => {
const { api } = useAPI();
const workspaceContext = useOptionalWorkspaceContext();
const defaultModel = getDefaultModel();
const scopeId = getScopeId(props.workspaceId, props.projectPath);
const thinkingKey = getThinkingLevelKey(scopeId);
const metadataAgentId = readPersistedState<string>(
getAgentIdKey(scopeId),
WORKSPACE_DEFAULTS.agentId
);
const metadataSettings = getWorkspaceAiSettingsFromMetadata(
props.workspaceId ? workspaceContext?.workspaceMetadata.get(props.workspaceId) : undefined,
metadataAgentId
);
// Workspace-scoped thinking. Null means no explicit user choice has been persisted yet.
const [persistedThinkingLevel, setThinkingLevelInternal] =
usePersistedState<ThinkingLevel | null>(thinkingKey, null, { listener: true });
const thinkingLevel =
persistedThinkingLevel ?? metadataSettings.thinkingLevel ?? THINKING_LEVEL_OFF;
// One-time migration: if the new workspace-scoped key is missing, seed from the legacy per-model key.
useEffect(() => {
const existing = readPersistedState<ThinkingLevel | null | undefined>(thinkingKey, undefined);
if (existing != null) {
return;
}
const model = getCanonicalModelForScope(scopeId, defaultModel);
const legacyKey = getThinkingLevelByModelKey(model);
const legacy = readPersistedState<ThinkingLevel | undefined>(legacyKey, undefined);
if (legacy === undefined) {
return;
}
updatePersistedState(thinkingKey, legacy);
}, [defaultModel, scopeId, thinkingKey]);
const setThinkingLevel = useCallback(
(level: ThinkingLevel) => {
const model = getCanonicalModelForScope(scopeId, defaultModel);
setThinkingLevelInternal(level);
// Workspace variant: persist to backend so settings follow the workspace across devices.
if (!props.workspaceId) {
return;
}
const workspaceId = props.workspaceId;
type WorkspaceAISettingsByAgentCache = Partial<
Record<string, { model: string; thinkingLevel: ThinkingLevel }>
>;
const normalizedAgentId =
readPersistedState<string>(getAgentIdKey(scopeId), WORKSPACE_DEFAULTS.agentId)
.trim()
.toLowerCase() || WORKSPACE_DEFAULTS.agentId;
updatePersistedState<WorkspaceAISettingsByAgentCache>(
getWorkspaceAISettingsByAgentKey(workspaceId),
(prev) => {
const record: WorkspaceAISettingsByAgentCache =
prev && typeof prev === "object" ? prev : {};
return {
...record,
[normalizedAgentId]: { model, thinkingLevel: level },
};
},
{}
);
if (!api) {
return;
}
// Avoid stale backend metadata clobbering newer local preferences when users
// click through levels quickly (tests reproduce this by cycling to xhigh).
markPendingWorkspaceAiSettings(workspaceId, normalizedAgentId, {
model,
thinkingLevel: level,
});
api.workspace
.updateAgentAISettings({
workspaceId,
agentId: normalizedAgentId,
aiSettings: { model, thinkingLevel: level },
})
.then((result) => {
if (!result.success) {
clearPendingWorkspaceAiSettings(workspaceId, normalizedAgentId);
}
})
.catch(() => {
clearPendingWorkspaceAiSettings(workspaceId, normalizedAgentId);
// Best-effort only. If offline or backend is old, the next sendMessage will persist.
});
},
[api, defaultModel, props.workspaceId, scopeId, setThinkingLevelInternal]
);
// Global keybind: cycle thinking level (Ctrl/Cmd+Shift+T).
// Implemented at the ThinkingProvider level so it works in both the workspace view
// and the "New Workspace" creation screen (which doesn't mount AIView).
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
if (!matchesKeybind(e, KEYBINDS.TOGGLE_THINKING)) {
return;
}
e.preventDefault();
const model = getCanonicalModelForScope(scopeId, defaultModel);
const allowed = getThinkingPolicyForModel(model);
if (allowed.length <= 1) {
return;
}
const effectiveThinkingLevel = enforceThinkingPolicy(model, thinkingLevel);
const currentIndex = allowed.indexOf(effectiveThinkingLevel);
const nextIndex = (currentIndex + 1) % allowed.length;
setThinkingLevel(allowed[nextIndex]);
};
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, [defaultModel, scopeId, thinkingLevel, setThinkingLevel]);
// Memoize context value to prevent unnecessary re-renders of consumers.
const contextValue = useMemo(
() => ({ thinkingLevel, setThinkingLevel }),
[thinkingLevel, setThinkingLevel]
);
return <ThinkingContext.Provider value={contextValue}>{props.children}</ThinkingContext.Provider>;
};
export const useThinking = () => {
const context = useContext(ThinkingContext);
if (!context) {
throw new Error("useThinking must be used within a ThinkingProvider");
}
return context;
};