feat: redact file contents from chat and put latest files into system prompt (#904)
Browse files
app/components/chat/Chat.client.tsx
CHANGED
@@ -118,7 +118,7 @@ export const ChatImpl = memo(
|
|
118 |
const [searchParams, setSearchParams] = useSearchParams();
|
119 |
const files = useStore(workbenchStore.files);
|
120 |
const actionAlert = useStore(workbenchStore.alert);
|
121 |
-
const { activeProviders, promptId } = useSettings();
|
122 |
|
123 |
const [model, setModel] = useState(() => {
|
124 |
const savedModel = Cookies.get('selectedModel');
|
@@ -141,6 +141,7 @@ export const ChatImpl = memo(
|
|
141 |
apiKeys,
|
142 |
files,
|
143 |
promptId,
|
|
|
144 |
},
|
145 |
sendExtraMessageFields: true,
|
146 |
onError: (error) => {
|
|
|
118 |
const [searchParams, setSearchParams] = useSearchParams();
|
119 |
const files = useStore(workbenchStore.files);
|
120 |
const actionAlert = useStore(workbenchStore.alert);
|
121 |
+
const { activeProviders, promptId, contextOptimizationEnabled } = useSettings();
|
122 |
|
123 |
const [model, setModel] = useState(() => {
|
124 |
const savedModel = Cookies.get('selectedModel');
|
|
|
141 |
apiKeys,
|
142 |
files,
|
143 |
promptId,
|
144 |
+
contextOptimization: contextOptimizationEnabled,
|
145 |
},
|
146 |
sendExtraMessageFields: true,
|
147 |
onError: (error) => {
|
app/components/settings/features/FeaturesTab.tsx
CHANGED
@@ -14,6 +14,8 @@ export default function FeaturesTab() {
|
|
14 |
enableLatestBranch,
|
15 |
promptId,
|
16 |
setPromptId,
|
|
|
|
|
17 |
} = useSettings();
|
18 |
|
19 |
const handleToggle = (enabled: boolean) => {
|
@@ -39,6 +41,19 @@ export default function FeaturesTab() {
|
|
39 |
</div>
|
40 |
<Switch className="ml-auto" checked={isLatestBranch} onCheckedChange={enableLatestBranch} />
|
41 |
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
</div>
|
43 |
</div>
|
44 |
|
|
|
14 |
enableLatestBranch,
|
15 |
promptId,
|
16 |
setPromptId,
|
17 |
+
enableContextOptimization,
|
18 |
+
contextOptimizationEnabled,
|
19 |
} = useSettings();
|
20 |
|
21 |
const handleToggle = (enabled: boolean) => {
|
|
|
41 |
</div>
|
42 |
<Switch className="ml-auto" checked={isLatestBranch} onCheckedChange={enableLatestBranch} />
|
43 |
</div>
|
44 |
+
<div className="flex items-center justify-between">
|
45 |
+
<div>
|
46 |
+
<span className="text-bolt-elements-textPrimary">Use Context Optimization</span>
|
47 |
+
<p className="text-sm text-bolt-elements-textSecondary">
|
48 |
+
redact file contents form chat and puts the latest file contents on the system prompt
|
49 |
+
</p>
|
50 |
+
</div>
|
51 |
+
<Switch
|
52 |
+
className="ml-auto"
|
53 |
+
checked={contextOptimizationEnabled}
|
54 |
+
onCheckedChange={enableContextOptimization}
|
55 |
+
/>
|
56 |
+
</div>
|
57 |
</div>
|
58 |
</div>
|
59 |
|
app/lib/.server/llm/stream-text.ts
CHANGED
@@ -150,8 +150,9 @@ export async function streamText(props: {
|
|
150 |
files?: FileMap;
|
151 |
providerSettings?: Record<string, IProviderSetting>;
|
152 |
promptId?: string;
|
|
|
153 |
}) {
|
154 |
-
const { messages, env: serverEnv, options, apiKeys, files, providerSettings, promptId } = props;
|
155 |
|
156 |
// console.log({serverEnv});
|
157 |
|
@@ -170,9 +171,11 @@ export async function streamText(props: {
|
|
170 |
|
171 |
return { ...message, content };
|
172 |
} else if (message.role == 'assistant') {
|
173 |
-
|
174 |
|
175 |
-
|
|
|
|
|
176 |
|
177 |
return { ...message, content };
|
178 |
}
|
@@ -192,11 +195,9 @@ export async function streamText(props: {
|
|
192 |
allowedHtmlElements: allowedHTMLElements,
|
193 |
modificationTagName: MODIFICATIONS_TAG_NAME,
|
194 |
}) ?? getSystemPrompt();
|
195 |
-
let codeContext = '';
|
196 |
|
197 |
-
if (files) {
|
198 |
-
codeContext = createFilesContext(files);
|
199 |
-
codeContext = '';
|
200 |
systemPrompt = `${systemPrompt}\n\n ${codeContext}`;
|
201 |
}
|
202 |
|
|
|
150 |
files?: FileMap;
|
151 |
providerSettings?: Record<string, IProviderSetting>;
|
152 |
promptId?: string;
|
153 |
+
contextOptimization?: boolean;
|
154 |
}) {
|
155 |
+
const { messages, env: serverEnv, options, apiKeys, files, providerSettings, promptId, contextOptimization } = props;
|
156 |
|
157 |
// console.log({serverEnv});
|
158 |
|
|
|
171 |
|
172 |
return { ...message, content };
|
173 |
} else if (message.role == 'assistant') {
|
174 |
+
let content = message.content;
|
175 |
|
176 |
+
if (contextOptimization) {
|
177 |
+
content = simplifyBoltActions(content);
|
178 |
+
}
|
179 |
|
180 |
return { ...message, content };
|
181 |
}
|
|
|
195 |
allowedHtmlElements: allowedHTMLElements,
|
196 |
modificationTagName: MODIFICATIONS_TAG_NAME,
|
197 |
}) ?? getSystemPrompt();
|
|
|
198 |
|
199 |
+
if (files && contextOptimization) {
|
200 |
+
const codeContext = createFilesContext(files);
|
|
|
201 |
systemPrompt = `${systemPrompt}\n\n ${codeContext}`;
|
202 |
}
|
203 |
|
app/lib/hooks/useSettings.tsx
CHANGED
@@ -7,6 +7,7 @@ import {
|
|
7 |
promptStore,
|
8 |
providersStore,
|
9 |
latestBranchStore,
|
|
|
10 |
} from '~/lib/stores/settings';
|
11 |
import { useCallback, useEffect, useState } from 'react';
|
12 |
import Cookies from 'js-cookie';
|
@@ -31,6 +32,7 @@ export function useSettings() {
|
|
31 |
const isLocalModel = useStore(isLocalModelsEnabled);
|
32 |
const isLatestBranch = useStore(latestBranchStore);
|
33 |
const [activeProviders, setActiveProviders] = useState<ProviderInfo[]>([]);
|
|
|
34 |
|
35 |
// Function to check if we're on stable version
|
36 |
const checkIsStableVersion = async () => {
|
@@ -118,6 +120,12 @@ export function useSettings() {
|
|
118 |
} else {
|
119 |
latestBranchStore.set(savedLatestBranch === 'true');
|
120 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
}, []);
|
122 |
|
123 |
// writing values to cookies on change
|
@@ -179,6 +187,12 @@ export function useSettings() {
|
|
179 |
Cookies.set('isLatestBranch', String(enabled));
|
180 |
}, []);
|
181 |
|
|
|
|
|
|
|
|
|
|
|
|
|
182 |
return {
|
183 |
providers,
|
184 |
activeProviders,
|
@@ -193,5 +207,7 @@ export function useSettings() {
|
|
193 |
setPromptId,
|
194 |
isLatestBranch,
|
195 |
enableLatestBranch,
|
|
|
|
|
196 |
};
|
197 |
}
|
|
|
7 |
promptStore,
|
8 |
providersStore,
|
9 |
latestBranchStore,
|
10 |
+
enableContextOptimizationStore,
|
11 |
} from '~/lib/stores/settings';
|
12 |
import { useCallback, useEffect, useState } from 'react';
|
13 |
import Cookies from 'js-cookie';
|
|
|
32 |
const isLocalModel = useStore(isLocalModelsEnabled);
|
33 |
const isLatestBranch = useStore(latestBranchStore);
|
34 |
const [activeProviders, setActiveProviders] = useState<ProviderInfo[]>([]);
|
35 |
+
const contextOptimizationEnabled = useStore(enableContextOptimizationStore);
|
36 |
|
37 |
// Function to check if we're on stable version
|
38 |
const checkIsStableVersion = async () => {
|
|
|
120 |
} else {
|
121 |
latestBranchStore.set(savedLatestBranch === 'true');
|
122 |
}
|
123 |
+
|
124 |
+
const savedContextOptimizationEnabled = Cookies.get('contextOptimizationEnabled');
|
125 |
+
|
126 |
+
if (savedContextOptimizationEnabled) {
|
127 |
+
enableContextOptimizationStore.set(savedContextOptimizationEnabled === 'true');
|
128 |
+
}
|
129 |
}, []);
|
130 |
|
131 |
// writing values to cookies on change
|
|
|
187 |
Cookies.set('isLatestBranch', String(enabled));
|
188 |
}, []);
|
189 |
|
190 |
+
const enableContextOptimization = useCallback((enabled: boolean) => {
|
191 |
+
enableContextOptimizationStore.set(enabled);
|
192 |
+
logStore.logSystem(`Context optimization ${enabled ? 'enabled' : 'disabled'}`);
|
193 |
+
Cookies.set('contextOptimizationEnabled', String(enabled));
|
194 |
+
}, []);
|
195 |
+
|
196 |
return {
|
197 |
providers,
|
198 |
activeProviders,
|
|
|
207 |
setPromptId,
|
208 |
isLatestBranch,
|
209 |
enableLatestBranch,
|
210 |
+
contextOptimizationEnabled,
|
211 |
+
enableContextOptimization,
|
212 |
};
|
213 |
}
|
app/lib/stores/settings.ts
CHANGED
@@ -39,6 +39,9 @@ PROVIDER_LIST.forEach((provider) => {
|
|
39 |
},
|
40 |
};
|
41 |
});
|
|
|
|
|
|
|
42 |
export const providersStore = map<ProviderSetting>(initialProviderSettings);
|
43 |
|
44 |
export const isDebugMode = atom(false);
|
@@ -50,3 +53,5 @@ export const isLocalModelsEnabled = atom(true);
|
|
50 |
export const promptStore = atom<string>('default');
|
51 |
|
52 |
export const latestBranchStore = atom(false);
|
|
|
|
|
|
39 |
},
|
40 |
};
|
41 |
});
|
42 |
+
|
43 |
+
//TODO: need to create one single map for all these flags
|
44 |
+
|
45 |
export const providersStore = map<ProviderSetting>(initialProviderSettings);
|
46 |
|
47 |
export const isDebugMode = atom(false);
|
|
|
53 |
export const promptStore = atom<string>('default');
|
54 |
|
55 |
export const latestBranchStore = atom(false);
|
56 |
+
|
57 |
+
export const enableContextOptimizationStore = atom(false);
|
app/routes/api.chat.ts
CHANGED
@@ -29,10 +29,11 @@ function parseCookies(cookieHeader: string): Record<string, string> {
|
|
29 |
}
|
30 |
|
31 |
async function chatAction({ context, request }: ActionFunctionArgs) {
|
32 |
-
const { messages, files, promptId } = await request.json<{
|
33 |
messages: Messages;
|
34 |
files: any;
|
35 |
promptId?: string;
|
|
|
36 |
}>();
|
37 |
|
38 |
const cookieHeader = request.headers.get('Cookie');
|
@@ -100,6 +101,7 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
|
|
100 |
files,
|
101 |
providerSettings,
|
102 |
promptId,
|
|
|
103 |
});
|
104 |
|
105 |
return stream.switchSource(result.toDataStream());
|
@@ -114,6 +116,7 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
|
|
114 |
files,
|
115 |
providerSettings,
|
116 |
promptId,
|
|
|
117 |
});
|
118 |
|
119 |
stream.switchSource(result.toDataStream());
|
|
|
29 |
}
|
30 |
|
31 |
async function chatAction({ context, request }: ActionFunctionArgs) {
|
32 |
+
const { messages, files, promptId, contextOptimization } = await request.json<{
|
33 |
messages: Messages;
|
34 |
files: any;
|
35 |
promptId?: string;
|
36 |
+
contextOptimization: boolean;
|
37 |
}>();
|
38 |
|
39 |
const cookieHeader = request.headers.get('Cookie');
|
|
|
101 |
files,
|
102 |
providerSettings,
|
103 |
promptId,
|
104 |
+
contextOptimization,
|
105 |
});
|
106 |
|
107 |
return stream.switchSource(result.toDataStream());
|
|
|
116 |
files,
|
117 |
providerSettings,
|
118 |
promptId,
|
119 |
+
contextOptimization,
|
120 |
});
|
121 |
|
122 |
stream.switchSource(result.toDataStream());
|