api.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import axios from 'axios';
  2. const api = axios.create({
  3. baseURL: '/api',
  4. timeout: 60000,
  5. });
  6. const cache = new Map<string, { data: any; timestamp: number }>();
  7. const CACHE_TTL = 5 * 60 * 1000; // 5 minutes
  8. async function fetchWithCache(url: string, force = false) {
  9. if (!force && cache.has(url)) {
  10. const cached = cache.get(url)!;
  11. if (Date.now() - cached.timestamp < CACHE_TTL) {
  12. return cached.data;
  13. }
  14. }
  15. const { data } = await api.get(url);
  16. cache.set(url, { data, timestamp: Date.now() });
  17. return data;
  18. }
  19. export const getCapabilities = async (limit = 100, offset = 0) => {
  20. return fetchWithCache(`/capability?limit=${limit}&offset=${offset}`);
  21. };
  22. export const getRequirements = async (limit = 100, offset = 0) => {
  23. return fetchWithCache(`/requirement?limit=${limit}&offset=${offset}`);
  24. };
  25. export const getTools = async (limit = 100, offset = 0) => {
  26. return fetchWithCache(`/tool?limit=${limit}&offset=${offset}`);
  27. };
  28. export const getStrategies = async (limit = 1000, offset = 0, status?: string) => {
  29. let url = `/strategy?limit=${limit}&offset=${offset}`;
  30. if (status) url += `&status=${encodeURIComponent(status)}`;
  31. return fetchWithCache(url);
  32. };
  33. export const getKnowledge = async (page = 1, pageSize = 100, filters: Record<string, string> = {}) => {
  34. const params = new URLSearchParams({ page: page.toString(), page_size: pageSize.toString() });
  35. for (const [key, val] of Object.entries(filters)) {
  36. if (val) params.append(key, val);
  37. }
  38. return fetchWithCache(`/knowledge?${params.toString()}`);
  39. };
  40. export const searchKnowledge = async (q: string, filters: Record<string, string> = {}) => {
  41. const params = new URLSearchParams({ q });
  42. for (const [key, val] of Object.entries(filters)) {
  43. if (val) params.append(key, val);
  44. }
  45. return fetchWithCache(`/knowledge/search?${params.toString()}`);
  46. };
  47. export const searchRequirements = async (q: string, top_k = 20) => {
  48. return fetchWithCache(`/requirement/search?q=${encodeURIComponent(q)}&top_k=${top_k}`);
  49. };
  50. export const searchCapabilities = async (q: string, top_k = 20) => {
  51. return fetchWithCache(`/capability/search?q=${encodeURIComponent(q)}&top_k=${top_k}`);
  52. };
  53. export const searchStrategies = async (q: string, top_k = 20) => {
  54. return fetchWithCache(`/strategy/search?q=${encodeURIComponent(q)}&top_k=${top_k}`);
  55. };
  56. export const searchTools = async (q: string, status?: string, top_k = 20) => {
  57. const params = new URLSearchParams({ q, top_k: top_k.toString() });
  58. if (status) params.append('status', status);
  59. return fetchWithCache(`/tool/search?${params.toString()}`);
  60. };
  61. export const getTags = async () => {
  62. return fetchWithCache(`/knowledge/meta/tags`);
  63. };
  64. export const getResource = async (resourceId: string) => {
  65. return fetchWithCache(`/resource/${resourceId}`);
  66. };
  67. export const getDashboardSnapshot = async (force = false) => {
  68. return fetchWithCache('/dashboard/snapshot', force);
  69. };
  70. export const batchGetResources = async (ids: string[]): Promise<Record<string, any>> => {
  71. if (ids.length === 0) return {};
  72. const resp = await fetch('/api/resource/batch', {
  73. method: 'POST',
  74. headers: { 'Content-Type': 'application/json' },
  75. body: JSON.stringify({ ids }),
  76. });
  77. if (!resp.ok) {
  78. throw new Error(`batchGetResources failed with status ${resp.status}`);
  79. }
  80. const data = await resp.json();
  81. return data.resources || {};
  82. };
  83. export const batchGetPosts = async (postIds: string[]): Promise<Record<string, any>> => {
  84. if (postIds.length === 0) return {};
  85. const resp = await fetch('/api/pattern/posts/batch', {
  86. method: 'POST',
  87. headers: { 'Content-Type': 'application/json' },
  88. body: JSON.stringify({ post_ids: postIds }),
  89. });
  90. if (!resp.ok) {
  91. throw new Error(`batchGetPosts failed with status ${resp.status}`);
  92. }
  93. const data = await resp.json();
  94. const map: Record<string, any> = {};
  95. (data.posts || []).forEach((p: any) => {
  96. const key = p.post_id || p.id;
  97. if (key) map[key] = p;
  98. });
  99. return map;
  100. };
  101. // --- Static Asset Fetchers for Dashboard (Temporary until DB implementation) ---
  102. export const getCategoryTree = async () => {
  103. const resp = await fetch('/category_tree.json');
  104. if (!resp.ok) throw new Error('Failed to fetch category_tree.json');
  105. return resp.json();
  106. };
  107. export const getItemsetsAll = async () => {
  108. const resp = await fetch('/api/pattern/itemsets?execution_id=56');
  109. if (!resp.ok) throw new Error('Failed to fetch itemsets');
  110. return resp.json();
  111. };
  112. export const getRequirementsPlanB = async () => {
  113. const resp = await fetch(`/requirements_planb.json?t=${Date.now()}`);
  114. if (!resp.ok) throw new Error('Failed to fetch requirements_planb.json');
  115. return resp.json();
  116. };
  117. export default api;