@assistant-ui/react 0.14.29 → 0.15.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 (138) hide show
  1. package/dist/client/ExternalThread.js +4 -3
  2. package/dist/client/ExternalThread.js.map +1 -1
  3. package/dist/client/InMemoryThreadList.js +3 -3
  4. package/dist/client/InMemoryThreadList.js.map +1 -1
  5. package/dist/context/index.d.ts +1 -8
  6. package/dist/context/index.js +1 -8
  7. package/dist/index.d.ts +1 -8
  8. package/dist/index.js +1 -8
  9. package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js +3 -3
  10. package/dist/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.js.map +1 -1
  11. package/dist/mcp-apps/McpAppRenderer.js +1 -1
  12. package/dist/mcp-apps/McpAppRenderer.js.map +1 -1
  13. package/dist/model-context/makeAssistantVisible.js +26 -25
  14. package/dist/model-context/makeAssistantVisible.js.map +1 -1
  15. package/dist/primitives/actionBar/ActionBarExportMarkdown.js +3 -3
  16. package/dist/primitives/actionBar/ActionBarExportMarkdown.js.map +1 -1
  17. package/dist/primitives/attachment/AttachmentRemove.js +3 -3
  18. package/dist/primitives/attachment/AttachmentRemove.js.map +1 -1
  19. package/dist/primitives/chainOfThought/ChainOfThoughtAccordionTrigger.js +3 -3
  20. package/dist/primitives/chainOfThought/ChainOfThoughtAccordionTrigger.js.map +1 -1
  21. package/dist/primitives/composer/ComposerAddAttachment.js +3 -3
  22. package/dist/primitives/composer/ComposerAddAttachment.js.map +1 -1
  23. package/dist/primitives/composer/ComposerAttachmentDropzone.js +1 -1
  24. package/dist/primitives/composer/ComposerAttachmentDropzone.js.map +1 -1
  25. package/dist/primitives/composer/ComposerInput.js +13 -13
  26. package/dist/primitives/composer/ComposerInput.js.map +1 -1
  27. package/dist/primitives/composer/ComposerQuote.js +1 -1
  28. package/dist/primitives/composer/ComposerQuote.js.map +1 -1
  29. package/dist/primitives/composer/ComposerStopDictation.js +3 -3
  30. package/dist/primitives/composer/ComposerStopDictation.js.map +1 -1
  31. package/dist/primitives/composer/trigger/triggerSelectionResource.d.ts.map +1 -1
  32. package/dist/primitives/composer/trigger/triggerSelectionResource.js +3 -3
  33. package/dist/primitives/composer/trigger/triggerSelectionResource.js.map +1 -1
  34. package/dist/primitives/message/MessagePartsGrouped.d.ts.map +1 -1
  35. package/dist/primitives/message/MessagePartsGrouped.js +67 -89
  36. package/dist/primitives/message/MessagePartsGrouped.js.map +1 -1
  37. package/dist/primitives/message/MessageRoot.js +3 -3
  38. package/dist/primitives/message/MessageRoot.js.map +1 -1
  39. package/dist/primitives/queueItem/QueueItemRemove.js +3 -3
  40. package/dist/primitives/queueItem/QueueItemRemove.js.map +1 -1
  41. package/dist/primitives/queueItem/QueueItemSteer.js +3 -3
  42. package/dist/primitives/queueItem/QueueItemSteer.js.map +1 -1
  43. package/dist/primitives/selectionToolbar/SelectionToolbarQuote.js +1 -1
  44. package/dist/primitives/selectionToolbar/SelectionToolbarQuote.js.map +1 -1
  45. package/dist/primitives/suggestion/SuggestionTrigger.js +16 -15
  46. package/dist/primitives/suggestion/SuggestionTrigger.js.map +1 -1
  47. package/dist/unstable/useComposerInput.js +2 -2
  48. package/dist/unstable/useComposerInput.js.map +1 -1
  49. package/dist/unstable/useComposerInputHistory.js +6 -6
  50. package/dist/unstable/useComposerInputHistory.js.map +1 -1
  51. package/dist/unstable/useMentionAdapter.js +1 -1
  52. package/dist/unstable/useMentionAdapter.js.map +1 -1
  53. package/dist/utils/smooth/SmoothContext.js +3 -3
  54. package/dist/utils/smooth/SmoothContext.js.map +1 -1
  55. package/dist/utils/smooth/useSmooth.js +1 -1
  56. package/dist/utils/smooth/useSmooth.js.map +1 -1
  57. package/package.json +5 -5
  58. package/src/client/ExternalThread.ts +5 -3
  59. package/src/client/InMemoryThreadList.ts +3 -3
  60. package/src/context/index.ts +0 -44
  61. package/src/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.test.tsx +5 -9
  62. package/src/legacy-runtime/runtime-cores/assistant-transport/useAssistantTransportRuntime.ts +1 -1
  63. package/src/mcp-apps/McpAppRenderer.tsx +1 -1
  64. package/src/model-context/makeAssistantVisible.test.tsx +1 -1
  65. package/src/model-context/makeAssistantVisible.tsx +1 -1
  66. package/src/primitives/actionBar/ActionBarExportMarkdown.tsx +1 -1
  67. package/src/primitives/attachment/AttachmentRemove.ts +1 -1
  68. package/src/primitives/chainOfThought/ChainOfThoughtAccordionTrigger.ts +1 -1
  69. package/src/primitives/composer/ComposerAddAttachment.test.tsx +2 -2
  70. package/src/primitives/composer/ComposerAddAttachment.ts +1 -1
  71. package/src/primitives/composer/ComposerAttachmentDropzone.test.tsx +2 -2
  72. package/src/primitives/composer/ComposerAttachmentDropzone.tsx +1 -1
  73. package/src/primitives/composer/ComposerInput.test.tsx +7 -7
  74. package/src/primitives/composer/ComposerInput.tsx +13 -13
  75. package/src/primitives/composer/ComposerQuote.tsx +1 -1
  76. package/src/primitives/composer/ComposerStopDictation.ts +1 -1
  77. package/src/primitives/composer/trigger/triggerSelectionResource.ts +7 -9
  78. package/src/primitives/message/MessagePartsGrouped.tsx +6 -8
  79. package/src/primitives/message/MessageRoot.tsx +1 -1
  80. package/src/primitives/queueItem/QueueItemRemove.ts +1 -1
  81. package/src/primitives/queueItem/QueueItemSteer.ts +1 -1
  82. package/src/primitives/selectionToolbar/SelectionToolbarQuote.tsx +1 -1
  83. package/src/primitives/suggestion/SuggestionTrigger.ts +9 -9
  84. package/src/tests/DataRenderers.test.tsx +1 -1
  85. package/src/tests/RemoteThreadListRuntime.deferredProvider.test.tsx +3 -3
  86. package/src/tests/external-thread-attachments.test.tsx +21 -23
  87. package/src/tests/external-thread-branches.test.tsx +10 -10
  88. package/src/tests/external-thread-parity.test.tsx +18 -20
  89. package/src/tests/local-runtime-queue.test.tsx +29 -34
  90. package/src/tests/messagePartSwitchRace.test.tsx +30 -13
  91. package/src/unstable/useComposerInput.test.tsx +2 -2
  92. package/src/unstable/useComposerInput.ts +2 -2
  93. package/src/unstable/useComposerInputHistory.test.tsx +3 -3
  94. package/src/unstable/useComposerInputHistory.ts +6 -6
  95. package/src/unstable/useMentionAdapter.ts +1 -1
  96. package/src/utils/smooth/SmoothContext.tsx +1 -1
  97. package/src/utils/smooth/useSmooth.test.tsx +1 -1
  98. package/src/utils/smooth/useSmooth.ts +1 -1
  99. package/dist/context/react/utils/createStateHookForRuntime.d.ts +0 -34
  100. package/dist/context/react/utils/createStateHookForRuntime.d.ts.map +0 -1
  101. package/dist/context/react/utils/createStateHookForRuntime.js +0 -21
  102. package/dist/context/react/utils/createStateHookForRuntime.js.map +0 -1
  103. package/dist/legacy-runtime/hooks/AssistantContext.d.ts +0 -74
  104. package/dist/legacy-runtime/hooks/AssistantContext.d.ts.map +0 -1
  105. package/dist/legacy-runtime/hooks/AssistantContext.js +0 -29
  106. package/dist/legacy-runtime/hooks/AssistantContext.js.map +0 -1
  107. package/dist/legacy-runtime/hooks/AttachmentContext.d.ts +0 -1007
  108. package/dist/legacy-runtime/hooks/AttachmentContext.d.ts.map +0 -1
  109. package/dist/legacy-runtime/hooks/AttachmentContext.js +0 -47
  110. package/dist/legacy-runtime/hooks/AttachmentContext.js.map +0 -1
  111. package/dist/legacy-runtime/hooks/ComposerContext.d.ts +0 -141
  112. package/dist/legacy-runtime/hooks/ComposerContext.d.ts.map +0 -1
  113. package/dist/legacy-runtime/hooks/ComposerContext.js +0 -65
  114. package/dist/legacy-runtime/hooks/ComposerContext.js.map +0 -1
  115. package/dist/legacy-runtime/hooks/MessageContext.d.ts +0 -155
  116. package/dist/legacy-runtime/hooks/MessageContext.d.ts.map +0 -1
  117. package/dist/legacy-runtime/hooks/MessageContext.js +0 -68
  118. package/dist/legacy-runtime/hooks/MessageContext.js.map +0 -1
  119. package/dist/legacy-runtime/hooks/MessagePartContext.d.ts +0 -44
  120. package/dist/legacy-runtime/hooks/MessagePartContext.d.ts.map +0 -1
  121. package/dist/legacy-runtime/hooks/MessagePartContext.js +0 -26
  122. package/dist/legacy-runtime/hooks/MessagePartContext.js.map +0 -1
  123. package/dist/legacy-runtime/hooks/ThreadContext.d.ts +0 -139
  124. package/dist/legacy-runtime/hooks/ThreadContext.d.ts.map +0 -1
  125. package/dist/legacy-runtime/hooks/ThreadContext.js +0 -84
  126. package/dist/legacy-runtime/hooks/ThreadContext.js.map +0 -1
  127. package/dist/legacy-runtime/hooks/ThreadListItemContext.d.ts +0 -44
  128. package/dist/legacy-runtime/hooks/ThreadListItemContext.d.ts.map +0 -1
  129. package/dist/legacy-runtime/hooks/ThreadListItemContext.js +0 -26
  130. package/dist/legacy-runtime/hooks/ThreadListItemContext.js.map +0 -1
  131. package/src/context/react/utils/createStateHookForRuntime.ts +0 -82
  132. package/src/legacy-runtime/hooks/AssistantContext.ts +0 -67
  133. package/src/legacy-runtime/hooks/AttachmentContext.ts +0 -99
  134. package/src/legacy-runtime/hooks/ComposerContext.ts +0 -131
  135. package/src/legacy-runtime/hooks/MessageContext.ts +0 -127
  136. package/src/legacy-runtime/hooks/MessagePartContext.ts +0 -32
  137. package/src/legacy-runtime/hooks/ThreadContext.ts +0 -120
  138. package/src/legacy-runtime/hooks/ThreadListItemContext.ts +0 -36
@@ -1 +1 @@
1
- {"version":3,"file":"useMentionAdapter.js","names":["useMemo","FC","useAui","Unstable_DirectiveFormatter","Unstable_TriggerAdapter","Unstable_TriggerCategory","Unstable_TriggerItem","unstable_defaultDirectiveFormatter","ReadonlyJSONObject","Unstable_IconComponent","className","Unstable_Mention","id","type","label","description","icon","metadata","Unstable_MentionCategory","items","Unstable_ModelContextToolsOptions","category","formatLabel","toolName","Unstable_UseMentionAdapterOptions","categories","includeModelContextTools","formatter","onInserted","item","iconMap","Record","fallbackIcon","Unstable_MentionDirective","unstable_useMentionAdapter","options","adapter","directive","aui","includeTools","toolsConfig","undefined","wantsTools","getModelContextTools","ctx","thread","getModelContext","tools","defaultIcon","Object","entries","map","name","tool","toTriggerItem","length","groups","cat","toolCategory","toolItems","allGroups","categoryItems","find","g","search","query","lower","toLowerCase","flatMap","filter","matchesQuery","flatItems","getFlatPool","seen","Set","i","t","has","m","includes"],"sources":["../../src/unstable/useMentionAdapter.ts"],"sourcesContent":["\"use client\";\n\nimport { useMemo, type FC } from \"react\";\nimport { useAui } from \"@assistant-ui/store\";\nimport type {\n Unstable_DirectiveFormatter,\n Unstable_TriggerAdapter,\n Unstable_TriggerCategory,\n Unstable_TriggerItem,\n} from \"@assistant-ui/core\";\nimport { unstable_defaultDirectiveFormatter } from \"@assistant-ui/core\";\nimport type { ReadonlyJSONObject } from \"assistant-stream/utils\";\n\n/** Icon component shape consumed by `ComposerTriggerPopover`'s `iconMap`. */\nexport type Unstable_IconComponent = FC<{ className?: string }>;\n\nexport type Unstable_Mention = {\n readonly id: string;\n readonly type: string;\n readonly label: string;\n readonly description?: string | undefined;\n /** Shortcut for `metadata.icon`; merged with `metadata` if both are given. */\n readonly icon?: string | undefined;\n readonly metadata?: ReadonlyJSONObject | undefined;\n};\n\nexport type Unstable_MentionCategory = {\n readonly id: string;\n readonly label: string;\n readonly items: readonly Unstable_Mention[];\n};\n\nexport type Unstable_ModelContextToolsOptions = {\n /** Wrap tools in a dedicated category (drill-down mode). */\n readonly category?: { readonly id: string; readonly label: string };\n /** Format tool name for display. */\n readonly formatLabel?: (toolName: string) => string;\n /** Default icon key for each tool. */\n readonly icon?: string;\n};\n\nexport type Unstable_UseMentionAdapterOptions = {\n /** Flat mention list. Ignored when `categories` is set. */\n readonly items?: readonly Unstable_Mention[];\n /** Categorized mentions for drill-down navigation. */\n readonly categories?: readonly Unstable_MentionCategory[];\n /**\n * How tools registered in model context integrate.\n * - `false`: exclude.\n * - `true`: include (default when no `items`/`categories`; as a category\n * if `categories` is set, flat otherwise).\n * - object: explicit config.\n *\n * Omitted → defaults to `true` iff neither `items` nor `categories`.\n */\n readonly includeModelContextTools?:\n | boolean\n | Unstable_ModelContextToolsOptions;\n /** Directive formatter. @default unstable_defaultDirectiveFormatter */\n readonly formatter?: Unstable_DirectiveFormatter;\n /** Fires after an item is inserted into the composer. */\n readonly onInserted?: (item: Unstable_TriggerItem) => void;\n /** Maps `metadata.icon` / `category.id` string keys to React components. */\n readonly iconMap?: Record<string, Unstable_IconComponent>;\n /** Fallback icon when no entry in `iconMap` matches. */\n readonly fallbackIcon?: Unstable_IconComponent;\n};\n\nexport type Unstable_MentionDirective = {\n readonly formatter: Unstable_DirectiveFormatter;\n readonly onInserted?: ((item: Unstable_TriggerItem) => void) | undefined;\n};\n\n/**\n * @deprecated Under active development and might change without notice.\n *\n * Creates a spreadable `{ adapter, directive }` bundle for `@` mentions.\n * Supports tools registered in model context, explicit items, or both —\n * flat or categorized.\n *\n * @example\n * ```tsx\n * const mention = unstable_useMentionAdapter();\n * <ComposerTriggerPopover char=\"@\" {...mention} />\n * ```\n */\nexport function unstable_useMentionAdapter(\n options?: Unstable_UseMentionAdapterOptions,\n): {\n adapter: Unstable_TriggerAdapter;\n directive: Unstable_MentionDirective;\n iconMap?: Record<string, Unstable_IconComponent>;\n fallbackIcon?: Unstable_IconComponent;\n} {\n const aui = useAui();\n\n const items = options?.items;\n const categories = options?.categories;\n const includeTools =\n options?.includeModelContextTools ?? (!items && !categories);\n const toolsConfig =\n typeof includeTools === \"object\" ? includeTools : undefined;\n const wantsTools = includeTools !== false;\n const formatter = options?.formatter;\n const onInserted = options?.onInserted;\n\n const adapter = useMemo<Unstable_TriggerAdapter>(() => {\n const getModelContextTools = (): Unstable_TriggerItem[] => {\n if (!wantsTools) return [];\n const ctx = aui.thread().getModelContext();\n const tools = ctx.tools;\n if (!tools) return [];\n const formatLabel = toolsConfig?.formatLabel;\n const defaultIcon = toolsConfig?.icon;\n return Object.entries(tools).map(([name, tool]) =>\n toTriggerItem({\n id: name,\n type: \"tool\",\n label: formatLabel ? formatLabel(name) : name,\n description: tool.description ?? undefined,\n icon: defaultIcon,\n }),\n );\n };\n\n // Categorized: drill-down mode\n if (categories && categories.length > 0) {\n const groups = categories.map((cat) => ({\n id: cat.id,\n label: cat.label,\n items: cat.items.map(toTriggerItem),\n }));\n\n let toolCategory: {\n id: string;\n label: string;\n items: Unstable_TriggerItem[];\n } | null = null;\n if (wantsTools) {\n const toolItems = getModelContextTools();\n if (toolItems.length > 0) {\n toolCategory = {\n id: toolsConfig?.category?.id ?? \"tools\",\n label: toolsConfig?.category?.label ?? \"Tools\",\n items: toolItems,\n };\n }\n }\n const allGroups = toolCategory ? [...groups, toolCategory] : groups;\n\n return {\n categories: () => allGroups.map(({ id, label }) => ({ id, label })),\n categoryItems: (id) => allGroups.find((g) => g.id === id)?.items ?? [],\n search: (query) => {\n const lower = query.toLowerCase();\n return allGroups\n .flatMap((g) => g.items)\n .filter((item) => matchesQuery(item, lower));\n },\n };\n }\n\n // Flat: items + (optionally) tools, all in one search pool\n const flatItems = (items ?? []).map(toTriggerItem);\n const getFlatPool = (): Unstable_TriggerItem[] => {\n if (!wantsTools) return flatItems;\n const toolItems = getModelContextTools();\n // Dedupe by id — explicit items win.\n const seen = new Set(flatItems.map((i) => i.id));\n return [...flatItems, ...toolItems.filter((t) => !seen.has(t.id))];\n };\n\n return {\n categories: (): readonly Unstable_TriggerCategory[] => [],\n categoryItems: () => [],\n search: (query) => {\n const lower = query.toLowerCase();\n return getFlatPool().filter((item) => matchesQuery(item, lower));\n },\n };\n }, [aui, items, categories, wantsTools, toolsConfig]);\n\n const directive = useMemo<Unstable_MentionDirective>(\n () => ({\n formatter: formatter ?? unstable_defaultDirectiveFormatter,\n ...(onInserted ? { onInserted } : {}),\n }),\n [formatter, onInserted],\n );\n\n return {\n adapter,\n directive,\n ...(options?.iconMap ? { iconMap: options.iconMap } : {}),\n ...(options?.fallbackIcon ? { fallbackIcon: options.fallbackIcon } : {}),\n };\n}\n\nfunction toTriggerItem(m: Unstable_Mention): Unstable_TriggerItem {\n const metadata =\n m.icon !== undefined ? { ...(m.metadata ?? {}), icon: m.icon } : m.metadata;\n return {\n id: m.id,\n type: m.type,\n label: m.label,\n ...(m.description !== undefined ? { description: m.description } : {}),\n ...(metadata !== undefined ? { metadata } : {}),\n };\n}\n\nfunction matchesQuery(item: Unstable_TriggerItem, lower: string): boolean {\n if (!lower) return true;\n if (item.id.toLowerCase().includes(lower)) return true;\n if (item.label.toLowerCase().includes(lower)) return true;\n if (item.description?.toLowerCase().includes(lower)) return true;\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAsFA,SAAgBkC,2BACdC,SAMA;CACA,MAAMG,MAAMpC,OAAO;CAEnB,MAAMiB,QAAQgB,SAAShB;CACvB,MAAMM,aAAaU,SAASV;CAC5B,MAAMc,eACJJ,SAAST,6BAA6B,CAACP,SAAS,CAACM;CACnD,MAAMe,cACJ,OAAOD,iBAAiB,WAAWA,eAAeE,KAAAA;CACpD,MAAMC,aAAaH,iBAAiB;CACpC,MAAMZ,YAAYQ,SAASR;CAC3B,MAAMC,aAAaO,SAASP;CAsF5B,OAAO;EACLQ,SArFcpC,cAAuC;GACrD,MAAM2C,6BAAqD;IACzD,IAAI,CAACD,YAAY,OAAO,CAAA;IAExB,MAAMK,QADMT,IAAIO,OAAO,CAAC,CAACC,gBACXF,CAAG,CAACG;IAClB,IAAI,CAACA,OAAO,OAAO,CAAA;IACnB,MAAMzB,cAAckB,aAAalB;IACjC,MAAM0B,cAAcR,aAAaxB;IACjC,OAAOiC,OAAOC,QAAQH,KAAK,CAAC,CAACI,KAAK,CAACC,MAAMC,UACvCC,cAAc;KACZ1C,IAAIwC;KACJvC,MAAM;KACNC,OAAOQ,cAAcA,YAAY8B,IAAI,IAAIA;KACzCrC,aAAasC,KAAKtC,eAAe0B,KAAAA;KACjCzB,MAAMgC;IACR,CAAC,CACH;GACF;GAGA,IAAIvB,cAAcA,WAAW8B,SAAS,GAAG;IACvC,MAAMC,SAAS/B,WAAW0B,KAAKM,SAAS;KACtC7C,IAAI6C,IAAI7C;KACRE,OAAO2C,IAAI3C;KACXK,OAAOsC,IAAItC,M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AOM;GAAYiB;GAAYF;EAAW,CAWjDJ;EACAC,WAVgBrC,eACT;GACL2B,WAAWA,aAAapB;GACxB,GAAIqB,aAAa,EAAEA,WAAW,IAAI,CAAC;EACrC,IACA,CAACD,WAAWC,UAAU,CAKtBS;EACA,GAAIF,SAASL,UAAU,EAAEA,SAASK,QAAQL,QAAQ,IAAI,CAAC;EACvD,GAAIK,SAASH,eAAe,EAAEA,cAAcG,QAAQH,aAAa,IAAI,CAAC;CACxE;AACF;AAEA,SAASsB,cAAcwB,GAA2C;CAChE,MAAM7D,WACJ6D,EAAE9D,SAASyB,KAAAA,IAAY;EAAE,GAAIqC,EAAE7D,YAAY,CAAC;EAAID,MAAM8D,EAAE9D;CAAK,IAAI8D,EAAE7D;CACrE,OAAO;EACLL,IAAIkE,EAAElE;EACNC,MAAMiE,EAAEjE;EACRC,OAAOgE,EAAEhE;EACT,GAAIgE,EAAE/D,gBAAgB0B,KAAAA,IAAY,EAAE1B,aAAa+D,EAAE/D,YAAY,IAAI,CAAC;EACpE,GAAIE,aAAawB,KAAAA,IAAY,EAAExB,SAAS,IAAI,CAAC;CAC/C;AACF;AAEA,SAASqD,aAAazC,MAA4BqC,OAAwB;CACxE,IAAI,CAACA,OAAO,OAAO;CACnB,IAAIrC,KAAKjB,GAAGuD,YAAY,CAAC,CAACY,SAASb,KAAK,GAAG,OAAO;CAClD,IAAIrC,KAAKf,MAAMqD,YAAY,CAAC,CAACY,SAASb,KAAK,GAAG,OAAO;CACrD,IAAIrC,KAAKd,aAAaoD,YAAY,CAAC,CAACY,SAASb,KAAK,GAAG,OAAO;CAC5D,OAAO;AACT"}
1
+ {"version":3,"file":"useMentionAdapter.js","names":["useMemo","FC","useAui","Unstable_DirectiveFormatter","Unstable_TriggerAdapter","Unstable_TriggerCategory","Unstable_TriggerItem","unstable_defaultDirectiveFormatter","ReadonlyJSONObject","Unstable_IconComponent","className","Unstable_Mention","id","type","label","description","icon","metadata","Unstable_MentionCategory","items","Unstable_ModelContextToolsOptions","category","formatLabel","toolName","Unstable_UseMentionAdapterOptions","categories","includeModelContextTools","formatter","onInserted","item","iconMap","Record","fallbackIcon","Unstable_MentionDirective","unstable_useMentionAdapter","options","adapter","directive","aui","includeTools","toolsConfig","undefined","wantsTools","getModelContextTools","ctx","thread","getModelContext","tools","defaultIcon","Object","entries","map","name","tool","toTriggerItem","length","groups","cat","toolCategory","toolItems","allGroups","categoryItems","find","g","search","query","lower","toLowerCase","flatMap","filter","matchesQuery","flatItems","getFlatPool","seen","Set","i","t","has","m","includes"],"sources":["../../src/unstable/useMentionAdapter.ts"],"sourcesContent":["\"use client\";\n\nimport { useMemo, type FC } from \"react\";\nimport { useAui } from \"@assistant-ui/store\";\nimport type {\n Unstable_DirectiveFormatter,\n Unstable_TriggerAdapter,\n Unstable_TriggerCategory,\n Unstable_TriggerItem,\n} from \"@assistant-ui/core\";\nimport { unstable_defaultDirectiveFormatter } from \"@assistant-ui/core\";\nimport type { ReadonlyJSONObject } from \"assistant-stream/utils\";\n\n/** Icon component shape consumed by `ComposerTriggerPopover`'s `iconMap`. */\nexport type Unstable_IconComponent = FC<{ className?: string }>;\n\nexport type Unstable_Mention = {\n readonly id: string;\n readonly type: string;\n readonly label: string;\n readonly description?: string | undefined;\n /** Shortcut for `metadata.icon`; merged with `metadata` if both are given. */\n readonly icon?: string | undefined;\n readonly metadata?: ReadonlyJSONObject | undefined;\n};\n\nexport type Unstable_MentionCategory = {\n readonly id: string;\n readonly label: string;\n readonly items: readonly Unstable_Mention[];\n};\n\nexport type Unstable_ModelContextToolsOptions = {\n /** Wrap tools in a dedicated category (drill-down mode). */\n readonly category?: { readonly id: string; readonly label: string };\n /** Format tool name for display. */\n readonly formatLabel?: (toolName: string) => string;\n /** Default icon key for each tool. */\n readonly icon?: string;\n};\n\nexport type Unstable_UseMentionAdapterOptions = {\n /** Flat mention list. Ignored when `categories` is set. */\n readonly items?: readonly Unstable_Mention[];\n /** Categorized mentions for drill-down navigation. */\n readonly categories?: readonly Unstable_MentionCategory[];\n /**\n * How tools registered in model context integrate.\n * - `false`: exclude.\n * - `true`: include (default when no `items`/`categories`; as a category\n * if `categories` is set, flat otherwise).\n * - object: explicit config.\n *\n * Omitted → defaults to `true` iff neither `items` nor `categories`.\n */\n readonly includeModelContextTools?:\n | boolean\n | Unstable_ModelContextToolsOptions;\n /** Directive formatter. @default unstable_defaultDirectiveFormatter */\n readonly formatter?: Unstable_DirectiveFormatter;\n /** Fires after an item is inserted into the composer. */\n readonly onInserted?: (item: Unstable_TriggerItem) => void;\n /** Maps `metadata.icon` / `category.id` string keys to React components. */\n readonly iconMap?: Record<string, Unstable_IconComponent>;\n /** Fallback icon when no entry in `iconMap` matches. */\n readonly fallbackIcon?: Unstable_IconComponent;\n};\n\nexport type Unstable_MentionDirective = {\n readonly formatter: Unstable_DirectiveFormatter;\n readonly onInserted?: ((item: Unstable_TriggerItem) => void) | undefined;\n};\n\n/**\n * @deprecated Under active development and might change without notice.\n *\n * Creates a spreadable `{ adapter, directive }` bundle for `@` mentions.\n * Supports tools registered in model context, explicit items, or both —\n * flat or categorized.\n *\n * @example\n * ```tsx\n * const mention = unstable_useMentionAdapter();\n * <ComposerTriggerPopover char=\"@\" {...mention} />\n * ```\n */\nexport function unstable_useMentionAdapter(\n options?: Unstable_UseMentionAdapterOptions,\n): {\n adapter: Unstable_TriggerAdapter;\n directive: Unstable_MentionDirective;\n iconMap?: Record<string, Unstable_IconComponent>;\n fallbackIcon?: Unstable_IconComponent;\n} {\n const aui = useAui();\n\n const items = options?.items;\n const categories = options?.categories;\n const includeTools =\n options?.includeModelContextTools ?? (!items && !categories);\n const toolsConfig =\n typeof includeTools === \"object\" ? includeTools : undefined;\n const wantsTools = includeTools !== false;\n const formatter = options?.formatter;\n const onInserted = options?.onInserted;\n\n const adapter = useMemo<Unstable_TriggerAdapter>(() => {\n const getModelContextTools = (): Unstable_TriggerItem[] => {\n if (!wantsTools) return [];\n const ctx = aui.thread.getModelContext();\n const tools = ctx.tools;\n if (!tools) return [];\n const formatLabel = toolsConfig?.formatLabel;\n const defaultIcon = toolsConfig?.icon;\n return Object.entries(tools).map(([name, tool]) =>\n toTriggerItem({\n id: name,\n type: \"tool\",\n label: formatLabel ? formatLabel(name) : name,\n description: tool.description ?? undefined,\n icon: defaultIcon,\n }),\n );\n };\n\n // Categorized: drill-down mode\n if (categories && categories.length > 0) {\n const groups = categories.map((cat) => ({\n id: cat.id,\n label: cat.label,\n items: cat.items.map(toTriggerItem),\n }));\n\n let toolCategory: {\n id: string;\n label: string;\n items: Unstable_TriggerItem[];\n } | null = null;\n if (wantsTools) {\n const toolItems = getModelContextTools();\n if (toolItems.length > 0) {\n toolCategory = {\n id: toolsConfig?.category?.id ?? \"tools\",\n label: toolsConfig?.category?.label ?? \"Tools\",\n items: toolItems,\n };\n }\n }\n const allGroups = toolCategory ? [...groups, toolCategory] : groups;\n\n return {\n categories: () => allGroups.map(({ id, label }) => ({ id, label })),\n categoryItems: (id) => allGroups.find((g) => g.id === id)?.items ?? [],\n search: (query) => {\n const lower = query.toLowerCase();\n return allGroups\n .flatMap((g) => g.items)\n .filter((item) => matchesQuery(item, lower));\n },\n };\n }\n\n // Flat: items + (optionally) tools, all in one search pool\n const flatItems = (items ?? []).map(toTriggerItem);\n const getFlatPool = (): Unstable_TriggerItem[] => {\n if (!wantsTools) return flatItems;\n const toolItems = getModelContextTools();\n // Dedupe by id — explicit items win.\n const seen = new Set(flatItems.map((i) => i.id));\n return [...flatItems, ...toolItems.filter((t) => !seen.has(t.id))];\n };\n\n return {\n categories: (): readonly Unstable_TriggerCategory[] => [],\n categoryItems: () => [],\n search: (query) => {\n const lower = query.toLowerCase();\n return getFlatPool().filter((item) => matchesQuery(item, lower));\n },\n };\n }, [aui, items, categories, wantsTools, toolsConfig]);\n\n const directive = useMemo<Unstable_MentionDirective>(\n () => ({\n formatter: formatter ?? unstable_defaultDirectiveFormatter,\n ...(onInserted ? { onInserted } : {}),\n }),\n [formatter, onInserted],\n );\n\n return {\n adapter,\n directive,\n ...(options?.iconMap ? { iconMap: options.iconMap } : {}),\n ...(options?.fallbackIcon ? { fallbackIcon: options.fallbackIcon } : {}),\n };\n}\n\nfunction toTriggerItem(m: Unstable_Mention): Unstable_TriggerItem {\n const metadata =\n m.icon !== undefined ? { ...(m.metadata ?? {}), icon: m.icon } : m.metadata;\n return {\n id: m.id,\n type: m.type,\n label: m.label,\n ...(m.description !== undefined ? { description: m.description } : {}),\n ...(metadata !== undefined ? { metadata } : {}),\n };\n}\n\nfunction matchesQuery(item: Unstable_TriggerItem, lower: string): boolean {\n if (!lower) return true;\n if (item.id.toLowerCase().includes(lower)) return true;\n if (item.label.toLowerCase().includes(lower)) return true;\n if (item.description?.toLowerCase().includes(lower)) return true;\n return false;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAsFA,SAAgBkC,2BACdC,SAMA;CACA,MAAMG,MAAMpC,OAAO;CAEnB,MAAMiB,QAAQgB,SAAShB;CACvB,MAAMM,aAAaU,SAASV;CAC5B,MAAMc,eACJJ,SAAST,6BAA6B,CAACP,SAAS,CAACM;CACnD,MAAMe,cACJ,OAAOD,iBAAiB,WAAWA,eAAeE,KAAAA;CACpD,MAAMC,aAAaH,iBAAiB;CACpC,MAAMZ,YAAYQ,SAASR;CAC3B,MAAMC,aAAaO,SAASP;CAsF5B,OAAO;EACLQ,SArFcpC,cAAuC;GACrD,MAAM2C,6BAAqD;IACzD,IAAI,CAACD,YAAY,OAAO,CAAA;IAExB,MAAMK,QADMT,IAAIO,OAAOC,gBACTF,CAAG,CAACG;IAClB,IAAI,CAACA,OAAO,OAAO,CAAA;IACnB,MAAMzB,cAAckB,aAAalB;IACjC,MAAM0B,cAAcR,aAAaxB;IACjC,OAAOiC,OAAOC,QAAQH,KAAK,CAAC,CAACI,KAAK,CAACC,MAAMC,UACvCC,cAAc;KACZ1C,IAAIwC;KACJvC,MAAM;KACNC,OAAOQ,cAAcA,YAAY8B,IAAI,IAAIA;KACzCrC,aAAasC,KAAKtC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9 @@ const SmoothContextProvider = (t0) => {
21
21
  const outer = useSmoothContext(t1);
22
22
  const aui = useAui();
23
23
  let t2;
24
- if ($[1] !== aui) {
25
- t2 = () => makeSmoothContext(aui.part().getState().status);
26
- $[1] = aui;
24
+ if ($[1] !== aui.part) {
25
+ t2 = () => makeSmoothContext(aui.part.getState().status);
26
+ $[1] = aui.part;
27
27
  $[2] = t2;
28
28
  } else t2 = $[2];
29
29
  const [context] = useState(t2);
@@ -1 +1 @@
1
- {"version":3,"file":"SmoothContext.js","names":["c","_c","ComponentType","createContext","FC","forwardRef","PropsWithChildren","useContext","useState","ReadonlyStore","create","UseBoundStore","MessagePartStatus","ToolCallMessagePartStatus","useAui","createContextStoreHook","SmoothContextValue","useSmoothStatus","SmoothContext","makeSmoothContext","initialState","SmoothContextProvider","t0","$","children","t1","Symbol","for","optional","outer","useSmoothContext","aui","t2","part","getState","status","context","t3","withSmoothContextProvider","Component","C","Wrapped","props","ref","displayName","options","Error","useSmoothStatusStore"],"sources":["../../../src/utils/smooth/SmoothContext.tsx"],"sourcesContent":["\"use client\";\n\nimport {\n type ComponentType,\n createContext,\n type FC,\n forwardRef,\n type PropsWithChildren,\n useContext,\n useState,\n} from \"react\";\nimport type { ReadonlyStore } from \"../../context/ReadonlyStore\";\nimport { create, type UseBoundStore } from \"zustand\";\nimport type {\n MessagePartStatus,\n ToolCallMessagePartStatus,\n} from \"@assistant-ui/core\";\nimport { useAui } from \"@assistant-ui/store\";\nimport { createContextStoreHook } from \"../../context/react/utils/createContextStoreHook\";\n\ntype SmoothContextValue = {\n useSmoothStatus: UseBoundStore<\n ReadonlyStore<MessagePartStatus | ToolCallMessagePartStatus>\n >;\n};\n\nconst SmoothContext = createContext<SmoothContextValue | null>(null);\n\nconst makeSmoothContext = (\n initialState: MessagePartStatus | ToolCallMessagePartStatus,\n) => {\n const useSmoothStatus = create(() => initialState);\n return { useSmoothStatus };\n};\n\nexport const SmoothContextProvider: FC<PropsWithChildren> = ({ children }) => {\n const outer = useSmoothContext({ optional: true });\n const aui = useAui();\n\n const [context] = useState(() =>\n makeSmoothContext(aui.part().getState().status),\n );\n\n // do not wrap if there is an outer SmoothContextProvider\n if (outer) return children;\n\n return (\n <SmoothContext.Provider value={context}>{children}</SmoothContext.Provider>\n );\n};\n\nexport const withSmoothContextProvider = <C extends ComponentType<any>>(\n Component: C,\n): C => {\n const Wrapped = forwardRef((props, ref) => {\n return (\n <SmoothContextProvider>\n <Component {...(props as any)} ref={ref} />\n </SmoothContextProvider>\n );\n });\n Wrapped.displayName = Component.displayName;\n return Wrapped as any;\n};\n\nfunction useSmoothContext(options?: {\n optional?: false | undefined;\n}): SmoothContextValue;\nfunction useSmoothContext(options?: {\n optional?: boolean | undefined;\n}): SmoothContextValue | null;\nfunction useSmoothContext(options?: { optional?: boolean | undefined }) {\n const context = useContext(SmoothContext);\n if (!options?.optional && !context)\n throw new Error(\n \"This component must be used within a SmoothContextProvider.\",\n );\n return context;\n}\n\nexport const { useSmoothStatus, useSmoothStatusStore } = createContextStoreHook(\n useSmoothContext,\n \"useSmoothStatus\",\n);\n"],"mappings":";;;;;;;;AA0BA,MAAMkB,gBAAgBf,cAAyC,IAAI;AAEnE,MAAMgB,qBACJC,iBACG;CAEH,OAAO,EAAEH,iBADeP,aAAaU,YAC5BH,EAAgB;AAC3B;AAEA,MAAaI,yBAA+CC,OAAA;CAAA,MAAAC,IAAAtB,EAAA,CAAA;CAAC,MAAA,EAAAuB,aAAAF;CAAY,IAAAG;CAAA,IAAAF,EAAA,OAAAG,OAAAC,IAAA,2BAAA,GAAA;EACxCF,KAAA,EAAAG,UAAY,KAAK;EAACL,EAAA,KAAAE;CAAA,OAAAA,KAAAF,EAAA;CAAjD,MAAAM,QAAcC,iBAAiBL,EAAkB;CACjD,MAAAM,MAAYjB,OAAO;CAAE,IAAAkB;CAAA,IAAAT,EAAA,OAAAQ,KAAA;EAEMC,WACzBb,kBAAkBY,IAAGE,KAAM,CAAC,CAAAC,SAAU,CAAC,CAAAC,MAAO;EAACZ,EAAA,KAAAQ;EAAAR,EAAA,KAAAS;CAAA,OAAAA,KAAAT,EAAA;CADjD,MAAA,CAAAa,WAAkB5B,SAASwB,EAE3B;CAGA,IAAIH,OAAK,OAASL;CAAS,IAAAa;CAAA,IAAAd,EAAA,OAAAC,YAAAD,EAAA,OAAAa,SAAA;EAGzBC,KAAA,oBAAA,cAAA,UAAA;GAA+BD,OAAAA;GAAUZ;EAAS,CAAA;EAAyBD,EAAA,KAAAC;EAAAD,EAAA,KAAAa;EAAAb,EAAA,KAAAc;CAAA,OAAAA,KAAAd,EAAA;CAAA,OAA3Ec;AAA2E;AAI/E,MAAaC,6BACXC,cACM;CACN,MAAME,UAAUpC,YAAWqC,OAAAC,QAAA;EAAA,MAAApB,IAAAtB,EAAA,CAAA;EAGL,MAAAqB,KAAAoB;EAAY,IAAAjB;EAAA,IAAAF,EAAA,OAAAoB,OAAApB,EAAA,OAAAD,IAAA;GAD9BG,KAAA,oBAAC,uBAAD,EAAA,UACE,oBAAC,WAAD;IAAU,GAAMH;IAAoBqB;GAAG,CAAA,EADnB,CAAA;GAEEpB,EAAA,KAAAoB;GAAApB,EAAA,KAAAD;GAAAC,EAAA,KAAAE;EAAA,OAAAA,KAAAF,EAAA;EAAA,OAFxBE;CAEwB,CAE3B;CACDgB,QAAQG,cAAcL,UAAUK;CAChC,OAAOH;AACT;AAQA,SAAAX,iBAAAe,SAAA;CACE,MAAAT,UAAgB7B,WAAWW,aAAa;CACxC,IAAI,CAAC2B,SAAOjB,YAAR,CAAuBQ,SACzB,MAAM,IAAIU,MACR,6DACF;CAAE,OACGV;AAAO;AAGhB,MAAa,EAAEnB,iBAAiB8B,yBAAyBhC,uBACvDe,kBACA,iBACF"}
1
+ {"version":3,"file":"SmoothContext.js","names":["c","_c","ComponentType","createContext","FC","forwardRef","PropsWithChildren","useContext","useState","ReadonlyStore","create","UseBoundStore","MessagePartStatus","ToolCallMessagePartStatus","useAui","createContextStoreHook","SmoothContextValue","useSmoothStatus","SmoothContext","makeSmoothContext","initialState","SmoothContextProvider","t0","$","children","t1","Symbol","for","optional","outer","useSmoothContext","aui","t2","part","getState","status","context","t3","withSmoothContextProvider","Component","C","Wrapped","props","ref","displayName","options","Error","useSmoothStatusStore"],"sources":["../../../src/utils/smooth/SmoothContext.tsx"],"sourcesContent":["\"use client\";\n\nimport {\n type ComponentType,\n createContext,\n type FC,\n forwardRef,\n type PropsWithChildren,\n useContext,\n useState,\n} from \"react\";\nimport type { ReadonlyStore } from \"../../context/ReadonlyStore\";\nimport { create, type UseBoundStore } from \"zustand\";\nimport type {\n MessagePartStatus,\n ToolCallMessagePartStatus,\n} from \"@assistant-ui/core\";\nimport { useAui } from \"@assistant-ui/store\";\nimport { createContextStoreHook } from \"../../context/react/utils/createContextStoreHook\";\n\ntype SmoothContextValue = {\n useSmoothStatus: UseBoundStore<\n ReadonlyStore<MessagePartStatus | ToolCallMessagePartStatus>\n >;\n};\n\nconst SmoothContext = createContext<SmoothContextValue | null>(null);\n\nconst makeSmoothContext = (\n initialState: MessagePartStatus | ToolCallMessagePartStatus,\n) => {\n const useSmoothStatus = create(() => initialState);\n return { useSmoothStatus };\n};\n\nexport const SmoothContextProvider: FC<PropsWithChildren> = ({ children }) => {\n const outer = useSmoothContext({ optional: true });\n const aui = useAui();\n\n const [context] = useState(() =>\n makeSmoothContext(aui.part.getState().status),\n );\n\n // do not wrap if there is an outer SmoothContextProvider\n if (outer) return children;\n\n return (\n <SmoothContext.Provider value={context}>{children}</SmoothContext.Provider>\n );\n};\n\nexport const withSmoothContextProvider = <C extends ComponentType<any>>(\n Component: C,\n): C => {\n const Wrapped = forwardRef((props, ref) => {\n return (\n <SmoothContextProvider>\n <Component {...(props as any)} ref={ref} />\n </SmoothContextProvider>\n );\n });\n Wrapped.displayName = Component.displayName;\n return Wrapped as any;\n};\n\nfunction useSmoothContext(options?: {\n optional?: false | undefined;\n}): SmoothContextValue;\nfunction useSmoothContext(options?: {\n optional?: boolean | undefined;\n}): SmoothContextValue | null;\nfunction useSmoothContext(options?: { optional?: boolean | undefined }) {\n const context = useContext(SmoothContext);\n if (!options?.optional && !context)\n throw new Error(\n \"This component must be used within a SmoothContextProvider.\",\n );\n return context;\n}\n\nexport const { useSmoothStatus, useSmoothStatusStore } = createContextStoreHook(\n useSmoothContext,\n \"useSmoothStatus\",\n);\n"],"mappings":";;;;;;;;AA0BA,MAAMkB,gBAAgBf,cAAyC,IAAI;AAEnE,MAAMgB,qBACJC,iBACG;CAEH,OAAO,EAAEH,iBADeP,aAAaU,YAC5BH,EAAgB;AAC3B;AAEA,MAAaI,yBAA+CC,OAAA;CAAA,MAAAC,IAAAtB,EAAA,CAAA;CAAC,MAAA,EAAAuB,aAAAF;CAAY,IAAAG;CAAA,IAAAF,EAAA,OAAAG,OAAAC,IAAA,2BAAA,GAAA;EACxCF,KAAA,EAAAG,UAAY,KAAK;EAACL,EAAA,KAAAE;CAAA,OAAAA,KAAAF,EAAA;CAAjD,MAAAM,QAAcC,iBAAiBL,EAAkB;CACjD,MAAAM,MAAYjB,OAAO;CAAE,IAAAkB;CAAA,IAAAT,EAAA,OAAAQ,IAAAE,MAAA;EAEMD,WACzBb,kBAAkBY,IAAGE,KAAKC,SAAU,CAAC,CAAAC,MAAO;EAACZ,EAAA,KAAAQ,IAAAE;EAAAV,EAAA,KAAAS;CAAA,OAAAA,KAAAT,EAAA;CAD/C,MAAA,CAAAa,WAAkB5B,SAASwB,EAE3B;CAGA,IAAIH,OAAK,OAASL;CAAS,IAAAa;CAAA,IAAAd,EAAA,OAAAC,YAAAD,EAAA,OAAAa,SAAA;EAGzBC,KAAA,oBAAA,cAAA,UAAA;GAA+BD,OAAAA;GAAUZ;EAAS,CAAA;EAAyBD,EAAA,KAAAC;EAAAD,EAAA,KAAAa;EAAAb,EAAA,KAAAc;CAAA,OAAAA,KAAAd,EAAA;CAAA,OAA3Ec;AAA2E;AAI/E,MAAaC,6BACXC,cACM;CACN,MAAME,UAAUpC,YAAWqC,OAAAC,QAAA;EAAA,MAAApB,IAAAtB,EAAA,CAAA;EAGL,MAAAqB,KAAAoB;EAAY,IAAAjB;EAAA,IAAAF,EAAA,OAAAoB,OAAApB,EAAA,OAAAD,IAAA;GAD9BG,KAAA,oBAAC,uBAAD,EAAA,UACE,oBAAC,WAAD;IAAU,GAAMH;IAAoBqB;GAAG,CAAA,EADnB,CAAA;GAEEpB,EAAA,KAAAoB;GAAApB,EAAA,KAAAD;GAAAC,EAAA,KAAAE;EAAA,OAAAA,KAAAF,EAAA;EAAA,OAFxBE;CAEwB,CAE3B;CACDgB,QAAQG,cAAcL,UAAUK;CAChC,OAAOH;AACT;AAQA,SAAAX,iBAAAe,SAAA;CACE,MAAAT,UAAgB7B,WAAWW,aAAa;CACxC,IAAI,CAAC2B,SAAOjB,YAAR,CAAuBQ,SACzB,MAAM,IAAIU,MACR,6DACF;CAAE,OACGV;AAAO;AAGhB,MAAa,EAAEnB,iBAAiB8B,yBAAyBhC,uBACvDe,kBACA,iBACF"}
@@ -90,7 +90,7 @@ const useSmooth = (state, smooth = false) => {
90
90
  const minCommitMs = positiveOr(options?.minCommitMs, 0);
91
91
  const [displayedText, setDisplayedText] = useState(state.status.type === "running" ? "" : text);
92
92
  const aui = useAui();
93
- const part = useAuiState(() => aui.part());
93
+ const part = useAuiState(() => aui.part);
94
94
  const [prevPart, setPrevPart] = useState(part);
95
95
  if (part !== prevPart || !text.startsWith(displayedText)) {
96
96
  setPrevPart(part);
@@ -1 +1 @@
1
- {"version":3,"file":"useSmooth.js","names":["useEffect","useMemo","useRef","useState","useAui","useAuiState","MessagePartStatus","ReasoningMessagePart","TextMessagePart","MessagePartState","useCallbackRef","useMediaQuery","useSmoothStatusStore","writableStore","SmoothOptions","drainMs","maxCharIntervalMs","maxCharsPerFrame","minCommitMs","DEFAULT_DRAIN_MS","DEFAULT_MAX_CHAR_INTERVAL_MS","TextStreamAnimator","animationFrameId","lastUpdateTime","Date","now","lastCommitTime","targetText","Infinity","currentText","setText","newText","constructor","start","animate","stop","cancelAnimationFrame","currentTime","deltaTime","timeToConsume","remainingChars","length","baseTimePerChar","Math","min","frameLimit","charsToAdd","requestAnimationFrame","slice","isComplete","SMOOTH_STATUS","Object","freeze","type","positiveOr","value","fallback","undefined","useSmooth","state","smooth","text","reduceMotion","options","enabled","displayedText","setDisplayedText","status","aui","part","prevPart","setPrevPart","startsWith","smoothStatusStore","optional","target","setState","animatorRef","animatorPartRef","partChanged","current"],"sources":["../../../src/utils/smooth/useSmooth.ts"],"sourcesContent":["\"use client\";\n\nimport { useEffect, useMemo, useRef, useState } from \"react\";\nimport { useAui, useAuiState } from \"@assistant-ui/store\";\nimport type {\n MessagePartStatus,\n ReasoningMessagePart,\n TextMessagePart,\n MessagePartState,\n} from \"@assistant-ui/core\";\nimport { useCallbackRef } from \"@radix-ui/react-use-callback-ref\";\nimport { useMediaQuery } from \"../hooks/useMediaQuery\";\nimport { useSmoothStatusStore } from \"./SmoothContext\";\nimport { writableStore } from \"../../context/ReadonlyStore\";\n\n/**\n * Tuning options for the smooth text streaming animation.\n */\nexport type SmoothOptions = {\n /**\n * Target time in milliseconds to drain the backlog of unrevealed\n * characters. Larger values reveal long backlogs more gradually.\n * @default 250\n */\n drainMs?: number | undefined;\n /**\n * Maximum time in milliseconds between revealed characters, i.e. the\n * slowest reveal rate when the backlog is short.\n * @default 5\n */\n maxCharIntervalMs?: number | undefined;\n /**\n * Maximum number of characters revealed per animation frame.\n * @default Infinity\n */\n maxCharsPerFrame?: number | undefined;\n /**\n * Minimum time in milliseconds between committed updates. The reveal keeps\n * advancing every frame, but the visible text (and the downstream re-render\n * and markdown re-parse it triggers) is committed at most once per interval.\n * The final frame always commits. `0` commits every frame.\n * @default 0\n */\n minCommitMs?: number | undefined;\n};\n\nconst DEFAULT_DRAIN_MS = 250;\nconst DEFAULT_MAX_CHAR_INTERVAL_MS = 5;\n\nclass TextStreamAnimator {\n private animationFrameId: number | null = null;\n private lastUpdateTime: number = Date.now();\n public lastCommitTime: number = 0;\n\n public targetText: string = \"\";\n public drainMs: number = DEFAULT_DRAIN_MS;\n public maxCharIntervalMs: number = DEFAULT_MAX_CHAR_INTERVAL_MS;\n public maxCharsPerFrame: number = Infinity;\n public minCommitMs: number = 0;\n\n public currentText: string;\n private setText: (newText: string) => void;\n\n constructor(currentText: string, setText: (newText: string) => void) {\n this.currentText = currentText;\n this.setText = setText;\n }\n\n start() {\n if (this.animationFrameId !== null) return;\n this.lastUpdateTime = Date.now();\n this.animate();\n }\n\n stop() {\n if (this.animationFrameId !== null) {\n cancelAnimationFrame(this.animationFrameId);\n this.animationFrameId = null;\n }\n }\n\n private animate = () => {\n const currentTime = Date.now();\n const deltaTime = currentTime - this.lastUpdateTime;\n let timeToConsume = deltaTime;\n\n const remainingChars = this.targetText.length - this.currentText.length;\n const baseTimePerChar = Math.min(\n this.maxCharIntervalMs,\n this.drainMs / remainingChars,\n );\n\n const frameLimit = Math.min(remainingChars, this.maxCharsPerFrame);\n let charsToAdd = 0;\n while (timeToConsume >= baseTimePerChar && charsToAdd < frameLimit) {\n charsToAdd++;\n timeToConsume -= baseTimePerChar;\n }\n // A cap-limited frame must not bank its surplus time, or the next\n // frame would burst past the cap.\n if (charsToAdd === frameLimit && frameLimit === this.maxCharsPerFrame) {\n timeToConsume = 0;\n }\n\n if (charsToAdd !== remainingChars) {\n this.animationFrameId = requestAnimationFrame(this.animate);\n } else {\n this.animationFrameId = null;\n }\n if (charsToAdd === 0) return;\n\n this.currentText = this.targetText.slice(\n 0,\n this.currentText.length + charsToAdd,\n );\n this.lastUpdateTime = currentTime - timeToConsume;\n\n const isComplete = charsToAdd === remainingChars;\n if (isComplete || currentTime - this.lastCommitTime >= this.minCommitMs) {\n this.lastCommitTime = currentTime;\n this.setText(this.currentText);\n }\n };\n}\n\nconst SMOOTH_STATUS: MessagePartStatus = Object.freeze({\n type: \"running\",\n});\n\nconst positiveOr = (value: number | undefined, fallback: number): number =>\n value !== undefined && value > 0 ? value : fallback;\n\n/**\n * Animates streamed message part text with a typewriter-style reveal.\n *\n * Takes the current part state and a `smooth` argument: `false` disables,\n * `true` uses the default rate, and a {@link SmoothOptions} object tunes\n * the reveal. Returns the part state with `text` replaced by the revealed\n * prefix and `status` reporting `running` until the reveal catches up.\n *\n * The reveal auto-disables under `prefers-reduced-motion: reduce`,\n * committing the full text immediately; this takes precedence over an\n * explicit `smooth={true}`.\n *\n * @example\n * ```tsx\n * const { text, status } = useSmooth(useMessagePartText(), {\n * drainMs: 500,\n * maxCharsPerFrame: 30,\n * });\n * ```\n */\nexport const useSmooth = (\n state: MessagePartState & (TextMessagePart | ReasoningMessagePart),\n smooth: boolean | SmoothOptions = false,\n): MessagePartState & (TextMessagePart | ReasoningMessagePart) => {\n const { text } = state;\n const reduceMotion = useMediaQuery(\"(prefers-reduced-motion: reduce)\");\n const options =\n typeof smooth === \"object\" && smooth !== null ? smooth : undefined;\n const enabled = smooth !== false && smooth !== null && !reduceMotion;\n const drainMs = positiveOr(options?.drainMs, DEFAULT_DRAIN_MS);\n const maxCharIntervalMs = positiveOr(\n options?.maxCharIntervalMs,\n DEFAULT_MAX_CHAR_INTERVAL_MS,\n );\n const maxCharsPerFrame = positiveOr(options?.maxCharsPerFrame, Infinity);\n const minCommitMs = positiveOr(options?.minCommitMs, 0);\n\n const [displayedText, setDisplayedText] = useState(\n state.status.type === \"running\" ? \"\" : text,\n );\n\n // Render-phase resync on part flip or text discontinuity, so the\n // first paint after a thread switch never shows the previous\n // part's text (#4051). `displayedText` is already a prefix of\n // `text` during normal streaming, so use it as the previous-text\n // reference instead of carrying separate state — avoids the\n // double render per streaming token. Read part identity through\n // `useAuiState` so we actually subscribe to its changes instead\n // of relying on a render-time proxy reference that may be stable\n // across thread swaps.\n const aui = useAui();\n const part = useAuiState(() => aui.part());\n const [prevPart, setPrevPart] = useState(part);\n if (part !== prevPart || !text.startsWith(displayedText)) {\n setPrevPart(part);\n setDisplayedText(state.status.type === \"running\" ? \"\" : text);\n }\n\n const smoothStatusStore = useSmoothStatusStore({ optional: true });\n const setText = useCallbackRef((text: string) => {\n setDisplayedText(text);\n if (smoothStatusStore) {\n const target =\n displayedText !== text || state.status.type === \"running\"\n ? SMOOTH_STATUS\n : state.status;\n writableStore(smoothStatusStore).setState(target, true);\n }\n });\n\n // TODO this is hacky\n useEffect(() => {\n if (smoothStatusStore) {\n const target =\n enabled && (displayedText !== text || state.status.type === \"running\")\n ? SMOOTH_STATUS\n : state.status;\n writableStore(smoothStatusStore).setState(target, true);\n }\n }, [smoothStatusStore, enabled, text, displayedText, state.status]);\n\n const [animatorRef] = useState<TextStreamAnimator>(\n new TextStreamAnimator(displayedText, setText),\n );\n\n useEffect(() => {\n animatorRef.drainMs = drainMs;\n animatorRef.maxCharIntervalMs = maxCharIntervalMs;\n animatorRef.maxCharsPerFrame = maxCharsPerFrame;\n animatorRef.minCommitMs = minCommitMs;\n }, [animatorRef, drainMs, maxCharIntervalMs, maxCharsPerFrame, minCommitMs]);\n\n const animatorPartRef = useRef(part);\n useEffect(() => {\n if (!enabled) {\n animatorRef.stop();\n return;\n }\n\n // Discontinuity: part flipped, or new text breaks continuation\n // of the animator's current target. Either case requires\n // resetting the cursor — without the part check, a new part\n // whose text happens to share a prefix with the previous target\n // would keep the stale cursor and flicker.\n const partChanged = animatorPartRef.current !== part;\n animatorPartRef.current = part;\n if (partChanged || !text.startsWith(animatorRef.targetText)) {\n if (state.status.type === \"running\") {\n animatorRef.currentText = \"\";\n animatorRef.targetText = text;\n animatorRef.lastCommitTime = 0;\n animatorRef.start();\n } else {\n animatorRef.currentText = text;\n animatorRef.targetText = text;\n animatorRef.stop();\n }\n return;\n }\n\n animatorRef.targetText = text;\n animatorRef.start();\n }, [animatorRef, enabled, text, state.status.type, part]);\n\n useEffect(() => {\n return () => {\n animatorRef.stop();\n };\n }, [animatorRef]);\n\n return useMemo(\n () =>\n enabled\n ? {\n ...state,\n text: displayedText,\n status: text === displayedText ? state.status : SMOOTH_STATUS,\n }\n : state,\n [enab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AAMQ,OAAOd;EAAMgB;CAAI,CAAC;CAExDrE,gBAAgB;EACd,aAAa;GACX6E,YAAY1C,KAAK;EACnB;CACF,GAAG,CAAC0C,WAAW,CAAC;CAEhB,OAAO5E,cAEH+D,UACI;EACE,GAAGL;EACHE,MAAMI;EACNE,QAAQN,SAASI,gBAAgBN,MAAMQ,SAASjB;CAClD,IACAS,OACN;EAACK;EAASC;EAAeN;EAAOE;CAAI,CACtC;AACF"}
1
+ {"version":3,"file":"useSmooth.js","names":["useEffect","useMemo","useRef","useState","useAui","useAuiState","MessagePartStatus","ReasoningMessagePart","TextMessagePart","MessagePartState","useCallbackRef","useMediaQuery","useSmoothStatusStore","writableStore","SmoothOptions","drainMs","maxCharIntervalMs","maxCharsPerFrame","minCommitMs","DEFAULT_DRAIN_MS","DEFAULT_MAX_CHAR_INTERVAL_MS","TextStreamAnimator","animationFrameId","lastUpdateTime","Date","now","lastCommitTime","targetText","Infinity","currentText","setText","newText","constructor","start","animate","stop","cancelAnimationFrame","currentTime","deltaTime","timeToConsume","remainingChars","length","baseTimePerChar","Math","min","frameLimit","charsToAdd","requestAnimationFrame","slice","isComplete","SMOOTH_STATUS","Object","freeze","type","positiveOr","value","fallback","undefined","useSmooth","state","smooth","text","reduceMotion","options","enabled","displayedText","setDisplayedText","status","aui","part","prevPart","setPrevPart","startsWith","smoothStatusStore","optional","target","setState","animatorRef","animatorPartRef","partChanged","current"],"sources":["../../../src/utils/smooth/useSmooth.ts"],"sourcesContent":["\"use client\";\n\nimport { useEffect, useMemo, useRef, useState } from \"react\";\nimport { useAui, useAuiState } from \"@assistant-ui/store\";\nimport type {\n MessagePartStatus,\n ReasoningMessagePart,\n TextMessagePart,\n MessagePartState,\n} from \"@assistant-ui/core\";\nimport { useCallbackRef } from \"@radix-ui/react-use-callback-ref\";\nimport { useMediaQuery } from \"../hooks/useMediaQuery\";\nimport { useSmoothStatusStore } from \"./SmoothContext\";\nimport { writableStore } from \"../../context/ReadonlyStore\";\n\n/**\n * Tuning options for the smooth text streaming animation.\n */\nexport type SmoothOptions = {\n /**\n * Target time in milliseconds to drain the backlog of unrevealed\n * characters. Larger values reveal long backlogs more gradually.\n * @default 250\n */\n drainMs?: number | undefined;\n /**\n * Maximum time in milliseconds between revealed characters, i.e. the\n * slowest reveal rate when the backlog is short.\n * @default 5\n */\n maxCharIntervalMs?: number | undefined;\n /**\n * Maximum number of characters revealed per animation frame.\n * @default Infinity\n */\n maxCharsPerFrame?: number | undefined;\n /**\n * Minimum time in milliseconds between committed updates. The reveal keeps\n * advancing every frame, but the visible text (and the downstream re-render\n * and markdown re-parse it triggers) is committed at most once per interval.\n * The final frame always commits. `0` commits every frame.\n * @default 0\n */\n minCommitMs?: number | undefined;\n};\n\nconst DEFAULT_DRAIN_MS = 250;\nconst DEFAULT_MAX_CHAR_INTERVAL_MS = 5;\n\nclass TextStreamAnimator {\n private animationFrameId: number | null = null;\n private lastUpdateTime: number = Date.now();\n public lastCommitTime: number = 0;\n\n public targetText: string = \"\";\n public drainMs: number = DEFAULT_DRAIN_MS;\n public maxCharIntervalMs: number = DEFAULT_MAX_CHAR_INTERVAL_MS;\n public maxCharsPerFrame: number = Infinity;\n public minCommitMs: number = 0;\n\n public currentText: string;\n private setText: (newText: string) => void;\n\n constructor(currentText: string, setText: (newText: string) => void) {\n this.currentText = currentText;\n this.setText = setText;\n }\n\n start() {\n if (this.animationFrameId !== null) return;\n this.lastUpdateTime = Date.now();\n this.animate();\n }\n\n stop() {\n if (this.animationFrameId !== null) {\n cancelAnimationFrame(this.animationFrameId);\n this.animationFrameId = null;\n }\n }\n\n private animate = () => {\n const currentTime = Date.now();\n const deltaTime = currentTime - this.lastUpdateTime;\n let timeToConsume = deltaTime;\n\n const remainingChars = this.targetText.length - this.currentText.length;\n const baseTimePerChar = Math.min(\n this.maxCharIntervalMs,\n this.drainMs / remainingChars,\n );\n\n const frameLimit = Math.min(remainingChars, this.maxCharsPerFrame);\n let charsToAdd = 0;\n while (timeToConsume >= baseTimePerChar && charsToAdd < frameLimit) {\n charsToAdd++;\n timeToConsume -= baseTimePerChar;\n }\n // A cap-limited frame must not bank its surplus time, or the next\n // frame would burst past the cap.\n if (charsToAdd === frameLimit && frameLimit === this.maxCharsPerFrame) {\n timeToConsume = 0;\n }\n\n if (charsToAdd !== remainingChars) {\n this.animationFrameId = requestAnimationFrame(this.animate);\n } else {\n this.animationFrameId = null;\n }\n if (charsToAdd === 0) return;\n\n this.currentText = this.targetText.slice(\n 0,\n this.currentText.length + charsToAdd,\n );\n this.lastUpdateTime = currentTime - timeToConsume;\n\n const isComplete = charsToAdd === remainingChars;\n if (isComplete || currentTime - this.lastCommitTime >= this.minCommitMs) {\n this.lastCommitTime = currentTime;\n this.setText(this.currentText);\n }\n };\n}\n\nconst SMOOTH_STATUS: MessagePartStatus = Object.freeze({\n type: \"running\",\n});\n\nconst positiveOr = (value: number | undefined, fallback: number): number =>\n value !== undefined && value > 0 ? value : fallback;\n\n/**\n * Animates streamed message part text with a typewriter-style reveal.\n *\n * Takes the current part state and a `smooth` argument: `false` disables,\n * `true` uses the default rate, and a {@link SmoothOptions} object tunes\n * the reveal. Returns the part state with `text` replaced by the revealed\n * prefix and `status` reporting `running` until the reveal catches up.\n *\n * The reveal auto-disables under `prefers-reduced-motion: reduce`,\n * committing the full text immediately; this takes precedence over an\n * explicit `smooth={true}`.\n *\n * @example\n * ```tsx\n * const { text, status } = useSmooth(useMessagePartText(), {\n * drainMs: 500,\n * maxCharsPerFrame: 30,\n * });\n * ```\n */\nexport const useSmooth = (\n state: MessagePartState & (TextMessagePart | ReasoningMessagePart),\n smooth: boolean | SmoothOptions = false,\n): MessagePartState & (TextMessagePart | ReasoningMessagePart) => {\n const { text } = state;\n const reduceMotion = useMediaQuery(\"(prefers-reduced-motion: reduce)\");\n const options =\n typeof smooth === \"object\" && smooth !== null ? smooth : undefined;\n const enabled = smooth !== false && smooth !== null && !reduceMotion;\n const drainMs = positiveOr(options?.drainMs, DEFAULT_DRAIN_MS);\n const maxCharIntervalMs = positiveOr(\n options?.maxCharIntervalMs,\n DEFAULT_MAX_CHAR_INTERVAL_MS,\n );\n const maxCharsPerFrame = positiveOr(options?.maxCharsPerFrame, Infinity);\n const minCommitMs = positiveOr(options?.minCommitMs, 0);\n\n const [displayedText, setDisplayedText] = useState(\n state.status.type === \"running\" ? \"\" : text,\n );\n\n // Render-phase resync on part flip or text discontinuity, so the\n // first paint after a thread switch never shows the previous\n // part's text (#4051). `displayedText` is already a prefix of\n // `text` during normal streaming, so use it as the previous-text\n // reference instead of carrying separate state — avoids the\n // double render per streaming token. Read part identity through\n // `useAuiState` so we actually subscribe to its changes instead\n // of relying on a render-time proxy reference that may be stable\n // across thread swaps.\n const aui = useAui();\n const part = useAuiState(() => aui.part);\n const [prevPart, setPrevPart] = useState(part);\n if (part !== prevPart || !text.startsWith(displayedText)) {\n setPrevPart(part);\n setDisplayedText(state.status.type === \"running\" ? \"\" : text);\n }\n\n const smoothStatusStore = useSmoothStatusStore({ optional: true });\n const setText = useCallbackRef((text: string) => {\n setDisplayedText(text);\n if (smoothStatusStore) {\n const target =\n displayedText !== text || state.status.type === \"running\"\n ? SMOOTH_STATUS\n : state.status;\n writableStore(smoothStatusStore).setState(target, true);\n }\n });\n\n // TODO this is hacky\n useEffect(() => {\n if (smoothStatusStore) {\n const target =\n enabled && (displayedText !== text || state.status.type === \"running\")\n ? SMOOTH_STATUS\n : state.status;\n writableStore(smoothStatusStore).setState(target, true);\n }\n }, [smoothStatusStore, enabled, text, displayedText, state.status]);\n\n const [animatorRef] = useState<TextStreamAnimator>(\n new TextStreamAnimator(displayedText, setText),\n );\n\n useEffect(() => {\n animatorRef.drainMs = drainMs;\n animatorRef.maxCharIntervalMs = maxCharIntervalMs;\n animatorRef.maxCharsPerFrame = maxCharsPerFrame;\n animatorRef.minCommitMs = minCommitMs;\n }, [animatorRef, drainMs, maxCharIntervalMs, maxCharsPerFrame, minCommitMs]);\n\n const animatorPartRef = useRef(part);\n useEffect(() => {\n if (!enabled) {\n animatorRef.stop();\n return;\n }\n\n // Discontinuity: part flipped, or new text breaks continuation\n // of the animator's current target. Either case requires\n // resetting the cursor — without the part check, a new part\n // whose text happens to share a prefix with the previous target\n // would keep the stale cursor and flicker.\n const partChanged = animatorPartRef.current !== part;\n animatorPartRef.current = part;\n if (partChanged || !text.startsWith(animatorRef.targetText)) {\n if (state.status.type === \"running\") {\n animatorRef.currentText = \"\";\n animatorRef.targetText = text;\n animatorRef.lastCommitTime = 0;\n animatorRef.start();\n } else {\n animatorRef.currentText = text;\n animatorRef.targetText = text;\n animatorRef.stop();\n }\n return;\n }\n\n animatorRef.targetText = text;\n animatorRef.start();\n }, [animatorRef, enabled, text, state.status.type, part]);\n\n useEffect(() => {\n return () => {\n animatorRef.stop();\n };\n }, [animatorRef]);\n\n return useMemo(\n () =>\n enabled\n ? {\n ...state,\n text: displayedText,\n status: text === displayedText ? state.status : SMOOTH_STATUS,\n }\n : state,\n [enabled, displayedText, state, text],\n );\n};\n"],"mappings":";;;;;;;;AA8CA,MAAMmB,mBAAmB;AACzB,MAAMC,+BAA+B;AAErC,IAAMC,qBAAN,MAAyB;CACvB,mBAA0C;CAC1C,iBAAiCG,KAAKC,IAAI;CAC1C,iBAAgC;CAEhC,aAA4B;CAC5B,UAAyBN;CACzB,oBAAmCC;CACnC,mBAAkCQ;CAClC,cAA6B;CAE7B;CACA;CAEAI,YAAYH,aAAqBC,SAAoC;EACnE,KAAKD,cAAcA;EACnB,KAAKC,UAAUA;CACjB;CAEAG,QAAQ;EACN,IAAI,KAAKX,qBAAqB,MAAM;EACpC,KAAKC,iBAAiBC,KAAKC,IAAI;EAC/B,KAAKS,QAAQ;CACf;CAEAC,OAAO;EACL,IAAI,KAAKb,qBAAqB,MAAM;GAClCc,qBAAqB,KAAKd,gBAAgB;GAC1C,KAAKA,mBAAmB;EAC1B;CACF;CAEA,gBAAwB;EACtB,MAAMe,cAAcb,KAAKC,IAAI;EAE7B,IAAIc,gBADcF,cAAc,KAA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AAkBC;CAAW,CAAC;CAE3E,MAAM4D,kBAAkB5E,OAAOmE,IAAI;CACnCrE,gBAAgB;EACd,IAAI,CAACgE,SAAS;GACZa,YAAY1C,KAAK;GACjB;EACF;EAOA,MAAM4C,cAAcD,gBAAgBE,YAAYX;EAChDS,gBAAgBE,UAAUX;EAC1B,IAAIU,eAAe,CAAClB,KAAKW,WAAWK,YAAYlD,UAAU,GAAG;GAC3D,IAAIgC,MAAMQ,OAAOd,SAAS,WAAW;IACnCwB,YAAYhD,cAAc;IAC1BgD,YAAYlD,aAAakC;IACzBgB,YAAYnD,iBAAiB;IAC7BmD,YAAY5C,MAAM;GACpB,OAAO;IACL4C,YAAYhD,cAAcgC;IAC1BgB,YAAYlD,aAAakC;IACzBgB,YAAY1C,KAAK;GACnB;GACA;EACF;EAEA0C,YAAYlD,aAAakC;EACzBgB,YAAY5C,MAAM;CACpB,GAAG;EAAC4C;EAAab;EAASH;EAAMF,MAAMQ,OAAOd;EAAMgB;CAAI,CAAC;CAExDrE,gBAAgB;EACd,aAAa;GACX6E,YAAY1C,KAAK;EACnB;CACF,GAAG,CAAC0C,WAAW,CAAC;CAEhB,OAAO5E,cAEH+D,UACI;EACE,GAAGL;EACHE,MAAMI;EACNE,QAAQN,SAASI,gBAAgBN,MAAMQ,SAASjB;CAClD,IACAS,OACN;EAACK;EAASC;EAAeN;EAAOE;CAAI,CACtC;AACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@assistant-ui/react",
3
- "version": "0.14.29",
3
+ "version": "0.15.0",
4
4
  "description": "Open-source TypeScript/React library for building production-grade AI chat experiences",
5
5
  "keywords": [
6
6
  "ai",
@@ -55,9 +55,9 @@
55
55
  ],
56
56
  "sideEffects": false,
57
57
  "dependencies": {
58
- "@assistant-ui/core": "^0.2.23",
59
- "@assistant-ui/store": "^0.2.22",
60
- "@assistant-ui/tap": "^0.9.6",
58
+ "@assistant-ui/core": "^0.3.0",
59
+ "@assistant-ui/store": "^0.3.0",
60
+ "@assistant-ui/tap": "^0.9.7",
61
61
  "@radix-ui/primitive": "^1.1.7",
62
62
  "@radix-ui/react-collection": "^1.1.15",
63
63
  "@radix-ui/react-compose-refs": "^1.1.5",
@@ -67,7 +67,7 @@
67
67
  "@radix-ui/react-use-controllable-state": "^1.2.6",
68
68
  "@radix-ui/react-use-escape-keydown": "^1.1.5",
69
69
  "assistant-cloud": "^0.1.37",
70
- "assistant-stream": "^0.3.28",
70
+ "assistant-stream": "^0.3.29",
71
71
  "nanoid": "^6.0.0",
72
72
  "radix-ui": "^1.6.7",
73
73
  "react-textarea-autosize": "^8.5.9",
@@ -959,10 +959,12 @@ attachTransformScopes(useExternalThread, (scopes, parent) => {
959
959
  if (!scopes.threads && parent.threads.source === null) {
960
960
  const threadElement = scopes.thread as ClientElement<"thread">;
961
961
  scopes.threads = SingleThreadList({ thread: threadElement });
962
+ // scopes mount in key order; re-declare thread after the threads source it resolves from
963
+ delete scopes.thread;
962
964
  scopes.thread = Derived({
963
965
  source: "threads",
964
966
  query: { type: "main" },
965
- get: (aui) => aui.threads().thread("main"),
967
+ get: (aui) => aui.threads.thread("main"),
966
968
  });
967
969
  }
968
970
 
@@ -970,14 +972,14 @@ attachTransformScopes(useExternalThread, (scopes, parent) => {
970
972
  scopes.threadListItem = Derived({
971
973
  source: "threads",
972
974
  query: { type: "main" },
973
- get: (aui) => aui.threads().item("main"),
975
+ get: (aui) => aui.threads.item("main"),
974
976
  });
975
977
  }
976
978
 
977
979
  scopes.composer ??= Derived({
978
980
  source: "thread",
979
981
  query: {},
980
- get: (aui) => aui.thread().composer(),
982
+ get: (aui) => aui.thread.composer(),
981
983
  });
982
984
 
983
985
  if (!scopes.modelContext && parent.modelContext.source === null) {
@@ -192,17 +192,17 @@ attachTransformScopes(useInMemoryThreadList, (scopes, parent) => {
192
192
  scopes.thread ??= Derived({
193
193
  source: "threads",
194
194
  query: { type: "main" },
195
- get: (aui) => aui.threads().thread("main"),
195
+ get: (aui) => aui.threads.thread("main"),
196
196
  });
197
197
  scopes.threadListItem ??= Derived({
198
198
  source: "threads",
199
199
  query: { type: "main" },
200
- get: (aui) => aui.threads().item("main"),
200
+ get: (aui) => aui.threads.item("main"),
201
201
  });
202
202
  scopes.composer ??= Derived({
203
203
  source: "thread",
204
204
  query: {},
205
- get: (aui) => aui.threads().thread("main").composer(),
205
+ get: (aui) => aui.threads.thread("main").composer(),
206
206
  });
207
207
 
208
208
  if (!scopes.modelContext && parent.modelContext.source === null) {
@@ -21,47 +21,3 @@ export {
21
21
  useThreadViewport,
22
22
  useThreadViewportStore,
23
23
  } from "./react/ThreadViewportContext";
24
-
25
- export {
26
- useAssistantRuntime,
27
- useThreadList,
28
- } from "../legacy-runtime/hooks/AssistantContext";
29
-
30
- export {
31
- useAttachmentRuntime,
32
- useAttachment,
33
- useThreadComposerAttachmentRuntime,
34
- useThreadComposerAttachment,
35
- useEditComposerAttachmentRuntime,
36
- useEditComposerAttachment,
37
- useMessageAttachment,
38
- useMessageAttachmentRuntime,
39
- } from "../legacy-runtime/hooks/AttachmentContext";
40
-
41
- export {
42
- useComposerRuntime,
43
- useComposer,
44
- } from "../legacy-runtime/hooks/ComposerContext";
45
-
46
- export {
47
- useMessageRuntime,
48
- useEditComposer,
49
- useMessage,
50
- } from "../legacy-runtime/hooks/MessageContext";
51
-
52
- export {
53
- useMessagePartRuntime,
54
- useMessagePart,
55
- } from "../legacy-runtime/hooks/MessagePartContext";
56
-
57
- export {
58
- useThreadRuntime,
59
- useThread,
60
- useThreadComposer,
61
- useThreadModelContext,
62
- } from "../legacy-runtime/hooks/ThreadContext";
63
-
64
- export {
65
- useThreadListItemRuntime,
66
- useThreadListItem,
67
- } from "../legacy-runtime/hooks/ThreadListItemContext";
@@ -121,7 +121,7 @@ describe("useAssistantTransportRuntime", () => {
121
121
  const { aui, sendCommand } = mountRuntime({ onError });
122
122
  await waitFor(() =>
123
123
  expect(
124
- (aui().thread().getState().extras as { sendCommand?: unknown })
124
+ (aui().thread.getState().extras as { sendCommand?: unknown })
125
125
  ?.sendCommand,
126
126
  ).toBeTypeOf("function"),
127
127
  );
@@ -139,9 +139,7 @@ describe("useAssistantTransportRuntime", () => {
139
139
 
140
140
  act(() => fetchMock.servers[0]!.close());
141
141
 
142
- await waitFor(() =>
143
- expect(aui().thread().getState().isRunning).toBe(false),
144
- );
142
+ await waitFor(() => expect(aui().thread.getState().isRunning).toBe(false));
145
143
  await act(async () => {});
146
144
  expect(onError).not.toHaveBeenCalled();
147
145
  expect(fetchMock.requests).toHaveLength(1);
@@ -154,7 +152,7 @@ describe("useAssistantTransportRuntime", () => {
154
152
  });
155
153
  await waitFor(() =>
156
154
  expect(
157
- (aui().thread().getState().extras as { sendCommand?: unknown })
155
+ (aui().thread.getState().extras as { sendCommand?: unknown })
158
156
  ?.sendCommand,
159
157
  ).toBeTypeOf("function"),
160
158
  );
@@ -165,7 +163,7 @@ describe("useAssistantTransportRuntime", () => {
165
163
  // Both land while the first run is active and coalesce into one follow-up.
166
164
  act(() => {
167
165
  sendCommand(createMessageCommand("b"));
168
- aui().thread().resumeRun({ parentId: null });
166
+ aui().thread.resumeRun({ parentId: null });
169
167
  });
170
168
 
171
169
  act(() => fetchMock.servers[0]!.close());
@@ -182,8 +180,6 @@ describe("useAssistantTransportRuntime", () => {
182
180
  ]);
183
181
 
184
182
  act(() => fetchMock.servers[2]!.close());
185
- await waitFor(() =>
186
- expect(aui().thread().getState().isRunning).toBe(false),
187
- );
183
+ await waitFor(() => expect(aui().thread.getState().isRunning).toBe(false));
188
184
  });
189
185
  });
@@ -96,7 +96,7 @@ export const useAssistantTransportSendCommand = () => {
96
96
  const aui = useAui();
97
97
 
98
98
  return (command: AssistantTransportCommand) => {
99
- const extras = aui.thread().getState().extras;
99
+ const extras = aui.thread.getState().extras;
100
100
  const transportExtras = asAssistantTransportExtras(extras);
101
101
  transportExtras.sendCommand(command);
102
102
  };
@@ -162,7 +162,7 @@ function InlineRenderer({
162
162
  sendMessage: (params) => {
163
163
  const text = extractSendMessageText(params);
164
164
  if (!text) return { ok: false, reason: "unrecognised params shape" };
165
- aui.thread().append({ content: [{ type: "text", text }] });
165
+ aui.thread.append({ content: [{ type: "text", text }] });
166
166
  return { ok: true };
167
167
  },
168
168
  callTool: (params) =>
@@ -14,7 +14,7 @@ const { registerMock, auiMock } = vi.hoisted(() => {
14
14
  return {
15
15
  registerMock: register,
16
16
  auiMock: {
17
- modelContext: () => ({ register }),
17
+ modelContext: { register },
18
18
  },
19
19
  };
20
20
  });
@@ -104,7 +104,7 @@ export const makeAssistantVisible = <T extends ComponentType<any>>(
104
104
 
105
105
  const { clickable, editable } = config ?? {};
106
106
  useEffect(() => {
107
- return aui.modelContext().register({
107
+ return aui.modelContext.register({
108
108
  getModelContext: () => {
109
109
  return {
110
110
  tools: {
@@ -23,7 +23,7 @@ const useActionBarExportMarkdown = ({
23
23
  });
24
24
 
25
25
  const callback = useCallback(async () => {
26
- const content = aui.message().getCopyText();
26
+ const content = aui.message.getCopyText();
27
27
  if (!content) return;
28
28
 
29
29
  if (onExport) {
@@ -12,7 +12,7 @@ const useAttachmentRemove = () => {
12
12
  const aui = useAui();
13
13
 
14
14
  const handleRemoveAttachment = useCallback(() => {
15
- aui.attachment().remove();
15
+ aui.attachment.remove();
16
16
  }, [aui]);
17
17
 
18
18
  return handleRemoveAttachment;
@@ -13,7 +13,7 @@ const useChainOfThoughtAccordionTrigger = () => {
13
13
  const collapsed = useAuiState((s) => s.chainOfThought.collapsed);
14
14
 
15
15
  const callback = useCallback(() => {
16
- aui.chainOfThought().setCollapsed(!collapsed);
16
+ aui.chainOfThought.setCollapsed(!collapsed);
17
17
  }, [aui, collapsed]);
18
18
 
19
19
  return callback;
@@ -30,9 +30,9 @@ vi.mock("@assistant-ui/store", async (importOriginal) => {
30
30
  return {
31
31
  ...actual,
32
32
  useAui: () => ({
33
- composer: () => ({
33
+ composer: {
34
34
  getState: () => ({ attachmentAccept: "*" }),
35
- }),
35
+ },
36
36
  }),
37
37
  };
38
38
  });
@@ -24,7 +24,7 @@ const useComposerAddAttachment = ({
24
24
  input.multiple = multiple;
25
25
  input.hidden = true;
26
26
 
27
- const attachmentAccept = aui.composer().getState().attachmentAccept;
27
+ const attachmentAccept = aui.composer.getState().attachmentAccept;
28
28
  if (attachmentAccept !== "*") {
29
29
  input.accept = attachmentAccept;
30
30
  }
@@ -15,9 +15,9 @@ vi.mock("@assistant-ui/store", async (importOriginal) => {
15
15
  return {
16
16
  ...actual,
17
17
  useAui: () => ({
18
- composer: () => ({
18
+ composer: {
19
19
  addAttachment,
20
- }),
20
+ },
21
21
  }),
22
22
  };
23
23
  });
@@ -70,7 +70,7 @@ export const ComposerPrimitiveAttachmentDropzone = forwardRef<
70
70
  await Promise.all(
71
71
  files.map(async (file) => {
72
72
  try {
73
- await aui.composer().addAttachment(file);
73
+ await aui.composer.addAttachment(file);
74
74
  } catch {
75
75
  // The composer runtime emits composer.attachmentAddError before rejecting;
76
76
  // the readonly and empty-thread stubs only throw.
@@ -38,16 +38,16 @@ let pluginRegistry: { getPlugins: () => (typeof plugin)[] } | null = null;
38
38
 
39
39
  vi.mock("@assistant-ui/store", () => {
40
40
  const aui = {
41
- composer: () => ({
42
- setText,
41
+ composer: {
42
+ setText: (text: string) => setText(text),
43
43
  getState: () => composerState,
44
44
  cancel: () => {},
45
- send: sendSpy,
46
- addAttachment,
47
- }),
48
- thread: () => ({
45
+ send: (options?: { steer?: boolean }) => sendSpy(options),
46
+ addAttachment: (file: File) => addAttachment(file),
47
+ },
48
+ thread: {
49
49
  getState: () => threadState,
50
- }),
50
+ },
51
51
  on: () => () => {},
52
52
  };
53
53
  type Selector<T> = (s: {
@@ -208,7 +208,7 @@ export const ComposerPrimitiveInput = forwardRef<
208
208
 
209
209
  if (!cancelOnEscape) return;
210
210
 
211
- const composer = aui.composer();
211
+ const composer = aui.composer;
212
212
  if (composer.getState().canCancel) {
213
213
  composer.cancel();
214
214
  e.preventDefault();
@@ -229,7 +229,7 @@ export const ComposerPrimitiveInput = forwardRef<
229
229
  }
230
230
 
231
231
  if (e.key === "Enter") {
232
- const threadState = aui.thread().getState();
232
+ const threadState = aui.thread.getState();
233
233
  const hasQueue = threadState.capabilities.queue;
234
234
 
235
235
  // Steer hotkey: Cmd/Ctrl+Shift+Enter (respects submitMode="none" and canSend)
@@ -238,10 +238,10 @@ export const ComposerPrimitiveInput = forwardRef<
238
238
  (e.ctrlKey || e.metaKey) &&
239
239
  hasQueue &&
240
240
  declaredSubmitMode !== "none" &&
241
- aui.composer().getState().canSend
241
+ aui.composer.getState().canSend
242
242
  ) {
243
243
  e.preventDefault();
244
- aui.composer().send({ steer: true });
244
+ aui.composer.send({ steer: true });
245
245
  return;
246
246
  }
247
247
 
@@ -267,7 +267,7 @@ export const ComposerPrimitiveInput = forwardRef<
267
267
 
268
268
  const handlePaste = async (e: ClipboardEvent<HTMLTextAreaElement>) => {
269
269
  if (!addAttachmentOnPaste) return;
270
- const threadCapabilities = aui.thread().getState().capabilities;
270
+ const threadCapabilities = aui.thread.getState().capabilities;
271
271
  const files = Array.from(e.clipboardData?.files || []);
272
272
 
273
273
  if (threadCapabilities.attachments && files.length > 0) {
@@ -275,7 +275,7 @@ export const ComposerPrimitiveInput = forwardRef<
275
275
  await Promise.all(
276
276
  files.map(async (file) => {
277
277
  try {
278
- await aui.composer().addAttachment(file);
278
+ await aui.composer.addAttachment(file);
279
279
  } catch {
280
280
  // The composer runtime emits composer.attachmentAddError before rejecting.
281
281
  }
@@ -319,7 +319,7 @@ export const ComposerPrimitiveInput = forwardRef<
319
319
 
320
320
  useOnScrollToBottom(() => {
321
321
  if (
322
- aui.composer().getState().type === "thread" &&
322
+ aui.composer.getState().type === "thread" &&
323
323
  unstable_focusOnScrollToBottom
324
324
  ) {
325
325
  focus();
@@ -328,7 +328,7 @@ export const ComposerPrimitiveInput = forwardRef<
328
328
 
329
329
  useEffect(() => {
330
330
  if (
331
- aui.composer().getState().type !== "thread" ||
331
+ aui.composer.getState().type !== "thread" ||
332
332
  !unstable_focusOnRunStart
333
333
  )
334
334
  return undefined;
@@ -338,7 +338,7 @@ export const ComposerPrimitiveInput = forwardRef<
338
338
 
339
339
  useEffect(() => {
340
340
  if (
341
- aui.composer().getState().type !== "thread" ||
341
+ aui.composer.getState().type !== "thread" ||
342
342
  !unstable_focusOnThreadSwitched
343
343
  )
344
344
  return undefined;
@@ -362,7 +362,7 @@ export const ComposerPrimitiveInput = forwardRef<
362
362
  onChange: composeEventHandlers(
363
363
  onChange,
364
364
  (e: React.ChangeEvent<HTMLTextAreaElement>) => {
365
- if (!aui.composer().getState().isEditing) return;
365
+ if (!aui.composer.getState().isEditing) return;
366
366
  const nativeIsComposing =
367
367
  (e.nativeEvent as { isComposing?: boolean }).isComposing === true;
368
368
  // recover stuck compositionRef when the browser drops compositionend
@@ -372,7 +372,7 @@ export const ComposerPrimitiveInput = forwardRef<
372
372
  const isComposing = nativeIsComposing || compositionRef.current;
373
373
  // keep controlled value in sync mid-IME so react does not reset the textarea to a stale value
374
374
  flushTapSync(() => {
375
- aui.composer().setText(e.target.value);
375
+ aui.composer.setText(e.target.value);
376
376
  });
377
377
  if (isComposing) return;
378
378
  const pos = e.target.selectionStart ?? e.target.value.length;
@@ -396,10 +396,10 @@ export const ComposerPrimitiveInput = forwardRef<
396
396
  .onCompositionEnd,
397
397
  (e: React.CompositionEvent<HTMLTextAreaElement>) => {
398
398
  compositionRef.current = false;
399
- if (!aui.composer().getState().isEditing) return;
399
+ if (!aui.composer.getState().isEditing) return;
400
400
  const target = e.target as HTMLTextAreaElement;
401
401
  flushTapSync(() => {
402
- aui.composer().setText(target.value);
402
+ aui.composer.setText(target.value);
403
403
  });
404
404
  const pos = target.selectionStart ?? target.value.length;
405
405
  if (pluginRegistry) {
@@ -93,7 +93,7 @@ export const ComposerPrimitiveQuoteDismiss = forwardRef<
93
93
  >(({ onClick, ...props }, forwardedRef) => {
94
94
  const aui = useAui();
95
95
  const handleDismiss = useCallback(() => {
96
- aui.composer().setQuote(undefined);
96
+ aui.composer.setQuote(undefined);
97
97
  }, [aui]);
98
98
 
99
99
  return (
@@ -14,7 +14,7 @@ const useComposerStopDictation = () => {
14
14
  const isDictating = useAuiState((s) => s.composer.dictation != null);
15
15
 
16
16
  const callback = useCallback(() => {
17
- aui.composer().stopDictation();
17
+ aui.composer.stopDictation();
18
18
  }, [aui]);
19
19
 
20
20
  if (!isDictating) return null;