context_drop 0.1.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +330 -116
- data/Rakefile +110 -1
- data/SPEC.md +74 -26
- data/app/assets/javascripts/context_drop/context_drop.js +6329 -682
- data/app/controllers/context_drop/agent_controller.rb +170 -0
- data/app/controllers/context_drop/application_controller.rb +52 -3
- data/app/controllers/context_drop/configs_controller.rb +12 -2
- data/app/controllers/context_drop/dashboard/feedbacks_controller.rb +15 -11
- data/app/controllers/context_drop/feedbacks_controller.rb +396 -20
- data/app/controllers/context_drop/mcp_controller.rb +2 -1
- data/app/helpers/context_drop/widget_helper.rb +11 -1
- data/app/javascript/context_drop/__tests__/api-client.test.ts +255 -0
- data/app/javascript/context_drop/__tests__/widget.test.ts +1015 -89
- data/app/javascript/context_drop/api-client.ts +422 -19
- data/app/javascript/context_drop/element-picker.ts +186 -34
- data/app/javascript/context_drop/icons.ts +28 -0
- data/app/javascript/context_drop/markdown.ts +146 -0
- data/app/javascript/context_drop/sanitizer.ts +413 -2
- data/app/javascript/context_drop/screenshot.ts +103 -9
- data/app/javascript/context_drop/styles.ts +1705 -248
- data/app/javascript/context_drop/types.ts +243 -6
- data/app/javascript/context_drop/widget.ts +3318 -294
- data/app/models/context_drop/feedback.rb +88 -8
- data/app/services/context_drop/ownership_token.rb +114 -0
- data/app/views/context_drop/dashboard/feedbacks/index.html.erb +13 -0
- data/app/views/context_drop/dashboard/feedbacks/show.html.erb +19 -0
- data/app/views/layouts/context_drop/application.html.erb +6 -0
- data/config/routes.rb +22 -0
- data/db/migrate/20261112000004_add_environment_to_context_drop_feedbacks.rb +10 -0
- data/db/migrate/20261113000005_add_deleted_at_to_context_drop_feedbacks.rb +11 -0
- data/lib/context_drop/agent/key_store.rb +290 -0
- data/lib/context_drop/agent/llm_client.rb +413 -0
- data/lib/context_drop/agent/orchestrator.rb +314 -0
- data/lib/context_drop/agent/workspace_sandbox.rb +499 -0
- data/lib/context_drop/configuration.rb +242 -3
- data/lib/context_drop/mcp/dispatcher.rb +71 -29
- data/lib/context_drop/mcp/environment_router.rb +115 -0
- data/lib/context_drop/mcp/prompts/investigate_feedback.rb +90 -9
- data/lib/context_drop/mcp/remote_client.rb +120 -0
- data/lib/context_drop/mcp/server.rb +9 -2
- data/lib/context_drop/mcp/tools/get_feedback.rb +62 -5
- data/lib/context_drop/mcp/tools/get_feedback_attachment.rb +29 -4
- data/lib/context_drop/mcp/tools/list_environments.rb +60 -0
- data/lib/context_drop/mcp/tools/list_feedbacks.rb +195 -23
- data/lib/context_drop/mcp/tools/update_feedback_status.rb +48 -5
- data/lib/context_drop/version.rb +1 -1
- data/lib/context_drop.rb +8 -2
- data/lib/generators/context_drop/install/templates/initializer.rb.tt +29 -2
- metadata +14 -2
|
@@ -1,16 +1,88 @@
|
|
|
1
1
|
import type {
|
|
2
|
+
AgentChatRequestPayload,
|
|
3
|
+
AgentChatResponse,
|
|
4
|
+
AgentKeysUpdatePayload,
|
|
5
|
+
AgentKeysUpdateResponse,
|
|
6
|
+
AgentStatusResponse,
|
|
7
|
+
ContextDropApiClientOptions,
|
|
8
|
+
CrossEnvFeedbackListResponse,
|
|
9
|
+
FeedbackDeleteResponse,
|
|
2
10
|
FeedbackSubmissionPayload,
|
|
3
11
|
FeedbackSubmissionResponse,
|
|
12
|
+
FeedbackUpdatePayload,
|
|
13
|
+
FeedbackUpdateResponse,
|
|
14
|
+
OwnFeedbackListResponse,
|
|
15
|
+
ProposedFileEdit,
|
|
4
16
|
WidgetConfig
|
|
5
17
|
} from "./types.ts";
|
|
6
18
|
|
|
19
|
+
const OWNERSHIP_STORAGE_KEY = "context_drop_ownership_tokens";
|
|
20
|
+
const MAX_STORED_OWNERSHIP_TOKENS = 50;
|
|
21
|
+
|
|
22
|
+
function getBrowserLocalStorage(): Storage | null {
|
|
23
|
+
try {
|
|
24
|
+
if (typeof window !== "undefined" && "localStorage" in window) {
|
|
25
|
+
const store = window.localStorage;
|
|
26
|
+
if (store && typeof store.getItem === "function" && typeof store.setItem === "function") {
|
|
27
|
+
return store;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
} catch {
|
|
31
|
+
// Storage unavailable or restricted by browser privacy policy
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
|
|
7
36
|
export class ContextDropApiClient {
|
|
8
37
|
private baseUrl: string;
|
|
38
|
+
private credentials: RequestCredentials;
|
|
39
|
+
private customHeaders: Record<string, string>;
|
|
9
40
|
private cachedCsrfToken: string | null = null;
|
|
41
|
+
private configFetched: boolean = false;
|
|
10
42
|
private inFlightConfigPromise: Promise<WidgetConfig> | null = null;
|
|
43
|
+
private memoryOwnershipTokens: Map<string, string> = new Map();
|
|
11
44
|
|
|
12
|
-
constructor(baseUrl: string = "/context_drop") {
|
|
45
|
+
constructor(baseUrl: string = "/context_drop", options: ContextDropApiClientOptions = {}) {
|
|
13
46
|
this.baseUrl = baseUrl.replace(/\/+$/, "") || "/context_drop";
|
|
47
|
+
this.credentials = options.credentials || "same-origin";
|
|
48
|
+
this.customHeaders = options.headers ? { ...options.headers } : {};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
public getBaseUrl(): string {
|
|
52
|
+
return this.baseUrl;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
public setCredentials(credentials: RequestCredentials): void {
|
|
56
|
+
this.credentials = credentials;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
public setHeaders(headers: Record<string, string>): void {
|
|
60
|
+
this.customHeaders = { ...headers };
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
private isCrossOriginEndpoint(): boolean {
|
|
64
|
+
if (!/^https?:\/\//i.test(this.baseUrl)) {
|
|
65
|
+
return false;
|
|
66
|
+
}
|
|
67
|
+
if (typeof window === "undefined" || !window.location || !window.location.origin) {
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
return new URL(this.baseUrl, window.location.origin).origin !== window.location.origin;
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private mergeClientHeaders(base: Record<string, string>): Record<string, string> {
|
|
78
|
+
const merged: Record<string, string> = {
|
|
79
|
+
...this.customHeaders,
|
|
80
|
+
...base
|
|
81
|
+
};
|
|
82
|
+
if (this.isCrossOriginEndpoint()) {
|
|
83
|
+
merged["X-Context-Drop-Client"] = "1";
|
|
84
|
+
}
|
|
85
|
+
return merged;
|
|
14
86
|
}
|
|
15
87
|
|
|
16
88
|
public getCsrfToken(): string | null {
|
|
@@ -41,10 +113,10 @@ export class ContextDropApiClient {
|
|
|
41
113
|
try {
|
|
42
114
|
const response = await fetch(`${this.baseUrl}/config`, {
|
|
43
115
|
method: "GET",
|
|
44
|
-
credentials:
|
|
45
|
-
headers: {
|
|
116
|
+
credentials: this.credentials,
|
|
117
|
+
headers: this.mergeClientHeaders({
|
|
46
118
|
Accept: "application/json"
|
|
47
|
-
}
|
|
119
|
+
})
|
|
48
120
|
});
|
|
49
121
|
|
|
50
122
|
if (!response.ok) {
|
|
@@ -52,6 +124,7 @@ export class ContextDropApiClient {
|
|
|
52
124
|
}
|
|
53
125
|
|
|
54
126
|
const config = (await response.json()) as WidgetConfig;
|
|
127
|
+
this.configFetched = true;
|
|
55
128
|
if (config && typeof config.csrf_token === "string" && config.csrf_token.trim().length > 0) {
|
|
56
129
|
this.cachedCsrfToken = config.csrf_token.trim();
|
|
57
130
|
if (typeof document !== "undefined") {
|
|
@@ -79,6 +152,9 @@ export class ContextDropApiClient {
|
|
|
79
152
|
if (this.cachedCsrfToken) {
|
|
80
153
|
return this.cachedCsrfToken;
|
|
81
154
|
}
|
|
155
|
+
if (this.configFetched) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
82
158
|
}
|
|
83
159
|
|
|
84
160
|
try {
|
|
@@ -123,7 +199,8 @@ export class ContextDropApiClient {
|
|
|
123
199
|
|
|
124
200
|
const errorMessage = parsedError || rawText || "";
|
|
125
201
|
const shouldRetryCsrf =
|
|
126
|
-
this.isCsrfError(response.status, errorMessage) ||
|
|
202
|
+
this.isCsrfError(response.status, errorMessage) ||
|
|
203
|
+
(!csrfToken && !this.configFetched && response.status === 422);
|
|
127
204
|
|
|
128
205
|
if (shouldRetryCsrf) {
|
|
129
206
|
csrfToken = await this.resolveCsrfToken(true);
|
|
@@ -146,6 +223,17 @@ export class ContextDropApiClient {
|
|
|
146
223
|
);
|
|
147
224
|
}
|
|
148
225
|
|
|
226
|
+
private buildJsonHeaders(csrfToken: string | null): Record<string, string> {
|
|
227
|
+
const headers: Record<string, string> = {
|
|
228
|
+
"Content-Type": "application/json",
|
|
229
|
+
Accept: "application/json"
|
|
230
|
+
};
|
|
231
|
+
if (csrfToken) {
|
|
232
|
+
headers["X-CSRF-Token"] = csrfToken;
|
|
233
|
+
}
|
|
234
|
+
return this.mergeClientHeaders(headers);
|
|
235
|
+
}
|
|
236
|
+
|
|
149
237
|
public async uploadAttachment(file: Blob, filename: string): Promise<{ signed_blob_id: string }> {
|
|
150
238
|
const makeRequest = async (csrfToken: string | null) => {
|
|
151
239
|
const formData = new FormData();
|
|
@@ -160,8 +248,8 @@ export class ContextDropApiClient {
|
|
|
160
248
|
|
|
161
249
|
return fetch(`${this.baseUrl}/attachments`, {
|
|
162
250
|
method: "POST",
|
|
163
|
-
credentials:
|
|
164
|
-
headers,
|
|
251
|
+
credentials: this.credentials,
|
|
252
|
+
headers: this.mergeClientHeaders(headers),
|
|
165
253
|
body: formData
|
|
166
254
|
});
|
|
167
255
|
};
|
|
@@ -169,24 +257,339 @@ export class ContextDropApiClient {
|
|
|
169
257
|
return this.executeWithCsrfRetry(makeRequest, "Upload failed");
|
|
170
258
|
}
|
|
171
259
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
260
|
+
private ownershipScopedKey(id: number | string): string {
|
|
261
|
+
return `${this.baseUrl}::${String(id)}`;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
private readStoredTokenMap(): Record<string, string> {
|
|
265
|
+
const store = getBrowserLocalStorage();
|
|
266
|
+
if (!store) {
|
|
267
|
+
const out: Record<string, string> = {};
|
|
268
|
+
for (const [k, v] of this.memoryOwnershipTokens.entries()) {
|
|
269
|
+
out[k] = v;
|
|
180
270
|
}
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
181
273
|
|
|
182
|
-
|
|
274
|
+
try {
|
|
275
|
+
const raw = store.getItem(OWNERSHIP_STORAGE_KEY);
|
|
276
|
+
if (!raw) return {};
|
|
277
|
+
const parsed = JSON.parse(raw) as unknown;
|
|
278
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
279
|
+
return {};
|
|
280
|
+
}
|
|
281
|
+
const clean: Record<string, string> = {};
|
|
282
|
+
for (const [k, v] of Object.entries(parsed as Record<string, unknown>)) {
|
|
283
|
+
if (typeof v === "string" && v.trim().length > 0) {
|
|
284
|
+
clean[k] = v.trim();
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return clean;
|
|
288
|
+
} catch {
|
|
289
|
+
return {};
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
private writeStoredTokenMap(map: Record<string, string>): void {
|
|
294
|
+
const entries = Object.entries(map);
|
|
295
|
+
const trimmedEntries =
|
|
296
|
+
entries.length > MAX_STORED_OWNERSHIP_TOKENS
|
|
297
|
+
? entries.slice(entries.length - MAX_STORED_OWNERSHIP_TOKENS)
|
|
298
|
+
: entries;
|
|
299
|
+
|
|
300
|
+
this.memoryOwnershipTokens.clear();
|
|
301
|
+
const bounded: Record<string, string> = {};
|
|
302
|
+
for (const [k, v] of trimmedEntries) {
|
|
303
|
+
bounded[k] = v;
|
|
304
|
+
this.memoryOwnershipTokens.set(k, v);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const store = getBrowserLocalStorage();
|
|
308
|
+
if (store) {
|
|
309
|
+
try {
|
|
310
|
+
store.setItem(OWNERSHIP_STORAGE_KEY, JSON.stringify(bounded));
|
|
311
|
+
} catch {
|
|
312
|
+
// Non-fatal if storage quota exceeded
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
public getOwnershipToken(id: number | string): string | null {
|
|
318
|
+
const map = this.readStoredTokenMap();
|
|
319
|
+
const scoped = map[this.ownershipScopedKey(id)];
|
|
320
|
+
if (scoped) return scoped;
|
|
321
|
+
const plain = map[String(id)];
|
|
322
|
+
return plain || null;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
public setOwnershipToken(id: number | string, token: string): void {
|
|
326
|
+
const trimmed = typeof token === "string" ? token.trim() : "";
|
|
327
|
+
if (!trimmed) return;
|
|
328
|
+
const map = this.readStoredTokenMap();
|
|
329
|
+
const key = this.ownershipScopedKey(id);
|
|
330
|
+
delete map[key];
|
|
331
|
+
map[key] = trimmed;
|
|
332
|
+
this.writeStoredTokenMap(map);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
public removeOwnershipToken(id: number | string): void {
|
|
336
|
+
const map = this.readStoredTokenMap();
|
|
337
|
+
const scopedKey = this.ownershipScopedKey(id);
|
|
338
|
+
const plainKey = String(id);
|
|
339
|
+
if (!(scopedKey in map) && !(plainKey in map)) return;
|
|
340
|
+
delete map[scopedKey];
|
|
341
|
+
delete map[plainKey];
|
|
342
|
+
this.writeStoredTokenMap(map);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
public getAllOwnershipTokens(): string[] {
|
|
346
|
+
const map = this.readStoredTokenMap();
|
|
347
|
+
const prefix = `${this.baseUrl}::`;
|
|
348
|
+
const tokens: string[] = [];
|
|
349
|
+
for (const [k, v] of Object.entries(map)) {
|
|
350
|
+
if ((k.startsWith(prefix) || !k.includes("::")) && v && !tokens.includes(v)) {
|
|
351
|
+
tokens.push(v);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return tokens;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
public buildAttachmentPreviewUrl(
|
|
358
|
+
feedbackId: number | string,
|
|
359
|
+
attachmentId: number | string,
|
|
360
|
+
ownershipToken?: string | null
|
|
361
|
+
): string {
|
|
362
|
+
const token = ownershipToken || this.getOwnershipToken(feedbackId);
|
|
363
|
+
const base = `${this.baseUrl}/feedback/${encodeURIComponent(String(feedbackId))}/attachments/${encodeURIComponent(String(attachmentId))}`;
|
|
364
|
+
return token ? `${base}?ownership_token=${encodeURIComponent(token)}` : base;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
public async submitFeedback(payload: FeedbackSubmissionPayload): Promise<FeedbackSubmissionResponse> {
|
|
368
|
+
const makeRequest = async (csrfToken: string | null) =>
|
|
369
|
+
fetch(`${this.baseUrl}/feedback`, {
|
|
183
370
|
method: "POST",
|
|
184
|
-
credentials:
|
|
185
|
-
headers,
|
|
371
|
+
credentials: this.credentials,
|
|
372
|
+
headers: this.buildJsonHeaders(csrfToken),
|
|
186
373
|
body: JSON.stringify({ feedback: payload })
|
|
187
374
|
});
|
|
375
|
+
|
|
376
|
+
const response = await this.executeWithCsrfRetry<FeedbackSubmissionResponse>(
|
|
377
|
+
makeRequest,
|
|
378
|
+
"Submission failed"
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
const token = response?.ownership_token || response?.feedback?.ownership_token;
|
|
382
|
+
if (response?.id !== undefined && token) {
|
|
383
|
+
this.setOwnershipToken(response.id, token);
|
|
384
|
+
}
|
|
385
|
+
return response;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
public async fetchOwnFeedbacks(limit: number = 30): Promise<OwnFeedbackListResponse> {
|
|
389
|
+
const headers: Record<string, string> = {
|
|
390
|
+
Accept: "application/json"
|
|
188
391
|
};
|
|
392
|
+
const tokens = this.getAllOwnershipTokens();
|
|
393
|
+
if (tokens.length > 0) {
|
|
394
|
+
headers["X-Context-Drop-Ownership-Tokens"] = tokens.join(",");
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const qs = limit > 0 ? `?limit=${encodeURIComponent(String(limit))}` : "";
|
|
398
|
+
const response = await fetch(`${this.baseUrl}/feedback${qs}`, {
|
|
399
|
+
method: "GET",
|
|
400
|
+
credentials: this.credentials,
|
|
401
|
+
headers: this.mergeClientHeaders(headers)
|
|
402
|
+
});
|
|
403
|
+
|
|
404
|
+
if (!response.ok) {
|
|
405
|
+
const errBody = await response.json().catch(() => ({}));
|
|
406
|
+
throw new Error(
|
|
407
|
+
(errBody as { error?: string }).error || `Failed to load own feedbacks (${response.status})`
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
const parsed = (await response.json().catch(() => ({}))) as Partial<OwnFeedbackListResponse>;
|
|
412
|
+
const feedbacks = Array.isArray(parsed?.feedbacks) ? parsed.feedbacks : [];
|
|
413
|
+
for (const item of feedbacks) {
|
|
414
|
+
if (item && item.id !== undefined && typeof item.ownership_token === "string") {
|
|
415
|
+
this.setOwnershipToken(item.id, item.ownership_token);
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
return {
|
|
420
|
+
total_count: typeof parsed?.total_count === "number" ? parsed.total_count : feedbacks.length,
|
|
421
|
+
feedbacks
|
|
422
|
+
};
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
public async updateFeedback(
|
|
426
|
+
id: number | string,
|
|
427
|
+
payload: FeedbackUpdatePayload,
|
|
428
|
+
ownershipToken?: string | null
|
|
429
|
+
): Promise<FeedbackUpdateResponse> {
|
|
430
|
+
const resolvedToken = ownershipToken || this.getOwnershipToken(id);
|
|
431
|
+
const makeRequest = async (csrfToken: string | null) => {
|
|
432
|
+
const headers = this.buildJsonHeaders(csrfToken);
|
|
433
|
+
if (resolvedToken) {
|
|
434
|
+
headers["X-Context-Drop-Ownership-Token"] = resolvedToken;
|
|
435
|
+
}
|
|
436
|
+
const bodyPayload: Record<string, unknown> = { feedback: payload };
|
|
437
|
+
if (resolvedToken) {
|
|
438
|
+
bodyPayload.ownership_token = resolvedToken;
|
|
439
|
+
}
|
|
440
|
+
return fetch(`${this.baseUrl}/feedback/${encodeURIComponent(String(id))}`, {
|
|
441
|
+
method: "PATCH",
|
|
442
|
+
credentials: this.credentials,
|
|
443
|
+
headers,
|
|
444
|
+
body: JSON.stringify(bodyPayload)
|
|
445
|
+
});
|
|
446
|
+
};
|
|
447
|
+
|
|
448
|
+
const response = await this.executeWithCsrfRetry<FeedbackUpdateResponse>(
|
|
449
|
+
makeRequest,
|
|
450
|
+
"Update failed"
|
|
451
|
+
);
|
|
452
|
+
|
|
453
|
+
const nextToken = response?.ownership_token || response?.feedback?.ownership_token;
|
|
454
|
+
if (response?.id !== undefined && nextToken) {
|
|
455
|
+
this.setOwnershipToken(response.id, nextToken);
|
|
456
|
+
}
|
|
457
|
+
return response;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
public async deleteFeedback(
|
|
461
|
+
id: number | string,
|
|
462
|
+
ownershipToken?: string | null
|
|
463
|
+
): Promise<FeedbackDeleteResponse> {
|
|
464
|
+
const resolvedToken = ownershipToken || this.getOwnershipToken(id);
|
|
465
|
+
const makeRequest = async (csrfToken: string | null) => {
|
|
466
|
+
const headers = this.buildJsonHeaders(csrfToken);
|
|
467
|
+
if (resolvedToken) {
|
|
468
|
+
headers["X-Context-Drop-Ownership-Token"] = resolvedToken;
|
|
469
|
+
}
|
|
470
|
+
return fetch(`${this.baseUrl}/feedback/${encodeURIComponent(String(id))}`, {
|
|
471
|
+
method: "DELETE",
|
|
472
|
+
credentials: this.credentials,
|
|
473
|
+
headers
|
|
474
|
+
});
|
|
475
|
+
};
|
|
476
|
+
|
|
477
|
+
const response = await this.executeWithCsrfRetry<FeedbackDeleteResponse>(
|
|
478
|
+
makeRequest,
|
|
479
|
+
"Delete failed"
|
|
480
|
+
);
|
|
481
|
+
|
|
482
|
+
this.removeOwnershipToken(id);
|
|
483
|
+
return response;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
public async fetchAgentStatus(): Promise<AgentStatusResponse> {
|
|
487
|
+
const response = await fetch(`${this.baseUrl}/agent/status`, {
|
|
488
|
+
method: "GET",
|
|
489
|
+
credentials: this.credentials,
|
|
490
|
+
headers: this.mergeClientHeaders({
|
|
491
|
+
Accept: "application/json"
|
|
492
|
+
})
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
if (!response.ok) {
|
|
496
|
+
const errBody = await response.json().catch(() => ({}));
|
|
497
|
+
throw new Error(
|
|
498
|
+
(errBody as { error?: string }).error || `Failed to load agent status (${response.status})`
|
|
499
|
+
);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
return (await response.json()) as AgentStatusResponse;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
public async saveAgentKeys(payload: AgentKeysUpdatePayload): Promise<AgentKeysUpdateResponse> {
|
|
506
|
+
const makeRequest = async (csrfToken: string | null) =>
|
|
507
|
+
fetch(`${this.baseUrl}/agent/keys`, {
|
|
508
|
+
method: "POST",
|
|
509
|
+
credentials: this.credentials,
|
|
510
|
+
headers: this.buildJsonHeaders(csrfToken),
|
|
511
|
+
body: JSON.stringify(payload)
|
|
512
|
+
});
|
|
513
|
+
|
|
514
|
+
return this.executeWithCsrfRetry(makeRequest, "Failed to save BYOK settings");
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
public async deleteAgentKeys(provider: string = "all"): Promise<AgentKeysUpdateResponse> {
|
|
518
|
+
const makeRequest = async (csrfToken: string | null) =>
|
|
519
|
+
fetch(`${this.baseUrl}/agent/keys?provider=${encodeURIComponent(provider)}`, {
|
|
520
|
+
method: "DELETE",
|
|
521
|
+
credentials: this.credentials,
|
|
522
|
+
headers: this.buildJsonHeaders(csrfToken)
|
|
523
|
+
});
|
|
524
|
+
|
|
525
|
+
return this.executeWithCsrfRetry(makeRequest, "Failed to clear BYOK key");
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
public async fetchAgentFeedbacks(
|
|
529
|
+
environment: string = "all",
|
|
530
|
+
status: string = "new"
|
|
531
|
+
): Promise<CrossEnvFeedbackListResponse> {
|
|
532
|
+
const qs = `environment=${encodeURIComponent(environment)}&status=${encodeURIComponent(status)}`;
|
|
533
|
+
const response = await fetch(`${this.baseUrl}/agent/feedbacks?${qs}`, {
|
|
534
|
+
method: "GET",
|
|
535
|
+
credentials: this.credentials,
|
|
536
|
+
headers: this.mergeClientHeaders({
|
|
537
|
+
Accept: "application/json"
|
|
538
|
+
})
|
|
539
|
+
});
|
|
540
|
+
|
|
541
|
+
if (!response.ok) {
|
|
542
|
+
const errBody = await response.json().catch(() => ({}));
|
|
543
|
+
throw new Error(
|
|
544
|
+
(errBody as { error?: string }).error || `Failed to load feedbacks (${response.status})`
|
|
545
|
+
);
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
return (await response.json()) as CrossEnvFeedbackListResponse;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
public async sendAgentChat(payload: AgentChatRequestPayload): Promise<AgentChatResponse> {
|
|
552
|
+
const makeRequest = async (csrfToken: string | null) =>
|
|
553
|
+
fetch(`${this.baseUrl}/agent/chat`, {
|
|
554
|
+
method: "POST",
|
|
555
|
+
credentials: this.credentials,
|
|
556
|
+
headers: this.buildJsonHeaders(csrfToken),
|
|
557
|
+
body: JSON.stringify(payload)
|
|
558
|
+
});
|
|
559
|
+
|
|
560
|
+
return this.executeWithCsrfRetry(makeRequest, "Agent request failed");
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
public async applyEdit(editId: string): Promise<{ edit: ProposedFileEdit }> {
|
|
564
|
+
const makeRequest = async (csrfToken: string | null) =>
|
|
565
|
+
fetch(`${this.baseUrl}/agent/edits/${encodeURIComponent(editId)}/apply`, {
|
|
566
|
+
method: "POST",
|
|
567
|
+
credentials: this.credentials,
|
|
568
|
+
headers: this.buildJsonHeaders(csrfToken)
|
|
569
|
+
});
|
|
570
|
+
|
|
571
|
+
return this.executeWithCsrfRetry(makeRequest, "Failed to apply file edit");
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
public async rejectEdit(editId: string): Promise<{ edit: ProposedFileEdit }> {
|
|
575
|
+
const makeRequest = async (csrfToken: string | null) =>
|
|
576
|
+
fetch(`${this.baseUrl}/agent/edits/${encodeURIComponent(editId)}/reject`, {
|
|
577
|
+
method: "POST",
|
|
578
|
+
credentials: this.credentials,
|
|
579
|
+
headers: this.buildJsonHeaders(csrfToken)
|
|
580
|
+
});
|
|
581
|
+
|
|
582
|
+
return this.executeWithCsrfRetry(makeRequest, "Failed to reject file edit");
|
|
583
|
+
}
|
|
584
|
+
|
|
585
|
+
public async revertEdit(editId: string): Promise<{ edit: ProposedFileEdit }> {
|
|
586
|
+
const makeRequest = async (csrfToken: string | null) =>
|
|
587
|
+
fetch(`${this.baseUrl}/agent/edits/${encodeURIComponent(editId)}/revert`, {
|
|
588
|
+
method: "POST",
|
|
589
|
+
credentials: this.credentials,
|
|
590
|
+
headers: this.buildJsonHeaders(csrfToken)
|
|
591
|
+
});
|
|
189
592
|
|
|
190
|
-
return this.executeWithCsrfRetry(makeRequest, "
|
|
593
|
+
return this.executeWithCsrfRetry(makeRequest, "Failed to revert file edit");
|
|
191
594
|
}
|
|
192
595
|
}
|