@neon/tools 0.0.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +202 -0
- package/README.md +306 -1
- package/dist/eve.d.ts +19 -0
- package/dist/eve.d.ts.map +1 -0
- package/dist/eve.js +9 -0
- package/dist/generated/zod.gen.d.ts +6018 -0
- package/dist/generated/zod.gen.d.ts.map +1 -0
- package/dist/generated/zod.gen.js +2394 -0
- package/dist/index.d.ts +56 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +62 -0
- package/dist/lib/auth.d.ts +5 -0
- package/dist/lib/auth.d.ts.map +1 -0
- package/dist/lib/auth.js +15 -0
- package/dist/lib/binary.d.ts +7 -0
- package/dist/lib/binary.d.ts.map +1 -0
- package/dist/lib/binary.js +19 -0
- package/dist/lib/customize.d.ts +52 -0
- package/dist/lib/customize.d.ts.map +1 -0
- package/dist/lib/customize.js +158 -0
- package/dist/lib/envelope.d.ts +5 -0
- package/dist/lib/envelope.d.ts.map +1 -0
- package/dist/lib/envelope.js +22 -0
- package/dist/lib/operation.d.ts +56 -0
- package/dist/lib/operation.d.ts.map +1 -0
- package/dist/lib/operation.js +30 -0
- package/dist/lib/result.d.ts +3 -0
- package/dist/lib/result.d.ts.map +1 -0
- package/dist/lib/result.js +18 -0
- package/dist/mastra.d.ts +32 -0
- package/dist/mastra.d.ts.map +1 -0
- package/dist/mastra.js +19 -0
- package/dist/mcp-v1.d.ts +3 -0
- package/dist/mcp-v1.d.ts.map +1 -0
- package/dist/mcp-v1.js +2 -0
- package/dist/mcp.d.ts +14 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +60 -0
- package/dist/operations.gen.d.ts +4437 -0
- package/dist/operations.gen.d.ts.map +1 -0
- package/dist/operations.gen.js +5951 -0
- package/dist/schemas.d.ts +2 -0
- package/dist/schemas.d.ts.map +1 -0
- package/dist/schemas.js +2 -0
- package/package.json +100 -3
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type NeonConfig, NeonError } from "@neon/sdk";
|
|
2
|
+
import { type NeonBearerCredential } from "./lib/auth.js";
|
|
3
|
+
import { type InjectedNeonTool, type NeonToolCustomizeOptions, type NeonToolInjectOptions, type NeonToolNameOverrides } from "./lib/customize.js";
|
|
4
|
+
import { type NeonOperationId, operationFactories, operationIds } from "./operations.gen.js";
|
|
5
|
+
export type { NeonBearerCredential } from "./lib/auth.js";
|
|
6
|
+
export type { InjectedNeonTool, NeonToolCustomizeOptions, NeonToolDescriptionOverrides, NeonToolDescriptionSource, NeonToolInjectOptions, NeonToolInjectValue, NeonToolNameOverrides, NeonToolNameSource, NeonToolOnExecute, NeonToolOnExecuteEvent, } from "./lib/customize.js";
|
|
7
|
+
export type { JsonSafe, JsonSafeBlob, NeonTool, NeonToolAnnotations, NeonToolExecutionContext, NeonToolMetadata, NeonToolResult, } from "./lib/operation.js";
|
|
8
|
+
export type { NeonOperationId };
|
|
9
|
+
export { NeonError, operationIds };
|
|
10
|
+
type OperationFactories = typeof operationFactories;
|
|
11
|
+
export type NeonTools<Operations extends readonly NeonOperationId[]> = {
|
|
12
|
+
[Operation in Operations[number]]: ReturnType<OperationFactories[Operation]>;
|
|
13
|
+
};
|
|
14
|
+
export type InjectedNeonTools<Operations extends readonly NeonOperationId[], Inject> = {
|
|
15
|
+
[Operation in Operations[number]]: InjectedNeonTool<ReturnType<OperationFactories[Operation]>, Inject>;
|
|
16
|
+
};
|
|
17
|
+
type WithPublishedId<Tool> = Tool extends {
|
|
18
|
+
id: string;
|
|
19
|
+
} ? Omit<Tool, "id"> & {
|
|
20
|
+
id: string;
|
|
21
|
+
} : Tool;
|
|
22
|
+
export interface NeonToolsClientOptions extends Pick<NeonConfig, "baseUrl" | "fetch">, Omit<NeonToolCustomizeOptions, "name" | "names"> {
|
|
23
|
+
apiKey?: NeonBearerCredential;
|
|
24
|
+
}
|
|
25
|
+
export interface CreateNeonToolsOptions<Operations extends readonly NeonOperationId[]> extends NeonToolsClientOptions {
|
|
26
|
+
operations: Operations;
|
|
27
|
+
}
|
|
28
|
+
type NamedNeonTools<Operations extends readonly NeonOperationId[], Inject> = {
|
|
29
|
+
[Operation in Operations[number]]: WithPublishedId<Inject extends NeonToolInjectOptions ? InjectedNeonTool<ReturnType<OperationFactories[Operation]>, Inject> : ReturnType<OperationFactories[Operation]>>;
|
|
30
|
+
};
|
|
31
|
+
type NamedNeonTool<Operation extends NeonOperationId, Inject> = WithPublishedId<Inject extends NeonToolInjectOptions ? InjectedNeonTool<ReturnType<OperationFactories[Operation]>, Inject> : ReturnType<OperationFactories[Operation]>>;
|
|
32
|
+
export declare function createNeonTools<const Operations extends readonly NeonOperationId[], const Inject extends NeonToolInjectOptions | undefined = undefined>(options: CreateNeonToolsOptions<Operations> & {
|
|
33
|
+
inject?: Inject;
|
|
34
|
+
} & ({
|
|
35
|
+
name: (id: string) => string;
|
|
36
|
+
names?: NeonToolNameOverrides;
|
|
37
|
+
} | {
|
|
38
|
+
names: NeonToolNameOverrides;
|
|
39
|
+
name?: (id: string) => string;
|
|
40
|
+
})): NamedNeonTools<Operations, Inject>;
|
|
41
|
+
export declare function createNeonTools<const Operations extends readonly NeonOperationId[], const Inject extends NeonToolInjectOptions | undefined = undefined>(options: CreateNeonToolsOptions<Operations> & {
|
|
42
|
+
inject?: Inject;
|
|
43
|
+
}): Inject extends NeonToolInjectOptions ? InjectedNeonTools<Operations, Inject> : NeonTools<Operations>;
|
|
44
|
+
export declare function createNeonTool<const Operation extends NeonOperationId, const Inject extends NeonToolInjectOptions | undefined = undefined>(operationId: Operation, options: NeonToolsClientOptions & {
|
|
45
|
+
inject?: Inject;
|
|
46
|
+
} & ({
|
|
47
|
+
name: (id: string) => string;
|
|
48
|
+
names?: NeonToolNameOverrides;
|
|
49
|
+
} | {
|
|
50
|
+
names: NeonToolNameOverrides;
|
|
51
|
+
name?: (id: string) => string;
|
|
52
|
+
})): NamedNeonTool<Operation, Inject>;
|
|
53
|
+
export declare function createNeonTool<const Operation extends NeonOperationId, const Inject extends NeonToolInjectOptions | undefined = undefined>(operationId: Operation, options: NeonToolsClientOptions & {
|
|
54
|
+
inject?: Inject;
|
|
55
|
+
}): Inject extends NeonToolInjectOptions ? InjectedNeonTool<ReturnType<OperationFactories[Operation]>, Inject> : ReturnType<OperationFactories[Operation]>;
|
|
56
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,KAAK,UAAU,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACzE,OAAO,EAEN,KAAK,oBAAoB,EAEzB,MAAM,eAAe,CAAC;AACvB,OAAO,EAIN,KAAK,gBAAgB,EACrB,KAAK,wBAAwB,EAC7B,KAAK,qBAAqB,EAC1B,KAAK,qBAAqB,EAC1B,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACN,KAAK,eAAe,EACpB,kBAAkB,EAClB,YAAY,EACZ,MAAM,qBAAqB,CAAC;AAE7B,YAAY,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAC1D,YAAY,EACX,gBAAgB,EAChB,wBAAwB,EACxB,4BAA4B,EAC5B,yBAAyB,EACzB,qBAAqB,EACrB,mBAAmB,EACnB,qBAAqB,EACrB,kBAAkB,EAClB,iBAAiB,EACjB,sBAAsB,GACtB,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EACX,QAAQ,EACR,YAAY,EACZ,QAAQ,EACR,mBAAmB,EACnB,wBAAwB,EACxB,gBAAgB,EAChB,cAAc,GACd,MAAM,oBAAoB,CAAC;AAC5B,YAAY,EAAE,eAAe,EAAE,CAAC;AAChC,OAAO,EAAE,SAAS,EAAE,YAAY,EAAE,CAAC;AAEnC,KAAK,kBAAkB,GAAG,OAAO,kBAAkB,CAAC;AAEpD,MAAM,MAAM,SAAS,CAAC,U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IAAE,IAAI,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,qBAAqB,CAAA;CAAE,GAC/D;IAAE,KAAK,EAAE,qBAAqB,CAAC;IAAC,IAAI,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,MAAM,CAAA;CAAE,CACjE,GACA,aAAa,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC;AACpC,wBAAgB,cAAc,CAC7B,KAAK,CAAC,SAAS,SAAS,eAAe,EACvC,KAAK,CAAC,MAAM,SAAS,qBAAqB,GAAG,SAAS,GAAG,SAAS,EAElE,WAAW,EAAE,SAAS,EACtB,OAAO,EAAE,sBAAsB,GAAG;IAAE,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,GACnD,MAAM,SAAS,qBAAqB,GACpC,gBAAgB,CAAC,UAAU,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC,GACnE,UAAU,CAAC,kBAAkB,CAAC,SAAS,CAAC,CAAC,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { missingBearerCredential, requireBearerCredential } from "./lib/auth.js";
|
|
2
|
+
import { applyToolCustomization, assertToolCustomizeOptions, hasToolCustomization } from "./lib/customize.js";
|
|
3
|
+
import { operationFactories, operationIds } from "./operations.gen.js";
|
|
4
|
+
import { NeonError, createNeonClient } from "@neon/sdk";
|
|
5
|
+
//#region src/index.ts
|
|
6
|
+
const createRawClient = (options) => createNeonClient({
|
|
7
|
+
apiKey: options.apiKey === void 0 ? missingBearerCredential : requireBearerCredential(options.apiKey),
|
|
8
|
+
...options.baseUrl === void 0 ? {} : { baseUrl: options.baseUrl },
|
|
9
|
+
...options.fetch === void 0 ? {} : { fetch: options.fetch }
|
|
10
|
+
}).client;
|
|
11
|
+
const operationFactoryFor = (operationId) => {
|
|
12
|
+
const knownOperationId = operationIds.find((candidate) => candidate === operationId);
|
|
13
|
+
if (knownOperationId === void 0) throw new TypeError(`Unknown Neon operation "${operationId}".`);
|
|
14
|
+
return operationFactories[knownOperationId];
|
|
15
|
+
};
|
|
16
|
+
const assertKnownNameKeys = (names, tools) => {
|
|
17
|
+
if (names === void 0 || typeof names === "function") return;
|
|
18
|
+
const known = new Set(tools.flatMap((tool) => [tool.operationId, tool.id]));
|
|
19
|
+
const unknown = Object.keys(names).filter((key) => !known.has(key));
|
|
20
|
+
if (unknown.length > 0) throw new TypeError(`Unknown Neon tool name override: ${unknown.join(", ")}`);
|
|
21
|
+
};
|
|
22
|
+
function assertNeonTools(value, operations) {
|
|
23
|
+
if (typeof value !== "object" || value === null) throw new TypeError("Expected generated Neon tools to be an object.");
|
|
24
|
+
for (const operationId of operations) if (!(operationId in value)) throw new TypeError(`Missing generated Neon tool "${operationId}".`);
|
|
25
|
+
}
|
|
26
|
+
const bindTools = (options) => {
|
|
27
|
+
assertToolCustomizeOptions(options);
|
|
28
|
+
const selected = /* @__PURE__ */ new Set();
|
|
29
|
+
const selectedFactories = options.operations.map((operationId) => {
|
|
30
|
+
if (selected.has(operationId)) throw new Error(`Duplicate Neon operation "${operationId}"`);
|
|
31
|
+
selected.add(operationId);
|
|
32
|
+
return [operationId, operationFactoryFor(operationId)];
|
|
33
|
+
});
|
|
34
|
+
const client = createRawClient(options);
|
|
35
|
+
const rawEntries = selectedFactories.map(([operationId, factory]) => {
|
|
36
|
+
return [operationId, factory(client)];
|
|
37
|
+
});
|
|
38
|
+
assertKnownNameKeys(options.names, rawEntries.map(([, tool]) => tool));
|
|
39
|
+
const entries = rawEntries.map(([operationId, tool]) => {
|
|
40
|
+
return [operationId, hasToolCustomization(options) ? applyToolCustomization(tool, options) : tool];
|
|
41
|
+
});
|
|
42
|
+
const publishedIds = /* @__PURE__ */ new Map();
|
|
43
|
+
for (const [operationId, tool] of entries) {
|
|
44
|
+
const previous = publishedIds.get(tool.id);
|
|
45
|
+
if (previous !== void 0) throw new Error(`Duplicate Neon tool id "${tool.id}" for ${previous}, ${operationId}`);
|
|
46
|
+
publishedIds.set(tool.id, operationId);
|
|
47
|
+
}
|
|
48
|
+
const tools = Object.fromEntries(entries);
|
|
49
|
+
assertNeonTools(tools, options.operations);
|
|
50
|
+
return tools;
|
|
51
|
+
};
|
|
52
|
+
function createNeonTools(options) {
|
|
53
|
+
return bindTools(options);
|
|
54
|
+
}
|
|
55
|
+
function createNeonTool(operationId, options) {
|
|
56
|
+
assertToolCustomizeOptions(options);
|
|
57
|
+
const tool = operationFactoryFor(operationId)(createRawClient(options));
|
|
58
|
+
assertKnownNameKeys(options.names, [tool]);
|
|
59
|
+
return hasToolCustomization(options) ? applyToolCustomization(tool, options) : tool;
|
|
60
|
+
}
|
|
61
|
+
//#endregion
|
|
62
|
+
export { NeonError, createNeonTool, createNeonTools, operationIds };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { NeonConfig } from "@neon/sdk";
|
|
2
|
+
export type NeonBearerCredential = NeonConfig["apiKey"];
|
|
3
|
+
export declare const missingBearerCredential: () => never;
|
|
4
|
+
export declare const requireBearerCredential: (value: NeonBearerCredential) => NeonBearerCredential;
|
|
5
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/lib/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAE5C,MAAM,MAAM,oBAAoB,GAAG,UAAU,CAAC,QAAQ,CAAC,CAAC;AAKxD,eAAO,MAAM,uBAAuB,QAAO,KAE1C,CAAC;AASF,eAAO,MAAM,uBAAuB,GACnC,OAAO,oBAAoB,KACzB,oBAKF,CAAC"}
|
package/dist/lib/auth.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/lib/auth.ts
|
|
2
|
+
const missingCredentialMessage = "A Neon API key or OAuth access token is required";
|
|
3
|
+
const missingBearerCredential = () => {
|
|
4
|
+
throw new TypeError(missingCredentialMessage);
|
|
5
|
+
};
|
|
6
|
+
const requireNonEmptyCredential = (value) => {
|
|
7
|
+
if (typeof value !== "string" || value.length === 0) throw new TypeError(missingCredentialMessage);
|
|
8
|
+
return value;
|
|
9
|
+
};
|
|
10
|
+
const requireBearerCredential = (value) => {
|
|
11
|
+
if (typeof value === "function") return async () => requireNonEmptyCredential(await value());
|
|
12
|
+
return requireNonEmptyCredential(value);
|
|
13
|
+
};
|
|
14
|
+
//#endregion
|
|
15
|
+
export { missingBearerCredential, requireBearerCredential };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"binary.d.ts","sourceRoot":"","sources":["../../src/lib/binary.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,YAAY,GAAI,OAAO,MAAM,KAAG,IAO5C,CAAC;AAEF,eAAO,MAAM,UAAU,GACtB,OAAO,IAAI,KACT,OAAO,CAAC;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,CAI9D,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
//#region src/lib/binary.ts
|
|
2
|
+
const bytesToBase64 = (bytes) => {
|
|
3
|
+
let binary = "";
|
|
4
|
+
for (const byte of bytes) binary += String.fromCharCode(byte);
|
|
5
|
+
return btoa(binary);
|
|
6
|
+
};
|
|
7
|
+
const decodeBase64 = (value) => {
|
|
8
|
+
const binary = atob(value);
|
|
9
|
+
const bytes = new Uint8Array(binary.length);
|
|
10
|
+
for (let index = 0; index < binary.length; index += 1) bytes[index] = binary.charCodeAt(index);
|
|
11
|
+
return new Blob([bytes]);
|
|
12
|
+
};
|
|
13
|
+
const encodeBlob = async (value) => ({
|
|
14
|
+
base64: bytesToBase64(new Uint8Array(await value.arrayBuffer())),
|
|
15
|
+
contentType: value.type,
|
|
16
|
+
size: value.size
|
|
17
|
+
});
|
|
18
|
+
//#endregion
|
|
19
|
+
export { decodeBase64, encodeBlob };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
import type { NeonTool, NeonToolExecutionContext, NeonToolResult } from "./operation.js";
|
|
3
|
+
export type NeonToolInjectValue = string | (() => string | undefined | Promise<string | undefined>);
|
|
4
|
+
export interface NeonToolInjectOptions {
|
|
5
|
+
projectId?: NeonToolInjectValue;
|
|
6
|
+
branchId?: NeonToolInjectValue;
|
|
7
|
+
omitFromSchema?: boolean;
|
|
8
|
+
}
|
|
9
|
+
export interface NeonToolDescriptionSource {
|
|
10
|
+
operationId: string;
|
|
11
|
+
id: string;
|
|
12
|
+
title: string;
|
|
13
|
+
description: string;
|
|
14
|
+
}
|
|
15
|
+
export type NeonToolDescriptionOverrides = Readonly<Record<string, string>> | ((tool: NeonToolDescriptionSource) => string);
|
|
16
|
+
export type NeonToolNameSource = Pick<NeonToolDescriptionSource, "operationId" | "id">;
|
|
17
|
+
export type NeonToolNameOverrides = Readonly<Record<string, string>> | ((tool: NeonToolNameSource) => string);
|
|
18
|
+
export interface NeonToolOnExecuteEvent<Output = unknown> {
|
|
19
|
+
operationId: string;
|
|
20
|
+
id: string;
|
|
21
|
+
input: unknown;
|
|
22
|
+
execute: () => Promise<NeonToolResult<Output>>;
|
|
23
|
+
}
|
|
24
|
+
export type NeonToolOnExecute = <Output>(event: NeonToolOnExecuteEvent<Output>) => Promise<NeonToolResult<Output>>;
|
|
25
|
+
export interface NeonToolCustomizeOptions {
|
|
26
|
+
descriptions?: NeonToolDescriptionOverrides;
|
|
27
|
+
names?: NeonToolNameOverrides;
|
|
28
|
+
name?: (id: string) => string;
|
|
29
|
+
onExecute?: NeonToolOnExecute;
|
|
30
|
+
inject?: NeonToolInjectOptions;
|
|
31
|
+
}
|
|
32
|
+
type InjectedPathKey<Inject> = ("projectId" extends keyof Inject ? Inject["projectId"] extends undefined ? never : "project_id" : never) | ("branchId" extends keyof Inject ? Inject["branchId"] extends undefined ? never : "branch_id" : never);
|
|
33
|
+
type RequiredKeys<Input> = {
|
|
34
|
+
[Key in keyof Input]-?: object extends Pick<Input, Key> ? never : Key;
|
|
35
|
+
}[keyof Input];
|
|
36
|
+
type InjectedRequiredKey<Input, Inject> = Extract<InjectedPathKey<Inject>, RequiredKeys<Input>>;
|
|
37
|
+
type ApplyInjectToInput<Input, Inject> = [
|
|
38
|
+
InjectedRequiredKey<Input, Inject>
|
|
39
|
+
] extends [never] ? Input : Inject extends {
|
|
40
|
+
omitFromSchema: true;
|
|
41
|
+
} ? Omit<Input, InjectedRequiredKey<Input, Inject>> : Omit<Input, InjectedRequiredKey<Input, Inject>> & Partial<Pick<Input, Extract<InjectedRequiredKey<Input, Inject>, keyof Input>>>;
|
|
42
|
+
export type InjectedNeonTool<Tool, Inject> = Tool extends NeonTool<infer Schema, string, infer Output> ? Omit<Tool, "execute"> & {
|
|
43
|
+
execute(input: ApplyInjectToInput<z.input<Schema>, Inject>, context?: NeonToolExecutionContext): Promise<NeonToolResult<Output>>;
|
|
44
|
+
} : Tool;
|
|
45
|
+
export declare const pathTemplateParams: (path: string) => Set<string>;
|
|
46
|
+
export declare const assertToolCustomizeOptions: (options: NeonToolCustomizeOptions) => void;
|
|
47
|
+
export declare const hasToolCustomization: (options: NeonToolCustomizeOptions) => boolean;
|
|
48
|
+
export declare const applyToolCustomization: <Tool extends NeonTool>(tool: Tool, options: NeonToolCustomizeOptions) => Omit<Tool, "id"> & {
|
|
49
|
+
id: string;
|
|
50
|
+
};
|
|
51
|
+
export {};
|
|
52
|
+
//# sourceMappingURL=customize.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"customize.d.ts","sourceRoot":"","sources":["../../src/lib/customize.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,KAAK,CAAC;AACzB,OAAO,KAAK,EACX,QAAQ,EACR,wBAAwB,EACxB,cAAc,EACd,MAAM,gBAAgB,CAAC;AAExB,MAAM,MAAM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CAAC,CACxD,CACD,CAAC;AAEN,MAAM,MAAM,gBAAgB,CAAC,IAAI,EAAE,MAAM,IACxC,IAAI,SAAS,QAAQ,CAAC,MAAM,MAAM,EAAE,MAAM,EAAE,MAAM,MAAM,CAAC,GACtD,IAAI,CAAC,IAAI,EAAE,SAAS,CAAC,GAAG;IACxB,OAAO,CACN,KAAK,EAAE,kBAAkB,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,EAClD,OAAO,CAAC,EAAE,wBAAwB,GAChC,OAAO,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC,CAAC;CACnC,GACA,IAAI,CAAC;AAsBT,eAAO,MAAM,kBAAkB,GAAI,MAAM,MAAM,KAAG,GAAG,CAAC,MAAM,CACU,CAAC;AAEvE,eAAO,MAAM,0BAA0B,GACtC,SAAS,wBAAwB,KAC/B,IAgBF,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAChC,SAAS,wBAAwB,KAC/B,OAKkC,CAAC;AAEtC,eAAO,MAAM,sBAAsB,GAAI,IAAI,SAAS,QAAQ,EAC3D,MAAM,IAAI,EACV,SAAS,wBAAwB,KAC/B,IAAI,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG;IAAE,EAAE,EAAE,MAAM,CAAA;CAoBjC,CAAC"}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import * as z from "zod";
|
|
2
|
+
//#region src/lib/customize.ts
|
|
3
|
+
const PATH_INJECT = [["projectId", "project_id"], ["branchId", "branch_id"]];
|
|
4
|
+
const PUBLISHED_ID = /^[a-z][a-z0-9_]*$/;
|
|
5
|
+
const missingInjectMessage = (name) => `A ${name} inject value is required`;
|
|
6
|
+
const isPlainObject = (value) => typeof value === "object" && value !== null && !Array.isArray(value);
|
|
7
|
+
const pathTemplateParams = (path) => new Set([...path.matchAll(/\{([^{}]+)\}/g)].map((match) => match[1]));
|
|
8
|
+
const assertToolCustomizeOptions = (options) => {
|
|
9
|
+
resolveInject(options.inject);
|
|
10
|
+
if (options.name !== void 0 && typeof options.name !== "function") throw new TypeError("Neon tool name transforms must be functions");
|
|
11
|
+
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
|
+
};
|
|
13
|
+
const hasToolCustomization = (options) => options.descriptions !== void 0 || options.names !== void 0 || options.name !== void 0 || options.onExecute !== void 0 || resolveInject(options.inject).hasAny;
|
|
14
|
+
const applyToolCustomization = (tool, options) => {
|
|
15
|
+
const inject = resolveInject(options.inject);
|
|
16
|
+
const id = publishedId(tool, options);
|
|
17
|
+
const description = applyDescription(tool, options.descriptions);
|
|
18
|
+
const inputSchema = publishInputSchema(tool.inputSchema, inject, pathTemplateParams(tool.metadata.path));
|
|
19
|
+
const wrapExecute = options.onExecute !== void 0 || inject.hasAny;
|
|
20
|
+
return {
|
|
21
|
+
...tool,
|
|
22
|
+
id,
|
|
23
|
+
description,
|
|
24
|
+
inputSchema,
|
|
25
|
+
execute: wrapExecute ? wrapToolExecute({
|
|
26
|
+
...tool,
|
|
27
|
+
id
|
|
28
|
+
}, options.onExecute, inject) : tool.execute
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
function publishedId(tool, options) {
|
|
32
|
+
let id = tool.id;
|
|
33
|
+
if (options.names !== void 0) {
|
|
34
|
+
if (typeof options.names === "function") {
|
|
35
|
+
id = options.names({
|
|
36
|
+
operationId: tool.operationId,
|
|
37
|
+
id: tool.id
|
|
38
|
+
});
|
|
39
|
+
if (typeof id !== "string") throw new TypeError(`Neon tool name overrides must return a string for ${tool.operationId}`);
|
|
40
|
+
} else {
|
|
41
|
+
const override = options.names[tool.operationId] ?? options.names[tool.id];
|
|
42
|
+
if (override !== void 0) id = override;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
if (options.name !== void 0) id = options.name(id);
|
|
46
|
+
if (typeof id !== "string" || !PUBLISHED_ID.test(id)) throw new TypeError(`Neon tool id must match ${PUBLISHED_ID} for ${tool.operationId}: received ${JSON.stringify(id)}`);
|
|
47
|
+
return id;
|
|
48
|
+
}
|
|
49
|
+
function resolveInject(inject) {
|
|
50
|
+
if (inject === void 0) return {
|
|
51
|
+
omitFromSchema: false,
|
|
52
|
+
hasAny: false
|
|
53
|
+
};
|
|
54
|
+
const projectId = requireInjectValue("projectId", inject.projectId);
|
|
55
|
+
const branchId = requireInjectValue("branchId", inject.branchId);
|
|
56
|
+
const hasAny = projectId !== void 0 || branchId !== void 0;
|
|
57
|
+
const omitFromSchema = inject.omitFromSchema === true;
|
|
58
|
+
if (omitFromSchema && !hasAny) throw new TypeError("omitFromSchema requires inject.projectId or inject.branchId");
|
|
59
|
+
return {
|
|
60
|
+
projectId,
|
|
61
|
+
branchId,
|
|
62
|
+
omitFromSchema,
|
|
63
|
+
hasAny
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
function requireInjectValue(name, value) {
|
|
67
|
+
if (value === void 0) return;
|
|
68
|
+
if (typeof value === "function") return value;
|
|
69
|
+
if (typeof value === "string") {
|
|
70
|
+
if (value.length === 0) throw new TypeError(missingInjectMessage(name));
|
|
71
|
+
return value;
|
|
72
|
+
}
|
|
73
|
+
throw new TypeError(missingInjectMessage(name));
|
|
74
|
+
}
|
|
75
|
+
function applyDescription(tool, descriptions) {
|
|
76
|
+
if (descriptions === void 0) return tool.description;
|
|
77
|
+
if (typeof descriptions === "function") {
|
|
78
|
+
const description = descriptions({
|
|
79
|
+
operationId: tool.operationId,
|
|
80
|
+
id: tool.id,
|
|
81
|
+
title: tool.title,
|
|
82
|
+
description: tool.description
|
|
83
|
+
});
|
|
84
|
+
if (typeof description !== "string") throw new TypeError("Neon tool description overrides must return a string");
|
|
85
|
+
return description;
|
|
86
|
+
}
|
|
87
|
+
const description = descriptions[tool.operationId] ?? descriptions[tool.id];
|
|
88
|
+
if (description === void 0) return tool.description;
|
|
89
|
+
if (typeof description !== "string") throw new TypeError("Neon tool description overrides must be strings");
|
|
90
|
+
return description;
|
|
91
|
+
}
|
|
92
|
+
function wrapToolExecute(tool, onExecute, inject) {
|
|
93
|
+
const originalExecute = tool.execute.bind(tool);
|
|
94
|
+
const availableKeys = pathTemplateParams(tool.metadata.path);
|
|
95
|
+
const mode = inject.omitFromSchema ? "override" : "fill";
|
|
96
|
+
return async (input, context) => {
|
|
97
|
+
const run = async () => originalExecute(mergeFields(input, await resolveInjectedFields(input, inject, availableKeys, mode), mode), context);
|
|
98
|
+
if (onExecute === void 0) return run();
|
|
99
|
+
return onExecute({
|
|
100
|
+
operationId: tool.operationId,
|
|
101
|
+
id: tool.id,
|
|
102
|
+
input: cloneInput(input),
|
|
103
|
+
execute: run
|
|
104
|
+
});
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
async function resolveInjectedFields(input, inject, availableKeys, mode) {
|
|
108
|
+
const present = isPlainObject(input) ? input : void 0;
|
|
109
|
+
const patch = {};
|
|
110
|
+
for (const [name, pathKey] of PATH_INJECT) {
|
|
111
|
+
if (!availableKeys.has(pathKey)) continue;
|
|
112
|
+
const injector = inject[name];
|
|
113
|
+
if (injector === void 0) continue;
|
|
114
|
+
if (mode === "fill" && present?.[pathKey] !== void 0) continue;
|
|
115
|
+
const value = typeof injector === "function" ? await injector() : injector;
|
|
116
|
+
if (value === void 0) {
|
|
117
|
+
if (mode === "override") throw new TypeError(missingInjectMessage(name));
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
if (typeof value !== "string" || value.length === 0) throw new TypeError(missingInjectMessage(name));
|
|
121
|
+
patch[pathKey] = value;
|
|
122
|
+
}
|
|
123
|
+
return patch;
|
|
124
|
+
}
|
|
125
|
+
function mergeFields(input, patch, mode) {
|
|
126
|
+
if (Object.keys(patch).length === 0) return input === void 0 ? {} : input;
|
|
127
|
+
if (input === void 0) return patch;
|
|
128
|
+
if (!isPlainObject(input)) return input;
|
|
129
|
+
const next = { ...input };
|
|
130
|
+
for (const [key, value] of Object.entries(patch)) if (mode === "override" || next[key] === void 0) next[key] = value;
|
|
131
|
+
return next;
|
|
132
|
+
}
|
|
133
|
+
function cloneInput(input) {
|
|
134
|
+
if (!isPlainObject(input)) return input;
|
|
135
|
+
return { ...input };
|
|
136
|
+
}
|
|
137
|
+
function objectShape(schema) {
|
|
138
|
+
if (schema instanceof z.ZodOptional) return objectShape(schema.unwrap());
|
|
139
|
+
if (schema instanceof z.ZodObject) return schema.shape;
|
|
140
|
+
}
|
|
141
|
+
function publishInputSchema(schema, inject, pathKeys) {
|
|
142
|
+
if (!inject.hasAny) return schema;
|
|
143
|
+
const shape = objectShape(schema);
|
|
144
|
+
if (shape === void 0) return schema;
|
|
145
|
+
const nextShape = { ...shape };
|
|
146
|
+
let changed = false;
|
|
147
|
+
for (const [name, pathKey] of PATH_INJECT) {
|
|
148
|
+
if (inject[name] === void 0 || !pathKeys.has(pathKey)) continue;
|
|
149
|
+
if (nextShape[pathKey] === void 0) continue;
|
|
150
|
+
changed = true;
|
|
151
|
+
if (inject.omitFromSchema) delete nextShape[pathKey];
|
|
152
|
+
else nextShape[pathKey] = nextShape[pathKey].optional();
|
|
153
|
+
}
|
|
154
|
+
if (!changed) return schema;
|
|
155
|
+
return Object.keys(nextShape).length === 0 ? z.strictObject({}) : z.strictObject(nextShape);
|
|
156
|
+
}
|
|
157
|
+
//#endregion
|
|
158
|
+
export { applyToolCustomization, assertToolCustomizeOptions, hasToolCustomization, pathTemplateParams };
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function optionalGroup<Value extends Record<string, unknown>>(value: Value, required: true): Value;
|
|
2
|
+
export declare function optionalGroup<Value extends Record<string, unknown>>(value: Value, required: false): Value | undefined;
|
|
3
|
+
export declare function decodeBinaryFields(body: Record<string, unknown>, fields: readonly string[], decode: (value: string) => Blob): Record<string, unknown>;
|
|
4
|
+
export declare function decodeBinaryFields(body: Record<string, unknown> | undefined, fields: readonly string[], decode: (value: string) => Blob): Record<string, unknown> | undefined;
|
|
5
|
+
//# sourceMappingURL=envelope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envelope.d.ts","sourceRoot":"","sources":["../../src/lib/envelope.ts"],"names":[],"mappings":"AAYA,wBAAgB,aAAa,CAAC,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAClE,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,IAAI,GACZ,KAAK,CAAC;AACT,wBAAgB,aAAa,CAAC,KAAK,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAClE,KAAK,EAAE,KAAK,EACZ,QAAQ,EAAE,KAAK,GACb,KAAK,GAAG,SAAS,CAAC;AAYrB,wBAAgB,kBAAkB,CACjC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAC7B,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAC7B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAC3B,wBAAgB,kBAAkB,CACjC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,EACzC,MAAM,EAAE,SAAS,MAAM,EAAE,EACzB,MAAM,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,GAC7B,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,SAAS,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
//#region src/lib/envelope.ts
|
|
2
|
+
const defined = (value) => {
|
|
3
|
+
const next = {};
|
|
4
|
+
for (const [key, child] of Object.entries(value)) if (child !== void 0) next[key] = child;
|
|
5
|
+
return next;
|
|
6
|
+
};
|
|
7
|
+
function optionalGroup(value, required) {
|
|
8
|
+
const next = defined(value);
|
|
9
|
+
if (!required && Object.keys(next).length === 0) return;
|
|
10
|
+
return next;
|
|
11
|
+
}
|
|
12
|
+
function decodeBinaryFields(body, fields, decode) {
|
|
13
|
+
if (body === void 0) return;
|
|
14
|
+
const next = { ...body };
|
|
15
|
+
for (const field of fields) {
|
|
16
|
+
const value = next[field];
|
|
17
|
+
if (typeof value === "string") next[field] = decode(value);
|
|
18
|
+
}
|
|
19
|
+
return next;
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { decodeBinaryFields, optionalGroup };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { type Client } from "@neon/sdk/raw";
|
|
2
|
+
import type * as z from "zod";
|
|
3
|
+
import { type NeonBearerCredential } from "./auth.js";
|
|
4
|
+
export interface NeonToolAnnotations {
|
|
5
|
+
readOnlyHint: boolean;
|
|
6
|
+
destructiveHint?: boolean;
|
|
7
|
+
idempotentHint?: boolean;
|
|
8
|
+
openWorldHint: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface NeonToolExecutionContext {
|
|
11
|
+
signal?: AbortSignal;
|
|
12
|
+
apiKey?: NeonBearerCredential;
|
|
13
|
+
}
|
|
14
|
+
export interface NeonToolMetadata {
|
|
15
|
+
method: string;
|
|
16
|
+
path: string;
|
|
17
|
+
stability: string;
|
|
18
|
+
deprecated: boolean;
|
|
19
|
+
tags: readonly string[];
|
|
20
|
+
}
|
|
21
|
+
export interface JsonSafeBlob {
|
|
22
|
+
base64: string;
|
|
23
|
+
contentType: string;
|
|
24
|
+
size: number;
|
|
25
|
+
}
|
|
26
|
+
export type JsonSafe<Value> = unknown extends Value ? unknown : Value extends null ? null : Value extends void ? null : Value extends bigint | Date ? string : Value extends Blob ? JsonSafeBlob : Value extends string | number | boolean ? Value : Value extends readonly (infer Item)[] ? JsonSafe<Item>[] : Value extends object ? {
|
|
27
|
+
[Key in keyof Value]: JsonSafe<Value[Key]>;
|
|
28
|
+
} : unknown;
|
|
29
|
+
export interface NeonToolResult<Data = unknown> {
|
|
30
|
+
data: Data;
|
|
31
|
+
}
|
|
32
|
+
export interface NeonTool<InputSchema extends z.ZodType = z.ZodType, Id extends string = string, Output = unknown> {
|
|
33
|
+
operationId: string;
|
|
34
|
+
id: Id;
|
|
35
|
+
title: string;
|
|
36
|
+
description: string;
|
|
37
|
+
inputSchema: InputSchema;
|
|
38
|
+
annotations: NeonToolAnnotations;
|
|
39
|
+
requiresApproval: boolean;
|
|
40
|
+
metadata: NeonToolMetadata;
|
|
41
|
+
execute(input: z.input<InputSchema>, context?: NeonToolExecutionContext): Promise<NeonToolResult<Output>>;
|
|
42
|
+
}
|
|
43
|
+
export interface NeonOperation<InputSchema extends z.ZodType, Id extends string = string, Output = unknown> {
|
|
44
|
+
operationId: string;
|
|
45
|
+
id: Id;
|
|
46
|
+
title: string;
|
|
47
|
+
description: string;
|
|
48
|
+
inputSchema: InputSchema;
|
|
49
|
+
annotations: NeonToolAnnotations;
|
|
50
|
+
requiresApproval: boolean;
|
|
51
|
+
metadata: NeonToolMetadata;
|
|
52
|
+
invoke(client: Client, input: z.output<InputSchema>, signal?: AbortSignal): Promise<Output>;
|
|
53
|
+
}
|
|
54
|
+
export declare const defineOperation: <const InputSchema extends z.ZodType, const Id extends string, Output>(operation: NeonOperation<InputSchema, Id, Output>) => NeonOperation<InputSchema, Id, Output>;
|
|
55
|
+
export declare const bindOperation: <const InputSchema extends z.ZodType, const Id extends string, Output>(operation: NeonOperation<InputSchema, Id, Output>, client: Client) => NeonTool<InputSchema, Id, JsonSafe<Awaited<Output>>>;
|
|
56
|
+
//# sourceMappingURL=operation.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"operation.d.ts","sourceRoot":"","sources":["../../src/lib/operation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,MAAM,EAAgB,MAAM,eAAe,CAAC;AAC1D,OAAO,KAAK,KAAK,CAAC,MAAM,KAAK,CAAC;AAC9B,OAAO,EAAE,KAAK,oBAAoB,EAA2B,MAAM,WAAW,CAAC;AAG/E,MAAM,WAAW,mBAAmB;IACnC,YAAY,EAAE,OAAO,CAAC;IACtB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C,OAAO,EACnC,KAAK,CAAC,EAAE,SAAS,MAAM,EACvB,MAAM,EAEN,WAAW,aAAa,CAAC,WAAW,EAAE,EAAE,EAAE,MAAM,CAAC,EACjD,QAAQ,MAAM,KACZ,QAAQ,CAAC,WAAW,EAAE,EAAE,EAAE,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAsBpD,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { requireBearerCredential } from "./auth.js";
|
|
2
|
+
import { toToolResult } from "./result.js";
|
|
3
|
+
import { createClient } from "@neon/sdk/raw";
|
|
4
|
+
//#region src/lib/operation.ts
|
|
5
|
+
const defineOperation = (operation) => operation;
|
|
6
|
+
const bindOperation = (operation, client) => ({
|
|
7
|
+
operationId: operation.operationId,
|
|
8
|
+
id: operation.id,
|
|
9
|
+
title: operation.title,
|
|
10
|
+
description: operation.description,
|
|
11
|
+
inputSchema: operation.inputSchema,
|
|
12
|
+
annotations: operation.annotations,
|
|
13
|
+
requiresApproval: operation.requiresApproval,
|
|
14
|
+
metadata: operation.metadata,
|
|
15
|
+
async execute(input, context) {
|
|
16
|
+
const parsed = await operation.inputSchema.parseAsync(input);
|
|
17
|
+
const activeClient = context !== void 0 && "apiKey" in context ? clientWithCredential(client, context.apiKey) : client;
|
|
18
|
+
const result = await operation.invoke(activeClient, parsed, context?.signal);
|
|
19
|
+
return toToolResult(result);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
const clientWithCredential = (client, apiKey) => {
|
|
23
|
+
if (apiKey === void 0) throw new TypeError("A Neon API key or OAuth access token is required");
|
|
24
|
+
return createClient({
|
|
25
|
+
...client.getConfig(),
|
|
26
|
+
auth: requireBearerCredential(apiKey)
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
//#endregion
|
|
30
|
+
export { bindOperation, defineOperation };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/lib/result.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAsC/D,eAAO,MAAM,YAAY,GAAU,KAAK,EACvC,OAAO,KAAK,KACV,OAAO,CAAC,cAAc,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,CAExC,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { encodeBlob } from "./binary.js";
|
|
2
|
+
//#region src/lib/result.ts
|
|
3
|
+
async function toJsonSafe(value) {
|
|
4
|
+
if (value === void 0 || value === null) return null;
|
|
5
|
+
if (typeof value === "bigint") return value.toString();
|
|
6
|
+
if (typeof value === "string" || typeof value === "boolean" || typeof value === "number") return value;
|
|
7
|
+
if (value instanceof Blob) return encodeBlob(value);
|
|
8
|
+
if (value instanceof Date) return value.toISOString();
|
|
9
|
+
if (Array.isArray(value)) return Promise.all(value.map(toJsonSafe));
|
|
10
|
+
if (typeof value === "object") {
|
|
11
|
+
const entries = await Promise.all(Object.entries(value).map(async ([key, child]) => [key, await toJsonSafe(child)]));
|
|
12
|
+
return Object.fromEntries(entries);
|
|
13
|
+
}
|
|
14
|
+
throw new TypeError(`Unsupported tool result value: ${typeof value}`);
|
|
15
|
+
}
|
|
16
|
+
const toToolResult = async (value) => ({ data: await toJsonSafe(value) });
|
|
17
|
+
//#endregion
|
|
18
|
+
export { toToolResult };
|
package/dist/mastra.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type * as z from "zod";
|
|
2
|
+
import type { NeonTool, NeonToolExecutionContext, NeonToolResult } from "./lib/operation.js";
|
|
3
|
+
export interface MastraToolContext {
|
|
4
|
+
abortSignal?: AbortSignal;
|
|
5
|
+
}
|
|
6
|
+
export type MastraToolConfig<Tool extends NeonTool> = {
|
|
7
|
+
id: Tool["id"];
|
|
8
|
+
description: string;
|
|
9
|
+
inputSchema: Tool["inputSchema"];
|
|
10
|
+
requireApproval: boolean;
|
|
11
|
+
execute(input: Parameters<Tool["execute"]>[0], context: MastraToolContext): ReturnType<Tool["execute"]>;
|
|
12
|
+
};
|
|
13
|
+
export type MastraTools<Tools extends Readonly<Record<string, NeonTool>>> = {
|
|
14
|
+
[Tool in Tools[keyof Tools] as Tool["id"]]: MastraToolConfig<Tool>;
|
|
15
|
+
};
|
|
16
|
+
type MastraToolSource = {
|
|
17
|
+
id: string;
|
|
18
|
+
description: string;
|
|
19
|
+
inputSchema: z.ZodType;
|
|
20
|
+
requiresApproval: boolean;
|
|
21
|
+
execute: (input: never, context?: NeonToolExecutionContext) => Promise<NeonToolResult<unknown>>;
|
|
22
|
+
};
|
|
23
|
+
export declare const toMastraTool: <const Tool extends MastraToolSource>(tool: Tool) => {
|
|
24
|
+
id: string;
|
|
25
|
+
description: string;
|
|
26
|
+
inputSchema: z.ZodType<unknown, unknown, z.core.$ZodTypeInternals<unknown, unknown>>;
|
|
27
|
+
requireApproval: boolean;
|
|
28
|
+
execute: (input: Parameters<Tool["execute"]>[0], context: MastraToolContext) => ReturnType<Tool["execute"]>;
|
|
29
|
+
};
|
|
30
|
+
export declare const toMastraTools: <const Tools extends Readonly<Record<string, NeonTool>>>(tools: Tools) => MastraTools<Tools>;
|
|
31
|
+
export {};
|
|
32
|
+
//# sourceMappingURL=mastra.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mastra.d.ts","sourceRoot":"","sources":["../src/mastra.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,CAAC,MAAM,KAAK,CAAC;AAC9B,OAAO,KAAK,EACX,QAAQ,EACR,wBAAwB,EACxB,cAAc,EACd,MAAM,oBAAoB,CAAC;AAE5B,MAAM,WAAW,iBAAiB;IACjC,WAAW,CAAC,EAAE,WAAW,CAAC;CAC1B;AAED,MAAM,MAAM,gBAAgB,CAAC,IAAI,SAAS,QAAQ,IAAI;IACrD,EAAE,EAAE,IAAI,CAAC,IAAI,CAAC,CAAC;IACf,WAAW,EAAE,MAAM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 +1,19 @@
|
|
|
1
|
+
//#region src/mastra.ts
|
|
2
|
+
const toMastraTool = (tool) => ({
|
|
3
|
+
id: tool.id,
|
|
4
|
+
description: tool.description,
|
|
5
|
+
inputSchema: tool.inputSchema,
|
|
6
|
+
requireApproval: tool.requiresApproval,
|
|
7
|
+
execute: (input, context) => tool.execute(input, { signal: context.abortSignal })
|
|
8
|
+
});
|
|
9
|
+
function assertMastraTools(value, tools) {
|
|
10
|
+
if (typeof value !== "object" || value === null) throw new TypeError("Expected Mastra tools to be an object.");
|
|
11
|
+
for (const tool of Object.values(tools)) if (!(tool.id in value)) throw new TypeError(`Missing Mastra tool "${tool.id}".`);
|
|
12
|
+
}
|
|
13
|
+
const toMastraTools = (tools) => {
|
|
14
|
+
const adapted = Object.fromEntries(Object.values(tools).map((tool) => [tool.id, toMastraTool(tool)]));
|
|
15
|
+
assertMastraTools(adapted, tools);
|
|
16
|
+
return adapted;
|
|
17
|
+
};
|
|
18
|
+
//#endregion
|
|
19
|
+
export { toMastraTool, toMastraTools };
|
package/dist/mcp-v1.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-v1.d.ts","sourceRoot":"","sources":["../src/mcp-v1.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,aAAa,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC"}
|
package/dist/mcp-v1.js
ADDED