File size: 10,722 Bytes
b5a1b3b 4d393e8 a46398e 4d393e8 17c354b 4d393e8 a46398e 4d393e8 a46398e 3fd3b7e a46398e e7e25da a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 7350a7f 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e b5a1b3b 4d393e8 a46398e 4d393e8 a46398e 4d393e8 fe9239d b5a1b3b 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 a46398e 4d393e8 | 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 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 | /**
* DiffWeave Studio API Client
* Talks directly to the FastAPI bridge, which delegates to DocWeave MCP Server.
* Supports Bearer token authentication and user sessions.
*/
export const CLOUD_BACKEND_URL = 'https://shak3008-diffweave.hf.space';
export function getApiBase() {
if (typeof window !== 'undefined') {
// If running on Vercel or any external static preview, route to HF Space backend
if (window.location.hostname.includes('vercel.app')) {
return `${CLOUD_BACKEND_URL}/api`;
}
}
return '/api';
}
export const API_BASE = getApiBase();
function getHeaders(custom = {}) {
const token = localStorage.getItem('diffweave_token');
const headers = { 'Content-Type': 'application/json', ...custom };
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
return headers;
}
export const api = {
// -------------------------------------------------------------------------
// Authentication & Session
// -------------------------------------------------------------------------
async signup(username, email, password) {
const res = await fetch(`${API_BASE}/auth/signup`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, email, password }),
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
if (data.access_token) {
localStorage.setItem('diffweave_token', data.access_token);
localStorage.setItem('diffweave_user', JSON.stringify(data));
}
return data;
},
async login(username, password) {
const res = await fetch(`${API_BASE}/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password }),
});
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
if (data.access_token) {
localStorage.setItem('diffweave_token', data.access_token);
localStorage.setItem('diffweave_user', JSON.stringify(data));
}
return data;
},
async demoLogin() {
const res = await fetch(`${API_BASE}/auth/demo-login`, { method: 'POST' });
if (!res.ok) throw new Error(await res.text());
const data = await res.json();
if (data.access_token) {
localStorage.setItem('diffweave_token', data.access_token);
localStorage.setItem('diffweave_user', JSON.stringify(data));
}
return data;
},
setApiKey(token) {
localStorage.setItem('diffweave_token', token);
localStorage.setItem('diffweave_user', JSON.stringify({ email: 'api-key-user', username: 'API Key User' }));
},
logout() {
localStorage.removeItem('diffweave_token');
localStorage.removeItem('diffweave_user');
},
async generateKey(name = 'CLI Personal Access Token', expiresInDays = 90) {
const res = await fetch(`${API_BASE}/auth/generate-key`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({ name, expires_in_days: expiresInDays }),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
},
async syncCLI(token, email, username) {
const res = await fetch(`${API_BASE}/auth/sync-cli`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({ access_token: token, email, username }),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
},
async getWhoami() {
try {
const res = await fetch(`${API_BASE}/auth/whoami`, { headers: getHeaders() });
if (res.ok) return await res.json();
return null;
} catch {
return null;
}
},
getCurrentUser() {
try {
const u = localStorage.getItem('diffweave_user');
return u ? JSON.parse(u) : null;
} catch {
return null;
}
},
// -------------------------------------------------------------------------
// Health & MCP
// -------------------------------------------------------------------------
async getHealth() {
const res = await fetch(`${API_BASE}/health`, { headers: getHeaders() });
return res.json();
},
async getMCPTools() {
const res = await fetch(`${API_BASE}/mcp/tools`, { headers: getHeaders() });
return res.json();
},
// -------------------------------------------------------------------------
// Workspaces
// -------------------------------------------------------------------------
async getWorkspaces() {
const res = await fetch(`${API_BASE}/workspaces`, { headers: getHeaders() });
return res.json();
},
async createWorkspace(name, description = '') {
const res = await fetch(`${API_BASE}/workspaces`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({ name, description }),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
},
async deleteWorkspace(workspaceId) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}`, {
method: 'DELETE',
headers: getHeaders(),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
},
async batchDeleteWorkspaces(workspaceIds = [], deleteAll = false) {
const res = await fetch(`${API_BASE}/workspaces/batch-delete`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({ workspace_ids: workspaceIds, delete_all: deleteAll }),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
},
async getWorkspaceStatus(workspaceId) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/status`, { headers: getHeaders() });
return res.json();
},
// -------------------------------------------------------------------------
// Documents
// -------------------------------------------------------------------------
async getDocuments(workspaceId) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/documents`, { headers: getHeaders() });
return res.json();
},
async uploadDocument(workspaceId, file) {
const formData = new FormData();
formData.append('file', file);
const token = localStorage.getItem('diffweave_token');
const headers = {};
if (token) headers['Authorization'] = `Bearer ${token}`;
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/upload`, {
method: 'POST',
headers,
body: formData,
});
if (!res.ok) throw new Error(await res.text());
return res.json();
},
// -------------------------------------------------------------------------
// Proposals & PRs
// -------------------------------------------------------------------------
async getProposals(workspaceId, status) {
const url = status
? `${API_BASE}/workspaces/${workspaceId}/proposals?status=${status}`
: `${API_BASE}/workspaces/${workspaceId}/proposals`;
const res = await fetch(url, { headers: getHeaders() });
return res.json();
},
async createProposal(workspaceId, payload) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/proposals`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify(payload),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
},
async reviewProposal(workspaceId, proposalId, decision, comments = '') {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/proposals/${proposalId}/review`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({ decision, comments }),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
},
async batchReview(workspaceId, decision, proposalIds = null, comments = '') {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/proposals/batch-review`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({ decision, proposal_ids: proposalIds, comments }),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
},
// -------------------------------------------------------------------------
// Semantic Diff
// -------------------------------------------------------------------------
async getSemanticDiff(workspaceId) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/diff`, { headers: getHeaders() });
return res.json();
},
// -------------------------------------------------------------------------
// Policy Rules
// -------------------------------------------------------------------------
async getRules(workspaceId) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/rules`, { headers: getHeaders() });
return res.json();
},
async createRule(workspaceId, name, operator, configuration) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/rules`, {
method: 'POST',
headers: getHeaders(),
body: JSON.stringify({ name, operator, configuration }),
});
if (!res.ok) throw new Error(await res.text());
return res.json();
},
async deleteRule(ruleId) {
const res = await fetch(`${API_BASE}/rules/${ruleId}`, {
method: 'DELETE',
headers: getHeaders(),
});
return res.json();
},
async validateProposals(workspaceId) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/validate`, { headers: getHeaders() });
return res.json();
},
// -------------------------------------------------------------------------
// Knowledge Register & Graph
// -------------------------------------------------------------------------
async getKnowledgeItems(workspaceId) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/knowledge`, { headers: getHeaders() });
return res.json();
},
async searchKnowledge(workspaceId, query) {
const res = await fetch(
`${API_BASE}/workspaces/${workspaceId}/knowledge/search?q=${encodeURIComponent(query)}`,
{ headers: getHeaders() }
);
return res.json();
},
async getKnowledgeGraph(workspaceId) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/graph`, { headers: getHeaders() });
return res.json();
},
// -------------------------------------------------------------------------
// Audit Trail & Metrics
// -------------------------------------------------------------------------
async getActivityFeed(workspaceId) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/activity`, { headers: getHeaders() });
return res.json();
},
async getWorkflows(workspaceId) {
const res = await fetch(`${API_BASE}/workspaces/${workspaceId}/workflows`, { headers: getHeaders() });
return res.json();
},
};
|