feat: pick the room model from a catalog dialog
The room model was a free-text field. It now opens a dialog that lists the provider's live catalog through the same adapter a turn uses, with context, output, price, and vision from models.dev. Rows filter by text, vision, or free price, and a custom id remains available when the provider lists nothing. The dialog is nested over room settings, so BaseDialog now gives Escape and the focus trap to the top dialog only. Clearing the model stores NULL, which returns the room to the provider's default.
This commit is contained in:
1 parent
2d36790fe1
commit
2216f37422
13 files changed
+724
-20
No files matched your search
@@ -0,0 +1,235 @@
|
||||
<script setup lang="ts">
|
||||
import { computed, nextTick, onMounted, ref } from 'vue'
|
||||
import { Check, Search } from 'lucide-vue-next'
|
||||
import type { ModelCard, ModelPricing, ProviderConfig, ProviderModelsResponse } from '~/types'
|
||||
import { apiErrorMessage } from '~/utils/errors'
|
||||
import { formatModelPrice, formatTokens } from '~/utils/format'
|
||||
import BaseDialog from '~/components/BaseDialog.vue'
|
||||
|
||||
const props = defineProps<{ provider: ProviderConfig; selected: string }>()
|
||||
const emit = defineEmits<{ close: []; select: [model: string] }>()
|
||||
|
||||
const api = useApi()
|
||||
|
||||
const models = ref<ModelCard[]>([])
|
||||
const listEl = ref<HTMLElement | null>(null)
|
||||
const loading = ref(true)
|
||||
const error = ref('')
|
||||
const filter = ref('')
|
||||
const visionOnly = ref(false)
|
||||
const freeOnly = ref(false)
|
||||
|
||||
const query = computed(() => filter.value.trim())
|
||||
const queryLower = computed(() => query.value.toLowerCase())
|
||||
const currentMissing = computed(
|
||||
() =>
|
||||
props.selected.length > 0 &&
|
||||
props.selected !== props.provider.model &&
|
||||
!models.value.some((model) => model.id.toLowerCase() === props.selected.toLowerCase()),
|
||||
)
|
||||
const showCustom = computed(
|
||||
() =>
|
||||
query.value.length > 0 &&
|
||||
!models.value.some((model) => model.id.toLowerCase() === queryLower.value),
|
||||
)
|
||||
const filtered = computed(() => {
|
||||
const q = queryLower.value
|
||||
const list = models.value.filter((model) => {
|
||||
if (visionOnly.value && model.supports_vision !== true) return false
|
||||
if (freeOnly.value) {
|
||||
const price = model.pricing
|
||||
if (!price || price.input_per_mtok !== 0 || price.output_per_mtok !== 0) return false
|
||||
}
|
||||
if (!q) return true
|
||||
return [model.id, model.display_name, model.description, model.family ?? '']
|
||||
.some((value) => value.toLowerCase().includes(q))
|
||||
})
|
||||
return list.sort(
|
||||
(a, b) => a.display_name.localeCompare(b.display_name) || a.id.localeCompare(b.id),
|
||||
)
|
||||
})
|
||||
const countLabel = computed(
|
||||
() => `${filtered.value.length} ${filtered.value.length === 1 ? 'model' : 'models'}`,
|
||||
)
|
||||
|
||||
onMounted(load)
|
||||
|
||||
async function load() {
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
const response = await api.get<ProviderModelsResponse>(
|
||||
`/provider-configs/${props.provider.id}/models`,
|
||||
)
|
||||
models.value = response.models
|
||||
} catch (err) {
|
||||
error.value = apiErrorMessage(err)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
await nextTick()
|
||||
listEl.value?.querySelector<HTMLElement>('.model-row-active')?.scrollIntoView({ block: 'nearest' })
|
||||
}
|
||||
|
||||
function priceLabel(pricing: ModelPricing): string {
|
||||
if (pricing.input_per_mtok === 0 && pricing.output_per_mtok === 0) return 'Free'
|
||||
return `${formatModelPrice(pricing.input_per_mtok)} in / ${formatModelPrice(pricing.output_per_mtok)} out`
|
||||
}
|
||||
|
||||
function choose(id: string) {
|
||||
emit('select', id)
|
||||
}
|
||||
|
||||
function onSearchEnter() {
|
||||
const only = filtered.value[0]
|
||||
if (filtered.value.length === 1 && only) {
|
||||
choose(only.id)
|
||||
return
|
||||
}
|
||||
if (filtered.value.length === 0 && showCustom.value) choose(query.value)
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<BaseDialog
|
||||
title="Select model"
|
||||
:description="`${provider.name} · ${provider.kind}. Prices are USD per 1M tokens.`"
|
||||
wide
|
||||
@close="emit('close')"
|
||||
>
|
||||
<div class="model-picker">
|
||||
<div class="model-search">
|
||||
<Search :size="16" class="model-search-icon" aria-hidden="true" />
|
||||
<input
|
||||
v-model="filter"
|
||||
class="input model-search-input"
|
||||
type="search"
|
||||
placeholder="Filter by name, id, or family"
|
||||
aria-label="Filter models"
|
||||
autofocus
|
||||
@keydown.enter.prevent="onSearchEnter"
|
||||
>
|
||||
</div>
|
||||
|
||||
<div class="model-filter-bar">
|
||||
<label class="model-check">
|
||||
<input v-model="visionOnly" type="checkbox">
|
||||
Vision
|
||||
</label>
|
||||
<label class="model-check">
|
||||
<input v-model="freeOnly" type="checkbox">
|
||||
Free
|
||||
</label>
|
||||
<span v-if="!loading && !error" class="model-count">{{ countLabel }}</span>
|
||||
</div>
|
||||
|
||||
<p v-if="loading" class="model-state">Loading models.</p>
|
||||
|
||||
<div v-else ref="listEl" class="model-list">
|
||||
<button
|
||||
type="button"
|
||||
class="model-row"
|
||||
:class="{ 'model-row-active': selected === '' }"
|
||||
@click="choose('')"
|
||||
>
|
||||
<span class="model-row-body">
|
||||
<span class="model-row-head">
|
||||
<span class="model-row-title">Provider default</span>
|
||||
<Check
|
||||
v-if="selected === ''"
|
||||
:size="16"
|
||||
class="model-row-check"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<span v-if="selected === ''" class="sr-only">Current model</span>
|
||||
</span>
|
||||
<span class="model-row-id">{{ provider.model }}</span>
|
||||
</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
v-if="currentMissing"
|
||||
type="button"
|
||||
class="model-row model-row-active"
|
||||
@click="choose(selected)"
|
||||
>
|
||||
<span class="model-row-body">
|
||||
<span class="model-row-head">
|
||||
<span class="model-row-title">Current model</span>
|
||||
<Check :size="16" class="model-row-check" aria-hidden="true" />
|
||||
</span>
|
||||
<span class="model-row-id">{{ selected }}</span>
|
||||
</span>
|
||||
</button>
|
||||
|
||||
<p v-if="error" class="model-error">
|
||||
<span class="form-error">{{ error }}</span>
|
||||
<button type="button" class="btn btn-sm" @click="load">Retry</button>
|
||||
</p>
|
||||
|
||||
<template v-else>
|
||||
<button
|
||||
v-for="model in filtered"
|
||||
:key="model.id"
|
||||
type="button"
|
||||
class="model-row"
|
||||
:class="{ 'model-row-active': model.id === selected }"
|
||||
@click="choose(model.id)"
|
||||
>
|
||||
<span class="model-row-body">
|
||||
<span class="model-row-head">
|
||||
<span class="model-row-title">{{ model.display_name || model.id }}</span>
|
||||
<Check
|
||||
v-if="model.id === selected"
|
||||
:size="16"
|
||||
class="model-row-check"
|
||||
aria-hidden="true"
|
||||
/>
|
||||
<span v-if="model.id === selected" class="sr-only">Current model</span>
|
||||
</span>
|
||||
<span class="model-row-id">{{ model.id }}</span>
|
||||
<span v-if="model.description" class="model-row-desc">{{ model.description }}</span>
|
||||
<span class="model-row-facts">
|
||||
<span v-if="model.supports_vision === true" class="model-fact model-fact-accent">
|
||||
Vision
|
||||
</span>
|
||||
<span v-if="model.input_token_limit" class="model-fact">
|
||||
{{ formatTokens(model.input_token_limit) }} context
|
||||
</span>
|
||||
<span v-if="model.output_token_limit" class="model-fact">
|
||||
{{ formatTokens(model.output_token_limit) }} output
|
||||
</span>
|
||||
<span v-if="model.pricing" class="model-fact">
|
||||
{{ priceLabel(model.pricing) }}
|
||||
</span>
|
||||
<span v-else class="model-fact model-fact-muted">Price unknown</span>
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
|
||||
<p v-if="filtered.length === 0 && !showCustom" class="model-empty">
|
||||
{{ models.length === 0 ? 'No models listed.' : 'No models match.' }}
|
||||
</p>
|
||||
</template>
|
||||
|
||||
<button
|
||||
v-if="showCustom"
|
||||
type="button"
|
||||
class="model-row"
|
||||
@click="choose(query)"
|
||||
>
|
||||
<span class="model-row-body">
|
||||
<span class="model-row-head">
|
||||
<span class="model-row-title">Use custom model</span>
|
||||
</span>
|
||||
<code class="model-row-id">{{ query }}</code>
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="dialog-actions">
|
||||
<button type="button" class="btn btn-quiet" @click="emit('close')">Cancel</button>
|
||||
</div>
|
||||
</div>
|
||||
</BaseDialog>
|
||||
</template>
|
||||
Reference in new issue
Block a user