move into utils

This commit is contained in:
Simon Knott 2025-02-13 10:17:01 +01:00
parent 9bc2c28847
commit e08c0cf341
No known key found for this signature in database
GPG key ID: 8CEDC00028084AEC
3 changed files with 12 additions and 10 deletions

View file

@ -16,6 +16,7 @@
import * as React from 'react'; import * as React from 'react';
import { EventEmitter } from '@testIsomorphic/events'; import { EventEmitter } from '@testIsomorphic/events';
import { useCookies } from '@web/uiUtils';
export type LLMMessage = { export type LLMMessage = {
role: 'user' | 'assistant' | 'developer'; role: 'user' | 'assistant' | 'developer';
@ -198,22 +199,16 @@ class Conversation {
const llmContext = React.createContext<LLMChat | undefined>(undefined); const llmContext = React.createContext<LLMChat | undefined>(undefined);
function parseCookie(cookie: string): [name: string, value: string][] {
return cookie.split(";").filter(v => v.includes("=")).map(kv => {
const separator = kv.indexOf("=");
return [kv.substring(0, separator), kv.substring(separator + 1)];
})
}
export function LLMProvider({ children }: React.PropsWithChildren<{}>) { export function LLMProvider({ children }: React.PropsWithChildren<{}>) {
const cookies = useCookies();
const chat = React.useMemo(() => { const chat = React.useMemo(() => {
for (const [name, value] of parseCookie(document.cookie)) { for (const [name, value] of cookies) {
if (name === 'openai_api_key') if (name === 'openai_api_key')
return new LLMChat(new OpenAI(value)); return new LLMChat(new OpenAI(value));
if (name === 'anthropic_api_key') if (name === 'anthropic_api_key')
return new LLMChat(new Anthropic(value)) return new LLMChat(new Anthropic(value))
} }
}, []); }, [cookies]);
return <llmContext.Provider value={chat}>{children}</llmContext.Provider>; return <llmContext.Provider value={chat}>{children}</llmContext.Provider>;
}; };

View file

@ -399,7 +399,7 @@ export const UIModeView: React.FC<{}> = ({
}); });
}, [closeInstallDialog, testServerConnection]); }, [closeInstallDialog, testServerConnection]);
return <LLMProvider openai={queryParams.openai_api_key} anthropic={queryParams.anthropic_api_key}><div className='vbox ui-mode'> return <LLMProvider><div className='vbox ui-mode'>
{!hasBrowsers && <dialog ref={dialogRef}> {!hasBrowsers && <dialog ref={dialogRef}>
<div className='title'><span className='codicon codicon-lightbulb'></span>Install browsers</div> <div className='title'><span className='codicon codicon-lightbulb'></span>Install browsers</div>
<div className='body'> <div className='body'>

View file

@ -248,3 +248,10 @@ export function useFlash(): [boolean, EffectCallback] {
}, [setFlash]); }, [setFlash]);
return [flash, trigger]; return [flash, trigger];
} }
export function useCookies() {
return document.cookie.split(";").filter(v => v.includes("=")).map(kv => {
const separator = kv.indexOf("=");
return [kv.substring(0, separator), kv.substring(separator + 1)];
})
}