@assistant-ui/react 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,70 @@
1
+ import { z } from 'zod';
2
+ import { ReactNode } from 'react';
3
+
4
+ type TextContentPart = {
5
+ type: "text";
6
+ text: string;
7
+ };
8
+ type ImageContentPart = {
9
+ type: "image";
10
+ image: string;
11
+ };
12
+ type UIContentPart = {
13
+ type: "ui";
14
+ display: ReactNode;
15
+ };
16
+ type ToolCallContentPart<TArgs = unknown, TResult = unknown> = {
17
+ type: "tool-call";
18
+ toolCallId: string;
19
+ toolName: string;
20
+ args: TArgs;
21
+ result?: TResult;
22
+ };
23
+ type UserContentPart = TextContentPart | ImageContentPart | UIContentPart;
24
+ type AssistantContentPart = TextContentPart | ToolCallContentPart | UIContentPart;
25
+ type AppendContentPart = TextContentPart | ImageContentPart;
26
+ type BaseMessage = {
27
+ id: string;
28
+ createdAt: Date;
29
+ };
30
+ type UserMessage = BaseMessage & {
31
+ role: "user";
32
+ content: UserContentPart[];
33
+ };
34
+ type AssistantMessage = BaseMessage & {
35
+ role: "assistant";
36
+ content: AssistantContentPart[];
37
+ status: "in_progress" | "done" | "error";
38
+ };
39
+ type AppendMessage = {
40
+ parentId: string | null;
41
+ content: AppendContentPart[];
42
+ };
43
+ type ThreadMessage = UserMessage | AssistantMessage;
44
+
45
+ type ToolExecuteFunction<TArgs, TResult> = (args: TArgs) => TResult | Promise<TResult>;
46
+ type Tool<TArgs = unknown, TResult = unknown> = {
47
+ description?: string;
48
+ parameters: z.ZodSchema<TArgs>;
49
+ execute: ToolExecuteFunction<TArgs, TResult>;
50
+ };
51
+ type ModelConfig = {
52
+ priority?: number;
53
+ system?: string;
54
+ tools?: Record<string, Tool<any, any>>;
55
+ };
56
+ type ModelConfigProvider = () => ModelConfig;
57
+
58
+ type Unsubscribe = () => void;
59
+
60
+ type ThreadState = {
61
+ messages: readonly ThreadMessage[];
62
+ isRunning: boolean;
63
+ getBranches: (messageId: string) => readonly string[];
64
+ switchToBranch: (branchId: string) => void;
65
+ append: (message: AppendMessage) => void;
66
+ startRun: (parentId: string | null) => void;
67
+ cancelRun: () => void;
68
+ };
69
+
70
+ export type { AssistantContentPart as A, ImageContentPart as I, ModelConfigProvider as M, TextContentPart as T, UIContentPart as U, ToolCallContentPart as a, ThreadState as b, Unsubscribe as c, ThreadMessage as d, ModelConfig as e, AppendMessage as f, AssistantMessage as g, UserMessage as h, UserContentPart as i, AppendContentPart as j, Tool as k };
@@ -0,0 +1,70 @@
1
+ import { z } from 'zod';
2
+ import { ReactNode } from 'react';
3
+
4
+ type TextContentPart = {
5
+ type: "text";
6
+ text: string;
7
+ };
8
+ type ImageContentPart = {
9
+ type: "image";
10
+ image: string;
11
+ };
12
+ type UIContentPart = {
13
+ type: "ui";
14
+ display: ReactNode;
15
+ };
16
+ type ToolCallContentPart<TArgs = unknown, TResult = unknown> = {
17
+ type: "tool-call";
18
+ toolCallId: string;
19
+ toolName: string;
20
+ args: TArgs;
21
+ result?: TResult;
22
+ };
23
+ type UserContentPart = TextContentPart | ImageContentPart | UIContentPart;
24
+ type AssistantContentPart = TextContentPart | ToolCallContentPart | UIContentPart;
25
+ type AppendContentPart = TextContentPart | ImageContentPart;
26
+ type BaseMessage = {
27
+ id: string;
28
+ createdAt: Date;
29
+ };
30
+ type UserMessage = BaseMessage & {
31
+ role: "user";
32
+ content: UserContentPart[];
33
+ };
34
+ type AssistantMessage = BaseMessage & {
35
+ role: "assistant";
36
+ content: AssistantContentPart[];
37
+ status: "in_progress" | "done" | "error";
38
+ };
39
+ type AppendMessage = {
40
+ parentId: string | null;
41
+ content: AppendContentPart[];
42
+ };
43
+ type ThreadMessage = UserMessage | AssistantMessage;
44
+
45
+ type ToolExecuteFunction<TArgs, TResult> = (args: TArgs) => TResult | Promise<TResult>;
46
+ type Tool<TArgs = unknown, TResult = unknown> = {
47
+ description?: string;
48
+ parameters: z.ZodSchema<TArgs>;
49
+ execute: ToolExecuteFunction<TArgs, TResult>;
50
+ };
51
+ type ModelConfig = {
52
+ priority?: number;
53
+ system?: string;
54
+ tools?: Record<string, Tool<any, any>>;
55
+ };
56
+ type ModelConfigProvider = () => ModelConfig;
57
+
58
+ type Unsubscribe = () => void;
59
+
60
+ type ThreadState = {
61
+ messages: readonly ThreadMessage[];
62
+ isRunning: boolean;
63
+ getBranches: (messageId: string) => readonly string[];
64
+ switchToBranch: (branchId: string) => void;
65
+ append: (message: AppendMessage) => void;
66
+ startRun: (parentId: string | null) => void;
67
+ cancelRun: () => void;
68
+ };
69
+
70
+ export type { AssistantContentPart as A, ImageContentPart as I, ModelConfigProvider as M, TextContentPart as T, UIContentPart as U, ToolCallContentPart as a, ThreadState as b, Unsubscribe as c, ThreadMessage as d, ModelConfig as e, AppendMessage as f, AssistantMessage as g, UserMessage as h, UserContentPart as i, AppendContentPart as j, Tool as k };
@@ -0,0 +1,83 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, { get: all[name], enumerable: true });
5
+ };
6
+
7
+ // src/context/MessageContext.ts
8
+ import { createContext, useContext } from "react";
9
+ var MessageContext = createContext(null);
10
+ var useMessageContext = () => {
11
+ const context = useContext(MessageContext);
12
+ if (!context)
13
+ throw new Error(
14
+ "This component can only be used inside a component passed to <ThreadPrimitive.Messages components={...} />."
15
+ );
16
+ return context;
17
+ };
18
+
19
+ // src/context/ThreadContext.ts
20
+ import { createContext as createContext2, useContext as useContext2 } from "react";
21
+ var ThreadContext = createContext2(null);
22
+ var useThreadContext = () => {
23
+ const context = useContext2(ThreadContext);
24
+ if (!context)
25
+ throw new Error("This component must be used within an AssistantRuntimeProvider.");
26
+ return context;
27
+ };
28
+
29
+ // src/context/ComposerContext.ts
30
+ import { useContext as useContext3, useMemo } from "react";
31
+ var useComposerContext = () => {
32
+ const { useComposer } = useThreadContext();
33
+ const { useComposer: useEditComposer } = useContext3(MessageContext) ?? {};
34
+ return useMemo(
35
+ () => ({
36
+ useComposer: useEditComposer ?? useComposer,
37
+ type: useEditComposer ? "edit" : "new"
38
+ }),
39
+ [useEditComposer, useComposer]
40
+ );
41
+ };
42
+
43
+ // src/context/AssistantContext.ts
44
+ import { createContext as createContext3, useContext as useContext4 } from "react";
45
+ var AssistantContext = createContext3(
46
+ null
47
+ );
48
+ var useAssistantContext = () => {
49
+ const context = useContext4(AssistantContext);
50
+ if (!context)
51
+ throw new Error(
52
+ "This component must be used within an AssistantRuntimeProvider."
53
+ );
54
+ return context;
55
+ };
56
+
57
+ // src/context/ContentPartContext.ts
58
+ import { createContext as createContext4, useContext as useContext5 } from "react";
59
+ var ContentPartContext = createContext4(
60
+ null
61
+ );
62
+ var useContentPartContext = () => {
63
+ const context = useContext5(ContentPartContext);
64
+ if (!context)
65
+ throw new Error(
66
+ "This component can only be used inside a component passed to <MessagePrimitive.Content components={...} >."
67
+ );
68
+ return context;
69
+ };
70
+
71
+ export {
72
+ __export,
73
+ MessageContext,
74
+ useMessageContext,
75
+ ThreadContext,
76
+ useThreadContext,
77
+ useComposerContext,
78
+ AssistantContext,
79
+ useAssistantContext,
80
+ ContentPartContext,
81
+ useContentPartContext
82
+ };
83
+ //# sourceMappingURL=chunk-KIP3YFVM.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context/MessageContext.ts","../src/context/ThreadContext.ts","../src/context/ComposerContext.ts","../src/context/AssistantContext.ts","../src/context/ContentPartContext.ts"],"sourcesContent":["import { createContext, useContext } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport type { MessageState } from \"./stores/Message\";\nimport type { EditComposerState } from \"./stores/MessageComposer\";\n\nexport type MessageContextValue = {\n useMessage: UseBoundStore<StoreApi<MessageState>>;\n useComposer: UseBoundStore<StoreApi<EditComposerState>>;\n};\n\nexport const MessageContext = createContext<MessageContextValue | null>(null);\n\nexport const useMessageContext = () => {\n const context = useContext(MessageContext);\n if (!context)\n throw new Error(\n \"This component can only be used inside a component passed to <ThreadPrimitive.Messages components={...} />.\",\n );\n return context;\n};\n","import { createContext, useContext } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport type { ComposerState } from \"./stores/Composer\";\nimport type { ThreadState } from \"./stores/Thread\";\nimport type { ThreadViewportState } from \"./stores/ThreadViewport\";\n\nexport type ThreadContextValue = {\n useThread: UseBoundStore<StoreApi<ThreadState>>;\n useComposer: UseBoundStore<StoreApi<ComposerState>>;\n useViewport: UseBoundStore<StoreApi<ThreadViewportState>>;\n};\n\nexport const ThreadContext = createContext<ThreadContextValue | null>(null);\n\nexport const useThreadContext = (): ThreadContextValue => {\n const context = useContext(ThreadContext);\n if (!context)\n throw new Error(\"This component must be used within an AssistantRuntimeProvider.\");\n return context;\n};\n","import { useContext, useMemo } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport { MessageContext } from \"./MessageContext\";\nimport { useThreadContext } from \"./ThreadContext\";\nimport type { ComposerState } from \"./stores/Composer\";\nimport type { EditComposerState } from \"./stores/MessageComposer\";\n\nexport type ComposerContextValue = {\n useComposer: UseBoundStore<StoreApi<EditComposerState | ComposerState>>;\n type: \"edit\" | \"new\";\n};\n\nexport const useComposerContext = (): ComposerContextValue => {\n const { useComposer } = useThreadContext();\n const { useComposer: useEditComposer } = useContext(MessageContext) ?? {};\n return useMemo(\n () => ({\n useComposer: (useEditComposer ?? useComposer) as UseBoundStore<\n StoreApi<EditComposerState | ComposerState>\n >,\n type: useEditComposer ? (\"edit\" as const) : (\"new\" as const),\n }),\n [useEditComposer, useComposer],\n );\n};\n","import { createContext, useContext } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport type { AssistantModelConfigState } from \"./stores/AssistantModelConfig\";\nimport type { AssistantToolRenderersState } from \"./stores/AssistantToolRenderers\";\n\nexport type AssistantContextValue = {\n useModelConfig: UseBoundStore<StoreApi<AssistantModelConfigState>>;\n useToolRenderers: UseBoundStore<StoreApi<AssistantToolRenderersState>>;\n};\n\nexport const AssistantContext = createContext<AssistantContextValue | null>(\n null,\n);\n\nexport const useAssistantContext = (): AssistantContextValue => {\n const context = useContext(AssistantContext);\n if (!context)\n throw new Error(\n \"This component must be used within an AssistantRuntimeProvider.\",\n );\n return context;\n};\n","import { createContext, useContext } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport type { ContentPartState } from \"./stores/ContentPart\";\n\nexport type ContentPartContextValue = {\n useContentPart: UseBoundStore<StoreApi<ContentPartState>>;\n};\n\nexport const ContentPartContext = createContext<ContentPartContextValue | null>(\n null,\n);\n\nexport const useContentPartContext = (): ContentPartContextValue => {\n const context = useContext(ContentPartContext);\n if (!context)\n throw new Error(\n \"This component can only be used inside a component passed to <MessagePrimitive.Content components={...} >.\",\n );\n return context;\n};\n"],"mappings":";;;;;;;AAAA,SAAS,eAAe,kBAAkB;AAUnC,IAAM,iBAAiB,cAA0C,IAAI;AAErE,IAAM,oBAAoB,MAAM;AACrC,QAAM,UAAU,WAAW,cAAc;AACzC,MAAI,CAAC;AACH,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AACF,SAAO;AACT;;;ACnBA,SAAS,iBAAAA,gBAAe,cAAAC,mBAAkB;AAYnC,IAAM,gBAAgBD,eAAyC,IAAI;AAEnE,IAAM,mBAAmB,MAA0B;AACxD,QAAM,UAAUC,YAAW,aAAa;AACxC,MAAI,CAAC;AACH,UAAM,IAAI,MAAM,iEAAiE;AACnF,SAAO;AACT;;;ACnBA,SAAS,cAAAC,aAAY,eAAe;AAY7B,IAAM,qBAAqB,MAA4B;AAC5D,QAAM,EAAE,YAAY,IAAI,iBAAiB;AACzC,QAAM,EAAE,aAAa,gBAAgB,IAAIC,YAAW,cAAc,KAAK,CAAC;AACxE,SAAO;AAAA,IACL,OAAO;AAAA,MACL,aAAc,mBAAmB;AAAA,MAGjC,MAAM,kBAAmB,SAAoB;AAAA,IAC/C;AAAA,IACA,CAAC,iBAAiB,WAAW;AAAA,EAC/B;AACF;;;ACxBA,SAAS,iBAAAC,gBAAe,cAAAC,mBAAkB;AAUnC,IAAM,mBAAmBD;AAAA,EAC9B;AACF;AAEO,IAAM,sBAAsB,MAA6B;AAC9D,QAAM,UAAUC,YAAW,gBAAgB;AAC3C,MAAI,CAAC;AACH,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AACF,SAAO;AACT;;;ACrBA,SAAS,iBAAAC,gBAAe,cAAAC,mBAAkB;AAQnC,IAAM,qBAAqBD;AAAA,EAChC;AACF;AAEO,IAAM,wBAAwB,MAA+B;AAClE,QAAM,UAAUC,YAAW,kBAAkB;AAC7C,MAAI,CAAC;AACH,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AACF,SAAO;AACT;","names":["createContext","useContext","useContext","useContext","createContext","useContext","createContext","useContext"]}
@@ -0,0 +1,108 @@
1
+ import { M as ModelConfigProvider, d as ThreadMessage, c as Unsubscribe, k as Tool, a as ToolCallContentPart, b as ThreadState } from './Thread-DcmnmkmV.mjs';
2
+ export { I as ImageContentPart, e as ModelConfig, U as UIContentPart } from './Thread-DcmnmkmV.mjs';
3
+ import { UseBoundStore, StoreApi } from 'zustand';
4
+ import { ReactNode, ComponentType } from 'react';
5
+ import 'zod';
6
+
7
+ type AssistantModelConfigState = {
8
+ getModelConfig: ModelConfigProvider;
9
+ registerModelConfigProvider: (provider: ModelConfigProvider) => () => void;
10
+ };
11
+
12
+ type ContentPartState = Readonly<{
13
+ status: "in_progress" | "done" | "error";
14
+ part: ThreadMessage["content"][number];
15
+ }>;
16
+
17
+ type MessageState = Readonly<{
18
+ message: Readonly<ThreadMessage>;
19
+ parentId: string | null;
20
+ branches: readonly string[];
21
+ isLast: boolean;
22
+ inProgressIndicator: ReactNode | null;
23
+ setInProgressIndicator: (value: ReactNode | null) => void;
24
+ isCopied: boolean;
25
+ setIsCopied: (value: boolean) => void;
26
+ isHovering: boolean;
27
+ setIsHovering: (value: boolean) => void;
28
+ }>;
29
+
30
+ type BaseComposerState = Readonly<{
31
+ value: string;
32
+ setValue: (value: string) => void;
33
+ }>;
34
+
35
+ type EditComposerState = BaseComposerState & Readonly<{
36
+ isEditing: boolean;
37
+ edit: () => void;
38
+ send: () => void;
39
+ cancel: () => boolean;
40
+ }>;
41
+
42
+ type ComposerState = BaseComposerState & Readonly<{
43
+ isEditing: true;
44
+ send: () => void;
45
+ cancel: () => boolean;
46
+ }>;
47
+
48
+ type ThreadViewportState = {
49
+ isAtBottom: boolean;
50
+ scrollToBottom: () => void;
51
+ onScrollToBottom: (callback: () => void) => Unsubscribe;
52
+ };
53
+
54
+ declare const useAssistantInstructions: (instruction: string) => void;
55
+
56
+ type UseAssistantTool<TArgs, TResult> = Tool<TArgs, TResult> & {
57
+ name: string;
58
+ render?: ToolRenderComponent<TArgs, TResult>;
59
+ };
60
+ declare const useAssistantTool: <TArgs, TResult>(tool: UseAssistantTool<TArgs, TResult>) => void;
61
+
62
+ type UseAssistantToolRenderer<TArgs, TResult> = {
63
+ name: string;
64
+ render: ToolRenderComponent<TArgs, TResult>;
65
+ };
66
+ declare const useAssistantToolRenderer: (tool: UseAssistantToolRenderer<any, any> | null) => void;
67
+
68
+ type ToolRenderComponent<TArgs, TResult> = ComponentType<{
69
+ part: ToolCallContentPart<TArgs, TResult>;
70
+ status: "done" | "in_progress" | "error";
71
+ }>;
72
+
73
+ type AssistantToolRenderersState = {
74
+ getToolRenderer: (name: string) => ToolRenderComponent<any, any> | null;
75
+ setToolRenderer: (name: string, render: ToolRenderComponent<any, any>) => () => void;
76
+ };
77
+
78
+ type AssistantContextValue = {
79
+ useModelConfig: UseBoundStore<StoreApi<AssistantModelConfigState>>;
80
+ useToolRenderers: UseBoundStore<StoreApi<AssistantToolRenderersState>>;
81
+ };
82
+ declare const useAssistantContext: () => AssistantContextValue;
83
+
84
+ type ThreadContextValue = {
85
+ useThread: UseBoundStore<StoreApi<ThreadState>>;
86
+ useComposer: UseBoundStore<StoreApi<ComposerState>>;
87
+ useViewport: UseBoundStore<StoreApi<ThreadViewportState>>;
88
+ };
89
+ declare const useThreadContext: () => ThreadContextValue;
90
+
91
+ type ComposerContextValue = {
92
+ useComposer: UseBoundStore<StoreApi<EditComposerState | ComposerState>>;
93
+ type: "edit" | "new";
94
+ };
95
+ declare const useComposerContext: () => ComposerContextValue;
96
+
97
+ type MessageContextValue = {
98
+ useMessage: UseBoundStore<StoreApi<MessageState>>;
99
+ useComposer: UseBoundStore<StoreApi<EditComposerState>>;
100
+ };
101
+ declare const useMessageContext: () => MessageContextValue;
102
+
103
+ type ContentPartContextValue = {
104
+ useContentPart: UseBoundStore<StoreApi<ContentPartState>>;
105
+ };
106
+ declare const useContentPartContext: () => ContentPartContextValue;
107
+
108
+ export { type AssistantContextValue, type AssistantModelConfigState, type ComposerContextValue, type ComposerState, type ContentPartContextValue, type ContentPartState, type EditComposerState, type MessageContextValue, type MessageState, ModelConfigProvider, type ThreadContextValue, ThreadState, type ThreadViewportState, ToolCallContentPart, useAssistantContext, useAssistantInstructions, useAssistantTool, useAssistantToolRenderer, useComposerContext, useContentPartContext, useMessageContext, useThreadContext };
@@ -0,0 +1,108 @@
1
+ import { M as ModelConfigProvider, d as ThreadMessage, c as Unsubscribe, k as Tool, a as ToolCallContentPart, b as ThreadState } from './Thread-DcmnmkmV.js';
2
+ export { I as ImageContentPart, e as ModelConfig, U as UIContentPart } from './Thread-DcmnmkmV.js';
3
+ import { UseBoundStore, StoreApi } from 'zustand';
4
+ import { ReactNode, ComponentType } from 'react';
5
+ import 'zod';
6
+
7
+ type AssistantModelConfigState = {
8
+ getModelConfig: ModelConfigProvider;
9
+ registerModelConfigProvider: (provider: ModelConfigProvider) => () => void;
10
+ };
11
+
12
+ type ContentPartState = Readonly<{
13
+ status: "in_progress" | "done" | "error";
14
+ part: ThreadMessage["content"][number];
15
+ }>;
16
+
17
+ type MessageState = Readonly<{
18
+ message: Readonly<ThreadMessage>;
19
+ parentId: string | null;
20
+ branches: readonly string[];
21
+ isLast: boolean;
22
+ inProgressIndicator: ReactNode | null;
23
+ setInProgressIndicator: (value: ReactNode | null) => void;
24
+ isCopied: boolean;
25
+ setIsCopied: (value: boolean) => void;
26
+ isHovering: boolean;
27
+ setIsHovering: (value: boolean) => void;
28
+ }>;
29
+
30
+ type BaseComposerState = Readonly<{
31
+ value: string;
32
+ setValue: (value: string) => void;
33
+ }>;
34
+
35
+ type EditComposerState = BaseComposerState & Readonly<{
36
+ isEditing: boolean;
37
+ edit: () => void;
38
+ send: () => void;
39
+ cancel: () => boolean;
40
+ }>;
41
+
42
+ type ComposerState = BaseComposerState & Readonly<{
43
+ isEditing: true;
44
+ send: () => void;
45
+ cancel: () => boolean;
46
+ }>;
47
+
48
+ type ThreadViewportState = {
49
+ isAtBottom: boolean;
50
+ scrollToBottom: () => void;
51
+ onScrollToBottom: (callback: () => void) => Unsubscribe;
52
+ };
53
+
54
+ declare const useAssistantInstructions: (instruction: string) => void;
55
+
56
+ type UseAssistantTool<TArgs, TResult> = Tool<TArgs, TResult> & {
57
+ name: string;
58
+ render?: ToolRenderComponent<TArgs, TResult>;
59
+ };
60
+ declare const useAssistantTool: <TArgs, TResult>(tool: UseAssistantTool<TArgs, TResult>) => void;
61
+
62
+ type UseAssistantToolRenderer<TArgs, TResult> = {
63
+ name: string;
64
+ render: ToolRenderComponent<TArgs, TResult>;
65
+ };
66
+ declare const useAssistantToolRenderer: (tool: UseAssistantToolRenderer<any, any> | null) => void;
67
+
68
+ type ToolRenderComponent<TArgs, TResult> = ComponentType<{
69
+ part: ToolCallContentPart<TArgs, TResult>;
70
+ status: "done" | "in_progress" | "error";
71
+ }>;
72
+
73
+ type AssistantToolRenderersState = {
74
+ getToolRenderer: (name: string) => ToolRenderComponent<any, any> | null;
75
+ setToolRenderer: (name: string, render: ToolRenderComponent<any, any>) => () => void;
76
+ };
77
+
78
+ type AssistantContextValue = {
79
+ useModelConfig: UseBoundStore<StoreApi<AssistantModelConfigState>>;
80
+ useToolRenderers: UseBoundStore<StoreApi<AssistantToolRenderersState>>;
81
+ };
82
+ declare const useAssistantContext: () => AssistantContextValue;
83
+
84
+ type ThreadContextValue = {
85
+ useThread: UseBoundStore<StoreApi<ThreadState>>;
86
+ useComposer: UseBoundStore<StoreApi<ComposerState>>;
87
+ useViewport: UseBoundStore<StoreApi<ThreadViewportState>>;
88
+ };
89
+ declare const useThreadContext: () => ThreadContextValue;
90
+
91
+ type ComposerContextValue = {
92
+ useComposer: UseBoundStore<StoreApi<EditComposerState | ComposerState>>;
93
+ type: "edit" | "new";
94
+ };
95
+ declare const useComposerContext: () => ComposerContextValue;
96
+
97
+ type MessageContextValue = {
98
+ useMessage: UseBoundStore<StoreApi<MessageState>>;
99
+ useComposer: UseBoundStore<StoreApi<EditComposerState>>;
100
+ };
101
+ declare const useMessageContext: () => MessageContextValue;
102
+
103
+ type ContentPartContextValue = {
104
+ useContentPart: UseBoundStore<StoreApi<ContentPartState>>;
105
+ };
106
+ declare const useContentPartContext: () => ContentPartContextValue;
107
+
108
+ export { type AssistantContextValue, type AssistantModelConfigState, type ComposerContextValue, type ComposerState, type ContentPartContextValue, type ContentPartState, type EditComposerState, type MessageContextValue, type MessageState, ModelConfigProvider, type ThreadContextValue, ThreadState, type ThreadViewportState, ToolCallContentPart, useAssistantContext, useAssistantInstructions, useAssistantTool, useAssistantToolRenderer, useComposerContext, useContentPartContext, useMessageContext, useThreadContext };
@@ -0,0 +1,161 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/experimental.ts
21
+ var experimental_exports = {};
22
+ __export(experimental_exports, {
23
+ useAssistantContext: () => useAssistantContext,
24
+ useAssistantInstructions: () => useAssistantInstructions,
25
+ useAssistantTool: () => useAssistantTool,
26
+ useAssistantToolRenderer: () => useAssistantToolRenderer,
27
+ useComposerContext: () => useComposerContext,
28
+ useContentPartContext: () => useContentPartContext,
29
+ useMessageContext: () => useMessageContext,
30
+ useThreadContext: () => useThreadContext
31
+ });
32
+ module.exports = __toCommonJS(experimental_exports);
33
+
34
+ // src/context/AssistantContext.ts
35
+ var import_react = require("react");
36
+ var AssistantContext = (0, import_react.createContext)(
37
+ null
38
+ );
39
+ var useAssistantContext = () => {
40
+ const context = (0, import_react.useContext)(AssistantContext);
41
+ if (!context)
42
+ throw new Error(
43
+ "This component must be used within an AssistantRuntimeProvider."
44
+ );
45
+ return context;
46
+ };
47
+
48
+ // src/context/ThreadContext.ts
49
+ var import_react2 = require("react");
50
+ var ThreadContext = (0, import_react2.createContext)(null);
51
+ var useThreadContext = () => {
52
+ const context = (0, import_react2.useContext)(ThreadContext);
53
+ if (!context)
54
+ throw new Error("This component must be used within an AssistantRuntimeProvider.");
55
+ return context;
56
+ };
57
+
58
+ // src/context/ComposerContext.ts
59
+ var import_react4 = require("react");
60
+
61
+ // src/context/MessageContext.ts
62
+ var import_react3 = require("react");
63
+ var MessageContext = (0, import_react3.createContext)(null);
64
+ var useMessageContext = () => {
65
+ const context = (0, import_react3.useContext)(MessageContext);
66
+ if (!context)
67
+ throw new Error(
68
+ "This component can only be used inside a component passed to <ThreadPrimitive.Messages components={...} />."
69
+ );
70
+ return context;
71
+ };
72
+
73
+ // src/context/ComposerContext.ts
74
+ var useComposerContext = () => {
75
+ const { useComposer } = useThreadContext();
76
+ const { useComposer: useEditComposer } = (0, import_react4.useContext)(MessageContext) ?? {};
77
+ return (0, import_react4.useMemo)(
78
+ () => ({
79
+ useComposer: useEditComposer ?? useComposer,
80
+ type: useEditComposer ? "edit" : "new"
81
+ }),
82
+ [useEditComposer, useComposer]
83
+ );
84
+ };
85
+
86
+ // src/context/ContentPartContext.ts
87
+ var import_react5 = require("react");
88
+ var ContentPartContext = (0, import_react5.createContext)(
89
+ null
90
+ );
91
+ var useContentPartContext = () => {
92
+ const context = (0, import_react5.useContext)(ContentPartContext);
93
+ if (!context)
94
+ throw new Error(
95
+ "This component can only be used inside a component passed to <MessagePrimitive.Content components={...} >."
96
+ );
97
+ return context;
98
+ };
99
+
100
+ // src/model-config/useAssistantInstructions.tsx
101
+ var import_react6 = require("react");
102
+ var useAssistantInstructions = (instruction) => {
103
+ const { useModelConfig } = useAssistantContext();
104
+ const registerModelConfigProvider = useModelConfig(
105
+ (s) => s.registerModelConfigProvider
106
+ );
107
+ (0, import_react6.useEffect)(() => {
108
+ const config = {
109
+ system: instruction
110
+ };
111
+ return registerModelConfigProvider(() => config);
112
+ }, [registerModelConfigProvider, instruction]);
113
+ };
114
+
115
+ // src/model-config/useAssistantTool.tsx
116
+ var import_react7 = require("react");
117
+ var useAssistantTool = (tool) => {
118
+ const { useModelConfig, useToolRenderers } = useAssistantContext();
119
+ const registerModelConfigProvider = useModelConfig(
120
+ (s) => s.registerModelConfigProvider
121
+ );
122
+ const setToolRenderer = useToolRenderers((s) => s.setToolRenderer);
123
+ (0, import_react7.useEffect)(() => {
124
+ const { name, render, ...rest } = tool;
125
+ const config = {
126
+ tools: {
127
+ [tool.name]: rest
128
+ }
129
+ };
130
+ const unsub1 = registerModelConfigProvider(() => config);
131
+ const unsub2 = render ? setToolRenderer(name, render) : void 0;
132
+ return () => {
133
+ unsub1();
134
+ unsub2?.();
135
+ };
136
+ }, [registerModelConfigProvider, setToolRenderer, tool]);
137
+ };
138
+
139
+ // src/model-config/useAssistantToolRenderer.tsx
140
+ var import_react8 = require("react");
141
+ var useAssistantToolRenderer = (tool) => {
142
+ const { useToolRenderers } = useAssistantContext();
143
+ const setToolRenderer = useToolRenderers((s) => s.setToolRenderer);
144
+ (0, import_react8.useEffect)(() => {
145
+ if (!tool) return;
146
+ const { name, render } = tool;
147
+ return setToolRenderer(name, render);
148
+ }, [setToolRenderer, tool]);
149
+ };
150
+ // Annotate the CommonJS export names for ESM import in node:
151
+ 0 && (module.exports = {
152
+ useAssistantContext,
153
+ useAssistantInstructions,
154
+ useAssistantTool,
155
+ useAssistantToolRenderer,
156
+ useComposerContext,
157
+ useContentPartContext,
158
+ useMessageContext,
159
+ useThreadContext
160
+ });
161
+ //# sourceMappingURL=experimental.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/experimental.ts","../src/context/AssistantContext.ts","../src/context/ThreadContext.ts","../src/context/ComposerContext.ts","../src/context/MessageContext.ts","../src/context/ContentPartContext.ts","../src/model-config/useAssistantInstructions.tsx","../src/model-config/useAssistantTool.tsx","../src/model-config/useAssistantToolRenderer.tsx"],"sourcesContent":["export type {\n ImageContentPart,\n ToolCallContentPart,\n UIContentPart,\n} from \"./utils/AssistantTypes\";\n\nexport type {\n ModelConfigProvider,\n ModelConfig,\n} from \"./utils/ModelConfigTypes\";\n\nexport * from \"./context\";\nexport { useAssistantInstructions } from \"./model-config/useAssistantInstructions\";\nexport { useAssistantTool } from \"./model-config/useAssistantTool\";\nexport { useAssistantToolRenderer } from \"./model-config/useAssistantToolRenderer\";\n","import { createContext, useContext } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport type { AssistantModelConfigState } from \"./stores/AssistantModelConfig\";\nimport type { AssistantToolRenderersState } from \"./stores/AssistantToolRenderers\";\n\nexport type AssistantContextValue = {\n useModelConfig: UseBoundStore<StoreApi<AssistantModelConfigState>>;\n useToolRenderers: UseBoundStore<StoreApi<AssistantToolRenderersState>>;\n};\n\nexport const AssistantContext = createContext<AssistantContextValue | null>(\n null,\n);\n\nexport const useAssistantContext = (): AssistantContextValue => {\n const context = useContext(AssistantContext);\n if (!context)\n throw new Error(\n \"This component must be used within an AssistantRuntimeProvider.\",\n );\n return context;\n};\n","import { createContext, useContext } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport type { ComposerState } from \"./stores/Composer\";\nimport type { ThreadState } from \"./stores/Thread\";\nimport type { ThreadViewportState } from \"./stores/ThreadViewport\";\n\nexport type ThreadContextValue = {\n useThread: UseBoundStore<StoreApi<ThreadState>>;\n useComposer: UseBoundStore<StoreApi<ComposerState>>;\n useViewport: UseBoundStore<StoreApi<ThreadViewportState>>;\n};\n\nexport const ThreadContext = createContext<ThreadContextValue | null>(null);\n\nexport const useThreadContext = (): ThreadContextValue => {\n const context = useContext(ThreadContext);\n if (!context)\n throw new Error(\"This component must be used within an AssistantRuntimeProvider.\");\n return context;\n};\n","import { useContext, useMemo } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport { MessageContext } from \"./MessageContext\";\nimport { useThreadContext } from \"./ThreadContext\";\nimport type { ComposerState } from \"./stores/Composer\";\nimport type { EditComposerState } from \"./stores/MessageComposer\";\n\nexport type ComposerContextValue = {\n useComposer: UseBoundStore<StoreApi<EditComposerState | ComposerState>>;\n type: \"edit\" | \"new\";\n};\n\nexport const useComposerContext = (): ComposerContextValue => {\n const { useComposer } = useThreadContext();\n const { useComposer: useEditComposer } = useContext(MessageContext) ?? {};\n return useMemo(\n () => ({\n useComposer: (useEditComposer ?? useComposer) as UseBoundStore<\n StoreApi<EditComposerState | ComposerState>\n >,\n type: useEditComposer ? (\"edit\" as const) : (\"new\" as const),\n }),\n [useEditComposer, useComposer],\n );\n};\n","import { createContext, useContext } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport type { MessageState } from \"./stores/Message\";\nimport type { EditComposerState } from \"./stores/MessageComposer\";\n\nexport type MessageContextValue = {\n useMessage: UseBoundStore<StoreApi<MessageState>>;\n useComposer: UseBoundStore<StoreApi<EditComposerState>>;\n};\n\nexport const MessageContext = createContext<MessageContextValue | null>(null);\n\nexport const useMessageContext = () => {\n const context = useContext(MessageContext);\n if (!context)\n throw new Error(\n \"This component can only be used inside a component passed to <ThreadPrimitive.Messages components={...} />.\",\n );\n return context;\n};\n","import { createContext, useContext } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport type { ContentPartState } from \"./stores/ContentPart\";\n\nexport type ContentPartContextValue = {\n useContentPart: UseBoundStore<StoreApi<ContentPartState>>;\n};\n\nexport const ContentPartContext = createContext<ContentPartContextValue | null>(\n null,\n);\n\nexport const useContentPartContext = (): ContentPartContextValue => {\n const context = useContext(ContentPartContext);\n if (!context)\n throw new Error(\n \"This component can only be used inside a component passed to <MessagePrimitive.Content components={...} >.\",\n );\n return context;\n};\n","\"use client\";\n\nimport { useEffect } from \"react\";\nimport { useAssistantContext } from \"../context/AssistantContext\";\n\nexport const useAssistantInstructions = (instruction: string) => {\n const { useModelConfig } = useAssistantContext();\n const registerModelConfigProvider = useModelConfig(\n (s) => s.registerModelConfigProvider,\n );\n useEffect(() => {\n const config = {\n system: instruction,\n };\n return registerModelConfigProvider(() => config);\n }, [registerModelConfigProvider, instruction]);\n};\n","\"use client\";\n\nimport { useEffect } from \"react\";\nimport { useAssistantContext } from \"../context/AssistantContext\";\nimport type { Tool } from \"../utils/ModelConfigTypes\";\nimport type { ToolRenderComponent } from \"./ToolRenderComponent\";\n\nexport type UseAssistantTool<TArgs, TResult> = Tool<TArgs, TResult> & {\n name: string;\n render?: ToolRenderComponent<TArgs, TResult>;\n};\n\nexport const useAssistantTool = <TArgs, TResult>(\n tool: UseAssistantTool<TArgs, TResult>,\n) => {\n const { useModelConfig, useToolRenderers } = useAssistantContext();\n const registerModelConfigProvider = useModelConfig(\n (s) => s.registerModelConfigProvider,\n );\n const setToolRenderer = useToolRenderers((s) => s.setToolRenderer);\n useEffect(() => {\n const { name, render, ...rest } = tool;\n const config = {\n tools: {\n [tool.name]: rest,\n },\n };\n const unsub1 = registerModelConfigProvider(() => config);\n const unsub2 = render ? setToolRenderer(name, render) : undefined;\n return () => {\n unsub1();\n unsub2?.();\n };\n }, [registerModelConfigProvider, setToolRenderer, tool]);\n};\n","\"use client\";\nimport { useEffect } from \"react\";\nimport { useAssistantContext } from \"../context/AssistantContext\";\nimport type { ToolRenderComponent } from \"./ToolRenderComponent\";\n\ntype UseAssistantToolRenderer<TArgs, TResult> = {\n name: string;\n render: ToolRenderComponent<TArgs, TResult>;\n};\n\nexport const useAssistantToolRenderer = (\n // biome-ignore lint/suspicious/noExplicitAny: intentional any\n tool: UseAssistantToolRenderer<any, any> | null,\n) => {\n const { useToolRenderers } = useAssistantContext();\n const setToolRenderer = useToolRenderers((s) => s.setToolRenderer);\n useEffect(() => {\n if (!tool) return;\n const { name, render } = tool;\n return setToolRenderer(name, render);\n }, [setToolRenderer, tool]);\n};\n"],"mappings":"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","names":["import_react","import_react","import_react","import_react","import_react","import_react","import_react"]}