@vertesia/tools-sdk 0.24.0-dev.202601221707

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.
Files changed (144) hide show
  1. package/LICENSE +13 -0
  2. package/README.md +122 -0
  3. package/lib/cjs/InteractionCollection.js +164 -0
  4. package/lib/cjs/InteractionCollection.js.map +1 -0
  5. package/lib/cjs/SkillCollection.js +376 -0
  6. package/lib/cjs/SkillCollection.js.map +1 -0
  7. package/lib/cjs/ToolCollection.js +228 -0
  8. package/lib/cjs/ToolCollection.js.map +1 -0
  9. package/lib/cjs/ToolRegistry.js +111 -0
  10. package/lib/cjs/ToolRegistry.js.map +1 -0
  11. package/lib/cjs/auth.js +104 -0
  12. package/lib/cjs/auth.js.map +1 -0
  13. package/lib/cjs/build/validate.js +7 -0
  14. package/lib/cjs/build/validate.js.map +1 -0
  15. package/lib/cjs/copy-assets.js +84 -0
  16. package/lib/cjs/copy-assets.js.map +1 -0
  17. package/lib/cjs/index.js +31 -0
  18. package/lib/cjs/index.js.map +1 -0
  19. package/lib/cjs/package.json +3 -0
  20. package/lib/cjs/server/interactions.js +66 -0
  21. package/lib/cjs/server/interactions.js.map +1 -0
  22. package/lib/cjs/server/mcp.js +45 -0
  23. package/lib/cjs/server/mcp.js.map +1 -0
  24. package/lib/cjs/server/site.js +30 -0
  25. package/lib/cjs/server/site.js.map +1 -0
  26. package/lib/cjs/server/skills.js +114 -0
  27. package/lib/cjs/server/skills.js.map +1 -0
  28. package/lib/cjs/server/tools.js +104 -0
  29. package/lib/cjs/server/tools.js.map +1 -0
  30. package/lib/cjs/server/types.js +3 -0
  31. package/lib/cjs/server/types.js.map +1 -0
  32. package/lib/cjs/server/widgets.js +27 -0
  33. package/lib/cjs/server/widgets.js.map +1 -0
  34. package/lib/cjs/server.js +132 -0
  35. package/lib/cjs/server.js.map +1 -0
  36. package/lib/cjs/site/styles.js +621 -0
  37. package/lib/cjs/site/styles.js.map +1 -0
  38. package/lib/cjs/site/templates.js +968 -0
  39. package/lib/cjs/site/templates.js.map +1 -0
  40. package/lib/cjs/types.js +3 -0
  41. package/lib/cjs/types.js.map +1 -0
  42. package/lib/cjs/utils.js +31 -0
  43. package/lib/cjs/utils.js.map +1 -0
  44. package/lib/esm/InteractionCollection.js +125 -0
  45. package/lib/esm/InteractionCollection.js.map +1 -0
  46. package/lib/esm/SkillCollection.js +369 -0
  47. package/lib/esm/SkillCollection.js.map +1 -0
  48. package/lib/esm/ToolCollection.js +190 -0
  49. package/lib/esm/ToolCollection.js.map +1 -0
  50. package/lib/esm/ToolRegistry.js +106 -0
  51. package/lib/esm/ToolRegistry.js.map +1 -0
  52. package/lib/esm/auth.js +97 -0
  53. package/lib/esm/auth.js.map +1 -0
  54. package/lib/esm/build/validate.js +4 -0
  55. package/lib/esm/build/validate.js.map +1 -0
  56. package/lib/esm/copy-assets.js +81 -0
  57. package/lib/esm/copy-assets.js.map +1 -0
  58. package/lib/esm/index.js +11 -0
  59. package/lib/esm/index.js.map +1 -0
  60. package/lib/esm/server/interactions.js +63 -0
  61. package/lib/esm/server/interactions.js.map +1 -0
  62. package/lib/esm/server/mcp.js +42 -0
  63. package/lib/esm/server/mcp.js.map +1 -0
  64. package/lib/esm/server/site.js +27 -0
  65. package/lib/esm/server/site.js.map +1 -0
  66. package/lib/esm/server/skills.js +111 -0
  67. package/lib/esm/server/skills.js.map +1 -0
  68. package/lib/esm/server/tools.js +101 -0
  69. package/lib/esm/server/tools.js.map +1 -0
  70. package/lib/esm/server/types.js +2 -0
  71. package/lib/esm/server/types.js.map +1 -0
  72. package/lib/esm/server/widgets.js +24 -0
  73. package/lib/esm/server/widgets.js.map +1 -0
  74. package/lib/esm/server.js +128 -0
  75. package/lib/esm/server.js.map +1 -0
  76. package/lib/esm/site/styles.js +618 -0
  77. package/lib/esm/site/styles.js.map +1 -0
  78. package/lib/esm/site/templates.js +956 -0
  79. package/lib/esm/site/templates.js.map +1 -0
  80. package/lib/esm/types.js +2 -0
  81. package/lib/esm/types.js.map +1 -0
  82. package/lib/esm/utils.js +26 -0
  83. package/lib/esm/utils.js.map +1 -0
  84. package/lib/types/InteractionCollection.d.ts +48 -0
  85. package/lib/types/InteractionCollection.d.ts.map +1 -0
  86. package/lib/types/SkillCollection.d.ts +118 -0
  87. package/lib/types/SkillCollection.d.ts.map +1 -0
  88. package/lib/types/ToolCollection.d.ts +72 -0
  89. package/lib/types/ToolCollection.d.ts.map +1 -0
  90. package/lib/types/ToolRegistry.d.ts +41 -0
  91. package/lib/types/ToolRegistry.d.ts.map +1 -0
  92. package/lib/types/auth.d.ts +32 -0
  93. package/lib/types/auth.d.ts.map +1 -0
  94. package/lib/types/build/validate.d.ts +2 -0
  95. package/lib/types/build/validate.d.ts.map +1 -0
  96. package/lib/types/copy-assets.d.ts +14 -0
  97. package/lib/types/copy-assets.d.ts.map +1 -0
  98. package/lib/types/index.d.ts +11 -0
  99. package/lib/types/index.d.ts.map +1 -0
  100. package/lib/types/server/interactions.d.ts +4 -0
  101. package/lib/types/server/interactions.d.ts.map +1 -0
  102. package/lib/types/server/mcp.d.ts +4 -0
  103. package/lib/types/server/mcp.d.ts.map +1 -0
  104. package/lib/types/server/site.d.ts +4 -0
  105. package/lib/types/server/site.d.ts.map +1 -0
  106. package/lib/types/server/skills.d.ts +4 -0
  107. package/lib/types/server/skills.d.ts.map +1 -0
  108. package/lib/types/server/tools.d.ts +4 -0
  109. package/lib/types/server/tools.d.ts.map +1 -0
  110. package/lib/types/server/types.d.ts +62 -0
  111. package/lib/types/server/types.d.ts.map +1 -0
  112. package/lib/types/server/widgets.d.ts +9 -0
  113. package/lib/types/server/widgets.d.ts.map +1 -0
  114. package/lib/types/server.d.ts +27 -0
  115. package/lib/types/server.d.ts.map +1 -0
  116. package/lib/types/site/styles.d.ts +5 -0
  117. package/lib/types/site/styles.d.ts.map +1 -0
  118. package/lib/types/site/templates.d.ts +54 -0
  119. package/lib/types/site/templates.d.ts.map +1 -0
  120. package/lib/types/types.d.ts +280 -0
  121. package/lib/types/types.d.ts.map +1 -0
  122. package/lib/types/utils.d.ts +4 -0
  123. package/lib/types/utils.d.ts.map +1 -0
  124. package/package.json +58 -0
  125. package/src/InteractionCollection.ts +143 -0
  126. package/src/SkillCollection.ts +461 -0
  127. package/src/ToolCollection.ts +223 -0
  128. package/src/ToolRegistry.ts +135 -0
  129. package/src/auth.ts +123 -0
  130. package/src/build/validate.ts +3 -0
  131. package/src/copy-assets.ts +104 -0
  132. package/src/index.ts +12 -0
  133. package/src/server/interactions.ts +79 -0
  134. package/src/server/mcp.ts +51 -0
  135. package/src/server/site.ts +46 -0
  136. package/src/server/skills.ts +133 -0
  137. package/src/server/tools.ts +128 -0
  138. package/src/server/types.ts +65 -0
  139. package/src/server/widgets.ts +38 -0
  140. package/src/server.ts +160 -0
  141. package/src/site/styles.ts +617 -0
  142. package/src/site/templates.ts +994 -0
  143. package/src/types.ts +303 -0
  144. package/src/utils.ts +23 -0
@@ -0,0 +1,128 @@
1
+ import { Context, Hono } from "hono";
2
+ import { HTTPException } from "hono/http-exception";
3
+ import { ToolCollection } from "../ToolCollection.js";
4
+ import { ToolCollectionDefinition, ToolDefinition } from "../types.js";
5
+ import { ToolContext, ToolServerConfig } from "./types.js";
6
+
7
+ export function createToolsRoute(app: Hono, basePath: string, config: ToolServerConfig) {
8
+ const { tools = [] } = config;
9
+
10
+ // Build a map of tool name -> collection for routing
11
+ const toolToCollection = new Map<string, ToolCollection>();
12
+ for (const coll of tools) {
13
+ for (const toolDef of coll.getToolDefinitions()) {
14
+ toolToCollection.set(toolDef.name, coll);
15
+ }
16
+ }
17
+
18
+ // GET /api/tools - Returns all tools from all collections
19
+ // Query params:
20
+ // - defaultOnly=true: Only return tools with default !== false
21
+ // - unlocked=tool1,tool2: Comma-separated list of unlocked tool names
22
+
23
+ app.get(basePath, (c) => {
24
+ const url = new URL(c.req.url);
25
+ const defaultOnly = c.req.query('defaultOnly') === 'true';
26
+ const unlockedParam = c.req.query('unlocked');
27
+ const unlockedTools = unlockedParam ? unlockedParam.split(',').map(t => t.trim()).filter(Boolean) : [];
28
+
29
+ const filterOptions = defaultOnly ? { defaultOnly, unlockedTools } : undefined;
30
+
31
+ const allTools: ToolDefinition[] = [];
32
+ let reserveToolCount = 0;
33
+
34
+ for (const coll of tools) {
35
+ allTools.push(...coll.getToolDefinitions(filterOptions));
36
+ if (defaultOnly) {
37
+ reserveToolCount += coll.getReserveTools(unlockedTools).length;
38
+ }
39
+ }
40
+
41
+ return c.json({
42
+ src: `${url.origin}${url.pathname}`,
43
+ title: 'All Tools',
44
+ description: 'All available tools across all collections',
45
+ tools: allTools,
46
+ reserveToolCount: defaultOnly ? reserveToolCount : undefined,
47
+ collections: tools.map(t => ({
48
+ name: t.name,
49
+ title: t.title,
50
+ description: t.description,
51
+ })),
52
+ } satisfies ToolCollectionDefinition & { collections: any[]; reserveToolCount?: number });
53
+ });
54
+
55
+ // POST /api/tools - Route to the correct collection based on tool_name
56
+ app.post(basePath, async (c) => {
57
+ const ctx = c as unknown as ToolContext;
58
+
59
+ // Payload is already parsed and validated by middleware
60
+ if (!ctx.payload) {
61
+ throw new HTTPException(400, {
62
+ message: 'Invalid or missing tool execution payload. Expected { tool_use: { id, tool_name, tool_input? }, metadata? }'
63
+ });
64
+ }
65
+
66
+ const toolName = ctx.payload.tool_use.tool_name;
67
+
68
+ // Find the collection for this tool
69
+ const collection = toolToCollection.get(toolName);
70
+ if (!collection) {
71
+ throw new HTTPException(404, {
72
+ message: `Tool not found: ${toolName}. Available tools: ${Array.from(toolToCollection.keys()).join(', ')}`
73
+ });
74
+ }
75
+
76
+ // Delegate to the collection's execute method with pre-parsed payload
77
+ return collection.execute(c, ctx.payload);
78
+ });
79
+
80
+ // Create tool collection endpoints
81
+ for (const coll of tools) {
82
+ app.route(`${basePath}/${coll.name}`, createToolEndpoints(coll));
83
+ }
84
+ }
85
+
86
+ function createToolEndpoints(coll: ToolCollection): Hono {
87
+ const endpoint = new Hono();
88
+
89
+ endpoint.post('/', (c: Context) => {
90
+ return coll.execute(c);
91
+ });
92
+
93
+ // GET /api/tools/{collection}
94
+ // Query params:
95
+ // - import: Return import source URL instead of API URL
96
+ // - defaultOnly=true: Only return tools with default !== false
97
+ // - unlocked=tool1,tool2: Comma-separated list of unlocked tool names
98
+ endpoint.get('/', (c) => {
99
+ const importSourceUrl = c.req.query('import') != null;
100
+ const defaultOnly = c.req.query('defaultOnly') === 'true';
101
+ const unlockedParam = c.req.query('unlocked');
102
+ const unlockedTools = unlockedParam ? unlockedParam.split(',').map(t => t.trim()).filter(Boolean) : [];
103
+
104
+ const filterOptions = defaultOnly ? { defaultOnly, unlockedTools } : undefined;
105
+ const url = new URL(c.req.url);
106
+
107
+ const response: ToolCollectionDefinition & { reserveToolCount?: number } = {
108
+ src: importSourceUrl
109
+ ? `${url.origin}/libs/vertesia-tools-${coll.name}.js`
110
+ : `${url.origin}${url.pathname}`,
111
+ title: coll.title || coll.name,
112
+ description: coll.description || '',
113
+ tools: coll.getToolDefinitions(filterOptions)
114
+ };
115
+
116
+ // Include reserve count when filtering
117
+ if (defaultOnly) {
118
+ response.reserveToolCount = coll.getReserveTools(unlockedTools).length;
119
+ }
120
+
121
+ return c.json(response);
122
+ });
123
+
124
+ return endpoint;
125
+ }
126
+
127
+
128
+
@@ -0,0 +1,65 @@
1
+ import { Context } from "hono";
2
+ import { InteractionCollection } from "../InteractionCollection.js";
3
+ import { SkillCollection } from "../SkillCollection.js";
4
+ import { ToolCollection } from "../ToolCollection.js";
5
+ import { ToolExecutionPayload } from "../types.js";
6
+
7
+ /**
8
+ * Extended context with parsed payload for tool/skill execution
9
+ */
10
+ export interface ToolContext extends Context {
11
+ /** The parsed request payload */
12
+ payload?: ToolExecutionPayload<any>;
13
+ /** The tool_use.id from the payload */
14
+ toolUseId?: string;
15
+ /** The tool_use.tool_name from the payload */
16
+ toolName?: string;
17
+ }
18
+
19
+ /**
20
+ * MCP Provider interface for server configuration
21
+ */
22
+ export interface MCPProviderConfig {
23
+ name: string;
24
+ description?: string;
25
+ createMCPConnection: (session: any, config: Record<string, any>) => Promise<{
26
+ name: string;
27
+ url: string;
28
+ token: string;
29
+ }>;
30
+ }
31
+
32
+ /**
33
+ * Server configuration options
34
+ */
35
+ export interface ToolServerConfig {
36
+ /**
37
+ * Server title for HTML pages (default: 'Tools Server')
38
+ */
39
+ title?: string;
40
+ /**
41
+ * API prefix (default: '/api')
42
+ */
43
+ prefix?: string;
44
+ /**
45
+ * Tool collections to expose
46
+ */
47
+ tools?: ToolCollection[];
48
+ /**
49
+ * Interaction collections to expose
50
+ */
51
+ interactions?: InteractionCollection[];
52
+ /**
53
+ * Skill collections to expose
54
+ */
55
+ skills?: SkillCollection[];
56
+ /**
57
+ * MCP providers to expose
58
+ */
59
+ mcpProviders?: MCPProviderConfig[];
60
+ /**
61
+ * Disable HTML pages (default: false)
62
+ */
63
+ disableHtml?: boolean;
64
+ }
65
+
@@ -0,0 +1,38 @@
1
+ import { Hono } from "hono";
2
+ import { ToolServerConfig } from "./types.js";
3
+
4
+ export interface WidgetInfo {
5
+ collection: string;
6
+ skill: string;
7
+ url: string;
8
+ }
9
+
10
+ export function createWidgetsRoute(app: Hono, basePath: string, config: ToolServerConfig) {
11
+
12
+ const { skills = [] } = config;
13
+
14
+ // GET /api/widgets - Returns all widgets from all skill collections
15
+ app.get(basePath, (c) => {
16
+ const url = new URL(c.req.url);
17
+
18
+ const widgets: Record<string, WidgetInfo> = {};
19
+ for (const coll of skills) {
20
+ const collWidgets = coll.getWidgets();
21
+ for (const widget of collWidgets) {
22
+ widgets[widget.name] = {
23
+ collection: coll.name,
24
+ skill: widget.skill,
25
+ url: `${url.origin}/widgets/${widget.name}.js`,
26
+ }
27
+ }
28
+ }
29
+
30
+ return c.json({
31
+ title: 'All Widgets',
32
+ description: 'All available widgets across all skill collections',
33
+ widgets,
34
+ });
35
+
36
+ });
37
+
38
+ }
package/src/server.ts ADDED
@@ -0,0 +1,160 @@
1
+ import { Context, Hono } from "hono";
2
+ import { cors } from "hono/cors";
3
+ import { HTTPException } from "hono/http-exception";
4
+ import { z } from "zod";
5
+ import { createInteractionsRoute } from "./server/interactions.js";
6
+ import { createMcpRoute } from "./server/mcp.js";
7
+ import { createSiteRoute } from "./server/site.js";
8
+ import { createSkillsRoute } from "./server/skills.js";
9
+ import { createToolsRoute } from "./server/tools.js";
10
+ import { ToolContext, ToolServerConfig } from "./server/types.js";
11
+ import { ToolExecutionPayload } from "./types.js";
12
+ import { createWidgetsRoute } from "./server/widgets.js";
13
+
14
+ // Schema for tool execution payload
15
+ const ToolExecutionPayloadSchema = z.object({
16
+ tool_use: z.object({
17
+ id: z.string(),
18
+ tool_name: z.string(),
19
+ tool_input: z.record(z.string(), z.any()).default({}),
20
+ }),
21
+ metadata: z.record(z.string(), z.any()).optional(),
22
+ });
23
+
24
+
25
+
26
+ /**
27
+ * Create a Hono server for tools, interactions, and skills.
28
+ *
29
+ * @example
30
+ * ```typescript
31
+ * import { createToolServer, ToolCollection, SkillCollection } from "@vertesia/tools-sdk";
32
+ *
33
+ * const server = createToolServer({
34
+ * tools: [myToolCollection],
35
+ * skills: [mySkillCollection],
36
+ * });
37
+ *
38
+ * export default server;
39
+ * ```
40
+ */
41
+ export function createToolServer(config: ToolServerConfig): Hono {
42
+ const {
43
+ prefix = '/api',
44
+ tools = [],
45
+ interactions = [],
46
+ skills = [],
47
+ mcpProviders = [],
48
+ disableHtml = false,
49
+ } = config;
50
+
51
+ const app = new Hono();
52
+
53
+ // Add CORS middleware globally
54
+ app.use('*', cors({ origin: '*', allowMethods: ['GET', 'POST', 'OPTIONS'] }));
55
+
56
+ // Middleware to parse and validate body, store on context for reuse
57
+ app.use('*', async (c, next) => {
58
+ const ctx = c as unknown as ToolContext;
59
+ if (c.req.method === 'POST') {
60
+ try {
61
+ const text = await c.req.text();
62
+ const body = JSON.parse(text);
63
+ const result = ToolExecutionPayloadSchema.safeParse(body);
64
+ if (result.success) {
65
+ ctx.payload = result.data as ToolExecutionPayload<any>;
66
+ ctx.toolUseId = result.data.tool_use.id;
67
+ ctx.toolName = result.data.tool_use.tool_name;
68
+ }
69
+ // If validation fails, still store raw body for error reporting
70
+ // but don't set payload - handlers will return validation error
71
+ } catch {
72
+ // Ignore parsing errors - body might not be JSON
73
+ }
74
+ }
75
+ await next();
76
+ });
77
+
78
+ // HTML pages (unless disabled)
79
+ if (!disableHtml) {
80
+ createSiteRoute(app, '', config);
81
+ }
82
+
83
+ // Add base API route
84
+ app.get(prefix, (c) => {
85
+ // Skills are exposed as tools, so include them in the tools list
86
+ const allToolEndpoints = [
87
+ ...tools.map(col => `${prefix}/tools/${col.name}`),
88
+ ...skills.map(col => `${prefix}/skills/${col.name}`),
89
+ ];
90
+ return c.json({
91
+ message: 'Vertesia Tools API',
92
+ version: '1.0.0',
93
+ endpoints: {
94
+ tools: allToolEndpoints,
95
+ interactions: interactions.map(col => `${prefix}/interactions/${col.name}`),
96
+ mcp: mcpProviders.map(p => `${prefix}/mcp/${p.name}`),
97
+ }
98
+ });
99
+ });
100
+
101
+ createToolsRoute(app, `${prefix}/tools`, config);
102
+ createSkillsRoute(app, `${prefix}/skills`, config);
103
+ createWidgetsRoute(app, `${prefix}/widgets`, config);
104
+ createInteractionsRoute(app, `${prefix}/interactions`, config);
105
+ createMcpRoute(app, `${prefix}/mcp`, config);
106
+
107
+
108
+ // Global error handler - returns ToolExecutionResponseError format
109
+ app.onError((err, c) => {
110
+ const ctx = c as unknown as ToolContext;
111
+ const status = err instanceof HTTPException ? err.status : 500;
112
+ const errorMessage = err instanceof HTTPException ? err.message : 'Internal Server Error';
113
+
114
+ if (!(err instanceof HTTPException)) {
115
+ console.error('Uncaught Error:', err);
116
+ }
117
+
118
+ return c.json({
119
+ tool_use_id: ctx.toolUseId || 'unknown',
120
+ status,
121
+ error: errorMessage,
122
+ data: ctx.toolName ? { tool_name: ctx.toolName } : undefined,
123
+ }, status);
124
+ });
125
+
126
+ // Not found handler - returns ToolExecutionResponseError format
127
+ app.notFound((c) => {
128
+ const ctx = c as unknown as ToolContext;
129
+ return c.json({
130
+ tool_use_id: ctx.toolUseId || 'unknown',
131
+ status: 404,
132
+ error: `Not found: ${c.req.method} ${c.req.path}`,
133
+ data: ctx.toolName ? { tool_name: ctx.toolName } : undefined,
134
+ }, 404);
135
+ });
136
+
137
+ return app;
138
+ }
139
+
140
+
141
+
142
+ // ================== Server Utilities ==================
143
+
144
+ /**
145
+ * Simple development server with static fimesale handling
146
+ *
147
+ * @deprecated Use tools server template
148
+ */
149
+ export function createDevServer(config: ToolServerConfig & {
150
+ staticHandler?: (c: Context, next: () => Promise<void>) => Promise<Response | void>;
151
+ }): Hono {
152
+ const app = createToolServer(config);
153
+
154
+ if (config.staticHandler) {
155
+ app.use('*', config.staticHandler);
156
+ }
157
+
158
+ return app;
159
+ }
160
+