veryfront 0.1.322 → 0.1.324

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.
@@ -0,0 +1,92 @@
1
+ import * as dntShim from "../../_dnt.shims.js";
2
+ import { z } from "zod";
3
+ import { dynamicTool, tool } from "./factory.js";
4
+ import type { JsonSchema } from "./schema/json-schema.js";
5
+ import type { Tool, ToolConfig, ToolExecutionContext } from "./types.js";
6
+
7
+ export interface HostToolDefinition {
8
+ description: string;
9
+ inputSchema: z.ZodSchema<unknown>;
10
+ inputSchemaJson?: JsonSchema;
11
+ execute: (input: unknown, context: ToolExecutionContext) => Promise<unknown> | unknown;
12
+ mcp?: ToolConfig["mcp"];
13
+ }
14
+
15
+ export interface HostToolMaterializationOptions {
16
+ generateToolCallId?: (toolName: string) => string;
17
+ }
18
+
19
+ function isRecord(value: unknown): value is Record<string, unknown> {
20
+ return typeof value === "object" && value !== null && !Array.isArray(value);
21
+ }
22
+
23
+ function isZodSchema(value: unknown): value is z.ZodSchema<unknown> {
24
+ if (!isRecord(value) || typeof value.parse !== "function") return false;
25
+ if (!isRecord(value._def)) return false;
26
+ return typeof value._def.typeName === "string" || typeof value._def.type === "string";
27
+ }
28
+
29
+ function isHostToolDefinition(value: unknown): value is HostToolDefinition {
30
+ return (
31
+ isRecord(value) &&
32
+ typeof value.description === "string" &&
33
+ isZodSchema(value.inputSchema) &&
34
+ typeof value.execute === "function"
35
+ );
36
+ }
37
+
38
+ function defaultToolCallId(toolName: string): string {
39
+ return `${toolName}-${dntShim.crypto.randomUUID()}`;
40
+ }
41
+
42
+ function normalizeExecutionContext(
43
+ toolName: string,
44
+ context: ToolExecutionContext | undefined,
45
+ options: HostToolMaterializationOptions,
46
+ ): ToolExecutionContext {
47
+ const toolCallId = typeof context?.toolCallId === "string" && context.toolCallId.length > 0
48
+ ? context.toolCallId
49
+ : (options.generateToolCallId ?? defaultToolCallId)(toolName);
50
+
51
+ return {
52
+ ...(isRecord(context) ? context : {}),
53
+ toolCallId,
54
+ };
55
+ }
56
+
57
+ export function createToolsFromHostDefinitions(
58
+ definitions: Record<string, unknown>,
59
+ options: HostToolMaterializationOptions = {},
60
+ ): Record<string, Tool<unknown, unknown>> {
61
+ const tools: Record<string, Tool<unknown, unknown>> = {};
62
+
63
+ for (const [toolName, definition] of Object.entries(definitions)) {
64
+ if (!isHostToolDefinition(definition)) continue;
65
+
66
+ const execute = async (input: unknown, context: ToolExecutionContext | undefined) =>
67
+ await definition.execute(input, normalizeExecutionContext(toolName, context, options));
68
+
69
+ try {
70
+ tools[toolName] = definition.inputSchemaJson
71
+ ? dynamicTool({
72
+ id: toolName,
73
+ description: definition.description,
74
+ inputSchema: definition.inputSchema,
75
+ inputSchemaJson: definition.inputSchemaJson,
76
+ execute,
77
+ mcp: definition.mcp,
78
+ })
79
+ : tool({
80
+ id: toolName,
81
+ description: definition.description,
82
+ inputSchema: definition.inputSchema,
83
+ execute,
84
+ mcp: definition.mcp,
85
+ });
86
+ } catch {
87
+ continue;
88
+ }
89
+ }
90
+
91
+ return tools;
92
+ }
@@ -60,6 +60,8 @@ export { createRemoteMCPToolSource } from "./remote-mcp.js";
60
60
  export type { RemoteMCPToolSourceConfig } from "./remote-mcp.js";
61
61
  export { createContext7ToolSource } from "./context7.js";
62
62
  export type { Context7ToolSourceConfig } from "./context7.js";
63
+ export { createToolsFromHostDefinitions } from "./host-tools.js";
64
+ export type { HostToolDefinition, HostToolMaterializationOptions } from "./host-tools.js";
63
65
  export {
64
66
  createToolsFromRemoteDefinitions,
65
67
  loadRemoteToolsFromSource,
@@ -1,3 +1,3 @@
1
1
  // Keep in sync with deno.json version.
2
2
  // scripts/release.ts updates this constant during releases.
3
- export const VERSION = "0.1.322";
3
+ export const VERSION = "0.1.324";