$instance['type'] ?? '', 'base_url' => $instance['base_url'] ?? '', ])); if ($refresh) { Cache::forget($cacheKey); } return Cache::remember($cacheKey, now()->addMinutes(5), fn () => $this->fetchModels($instance)); } public function cachedUntil(array $instance): ?string { $models = $this->modelsFor($instance); return $models === [] ? null : now()->addMinutes(5)->format('H:i:s'); } private function fetchModels(array $instance): array { $type = (string) ($instance['type'] ?? ''); $baseUrl = rtrim((string) ($instance['base_url'] ?? ''), '/'); if ($baseUrl === '') { return []; } return match ($type) { 'lmstudio' => $this->fetchLmStudioModels($baseUrl), 'ollama' => $this->fetchOllamaModels($baseUrl), default => [], }; } private function fetchLmStudioModels(string $baseUrl): array { $response = Http::connectTimeout(2) ->timeout(5) ->get($baseUrl.'/v1/models') ->throw() ->json(); return collect($response['data'] ?? []) ->pluck('id') ->filter() ->map(fn ($model) => (string) $model) ->unique() ->sort() ->values() ->all(); } private function fetchOllamaModels(string $baseUrl): array { $response = Http::connectTimeout(2) ->timeout(5) ->get($baseUrl.'/api/tags') ->throw() ->json(); return collect($response['models'] ?? []) ->map(fn ($model) => (string) ($model['name'] ?? $model['model'] ?? '')) ->filter() ->unique() ->sort() ->values() ->all(); } }