@tanstack/ai-octane 0.9.1 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -28
- package/package.json +7 -7
- package/src/chat-ui/create-chat-hook.tsrx +7 -12
- package/src/chat-ui/create-chat-hook.tsrx.d.ts +64 -14
- package/src/chat-ui/create-ui.tsrx +6 -3
- package/src/chat-ui/create-ui.tsrx.d.ts +204 -58
- package/src/create-chat-hook.ts +26 -11
- package/src/index.ts +6 -2
- package/src/realtime-types.ts +1 -1
- package/src/types.ts +0 -15
- package/src/use-audio-recorder.tsrx +1 -1
- package/src/use-audio-recorder.tsrx.d.ts +1 -1
- package/src/use-chat.tsrx +8 -41
- package/src/use-generate-3d-model.tsrx +143 -0
- package/src/use-generate-3d-model.tsrx.d.ts +116 -0
- package/src/use-generate-audio.tsrx +3 -2
- package/src/use-generate-audio.tsrx.d.ts +3 -2
- package/src/use-generate-image.tsrx +3 -2
- package/src/use-generate-image.tsrx.d.ts +3 -2
- package/src/use-generate-speech.tsrx +3 -2
- package/src/use-generate-speech.tsrx.d.ts +3 -2
- package/src/use-generate-video.tsrx +6 -6
- package/src/use-generate-video.tsrx.d.ts +3 -2
- package/src/use-generation.tsrx +5 -6
- package/src/use-generation.tsrx.d.ts +2 -2
- package/src/use-summarize.tsrx +3 -2
- package/src/use-summarize.tsrx.d.ts +3 -2
- package/src/use-transcription.tsrx +3 -2
- package/src/use-transcription.tsrx.d.ts +3 -2
- package/src/use-web-mcp-tools.tsrx +1 -10
- package/src/use-web-mcp-tools.tsrx.d.ts +1 -8
package/README.md
CHANGED
|
@@ -31,7 +31,9 @@ import:
|
|
|
31
31
|
```ts
|
|
32
32
|
// before
|
|
33
33
|
import { useChat } from '@tanstack/ai-react'
|
|
34
|
+
```
|
|
34
35
|
|
|
36
|
+
```ts
|
|
35
37
|
// after
|
|
36
38
|
import { useChat } from '@tanstack/ai-octane'
|
|
37
39
|
```
|
|
@@ -57,36 +59,42 @@ pnpm add @tanstack/ai-octane @tanstack/ai @tanstack/ai-client octane
|
|
|
57
59
|
```tsx
|
|
58
60
|
import { useState } from 'octane'
|
|
59
61
|
import { useChat } from '@tanstack/ai-octane'
|
|
62
|
+
import type { ChatFetcher } from '@tanstack/ai-octane'
|
|
60
63
|
|
|
61
|
-
|
|
64
|
+
declare const myFetcher: ChatFetcher
|
|
65
|
+
|
|
66
|
+
export function Chat() {
|
|
62
67
|
const [input, setInput] = useState('')
|
|
63
68
|
const chat = useChat({
|
|
64
69
|
fetcher: myFetcher,
|
|
70
|
+
threadId: 'support-42',
|
|
65
71
|
})
|
|
66
72
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
{message.
|
|
71
|
-
|
|
72
|
-
.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
73
|
+
return (
|
|
74
|
+
<div>
|
|
75
|
+
{chat.messages.map((message) => (
|
|
76
|
+
<p key={message.id}>
|
|
77
|
+
{message.role}:
|
|
78
|
+
{message.parts
|
|
79
|
+
.filter((part) => part.type === 'text')
|
|
80
|
+
.map((part) => part.content)
|
|
81
|
+
.join('')}
|
|
82
|
+
</p>
|
|
83
|
+
))}
|
|
84
|
+
<input
|
|
85
|
+
value={input}
|
|
86
|
+
onInput={(event) => setInput(event.currentTarget.value)}
|
|
87
|
+
/>
|
|
88
|
+
<button
|
|
89
|
+
onClick={() => {
|
|
90
|
+
void chat.sendMessage(input)
|
|
91
|
+
setInput('')
|
|
92
|
+
}}
|
|
93
|
+
>
|
|
94
|
+
Send
|
|
95
|
+
</button>
|
|
96
|
+
</div>
|
|
97
|
+
)
|
|
90
98
|
}
|
|
91
99
|
```
|
|
92
100
|
|
|
@@ -98,10 +106,10 @@ synthetic `onChange`.
|
|
|
98
106
|
## API
|
|
99
107
|
|
|
100
108
|
The adapter includes `useChat`, `useRealtimeChat`, `useMcpAppBridge`,
|
|
101
|
-
`useGeneration`, `useGenerateImage`, `
|
|
102
|
-
`
|
|
103
|
-
`useAudioRecorder`. It also re-exports all 30
|
|
104
|
-
convenience helpers and types (`fetchServerSentEvents`, `fetchHttpStream`,
|
|
109
|
+
`useGeneration`, `useGenerateImage`, `useGenerate3DModel` (experimental),
|
|
110
|
+
`useGenerateAudio`, `useGenerateSpeech`, `useGenerateVideo`, `useTranscription`,
|
|
111
|
+
`useSummarize`, and `useAudioRecorder`. It also re-exports all 30
|
|
112
|
+
`@tanstack/ai-client` convenience helpers and types (`fetchServerSentEvents`, `fetchHttpStream`,
|
|
105
113
|
`xhrServerSentEvents`, `xhrHttpStream`, `stream`, `rpcStream`,
|
|
106
114
|
`createChatClientOptions`, `createMcpAppBridge`, and their associated types)
|
|
107
115
|
unchanged, mirroring the `@tanstack/ai-react` index.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/ai-octane",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Octane bindings for TanStack AI streaming chat, structured outputs, and media generation.",
|
|
5
5
|
"author": "Dominic Gannaway",
|
|
6
6
|
"license": "MIT",
|
|
@@ -48,23 +48,23 @@
|
|
|
48
48
|
],
|
|
49
49
|
"//lint": "The .tsrx.d.ts companions are generated declaration emits of the .tsrx implementations, so they are not hand-formatted and are excluded from lint rather than edited in place (a regeneration would undo any fix).",
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@tanstack/ai-client": "
|
|
51
|
+
"@tanstack/ai-client": "^1.2.0"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
|
54
54
|
"octane": ">=0.12.0",
|
|
55
|
-
"@tanstack/ai": "^
|
|
55
|
+
"@tanstack/ai": "^1.7.0"
|
|
56
56
|
},
|
|
57
57
|
"devDependencies": {
|
|
58
58
|
"@octanejs/testing-library": "0.1.62",
|
|
59
59
|
"@standard-schema/spec": "^1.1.0",
|
|
60
60
|
"@tsrx/oxc": "0.20.0",
|
|
61
61
|
"@types/node": "^24.10.1",
|
|
62
|
-
"@vitest/coverage-v8": "4.1.
|
|
63
|
-
"happy-dom": "^20.
|
|
62
|
+
"@vitest/coverage-v8": "4.1.12",
|
|
63
|
+
"happy-dom": "^20.14.6",
|
|
64
64
|
"octane": "0.12.1",
|
|
65
|
-
"vite": "^8.
|
|
65
|
+
"vite": "^8.3.8",
|
|
66
66
|
"zod": "^4.2.0",
|
|
67
|
-
"@tanstack/ai": "
|
|
67
|
+
"@tanstack/ai": "1.7.0"
|
|
68
68
|
},
|
|
69
69
|
"scripts": {
|
|
70
70
|
"lint:fix": "oxlint src --type-aware --fix",
|
|
@@ -21,10 +21,9 @@ type HeadlessOptions<TOptions> = UseChatOptions<
|
|
|
21
21
|
>
|
|
22
22
|
|
|
23
23
|
type ChatInstanceOverrides<TOptions> = {
|
|
24
|
-
threadId
|
|
24
|
+
threadId: string
|
|
25
25
|
live?: boolean
|
|
26
26
|
forwardedProps?: Record<string, any>
|
|
27
|
-
body?: Record<string, any>
|
|
28
27
|
initialMessages?: HeadlessOptions<TOptions>['initialMessages']
|
|
29
28
|
}
|
|
30
29
|
|
|
@@ -36,7 +35,7 @@ type ChatInstanceOverrides<TOptions> = {
|
|
|
36
35
|
* `useChatContext` reads that instance. Part and interrupt widgets take
|
|
37
36
|
* `part` / `interrupt` as props. Do not read those from context.
|
|
38
37
|
*
|
|
39
|
-
* Pass
|
|
38
|
+
* Pass `threadId` and other instance overrides into `useAppChat()`.
|
|
40
39
|
*
|
|
41
40
|
* @example
|
|
42
41
|
* ```tsx
|
|
@@ -70,15 +69,11 @@ export function createChatHook<
|
|
|
70
69
|
chatComponents as ChatUIFactoryConfig<NoInfer<TOptions>, TInput>,
|
|
71
70
|
)
|
|
72
71
|
|
|
73
|
-
function useAppChat(overrides
|
|
74
|
-
const chat = (
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
...overrides,
|
|
79
|
-
})
|
|
80
|
-
: useUnboundChat(options as HeadlessOptions<TOptions>)
|
|
81
|
-
) as ChatUIHost<TOptions>
|
|
72
|
+
function useAppChat(overrides: ChatInstanceOverrides<TOptions>) {
|
|
73
|
+
const chat = useUnboundChat({
|
|
74
|
+
...(options as HeadlessOptions<TOptions>),
|
|
75
|
+
...overrides,
|
|
76
|
+
}) as ChatUIHost<TOptions>
|
|
82
77
|
|
|
83
78
|
// `useChat` returns a fresh object on every render. The mixed Chat
|
|
84
79
|
// component must stay stable, or React remounts the tree on each
|
|
@@ -1,20 +1,70 @@
|
|
|
1
1
|
import type { ComponentBody } from 'octane'
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
type ComponentType<P = any> = ComponentBody<P>
|
|
3
|
+
import type { InferredClientContext } from '@tanstack/ai-client'
|
|
4
|
+
import type {
|
|
5
|
+
ChatUIInterruptsOf,
|
|
6
|
+
ChatUISchemaOf,
|
|
7
|
+
ChatUIToolsOf,
|
|
8
|
+
} from '@tanstack/ai-client/ui'
|
|
9
|
+
import type { UseChatOptions } from '../types'
|
|
10
|
+
import type {
|
|
11
|
+
ChatUIFactoryConfig,
|
|
12
|
+
ChatUIHost,
|
|
13
|
+
InputProps,
|
|
14
|
+
} from './create-ui.tsrx'
|
|
15
|
+
type HeadlessOptions<TOptions> = UseChatOptions<
|
|
16
|
+
ChatUIToolsOf<TOptions>,
|
|
17
|
+
ChatUISchemaOf<TOptions>,
|
|
18
|
+
InferredClientContext<ChatUIToolsOf<TOptions>>,
|
|
19
|
+
ChatUIInterruptsOf<TOptions>
|
|
20
|
+
>
|
|
21
|
+
type ChatInstanceOverrides<TOptions> = {
|
|
22
|
+
threadId: string
|
|
23
|
+
live?: boolean
|
|
24
|
+
forwardedProps?: Record<string, any>
|
|
25
|
+
initialMessages?: HeadlessOptions<TOptions>['initialMessages']
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Bind chat options and UI widgets once at module scope.
|
|
29
|
+
*
|
|
30
|
+
* This matches Form `createFormHook` and Table `createTableHook`. The
|
|
31
|
+
* returned `useAppChat` creates a chat instance. `chat.AppChat` renders it.
|
|
32
|
+
* `useChatContext` reads that instance. Part and interrupt widgets take
|
|
33
|
+
* `part` / `interrupt` as props. Do not read those from context.
|
|
34
|
+
*
|
|
35
|
+
* Pass `threadId` and other instance overrides into `useAppChat()`.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```tsx
|
|
39
|
+
* const { useAppChat, useChatContext } = createChatHook({
|
|
40
|
+
* options: chatOptions,
|
|
41
|
+
* components: { layout: ChatLayout, message: ChatMessage },
|
|
42
|
+
* partsComponents: { fallback: FallbackPart },
|
|
43
|
+
* toolsComponents: { getWeather: WeatherTool, purchaseItem: PurchaseTool },
|
|
44
|
+
* interruptsComponents: { generic: { choosePlan: ChoosePlan } },
|
|
45
|
+
* })
|
|
46
|
+
*
|
|
47
|
+
* function Support() {
|
|
48
|
+
* const chat = useAppChat({ threadId: 'support-1' })
|
|
49
|
+
* return <chat.AppChat />
|
|
50
|
+
* }
|
|
51
|
+
* ```
|
|
52
|
+
*/
|
|
4
53
|
export declare function createChatHook<
|
|
5
54
|
const TOptions,
|
|
6
|
-
TInput extends
|
|
7
|
-
|
|
|
55
|
+
TInput extends ComponentType<any> | undefined =
|
|
56
|
+
| ComponentType<InputProps<NoInfer<TOptions>>>
|
|
8
57
|
| undefined,
|
|
9
|
-
>(
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
58
|
+
>({
|
|
59
|
+
options,
|
|
60
|
+
...chatComponents
|
|
61
|
+
}: {
|
|
62
|
+
options: TOptions
|
|
63
|
+
} & ChatUIFactoryConfig<NoInfer<TOptions>, TInput>): {
|
|
64
|
+
useAppChat: (
|
|
65
|
+
overrides: ChatInstanceOverrides<TOptions>,
|
|
66
|
+
) => ChatUIHost<TOptions> & {
|
|
67
|
+
AppChat: ComponentType
|
|
68
|
+
}
|
|
19
69
|
useChatContext: () => ChatUIHost<TOptions>
|
|
20
70
|
}
|
|
@@ -181,9 +181,11 @@ export type ChatUIFactoryConfig<
|
|
|
181
181
|
context?: ChatUIContextConfig
|
|
182
182
|
}
|
|
183
183
|
|
|
184
|
-
|
|
184
|
+
/** A zero-prop widget bound to the surrounding part or interrupt. */
|
|
185
|
+
export type BoundWidget = ComponentType<Record<string, never>>
|
|
185
186
|
|
|
186
|
-
|
|
187
|
+
/** The widgets `UI.Part` mixes onto the selected part it passes to children. */
|
|
188
|
+
export type PartMixins<TOptions> = {
|
|
187
189
|
[K in ChatUIPartKey]?: BoundWidget
|
|
188
190
|
} & {
|
|
189
191
|
[K in ChatUIToolName<TOptions>]: BoundWidget
|
|
@@ -191,7 +193,8 @@ type PartMixins<TOptions> = {
|
|
|
191
193
|
Render: BoundWidget
|
|
192
194
|
}
|
|
193
195
|
|
|
194
|
-
|
|
196
|
+
/** The widgets `UI.Interrupt` mixes onto the interrupt it passes to children. */
|
|
197
|
+
export type InterruptMixins<TOptions> = {
|
|
195
198
|
[K in ChatUINamedInterruptId<TOptions>]: BoundWidget
|
|
196
199
|
} & {
|
|
197
200
|
[K in ChatUIToolName<TOptions>]?: BoundWidget
|
|
@@ -1,43 +1,62 @@
|
|
|
1
|
-
import type { ComponentBody,
|
|
2
|
-
|
|
3
|
-
import type { ChatUIContexts } from './create-ui-contexts.tsrx'
|
|
1
|
+
import type { ComponentBody, OctaneNode } from 'octane'
|
|
2
|
+
type ComponentType<P = any> = ComponentBody<P>
|
|
4
3
|
import type {
|
|
4
|
+
ChatUIData,
|
|
5
|
+
ChatUIHasNamedInterrupts,
|
|
6
|
+
ChatUIHasNamedTools,
|
|
7
|
+
ChatUIInterrupt,
|
|
5
8
|
ChatUIInterruptName,
|
|
9
|
+
ChatUIInterruptOf,
|
|
6
10
|
ChatUIInterruptsOf,
|
|
11
|
+
ChatUIMessages,
|
|
12
|
+
ChatUINamedInterruptId,
|
|
7
13
|
ChatUIPartKey,
|
|
8
14
|
ChatUIPartOf,
|
|
9
15
|
ChatUISchemaOf,
|
|
16
|
+
ChatUISelectedPart,
|
|
17
|
+
ChatUIToolApproval,
|
|
10
18
|
ChatUIToolName,
|
|
11
19
|
ChatUIToolsOf,
|
|
12
20
|
} from '@tanstack/ai-client/ui'
|
|
13
|
-
import type {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
21
|
+
import type {
|
|
22
|
+
MessagePart,
|
|
23
|
+
QueuedMessage,
|
|
24
|
+
ToolCallPart,
|
|
25
|
+
ToolResultPart,
|
|
26
|
+
UIMessage,
|
|
27
|
+
} from '@tanstack/ai-client'
|
|
28
|
+
import type { UseChatReturn } from '../types'
|
|
29
|
+
import type { ChatUIContexts } from './create-ui-contexts.tsrx'
|
|
17
30
|
export type ChatUIHost<TOptions = unknown> = UseChatReturn<
|
|
18
31
|
ChatUIToolsOf<TOptions>,
|
|
19
32
|
ChatUISchemaOf<TOptions>,
|
|
20
33
|
ChatUIInterruptsOf<TOptions>
|
|
21
34
|
>
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
35
|
+
export type ChatUIQueueItem = QueuedMessage & {
|
|
36
|
+
cancelQueued: () => void
|
|
37
|
+
}
|
|
25
38
|
export type LayoutProps<
|
|
26
39
|
TOptions,
|
|
27
|
-
TInput extends ComponentType<any> | undefined = ComponentType<
|
|
40
|
+
TInput extends ComponentType<any> | undefined = ComponentType<
|
|
41
|
+
InputProps<TOptions>
|
|
42
|
+
>,
|
|
28
43
|
> = {
|
|
29
44
|
Messages: ComponentType
|
|
30
45
|
Interrupts: ComponentType
|
|
31
46
|
Queue: ComponentType
|
|
32
47
|
readonly __ui?: TOptions
|
|
33
|
-
} & (TInput extends ComponentType<any>
|
|
34
|
-
|
|
48
|
+
} & (TInput extends ComponentType<any>
|
|
49
|
+
? {
|
|
50
|
+
Input: ComponentType
|
|
51
|
+
}
|
|
52
|
+
: {})
|
|
35
53
|
export type MessageProps<TOptions> = {
|
|
36
|
-
message:
|
|
54
|
+
message: UIMessage<ChatUIToolsOf<TOptions>, ChatUIData<TOptions>>
|
|
37
55
|
Parts: ComponentType
|
|
38
56
|
}
|
|
39
|
-
|
|
40
|
-
|
|
57
|
+
export type InputProps<TOptions> = {
|
|
58
|
+
readonly __ui?: TOptions
|
|
59
|
+
}
|
|
41
60
|
export type QueueProps<TOptions> = {
|
|
42
61
|
item: ChatUIQueueItem
|
|
43
62
|
readonly __ui?: TOptions
|
|
@@ -49,68 +68,195 @@ export type ToolProps<
|
|
|
49
68
|
TOptions,
|
|
50
69
|
TName extends ChatUIToolName<TOptions> = ChatUIToolName<TOptions>,
|
|
51
70
|
> = {
|
|
52
|
-
part:
|
|
53
|
-
|
|
54
|
-
|
|
71
|
+
part: Extract<
|
|
72
|
+
ToolCallPart<ChatUIToolsOf<TOptions>>,
|
|
73
|
+
{
|
|
74
|
+
name: TName
|
|
75
|
+
}
|
|
76
|
+
>
|
|
77
|
+
result?: ToolResultPart
|
|
78
|
+
interrupt?: ChatUIToolApproval<TOptions, TName>
|
|
55
79
|
}
|
|
56
80
|
export type InterruptProps<
|
|
57
81
|
TOptions,
|
|
58
82
|
TName extends ChatUIInterruptName<TOptions> = never,
|
|
59
83
|
> = {
|
|
60
|
-
interrupt:
|
|
84
|
+
interrupt: ChatUIInterruptOf<TOptions, TName>
|
|
61
85
|
readonly __ui?: TOptions
|
|
62
86
|
}
|
|
63
|
-
|
|
64
|
-
|
|
87
|
+
type GenericInterruptComponents<TOptions> =
|
|
88
|
+
ChatUIHasNamedInterrupts<TOptions> extends true
|
|
89
|
+
? {
|
|
90
|
+
[K in ChatUINamedInterruptId<TOptions>]: ComponentType<
|
|
91
|
+
InterruptProps<TOptions, K & ChatUIInterruptName<TOptions>>
|
|
92
|
+
>
|
|
93
|
+
} & {
|
|
94
|
+
fallback?: ComponentType<InterruptProps<TOptions>>
|
|
95
|
+
}
|
|
96
|
+
: {
|
|
97
|
+
fallback?: ComponentType<InterruptProps<TOptions>>
|
|
98
|
+
}
|
|
99
|
+
type ToolApprovalMap<TOptions> = {
|
|
100
|
+
[K in ChatUIToolName<TOptions>]?: ComponentType<
|
|
101
|
+
InterruptProps<TOptions, K & ChatUIInterruptName<TOptions>>
|
|
102
|
+
>
|
|
103
|
+
}
|
|
104
|
+
/** The chrome around the message list: `layout`, `message`, and `input`. */
|
|
105
|
+
export type ChatUIChromeComponents<
|
|
65
106
|
TOptions,
|
|
66
|
-
TInput extends ComponentType<
|
|
107
|
+
TInput extends ComponentType<InputProps<TOptions>> | undefined =
|
|
108
|
+
ComponentType<InputProps<TOptions>>,
|
|
67
109
|
> = {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
chatContext?: ChatUIContexts['chatContext']
|
|
82
|
-
partContext?: ChatUIContexts['partContext']
|
|
83
|
-
interruptContext?: ChatUIContexts['interruptContext']
|
|
84
|
-
}
|
|
110
|
+
layout: ComponentType<LayoutProps<TOptions, TInput>>
|
|
111
|
+
message: ComponentType<MessageProps<TOptions>>
|
|
112
|
+
input?: TInput
|
|
113
|
+
queue?: ComponentType<QueueProps<TOptions>>
|
|
114
|
+
}
|
|
115
|
+
export type ChatUIPartsComponents<TOptions> = {
|
|
116
|
+
[K in ChatUIPartKey]?: ComponentType<PartProps<TOptions, K>>
|
|
117
|
+
} & {
|
|
118
|
+
fallback?: ComponentType<PartProps<TOptions>>
|
|
119
|
+
}
|
|
120
|
+
export type ChatUIInterruptsComponents<TOptions> = {
|
|
121
|
+
tools?: ToolApprovalMap<TOptions>
|
|
122
|
+
generic: GenericInterruptComponents<TOptions>
|
|
85
123
|
}
|
|
86
|
-
|
|
87
124
|
export type ChatUIComponents<
|
|
88
125
|
TOptions,
|
|
89
|
-
TInput extends ComponentType<
|
|
90
|
-
|
|
91
|
-
|
|
126
|
+
TInput extends ComponentType<InputProps<TOptions>> | undefined =
|
|
127
|
+
ComponentType<InputProps<TOptions>>,
|
|
128
|
+
> = {
|
|
129
|
+
components: ChatUIChromeComponents<TOptions, TInput>
|
|
130
|
+
partsComponents: ChatUIPartsComponents<TOptions>
|
|
131
|
+
} & (ChatUIHasNamedTools<TOptions> extends true
|
|
132
|
+
? {
|
|
133
|
+
toolsComponents: {
|
|
134
|
+
[K in ChatUIToolName<TOptions>]: ComponentType<ToolProps<TOptions, K>>
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
: {
|
|
138
|
+
toolsComponents?: {
|
|
139
|
+
[K in ChatUIToolName<TOptions>]?: ComponentType<ToolProps<TOptions, K>>
|
|
140
|
+
}
|
|
141
|
+
}) &
|
|
142
|
+
(ChatUIHasNamedInterrupts<TOptions> extends true
|
|
143
|
+
? {
|
|
144
|
+
interruptsComponents: ChatUIInterruptsComponents<TOptions>
|
|
145
|
+
}
|
|
146
|
+
: {
|
|
147
|
+
interruptsComponents?: {
|
|
148
|
+
tools?: ToolApprovalMap<TOptions>
|
|
149
|
+
generic?: GenericInterruptComponents<TOptions>
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
/** Scoped contexts, for widgets in other files or nested chat trees. */
|
|
153
|
+
export type ChatUIContextConfig = {
|
|
154
|
+
chatContext?: ChatUIContexts['chatContext']
|
|
155
|
+
partContext?: ChatUIContexts['partContext']
|
|
156
|
+
interruptContext?: ChatUIContexts['interruptContext']
|
|
157
|
+
}
|
|
158
|
+
export type ChatUIFactoryConfig<
|
|
159
|
+
TOptions,
|
|
160
|
+
TInput extends ComponentType<InputProps<TOptions>> | undefined =
|
|
161
|
+
ComponentType<InputProps<TOptions>>,
|
|
162
|
+
> = ChatUIComponents<TOptions, TInput> & {
|
|
163
|
+
context?: ChatUIContextConfig
|
|
164
|
+
}
|
|
165
|
+
/** A zero-prop widget bound to the surrounding part or interrupt. */
|
|
166
|
+
export type BoundWidget = ComponentType<Record<string, never>>
|
|
167
|
+
/** The widgets `UI.Part` mixes onto the selected part it passes to children. */
|
|
168
|
+
export type PartMixins<TOptions> = {
|
|
169
|
+
[K in ChatUIPartKey]?: BoundWidget
|
|
170
|
+
} & {
|
|
171
|
+
[K in ChatUIToolName<TOptions>]: BoundWidget
|
|
172
|
+
} & {
|
|
173
|
+
Render: BoundWidget
|
|
174
|
+
}
|
|
175
|
+
/** The widgets `UI.Interrupt` mixes onto the interrupt it passes to children. */
|
|
176
|
+
export type InterruptMixins<TOptions> = {
|
|
177
|
+
[K in ChatUINamedInterruptId<TOptions>]: BoundWidget
|
|
178
|
+
} & {
|
|
179
|
+
[K in ChatUIToolName<TOptions>]?: BoundWidget
|
|
180
|
+
} & {
|
|
181
|
+
fallback?: BoundWidget
|
|
182
|
+
Render: BoundWidget
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Bind chat options and UI widgets once at module scope. This matches Form
|
|
186
|
+
* `createFormHook` and Table `createTableHook`: widgets register here, mix
|
|
187
|
+
* onto Part / Interrupt / the UI kit, and automatic dispatch still walks the
|
|
188
|
+
* message list.
|
|
189
|
+
*
|
|
190
|
+
* `chatOptions` is type-only at runtime. Pass optional `chatContext`,
|
|
191
|
+
* `partContext`, and `interruptContext` from {@link createChatHookContexts}
|
|
192
|
+
* when widgets live in other files or when nested chats need isolation.
|
|
193
|
+
*/
|
|
92
194
|
export declare function createChatUI<
|
|
93
195
|
const TOptions,
|
|
94
196
|
TInput extends ComponentType<any> | undefined =
|
|
95
|
-
| ComponentType<
|
|
197
|
+
| ComponentType<InputProps<NoInfer<TOptions>>>
|
|
96
198
|
| undefined,
|
|
97
199
|
>(
|
|
98
200
|
options: TOptions,
|
|
99
|
-
config: ChatUIFactoryConfig<TOptions
|
|
201
|
+
config: ChatUIFactoryConfig<NoInfer<TOptions>, TInput>,
|
|
100
202
|
): {
|
|
101
|
-
Chat:
|
|
102
|
-
|
|
203
|
+
Chat: ({
|
|
204
|
+
chat,
|
|
205
|
+
}: {
|
|
206
|
+
chat: ChatUIHost<TOptions>
|
|
207
|
+
}) => import('octane/jsx-runtime').JSX.Element
|
|
208
|
+
Provider: ({
|
|
209
|
+
chat,
|
|
210
|
+
children,
|
|
211
|
+
}: {
|
|
103
212
|
chat: ChatUIHost<TOptions>
|
|
104
213
|
children?: OctaneNode
|
|
105
|
-
}
|
|
106
|
-
Messages:
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
214
|
+
}) => import('octane/jsx-runtime').JSX.Element
|
|
215
|
+
Messages: ({
|
|
216
|
+
children,
|
|
217
|
+
}?: {
|
|
218
|
+
children?: (messages: ChatUIMessages<TOptions>) => OctaneNode
|
|
219
|
+
}) => import('octane/jsx-runtime').JSX.Element
|
|
220
|
+
Message: ({
|
|
221
|
+
message,
|
|
222
|
+
children,
|
|
223
|
+
}: {
|
|
224
|
+
message: ChatUIMessages<TOptions>[number]
|
|
225
|
+
children?: (parts: Array<ChatUISelectedPart>) => OctaneNode
|
|
226
|
+
}) => import('octane/jsx-runtime').JSX.Element
|
|
227
|
+
Part: ({
|
|
228
|
+
part,
|
|
229
|
+
children,
|
|
230
|
+
}: {
|
|
231
|
+
part: MessagePart | ChatUISelectedPart
|
|
232
|
+
children?: (mixed: ChatUISelectedPart & PartMixins<TOptions>) => OctaneNode
|
|
233
|
+
}) => import('octane/jsx-runtime').JSX.Element | null
|
|
234
|
+
Interrupts: ({
|
|
235
|
+
children,
|
|
236
|
+
}?: {
|
|
237
|
+
children?: (interrupts: ReadonlyArray<ChatUIInterrupt>) => OctaneNode
|
|
238
|
+
}) => import('octane/jsx-runtime').JSX.Element
|
|
239
|
+
Interrupt: (({
|
|
240
|
+
interrupt,
|
|
241
|
+
children,
|
|
242
|
+
}: {
|
|
243
|
+
interrupt: ChatUIInterrupt
|
|
244
|
+
children?: (
|
|
245
|
+
mixed: ChatUIInterrupt & InterruptMixins<TOptions>,
|
|
246
|
+
) => OctaneNode
|
|
247
|
+
}) => import('octane/jsx-runtime').JSX.Element) & {
|
|
248
|
+
readonly type: ({
|
|
249
|
+
interrupt,
|
|
250
|
+
children,
|
|
251
|
+
}: {
|
|
252
|
+
interrupt: ChatUIInterrupt
|
|
253
|
+
children?: (
|
|
254
|
+
mixed: ChatUIInterrupt & InterruptMixins<TOptions>,
|
|
255
|
+
) => OctaneNode
|
|
256
|
+
}) => import('octane/jsx-runtime').JSX.Element
|
|
257
|
+
displayName?: string
|
|
258
|
+
}
|
|
259
|
+
Queue: () => import('octane/jsx-runtime').JSX.Element | null
|
|
112
260
|
useChatContext: () => ChatUIHost<TOptions>
|
|
113
|
-
Input: TInput
|
|
261
|
+
Input: TInput | undefined
|
|
114
262
|
}
|
|
115
|
-
|
|
116
|
-
export type { Context }
|
package/src/create-chat-hook.ts
CHANGED
|
@@ -4,7 +4,10 @@ import type {
|
|
|
4
4
|
InterruptDefinition,
|
|
5
5
|
SchemaInput,
|
|
6
6
|
} from '@tanstack/ai/client'
|
|
7
|
-
import type {
|
|
7
|
+
import type {
|
|
8
|
+
DistributedOmit,
|
|
9
|
+
InferredClientContext,
|
|
10
|
+
} from '@tanstack/ai-client'
|
|
8
11
|
import type { UseChatOptions } from './types'
|
|
9
12
|
|
|
10
13
|
type ChatHookOverrides<
|
|
@@ -13,10 +16,9 @@ type ChatHookOverrides<
|
|
|
13
16
|
TContext,
|
|
14
17
|
TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>>,
|
|
15
18
|
> = {
|
|
16
|
-
threadId
|
|
19
|
+
threadId: string
|
|
17
20
|
live?: boolean
|
|
18
21
|
forwardedProps?: Record<string, any>
|
|
19
|
-
body?: Record<string, any>
|
|
20
22
|
initialMessages?: UseChatOptions<
|
|
21
23
|
TTools,
|
|
22
24
|
TSchema,
|
|
@@ -29,8 +31,8 @@ type ChatHookOverrides<
|
|
|
29
31
|
* Bind chat options once at module scope. The returned `useChat` hook
|
|
30
32
|
* creates a chat instance from those options.
|
|
31
33
|
*
|
|
32
|
-
* Pass
|
|
33
|
-
* `
|
|
34
|
+
* Pass `threadId` on every call. You can also override `initialMessages`,
|
|
35
|
+
* `live`, and `forwardedProps` per call. Do not change `tools`,
|
|
34
36
|
* `interrupts`, or `outputSchema` here. Those stay on the factory options.
|
|
35
37
|
*/
|
|
36
38
|
export function createChatHook<
|
|
@@ -40,14 +42,27 @@ export function createChatHook<
|
|
|
40
42
|
const TInterrupts extends ReadonlyArray<
|
|
41
43
|
InterruptDefinition<any, any, any, any>
|
|
42
44
|
> = readonly [],
|
|
43
|
-
>(
|
|
45
|
+
>(
|
|
46
|
+
options: DistributedOmit<
|
|
47
|
+
UseChatOptions<TTools, TSchema, TContext, TInterrupts>,
|
|
48
|
+
'threadId'
|
|
49
|
+
> & {
|
|
50
|
+
// Plain keys so TypeScript can infer the generics through the Omit.
|
|
51
|
+
tools?: TTools
|
|
52
|
+
outputSchema?: TSchema
|
|
53
|
+
interrupts?: TInterrupts
|
|
54
|
+
},
|
|
55
|
+
) {
|
|
44
56
|
function useChat(
|
|
45
|
-
overrides
|
|
57
|
+
overrides: ChatHookOverrides<TTools, TSchema, TContext, TInterrupts>,
|
|
46
58
|
) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
59
|
+
// `overrides` carries the required `threadId`.
|
|
60
|
+
return useUnboundChat({ ...options, ...overrides } as UseChatOptions<
|
|
61
|
+
TTools,
|
|
62
|
+
TSchema,
|
|
63
|
+
TContext,
|
|
64
|
+
TInterrupts
|
|
65
|
+
>)
|
|
51
66
|
}
|
|
52
67
|
|
|
53
68
|
return { useChat }
|