@tambo-ai/react 0.21.4 → 0.23.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/dist/hooks/use-component-state.js +3 -2
- package/dist/hooks/use-component-state.js.map +1 -1
- package/dist/hooks/use-current-message.d.ts +12 -9
- package/dist/hooks/use-current-message.d.ts.map +1 -1
- package/dist/hooks/use-current-message.js +5 -4
- package/dist/hooks/use-current-message.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/model/component-metadata.d.ts +10 -1
- package/dist/model/component-metadata.d.ts.map +1 -1
- package/dist/model/component-metadata.js.map +1 -1
- package/dist/providers/tambo-mcp-provider.d.ts +10 -0
- package/dist/providers/tambo-mcp-provider.d.ts.map +1 -0
- package/dist/providers/tambo-mcp-provider.js +65 -0
- package/dist/providers/tambo-mcp-provider.js.map +1 -0
- package/dist/testing/tools.js.map +1 -1
- package/dist/util/mcp-client.d.ts +198 -0
- package/dist/util/mcp-client.d.ts.map +1 -0
- package/dist/util/mcp-client.js +108 -0
- package/dist/util/mcp-client.js.map +1 -0
- package/dist/util/mcp-tools-client.d.ts +96 -0
- package/dist/util/mcp-tools-client.d.ts.map +1 -0
- package/dist/util/mcp-tools-client.js +178 -0
- package/dist/util/mcp-tools-client.js.map +1 -0
- package/dist/util/registry.d.ts.map +1 -1
- package/dist/util/registry.js +18 -0
- package/dist/util/registry.js.map +1 -1
- package/esm/hooks/use-component-state.js +3 -2
- package/esm/hooks/use-component-state.js.map +1 -1
- package/esm/hooks/use-current-message.d.ts +12 -9
- package/esm/hooks/use-current-message.d.ts.map +1 -1
- package/esm/hooks/use-current-message.js +5 -4
- package/esm/hooks/use-current-message.js.map +1 -1
- package/esm/index.d.ts +1 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/model/component-metadata.d.ts +10 -1
- package/esm/model/component-metadata.d.ts.map +1 -1
- package/esm/model/component-metadata.js.map +1 -1
- package/esm/providers/tambo-mcp-provider.d.ts +10 -0
- package/esm/providers/tambo-mcp-provider.d.ts.map +1 -0
- package/esm/providers/tambo-mcp-provider.js +61 -0
- package/esm/providers/tambo-mcp-provider.js.map +1 -0
- package/esm/testing/tools.js.map +1 -1
- package/esm/util/mcp-client.d.ts +198 -0
- package/esm/util/mcp-client.d.ts.map +1 -0
- package/esm/util/mcp-client.js +104 -0
- package/esm/util/mcp-client.js.map +1 -0
- package/esm/util/mcp-tools-client.d.ts +96 -0
- package/esm/util/mcp-tools-client.d.ts.map +1 -0
- package/esm/util/mcp-tools-client.js +174 -0
- package/esm/util/mcp-tools-client.js.map +1 -0
- package/esm/util/registry.d.ts.map +1 -1
- package/esm/util/registry.js +18 -0
- package/esm/util/registry.js.map +1 -1
- package/package.json +8 -7
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MCPClient = exports.MCPTransport = void 0;
|
|
4
|
+
const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
|
|
5
|
+
const sse_js_1 = require("@modelcontextprotocol/sdk/client/sse.js");
|
|
6
|
+
const streamableHttp_js_1 = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
|
|
7
|
+
var MCPTransport;
|
|
8
|
+
(function (MCPTransport) {
|
|
9
|
+
MCPTransport["SSE"] = "sse";
|
|
10
|
+
MCPTransport["HTTP"] = "http";
|
|
11
|
+
})(MCPTransport || (exports.MCPTransport = MCPTransport = {}));
|
|
12
|
+
/**
|
|
13
|
+
* A client for interacting with MCP (Model Context Protocol) servers.
|
|
14
|
+
* Provides a simple interface for listing and calling tools exposed by the server.
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* const mcp = await MCPClient.create('https://api.example.com/mcp');
|
|
18
|
+
* const tools = await mcp.listTools();
|
|
19
|
+
* const result = await mcp.callTool('toolName', { arg1: 'value1' });
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
class MCPClient {
|
|
23
|
+
client;
|
|
24
|
+
transport;
|
|
25
|
+
/**
|
|
26
|
+
* Private constructor to enforce using the static create method.
|
|
27
|
+
* @param endpoint - The URL of the MCP server to connect to
|
|
28
|
+
* @param transport - The transport to use for the MCP client
|
|
29
|
+
* @param headers - Optional custom headers to include in requests
|
|
30
|
+
*/
|
|
31
|
+
constructor(endpoint, transport, headers) {
|
|
32
|
+
if (transport === MCPTransport.SSE) {
|
|
33
|
+
this.transport = new sse_js_1.SSEClientTransport(new URL(endpoint), {
|
|
34
|
+
requestInit: { headers },
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
this.transport = new streamableHttp_js_1.StreamableHTTPClientTransport(new URL(endpoint), {
|
|
39
|
+
requestInit: { headers },
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
this.client = new index_js_1.Client({
|
|
43
|
+
name: "tambo-mcp-client",
|
|
44
|
+
version: "1.0.0",
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Creates and initializes a new MCPClient instance.
|
|
49
|
+
* This is the recommended way to create an MCPClient as it handles both
|
|
50
|
+
* instantiation and connection setup.
|
|
51
|
+
* @param endpoint - The URL of the MCP server to connect to
|
|
52
|
+
* @param headers - Optional custom headers to include in requests
|
|
53
|
+
* @returns A connected MCPClient instance ready for use
|
|
54
|
+
* @throws Will throw an error if connection fails
|
|
55
|
+
*/
|
|
56
|
+
static async create(endpoint, transport = MCPTransport.HTTP, headers) {
|
|
57
|
+
const mcpClient = new MCPClient(endpoint, transport, headers);
|
|
58
|
+
await mcpClient.client.connect(mcpClient.transport);
|
|
59
|
+
return mcpClient;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Retrieves a complete list of all available tools from the MCP server.
|
|
63
|
+
* Handles pagination automatically by following cursors until all tools are fetched.
|
|
64
|
+
* @returns A complete list of all available tools and their descriptions
|
|
65
|
+
* @throws Will throw an error if any server request fails during pagination
|
|
66
|
+
*/
|
|
67
|
+
async listTools() {
|
|
68
|
+
const allTools = [];
|
|
69
|
+
let hasMore = true;
|
|
70
|
+
let cursor = undefined;
|
|
71
|
+
while (hasMore) {
|
|
72
|
+
const response = await this.client.listTools({ cursor }, {});
|
|
73
|
+
allTools.push(...response.tools.map((tool) => {
|
|
74
|
+
if (tool.inputSchema.type !== "object") {
|
|
75
|
+
throw new Error(`Input schema for tool ${tool.name} is not an object`);
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
name: tool.name,
|
|
79
|
+
description: tool.description,
|
|
80
|
+
inputSchema: tool.inputSchema,
|
|
81
|
+
};
|
|
82
|
+
}));
|
|
83
|
+
if (response.nextCursor) {
|
|
84
|
+
cursor = response.nextCursor;
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
hasMore = false;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return allTools;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Calls a specific tool on the MCP server with the provided arguments.
|
|
94
|
+
* @param name - The name of the tool to call
|
|
95
|
+
* @param args - Arguments to pass to the tool, must match the tool's expected schema
|
|
96
|
+
* @returns The result from the tool execution
|
|
97
|
+
* @throws Will throw an error if the tool call fails or if arguments are invalid
|
|
98
|
+
*/
|
|
99
|
+
async callTool(name, args) {
|
|
100
|
+
const result = await this.client.callTool({
|
|
101
|
+
name,
|
|
102
|
+
arguments: args,
|
|
103
|
+
});
|
|
104
|
+
return result;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
exports.MCPClient = MCPClient;
|
|
108
|
+
//# sourceMappingURL=mcp-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-client.js","sourceRoot":"","sources":["../../src/util/mcp-client.ts"],"names":[],"mappings":";;;AAAA,wEA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M,GAAuB,SAAS,CAAC;QAE3C,OAAO,OAAO,EAAE,CAAC;YACf,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,EAAE,EAAE,CAAC,CAAC;YAC7D,QAAQ,CAAC,IAAI,CACX,GAAG,QAAQ,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAe,EAAE;gBAC1C,IAAI,IAAI,CAAC,WAAW,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;oBACvC,MAAM,IAAI,KAAK,CACb,yBAAyB,IAAI,CAAC,IAAI,mBAAmB,CACtD,CAAC;gBACJ,CAAC;gBAED,OAAO;oBACL,IAAI,EAAE,IAAI,CAAC,IAAI;oBACf,WAAW,EAAE,IAAI,CAAC,WAAW;oBAC7B,WAAW,EAAE,IAAI,CAAC,WAA0B;iBAC7C,CAAC;YACJ,CAAC,CAAC,CACH,CAAC;YAEF,IAAI,QAAQ,CAAC,UAAU,EAAE,CAAC;gBACxB,MAAM,GAAG,QAAQ,CAAC,UAAU,CAAC;YAC/B,CAAC;iBAAM,CAAC;gBACN,OAAO,GAAG,KAAK,CAAC;YAClB,CAAC;QACH,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,QAAQ,CAAC,IAAY,EAAE,IAA6B;QACxD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,QAAQ,CAAC;YACxC,IAAI;YACJ,SAAS,EAAE,IAAI;SAChB,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;CACF;AAtGD,8BAsGC","sourcesContent":["import { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { SSEClientTransport } from \"@modelcontextprotocol/sdk/client/sse.js\";\nimport { StreamableHTTPClientTransport } from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport { JSONSchema7 } from \"json-schema\";\n\nexport enum MCPTransport {\n SSE = \"sse\",\n HTTP = \"http\",\n}\n/**\n * A client for interacting with MCP (Model Context Protocol) servers.\n * Provides a simple interface for listing and calling tools exposed by the server.\n * @example\n * ```typescript\n * const mcp = await MCPClient.create('https://api.example.com/mcp');\n * const tools = await mcp.listTools();\n * const result = await mcp.callTool('toolName', { arg1: 'value1' });\n * ```\n */\nexport class MCPClient {\n private client: Client;\n private transport: SSEClientTransport | StreamableHTTPClientTransport;\n\n /**\n * Private constructor to enforce using the static create method.\n * @param endpoint - The URL of the MCP server to connect to\n * @param transport - The transport to use for the MCP client\n * @param headers - Optional custom headers to include in requests\n */\n private constructor(\n endpoint: string,\n transport: MCPTransport,\n headers?: Record<string, string>,\n ) {\n if (transport === MCPTransport.SSE) {\n this.transport = new SSEClientTransport(new URL(endpoint), {\n requestInit: { headers },\n });\n } else {\n this.transport = new StreamableHTTPClientTransport(new URL(endpoint), {\n requestInit: { headers },\n });\n }\n this.client = new Client({\n name: \"tambo-mcp-client\",\n version: \"1.0.0\",\n });\n }\n\n /**\n * Creates and initializes a new MCPClient instance.\n * This is the recommended way to create an MCPClient as it handles both\n * instantiation and connection setup.\n * @param endpoint - The URL of the MCP server to connect to\n * @param headers - Optional custom headers to include in requests\n * @returns A connected MCPClient instance ready for use\n * @throws Will throw an error if connection fails\n */\n static async create(\n endpoint: string,\n transport: MCPTransport = MCPTransport.HTTP,\n headers?: Record<string, string>,\n ): Promise<MCPClient> {\n const mcpClient = new MCPClient(endpoint, transport, headers);\n await mcpClient.client.connect(mcpClient.transport);\n return mcpClient;\n }\n\n /**\n * Retrieves a complete list of all available tools from the MCP server.\n * Handles pagination automatically by following cursors until all tools are fetched.\n * @returns A complete list of all available tools and their descriptions\n * @throws Will throw an error if any server request fails during pagination\n */\n async listTools(): Promise<MCPToolSpec[]> {\n const allTools: MCPToolSpec[] = [];\n let hasMore = true;\n let cursor: string | undefined = undefined;\n\n while (hasMore) {\n const response = await this.client.listTools({ cursor }, {});\n allTools.push(\n ...response.tools.map((tool): MCPToolSpec => {\n if (tool.inputSchema.type !== \"object\") {\n throw new Error(\n `Input schema for tool ${tool.name} is not an object`,\n );\n }\n\n return {\n name: tool.name,\n description: tool.description,\n inputSchema: tool.inputSchema as JSONSchema7,\n };\n }),\n );\n\n if (response.nextCursor) {\n cursor = response.nextCursor;\n } else {\n hasMore = false;\n }\n }\n\n return allTools;\n }\n\n /**\n * Calls a specific tool on the MCP server with the provided arguments.\n * @param name - The name of the tool to call\n * @param args - Arguments to pass to the tool, must match the tool's expected schema\n * @returns The result from the tool execution\n * @throws Will throw an error if the tool call fails or if arguments are invalid\n */\n async callTool(name: string, args: Record<string, unknown>) {\n const result = await this.client.callTool({\n name,\n arguments: args,\n });\n return result;\n }\n}\n\n// Example usage:\n/*\nconst mcp = await MCPClient.create('https://api.example.com/mcp', MCPTransport.HTTP);\nconst tools = await mcp.listTools();\nconst result = await mcp.callTool('toolName', { arg1: 'value1' });\n*/\n\nexport interface MCPToolSpec {\n name: string;\n description?: string;\n inputSchema?: JSONSchema7;\n}\n"]}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal Model Context Protocol (MCP) client using JSON-RPC 2.0
|
|
3
|
+
* Supports only listTools() and callTool() operations
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Interface representing a Tool in the MCP
|
|
7
|
+
*/
|
|
8
|
+
export interface Tool {
|
|
9
|
+
name: string;
|
|
10
|
+
description?: string;
|
|
11
|
+
inputSchema: {
|
|
12
|
+
type: "object";
|
|
13
|
+
properties: Record<string, any>;
|
|
14
|
+
required?: string[];
|
|
15
|
+
};
|
|
16
|
+
annotations?: Record<string, any>;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Response from tools/list endpoint
|
|
20
|
+
*/
|
|
21
|
+
export interface ListToolsResult {
|
|
22
|
+
tools: Tool[];
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Content type for tool responses
|
|
26
|
+
*/
|
|
27
|
+
export interface ToolContent {
|
|
28
|
+
type: string;
|
|
29
|
+
text?: string;
|
|
30
|
+
annotations?: Record<string, any>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Response from tools/call endpoint
|
|
34
|
+
*/
|
|
35
|
+
export interface CallToolResult {
|
|
36
|
+
content: ToolContent[];
|
|
37
|
+
isError?: boolean;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A minimal TypeScript client for the Model Context Protocol (MCP)
|
|
41
|
+
*
|
|
42
|
+
* This client provides a streamlined interface to communicate with MCP servers
|
|
43
|
+
* using JSON-RPC 2.0 over HTTP. It supports listing available tools and calling tools
|
|
44
|
+
* with arguments, including support for Server-Sent Events (SSE) streaming responses.
|
|
45
|
+
* @example
|
|
46
|
+
* ```typescript
|
|
47
|
+
* // Basic usage
|
|
48
|
+
* const mcpClient = new MCPClient('https://example.com/mcp');
|
|
49
|
+
* const tools = await mcpClient.listTools();
|
|
50
|
+
* const toolResponse = await mcpClient.callTool('my-tool', { arg1: 'value1' });
|
|
51
|
+
*
|
|
52
|
+
* // For streaming responses:
|
|
53
|
+
* const stream = await mcpClient.callToolStream('streaming-tool', { arg1: 'value1' });
|
|
54
|
+
* for await (const chunk of mcpClient.parseStreamingResponses(stream)) {
|
|
55
|
+
* console.log('Received chunk:', chunk);
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
export declare class MCPClient {
|
|
60
|
+
private baseUrl;
|
|
61
|
+
private headers;
|
|
62
|
+
private requestId;
|
|
63
|
+
/**
|
|
64
|
+
* Creates a new MCP client
|
|
65
|
+
* @param url - The base URL of the MCP server
|
|
66
|
+
* @param extraHeaders - Optional additional headers to include in requests
|
|
67
|
+
*/
|
|
68
|
+
constructor(url: string, extraHeaders?: Record<string, string>);
|
|
69
|
+
/**
|
|
70
|
+
* Lists available tools on the MCP server
|
|
71
|
+
* @returns Promise resolving to the list of available tools
|
|
72
|
+
*/
|
|
73
|
+
listTools(): Promise<ListToolsResult>;
|
|
74
|
+
/**
|
|
75
|
+
* Calls a tool with the provided arguments
|
|
76
|
+
* @param toolName - The name of the tool to call
|
|
77
|
+
* @param args - The arguments to pass to the tool
|
|
78
|
+
* @returns Promise resolving to the tool's response
|
|
79
|
+
*/
|
|
80
|
+
callTool(toolName: string, args: any): Promise<CallToolResult>;
|
|
81
|
+
/**
|
|
82
|
+
* Calls a tool with the provided arguments and returns a stream of results
|
|
83
|
+
* @param toolName - The name of the tool to call
|
|
84
|
+
* @param args - The arguments to pass to the tool
|
|
85
|
+
* @returns ReadableStream of the tool's response
|
|
86
|
+
*/
|
|
87
|
+
callToolStream(toolName: string, args: any): Promise<ReadableStream<Uint8Array>>;
|
|
88
|
+
/**
|
|
89
|
+
* Parse SSE messages from a stream into JSON-RPC responses
|
|
90
|
+
* @param stream - ReadableStream to parse
|
|
91
|
+
* @returns AsyncGenerator yielding parsed JSON-RPC responses
|
|
92
|
+
* @yields {CallToolResult} The parsed JSON-RPC response
|
|
93
|
+
*/
|
|
94
|
+
parseStreamingResponses(stream: ReadableStream<Uint8Array>): AsyncGenerator<CallToolResult, void, unknown>;
|
|
95
|
+
}
|
|
96
|
+
//# sourceMappingURL=mcp-tools-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-tools-client.d.ts","sourceRoot":"","sources":["../../src/util/mcp-tools-client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;GAEG;AACH,MAAM,WAAW,IAAI;IACnB,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,WAAW,EAAE;QACX,IAAI,EAAE,QAAQ,CAAC;QACf,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAChC,QAAQ,CAAC,EAAE,MAAM,EAAE,CAAC;KACrB,CAAC;IACF,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,IAAI,EAAE,CAAC;CACf;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CACnC;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,WAAW,EAAE,CAAC;IACvB,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,qBAAa,SAAS;IACpB,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,OAAO,CAAyB;IACxC,OAAO,CAAC,SAAS,CAAK;IAEtB;;;;OAIG;gBACS,GAAG,EAAE,MAAM,EAAE,YAAY,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC;IAS9D;;;OAGG;IACG,SAAS,IAAI,OAAO,CAAC,eAAe,CAAC;IAgC3C;;;;;OAKG;IACG,QAAQ,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,cAAc,CAAC;IAmCpE;;;;;OAKG;IACG,cAAc,CAClB,QAAQ,EAAE,MAAM,EAChB,IAAI,EAAE,GAAG,GACR,OAAO,CAAC,cAAc,CAAC,UAAU,CAAC,CAAC;IAkCtC;;;;;OAKG;IACI,uBAAuB,CAC5B,MAAM,EAAE,cAAc,CAAC,UAAU,CAAC,GACjC,cAAc,CAAC,cAAc,EAAE,IAAI,EAAE,OAAO,CAAC;CAwCjD"}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Minimal Model Context Protocol (MCP) client using JSON-RPC 2.0
|
|
4
|
+
* Supports only listTools() and callTool() operations
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.MCPClient = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* A minimal TypeScript client for the Model Context Protocol (MCP)
|
|
10
|
+
*
|
|
11
|
+
* This client provides a streamlined interface to communicate with MCP servers
|
|
12
|
+
* using JSON-RPC 2.0 over HTTP. It supports listing available tools and calling tools
|
|
13
|
+
* with arguments, including support for Server-Sent Events (SSE) streaming responses.
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* // Basic usage
|
|
17
|
+
* const mcpClient = new MCPClient('https://example.com/mcp');
|
|
18
|
+
* const tools = await mcpClient.listTools();
|
|
19
|
+
* const toolResponse = await mcpClient.callTool('my-tool', { arg1: 'value1' });
|
|
20
|
+
*
|
|
21
|
+
* // For streaming responses:
|
|
22
|
+
* const stream = await mcpClient.callToolStream('streaming-tool', { arg1: 'value1' });
|
|
23
|
+
* for await (const chunk of mcpClient.parseStreamingResponses(stream)) {
|
|
24
|
+
* console.log('Received chunk:', chunk);
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
class MCPClient {
|
|
29
|
+
baseUrl;
|
|
30
|
+
headers;
|
|
31
|
+
requestId = 1;
|
|
32
|
+
/**
|
|
33
|
+
* Creates a new MCP client
|
|
34
|
+
* @param url - The base URL of the MCP server
|
|
35
|
+
* @param extraHeaders - Optional additional headers to include in requests
|
|
36
|
+
*/
|
|
37
|
+
constructor(url, extraHeaders) {
|
|
38
|
+
this.baseUrl = url.endsWith("/") ? url : `${url}/`;
|
|
39
|
+
this.headers = {
|
|
40
|
+
"Content-Type": "application/json",
|
|
41
|
+
Accept: "application/json",
|
|
42
|
+
...(extraHeaders ?? {}),
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Lists available tools on the MCP server
|
|
47
|
+
* @returns Promise resolving to the list of available tools
|
|
48
|
+
*/
|
|
49
|
+
async listTools() {
|
|
50
|
+
const jsonRpcRequest = {
|
|
51
|
+
jsonrpc: "2.0",
|
|
52
|
+
method: "tools/list",
|
|
53
|
+
params: {},
|
|
54
|
+
id: this.requestId++,
|
|
55
|
+
};
|
|
56
|
+
const response = await fetch(this.baseUrl, {
|
|
57
|
+
method: "POST",
|
|
58
|
+
headers: this.headers,
|
|
59
|
+
body: JSON.stringify(jsonRpcRequest),
|
|
60
|
+
});
|
|
61
|
+
if (!response.ok) {
|
|
62
|
+
throw new Error(`Failed to list tools: ${response.status} ${response.statusText}`);
|
|
63
|
+
}
|
|
64
|
+
const jsonRpcResponse = await response.json();
|
|
65
|
+
// Handle JSON-RPC error
|
|
66
|
+
if (jsonRpcResponse.error) {
|
|
67
|
+
throw new Error(`JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`);
|
|
68
|
+
}
|
|
69
|
+
return jsonRpcResponse.result;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Calls a tool with the provided arguments
|
|
73
|
+
* @param toolName - The name of the tool to call
|
|
74
|
+
* @param args - The arguments to pass to the tool
|
|
75
|
+
* @returns Promise resolving to the tool's response
|
|
76
|
+
*/
|
|
77
|
+
async callTool(toolName, args) {
|
|
78
|
+
const jsonRpcRequest = {
|
|
79
|
+
jsonrpc: "2.0",
|
|
80
|
+
method: "tools/call",
|
|
81
|
+
params: {
|
|
82
|
+
name: toolName,
|
|
83
|
+
arguments: args,
|
|
84
|
+
},
|
|
85
|
+
id: this.requestId++,
|
|
86
|
+
};
|
|
87
|
+
const response = await fetch(this.baseUrl, {
|
|
88
|
+
method: "POST",
|
|
89
|
+
headers: this.headers,
|
|
90
|
+
body: JSON.stringify(jsonRpcRequest),
|
|
91
|
+
});
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
throw new Error(`Failed to call tool ${toolName}: ${response.status} ${response.statusText}`);
|
|
94
|
+
}
|
|
95
|
+
const jsonRpcResponse = await response.json();
|
|
96
|
+
// Handle JSON-RPC error
|
|
97
|
+
if (jsonRpcResponse.error) {
|
|
98
|
+
throw new Error(`JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`);
|
|
99
|
+
}
|
|
100
|
+
return jsonRpcResponse.result;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Calls a tool with the provided arguments and returns a stream of results
|
|
104
|
+
* @param toolName - The name of the tool to call
|
|
105
|
+
* @param args - The arguments to pass to the tool
|
|
106
|
+
* @returns ReadableStream of the tool's response
|
|
107
|
+
*/
|
|
108
|
+
async callToolStream(toolName, args) {
|
|
109
|
+
const jsonRpcRequest = {
|
|
110
|
+
jsonrpc: "2.0",
|
|
111
|
+
method: "tools/call",
|
|
112
|
+
params: {
|
|
113
|
+
name: toolName,
|
|
114
|
+
arguments: args,
|
|
115
|
+
},
|
|
116
|
+
id: this.requestId++,
|
|
117
|
+
};
|
|
118
|
+
const response = await fetch(this.baseUrl, {
|
|
119
|
+
method: "POST",
|
|
120
|
+
headers: {
|
|
121
|
+
...this.headers,
|
|
122
|
+
Accept: "text/event-stream",
|
|
123
|
+
},
|
|
124
|
+
body: JSON.stringify(jsonRpcRequest),
|
|
125
|
+
});
|
|
126
|
+
if (!response.ok) {
|
|
127
|
+
throw new Error(`Failed to call tool ${toolName}: ${response.status} ${response.statusText}`);
|
|
128
|
+
}
|
|
129
|
+
if (!response.body) {
|
|
130
|
+
throw new Error("Response body is null");
|
|
131
|
+
}
|
|
132
|
+
// Return the response body as a stream
|
|
133
|
+
return response.body;
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Parse SSE messages from a stream into JSON-RPC responses
|
|
137
|
+
* @param stream - ReadableStream to parse
|
|
138
|
+
* @returns AsyncGenerator yielding parsed JSON-RPC responses
|
|
139
|
+
* @yields {CallToolResult} The parsed JSON-RPC response
|
|
140
|
+
*/
|
|
141
|
+
async *parseStreamingResponses(stream) {
|
|
142
|
+
const reader = stream.getReader();
|
|
143
|
+
const decoder = new TextDecoder();
|
|
144
|
+
let buffer = "";
|
|
145
|
+
try {
|
|
146
|
+
while (true) {
|
|
147
|
+
const { done, value } = await reader.read();
|
|
148
|
+
if (done)
|
|
149
|
+
break;
|
|
150
|
+
buffer += decoder.decode(value, { stream: true });
|
|
151
|
+
// Process complete SSE messages
|
|
152
|
+
const lines = buffer.split("\n\n");
|
|
153
|
+
buffer = lines.pop() ?? "";
|
|
154
|
+
for (const line of lines) {
|
|
155
|
+
if (line.startsWith("data: ")) {
|
|
156
|
+
const jsonData = line.slice(6).trim();
|
|
157
|
+
try {
|
|
158
|
+
const jsonRpcResponse = JSON.parse(jsonData);
|
|
159
|
+
// Handle JSON-RPC error
|
|
160
|
+
if (jsonRpcResponse.error) {
|
|
161
|
+
throw new Error(`JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`);
|
|
162
|
+
}
|
|
163
|
+
yield jsonRpcResponse.result;
|
|
164
|
+
}
|
|
165
|
+
catch (e) {
|
|
166
|
+
console.error("Failed to parse JSON:", e);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
finally {
|
|
173
|
+
reader.releaseLock();
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
exports.MCPClient = MCPClient;
|
|
178
|
+
//# sourceMappingURL=mcp-tools-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp-tools-client.js","sourceRoot":"","sources":["../../src/util/mcp-tools-client.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAwCH;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAa,SAAS;IACZ,OAAO,CAAS;IAChB,OAAO,CAAyB;IAChC,SAAS,GAAG,CAAC,CAAC;IAEtB;;;;OAIG;IACH,YAAY,GAAW,EAAE,YAAqC;QAC5D,IAAI,CAAC,OAAO,GAAG,GAAG,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,CA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C,KAAK,CAAC,QAAQ,CAAC,CAAC;4BAE7C,wBAAwB;4BACxB,IAAI,eAAe,CAAC,KAAK,EAAE,CAAC;gCAC1B,MAAM,IAAI,KAAK,CACb,mBAAmB,eAAe,CAAC,KAAK,CAAC,IAAI,MAAM,eAAe,CAAC,KAAK,CAAC,OAAO,EAAE,CACnF,CAAC;4BACJ,CAAC;4BAED,MAAM,eAAe,CAAC,MAAM,CAAC;wBAC/B,CAAC;wBAAC,OAAO,CAAC,EAAE,CAAC;4BACX,OAAO,CAAC,KAAK,CAAC,uBAAuB,EAAE,CAAC,CAAC,CAAC;wBAC5C,CAAC;oBACH,CAAC;gBACH,CAAC;YACH,CAAC;QACH,CAAC;gBAAS,CAAC;YACT,MAAM,CAAC,WAAW,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;CACF;AA3LD,8BA2LC","sourcesContent":["/**\n * Minimal Model Context Protocol (MCP) client using JSON-RPC 2.0\n * Supports only listTools() and callTool() operations\n */\n\n/**\n * Interface representing a Tool in the MCP\n */\nexport interface Tool {\n name: string;\n description?: string;\n inputSchema: {\n type: \"object\";\n properties: Record<string, any>;\n required?: string[];\n };\n annotations?: Record<string, any>;\n}\n\n/**\n * Response from tools/list endpoint\n */\nexport interface ListToolsResult {\n tools: Tool[];\n}\n\n/**\n * Content type for tool responses\n */\nexport interface ToolContent {\n type: string;\n text?: string;\n annotations?: Record<string, any>;\n}\n\n/**\n * Response from tools/call endpoint\n */\nexport interface CallToolResult {\n content: ToolContent[];\n isError?: boolean;\n}\n\n/**\n * A minimal TypeScript client for the Model Context Protocol (MCP)\n *\n * This client provides a streamlined interface to communicate with MCP servers\n * using JSON-RPC 2.0 over HTTP. It supports listing available tools and calling tools\n * with arguments, including support for Server-Sent Events (SSE) streaming responses.\n * @example\n * ```typescript\n * // Basic usage\n * const mcpClient = new MCPClient('https://example.com/mcp');\n * const tools = await mcpClient.listTools();\n * const toolResponse = await mcpClient.callTool('my-tool', { arg1: 'value1' });\n *\n * // For streaming responses:\n * const stream = await mcpClient.callToolStream('streaming-tool', { arg1: 'value1' });\n * for await (const chunk of mcpClient.parseStreamingResponses(stream)) {\n * console.log('Received chunk:', chunk);\n * }\n * ```\n */\nexport class MCPClient {\n private baseUrl: string;\n private headers: Record<string, string>;\n private requestId = 1;\n\n /**\n * Creates a new MCP client\n * @param url - The base URL of the MCP server\n * @param extraHeaders - Optional additional headers to include in requests\n */\n constructor(url: string, extraHeaders?: Record<string, string>) {\n this.baseUrl = url.endsWith(\"/\") ? url : `${url}/`;\n this.headers = {\n \"Content-Type\": \"application/json\",\n Accept: \"application/json\",\n ...(extraHeaders ?? {}),\n };\n }\n\n /**\n * Lists available tools on the MCP server\n * @returns Promise resolving to the list of available tools\n */\n async listTools(): Promise<ListToolsResult> {\n const jsonRpcRequest = {\n jsonrpc: \"2.0\",\n method: \"tools/list\",\n params: {},\n id: this.requestId++,\n };\n\n const response = await fetch(this.baseUrl, {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(jsonRpcRequest),\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to list tools: ${response.status} ${response.statusText}`,\n );\n }\n\n const jsonRpcResponse = await response.json();\n\n // Handle JSON-RPC error\n if (jsonRpcResponse.error) {\n throw new Error(\n `JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`,\n );\n }\n\n return jsonRpcResponse.result;\n }\n\n /**\n * Calls a tool with the provided arguments\n * @param toolName - The name of the tool to call\n * @param args - The arguments to pass to the tool\n * @returns Promise resolving to the tool's response\n */\n async callTool(toolName: string, args: any): Promise<CallToolResult> {\n const jsonRpcRequest = {\n jsonrpc: \"2.0\",\n method: \"tools/call\",\n params: {\n name: toolName,\n arguments: args,\n },\n id: this.requestId++,\n };\n\n const response = await fetch(this.baseUrl, {\n method: \"POST\",\n headers: this.headers,\n body: JSON.stringify(jsonRpcRequest),\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to call tool ${toolName}: ${response.status} ${response.statusText}`,\n );\n }\n\n const jsonRpcResponse = await response.json();\n\n // Handle JSON-RPC error\n if (jsonRpcResponse.error) {\n throw new Error(\n `JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`,\n );\n }\n\n return jsonRpcResponse.result;\n }\n\n /**\n * Calls a tool with the provided arguments and returns a stream of results\n * @param toolName - The name of the tool to call\n * @param args - The arguments to pass to the tool\n * @returns ReadableStream of the tool's response\n */\n async callToolStream(\n toolName: string,\n args: any,\n ): Promise<ReadableStream<Uint8Array>> {\n const jsonRpcRequest = {\n jsonrpc: \"2.0\",\n method: \"tools/call\",\n params: {\n name: toolName,\n arguments: args,\n },\n id: this.requestId++,\n };\n\n const response = await fetch(this.baseUrl, {\n method: \"POST\",\n headers: {\n ...this.headers,\n Accept: \"text/event-stream\",\n },\n body: JSON.stringify(jsonRpcRequest),\n });\n\n if (!response.ok) {\n throw new Error(\n `Failed to call tool ${toolName}: ${response.status} ${response.statusText}`,\n );\n }\n\n if (!response.body) {\n throw new Error(\"Response body is null\");\n }\n\n // Return the response body as a stream\n return response.body;\n }\n\n /**\n * Parse SSE messages from a stream into JSON-RPC responses\n * @param stream - ReadableStream to parse\n * @returns AsyncGenerator yielding parsed JSON-RPC responses\n * @yields {CallToolResult} The parsed JSON-RPC response\n */\n async *parseStreamingResponses(\n stream: ReadableStream<Uint8Array>,\n ): AsyncGenerator<CallToolResult, void, unknown> {\n const reader = stream.getReader();\n const decoder = new TextDecoder();\n let buffer = \"\";\n\n try {\n while (true) {\n const { done, value } = await reader.read();\n if (done) break;\n\n buffer += decoder.decode(value, { stream: true });\n\n // Process complete SSE messages\n const lines = buffer.split(\"\\n\\n\");\n buffer = lines.pop() ?? \"\";\n\n for (const line of lines) {\n if (line.startsWith(\"data: \")) {\n const jsonData = line.slice(6).trim();\n try {\n const jsonRpcResponse = JSON.parse(jsonData);\n\n // Handle JSON-RPC error\n if (jsonRpcResponse.error) {\n throw new Error(\n `JSON-RPC error: ${jsonRpcResponse.error.code} - ${jsonRpcResponse.error.message}`,\n );\n }\n\n yield jsonRpcResponse.result;\n } catch (e) {\n console.error(\"Failed to parse JSON:\", e);\n }\n }\n }\n }\n } finally {\n reader.releaseLock();\n }\n }\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/util/registry.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,0BAA0B,CAAC;AAG/C,OAAO,EACL,4BAA4B,EAC5B,iBAAiB,
|
|
1
|
+
{"version":3,"file":"registry.d.ts","sourceRoot":"","sources":["../../src/util/registry.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,0BAA0B,CAAC;AAG/C,OAAO,EACL,4BAA4B,EAC5B,iBAAiB,EAGjB,mBAAmB,EACnB,SAAS,EACT,qBAAqB,EACrB,iBAAiB,EAClB,MAAM,6BAA6B,CAAC;AAErC;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,GACjC,mBAAmB,iBAAiB,EACpC,cAAc,iBAAiB,EAC/B,kBAAkB,qBAAqB,KACtC,OAAO,CAAC,kBAAkB,EAuB5B,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,oBAAoB,GAC/B,cAAc,iBAAiB,EAC/B,kBAAkB,qBAAqB,KACtC,SAAS,EAKX,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,GACnC,WAAW,mBAAmB,KAC7B,GAYF,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,wBAAwB,GACnC,eAAe,MAAM,EACrB,mBAAmB,iBAAiB,KACnC,mBAUF,CAAC;AAUF;;;GAGG;AACH,eAAO,MAAM,gBAAgB,QAAO,MAGnC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,GACpC,MAAM,SAAS,KACd,4BAQF,CAAC"}
|
package/dist/util/registry.js
CHANGED
|
@@ -108,7 +108,25 @@ const mapTamboToolToContextTool = (tool) => {
|
|
|
108
108
|
};
|
|
109
109
|
};
|
|
110
110
|
exports.mapTamboToolToContextTool = mapTamboToolToContextTool;
|
|
111
|
+
function isJsonSchema(schema) {
|
|
112
|
+
return (typeof schema === "object" &&
|
|
113
|
+
schema !== null &&
|
|
114
|
+
"type" in schema &&
|
|
115
|
+
typeof schema.type === "string" &&
|
|
116
|
+
schema.type === "object");
|
|
117
|
+
}
|
|
111
118
|
const getParametersFromZodFunction = (schema) => {
|
|
119
|
+
if (isJsonSchema(schema)) {
|
|
120
|
+
return [
|
|
121
|
+
{
|
|
122
|
+
name: "args",
|
|
123
|
+
type: "object",
|
|
124
|
+
description: schema.description ?? "",
|
|
125
|
+
isRequired: true,
|
|
126
|
+
schema: schema,
|
|
127
|
+
},
|
|
128
|
+
];
|
|
129
|
+
}
|
|
112
130
|
const parameters = schema.parameters();
|
|
113
131
|
return parameters.items.map((param, index) => {
|
|
114
132
|
const name = `param${index + 1}`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/util/registry.ts"],"names":[],"mappings":";;;;;;AAEA,4EAAiD;AAWjD;;;;;;GAMG;AACI,MAAM,sBAAsB,GAAG,CACpC,iBAAoC,EACpC,YAA+B,EAC/B,gBAAuC,EACT,EAAE;IAChC,MAAM,mBAAmB,GAAiC,EAAE,CAAC;IAE7D,KAAK,MAAM,CAAC,IAAI,EAAE,cAAc,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC,EAAE,CAAC;QACvE,MAAM,mBAAmB,GAAG,gBAAgB,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;QAEzD,MAAM,YAAY,GAAG;YACnB,GAAG,mBAAmB,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;gBACtC,MAAM,IAAI,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;gBACpC,IAAI,CAAC,IAAI;oBAAE,OAAO,IAAI,CAAC;gBACvB,OAAO,IAAA,iCAAyB,EAAC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U,EAAE,CAAC;IACnD,OAAO,UAAU,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAiB,EAAE;QAC1D,MAAM,IAAI,GAAG,QAAQ,KAAK,GAAG,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;QACnC,MAAM,WAAW,GAAG,KAAK,CAAC,WAAW,IAAI,EAAE,CAAC;QAC5C,MAAM,UAAU,GAAG,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC;QACvC,MAAM,MAAM,GAAG,IAAA,4BAAe,EAAC,KAAK,CAAC,CAAC;QAEtC,OAAO;YACL,IAAI;YACJ,IAAI;YACJ,WAAW;YACX,UAAU;YACV,MAAM;SACP,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,cAAc,GAAG,CAAC,MAAoB,EAAU,EAAE;IACtD,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;IACtC,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,WAAW;YACd,OAAO,QAAQ,CAAC;QAClB,KAAK,WAAW;YACd,OAAO,QAAQ,CAAC;QAClB,KAAK,YAAY;YACf,OAAO,SAAS,CAAC;QACnB,KAAK,UAAU;YACb,OAAO,OAAO,CAAC;QACjB,KAAK,SAAS;YACZ,OAAO,MAAM,CAAC;QAChB,KAAK,SAAS;YACZ,OAAO,MAAM,CAAC;QAChB,KAAK,WAAW;YACd,OAAO,QAAQ,CAAC;QAClB;YACE,OAAO,CAAC,IAAI,CAAC,4BAA4B,EAAE,QAAQ,CAAC,CAAC;YACrD,OAAO,QAAQ,CAAC;IACpB,CAAC;AACH,CAAC,CAAC","sourcesContent":["import TamboAI from \"@tambo-ai/typescript-sdk\";\nimport { z } from \"zod\";\nimport zodToJsonSchema from \"zod-to-json-schema\";\nimport {\n ComponentContextToolMetadata,\n ComponentRegistry,\n ParameterSpec,\n RegisteredComponent,\n TamboTool,\n TamboToolAssociations,\n TamboToolRegistry,\n} from \"../model/component-metadata\";\n\n/**\n * Get all the available components from the component registry\n * @param componentRegistry - The component registry\n * @param toolRegistry - The tool registry\n * @param toolAssociations - The tool associations\n * @returns The available components\n */\nexport const getAvailableComponents = (\n componentRegistry: ComponentRegistry,\n toolRegistry: TamboToolRegistry,\n toolAssociations: TamboToolAssociations,\n): TamboAI.AvailableComponent[] => {\n const availableComponents: TamboAI.AvailableComponent[] = [];\n\n for (const [name, componentEntry] of Object.entries(componentRegistry)) {\n const associatedToolNames = toolAssociations[name] || [];\n\n const contextTools = [\n ...associatedToolNames.map((toolName) => {\n const tool = toolRegistry[toolName];\n if (!tool) return null;\n return mapTamboToolToContextTool(tool);\n }),\n ].filter((tool): tool is ComponentContextToolMetadata => tool !== null);\n\n availableComponents.push({\n name: componentEntry.name,\n description: componentEntry.description,\n props: componentEntry.props,\n contextTools,\n });\n }\n\n return availableComponents;\n};\n\n/**\n * Get tools from tool registry that are not associated with any component\n * @param toolRegistry - The tool registry\n * @param toolAssociations - The tool associations\n * @returns The tools that are not associated with any component\n */\nexport const getUnassociatedTools = (\n toolRegistry: TamboToolRegistry,\n toolAssociations: TamboToolAssociations,\n): TamboTool[] => {\n return Object.values(toolRegistry).filter((tool) => {\n // Check if the tool's name appears in any of the tool association arrays\n return !Object.values(toolAssociations).flat().includes(tool.name);\n });\n};\n\n/**\n * Helper function to convert component props from Zod schema to JSON Schema\n * @param component - The component to convert\n * @returns The converted props\n */\nexport const convertPropsToJsonSchema = (\n component: RegisteredComponent,\n): any => {\n if (!component.props) {\n return component.props;\n }\n\n // Check if props is a Zod schema (we can't directly check the type, so we check for _def)\n if (component.props._def && typeof component.props.parse === \"function\") {\n // Use two-step type assertion for safety\n return zodToJsonSchema(component.props as unknown as z.ZodTypeAny);\n }\n\n return component.props;\n};\n\n/**\n * Get a component by name from the component registry\n * @param componentName - The name of the component to get\n * @param componentRegistry - The component registry\n * @returns The component registration information\n */\nexport const getComponentFromRegistry = (\n componentName: string,\n componentRegistry: ComponentRegistry,\n): RegisteredComponent => {\n const componentEntry = componentRegistry[componentName];\n\n if (!componentEntry) {\n throw new Error(\n `Tambo tried to use Component ${componentName}, but it was not found.`,\n );\n }\n\n return componentEntry;\n};\n\nconst getDefaultContextAdditions = (): string[] => {\n const utcOffsetHours = new Date().getTimezoneOffset() / 60;\n const utcOffset = `(UTC${utcOffsetHours > 0 ? \"+\" : \"\"}${utcOffsetHours})`;\n return [\n `The current time in user's timezone (${utcOffset}) is: ${new Date().toLocaleString()}`,\n ];\n};\n\n/**\n * Get the client context for the current thread, such as the current time in the user's timezone\n * @returns a string of context additions that will be added to the prompt when the thread is advanced.\n */\nexport const getClientContext = (): string => {\n const contextAdditions = getDefaultContextAdditions();\n return contextAdditions.join(\"\\n\");\n};\n\n/**\n * Map a Tambo tool to a context tool\n * @param tool - The tool to map\n * @returns The context tool\n */\nexport const mapTamboToolToContextTool = (\n tool: TamboTool,\n): ComponentContextToolMetadata => {\n const parameters = getParametersFromZodFunction(tool.toolSchema);\n\n return {\n name: tool.name,\n description: tool.description,\n parameters,\n };\n};\n\nconst getParametersFromZodFunction = (\n schema: z.ZodFunction<any, any>,\n): ParameterSpec[] => {\n const parameters: z.ZodTuple = schema.parameters();\n return parameters.items.map((param, index): ParameterSpec => {\n const name = `param${index + 1}`;\n const type = getZodBaseType(param);\n const description = param.description ?? \"\";\n const isRequired = !param.isOptional();\n const schema = zodToJsonSchema(param);\n\n return {\n name,\n type,\n description,\n isRequired,\n schema,\n };\n });\n};\n\nconst getZodBaseType = (schema: z.ZodTypeAny): string => {\n const typeName = schema._def.typeName;\n switch (typeName) {\n case \"ZodString\":\n return \"string\";\n case \"ZodNumber\":\n return \"number\";\n case \"ZodBoolean\":\n return \"boolean\";\n case \"ZodArray\":\n return \"array\";\n case \"ZodEnum\":\n return \"enum\";\n case \"ZodDate\":\n return \"date\";\n case \"ZodObject\":\n return \"object\";\n default:\n console.warn(\"falling back to string for\", typeName);\n return \"string\";\n }\n};\n"]}
|
|
1
|
+
{"version":3,"file":"registry.js","sourceRoot":"","sources":["../../src/util/registry.ts"],"names":[],"mappings":";;;;;;AAEA,4EAAiD;AAYjD;;;;;;GAMG;AACI,MAAM,sBAAsB,GAAG,CACpC,iBAAoC,EACpC,YAA+B,EAC/B,gBAAuC,EACT,EAAE;IAChC,MAAM,mBAAmB,GAAiC,EAAE,CAAC;IAE7D,KAAK,MAAM,CAAC,IAAI,EAAE,cAAc,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,iBAAiB,CAAC,EAAE,CAAC;QACvE,MAAM,mBAAmB,GAAG,gBAAgB,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC;QAEzD,MAAM,YAAY,GAAG;YACnB,GAAG,mBAAmB,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE;gBACtC,MAAM,IAAI,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAC;gBACpC,IAAI,CAAC,IAAI;oBAAE,OAAO,IAAI,CAAC;gBACvB,OAAO,IAAA,iCAAyB,EAAC,IAAI,CAAC,CAAC;YACzC,CAAC,CAAC;SACH,CAAC,MAAM,CAAC,CAAC,IAAI,EAAwC,EAAE,CAAC,IAAI,KAAK,IAAI,CAAC,CAAC;QAExE,mBAAmB,CAAC,IAAI,CAAC;YACvB,IAAI,EAAE,cAAc,CAAC,IAAI;YACzB,WAAW,EAAE,cAAc,CAAC,WAAW;YACvC,KAAK,EAAE,cAAc,CAAC,KAAK;YAC3B,YAAY;SACb,CAAC,CAAC;IACL,CAAC;IAED,OAAO,mBAAmB,CAAC;AAC7B,CAAC,CAAC;AA3BW,QAAA,sBAAsB,0BA2BjC;AAEF;;;;;GAKG;AACI,MAAM,oBAAoB,GAAG,CAClC,YAA+B,EAC/B,gBAAuC,EAC1B,EAAE;IACf,OAAO,MAAM,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,EAAE;QACjD,yEAA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B,EAAE;QAC1D,MAAM,IAAI,GAAG,QAAQ,KAAK,GAAG,CAAC,EAAE,CAAC;QACjC,MAAM,IAAI,GAAG,cAAc,CAAC,KAAK,CAAC,CAAC;QACnC,MAAM,WAAW,GAAG,KAAK,CAAC,WAAW,IAAI,EAAE,CAAC;QAC5C,MAAM,UAAU,GAAG,CAAC,KAAK,CAAC,UAAU,EAAE,CAAC;QACvC,MAAM,MAAM,GAAG,IAAA,4BAAe,EAAC,KAAK,CAAC,CAAC;QAEtC,OAAO;YACL,IAAI;YACJ,IAAI;YACJ,WAAW;YACX,UAAU;YACV,MAAM;SACP,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,cAAc,GAAG,CAAC,MAAoB,EAAU,EAAE;IACtD,MAAM,QAAQ,GAAG,MAAM,CAAC,IAAI,CAAC,QAAQ,CAAC;IACtC,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,WAAW;YACd,OAAO,QAAQ,CAAC;QAClB,KAAK,WAAW;YACd,OAAO,QAAQ,CAAC;QAClB,KAAK,YAAY;YACf,OAAO,SAAS,CAAC;QACnB,KAAK,UAAU;YACb,OAAO,OAAO,CAAC;QACjB,KAAK,SAAS;YACZ,OAAO,MAAM,CAAC;QAChB,KAAK,SAAS;YACZ,OAAO,MAAM,CAAC;QAChB,KAAK,WAAW;YACd,OAAO,QAAQ,CAAC;QAClB;YACE,OAAO,CAAC,IAAI,CAAC,4BAA4B,EAAE,QAAQ,CAAC,CAAC;YACrD,OAAO,QAAQ,CAAC;IACpB,CAAC;AACH,CAAC,CAAC","sourcesContent":["import TamboAI from \"@tambo-ai/typescript-sdk\";\nimport { z } from \"zod\";\nimport zodToJsonSchema from \"zod-to-json-schema\";\nimport {\n ComponentContextToolMetadata,\n ComponentRegistry,\n JSONSchemaLite,\n ParameterSpec,\n RegisteredComponent,\n TamboTool,\n TamboToolAssociations,\n TamboToolRegistry,\n} from \"../model/component-metadata\";\n\n/**\n * Get all the available components from the component registry\n * @param componentRegistry - The component registry\n * @param toolRegistry - The tool registry\n * @param toolAssociations - The tool associations\n * @returns The available components\n */\nexport const getAvailableComponents = (\n componentRegistry: ComponentRegistry,\n toolRegistry: TamboToolRegistry,\n toolAssociations: TamboToolAssociations,\n): TamboAI.AvailableComponent[] => {\n const availableComponents: TamboAI.AvailableComponent[] = [];\n\n for (const [name, componentEntry] of Object.entries(componentRegistry)) {\n const associatedToolNames = toolAssociations[name] || [];\n\n const contextTools = [\n ...associatedToolNames.map((toolName) => {\n const tool = toolRegistry[toolName];\n if (!tool) return null;\n return mapTamboToolToContextTool(tool);\n }),\n ].filter((tool): tool is ComponentContextToolMetadata => tool !== null);\n\n availableComponents.push({\n name: componentEntry.name,\n description: componentEntry.description,\n props: componentEntry.props,\n contextTools,\n });\n }\n\n return availableComponents;\n};\n\n/**\n * Get tools from tool registry that are not associated with any component\n * @param toolRegistry - The tool registry\n * @param toolAssociations - The tool associations\n * @returns The tools that are not associated with any component\n */\nexport const getUnassociatedTools = (\n toolRegistry: TamboToolRegistry,\n toolAssociations: TamboToolAssociations,\n): TamboTool[] => {\n return Object.values(toolRegistry).filter((tool) => {\n // Check if the tool's name appears in any of the tool association arrays\n return !Object.values(toolAssociations).flat().includes(tool.name);\n });\n};\n\n/**\n * Helper function to convert component props from Zod schema to JSON Schema\n * @param component - The component to convert\n * @returns The converted props\n */\nexport const convertPropsToJsonSchema = (\n component: RegisteredComponent,\n): any => {\n if (!component.props) {\n return component.props;\n }\n\n // Check if props is a Zod schema (we can't directly check the type, so we check for _def)\n if (component.props._def && typeof component.props.parse === \"function\") {\n // Use two-step type assertion for safety\n return zodToJsonSchema(component.props as unknown as z.ZodTypeAny);\n }\n\n return component.props;\n};\n\n/**\n * Get a component by name from the component registry\n * @param componentName - The name of the component to get\n * @param componentRegistry - The component registry\n * @returns The component registration information\n */\nexport const getComponentFromRegistry = (\n componentName: string,\n componentRegistry: ComponentRegistry,\n): RegisteredComponent => {\n const componentEntry = componentRegistry[componentName];\n\n if (!componentEntry) {\n throw new Error(\n `Tambo tried to use Component ${componentName}, but it was not found.`,\n );\n }\n\n return componentEntry;\n};\n\nconst getDefaultContextAdditions = (): string[] => {\n const utcOffsetHours = new Date().getTimezoneOffset() / 60;\n const utcOffset = `(UTC${utcOffsetHours > 0 ? \"+\" : \"\"}${utcOffsetHours})`;\n return [\n `The current time in user's timezone (${utcOffset}) is: ${new Date().toLocaleString()}`,\n ];\n};\n\n/**\n * Get the client context for the current thread, such as the current time in the user's timezone\n * @returns a string of context additions that will be added to the prompt when the thread is advanced.\n */\nexport const getClientContext = (): string => {\n const contextAdditions = getDefaultContextAdditions();\n return contextAdditions.join(\"\\n\");\n};\n\n/**\n * Map a Tambo tool to a context tool\n * @param tool - The tool to map\n * @returns The context tool\n */\nexport const mapTamboToolToContextTool = (\n tool: TamboTool,\n): ComponentContextToolMetadata => {\n const parameters = getParametersFromZodFunction(tool.toolSchema);\n\n return {\n name: tool.name,\n description: tool.description,\n parameters,\n };\n};\n\nfunction isJsonSchema(\n schema: unknown,\n): schema is ReturnType<typeof zodToJsonSchema> {\n return (\n typeof schema === \"object\" &&\n schema !== null &&\n \"type\" in schema &&\n typeof (schema as { type: unknown }).type === \"string\" &&\n (schema as { type: string }).type === \"object\"\n );\n}\n\nconst getParametersFromZodFunction = (\n schema: z.ZodFunction<any, any> | JSONSchemaLite,\n): ParameterSpec[] => {\n if (isJsonSchema(schema)) {\n return [\n {\n name: \"args\",\n type: \"object\",\n description: schema.description ?? \"\",\n isRequired: true,\n schema: schema,\n },\n ];\n }\n\n const parameters: z.ZodTuple = schema.parameters();\n return parameters.items.map((param, index): ParameterSpec => {\n const name = `param${index + 1}`;\n const type = getZodBaseType(param);\n const description = param.description ?? \"\";\n const isRequired = !param.isOptional();\n const schema = zodToJsonSchema(param);\n\n return {\n name,\n type,\n description,\n isRequired,\n schema,\n };\n });\n};\n\nconst getZodBaseType = (schema: z.ZodTypeAny): string => {\n const typeName = schema._def.typeName;\n switch (typeName) {\n case \"ZodString\":\n return \"string\";\n case \"ZodNumber\":\n return \"number\";\n case \"ZodBoolean\":\n return \"boolean\";\n case \"ZodArray\":\n return \"array\";\n case \"ZodEnum\":\n return \"enum\";\n case \"ZodDate\":\n return \"date\";\n case \"ZodObject\":\n return \"object\";\n default:\n console.warn(\"falling back to string for\", typeName);\n return \"string\";\n }\n};\n"]}
|
|
@@ -4,10 +4,11 @@ import { useTamboClient, useTamboThread } from "../providers";
|
|
|
4
4
|
import { useTamboCurrentMessage, useTamboMessageContext, } from "./use-current-message";
|
|
5
5
|
// eslint-disable-next-line jsdoc/require-jsdoc
|
|
6
6
|
export function useTamboComponentState(keyName, initialValue, debounceTime = 500) {
|
|
7
|
-
const {
|
|
8
|
-
const { updateThreadMessage } = useTamboThread();
|
|
7
|
+
const { messageId } = useTamboMessageContext();
|
|
8
|
+
const { updateThreadMessage, thread } = useTamboThread();
|
|
9
9
|
const client = useTamboClient();
|
|
10
10
|
const message = useTamboCurrentMessage();
|
|
11
|
+
const threadId = thread.id;
|
|
11
12
|
// Initial value management
|
|
12
13
|
const [cachedInitialValue] = useState(() => initialValue);
|
|
13
14
|
// UI state management
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-component-state.js","sourceRoot":"","sources":["../../src/hooks/use-component-state.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AACzD,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC9D,OAAO,EACL,sBAAsB,EACtB,sBAAsB,GACvB,MAAM,uBAAuB,CAAC;AAmD/B,+CAA+C;AAC/C,MAAM,UAAU,sBAAsB,CACpC,OAAe,EACf,YAAgB,EAChB,YAAY,GAAG,GAAG;IAElB,MAAM,EAAE,QAAQ,EAAE,SAAS,EAAE,GAAG,sBAAsB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C,SAAS,cAAc,OAAO,GAAG,CAC9E,CAAC;QACJ,CAAC;IACH,CAAC,EACD,CAAC,OAAO,EAAE,oBAAoB,EAAE,OAAO,EAAE,mBAAmB,EAAE,SAAS,CAAC,CACzE,CAAC;IAEF,gDAAgD;IAChD,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,GAAG,EAAE;YACV,oBAAoB,CAAC,KAAK,EAAE,CAAC;QAC/B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,oBAAoB,CAAC,CAAC,CAAC;IAE3B,oDAAoD;IACpD,OAAO,CAAC,UAAe,EAAE,QAAQ,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;AACpD,CAAC","sourcesContent":["import { useCallback, useEffect, useState } from \"react\";\nimport { useDebouncedCallback } from \"use-debounce\";\nimport { useTamboClient, useTamboThread } from \"../providers\";\nimport {\n useTamboCurrentMessage,\n useTamboMessageContext,\n} from \"./use-current-message\";\n// Define metadata interface for better extensibility\ninterface ComponentStateMeta {\n isPending: boolean;\n}\n\ntype StateUpdateResult<T> = [\n currentState: T,\n setState: (newState: T) => void,\n meta: ComponentStateMeta,\n];\n\n/**\n * A React hook that provides state management and passes user updates to Tambo.\n * Benefits: Passes user changes to AI, and when threads are returned, state is preserved.\n * @param keyName - The unique key to identify this state within the message's componentState object\n * @param initialValue - Optional initial value for the state, used if no value exists in the message\n * @param debounceTime - Optional debounce time in milliseconds (default: 300ms) to limit API calls\n * @returns A tuple containing:\n * - The current state value\n * - A setter function to update the state (updates UI immediately, debounces server sync)\n * - A metadata object with properties like isPending to track sync status\n * @example\n * // Basic usage\n * const [count, setCount, { isPending }] = useTamboComponentState(\"counter\", 0);\n *\n * // Usage with object state\n * const [formState, setFormState] = useTamboComponentState(\"myForm\", {\n * name: \"\",\n * email: \"\",\n * message: \"\"\n * });\n *\n * // Handling form input\n * const handleChange = (e) => {\n * setFormState({\n * ...formState,\n * [e.target.name]: e.target.value\n * });\n * };\n */\nexport function useTamboComponentState<S = undefined>(\n keyName: string,\n initialValue?: S,\n debounceTime?: number,\n): StateUpdateResult<S | undefined>;\nexport function useTamboComponentState<S>(\n keyName: string,\n initialValue: S,\n debounceTime?: number,\n): StateUpdateResult<S>;\n// eslint-disable-next-line jsdoc/require-jsdoc\nexport function useTamboComponentState<S>(\n keyName: string,\n initialValue?: S,\n debounceTime = 500,\n): StateUpdateResult<S> {\n const { threadId, messageId } = useTamboMessageContext();\n const { updateThreadMessage } = useTamboThread();\n const client = useTamboClient();\n const message = useTamboCurrentMessage();\n\n // Initial value management\n const [cachedInitialValue] = useState(() => initialValue);\n // UI state management\n const [localState, setLocalState] = useState<S | undefined>(\n cachedInitialValue,\n );\n // Synchronization state\n const [isPending, setIsPending] = useState(false);\n // Track the last user-initiated value instead of a simple boolean flag\n const [lastUserValue, setLastUserValue] = useState<S | null>(null);\n const [haveInitialized, setHaveInitialized] = useState(false);\n\n // Determine if we need to initialize state\n const shouldInitialize =\n !haveInitialized &&\n message &&\n cachedInitialValue !== undefined &&\n (!message.componentState || !(keyName in message.componentState));\n\n // Sync local state with message state on initial load and when message changes\n useEffect(() => {\n if (message?.componentState && keyName in message.componentState) {\n const messageState = message.componentState[keyName] as S;\n\n // Only update local state if we haven't had any user changes yet\n if (lastUserValue === null) {\n setLocalState(messageState);\n }\n }\n // Otherwise fall back to initial value if we have one and no user changes\n else if (\n cachedInitialValue !== undefined &&\n !localState &&\n lastUserValue === null\n ) {\n setLocalState(cachedInitialValue);\n }\n }, [\n keyName,\n message?.componentState,\n cachedInitialValue,\n lastUserValue,\n localState,\n ]);\n\n // Create debounced save function for efficient server synchronization\n const debouncedServerWrite = useDebouncedCallback(async (newValue: S) => {\n setIsPending(true);\n try {\n const componentStateUpdate = {\n state: { [keyName]: newValue },\n };\n\n await client.beta.threads.messages.updateComponentState(\n threadId,\n messageId,\n componentStateUpdate,\n );\n } catch (err) {\n console.error(\n `Failed to save component state for key \"${keyName}\":`,\n err,\n );\n } finally {\n setIsPending(false);\n }\n }, debounceTime);\n\n // Initialize state on first render if needed\n const initializeState = useCallback(async () => {\n if (!message) {\n console.warn(\n `Cannot initialize state for missing message ${messageId} with key \"${keyName}\"`,\n );\n return;\n }\n\n try {\n const messageUpdate = {\n ...message,\n componentState: {\n ...message.componentState,\n [keyName]: cachedInitialValue,\n },\n };\n\n const componentStateUpdate = {\n state: { [keyName]: cachedInitialValue },\n };\n\n await Promise.all([\n updateThreadMessage(messageId, messageUpdate, false),\n client.beta.threads.messages.updateComponentState(\n threadId,\n messageId,\n componentStateUpdate,\n ),\n ]);\n } catch (err) {\n console.warn(\n `Failed to initialize component state for key \"${keyName}\":`,\n err,\n );\n }\n }, [\n cachedInitialValue,\n client.beta.threads.messages,\n keyName,\n message,\n messageId,\n threadId,\n updateThreadMessage,\n ]);\n\n // Send initial state when component mounts\n useEffect(() => {\n if (shouldInitialize) {\n initializeState();\n setHaveInitialized(true);\n }\n }, [initializeState, shouldInitialize]);\n\n // setValue function for updating state\n // Updates local state immediately and schedules debounced server sync\n const setValue = useCallback(\n (newValue: S) => {\n // Track this as a user-initiated update\n setLastUserValue(newValue);\n setLocalState(newValue);\n\n // Only trigger server updates if we have a message\n if (message) {\n debouncedServerWrite(newValue);\n const messageUpdate = {\n ...message,\n componentState: {\n ...message.componentState,\n [keyName]: newValue,\n },\n };\n\n updateThreadMessage(messageId, messageUpdate, false);\n } else {\n console.warn(\n `Cannot update server for missing message ${messageId} with key \"${keyName}\"`,\n );\n }\n },\n [message, debouncedServerWrite, keyName, updateThreadMessage, messageId],\n );\n\n // Ensure pending changes are flushed on unmount\n useEffect(() => {\n return () => {\n debouncedServerWrite.flush();\n };\n }, [debouncedServerWrite]);\n\n // Return the local state for immediate UI rendering\n return [localState as S, setValue, { isPending }];\n}\n"]}
|
|
1
|
+
{"version":3,"file":"use-component-state.js","sourceRoot":"","sources":["../../src/hooks/use-component-state.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,O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B,aAAa,CAAC,QAAQ,CAAC,CAAC;QAExB,mDAAmD;QACnD,IAAI,OAAO,EAAE,CAAC;YACZ,oBAAoB,CAAC,QAAQ,CAAC,CAAC;YAC/B,MAAM,aAAa,GAAG;gBACpB,GAAG,OAAO;gBACV,cAAc,EAAE;oBACd,GAAG,OAAO,CAAC,cAAc;oBACzB,CAAC,OAAO,CAAC,EAAE,QAAQ;iBACpB;aACF,CAAC;YAEF,mBAAmB,CAAC,SAAS,EAAE,aAAa,EAAE,KAAK,CAAC,CAAC;QACvD,CAAC;aAAM,CAAC;YACN,OAAO,CAAC,IAAI,CACV,4CAA4C,SAAS,cAAc,OAAO,GAAG,CAC9E,CAAC;QACJ,CAAC;IACH,CAAC,EACD,CAAC,OAAO,EAAE,oBAAoB,EAAE,OAAO,EAAE,mBAAmB,EAAE,SAAS,CAAC,CACzE,CAAC;IAEF,gDAAgD;IAChD,SAAS,CAAC,GAAG,EAAE;QACb,OAAO,GAAG,EAAE;YACV,oBAAoB,CAAC,KAAK,EAAE,CAAC;QAC/B,CAAC,CAAC;IACJ,CAAC,EAAE,CAAC,oBAAoB,CAAC,CAAC,CAAC;IAE3B,oDAAoD;IACpD,OAAO,CAAC,UAAe,EAAE,QAAQ,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;AACpD,CAAC","sourcesContent":["import { useCallback, useEffect, useState } from \"react\";\nimport { useDebouncedCallback } from \"use-debounce\";\nimport { useTamboClient, useTamboThread } from \"../providers\";\nimport {\n useTamboCurrentMessage,\n useTamboMessageContext,\n} from \"./use-current-message\";\n// Define metadata interface for better extensibility\ninterface ComponentStateMeta {\n isPending: boolean;\n}\n\ntype StateUpdateResult<T> = [\n currentState: T,\n setState: (newState: T) => void,\n meta: ComponentStateMeta,\n];\n\n/**\n * A React hook that provides state management and passes user updates to Tambo.\n * Benefits: Passes user changes to AI, and when threads are returned, state is preserved.\n * @param keyName - The unique key to identify this state within the message's componentState object\n * @param initialValue - Optional initial value for the state, used if no value exists in the message\n * @param debounceTime - Optional debounce time in milliseconds (default: 300ms) to limit API calls\n * @returns A tuple containing:\n * - The current state value\n * - A setter function to update the state (updates UI immediately, debounces server sync)\n * - A metadata object with properties like isPending to track sync status\n * @example\n * // Basic usage\n * const [count, setCount, { isPending }] = useTamboComponentState(\"counter\", 0);\n *\n * // Usage with object state\n * const [formState, setFormState] = useTamboComponentState(\"myForm\", {\n * name: \"\",\n * email: \"\",\n * message: \"\"\n * });\n *\n * // Handling form input\n * const handleChange = (e) => {\n * setFormState({\n * ...formState,\n * [e.target.name]: e.target.value\n * });\n * };\n */\nexport function useTamboComponentState<S = undefined>(\n keyName: string,\n initialValue?: S,\n debounceTime?: number,\n): StateUpdateResult<S | undefined>;\nexport function useTamboComponentState<S>(\n keyName: string,\n initialValue: S,\n debounceTime?: number,\n): StateUpdateResult<S>;\n// eslint-disable-next-line jsdoc/require-jsdoc\nexport function useTamboComponentState<S>(\n keyName: string,\n initialValue?: S,\n debounceTime = 500,\n): StateUpdateResult<S> {\n const { messageId } = useTamboMessageContext();\n const { updateThreadMessage, thread } = useTamboThread();\n const client = useTamboClient();\n const message = useTamboCurrentMessage();\n const threadId = thread.id;\n\n // Initial value management\n const [cachedInitialValue] = useState(() => initialValue);\n // UI state management\n const [localState, setLocalState] = useState<S | undefined>(\n cachedInitialValue,\n );\n // Synchronization state\n const [isPending, setIsPending] = useState(false);\n // Track the last user-initiated value instead of a simple boolean flag\n const [lastUserValue, setLastUserValue] = useState<S | null>(null);\n const [haveInitialized, setHaveInitialized] = useState(false);\n\n // Determine if we need to initialize state\n const shouldInitialize =\n !haveInitialized &&\n message &&\n cachedInitialValue !== undefined &&\n (!message.componentState || !(keyName in message.componentState));\n\n // Sync local state with message state on initial load and when message changes\n useEffect(() => {\n if (message?.componentState && keyName in message.componentState) {\n const messageState = message.componentState[keyName] as S;\n\n // Only update local state if we haven't had any user changes yet\n if (lastUserValue === null) {\n setLocalState(messageState);\n }\n }\n // Otherwise fall back to initial value if we have one and no user changes\n else if (\n cachedInitialValue !== undefined &&\n !localState &&\n lastUserValue === null\n ) {\n setLocalState(cachedInitialValue);\n }\n }, [\n keyName,\n message?.componentState,\n cachedInitialValue,\n lastUserValue,\n localState,\n ]);\n\n // Create debounced save function for efficient server synchronization\n const debouncedServerWrite = useDebouncedCallback(async (newValue: S) => {\n setIsPending(true);\n try {\n const componentStateUpdate = {\n state: { [keyName]: newValue },\n };\n\n await client.beta.threads.messages.updateComponentState(\n threadId,\n messageId,\n componentStateUpdate,\n );\n } catch (err) {\n console.error(\n `Failed to save component state for key \"${keyName}\":`,\n err,\n );\n } finally {\n setIsPending(false);\n }\n }, debounceTime);\n\n // Initialize state on first render if needed\n const initializeState = useCallback(async () => {\n if (!message) {\n console.warn(\n `Cannot initialize state for missing message ${messageId} with key \"${keyName}\"`,\n );\n return;\n }\n\n try {\n const messageUpdate = {\n ...message,\n componentState: {\n ...message.componentState,\n [keyName]: cachedInitialValue,\n },\n };\n\n const componentStateUpdate = {\n state: { [keyName]: cachedInitialValue },\n };\n\n await Promise.all([\n updateThreadMessage(messageId, messageUpdate, false),\n client.beta.threads.messages.updateComponentState(\n threadId,\n messageId,\n componentStateUpdate,\n ),\n ]);\n } catch (err) {\n console.warn(\n `Failed to initialize component state for key \"${keyName}\":`,\n err,\n );\n }\n }, [\n cachedInitialValue,\n client.beta.threads.messages,\n keyName,\n message,\n messageId,\n threadId,\n updateThreadMessage,\n ]);\n\n // Send initial state when component mounts\n useEffect(() => {\n if (shouldInitialize) {\n initializeState();\n setHaveInitialized(true);\n }\n }, [initializeState, shouldInitialize]);\n\n // setValue function for updating state\n // Updates local state immediately and schedules debounced server sync\n const setValue = useCallback(\n (newValue: S) => {\n // Track this as a user-initiated update\n setLastUserValue(newValue);\n setLocalState(newValue);\n\n // Only trigger server updates if we have a message\n if (message) {\n debouncedServerWrite(newValue);\n const messageUpdate = {\n ...message,\n componentState: {\n ...message.componentState,\n [keyName]: newValue,\n },\n };\n\n updateThreadMessage(messageId, messageUpdate, false);\n } else {\n console.warn(\n `Cannot update server for missing message ${messageId} with key \"${keyName}\"`,\n );\n }\n },\n [message, debouncedServerWrite, keyName, updateThreadMessage, messageId],\n );\n\n // Ensure pending changes are flushed on unmount\n useEffect(() => {\n return () => {\n debouncedServerWrite.flush();\n };\n }, [debouncedServerWrite]);\n\n // Return the local state for immediate UI rendering\n return [localState as S, setValue, { isPending }];\n}\n"]}
|
|
@@ -1,16 +1,21 @@
|
|
|
1
1
|
import React, { PropsWithChildren } from "react";
|
|
2
|
+
interface TamboMessageContextProps {
|
|
3
|
+
/**
|
|
4
|
+
* The threadId of the thread
|
|
5
|
+
* @deprecated Use the thread object from the TamboThreadProvider instead
|
|
6
|
+
*/
|
|
7
|
+
threadId?: string;
|
|
8
|
+
/** The messageId of the message */
|
|
9
|
+
messageId: string;
|
|
10
|
+
}
|
|
2
11
|
/**
|
|
3
12
|
* Wraps all components, so that they can find what thread and message they are in
|
|
4
13
|
* @param props - props for the TamboMessageProvider
|
|
5
14
|
* @param props.children - The children to wrap
|
|
6
|
-
* @param props.threadId - The threadId of the thread
|
|
7
15
|
* @param props.messageId - The messageId of the message
|
|
8
16
|
* @returns The wrapped component
|
|
9
17
|
*/
|
|
10
|
-
export declare const TamboMessageProvider: React.FC<PropsWithChildren<
|
|
11
|
-
threadId: string;
|
|
12
|
-
messageId: string;
|
|
13
|
-
}>>;
|
|
18
|
+
export declare const TamboMessageProvider: React.FC<PropsWithChildren<TamboMessageContextProps>>;
|
|
14
19
|
/**
|
|
15
20
|
* Wraps a component with a ComponentMessageProvider - this allows the provider
|
|
16
21
|
* to be used outside of a TSX file
|
|
@@ -25,10 +30,7 @@ export declare function wrapWithTamboMessageProvider(children: React.ReactNode,
|
|
|
25
30
|
* the threadId and messageId
|
|
26
31
|
* @returns The threadId and messageId
|
|
27
32
|
*/
|
|
28
|
-
export declare const useTamboMessageContext: () =>
|
|
29
|
-
threadId: string;
|
|
30
|
-
messageId: string;
|
|
31
|
-
};
|
|
33
|
+
export declare const useTamboMessageContext: () => TamboMessageContextProps;
|
|
32
34
|
/**
|
|
33
35
|
* Hook used inside a component wrapped with ComponentMessageProvider, to get
|
|
34
36
|
* the current message. The current thread will be fetched from the server, if
|
|
@@ -36,4 +38,5 @@ export declare const useTamboMessageContext: () => {
|
|
|
36
38
|
* @returns The current message that is used to render the component
|
|
37
39
|
*/
|
|
38
40
|
export declare const useTamboCurrentMessage: () => import("..").TamboThreadMessage | undefined;
|
|
41
|
+
export {};
|
|
39
42
|
//# sourceMappingURL=use-current-message.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-current-message.d.ts","sourceRoot":"","sources":["../../src/hooks/use-current-message.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAiB,iBAAiB,EAAc,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"use-current-message.d.ts","sourceRoot":"","sources":["../../src/hooks/use-current-message.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAiB,iBAAiB,EAAc,MAAM,OAAO,CAAC;AAG5E,UAAU,wBAAwB;IAChC;;;OAGG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,mCAAmC;IACnC,SAAS,EAAE,MAAM,CAAC;CACnB;AAMD;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,EAAE,KAAK,CAAC,EAAE,CACzC,iBAAiB,CAAC,wBAAwB,CAAC,CAU5C,CAAC;AAEF;;;;;;;GAOG;AACH,wBAAgB,4BAA4B,CAC1C,QAAQ,EAAE,KAAK,CAAC,SAAS,EACzB,QAAQ,EAAE,MAAM,EAChB,SAAS,EAAE,MAAM,qBAOlB;AACD;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,gCAQlC,CAAC;AAEF;;;;;GAKG;AACH,eAAO,MAAM,sBAAsB,mDASlC,CAAC"}
|