@assistant-ui/react 0.0.29 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/AssistantRuntime-BM_jVV3g.d.mts +23 -0
- package/dist/AssistantRuntime-CBMSAJqH.d.mts +23 -0
- package/dist/AssistantRuntime-C_BvM7ZT.d.ts +23 -0
- package/dist/AssistantRuntime-DELpXWfG.d.ts +23 -0
- package/dist/ModelConfigTypes-B9UY4zxv.d.mts +55 -0
- package/dist/ModelConfigTypes-B9UY4zxv.d.ts +55 -0
- package/dist/ModelConfigTypes-BF5HxVrH.d.mts +55 -0
- package/dist/ModelConfigTypes-BF5HxVrH.d.ts +55 -0
- package/dist/ModelConfigTypes-Cf3yjaDu.d.mts +55 -0
- package/dist/ModelConfigTypes-Cf3yjaDu.d.ts +55 -0
- package/dist/chunk-3XZUKECF.mjs +207 -0
- package/dist/chunk-3XZUKECF.mjs.map +1 -0
- package/dist/chunk-4DQ2CIAD.mjs +69 -0
- package/dist/chunk-4DQ2CIAD.mjs.map +1 -0
- package/dist/chunk-5YONSDN4.mjs +200 -0
- package/dist/chunk-5YONSDN4.mjs.map +1 -0
- package/dist/chunk-CY4TTHR7.mjs +76 -0
- package/dist/chunk-CY4TTHR7.mjs.map +1 -0
- package/dist/chunk-DKAWDNW5.mjs +22 -0
- package/dist/chunk-DKAWDNW5.mjs.map +1 -0
- package/dist/chunk-GQKH2ADD.mjs +165 -0
- package/dist/chunk-GQKH2ADD.mjs.map +1 -0
- package/dist/chunk-J5LGTIGS.mjs +10 -0
- package/dist/chunk-J5LGTIGS.mjs.map +1 -0
- package/dist/chunk-X4HBDEFP.mjs +30 -0
- package/dist/chunk-X4HBDEFP.mjs.map +1 -0
- package/dist/experimental.d.mts +120 -0
- package/dist/experimental.d.ts +120 -0
- package/dist/experimental.js +407 -0
- package/dist/experimental.js.map +1 -0
- package/dist/experimental.mjs +144 -0
- package/dist/experimental.mjs.map +1 -0
- package/dist/index.d.mts +6 -266
- package/dist/index.d.ts +6 -266
- package/dist/index.js +77 -730
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +62 -786
- package/dist/index.mjs.map +1 -1
- package/dist/internal-dlLjX30u.d.mts +75 -0
- package/dist/internal-dlLjX30u.d.ts +75 -0
- package/dist/internal.d.mts +25 -0
- package/dist/internal.d.ts +25 -0
- package/dist/internal.js +225 -0
- package/dist/internal.js.map +1 -0
- package/dist/internal.mjs +12 -0
- package/dist/internal.mjs.map +1 -0
- package/package.json +23 -3
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/experimental.ts","../src/runtime/local/useLocalRuntime.tsx","../src/utils/ModelConfigTypes.ts","../src/runtime/utils/idUtils.tsx","../src/runtime/utils/MessageRepository.tsx","../src/runtime/local/LocalRuntime.tsx","../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"],"sourcesContent":["export type {\n ImageContentPart,\n ToolCallContentPart,\n UIContentPart,\n} from \"./utils/AssistantTypes\";\n\nexport type { ModelConfigProvider } from \"./utils/ModelConfigTypes\";\n\nexport { useLocalRuntime } from \"./runtime/local/useLocalRuntime\";\nexport type {\n ChatModelAdapter,\n ChatModelRunOptions,\n} from \"./runtime/local/ChatModelAdapter\";\n\nexport * from \"./context\";\nexport { useAssistantInstructions } from \"./model-config/useAssistantInstructions\";\n","\"use client\";\n\nimport { useInsertionEffect, useState } from \"react\";\nimport type { ChatModelAdapter } from \"./ChatModelAdapter\";\nimport { LocalRuntime } from \"./LocalRuntime\";\n\nexport const useLocalRuntime = (adapter: ChatModelAdapter) => {\n const [runtime] = useState(() => new LocalRuntime(adapter));\n\n useInsertionEffect(() => {\n runtime.adapter = adapter;\n });\n\n return runtime;\n};\n","\"use client\";\nimport type { z } from \"zod\";\n\nexport type Tool<TArgs> = {\n description: string;\n parameters: z.ZodSchema<TArgs>;\n execute: (args: TArgs) => unknown; // TODO return type\n};\n\nexport type ModelConfig = {\n system?: string;\n // biome-ignore lint/suspicious/noExplicitAny: TODO\n tools?: Record<string, Tool<any>>;\n};\n\nexport type ModelConfigProvider = () => ModelConfig;\n\nexport const mergeModelConfigs = (configs: ModelConfig[]): ModelConfig => {\n return configs.reduce((acc, config) => {\n if (config.system) {\n if (acc.system) {\n // TODO should the separator be configurable?\n acc.system += `\\n\\n${config.system}`;\n } else {\n acc.system = config.system;\n }\n }\n if (config.tools) {\n acc.tools = { ...acc.tools, ...config.tools };\n }\n return acc;\n }, {} as ModelConfig);\n};\n","import { customAlphabet } from \"nanoid/non-secure\";\n\nexport const generateId = customAlphabet(\n \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz\",\n 7,\n);\n\nconst optimisticPrefix = \"__optimistic__\";\nexport const generateOptimisticId = () => `${optimisticPrefix}${generateId()}`;\nexport const isOptimisticId = (id: string) => id.startsWith(optimisticPrefix);\n","import type { ThreadMessage } from \"../../utils/AssistantTypes\";\nimport { generateOptimisticId } from \"./idUtils\";\n\ntype RepositoryParent = {\n children: string[];\n};\n\ntype RepositoryMessage = RepositoryParent & {\n prev: RepositoryMessage | null;\n current: ThreadMessage;\n next: RepositoryMessage | null;\n level: number;\n};\n\nconst findHead = (message: RepositoryMessage): RepositoryMessage => {\n if (message.next) return findHead(message.next);\n return message;\n};\n\nexport class MessageRepository {\n private messages = new Map<string, RepositoryMessage>(); // message_id -> item\n private head: RepositoryMessage | null = null;\n private root: RepositoryParent = {\n children: [],\n };\n\n private performOp(\n newParent: RepositoryMessage | null,\n child: RepositoryMessage,\n operation: \"cut\" | \"link\" | \"relink\",\n ) {\n const parentOrRoot = child.prev ?? this.root;\n const newParentOrRoot = newParent ?? this.root;\n\n if (operation === \"relink\" && parentOrRoot === newParentOrRoot) return;\n\n // cut\n if (operation !== \"link\") {\n parentOrRoot.children = parentOrRoot.children.filter(\n (m) => m !== child.current.id,\n );\n\n if (child.prev?.next === child) {\n const fallbackId = child.prev.children.at(-1);\n const fallback = fallbackId ? this.messages.get(fallbackId) : null;\n if (fallback === undefined) {\n throw new Error(\n \"MessageRepository(performOp/cut): Fallback sibling message not found. This is likely an internal bug in assistant-ui.\",\n );\n }\n child.prev.next = fallback;\n }\n }\n\n // link\n if (operation !== \"cut\") {\n newParentOrRoot.children = [\n ...newParentOrRoot.children,\n child.current.id,\n ];\n\n if (\n newParent &&\n (findHead(child) === this.head || newParent.next === null)\n ) {\n newParent.next = child;\n }\n\n child.prev = newParent;\n }\n }\n getMessages() {\n const messages = new Array<ThreadMessage>(this.head?.level ?? 0);\n for (let current = this.head; current; current = current.prev) {\n messages[current.level] = current.current;\n }\n return messages;\n }\n\n addOrUpdateMessage(parentId: string | null, message: ThreadMessage) {\n const existingItem = this.messages.get(message.id);\n const prev = parentId ? this.messages.get(parentId) : null;\n if (prev === undefined)\n throw new Error(\n \"MessageRepository(addOrUpdateMessage): Parent message not found. This is likely an internal bug in assistant-ui.\",\n );\n\n // update existing message\n if (existingItem) {\n existingItem.current = message;\n this.performOp(prev, existingItem, \"relink\");\n return;\n }\n\n // create a new message\n const newItem: RepositoryMessage = {\n prev,\n current: message,\n next: null,\n children: [],\n level: prev ? prev.level + 1 : 0,\n };\n\n this.messages.set(message.id, newItem);\n this.performOp(prev, newItem, \"link\");\n\n if (this.head === prev) {\n this.head = newItem;\n }\n }\n\n appendOptimisticMessage(\n parentId: string | null,\n message: Omit<ThreadMessage, \"id\" | \"createdAt\">,\n ) {\n let optimisticId: string;\n do {\n optimisticId = generateOptimisticId();\n } while (this.messages.has(optimisticId));\n\n this.addOrUpdateMessage(parentId, {\n ...message,\n id: optimisticId,\n createdAt: new Date(),\n ...(message.role === \"assistant\" ? { status: \"in_progress\" } : undefined),\n } as ThreadMessage);\n\n return optimisticId;\n }\n\n deleteMessage(messageId: string, replacementId?: string | null | undefined) {\n const message = this.messages.get(messageId);\n\n if (!message)\n throw new Error(\n \"MessageRepository(deleteMessage): Optimistic message not found. This is likely an internal bug in assistant-ui.\",\n );\n\n const replacement =\n replacementId === undefined\n ? message.prev // if no replacementId is provided, use the parent\n : replacementId === null\n ? null\n : this.messages.get(replacementId);\n if (replacement === undefined)\n throw new Error(\n \"MessageRepository(deleteMessage): Replacement not found. This is likely an internal bug in assistant-ui.\",\n );\n\n for (const child of message.children) {\n const childMessage = this.messages.get(child);\n if (!childMessage)\n throw new Error(\n \"MessageRepository(deleteMessage): Child message not found. This is likely an internal bug in assistant-ui.\",\n );\n this.performOp(replacement, childMessage, \"relink\");\n }\n\n this.performOp(null, message, \"cut\");\n this.messages.delete(messageId);\n\n if (this.head === message) {\n this.head = replacement ? findHead(replacement) : null;\n }\n }\n\n getBranches(messageId: string) {\n const message = this.messages.get(messageId);\n if (!message)\n throw new Error(\n \"MessageRepository(getBranches): Message not found. This is likely an internal bug in assistant-ui.\",\n );\n\n const { children } = message.prev ?? this.root;\n return children;\n }\n\n switchToBranch(messageId: string) {\n const message = this.messages.get(messageId);\n if (!message)\n throw new Error(\n \"MessageRepository(switchToBranch): Branch not found. This is likely an internal bug in assistant-ui.\",\n );\n\n if (message.prev) {\n message.prev.next = message;\n }\n\n this.head = findHead(message);\n }\n\n resetHead(messageId: string | null) {\n if (messageId === null) {\n this.head = null;\n return;\n }\n\n const message = this.messages.get(messageId);\n if (!message)\n throw new Error(\n \"MessageRepository(resetHead): Branch not found. This is likely an internal bug in assistant-ui.\",\n );\n\n this.head = message;\n for (\n let current: RepositoryMessage | null = message;\n current;\n current = current.prev\n ) {\n if (current.prev) {\n current.prev.next = current;\n }\n }\n }\n}\n","import type {\n AppendMessage,\n AssistantMessage,\n UserMessage,\n} from \"../../utils/AssistantTypes\";\nimport {\n type ModelConfigProvider,\n mergeModelConfigs,\n} from \"../../utils/ModelConfigTypes\";\nimport type { Unsubscribe } from \"../../utils/Unsubscribe\";\nimport type { AssistantRuntime } from \"../core/AssistantRuntime\";\nimport { MessageRepository } from \"../utils/MessageRepository\";\nimport { generateId } from \"../utils/idUtils\";\nimport type { ChatModelAdapter, ChatModelRunResult } from \"./ChatModelAdapter\";\n\nexport class LocalRuntime implements AssistantRuntime {\n private _subscriptions = new Set<() => void>();\n private _configProviders = new Set<ModelConfigProvider>();\n\n private abortController: AbortController | null = null;\n private repository = new MessageRepository();\n\n public get messages() {\n return this.repository.getMessages();\n }\n public get isRunning() {\n return this.abortController != null;\n }\n\n constructor(public adapter: ChatModelAdapter) {}\n\n public getBranches(messageId: string): string[] {\n return this.repository.getBranches(messageId);\n }\n\n public switchToBranch(branchId: string): void {\n this.repository.switchToBranch(branchId);\n this.notifySubscribers();\n }\n\n public async append(message: AppendMessage): Promise<void> {\n // add user message\n const userMessageId = generateId();\n const userMessage: UserMessage = {\n id: userMessageId,\n role: \"user\",\n content: message.content,\n createdAt: new Date(),\n };\n this.repository.addOrUpdateMessage(message.parentId, userMessage);\n\n await this.startRun(userMessageId);\n }\n\n public async startRun(parentId: string | null): Promise<void> {\n const id = generateId();\n\n this.repository.resetHead(parentId);\n const messages = this.repository.getMessages();\n\n // add assistant message\n const message: AssistantMessage = {\n id,\n role: \"assistant\",\n status: \"in_progress\",\n content: [{ type: \"text\", text: \"\" }],\n createdAt: new Date(),\n };\n this.repository.addOrUpdateMessage(parentId, { ...message });\n\n // abort existing run\n this.abortController?.abort();\n this.abortController = new AbortController();\n\n this.notifySubscribers();\n\n try {\n const updateHandler = ({ content }: ChatModelRunResult) => {\n message.content = content;\n this.repository.addOrUpdateMessage(parentId, { ...message });\n this.notifySubscribers();\n };\n const result = await this.adapter.run({\n messages,\n abortSignal: this.abortController.signal,\n config: mergeModelConfigs([...this._configProviders].map((p) => p())),\n onUpdate: updateHandler,\n });\n updateHandler(result);\n\n message.status = \"done\";\n this.repository.addOrUpdateMessage(parentId, { ...message });\n } catch (e) {\n message.status = \"error\";\n this.repository.addOrUpdateMessage(parentId, { ...message });\n console.error(e);\n } finally {\n this.abortController = null;\n this.notifySubscribers();\n }\n }\n\n cancelRun(): void {\n if (!this.abortController) return;\n\n this.abortController.abort();\n this.abortController = null;\n this.notifySubscribers();\n }\n\n private notifySubscribers() {\n for (const callback of this._subscriptions) callback();\n }\n\n public subscribe(callback: () => void): Unsubscribe {\n this._subscriptions.add(callback);\n return () => this._subscriptions.delete(callback);\n }\n\n registerModelConfigProvider(provider: ModelConfigProvider) {\n this._configProviders.add(provider);\n return () => this._configProviders.delete(provider);\n }\n}\n","import { createContext, useContext } from \"react\";\nimport type { StoreApi, UseBoundStore } from \"zustand\";\nimport type { AssistantModelConfigState } from \"./stores/AssistantModelConfig\";\n\nexport type AssistantContextValue = {\n useModelConfig: UseBoundStore<StoreApi<AssistantModelConfigState>>;\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 addContextProvider = useModelConfig(\n (s) => s.registerModelConfigProvider,\n );\n useEffect(\n () =>\n addContextProvider(() => {\n return {\n system: instruction,\n };\n }),\n [addContextProvider, instruction],\n );\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA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aAAc,mBAAmB;AAAA,MAGjC,MAAM,kBAAmB,SAAoB;AAAA,IAC/C;AAAA,IACA,CAAC,iBAAiB,WAAW;AAAA,EAC/B;AACF;;;AExBA,IAAAC,gBAA0C;AAQnC,IAAM,yBAAqB;AAAA,EAChC;AACF;AAEO,IAAM,wBAAwB,MAA+B;AAClE,QAAM,cAAU,0BAAW,kBAAkB;AAC7C,MAAI,CAAC;AACH,UAAM,IAAI;AAAA,MACR;AAAA,IACF;AACF,SAAO;AACT;;;ACjBA,IAAAC,gBAA0B;AAGnB,IAAM,2BAA2B,CAAC,gBAAwB;AAC/D,QAAM,EAAE,eAAe,IAAI,oBAAoB;AAC/C,QAAM,qBAAqB;AAAA,IACzB,CAAC,MAAM,EAAE;AAAA,EACX;AACA;AAAA,IACE,MACE,mBAAmB,MAAM;AACvB,aAAO;AAAA,QACL,QAAQ;AAAA,MACV;AAAA,IACF,CAAC;AAAA,IACH,CAAC,oBAAoB,WAAW;AAAA,EAClC;AACF;","names":["import_react","import_react","import_react","import_react","import_react","import_react"]}
|
@@ -0,0 +1,144 @@
|
|
1
|
+
import {
|
2
|
+
useAssistantContext,
|
3
|
+
useComposerContext,
|
4
|
+
useContentPartContext,
|
5
|
+
useMessageContext,
|
6
|
+
useThreadContext
|
7
|
+
} from "./chunk-CY4TTHR7.mjs";
|
8
|
+
import {
|
9
|
+
MessageRepository,
|
10
|
+
generateId
|
11
|
+
} from "./chunk-GQKH2ADD.mjs";
|
12
|
+
import {
|
13
|
+
mergeModelConfigs
|
14
|
+
} from "./chunk-X4HBDEFP.mjs";
|
15
|
+
|
16
|
+
// src/runtime/local/useLocalRuntime.tsx
|
17
|
+
import { useInsertionEffect, useState } from "react";
|
18
|
+
|
19
|
+
// src/runtime/local/LocalRuntime.tsx
|
20
|
+
var LocalRuntime = class {
|
21
|
+
constructor(adapter) {
|
22
|
+
this.adapter = adapter;
|
23
|
+
}
|
24
|
+
_subscriptions = /* @__PURE__ */ new Set();
|
25
|
+
_configProviders = /* @__PURE__ */ new Set();
|
26
|
+
abortController = null;
|
27
|
+
repository = new MessageRepository();
|
28
|
+
get messages() {
|
29
|
+
return this.repository.getMessages();
|
30
|
+
}
|
31
|
+
get isRunning() {
|
32
|
+
return this.abortController != null;
|
33
|
+
}
|
34
|
+
getBranches(messageId) {
|
35
|
+
return this.repository.getBranches(messageId);
|
36
|
+
}
|
37
|
+
switchToBranch(branchId) {
|
38
|
+
this.repository.switchToBranch(branchId);
|
39
|
+
this.notifySubscribers();
|
40
|
+
}
|
41
|
+
async append(message) {
|
42
|
+
const userMessageId = generateId();
|
43
|
+
const userMessage = {
|
44
|
+
id: userMessageId,
|
45
|
+
role: "user",
|
46
|
+
content: message.content,
|
47
|
+
createdAt: /* @__PURE__ */ new Date()
|
48
|
+
};
|
49
|
+
this.repository.addOrUpdateMessage(message.parentId, userMessage);
|
50
|
+
await this.startRun(userMessageId);
|
51
|
+
}
|
52
|
+
async startRun(parentId) {
|
53
|
+
const id = generateId();
|
54
|
+
this.repository.resetHead(parentId);
|
55
|
+
const messages = this.repository.getMessages();
|
56
|
+
const message = {
|
57
|
+
id,
|
58
|
+
role: "assistant",
|
59
|
+
status: "in_progress",
|
60
|
+
content: [{ type: "text", text: "" }],
|
61
|
+
createdAt: /* @__PURE__ */ new Date()
|
62
|
+
};
|
63
|
+
this.repository.addOrUpdateMessage(parentId, { ...message });
|
64
|
+
this.abortController?.abort();
|
65
|
+
this.abortController = new AbortController();
|
66
|
+
this.notifySubscribers();
|
67
|
+
try {
|
68
|
+
const updateHandler = ({ content }) => {
|
69
|
+
message.content = content;
|
70
|
+
this.repository.addOrUpdateMessage(parentId, { ...message });
|
71
|
+
this.notifySubscribers();
|
72
|
+
};
|
73
|
+
const result = await this.adapter.run({
|
74
|
+
messages,
|
75
|
+
abortSignal: this.abortController.signal,
|
76
|
+
config: mergeModelConfigs([...this._configProviders].map((p) => p())),
|
77
|
+
onUpdate: updateHandler
|
78
|
+
});
|
79
|
+
updateHandler(result);
|
80
|
+
message.status = "done";
|
81
|
+
this.repository.addOrUpdateMessage(parentId, { ...message });
|
82
|
+
} catch (e) {
|
83
|
+
message.status = "error";
|
84
|
+
this.repository.addOrUpdateMessage(parentId, { ...message });
|
85
|
+
console.error(e);
|
86
|
+
} finally {
|
87
|
+
this.abortController = null;
|
88
|
+
this.notifySubscribers();
|
89
|
+
}
|
90
|
+
}
|
91
|
+
cancelRun() {
|
92
|
+
if (!this.abortController) return;
|
93
|
+
this.abortController.abort();
|
94
|
+
this.abortController = null;
|
95
|
+
this.notifySubscribers();
|
96
|
+
}
|
97
|
+
notifySubscribers() {
|
98
|
+
for (const callback of this._subscriptions) callback();
|
99
|
+
}
|
100
|
+
subscribe(callback) {
|
101
|
+
this._subscriptions.add(callback);
|
102
|
+
return () => this._subscriptions.delete(callback);
|
103
|
+
}
|
104
|
+
registerModelConfigProvider(provider) {
|
105
|
+
this._configProviders.add(provider);
|
106
|
+
return () => this._configProviders.delete(provider);
|
107
|
+
}
|
108
|
+
};
|
109
|
+
|
110
|
+
// src/runtime/local/useLocalRuntime.tsx
|
111
|
+
var useLocalRuntime = (adapter) => {
|
112
|
+
const [runtime] = useState(() => new LocalRuntime(adapter));
|
113
|
+
useInsertionEffect(() => {
|
114
|
+
runtime.adapter = adapter;
|
115
|
+
});
|
116
|
+
return runtime;
|
117
|
+
};
|
118
|
+
|
119
|
+
// src/model-config/useAssistantInstructions.tsx
|
120
|
+
import { useEffect } from "react";
|
121
|
+
var useAssistantInstructions = (instruction) => {
|
122
|
+
const { useModelConfig } = useAssistantContext();
|
123
|
+
const addContextProvider = useModelConfig(
|
124
|
+
(s) => s.registerModelConfigProvider
|
125
|
+
);
|
126
|
+
useEffect(
|
127
|
+
() => addContextProvider(() => {
|
128
|
+
return {
|
129
|
+
system: instruction
|
130
|
+
};
|
131
|
+
}),
|
132
|
+
[addContextProvider, instruction]
|
133
|
+
);
|
134
|
+
};
|
135
|
+
export {
|
136
|
+
useAssistantContext,
|
137
|
+
useAssistantInstructions,
|
138
|
+
useComposerContext,
|
139
|
+
useContentPartContext,
|
140
|
+
useLocalRuntime,
|
141
|
+
useMessageContext,
|
142
|
+
useThreadContext
|
143
|
+
};
|
144
|
+
//# sourceMappingURL=experimental.mjs.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../src/runtime/local/useLocalRuntime.tsx","../src/runtime/local/LocalRuntime.tsx","../src/model-config/useAssistantInstructions.tsx"],"sourcesContent":["\"use client\";\n\nimport { useInsertionEffect, useState } from \"react\";\nimport type { ChatModelAdapter } from \"./ChatModelAdapter\";\nimport { LocalRuntime } from \"./LocalRuntime\";\n\nexport const useLocalRuntime = (adapter: ChatModelAdapter) => {\n const [runtime] = useState(() => new LocalRuntime(adapter));\n\n useInsertionEffect(() => {\n runtime.adapter = adapter;\n });\n\n return runtime;\n};\n","import type {\n AppendMessage,\n AssistantMessage,\n UserMessage,\n} from \"../../utils/AssistantTypes\";\nimport {\n type ModelConfigProvider,\n mergeModelConfigs,\n} from \"../../utils/ModelConfigTypes\";\nimport type { Unsubscribe } from \"../../utils/Unsubscribe\";\nimport type { AssistantRuntime } from \"../core/AssistantRuntime\";\nimport { MessageRepository } from \"../utils/MessageRepository\";\nimport { generateId } from \"../utils/idUtils\";\nimport type { ChatModelAdapter, ChatModelRunResult } from \"./ChatModelAdapter\";\n\nexport class LocalRuntime implements AssistantRuntime {\n private _subscriptions = new Set<() => void>();\n private _configProviders = new Set<ModelConfigProvider>();\n\n private abortController: AbortController | null = null;\n private repository = new MessageRepository();\n\n public get messages() {\n return this.repository.getMessages();\n }\n public get isRunning() {\n return this.abortController != null;\n }\n\n constructor(public adapter: ChatModelAdapter) {}\n\n public getBranches(messageId: string): string[] {\n return this.repository.getBranches(messageId);\n }\n\n public switchToBranch(branchId: string): void {\n this.repository.switchToBranch(branchId);\n this.notifySubscribers();\n }\n\n public async append(message: AppendMessage): Promise<void> {\n // add user message\n const userMessageId = generateId();\n const userMessage: UserMessage = {\n id: userMessageId,\n role: \"user\",\n content: message.content,\n createdAt: new Date(),\n };\n this.repository.addOrUpdateMessage(message.parentId, userMessage);\n\n await this.startRun(userMessageId);\n }\n\n public async startRun(parentId: string | null): Promise<void> {\n const id = generateId();\n\n this.repository.resetHead(parentId);\n const messages = this.repository.getMessages();\n\n // add assistant message\n const message: AssistantMessage = {\n id,\n role: \"assistant\",\n status: \"in_progress\",\n content: [{ type: \"text\", text: \"\" }],\n createdAt: new Date(),\n };\n this.repository.addOrUpdateMessage(parentId, { ...message });\n\n // abort existing run\n this.abortController?.abort();\n this.abortController = new AbortController();\n\n this.notifySubscribers();\n\n try {\n const updateHandler = ({ content }: ChatModelRunResult) => {\n message.content = content;\n this.repository.addOrUpdateMessage(parentId, { ...message });\n this.notifySubscribers();\n };\n const result = await this.adapter.run({\n messages,\n abortSignal: this.abortController.signal,\n config: mergeModelConfigs([...this._configProviders].map((p) => p())),\n onUpdate: updateHandler,\n });\n updateHandler(result);\n\n message.status = \"done\";\n this.repository.addOrUpdateMessage(parentId, { ...message });\n } catch (e) {\n message.status = \"error\";\n this.repository.addOrUpdateMessage(parentId, { ...message });\n console.error(e);\n } finally {\n this.abortController = null;\n this.notifySubscribers();\n }\n }\n\n cancelRun(): void {\n if (!this.abortController) return;\n\n this.abortController.abort();\n this.abortController = null;\n this.notifySubscribers();\n }\n\n private notifySubscribers() {\n for (const callback of this._subscriptions) callback();\n }\n\n public subscribe(callback: () => void): Unsubscribe {\n this._subscriptions.add(callback);\n return () => this._subscriptions.delete(callback);\n }\n\n registerModelConfigProvider(provider: ModelConfigProvider) {\n this._configProviders.add(provider);\n return () => this._configProviders.delete(provider);\n }\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 addContextProvider = useModelConfig(\n (s) => s.registerModelConfigProvider,\n );\n useEffect(\n () =>\n addContextProvider(() => {\n return {\n system: instruction,\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8B;AAC5D,QAAM,CAAC,OAAO,IAAI,SAAS,MAAM,IAAI,aAAa,OAAO,CAAC;AAE1D,qBAAmB,MAAM;AACvB,YAAQ,UAAU;AAAA,EACpB,CAAC;AAED,SAAO;AACT;;;AEZA,SAAS,iBAAiB;AAGnB,IAAM,2BAA2B,CAAC,gBAAwB;AAC/D,QAAM,EAAE,eAAe,IAAI,oBAAoB;AAC/C,QAAM,qBAAqB;AAAA,IACzB,CAAC,MAAM,EAAE;AAAA,EACX;AACA;AAAA,IACE,MACE,mBAAmB,MAAM;AACvB,aAAO;AAAA,QACL,QAAQ;AAAA,MACV;AAAA,IACF,CAAC;AAAA,IACH,CAAC,oBAAoB,WAAW;AAAA,EAClC;AACF;","names":[]}
|
package/dist/index.d.mts
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
import * as react from 'react';
|
2
2
|
import { FC, ReactNode, PropsWithChildren, ComponentType } from 'react';
|
3
3
|
import { TextareaAutosizeProps } from 'react-textarea-autosize';
|
4
|
-
import {
|
5
|
-
|
6
|
-
import {
|
7
|
-
|
4
|
+
import { T as TextContentPart, I as ImageContentPart, U as UIContentPart, a as ToolCallContentPart } from './ModelConfigTypes-B9UY4zxv.mjs';
|
5
|
+
export { g as AppendContentPart, d as AppendMessage, e as AssistantContentPart, A as AssistantMessage, b as ThreadMessage, f as UserContentPart, c as UserMessage } from './ModelConfigTypes-B9UY4zxv.mjs';
|
6
|
+
import { T as ThreadRuntime, A as AssistantRuntime } from './AssistantRuntime-CBMSAJqH.mjs';
|
7
|
+
export { U as Unsubscribe } from './AssistantRuntime-CBMSAJqH.mjs';
|
8
|
+
import 'zod';
|
8
9
|
|
9
10
|
declare const useCopyMessage: ({ copiedDuration }: {
|
10
11
|
copiedDuration?: number | undefined;
|
@@ -133,46 +134,6 @@ type MessageIfProps = RequireAtLeastOne<MessageIfFilters> & {
|
|
133
134
|
};
|
134
135
|
declare const MessageIf: FC<MessageIfProps>;
|
135
136
|
|
136
|
-
type TextContentPart = {
|
137
|
-
type: "text";
|
138
|
-
text: string;
|
139
|
-
};
|
140
|
-
type ImageContentPart = {
|
141
|
-
type: "image";
|
142
|
-
image: string;
|
143
|
-
};
|
144
|
-
type UIContentPart = {
|
145
|
-
type: "ui";
|
146
|
-
display: ReactNode;
|
147
|
-
};
|
148
|
-
type ToolCallContentPart = {
|
149
|
-
type: "tool-call";
|
150
|
-
name: string;
|
151
|
-
args: object;
|
152
|
-
result?: object;
|
153
|
-
};
|
154
|
-
type UserContentPart = TextContentPart | ImageContentPart | UIContentPart;
|
155
|
-
type AssistantContentPart = TextContentPart | UIContentPart | ToolCallContentPart;
|
156
|
-
type AppendContentPart = TextContentPart | ImageContentPart;
|
157
|
-
type BaseMessage = {
|
158
|
-
id: string;
|
159
|
-
createdAt: Date;
|
160
|
-
};
|
161
|
-
type UserMessage = BaseMessage & {
|
162
|
-
role: "user";
|
163
|
-
content: UserContentPart[];
|
164
|
-
};
|
165
|
-
type AssistantMessage = BaseMessage & {
|
166
|
-
role: "assistant";
|
167
|
-
content: AssistantContentPart[];
|
168
|
-
status: "in_progress" | "done" | "error";
|
169
|
-
};
|
170
|
-
type AppendMessage = {
|
171
|
-
parentId: string | null;
|
172
|
-
content: AppendContentPart[];
|
173
|
-
};
|
174
|
-
type ThreadMessage = UserMessage | AssistantMessage;
|
175
|
-
|
176
137
|
type MessageContentProps = {
|
177
138
|
components?: {
|
178
139
|
Text?: ComponentType<{
|
@@ -275,234 +236,13 @@ declare namespace index {
|
|
275
236
|
export { ContentPartInProgressIndicator as InProgressIndicator };
|
276
237
|
}
|
277
238
|
|
278
|
-
type VercelRSCMessage = {
|
279
|
-
id: string;
|
280
|
-
role: "user" | "assistant";
|
281
|
-
display: ReactNode;
|
282
|
-
createdAt?: Date;
|
283
|
-
};
|
284
|
-
|
285
|
-
type RSCMessageConverter<T> = {
|
286
|
-
convertMessage: (message: T) => VercelRSCMessage;
|
287
|
-
};
|
288
|
-
type VercelRSCAdapterBase<T> = {
|
289
|
-
messages: T[];
|
290
|
-
append: (message: AppendMessage) => Promise<void>;
|
291
|
-
edit?: (message: AppendMessage) => Promise<void>;
|
292
|
-
reload?: (parentId: string | null) => Promise<void>;
|
293
|
-
convertMessage?: (message: T) => VercelRSCMessage;
|
294
|
-
};
|
295
|
-
type VercelRSCAdapter<T = VercelRSCMessage> = VercelRSCAdapterBase<T> & (T extends VercelRSCMessage ? object : RSCMessageConverter<T>);
|
296
|
-
|
297
|
-
declare class VercelRSCRuntime<T extends WeakKey = VercelRSCMessage> implements AssistantRuntime, ReactThreadRuntime {
|
298
|
-
adapter: VercelRSCAdapter<T>;
|
299
|
-
private useAdapter;
|
300
|
-
private _subscriptions;
|
301
|
-
isRunning: boolean;
|
302
|
-
messages: ThreadMessage[];
|
303
|
-
constructor(adapter: VercelRSCAdapter<T>);
|
304
|
-
private withRunning;
|
305
|
-
getBranches(): readonly string[];
|
306
|
-
switchToBranch(): void;
|
307
|
-
append(message: AppendMessage): Promise<void>;
|
308
|
-
startRun(parentId: string | null): Promise<void>;
|
309
|
-
cancelRun(): void;
|
310
|
-
subscribe(callback: () => void): () => void;
|
311
|
-
onAdapterUpdated(): void;
|
312
|
-
private updateData;
|
313
|
-
unstable_synchronizer: () => null;
|
314
|
-
}
|
315
|
-
|
316
|
-
declare const useVercelRSCRuntime: <T extends WeakKey>(adapter: VercelRSCAdapter<T>) => VercelRSCRuntime<T>;
|
317
|
-
|
318
|
-
declare const getVercelRSCMessage: <T>(message: ThreadMessage) => T | undefined;
|
319
|
-
|
320
|
-
type VercelHelpers = UseChatHelpers | UseAssistantHelpers;
|
321
|
-
|
322
|
-
declare class VercelAIRuntime implements AssistantRuntime, ReactThreadRuntime {
|
323
|
-
vercel: VercelHelpers;
|
324
|
-
private _subscriptions;
|
325
|
-
private repository;
|
326
|
-
private assistantOptimisticId;
|
327
|
-
private useVercel;
|
328
|
-
messages: ThreadMessage[];
|
329
|
-
isRunning: boolean;
|
330
|
-
constructor(vercel: VercelHelpers);
|
331
|
-
getBranches(messageId: string): string[];
|
332
|
-
switchToBranch(branchId: string): void;
|
333
|
-
append(message: AppendMessage): Promise<void>;
|
334
|
-
startRun(parentId: string | null): Promise<void>;
|
335
|
-
cancelRun(): void;
|
336
|
-
subscribe(callback: () => void): () => void;
|
337
|
-
private updateVercelMessages;
|
338
|
-
onVercelUpdated(): void;
|
339
|
-
private updateData;
|
340
|
-
unstable_synchronizer: () => null;
|
341
|
-
}
|
342
|
-
|
343
|
-
declare const useVercelUseChatRuntime: (chatHelpers: UseChatHelpers) => VercelAIRuntime;
|
344
|
-
|
345
|
-
declare const useVercelUseAssistantRuntime: (assistantHelpers: UseAssistantHelpers) => VercelAIRuntime;
|
346
|
-
|
347
|
-
declare const getVercelAIMessage: (message: ThreadMessage) => Message[] | undefined;
|
348
|
-
|
349
|
-
type ContentPartState = Readonly<{
|
350
|
-
status: "in_progress" | "done" | "error";
|
351
|
-
part: ThreadMessage["content"][number];
|
352
|
-
}>;
|
353
|
-
|
354
|
-
type MessageState = Readonly<{
|
355
|
-
message: Readonly<ThreadMessage>;
|
356
|
-
parentId: string | null;
|
357
|
-
branches: readonly string[];
|
358
|
-
isLast: boolean;
|
359
|
-
inProgressIndicator: ReactNode | null;
|
360
|
-
setInProgressIndicator: (value: ReactNode | null) => void;
|
361
|
-
isCopied: boolean;
|
362
|
-
setIsCopied: (value: boolean) => void;
|
363
|
-
isHovering: boolean;
|
364
|
-
setIsHovering: (value: boolean) => void;
|
365
|
-
}>;
|
366
|
-
|
367
|
-
type BaseComposerState = Readonly<{
|
368
|
-
value: string;
|
369
|
-
setValue: (value: string) => void;
|
370
|
-
}>;
|
371
|
-
|
372
|
-
type EditComposerState = BaseComposerState & Readonly<{
|
373
|
-
isEditing: boolean;
|
374
|
-
edit: () => void;
|
375
|
-
send: () => void;
|
376
|
-
cancel: () => boolean;
|
377
|
-
}>;
|
378
|
-
|
379
|
-
type ThreadState = {
|
380
|
-
messages: readonly ThreadMessage[];
|
381
|
-
isRunning: boolean;
|
382
|
-
getBranches: (messageId: string) => readonly string[];
|
383
|
-
switchToBranch: (branchId: string) => void;
|
384
|
-
append: (message: AppendMessage) => void;
|
385
|
-
startRun: (parentId: string | null) => void;
|
386
|
-
cancelRun: () => void;
|
387
|
-
};
|
388
|
-
|
389
|
-
type ComposerState = BaseComposerState & Readonly<{
|
390
|
-
isEditing: true;
|
391
|
-
send: () => void;
|
392
|
-
cancel: () => boolean;
|
393
|
-
}>;
|
394
|
-
|
395
|
-
type ThreadViewportState = {
|
396
|
-
isAtBottom: boolean;
|
397
|
-
scrollToBottom: () => void;
|
398
|
-
onScrollToBottom: (callback: () => void) => () => void;
|
399
|
-
};
|
400
|
-
|
401
|
-
type ThreadContextValue = {
|
402
|
-
useThread: UseBoundStore<StoreApi<ThreadState>>;
|
403
|
-
useComposer: UseBoundStore<StoreApi<ComposerState>>;
|
404
|
-
useViewport: UseBoundStore<StoreApi<ThreadViewportState>>;
|
405
|
-
};
|
406
|
-
declare const useThreadContext: () => ThreadContextValue;
|
407
|
-
|
408
|
-
type ComposerContextValue = {
|
409
|
-
useComposer: UseBoundStore<StoreApi<EditComposerState | ComposerState>>;
|
410
|
-
type: "edit" | "new";
|
411
|
-
};
|
412
|
-
declare const useComposerContext: () => ComposerContextValue;
|
413
|
-
|
414
|
-
type MessageContextValue = {
|
415
|
-
useMessage: UseBoundStore<StoreApi<MessageState>>;
|
416
|
-
useComposer: UseBoundStore<StoreApi<EditComposerState>>;
|
417
|
-
};
|
418
|
-
declare const useMessageContext: () => MessageContextValue;
|
419
|
-
|
420
|
-
type ContentPartContextValue = {
|
421
|
-
useContentPart: UseBoundStore<StoreApi<ContentPartState>>;
|
422
|
-
};
|
423
|
-
declare const useContentPartContext: () => ContentPartContextValue;
|
424
|
-
|
425
|
-
type Unsubscribe = () => void;
|
426
|
-
type ThreadRuntime = Readonly<ThreadState & {
|
427
|
-
subscribe: (callback: () => void) => Unsubscribe;
|
428
|
-
}>;
|
429
|
-
|
430
|
-
type AssistantRuntime = ThreadRuntime;
|
431
|
-
|
432
239
|
type ReactThreadRuntime = ThreadRuntime & {
|
433
240
|
unstable_synchronizer?: ComponentType;
|
434
241
|
};
|
435
242
|
|
436
|
-
declare class MessageRepository {
|
437
|
-
private messages;
|
438
|
-
private head;
|
439
|
-
private root;
|
440
|
-
private performOp;
|
441
|
-
getMessages(): ThreadMessage[];
|
442
|
-
addOrUpdateMessage(parentId: string | null, message: ThreadMessage): void;
|
443
|
-
appendOptimisticMessage(parentId: string | null, message: Omit<ThreadMessage, "id" | "createdAt">): string;
|
444
|
-
deleteMessage(messageId: string, replacementId?: string | null | undefined): void;
|
445
|
-
getBranches(messageId: string): string[];
|
446
|
-
switchToBranch(messageId: string): void;
|
447
|
-
resetHead(messageId: string | null): void;
|
448
|
-
}
|
449
|
-
|
450
|
-
/**
|
451
|
-
* @deprecated Will be removed in 0.1.0.
|
452
|
-
*/
|
453
|
-
type VercelAIAssistantProviderProps = PropsWithChildren<{
|
454
|
-
chat: UseChatHelpers;
|
455
|
-
} | {
|
456
|
-
assistant: UseAssistantHelpers;
|
457
|
-
}>;
|
458
|
-
/**
|
459
|
-
* @deprecated `const runtime = useVercelUseChatRuntime(chat)` and `<AssistantRuntimeProvider runtime={...} />`. Will be removed in 0.1.0.
|
460
|
-
*/
|
461
|
-
declare const VercelAIAssistantProvider: FC<VercelAIAssistantProviderProps>;
|
462
|
-
|
463
|
-
/**
|
464
|
-
* @deprecated Will be removed in 0.1.0.
|
465
|
-
*/
|
466
|
-
type VercelRSCAssistantProviderProps<T> = PropsWithChildren<VercelRSCAdapter<T>>;
|
467
|
-
/**
|
468
|
-
* @deprecated Replaced with `const runtime = useVercelRSCRuntime({ messages, append })` and `<AssistantRuntimeProvider runtime={runtime} />`. Will be removed in 0.1.0.
|
469
|
-
*/
|
470
|
-
declare const VercelRSCAssistantProvider: <T extends WeakKey = VercelRSCMessage>({ children, ...adapter }: VercelRSCAssistantProviderProps<T>) => react_jsx_runtime.JSX.Element;
|
471
|
-
|
472
|
-
type ChatModelRunResult = {
|
473
|
-
content: AssistantContentPart[];
|
474
|
-
};
|
475
|
-
type ChatModelRunOptions = {
|
476
|
-
messages: ThreadMessage[];
|
477
|
-
abortSignal: AbortSignal;
|
478
|
-
onUpdate: (result: ChatModelRunResult) => void;
|
479
|
-
};
|
480
|
-
type ChatModelAdapter = {
|
481
|
-
run: (options: ChatModelRunOptions) => Promise<ChatModelRunResult>;
|
482
|
-
};
|
483
|
-
|
484
|
-
declare class LocalRuntime implements AssistantRuntime {
|
485
|
-
adapter: ChatModelAdapter;
|
486
|
-
private _subscriptions;
|
487
|
-
private abortController;
|
488
|
-
private repository;
|
489
|
-
get messages(): ThreadMessage[];
|
490
|
-
get isRunning(): boolean;
|
491
|
-
constructor(adapter: ChatModelAdapter);
|
492
|
-
getBranches(messageId: string): string[];
|
493
|
-
switchToBranch(branchId: string): void;
|
494
|
-
append(message: AppendMessage): Promise<void>;
|
495
|
-
startRun(parentId: string | null): Promise<void>;
|
496
|
-
cancelRun(): void;
|
497
|
-
private notifySubscribers;
|
498
|
-
subscribe(callback: () => void): Unsubscribe;
|
499
|
-
}
|
500
|
-
|
501
|
-
declare const useLocalRuntime: (adapter: ChatModelAdapter) => LocalRuntime;
|
502
|
-
|
503
243
|
type AssistantRuntimeProviderProps = {
|
504
244
|
runtime: AssistantRuntime;
|
505
245
|
};
|
506
246
|
declare const AssistantRuntimeProvider: react.NamedExoticComponent<PropsWithChildren<AssistantRuntimeProviderProps>>;
|
507
247
|
|
508
|
-
export { index$1 as ActionBarPrimitive,
|
248
|
+
export { index$1 as ActionBarPrimitive, AssistantRuntime, AssistantRuntimeProvider, index$2 as BranchPickerPrimitive, index$4 as ComposerPrimitive, index as ContentPartPrimitive, index$3 as MessagePrimitive, type ReactThreadRuntime, TextContentPart, index$5 as ThreadPrimitive, ThreadRuntime, useBeginMessageEdit, useCopyMessage, useGoToNextBranch, useGoToPreviousBranch, useReloadMessage };
|