Skip to content

Commit cef7776

Browse files
webui: stabilize dropdown, simplify model extraction, and init assistant model field
1 parent e0dc324 commit cef7776

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
@@ -652,65 +652,35 @@ export class ChatService {
652652
}
653653

654654
private extractModelName(data: unknown): string | undefined {
655-
if (!data || typeof data !== 'object') {
656-
return undefined;
657-
}
658-
659-
const record = data as Record<string, unknown>;
660-
const normalize = (value: unknown): string | undefined => {
661-
if (typeof value !== 'string') {
662-
return undefined;
663-
}
664-
665-
const trimmed = value.trim();
655+
const asRecord = (value: unknown): Record<string, unknown> | undefined => {
656+
return typeof value === 'object' && value !== null
657+
? (value as Record<string, unknown>)
658+
: undefined;
659+
};
666660

667-
return trimmed.length > 0 ? trimmed : undefined;
661+
const getTrimmedString = (value: unknown): string | undefined => {
662+
return typeof value === 'string' && value.trim() ? value.trim() : undefined;
668663
};
669664

670-
const rootModel = normalize(record['model']);
671-
if (rootModel) {
672-
return rootModel;
673-
}
665+
const root = asRecord(data);
666+
if (!root) return undefined;
674667

675-
const choices = record['choices'];
676-
if (!Array.isArray(choices) || choices.length === 0) {
677-
return undefined;
678-
}
668+
// 1) root (some implementations provide `model` at the top level)
669+
const rootModel = getTrimmedString(root.model);
670+
if (rootModel) return rootModel;
679671

680-
const firstChoice = choices[0] as Record<string, unknown> | undefined;
681-
if (!firstChoice) {
682-
return undefined;
683-
}
672+
// 2) streaming choice (delta) or final response (message)
673+
const firstChoice = Array.isArray(root.choices) ? asRecord(root.choices[0]) : undefined;
674+
if (!firstChoice) return undefined;
684675

685-
const choiceModel = normalize(firstChoice['model']);
686-
if (choiceModel) {
687-
return choiceModel;
688-
}
676+
// priority: delta.model (first chunk) else message.model (final response)
677+
const deltaModel = getTrimmedString(asRecord(firstChoice.delta)?.model);
678+
if (deltaModel) return deltaModel;
689679

690-
const delta = firstChoice['delta'] as Record<string, unknown> | undefined;
691-
if (delta) {
692-
const deltaModel = normalize(delta['model']);
693-
if (deltaModel) {
694-
return deltaModel;
695-
}
696-
}
697-
698-
const message = firstChoice['message'] as Record<string, unknown> | undefined;
699-
if (message) {
700-
const messageModel = normalize(message['model']);
701-
if (messageModel) {
702-
return messageModel;
703-
}
704-
}
705-
706-
const metadata = firstChoice['metadata'] as Record<string, unknown> | undefined;
707-
if (metadata) {
708-
const metadataModel = normalize(metadata['model']);
709-
if (metadataModel) {
710-
return metadataModel;
711-
}
712-
}
680+
const messageModel = getTrimmedString(asRecord(firstChoice.message)?.model);
681+
if (messageModel) return messageModel;
713682

683+
// avoid guessing from non-standard locations (metadata, etc.)
714684
return undefined;
715685
}
716686

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)