File size: 4,151 Bytes
04f192c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<!--
SPDX-FileCopyrightText: Copyright contributors to the CopilotJ project.

SPDX-License-Identifier: Apache-2.0
-->

<script setup lang="ts">
import { computed, ref, watch } from "vue";
import { IconAlertTriangle, IconLoader2 } from "@tabler/icons-vue";
import { useModelGroups } from "../composables";

const props = withDefaults(
  defineProps<{
    modelValue: string;
    provider: string;
    disabled?: boolean;
    inputId?: string;
    placeholder?: string;
    /** Ollama host; only used when provider is "ollama" to list installed models. */
    baseUrl?: string;
  }>(),
  {
    disabled: false,
    inputId: undefined,
    placeholder: "Select or type a model",
    baseUrl: undefined,
  },
);

const emit = defineEmits<{
  (e: "update:modelValue", value: string): void;
}>();

// AutoComplete's v-model is polymorphic: string on free text, object on selection.
// We normalise it to always emit a plain string.
const internalValue = ref<string | { label: string; value: string }>(props.modelValue);

// Use a "committed" snapshot for the composable so the "Current" fallback group
// stays stable during active typing and only updates on blur / selection.
const committedModel = ref(props.modelValue);

const providerRef = computed(() => props.provider);
const baseUrlRef = computed(() => props.baseUrl);

const { suggestions, search, isOllamaModel, ollamaStatus, reloadOllama } = useModelGroups(
  computed(() => {
    const v = internalValue.value;
    return typeof v === "string" ? v : (v?.value ?? "");
  }),
  providerRef,
  baseUrlRef,
);

// Sync incoming prop changes.
watch(
  () => props.modelValue,
  (newVal) => {
    internalValue.value = newVal;
    committedModel.value = newVal;
  },
);

// Normalise outgoing value to always be a string.
watch(internalValue, (newVal) => {
  const normalized = typeof newVal === "string" ? newVal : (newVal?.value ?? "");
  if (normalized !== props.modelValue) {
    emit("update:modelValue", normalized);
  }
});

function onBlur() {
  const v = internalValue.value;
  committedModel.value = typeof v === "string" ? v : (v?.value ?? "");
}

defineExpose({ isOllamaModel, reloadOllama });
</script>

<template>
  <div>
    <AutoComplete
      v-model="internalValue"
      :suggestions="suggestions"
      @complete="search"
      @blur="onBlur"
      optionLabel="label"
      optionGroupLabel="label"
      optionGroupChildren="items"
      dropdown
      dropdownMode="blank"
      :forceSelection="false"
      :disabled="disabled"
      :inputId="inputId"
      :placeholder="placeholder"
      class="w-full"
    >
      <template #optiongroup="slotProps">
        <div class="font-bold text-sm text-slate-500 dark:text-slate-400 px-2 py-1">
          {{ slotProps.option.label }}
        </div>
      </template>
      <template #option="slotProps">
        <div class="flex items-center justify-between gap-2">
          <span>{{ slotProps.option.label }}</span>
          <span class="text-xs text-slate-400 font-mono">{{ slotProps.option.value }}</span>
        </div>
      </template>
    </AutoComplete>

    <!-- Ollama fetch status: surface failures instead of an empty dropdown. -->
    <p
      v-if="provider === 'ollama' && ollamaStatus !== 'idle' && ollamaStatus !== 'ready'"
      class="mt-1 flex items-center gap-1.5 text-xs"
      :class="
        ollamaStatus === 'unreachable' ? 'text-amber-600 dark:text-amber-500' : 'text-slate-500 dark:text-slate-400'
      "
    >
      <template v-if="ollamaStatus === 'loading'">
        <IconLoader2 size="14" class="animate-spin" />
        <span>Loading models from Ollama…</span>
      </template>
      <template v-else-if="ollamaStatus === 'unreachable'">
        <IconAlertTriangle size="14" />
        <span
          >Couldn't reach Ollama at <code class="font-mono">{{ baseUrl || "the configured host" }}</code
          >. Is it running?</span
        >
      </template>
      <template v-else>
        <span
          >Ollama is reachable but has no models installed. Run
          <code class="font-mono">ollama pull &lt;model&gt;</code>.</span
        >
      </template>
    </p>
  </div>
</template>