refactor: refactor ai module
This commit is contained in:
parent
07bc7c9cd5
commit
312945cca0
|
@ -15,7 +15,7 @@ export interface AiGlobalConfig {
|
|||
models: Record<string, AiModelConfig>,
|
||||
modelFactory?: AiModelFactory,
|
||||
onTokenConsume?: (modelName: string, modelConfig: AiModelConfig, count: number) => void,
|
||||
onCreateClientUrl?: (modelName: string, modelConfig: AiModelConfig, onFinished: (url: string) => void) => void,
|
||||
onCreateClientUrl?: (modelName: string, modelConfig: AiModelConfig, onSuccess: (url: string) => void, onFailure: () => void) => void
|
||||
bubblePanelEnable?: boolean,
|
||||
bubblePanelModel?: string,
|
||||
menus?: AiMenu[],
|
||||
|
|
|
@ -20,31 +20,38 @@ export abstract class AiModel {
|
|||
}
|
||||
|
||||
chatWithPayload(payload: any, listener: AiMessageListener): void {
|
||||
const startFunc = (url: string) => {
|
||||
const onSuccess = (url: string) => {
|
||||
const aiClient = this.createAiClient(url, listener);
|
||||
aiClient.start(typeof payload === "string" ? payload : JSON.stringify(payload))
|
||||
}
|
||||
const onFailure = () => {
|
||||
listener?.onStop();
|
||||
}
|
||||
|
||||
if (this.globalConfig.onCreateClientUrl) {
|
||||
this.globalConfig.onCreateClientUrl(this.aiModelName, this.aiModelConfig, startFunc)
|
||||
this.globalConfig.onCreateClientUrl(this.aiModelName, this.aiModelConfig, onSuccess, onFailure)
|
||||
} else {
|
||||
startFunc(this.createAiClientUrl())
|
||||
onSuccess(this.createAiClientUrl())
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
chat(selectedText: string, prompt: string, listener: AiMessageListener): void {
|
||||
const startFunc = (url: string) => {
|
||||
const onSuccess = (url: string) => {
|
||||
const aiClient = this.createAiClient(url, listener);
|
||||
const finalPrompt = prompt.includes("{content}") ? prompt.split('{content}').join(selectedText) : `${selectedText}\n${prompt}`
|
||||
const payload = this.wrapPayload(finalPrompt);
|
||||
aiClient.start(typeof payload === "string" ? payload : JSON.stringify(payload))
|
||||
}
|
||||
|
||||
const onFailure = () => {
|
||||
listener?.onStop();
|
||||
}
|
||||
|
||||
if (this.globalConfig.onCreateClientUrl) {
|
||||
this.globalConfig.onCreateClientUrl(this.aiModelName, this.aiModelConfig, startFunc)
|
||||
this.globalConfig.onCreateClientUrl(this.aiModelName, this.aiModelConfig, onSuccess, onFailure)
|
||||
} else {
|
||||
startFunc(this.createAiClientUrl())
|
||||
onSuccess(this.createAiClientUrl())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
16
src/index.ts
16
src/index.ts
|
@ -1,4 +1,16 @@
|
|||
|
||||
export * from './core/AiEditor.ts';
|
||||
export * from './ai/AiGlobalConfig.ts';
|
||||
export * from './ai/AiModelFactory.ts';
|
||||
export * from './ai/AiModelManager.ts';
|
||||
export * from './ai/spark/SparkAiModel.ts';
|
||||
export * from './ai/core/AiClient.ts';
|
||||
export * from './ai/core/AiClientListener.ts';
|
||||
export * from './ai/core/AiMessage.ts';
|
||||
export * from './ai/core/AiMessageListener.ts';
|
||||
export * from './ai/custom/CustomAiModel.ts';
|
||||
export * from './ai/custom/CustomAiModelConfig.ts';
|
||||
export * from './ai/openai/OpenaiAiModel.ts';
|
||||
export * from './ai/openai/OpenaiModelConfig.ts';
|
||||
export * from './ai/spark/SparkAiModel.ts';
|
||||
export * from './ai/spark/SparkAiModelConfig.ts';
|
||||
export * from './ai/wenxin/WenXinAiModel.ts';
|
||||
export * from './ai/wenxin/WenXinAiModelConfig.ts';
|
Loading…
Reference in New Issue