postgresai 0.14.0-dev.7 → 0.14.0-dev.71

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 (83) hide show
  1. package/README.md +161 -61
  2. package/bin/postgres-ai.ts +1982 -404
  3. package/bun.lock +258 -0
  4. package/bunfig.toml +20 -0
  5. package/dist/bin/postgres-ai.js +29395 -1576
  6. package/dist/sql/01.role.sql +16 -0
  7. package/dist/sql/02.permissions.sql +37 -0
  8. package/dist/sql/03.optional_rds.sql +6 -0
  9. package/dist/sql/04.optional_self_managed.sql +8 -0
  10. package/dist/sql/05.helpers.sql +439 -0
  11. package/dist/sql/sql/01.role.sql +16 -0
  12. package/dist/sql/sql/02.permissions.sql +37 -0
  13. package/dist/sql/sql/03.optional_rds.sql +6 -0
  14. package/dist/sql/sql/04.optional_self_managed.sql +8 -0
  15. package/dist/sql/sql/05.helpers.sql +439 -0
  16. package/lib/auth-server.ts +124 -106
  17. package/lib/checkup-api.ts +386 -0
  18. package/lib/checkup.ts +1396 -0
  19. package/lib/config.ts +6 -3
  20. package/lib/init.ts +568 -155
  21. package/lib/issues.ts +400 -191
  22. package/lib/mcp-server.ts +213 -90
  23. package/lib/metrics-embedded.ts +79 -0
  24. package/lib/metrics-loader.ts +127 -0
  25. package/lib/supabase.ts +769 -0
  26. package/lib/util.ts +61 -0
  27. package/package.json +20 -10
  28. package/packages/postgres-ai/README.md +26 -0
  29. package/packages/postgres-ai/bin/postgres-ai.js +27 -0
  30. package/packages/postgres-ai/package.json +27 -0
  31. package/scripts/embed-metrics.ts +154 -0
  32. package/sql/01.role.sql +16 -0
  33. package/sql/02.permissions.sql +37 -0
  34. package/sql/03.optional_rds.sql +6 -0
  35. package/sql/04.optional_self_managed.sql +8 -0
  36. package/sql/05.helpers.sql +439 -0
  37. package/test/auth.test.ts +258 -0
  38. package/test/checkup.integration.test.ts +321 -0
  39. package/test/checkup.test.ts +1117 -0
  40. package/test/config-consistency.test.ts +36 -0
  41. package/test/init.integration.test.ts +500 -0
  42. package/test/init.test.ts +682 -0
  43. package/test/issues.cli.test.ts +314 -0
  44. package/test/issues.test.ts +456 -0
  45. package/test/mcp-server.test.ts +988 -0
  46. package/test/schema-validation.test.ts +81 -0
  47. package/test/supabase.test.ts +568 -0
  48. package/test/test-utils.ts +128 -0
  49. package/tsconfig.json +12 -20
  50. package/dist/bin/postgres-ai.d.ts +0 -3
  51. package/dist/bin/postgres-ai.d.ts.map +0 -1
  52. package/dist/bin/postgres-ai.js.map +0 -1
  53. package/dist/lib/auth-server.d.ts +0 -31
  54. package/dist/lib/auth-server.d.ts.map +0 -1
  55. package/dist/lib/auth-server.js +0 -263
  56. package/dist/lib/auth-server.js.map +0 -1
  57. package/dist/lib/config.d.ts +0 -45
  58. package/dist/lib/config.d.ts.map +0 -1
  59. package/dist/lib/config.js +0 -181
  60. package/dist/lib/config.js.map +0 -1
  61. package/dist/lib/init.d.ts +0 -61
  62. package/dist/lib/init.d.ts.map +0 -1
  63. package/dist/lib/init.js +0 -359
  64. package/dist/lib/init.js.map +0 -1
  65. package/dist/lib/issues.d.ts +0 -75
  66. package/dist/lib/issues.d.ts.map +0 -1
  67. package/dist/lib/issues.js +0 -336
  68. package/dist/lib/issues.js.map +0 -1
  69. package/dist/lib/mcp-server.d.ts +0 -9
  70. package/dist/lib/mcp-server.d.ts.map +0 -1
  71. package/dist/lib/mcp-server.js +0 -168
  72. package/dist/lib/mcp-server.js.map +0 -1
  73. package/dist/lib/pkce.d.ts +0 -32
  74. package/dist/lib/pkce.d.ts.map +0 -1
  75. package/dist/lib/pkce.js +0 -101
  76. package/dist/lib/pkce.js.map +0 -1
  77. package/dist/lib/util.d.ts +0 -27
  78. package/dist/lib/util.d.ts.map +0 -1
  79. package/dist/lib/util.js +0 -46
  80. package/dist/lib/util.js.map +0 -1
  81. package/dist/package.json +0 -46
  82. package/test/init.integration.test.cjs +0 -269
  83. package/test/init.test.cjs +0 -69
@@ -0,0 +1,386 @@
1
+ import * as https from "https";
2
+ import { URL } from "url";
3
+ import { normalizeBaseUrl } from "./util";
4
+
5
+ /**
6
+ * Retry configuration for network operations
7
+ */
8
+ export interface RetryConfig {
9
+ maxAttempts: number;
10
+ initialDelayMs: number;
11
+ maxDelayMs: number;
12
+ backoffMultiplier: number;
13
+ }
14
+
15
+ const DEFAULT_RETRY_CONFIG: RetryConfig = {
16
+ maxAttempts: 3,
17
+ initialDelayMs: 1000,
18
+ maxDelayMs: 10000,
19
+ backoffMultiplier: 2,
20
+ };
21
+
22
+ /**
23
+ * Check if an error is retryable (network errors, timeouts, 5xx errors)
24
+ */
25
+ function isRetryableError(err: unknown): boolean {
26
+ if (err instanceof RpcError) {
27
+ // Retry on server errors (5xx), not on client errors (4xx)
28
+ return err.statusCode >= 500 && err.statusCode < 600;
29
+ }
30
+
31
+ // Check for Node.js error codes (works on Error and Error-like objects)
32
+ if (typeof err === "object" && err !== null && "code" in err) {
33
+ const code = String((err as { code: unknown }).code);
34
+ if (["ECONNRESET", "ECONNREFUSED", "ENOTFOUND", "ETIMEDOUT"].includes(code)) {
35
+ return true;
36
+ }
37
+ }
38
+
39
+ if (err instanceof Error) {
40
+ const msg = err.message.toLowerCase();
41
+ // Retry on network-related errors based on message content
42
+ return (
43
+ msg.includes("timeout") ||
44
+ msg.includes("timed out") ||
45
+ msg.includes("econnreset") ||
46
+ msg.includes("econnrefused") ||
47
+ msg.includes("enotfound") ||
48
+ msg.includes("socket hang up") ||
49
+ msg.includes("network")
50
+ );
51
+ }
52
+
53
+ return false;
54
+ }
55
+
56
+ /**
57
+ * Execute an async function with exponential backoff retry.
58
+ * Retries on network errors, timeouts, and 5xx server errors.
59
+ * Does not retry on 4xx client errors.
60
+ *
61
+ * @param fn - Async function to execute
62
+ * @param config - Optional retry configuration (uses defaults if not provided)
63
+ * @param onRetry - Optional callback invoked before each retry attempt
64
+ * @returns Promise resolving to the function result
65
+ * @throws The last error if all retry attempts fail or error is non-retryable
66
+ *
67
+ * @example
68
+ * ```typescript
69
+ * const result = await withRetry(
70
+ * () => fetchData(),
71
+ * { maxAttempts: 3 },
72
+ * (attempt, err, delay) => console.log(`Retry ${attempt}, waiting ${delay}ms`)
73
+ * );
74
+ * ```
75
+ */
76
+ export async function withRetry<T>(
77
+ fn: () => Promise<T>,
78
+ config: Partial<RetryConfig> = {},
79
+ onRetry?: (attempt: number, error: unknown, delayMs: number) => void
80
+ ): Promise<T> {
81
+ const { maxAttempts, initialDelayMs, maxDelayMs, backoffMultiplier } = {
82
+ ...DEFAULT_RETRY_CONFIG,
83
+ ...config,
84
+ };
85
+
86
+ let lastError: unknown;
87
+ let delayMs = initialDelayMs;
88
+
89
+ for (let attempt = 1; attempt <= maxAttempts; attempt++) {
90
+ try {
91
+ return await fn();
92
+ } catch (err) {
93
+ lastError = err;
94
+
95
+ if (attempt === maxAttempts || !isRetryableError(err)) {
96
+ throw err;
97
+ }
98
+
99
+ if (onRetry) {
100
+ onRetry(attempt, err, delayMs);
101
+ }
102
+
103
+ await new Promise((resolve) => setTimeout(resolve, delayMs));
104
+ delayMs = Math.min(delayMs * backoffMultiplier, maxDelayMs);
105
+ }
106
+ }
107
+
108
+ throw lastError;
109
+ }
110
+
111
+ /**
112
+ * Error thrown when an RPC call to the PostgresAI API fails.
113
+ * Contains detailed information about the failure for debugging and display.
114
+ */
115
+ export class RpcError extends Error {
116
+ /** Name of the RPC endpoint that failed */
117
+ rpcName: string;
118
+ /** HTTP status code returned by the server */
119
+ statusCode: number;
120
+ /** Raw response body text */
121
+ payloadText: string;
122
+ /** Parsed JSON response body, or null if parsing failed */
123
+ payloadJson: any | null;
124
+
125
+ constructor(params: { rpcName: string; statusCode: number; payloadText: string; payloadJson: any | null }) {
126
+ const { rpcName, statusCode, payloadText, payloadJson } = params;
127
+ super(`RPC ${rpcName} failed: HTTP ${statusCode}`);
128
+ this.name = "RpcError";
129
+ this.rpcName = rpcName;
130
+ this.statusCode = statusCode;
131
+ this.payloadText = payloadText;
132
+ this.payloadJson = payloadJson;
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Format an RpcError for human-readable console display.
138
+ * Extracts message, details, and hint from the error payload if available.
139
+ *
140
+ * @param err - The RpcError to format
141
+ * @returns Array of lines suitable for console output
142
+ */
143
+ export function formatRpcErrorForDisplay(err: RpcError): string[] {
144
+ const lines: string[] = [];
145
+ lines.push(`Error: RPC ${err.rpcName} failed: HTTP ${err.statusCode}`);
146
+
147
+ const obj = err.payloadJson && typeof err.payloadJson === "object" ? err.payloadJson : null;
148
+ const details = obj && typeof (obj as any).details === "string" ? (obj as any).details : "";
149
+ const hint = obj && typeof (obj as any).hint === "string" ? (obj as any).hint : "";
150
+ const message = obj && typeof (obj as any).message === "string" ? (obj as any).message : "";
151
+
152
+ if (message) lines.push(`Message: ${message}`);
153
+ if (details) lines.push(`Details: ${details}`);
154
+ if (hint) lines.push(`Hint: ${hint}`);
155
+
156
+ // Fallback to raw payload if we couldn't extract anything useful.
157
+ if (!message && !details && !hint) {
158
+ const t = (err.payloadText || "").trim();
159
+ if (t) lines.push(t);
160
+ }
161
+ return lines;
162
+ }
163
+
164
+ function unwrapRpcResponse(parsed: unknown): any {
165
+ // Some deployments return a plain object, others return an array of rows,
166
+ // and some wrap OUT params under a "result" key.
167
+ if (Array.isArray(parsed)) {
168
+ if (parsed.length === 1) return unwrapRpcResponse(parsed[0]);
169
+ return parsed;
170
+ }
171
+ if (parsed && typeof parsed === "object") {
172
+ const obj = parsed as any;
173
+ if (obj.result !== undefined) return obj.result;
174
+ }
175
+ return parsed as any;
176
+ }
177
+
178
+ // Default timeout for HTTP requests (30 seconds)
179
+ const HTTP_TIMEOUT_MS = 30_000;
180
+
181
+ async function postRpc<T>(params: {
182
+ apiKey: string;
183
+ apiBaseUrl: string;
184
+ rpcName: string;
185
+ bodyObj: Record<string, unknown>;
186
+ timeoutMs?: number;
187
+ }): Promise<T> {
188
+ const { apiKey, apiBaseUrl, rpcName, bodyObj, timeoutMs = HTTP_TIMEOUT_MS } = params;
189
+ if (!apiKey) throw new Error("API key is required");
190
+ const base = normalizeBaseUrl(apiBaseUrl);
191
+ const url = new URL(`${base}/rpc/${rpcName}`);
192
+ const body = JSON.stringify(bodyObj);
193
+
194
+ const headers: Record<string, string> = {
195
+ // API key is sent in BOTH header and body (see bodyObj.access_token):
196
+ // - Header: Used by the API gateway/proxy for HTTP authentication
197
+ // - Body: Passed to PostgreSQL RPC function for in-database authorization
198
+ // This is intentional for defense-in-depth; backend validates both.
199
+ "access-token": apiKey,
200
+ "Prefer": "return=representation",
201
+ "Content-Type": "application/json",
202
+ "Content-Length": Buffer.byteLength(body).toString(),
203
+ };
204
+
205
+ // Use AbortController for clean timeout handling
206
+ const controller = new AbortController();
207
+ let timeoutId: ReturnType<typeof setTimeout> | null = null;
208
+ let settled = false;
209
+
210
+ return new Promise((resolve, reject) => {
211
+ const settledReject = (err: Error) => {
212
+ if (settled) return;
213
+ settled = true;
214
+ if (timeoutId) clearTimeout(timeoutId);
215
+ reject(err);
216
+ };
217
+
218
+ const settledResolve = (value: T) => {
219
+ if (settled) return;
220
+ settled = true;
221
+ if (timeoutId) clearTimeout(timeoutId);
222
+ resolve(value);
223
+ };
224
+
225
+ const req = https.request(
226
+ url,
227
+ {
228
+ method: "POST",
229
+ headers,
230
+ signal: controller.signal,
231
+ },
232
+ (res) => {
233
+ // Response started (headers received) - clear the connection timeout.
234
+ // Once the server starts responding, we let it complete rather than
235
+ // timing out mid-response which would cause confusing errors.
236
+ if (timeoutId) {
237
+ clearTimeout(timeoutId);
238
+ timeoutId = null;
239
+ }
240
+ let data = "";
241
+ res.on("data", (chunk) => (data += chunk));
242
+ res.on("end", () => {
243
+ if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
244
+ try {
245
+ const parsed = JSON.parse(data);
246
+ settledResolve(unwrapRpcResponse(parsed) as T);
247
+ } catch {
248
+ settledReject(new Error(`Failed to parse RPC response: ${data}`));
249
+ }
250
+ } else {
251
+ const statusCode = res.statusCode || 0;
252
+ let payloadJson: any | null = null;
253
+ if (data) {
254
+ try {
255
+ payloadJson = JSON.parse(data);
256
+ } catch {
257
+ payloadJson = null;
258
+ }
259
+ }
260
+ settledReject(new RpcError({ rpcName, statusCode, payloadText: data, payloadJson }));
261
+ }
262
+ });
263
+ res.on("error", (err) => {
264
+ settledReject(err);
265
+ });
266
+ }
267
+ );
268
+
269
+ // Set up connection timeout - applies until response headers are received.
270
+ // Once response starts, timeout is cleared (see response callback above).
271
+ timeoutId = setTimeout(() => {
272
+ controller.abort();
273
+ req.destroy(); // Backup: ensure request is terminated
274
+ settledReject(new Error(`RPC ${rpcName} timed out after ${timeoutMs}ms (no response)`));
275
+ }, timeoutMs);
276
+
277
+ req.on("error", (err: Error) => {
278
+ // Handle abort as timeout (may already be rejected by timeout handler)
279
+ if (err.name === "AbortError" || (err as any).code === "ABORT_ERR") {
280
+ settledReject(new Error(`RPC ${rpcName} timed out after ${timeoutMs}ms`));
281
+ return;
282
+ }
283
+ // Provide clearer error for common network issues
284
+ if ((err as any).code === "ECONNREFUSED") {
285
+ settledReject(new Error(`RPC ${rpcName} failed: connection refused to ${url.host}`));
286
+ } else if ((err as any).code === "ENOTFOUND") {
287
+ settledReject(new Error(`RPC ${rpcName} failed: DNS lookup failed for ${url.host}`));
288
+ } else if ((err as any).code === "ECONNRESET") {
289
+ settledReject(new Error(`RPC ${rpcName} failed: connection reset by server`));
290
+ } else {
291
+ settledReject(err);
292
+ }
293
+ });
294
+
295
+ req.write(body);
296
+ req.end();
297
+ });
298
+ }
299
+
300
+ /**
301
+ * Create a new checkup report in the PostgresAI backend.
302
+ * This creates the parent report container; individual check results
303
+ * are uploaded separately via uploadCheckupReportJson().
304
+ *
305
+ * @param params - Configuration for report creation
306
+ * @param params.apiKey - PostgresAI API access token
307
+ * @param params.apiBaseUrl - Base URL of the PostgresAI API
308
+ * @param params.project - Project name or ID to associate the report with
309
+ * @param params.status - Optional initial status for the report
310
+ * @returns Promise resolving to the created report ID
311
+ * @throws {RpcError} On API failures (4xx/5xx responses)
312
+ * @throws {Error} On network errors or unexpected response format
313
+ */
314
+ export async function createCheckupReport(params: {
315
+ apiKey: string;
316
+ apiBaseUrl: string;
317
+ project: string;
318
+ status?: string;
319
+ }): Promise<{ reportId: number }> {
320
+ const { apiKey, apiBaseUrl, project, status } = params;
321
+ const bodyObj: Record<string, unknown> = {
322
+ access_token: apiKey,
323
+ project,
324
+ };
325
+ if (status) bodyObj.status = status;
326
+
327
+ const resp = await postRpc<any>({
328
+ apiKey,
329
+ apiBaseUrl,
330
+ rpcName: "checkup_report_create",
331
+ bodyObj,
332
+ });
333
+ const reportId = Number(resp?.report_id);
334
+ if (!Number.isFinite(reportId) || reportId <= 0) {
335
+ throw new Error(`Unexpected checkup_report_create response: ${JSON.stringify(resp)}`);
336
+ }
337
+ return { reportId };
338
+ }
339
+
340
+ /**
341
+ * Upload a JSON check result to an existing checkup report.
342
+ * Each check (e.g., H001, A003) is uploaded as a separate JSON file.
343
+ *
344
+ * @param params - Configuration for the upload
345
+ * @param params.apiKey - PostgresAI API access token
346
+ * @param params.apiBaseUrl - Base URL of the PostgresAI API
347
+ * @param params.reportId - ID of the parent report (from createCheckupReport)
348
+ * @param params.filename - Filename for the uploaded JSON (e.g., "H001.json")
349
+ * @param params.checkId - Check identifier (e.g., "H001", "A003")
350
+ * @param params.jsonText - JSON content as a string
351
+ * @returns Promise resolving to the created report chunk ID
352
+ * @throws {RpcError} On API failures (4xx/5xx responses)
353
+ * @throws {Error} On network errors or unexpected response format
354
+ */
355
+ export async function uploadCheckupReportJson(params: {
356
+ apiKey: string;
357
+ apiBaseUrl: string;
358
+ reportId: number;
359
+ filename: string;
360
+ checkId: string;
361
+ jsonText: string;
362
+ }): Promise<{ reportChunkId: number }> {
363
+ const { apiKey, apiBaseUrl, reportId, filename, checkId, jsonText } = params;
364
+ const bodyObj: Record<string, unknown> = {
365
+ access_token: apiKey,
366
+ checkup_report_id: reportId,
367
+ filename,
368
+ check_id: checkId,
369
+ data: jsonText,
370
+ type: "json",
371
+ generate_issue: true,
372
+ };
373
+
374
+ const resp = await postRpc<any>({
375
+ apiKey,
376
+ apiBaseUrl,
377
+ rpcName: "checkup_report_file_post",
378
+ bodyObj,
379
+ });
380
+ // Backend has a typo: "report_chunck_id" (with 'ck') - handle both spellings for compatibility
381
+ const chunkId = Number(resp?.report_chunck_id ?? resp?.report_chunk_id);
382
+ if (!Number.isFinite(chunkId) || chunkId <= 0) {
383
+ throw new Error(`Unexpected checkup_report_file_post response: ${JSON.stringify(resp)}`);
384
+ }
385
+ return { reportChunkId: chunkId };
386
+ }