mirror of
https://github.com/stackblitz-labs/bolt.diy
synced 2025-03-12 14:58:53 +00:00
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
This commit is contained in:
parent
3a298f1586
commit
fad41973e2
@ -2,7 +2,6 @@ import React, { useState, useEffect, useCallback } from 'react';
|
|||||||
import { IconButton } from '~/components/ui/IconButton';
|
import { IconButton } from '~/components/ui/IconButton';
|
||||||
import type { ProviderInfo } from '~/types/model';
|
import type { ProviderInfo } from '~/types/model';
|
||||||
import Cookies from 'js-cookie';
|
import Cookies from 'js-cookie';
|
||||||
import { providerBaseUrlEnvKeys } from '~/utils/constants';
|
|
||||||
|
|
||||||
interface APIKeyManagerProps {
|
interface APIKeyManagerProps {
|
||||||
provider: ProviderInfo;
|
provider: ProviderInfo;
|
||||||
@ -93,18 +92,16 @@ export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey,
|
|||||||
<span className="text-sm font-medium text-bolt-elements-textSecondary">{provider?.name} API Key:</span>
|
<span className="text-sm font-medium text-bolt-elements-textSecondary">{provider?.name} API Key:</span>
|
||||||
{!isEditing && (
|
{!isEditing && (
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
{isEnvKeySet ? (
|
{apiKey ? (
|
||||||
<>
|
|
||||||
<div className="i-ph:check-circle-fill text-green-500 w-4 h-4" />
|
|
||||||
<span className="text-xs text-green-500">
|
|
||||||
Set via {providerBaseUrlEnvKeys[provider.name].apiTokenKey} environment variable
|
|
||||||
</span>
|
|
||||||
</>
|
|
||||||
) : apiKey ? (
|
|
||||||
<>
|
<>
|
||||||
<div className="i-ph:check-circle-fill text-green-500 w-4 h-4" />
|
<div className="i-ph:check-circle-fill text-green-500 w-4 h-4" />
|
||||||
<span className="text-xs text-green-500">Set via UI</span>
|
<span className="text-xs text-green-500">Set via UI</span>
|
||||||
</>
|
</>
|
||||||
|
) : isEnvKeySet ? (
|
||||||
|
<>
|
||||||
|
<div className="i-ph:check-circle-fill text-green-500 w-4 h-4" />
|
||||||
|
<span className="text-xs text-green-500">Set via environment variable</span>
|
||||||
|
</>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
<div className="i-ph:x-circle-fill text-red-500 w-4 h-4" />
|
<div className="i-ph:x-circle-fill text-red-500 w-4 h-4" />
|
||||||
@ -117,7 +114,7 @@ export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey,
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className="flex items-center gap-2 shrink-0">
|
<div className="flex items-center gap-2 shrink-0">
|
||||||
{isEditing && !isEnvKeySet ? (
|
{isEditing ? (
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<input
|
<input
|
||||||
type="password"
|
type="password"
|
||||||
@ -145,7 +142,7 @@ export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey,
|
|||||||
</div>
|
</div>
|
||||||
) : (
|
) : (
|
||||||
<>
|
<>
|
||||||
{!isEnvKeySet && (
|
{
|
||||||
<IconButton
|
<IconButton
|
||||||
onClick={() => setIsEditing(true)}
|
onClick={() => setIsEditing(true)}
|
||||||
title="Edit API Key"
|
title="Edit API Key"
|
||||||
@ -153,8 +150,8 @@ export const APIKeyManager: React.FC<APIKeyManagerProps> = ({ provider, apiKey,
|
|||||||
>
|
>
|
||||||
<div className="i-ph:pencil-simple w-4 h-4" />
|
<div className="i-ph:pencil-simple w-4 h-4" />
|
||||||
</IconButton>
|
</IconButton>
|
||||||
)}
|
}
|
||||||
{provider?.getApiKeyLink && !isEnvKeySet && (
|
{provider?.getApiKeyLink && !apiKey && (
|
||||||
<IconButton
|
<IconButton
|
||||||
onClick={() => window.open(provider?.getApiKeyLink)}
|
onClick={() => window.open(provider?.getApiKeyLink)}
|
||||||
title="Get API Key"
|
title="Get API Key"
|
||||||
|
@ -137,7 +137,8 @@ export const ChatImpl = memo(
|
|||||||
|
|
||||||
const [apiKeys, setApiKeys] = useState<Record<string, string>>({});
|
const [apiKeys, setApiKeys] = useState<Record<string, string>>({});
|
||||||
|
|
||||||
const { messages, isLoading, input, handleInputChange, setInput, stop, append, setMessages, reload } = useChat({
|
const { messages, isLoading, input, handleInputChange, setInput, stop, append, setMessages, reload, error } =
|
||||||
|
useChat({
|
||||||
api: '/api/chat',
|
api: '/api/chat',
|
||||||
body: {
|
body: {
|
||||||
apiKeys,
|
apiKeys,
|
||||||
@ -146,10 +147,10 @@ export const ChatImpl = memo(
|
|||||||
contextOptimization: contextOptimizationEnabled,
|
contextOptimization: contextOptimizationEnabled,
|
||||||
},
|
},
|
||||||
sendExtraMessageFields: true,
|
sendExtraMessageFields: true,
|
||||||
onError: (error) => {
|
onError: (e) => {
|
||||||
logger.error('Request failed\n\n', error);
|
logger.error('Request failed\n\n', e, error);
|
||||||
toast.error(
|
toast.error(
|
||||||
'There was an error processing your request: ' + (error.message ? error.message : 'No details were returned'),
|
'There was an error processing your request: ' + (e.message ? e.message : 'No details were returned'),
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
onFinish: (message, response) => {
|
onFinish: (message, response) => {
|
||||||
@ -263,6 +264,10 @@ export const ChatImpl = memo(
|
|||||||
*/
|
*/
|
||||||
await workbenchStore.saveAllFiles();
|
await workbenchStore.saveAllFiles();
|
||||||
|
|
||||||
|
if (error != null) {
|
||||||
|
setMessages(messages.slice(0, -1));
|
||||||
|
}
|
||||||
|
|
||||||
const fileModifications = workbenchStore.getFileModifcations();
|
const fileModifications = workbenchStore.getFileModifcations();
|
||||||
|
|
||||||
chatStore.setKey('aborted', false);
|
chatStore.setKey('aborted', false);
|
||||||
|
@ -226,7 +226,7 @@ export async function streamText(props: {
|
|||||||
|
|
||||||
logger.info(`Sending llm call to ${provider.name} with model ${modelDetails.name}`);
|
logger.info(`Sending llm call to ${provider.name} with model ${modelDetails.name}`);
|
||||||
|
|
||||||
return _streamText({
|
return await _streamText({
|
||||||
model: provider.getModelInstance({
|
model: provider.getModelInstance({
|
||||||
model: currentModel,
|
model: currentModel,
|
||||||
serverEnv,
|
serverEnv,
|
||||||
|
@ -122,6 +122,8 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
|
|||||||
return;
|
return;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
const totalMessageContent = messages.reduce((acc, message) => acc + message.content, '');
|
||||||
|
logger.debug(`Total message length: ${totalMessageContent.split(' ').length}, words`);
|
||||||
|
|
||||||
const result = await streamText({
|
const result = await streamText({
|
||||||
messages,
|
messages,
|
||||||
@ -134,13 +136,27 @@ async function chatAction({ context, request }: ActionFunctionArgs) {
|
|||||||
contextOptimization,
|
contextOptimization,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
(async () => {
|
||||||
|
for await (const part of result.fullStream) {
|
||||||
|
if (part.type === 'error') {
|
||||||
|
const error: any = part.error;
|
||||||
|
logger.error(`${error}`);
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
||||||
stream.switchSource(result.toDataStream());
|
stream.switchSource(result.toDataStream());
|
||||||
|
|
||||||
|
// return createrespo
|
||||||
return new Response(stream.readable, {
|
return new Response(stream.readable, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
contentType: 'text/event-stream',
|
'Content-Type': 'text/event-stream; charset=utf-8',
|
||||||
connection: 'keep-alive',
|
Connection: 'keep-alive',
|
||||||
|
'Cache-Control': 'no-cache',
|
||||||
|
'Text-Encoding': 'chunked',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import type { LoaderFunction } from '@remix-run/node';
|
import type { LoaderFunction } from '@remix-run/cloudflare';
|
||||||
import { providerBaseUrlEnvKeys } from '~/utils/constants';
|
import { providerBaseUrlEnvKeys } from '~/utils/constants';
|
||||||
|
|
||||||
export const loader: LoaderFunction = async ({ context, request }) => {
|
export const loader: LoaderFunction = async ({ context, request }) => {
|
||||||
|
@ -107,7 +107,10 @@ async function enhancerAction({ context, request }: ActionFunctionArgs) {
|
|||||||
return new Response(result.textStream, {
|
return new Response(result.textStream, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'text/plain; charset=utf-8',
|
'Content-Type': 'text/event-stream',
|
||||||
|
Connection: 'keep-alive',
|
||||||
|
'Cache-Control': 'no-cache',
|
||||||
|
'Text-Encoding': 'chunked',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
|
Loading…
Reference in New Issue
Block a user