@yak-io/trpc 0.1.0 → 0.1.1
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/dist/caller.d.ts.map +1 -1
- package/dist/caller.js +4 -2
- package/package.json +8 -5
- package/dist/createNextChatbotConfigHandler.d.ts +0 -47
- package/dist/createNextChatbotConfigHandler.d.ts.map +0 -1
- package/dist/createNextChatbotConfigHandler.js +0 -49
- package/dist/createNextChatbotToolManifestHandler.d.ts +0 -44
- package/dist/createNextChatbotToolManifestHandler.d.ts.map +0 -1
- package/dist/createNextChatbotToolManifestHandler.js +0 -36
- package/dist/createNextChatbotToolsHandler.d.ts +0 -50
- package/dist/createNextChatbotToolsHandler.d.ts.map +0 -1
- package/dist/createNextChatbotToolsHandler.js +0 -126
package/dist/caller.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"caller.d.ts","sourceRoot":"","sources":["../src/caller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;
|
|
1
|
+
{"version":3,"file":"caller.d.ts","sourceRoot":"","sources":["../src/caller.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,KAAK,CAAC;AAyEnC;;;;GAIG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,OAAO,EACd,MAAM,EAAE,OAAO,GAAG,SAAS,GAC1B,OAAO,CA4DT;AAED;;GAEG;AACH,wBAAgB,kBAAkB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAEzD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,mBAAmB,CACjC,MAAM,EAAE,GAAG,EAAE,yDAAyD;AACtE,GAAG,EAAE,GAAG,GACP,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,CA+CpD"}
|
package/dist/caller.js
CHANGED
|
@@ -4,9 +4,11 @@
|
|
|
4
4
|
const FORBIDDEN_PROPS = new Set(["__proto__", "constructor", "prototype"]);
|
|
5
5
|
/**
|
|
6
6
|
* ISO 8601 date string pattern
|
|
7
|
-
* Matches:
|
|
7
|
+
* Matches:
|
|
8
|
+
* - Date only: 2024-01-15
|
|
9
|
+
* - Full datetime: 2024-01-15T10:30:00.000Z, 2024-01-15T10:30:00Z, 2024-01-15T10:30:00+00:00
|
|
8
10
|
*/
|
|
9
|
-
const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2})
|
|
11
|
+
const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}(?:T\d{2}:\d{2}:\d{2}(?:\.\d{3})?(?:Z|[+-]\d{2}:\d{2}))?$/;
|
|
10
12
|
/**
|
|
11
13
|
* Gets the Zod type name from a schema (supports both Zod 3 and Zod 4)
|
|
12
14
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@yak-io/trpc",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "tRPC adapter for yak chatbot - enables tRPC procedures as chatbot tools",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "SEE LICENSE IN LICENSE",
|
|
@@ -11,8 +11,7 @@
|
|
|
11
11
|
"directory": "packages/trpc"
|
|
12
12
|
},
|
|
13
13
|
"publishConfig": {
|
|
14
|
-
"access": "public"
|
|
15
|
-
"provenance": false
|
|
14
|
+
"access": "public"
|
|
16
15
|
},
|
|
17
16
|
"keywords": [
|
|
18
17
|
"yak",
|
|
@@ -29,15 +28,19 @@
|
|
|
29
28
|
"LICENSE"
|
|
30
29
|
],
|
|
31
30
|
"sideEffects": false,
|
|
31
|
+
"main": "./dist/index.js",
|
|
32
|
+
"module": "./dist/index.js",
|
|
33
|
+
"types": "./dist/index.d.ts",
|
|
32
34
|
"exports": {
|
|
33
35
|
".": {
|
|
34
36
|
"types": "./dist/index.d.ts",
|
|
35
|
-
"import": "./dist/index.js"
|
|
37
|
+
"import": "./dist/index.js",
|
|
38
|
+
"default": "./dist/index.js"
|
|
36
39
|
},
|
|
37
40
|
"./package.json": "./package.json"
|
|
38
41
|
},
|
|
39
42
|
"dependencies": {
|
|
40
|
-
"@yak-io/javascript": "0.1.
|
|
43
|
+
"@yak-io/javascript": "0.1.1"
|
|
41
44
|
},
|
|
42
45
|
"peerDependencies": {
|
|
43
46
|
"@trpc/server": "^10.0.0 || ^11.0.0",
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { AnyRouter } from "@trpc/server";
|
|
2
|
-
import type { RouteInfo } from "./types.js";
|
|
3
|
-
/**
|
|
4
|
-
* Configuration for the chat config handler
|
|
5
|
-
*/
|
|
6
|
-
export type NextChatbotConfigHandlerConfig<TRouter extends AnyRouter> = {
|
|
7
|
-
/**
|
|
8
|
-
* The tRPC router to introspect for tools
|
|
9
|
-
*/
|
|
10
|
-
router: TRouter;
|
|
11
|
-
/**
|
|
12
|
-
* List of allowed procedure paths for tools
|
|
13
|
-
*/
|
|
14
|
-
allowedProcedures: string[];
|
|
15
|
-
/**
|
|
16
|
-
* Function to get route information
|
|
17
|
-
* This should scan your Next.js app directory and return route info
|
|
18
|
-
*/
|
|
19
|
-
getRoutes: () => Promise<RouteInfo[]>;
|
|
20
|
-
};
|
|
21
|
-
/**
|
|
22
|
-
* Create a Next.js App Router handler that exposes combined chat configuration
|
|
23
|
-
*
|
|
24
|
-
* This handler combines both the tool manifest (tRPC procedures) and route manifest
|
|
25
|
-
* (Next.js routes) into a single endpoint for the chatbot to consume.
|
|
26
|
-
*
|
|
27
|
-
* @param config - Configuration object
|
|
28
|
-
* @returns A Next.js route handler function
|
|
29
|
-
*
|
|
30
|
-
* @example
|
|
31
|
-
* ```ts
|
|
32
|
-
* // app/api/chatbot/config/route.ts
|
|
33
|
-
* import { appRouter } from "@/server/trpc/router";
|
|
34
|
-
* import { createNextChatbotConfigHandler } from "@yak/trpc";
|
|
35
|
-
* import { scanRoutes } from "@yak/nextjs/server";
|
|
36
|
-
*
|
|
37
|
-
* export const GET = createNextChatbotConfigHandler({
|
|
38
|
-
* router: appRouter,
|
|
39
|
-
* allowedProcedures: ["orders.list", "user.updateProfile"],
|
|
40
|
-
* getRoutes: async () => {
|
|
41
|
-
* return scanRoutes("./src/app", false);
|
|
42
|
-
* },
|
|
43
|
-
* });
|
|
44
|
-
* ```
|
|
45
|
-
*/
|
|
46
|
-
export declare function createNextChatbotConfigHandler<TRouter extends AnyRouter>(config: NextChatbotConfigHandlerConfig<TRouter>): (_req: Request) => Promise<Response>;
|
|
47
|
-
//# sourceMappingURL=createNextChatbotConfigHandler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createNextChatbotConfigHandler.d.ts","sourceRoot":"","sources":["../src/createNextChatbotConfigHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAC9C,OAAO,KAAK,EAAc,SAAS,EAAE,MAAM,YAAY,CAAC;AAGxD;;GAEG;AACH,MAAM,MAAM,8BAA8B,CAAC,OAAO,SAAS,SAAS,IAAI;IACtE;;OAEG;IACH,MAAM,EAAE,OAAO,CAAC;IAEhB;;OAEG;IACH,iBAAiB,EAAE,MAAM,EAAE,CAAC;IAE5B;;;OAGG;IACH,SAAS,EAAE,MAAM,OAAO,CAAC,SAAS,EAAE,CAAC,CAAC;CACvC,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,8BAA8B,CAC5C,OAAO,SAAS,SAAS,EAEzB,MAAM,EAAE,8BAA8B,CAAC,OAAO,CAAC,IAG7C,MAAM,OAAO,KACZ,OAAO,CAAC,QAAQ,CAAC,CAwBrB"}
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { buildToolManifest } from "./introspect.js";
|
|
2
|
-
/**
|
|
3
|
-
* Create a Next.js App Router handler that exposes combined chat configuration
|
|
4
|
-
*
|
|
5
|
-
* This handler combines both the tool manifest (tRPC procedures) and route manifest
|
|
6
|
-
* (Next.js routes) into a single endpoint for the chatbot to consume.
|
|
7
|
-
*
|
|
8
|
-
* @param config - Configuration object
|
|
9
|
-
* @returns A Next.js route handler function
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* // app/api/chatbot/config/route.ts
|
|
14
|
-
* import { appRouter } from "@/server/trpc/router";
|
|
15
|
-
* import { createNextChatbotConfigHandler } from "@yak/trpc";
|
|
16
|
-
* import { scanRoutes } from "@yak/nextjs/server";
|
|
17
|
-
*
|
|
18
|
-
* export const GET = createNextChatbotConfigHandler({
|
|
19
|
-
* router: appRouter,
|
|
20
|
-
* allowedProcedures: ["orders.list", "user.updateProfile"],
|
|
21
|
-
* getRoutes: async () => {
|
|
22
|
-
* return scanRoutes("./src/app", false);
|
|
23
|
-
* },
|
|
24
|
-
* });
|
|
25
|
-
* ```
|
|
26
|
-
*/
|
|
27
|
-
export function createNextChatbotConfigHandler(config) {
|
|
28
|
-
return async function handleConfig(_req) {
|
|
29
|
-
const toolManifest = buildToolManifest(config.router, {
|
|
30
|
-
allowedProcedures: config.allowedProcedures,
|
|
31
|
-
});
|
|
32
|
-
const routes = await config.getRoutes();
|
|
33
|
-
const routeManifest = {
|
|
34
|
-
routes,
|
|
35
|
-
generated_at: new Date().toISOString(),
|
|
36
|
-
};
|
|
37
|
-
const chatConfig = {
|
|
38
|
-
tools: toolManifest,
|
|
39
|
-
routes: routeManifest,
|
|
40
|
-
};
|
|
41
|
-
return new Response(JSON.stringify(chatConfig), {
|
|
42
|
-
status: 200,
|
|
43
|
-
headers: {
|
|
44
|
-
"Content-Type": "application/json",
|
|
45
|
-
"Cache-Control": "no-store",
|
|
46
|
-
},
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
}
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { AnyRouter } from "@trpc/server";
|
|
2
|
-
/**
|
|
3
|
-
* Generic request type
|
|
4
|
-
*/
|
|
5
|
-
export type GenericRequest = {
|
|
6
|
-
json(): Promise<unknown>;
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Configuration for the tool manifest handler
|
|
10
|
-
*/
|
|
11
|
-
export type NextChatbotToolManifestHandlerConfig<TRouter extends AnyRouter> = {
|
|
12
|
-
/**
|
|
13
|
-
* The tRPC router to introspect
|
|
14
|
-
*/
|
|
15
|
-
router: TRouter;
|
|
16
|
-
/**
|
|
17
|
-
* List of allowed procedure paths, e.g. ["orders.list", "user.updateProfile"].
|
|
18
|
-
* Only these procedures will appear in the tool manifest.
|
|
19
|
-
*/
|
|
20
|
-
allowedProcedures: string[];
|
|
21
|
-
};
|
|
22
|
-
/**
|
|
23
|
-
* Create a Next.js App Router handler that exposes a tool manifest
|
|
24
|
-
*
|
|
25
|
-
* This handler introspects your tRPC router at runtime and returns a JSON manifest
|
|
26
|
-
* of tools (procedures) available to the chatbot, including their input/output schemas.
|
|
27
|
-
*
|
|
28
|
-
* @param config - Configuration object
|
|
29
|
-
* @returns A Next.js route handler function
|
|
30
|
-
*
|
|
31
|
-
* @example
|
|
32
|
-
* ```ts
|
|
33
|
-
* // app/api/chatbot/tools/manifest/route.ts
|
|
34
|
-
* import { appRouter } from "@/server/trpc/router";
|
|
35
|
-
* import { createNextChatbotToolManifestHandler } from "@yak/trpc";
|
|
36
|
-
*
|
|
37
|
-
* export const GET = createNextChatbotToolManifestHandler({
|
|
38
|
-
* router: appRouter,
|
|
39
|
-
* allowedProcedures: ["orders.list", "user.updateProfile"],
|
|
40
|
-
* });
|
|
41
|
-
* ```
|
|
42
|
-
*/
|
|
43
|
-
export declare function createNextChatbotToolManifestHandler<TRouter extends AnyRouter>(config: NextChatbotToolManifestHandlerConfig<TRouter>): (_req: GenericRequest) => Promise<Response>;
|
|
44
|
-
//# sourceMappingURL=createNextChatbotToolManifestHandler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createNextChatbotToolManifestHandler.d.ts","sourceRoot":"","sources":["../src/createNextChatbotToolManifestHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAI9C;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,oCAAoC,CAAC,OAAO,SAAS,SAAS,IAAI;IAC5E;;OAEG;IACH,MAAM,EAAE,OAAO,CAAC;IAEhB;;;OAGG;IACH,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC7B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,oCAAoC,CAClD,OAAO,SAAS,SAAS,EAEzB,MAAM,EAAE,oCAAoC,CAAC,OAAO,CAAC,IAGnD,MAAM,cAAc,KACnB,OAAO,CAAC,QAAQ,CAAC,CAarB"}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
import { buildToolManifest } from "./introspect.js";
|
|
2
|
-
/**
|
|
3
|
-
* Create a Next.js App Router handler that exposes a tool manifest
|
|
4
|
-
*
|
|
5
|
-
* This handler introspects your tRPC router at runtime and returns a JSON manifest
|
|
6
|
-
* of tools (procedures) available to the chatbot, including their input/output schemas.
|
|
7
|
-
*
|
|
8
|
-
* @param config - Configuration object
|
|
9
|
-
* @returns A Next.js route handler function
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```ts
|
|
13
|
-
* // app/api/chatbot/tools/manifest/route.ts
|
|
14
|
-
* import { appRouter } from "@/server/trpc/router";
|
|
15
|
-
* import { createNextChatbotToolManifestHandler } from "@yak/trpc";
|
|
16
|
-
*
|
|
17
|
-
* export const GET = createNextChatbotToolManifestHandler({
|
|
18
|
-
* router: appRouter,
|
|
19
|
-
* allowedProcedures: ["orders.list", "user.updateProfile"],
|
|
20
|
-
* });
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
export function createNextChatbotToolManifestHandler(config) {
|
|
24
|
-
return async function handleManifest(_req) {
|
|
25
|
-
const manifest = buildToolManifest(config.router, {
|
|
26
|
-
allowedProcedures: config.allowedProcedures,
|
|
27
|
-
});
|
|
28
|
-
return new Response(JSON.stringify(manifest), {
|
|
29
|
-
status: 200,
|
|
30
|
-
headers: {
|
|
31
|
-
"Content-Type": "application/json",
|
|
32
|
-
"Cache-Control": "no-store",
|
|
33
|
-
},
|
|
34
|
-
});
|
|
35
|
-
};
|
|
36
|
-
}
|
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
import type { AnyRouter } from "@trpc/server";
|
|
2
|
-
/**
|
|
3
|
-
* Generic request type that works with Next.js Request
|
|
4
|
-
*/
|
|
5
|
-
export type GenericRequest = {
|
|
6
|
-
json(): Promise<unknown>;
|
|
7
|
-
};
|
|
8
|
-
/**
|
|
9
|
-
* Configuration for the Next.js chatbot tools handler
|
|
10
|
-
*/
|
|
11
|
-
export type NextChatbotToolsHandlerConfig<TRouter extends AnyRouter, TContext, TRequest extends GenericRequest = GenericRequest> = {
|
|
12
|
-
/** tRPC router instance */
|
|
13
|
-
router: TRouter;
|
|
14
|
-
/** Function to create tRPC context from request */
|
|
15
|
-
createContext: (opts: {
|
|
16
|
-
req: TRequest;
|
|
17
|
-
}) => Promise<TContext>;
|
|
18
|
-
/**
|
|
19
|
-
* List of allowed procedure paths, e.g. ["orders.list", "user.updateProfile"].
|
|
20
|
-
* Only these procedures can be invoked as tools.
|
|
21
|
-
*/
|
|
22
|
-
allowedProcedures: string[];
|
|
23
|
-
};
|
|
24
|
-
/**
|
|
25
|
-
* Creates a Next.js App Router-compatible POST handler for chatbot tool calls.
|
|
26
|
-
*
|
|
27
|
-
* This handler:
|
|
28
|
-
* 1. Parses the tool call payload from the request
|
|
29
|
-
* 2. Validates the procedure name is in the allowlist
|
|
30
|
-
* 3. Creates a tRPC caller with the request context
|
|
31
|
-
* 4. Dynamically invokes the requested procedure
|
|
32
|
-
* 5. Returns the result or error as JSON
|
|
33
|
-
*
|
|
34
|
-
* @example
|
|
35
|
-
* ```ts
|
|
36
|
-
* // app/api/chatbot/tools/route.ts
|
|
37
|
-
* import { NextRequest } from "next/server";
|
|
38
|
-
* import { appRouter } from "@/server/trpc/router";
|
|
39
|
-
* import { createContext } from "@/server/trpc/context";
|
|
40
|
-
* import { createNextChatbotToolsHandler } from "@yak/trpc";
|
|
41
|
-
*
|
|
42
|
-
* export const POST = createNextChatbotToolsHandler({
|
|
43
|
-
* router: appRouter,
|
|
44
|
-
* createContext,
|
|
45
|
-
* allowedProcedures: ["orders.list", "user.updateProfile"],
|
|
46
|
-
* });
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
export declare function createNextChatbotToolsHandler<TRouter extends AnyRouter, TContext, TRequest extends GenericRequest = GenericRequest>(config: NextChatbotToolsHandlerConfig<TRouter, TContext, TRequest>): (req: TRequest) => Promise<Response>;
|
|
50
|
-
//# sourceMappingURL=createNextChatbotToolsHandler.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"createNextChatbotToolsHandler.d.ts","sourceRoot":"","sources":["../src/createNextChatbotToolsHandler.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AAK9C;;GAEG;AACH,MAAM,MAAM,cAAc,GAAG;IAC3B,IAAI,IAAI,OAAO,CAAC,OAAO,CAAC,CAAC;CAC1B,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,6BAA6B,CAAC,OAAO,SAAS,SAAS,EAAE,QAAQ,EAAE,QAAQ,SAAS,cAAc,GAAG,cAAc,IAAI;IACjI,2BAA2B;IAC3B,MAAM,EAAE,OAAO,CAAC;IAChB,mDAAmD;IACnD,aAAa,EAAE,CAAC,IAAI,EAAE;QAAE,GAAG,EAAE,QAAQ,CAAA;KAAE,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC;IAC9D;;;OAGG;IACH,iBAAiB,EAAE,MAAM,EAAE,CAAC;CAC7B,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,6BAA6B,CAC3C,OAAO,SAAS,SAAS,EACzB,QAAQ,EACR,QAAQ,SAAS,cAAc,GAAG,cAAc,EAEhD,MAAM,EAAE,6BAA6B,CAAC,OAAO,EAAE,QAAQ,EAAE,QAAQ,CAAC,GACjE,CAAC,GAAG,EAAE,QAAQ,KAAK,OAAO,CAAC,QAAQ,CAAC,CA8DtC"}
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { createDynamicCaller } from "./caller.js";
|
|
2
|
-
import { logger } from "./logger.js";
|
|
3
|
-
/**
|
|
4
|
-
* Creates a Next.js App Router-compatible POST handler for chatbot tool calls.
|
|
5
|
-
*
|
|
6
|
-
* This handler:
|
|
7
|
-
* 1. Parses the tool call payload from the request
|
|
8
|
-
* 2. Validates the procedure name is in the allowlist
|
|
9
|
-
* 3. Creates a tRPC caller with the request context
|
|
10
|
-
* 4. Dynamically invokes the requested procedure
|
|
11
|
-
* 5. Returns the result or error as JSON
|
|
12
|
-
*
|
|
13
|
-
* @example
|
|
14
|
-
* ```ts
|
|
15
|
-
* // app/api/chatbot/tools/route.ts
|
|
16
|
-
* import { NextRequest } from "next/server";
|
|
17
|
-
* import { appRouter } from "@/server/trpc/router";
|
|
18
|
-
* import { createContext } from "@/server/trpc/context";
|
|
19
|
-
* import { createNextChatbotToolsHandler } from "@yak/trpc";
|
|
20
|
-
*
|
|
21
|
-
* export const POST = createNextChatbotToolsHandler({
|
|
22
|
-
* router: appRouter,
|
|
23
|
-
* createContext,
|
|
24
|
-
* allowedProcedures: ["orders.list", "user.updateProfile"],
|
|
25
|
-
* });
|
|
26
|
-
* ```
|
|
27
|
-
*/
|
|
28
|
-
export function createNextChatbotToolsHandler(config) {
|
|
29
|
-
const { router, createContext, allowedProcedures } = config;
|
|
30
|
-
return async (req) => {
|
|
31
|
-
try {
|
|
32
|
-
// Parse request body
|
|
33
|
-
const body = await req.json();
|
|
34
|
-
// Validate payload structure
|
|
35
|
-
if (!isToolCallPayload(body)) {
|
|
36
|
-
logger.warn("Invalid tool call payload received");
|
|
37
|
-
return createErrorResponse("Invalid request payload", 400);
|
|
38
|
-
}
|
|
39
|
-
const { id, name, args } = body;
|
|
40
|
-
logger.debug(`Tool call received: ${name}`, { id, args });
|
|
41
|
-
// Check if procedure is allowed
|
|
42
|
-
if (!allowedProcedures.includes(name)) {
|
|
43
|
-
logger.warn(`Procedure not allowed: ${name}`);
|
|
44
|
-
return createToolErrorResponse(id, `Procedure '${name}' is not allowed`);
|
|
45
|
-
}
|
|
46
|
-
// Create tRPC context
|
|
47
|
-
const ctx = await createContext({ req });
|
|
48
|
-
// Create dynamic caller
|
|
49
|
-
const caller = createDynamicCaller(router, ctx);
|
|
50
|
-
// Invoke the procedure
|
|
51
|
-
const result = await caller(name, args);
|
|
52
|
-
logger.debug(`Tool call succeeded: ${name}`, { id });
|
|
53
|
-
// Return success response
|
|
54
|
-
const successResult = {
|
|
55
|
-
id,
|
|
56
|
-
ok: true,
|
|
57
|
-
result,
|
|
58
|
-
};
|
|
59
|
-
return new Response(JSON.stringify(successResult), {
|
|
60
|
-
status: 200,
|
|
61
|
-
headers: {
|
|
62
|
-
"Content-Type": "application/json",
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
logger.error("Tool call failed", error);
|
|
68
|
-
// Extract error details
|
|
69
|
-
const body = await req.json().catch(() => ({}));
|
|
70
|
-
const id = body.id ?? "unknown";
|
|
71
|
-
const errorMessage = extractErrorMessage(error);
|
|
72
|
-
return createToolErrorResponse(id, errorMessage);
|
|
73
|
-
}
|
|
74
|
-
};
|
|
75
|
-
}
|
|
76
|
-
/**
|
|
77
|
-
* Type guard to validate tool call payload
|
|
78
|
-
*/
|
|
79
|
-
function isToolCallPayload(payload) {
|
|
80
|
-
return (typeof payload === "object" &&
|
|
81
|
-
payload !== null &&
|
|
82
|
-
"id" in payload &&
|
|
83
|
-
typeof payload.id === "string" &&
|
|
84
|
-
"name" in payload &&
|
|
85
|
-
typeof payload.name === "string" &&
|
|
86
|
-
"args" in payload);
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Extract a safe error message from an unknown error
|
|
90
|
-
*/
|
|
91
|
-
function extractErrorMessage(error) {
|
|
92
|
-
if (error instanceof Error) {
|
|
93
|
-
return error.message;
|
|
94
|
-
}
|
|
95
|
-
if (typeof error === "string") {
|
|
96
|
-
return error;
|
|
97
|
-
}
|
|
98
|
-
return "An unknown error occurred";
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Create a JSON response for tool call errors
|
|
102
|
-
*/
|
|
103
|
-
function createToolErrorResponse(id, error) {
|
|
104
|
-
const errorResult = {
|
|
105
|
-
id,
|
|
106
|
-
ok: false,
|
|
107
|
-
error,
|
|
108
|
-
};
|
|
109
|
-
return new Response(JSON.stringify(errorResult), {
|
|
110
|
-
status: 200, // Return 200 with error payload so client can process it
|
|
111
|
-
headers: {
|
|
112
|
-
"Content-Type": "application/json",
|
|
113
|
-
},
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Create a generic HTTP error response
|
|
118
|
-
*/
|
|
119
|
-
function createErrorResponse(message, status) {
|
|
120
|
-
return new Response(JSON.stringify({ error: message }), {
|
|
121
|
-
status,
|
|
122
|
-
headers: {
|
|
123
|
-
"Content-Type": "application/json",
|
|
124
|
-
},
|
|
125
|
-
});
|
|
126
|
-
}
|