mirror of
https://github.com/voideditor/void
synced 2026-05-24 09:58:23 +00:00
fixed merge conflicts
This commit is contained in:
parent
c3e2dc1621
commit
14a8b0ee4e
1 changed files with 69 additions and 6 deletions
|
|
@ -8,11 +8,11 @@ export type ApiConfig = {
|
||||||
apikey: string;
|
apikey: string;
|
||||||
model: string;
|
model: string;
|
||||||
maxTokens: string;
|
maxTokens: string;
|
||||||
};
|
},
|
||||||
openai: {
|
openai: {
|
||||||
apikey: string;
|
apikey: string;
|
||||||
model: string;
|
model: string;
|
||||||
};
|
},
|
||||||
greptile: {
|
greptile: {
|
||||||
apikey: string;
|
apikey: string;
|
||||||
githubPAT: string;
|
githubPAT: string;
|
||||||
|
|
@ -20,14 +20,19 @@ export type ApiConfig = {
|
||||||
remote: string; // e.g. 'github'
|
remote: string; // e.g. 'github'
|
||||||
repository: string; // e.g. 'voideditor/void'
|
repository: string; // e.g. 'voideditor/void'
|
||||||
branch: string; // e.g. 'main'
|
branch: string; // e.g. 'main'
|
||||||
};
|
}
|
||||||
};
|
},
|
||||||
ollama: {
|
ollama: {
|
||||||
endpoint: string;
|
endpoint: string;
|
||||||
model: string;
|
model: string;
|
||||||
};
|
},
|
||||||
|
openaicompatible: {
|
||||||
|
endpoint: string,
|
||||||
|
model: string,
|
||||||
|
apikey: string
|
||||||
|
}
|
||||||
whichApi: string;
|
whichApi: string;
|
||||||
};
|
}
|
||||||
|
|
||||||
type OnText = (newText: string, fullText: string) => void;
|
type OnText = (newText: string, fullText: string) => void;
|
||||||
|
|
||||||
|
|
@ -199,6 +204,56 @@ const sendOpenAIMsg: SendLLMMessageFnTypeInternal = ({
|
||||||
return { abort };
|
return { abort };
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// OpenAI Compatible
|
||||||
|
const sendOpenAICompatibleMsg: SendLLMMessageFnTypeInternal = ({ messages, onText, onFinalMessage, onError, apiConfig }) => {
|
||||||
|
|
||||||
|
let didAbort = false
|
||||||
|
let fullText = ''
|
||||||
|
|
||||||
|
// if abort is called, onFinalMessage is NOT called, and no later onTexts are called either
|
||||||
|
let abort: () => void = () => {
|
||||||
|
didAbort = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
const openai = new OpenAI({ apiKey: apiConfig.openaicompatible.apikey, baseURL: apiConfig.openaicompatible.endpoint, dangerouslyAllowBrowser: true });
|
||||||
|
|
||||||
|
openai.chat.completions.create({
|
||||||
|
model: apiConfig.openaicompatible.model,
|
||||||
|
messages: messages,
|
||||||
|
stream: true,
|
||||||
|
})
|
||||||
|
.then(async response => {
|
||||||
|
abort = () => {
|
||||||
|
response.controller.abort()
|
||||||
|
didAbort = true;
|
||||||
|
}
|
||||||
|
// when receive text
|
||||||
|
try {
|
||||||
|
for await (const chunk of response) {
|
||||||
|
if (didAbort) return;
|
||||||
|
const newText = chunk.choices[0]?.delta?.content || '';
|
||||||
|
fullText += newText;
|
||||||
|
onText(newText, fullText);
|
||||||
|
}
|
||||||
|
onFinalMessage(fullText);
|
||||||
|
}
|
||||||
|
// when error/fail
|
||||||
|
catch (error) {
|
||||||
|
onError(`Error in OpenAI stream:, ${error}`)
|
||||||
|
console.error('Error in OpenAI stream:', error);
|
||||||
|
onFinalMessage(fullText);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((responseError) => {
|
||||||
|
if (responseError.status === 401) {
|
||||||
|
onError('Unauthorized: Invalid API key');
|
||||||
|
} else {
|
||||||
|
onError(responseError.message);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return { abort };
|
||||||
|
};
|
||||||
|
|
||||||
// Ollama
|
// Ollama
|
||||||
const sendOllamaMsg: SendLLMMessageFnTypeInternal = ({
|
const sendOllamaMsg: SendLLMMessageFnTypeInternal = ({
|
||||||
messages,
|
messages,
|
||||||
|
|
@ -401,6 +456,14 @@ export const sendLLMMessage: SendLLMMessageFnTypeExternal = ({
|
||||||
onError,
|
onError,
|
||||||
apiConfig,
|
apiConfig,
|
||||||
});
|
});
|
||||||
|
case 'openaicompatible':
|
||||||
|
return sendOpenAICompatibleMsg({
|
||||||
|
messages,
|
||||||
|
onText,
|
||||||
|
onFinalMessage,
|
||||||
|
onError,
|
||||||
|
apiConfig,
|
||||||
|
});
|
||||||
default:
|
default:
|
||||||
onError(`Error: whichApi was '${apiConfig.whichApi}', which is not recognized!`);
|
onError(`Error: whichApi was '${apiConfig.whichApi}', which is not recognized!`);
|
||||||
return { abort: () => {} };
|
return { abort: () => {} };
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue