@neon/tools 0.5.0 → 0.6.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 +55 -109
- package/dist/index.d.ts +24 -50
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +22 -43
- package/dist/lib/ergonomic/bind.d.ts +54 -0
- package/dist/lib/ergonomic/bind.d.ts.map +1 -0
- package/dist/lib/ergonomic/bind.js +121 -0
- package/dist/lib/ergonomic/catalog.d.ts +2601 -0
- package/dist/lib/ergonomic/catalog.d.ts.map +1 -0
- package/dist/lib/ergonomic/catalog.js +703 -0
- package/dist/lib/ergonomic/composed.d.ts +450 -0
- package/dist/lib/ergonomic/composed.d.ts.map +1 -0
- package/dist/lib/ergonomic/composed.js +186 -0
- package/dist/lib/ergonomic/ids.d.ts +7 -0
- package/dist/lib/ergonomic/ids.d.ts.map +1 -0
- package/dist/lib/ergonomic/ids.js +122 -0
- package/dist/lib/ergonomic/index.d.ts +4 -0
- package/dist/lib/ergonomic/index.d.ts.map +1 -0
- package/dist/lib/ergonomic/index.js +4 -0
- package/package.json +2 -2
- package/dist/lib/workflows.d.ts +0 -315
- package/dist/lib/workflows.d.ts.map +0 -1
- package/dist/lib/workflows.js +0 -113
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { missingBearerCredential, requireBearerCredential } from "../auth.js";
|
|
2
|
+
import { toToolResult } from "../result.js";
|
|
3
|
+
import { operationFactories } from "../../operations.gen.js";
|
|
4
|
+
import { NeonError, createNeonClient } from "@neon/sdk";
|
|
5
|
+
import * as z from "zod";
|
|
6
|
+
//#region src/lib/ergonomic/bind.ts
|
|
7
|
+
const ALL_PAGES = " This tool returns every page. Pass limit to cap how many items come back. Do not pass a cursor.";
|
|
8
|
+
const inertClient = createNeonClient({ apiKey: "unused" }).client;
|
|
9
|
+
const publishedId = (id) => id.split(".").map((segment) => segment.replace(/[A-Z]/g, (character) => `_${character.toLowerCase()}`)).join("_");
|
|
10
|
+
const resolveApiKey = (options, context) => {
|
|
11
|
+
if (context !== void 0 && "apiKey" in context) {
|
|
12
|
+
if (context.apiKey === void 0) throw new TypeError("A Neon API key or OAuth access token is required");
|
|
13
|
+
return requireBearerCredential(context.apiKey);
|
|
14
|
+
}
|
|
15
|
+
return options.apiKey === void 0 ? missingBearerCredential : requireBearerCredential(options.apiKey);
|
|
16
|
+
};
|
|
17
|
+
const toolClient = (options, context) => createNeonClient({
|
|
18
|
+
apiKey: resolveApiKey(options, context),
|
|
19
|
+
throwOnError: true,
|
|
20
|
+
waitForReadiness: true,
|
|
21
|
+
...options.baseUrl === void 0 ? {} : { baseUrl: options.baseUrl },
|
|
22
|
+
...options.fetch === void 0 ? {} : { fetch: options.fetch },
|
|
23
|
+
...options.wait === void 0 ? {} : { wait: options.wait }
|
|
24
|
+
});
|
|
25
|
+
const objectListPage = (value) => {
|
|
26
|
+
if ("objects" in value && "folders" in value) return value;
|
|
27
|
+
if ("error" in value && value.error !== void 0) throw value.error;
|
|
28
|
+
if ("data" in value && value.data !== void 0) return value.data;
|
|
29
|
+
throw new NeonError("Object list returned no data.", "client");
|
|
30
|
+
};
|
|
31
|
+
const collectObjectList = async (neon, input, signal) => {
|
|
32
|
+
const folders = [];
|
|
33
|
+
const objects = [];
|
|
34
|
+
let prefix = "";
|
|
35
|
+
let cursor;
|
|
36
|
+
for (;;) {
|
|
37
|
+
const page = objectListPage(await neon.storage.objects.list(input.project_id, input.branch_id, input.bucket_name, {
|
|
38
|
+
...input.prefix === void 0 ? {} : { prefix: input.prefix },
|
|
39
|
+
...input.delimiter === void 0 ? {} : { delimiter: input.delimiter },
|
|
40
|
+
...input.limit === void 0 ? {} : { limit: input.limit },
|
|
41
|
+
...cursor === void 0 ? {} : { cursor }
|
|
42
|
+
}, { signal }));
|
|
43
|
+
for (const folder of page.folders) {
|
|
44
|
+
if (input.limit !== void 0 && folders.length + objects.length >= input.limit) break;
|
|
45
|
+
folders.push(folder);
|
|
46
|
+
}
|
|
47
|
+
for (const object of page.objects) {
|
|
48
|
+
if (input.limit !== void 0 && folders.length + objects.length >= input.limit) break;
|
|
49
|
+
objects.push(object);
|
|
50
|
+
}
|
|
51
|
+
prefix = page.prefix;
|
|
52
|
+
if (input.limit !== void 0 && folders.length + objects.length >= input.limit) break;
|
|
53
|
+
if (page.next_cursor) {
|
|
54
|
+
cursor = page.next_cursor;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (page.is_truncated) throw new NeonError("Object list was truncated without a next cursor.", "client");
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
return {
|
|
61
|
+
folders,
|
|
62
|
+
objects,
|
|
63
|
+
prefix
|
|
64
|
+
};
|
|
65
|
+
};
|
|
66
|
+
const collectPages = async (list, maxItems) => {
|
|
67
|
+
if (maxItems === void 0) {
|
|
68
|
+
const result = await list.all();
|
|
69
|
+
if (result.error) throw result.error;
|
|
70
|
+
if (result.data === void 0) throw new NeonError("List returned no data.", "client");
|
|
71
|
+
return result.data;
|
|
72
|
+
}
|
|
73
|
+
const items = [];
|
|
74
|
+
let cursor;
|
|
75
|
+
for (;;) {
|
|
76
|
+
const result = await list.page(cursor);
|
|
77
|
+
if (result.error) throw result.error;
|
|
78
|
+
if (result.data === void 0) throw new NeonError("List returned no data.", "client");
|
|
79
|
+
items.push(...result.data.items);
|
|
80
|
+
if (items.length >= maxItems) return items.slice(0, maxItems);
|
|
81
|
+
if (!result.data.cursor || result.data.items.length === 0) break;
|
|
82
|
+
cursor = result.data.cursor;
|
|
83
|
+
}
|
|
84
|
+
return items;
|
|
85
|
+
};
|
|
86
|
+
const bindTool = (options, tool, run) => ({
|
|
87
|
+
...tool,
|
|
88
|
+
async execute(input, context) {
|
|
89
|
+
const parsed = await tool.inputSchema.parseAsync(input);
|
|
90
|
+
const result = await run(toolClient(options, context), parsed, context?.signal);
|
|
91
|
+
return toToolResult(result);
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
function fromGenerated(options, spec) {
|
|
95
|
+
const generated = operationFactories[spec.generated](inertClient);
|
|
96
|
+
const schema = generated.inputSchema;
|
|
97
|
+
const inputSchema = spec.omit === void 0 || spec.omit.length === 0 ? schema : omitObjectKeys(schema, spec.omit);
|
|
98
|
+
return {
|
|
99
|
+
operationId: spec.id,
|
|
100
|
+
id: publishedId(spec.id),
|
|
101
|
+
title: generated.title,
|
|
102
|
+
description: spec.list ? `${generated.description.trimEnd()}${ALL_PAGES}` : generated.description,
|
|
103
|
+
inputSchema,
|
|
104
|
+
annotations: spec.annotations ?? generated.annotations,
|
|
105
|
+
requiresApproval: spec.requiresApproval ?? generated.requiresApproval,
|
|
106
|
+
metadata: generated.metadata,
|
|
107
|
+
async execute(input, context) {
|
|
108
|
+
const parsed = await inputSchema.parseAsync(input);
|
|
109
|
+
const result = await spec.run(toolClient(options, context), parsed, context?.signal);
|
|
110
|
+
return toToolResult(result);
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
const omitObjectKeys = (schema, keys) => {
|
|
115
|
+
if (!(schema instanceof z.ZodObject)) throw new TypeError("Generated tool input schemas must be objects");
|
|
116
|
+
const mask = {};
|
|
117
|
+
for (const key of keys) mask[key] = true;
|
|
118
|
+
return schema.omit(mask);
|
|
119
|
+
};
|
|
120
|
+
//#endregion
|
|
121
|
+
export { bindTool, collectObjectList, collectPages, fromGenerated, publishedId, toolClient };
|