@assistant-ui/react-ag-ui 0.0.30 → 0.0.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +3 -4
- package/dist/index.js +2 -2
- package/dist/runtime/AgUiThreadRuntimeCore.d.ts +85 -82
- package/dist/runtime/AgUiThreadRuntimeCore.d.ts.map +1 -1
- package/dist/runtime/AgUiThreadRuntimeCore.js +542 -627
- package/dist/runtime/AgUiThreadRuntimeCore.js.map +1 -1
- package/dist/runtime/adapter/conversions.d.ts +39 -36
- package/dist/runtime/adapter/conversions.d.ts.map +1 -1
- package/dist/runtime/adapter/conversions.js +294 -390
- package/dist/runtime/adapter/conversions.js.map +1 -1
- package/dist/runtime/adapter/run-aggregator.d.ts +54 -47
- package/dist/runtime/adapter/run-aggregator.d.ts.map +1 -1
- package/dist/runtime/adapter/run-aggregator.js +367 -364
- package/dist/runtime/adapter/run-aggregator.js.map +1 -1
- package/dist/runtime/adapter/subscriber.d.ts +94 -91
- package/dist/runtime/adapter/subscriber.d.ts.map +1 -1
- package/dist/runtime/adapter/subscriber.js +77 -86
- package/dist/runtime/adapter/subscriber.js.map +1 -1
- package/dist/runtime/event-parser.d.ts +9 -5
- package/dist/runtime/event-parser.d.ts.map +1 -1
- package/dist/runtime/event-parser.js +172 -177
- package/dist/runtime/event-parser.js.map +1 -1
- package/dist/runtime/logger.d.ts +7 -4
- package/dist/runtime/logger.d.ts.map +1 -1
- package/dist/runtime/logger.js +7 -3
- package/dist/runtime/logger.js.map +1 -1
- package/dist/runtime/types.d.ts +121 -117
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/runtime/types.js +0 -2
- package/dist/useAgUiRuntime.d.ts +10 -6
- package/dist/useAgUiRuntime.d.ts.map +1 -1
- package/dist/useAgUiRuntime.js +136 -142
- package/dist/useAgUiRuntime.js.map +1 -1
- package/package.json +9 -8
- package/src/runtime/AgUiThreadRuntimeCore.ts +1 -0
- package/src/runtime/adapter/run-aggregator.ts +111 -45
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/runtime/types.js.map +0 -1
package/dist/runtime/types.d.ts
CHANGED
|
@@ -1,156 +1,160 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { Logger } from "./logger.js";
|
|
2
|
+
import { AttachmentAdapter, DictationAdapter, FeedbackAdapter, SpeechSynthesisAdapter, ThreadHistoryAdapter, ThreadMessage } from "@assistant-ui/core";
|
|
3
|
+
import { HttpAgent } from "@ag-ui/client";
|
|
4
|
+
import { ReadonlyJSONValue } from "assistant-stream/utils";
|
|
5
|
+
|
|
6
|
+
//#region src/runtime/types.d.ts
|
|
5
7
|
/**
|
|
6
8
|
* @experimental This API is still under active development and might change without notice.
|
|
7
9
|
*/
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
10
|
+
type UseAgUiThreadListAdapter = {
|
|
11
|
+
threadId?: string | undefined;
|
|
12
|
+
onSwitchToNewThread?: (() => Promise<void> | void) | undefined;
|
|
13
|
+
onSwitchToThread?: ((threadId: string) => Promise<{
|
|
14
|
+
messages: readonly ThreadMessage[];
|
|
15
|
+
state?: ReadonlyJSONValue;
|
|
16
|
+
}> | {
|
|
17
|
+
messages: readonly ThreadMessage[];
|
|
18
|
+
state?: ReadonlyJSONValue;
|
|
19
|
+
}) | undefined;
|
|
18
20
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
21
|
+
type UseAgUiRuntimeAdapters = {
|
|
22
|
+
attachments?: AttachmentAdapter;
|
|
23
|
+
speech?: SpeechSynthesisAdapter;
|
|
24
|
+
dictation?: DictationAdapter;
|
|
25
|
+
feedback?: FeedbackAdapter;
|
|
26
|
+
history?: ThreadHistoryAdapter;
|
|
27
|
+
/**
|
|
28
|
+
* @experimental This API is still under active development and might change without notice.
|
|
29
|
+
*/
|
|
30
|
+
threadList?: UseAgUiThreadListAdapter;
|
|
29
31
|
};
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
type UseAgUiRuntimeOptions = {
|
|
33
|
+
agent: HttpAgent;
|
|
34
|
+
logger?: Partial<Logger>;
|
|
35
|
+
showThinking?: boolean;
|
|
36
|
+
onError?: (e: Error) => void;
|
|
37
|
+
onCancel?: () => void;
|
|
38
|
+
adapters?: UseAgUiRuntimeAdapters;
|
|
37
39
|
};
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
40
|
+
type AgUiInterruptReason = "tool_call" | "input_required" | "confirmation" | (string & {});
|
|
41
|
+
type AgUiInterrupt = {
|
|
42
|
+
id: string;
|
|
43
|
+
reason: AgUiInterruptReason;
|
|
44
|
+
message?: string;
|
|
45
|
+
toolCallId?: string;
|
|
46
|
+
responseSchema?: Record<string, unknown>;
|
|
47
|
+
expiresAt?: string;
|
|
48
|
+
metadata?: Record<string, unknown>;
|
|
47
49
|
};
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
50
|
+
type AgUiResumeEntry = {
|
|
51
|
+
interruptId: string;
|
|
52
|
+
status: "resolved" | "cancelled";
|
|
53
|
+
payload?: unknown;
|
|
52
54
|
};
|
|
53
|
-
|
|
54
|
-
|
|
55
|
+
type AgUiRunFinishedOutcome = {
|
|
56
|
+
type: "success";
|
|
55
57
|
} | {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
+
type: "interrupt";
|
|
59
|
+
interrupts: AgUiInterrupt[];
|
|
58
60
|
};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
61
|
+
type AgUiEvent = {
|
|
62
|
+
type: "RUN_STARTED";
|
|
63
|
+
runId: string;
|
|
62
64
|
} | {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
65
|
+
type: "RUN_FINISHED";
|
|
66
|
+
runId: string;
|
|
67
|
+
outcome?: AgUiRunFinishedOutcome;
|
|
66
68
|
} | {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
+
type: "RUN_CANCELLED";
|
|
70
|
+
runId?: string;
|
|
69
71
|
} | {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
72
|
+
type: "RUN_ERROR";
|
|
73
|
+
message?: string;
|
|
74
|
+
code?: string;
|
|
73
75
|
} | {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
+
type: "TEXT_MESSAGE_START";
|
|
77
|
+
messageId?: string;
|
|
76
78
|
} | {
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
79
|
+
type: "TEXT_MESSAGE_CONTENT";
|
|
80
|
+
messageId?: string;
|
|
81
|
+
delta: string;
|
|
80
82
|
} | {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
+
type: "TEXT_MESSAGE_END";
|
|
84
|
+
messageId?: string;
|
|
83
85
|
} | {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
+
type: "TEXT_MESSAGE_CHUNK";
|
|
87
|
+
delta: string;
|
|
86
88
|
} | {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
+
type: "THINKING_START";
|
|
90
|
+
title?: string;
|
|
89
91
|
} | {
|
|
90
|
-
|
|
92
|
+
type: "THINKING_TEXT_MESSAGE_START";
|
|
91
93
|
} | {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
+
type: "THINKING_TEXT_MESSAGE_CONTENT";
|
|
95
|
+
delta: string;
|
|
94
96
|
} | {
|
|
95
|
-
|
|
97
|
+
type: "THINKING_TEXT_MESSAGE_END";
|
|
96
98
|
} | {
|
|
97
|
-
|
|
99
|
+
type: "THINKING_END";
|
|
98
100
|
} | {
|
|
99
|
-
|
|
100
|
-
|
|
101
|
+
type: "REASONING_START";
|
|
102
|
+
messageId?: string;
|
|
101
103
|
} | {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
+
type: "REASONING_MESSAGE_START";
|
|
105
|
+
messageId?: string;
|
|
104
106
|
} | {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
107
|
+
type: "REASONING_MESSAGE_CONTENT";
|
|
108
|
+
messageId?: string;
|
|
109
|
+
delta: string;
|
|
108
110
|
} | {
|
|
109
|
-
|
|
110
|
-
|
|
111
|
+
type: "REASONING_MESSAGE_END";
|
|
112
|
+
messageId?: string;
|
|
111
113
|
} | {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
+
type: "REASONING_END";
|
|
115
|
+
messageId?: string;
|
|
114
116
|
} | {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
117
|
+
type: "TOOL_CALL_START";
|
|
118
|
+
toolCallId: string;
|
|
119
|
+
toolCallName?: string;
|
|
120
|
+
parentMessageId?: string;
|
|
119
121
|
} | {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
122
|
+
type: "TOOL_CALL_ARGS";
|
|
123
|
+
toolCallId: string;
|
|
124
|
+
delta: string;
|
|
123
125
|
} | {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
+
type: "TOOL_CALL_END";
|
|
127
|
+
toolCallId: string;
|
|
126
128
|
} | {
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
129
|
+
type: "TOOL_CALL_CHUNK";
|
|
130
|
+
toolCallId?: string;
|
|
131
|
+
toolCallName?: string;
|
|
132
|
+
parentMessageId?: string;
|
|
133
|
+
delta?: string;
|
|
132
134
|
} | {
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
135
|
+
type: "TOOL_CALL_RESULT";
|
|
136
|
+
messageId?: string;
|
|
137
|
+
toolCallId: string;
|
|
138
|
+
content: string;
|
|
139
|
+
role?: "tool";
|
|
138
140
|
} | {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
141
|
+
type: "RAW";
|
|
142
|
+
event: any;
|
|
143
|
+
source?: string;
|
|
142
144
|
} | {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
145
|
+
type: "CUSTOM";
|
|
146
|
+
name: string;
|
|
147
|
+
value: any;
|
|
146
148
|
} | {
|
|
147
|
-
|
|
148
|
-
|
|
149
|
+
type: "STATE_SNAPSHOT";
|
|
150
|
+
snapshot: any;
|
|
149
151
|
} | {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
+
type: "STATE_DELTA";
|
|
153
|
+
delta: any[];
|
|
152
154
|
} | {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
+
type: "MESSAGES_SNAPSHOT";
|
|
156
|
+
messages: any[];
|
|
155
157
|
};
|
|
158
|
+
//#endregion
|
|
159
|
+
export { AgUiEvent, AgUiInterrupt, AgUiInterruptReason, AgUiResumeEntry, AgUiRunFinishedOutcome, UseAgUiRuntimeAdapters, UseAgUiRuntimeOptions, UseAgUiThreadListAdapter };
|
|
156
160
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","
|
|
1
|
+
{"version":3,"file":"types.d.ts","names":[],"sources":["../../src/runtime/types.ts"],"mappings":";;;;;;;;AAeA;KAAY,wBAAA;EACV,QAAA;EACA,mBAAA,UAA6B,OAAA;EAC7B,gBAAA,KACM,QAAA,aACE,OAAA;IACE,QAAA,WAAmB,aAAA;IACnB,KAAA,GAAQ,iBAAA;EAAA;IAER,QAAA,WAAmB,aAAA;IAAiB,KAAA,GAAQ,iBAAA;EAAA;AAAA;AAAA,KAI5C,sBAAA;EACV,WAAA,GAAc,iBAAA;EACd,MAAA,GAAS,sBAAA;EACT,SAAA,GAAY,gBAAA;EACZ,QAAA,GAAW,eAAA;EACX,OAAA,GAAU,oBAAA;EAZmB;;;EAgB7B,UAAA,GAAa,wBAAA;AAAA;AAAA,KAGH,qBAAA;EACV,KAAA,EAAO,SAAA;EACP,MAAA,GAAS,OAAA,CAAQ,MAAA;EACjB,YAAA;EACA,OAAA,IAAW,CAAA,EAAG,KAAA;EACd,QAAA;EACA,QAAA,GAAW,sBAAA;AAAA;AAAA,KAGD,mBAAA;AAAA,KAMA,aAAA;EACV,EAAA;EACA,MAAA,EAAQ,mBAAA;EACR,OAAA;EACA,UAAA;EACA,cAAA,GAAiB,MAAA;EACjB,SAAA;EACA,QAAA,GAAW,MAAA;AAAA;AAAA,KAGD,eAAA;EACV,WAAA;EACA,MAAA;EACA,OAAA;AAAA;AAAA,KAGU,sBAAA;EACN,IAAA;AAAA;EACA,IAAA;EAAmB,UAAA,EAAY,aAAa;AAAA;AAAA,KAEtC,SAAA;EACN,IAAA;EAAqB,KAAA;AAAA;EAErB,IAAA;EACA,KAAA;EACA,OAAA,GAAU,sBAAsB;AAAA;EAEhC,IAAA;EAAuB,KAAA;AAAA;EACvB,IAAA;EAAmB,OAAA;EAAkB,IAAA;AAAA;EACrC,IAAA;EAA4B,SAAA;AAAA;EAC5B,IAAA;EAA8B,SAAA;EAAoB,KAAA;AAAA;EAClD,IAAA;EAA0B,SAAA;AAAA;EAC1B,IAAA;EAA4B,KAAA;AAAA;EAC5B,IAAA;EAAwB,KAAA;AAAA;EACxB,IAAA;AAAA;EACA,IAAA;EAAuC,KAAA;AAAA;EACvC,IAAA;AAAA;EACA,IAAA;AAAA;EACA,IAAA;EAAyB,SAAA;AAAA;EACzB,IAAA;EAAiC,SAAA;AAAA;EACjC,IAAA;EAAmC,SAAA;EAAoB,KAAA;AAAA;EACvD,IAAA;EAA+B,SAAA;AAAA;EAC/B,IAAA;EAAuB,SAAA;AAAA;EAEvB,IAAA;EACA,UAAA;EACA,YAAA;EACA,eAAA;AAAA;EAEA,IAAA;EAAwB,UAAA;EAAoB,KAAA;AAAA;EAC5C,IAAA;EAAuB,UAAA;AAAA;EAEvB,IAAA;EACA,UAAA;EACA,YAAA;EACA,eAAA;EACA,KAAA;AAAA;EAGA,IAAA;EACA,SAAA;EACA,UAAA;EACA,OAAA;EACA,IAAA;AAAA;EAEA,IAAA;EAAa,KAAA;EAAY,MAAA;AAAA;EACzB,IAAA;EAAgB,IAAA;EAAc,KAAA;AAAA;EAC9B,IAAA;EAAwB,QAAA;AAAA;EACxB,IAAA;EAAqB,KAAA;AAAA;EACrB,IAAA;EAA2B,QAAA;AAAA"}
|
package/dist/runtime/types.js
CHANGED
package/dist/useAgUiRuntime.d.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { AgUiInterrupt, AgUiResumeEntry, UseAgUiRuntimeOptions } from "./runtime/types.js";
|
|
2
|
+
import { AssistantRuntime } from "@assistant-ui/core";
|
|
3
|
+
|
|
4
|
+
//#region src/useAgUiRuntime.d.ts
|
|
5
|
+
type AgUiAssistantRuntime = AssistantRuntime & {
|
|
6
|
+
unstable_getPendingInterrupts: () => readonly AgUiInterrupt[];
|
|
7
|
+
unstable_submitInterruptResponses: (responses: readonly AgUiResumeEntry[]) => Promise<void>;
|
|
6
8
|
};
|
|
7
|
-
|
|
9
|
+
declare function useAgUiRuntime(options: UseAgUiRuntimeOptions): AgUiAssistantRuntime;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { AgUiAssistantRuntime, useAgUiRuntime };
|
|
8
12
|
//# sourceMappingURL=useAgUiRuntime.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useAgUiRuntime.d.ts","
|
|
1
|
+
{"version":3,"file":"useAgUiRuntime.d.ts","names":[],"sources":["../src/useAgUiRuntime.ts"],"mappings":";;;;KAwBY,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
|
@@ -1,147 +1,141 @@
|
|
|
1
1
|
"use client";
|
|
2
|
-
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
3
|
-
import { useExternalStoreRuntime, useRuntimeAdapters, useToolInvocations, } from "@assistant-ui/core/react";
|
|
4
2
|
import { makeLogger } from "./runtime/logger.js";
|
|
5
3
|
import { AgUiThreadRuntimeCore } from "./runtime/AgUiThreadRuntimeCore.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
core.detachRuntime();
|
|
140
|
-
};
|
|
141
|
-
}, [core, runtime]);
|
|
142
|
-
useEffect(() => {
|
|
143
|
-
core.__internal_load();
|
|
144
|
-
}, [core]);
|
|
145
|
-
return runtime;
|
|
4
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
5
|
+
import { useExternalStoreRuntime, useRuntimeAdapters, useToolInvocations } from "@assistant-ui/core/react";
|
|
6
|
+
//#region src/useAgUiRuntime.ts
|
|
7
|
+
function useAgUiRuntime(options) {
|
|
8
|
+
const logger = useMemo(() => makeLogger(options.logger), [options.logger]);
|
|
9
|
+
const [_version, setVersion] = useState(0);
|
|
10
|
+
const notifyUpdate = useCallback(() => setVersion((v) => v + 1), []);
|
|
11
|
+
const coreRef = useRef(null);
|
|
12
|
+
const runtimeAdapters = useRuntimeAdapters();
|
|
13
|
+
const historyAdapter = options.adapters?.history ?? runtimeAdapters?.history;
|
|
14
|
+
const threadListAdapter = options.adapters?.threadList;
|
|
15
|
+
if (!coreRef.current) coreRef.current = new AgUiThreadRuntimeCore({
|
|
16
|
+
agent: options.agent,
|
|
17
|
+
logger,
|
|
18
|
+
showThinking: options.showThinking ?? true,
|
|
19
|
+
...options.onError && { onError: options.onError },
|
|
20
|
+
...options.onCancel && { onCancel: options.onCancel },
|
|
21
|
+
...historyAdapter && { history: historyAdapter },
|
|
22
|
+
notifyUpdate
|
|
23
|
+
});
|
|
24
|
+
const core = coreRef.current;
|
|
25
|
+
core.updateOptions({
|
|
26
|
+
agent: options.agent,
|
|
27
|
+
logger,
|
|
28
|
+
showThinking: options.showThinking ?? true,
|
|
29
|
+
...options.onError && { onError: options.onError },
|
|
30
|
+
...options.onCancel && { onCancel: options.onCancel },
|
|
31
|
+
...historyAdapter && { history: historyAdapter }
|
|
32
|
+
});
|
|
33
|
+
const [toolStatuses, setToolStatuses] = useState({});
|
|
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
|
+
const threadList = useMemo(() => {
|
|
44
|
+
if (!threadListAdapter) return void 0;
|
|
45
|
+
const { onSwitchToNewThread, onSwitchToThread } = threadListAdapter;
|
|
46
|
+
return {
|
|
47
|
+
threadId: threadListAdapter.threadId,
|
|
48
|
+
onSwitchToNewThread: onSwitchToNewThread ? async () => {
|
|
49
|
+
toolInvocationsRef.current.reset();
|
|
50
|
+
await onSwitchToNewThread();
|
|
51
|
+
core.applyExternalMessages([]);
|
|
52
|
+
} : void 0,
|
|
53
|
+
onSwitchToThread: onSwitchToThread ? async (threadId) => {
|
|
54
|
+
toolInvocationsRef.current.reset();
|
|
55
|
+
const result = await onSwitchToThread(threadId);
|
|
56
|
+
core.applyExternalMessages(result.messages);
|
|
57
|
+
if (result.state) core.loadExternalState(result.state);
|
|
58
|
+
} : void 0
|
|
59
|
+
};
|
|
60
|
+
}, [threadListAdapter, core]);
|
|
61
|
+
const adapters = options.adapters;
|
|
62
|
+
const adapterAdapters = useMemo(() => ({
|
|
63
|
+
attachments: adapters?.attachments ?? runtimeAdapters?.attachments,
|
|
64
|
+
speech: adapters?.speech,
|
|
65
|
+
dictation: adapters?.dictation,
|
|
66
|
+
feedback: adapters?.feedback,
|
|
67
|
+
threadList
|
|
68
|
+
}), [
|
|
69
|
+
adapters,
|
|
70
|
+
runtimeAdapters,
|
|
71
|
+
threadList
|
|
72
|
+
]);
|
|
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
|
+
const baseRuntime = useExternalStoreRuntime(useMemo(() => {
|
|
95
|
+
return {
|
|
96
|
+
isLoading: core.isLoading,
|
|
97
|
+
messages: core.getMessages(),
|
|
98
|
+
state: core.getState(),
|
|
99
|
+
isRunning: core.isRunning() || hasExecutingTools,
|
|
100
|
+
onNew: (message) => core.append(message),
|
|
101
|
+
onEdit: (message) => core.edit(message),
|
|
102
|
+
onReload: (parentId, config) => core.reload(parentId, config),
|
|
103
|
+
onCancel: async () => {
|
|
104
|
+
core.cancel();
|
|
105
|
+
await toolInvocationsRef.current.abort();
|
|
106
|
+
},
|
|
107
|
+
onAddToolResult: (options) => core.addToolResult(options),
|
|
108
|
+
onResume: (config) => core.resume(config),
|
|
109
|
+
onResumeToolCall: (options) => toolInvocationsRef.current.resume(options.toolCallId, options.payload),
|
|
110
|
+
setMessages: (messages) => core.applyExternalMessages(messages),
|
|
111
|
+
onImport: (messages) => core.applyExternalMessages(messages),
|
|
112
|
+
onLoadExternalState: (state) => core.loadExternalState(state),
|
|
113
|
+
adapters: adapterAdapters
|
|
114
|
+
};
|
|
115
|
+
}, [
|
|
116
|
+
adapterAdapters,
|
|
117
|
+
core,
|
|
118
|
+
_version,
|
|
119
|
+
hasExecutingTools
|
|
120
|
+
]));
|
|
121
|
+
const runtime = useMemo(() => {
|
|
122
|
+
const wrapper = Object.create(baseRuntime);
|
|
123
|
+
wrapper.unstable_getPendingInterrupts = () => core.getPendingInterrupts()?.interrupts ?? [];
|
|
124
|
+
wrapper.unstable_submitInterruptResponses = (responses) => core.submitInterruptResponses(responses);
|
|
125
|
+
return wrapper;
|
|
126
|
+
}, [baseRuntime, core]);
|
|
127
|
+
useEffect(() => {
|
|
128
|
+
core.attachRuntime(runtime);
|
|
129
|
+
return () => {
|
|
130
|
+
core.detachRuntime();
|
|
131
|
+
};
|
|
132
|
+
}, [core, runtime]);
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
core.__internal_load();
|
|
135
|
+
}, [core]);
|
|
136
|
+
return runtime;
|
|
146
137
|
}
|
|
138
|
+
//#endregion
|
|
139
|
+
export { useAgUiRuntime };
|
|
140
|
+
|
|
147
141
|
//# sourceMappingURL=useAgUiRuntime.js.map
|