@copilotkit/react-core 0.17.0 → 0.17.1-alpha.1
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/.turbo/turbo-build.log +164 -160
- package/CHANGELOG.md +16 -0
- package/dist/{chunk-WMXA2TIX.mjs → chunk-R2WOR4SW.mjs} +15 -4
- package/dist/chunk-R2WOR4SW.mjs.map +1 -0
- package/dist/{chunk-XSS45JC5.mjs → chunk-R75NEA76.mjs} +2 -2
- package/dist/hooks/index.mjs +2 -2
- package/dist/hooks/use-chat.mjs +1 -1
- package/dist/hooks/use-copilot-chat.mjs +2 -2
- package/dist/index.mjs +2 -2
- package/package.json +2 -2
- package/src/hooks/use-chat.ts +15 -2
- package/dist/chunk-WMXA2TIX.mjs.map +0 -1
- /package/dist/{chunk-XSS45JC5.mjs.map → chunk-R75NEA76.mjs.map} +0 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
+
Debugger attached.
|
|
1
2
|
|
|
2
|
-
> @copilotkit/react-core@0.17.0 build /
|
|
3
|
+
> @copilotkit/react-core@0.17.1-alpha.0 build /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/CopilotKit/packages/react-core
|
|
3
4
|
> tsup --treeshake
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
Debugger attached.
|
|
7
|
+
CLI Building entry: src/index.tsx, src/components/index.ts, src/context/copilot-context.tsx, src/context/index.ts, src/hooks/index.ts, src/hooks/use-chat.ts, src/hooks/use-copilot-chat.ts, src/hooks/use-flat-category-store.ts, src/hooks/use-make-copilot-actionable.ts, src/hooks/use-make-copilot-document-readable.ts, src/hooks/use-make-copilot-readable.ts, src/hooks/use-tree.ts, src/openai/chat-completion-client.ts, src/openai/chat-completion-stream.ts, src/openai/chat-completion-transport.ts, src/openai/index.tsx, src/openai-assistants/index.ts, src/types/annotated-function.ts, src/types/base.ts, src/types/document-pointer.ts, src/types/index.ts, src/types/message.ts, src/utils/utils.test.ts, src/utils/utils.ts, src/components/copilot-provider/copilot-provider-props.tsx, src/components/copilot-provider/copilot-provider.tsx, src/components/copilot-provider/index.ts, src/components/copilot-provider/standard-copilot-api-config.tsx, src/openai-assistants/hooks/index.ts, src/openai-assistants/hooks/use-assistants.ts, src/openai-assistants/hooks/use-copilot-chat-v2.ts, src/openai-assistants/utils/index.ts, src/openai-assistants/utils/process-message-stream.ts
|
|
8
|
+
CLI Using tsconfig: tsconfig.json
|
|
9
|
+
CLI tsup v6.7.0
|
|
10
|
+
CLI Using tsup config: /Users/ataibarkai/LocalGit/Recursively.ai/CopilotKit/CopilotKit/packages/react-core/tsup.config.ts
|
|
11
|
+
CLI Target: es6
|
|
12
|
+
CLI Cleaning output folder
|
|
13
|
+
ESM Build start
|
|
12
14
|
Module level directives cause errors when bundled, "use client" in "dist/components/copilot-provider/copilot-provider-props.mjs" was ignored.
|
|
13
15
|
Module level directives cause errors when bundled, "use client" in "dist/components/copilot-provider/copilot-provider.mjs" was ignored.
|
|
14
16
|
Module level directives cause errors when bundled, "use client" in "dist/openai-assistants/hooks/use-assistants.mjs" was ignored.
|
|
@@ -31,155 +33,157 @@ Generated an empty chunk: "chunk-VNRDQJXW".
|
|
|
31
33
|
Generated an empty chunk: "chunk-5UGLWBZJ".
|
|
32
34
|
Generated an empty chunk: "chunk-JD7BAH7U".
|
|
33
35
|
Generated an empty chunk: "chunk-SPCZTZCY".
|
|
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
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
36
|
+
ESM dist/components/copilot-provider/copilot-provider-props.mjs 84.00 B
|
|
37
|
+
ESM dist/types/annotated-function.mjs 80.00 B
|
|
38
|
+
ESM dist/types/base.mjs 66.00 B
|
|
39
|
+
ESM dist/types/message.mjs 69.00 B
|
|
40
|
+
ESM dist/utils/utils.test.mjs 202.00 B
|
|
41
|
+
ESM dist/types/document-pointer.mjs 78.00 B
|
|
42
|
+
ESM dist/utils/utils.mjs 67.00 B
|
|
43
|
+
ESM dist/chunk-BABVSMJR.mjs 76.00 B
|
|
44
|
+
ESM dist/chunk-FRAKUJWH.mjs 76.00 B
|
|
45
|
+
ESM dist/chunk-VNRDQJXW.mjs 76.00 B
|
|
46
|
+
ESM dist/chunk-5UGLWBZJ.mjs 76.00 B
|
|
47
|
+
ESM dist/chunk-PUFR5OAI.mjs 76.00 B
|
|
48
|
+
ESM dist/chunk-JD7BAH7U.mjs 76.00 B
|
|
49
|
+
ESM dist/chunk-SPCZTZCY.mjs 76.00 B
|
|
50
|
+
ESM dist/chunk-MRXNTQOX.mjs 1.92 KB
|
|
51
|
+
ESM dist/chunk-F2JIAPZQ.mjs 435.00 B
|
|
52
|
+
ESM dist/chunk-EFZPSZWO.mjs 76.00 B
|
|
53
|
+
ESM dist/openai-assistants/utils/process-message-stream.mjs 183.00 B
|
|
54
|
+
ESM dist/components/copilot-provider/index.mjs 369.00 B
|
|
55
|
+
ESM dist/components/copilot-provider/standard-copilot-api-config.mjs 192.00 B
|
|
56
|
+
ESM dist/components/copilot-provider/copilot-provider.mjs 345.00 B
|
|
57
|
+
ESM dist/openai-assistants/utils/index.mjs 201.00 B
|
|
58
|
+
ESM dist/openai-assistants/index.mjs 570.00 B
|
|
59
|
+
ESM dist/chunk-7GFKOIO7.mjs 76.00 B
|
|
60
|
+
ESM dist/types/index.mjs 98.00 B
|
|
61
|
+
ESM dist/hooks/use-make-copilot-actionable.mjs 218.00 B
|
|
62
|
+
ESM dist/hooks/use-make-copilot-readable.mjs 214.00 B
|
|
63
|
+
ESM dist/hooks/use-make-copilot-document-readable.mjs 231.00 B
|
|
64
|
+
ESM dist/openai-assistants/hooks/index.mjs 547.00 B
|
|
65
|
+
ESM dist/hooks/use-tree.mjs 170.00 B
|
|
66
|
+
ESM dist/openai-assistants/hooks/use-copilot-chat-v2.mjs 548.00 B
|
|
67
|
+
ESM dist/openai/chat-completion-stream.mjs 209.00 B
|
|
68
|
+
ESM dist/openai/chat-completion-transport.mjs 183.00 B
|
|
69
|
+
ESM dist/openai/chat-completion-client.mjs 209.00 B
|
|
70
|
+
ESM dist/openai/index.mjs 224.00 B
|
|
71
|
+
ESM dist/index.mjs 1.11 KB
|
|
72
|
+
ESM dist/chunk-TU4DNDO7.mjs 4.33 KB
|
|
73
|
+
ESM dist/chunk-MZ5UN3BY.mjs 830.00 B
|
|
74
|
+
ESM dist/chunk-6SOQYBNX.mjs 1.24 KB
|
|
75
|
+
ESM dist/components/index.mjs 380.00 B
|
|
76
|
+
ESM dist/context/copilot-context.mjs 164.00 B
|
|
77
|
+
ESM dist/context/index.mjs 186.00 B
|
|
78
|
+
ESM dist/hooks/index.mjs 708.00 B
|
|
79
|
+
ESM dist/chunk-FP4EXCGS.mjs 954.00 B
|
|
80
|
+
ESM dist/chunk-7P4OYNP3.mjs 677.00 B
|
|
81
|
+
ESM dist/chunk-HW5IH6PV.mjs 626.00 B
|
|
82
|
+
ESM dist/hooks/use-chat.mjs 214.00 B
|
|
83
|
+
ESM dist/chunk-R75NEA76.mjs 2.71 KB
|
|
84
|
+
ESM dist/chunk-QTET3FBV.mjs 5.18 KB
|
|
85
|
+
ESM dist/hooks/use-copilot-chat.mjs 507.00 B
|
|
86
|
+
ESM dist/chunk-VUY2K2DI.mjs 3.89 KB
|
|
87
|
+
ESM dist/chunk-R2WOR4SW.mjs 3.69 KB
|
|
88
|
+
ESM dist/chunk-TKXPMMND.mjs 5.33 KB
|
|
89
|
+
ESM dist/hooks/use-flat-category-store.mjs 200.00 B
|
|
90
|
+
ESM dist/chunk-YULKJPY3.mjs 1.87 KB
|
|
91
|
+
ESM dist/chunk-VK25FIML.mjs 4.48 KB
|
|
92
|
+
ESM dist/chunk-6A4PCNMZ.mjs 1.30 KB
|
|
93
|
+
ESM dist/openai-assistants/hooks/use-assistants.mjs 2.55 KB
|
|
94
|
+
ESM dist/components/copilot-provider/copilot-provider-props.mjs.map 51.00 B
|
|
95
|
+
ESM dist/utils/utils.test.mjs.map 351.00 B
|
|
96
|
+
ESM dist/types/annotated-function.mjs.map 51.00 B
|
|
97
|
+
ESM dist/utils/utils.mjs.map 51.00 B
|
|
98
|
+
ESM dist/types/base.mjs.map 51.00 B
|
|
99
|
+
ESM dist/chunk-BABVSMJR.mjs.map 51.00 B
|
|
100
|
+
ESM dist/chunk-FRAKUJWH.mjs.map 51.00 B
|
|
101
|
+
ESM dist/chunk-VNRDQJXW.mjs.map 51.00 B
|
|
102
|
+
ESM dist/chunk-PUFR5OAI.mjs.map 51.00 B
|
|
103
|
+
ESM dist/chunk-JD7BAH7U.mjs.map 51.00 B
|
|
104
|
+
ESM dist/chunk-F2JIAPZQ.mjs.map 1.12 KB
|
|
105
|
+
ESM dist/chunk-MRXNTQOX.mjs.map 71.00 B
|
|
106
|
+
ESM dist/chunk-EFZPSZWO.mjs.map 51.00 B
|
|
107
|
+
ESM dist/openai-assistants/utils/process-message-stream.mjs.map 51.00 B
|
|
108
|
+
ESM dist/components/copilot-provider/index.mjs.map 51.00 B
|
|
109
|
+
ESM dist/components/copilot-provider/standard-copilot-api-config.mjs.map 51.00 B
|
|
110
|
+
ESM dist/components/copilot-provider/copilot-provider.mjs.map 51.00 B
|
|
111
|
+
ESM dist/openai-assistants/index.mjs.map 51.00 B
|
|
112
|
+
ESM dist/chunk-7GFKOIO7.mjs.map 51.00 B
|
|
113
|
+
ESM dist/types/index.mjs.map 51.00 B
|
|
114
|
+
ESM dist/hooks/use-make-copilot-actionable.mjs.map 51.00 B
|
|
115
|
+
ESM dist/hooks/use-make-copilot-readable.mjs.map 51.00 B
|
|
116
|
+
ESM dist/hooks/use-make-copilot-document-readable.mjs.map 51.00 B
|
|
117
|
+
ESM dist/openai-assistants/hooks/index.mjs.map 51.00 B
|
|
118
|
+
ESM dist/hooks/use-tree.mjs.map 51.00 B
|
|
119
|
+
ESM dist/types/message.mjs.map 51.00 B
|
|
120
|
+
ESM dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map 51.00 B
|
|
121
|
+
ESM dist/openai/chat-completion-stream.mjs.map 51.00 B
|
|
122
|
+
ESM dist/types/document-pointer.mjs.map 51.00 B
|
|
123
|
+
ESM dist/openai/chat-completion-transport.mjs.map 51.00 B
|
|
124
|
+
ESM dist/openai/chat-completion-client.mjs.map 51.00 B
|
|
125
|
+
ESM dist/chunk-5UGLWBZJ.mjs.map 51.00 B
|
|
126
|
+
ESM dist/index.mjs.map 51.00 B
|
|
127
|
+
ESM dist/chunk-SPCZTZCY.mjs.map 51.00 B
|
|
128
|
+
ESM dist/openai/index.mjs.map 51.00 B
|
|
129
|
+
ESM dist/chunk-TU4DNDO7.mjs.map 7.90 KB
|
|
130
|
+
ESM dist/chunk-MZ5UN3BY.mjs.map 1.26 KB
|
|
131
|
+
ESM dist/chunk-6SOQYBNX.mjs.map 2.07 KB
|
|
132
|
+
ESM dist/components/index.mjs.map 51.00 B
|
|
133
|
+
ESM dist/context/copilot-context.mjs.map 51.00 B
|
|
134
|
+
ESM dist/context/index.mjs.map 51.00 B
|
|
135
|
+
ESM dist/chunk-FP4EXCGS.mjs.map 1.67 KB
|
|
136
|
+
ESM dist/hooks/index.mjs.map 51.00 B
|
|
137
|
+
ESM dist/chunk-HW5IH6PV.mjs.map 1.47 KB
|
|
138
|
+
ESM dist/hooks/use-chat.mjs.map 51.00 B
|
|
139
|
+
ESM dist/chunk-R75NEA76.mjs.map 4.37 KB
|
|
140
|
+
ESM dist/openai-assistants/utils/index.mjs.map 51.00 B
|
|
141
|
+
ESM dist/hooks/use-copilot-chat.mjs.map 51.00 B
|
|
142
|
+
ESM dist/chunk-R2WOR4SW.mjs.map 8.47 KB
|
|
143
|
+
ESM dist/chunk-TKXPMMND.mjs.map 9.93 KB
|
|
144
|
+
ESM dist/hooks/use-flat-category-store.mjs.map 51.00 B
|
|
145
|
+
ESM dist/chunk-YULKJPY3.mjs.map 4.21 KB
|
|
146
|
+
ESM dist/chunk-VK25FIML.mjs.map 7.84 KB
|
|
147
|
+
ESM dist/chunk-VUY2K2DI.mjs.map 8.27 KB
|
|
148
|
+
ESM dist/chunk-6A4PCNMZ.mjs.map 4.26 KB
|
|
149
|
+
ESM dist/openai-assistants/hooks/use-assistants.mjs.map 4.67 KB
|
|
150
|
+
ESM dist/chunk-7P4OYNP3.mjs.map 1.42 KB
|
|
151
|
+
ESM dist/chunk-QTET3FBV.mjs.map 11.03 KB
|
|
152
|
+
ESM ⚡️ Build success in 405ms
|
|
153
|
+
DTS Build start
|
|
154
|
+
DTS ⚡️ Build success in 3554ms
|
|
155
|
+
DTS dist/index.d.ts 1.61 KB
|
|
156
|
+
DTS dist/components/copilot-provider/copilot-provider.d.ts 1.55 KB
|
|
157
|
+
DTS dist/components/copilot-provider/copilot-provider-props.d.ts 1.52 KB
|
|
158
|
+
DTS dist/context/copilot-context.d.ts 1.98 KB
|
|
159
|
+
DTS dist/hooks/use-copilot-chat.d.ts 955.00 B
|
|
160
|
+
DTS dist/hooks/use-make-copilot-actionable.d.ts 255.00 B
|
|
161
|
+
DTS dist/hooks/use-make-copilot-readable.d.ts 598.00 B
|
|
162
|
+
DTS dist/hooks/use-make-copilot-document-readable.d.ts 516.00 B
|
|
163
|
+
DTS dist/hooks/use-chat.d.ts 2.75 KB
|
|
164
|
+
DTS dist/types/annotated-function.d.ts 767.00 B
|
|
165
|
+
DTS dist/types/document-pointer.d.ts 181.00 B
|
|
166
|
+
DTS dist/types/base.d.ts 1.96 KB
|
|
167
|
+
DTS dist/openai-assistants/hooks/use-copilot-chat-v2.d.ts 1.41 KB
|
|
168
|
+
DTS dist/openai-assistants/utils/process-message-stream.d.ts 196.00 B
|
|
169
|
+
DTS dist/openai/chat-completion-stream.d.ts 694.00 B
|
|
170
|
+
DTS dist/openai/chat-completion-transport.d.ts 1.26 KB
|
|
171
|
+
DTS dist/hooks/use-tree.d.ts 543.00 B
|
|
172
|
+
DTS dist/components/index.d.ts 482.00 B
|
|
173
|
+
DTS dist/context/index.d.ts 281.00 B
|
|
174
|
+
DTS dist/hooks/index.d.ts 608.00 B
|
|
175
|
+
DTS dist/hooks/use-flat-category-store.d.ts 424.00 B
|
|
176
|
+
DTS dist/openai/chat-completion-client.d.ts 1.44 KB
|
|
177
|
+
DTS dist/openai/index.d.ts 438.00 B
|
|
178
|
+
DTS dist/openai-assistants/index.d.ts 262.00 B
|
|
179
|
+
DTS dist/types/index.d.ts 233.00 B
|
|
180
|
+
DTS dist/types/message.d.ts 12.00 B
|
|
181
|
+
DTS dist/utils/utils.test.d.ts 12.00 B
|
|
182
|
+
DTS dist/utils/utils.d.ts 12.00 B
|
|
183
|
+
DTS dist/components/copilot-provider/index.d.ts 463.00 B
|
|
184
|
+
DTS dist/components/copilot-provider/standard-copilot-api-config.d.ts 911.00 B
|
|
185
|
+
DTS dist/openai-assistants/hooks/index.d.ts 182.00 B
|
|
186
|
+
DTS dist/openai-assistants/hooks/use-assistants.d.ts 551.00 B
|
|
187
|
+
DTS dist/openai-assistants/utils/index.d.ts 68.00 B
|
|
188
|
+
Waiting for the debugger to disconnect...
|
|
189
|
+
Waiting for the debugger to disconnect...
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,21 @@
|
|
|
1
1
|
# ui
|
|
2
2
|
|
|
3
|
+
## 0.17.1-alpha.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- change order of operations on stop cleanup
|
|
8
|
+
- Updated dependencies
|
|
9
|
+
- @copilotkit/shared@0.1.1-alpha.1
|
|
10
|
+
|
|
11
|
+
## 0.17.1-alpha.0
|
|
12
|
+
|
|
13
|
+
### Patch Changes
|
|
14
|
+
|
|
15
|
+
- fix stop generate
|
|
16
|
+
- Updated dependencies
|
|
17
|
+
- @copilotkit/shared@0.1.1-alpha.0
|
|
18
|
+
|
|
3
19
|
## 0.17.0
|
|
4
20
|
|
|
5
21
|
### Minor Changes
|
|
@@ -1,15 +1,18 @@
|
|
|
1
1
|
import { ChatCompletionClient } from './chunk-TKXPMMND.mjs';
|
|
2
2
|
import { __async, __spreadValues } from './chunk-MRXNTQOX.mjs';
|
|
3
|
-
import { useState } from 'react';
|
|
3
|
+
import { useState, useRef } from 'react';
|
|
4
4
|
import { nanoid } from 'nanoid';
|
|
5
5
|
|
|
6
6
|
function useChat(options) {
|
|
7
7
|
const [messages, setMessages] = useState([]);
|
|
8
8
|
const [input, setInput] = useState("");
|
|
9
9
|
const [isLoading, setIsLoading] = useState(false);
|
|
10
|
+
const abortControllerRef = useRef();
|
|
10
11
|
const runChatCompletion = (messages2) => __async(this, null, function* () {
|
|
11
12
|
return new Promise((resolve, reject) => {
|
|
12
13
|
setIsLoading(true);
|
|
14
|
+
const abortController = new AbortController();
|
|
15
|
+
abortControllerRef.current = abortController;
|
|
13
16
|
const assistantMessage = {
|
|
14
17
|
id: nanoid(),
|
|
15
18
|
createdAt: /* @__PURE__ */ new Date(),
|
|
@@ -24,7 +27,13 @@ function useChat(options) {
|
|
|
24
27
|
client.off("end");
|
|
25
28
|
client.off("error");
|
|
26
29
|
client.off("function");
|
|
30
|
+
abortControllerRef.current = void 0;
|
|
27
31
|
};
|
|
32
|
+
abortController.signal.addEventListener("abort", () => {
|
|
33
|
+
cleanup();
|
|
34
|
+
setIsLoading(false);
|
|
35
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
36
|
+
});
|
|
28
37
|
client.on("content", (content) => {
|
|
29
38
|
assistantMessage.content += content;
|
|
30
39
|
setMessages([...messages2, __spreadValues({}, assistantMessage)]);
|
|
@@ -53,7 +62,8 @@ function useChat(options) {
|
|
|
53
62
|
copilotConfig: options.copilotConfig,
|
|
54
63
|
messages: messagesWithContext,
|
|
55
64
|
functions: options.functions,
|
|
56
|
-
headers: options.headers
|
|
65
|
+
headers: options.headers,
|
|
66
|
+
signal: abortController.signal
|
|
57
67
|
});
|
|
58
68
|
});
|
|
59
69
|
});
|
|
@@ -84,7 +94,8 @@ function useChat(options) {
|
|
|
84
94
|
return runChatCompletionAndHandleFunctionCall(newMessages);
|
|
85
95
|
});
|
|
86
96
|
const stop = () => {
|
|
87
|
-
|
|
97
|
+
var _a;
|
|
98
|
+
(_a = abortControllerRef.current) == null ? void 0 : _a.abort();
|
|
88
99
|
};
|
|
89
100
|
return {
|
|
90
101
|
messages,
|
|
@@ -99,4 +110,4 @@ function useChat(options) {
|
|
|
99
110
|
|
|
100
111
|
export { useChat };
|
|
101
112
|
//# sourceMappingURL=out.js.map
|
|
102
|
-
//# sourceMappingURL=chunk-
|
|
113
|
+
//# sourceMappingURL=chunk-R2WOR4SW.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-chat.ts"],"names":["messages"],"mappings":";;;;;;;;;AAAA,SAAS,QAAQ,gBAAgB;AAEjC,SAAS,cAAc;AAgFhB,SAAS,QAAQ,SAA0D;AAChF,QAAM,CAAC,UAAU,WAAW,IAAI,SAAoB,CAAC,CAAC;AACtD,QAAM,CAAC,OAAO,QAAQ,IAAI,SAAS,EAAE;AACrC,QAAM,CAAC,WAAW,YAAY,IAAI,SA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from \"../types\";\nimport { nanoid } from \"nanoid\";\nimport { ChatCompletionClient } from \"../openai/chat-completion-client\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport type UseChatOptions = {\n /**\n * The API endpoint that accepts a `{ messages: Message[] }` object and returns\n * a stream of tokens of the AI chat response. Defaults to `/api/chat`.\n */\n api?: string;\n /**\n * A unique identifier for the chat. If not provided, a random one will be\n * generated. When provided, the `useChat` hook with the same `id` will\n * have shared states across components.\n */\n id?: string;\n /**\n * System messages of the chat. Defaults to an empty array.\n */\n initialMessages?: Message[];\n /**\n * Callback function to be called when a function call is received.\n * If the function returns a `ChatRequest` object, the request will be sent\n * automatically to the API and will be used to update the chat.\n */\n onFunctionCall?: FunctionCallHandler;\n /**\n * HTTP headers to be sent with the API request.\n */\n headers?: Record<string, string> | Headers;\n /**\n * Extra body object to be sent with the API request.\n * @example\n * Send a `sessionId` to the API along with the messages.\n * ```js\n * useChat({\n * body: {\n * sessionId: '123',\n * }\n * })\n * ```\n */\n body?: object;\n /**\n * Function definitions to be sent to the API.\n */\n functions?: Function[];\n};\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Message[];\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n */\n append: (message: Message) => Promise<void>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: () => Promise<void>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /** Whether the API request is in progress */\n isLoading: boolean;\n};\n\nexport type UseChatOptionsWithCopilotConfig = UseChatOptions & {\n copilotConfig: CopilotApiConfig;\n};\n\nexport function useChat(options: UseChatOptionsWithCopilotConfig): UseChatHelpers {\n const [messages, setMessages] = useState<Message[]>([]);\n const [input, setInput] = useState(\"\");\n const [isLoading, setIsLoading] = useState(false);\n const abortControllerRef = useRef<AbortController>();\n\n const runChatCompletion = async (messages: Message[]): Promise<Message> => {\n return new Promise<Message>((resolve, reject) => {\n setIsLoading(true);\n\n const abortController = new AbortController();\n abortControllerRef.current = abortController;\n\n const assistantMessage: Message = {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n };\n\n // Assistant messages are always copied when using setState\n setMessages([...messages, { ...assistantMessage }]);\n\n const messagesWithContext = [...(options.initialMessages || []), ...messages];\n\n const client = new ChatCompletionClient({});\n\n const cleanup = () => {\n client.off(\"content\");\n client.off(\"end\");\n client.off(\"error\");\n client.off(\"function\");\n\n abortControllerRef.current = undefined;\n };\n\n abortController.signal.addEventListener(\"abort\", () => {\n cleanup();\n setIsLoading(false);\n reject(new DOMException(\"Aborted\", \"AbortError\"));\n });\n\n client.on(\"content\", (content) => {\n assistantMessage.content += content;\n setMessages([...messages, { ...assistantMessage }]);\n });\n\n client.on(\"end\", () => {\n setIsLoading(false);\n cleanup();\n resolve({ ...assistantMessage });\n });\n\n client.on(\"error\", (error) => {\n setIsLoading(false);\n cleanup();\n reject(error);\n });\n\n client.on(\"function\", async (functionCall) => {\n assistantMessage.function_call = {\n name: functionCall.name,\n arguments: JSON.stringify(functionCall.arguments),\n };\n setMessages([...messages, { ...assistantMessage }]);\n // quit early if we get a function call\n setIsLoading(false);\n cleanup();\n resolve({ ...assistantMessage });\n });\n\n client.fetch({\n copilotConfig: options.copilotConfig,\n messages: messagesWithContext,\n functions: options.functions,\n headers: options.headers,\n signal: abortController.signal,\n });\n });\n };\n\n const runChatCompletionAndHandleFunctionCall = async (messages: Message[]): Promise<void> => {\n const message = await runChatCompletion(messages);\n if (message.function_call && options.onFunctionCall) {\n await options.onFunctionCall(messages, message.function_call);\n }\n };\n\n const append = async (message: Message): Promise<void> => {\n if (isLoading) {\n return;\n }\n const newMessages = [...messages, message];\n setMessages(newMessages);\n return runChatCompletionAndHandleFunctionCall(newMessages);\n };\n\n const reload = async (): Promise<void> => {\n if (isLoading || messages.length === 0) {\n return;\n }\n let newMessages = [...messages];\n const lastMessage = messages[messages.length - 1];\n\n if (lastMessage.role === \"assistant\") {\n newMessages = newMessages.slice(0, -1);\n }\n setMessages(newMessages);\n\n return runChatCompletionAndHandleFunctionCall(newMessages);\n };\n\n const stop = (): void => {\n abortControllerRef.current?.abort();\n };\n\n return {\n messages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n"]}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { defaultCopilotContextCategories } from './chunk-QTET3FBV.mjs';
|
|
2
2
|
import { CopilotContext } from './chunk-6A4PCNMZ.mjs';
|
|
3
|
-
import { useChat } from './chunk-
|
|
3
|
+
import { useChat } from './chunk-R2WOR4SW.mjs';
|
|
4
4
|
import { __objRest, __spreadProps, __spreadValues } from './chunk-MRXNTQOX.mjs';
|
|
5
5
|
import { useContext, useMemo } from 'react';
|
|
6
6
|
|
|
@@ -74,4 +74,4 @@ If you would like to call a function, call it without saying anything else.
|
|
|
74
74
|
|
|
75
75
|
export { defaultSystemMessage, useCopilotChat };
|
|
76
76
|
//# sourceMappingURL=out.js.map
|
|
77
|
-
//# sourceMappingURL=chunk-
|
|
77
|
+
//# sourceMappingURL=chunk-R75NEA76.mjs.map
|
package/dist/hooks/index.mjs
CHANGED
|
@@ -2,14 +2,14 @@ import '../chunk-5UGLWBZJ.mjs';
|
|
|
2
2
|
export { useMakeCopilotActionable } from '../chunk-FP4EXCGS.mjs';
|
|
3
3
|
export { useMakeCopilotDocumentReadable } from '../chunk-7P4OYNP3.mjs';
|
|
4
4
|
export { useMakeCopilotReadable } from '../chunk-HW5IH6PV.mjs';
|
|
5
|
-
export { useCopilotChat } from '../chunk-
|
|
5
|
+
export { useCopilotChat } from '../chunk-R75NEA76.mjs';
|
|
6
6
|
import '../chunk-JD7BAH7U.mjs';
|
|
7
7
|
import '../chunk-SPCZTZCY.mjs';
|
|
8
8
|
import '../chunk-QTET3FBV.mjs';
|
|
9
9
|
import '../chunk-F2JIAPZQ.mjs';
|
|
10
10
|
import '../chunk-VUY2K2DI.mjs';
|
|
11
11
|
import '../chunk-6A4PCNMZ.mjs';
|
|
12
|
-
import '../chunk-
|
|
12
|
+
import '../chunk-R2WOR4SW.mjs';
|
|
13
13
|
import '../chunk-TKXPMMND.mjs';
|
|
14
14
|
import '../chunk-VK25FIML.mjs';
|
|
15
15
|
import '../chunk-YULKJPY3.mjs';
|
package/dist/hooks/use-chat.mjs
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export { defaultSystemMessage, useCopilotChat } from '../chunk-
|
|
1
|
+
export { defaultSystemMessage, useCopilotChat } from '../chunk-R75NEA76.mjs';
|
|
2
2
|
import '../chunk-JD7BAH7U.mjs';
|
|
3
3
|
import '../chunk-SPCZTZCY.mjs';
|
|
4
4
|
import '../chunk-QTET3FBV.mjs';
|
|
5
5
|
import '../chunk-F2JIAPZQ.mjs';
|
|
6
6
|
import '../chunk-VUY2K2DI.mjs';
|
|
7
7
|
import '../chunk-6A4PCNMZ.mjs';
|
|
8
|
-
import '../chunk-
|
|
8
|
+
import '../chunk-R2WOR4SW.mjs';
|
|
9
9
|
import '../chunk-TKXPMMND.mjs';
|
|
10
10
|
import '../chunk-VK25FIML.mjs';
|
|
11
11
|
import '../chunk-YULKJPY3.mjs';
|
package/dist/index.mjs
CHANGED
|
@@ -11,14 +11,14 @@ import './chunk-5UGLWBZJ.mjs';
|
|
|
11
11
|
export { useMakeCopilotActionable } from './chunk-FP4EXCGS.mjs';
|
|
12
12
|
export { useMakeCopilotDocumentReadable } from './chunk-7P4OYNP3.mjs';
|
|
13
13
|
export { useMakeCopilotReadable } from './chunk-HW5IH6PV.mjs';
|
|
14
|
-
export { useCopilotChat } from './chunk-
|
|
14
|
+
export { useCopilotChat } from './chunk-R75NEA76.mjs';
|
|
15
15
|
import './chunk-JD7BAH7U.mjs';
|
|
16
16
|
import './chunk-SPCZTZCY.mjs';
|
|
17
17
|
export { CopilotProvider, defaultCopilotContextCategories } from './chunk-QTET3FBV.mjs';
|
|
18
18
|
import './chunk-F2JIAPZQ.mjs';
|
|
19
19
|
import './chunk-VUY2K2DI.mjs';
|
|
20
20
|
export { CopilotContext } from './chunk-6A4PCNMZ.mjs';
|
|
21
|
-
import './chunk-
|
|
21
|
+
import './chunk-R2WOR4SW.mjs';
|
|
22
22
|
import './chunk-TKXPMMND.mjs';
|
|
23
23
|
import './chunk-VK25FIML.mjs';
|
|
24
24
|
import './chunk-YULKJPY3.mjs';
|
package/package.json
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public"
|
|
6
6
|
},
|
|
7
|
-
"version": "0.17.
|
|
7
|
+
"version": "0.17.1-alpha.1",
|
|
8
8
|
"sideEffects": false,
|
|
9
9
|
"main": "./dist/index.js",
|
|
10
10
|
"module": "./dist/index.mjs",
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"eventemitter3": "^5.0.1",
|
|
31
31
|
"nanoid": "^4.0.2",
|
|
32
32
|
"openai": "^4.6.0",
|
|
33
|
-
"@copilotkit/shared": "0.1.
|
|
33
|
+
"@copilotkit/shared": "0.1.1-alpha.1"
|
|
34
34
|
},
|
|
35
35
|
"scripts": {
|
|
36
36
|
"build": "tsup --treeshake",
|
package/src/hooks/use-chat.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
1
|
+
import { useRef, useState } from "react";
|
|
2
2
|
import { Message, Function, FunctionCallHandler } from "../types";
|
|
3
3
|
import { nanoid } from "nanoid";
|
|
4
4
|
import { ChatCompletionClient } from "../openai/chat-completion-client";
|
|
@@ -84,11 +84,15 @@ export function useChat(options: UseChatOptionsWithCopilotConfig): UseChatHelper
|
|
|
84
84
|
const [messages, setMessages] = useState<Message[]>([]);
|
|
85
85
|
const [input, setInput] = useState("");
|
|
86
86
|
const [isLoading, setIsLoading] = useState(false);
|
|
87
|
+
const abortControllerRef = useRef<AbortController>();
|
|
87
88
|
|
|
88
89
|
const runChatCompletion = async (messages: Message[]): Promise<Message> => {
|
|
89
90
|
return new Promise<Message>((resolve, reject) => {
|
|
90
91
|
setIsLoading(true);
|
|
91
92
|
|
|
93
|
+
const abortController = new AbortController();
|
|
94
|
+
abortControllerRef.current = abortController;
|
|
95
|
+
|
|
92
96
|
const assistantMessage: Message = {
|
|
93
97
|
id: nanoid(),
|
|
94
98
|
createdAt: new Date(),
|
|
@@ -108,8 +112,16 @@ export function useChat(options: UseChatOptionsWithCopilotConfig): UseChatHelper
|
|
|
108
112
|
client.off("end");
|
|
109
113
|
client.off("error");
|
|
110
114
|
client.off("function");
|
|
115
|
+
|
|
116
|
+
abortControllerRef.current = undefined;
|
|
111
117
|
};
|
|
112
118
|
|
|
119
|
+
abortController.signal.addEventListener("abort", () => {
|
|
120
|
+
setIsLoading(false);
|
|
121
|
+
cleanup();
|
|
122
|
+
reject(new DOMException("Aborted", "AbortError"));
|
|
123
|
+
});
|
|
124
|
+
|
|
113
125
|
client.on("content", (content) => {
|
|
114
126
|
assistantMessage.content += content;
|
|
115
127
|
setMessages([...messages, { ...assistantMessage }]);
|
|
@@ -144,6 +156,7 @@ export function useChat(options: UseChatOptionsWithCopilotConfig): UseChatHelper
|
|
|
144
156
|
messages: messagesWithContext,
|
|
145
157
|
functions: options.functions,
|
|
146
158
|
headers: options.headers,
|
|
159
|
+
signal: abortController.signal,
|
|
147
160
|
});
|
|
148
161
|
});
|
|
149
162
|
};
|
|
@@ -180,7 +193,7 @@ export function useChat(options: UseChatOptionsWithCopilotConfig): UseChatHelper
|
|
|
180
193
|
};
|
|
181
194
|
|
|
182
195
|
const stop = (): void => {
|
|
183
|
-
|
|
196
|
+
abortControllerRef.current?.abort();
|
|
184
197
|
};
|
|
185
198
|
|
|
186
199
|
return {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/hooks/use-chat.ts"],"names":["messages"],"mappings":";;;;;;;;;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D;AACA,oBAAY,CAAC,GAAGA,WAAU,mBAAK,iBAAkB,CAAC;AAElD,qBAAa,KAAK;AAClB,gBAAQ;AACR,gBAAQ,mBAAK,iBAAkB;AAAA,MACjC,EAAC;AAED,aAAO,MAAM;AAAA,QACX,eAAe,QAAQ;AAAA,QACvB,UAAU;AAAA,QACV,WAAW,QAAQ;AAAA,QACnB,SAAS,QAAQ;AAAA,MACnB,CAAC;AAAA,IACH,CAAC;AAAA,EACH;AAEA,QAAM,yCAAyC,CAAOA,cAAuC;AAC3F,UAAM,UAAU,MAAM,kBAAkBA,SAAQ;AAChD,QAAI,QAAQ,iBAAiB,QAAQ,gBAAgB;AACnD,YAAM,QAAQ,eAAeA,WAAU,QAAQ,aAAa;AAAA,IAC9D;AAAA,EACF;AAEA,QAAM,SAAS,CAAO,YAAoC;AACxD,QAAI,WAAW;AACb;AAAA,IACF;AACA,UAAM,cAAc,CAAC,GAAG,UAAU,OAAO;AACzC,gBAAY,WAAW;AACvB,WAAO,uCAAuC,WAAW;AAAA,EAC3D;AAEA,QAAM,SAAS,MAA2B;AACxC,QAAI,aAAa,SAAS,WAAW,GAAG;AACtC;AAAA,IACF;AACA,QAAI,cAAc,CAAC,GAAG,QAAQ;AAC9B,UAAM,cAAc,SAAS,SAAS,SAAS,CAAC;AAEhD,QAAI,YAAY,SAAS,aAAa;AACpC,oBAAc,YAAY,MAAM,GAAG,EAAE;AAAA,IACvC;AACA,gBAAY,WAAW;AAEvB,WAAO,uCAAuC,WAAW;AAAA,EAC3D;AAEA,QAAM,OAAO,MAAY;AACvB,UAAM,IAAI,MAAM,iBAAiB;AAAA,EACnC;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF","sourcesContent":["import { useState } from \"react\";\nimport { Message, Function, FunctionCallHandler } from \"../types\";\nimport { nanoid } from \"nanoid\";\nimport { ChatCompletionClient } from \"../openai/chat-completion-client\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport type UseChatOptions = {\n /**\n * The API endpoint that accepts a `{ messages: Message[] }` object and returns\n * a stream of tokens of the AI chat response. Defaults to `/api/chat`.\n */\n api?: string;\n /**\n * A unique identifier for the chat. If not provided, a random one will be\n * generated. When provided, the `useChat` hook with the same `id` will\n * have shared states across components.\n */\n id?: string;\n /**\n * System messages of the chat. Defaults to an empty array.\n */\n initialMessages?: Message[];\n /**\n * Callback function to be called when a function call is received.\n * If the function returns a `ChatRequest` object, the request will be sent\n * automatically to the API and will be used to update the chat.\n */\n onFunctionCall?: FunctionCallHandler;\n /**\n * HTTP headers to be sent with the API request.\n */\n headers?: Record<string, string> | Headers;\n /**\n * Extra body object to be sent with the API request.\n * @example\n * Send a `sessionId` to the API along with the messages.\n * ```js\n * useChat({\n * body: {\n * sessionId: '123',\n * }\n * })\n * ```\n */\n body?: object;\n /**\n * Function definitions to be sent to the API.\n */\n functions?: Function[];\n};\n\nexport type UseChatHelpers = {\n /** Current messages in the chat */\n messages: Message[];\n /**\n * Append a user message to the chat list. This triggers the API call to fetch\n * the assistant's response.\n * @param message The message to append\n */\n append: (message: Message) => Promise<void>;\n /**\n * Reload the last AI chat response for the given chat history. If the last\n * message isn't from the assistant, it will request the API to generate a\n * new response.\n */\n reload: () => Promise<void>;\n /**\n * Abort the current request immediately, keep the generated tokens if any.\n */\n stop: () => void;\n /** The current value of the input */\n input: string;\n /** setState-powered method to update the input value */\n setInput: React.Dispatch<React.SetStateAction<string>>;\n /** Whether the API request is in progress */\n isLoading: boolean;\n};\n\nexport type UseChatOptionsWithCopilotConfig = UseChatOptions & {\n copilotConfig: CopilotApiConfig;\n};\n\nexport function useChat(options: UseChatOptionsWithCopilotConfig): UseChatHelpers {\n const [messages, setMessages] = useState<Message[]>([]);\n const [input, setInput] = useState(\"\");\n const [isLoading, setIsLoading] = useState(false);\n\n const runChatCompletion = async (messages: Message[]): Promise<Message> => {\n return new Promise<Message>((resolve, reject) => {\n setIsLoading(true);\n\n const assistantMessage: Message = {\n id: nanoid(),\n createdAt: new Date(),\n content: \"\",\n role: \"assistant\",\n };\n\n // Assistant messages are always copied when using setState\n setMessages([...messages, { ...assistantMessage }]);\n\n const messagesWithContext = [...(options.initialMessages || []), ...messages];\n\n const client = new ChatCompletionClient({});\n\n const cleanup = () => {\n client.off(\"content\");\n client.off(\"end\");\n client.off(\"error\");\n client.off(\"function\");\n };\n\n client.on(\"content\", (content) => {\n assistantMessage.content += content;\n setMessages([...messages, { ...assistantMessage }]);\n });\n\n client.on(\"end\", () => {\n setIsLoading(false);\n cleanup();\n resolve({ ...assistantMessage });\n });\n\n client.on(\"error\", (error) => {\n setIsLoading(false);\n cleanup();\n reject(error);\n });\n\n client.on(\"function\", async (functionCall) => {\n assistantMessage.function_call = {\n name: functionCall.name,\n arguments: JSON.stringify(functionCall.arguments),\n };\n setMessages([...messages, { ...assistantMessage }]);\n // quit early if we get a function call\n setIsLoading(false);\n cleanup();\n resolve({ ...assistantMessage });\n });\n\n client.fetch({\n copilotConfig: options.copilotConfig,\n messages: messagesWithContext,\n functions: options.functions,\n headers: options.headers,\n });\n });\n };\n\n const runChatCompletionAndHandleFunctionCall = async (messages: Message[]): Promise<void> => {\n const message = await runChatCompletion(messages);\n if (message.function_call && options.onFunctionCall) {\n await options.onFunctionCall(messages, message.function_call);\n }\n };\n\n const append = async (message: Message): Promise<void> => {\n if (isLoading) {\n return;\n }\n const newMessages = [...messages, message];\n setMessages(newMessages);\n return runChatCompletionAndHandleFunctionCall(newMessages);\n };\n\n const reload = async (): Promise<void> => {\n if (isLoading || messages.length === 0) {\n return;\n }\n let newMessages = [...messages];\n const lastMessage = messages[messages.length - 1];\n\n if (lastMessage.role === \"assistant\") {\n newMessages = newMessages.slice(0, -1);\n }\n setMessages(newMessages);\n\n return runChatCompletionAndHandleFunctionCall(newMessages);\n };\n\n const stop = (): void => {\n throw new Error(\"Not implemented\");\n };\n\n return {\n messages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n"]}
|
|
File without changes
|