@neon/tools 2.12.0 → 2.14.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.
- package/README.md +152 -7
- package/dist/descriptions.gen.d.ts +10 -0
- package/dist/descriptions.gen.d.ts.map +1 -0
- package/dist/descriptions.gen.js +150 -0
- package/dist/generated/zod.gen.d.ts +6 -6
- package/dist/index.d.ts +7 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +15 -11
- package/dist/lib/custom/auth-config.d.ts +164 -0
- package/dist/lib/custom/auth-config.d.ts.map +1 -0
- package/dist/lib/custom/auth-config.js +177 -0
- package/dist/lib/custom/docs.d.ts +58 -0
- package/dist/lib/custom/docs.d.ts.map +1 -0
- package/dist/lib/custom/docs.js +143 -0
- package/dist/lib/custom/feedback.d.ts +15 -0
- package/dist/lib/custom/feedback.d.ts.map +1 -0
- package/dist/lib/custom/feedback.js +55 -0
- package/dist/lib/custom/fetch.d.ts +402 -0
- package/dist/lib/custom/fetch.d.ts.map +1 -0
- package/dist/lib/custom/fetch.js +198 -0
- package/dist/lib/custom/organizations.d.ts +23 -0
- package/dist/lib/custom/organizations.d.ts.map +1 -0
- package/dist/lib/custom/organizations.js +32 -0
- package/dist/lib/custom/resources.d.ts +22 -0
- package/dist/lib/custom/resources.d.ts.map +1 -0
- package/dist/lib/custom/resources.js +113 -0
- package/dist/lib/custom/sql/connection.d.ts +46 -0
- package/dist/lib/custom/sql/connection.d.ts.map +1 -0
- package/dist/lib/custom/sql/connection.js +78 -0
- package/dist/lib/custom/sql/describe-branch.d.ts +84 -0
- package/dist/lib/custom/sql/describe-branch.d.ts.map +1 -0
- package/dist/lib/custom/sql/describe-branch.js +155 -0
- package/dist/lib/custom/sql/inspect/catalog.d.ts +22 -0
- package/dist/lib/custom/sql/inspect/catalog.d.ts.map +1 -0
- package/dist/lib/custom/sql/inspect/catalog.js +18 -0
- package/dist/lib/custom/sql/inspect/inspect.d.ts +75 -0
- package/dist/lib/custom/sql/inspect/inspect.d.ts.map +1 -0
- package/dist/lib/custom/sql/inspect/inspect.js +135 -0
- package/dist/lib/custom/sql/inspect/queries.d.ts +63 -0
- package/dist/lib/custom/sql/inspect/queries.d.ts.map +1 -0
- package/dist/lib/custom/sql/inspect/queries.js +528 -0
- package/dist/lib/custom/sql/inspect/report.d.ts +33 -0
- package/dist/lib/custom/sql/inspect/report.d.ts.map +1 -0
- package/dist/lib/custom/sql/inspect/report.js +45 -0
- package/dist/lib/custom/sql/inspect/targets.d.ts +27 -0
- package/dist/lib/custom/sql/inspect/targets.d.ts.map +1 -0
- package/dist/lib/custom/sql/inspect/targets.js +33 -0
- package/dist/lib/custom/sql/migrations.d.ts +69 -0
- package/dist/lib/custom/sql/migrations.d.ts.map +1 -0
- package/dist/lib/custom/sql/migrations.js +144 -0
- package/dist/lib/custom/sql/plan.d.ts +27 -0
- package/dist/lib/custom/sql/plan.d.ts.map +1 -0
- package/dist/lib/custom/sql/plan.js +53 -0
- package/dist/lib/custom/sql/query-tuning.d.ts +107 -0
- package/dist/lib/custom/sql/query-tuning.d.ts.map +1 -0
- package/dist/lib/custom/sql/query-tuning.js +129 -0
- package/dist/lib/custom/sql/rows.d.ts +11 -0
- package/dist/lib/custom/sql/rows.d.ts.map +1 -0
- package/dist/lib/custom/sql/rows.js +19 -0
- package/dist/lib/custom/sql/run.d.ts +81 -0
- package/dist/lib/custom/sql/run.d.ts.map +1 -0
- package/dist/lib/custom/sql/run.js +90 -0
- package/dist/lib/custom/sql/schema.d.ts +84 -0
- package/dist/lib/custom/sql/schema.d.ts.map +1 -0
- package/dist/lib/custom/sql/schema.js +201 -0
- package/dist/lib/custom/sql/slow-queries.d.ts +43 -0
- package/dist/lib/custom/sql/slow-queries.d.ts.map +1 -0
- package/dist/lib/custom/sql/slow-queries.js +87 -0
- package/dist/lib/customize.d.ts +9 -2
- package/dist/lib/customize.d.ts.map +1 -1
- package/dist/lib/customize.js +21 -7
- package/dist/lib/ergonomic/bind.d.ts +11 -1
- package/dist/lib/ergonomic/bind.d.ts.map +1 -1
- package/dist/lib/ergonomic/bind.js +13 -3
- package/dist/lib/ergonomic/catalog.d.ts +902 -87
- package/dist/lib/ergonomic/catalog.d.ts.map +1 -1
- package/dist/lib/ergonomic/catalog.js +35 -8
- package/dist/lib/ergonomic/composed.d.ts +4 -4
- package/dist/lib/ergonomic/composed.d.ts.map +1 -1
- package/dist/lib/ergonomic/composed.js +47 -45
- package/dist/lib/ergonomic/ids.d.ts +3 -1
- package/dist/lib/ergonomic/ids.d.ts.map +1 -1
- package/dist/lib/ergonomic/ids.js +41 -2
- package/dist/lib/ergonomic/index.d.ts +1 -1
- package/dist/lib/ergonomic/index.d.ts.map +1 -1
- package/dist/lib/ergonomic/index.js +2 -2
- package/dist/lib/operation.d.ts +11 -0
- package/dist/lib/operation.d.ts.map +1 -1
- package/dist/lib/references.d.ts +10 -0
- package/dist/lib/references.d.ts.map +1 -0
- package/dist/lib/references.js +28 -0
- package/dist/operations.gen.d.ts +2 -2
- package/dist/operations.gen.js +302 -302
- package/package.json +8 -5
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { bindTool, publishedId } from "../../ergonomic/bind.js";
|
|
2
|
+
import { SQL_METADATA, branchIdField, computeIdField, databaseNameField, projectIdField, refuseInReadOnly, resolveConnection, roleNameField, sqlClient } from "./connection.js";
|
|
3
|
+
import { SQL_MAX_ROWS_LIMIT, capRows } from "./rows.js";
|
|
4
|
+
import * as z from "zod";
|
|
5
|
+
// src/lib/custom/sql/run.ts
|
|
6
|
+
const sqlWriteAnnotations = {
|
|
7
|
+
readOnlyHint: false,
|
|
8
|
+
destructiveHint: true,
|
|
9
|
+
idempotentHint: false,
|
|
10
|
+
openWorldHint: false
|
|
11
|
+
};
|
|
12
|
+
const maxRowsField = z.number().int().min(1).max(SQL_MAX_ROWS_LIMIT).default(100).describe("Rows to return per statement. Raise it only when a result has truncated: true.");
|
|
13
|
+
const runSqlInputSchema = z.strictObject({
|
|
14
|
+
sql: z.string().describe("The SQL statement to run."),
|
|
15
|
+
project_id: projectIdField,
|
|
16
|
+
branch_id: branchIdField,
|
|
17
|
+
database_name: databaseNameField,
|
|
18
|
+
role_name: roleNameField,
|
|
19
|
+
compute_id: computeIdField,
|
|
20
|
+
max_rows: maxRowsField
|
|
21
|
+
});
|
|
22
|
+
const runSqlTransactionInputSchema = z.strictObject({
|
|
23
|
+
sql_statements: z.array(z.string()).describe("The SQL statements to run, in order."),
|
|
24
|
+
project_id: projectIdField,
|
|
25
|
+
branch_id: branchIdField,
|
|
26
|
+
database_name: databaseNameField,
|
|
27
|
+
role_name: roleNameField,
|
|
28
|
+
compute_id: computeIdField,
|
|
29
|
+
max_rows: maxRowsField
|
|
30
|
+
});
|
|
31
|
+
const explainSqlInputSchema = z.strictObject({
|
|
32
|
+
sql: z.string().describe("The SQL statement to explain."),
|
|
33
|
+
project_id: projectIdField,
|
|
34
|
+
branch_id: branchIdField,
|
|
35
|
+
database_name: databaseNameField,
|
|
36
|
+
analyze: z.boolean().default(false).describe("Run EXPLAIN ANALYZE, which executes the statement and its side effects.")
|
|
37
|
+
});
|
|
38
|
+
/** One caller statement, in its own `READ ONLY` transaction when `readOnly` is set. */
|
|
39
|
+
const runStatement = async (neonClient, target, sql, context, signal) => {
|
|
40
|
+
const connection = await resolveConnection(neonClient, target, context, signal);
|
|
41
|
+
const query = sqlClient(connection.uri, signal);
|
|
42
|
+
if (context.readOnly === true) {
|
|
43
|
+
const [rows] = await query.transaction([query.query(sql)], { readOnly: true });
|
|
44
|
+
return rows;
|
|
45
|
+
}
|
|
46
|
+
return query.query(sql);
|
|
47
|
+
};
|
|
48
|
+
const runTransaction = async (neonClient, target, statements, context, signal) => {
|
|
49
|
+
const connection = await resolveConnection(neonClient, target, context, signal);
|
|
50
|
+
const query = sqlClient(connection.uri, signal);
|
|
51
|
+
return query.transaction(statements.map((statement) => query.query(statement)));
|
|
52
|
+
};
|
|
53
|
+
const explainStatement = (neonClient, input, context, signal) => {
|
|
54
|
+
const prefix = input.analyze ? "EXPLAIN (ANALYZE, VERBOSE, BUFFERS, FILECACHE, FORMAT JSON)" : "EXPLAIN (VERBOSE, FORMAT JSON)";
|
|
55
|
+
return runStatement(neonClient, input, `${prefix} ${input.sql}`, context, signal);
|
|
56
|
+
};
|
|
57
|
+
const runSqlTool = (options) => bindTool(options, {
|
|
58
|
+
operationId: "sql.run",
|
|
59
|
+
id: publishedId("sql.run"),
|
|
60
|
+
title: "Run SQL",
|
|
61
|
+
description: "Runs one SQL statement on a branch's database and returns its rows. After a prepare step, pass its temporary branch_id. NEVER run destructive SQL autonomously; always ask the user first.",
|
|
62
|
+
inputSchema: runSqlInputSchema,
|
|
63
|
+
annotations: sqlWriteAnnotations,
|
|
64
|
+
requiresApproval: true,
|
|
65
|
+
metadata: SQL_METADATA
|
|
66
|
+
}, async (neonClient, input, signal, context) => capRows(await runStatement(neonClient, input, input.sql, context, signal), input.max_rows));
|
|
67
|
+
const runSqlTransactionTool = (options) => bindTool(options, {
|
|
68
|
+
operationId: "sql.runTransaction",
|
|
69
|
+
id: publishedId("sql.runTransaction"),
|
|
70
|
+
title: "Run SQL Transaction",
|
|
71
|
+
description: "Runs several SQL statements in one transaction and returns each one's rows. After a prepare step, pass its temporary branch_id. NEVER run destructive SQL autonomously; always ask the user first.",
|
|
72
|
+
inputSchema: runSqlTransactionInputSchema,
|
|
73
|
+
annotations: sqlWriteAnnotations,
|
|
74
|
+
requiresApproval: true,
|
|
75
|
+
metadata: SQL_METADATA
|
|
76
|
+
}, async (neonClient, input, signal, context) => {
|
|
77
|
+
refuseInReadOnly(context);
|
|
78
|
+
return (await runTransaction(neonClient, input, input.sql_statements, context, signal)).map((rows) => capRows(rows, input.max_rows));
|
|
79
|
+
});
|
|
80
|
+
const explainSqlTool = (options) => bindTool(options, {
|
|
81
|
+
operationId: "sql.explain",
|
|
82
|
+
id: publishedId("sql.explain"),
|
|
83
|
+
title: "Explain SQL Statement",
|
|
84
|
+
description: "Returns a statement's execution plan as JSON. analyze true executes it, side effects included, so never analyze potentially destructive SQL autonomously. For rows, use {{sql.run}}.",
|
|
85
|
+
inputSchema: explainSqlInputSchema,
|
|
86
|
+
annotations: sqlWriteAnnotations,
|
|
87
|
+
requiresApproval: true,
|
|
88
|
+
metadata: SQL_METADATA
|
|
89
|
+
}, (neonClient, input, signal, context) => explainStatement(neonClient, input, context, signal));
|
|
90
|
+
export { explainSqlInputSchema, explainSqlTool, explainStatement, runSqlInputSchema, runSqlTool, runSqlTransactionInputSchema, runSqlTransactionTool, runStatement, runTransaction, sqlWriteAnnotations };
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { type ToolClientOptions } from "../../ergonomic/bind.js";
|
|
3
|
+
import type { NeonToolAnnotations } from "../../operation.js";
|
|
4
|
+
export declare const sqlReadAnnotations: NeonToolAnnotations;
|
|
5
|
+
export declare const listTablesInputSchema: z.ZodObject<{
|
|
6
|
+
project_id: z.ZodString;
|
|
7
|
+
branch_id: z.ZodOptional<z.ZodString>;
|
|
8
|
+
database_name: z.ZodOptional<z.ZodString>;
|
|
9
|
+
}, z.core.$strict>;
|
|
10
|
+
export declare const describeTableInputSchema: z.ZodObject<{
|
|
11
|
+
table_name: z.ZodString;
|
|
12
|
+
project_id: z.ZodString;
|
|
13
|
+
branch_id: z.ZodOptional<z.ZodString>;
|
|
14
|
+
database_name: z.ZodOptional<z.ZodString>;
|
|
15
|
+
}, z.core.$strict>;
|
|
16
|
+
interface ColumnDescription {
|
|
17
|
+
name: string;
|
|
18
|
+
type: string;
|
|
19
|
+
nullable: boolean;
|
|
20
|
+
default: string | null;
|
|
21
|
+
description: string | null;
|
|
22
|
+
}
|
|
23
|
+
interface IndexDescription {
|
|
24
|
+
name: string;
|
|
25
|
+
definition: string;
|
|
26
|
+
size: string;
|
|
27
|
+
}
|
|
28
|
+
interface ConstraintDescription {
|
|
29
|
+
name: string;
|
|
30
|
+
type: string;
|
|
31
|
+
definition: string;
|
|
32
|
+
}
|
|
33
|
+
export interface TableDescription {
|
|
34
|
+
columns: ColumnDescription[];
|
|
35
|
+
indexes: IndexDescription[];
|
|
36
|
+
constraints: ConstraintDescription[];
|
|
37
|
+
tableSize: string;
|
|
38
|
+
indexSize: string;
|
|
39
|
+
totalSize: string;
|
|
40
|
+
}
|
|
41
|
+
export declare const formatTableDescription: (desc: TableDescription) => string;
|
|
42
|
+
export declare const describeTableAt: (uri: string, tableName: string, signal?: AbortSignal) => Promise<{
|
|
43
|
+
raw: TableDescription;
|
|
44
|
+
formatted: string;
|
|
45
|
+
}>;
|
|
46
|
+
export declare const listTablesTool: (options: ToolClientOptions) => import("../../operation.js").NeonTool<z.ZodObject<{
|
|
47
|
+
project_id: z.ZodString;
|
|
48
|
+
branch_id: z.ZodOptional<z.ZodString>;
|
|
49
|
+
database_name: z.ZodOptional<z.ZodString>;
|
|
50
|
+
}, z.core.$strict>, "list_tables_schema", {
|
|
51
|
+
[x: string]: unknown;
|
|
52
|
+
}[]>;
|
|
53
|
+
export declare const describeTableTool: (options: ToolClientOptions) => import("../../operation.js").NeonTool<z.ZodObject<{
|
|
54
|
+
table_name: z.ZodString;
|
|
55
|
+
project_id: z.ZodString;
|
|
56
|
+
branch_id: z.ZodOptional<z.ZodString>;
|
|
57
|
+
database_name: z.ZodOptional<z.ZodString>;
|
|
58
|
+
}, z.core.$strict>, "describe_table_schema", {
|
|
59
|
+
raw: {
|
|
60
|
+
columns: {
|
|
61
|
+
name: string;
|
|
62
|
+
type: string;
|
|
63
|
+
nullable: boolean;
|
|
64
|
+
default: string | null;
|
|
65
|
+
description: string | null;
|
|
66
|
+
}[];
|
|
67
|
+
indexes: {
|
|
68
|
+
name: string;
|
|
69
|
+
definition: string;
|
|
70
|
+
size: string;
|
|
71
|
+
}[];
|
|
72
|
+
constraints: {
|
|
73
|
+
name: string;
|
|
74
|
+
type: string;
|
|
75
|
+
definition: string;
|
|
76
|
+
}[];
|
|
77
|
+
tableSize: string;
|
|
78
|
+
indexSize: string;
|
|
79
|
+
totalSize: string;
|
|
80
|
+
};
|
|
81
|
+
formatted: string;
|
|
82
|
+
}>;
|
|
83
|
+
export {};
|
|
84
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../../../../src/lib/custom/sql/schema.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,EAGN,KAAK,iBAAiB,EACtB,MAAM,yBAAyB,CAAC;AACjC,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,oBAAoB,CAAC;AAU9D,eAAO,MAAM,kBAAkB,EAAE,mBAKhC,CAAC;AAEF,eAAO,MAAM,qBAAqB;;;;kBAIhC,CAAC;AAEH,eAAO,MAAM,wBAAwB;;;;;kBASnC,CAAC;AAsFH,UAAU,iBAAiB;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,OAAO,CAAC;IAClB,OAAO,EAAE,MAAM,GAAG,IAAI,CAAC;IACvB,WAAW,EAAE,MAAM,GAAG,IAAI,CAAC;CAC3B;AAED,UAAU,gBAAgB;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;IACnB,IAAI,EAAE,MAAM,CAAC;CACb;AAED,UAAU,qBAAqB;IAC9B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,UAAU,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,gBAAgB;IAChC,OAAO,EAAE,iBAAiB,EAAE,CAAC;IAC7B,OAAO,EAAE,gBAAgB,EAAE,CAAC;IAC5B,WAAW,EAAE,qBAAqB,EAAE,CAAC;IACrC,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;CAClB;AAgDD,eAAO,MAAM,sBAAsB,GAAI,MAAM,gBAAgB,KAAG,MAmC/D,CAAC;AAEF,eAAO,MAAM,eAAe,GAC3B,KAAK,MAAM,EACX,WAAW,MAAM,EACjB,SAAS,WAAW;;;EAUpB,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,SAAS,iBAAiB;;;;;;IAuBvD,CAAC;AAEH,eAAO,MAAM,iBAAiB,GAAI,SAAS,iBAAiB;;;;;;;;kBAvJrD,MAAM;kBACN,MAAM;sBACF,OAAO;qBACR,MAAM,GAAG,IAAI;yBACT,MAAM,GAAG,IAAI;;;kBAIpB,MAAM;wBACA,MAAM;kBACZ,MAAM;;;kBAIN,MAAM;kBACN,MAAM;wBACA,MAAM;;mBAOP,MAAM;mBACN,MAAM;mBACN,MAAM;;;EAqJhB,CAAC"}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { bindTool, publishedId } from "../../ergonomic/bind.js";
|
|
2
|
+
import { SQL_METADATA, branchIdField, databaseNameField, projectIdField, resolveConnection, sqlClient } from "./connection.js";
|
|
3
|
+
import { NeonClientError } from "@neon/sdk";
|
|
4
|
+
import * as z from "zod";
|
|
5
|
+
// src/lib/custom/sql/schema.ts
|
|
6
|
+
const sqlReadAnnotations = {
|
|
7
|
+
readOnlyHint: true,
|
|
8
|
+
destructiveHint: false,
|
|
9
|
+
idempotentHint: true,
|
|
10
|
+
openWorldHint: false
|
|
11
|
+
};
|
|
12
|
+
const listTablesInputSchema = z.strictObject({
|
|
13
|
+
project_id: projectIdField,
|
|
14
|
+
branch_id: branchIdField,
|
|
15
|
+
database_name: databaseNameField
|
|
16
|
+
});
|
|
17
|
+
const describeTableInputSchema = z.strictObject({
|
|
18
|
+
table_name: z.string().describe("The table name, optionally schema-qualified (for example, crm.contacts)"),
|
|
19
|
+
project_id: projectIdField,
|
|
20
|
+
branch_id: branchIdField,
|
|
21
|
+
database_name: databaseNameField
|
|
22
|
+
});
|
|
23
|
+
const LIST_TABLES_SQL = `
|
|
24
|
+
SELECT
|
|
25
|
+
table_schema,
|
|
26
|
+
table_name,
|
|
27
|
+
table_type
|
|
28
|
+
FROM information_schema.tables
|
|
29
|
+
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
|
|
30
|
+
ORDER BY table_schema, table_name;
|
|
31
|
+
`;
|
|
32
|
+
const DESCRIBE_COLUMNS_SQL = `
|
|
33
|
+
WITH target AS (
|
|
34
|
+
SELECT
|
|
35
|
+
cls.oid,
|
|
36
|
+
ns.nspname AS table_schema,
|
|
37
|
+
cls.relname AS table_name
|
|
38
|
+
FROM pg_catalog.pg_class cls
|
|
39
|
+
JOIN pg_catalog.pg_namespace ns ON ns.oid = cls.relnamespace
|
|
40
|
+
WHERE cls.oid = to_regclass($1)
|
|
41
|
+
)
|
|
42
|
+
SELECT
|
|
43
|
+
c.column_name as name,
|
|
44
|
+
c.data_type as type,
|
|
45
|
+
c.is_nullable = 'YES' as nullable,
|
|
46
|
+
c.column_default as default,
|
|
47
|
+
pd.description
|
|
48
|
+
FROM target t
|
|
49
|
+
JOIN information_schema.columns c
|
|
50
|
+
ON c.table_schema = t.table_schema
|
|
51
|
+
AND c.table_name = t.table_name
|
|
52
|
+
LEFT JOIN pg_catalog.pg_description pd
|
|
53
|
+
ON pd.objoid = t.oid
|
|
54
|
+
AND pd.objsubid = c.ordinal_position
|
|
55
|
+
ORDER BY c.ordinal_position;
|
|
56
|
+
`;
|
|
57
|
+
const DESCRIBE_INDEXES_SQL = `
|
|
58
|
+
WITH target AS (
|
|
59
|
+
SELECT to_regclass($1) AS oid
|
|
60
|
+
)
|
|
61
|
+
SELECT
|
|
62
|
+
i.relname as name,
|
|
63
|
+
pg_get_indexdef(i.oid) as definition,
|
|
64
|
+
pg_size_pretty(pg_relation_size(i.oid)) as size
|
|
65
|
+
FROM target
|
|
66
|
+
JOIN pg_catalog.pg_class t ON t.oid = target.oid
|
|
67
|
+
JOIN pg_catalog.pg_index ix ON t.oid = ix.indrelid
|
|
68
|
+
JOIN pg_catalog.pg_class i ON i.oid = ix.indexrelid
|
|
69
|
+
WHERE t.relkind = 'r';
|
|
70
|
+
`;
|
|
71
|
+
const DESCRIBE_CONSTRAINTS_SQL = `
|
|
72
|
+
WITH target AS (
|
|
73
|
+
SELECT
|
|
74
|
+
cls.oid,
|
|
75
|
+
ns.nspname AS table_schema,
|
|
76
|
+
cls.relname AS table_name
|
|
77
|
+
FROM pg_catalog.pg_class cls
|
|
78
|
+
JOIN pg_catalog.pg_namespace ns ON ns.oid = cls.relnamespace
|
|
79
|
+
WHERE cls.oid = to_regclass($1)
|
|
80
|
+
)
|
|
81
|
+
SELECT
|
|
82
|
+
tc.constraint_name as name,
|
|
83
|
+
tc.constraint_type as type,
|
|
84
|
+
pg_get_constraintdef(cc.oid) as definition
|
|
85
|
+
FROM target t
|
|
86
|
+
JOIN information_schema.table_constraints tc
|
|
87
|
+
ON tc.table_schema = t.table_schema
|
|
88
|
+
AND tc.table_name = t.table_name
|
|
89
|
+
JOIN pg_catalog.pg_constraint cc
|
|
90
|
+
ON cc.conrelid = t.oid
|
|
91
|
+
AND cc.conname = tc.constraint_name;
|
|
92
|
+
`;
|
|
93
|
+
const DESCRIBE_SIZES_SQL = `
|
|
94
|
+
SELECT
|
|
95
|
+
pg_size_pretty(pg_total_relation_size(to_regclass($1))) as total_size,
|
|
96
|
+
pg_size_pretty(pg_relation_size(to_regclass($1))) as table_size,
|
|
97
|
+
pg_size_pretty(
|
|
98
|
+
pg_total_relation_size(to_regclass($1)) - pg_relation_size(to_regclass($1))
|
|
99
|
+
) as index_size;
|
|
100
|
+
`;
|
|
101
|
+
const describeTable = async (uri, tableName, signal) => {
|
|
102
|
+
const sql = sqlClient(uri, signal);
|
|
103
|
+
if (typeof (await sql.query("SELECT to_regclass($1)::text AS relation_name", [tableName]))[0]?.relation_name !== "string") return null;
|
|
104
|
+
const [columns, indexes, constraints, sizes] = await Promise.all([
|
|
105
|
+
sql.query(DESCRIBE_COLUMNS_SQL, [tableName]),
|
|
106
|
+
sql.query(DESCRIBE_INDEXES_SQL, [tableName]),
|
|
107
|
+
sql.query(DESCRIBE_CONSTRAINTS_SQL, [tableName]),
|
|
108
|
+
sql.query(DESCRIBE_SIZES_SQL, [tableName])
|
|
109
|
+
]);
|
|
110
|
+
const size = sizes[0];
|
|
111
|
+
if (size === void 0) throw new Error(`Table size query returned no row for ${tableName}`);
|
|
112
|
+
return {
|
|
113
|
+
columns: columns.map((column) => ({
|
|
114
|
+
name: column.name,
|
|
115
|
+
type: column.type,
|
|
116
|
+
nullable: column.nullable,
|
|
117
|
+
default: column.default,
|
|
118
|
+
description: column.description
|
|
119
|
+
})),
|
|
120
|
+
indexes: indexes.map((index) => ({
|
|
121
|
+
name: index.name,
|
|
122
|
+
definition: index.definition,
|
|
123
|
+
size: index.size
|
|
124
|
+
})),
|
|
125
|
+
constraints: constraints.map((constraint) => ({
|
|
126
|
+
name: constraint.name,
|
|
127
|
+
type: constraint.type,
|
|
128
|
+
definition: constraint.definition
|
|
129
|
+
})),
|
|
130
|
+
tableSize: size.table_size,
|
|
131
|
+
indexSize: size.index_size,
|
|
132
|
+
totalSize: size.total_size
|
|
133
|
+
};
|
|
134
|
+
};
|
|
135
|
+
const formatTableDescription = (desc) => {
|
|
136
|
+
const lines = [
|
|
137
|
+
`Table size: ${desc.tableSize}`,
|
|
138
|
+
`Index size: ${desc.indexSize}`,
|
|
139
|
+
`Total size: ${desc.totalSize}`,
|
|
140
|
+
"",
|
|
141
|
+
"Columns:"
|
|
142
|
+
];
|
|
143
|
+
for (const column of desc.columns) {
|
|
144
|
+
const nullable = column.nullable ? "NULL" : "NOT NULL";
|
|
145
|
+
const defaultValue = column.default ? ` DEFAULT ${column.default}` : "";
|
|
146
|
+
const description = column.description ? `\n ${column.description}` : "";
|
|
147
|
+
lines.push(` ${column.name} ${column.type} ${nullable}${defaultValue}${description}`);
|
|
148
|
+
}
|
|
149
|
+
lines.push("");
|
|
150
|
+
if (desc.indexes.length > 0) {
|
|
151
|
+
lines.push("Indexes:");
|
|
152
|
+
for (const index of desc.indexes) {
|
|
153
|
+
lines.push(` ${index.name} (${index.size})`);
|
|
154
|
+
lines.push(` ${index.definition}`);
|
|
155
|
+
}
|
|
156
|
+
lines.push("");
|
|
157
|
+
}
|
|
158
|
+
if (desc.constraints.length > 0) {
|
|
159
|
+
lines.push("Constraints:");
|
|
160
|
+
for (const constraint of desc.constraints) {
|
|
161
|
+
lines.push(` ${constraint.name} (${constraint.type})`);
|
|
162
|
+
lines.push(` ${constraint.definition}`);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return lines.join("\n");
|
|
166
|
+
};
|
|
167
|
+
const describeTableAt = async (uri, tableName, signal) => {
|
|
168
|
+
const description = await describeTable(uri, tableName, signal);
|
|
169
|
+
if (description === null) throw new NeonClientError(`Table not found: ${tableName}`);
|
|
170
|
+
return {
|
|
171
|
+
raw: description,
|
|
172
|
+
formatted: formatTableDescription(description)
|
|
173
|
+
};
|
|
174
|
+
};
|
|
175
|
+
const listTablesTool = (options) => bindTool(options, {
|
|
176
|
+
operationId: "schema.listTables",
|
|
177
|
+
id: publishedId("schema.listTables"),
|
|
178
|
+
title: "Get Database Tables",
|
|
179
|
+
description: "Lists a database's tables and views outside the system schemas. For one table's columns, use {{schema.describeTable}}.",
|
|
180
|
+
inputSchema: listTablesInputSchema,
|
|
181
|
+
annotations: sqlReadAnnotations,
|
|
182
|
+
requiresApproval: false,
|
|
183
|
+
metadata: SQL_METADATA
|
|
184
|
+
}, async (neonClient, input, signal, context) => {
|
|
185
|
+
const connection = await resolveConnection(neonClient, input, context, signal);
|
|
186
|
+
return sqlClient(connection.uri, signal).query(LIST_TABLES_SQL);
|
|
187
|
+
});
|
|
188
|
+
const describeTableTool = (options) => bindTool(options, {
|
|
189
|
+
operationId: "schema.describeTable",
|
|
190
|
+
id: publishedId("schema.describeTable"),
|
|
191
|
+
title: "Describe Table Schema",
|
|
192
|
+
description: "Returns a table's columns, indexes, constraints, and sizes. For every table, use {{schema.listTables}}.",
|
|
193
|
+
inputSchema: describeTableInputSchema,
|
|
194
|
+
annotations: sqlReadAnnotations,
|
|
195
|
+
requiresApproval: false,
|
|
196
|
+
metadata: SQL_METADATA
|
|
197
|
+
}, async (neonClient, input, signal, context) => {
|
|
198
|
+
const connection = await resolveConnection(neonClient, input, context, signal);
|
|
199
|
+
return describeTableAt(connection.uri, input.table_name, signal);
|
|
200
|
+
});
|
|
201
|
+
export { describeTableAt, describeTableInputSchema, describeTableTool, formatTableDescription, listTablesInputSchema, listTablesTool, sqlReadAnnotations };
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import { type ToolClientOptions } from "../../ergonomic/bind.js";
|
|
3
|
+
export declare const listSlowQueriesInputSchema: z.ZodObject<{
|
|
4
|
+
project_id: z.ZodString;
|
|
5
|
+
branch_id: z.ZodOptional<z.ZodString>;
|
|
6
|
+
database_name: z.ZodOptional<z.ZodString>;
|
|
7
|
+
compute_id: z.ZodOptional<z.ZodString>;
|
|
8
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
9
|
+
min_execution_time: z.ZodDefault<z.ZodNumber>;
|
|
10
|
+
}, z.core.$strict>;
|
|
11
|
+
export declare const listSlowQueriesTool: (options: ToolClientOptions) => import("../../operation.js").NeonTool<z.ZodObject<{
|
|
12
|
+
project_id: z.ZodString;
|
|
13
|
+
branch_id: z.ZodOptional<z.ZodString>;
|
|
14
|
+
database_name: z.ZodOptional<z.ZodString>;
|
|
15
|
+
compute_id: z.ZodOptional<z.ZodString>;
|
|
16
|
+
limit: z.ZodDefault<z.ZodNumber>;
|
|
17
|
+
min_execution_time: z.ZodDefault<z.ZodNumber>;
|
|
18
|
+
}, z.core.$strict>, "list_slow_queries", {
|
|
19
|
+
slow_queries: {
|
|
20
|
+
query: unknown;
|
|
21
|
+
calls: unknown;
|
|
22
|
+
total_exec_time_ms: unknown;
|
|
23
|
+
mean_exec_time_ms: unknown;
|
|
24
|
+
rows: unknown;
|
|
25
|
+
shared_blocks: {
|
|
26
|
+
hit: unknown;
|
|
27
|
+
read: unknown;
|
|
28
|
+
written: unknown;
|
|
29
|
+
dirtied: unknown;
|
|
30
|
+
};
|
|
31
|
+
temp_blocks: {
|
|
32
|
+
read: unknown;
|
|
33
|
+
written: unknown;
|
|
34
|
+
};
|
|
35
|
+
wal: {
|
|
36
|
+
records: unknown;
|
|
37
|
+
full_page_images: unknown;
|
|
38
|
+
bytes: unknown;
|
|
39
|
+
};
|
|
40
|
+
}[];
|
|
41
|
+
total_queries_found: number;
|
|
42
|
+
}>;
|
|
43
|
+
//# sourceMappingURL=slow-queries.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slow-queries.d.ts","sourceRoot":"","sources":["../../../../src/lib/custom/sql/slow-queries.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,EAGN,KAAK,iBAAiB,EACtB,MAAM,yBAAyB,CAAC;AAWjC,eAAO,MAAM,0BAA0B;;;;;;;kBAarC,CAAC;AA+BH,eAAO,MAAM,mBAAmB,GAAI,SAAS,iBAAiB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA+D5D,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { bindTool, publishedId } from "../../ergonomic/bind.js";
|
|
2
|
+
import { SQL_METADATA, branchIdField, computeIdField, databaseNameField, projectIdField, resolveConnection, sqlClient } from "./connection.js";
|
|
3
|
+
import { NeonClientError } from "@neon/sdk";
|
|
4
|
+
import * as z from "zod";
|
|
5
|
+
// src/lib/custom/sql/slow-queries.ts
|
|
6
|
+
const listSlowQueriesInputSchema = z.strictObject({
|
|
7
|
+
project_id: projectIdField,
|
|
8
|
+
branch_id: branchIdField,
|
|
9
|
+
database_name: databaseNameField,
|
|
10
|
+
compute_id: computeIdField,
|
|
11
|
+
limit: z.number().default(10).describe("Maximum number of queries to return."),
|
|
12
|
+
min_execution_time: z.number().default(1e3).describe("Ignored; results are not filtered by time.")
|
|
13
|
+
});
|
|
14
|
+
const EXTENSION_CHECK_SQL = `
|
|
15
|
+
SELECT EXISTS (
|
|
16
|
+
SELECT 1 FROM pg_extension WHERE extname = 'pg_stat_statements'
|
|
17
|
+
) as extension_exists;
|
|
18
|
+
`;
|
|
19
|
+
const SLOW_QUERIES_SQL = `
|
|
20
|
+
SELECT
|
|
21
|
+
query,
|
|
22
|
+
calls,
|
|
23
|
+
total_exec_time,
|
|
24
|
+
mean_exec_time,
|
|
25
|
+
rows,
|
|
26
|
+
shared_blks_hit,
|
|
27
|
+
shared_blks_read,
|
|
28
|
+
shared_blks_written,
|
|
29
|
+
shared_blks_dirtied,
|
|
30
|
+
temp_blks_read,
|
|
31
|
+
temp_blks_written,
|
|
32
|
+
wal_records,
|
|
33
|
+
wal_fpi,
|
|
34
|
+
wal_bytes
|
|
35
|
+
FROM pg_stat_statements
|
|
36
|
+
WHERE query NOT LIKE '%pg_stat_statements%'
|
|
37
|
+
AND query NOT LIKE '%EXPLAIN%'
|
|
38
|
+
ORDER BY mean_exec_time DESC
|
|
39
|
+
LIMIT $1;
|
|
40
|
+
`;
|
|
41
|
+
const listSlowQueriesTool = (options) => bindTool(options, {
|
|
42
|
+
operationId: "queries.listSlow",
|
|
43
|
+
id: publishedId("queries.listSlow"),
|
|
44
|
+
title: "List Slow Queries",
|
|
45
|
+
description: "Lists queries from pg_stat_statements, slowest mean execution time first. For sizes, indexes, locks, cache, bloat, or replication, use {{database.inspect}}.",
|
|
46
|
+
inputSchema: listSlowQueriesInputSchema,
|
|
47
|
+
annotations: {
|
|
48
|
+
readOnlyHint: true,
|
|
49
|
+
destructiveHint: false,
|
|
50
|
+
idempotentHint: false,
|
|
51
|
+
openWorldHint: false
|
|
52
|
+
},
|
|
53
|
+
requiresApproval: false,
|
|
54
|
+
metadata: SQL_METADATA
|
|
55
|
+
}, async (neonClient, input, signal, context) => {
|
|
56
|
+
const connection = await resolveConnection(neonClient, input, context, signal);
|
|
57
|
+
const sql = sqlClient(connection.uri, signal);
|
|
58
|
+
const [extension] = await sql.query(EXTENSION_CHECK_SQL);
|
|
59
|
+
if (extension?.extension_exists !== true) throw new NeonClientError("pg_stat_statements extension is not installed on the database. Please install it using the following command: CREATE EXTENSION pg_stat_statements;");
|
|
60
|
+
const formatted = (await sql.query(SLOW_QUERIES_SQL, [input.limit])).map((query) => ({
|
|
61
|
+
query: query.query,
|
|
62
|
+
calls: query.calls,
|
|
63
|
+
total_exec_time_ms: query.total_exec_time,
|
|
64
|
+
mean_exec_time_ms: query.mean_exec_time,
|
|
65
|
+
rows: query.rows,
|
|
66
|
+
shared_blocks: {
|
|
67
|
+
hit: query.shared_blks_hit,
|
|
68
|
+
read: query.shared_blks_read,
|
|
69
|
+
written: query.shared_blks_written,
|
|
70
|
+
dirtied: query.shared_blks_dirtied
|
|
71
|
+
},
|
|
72
|
+
temp_blocks: {
|
|
73
|
+
read: query.temp_blks_read,
|
|
74
|
+
written: query.temp_blks_written
|
|
75
|
+
},
|
|
76
|
+
wal: {
|
|
77
|
+
records: query.wal_records,
|
|
78
|
+
full_page_images: query.wal_fpi,
|
|
79
|
+
bytes: query.wal_bytes
|
|
80
|
+
}
|
|
81
|
+
}));
|
|
82
|
+
return {
|
|
83
|
+
slow_queries: formatted,
|
|
84
|
+
total_queries_found: formatted.length
|
|
85
|
+
};
|
|
86
|
+
});
|
|
87
|
+
export { listSlowQueriesInputSchema, listSlowQueriesTool };
|
package/dist/lib/customize.d.ts
CHANGED
|
@@ -44,9 +44,16 @@ export type InjectedNeonTool<Tool, Inject> = Tool extends NeonTool<infer Schema,
|
|
|
44
44
|
} : Tool;
|
|
45
45
|
export declare const pathTemplateParams: (path: string) => Set<string>;
|
|
46
46
|
export declare const assertToolCustomizeOptions: (options: NeonToolCustomizeOptions) => void;
|
|
47
|
-
|
|
48
|
-
|
|
47
|
+
/**
|
|
48
|
+
* Runs for every tool, customized or not. `publishedIds` holds the final id of
|
|
49
|
+
* each selected tool, computed once with `publishedToolId`, so `names` and `name`
|
|
50
|
+
* run once per selected tool. A reference to a tool that is not selected
|
|
51
|
+
* resolves to its default id without calling them.
|
|
52
|
+
*/
|
|
53
|
+
export declare const applyToolCustomization: <Tool extends NeonTool>(tool: Tool, options: NeonToolCustomizeOptions, publishedIds: ReadonlyMap<string, string>) => Omit<Tool, "id"> & {
|
|
49
54
|
id: string;
|
|
50
55
|
};
|
|
56
|
+
/** Applies `names`, then `name`, to a tool's default id. Call it once per selected tool. */
|
|
57
|
+
export declare function publishedToolId(tool: NeonToolNameSource, options: NeonToolCustomizeOptions): string;
|
|
51
58
|
export {};
|
|
52
59
|
//# sourceMappingURL=customize.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"customize.d.ts","sourceRoot":"","sources":["../../src/lib/customize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"customize.d.ts","sourceRoot":"","sources":["../../src/lib/customize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,OAAO,KAAK,EACX,QAAQ,EACR,wBAAwB,EACxB,cAAc,EACd,MAAM,gBAAgB,CAAC;AAOxB,MAAM,MAAM,mBAAmB,GAC5B,MAAM,GACN,CAAC,MAAM,MAAM,GAAG,SAAS,GAAG,OAAO,CAAC,MAAM,GAAG,SAAS,CAAC,CAAC,CAAC;AAE5D,MAAM,WAAW,qBAAqB;IACrC,SAAS,CAAC,EAAE,mBAAmB,CAAC;IAChC,QAAQ,CAAC,EAAE,mBAAmB,CAAC;IAC/B,cAAc,CAAC,EAAE,OAAO,CAAC;CACzB;AAED,MAAM,WAAW,yBAAyB;IACzC,WAAW,EAAE,MAAM,CAAC;IACpB,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,WAAW,EAAE,MAAM,CAAC;CACpB;AAED,MAAM,MA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AM,kBAAkB,GAAI,MAAM,MAAM,KAAG,GAAG,CAAC,MAAM,CACU,CAAC;AAEvE,eAAO,MAAM,0BAA0B,GACtC,SAAS,wBAAwB,KAC/B,IAgBF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,GAAI,IAAI,SAAS,QAAQ,EAC3D,MAAM,IAAI,EACV,SAAS,wBAAwB,EACjC,cAAc,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,KACvC,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG;IAAE,EAAE,EAAE,MAAM,CAAA;CA4CjC,CAAC;AAEF,4FAA4F;AAC5F,wBAAgB,eAAe,CAC9B,IAAI,EAAE,kBAAkB,EACxB,OAAO,EAAE,wBAAwB,GAC/B,MAAM,CA8BR"}
|
package/dist/lib/customize.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { publishedId } from "./ergonomic/bind.js";
|
|
2
|
+
import { resolveInputReferences, resolveToolReferences } from "./references.js";
|
|
1
3
|
import * as z from "zod";
|
|
2
4
|
// src/lib/customize.ts
|
|
3
5
|
const PATH_INJECT = [["projectId", "project_id"], ["branchId", "branch_id"]];
|
|
@@ -10,12 +12,23 @@ const assertToolCustomizeOptions = (options) => {
|
|
|
10
12
|
if (options.name !== void 0 && typeof options.name !== "function") throw new TypeError("Neon tool name transforms must be functions");
|
|
11
13
|
if (options.names !== void 0 && typeof options.names !== "function" && (typeof options.names !== "object" || options.names === null || Array.isArray(options.names))) throw new TypeError("Neon tool name overrides must be a map or a function");
|
|
12
14
|
};
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
/**
|
|
16
|
+
* Runs for every tool, customized or not. `publishedIds` holds the final id of
|
|
17
|
+
* each selected tool, computed once with `publishedToolId`, so `names` and `name`
|
|
18
|
+
* run once per selected tool. A reference to a tool that is not selected
|
|
19
|
+
* resolves to its default id without calling them.
|
|
20
|
+
*/
|
|
21
|
+
const applyToolCustomization = (tool, options, publishedIds) => {
|
|
15
22
|
const inject = resolveInject(options.inject);
|
|
16
|
-
const id =
|
|
17
|
-
|
|
18
|
-
const
|
|
23
|
+
const id = publishedIds.get(tool.operationId);
|
|
24
|
+
if (id === void 0) throw new TypeError(`No published id for ${tool.operationId}`);
|
|
25
|
+
const nameOf = (selector) => publishedIds.get(selector) ?? publishedId(selector);
|
|
26
|
+
const where = `the description of ${tool.operationId}`;
|
|
27
|
+
const description = resolveToolReferences(applyDescription({
|
|
28
|
+
...tool,
|
|
29
|
+
description: resolveToolReferences(tool.description, nameOf, where)
|
|
30
|
+
}, options.descriptions), nameOf, where);
|
|
31
|
+
const inputSchema = publishInputSchema(resolveInputReferences(tool.inputSchema, nameOf, `the input of ${tool.operationId}`), inject, pathTemplateParams(tool.metadata.path));
|
|
19
32
|
const wrapExecute = options.onExecute !== void 0 || inject.hasAny;
|
|
20
33
|
return {
|
|
21
34
|
...tool,
|
|
@@ -28,7 +41,8 @@ const applyToolCustomization = (tool, options) => {
|
|
|
28
41
|
}, options.onExecute, inject) : tool.execute
|
|
29
42
|
};
|
|
30
43
|
};
|
|
31
|
-
|
|
44
|
+
/** Applies `names`, then `name`, to a tool's default id. Call it once per selected tool. */
|
|
45
|
+
function publishedToolId(tool, options) {
|
|
32
46
|
let id = tool.id;
|
|
33
47
|
if (options.names !== void 0) {
|
|
34
48
|
if (typeof options.names === "function") {
|
|
@@ -154,4 +168,4 @@ function publishInputSchema(schema, inject, pathKeys) {
|
|
|
154
168
|
if (!changed) return schema;
|
|
155
169
|
return Object.keys(nextShape).length === 0 ? z.strictObject({}) : z.strictObject(nextShape);
|
|
156
170
|
}
|
|
157
|
-
export { applyToolCustomization, assertToolCustomizeOptions,
|
|
171
|
+
export { applyToolCustomization, assertToolCustomizeOptions, pathTemplateParams, publishedToolId };
|
|
@@ -9,6 +9,14 @@ export interface ToolClientOptions {
|
|
|
9
9
|
baseUrl?: string;
|
|
10
10
|
fetch?: typeof fetch;
|
|
11
11
|
wait?: NeonConfig["wait"];
|
|
12
|
+
/** Where `docs.search` posts its query. */
|
|
13
|
+
siteSearchUrl?: string;
|
|
14
|
+
/** Where `docs.get` reads `<docsBaseUrl>/<slug>.md`. */
|
|
15
|
+
docsBaseUrl?: string;
|
|
16
|
+
/** Where `feedback.send` posts. */
|
|
17
|
+
feedbackUrl?: string;
|
|
18
|
+
/** The `source` `feedback.send` reports. */
|
|
19
|
+
feedbackSource?: string;
|
|
12
20
|
}
|
|
13
21
|
type Snake<S extends string> = S extends `${infer Head}${infer Rest}` ? Head extends Uppercase<Head> ? Head extends Lowercase<Head> ? `${Head}${Snake<Rest>}` : `_${Lowercase<Head>}${Snake<Rest>}` : `${Head}${Snake<Rest>}` : S;
|
|
14
22
|
type LastSegment<Id extends string> = Id extends `${string}.${infer Rest}` ? LastSegment<Rest> : Id;
|
|
@@ -30,7 +38,9 @@ export declare const collectObjectList: (neon: NeonClient<true>, input: {
|
|
|
30
38
|
prefix: string;
|
|
31
39
|
}>;
|
|
32
40
|
export declare const collectPages: <T>(list: Paginated<T, true>, maxItems?: number) => Promise<T[]>;
|
|
33
|
-
export declare const bindTool: <const InputSchema extends z.ZodType, const Id extends string, Output>(options: ToolClientOptions, tool: Omit<NeonTool<InputSchema, Id, JsonSafe<Awaited<Output>>>, "execute">, run: (neon: NeonClient<true>, input: z.output<InputSchema>, signal
|
|
41
|
+
export declare const bindTool: <const InputSchema extends z.ZodType, const Id extends string, Output>(options: ToolClientOptions, tool: Omit<NeonTool<InputSchema, Id, JsonSafe<Awaited<Output>>>, "execute">, run: (neon: NeonClient<true>, input: z.output<InputSchema>, signal: AbortSignal | undefined, context: NeonToolExecutionContext) => Promise<Output>) => NeonTool<InputSchema, Id, JsonSafe<Awaited<Output>>>;
|
|
42
|
+
/** A tool that calls no Neon API and so needs no credential. */
|
|
43
|
+
export declare const bindPublicTool: <const InputSchema extends z.ZodType, const Id extends string, Output>(tool: Omit<NeonTool<InputSchema, Id, JsonSafe<Awaited<Output>>>, "execute">, run: (input: z.output<InputSchema>, context: NeonToolExecutionContext) => Promise<Output>) => NeonTool<InputSchema, Id, JsonSafe<Awaited<Output>>>;
|
|
34
44
|
type GeneratedTool<G extends NeonOperationId> = ReturnType<(typeof operationFactories)[G]>;
|
|
35
45
|
type GeneratedSchema<G extends NeonOperationId> = GeneratedTool<G>["inputSchema"];
|
|
36
46
|
type GeneratedInput<G extends NeonOperationId> = z.output<GeneratedSchema<G>>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"bind.d.ts","sourceRoot":"","sources":["../../../src/lib/ergonomic/bind.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,UAAU,EAEf,KAAK,UAAU,EACf,KAAK,SAAS,EACd,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"bind.d.ts","sourceRoot":"","sources":["../../../src/lib/ergonomic/bind.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,KAAK,UAAU,EAEf,KAAK,UAAU,EACf,KAAK,SAAS,EACd,MAAM,WAAW,CAAC;AACnB,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AAEzB,OAAO,EACN,KAAK,eAAe,EACpB,kBAAkB,EAClB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAEN,KAAK,oBAAoB,EAEzB,MAAM,YAAY,CAAC;AACpB,OAAO,KAAK,EACX,QAAQ,EACR,QAAQ,EACR,mBAAmB,EACnB,wBAAwB,EACxB,MAAM,iBAAiB,CAAC;AAEzB,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,UAAU,CAAC;AAE3C,MAAM,WAAW,iBAAiB;IACjC,MAAM,CAAC,EAAE,oBAAoB,CAAC;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,IAAI,CAAC,EAAE,UAAU,CAAC,MAAM,CAAC,CAAC;IAC1B,2CAA2C;IAC3C,aAAa,CA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