@tambo-ai/react 0.55.0 → 0.57.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.
Files changed (55) hide show
  1. package/dist/index.d.ts +3 -2
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js +2 -2
  4. package/dist/index.js.map +1 -1
  5. package/dist/mcp/__tests__/mcp-client.test.js +52 -6
  6. package/dist/mcp/__tests__/mcp-client.test.js.map +1 -1
  7. package/dist/mcp/mcp-client.d.ts +34 -2
  8. package/dist/mcp/mcp-client.d.ts.map +1 -1
  9. package/dist/mcp/mcp-client.js +63 -4
  10. package/dist/mcp/mcp-client.js.map +1 -1
  11. package/dist/providers/__tests__/tambo-thread-provider-initial-messages.test.d.ts +2 -0
  12. package/dist/providers/__tests__/tambo-thread-provider-initial-messages.test.d.ts.map +1 -0
  13. package/dist/providers/__tests__/tambo-thread-provider-initial-messages.test.js +179 -0
  14. package/dist/providers/__tests__/tambo-thread-provider-initial-messages.test.js.map +1 -0
  15. package/dist/providers/__tests__/tambo-thread-provider.test.js +2 -6
  16. package/dist/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
  17. package/dist/providers/index.d.ts +1 -1
  18. package/dist/providers/index.d.ts.map +1 -1
  19. package/dist/providers/index.js.map +1 -1
  20. package/dist/providers/tambo-provider.d.ts +1 -0
  21. package/dist/providers/tambo-provider.d.ts.map +1 -1
  22. package/dist/providers/tambo-provider.js +3 -2
  23. package/dist/providers/tambo-provider.js.map +1 -1
  24. package/dist/providers/tambo-thread-provider.d.ts +13 -0
  25. package/dist/providers/tambo-thread-provider.d.ts.map +1 -1
  26. package/dist/providers/tambo-thread-provider.js +51 -22
  27. package/dist/providers/tambo-thread-provider.js.map +1 -1
  28. package/esm/index.d.ts +3 -2
  29. package/esm/index.d.ts.map +1 -1
  30. package/esm/index.js +1 -1
  31. package/esm/index.js.map +1 -1
  32. package/esm/mcp/__tests__/mcp-client.test.js +52 -6
  33. package/esm/mcp/__tests__/mcp-client.test.js.map +1 -1
  34. package/esm/mcp/mcp-client.d.ts +34 -2
  35. package/esm/mcp/mcp-client.d.ts.map +1 -1
  36. package/esm/mcp/mcp-client.js +63 -4
  37. package/esm/mcp/mcp-client.js.map +1 -1
  38. package/esm/providers/__tests__/tambo-thread-provider-initial-messages.test.d.ts +2 -0
  39. package/esm/providers/__tests__/tambo-thread-provider-initial-messages.test.d.ts.map +1 -0
  40. package/esm/providers/__tests__/tambo-thread-provider-initial-messages.test.js +174 -0
  41. package/esm/providers/__tests__/tambo-thread-provider-initial-messages.test.js.map +1 -0
  42. package/esm/providers/__tests__/tambo-thread-provider.test.js +2 -6
  43. package/esm/providers/__tests__/tambo-thread-provider.test.js.map +1 -1
  44. package/esm/providers/index.d.ts +1 -1
  45. package/esm/providers/index.d.ts.map +1 -1
  46. package/esm/providers/index.js.map +1 -1
  47. package/esm/providers/tambo-provider.d.ts +1 -0
  48. package/esm/providers/tambo-provider.d.ts.map +1 -1
  49. package/esm/providers/tambo-provider.js +3 -2
  50. package/esm/providers/tambo-provider.js.map +1 -1
  51. package/esm/providers/tambo-thread-provider.d.ts +13 -0
  52. package/esm/providers/tambo-thread-provider.d.ts.map +1 -1
  53. package/esm/providers/tambo-thread-provider.js +51 -22
  54. package/esm/providers/tambo-thread-provider.js.map +1 -1
  55. package/package.json +11 -14
@@ -4,6 +4,7 @@ exports.MCPClient = exports.MCPTransport = void 0;
4
4
  const index_js_1 = require("@modelcontextprotocol/sdk/client/index.js");
5
5
  const sse_js_1 = require("@modelcontextprotocol/sdk/client/sse.js");
6
6
  const streamableHttp_js_1 = require("@modelcontextprotocol/sdk/client/streamableHttp.js");
7
+ const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
7
8
  var MCPTransport;
8
9
  (function (MCPTransport) {
9
10
  MCPTransport["SSE"] = "sse";
@@ -27,6 +28,7 @@ class MCPClient {
27
28
  endpoint;
28
29
  headers;
29
30
  authProvider;
31
+ handlers;
30
32
  /**
31
33
  * Tracks an in-flight reconnect so concurrent triggers coalesce
32
34
  * (single-flight). When set, additional calls to `reconnect()` or
@@ -62,17 +64,19 @@ class MCPClient {
62
64
  static BACKOFF_MULTIPLIER = 2;
63
65
  static BACKOFF_MAX_MS = 30_000;
64
66
  static BACKOFF_JITTER_RATIO = 0.2;
67
+ elicitation = new EventTarget();
65
68
  /**
66
69
  * Private constructor to enforce using the static create method.
67
70
  * @param endpoint - The URL of the MCP server to connect to
68
71
  * @param transportType - The transport to use for the MCP client
69
72
  * @param headers - Optional custom headers to include in requests
70
73
  */
71
- constructor(endpoint, transportType, headers, authProvider, sessionId) {
74
+ constructor(endpoint, transportType, headers, authProvider, sessionId, handlers = {}) {
72
75
  this.endpoint = endpoint;
73
76
  this.headers = headers ?? {};
74
77
  this.authProvider = authProvider;
75
78
  this.transportType = transportType;
79
+ this.handlers = handlers;
76
80
  this.transport = this.initializeTransport(sessionId);
77
81
  this.client = this.initializeClient();
78
82
  }
@@ -89,8 +93,8 @@ class MCPClient {
89
93
  * @returns A connected MCPClient instance ready for use
90
94
  * @throws {Error} Will throw an error if connection fails
91
95
  */
92
- static async create(endpoint, transportType = MCPTransport.HTTP, headers, authProvider, sessionId) {
93
- const mcpClient = new MCPClient(endpoint, transportType, headers, authProvider, sessionId);
96
+ static async create(endpoint, transportType = MCPTransport.HTTP, headers, authProvider, sessionId, handlers = {}) {
97
+ const mcpClient = new MCPClient(endpoint, transportType, headers, authProvider, sessionId, handlers);
94
98
  await mcpClient.client.connect(mcpClient.transport);
95
99
  if ("sessionId" in mcpClient.transport) {
96
100
  mcpClient.sessionId = mcpClient.transport.sessionId;
@@ -233,12 +237,31 @@ class MCPClient {
233
237
  });
234
238
  }
235
239
  }
240
+ /**
241
+ * Initializes the MCP client with the appropriate capabilities and handlers
242
+ * @returns The initialized MCP client
243
+ */
236
244
  initializeClient() {
245
+ const elicitationCapability = this.handlers.elicitation
246
+ ? { elicitation: {} }
247
+ : {};
248
+ const samplingCapability = this.handlers.sampling ? { sampling: {} } : {};
237
249
  const client = new index_js_1.Client({
238
250
  name: "tambo-mcp-client",
239
251
  version: "1.0.0",
252
+ }, {
253
+ capabilities: {
254
+ ...elicitationCapability,
255
+ ...samplingCapability,
256
+ },
240
257
  });
241
258
  client.onclose = this.onclose.bind(this);
259
+ if (this.handlers.elicitation) {
260
+ client.setRequestHandler(types_js_1.ElicitRequestSchema, this.handlers.elicitation);
261
+ }
262
+ if (this.handlers.sampling) {
263
+ client.setRequestHandler(types_js_1.CreateMessageRequestSchema, this.handlers.sampling);
264
+ }
242
265
  return client;
243
266
  }
244
267
  /**
@@ -254,7 +277,9 @@ class MCPClient {
254
277
  while (hasMore) {
255
278
  const response = await this.client.listTools({ cursor }, {});
256
279
  allTools.push(...response.tools.map((tool) => {
257
- if (tool.inputSchema.type !== "object") {
280
+ // make sure the right type is used
281
+ const inputSchemaType = tool.inputSchema.type;
282
+ if (inputSchemaType !== "object") {
258
283
  throw new Error(`Input schema for tool ${tool.name} is not an object`);
259
284
  }
260
285
  return {
@@ -295,6 +320,40 @@ class MCPClient {
295
320
  });
296
321
  return result;
297
322
  }
323
+ updateElicitationHandler(handler) {
324
+ // Because we advertise the elicitation capability on initial connection, we can only update
325
+ // an existing handler, not add it if we haven't set it yet.
326
+ if (handler && !this.handlers.elicitation) {
327
+ throw new Error("Elicitation handler must be set on create");
328
+ }
329
+ this.handlers = {
330
+ ...this.handlers,
331
+ elicitation: handler,
332
+ };
333
+ if (!handler) {
334
+ const method = types_js_1.ElicitRequestSchema.shape.method.value;
335
+ this.client.removeRequestHandler(method);
336
+ return;
337
+ }
338
+ this.client.setRequestHandler(types_js_1.ElicitRequestSchema, handler);
339
+ }
340
+ updateSamplingHandler(handler) {
341
+ // Because we advertise the sampling capability on initial connection, we can only update
342
+ // an existing handler, not add it if we haven't set it yet.
343
+ if (handler && !this.handlers.sampling) {
344
+ throw new Error("Sampling handler must be set on create");
345
+ }
346
+ this.handlers = {
347
+ ...this.handlers,
348
+ sampling: handler,
349
+ };
350
+ if (!handler) {
351
+ const method = types_js_1.CreateMessageRequestSchema.shape.method.value;
352
+ this.client.removeRequestHandler(method);
353
+ return;
354
+ }
355
+ this.client.setRequestHandler(types_js_1.CreateMessageRequestSchema, handler);
356
+ }
298
357
  }
299
358
  exports.MCPClient = MCPClient;
300
359
  //# sourceMappingURL=mcp-client.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"mcp-client.js","sourceRoot":"","sources":["../../src/mcp/mcp-client.ts"],"names":[],"mappings":";;;AACA,wEAAmE;AACnE,oEAA6E;AAC7E,0FAAmG;AAGnG,IAAY,YAGX;AAHD,WAAY,YAAY;IACtB,2BAAW,CAAA;IACX,6BAAa,CAAA;AACf,CAAC,EAHW,YAAY,4BAAZ,YAAY,QAGvB;AACD;;;;;;;;;GASG;AACH,MAAa,SAAS;IACZ,MAAM,CAAS;IACf,SAAS,CAAqD;IAC9D,aAAa,CAAe;IAC7B,SAAS,CAAU;IAClB,QAAQ,CAAS;IACjB,OAAO,CAAyB;IAChC,YAAY,CAAuB;IAC3C;;;;;OAKG;IACK,YAAY,CAAiB;IACrC;;;OAGG;IACK,cAAc,CAAiC;IACvD;;;OAGG;IACK,eAAe,GAAG,CAAC,CAAC;IAE5B;;;;;;;;;;;;;OAaG;IACH,MAAM,CAAU,kBAAkB,GAAG,GAAG,CAAC;IACzC,MAAM,CAAU,kBAAkB,GAAG,CAAC,CAAC;IACvC,MAAM,CAAU,cAAc,GAAG,MAAM,CAAC;IACxC,MAAM,CAAU,oBAAoB,GAAG,GAAG,CAAC;IAE3C;;;;;OAKG;IACH,YACE,QAAgB,EAChB,aAA2B,EAC3B,OAAgC,EAChC,YAAkC,EAClC,SAAkB;QAElB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,CAAC,OAAO,GAAG,OAAO,IAAI,EAAE,CAAC;QAC7B,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC;QACjC,IAAI,CAAC,aAAa,GAAG,aAAa,CA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I;SAChB,CAAC,CAAC;QACH,OAAO,MAAM,CAAC;IAChB,CAAC;;AAzUH,8BA0UC","sourcesContent":["import { type OAuthClientProvider } from \"@modelcontextprotocol/sdk/client/auth.js\";\nimport { 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 private transportType: MCPTransport;\n public sessionId?: string;\n private endpoint: string;\n private headers: Record<string, string>;\n private authProvider?: OAuthClientProvider;\n /**\n * Tracks an in-flight reconnect so concurrent triggers coalesce\n * (single-flight). When set, additional calls to `reconnect()` or\n * the automatic `onclose` handler will await the same Promise instead of\n * starting another reconnect sequence.\n */\n private reconnecting?: Promise<void>;\n /**\n * Timer id for a scheduled automatic reconnect (used by `onclose`).\n * Present only while waiting for the backoff delay to elapse.\n */\n private reconnectTimer?: ReturnType<typeof setTimeout>;\n /**\n * Count of consecutive automatic reconnect failures used to compute\n * exponential backoff. Reset to 0 after a successful connection.\n */\n private backoffAttempts = 0;\n\n /**\n * Backoff policy (discoverable constants)\n * - BACKOFF_INITIAL_MS: initial delay for the first automatic retry\n * - BACKOFF_MULTIPLIER: exponential growth factor for each failed attempt\n * - BACKOFF_MAX_MS: upper bound for the delay\n * - BACKOFF_JITTER_RATIO: jitter range as a fraction of the base delay\n *\n * Jitter is applied symmetrically in [-ratio, +ratio]. For example, with a\n * 500ms base delay and 0.2 ratio, the actual delay is in [400ms, 600ms].\n *\n * The backoff applies only to automatic reconnects started from the\n * `onclose` handler. Explicit/manual calls to `reconnect()` run immediately\n * (no backoff), and will preempt any scheduled automatic attempt.\n */\n static readonly BACKOFF_INITIAL_MS = 500;\n static readonly BACKOFF_MULTIPLIER = 2;\n static readonly BACKOFF_MAX_MS = 30_000;\n static readonly BACKOFF_JITTER_RATIO = 0.2;\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 transportType - 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 transportType: MCPTransport,\n headers?: Record<string, string>,\n authProvider?: OAuthClientProvider,\n sessionId?: string,\n ) {\n this.endpoint = endpoint;\n this.headers = headers ?? {};\n this.authProvider = authProvider;\n this.transportType = transportType;\n this.transport = this.initializeTransport(sessionId);\n this.client = this.initializeClient();\n }\n\n /**\n * Creates and initializes a new MCPClient instance. This is the recommended\n * way to create an MCPClient as it handles both instantiation and connection\n * setup.\n * @param endpoint - The URL of the MCP server to connect to\n * @param transportType - The transport type to use for the MCP client. Defaults to HTTP.\n * @param headers - Optional custom headers to include in requests\n * @param authProvider - Optional auth provider to use for authentication\n * @param sessionId - Optional session id to use for the MCP client - if not\n * provided, a new session will be created\n * @returns A connected MCPClient instance ready for use\n * @throws {Error} Will throw an error if connection fails\n */\n static async create(\n endpoint: string,\n transportType: MCPTransport = MCPTransport.HTTP,\n headers: Record<string, string> | undefined,\n authProvider: OAuthClientProvider | undefined,\n sessionId: string | undefined,\n ): Promise<MCPClient> {\n const mcpClient = new MCPClient(\n endpoint,\n transportType,\n headers,\n authProvider,\n sessionId,\n );\n await mcpClient.client.connect(mcpClient.transport);\n if (\"sessionId\" in mcpClient.transport) {\n mcpClient.sessionId = mcpClient.transport.sessionId;\n }\n return mcpClient;\n }\n /**\n * Reconnects to the MCP server, optionally retaining the same session ID.\n *\n * Single‑flight semantics:\n * - If a reconnect is already in progress (triggered either manually or by\n * the automatic `onclose` handler), additional calls will await the\n * in-flight reconnect rather than start another one.\n * - If an automatic reconnect has been scheduled but not yet started (i.e.,\n * we are waiting in a backoff delay), calling `reconnect()` manually will\n * cancel the scheduled attempt and perform an immediate reconnect.\n *\n * Backoff policy:\n * - Backoff delays with jitter are applied only for automatic reconnects\n * (via `onclose`). Manual calls to `reconnect()` do not use backoff.\n * @param newSession - Whether to create a new session (true) or reuse existing session ID (false)\n * @param reportErrorOnClose - Whether to report errors when closing the client\n * Note that only StreamableHTTPClientTransport supports session IDs.\n * @returns A promise that resolves when the reconnect is complete\n */\n async reconnect(newSession = false, reportErrorOnClose = true) {\n // If a reconnect is already running, coalesce into it.\n if (this.reconnecting) {\n return await this.reconnecting;\n }\n\n // Manual reconnect preempts any scheduled automatic attempt.\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = undefined;\n }\n\n const doReconnect = async () => {\n const sessionId = newSession ? undefined : this.sessionId;\n\n // Prevent re-entrant onclose during deliberate close by detaching\n // the handler from the previous client instance.\n const prevClient = this.client;\n // Prevent re-entrant onclose callbacks from the previous client\n prevClient.onclose = undefined;\n\n try {\n await prevClient.close();\n } catch (error) {\n if (reportErrorOnClose) {\n console.error(\"Error closing Tambo MCP Client:\", error);\n }\n }\n\n this.transport = this.initializeTransport(sessionId);\n this.client = this.initializeClient();\n await this.client.connect(this.transport);\n // We may have gotten a session id from the server, so we need to set it\n if (\"sessionId\" in this.transport) {\n this.sessionId = this.transport.sessionId;\n if (sessionId !== this.sessionId) {\n // This is a pretty unusual thing to happen, but it might be possible?\n console.warn(\"Session id mismatch\", sessionId, this.sessionId);\n }\n }\n };\n\n this.reconnecting = (async () => {\n try {\n await doReconnect();\n // Successful manual reconnect: reset backoff.\n this.backoffAttempts = 0;\n } finally {\n this.reconnecting = undefined;\n }\n })();\n\n return await this.reconnecting;\n }\n\n /**\n * Called by the underlying MCP SDK when the connection closes.\n * Schedules an automatic reconnect with bounded exponential backoff and\n * jitter. If a reconnect is already scheduled or running, this is a no-op.\n */\n private onclose() {\n this.scheduleAutoReconnect();\n }\n\n /**\n * Compute the next backoff delay with symmetric jitter.\n * @returns The next backoff delay in milliseconds\n */\n private computeBackoffDelayMs(): number {\n const base = Math.min(\n MCPClient.BACKOFF_MAX_MS,\n MCPClient.BACKOFF_INITIAL_MS *\n Math.pow(MCPClient.BACKOFF_MULTIPLIER, this.backoffAttempts),\n );\n const jitterRange = MCPClient.BACKOFF_JITTER_RATIO * base;\n const jitter = (Math.random() * 2 - 1) * jitterRange; // [-range, +range]\n const ms = Math.max(0, Math.round(base + jitter));\n return ms;\n }\n\n /**\n * Schedule an automatic reconnect attempt if one is not already scheduled\n * or running. Uses the backoff policy and self-reschedules on failure.\n */\n private scheduleAutoReconnect() {\n if (this.reconnecting || this.reconnectTimer) {\n return;\n }\n\n const delayMs = this.computeBackoffDelayMs();\n console.warn(\n \"Tambo MCP Client closed; attempting automatic reconnect in\",\n `${delayMs}ms`,\n );\n\n this.reconnectTimer = setTimeout(async () => {\n this.reconnectTimer = undefined;\n // Start the actual reconnect (single-flight)\n const inFlight = (this.reconnecting = this.reconnect(false, false));\n try {\n await inFlight;\n // Success: reset attempts\n this.backoffAttempts = 0;\n } catch (err) {\n // Failure: increase attempts; scheduling occurs in finally below so the\n // new timer isn't blocked by `this.reconnecting` being truthy.\n this.backoffAttempts += 1;\n console.warn(\n \"Automatic reconnect failed; will retry with backoff.\",\n err,\n );\n } finally {\n this.reconnecting = undefined;\n if (this.backoffAttempts > 0) {\n this.scheduleAutoReconnect();\n }\n }\n }, delayMs);\n }\n\n private initializeTransport(sessionId: string | undefined) {\n if (this.transportType === MCPTransport.SSE) {\n return new SSEClientTransport(new URL(this.endpoint), {\n authProvider: this.authProvider,\n requestInit: { headers: this.headers },\n });\n } else {\n return new StreamableHTTPClientTransport(new URL(this.endpoint), {\n authProvider: this.authProvider,\n requestInit: { headers: this.headers },\n sessionId,\n });\n }\n }\n\n private initializeClient() {\n const client = new Client({\n name: \"tambo-mcp-client\",\n version: \"1.0.0\",\n });\n client.onclose = this.onclose.bind(this);\n return client;\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 {Error} 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 getServerCapabilities() {\n return this.client.getServerCapabilities();\n }\n\n getServerVersion() {\n return this.client.getServerVersion();\n }\n\n getInstructions() {\n return this.client.getInstructions();\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 {Error} Will throw an error if the tool call fails or if arguments are invalid\n */\n async callTool(\n name: string,\n args: Record<string, unknown>,\n ): Promise<MCPToolCallResult> {\n const result = await this.client.callTool({\n name,\n arguments: args,\n });\n return result;\n }\n}\n\n/**\n * The result of a tool call.\n * This is the same as the result of a tool call in the OpenAI SDK, but is reified here\n */\nexport type MCPToolCallResult = Awaited<\n ReturnType<typeof Client.prototype.callTool>\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?: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AO;QACT,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,8BAAmB,EAAE,OAAO,CAAC,CAAC;IAC9D,CAAC;IAED,qBAAqB,CACnB,OAEa;QAEb,yFAAyF;QACzF,4DAA4D;QAC5D,IAAI,OAAO,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,QAAQ,EAAE,CAAC;YACvC,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;QAC5D,CAAC;QACD,IAAI,CAAC,QAAQ,GAAG;YACd,GAAG,IAAI,CAAC,QAAQ;YAChB,QAAQ,EAAE,OAAO;SAClB,CAAC;QACF,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,MAAM,MAAM,GAAG,qCAA0B,CAAC,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC;YAC7D,IAAI,CAAC,MAAM,CAAC,oBAAoB,CAAC,MAAM,CAAC,CAAC;YACzC,OAAO;QACT,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,iBAAiB,CAAC,qCAA0B,EAAE,OAAO,CAAC,CAAC;IACrE,CAAC;;AApZH,8BAqZC","sourcesContent":["import { type OAuthClientProvider } from \"@modelcontextprotocol/sdk/client/auth.js\";\nimport { Client } from \"@modelcontextprotocol/sdk/client/index.js\";\nimport { SSEClientTransport } from \"@modelcontextprotocol/sdk/client/sse.js\";\nimport { StreamableHTTPClientTransport } from \"@modelcontextprotocol/sdk/client/streamableHttp.js\";\nimport {\n CreateMessageRequest,\n CreateMessageRequestSchema,\n CreateMessageResult,\n ElicitRequest,\n ElicitRequestSchema,\n ElicitResult,\n} from \"@modelcontextprotocol/sdk/types.js\";\nimport { JSONSchema7 } from \"json-schema\";\n\nexport enum MCPTransport {\n SSE = \"sse\",\n HTTP = \"http\",\n}\n\n/**\n * Handlers for MCP requests - these are only used if the server supports the corresponding capabilities\n * @param elicitation - Handler for elicitation requests\n * @param sampling - Handler for sampling requests\n * @example\n * ```typescript\n * const mcp = await MCPClient.create(\n * 'https://api.example.com/mcp',\n * MCPTransport.HTTP,\n * {},\n * undefined,\n * undefined,\n * {\n * elicitation: (e: ElicitRequest) => Promise.resolve({...}),\n * },\n * });\n * ```\n */\ninterface MCPHandlers {\n elicitation?: (e: ElicitRequest) => Promise<ElicitResult>;\n sampling?: (e: CreateMessageRequest) => Promise<CreateMessageResult>;\n}\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 private transportType: MCPTransport;\n public sessionId?: string;\n private endpoint: string;\n private headers: Record<string, string>;\n private authProvider?: OAuthClientProvider;\n private handlers: MCPHandlers;\n /**\n * Tracks an in-flight reconnect so concurrent triggers coalesce\n * (single-flight). When set, additional calls to `reconnect()` or\n * the automatic `onclose` handler will await the same Promise instead of\n * starting another reconnect sequence.\n */\n private reconnecting?: Promise<void>;\n /**\n * Timer id for a scheduled automatic reconnect (used by `onclose`).\n * Present only while waiting for the backoff delay to elapse.\n */\n private reconnectTimer?: ReturnType<typeof setTimeout>;\n /**\n * Count of consecutive automatic reconnect failures used to compute\n * exponential backoff. Reset to 0 after a successful connection.\n */\n private backoffAttempts = 0;\n\n /**\n * Backoff policy (discoverable constants)\n * - BACKOFF_INITIAL_MS: initial delay for the first automatic retry\n * - BACKOFF_MULTIPLIER: exponential growth factor for each failed attempt\n * - BACKOFF_MAX_MS: upper bound for the delay\n * - BACKOFF_JITTER_RATIO: jitter range as a fraction of the base delay\n *\n * Jitter is applied symmetrically in [-ratio, +ratio]. For example, with a\n * 500ms base delay and 0.2 ratio, the actual delay is in [400ms, 600ms].\n *\n * The backoff applies only to automatic reconnects started from the\n * `onclose` handler. Explicit/manual calls to `reconnect()` run immediately\n * (no backoff), and will preempt any scheduled automatic attempt.\n */\n static readonly BACKOFF_INITIAL_MS = 500;\n static readonly BACKOFF_MULTIPLIER = 2;\n static readonly BACKOFF_MAX_MS = 30_000;\n static readonly BACKOFF_JITTER_RATIO = 0.2;\n\n public elicitation: EventTarget = new EventTarget();\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 transportType - 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 transportType: MCPTransport,\n headers?: Record<string, string>,\n authProvider?: OAuthClientProvider,\n sessionId?: string,\n handlers: MCPHandlers = {},\n ) {\n this.endpoint = endpoint;\n this.headers = headers ?? {};\n this.authProvider = authProvider;\n this.transportType = transportType;\n this.handlers = handlers;\n this.transport = this.initializeTransport(sessionId);\n this.client = this.initializeClient();\n }\n\n /**\n * Creates and initializes a new MCPClient instance. This is the recommended\n * way to create an MCPClient as it handles both instantiation and connection\n * setup.\n * @param endpoint - The URL of the MCP server to connect to\n * @param transportType - The transport type to use for the MCP client. Defaults to HTTP.\n * @param headers - Optional custom headers to include in requests\n * @param authProvider - Optional auth provider to use for authentication\n * @param sessionId - Optional session id to use for the MCP client - if not\n * provided, a new session will be created\n * @returns A connected MCPClient instance ready for use\n * @throws {Error} Will throw an error if connection fails\n */\n static async create(\n endpoint: string,\n transportType: MCPTransport = MCPTransport.HTTP,\n headers: Record<string, string> | undefined,\n authProvider: OAuthClientProvider | undefined,\n sessionId: string | undefined,\n handlers: MCPHandlers = {},\n ): Promise<MCPClient> {\n const mcpClient = new MCPClient(\n endpoint,\n transportType,\n headers,\n authProvider,\n sessionId,\n handlers,\n );\n await mcpClient.client.connect(mcpClient.transport);\n if (\"sessionId\" in mcpClient.transport) {\n mcpClient.sessionId = mcpClient.transport.sessionId;\n }\n return mcpClient;\n }\n /**\n * Reconnects to the MCP server, optionally retaining the same session ID.\n *\n * Single‑flight semantics:\n * - If a reconnect is already in progress (triggered either manually or by\n * the automatic `onclose` handler), additional calls will await the\n * in-flight reconnect rather than start another one.\n * - If an automatic reconnect has been scheduled but not yet started (i.e.,\n * we are waiting in a backoff delay), calling `reconnect()` manually will\n * cancel the scheduled attempt and perform an immediate reconnect.\n *\n * Backoff policy:\n * - Backoff delays with jitter are applied only for automatic reconnects\n * (via `onclose`). Manual calls to `reconnect()` do not use backoff.\n * @param newSession - Whether to create a new session (true) or reuse existing session ID (false)\n * @param reportErrorOnClose - Whether to report errors when closing the client\n * Note that only StreamableHTTPClientTransport supports session IDs.\n * @returns A promise that resolves when the reconnect is complete\n */\n async reconnect(newSession = false, reportErrorOnClose = true) {\n // If a reconnect is already running, coalesce into it.\n if (this.reconnecting) {\n return await this.reconnecting;\n }\n\n // Manual reconnect preempts any scheduled automatic attempt.\n if (this.reconnectTimer) {\n clearTimeout(this.reconnectTimer);\n this.reconnectTimer = undefined;\n }\n\n const doReconnect = async () => {\n const sessionId = newSession ? undefined : this.sessionId;\n\n // Prevent re-entrant onclose during deliberate close by detaching\n // the handler from the previous client instance.\n const prevClient = this.client;\n // Prevent re-entrant onclose callbacks from the previous client\n prevClient.onclose = undefined;\n\n try {\n await prevClient.close();\n } catch (error) {\n if (reportErrorOnClose) {\n console.error(\"Error closing Tambo MCP Client:\", error);\n }\n }\n\n this.transport = this.initializeTransport(sessionId);\n this.client = this.initializeClient();\n await this.client.connect(this.transport);\n // We may have gotten a session id from the server, so we need to set it\n if (\"sessionId\" in this.transport) {\n this.sessionId = this.transport.sessionId;\n if (sessionId !== this.sessionId) {\n // This is a pretty unusual thing to happen, but it might be possible?\n console.warn(\"Session id mismatch\", sessionId, this.sessionId);\n }\n }\n };\n\n this.reconnecting = (async () => {\n try {\n await doReconnect();\n // Successful manual reconnect: reset backoff.\n this.backoffAttempts = 0;\n } finally {\n this.reconnecting = undefined;\n }\n })();\n\n return await this.reconnecting;\n }\n\n /**\n * Called by the underlying MCP SDK when the connection closes.\n * Schedules an automatic reconnect with bounded exponential backoff and\n * jitter. If a reconnect is already scheduled or running, this is a no-op.\n */\n private onclose() {\n this.scheduleAutoReconnect();\n }\n\n /**\n * Compute the next backoff delay with symmetric jitter.\n * @returns The next backoff delay in milliseconds\n */\n private computeBackoffDelayMs(): number {\n const base = Math.min(\n MCPClient.BACKOFF_MAX_MS,\n MCPClient.BACKOFF_INITIAL_MS *\n Math.pow(MCPClient.BACKOFF_MULTIPLIER, this.backoffAttempts),\n );\n const jitterRange = MCPClient.BACKOFF_JITTER_RATIO * base;\n const jitter = (Math.random() * 2 - 1) * jitterRange; // [-range, +range]\n const ms = Math.max(0, Math.round(base + jitter));\n return ms;\n }\n\n /**\n * Schedule an automatic reconnect attempt if one is not already scheduled\n * or running. Uses the backoff policy and self-reschedules on failure.\n */\n private scheduleAutoReconnect() {\n if (this.reconnecting || this.reconnectTimer) {\n return;\n }\n\n const delayMs = this.computeBackoffDelayMs();\n console.warn(\n \"Tambo MCP Client closed; attempting automatic reconnect in\",\n `${delayMs}ms`,\n );\n\n this.reconnectTimer = setTimeout(async () => {\n this.reconnectTimer = undefined;\n // Start the actual reconnect (single-flight)\n const inFlight = (this.reconnecting = this.reconnect(false, false));\n try {\n await inFlight;\n // Success: reset attempts\n this.backoffAttempts = 0;\n } catch (err) {\n // Failure: increase attempts; scheduling occurs in finally below so the\n // new timer isn't blocked by `this.reconnecting` being truthy.\n this.backoffAttempts += 1;\n console.warn(\n \"Automatic reconnect failed; will retry with backoff.\",\n err,\n );\n } finally {\n this.reconnecting = undefined;\n if (this.backoffAttempts > 0) {\n this.scheduleAutoReconnect();\n }\n }\n }, delayMs);\n }\n\n private initializeTransport(sessionId: string | undefined) {\n if (this.transportType === MCPTransport.SSE) {\n return new SSEClientTransport(new URL(this.endpoint), {\n authProvider: this.authProvider,\n requestInit: { headers: this.headers },\n });\n } else {\n return new StreamableHTTPClientTransport(new URL(this.endpoint), {\n authProvider: this.authProvider,\n requestInit: { headers: this.headers },\n sessionId,\n });\n }\n }\n\n /**\n * Initializes the MCP client with the appropriate capabilities and handlers\n * @returns The initialized MCP client\n */\n private initializeClient() {\n const elicitationCapability = this.handlers.elicitation\n ? { elicitation: {} }\n : {};\n const samplingCapability = this.handlers.sampling ? { sampling: {} } : {};\n const client = new Client(\n {\n name: \"tambo-mcp-client\",\n version: \"1.0.0\",\n },\n {\n capabilities: {\n ...elicitationCapability,\n ...samplingCapability,\n },\n },\n );\n client.onclose = this.onclose.bind(this);\n if (this.handlers.elicitation) {\n client.setRequestHandler(ElicitRequestSchema, this.handlers.elicitation);\n }\n if (this.handlers.sampling) {\n client.setRequestHandler(\n CreateMessageRequestSchema,\n this.handlers.sampling,\n );\n }\n return client;\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 {Error} 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 // make sure the right type is used\n const inputSchemaType: string = tool.inputSchema.type;\n if (inputSchemaType !== \"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 getServerCapabilities() {\n return this.client.getServerCapabilities();\n }\n\n getServerVersion() {\n return this.client.getServerVersion();\n }\n\n getInstructions() {\n return this.client.getInstructions();\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 {Error} Will throw an error if the tool call fails or if arguments are invalid\n */\n async callTool(\n name: string,\n args: Record<string, unknown>,\n ): Promise<MCPToolCallResult> {\n const result = await this.client.callTool({\n name,\n arguments: args,\n });\n return result;\n }\n\n updateElicitationHandler(\n handler: ((e: ElicitRequest) => Promise<ElicitResult>) | undefined,\n ) {\n // Because we advertise the elicitation capability on initial connection, we can only update\n // an existing handler, not add it if we haven't set it yet.\n if (handler && !this.handlers.elicitation) {\n throw new Error(\"Elicitation handler must be set on create\");\n }\n this.handlers = {\n ...this.handlers,\n elicitation: handler,\n };\n if (!handler) {\n const method = ElicitRequestSchema.shape.method.value;\n this.client.removeRequestHandler(method);\n return;\n }\n this.client.setRequestHandler(ElicitRequestSchema, handler);\n }\n\n updateSamplingHandler(\n handler:\n | ((e: CreateMessageRequest) => Promise<CreateMessageResult>)\n | undefined,\n ) {\n // Because we advertise the sampling capability on initial connection, we can only update\n // an existing handler, not add it if we haven't set it yet.\n if (handler && !this.handlers.sampling) {\n throw new Error(\"Sampling handler must be set on create\");\n }\n this.handlers = {\n ...this.handlers,\n sampling: handler,\n };\n if (!handler) {\n const method = CreateMessageRequestSchema.shape.method.value;\n this.client.removeRequestHandler(method);\n return;\n }\n this.client.setRequestHandler(CreateMessageRequestSchema, handler);\n }\n}\n\n/**\n * The result of a tool call.\n * This is the same as the result of a tool call in the OpenAI SDK, but is reified here\n */\nexport type MCPToolCallResult = Awaited<\n ReturnType<typeof Client.prototype.callTool>\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,2 @@
1
+ export {};
2
+ //# sourceMappingURL=tambo-thread-provider-initial-messages.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tambo-thread-provider-initial-messages.test.d.ts","sourceRoot":"","sources":["../../../src/providers/__tests__/tambo-thread-provider-initial-messages.test.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,179 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const typescript_sdk_1 = require("@tambo-ai/typescript-sdk");
7
+ const react_1 = require("@testing-library/react");
8
+ const react_2 = __importDefault(require("react"));
9
+ const generate_component_response_1 = require("../../model/generate-component-response");
10
+ const tambo_client_provider_1 = require("../tambo-client-provider");
11
+ const tambo_context_helpers_provider_1 = require("../tambo-context-helpers-provider");
12
+ const tambo_registry_provider_1 = require("../tambo-registry-provider");
13
+ const tambo_thread_provider_1 = require("../tambo-thread-provider");
14
+ // Mock crypto.randomUUID
15
+ Object.defineProperty(global, "crypto", {
16
+ value: {
17
+ randomUUID: jest.fn().mockReturnValue("test-uuid"),
18
+ },
19
+ });
20
+ // Mock the required providers
21
+ jest.mock("../tambo-client-provider", () => ({
22
+ useTamboClient: jest.fn(),
23
+ useTamboQueryClient: jest.fn(),
24
+ }));
25
+ jest.mock("@tambo-ai/typescript-sdk", () => ({
26
+ advanceStream: jest.fn(),
27
+ }));
28
+ // Test utilities
29
+ const createMockMessage = (overrides = {}) => ({
30
+ id: "test-message-1",
31
+ content: [{ type: "text", text: "Hello" }],
32
+ role: "user",
33
+ threadId: "test-thread-1",
34
+ createdAt: new Date().toISOString(),
35
+ componentState: {},
36
+ ...overrides,
37
+ });
38
+ // Test wrapper
39
+ const createWrapper = (initialMessages = []) => {
40
+ const TestWrapper = ({ children }) => (react_2.default.createElement(tambo_registry_provider_1.TamboRegistryProvider, { components: [], tools: [] },
41
+ react_2.default.createElement(tambo_context_helpers_provider_1.TamboContextHelpersProvider, null,
42
+ react_2.default.createElement(tambo_thread_provider_1.TamboThreadProvider, { initialMessages: initialMessages }, children))));
43
+ TestWrapper.displayName = "TestWrapper";
44
+ return TestWrapper;
45
+ };
46
+ describe("TamboThreadProvider with initial messages", () => {
47
+ const mockClient = {
48
+ beta: {
49
+ threads: {
50
+ advance: jest.fn(),
51
+ advanceByID: jest.fn(),
52
+ cancel: jest.fn(),
53
+ messages: {
54
+ create: jest.fn(),
55
+ },
56
+ },
57
+ },
58
+ };
59
+ beforeEach(() => {
60
+ jest.clearAllMocks();
61
+ tambo_client_provider_1.useTamboClient.mockReturnValue(mockClient);
62
+ // Provide a minimal mock for the query client used by the provider
63
+ const mockQueryClient = {
64
+ setQueryData: jest.fn(),
65
+ invalidateQueries: jest.fn(),
66
+ };
67
+ tambo_client_provider_1.useTamboQueryClient.mockReturnValue(mockQueryClient);
68
+ typescript_sdk_1.advanceStream.mockImplementation(async function* () {
69
+ yield {
70
+ responseMessageDto: {
71
+ id: "response-1",
72
+ role: "assistant",
73
+ content: [{ type: "text", text: "Hello back!" }],
74
+ threadId: "new-thread-id",
75
+ componentState: {},
76
+ createdAt: new Date().toISOString(),
77
+ },
78
+ generationStage: generate_component_response_1.GenerationStage.COMPLETE,
79
+ };
80
+ });
81
+ });
82
+ it("should initialize with empty messages when no initial messages provided", () => {
83
+ const { result } = (0, react_1.renderHook)(() => (0, tambo_thread_provider_1.useTamboThread)(), {
84
+ wrapper: createWrapper(),
85
+ });
86
+ expect(result.current.thread.messages).toEqual([]);
87
+ });
88
+ it("should initialize with provided initial messages", () => {
89
+ const initialMessages = [
90
+ createMockMessage({
91
+ id: "initial-1",
92
+ role: "system",
93
+ content: [{ type: "text", text: "You are a helpful assistant." }],
94
+ }),
95
+ createMockMessage({
96
+ id: "initial-2",
97
+ role: "user",
98
+ content: [{ type: "text", text: "Hello!" }],
99
+ }),
100
+ ];
101
+ const { result } = (0, react_1.renderHook)(() => (0, tambo_thread_provider_1.useTamboThread)(), {
102
+ wrapper: createWrapper(initialMessages),
103
+ });
104
+ expect(result.current.thread.messages).toHaveLength(2);
105
+ expect(result.current.thread.messages[0].content[0].text).toBe("You are a helpful assistant.");
106
+ expect(result.current.thread.messages[1].content[0].text).toBe("Hello!");
107
+ });
108
+ it("should include initial messages when sending a message to a new thread", async () => {
109
+ const initialMessages = [
110
+ createMockMessage({
111
+ id: "initial-1",
112
+ role: "system",
113
+ content: [{ type: "text", text: "You are a helpful assistant." }],
114
+ }),
115
+ ];
116
+ const { result } = (0, react_1.renderHook)(() => (0, tambo_thread_provider_1.useTamboThread)(), {
117
+ wrapper: createWrapper(initialMessages),
118
+ });
119
+ await (0, react_1.act)(async () => {
120
+ await result.current.sendThreadMessage("Test message");
121
+ });
122
+ // Check that advanceStream was called with initial messages
123
+ expect(typescript_sdk_1.advanceStream).toHaveBeenCalledWith(mockClient, expect.objectContaining({
124
+ initialMessages: [
125
+ {
126
+ content: [{ type: "text", text: "You are a helpful assistant." }],
127
+ role: "system",
128
+ additionalContext: undefined,
129
+ },
130
+ ],
131
+ }), undefined);
132
+ });
133
+ it("should not include initial messages when sending to an existing thread", async () => {
134
+ const initialMessages = [
135
+ createMockMessage({
136
+ id: "initial-1",
137
+ role: "system",
138
+ content: [{ type: "text", text: "You are a helpful assistant." }],
139
+ }),
140
+ ];
141
+ const { result } = (0, react_1.renderHook)(() => (0, tambo_thread_provider_1.useTamboThread)(), {
142
+ wrapper: createWrapper(initialMessages),
143
+ });
144
+ // Switch to an existing thread first
145
+ await (0, react_1.act)(async () => {
146
+ result.current.switchCurrentThread("existing-thread-id", false);
147
+ });
148
+ await (0, react_1.act)(async () => {
149
+ await result.current.sendThreadMessage("Test message");
150
+ });
151
+ // Check that advanceStream was called without initial messages
152
+ expect(typescript_sdk_1.advanceStream).toHaveBeenCalledWith(mockClient, expect.not.objectContaining({
153
+ initialMessages: expect.anything(),
154
+ }), "existing-thread-id");
155
+ });
156
+ it("should reset to initial messages when starting a new thread", () => {
157
+ const initialMessages = [
158
+ createMockMessage({
159
+ id: "initial-1",
160
+ role: "system",
161
+ content: [{ type: "text", text: "You are a helpful assistant." }],
162
+ }),
163
+ ];
164
+ const { result } = (0, react_1.renderHook)(() => (0, tambo_thread_provider_1.useTamboThread)(), {
165
+ wrapper: createWrapper(initialMessages),
166
+ });
167
+ // Switch to an existing thread
168
+ (0, react_1.act)(() => {
169
+ result.current.switchCurrentThread("existing-thread-id", false);
170
+ });
171
+ // Start a new thread
172
+ (0, react_1.act)(() => {
173
+ result.current.startNewThread();
174
+ });
175
+ expect(result.current.thread.messages).toHaveLength(1);
176
+ expect(result.current.thread.messages[0].content[0].text).toBe("You are a helpful assistant.");
177
+ });
178
+ });
179
+ //# sourceMappingURL=tambo-thread-provider-initial-messages.test.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tambo-thread-provider-initial-messages.test.js","sourceRoot":"","sources":["../../../src/providers/__tests__/tambo-thread-provider-initial-messages.test.tsx"],"names":[],"mappings":";;;;;AAAA,6DAAkE;AAClE,kDAAyD;AACzD,kDAA0B;AAE1B,yFAGiD;AACjD,oEAA+E;AAC/E,sFAAgF;AAChF,wEAAmE;AACnE,oEAA+E;AAI/E,yBAAyB;AACzB,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,QAAQ,EAAE;IACtC,KAAK,EAAE;QACL,UAAU,EAAE,IAAI,CAAC,EAAE,EAAE,CAAC,eAAe,CAAC,WAAW,CAAC;KACnD;CACF,CAAC,CAAC;AAEH,8BAA8B;AAC9B,IAAI,CAAC,IAAI,CAAC,0BAA0B,EAAE,GAAG,EAAE,CAAC,CAAC;IAC3C,cAAc,EAAE,IAAI,CAAC,EAAE,EAAE;IACzB,mBAAmB,EAAE,IAAI,CAAC,EAAE,EAAE;CAC/B,CAAC,CAAC,CAAC;AACJ,IAAI,CAAC,IAAI,CAAC,0BAA0B,EAAE,GAAG,EAAE,CAAC,CAAC;IAC3C,aAAa,EAAE,IAAI,CAAC,EAAE,EAAE;CACzB,CAAC,CAAC,CAAC;AAEJ,iBAAiB;AACjB,MAAM,iBAAiB,GAAG,CACxB,YAAyC,EAAE,EACvB,EAAE,CAAC,CAAC;IACxB,EAAE,EAAE,gBAAgB;IACpB,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MA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,GAAyB;YAC5C,iBAAiB,CAAC;gBAChB,EAAE,EAAE,WAAW;gBACf,IAAI,EAAE,QAAQ;gBACd,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,8BAA8B,EAAE,CAAC;aAClE,CAAC;SACH,CAAC;QAEF,MAAM,EAAE,MAAM,EAAE,GAAG,IAAA,kBAAU,EAAC,GAAG,EAAE,CAAC,IAAA,sCAAc,GAAE,EAAE;YACpD,OAAO,EAAE,aAAa,CAAC,eAAe,CAAC;SACxC,CAAC,CAAC;QAEH,+BAA+B;QAC/B,IAAA,WAAG,EAAC,GAAG,EAAE;YACP,MAAM,CAAC,OAAO,CAAC,mBAAmB,CAAC,oBAAoB,EAAE,KAAK,CAAC,CAAC;QAClE,CAAC,CAAC,CAAC;QAEH,qBAAqB;QACrB,IAAA,WAAG,EAAC,GAAG,EAAE;YACP,MAAM,CAAC,OAAO,CAAC,cAAc,EAAE,CAAC;QAClC,CAAC,CAAC,CAAC;QAEH,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;QACvD,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,CAC5D,8BAA8B,CAC/B,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC","sourcesContent":["import TamboAI, { advanceStream } from \"@tambo-ai/typescript-sdk\";\nimport { act, renderHook } from \"@testing-library/react\";\nimport React from \"react\";\nimport { DeepPartial } from \"ts-essentials\";\nimport {\n GenerationStage,\n TamboThreadMessage,\n} from \"../../model/generate-component-response\";\nimport { useTamboClient, useTamboQueryClient } from \"../tambo-client-provider\";\nimport { TamboContextHelpersProvider } from \"../tambo-context-helpers-provider\";\nimport { TamboRegistryProvider } from \"../tambo-registry-provider\";\nimport { TamboThreadProvider, useTamboThread } from \"../tambo-thread-provider\";\n\ntype PartialTamboAI = DeepPartial<TamboAI>;\n\n// Mock crypto.randomUUID\nObject.defineProperty(global, \"crypto\", {\n value: {\n randomUUID: jest.fn().mockReturnValue(\"test-uuid\"),\n },\n});\n\n// Mock the required providers\njest.mock(\"../tambo-client-provider\", () => ({\n useTamboClient: jest.fn(),\n useTamboQueryClient: jest.fn(),\n}));\njest.mock(\"@tambo-ai/typescript-sdk\", () => ({\n advanceStream: jest.fn(),\n}));\n\n// Test utilities\nconst createMockMessage = (\n overrides: Partial<TamboThreadMessage> = {},\n): TamboThreadMessage => ({\n id: \"test-message-1\",\n content: [{ type: \"text\", text: \"Hello\" }],\n role: \"user\",\n threadId: \"test-thread-1\",\n createdAt: new Date().toISOString(),\n componentState: {},\n ...overrides,\n});\n\n// Test wrapper\nconst createWrapper = (initialMessages: TamboThreadMessage[] = []) => {\n const TestWrapper = ({ children }: { children: React.ReactNode }) => (\n <TamboRegistryProvider components={[]} tools={[]}>\n <TamboContextHelpersProvider>\n <TamboThreadProvider initialMessages={initialMessages}>\n {children}\n </TamboThreadProvider>\n </TamboContextHelpersProvider>\n </TamboRegistryProvider>\n );\n TestWrapper.displayName = \"TestWrapper\";\n return TestWrapper;\n};\n\ndescribe(\"TamboThreadProvider with initial messages\", () => {\n const mockClient: PartialTamboAI = {\n beta: {\n threads: {\n advance: jest.fn(),\n advanceByID: jest.fn(),\n cancel: jest.fn(),\n messages: {\n create: jest.fn(),\n },\n },\n },\n };\n\n beforeEach(() => {\n jest.clearAllMocks();\n (useTamboClient as jest.Mock).mockReturnValue(mockClient);\n // Provide a minimal mock for the query client used by the provider\n const mockQueryClient = {\n setQueryData: jest.fn(),\n invalidateQueries: jest.fn(),\n };\n (useTamboQueryClient as jest.Mock).mockReturnValue(mockQueryClient);\n (advanceStream as jest.Mock).mockImplementation(async function* () {\n yield {\n responseMessageDto: {\n id: \"response-1\",\n role: \"assistant\",\n content: [{ type: \"text\", text: \"Hello back!\" }],\n threadId: \"new-thread-id\",\n componentState: {},\n createdAt: new Date().toISOString(),\n },\n generationStage: GenerationStage.COMPLETE,\n };\n });\n });\n\n it(\"should initialize with empty messages when no initial messages provided\", () => {\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(),\n });\n\n expect(result.current.thread.messages).toEqual([]);\n });\n\n it(\"should initialize with provided initial messages\", () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n createMockMessage({\n id: \"initial-2\",\n role: \"user\",\n content: [{ type: \"text\", text: \"Hello!\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n expect(result.current.thread.messages).toHaveLength(2);\n expect(result.current.thread.messages[0].content[0].text).toBe(\n \"You are a helpful assistant.\",\n );\n expect(result.current.thread.messages[1].content[0].text).toBe(\"Hello!\");\n });\n\n it(\"should include initial messages when sending a message to a new thread\", async () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n await act(async () => {\n await result.current.sendThreadMessage(\"Test message\");\n });\n\n // Check that advanceStream was called with initial messages\n expect(advanceStream).toHaveBeenCalledWith(\n mockClient,\n expect.objectContaining({\n initialMessages: [\n {\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n role: \"system\",\n additionalContext: undefined,\n },\n ],\n }),\n undefined,\n );\n });\n\n it(\"should not include initial messages when sending to an existing thread\", async () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n // Switch to an existing thread first\n await act(async () => {\n result.current.switchCurrentThread(\"existing-thread-id\", false);\n });\n\n await act(async () => {\n await result.current.sendThreadMessage(\"Test message\");\n });\n\n // Check that advanceStream was called without initial messages\n expect(advanceStream).toHaveBeenCalledWith(\n mockClient,\n expect.not.objectContaining({\n initialMessages: expect.anything(),\n }),\n \"existing-thread-id\",\n );\n });\n\n it(\"should reset to initial messages when starting a new thread\", () => {\n const initialMessages: TamboThreadMessage[] = [\n createMockMessage({\n id: \"initial-1\",\n role: \"system\",\n content: [{ type: \"text\", text: \"You are a helpful assistant.\" }],\n }),\n ];\n\n const { result } = renderHook(() => useTamboThread(), {\n wrapper: createWrapper(initialMessages),\n });\n\n // Switch to an existing thread\n act(() => {\n result.current.switchCurrentThread(\"existing-thread-id\", false);\n });\n\n // Start a new thread\n act(() => {\n result.current.startNewThread();\n });\n\n expect(result.current.thread.messages).toHaveLength(1);\n expect(result.current.thread.messages[0].content[0].text).toBe(\n \"You are a helpful assistant.\",\n );\n });\n});\n"]}
@@ -157,9 +157,7 @@ describe("TamboThreadProvider", () => {
157
157
  await (0, react_1.act)(async () => {
158
158
  await result.current.switchCurrentThread("test-thread-1");
159
159
  });
160
- expect(mockThreadsApi.retrieve).toHaveBeenCalledWith("test-thread-1", {
161
- includeInternal: true,
162
- });
160
+ expect(mockThreadsApi.retrieve).toHaveBeenCalledWith("test-thread-1");
163
161
  expect(result.current.thread.id).toBe("test-thread-1");
164
162
  });
165
163
  it("should start a new thread", async () => {
@@ -843,9 +841,7 @@ describe("TamboThreadProvider", () => {
843
841
  await result.current.switchCurrentThread("existing-thread-123");
844
842
  });
845
843
  // Verify that the thread retrieval was called
846
- expect(mockThreadsApi.retrieve).toHaveBeenCalledWith("existing-thread-123", {
847
- includeInternal: true,
848
- });
844
+ expect(mockThreadsApi.retrieve).toHaveBeenCalledWith("existing-thread-123");
849
845
  // Verify that neither setQueryData nor refetchQueries were called
850
846
  expect(mockQueryClient.setQueryData).not.toHaveBeenCalled();
851
847
  expect(mockQueryClient.invalidateQueries).not.toHaveBeenCalled();