postgresai 0.16.0-rc.4 → 0.16.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.
Files changed (85) hide show
  1. package/README.md +154 -0
  2. package/dist/bin/postgres-ai.js +2911 -255
  3. package/package.json +12 -3
  4. package/schemas/A002.schema.json +63 -0
  5. package/schemas/A003.schema.json +73 -0
  6. package/schemas/A004.schema.json +81 -0
  7. package/schemas/A007.schema.json +71 -0
  8. package/schemas/A013.schema.json +61 -0
  9. package/schemas/D001.schema.json +71 -0
  10. package/schemas/D004.schema.json +136 -0
  11. package/schemas/F001.schema.json +73 -0
  12. package/schemas/F002.schema.json +108 -0
  13. package/schemas/F003.schema.json +138 -0
  14. package/schemas/F004.schema.json +125 -0
  15. package/schemas/F005.schema.json +131 -0
  16. package/schemas/F009.schema.json +155 -0
  17. package/schemas/G001.schema.json +135 -0
  18. package/schemas/G003.schema.json +90 -0
  19. package/schemas/H001.schema.json +141 -0
  20. package/schemas/H002.schema.json +129 -0
  21. package/schemas/H004.schema.json +128 -0
  22. package/schemas/I001.schema.json +149 -0
  23. package/schemas/K001.schema.json +161 -0
  24. package/schemas/K003.schema.json +163 -0
  25. package/schemas/K004.schema.json +110 -0
  26. package/schemas/K005.schema.json +110 -0
  27. package/schemas/K006.schema.json +110 -0
  28. package/schemas/K007.schema.json +110 -0
  29. package/schemas/K008.schema.json +110 -0
  30. package/schemas/M001.schema.json +119 -0
  31. package/schemas/M002.schema.json +110 -0
  32. package/schemas/M003.schema.json +128 -0
  33. package/schemas/N001.schema.json +161 -0
  34. package/schemas/query.schema.json +62 -0
  35. package/CHANGELOG.md +0 -11
  36. package/bin/postgres-ai.ts +0 -5578
  37. package/bun.lock +0 -258
  38. package/bunfig.toml +0 -20
  39. package/lib/aas-onboard.ts +0 -251
  40. package/lib/auth-server.ts +0 -285
  41. package/lib/checkup-api.ts +0 -526
  42. package/lib/checkup-dictionary.ts +0 -103
  43. package/lib/checkup-summary.ts +0 -338
  44. package/lib/checkup.ts +0 -2261
  45. package/lib/config.ts +0 -171
  46. package/lib/init.ts +0 -1152
  47. package/lib/instances.ts +0 -245
  48. package/lib/issues.ts +0 -1060
  49. package/lib/mcp-server.ts +0 -667
  50. package/lib/metrics-loader.ts +0 -134
  51. package/lib/pkce.ts +0 -79
  52. package/lib/reports.ts +0 -373
  53. package/lib/storage.ts +0 -367
  54. package/lib/supabase.ts +0 -826
  55. package/lib/util.ts +0 -134
  56. package/packages/postgres-ai/README.md +0 -26
  57. package/packages/postgres-ai/bin/postgres-ai.js +0 -27
  58. package/packages/postgres-ai/package.json +0 -27
  59. package/scripts/embed-checkup-dictionary.ts +0 -115
  60. package/scripts/embed-metrics.ts +0 -160
  61. package/scripts/generate-release-notes.ts +0 -668
  62. package/test/PERMISSION_CHECK_TEST_SUMMARY.md +0 -139
  63. package/test/aas-onboard.test.ts +0 -301
  64. package/test/auth.test.ts +0 -287
  65. package/test/checkup.integration.test.ts +0 -413
  66. package/test/checkup.test.ts +0 -3626
  67. package/test/compose-cmd.test.ts +0 -120
  68. package/test/config-consistency.test.ts +0 -352
  69. package/test/init.integration.test.ts +0 -438
  70. package/test/init.test.ts +0 -1816
  71. package/test/issues.cli.test.ts +0 -1162
  72. package/test/issues.test.ts +0 -456
  73. package/test/mcp-server.test.ts +0 -2530
  74. package/test/monitoring.test.ts +0 -746
  75. package/test/permission-check-sql.test.ts +0 -116
  76. package/test/reports.cli.test.ts +0 -793
  77. package/test/reports.test.ts +0 -977
  78. package/test/schema-validation.test.ts +0 -231
  79. package/test/storage.test.ts +0 -935
  80. package/test/supabase.test.ts +0 -709
  81. package/test/targets-add-config.test.ts +0 -28
  82. package/test/test-utils.ts +0 -190
  83. package/test/upgrade.test.ts +0 -1056
  84. package/test/util.test.ts +0 -44
  85. package/tsconfig.json +0 -20
package/lib/issues.ts DELETED
@@ -1,1060 +0,0 @@
1
- import { formatHttpError, maskSecret, normalizeBaseUrl } from "./util";
2
-
3
- /**
4
- * Issue status constants.
5
- * Used in updateIssue to change issue state.
6
- */
7
- export const IssueStatus = {
8
- /** Issue is open and active */
9
- OPEN: 0,
10
- /** Issue is closed/resolved */
11
- CLOSED: 1,
12
- } as const;
13
-
14
- /**
15
- * Represents a PostgreSQL configuration parameter change recommendation.
16
- * Used in action items to suggest config tuning.
17
- */
18
- export interface ConfigChange {
19
- /** PostgreSQL configuration parameter name (e.g., 'work_mem', 'shared_buffers') */
20
- parameter: string;
21
- /** Recommended value for the parameter (e.g., '256MB', '4GB') */
22
- value: string;
23
- }
24
-
25
- export interface IssueActionItem {
26
- id: string;
27
- issue_id: string;
28
- title: string;
29
- description: string | null;
30
- severity: number;
31
- is_done: boolean;
32
- done_by: number | null;
33
- done_at: string | null;
34
- status: string;
35
- status_reason: string | null;
36
- status_changed_by: number | null;
37
- status_changed_at: string | null;
38
- sql_action: string | null;
39
- configs: ConfigChange[];
40
- created_at: string;
41
- updated_at: string;
42
- }
43
-
44
- /**
45
- * Summary of an action item (minimal fields for list views).
46
- * Used in issue detail responses to provide quick overview of action items.
47
- */
48
- export interface IssueActionItemSummary {
49
- /** Action item ID (UUID) */
50
- id: string;
51
- /** Action item title */
52
- title: string;
53
- }
54
-
55
- export interface Issue {
56
- id: string;
57
- title: string;
58
- description: string | null;
59
- created_at: string;
60
- updated_at: string;
61
- status: number;
62
- url_main: string | null;
63
- urls_extra: string[] | null;
64
- data: unknown | null;
65
- author_id: number;
66
- org_id: number;
67
- project_id: number | null;
68
- is_ai_generated: boolean;
69
- assigned_to: number[] | null;
70
- labels: string[] | null;
71
- is_edited: boolean;
72
- author_display_name: string;
73
- comment_count: number;
74
- action_items: IssueActionItem[];
75
- }
76
-
77
- export interface IssueComment {
78
- id: string;
79
- issue_id: string;
80
- author_id: number;
81
- parent_comment_id: string | null;
82
- content: string;
83
- created_at: string;
84
- updated_at: string;
85
- data: unknown | null;
86
- }
87
-
88
- export type IssueListItem = Pick<Issue, "id" | "title" | "status" | "created_at">;
89
-
90
- export type IssueDetail = Pick<Issue, "id" | "title" | "description" | "status" | "created_at" | "author_display_name"> & {
91
- action_items: IssueActionItemSummary[];
92
- };
93
- export interface FetchIssuesParams {
94
- apiKey: string;
95
- apiBaseUrl: string;
96
- orgId?: number;
97
- status?: "open" | "closed";
98
- limit?: number;
99
- offset?: number;
100
- debug?: boolean;
101
- }
102
-
103
- export async function fetchIssues(params: FetchIssuesParams): Promise<IssueListItem[]> {
104
- const { apiKey, apiBaseUrl, orgId, status, limit = 20, offset = 0, debug } = params;
105
- if (!apiKey) {
106
- throw new Error("API key is required");
107
- }
108
-
109
- const base = normalizeBaseUrl(apiBaseUrl);
110
- const url = new URL(`${base}/issues`);
111
- url.searchParams.set("select", "id,title,status,created_at");
112
- url.searchParams.set("order", "id.desc");
113
- url.searchParams.set("limit", String(limit));
114
- url.searchParams.set("offset", String(offset));
115
- if (typeof orgId === "number") {
116
- url.searchParams.set("org_id", `eq.${orgId}`);
117
- }
118
- if (status === "open") {
119
- url.searchParams.set("status", "eq.0");
120
- } else if (status === "closed") {
121
- url.searchParams.set("status", "eq.1");
122
- }
123
-
124
- const headers: Record<string, string> = {
125
- "access-token": apiKey,
126
- "Prefer": "return=representation",
127
- "Content-Type": "application/json",
128
- "Connection": "close",
129
- };
130
-
131
- if (debug) {
132
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
133
- console.error(`Debug: Resolved API base URL: ${base}`);
134
- console.error(`Debug: GET URL: ${url.toString()}`);
135
- console.error(`Debug: Auth scheme: access-token`);
136
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
137
- }
138
-
139
- const response = await fetch(url.toString(), {
140
- method: "GET",
141
- headers,
142
- });
143
-
144
- if (debug) {
145
- console.error(`Debug: Response status: ${response.status}`);
146
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
147
- }
148
-
149
- const data = await response.text();
150
-
151
- if (response.ok) {
152
- try {
153
- return JSON.parse(data) as IssueListItem[];
154
- } catch {
155
- throw new Error(`Failed to parse issues response: ${data}`);
156
- }
157
- } else {
158
- throw new Error(formatHttpError("Failed to fetch issues", response.status, data));
159
- }
160
- }
161
-
162
-
163
- export interface FetchIssueCommentsParams {
164
- apiKey: string;
165
- apiBaseUrl: string;
166
- issueId: string;
167
- debug?: boolean;
168
- }
169
-
170
- export async function fetchIssueComments(params: FetchIssueCommentsParams): Promise<IssueComment[]> {
171
- const { apiKey, apiBaseUrl, issueId, debug } = params;
172
- if (!apiKey) {
173
- throw new Error("API key is required");
174
- }
175
- if (!issueId) {
176
- throw new Error("issueId is required");
177
- }
178
-
179
- const base = normalizeBaseUrl(apiBaseUrl);
180
- const url = new URL(`${base}/issue_comments?issue_id=eq.${encodeURIComponent(issueId)}`);
181
-
182
- const headers: Record<string, string> = {
183
- "access-token": apiKey,
184
- "Prefer": "return=representation",
185
- "Content-Type": "application/json",
186
- "Connection": "close",
187
- };
188
-
189
- if (debug) {
190
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
191
- console.error(`Debug: Resolved API base URL: ${base}`);
192
- console.error(`Debug: GET URL: ${url.toString()}`);
193
- console.error(`Debug: Auth scheme: access-token`);
194
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
195
- }
196
-
197
- const response = await fetch(url.toString(), {
198
- method: "GET",
199
- headers,
200
- });
201
-
202
- if (debug) {
203
- console.error(`Debug: Response status: ${response.status}`);
204
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
205
- }
206
-
207
- const data = await response.text();
208
-
209
- if (response.ok) {
210
- try {
211
- return JSON.parse(data) as IssueComment[];
212
- } catch {
213
- throw new Error(`Failed to parse issue comments response: ${data}`);
214
- }
215
- } else {
216
- throw new Error(formatHttpError("Failed to fetch issue comments", response.status, data));
217
- }
218
- }
219
-
220
- export interface FetchIssueParams {
221
- apiKey: string;
222
- apiBaseUrl: string;
223
- issueId: string;
224
- debug?: boolean;
225
- }
226
-
227
- export async function fetchIssue(params: FetchIssueParams): Promise<IssueDetail | null> {
228
- const { apiKey, apiBaseUrl, issueId, debug } = params;
229
- if (!apiKey) {
230
- throw new Error("API key is required");
231
- }
232
- if (!issueId) {
233
- throw new Error("issueId is required");
234
- }
235
-
236
- const base = normalizeBaseUrl(apiBaseUrl);
237
- const url = new URL(`${base}/issues`);
238
- url.searchParams.set("select", "id,title,description,status,created_at,author_display_name,action_items");
239
- url.searchParams.set("id", `eq.${issueId}`);
240
- url.searchParams.set("limit", "1");
241
-
242
- const headers: Record<string, string> = {
243
- "access-token": apiKey,
244
- "Prefer": "return=representation",
245
- "Content-Type": "application/json",
246
- "Connection": "close",
247
- };
248
-
249
- if (debug) {
250
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
251
- console.error(`Debug: Resolved API base URL: ${base}`);
252
- console.error(`Debug: GET URL: ${url.toString()}`);
253
- console.error(`Debug: Auth scheme: access-token`);
254
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
255
- }
256
-
257
- const response = await fetch(url.toString(), {
258
- method: "GET",
259
- headers,
260
- });
261
-
262
- if (debug) {
263
- console.error(`Debug: Response status: ${response.status}`);
264
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
265
- }
266
-
267
- const data = await response.text();
268
-
269
- if (response.ok) {
270
- try {
271
- const parsed = JSON.parse(data);
272
- const rawIssue = Array.isArray(parsed) ? parsed[0] : parsed;
273
- if (!rawIssue) {
274
- return null;
275
- }
276
- // Map action_items to summary (id, title only)
277
- const actionItemsSummary: IssueActionItemSummary[] = Array.isArray(rawIssue.action_items)
278
- ? rawIssue.action_items.map((item: IssueActionItem) => ({ id: item.id, title: item.title }))
279
- : [];
280
- return {
281
- id: rawIssue.id,
282
- title: rawIssue.title,
283
- description: rawIssue.description,
284
- status: rawIssue.status,
285
- created_at: rawIssue.created_at,
286
- author_display_name: rawIssue.author_display_name,
287
- action_items: actionItemsSummary,
288
- } as IssueDetail;
289
- } catch {
290
- throw new Error(`Failed to parse issue response: ${data}`);
291
- }
292
- } else {
293
- throw new Error(formatHttpError("Failed to fetch issue", response.status, data));
294
- }
295
- }
296
-
297
- export interface CreateIssueParams {
298
- apiKey: string;
299
- apiBaseUrl: string;
300
- title: string;
301
- orgId: number;
302
- description?: string;
303
- projectId?: number;
304
- labels?: string[];
305
- debug?: boolean;
306
- }
307
-
308
- export interface CreatedIssue {
309
- id: string;
310
- title: string;
311
- description: string | null;
312
- created_at: string;
313
- status: number;
314
- project_id: number | null;
315
- labels: string[] | null;
316
- }
317
-
318
- /**
319
- * Create a new issue in the PostgresAI platform.
320
- *
321
- * @param params - The parameters for creating an issue
322
- * @param params.apiKey - API key for authentication
323
- * @param params.apiBaseUrl - Base URL for the API
324
- * @param params.title - Issue title (required)
325
- * @param params.orgId - Organization ID (required)
326
- * @param params.description - Optional issue description
327
- * @param params.projectId - Optional project ID to associate with
328
- * @param params.labels - Optional array of label strings
329
- * @param params.debug - Enable debug logging
330
- * @returns The created issue object
331
- * @throws Error if API key, title, or orgId is missing, or if the API call fails
332
- */
333
- export async function createIssue(params: CreateIssueParams): Promise<CreatedIssue> {
334
- const { apiKey, apiBaseUrl, title, orgId, description, projectId, labels, debug } = params;
335
- if (!apiKey) {
336
- throw new Error("API key is required");
337
- }
338
- if (!title) {
339
- throw new Error("title is required");
340
- }
341
- if (typeof orgId !== "number") {
342
- throw new Error("orgId is required");
343
- }
344
-
345
- const base = normalizeBaseUrl(apiBaseUrl);
346
- const url = new URL(`${base}/rpc/issue_create`);
347
-
348
- const bodyObj: Record<string, unknown> = {
349
- title: title,
350
- org_id: orgId,
351
- };
352
- if (description !== undefined) {
353
- bodyObj.description = description;
354
- }
355
- if (projectId !== undefined) {
356
- bodyObj.project_id = projectId;
357
- }
358
- if (labels && labels.length > 0) {
359
- bodyObj.labels = labels;
360
- }
361
- const body = JSON.stringify(bodyObj);
362
-
363
- const headers: Record<string, string> = {
364
- "access-token": apiKey,
365
- "Prefer": "return=representation",
366
- "Content-Type": "application/json",
367
- "Connection": "close",
368
- };
369
-
370
- if (debug) {
371
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
372
- console.error(`Debug: Resolved API base URL: ${base}`);
373
- console.error(`Debug: POST URL: ${url.toString()}`);
374
- console.error(`Debug: Auth scheme: access-token`);
375
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
376
- console.error(`Debug: Request body: ${body}`);
377
- }
378
-
379
- const response = await fetch(url.toString(), {
380
- method: "POST",
381
- headers,
382
- body,
383
- });
384
-
385
- if (debug) {
386
- console.error(`Debug: Response status: ${response.status}`);
387
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
388
- }
389
-
390
- const data = await response.text();
391
-
392
- if (response.ok) {
393
- try {
394
- return JSON.parse(data) as CreatedIssue;
395
- } catch {
396
- throw new Error(`Failed to parse create issue response: ${data}`);
397
- }
398
- } else {
399
- throw new Error(formatHttpError("Failed to create issue", response.status, data));
400
- }
401
- }
402
-
403
- export interface CreateIssueCommentParams {
404
- apiKey: string;
405
- apiBaseUrl: string;
406
- issueId: string;
407
- content: string;
408
- parentCommentId?: string;
409
- debug?: boolean;
410
- }
411
-
412
- export async function createIssueComment(params: CreateIssueCommentParams): Promise<IssueComment> {
413
- const { apiKey, apiBaseUrl, issueId, content, parentCommentId, debug } = params;
414
- if (!apiKey) {
415
- throw new Error("API key is required");
416
- }
417
- if (!issueId) {
418
- throw new Error("issueId is required");
419
- }
420
- if (!content) {
421
- throw new Error("content is required");
422
- }
423
-
424
- const base = normalizeBaseUrl(apiBaseUrl);
425
- const url = new URL(`${base}/rpc/issue_comment_create`);
426
-
427
- const bodyObj: Record<string, unknown> = {
428
- issue_id: issueId,
429
- content: content,
430
- };
431
- if (parentCommentId) {
432
- bodyObj.parent_comment_id = parentCommentId;
433
- }
434
- const body = JSON.stringify(bodyObj);
435
-
436
- const headers: Record<string, string> = {
437
- "access-token": apiKey,
438
- "Prefer": "return=representation",
439
- "Content-Type": "application/json",
440
- "Connection": "close",
441
- };
442
-
443
- if (debug) {
444
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
445
- console.error(`Debug: Resolved API base URL: ${base}`);
446
- console.error(`Debug: POST URL: ${url.toString()}`);
447
- console.error(`Debug: Auth scheme: access-token`);
448
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
449
- console.error(`Debug: Request body: ${body}`);
450
- }
451
-
452
- const response = await fetch(url.toString(), {
453
- method: "POST",
454
- headers,
455
- body,
456
- });
457
-
458
- if (debug) {
459
- console.error(`Debug: Response status: ${response.status}`);
460
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
461
- }
462
-
463
- const data = await response.text();
464
-
465
- if (response.ok) {
466
- try {
467
- return JSON.parse(data) as IssueComment;
468
- } catch {
469
- throw new Error(`Failed to parse create comment response: ${data}`);
470
- }
471
- } else {
472
- throw new Error(formatHttpError("Failed to create issue comment", response.status, data));
473
- }
474
- }
475
-
476
- export interface UpdateIssueParams {
477
- apiKey: string;
478
- apiBaseUrl: string;
479
- issueId: string;
480
- title?: string;
481
- description?: string;
482
- status?: number;
483
- labels?: string[];
484
- debug?: boolean;
485
- }
486
-
487
- export interface UpdatedIssue {
488
- id: string;
489
- title: string;
490
- description: string | null;
491
- status: number;
492
- updated_at: string;
493
- labels: string[] | null;
494
- }
495
-
496
- /**
497
- * Update an existing issue in the PostgresAI platform.
498
- *
499
- * @param params - The parameters for updating an issue
500
- * @param params.apiKey - API key for authentication
501
- * @param params.apiBaseUrl - Base URL for the API
502
- * @param params.issueId - ID of the issue to update (required)
503
- * @param params.title - New title (optional)
504
- * @param params.description - New description (optional)
505
- * @param params.status - New status: 0 = open, 1 = closed (optional)
506
- * @param params.labels - New labels array (optional, replaces existing)
507
- * @param params.debug - Enable debug logging
508
- * @returns The updated issue object
509
- * @throws Error if API key or issueId is missing, if no fields to update are provided, or if the API call fails
510
- */
511
- export async function updateIssue(params: UpdateIssueParams): Promise<UpdatedIssue> {
512
- const { apiKey, apiBaseUrl, issueId, title, description, status, labels, debug } = params;
513
- if (!apiKey) {
514
- throw new Error("API key is required");
515
- }
516
- if (!issueId) {
517
- throw new Error("issueId is required");
518
- }
519
- if (title === undefined && description === undefined && status === undefined && labels === undefined) {
520
- throw new Error("At least one field to update is required (title, description, status, or labels)");
521
- }
522
-
523
- const base = normalizeBaseUrl(apiBaseUrl);
524
- const url = new URL(`${base}/rpc/issue_update`);
525
-
526
- // Prod RPC expects p_* argument names (see OpenAPI at /api/general/).
527
- const bodyObj: Record<string, unknown> = {
528
- p_id: issueId,
529
- };
530
- if (title !== undefined) {
531
- bodyObj.p_title = title;
532
- }
533
- if (description !== undefined) {
534
- bodyObj.p_description = description;
535
- }
536
- if (status !== undefined) {
537
- bodyObj.p_status = status;
538
- }
539
- if (labels !== undefined) {
540
- bodyObj.p_labels = labels;
541
- }
542
- const body = JSON.stringify(bodyObj);
543
-
544
- const headers: Record<string, string> = {
545
- "access-token": apiKey,
546
- "Prefer": "return=representation",
547
- "Content-Type": "application/json",
548
- "Connection": "close",
549
- };
550
-
551
- if (debug) {
552
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
553
- console.error(`Debug: Resolved API base URL: ${base}`);
554
- console.error(`Debug: POST URL: ${url.toString()}`);
555
- console.error(`Debug: Auth scheme: access-token`);
556
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
557
- console.error(`Debug: Request body: ${body}`);
558
- }
559
-
560
- const response = await fetch(url.toString(), {
561
- method: "POST",
562
- headers,
563
- body,
564
- });
565
-
566
- if (debug) {
567
- console.error(`Debug: Response status: ${response.status}`);
568
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
569
- }
570
-
571
- const data = await response.text();
572
-
573
- if (response.ok) {
574
- try {
575
- return JSON.parse(data) as UpdatedIssue;
576
- } catch {
577
- throw new Error(`Failed to parse update issue response: ${data}`);
578
- }
579
- } else {
580
- throw new Error(formatHttpError("Failed to update issue", response.status, data));
581
- }
582
- }
583
-
584
- export interface UpdateIssueCommentParams {
585
- apiKey: string;
586
- apiBaseUrl: string;
587
- commentId: string;
588
- content: string;
589
- debug?: boolean;
590
- }
591
-
592
- export interface UpdatedIssueComment {
593
- id: string;
594
- issue_id: string;
595
- content: string;
596
- updated_at: string;
597
- }
598
-
599
- /**
600
- * Update an existing issue comment in the PostgresAI platform.
601
- *
602
- * @param params - The parameters for updating a comment
603
- * @param params.apiKey - API key for authentication
604
- * @param params.apiBaseUrl - Base URL for the API
605
- * @param params.commentId - ID of the comment to update (required)
606
- * @param params.content - New comment content (required)
607
- * @param params.debug - Enable debug logging
608
- * @returns The updated comment object
609
- * @throws Error if API key, commentId, or content is missing, or if the API call fails
610
- */
611
- export async function updateIssueComment(params: UpdateIssueCommentParams): Promise<UpdatedIssueComment> {
612
- const { apiKey, apiBaseUrl, commentId, content, debug } = params;
613
- if (!apiKey) {
614
- throw new Error("API key is required");
615
- }
616
- if (!commentId) {
617
- throw new Error("commentId is required");
618
- }
619
- if (!content) {
620
- throw new Error("content is required");
621
- }
622
-
623
- const base = normalizeBaseUrl(apiBaseUrl);
624
- const url = new URL(`${base}/rpc/issue_comment_update`);
625
-
626
- const bodyObj: Record<string, unknown> = {
627
- // Prod RPC expects p_* argument names (see OpenAPI at /api/general/).
628
- p_id: commentId,
629
- p_content: content,
630
- };
631
- const body = JSON.stringify(bodyObj);
632
-
633
- const headers: Record<string, string> = {
634
- "access-token": apiKey,
635
- "Prefer": "return=representation",
636
- "Content-Type": "application/json",
637
- "Connection": "close",
638
- };
639
-
640
- if (debug) {
641
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
642
- console.error(`Debug: Resolved API base URL: ${base}`);
643
- console.error(`Debug: POST URL: ${url.toString()}`);
644
- console.error(`Debug: Auth scheme: access-token`);
645
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
646
- console.error(`Debug: Request body: ${body}`);
647
- }
648
-
649
- const response = await fetch(url.toString(), {
650
- method: "POST",
651
- headers,
652
- body,
653
- });
654
-
655
- if (debug) {
656
- console.error(`Debug: Response status: ${response.status}`);
657
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
658
- }
659
-
660
- const data = await response.text();
661
-
662
- if (response.ok) {
663
- try {
664
- return JSON.parse(data) as UpdatedIssueComment;
665
- } catch {
666
- throw new Error(`Failed to parse update comment response: ${data}`);
667
- }
668
- } else {
669
- throw new Error(formatHttpError("Failed to update issue comment", response.status, data));
670
- }
671
- }
672
-
673
- // ============================================================================
674
- // Action Items API Functions
675
- // ============================================================================
676
-
677
- export interface FetchActionItemParams {
678
- apiKey: string;
679
- apiBaseUrl: string;
680
- actionItemIds: string | string[];
681
- debug?: boolean;
682
- }
683
-
684
- /**
685
- * Fetch action item(s) by ID(s).
686
- * Supports single ID or array of IDs.
687
- *
688
- * @param params - Fetch parameters
689
- * @param params.apiKey - API authentication key
690
- * @param params.apiBaseUrl - Base URL for the API
691
- * @param params.actionItemIds - Single action item ID or array of IDs (UUIDs)
692
- * @param params.debug - Enable debug logging
693
- * @returns Array of action items matching the provided IDs
694
- * @throws Error if API key is missing or no valid IDs provided
695
- *
696
- * @example
697
- * // Fetch single action item
698
- * const items = await fetchActionItem({ apiKey, apiBaseUrl, actionItemIds: "uuid-123" });
699
- *
700
- * @example
701
- * // Fetch multiple action items
702
- * const items = await fetchActionItem({ apiKey, apiBaseUrl, actionItemIds: ["uuid-1", "uuid-2"] });
703
- */
704
- export async function fetchActionItem(params: FetchActionItemParams): Promise<IssueActionItem[]> {
705
- const { apiKey, apiBaseUrl, actionItemIds, debug } = params;
706
- if (!apiKey) {
707
- throw new Error("API key is required");
708
- }
709
- // UUID format pattern for validation
710
- const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
711
- // Normalize to array, filter out null/undefined, trim, and validate UUID format
712
- const rawIds = Array.isArray(actionItemIds) ? actionItemIds : [actionItemIds];
713
- const validIds = rawIds
714
- .filter((id): id is string => id != null && typeof id === "string")
715
- .map(id => id.trim())
716
- .filter(id => id.length > 0 && uuidPattern.test(id));
717
- if (validIds.length === 0) {
718
- throw new Error("actionItemId is required and must be a valid UUID");
719
- }
720
-
721
- const base = normalizeBaseUrl(apiBaseUrl);
722
- const url = new URL(`${base}/issue_action_items`);
723
- if (validIds.length === 1) {
724
- url.searchParams.set("id", `eq.${validIds[0]}`);
725
- } else {
726
- // PostgREST IN syntax: id=in.(val1,val2,val3)
727
- url.searchParams.set("id", `in.(${validIds.join(",")})`)
728
- }
729
-
730
- const headers: Record<string, string> = {
731
- "access-token": apiKey,
732
- "Prefer": "return=representation",
733
- "Content-Type": "application/json",
734
- "Connection": "close",
735
- };
736
-
737
- if (debug) {
738
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
739
- console.error(`Debug: Resolved API base URL: ${base}`);
740
- console.error(`Debug: GET URL: ${url.toString()}`);
741
- console.error(`Debug: Auth scheme: access-token`);
742
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
743
- }
744
-
745
- const response = await fetch(url.toString(), {
746
- method: "GET",
747
- headers,
748
- });
749
-
750
- if (debug) {
751
- console.error(`Debug: Response status: ${response.status}`);
752
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
753
- }
754
-
755
- const data = await response.text();
756
-
757
- if (response.ok) {
758
- try {
759
- const parsed = JSON.parse(data);
760
- if (Array.isArray(parsed)) {
761
- return parsed as IssueActionItem[];
762
- }
763
- return parsed ? [parsed as IssueActionItem] : [];
764
- } catch {
765
- throw new Error(`Failed to parse action item response: ${data}`);
766
- }
767
- } else {
768
- throw new Error(formatHttpError("Failed to fetch action item", response.status, data));
769
- }
770
- }
771
-
772
- export interface FetchActionItemsParams {
773
- apiKey: string;
774
- apiBaseUrl: string;
775
- issueId: string;
776
- debug?: boolean;
777
- }
778
-
779
- /**
780
- * Fetch all action items for an issue.
781
- *
782
- * @param params - Fetch parameters
783
- * @param params.apiKey - API authentication key
784
- * @param params.apiBaseUrl - Base URL for the API
785
- * @param params.issueId - Issue ID (UUID) to fetch action items for
786
- * @param params.debug - Enable debug logging
787
- * @returns Array of action items for the specified issue
788
- * @throws Error if API key or issue ID is missing
789
- */
790
- export async function fetchActionItems(params: FetchActionItemsParams): Promise<IssueActionItem[]> {
791
- const { apiKey, apiBaseUrl, issueId, debug } = params;
792
- if (!apiKey) {
793
- throw new Error("API key is required");
794
- }
795
- if (!issueId) {
796
- throw new Error("issueId is required");
797
- }
798
- // Validate UUID format to prevent PostgREST injection
799
- const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
800
- if (!uuidPattern.test(issueId.trim())) {
801
- throw new Error("issueId must be a valid UUID");
802
- }
803
-
804
- const base = normalizeBaseUrl(apiBaseUrl);
805
- const url = new URL(`${base}/issue_action_items`);
806
- url.searchParams.set("issue_id", `eq.${issueId.trim()}`);
807
-
808
- const headers: Record<string, string> = {
809
- "access-token": apiKey,
810
- "Prefer": "return=representation",
811
- "Content-Type": "application/json",
812
- "Connection": "close",
813
- };
814
-
815
- if (debug) {
816
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
817
- console.error(`Debug: Resolved API base URL: ${base}`);
818
- console.error(`Debug: GET URL: ${url.toString()}`);
819
- console.error(`Debug: Auth scheme: access-token`);
820
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
821
- }
822
-
823
- const response = await fetch(url.toString(), {
824
- method: "GET",
825
- headers,
826
- });
827
-
828
- if (debug) {
829
- console.error(`Debug: Response status: ${response.status}`);
830
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
831
- }
832
-
833
- const data = await response.text();
834
-
835
- if (response.ok) {
836
- try {
837
- return JSON.parse(data) as IssueActionItem[];
838
- } catch {
839
- throw new Error(`Failed to parse action items response: ${data}`);
840
- }
841
- } else {
842
- throw new Error(formatHttpError("Failed to fetch action items", response.status, data));
843
- }
844
- }
845
-
846
- export interface CreateActionItemParams {
847
- apiKey: string;
848
- apiBaseUrl: string;
849
- issueId: string;
850
- title: string;
851
- description?: string;
852
- sqlAction?: string;
853
- configs?: ConfigChange[];
854
- debug?: boolean;
855
- }
856
-
857
- /**
858
- * Create a new action item for an issue.
859
- *
860
- * @param params - Creation parameters
861
- * @param params.apiKey - API authentication key
862
- * @param params.apiBaseUrl - Base URL for the API
863
- * @param params.issueId - Issue ID (UUID) to create action item for
864
- * @param params.title - Action item title
865
- * @param params.description - Optional detailed description
866
- * @param params.sqlAction - Optional SQL command to execute
867
- * @param params.configs - Optional configuration parameter changes
868
- * @param params.debug - Enable debug logging
869
- * @returns Created action item ID
870
- * @throws Error if required fields are missing or API call fails
871
- */
872
- export async function createActionItem(params: CreateActionItemParams): Promise<string> {
873
- const { apiKey, apiBaseUrl, issueId, title, description, sqlAction, configs, debug } = params;
874
- if (!apiKey) {
875
- throw new Error("API key is required");
876
- }
877
- if (!issueId) {
878
- throw new Error("issueId is required");
879
- }
880
- // Validate UUID format
881
- const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
882
- if (!uuidPattern.test(issueId.trim())) {
883
- throw new Error("issueId must be a valid UUID");
884
- }
885
- if (!title) {
886
- throw new Error("title is required");
887
- }
888
-
889
- const base = normalizeBaseUrl(apiBaseUrl);
890
- const url = new URL(`${base}/rpc/issue_action_item_create`);
891
-
892
- const bodyObj: Record<string, unknown> = {
893
- issue_id: issueId,
894
- title: title,
895
- };
896
- if (description !== undefined) {
897
- bodyObj.description = description;
898
- }
899
- if (sqlAction !== undefined) {
900
- bodyObj.sql_action = sqlAction;
901
- }
902
- if (configs !== undefined) {
903
- bodyObj.configs = configs;
904
- }
905
- const body = JSON.stringify(bodyObj);
906
-
907
- const headers: Record<string, string> = {
908
- "access-token": apiKey,
909
- "Prefer": "return=representation",
910
- "Content-Type": "application/json",
911
- "Connection": "close",
912
- };
913
-
914
- if (debug) {
915
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
916
- console.error(`Debug: Resolved API base URL: ${base}`);
917
- console.error(`Debug: POST URL: ${url.toString()}`);
918
- console.error(`Debug: Auth scheme: access-token`);
919
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
920
- console.error(`Debug: Request body: ${body}`);
921
- }
922
-
923
- const response = await fetch(url.toString(), {
924
- method: "POST",
925
- headers,
926
- body,
927
- });
928
-
929
- if (debug) {
930
- console.error(`Debug: Response status: ${response.status}`);
931
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
932
- }
933
-
934
- const data = await response.text();
935
-
936
- if (response.ok) {
937
- try {
938
- return JSON.parse(data) as string;
939
- } catch {
940
- throw new Error(`Failed to parse create action item response: ${data}`);
941
- }
942
- } else {
943
- throw new Error(formatHttpError("Failed to create action item", response.status, data));
944
- }
945
- }
946
-
947
- export interface UpdateActionItemParams {
948
- apiKey: string;
949
- apiBaseUrl: string;
950
- actionItemId: string;
951
- title?: string;
952
- description?: string;
953
- isDone?: boolean;
954
- status?: string;
955
- statusReason?: string;
956
- sqlAction?: string;
957
- configs?: ConfigChange[];
958
- debug?: boolean;
959
- }
960
-
961
- /**
962
- * Update an existing action item.
963
- *
964
- * @param params - Update parameters
965
- * @param params.apiKey - API authentication key
966
- * @param params.apiBaseUrl - Base URL for the API
967
- * @param params.actionItemId - Action item ID (UUID) to update
968
- * @param params.title - New title
969
- * @param params.description - New description
970
- * @param params.isDone - Mark as done/not done
971
- * @param params.status - Approval status: 'waiting_for_approval', 'approved', 'rejected'
972
- * @param params.statusReason - Reason for status change
973
- * @param params.sqlAction - SQL command to execute
974
- * @param params.configs - Configuration parameter changes
975
- * @param params.debug - Enable debug logging
976
- * @throws Error if required fields missing or no update fields provided
977
- */
978
- export async function updateActionItem(params: UpdateActionItemParams): Promise<void> {
979
- const { apiKey, apiBaseUrl, actionItemId, title, description, isDone, status, statusReason, sqlAction, configs, debug } = params;
980
- if (!apiKey) {
981
- throw new Error("API key is required");
982
- }
983
- if (!actionItemId) {
984
- throw new Error("actionItemId is required");
985
- }
986
- // Validate UUID format
987
- const uuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
988
- if (!uuidPattern.test(actionItemId.trim())) {
989
- throw new Error("actionItemId must be a valid UUID");
990
- }
991
-
992
- // Check that at least one update field is provided
993
- const hasUpdateField = title !== undefined || description !== undefined ||
994
- isDone !== undefined || status !== undefined ||
995
- statusReason !== undefined || sqlAction !== undefined || configs !== undefined;
996
- if (!hasUpdateField) {
997
- throw new Error("At least one field to update is required");
998
- }
999
-
1000
- const base = normalizeBaseUrl(apiBaseUrl);
1001
- const url = new URL(`${base}/rpc/issue_action_item_update`);
1002
-
1003
- const bodyObj: Record<string, unknown> = {
1004
- action_item_id: actionItemId,
1005
- };
1006
- if (title !== undefined) {
1007
- bodyObj.title = title;
1008
- }
1009
- if (description !== undefined) {
1010
- bodyObj.description = description;
1011
- }
1012
- if (isDone !== undefined) {
1013
- bodyObj.is_done = isDone;
1014
- }
1015
- if (status !== undefined) {
1016
- bodyObj.status = status;
1017
- }
1018
- if (statusReason !== undefined) {
1019
- bodyObj.status_reason = statusReason;
1020
- }
1021
- if (sqlAction !== undefined) {
1022
- bodyObj.sql_action = sqlAction;
1023
- }
1024
- if (configs !== undefined) {
1025
- bodyObj.configs = configs;
1026
- }
1027
- const body = JSON.stringify(bodyObj);
1028
-
1029
- const headers: Record<string, string> = {
1030
- "access-token": apiKey,
1031
- "Prefer": "return=representation",
1032
- "Content-Type": "application/json",
1033
- "Connection": "close",
1034
- };
1035
-
1036
- if (debug) {
1037
- const debugHeaders: Record<string, string> = { ...headers, "access-token": maskSecret(apiKey) };
1038
- console.error(`Debug: Resolved API base URL: ${base}`);
1039
- console.error(`Debug: POST URL: ${url.toString()}`);
1040
- console.error(`Debug: Auth scheme: access-token`);
1041
- console.error(`Debug: Request headers: ${JSON.stringify(debugHeaders)}`);
1042
- console.error(`Debug: Request body: ${body}`);
1043
- }
1044
-
1045
- const response = await fetch(url.toString(), {
1046
- method: "POST",
1047
- headers,
1048
- body,
1049
- });
1050
-
1051
- if (debug) {
1052
- console.error(`Debug: Response status: ${response.status}`);
1053
- console.error(`Debug: Response headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}`);
1054
- }
1055
-
1056
- if (!response.ok) {
1057
- const data = await response.text();
1058
- throw new Error(formatHttpError("Failed to update action item", response.status, data));
1059
- }
1060
- }