@tambo-ai/react 0.72.0 → 0.73.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 (129) hide show
  1. package/dist/v1/hooks/use-tambo-v1-messages.test.js +22 -9
  2. package/dist/v1/hooks/use-tambo-v1-messages.test.js.map +1 -1
  3. package/dist/v1/hooks/use-tambo-v1-send-message.d.ts +1 -0
  4. package/dist/v1/hooks/use-tambo-v1-send-message.d.ts.map +1 -1
  5. package/dist/v1/hooks/use-tambo-v1-send-message.js +9 -2
  6. package/dist/v1/hooks/use-tambo-v1-send-message.js.map +1 -1
  7. package/dist/v1/hooks/use-tambo-v1-send-message.test.js +22 -9
  8. package/dist/v1/hooks/use-tambo-v1-send-message.test.js.map +1 -1
  9. package/dist/v1/hooks/use-tambo-v1-suggestions.d.ts +91 -0
  10. package/dist/v1/hooks/use-tambo-v1-suggestions.d.ts.map +1 -0
  11. package/dist/v1/hooks/use-tambo-v1-suggestions.js +152 -0
  12. package/dist/v1/hooks/use-tambo-v1-suggestions.js.map +1 -0
  13. package/dist/v1/hooks/use-tambo-v1-suggestions.test.d.ts +2 -0
  14. package/dist/v1/hooks/use-tambo-v1-suggestions.test.d.ts.map +1 -0
  15. package/dist/v1/hooks/use-tambo-v1-suggestions.test.js +511 -0
  16. package/dist/v1/hooks/use-tambo-v1-suggestions.test.js.map +1 -0
  17. package/dist/v1/hooks/use-tambo-v1-thread-input.d.ts +6 -57
  18. package/dist/v1/hooks/use-tambo-v1-thread-input.d.ts.map +1 -1
  19. package/dist/v1/hooks/use-tambo-v1-thread-input.js +7 -67
  20. package/dist/v1/hooks/use-tambo-v1-thread-input.js.map +1 -1
  21. package/dist/v1/hooks/use-tambo-v1-thread-input.test.js +201 -72
  22. package/dist/v1/hooks/use-tambo-v1-thread-input.test.js.map +1 -1
  23. package/dist/v1/hooks/use-tambo-v1-thread-list.d.ts +6 -4
  24. package/dist/v1/hooks/use-tambo-v1-thread-list.d.ts.map +1 -1
  25. package/dist/v1/hooks/use-tambo-v1-thread-list.js +2 -2
  26. package/dist/v1/hooks/use-tambo-v1-thread-list.js.map +1 -1
  27. package/dist/v1/hooks/use-tambo-v1-thread-list.test.js +2 -2
  28. package/dist/v1/hooks/use-tambo-v1-thread-list.test.js.map +1 -1
  29. package/dist/v1/hooks/use-tambo-v1.test.js +16 -7
  30. package/dist/v1/hooks/use-tambo-v1.test.js.map +1 -1
  31. package/dist/v1/index.d.ts +22 -13
  32. package/dist/v1/index.d.ts.map +1 -1
  33. package/dist/v1/index.js +31 -39
  34. package/dist/v1/index.js.map +1 -1
  35. package/dist/v1/providers/tambo-v1-provider.d.ts +27 -9
  36. package/dist/v1/providers/tambo-v1-provider.d.ts.map +1 -1
  37. package/dist/v1/providers/tambo-v1-provider.js +22 -11
  38. package/dist/v1/providers/tambo-v1-provider.js.map +1 -1
  39. package/dist/v1/providers/tambo-v1-provider.test.js +27 -10
  40. package/dist/v1/providers/tambo-v1-provider.test.js.map +1 -1
  41. package/dist/v1/providers/tambo-v1-stream-context.d.ts +19 -10
  42. package/dist/v1/providers/tambo-v1-stream-context.d.ts.map +1 -1
  43. package/dist/v1/providers/tambo-v1-stream-context.js +43 -53
  44. package/dist/v1/providers/tambo-v1-stream-context.js.map +1 -1
  45. package/dist/v1/providers/tambo-v1-stream-context.test.js +94 -19
  46. package/dist/v1/providers/tambo-v1-stream-context.test.js.map +1 -1
  47. package/dist/v1/providers/tambo-v1-stub-provider.d.ts +74 -0
  48. package/dist/v1/providers/tambo-v1-stub-provider.d.ts.map +1 -0
  49. package/dist/v1/providers/tambo-v1-stub-provider.js +212 -0
  50. package/dist/v1/providers/tambo-v1-stub-provider.js.map +1 -0
  51. package/dist/v1/providers/tambo-v1-stub-provider.test.d.ts +2 -0
  52. package/dist/v1/providers/tambo-v1-stub-provider.test.d.ts.map +1 -0
  53. package/dist/v1/providers/tambo-v1-stub-provider.test.js +162 -0
  54. package/dist/v1/providers/tambo-v1-stub-provider.test.js.map +1 -0
  55. package/dist/v1/providers/tambo-v1-thread-input-provider.d.ts +105 -0
  56. package/dist/v1/providers/tambo-v1-thread-input-provider.d.ts.map +1 -0
  57. package/dist/v1/providers/tambo-v1-thread-input-provider.js +191 -0
  58. package/dist/v1/providers/tambo-v1-thread-input-provider.js.map +1 -0
  59. package/dist/v1/utils/component-renderer.d.ts +15 -67
  60. package/dist/v1/utils/component-renderer.d.ts.map +1 -1
  61. package/dist/v1/utils/component-renderer.js +3 -149
  62. package/dist/v1/utils/component-renderer.js.map +1 -1
  63. package/dist/v1/utils/component-renderer.test.js +15 -350
  64. package/dist/v1/utils/component-renderer.test.js.map +1 -1
  65. package/esm/v1/hooks/use-tambo-v1-messages.test.js +22 -9
  66. package/esm/v1/hooks/use-tambo-v1-messages.test.js.map +1 -1
  67. package/esm/v1/hooks/use-tambo-v1-send-message.d.ts +1 -0
  68. package/esm/v1/hooks/use-tambo-v1-send-message.d.ts.map +1 -1
  69. package/esm/v1/hooks/use-tambo-v1-send-message.js +9 -2
  70. package/esm/v1/hooks/use-tambo-v1-send-message.js.map +1 -1
  71. package/esm/v1/hooks/use-tambo-v1-send-message.test.js +22 -9
  72. package/esm/v1/hooks/use-tambo-v1-send-message.test.js.map +1 -1
  73. package/esm/v1/hooks/use-tambo-v1-suggestions.d.ts +91 -0
  74. package/esm/v1/hooks/use-tambo-v1-suggestions.d.ts.map +1 -0
  75. package/esm/v1/hooks/use-tambo-v1-suggestions.js +149 -0
  76. package/esm/v1/hooks/use-tambo-v1-suggestions.js.map +1 -0
  77. package/esm/v1/hooks/use-tambo-v1-suggestions.test.d.ts +2 -0
  78. package/esm/v1/hooks/use-tambo-v1-suggestions.test.d.ts.map +1 -0
  79. package/esm/v1/hooks/use-tambo-v1-suggestions.test.js +506 -0
  80. package/esm/v1/hooks/use-tambo-v1-suggestions.test.js.map +1 -0
  81. package/esm/v1/hooks/use-tambo-v1-thread-input.d.ts +6 -57
  82. package/esm/v1/hooks/use-tambo-v1-thread-input.d.ts.map +1 -1
  83. package/esm/v1/hooks/use-tambo-v1-thread-input.js +5 -66
  84. package/esm/v1/hooks/use-tambo-v1-thread-input.js.map +1 -1
  85. package/esm/v1/hooks/use-tambo-v1-thread-input.test.js +199 -73
  86. package/esm/v1/hooks/use-tambo-v1-thread-input.test.js.map +1 -1
  87. package/esm/v1/hooks/use-tambo-v1-thread-list.d.ts +6 -4
  88. package/esm/v1/hooks/use-tambo-v1-thread-list.d.ts.map +1 -1
  89. package/esm/v1/hooks/use-tambo-v1-thread-list.js +2 -2
  90. package/esm/v1/hooks/use-tambo-v1-thread-list.js.map +1 -1
  91. package/esm/v1/hooks/use-tambo-v1-thread-list.test.js +2 -2
  92. package/esm/v1/hooks/use-tambo-v1-thread-list.test.js.map +1 -1
  93. package/esm/v1/hooks/use-tambo-v1.test.js +16 -7
  94. package/esm/v1/hooks/use-tambo-v1.test.js.map +1 -1
  95. package/esm/v1/index.d.ts +22 -13
  96. package/esm/v1/index.d.ts.map +1 -1
  97. package/esm/v1/index.js +23 -18
  98. package/esm/v1/index.js.map +1 -1
  99. package/esm/v1/providers/tambo-v1-provider.d.ts +27 -9
  100. package/esm/v1/providers/tambo-v1-provider.d.ts.map +1 -1
  101. package/esm/v1/providers/tambo-v1-provider.js +20 -10
  102. package/esm/v1/providers/tambo-v1-provider.js.map +1 -1
  103. package/esm/v1/providers/tambo-v1-provider.test.js +28 -11
  104. package/esm/v1/providers/tambo-v1-provider.test.js.map +1 -1
  105. package/esm/v1/providers/tambo-v1-stream-context.d.ts +19 -10
  106. package/esm/v1/providers/tambo-v1-stream-context.d.ts.map +1 -1
  107. package/esm/v1/providers/tambo-v1-stream-context.js +44 -54
  108. package/esm/v1/providers/tambo-v1-stream-context.js.map +1 -1
  109. package/esm/v1/providers/tambo-v1-stream-context.test.js +95 -20
  110. package/esm/v1/providers/tambo-v1-stream-context.test.js.map +1 -1
  111. package/esm/v1/providers/tambo-v1-stub-provider.d.ts +74 -0
  112. package/esm/v1/providers/tambo-v1-stub-provider.d.ts.map +1 -0
  113. package/esm/v1/providers/tambo-v1-stub-provider.js +176 -0
  114. package/esm/v1/providers/tambo-v1-stub-provider.js.map +1 -0
  115. package/esm/v1/providers/tambo-v1-stub-provider.test.d.ts +2 -0
  116. package/esm/v1/providers/tambo-v1-stub-provider.test.d.ts.map +1 -0
  117. package/esm/v1/providers/tambo-v1-stub-provider.test.js +157 -0
  118. package/esm/v1/providers/tambo-v1-stub-provider.test.js.map +1 -0
  119. package/esm/v1/providers/tambo-v1-thread-input-provider.d.ts +105 -0
  120. package/esm/v1/providers/tambo-v1-thread-input-provider.d.ts.map +1 -0
  121. package/esm/v1/providers/tambo-v1-thread-input-provider.js +153 -0
  122. package/esm/v1/providers/tambo-v1-thread-input-provider.js.map +1 -0
  123. package/esm/v1/utils/component-renderer.d.ts +15 -67
  124. package/esm/v1/utils/component-renderer.d.ts.map +1 -1
  125. package/esm/v1/utils/component-renderer.js +4 -146
  126. package/esm/v1/utils/component-renderer.js.map +1 -1
  127. package/esm/v1/utils/component-renderer.test.js +16 -351
  128. package/esm/v1/utils/component-renderer.test.js.map +1 -1
  129. package/package.json +2 -2
@@ -0,0 +1,153 @@
1
+ "use client";
2
+ /**
3
+ * TamboV1ThreadInputProvider - Shared Thread Input Context for v1 API
4
+ *
5
+ * Provides shared input state across all components, enabling features like
6
+ * suggestions to update the input field directly.
7
+ *
8
+ * This mirrors the beta SDK's TamboThreadInputProvider pattern.
9
+ */
10
+ import React, { createContext, useCallback, useContext, useState, } from "react";
11
+ import { useMessageImages, } from "../../hooks/use-message-images";
12
+ import { useTamboMutation, } from "../../hooks/react-query-hooks";
13
+ import { useTamboV1SendMessage } from "../hooks/use-tambo-v1-send-message";
14
+ import { useStreamState } from "./tambo-v1-stream-context";
15
+ // Error messages for various input-related error scenarios.
16
+ // TODO: Reintroduce explicit `NETWORK` and `SERVER` keys once `submit()` maps
17
+ // failures into a small, stable set of user-facing error codes (at minimum:
18
+ // connectivity failures vs non-2xx responses).
19
+ export const INPUT_ERROR_MESSAGES = {
20
+ EMPTY: "Message cannot be empty",
21
+ VALIDATION: "Invalid message format",
22
+ };
23
+ function stagedImageToResourceContent(image) {
24
+ if (!image.dataUrl.startsWith("data:")) {
25
+ throw new Error(INPUT_ERROR_MESSAGES.VALIDATION);
26
+ }
27
+ const commaIndex = image.dataUrl.indexOf(",");
28
+ if (commaIndex === -1) {
29
+ throw new Error(INPUT_ERROR_MESSAGES.VALIDATION);
30
+ }
31
+ const header = image.dataUrl.slice("data:".length, commaIndex);
32
+ const [mimeType, ...params] = header.split(";");
33
+ const isBase64 = params.includes("base64");
34
+ if (mimeType !== image.type || !isBase64) {
35
+ throw new Error(INPUT_ERROR_MESSAGES.VALIDATION);
36
+ }
37
+ return {
38
+ type: "resource",
39
+ resource: {
40
+ name: image.name,
41
+ mimeType: image.type,
42
+ // Shared.Resource.blob expects base64-encoded data; this is the base64
43
+ // payload from the `data:` URL.
44
+ blob: image.dataUrl.slice(commaIndex + 1),
45
+ },
46
+ };
47
+ }
48
+ /**
49
+ * Context for thread input state.
50
+ * @internal
51
+ */
52
+ export const TamboV1ThreadInputContext = createContext(undefined);
53
+ /**
54
+ * Provider that manages shared thread input state across all components.
55
+ *
56
+ * This ensures that useTamboV1ThreadInput, useTamboV1Suggestions, and other components
57
+ * all share the same input state.
58
+ * @param props - Provider props
59
+ * @param props.children - Child components
60
+ * @returns Thread input context provider
61
+ */
62
+ export function TamboV1ThreadInputProvider({ children }) {
63
+ const [inputValue, setInputValue] = useState("");
64
+ const [threadId, setThreadId] = useState(undefined);
65
+ const imageState = useMessageImages();
66
+ const streamState = useStreamState();
67
+ // Use the current thread from stream state if no explicit threadId is set
68
+ const inheritedThreadId = streamState.currentThreadId ?? undefined;
69
+ const effectiveThreadId = threadId ?? inheritedThreadId;
70
+ const shouldAdoptThreadId = threadId === undefined && inheritedThreadId === undefined;
71
+ const sendMessage = useTamboV1SendMessage(effectiveThreadId);
72
+ const submitFn = useCallback(async (options) => {
73
+ const trimmedValue = inputValue.trim();
74
+ // Check if we have content to send
75
+ if (!trimmedValue && imageState.images.length === 0) {
76
+ throw new Error(INPUT_ERROR_MESSAGES.EMPTY);
77
+ }
78
+ const content = [];
79
+ if (trimmedValue) {
80
+ content.push({ type: "text", text: trimmedValue });
81
+ }
82
+ for (const image of imageState.images) {
83
+ content.push(stagedImageToResourceContent(image));
84
+ }
85
+ const result = await sendMessage.mutateAsync({
86
+ message: {
87
+ role: "user",
88
+ content,
89
+ },
90
+ debug: options?.debug,
91
+ });
92
+ // Clear input and images after successful submission
93
+ setInputValue("");
94
+ imageState.clearImages();
95
+ // Update threadId if a new thread was created
96
+ if (result.threadId && shouldAdoptThreadId) {
97
+ setThreadId(result.threadId);
98
+ }
99
+ return result;
100
+ },
101
+ // `stagedImageToResourceContent` is a pure module-level helper (not a hook value).
102
+ [inputValue, imageState, sendMessage, shouldAdoptThreadId]);
103
+ const { mutateAsync: submitAsync, mutate: _unusedSubmit, ...mutationState } = useTamboMutation({
104
+ mutationFn: submitFn,
105
+ });
106
+ const contextValue = {
107
+ ...mutationState,
108
+ value: inputValue,
109
+ setValue: setInputValue,
110
+ submit: submitAsync,
111
+ images: imageState.images,
112
+ addImage: imageState.addImage,
113
+ addImages: imageState.addImages,
114
+ removeImage: imageState.removeImage,
115
+ clearImages: imageState.clearImages,
116
+ threadId: effectiveThreadId,
117
+ setThreadId,
118
+ };
119
+ return (React.createElement(TamboV1ThreadInputContext.Provider, { value: contextValue }, children));
120
+ }
121
+ /**
122
+ * Hook to access the shared thread input state.
123
+ *
124
+ * All components using this hook share the same input state, enabling
125
+ * features like suggestions to update the input field directly.
126
+ * @returns The shared thread input context
127
+ * @throws {Error} If used outside TamboV1ThreadInputProvider
128
+ * @example
129
+ * ```tsx
130
+ * function ChatInput() {
131
+ * const { value, setValue, submit, isPending } = useTamboV1ThreadInput();
132
+ *
133
+ * return (
134
+ * <form onSubmit={(e) => { e.preventDefault(); submit(); }}>
135
+ * <input
136
+ * value={value}
137
+ * onChange={(e) => setValue(e.target.value)}
138
+ * disabled={isPending}
139
+ * />
140
+ * <button type="submit" disabled={isPending}>Send</button>
141
+ * </form>
142
+ * );
143
+ * }
144
+ * ```
145
+ */
146
+ export function useTamboV1ThreadInput() {
147
+ const context = useContext(TamboV1ThreadInputContext);
148
+ if (!context) {
149
+ throw new Error("useTamboV1ThreadInput must be used within TamboV1ThreadInputProvider");
150
+ }
151
+ return context;
152
+ }
153
+ //# sourceMappingURL=tambo-v1-thread-input-provider.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tambo-v1-thread-input-provider.js","sourceRoot":"","sources":["../../../src/v1/providers/tambo-v1-thread-input-provider.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb;;;;;;;GAOG;AAEH,OAAO,KAAK,EAAE,EACZ,aAAa,EACb,WAAW,EACX,UAAU,EACV,QAAQ,GAET,MAAM,OAAO,CAAC;AACf,OAAO,EACL,gBAAgB,GAEjB,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACL,gBAAgB,GAEjB,MAAM,+BAA+B,CAAC;AACvC,OAAO,EAAE,qBAAqB,EAAE,MAAM,oCAAoC,CAAC;AAE3E,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AAE3D,4DAA4D;AAC5D,8EAA8E;AAC9E,4EAA4E;AAC5E,+CAA+C;AAC/C,MAAM,CAAC,MAAM,oBAAoB,GAAG;IAClC,KAAK,EAAE,yBAAyB;IAChC,UAAU,EAAE,wBAAwB;CAC5B,CAAC;AAEX,SAAS,4BAA4B,CACnC,KAAkB;IAElB,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;QACvC,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,UAAU,CAAC,CAAC;IACnD,CAAC;IAED,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IAC9C,IAAI,UAAU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CAAC;QAED,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,mFAAmF;IACnF,CAAC,UAAU,EAAE,UAAU,EAAE,WAAW,EAAE,mBAAmB,CAAC,CAC3D,CAAC;IAEF,MAAM,EACJ,WAAW,EAAE,WAAW,EACxB,MAAM,EAAE,aAAa,EACrB,GAAG,aAAa,EACjB,GAAG,gBAAgB,CAAC;QACnB,UAAU,EAAE,QAAQ;KACrB,CAAC,CAAC;IAEH,MAAM,YAAY,GAAmC;QACnD,GAAG,aAAa;QAChB,KAAK,EAAE,UAAU;QACjB,QAAQ,EAAE,aAAa;QACvB,MAAM,EAAE,WAAW;QACnB,MAAM,EAAE,UAAU,CAAC,MAAM;QACzB,QAAQ,EAAE,UAAU,CAAC,QAAQ;QAC7B,SAAS,EAAE,UAAU,CAAC,SAAS;QAC/B,WAAW,EAAE,UAAU,CAAC,WAAW;QACnC,WAAW,EAAE,UAAU,CAAC,WAAW;QACnC,QAAQ,EAAE,iBAAiB;QAC3B,WAAW;KACZ,CAAC;IAEF,OAAO,CACL,oBAAC,yBAAyB,CAAC,QAAQ,IAAC,KAAK,EAAE,YAAY,IACpD,QAAQ,CAC0B,CACtC,CAAC;AACJ,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,qBAAqB;IACnC,MAAM,OAAO,GAAG,UAAU,CAAC,yBAAyB,CAAC,CAAC;IACtD,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACb,sEAAsE,CACvE,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["\"use client\";\n\n/**\n * TamboV1ThreadInputProvider - Shared Thread Input Context for v1 API\n *\n * Provides shared input state across all components, enabling features like\n * suggestions to update the input field directly.\n *\n * This mirrors the beta SDK's TamboThreadInputProvider pattern.\n */\n\nimport React, {\n createContext,\n useCallback,\n useContext,\n useState,\n type PropsWithChildren,\n} from \"react\";\nimport {\n useMessageImages,\n type StagedImage,\n} from \"../../hooks/use-message-images\";\nimport {\n useTamboMutation,\n type UseTamboMutationResult,\n} from \"../../hooks/react-query-hooks\";\nimport { useTamboV1SendMessage } from \"../hooks/use-tambo-v1-send-message\";\nimport type { InputMessage } from \"../types/message\";\nimport { useStreamState } from \"./tambo-v1-stream-context\";\n\n// Error messages for various input-related error scenarios.\n// TODO: Reintroduce explicit `NETWORK` and `SERVER` keys once `submit()` maps\n// failures into a small, stable set of user-facing error codes (at minimum:\n// connectivity failures vs non-2xx responses).\nexport const INPUT_ERROR_MESSAGES = {\n EMPTY: \"Message cannot be empty\",\n VALIDATION: \"Invalid message format\",\n} as const;\n\nfunction stagedImageToResourceContent(\n image: StagedImage,\n): InputMessage[\"content\"][number] {\n if (!image.dataUrl.startsWith(\"data:\")) {\n throw new Error(INPUT_ERROR_MESSAGES.VALIDATION);\n }\n\n const commaIndex = image.dataUrl.indexOf(\",\");\n if (commaIndex === -1) {\n throw new Error(INPUT_ERROR_MESSAGES.VALIDATION);\n }\n\n const header = image.dataUrl.slice(\"data:\".length, commaIndex);\n const [mimeType, ...params] = header.split(\";\");\n const isBase64 = params.includes(\"base64\");\n\n if (mimeType !== image.type || !isBase64) {\n throw new Error(INPUT_ERROR_MESSAGES.VALIDATION);\n }\n\n return {\n type: \"resource\",\n resource: {\n name: image.name,\n mimeType: image.type,\n // Shared.Resource.blob expects base64-encoded data; this is the base64\n // payload from the `data:` URL.\n blob: image.dataUrl.slice(commaIndex + 1),\n },\n };\n}\n\n/**\n * Options for submitting a message\n */\nexport interface SubmitOptions {\n /**\n * Enable debug logging for the stream\n */\n debug?: boolean;\n}\n\n/**\n * Context props for thread input state\n */\nexport interface TamboV1ThreadInputContextProps extends Omit<\n UseTamboMutationResult<\n { threadId: string | undefined },\n Error,\n SubmitOptions | undefined\n >,\n \"mutate\" | \"mutateAsync\"\n> {\n /** Current value of the input field */\n value: string;\n\n /**\n * Function to update the input value\n * @param value - New value for the input field\n */\n setValue: React.Dispatch<React.SetStateAction<string>>;\n\n /**\n * Function to submit the current input value\n * @param options - Submission options\n * @returns Promise with the threadId\n */\n submit: (\n options?: SubmitOptions,\n ) => Promise<{ threadId: string | undefined }>;\n\n /** Currently staged images */\n images: StagedImage[];\n\n /** Add a single image */\n addImage: (file: File) => Promise<void>;\n\n /** Add multiple images */\n addImages: (files: File[]) => Promise<void>;\n\n /** Remove an image by id */\n removeImage: (id: string) => void;\n\n /** Clear all staged images */\n clearImages: () => void;\n\n /** Current thread ID being used for input */\n threadId: string | undefined;\n\n /**\n * Set the thread ID for input submission.\n * If not set, a new thread will be created on submit.\n */\n setThreadId: React.Dispatch<React.SetStateAction<string | undefined>>;\n}\n\n/**\n * Context for thread input state.\n * @internal\n */\nexport const TamboV1ThreadInputContext = createContext<\n TamboV1ThreadInputContextProps | undefined\n>(undefined);\n\n/**\n * Provider that manages shared thread input state across all components.\n *\n * This ensures that useTamboV1ThreadInput, useTamboV1Suggestions, and other components\n * all share the same input state.\n * @param props - Provider props\n * @param props.children - Child components\n * @returns Thread input context provider\n */\nexport function TamboV1ThreadInputProvider({ children }: PropsWithChildren) {\n const [inputValue, setInputValue] = useState(\"\");\n const [threadId, setThreadId] = useState<string | undefined>(undefined);\n const imageState = useMessageImages();\n const streamState = useStreamState();\n\n // Use the current thread from stream state if no explicit threadId is set\n const inheritedThreadId = streamState.currentThreadId ?? undefined;\n const effectiveThreadId = threadId ?? inheritedThreadId;\n const shouldAdoptThreadId =\n threadId === undefined && inheritedThreadId === undefined;\n const sendMessage = useTamboV1SendMessage(effectiveThreadId);\n\n const submitFn = useCallback(\n async (\n options?: SubmitOptions,\n ): Promise<{ threadId: string | undefined }> => {\n const trimmedValue = inputValue.trim();\n\n // Check if we have content to send\n if (!trimmedValue && imageState.images.length === 0) {\n throw new Error(INPUT_ERROR_MESSAGES.EMPTY);\n }\n\n const content: InputMessage[\"content\"] = [];\n\n if (trimmedValue) {\n content.push({ type: \"text\", text: trimmedValue });\n }\n\n for (const image of imageState.images) {\n content.push(stagedImageToResourceContent(image));\n }\n\n const result = await sendMessage.mutateAsync({\n message: {\n role: \"user\",\n content,\n },\n debug: options?.debug,\n });\n\n // Clear input and images after successful submission\n setInputValue(\"\");\n imageState.clearImages();\n\n // Update threadId if a new thread was created\n if (result.threadId && shouldAdoptThreadId) {\n setThreadId(result.threadId);\n }\n\n return result;\n },\n // `stagedImageToResourceContent` is a pure module-level helper (not a hook value).\n [inputValue, imageState, sendMessage, shouldAdoptThreadId],\n );\n\n const {\n mutateAsync: submitAsync,\n mutate: _unusedSubmit,\n ...mutationState\n } = useTamboMutation({\n mutationFn: submitFn,\n });\n\n const contextValue: TamboV1ThreadInputContextProps = {\n ...mutationState,\n value: inputValue,\n setValue: setInputValue,\n submit: submitAsync,\n images: imageState.images,\n addImage: imageState.addImage,\n addImages: imageState.addImages,\n removeImage: imageState.removeImage,\n clearImages: imageState.clearImages,\n threadId: effectiveThreadId,\n setThreadId,\n };\n\n return (\n <TamboV1ThreadInputContext.Provider value={contextValue}>\n {children}\n </TamboV1ThreadInputContext.Provider>\n );\n}\n\n/**\n * Hook to access the shared thread input state.\n *\n * All components using this hook share the same input state, enabling\n * features like suggestions to update the input field directly.\n * @returns The shared thread input context\n * @throws {Error} If used outside TamboV1ThreadInputProvider\n * @example\n * ```tsx\n * function ChatInput() {\n * const { value, setValue, submit, isPending } = useTamboV1ThreadInput();\n *\n * return (\n * <form onSubmit={(e) => { e.preventDefault(); submit(); }}>\n * <input\n * value={value}\n * onChange={(e) => setValue(e.target.value)}\n * disabled={isPending}\n * />\n * <button type=\"submit\" disabled={isPending}>Send</button>\n * </form>\n * );\n * }\n * ```\n */\nexport function useTamboV1ThreadInput(): TamboV1ThreadInputContextProps {\n const context = useContext(TamboV1ThreadInputContext);\n if (!context) {\n throw new Error(\n \"useTamboV1ThreadInput must be used within TamboV1ThreadInputProvider\",\n );\n }\n return context;\n}\n"]}
@@ -1,5 +1,10 @@
1
- import type { ComponentRegistry } from "../../model/component-metadata";
2
- import type { Content, TamboV1Message, V1ComponentContent } from "../types/message";
1
+ /**
2
+ * Component Renderer Utility for v1 API
3
+ *
4
+ * Provides the component content context for rendered components.
5
+ * Components can use useV1ComponentContent() to access their context.
6
+ */
7
+ import React from "react";
3
8
  /**
4
9
  * Context for component content blocks.
5
10
  * Provides access to the component ID and thread ID for component state hooks.
@@ -14,6 +19,14 @@ export interface V1ComponentContentContext {
14
19
  /** Component name */
15
20
  componentName: string;
16
21
  }
22
+ /**
23
+ * Provider for component content context.
24
+ * Wraps rendered components to provide access to component metadata.
25
+ * @returns Provider component with memoized context value
26
+ */
27
+ export declare function V1ComponentContentProvider({ children, componentId, threadId, messageId, componentName, }: V1ComponentContentContext & {
28
+ children: React.ReactNode;
29
+ }): React.JSX.Element;
17
30
  /**
18
31
  * Hook to access the current component content context.
19
32
  * Must be used within a rendered component.
@@ -21,69 +34,4 @@ export interface V1ComponentContentContext {
21
34
  * @throws {Error} If used outside a rendered component
22
35
  */
23
36
  export declare function useV1ComponentContent(): V1ComponentContentContext;
24
- /**
25
- * Hook to optionally access the current component content context.
26
- * Returns null if not within a rendered component.
27
- * @returns Component content context or null
28
- */
29
- export declare function useV1ComponentContentOptional(): V1ComponentContentContext | null;
30
- /**
31
- * Options for rendering a component content block.
32
- */
33
- export interface RenderComponentOptions {
34
- /** Thread ID for the component context */
35
- threadId: string;
36
- /** Message ID the component belongs to */
37
- messageId: string;
38
- /** Component registry to look up components */
39
- componentList: ComponentRegistry;
40
- }
41
- /**
42
- * Check if a content block is a component.
43
- * @param content - Content block to check
44
- * @returns True if content is a V1ComponentContent
45
- */
46
- export declare function isComponentContent(content: Content): content is V1ComponentContent;
47
- /**
48
- * Render a component content block into a React element.
49
- *
50
- * Looks up the component in the registry, creates a React element with props,
51
- * and wraps it with the component content context provider.
52
- * @param content - Component content block to render
53
- * @param options - Rendering options including registry and context info
54
- * @returns V1ComponentContent with the renderedComponent attached
55
- * @example
56
- * ```tsx
57
- * const rendered = renderComponentContent(componentContent, {
58
- * threadId: 'thread_123',
59
- * messageId: 'msg_456',
60
- * componentList: registry.componentList,
61
- * });
62
- *
63
- * // Use in JSX:
64
- * {rendered.renderedComponent}
65
- * ```
66
- */
67
- export declare function renderComponentContent(content: V1ComponentContent, options: RenderComponentOptions): V1ComponentContent;
68
- /**
69
- * Render all component content blocks in a message.
70
- *
71
- * Renders component content blocks and attaches renderedComponent.
72
- * Non-component content blocks are passed through unchanged.
73
- * @param content - Array of content blocks
74
- * @param options - Rendering options including registry and context info
75
- * @returns Array of content with rendered components
76
- */
77
- export declare function renderMessageContent(content: Content[], options: RenderComponentOptions): Content[];
78
- /**
79
- * Render all components in a message.
80
- *
81
- * Creates a new message object with all component content blocks rendered.
82
- * @param message - Message to render components for
83
- * @param options - Rendering options (threadId is extracted from message if not provided)
84
- * @returns Message with rendered component content
85
- */
86
- export declare function renderMessageComponents(message: TamboV1Message, options: Omit<RenderComponentOptions, "messageId"> & {
87
- threadId: string;
88
- }): TamboV1Message;
89
37
  //# sourceMappingURL=component-renderer.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"component-renderer.d.ts","sourceRoot":"","sources":["../../../src/v1/utils/component-renderer.tsx"],"names":[],"mappings":"AAuDA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,gCAAgC,CAAC;AACxE,OAAO,KAAK,EACV,OAAO,EACP,cAAc,EACd,kBAAkB,EACnB,MAAM,kBAAkB,CAAC;AAE1B;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACxC,4BAA4B;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,yCAAyC;IACzC,QAAQ,EAAE,MAAM,CAAC;IACjB,0CAA0C;IAC1C,SAAS,EAAE,MAAM,CAAC;IAClB,qBAAqB;IACrB,aAAa,EAAE,MAAM,CAAC;CACvB;AA+BD;;;;;GAKG;AACH,wBAAgB,qBAAqB,IAAI,yBAAyB,CAQjE;AAED;;;;GAIG;AACH,wBAAgB,6BAA6B,IAAI,yBAAyB,GAAG,IAAI,CAEhF;AAED;;GAEG;AACH,MAAM,WAAW,sBAAsB;IACrC,0CAA0C;IAC1C,QAAQ,EAAE,MAAM,CAAC;IACjB,0CAA0C;IAC1C,SAAS,EAAE,MAAM,CAAC;IAClB,+CAA+C;IAC/C,aAAa,EAAE,iBAAiB,CAAC;CAClC;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,OAAO,GACf,OAAO,IAAI,kBAAkB,CAE/B;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,sBAAsB,CACpC,OAAO,EAAE,kBAAkB,EAC3B,OAAO,EAAE,sBAAsB,GAC9B,kBAAkB,CAwDpB;AAED;;;;;;;;GAQG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,OAAO,EAAE,EAClB,OAAO,EAAE,sBAAsB,GAC9B,OAAO,EAAE,CAQX;AAED;;;;;;;GAOG;AACH,wBAAgB,uBAAuB,CACrC,OAAO,EAAE,cAAc,EACvB,OAAO,EAAE,IAAI,CAAC,sBAAsB,EAAE,WAAW,CAAC,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,GACxE,cAAc,CAUhB"}
1
+ {"version":3,"file":"component-renderer.d.ts","sourceRoot":"","sources":["../../../src/v1/utils/component-renderer.tsx"],"names":[],"mappings":"AAEA;;;;;GAKG;AAEH,OAAO,KAA6C,MAAM,OAAO,CAAC;AAElE;;;GAGG;AACH,MAAM,WAAW,yBAAyB;IACxC,4BAA4B;IAC5B,WAAW,EAAE,MAAM,CAAC;IACpB,yCAAyC;IACzC,QAAQ,EAAE,MAAM,CAAC;IACjB,0CAA0C;IAC1C,SAAS,EAAE,MAAM,CAAC;IAClB,qBAAqB;IACrB,aAAa,EAAE,MAAM,CAAC;CACvB;AAMD;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,EACzC,QAAQ,EACR,WAAW,EACX,QAAQ,EACR,SAAS,EACT,aAAa,GACd,EAAE,yBAAyB,GAAG;IAAE,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAA;CAAE,qBAY3D;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,IAAI,yBAAyB,CAQjE"}
@@ -2,47 +2,17 @@
2
2
  /**
3
3
  * Component Renderer Utility for v1 API
4
4
  *
5
- * Provides utilities for rendering React components from component content blocks.
6
- * Components are looked up in the registry and wrapped with context providers.
5
+ * Provides the component content context for rendered components.
6
+ * Components can use useV1ComponentContent() to access their context.
7
7
  */
8
- import React, { createContext, createElement, useContext, useMemo, } from "react";
9
- /**
10
- * Props that should be filtered out when rendering components.
11
- * These could be used for event handler injection or other security concerns.
12
- */
13
- const DANGEROUS_PROP_PATTERNS = [
14
- /^on[A-Z]/, // Event handlers (onClick, onError, etc.)
15
- /^dangerouslySetInnerHTML$/,
16
- ];
17
- const DANGEROUS_PROP_NAMES = new Set(["ref", "key", "children"]);
18
- /**
19
- * Sanitize props by removing potentially dangerous properties.
20
- * Filters out event handlers, refs, and other props that could be abused.
21
- * @param props - Raw props from the component content
22
- * @returns Sanitized props safe for spreading to components
23
- */
24
- function sanitizeProps(props) {
25
- const sanitized = {};
26
- for (const [key, value] of Object.entries(props)) {
27
- // Skip dangerous prop names
28
- if (DANGEROUS_PROP_NAMES.has(key)) {
29
- continue;
30
- }
31
- // Skip props matching dangerous patterns
32
- if (DANGEROUS_PROP_PATTERNS.some((pattern) => pattern.test(key))) {
33
- continue;
34
- }
35
- sanitized[key] = value;
36
- }
37
- return sanitized;
38
- }
8
+ import React, { createContext, useContext, useMemo } from "react";
39
9
  const ComponentContentContext = createContext(null);
40
10
  /**
41
11
  * Provider for component content context.
42
12
  * Wraps rendered components to provide access to component metadata.
43
13
  * @returns Provider component with memoized context value
44
14
  */
45
- function V1ComponentContentProvider({ children, componentId, threadId, messageId, componentName, }) {
15
+ export function V1ComponentContentProvider({ children, componentId, threadId, messageId, componentName, }) {
46
16
  // Memoize context value to prevent unnecessary re-renders of consumers
47
17
  const value = useMemo(() => ({ componentId, threadId, messageId, componentName }), [componentId, threadId, messageId, componentName]);
48
18
  return (React.createElement(ComponentContentContext.Provider, { value: value }, children));
@@ -60,116 +30,4 @@ export function useV1ComponentContent() {
60
30
  }
61
31
  return context;
62
32
  }
63
- /**
64
- * Hook to optionally access the current component content context.
65
- * Returns null if not within a rendered component.
66
- * @returns Component content context or null
67
- */
68
- export function useV1ComponentContentOptional() {
69
- return useContext(ComponentContentContext);
70
- }
71
- /**
72
- * Check if a content block is a component.
73
- * @param content - Content block to check
74
- * @returns True if content is a V1ComponentContent
75
- */
76
- export function isComponentContent(content) {
77
- return content.type === "component";
78
- }
79
- /**
80
- * Render a component content block into a React element.
81
- *
82
- * Looks up the component in the registry, creates a React element with props,
83
- * and wraps it with the component content context provider.
84
- * @param content - Component content block to render
85
- * @param options - Rendering options including registry and context info
86
- * @returns V1ComponentContent with the renderedComponent attached
87
- * @example
88
- * ```tsx
89
- * const rendered = renderComponentContent(componentContent, {
90
- * threadId: 'thread_123',
91
- * messageId: 'msg_456',
92
- * componentList: registry.componentList,
93
- * });
94
- *
95
- * // Use in JSX:
96
- * {rendered.renderedComponent}
97
- * ```
98
- */
99
- export function renderComponentContent(content, options) {
100
- const { threadId, messageId, componentList } = options;
101
- // Look up component in registry
102
- const registeredComponent = componentList[content.name];
103
- if (!registeredComponent) {
104
- console.warn(`Component "${content.name}" not found in registry`);
105
- return {
106
- ...content,
107
- renderedComponent: null,
108
- };
109
- }
110
- const Component = registeredComponent.component;
111
- const LoadingComponent = registeredComponent.loadingComponent;
112
- // Determine if we should show loading state
113
- const isStreaming = content.streamingState !== "done";
114
- // Sanitize props to prevent injection of event handlers or other dangerous props
115
- const props = sanitizeProps(content.props);
116
- // Create the component element
117
- let element;
118
- if (isStreaming && LoadingComponent) {
119
- // Show loading component during streaming (with props for partial data display)
120
- element = createElement(LoadingComponent, props);
121
- }
122
- else {
123
- // Show main component - props stream in as they're filled out
124
- element = createElement(Component, {
125
- ...props,
126
- // Pass state as initialState prop only if not already provided
127
- ...(props.initialState === undefined
128
- ? { initialState: content.state }
129
- : {}),
130
- });
131
- }
132
- // Wrap with component content context
133
- const wrappedElement = (React.createElement(V1ComponentContentProvider, { componentId: content.id, threadId: threadId, messageId: messageId, componentName: content.name }, element));
134
- return {
135
- ...content,
136
- renderedComponent: wrappedElement,
137
- };
138
- }
139
- /**
140
- * Render all component content blocks in a message.
141
- *
142
- * Renders component content blocks and attaches renderedComponent.
143
- * Non-component content blocks are passed through unchanged.
144
- * @param content - Array of content blocks
145
- * @param options - Rendering options including registry and context info
146
- * @returns Array of content with rendered components
147
- */
148
- export function renderMessageContent(content, options) {
149
- return content.map((block) => {
150
- if (isComponentContent(block)) {
151
- return renderComponentContent(block, options);
152
- }
153
- // Pass through non-component content unchanged
154
- return block;
155
- });
156
- }
157
- /**
158
- * Render all components in a message.
159
- *
160
- * Creates a new message object with all component content blocks rendered.
161
- * @param message - Message to render components for
162
- * @param options - Rendering options (threadId is extracted from message if not provided)
163
- * @returns Message with rendered component content
164
- */
165
- export function renderMessageComponents(message, options) {
166
- const renderedContent = renderMessageContent(message.content, {
167
- ...options,
168
- messageId: message.id,
169
- });
170
- return {
171
- ...message,
172
- content: renderedContent,
173
- };
174
- }
175
33
  //# sourceMappingURL=component-renderer.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"component-renderer.js","sourceRoot":"","sources":["../../../src/v1/utils/component-renderer.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,EACZ,aAAa,EACb,aAAa,EACb,UAAU,EACV,OAAO,GAER,MAAM,OAAO,CAAC;AAEf;;;GAGG;AACH,MAAM,uBAAuB,GAAG;IAC9B,UAAU,EAAE,0CAA0C;IACtD,2BAA2B;CAC5B,CAAC;AAEF,MAAM,oBAAoB,GAAG,IAAI,GAAG,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,UAAU,CAAC,CAAC,CAAC;AAEjE;;;;;GAKG;AACH,SAAS,aAAa,C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AS;gBAClC,CAAC,CAAC,EAAE,YAAY,EAAE,OAAO,CAAC,KAAK,EAAE;gBACjC,CAAC,CAAC,EAAE,CAAC;SACR,CAAC,CAAC;IACL,CAAC;IAED,sCAAsC;IACtC,MAAM,cAAc,GAAG,CACrB,oBAAC,0BAA0B,IACzB,WAAW,EAAE,OAAO,CAAC,EAAE,EACvB,QAAQ,EAAE,QAAQ,EAClB,SAAS,EAAE,SAAS,EACpB,aAAa,EAAE,OAAO,CAAC,IAAI,IAE1B,OAAO,CACmB,CAC9B,CAAC;IAEF,OAAO;QACL,GAAG,OAAO;QACV,iBAAiB,EAAE,cAAc;KAClC,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAAkB,EAClB,OAA+B;IAE/B,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE;QAC3B,IAAI,kBAAkB,CAAC,KAAK,CAAC,EAAE,CAAC;YAC9B,OAAO,sBAAsB,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;QAChD,CAAC;QACD,+CAA+C;QAC/C,OAAO,KAAK,CAAC;IACf,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,uBAAuB,CACrC,OAAuB,EACvB,OAAyE;IAEzE,MAAM,eAAe,GAAG,oBAAoB,CAAC,OAAO,CAAC,OAAO,EAAE;QAC5D,GAAG,OAAO;QACV,SAAS,EAAE,OAAO,CAAC,EAAE;KACtB,CAAC,CAAC;IAEH,OAAO;QACL,GAAG,OAAO;QACV,OAAO,EAAE,eAAe;KACzB,CAAC;AACJ,CAAC","sourcesContent":["\"use client\";\n\n/**\n * Component Renderer Utility for v1 API\n *\n * Provides utilities for rendering React components from component content blocks.\n * Components are looked up in the registry and wrapped with context providers.\n */\n\nimport React, {\n createContext,\n createElement,\n useContext,\n useMemo,\n type ReactElement,\n} from \"react\";\n\n/**\n * Props that should be filtered out when rendering components.\n * These could be used for event handler injection or other security concerns.\n */\nconst DANGEROUS_PROP_PATTERNS = [\n /^on[A-Z]/, // Event handlers (onClick, onError, etc.)\n /^dangerouslySetInnerHTML$/,\n];\n\nconst DANGEROUS_PROP_NAMES = new Set([\"ref\", \"key\", \"children\"]);\n\n/**\n * Sanitize props by removing potentially dangerous properties.\n * Filters out event handlers, refs, and other props that could be abused.\n * @param props - Raw props from the component content\n * @returns Sanitized props safe for spreading to components\n */\nfunction sanitizeProps(\n props: Record<string, unknown>,\n): Record<string, unknown> {\n const sanitized: Record<string, unknown> = {};\n\n for (const [key, value] of Object.entries(props)) {\n // Skip dangerous prop names\n if (DANGEROUS_PROP_NAMES.has(key)) {\n continue;\n }\n\n // Skip props matching dangerous patterns\n if (DANGEROUS_PROP_PATTERNS.some((pattern) => pattern.test(key))) {\n continue;\n }\n\n sanitized[key] = value;\n }\n\n return sanitized;\n}\nimport type { ComponentRegistry } from \"../../model/component-metadata\";\nimport type {\n Content,\n TamboV1Message,\n V1ComponentContent,\n} from \"../types/message\";\n\n/**\n * Context for component content blocks.\n * Provides access to the component ID and thread ID for component state hooks.\n */\nexport interface V1ComponentContentContext {\n /** Component instance ID */\n componentId: string;\n /** Thread ID the component belongs to */\n threadId: string;\n /** Message ID the component belongs to */\n messageId: string;\n /** Component name */\n componentName: string;\n}\n\nconst ComponentContentContext = createContext<V1ComponentContentContext | null>(\n null,\n);\n\n/**\n * Provider for component content context.\n * Wraps rendered components to provide access to component metadata.\n * @returns Provider component with memoized context value\n */\nfunction V1ComponentContentProvider({\n children,\n componentId,\n threadId,\n messageId,\n componentName,\n}: V1ComponentContentContext & { children: React.ReactNode }) {\n // Memoize context value to prevent unnecessary re-renders of consumers\n const value = useMemo(\n () => ({ componentId, threadId, messageId, componentName }),\n [componentId, threadId, messageId, componentName],\n );\n\n return (\n <ComponentContentContext.Provider value={value}>\n {children}\n </ComponentContentContext.Provider>\n );\n}\n\n/**\n * Hook to access the current component content context.\n * Must be used within a rendered component.\n * @returns Component content context\n * @throws {Error} If used outside a rendered component\n */\nexport function useV1ComponentContent(): V1ComponentContentContext {\n const context = useContext(ComponentContentContext);\n if (!context) {\n throw new Error(\n \"useV1ComponentContent must be used within a rendered component\",\n );\n }\n return context;\n}\n\n/**\n * Hook to optionally access the current component content context.\n * Returns null if not within a rendered component.\n * @returns Component content context or null\n */\nexport function useV1ComponentContentOptional(): V1ComponentContentContext | null {\n return useContext(ComponentContentContext);\n}\n\n/**\n * Options for rendering a component content block.\n */\nexport interface RenderComponentOptions {\n /** Thread ID for the component context */\n threadId: string;\n /** Message ID the component belongs to */\n messageId: string;\n /** Component registry to look up components */\n componentList: ComponentRegistry;\n}\n\n/**\n * Check if a content block is a component.\n * @param content - Content block to check\n * @returns True if content is a V1ComponentContent\n */\nexport function isComponentContent(\n content: Content,\n): content is V1ComponentContent {\n return content.type === \"component\";\n}\n\n/**\n * Render a component content block into a React element.\n *\n * Looks up the component in the registry, creates a React element with props,\n * and wraps it with the component content context provider.\n * @param content - Component content block to render\n * @param options - Rendering options including registry and context info\n * @returns V1ComponentContent with the renderedComponent attached\n * @example\n * ```tsx\n * const rendered = renderComponentContent(componentContent, {\n * threadId: 'thread_123',\n * messageId: 'msg_456',\n * componentList: registry.componentList,\n * });\n *\n * // Use in JSX:\n * {rendered.renderedComponent}\n * ```\n */\nexport function renderComponentContent(\n content: V1ComponentContent,\n options: RenderComponentOptions,\n): V1ComponentContent {\n const { threadId, messageId, componentList } = options;\n\n // Look up component in registry\n const registeredComponent = componentList[content.name];\n\n if (!registeredComponent) {\n console.warn(`Component \"${content.name}\" not found in registry`);\n return {\n ...content,\n renderedComponent: null,\n };\n }\n\n const Component = registeredComponent.component;\n const LoadingComponent = registeredComponent.loadingComponent;\n\n // Determine if we should show loading state\n const isStreaming = content.streamingState !== \"done\";\n\n // Sanitize props to prevent injection of event handlers or other dangerous props\n const props = sanitizeProps(content.props as Record<string, unknown>);\n\n // Create the component element\n let element: ReactElement;\n\n if (isStreaming && LoadingComponent) {\n // Show loading component during streaming (with props for partial data display)\n element = createElement(LoadingComponent, props);\n } else {\n // Show main component - props stream in as they're filled out\n element = createElement(Component, {\n ...props,\n // Pass state as initialState prop only if not already provided\n ...(props.initialState === undefined\n ? { initialState: content.state }\n : {}),\n });\n }\n\n // Wrap with component content context\n const wrappedElement = (\n <V1ComponentContentProvider\n componentId={content.id}\n threadId={threadId}\n messageId={messageId}\n componentName={content.name}\n >\n {element}\n </V1ComponentContentProvider>\n );\n\n return {\n ...content,\n renderedComponent: wrappedElement,\n };\n}\n\n/**\n * Render all component content blocks in a message.\n *\n * Renders component content blocks and attaches renderedComponent.\n * Non-component content blocks are passed through unchanged.\n * @param content - Array of content blocks\n * @param options - Rendering options including registry and context info\n * @returns Array of content with rendered components\n */\nexport function renderMessageContent(\n content: Content[],\n options: RenderComponentOptions,\n): Content[] {\n return content.map((block) => {\n if (isComponentContent(block)) {\n return renderComponentContent(block, options);\n }\n // Pass through non-component content unchanged\n return block;\n });\n}\n\n/**\n * Render all components in a message.\n *\n * Creates a new message object with all component content blocks rendered.\n * @param message - Message to render components for\n * @param options - Rendering options (threadId is extracted from message if not provided)\n * @returns Message with rendered component content\n */\nexport function renderMessageComponents(\n message: TamboV1Message,\n options: Omit<RenderComponentOptions, \"messageId\"> & { threadId: string },\n): TamboV1Message {\n const renderedContent = renderMessageContent(message.content, {\n ...options,\n messageId: message.id,\n });\n\n return {\n ...message,\n content: renderedContent,\n };\n}\n"]}
1
+ {"version":3,"file":"component-renderer.js","sourceRoot":"","sources":["../../../src/v1/utils/component-renderer.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb;;;;;GAKG;AAEH,OAAO,KAAK,EAAE,EAAE,aAAa,EAAE,UAAU,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAiBlE,MAAM,uBAAuB,GAAG,aAAa,CAC3C,IAAI,CACL,CAAC;AAEF;;;;GAIG;AACH,MAAM,UAAU,0BAA0B,CAAC,EACzC,QAAQ,EACR,WAAW,EACX,QAAQ,EACR,SAAS,EACT,aAAa,GAC6C;IAC1D,uEAAuE;IACvE,MAAM,KAAK,GAAG,OAAO,CACnB,GAAG,EAAE,CAAC,CAAC,EAAE,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,aAAa,EAAE,CAAC,EAC3D,CAAC,WAAW,EAAE,QAAQ,EAAE,SAAS,EAAE,aAAa,CAAC,CAClD,CAAC;IAEF,OAAO,CACL,oBAAC,uBAAuB,CAAC,QAAQ,IAAC,KAAK,EAAE,KAAK,IAC3C,QAAQ,CACwB,CACpC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,qBAAqB;IACnC,MAAM,OAAO,GAAG,UAAU,CAAC,uBAAuB,CAAC,CAAC;IACpD,IAAI,CAAC,OAAO,EAAE,CAAC;QACb,MAAM,IAAI,KAAK,CACb,gEAAgE,CACjE,CAAC;IACJ,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["\"use client\";\n\n/**\n * Component Renderer Utility for v1 API\n *\n * Provides the component content context for rendered components.\n * Components can use useV1ComponentContent() to access their context.\n */\n\nimport React, { createContext, useContext, useMemo } from \"react\";\n\n/**\n * Context for component content blocks.\n * Provides access to the component ID and thread ID for component state hooks.\n */\nexport interface V1ComponentContentContext {\n /** Component instance ID */\n componentId: string;\n /** Thread ID the component belongs to */\n threadId: string;\n /** Message ID the component belongs to */\n messageId: string;\n /** Component name */\n componentName: string;\n}\n\nconst ComponentContentContext = createContext<V1ComponentContentContext | null>(\n null,\n);\n\n/**\n * Provider for component content context.\n * Wraps rendered components to provide access to component metadata.\n * @returns Provider component with memoized context value\n */\nexport function V1ComponentContentProvider({\n children,\n componentId,\n threadId,\n messageId,\n componentName,\n}: V1ComponentContentContext & { children: React.ReactNode }) {\n // Memoize context value to prevent unnecessary re-renders of consumers\n const value = useMemo(\n () => ({ componentId, threadId, messageId, componentName }),\n [componentId, threadId, messageId, componentName],\n );\n\n return (\n <ComponentContentContext.Provider value={value}>\n {children}\n </ComponentContentContext.Provider>\n );\n}\n\n/**\n * Hook to access the current component content context.\n * Must be used within a rendered component.\n * @returns Component content context\n * @throws {Error} If used outside a rendered component\n */\nexport function useV1ComponentContent(): V1ComponentContentContext {\n const context = useContext(ComponentContentContext);\n if (!context) {\n throw new Error(\n \"useV1ComponentContent must be used within a rendered component\",\n );\n }\n return context;\n}\n"]}