| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140 |
- import axios from 'axios';
- const api = axios.create({
- baseURL: '/api',
- timeout: 60000,
- });
- const cache = new Map<string, { data: any; timestamp: number }>();
- const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
- async function fetchWithCache(url: string, force = false) {
- if (!force && cache.has(url)) {
- const cached = cache.get(url)!;
- if (Date.now() - cached.timestamp < CACHE_TTL) {
- return cached.data;
- }
- }
- const { data } = await api.get(url);
- cache.set(url, { data, timestamp: Date.now() });
- return data;
- }
- export const getCapabilities = async (limit = 100, offset = 0) => {
- return fetchWithCache(`/capability?limit=${limit}&offset=${offset}`);
- };
- export const getRequirements = async (limit = 100, offset = 0) => {
- return fetchWithCache(`/requirement?limit=${limit}&offset=${offset}`);
- };
- export const getTools = async (limit = 100, offset = 0) => {
- return fetchWithCache(`/tool?limit=${limit}&offset=${offset}`);
- };
- export const getStrategies = async (limit = 1000, offset = 0, status?: string) => {
- let url = `/strategy?limit=${limit}&offset=${offset}`;
- if (status) url += `&status=${encodeURIComponent(status)}`;
- return fetchWithCache(url);
- };
- export const getKnowledge = async (page = 1, pageSize = 100, filters: Record<string, string> = {}) => {
- const params = new URLSearchParams({ page: page.toString(), page_size: pageSize.toString() });
- for (const [key, val] of Object.entries(filters)) {
- if (val) params.append(key, val);
- }
- return fetchWithCache(`/knowledge?${params.toString()}`);
- };
- export const searchKnowledge = async (q: string, filters: Record<string, string> = {}) => {
- const params = new URLSearchParams({ q });
- for (const [key, val] of Object.entries(filters)) {
- if (val) params.append(key, val);
- }
- return fetchWithCache(`/knowledge/search?${params.toString()}`);
- };
- export const searchRequirements = async (q: string, top_k = 20) => {
- return fetchWithCache(`/requirement/search?q=${encodeURIComponent(q)}&top_k=${top_k}`);
- };
- export const searchCapabilities = async (q: string, top_k = 20) => {
- return fetchWithCache(`/capability/search?q=${encodeURIComponent(q)}&top_k=${top_k}`);
- };
- export const searchStrategies = async (q: string, top_k = 20) => {
- return fetchWithCache(`/strategy/search?q=${encodeURIComponent(q)}&top_k=${top_k}`);
- };
- export const searchTools = async (q: string, status?: string, top_k = 20) => {
- const params = new URLSearchParams({ q, top_k: top_k.toString() });
- if (status) params.append('status', status);
- return fetchWithCache(`/tool/search?${params.toString()}`);
- };
- export const getTags = async () => {
- return fetchWithCache(`/knowledge/meta/tags`);
- };
- export const getResource = async (resourceId: string) => {
- return fetchWithCache(`/resource/${resourceId}`);
- };
- export const getDashboardSnapshot = async (force = false) => {
- return fetchWithCache('/dashboard/snapshot', force);
- };
- export const batchGetResources = async (ids: string[]): Promise<Record<string, any>> => {
- if (ids.length === 0) return {};
- const resp = await fetch('/api/resource/batch', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ ids }),
- });
- if (!resp.ok) {
- throw new Error(`batchGetResources failed with status ${resp.status}`);
- }
- const data = await resp.json();
- return data.resources || {};
- };
- export const batchGetPosts = async (postIds: string[]): Promise<Record<string, any>> => {
- if (postIds.length === 0) return {};
- const resp = await fetch('/api/pattern/posts/batch', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({ post_ids: postIds }),
- });
- if (!resp.ok) {
- throw new Error(`batchGetPosts failed with status ${resp.status}`);
- }
- const data = await resp.json();
- const map: Record<string, any> = {};
- (data.posts || []).forEach((p: any) => {
- const key = p.post_id || p.id;
- if (key) map[key] = p;
- });
- return map;
- };
- // --- Static Asset Fetchers for Dashboard (Temporary until DB implementation) ---
- export const getCategoryTree = async () => {
- const resp = await fetch('/category_tree.json');
- if (!resp.ok) throw new Error('Failed to fetch category_tree.json');
- return resp.json();
- };
- export const getItemsetsAll = async () => {
- const resp = await fetch('/api/pattern/itemsets?execution_id=56');
- if (!resp.ok) throw new Error('Failed to fetch itemsets');
- return resp.json();
- };
- export const getRequirementsPlanB = async () => {
- const resp = await fetch(`/requirements_planb.json?t=${Date.now()}`);
- if (!resp.ok) throw new Error('Failed to fetch requirements_planb.json');
- return resp.json();
- };
- export default api;
|