Skip to content

Commit 9ee7500

Browse files
committed
refactor(chat): replace direct property access with getChatOptions method in CustomModelProvider and update GenuiChat initialization
1 parent 1831048 commit 9ee7500

2 files changed

Lines changed: 46 additions & 67 deletions

File tree

packages/frameworks/vue/src/chat/CustomModelProvider.ts

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ async function readChunk(reader: ReadableStreamDefaultReader<Uint8Array>, handle
2525
}
2626
return true;
2727
}
28-
export interface ICustomModelProviderOptions {
28+
29+
/** 与 {@link chat} 入参对齐的选项;由调用方在每次请求前给出当前值(与 props / 状态同步) */
30+
export interface ICustomModelChatOptions {
2931
url: string;
3032
model: string;
3133
temperature: number;
@@ -36,58 +38,47 @@ export interface ICustomModelProviderOptions {
3638
customActions: ICustomActionItem[];
3739
customFetch?: CustomFetch;
3840
}
41+
42+
export interface ICustomModelProviderOptions {
43+
getChatOptions: () => ICustomModelChatOptions;
44+
}
45+
3946
export class CustomModelProvider extends BaseModelProvider {
40-
private url: string;
41-
private model: string;
42-
private temperature: number;
43-
private customComponents: ICustomComponentItem[];
44-
private customSnippets: IGenPromptSnippet[];
45-
private customExamples: IGenPromptExample[];
46-
private customActions: ICustomActionItem[];
47-
private chatConfig: IChatConfig;
48-
private customFetch?: CustomFetch;
47+
private getChatOptions: () => ICustomModelChatOptions;
4948
protected responseHandlers: IResponseHandler<IStreamData>[] = [];
50-
constructor({ url, model, temperature, chatConfig, customComponents, customSnippets, customExamples, customActions, customFetch }: ICustomModelProviderOptions) {
49+
constructor({ getChatOptions }: ICustomModelProviderOptions) {
5150
super({ provider: 'custom' });
52-
this.url = url;
53-
this.model = model;
54-
this.temperature = temperature;
55-
this.customComponents = customComponents;
56-
this.customSnippets = customSnippets;
57-
this.customExamples = customExamples;
58-
this.customActions = customActions;
59-
this.chatConfig = chatConfig;
60-
this.customFetch = customFetch;
51+
this.getChatOptions = getChatOptions;
6152
}
6253
validateRequest(_: ChatCompletionRequest) { }
6354

64-
changeLlmConfig(model: string, temperature: number) {
65-
this.model = model;
66-
this.temperature = temperature;
67-
}
68-
69-
setCustomExamples(customExamples: IGenPromptExample[]) {
70-
this.customExamples = customExamples;
71-
}
7255
setResponseHandlers(handlers: IResponseHandler<IStreamData>[]) {
7356
this.responseHandlers = handlers;
7457
}
7558

7659
async getData(request: ChatCompletionRequest) {
77-
return await chat(
78-
{
79-
url: this.url,
80-
messages: request.messages,
81-
model: this.model,
82-
temperature: this.temperature,
83-
signal: request.options?.signal,
84-
customComponents: this.customComponents,
85-
customSnippets: this.customSnippets,
86-
customExamples: this.customExamples,
87-
customActions: this.customActions,
88-
customFetch: this.customFetch,
89-
}
90-
);
60+
const {
61+
url,
62+
model,
63+
temperature,
64+
customComponents,
65+
customSnippets,
66+
customExamples,
67+
customActions,
68+
customFetch,
69+
} = this.getChatOptions();
70+
return await chat({
71+
url,
72+
messages: request.messages,
73+
model,
74+
temperature,
75+
signal: request.options?.signal,
76+
customComponents,
77+
customSnippets,
78+
customExamples,
79+
customActions,
80+
customFetch,
81+
});
9182
}
9283

9384
async chat(_: ChatCompletionRequest) {
@@ -108,7 +99,8 @@ export class CustomModelProvider extends BaseModelProvider {
10899
const reader = bodyStream.getReader();
109100

110101
const context: any = {};
111-
context.chatConfig = this.chatConfig;
102+
const { chatConfig } = this.getChatOptions();
103+
context.chatConfig = chatConfig;
112104

113105
const signal = request.options?.signal;
114106
signal?.addEventListener('abort',

packages/frameworks/vue/src/chat/GenuiChat.vue

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,17 @@ const responseHandlers: Ref<IResponseHandler<IStreamData>[]> = ref(defaultRespon
242242
243243
244244
const customModelProvider = new CustomModelProvider({
245-
url: props.url,
246-
model: props.model || '',
247-
temperature: props.temperature ?? 0.3,
248-
chatConfig: props.chatConfig || { addToolCallContext: false, showThinkingResult: false },
249-
customComponents: props.customComponents || [],
250-
customSnippets: props.customSnippets || [],
251-
customExamples: props.customExamples || [],
252-
customActions: [...(props.customActions || []), continueChatAction, saveStateAction],
253-
customFetch: props.customFetch,
245+
getChatOptions: () => ({
246+
url: props.url,
247+
model: props.model || '',
248+
temperature: props.temperature ?? 0.3,
249+
chatConfig: props.chatConfig || { addToolCallContext: false, showThinkingResult: false },
250+
customComponents: props.customComponents || [],
251+
customSnippets: props.customSnippets || [],
252+
customExamples: props.customExamples || [],
253+
customActions: [...(props.customActions || []), continueChatAction, saveStateAction],
254+
customFetch: props.customFetch,
255+
}),
254256
});
255257
customModelProvider.setResponseHandlers(responseHandlers.value);
256258
@@ -461,21 +463,6 @@ watch(
461463
},
462464
);
463465
464-
watch(
465-
() => [props.model, props.temperature],
466-
() => {
467-
customModelProvider.changeLlmConfig(props.model || '', props.temperature ?? 0.3);
468-
},
469-
);
470-
471-
watch(
472-
() => props.customExamples,
473-
(examples) => {
474-
customModelProvider.setCustomExamples(examples ?? []);
475-
},
476-
{ deep: true },
477-
);
478-
479466
defineExpose({
480467
setInputMessage,
481468
handleNewConversation,

0 commit comments

Comments
 (0)