@tanstack/ai-preact 0.14.4 → 0.16.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 +11 -15
- package/dist/esm/chat-ui/create-chat-hook.d.ts +48 -0
- package/dist/esm/chat-ui/create-chat-hook.js +57 -0
- package/dist/esm/chat-ui/create-chat-hook.js.map +1 -0
- package/dist/esm/chat-ui/create-ui-contexts.d.ts +29 -0
- package/dist/esm/chat-ui/create-ui-contexts.js +36 -0
- package/dist/esm/chat-ui/create-ui-contexts.js.map +1 -0
- package/dist/esm/chat-ui/create-ui.d.ts +154 -0
- package/dist/esm/chat-ui/create-ui.js +294 -0
- package/dist/esm/chat-ui/create-ui.js.map +1 -0
- package/dist/esm/create-chat-hook.d.ts +22 -0
- package/dist/esm/create-chat-hook.js +24 -0
- package/dist/esm/create-chat-hook.js.map +1 -0
- package/dist/esm/index.d.ts +3 -0
- package/dist/esm/index.js +3 -1
- package/dist/esm/ui.d.ts +3 -0
- package/dist/esm/ui.js +4 -0
- package/dist/esm/use-web-mcp-tools.d.ts +27 -0
- package/dist/esm/use-web-mcp-tools.js +36 -0
- package/dist/esm/use-web-mcp-tools.js.map +1 -0
- package/package.json +8 -4
- package/src/chat-ui/create-chat-hook.tsx +98 -0
- package/src/chat-ui/create-ui-contexts.tsx +57 -0
- package/src/chat-ui/create-ui.tsx +764 -0
- package/src/create-chat-hook.ts +47 -0
- package/src/index.ts +3 -0
- package/src/ui.ts +20 -0
- package/src/use-web-mcp-tools.ts +59 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { useChat as useUnboundChat } from './use-chat'
|
|
2
|
+
import type { AnyClientTool, InterruptDefinition } from '@tanstack/ai/client'
|
|
3
|
+
import type { InferredClientContext } from '@tanstack/ai-client'
|
|
4
|
+
import type { UseChatOptions } from './types'
|
|
5
|
+
|
|
6
|
+
type ChatHookOverrides<
|
|
7
|
+
TTools extends ReadonlyArray<AnyClientTool>,
|
|
8
|
+
TContext,
|
|
9
|
+
TInterrupts extends ReadonlyArray<InterruptDefinition<any, any, any, any>>,
|
|
10
|
+
> = {
|
|
11
|
+
threadId?: string
|
|
12
|
+
live?: boolean
|
|
13
|
+
forwardedProps?: Record<string, any>
|
|
14
|
+
body?: Record<string, any>
|
|
15
|
+
initialMessages?: UseChatOptions<
|
|
16
|
+
TTools,
|
|
17
|
+
TContext,
|
|
18
|
+
TInterrupts
|
|
19
|
+
>['initialMessages']
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Bind chat options once at module scope. The returned `useChat` hook
|
|
24
|
+
* creates a chat instance from those options.
|
|
25
|
+
*
|
|
26
|
+
* Pass per-call overrides for instance keys such as `threadId`,
|
|
27
|
+
* `initialMessages`, `live`, and `forwardedProps`. Do not change `tools`,
|
|
28
|
+
* `interrupts`, or `outputSchema` here. Those stay on the factory options.
|
|
29
|
+
*/
|
|
30
|
+
export function createChatHook<
|
|
31
|
+
const TTools extends ReadonlyArray<AnyClientTool> = any,
|
|
32
|
+
TContext = InferredClientContext<TTools>,
|
|
33
|
+
const TInterrupts extends ReadonlyArray<
|
|
34
|
+
InterruptDefinition<any, any, any, any>
|
|
35
|
+
> = readonly [],
|
|
36
|
+
>(options: UseChatOptions<TTools, TContext, TInterrupts>) {
|
|
37
|
+
function useChat(
|
|
38
|
+
overrides?: ChatHookOverrides<TTools, TContext, TInterrupts>,
|
|
39
|
+
) {
|
|
40
|
+
if (!overrides) {
|
|
41
|
+
return useUnboundChat(options)
|
|
42
|
+
}
|
|
43
|
+
return useUnboundChat({ ...options, ...overrides })
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
return { useChat }
|
|
47
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
export { useChat } from './use-chat'
|
|
2
|
+
export { createChatHook } from './create-chat-hook'
|
|
2
3
|
export { useByok } from './use-byok'
|
|
3
4
|
export { useMcpAppBridge } from './use-mcp-app-bridge'
|
|
4
5
|
export type { UseMcpAppBridgeOptions } from './use-mcp-app-bridge'
|
|
6
|
+
export { useWebMCPTools } from './use-web-mcp-tools'
|
|
7
|
+
export type { UseWebMCPToolsOptions } from './use-web-mcp-tools'
|
|
5
8
|
export type {
|
|
6
9
|
UseChatOptions,
|
|
7
10
|
UseChatReturn,
|
package/src/ui.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Barrel entry for the `@tanstack/ai-preact/ui` subpath.
|
|
2
|
+
export {
|
|
3
|
+
createChatUI,
|
|
4
|
+
type ChatUIComponents,
|
|
5
|
+
type ChatUIFactoryConfig,
|
|
6
|
+
type ChatUIHost,
|
|
7
|
+
type ChatUIQueueItem,
|
|
8
|
+
type InputProps,
|
|
9
|
+
type InterruptProps,
|
|
10
|
+
type LayoutProps,
|
|
11
|
+
type MessageProps,
|
|
12
|
+
type PartProps,
|
|
13
|
+
type QueueProps,
|
|
14
|
+
type ToolProps,
|
|
15
|
+
} from './chat-ui/create-ui'
|
|
16
|
+
export { createChatHook } from './chat-ui/create-chat-hook'
|
|
17
|
+
export {
|
|
18
|
+
createChatHookContexts,
|
|
19
|
+
type ChatUIContexts,
|
|
20
|
+
} from './chat-ui/create-ui-contexts'
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { useEffect } from 'preact/hooks'
|
|
2
|
+
import { registerWebMCPTools } from '@tanstack/ai-client'
|
|
3
|
+
import type {
|
|
4
|
+
AnyClientTool,
|
|
5
|
+
InferredClientContext,
|
|
6
|
+
RegisterWebMCPToolsOptions,
|
|
7
|
+
} from '@tanstack/ai-client'
|
|
8
|
+
|
|
9
|
+
/** Options for the Preact {@link useWebMCPTools} lifecycle hook. */
|
|
10
|
+
export type UseWebMCPToolsOptions<
|
|
11
|
+
TTools extends ReadonlyArray<AnyClientTool>,
|
|
12
|
+
TContext = InferredClientContext<TTools>,
|
|
13
|
+
> = Omit<RegisterWebMCPToolsOptions<TTools, TContext>, 'signal'> & {
|
|
14
|
+
/** Receives an asynchronous registration failure. */
|
|
15
|
+
onError?: (error: unknown) => void
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
type UseWebMCPToolsArguments<
|
|
19
|
+
TTools extends ReadonlyArray<AnyClientTool>,
|
|
20
|
+
TContext,
|
|
21
|
+
> =
|
|
22
|
+
RegisterWebMCPToolsOptions<TTools, TContext> extends { context: unknown }
|
|
23
|
+
? [options: UseWebMCPToolsOptions<TTools, TContext>]
|
|
24
|
+
: [options?: UseWebMCPToolsOptions<TTools, TContext>]
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Registers client tools with WebMCP for the lifetime of a Preact component.
|
|
28
|
+
*
|
|
29
|
+
* The hook replaces the registration when `tools` or `options` changes.
|
|
30
|
+
* Unsupported browsers and server rendering do not register tools.
|
|
31
|
+
*
|
|
32
|
+
* @param tools - The executable client tools to expose through WebMCP.
|
|
33
|
+
* @param options - Runtime context, per-tool options, and an error callback.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```tsx
|
|
37
|
+
* useWebMCPTools([searchProducts], {
|
|
38
|
+
* toolOptions: { searchProducts: { title: 'Search products' } },
|
|
39
|
+
* })
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
42
|
+
export function useWebMCPTools<
|
|
43
|
+
const TTools extends ReadonlyArray<AnyClientTool>,
|
|
44
|
+
TContext = InferredClientContext<TTools>,
|
|
45
|
+
>(tools: TTools, ...[options]: UseWebMCPToolsArguments<TTools, TContext>) {
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
const controller = new AbortController()
|
|
48
|
+
const { onError, ...registrationOptions } = options ?? {}
|
|
49
|
+
|
|
50
|
+
registerWebMCPTools(tools, {
|
|
51
|
+
...registrationOptions,
|
|
52
|
+
signal: controller.signal,
|
|
53
|
+
}).catch((error) => {
|
|
54
|
+
if (!controller.signal.aborted) onError?.(error)
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
return () => controller.abort()
|
|
58
|
+
}, [tools, options])
|
|
59
|
+
}
|