Skip to content

Commit 69269b5

Browse files
webui: stabilize dropdown, simplify model extraction, and init assistant model field
1 parent 26742f7 commit 69269b5

File tree

3 files changed

+28
-55
lines changed

3 files changed

+28
-55
lines changed

tools/server/webui/src/lib/components/app/chat/ChatForm/ChatFormModelSelector.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@
142142
$effect(() => {
143143
const optionCount = options.length;
144144
145-
if (!isOpen || optionCount < 0) return;
145+
if (!isOpen || optionCount <= 0) return;
146146
147147
queueMicrotask(() => updateMenuPosition());
148148
});

tools/server/webui/src/lib/services/chat.ts

Lines changed: 21 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -639,65 +639,35 @@ export class ChatService {
639639
}
640640

641641
private extractModelName(data: unknown): string | undefined {
642-
if (!data || typeof data !== 'object') {
643-
return undefined;
644-
}
645-
646-
const record = data as Record<string, unknown>;
647-
const normalize = (value: unknown): string | undefined => {
648-
if (typeof value !== 'string') {
649-
return undefined;
650-
}
651-
652-
const trimmed = value.trim();
642+
const asRecord = (value: unknown): Record<string, unknown> | undefined => {
643+
return typeof value === 'object' && value !== null
644+
? (value as Record<string, unknown>)
645+
: undefined;
646+
};
653647

654-
return trimmed.length > 0 ? trimmed : undefined;
648+
const getTrimmedString = (value: unknown): string | undefined => {
649+
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
655650
};
656651

657-
const rootModel = normalize(record['model']);
658-
if (rootModel) {
659-
return rootModel;
660-
}
652+
const root = asRecord(data);
653+
if (!root) return undefined;
661654

662-
const choices = record['choices'];
663-
if (!Array.isArray(choices) || choices.length === 0) {
664-
return undefined;
665-
}
655+
// 1) root (some implementations provide `model` at the top level)
656+
const rootModel = getTrimmedString(root.model);
657+
if (rootModel) return rootModel;
666658

667-
const firstChoice = choices[0] as Record<string, unknown> | undefined;
668-
if (!firstChoice) {
669-
return undefined;
670-
}
659+
// 2) streaming choice (delta) or final response (message)
660+
const firstChoice = Array.isArray(root.choices) ? asRecord(root.choices[0]) : undefined;
661+
if (!firstChoice) return undefined;
671662

672-
const choiceModel = normalize(firstChoice['model']);
673-
if (choiceModel) {
674-
return choiceModel;
675-
}
663+
// priority: delta.model (first chunk) else message.model (final response)
664+
const deltaModel = getTrimmedString(asRecord(firstChoice.delta)?.model);
665+
if (deltaModel) return deltaModel;
676666

677-
const delta = firstChoice['delta'] as Record<string, unknown> | undefined;
678-
if (delta) {
679-
const deltaModel = normalize(delta['model']);
680-
if (deltaModel) {
681-
return deltaModel;
682-
}
683-
}
684-
685-
const message = firstChoice['message'] as Record<string, unknown> | undefined;
686-
if (message) {
687-
const messageModel = normalize(message['model']);
688-
if (messageModel) {
689-
return messageModel;
690-
}
691-
}
692-
693-
const metadata = firstChoice['metadata'] as Record<string, unknown> | undefined;
694-
if (metadata) {
695-
const metadataModel = normalize(metadata['model']);
696-
if (metadataModel) {
697-
return metadataModel;
698-
}
699-
}
667+
const messageModel = getTrimmedString(asRecord(firstChoice.message)?.model);
668+
if (messageModel) return messageModel;
700669

670+
// avoid guessing from non-standard locations (metadata, etc.)
701671
return undefined;
702672
}
703673

tools/server/webui/src/lib/stores/chat.svelte.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,8 @@ class ChatStore {
570570
content: '',
571571
timestamp: Date.now(),
572572
thinking: '',
573-
children: []
573+
children: [],
574+
model: null
574575
},
575576
parentId || null
576577
);
@@ -1538,7 +1539,8 @@ class ChatStore {
15381539
role: 'assistant',
15391540
content: '',
15401541
thinking: '',
1541-
children: []
1542+
children: [],
1543+
model: null
15421544
},
15431545
parentMessage.id
15441546
);
@@ -1595,7 +1597,8 @@ class ChatStore {
15951597
role: 'assistant',
15961598
content: '',
15971599
thinking: '',
1598-
children: []
1600+
children: [],
1601+
model: null
15991602
},
16001603
userMessageId
16011604
);

0 commit comments

Comments
 (0)