@assistant-ui/react-ag-ui 0.0.32 → 0.0.33
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAgUiRuntime.d.ts","names":[],"sources":["../src/useAgUiRuntime.ts"],"mappings":";;;;
|
|
1
|
+
{"version":3,"file":"useAgUiRuntime.d.ts","names":[],"sources":["../src/useAgUiRuntime.ts"],"mappings":";;;;KAuBY,oBAAA,GAAuB,gBAAA;EACjC,6BAAA,iBAA8C,aAAA;EAC9C,iCAAA,GACE,SAAA,WAAoB,eAAA,OACjB,OAAA;AAAA;AAAA,iBAGS,cAAA,CACd,OAAA,EAAS,qBAAA,GACR,oBAAoB"}
|
package/dist/useAgUiRuntime.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { makeLogger } from "./runtime/logger.js";
|
|
3
3
|
import { AgUiThreadRuntimeCore } from "./runtime/AgUiThreadRuntimeCore.js";
|
|
4
4
|
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
5
|
-
import { useExternalStoreRuntime, useRuntimeAdapters
|
|
5
|
+
import { useExternalStoreRuntime, useRuntimeAdapters } from "@assistant-ui/core/react";
|
|
6
6
|
//#region src/useAgUiRuntime.ts
|
|
7
7
|
function useAgUiRuntime(options) {
|
|
8
8
|
const logger = useMemo(() => makeLogger(options.logger), [options.logger]);
|
|
@@ -32,26 +32,16 @@ function useAgUiRuntime(options) {
|
|
|
32
32
|
});
|
|
33
33
|
const [toolStatuses, setToolStatuses] = useState({});
|
|
34
34
|
const hasExecutingTools = Object.values(toolStatuses).some((s) => s?.type === "executing");
|
|
35
|
-
const [runtimeRef] = useState(() => ({ get current() {
|
|
36
|
-
return runtime;
|
|
37
|
-
} }));
|
|
38
|
-
const toolInvocationsRef = useRef({
|
|
39
|
-
reset: () => {},
|
|
40
|
-
abort: () => Promise.resolve(),
|
|
41
|
-
resume: (_toolCallId, _payload) => {}
|
|
42
|
-
});
|
|
43
35
|
const threadList = useMemo(() => {
|
|
44
36
|
if (!threadListAdapter) return void 0;
|
|
45
37
|
const { onSwitchToNewThread, onSwitchToThread } = threadListAdapter;
|
|
46
38
|
return {
|
|
47
39
|
threadId: threadListAdapter.threadId,
|
|
48
40
|
onSwitchToNewThread: onSwitchToNewThread ? async () => {
|
|
49
|
-
toolInvocationsRef.current.reset();
|
|
50
41
|
await onSwitchToNewThread();
|
|
51
42
|
core.applyExternalMessages([]);
|
|
52
43
|
} : void 0,
|
|
53
44
|
onSwitchToThread: onSwitchToThread ? async (threadId) => {
|
|
54
|
-
toolInvocationsRef.current.reset();
|
|
55
45
|
const result = await onSwitchToThread(threadId);
|
|
56
46
|
core.applyExternalMessages(result.messages);
|
|
57
47
|
if (result.state) core.loadExternalState(result.state);
|
|
@@ -70,43 +60,22 @@ function useAgUiRuntime(options) {
|
|
|
70
60
|
runtimeAdapters,
|
|
71
61
|
threadList
|
|
72
62
|
]);
|
|
73
|
-
toolInvocationsRef.current = useToolInvocations({
|
|
74
|
-
state: {
|
|
75
|
-
messages: core.getMessages(),
|
|
76
|
-
isRunning: core.isRunning() || hasExecutingTools
|
|
77
|
-
},
|
|
78
|
-
getTools: () => runtimeRef.current.thread.getModelContext().tools,
|
|
79
|
-
onResult: (command) => {
|
|
80
|
-
if (command.type === "add-tool-result") {
|
|
81
|
-
const messageId = core.findMessageIdForToolCall(command.toolCallId);
|
|
82
|
-
if (messageId) core.addToolResult({
|
|
83
|
-
messageId,
|
|
84
|
-
toolCallId: command.toolCallId,
|
|
85
|
-
toolName: command.toolName,
|
|
86
|
-
result: command.result,
|
|
87
|
-
isError: command.isError,
|
|
88
|
-
...command.artifact && { artifact: command.artifact }
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
},
|
|
92
|
-
setToolStatuses
|
|
93
|
-
});
|
|
94
63
|
const baseRuntime = useExternalStoreRuntime(useMemo(() => {
|
|
95
64
|
return {
|
|
96
65
|
isLoading: core.isLoading,
|
|
97
66
|
messages: core.getMessages(),
|
|
98
67
|
state: core.getState(),
|
|
99
68
|
isRunning: core.isRunning() || hasExecutingTools,
|
|
69
|
+
unstable_enableToolInvocations: true,
|
|
70
|
+
setToolStatuses,
|
|
100
71
|
onNew: (message) => core.append(message),
|
|
101
72
|
onEdit: (message) => core.edit(message),
|
|
102
73
|
onReload: (parentId, config) => core.reload(parentId, config),
|
|
103
74
|
onCancel: async () => {
|
|
104
75
|
core.cancel();
|
|
105
|
-
await toolInvocationsRef.current.abort();
|
|
106
76
|
},
|
|
107
77
|
onAddToolResult: (options) => core.addToolResult(options),
|
|
108
78
|
onResume: (config) => core.resume(config),
|
|
109
|
-
onResumeToolCall: (options) => toolInvocationsRef.current.resume(options.toolCallId, options.payload),
|
|
110
79
|
setMessages: (messages) => core.applyExternalMessages(messages),
|
|
111
80
|
onImport: (messages) => core.applyExternalMessages(messages),
|
|
112
81
|
onLoadExternalState: (state) => core.loadExternalState(state),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAgUiRuntime.js","names":[],"sources":["../src/useAgUiRuntime.ts"],"sourcesContent":["\"use client\";\n\nimport { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport {\n useExternalStoreRuntime,\n useRuntimeAdapters,\n useToolInvocations,\n} from \"@assistant-ui/core/react\";\nimport type { ToolExecutionStatus } from \"@assistant-ui/core/react\";\nimport type {\n AssistantRuntime,\n AppendMessage,\n ExternalStoreAdapter,\n ThreadMessage,\n} from \"@assistant-ui/core\";\nimport type { ReadonlyJSONValue } from \"assistant-stream/utils\";\nimport { makeLogger } from \"./runtime/logger\";\nimport type {\n AgUiInterrupt,\n AgUiResumeEntry,\n UseAgUiRuntimeOptions,\n} from \"./runtime/types\";\nimport { AgUiThreadRuntimeCore } from \"./runtime/AgUiThreadRuntimeCore\";\n\nexport type AgUiAssistantRuntime = AssistantRuntime & {\n unstable_getPendingInterrupts: () => readonly AgUiInterrupt[];\n unstable_submitInterruptResponses: (\n responses: readonly AgUiResumeEntry[],\n ) => Promise<void>;\n};\n\nexport function useAgUiRuntime(\n options: UseAgUiRuntimeOptions,\n): AgUiAssistantRuntime {\n const logger = useMemo(() => makeLogger(options.logger), [options.logger]);\n const [_version, setVersion] = useState(0);\n const notifyUpdate = useCallback(() => setVersion((v) => v + 1), []);\n const coreRef = useRef<AgUiThreadRuntimeCore | null>(null);\n const runtimeAdapters = useRuntimeAdapters();\n\n const historyAdapter = options.adapters?.history ?? runtimeAdapters?.history;\n const threadListAdapter = options.adapters?.threadList;\n\n if (!coreRef.current) {\n coreRef.current = new AgUiThreadRuntimeCore({\n agent: options.agent,\n logger,\n showThinking: options.showThinking ?? true,\n ...(options.onError && { onError: options.onError }),\n ...(options.onCancel && { onCancel: options.onCancel }),\n ...(historyAdapter && { history: historyAdapter }),\n notifyUpdate,\n });\n }\n\n const core = coreRef.current;\n core.updateOptions({\n agent: options.agent,\n logger,\n showThinking: options.showThinking ?? true,\n ...(options.onError && { onError: options.onError }),\n ...(options.onCancel && { onCancel: options.onCancel }),\n ...(historyAdapter && { history: historyAdapter }),\n });\n\n const [toolStatuses, setToolStatuses] = useState<\n Record<string, ToolExecutionStatus>\n >({});\n\n const hasExecutingTools = Object.values(toolStatuses).some(\n (s) => s?.type === \"executing\",\n );\n\n const [runtimeRef] = useState(() => ({\n get current(): AssistantRuntime {\n return runtime;\n },\n }));\n\n const toolInvocationsRef = useRef({\n reset: () => {},\n abort: (): Promise<void> => Promise.resolve(),\n resume: (_toolCallId: string, _payload: unknown) => {},\n });\n\n const threadList = useMemo(() => {\n if (!threadListAdapter) return undefined;\n\n const { onSwitchToNewThread, onSwitchToThread } = threadListAdapter;\n\n return {\n threadId: threadListAdapter.threadId,\n onSwitchToNewThread: onSwitchToNewThread\n ? async () => {\n toolInvocationsRef.current.reset();\n await onSwitchToNewThread();\n core.applyExternalMessages([]);\n }\n : undefined,\n onSwitchToThread: onSwitchToThread\n ? async (threadId: string) => {\n toolInvocationsRef.current.reset();\n const result = await onSwitchToThread(threadId);\n core.applyExternalMessages(result.messages);\n if (result.state) {\n core.loadExternalState(result.state);\n }\n }\n : undefined,\n };\n }, [threadListAdapter, core]);\n\n const adapters = options.adapters;\n const adapterAdapters = useMemo(\n () => ({\n attachments: adapters?.attachments ?? runtimeAdapters?.attachments,\n speech: adapters?.speech,\n dictation: adapters?.dictation,\n feedback: adapters?.feedback,\n threadList,\n }),\n [adapters, runtimeAdapters, threadList],\n );\n\n const toolInvocations = useToolInvocations({\n state: {\n messages: core.getMessages(),\n isRunning: core.isRunning() || hasExecutingTools,\n },\n getTools: () => runtimeRef.current.thread.getModelContext().tools,\n onResult: (command) => {\n if (command.type === \"add-tool-result\") {\n const messageId = core.findMessageIdForToolCall(command.toolCallId);\n if (messageId) {\n core.addToolResult({\n messageId,\n toolCallId: command.toolCallId,\n toolName: command.toolName,\n result: command.result,\n isError: command.isError,\n ...(command.artifact && { artifact: command.artifact }),\n });\n }\n }\n },\n setToolStatuses,\n });\n toolInvocationsRef.current = toolInvocations;\n\n const store = useMemo(\n () => {\n void _version; // rerender on version change\n\n return {\n isLoading: core.isLoading,\n messages: core.getMessages(),\n state: core.getState(),\n isRunning: core.isRunning() || hasExecutingTools,\n onNew: (message: AppendMessage) => core.append(message),\n onEdit: (message: AppendMessage) => core.edit(message),\n onReload: (parentId: string | null, config: { runConfig?: any }) =>\n core.reload(parentId, config),\n onCancel: async () => {\n core.cancel();\n await toolInvocationsRef.current.abort();\n },\n onAddToolResult: (options) => core.addToolResult(options),\n onResume: (config) => core.resume(config),\n onResumeToolCall: (options) =>\n toolInvocationsRef.current.resume(\n options.toolCallId,\n options.payload,\n ),\n setMessages: (messages: readonly ThreadMessage[]) =>\n core.applyExternalMessages(messages),\n onImport: (messages: readonly ThreadMessage[]) =>\n core.applyExternalMessages(messages),\n onLoadExternalState: (state: ReadonlyJSONValue) =>\n core.loadExternalState(state),\n adapters: adapterAdapters,\n } satisfies ExternalStoreAdapter<ThreadMessage>;\n },\n // _version is intentionally included to trigger re-computation when core state changes via notifyUpdate\n // toolInvocations intentionally excluded: abort/resume use refs internally and work with stale captures\n [adapterAdapters, core, _version, hasExecutingTools],\n );\n\n const baseRuntime = useExternalStoreRuntime(store);\n\n const runtime = useMemo<AgUiAssistantRuntime>(() => {\n const wrapper = Object.create(baseRuntime) as AgUiAssistantRuntime;\n wrapper.unstable_getPendingInterrupts = () =>\n core.getPendingInterrupts()?.interrupts ?? [];\n wrapper.unstable_submitInterruptResponses = (responses) =>\n core.submitInterruptResponses(responses);\n return wrapper;\n }, [baseRuntime, core]);\n\n useEffect(() => {\n core.attachRuntime(runtime);\n return () => {\n core.detachRuntime();\n };\n }, [core, runtime]);\n\n useEffect(() => {\n core.__internal_load();\n }, [core]);\n\n return runtime;\n}\n"],"mappings":";;;;;;AA+BA,SAAgB,eACd,SACsB;CACtB,MAAM,SAAS,cAAc,WAAW,QAAQ,MAAM,GAAG,CAAC,QAAQ,MAAM,CAAC;CACzE,MAAM,CAAC,UAAU,cAAc,SAAS,CAAC;CACzC,MAAM,eAAe,kBAAkB,YAAY,MAAM,IAAI,CAAC,GAAG,CAAC,CAAC;CACnE,MAAM,UAAU,OAAqC,IAAI;CACzD,MAAM,kBAAkB,mBAAmB;CAE3C,MAAM,iBAAiB,QAAQ,UAAU,WAAW,iBAAiB;CACrE,MAAM,oBAAoB,QAAQ,UAAU;CAE5C,IAAI,CAAC,QAAQ,SACX,QAAQ,UAAU,IAAI,sBAAsB;EAC1C,OAAO,QAAQ;EACf;EACA,cAAc,QAAQ,gBAAgB;EACtC,GAAI,QAAQ,WAAW,EAAE,SAAS,QAAQ,QAAQ;EAClD,GAAI,QAAQ,YAAY,EAAE,UAAU,QAAQ,SAAS;EACrD,GAAI,kBAAkB,EAAE,SAAS,eAAe;EAChD;CACF,CAAC;CAGH,MAAM,OAAO,QAAQ;CACrB,KAAK,cAAc;EACjB,OAAO,QAAQ;EACf;EACA,cAAc,QAAQ,gBAAg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EACzC,OAAO;CACT,GAAG,CAAC,aAAa,IAAI,CAAC;CAEtB,gBAAgB;EACd,KAAK,cAAc,OAAO;EAC1B,aAAa;GACX,KAAK,cAAc;EACrB;CACF,GAAG,CAAC,MAAM,OAAO,CAAC;CAElB,gBAAgB;EACd,KAAK,gBAAgB;CACvB,GAAG,CAAC,IAAI,CAAC;CAET,OAAO;AACT"}
|
|
1
|
+
{"version":3,"file":"useAgUiRuntime.js","names":[],"sources":["../src/useAgUiRuntime.ts"],"sourcesContent":["\"use client\";\n\nimport { useCallback, useEffect, useMemo, useRef, useState } from \"react\";\nimport {\n useExternalStoreRuntime,\n useRuntimeAdapters,\n} from \"@assistant-ui/core/react\";\nimport type { ToolExecutionStatus } from \"@assistant-ui/core\";\nimport type {\n AssistantRuntime,\n AppendMessage,\n ExternalStoreAdapter,\n ThreadMessage,\n} from \"@assistant-ui/core\";\nimport type { ReadonlyJSONValue } from \"assistant-stream/utils\";\nimport { makeLogger } from \"./runtime/logger\";\nimport type {\n AgUiInterrupt,\n AgUiResumeEntry,\n UseAgUiRuntimeOptions,\n} from \"./runtime/types\";\nimport { AgUiThreadRuntimeCore } from \"./runtime/AgUiThreadRuntimeCore\";\n\nexport type AgUiAssistantRuntime = AssistantRuntime & {\n unstable_getPendingInterrupts: () => readonly AgUiInterrupt[];\n unstable_submitInterruptResponses: (\n responses: readonly AgUiResumeEntry[],\n ) => Promise<void>;\n};\n\nexport function useAgUiRuntime(\n options: UseAgUiRuntimeOptions,\n): AgUiAssistantRuntime {\n const logger = useMemo(() => makeLogger(options.logger), [options.logger]);\n const [_version, setVersion] = useState(0);\n const notifyUpdate = useCallback(() => setVersion((v) => v + 1), []);\n const coreRef = useRef<AgUiThreadRuntimeCore | null>(null);\n const runtimeAdapters = useRuntimeAdapters();\n\n const historyAdapter = options.adapters?.history ?? runtimeAdapters?.history;\n const threadListAdapter = options.adapters?.threadList;\n\n if (!coreRef.current) {\n coreRef.current = new AgUiThreadRuntimeCore({\n agent: options.agent,\n logger,\n showThinking: options.showThinking ?? true,\n ...(options.onError && { onError: options.onError }),\n ...(options.onCancel && { onCancel: options.onCancel }),\n ...(historyAdapter && { history: historyAdapter }),\n notifyUpdate,\n });\n }\n\n const core = coreRef.current;\n core.updateOptions({\n agent: options.agent,\n logger,\n showThinking: options.showThinking ?? true,\n ...(options.onError && { onError: options.onError }),\n ...(options.onCancel && { onCancel: options.onCancel }),\n ...(historyAdapter && { history: historyAdapter }),\n });\n\n const [toolStatuses, setToolStatuses] = useState<\n Record<string, ToolExecutionStatus>\n >({});\n\n const hasExecutingTools = Object.values(toolStatuses).some(\n (s) => s?.type === \"executing\",\n );\n\n const threadList = useMemo(() => {\n if (!threadListAdapter) return undefined;\n\n const { onSwitchToNewThread, onSwitchToThread } = threadListAdapter;\n\n return {\n threadId: threadListAdapter.threadId,\n onSwitchToNewThread: onSwitchToNewThread\n ? async () => {\n await onSwitchToNewThread();\n core.applyExternalMessages([]);\n }\n : undefined,\n onSwitchToThread: onSwitchToThread\n ? async (threadId: string) => {\n const result = await onSwitchToThread(threadId);\n core.applyExternalMessages(result.messages);\n if (result.state) {\n core.loadExternalState(result.state);\n }\n }\n : undefined,\n };\n }, [threadListAdapter, core]);\n\n const adapters = options.adapters;\n const adapterAdapters = useMemo(\n () => ({\n attachments: adapters?.attachments ?? runtimeAdapters?.attachments,\n speech: adapters?.speech,\n dictation: adapters?.dictation,\n feedback: adapters?.feedback,\n threadList,\n }),\n [adapters, runtimeAdapters, threadList],\n );\n\n const store = useMemo(\n () => {\n void _version; // rerender on version change\n\n return {\n isLoading: core.isLoading,\n messages: core.getMessages(),\n state: core.getState(),\n isRunning: core.isRunning() || hasExecutingTools,\n unstable_enableToolInvocations: true,\n setToolStatuses,\n onNew: (message: AppendMessage) => core.append(message),\n onEdit: (message: AppendMessage) => core.edit(message),\n onReload: (parentId: string | null, config: { runConfig?: any }) =>\n core.reload(parentId, config),\n onCancel: async () => {\n core.cancel();\n },\n onAddToolResult: (options) => core.addToolResult(options),\n onResume: (config) => core.resume(config),\n setMessages: (messages: readonly ThreadMessage[]) =>\n core.applyExternalMessages(messages),\n onImport: (messages: readonly ThreadMessage[]) =>\n core.applyExternalMessages(messages),\n onLoadExternalState: (state: ReadonlyJSONValue) =>\n core.loadExternalState(state),\n adapters: adapterAdapters,\n } satisfies ExternalStoreAdapter<ThreadMessage>;\n },\n // _version is intentionally included to trigger re-computation when core state changes via notifyUpdate\n // toolInvocations intentionally excluded: abort/resume use refs internally and work with stale captures\n [adapterAdapters, core, _version, hasExecutingTools],\n );\n\n const baseRuntime = useExternalStoreRuntime(store);\n\n const runtime = useMemo<AgUiAssistantRuntime>(() => {\n const wrapper = Object.create(baseRuntime) as AgUiAssistantRuntime;\n wrapper.unstable_getPendingInterrupts = () =>\n core.getPendingInterrupts()?.interrupts ?? [];\n wrapper.unstable_submitInterruptResponses = (responses) =>\n core.submitInterruptResponses(responses);\n return wrapper;\n }, [baseRuntime, core]);\n\n useEffect(() => {\n core.attachRuntime(runtime);\n return () => {\n core.detachRuntime();\n };\n }, [core, runtime]);\n\n useEffect(() => {\n core.__internal_load();\n }, [core]);\n\n return runtime;\n}\n"],"mappings":";;;;;;AA8BA,SAAgB,eACd,SACsB;CACtB,MAAM,SAAS,cAAc,WAAW,QAAQ,MAAM,GAAG,CAAC,QAAQ,MAAM,CAAC;CACzE,MAAM,CAAC,UAAU,cAAc,SAAS,CAAC;CACzC,MAAM,eAAe,kBAAkB,YAAY,MAAM,IAAI,CAAC,GAAG,CAAC,CAAC;CACnE,MAAM,UAAU,OAAqC,IAAI;CACzD,MAAM,kBAAkB,mBAAmB;CAE3C,MAAM,iBAAiB,QAAQ,UAAU,WAAW,iBAAiB;CACrE,MAAM,oBAAoB,QAAQ,UAAU;CAE5C,IAAI,CAAC,QAAQ,SACX,QAAQ,UAAU,IAAI,sBAAsB;EAC1C,OAAO,QAAQ;EACf;EACA,cAAc,QAAQ,gBAAgB;EACtC,GAAI,QAAQ,WAAW,EAAE,SAAS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AAK,SAAS;GACrB,WAAW,KAAK,UAAU,KAAK;GAC/B,gCAAgC;GAChC;GACA,QAAQ,YAA2B,KAAK,OAAO,OAAO;GACtD,SAAS,YAA2B,KAAK,KAAK,OAAO;GACrD,WAAW,UAAyB,WAClC,KAAK,OAAO,UAAU,MAAM;GAC9B,UAAU,YAAY;IACpB,KAAK,OAAO;GACd;GACA,kBAAkB,YAAY,KAAK,cAAc,OAAO;GACxD,WAAW,WAAW,KAAK,OAAO,MAAM;GACxC,cAAc,aACZ,KAAK,sBAAsB,QAAQ;GACrC,WAAW,aACT,KAAK,sBAAsB,QAAQ;GACrC,sBAAsB,UACpB,KAAK,kBAAkB,KAAK;GAC9B,UAAU;EACZ;CACF,GAGA;EAAC;EAAiB;EAAM;EAAU;CAAiB,CAGL,CAAC;CAEjD,MAAM,UAAU,cAAoC;EAClD,MAAM,UAAU,OAAO,OAAO,WAAW;EACzC,QAAQ,sCACN,KAAK,qBAAqB,GAAG,cAAc,CAAC;EAC9C,QAAQ,qCAAqC,cAC3C,KAAK,yBAAyB,SAAS;EACzC,OAAO;CACT,GAAG,CAAC,aAAa,IAAI,CAAC;CAEtB,gBAAgB;EACd,KAAK,cAAc,OAAO;EAC1B,aAAa;GACX,KAAK,cAAc;EACrB;CACF,GAAG,CAAC,MAAM,OAAO,CAAC;CAElB,gBAAgB;EACd,KAAK,gBAAgB;CACvB,GAAG,CAAC,IAAI,CAAC;CAET,OAAO;AACT"}
|
package/package.json
CHANGED
package/src/useAgUiRuntime.ts
CHANGED
|
@@ -4,9 +4,8 @@ import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
|
4
4
|
import {
|
|
5
5
|
useExternalStoreRuntime,
|
|
6
6
|
useRuntimeAdapters,
|
|
7
|
-
useToolInvocations,
|
|
8
7
|
} from "@assistant-ui/core/react";
|
|
9
|
-
import type { ToolExecutionStatus } from "@assistant-ui/core
|
|
8
|
+
import type { ToolExecutionStatus } from "@assistant-ui/core";
|
|
10
9
|
import type {
|
|
11
10
|
AssistantRuntime,
|
|
12
11
|
AppendMessage,
|
|
@@ -71,18 +70,6 @@ export function useAgUiRuntime(
|
|
|
71
70
|
(s) => s?.type === "executing",
|
|
72
71
|
);
|
|
73
72
|
|
|
74
|
-
const [runtimeRef] = useState(() => ({
|
|
75
|
-
get current(): AssistantRuntime {
|
|
76
|
-
return runtime;
|
|
77
|
-
},
|
|
78
|
-
}));
|
|
79
|
-
|
|
80
|
-
const toolInvocationsRef = useRef({
|
|
81
|
-
reset: () => {},
|
|
82
|
-
abort: (): Promise<void> => Promise.resolve(),
|
|
83
|
-
resume: (_toolCallId: string, _payload: unknown) => {},
|
|
84
|
-
});
|
|
85
|
-
|
|
86
73
|
const threadList = useMemo(() => {
|
|
87
74
|
if (!threadListAdapter) return undefined;
|
|
88
75
|
|
|
@@ -92,14 +79,12 @@ export function useAgUiRuntime(
|
|
|
92
79
|
threadId: threadListAdapter.threadId,
|
|
93
80
|
onSwitchToNewThread: onSwitchToNewThread
|
|
94
81
|
? async () => {
|
|
95
|
-
toolInvocationsRef.current.reset();
|
|
96
82
|
await onSwitchToNewThread();
|
|
97
83
|
core.applyExternalMessages([]);
|
|
98
84
|
}
|
|
99
85
|
: undefined,
|
|
100
86
|
onSwitchToThread: onSwitchToThread
|
|
101
87
|
? async (threadId: string) => {
|
|
102
|
-
toolInvocationsRef.current.reset();
|
|
103
88
|
const result = await onSwitchToThread(threadId);
|
|
104
89
|
core.applyExternalMessages(result.messages);
|
|
105
90
|
if (result.state) {
|
|
@@ -122,31 +107,6 @@ export function useAgUiRuntime(
|
|
|
122
107
|
[adapters, runtimeAdapters, threadList],
|
|
123
108
|
);
|
|
124
109
|
|
|
125
|
-
const toolInvocations = useToolInvocations({
|
|
126
|
-
state: {
|
|
127
|
-
messages: core.getMessages(),
|
|
128
|
-
isRunning: core.isRunning() || hasExecutingTools,
|
|
129
|
-
},
|
|
130
|
-
getTools: () => runtimeRef.current.thread.getModelContext().tools,
|
|
131
|
-
onResult: (command) => {
|
|
132
|
-
if (command.type === "add-tool-result") {
|
|
133
|
-
const messageId = core.findMessageIdForToolCall(command.toolCallId);
|
|
134
|
-
if (messageId) {
|
|
135
|
-
core.addToolResult({
|
|
136
|
-
messageId,
|
|
137
|
-
toolCallId: command.toolCallId,
|
|
138
|
-
toolName: command.toolName,
|
|
139
|
-
result: command.result,
|
|
140
|
-
isError: command.isError,
|
|
141
|
-
...(command.artifact && { artifact: command.artifact }),
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
},
|
|
146
|
-
setToolStatuses,
|
|
147
|
-
});
|
|
148
|
-
toolInvocationsRef.current = toolInvocations;
|
|
149
|
-
|
|
150
110
|
const store = useMemo(
|
|
151
111
|
() => {
|
|
152
112
|
void _version; // rerender on version change
|
|
@@ -156,21 +116,17 @@ export function useAgUiRuntime(
|
|
|
156
116
|
messages: core.getMessages(),
|
|
157
117
|
state: core.getState(),
|
|
158
118
|
isRunning: core.isRunning() || hasExecutingTools,
|
|
119
|
+
unstable_enableToolInvocations: true,
|
|
120
|
+
setToolStatuses,
|
|
159
121
|
onNew: (message: AppendMessage) => core.append(message),
|
|
160
122
|
onEdit: (message: AppendMessage) => core.edit(message),
|
|
161
123
|
onReload: (parentId: string | null, config: { runConfig?: any }) =>
|
|
162
124
|
core.reload(parentId, config),
|
|
163
125
|
onCancel: async () => {
|
|
164
126
|
core.cancel();
|
|
165
|
-
await toolInvocationsRef.current.abort();
|
|
166
127
|
},
|
|
167
128
|
onAddToolResult: (options) => core.addToolResult(options),
|
|
168
129
|
onResume: (config) => core.resume(config),
|
|
169
|
-
onResumeToolCall: (options) =>
|
|
170
|
-
toolInvocationsRef.current.resume(
|
|
171
|
-
options.toolCallId,
|
|
172
|
-
options.payload,
|
|
173
|
-
),
|
|
174
130
|
setMessages: (messages: readonly ThreadMessage[]) =>
|
|
175
131
|
core.applyExternalMessages(messages),
|
|
176
132
|
onImport: (messages: readonly ThreadMessage[]) =>
|