Skip to content

Commit fad4197

Browse files
authored
fix: api-key manager cleanup and log error on llm call (#1077)
* fix: api-key manager cleanup and log error on llm call * log improved
1 parent 3a298f1 commit fad4197

File tree

6 files changed

+65
-44
lines changed

6 files changed

+65
-44
lines changed

app/components/chat/APIKeyManager.tsx

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import React, { useState, useEffect, useCallback } from 'react';
22
import { IconButton } from '~/components/ui/IconButton';
33
import type { ProviderInfo } from '~/types/model';
44
import Cookies from 'js-cookie';
5-
import { providerBaseUrlEnvKeys } from '~/utils/constants';
65

76
interface APIKeyManagerProps {
87
provider: ProviderInfo;
@@ -93,17 +92,15 @@ export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey,
9392
<span className="text-sm font-medium text-bolt-elements-textSecondary">{provider?.name} API Key:</span>
9493
{!isEditing && (
9594
<div className="flex items-center gap-2">
96-
{isEnvKeySet ? (
95+
{apiKey ? (
9796
<>
9897
<div className="i-ph:check-circle-fill text-green-500 w-4 h-4" />
99-
<span className="text-xs text-green-500">
100-
Set via {providerBaseUrlEnvKeys[provider.name].apiTokenKey} environment variable
101-
</span>
98+
<span className="text-xs text-green-500">Set via UI</span>
10299
</>
103-
) : apiKey ? (
100+
) : isEnvKeySet ? (
104101
<>
105102
<div className="i-ph:check-circle-fill text-green-500 w-4 h-4" />
106-
<span className="text-xs text-green-500">Set via UI</span>
103+
<span className="text-xs text-green-500">Set via environment variable</span>
107104
</>
108105
) : (
109106
<>
@@ -117,7 +114,7 @@ export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey,
117114
</div>
118115

119116
<div className="flex items-center gap-2 shrink-0">
120-
{isEditing && !isEnvKeySet ? (
117+
{isEditing ? (
121118
<div className="flex items-center gap-2">
122119
<input
123120
type="password"
@@ -145,16 +142,16 @@ export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey,
145142
</div>
146143
) : (
147144
<>
148-
{!isEnvKeySet && (
145+
{
149146
<IconButton
150147
onClick={() => setIsEditing(true)}
151148
title="Edit API Key"
152149
className="bg-blue-500/10 hover:bg-blue-500/20 text-blue-500"
153150
>
154151
<div className="i-ph:pencil-simple w-4 h-4" />
155152
</IconButton>
156-
)}
157-
{provider?.getApiKeyLink && !isEnvKeySet && (
153+
}
154+
{provider?.getApiKeyLink && !apiKey && (
158155
<IconButton
159156
onClick={() => window.open(provider?.getApiKeyLink)}
160157
title="Get API Key"

app/components/chat/Chat.client.tsx

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -137,35 +137,36 @@ export const ChatImpl = memo(
137137

138138
const [apiKeys, setApiKeys] = useState<Record<string, string>>({});
139139

140-
const { messages, isLoading, input, handleInputChange, setInput, stop, append, setMessages, reload } = useChat({
141-
api: '/api/chat',
142-
body: {
143-
apiKeys,
144-
files,
145-
promptId,
146-
contextOptimization: contextOptimizationEnabled,
147-
},
148-
sendExtraMessageFields: true,
149-
onError: (error) => {
150-
logger.error('Request failed\n\n', error);
151-
toast.error(
152-
'There was an error processing your request: ' + (error.message ? error.message : 'No details were returned'),
153-
);
154-
},
155-
onFinish: (message, response) => {
156-
const usage = response.usage;
157-
158-
if (usage) {
159-
console.log('Token usage:', usage);
160-
161-
// You can now use the usage data as needed
162-
}
140+
const { messages, isLoading, input, handleInputChange, setInput, stop, append, setMessages, reload, error } =
141+
useChat({
142+
api: '/api/chat',
143+
body: {
144+
apiKeys,
145+
files,
146+
promptId,
147+
contextOptimization: contextOptimizationEnabled,
148+
},
149+
sendExtraMessageFields: true,
150+
onError: (e) => {
151+
logger.error('Request failed\n\n', e, error);
152+
toast.error(
153+
'There was an error processing your request: ' + (e.message ? e.message : 'No details were returned'),
154+
);
155+
},
156+
onFinish: (message, response) => {
157+
const usage = response.usage;
163158

164-
logger.debug('Finished streaming');
165-
},
166-
initialMessages,
167-
initialInput: Cookies.get(PROMPT_COOKIE_KEY) || '',
168-
});
159+
if (usage) {
160+
console.log('Token usage:', usage);
161+
162+
// You can now use the usage data as needed
163+
}
164+
165+
logger.debug('Finished streaming');
166+
},
167+
initialMessages,
168+
initialInput: Cookies.get(PROMPT_COOKIE_KEY) || '',
169+
});
169170
useEffect(() => {
170171
const prompt = searchParams.get('prompt');
171172

@@ -263,6 +264,10 @@ export const ChatImpl = memo(
263264
*/
264265
await workbenchStore.saveAllFiles();
265266

267+
if (error != null) {
268+
setMessages(messages.slice(0, -1));
269+
}
270+
266271
const fileModifications = workbenchStore.getFileModifcations();
267272

268273
chatStore.setKey('aborted', false);

app/lib/.server/llm/stream-text.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ export async function streamText(props: {
226226

227227
logger.info(`Sending llm call to ${provider.name} with model ${modelDetails.name}`);
228228

229-
return _streamText({
229+
return await _streamText({
230230
model: provider.getModelInstance({
231231
model: currentModel,
232232
serverEnv,

app/routes/api.chat.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
122122
return;
123123
},
124124
};
125+
const totalMessageContent = messages.reduce((acc, message) => acc + message.content, '');
126+
logger.debug(`Total message length: ${totalMessageContent.split(' ').length}, words`);
125127

126128
const result = await streamText({
127129
messages,
@@ -134,13 +136,27 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
134136
contextOptimization,
135137
});
136138

139+
(async () => {
140+
for await (const part of result.fullStream) {
141+
if (part.type === 'error') {
142+
const error: any = part.error;
143+
logger.error(`${error}`);
144+
145+
return;
146+
}
147+
}
148+
})();
149+
137150
stream.switchSource(result.toDataStream());
138151

152+
// return createrespo
139153
return new Response(stream.readable, {
140154
status: 200,
141155
headers: {
142-
contentType: 'text/event-stream',
143-
connection: 'keep-alive',
156+
'Content-Type': 'text/event-stream; charset=utf-8',
157+
Connection: 'keep-alive',
158+
'Cache-Control': 'no-cache',
159+
'Text-Encoding': 'chunked',
144160
},
145161
});
146162
} catch (error: any) {

app/routes/api.check-env-key.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { LoaderFunction } from '@remix-run/node';
1+
import type { LoaderFunction } from '@remix-run/cloudflare';
22
import { providerBaseUrlEnvKeys } from '~/utils/constants';
33

44
export const loader: LoaderFunction = async ({ context, request }) => {

app/routes/api.enhancer.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,10 @@ async function enhancerAction({ context, request }: ActionFunctionArgs) {
107107
return new Response(result.textStream, {
108108
status: 200,
109109
headers: {
110-
'Content-Type': 'text/plain; charset=utf-8',
110+
'Content-Type': 'text/event-stream',
111+
Connection: 'keep-alive',
112+
'Cache-Control': 'no-cache',
113+
'Text-Encoding': 'chunked',
111114
},
112115
});
113116
} catch (error: unknown) {

0 commit comments

Comments
 (0)