baseUrl = rtrim($baseUrl ?: config('knowledge.base_url'), '/'); $this->timeout = $timeout ?: (int) config('knowledge.timeout', 10); $this->cacheTtl = $cacheTtl ?: (int) config('knowledge.cache_ttl', 300); } /** * @return Collection> */ public function listKnowledgePoints(int $limit = 300): Collection { return Cache::remember( key: "knowledge-points-{$limit}", ttl: now()->addSeconds($this->cacheTtl), callback: fn () => collect($this->request('GET', '/knowledge-points', [ 'limit' => $limit, ])), ); } /** * @return array */ public function getKnowledgePointDetail(string $kpCode): array { return Cache::remember( key: "knowledge-point-detail-{$kpCode}", ttl: now()->addSeconds($this->cacheTtl), callback: fn () => $this->request('GET', "/graph/node/{$kpCode}"), ); } /** * @return Collection> */ public function listSkills(?string $kpCode = null, int $limit = 200): Collection { return Cache::remember( key: "knowledge-skills-{$kpCode}-{$limit}", ttl: now()->addSeconds($this->cacheTtl), callback: fn () => collect($this->request('GET', '/skills', array_filter([ 'kp_code' => $kpCode, 'limit' => $limit, ]))), ); } /** * @throws RequestException * @return mixed */ protected function request(string $method, string $path, array $params = []): mixed { $response = $this->http()->send($method, ltrim($path, '/'), [ 'query' => $params, ]); return $response->throw()->json(); } protected function http(): PendingRequest { return Http::baseUrl($this->baseUrl) ->acceptJson() ->timeout($this->timeout) ->retry(2, 200); } }