@copilotkit/react-core 1.5.0-tyler-reset-chat.0 → 1.5.1-custom-tag-pre.0

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.
Files changed (151) hide show
  1. package/CHANGELOG.md +168 -4
  2. package/README.md +2 -0
  3. package/dist/{chunk-USL3EHJB.mjs → chunk-6KHA5W5R.mjs} +2 -2
  4. package/dist/{chunk-ODN4H66E.mjs → chunk-7LRDVJH5.mjs} +6 -2
  5. package/dist/chunk-7LRDVJH5.mjs.map +1 -0
  6. package/dist/chunk-M3SYKJ3W.mjs +393 -0
  7. package/dist/chunk-M3SYKJ3W.mjs.map +1 -0
  8. package/dist/{chunk-3R4J2TPH.mjs → chunk-MKEQ2MJG.mjs} +29 -13
  9. package/dist/chunk-MKEQ2MJG.mjs.map +1 -0
  10. package/dist/{chunk-JR55I3FL.mjs → chunk-NYBGR2NK.mjs} +14 -13
  11. package/dist/chunk-NYBGR2NK.mjs.map +1 -0
  12. package/dist/{chunk-CZMEZR6F.mjs → chunk-OAF4ASJH.mjs} +36 -19
  13. package/dist/chunk-OAF4ASJH.mjs.map +1 -0
  14. package/dist/{chunk-3AYELZJS.mjs → chunk-ODAH4HNG.mjs} +2 -2
  15. package/dist/{chunk-3AYELZJS.mjs.map → chunk-ODAH4HNG.mjs.map} +1 -1
  16. package/dist/chunk-QCUP6HLK.mjs +37 -0
  17. package/dist/chunk-QCUP6HLK.mjs.map +1 -0
  18. package/dist/{chunk-2KCEHGSI.mjs → chunk-SFPANIOY.mjs} +99 -49
  19. package/dist/chunk-SFPANIOY.mjs.map +1 -0
  20. package/dist/{chunk-2JP64U3A.mjs → chunk-STZUYPHJ.mjs} +5 -1
  21. package/dist/chunk-STZUYPHJ.mjs.map +1 -0
  22. package/dist/{chunk-XUPO37VH.mjs → chunk-VPTC36RZ.mjs} +2 -2
  23. package/dist/{chunk-SEPYQHH7.mjs → chunk-VQDGBYWQ.mjs} +34 -28
  24. package/dist/chunk-VQDGBYWQ.mjs.map +1 -0
  25. package/dist/chunk-XERJQUHA.mjs +31 -0
  26. package/dist/chunk-XERJQUHA.mjs.map +1 -0
  27. package/dist/{chunk-6QKA3SNN.mjs → chunk-YL6V3QTN.mjs} +26 -8
  28. package/dist/chunk-YL6V3QTN.mjs.map +1 -0
  29. package/dist/components/copilot-provider/copilot-messages.d.ts +1 -0
  30. package/dist/components/copilot-provider/copilotkit-props.d.ts +5 -0
  31. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
  32. package/dist/components/copilot-provider/copilotkit.d.ts +1 -0
  33. package/dist/components/copilot-provider/copilotkit.js +175 -92
  34. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  35. package/dist/components/copilot-provider/copilotkit.mjs +5 -4
  36. package/dist/components/copilot-provider/index.d.ts +1 -0
  37. package/dist/components/copilot-provider/index.js +175 -92
  38. package/dist/components/copilot-provider/index.js.map +1 -1
  39. package/dist/components/copilot-provider/index.mjs +5 -4
  40. package/dist/components/error-boundary/error-boundary.d.ts +22 -0
  41. package/dist/components/error-boundary/error-boundary.js +183 -0
  42. package/dist/components/error-boundary/error-boundary.js.map +1 -0
  43. package/dist/components/error-boundary/error-boundary.mjs +12 -0
  44. package/dist/components/error-boundary/error-boundary.mjs.map +1 -0
  45. package/dist/components/error-boundary/error-utils.d.ts +11 -0
  46. package/dist/components/error-boundary/error-utils.js +177 -0
  47. package/dist/components/error-boundary/error-utils.js.map +1 -0
  48. package/dist/components/error-boundary/error-utils.mjs +13 -0
  49. package/dist/components/error-boundary/error-utils.mjs.map +1 -0
  50. package/dist/components/index.d.ts +1 -0
  51. package/dist/components/index.js +175 -92
  52. package/dist/components/index.js.map +1 -1
  53. package/dist/components/index.mjs +5 -4
  54. package/dist/components/toast/toast-provider.d.ts +2 -1
  55. package/dist/components/toast/toast-provider.js +76 -62
  56. package/dist/components/toast/toast-provider.js.map +1 -1
  57. package/dist/components/toast/toast-provider.mjs +1 -1
  58. package/dist/context/copilot-context.d.ts +9 -3
  59. package/dist/context/copilot-context.js +4 -0
  60. package/dist/context/copilot-context.js.map +1 -1
  61. package/dist/context/copilot-context.mjs +1 -1
  62. package/dist/context/index.d.ts +1 -1
  63. package/dist/context/index.js +4 -0
  64. package/dist/context/index.js.map +1 -1
  65. package/dist/context/index.mjs +1 -1
  66. package/dist/hooks/index.js +558 -308
  67. package/dist/hooks/index.js.map +1 -1
  68. package/dist/hooks/index.mjs +13 -11
  69. package/dist/hooks/use-chat.d.ts +11 -3
  70. package/dist/hooks/use-chat.js +435 -219
  71. package/dist/hooks/use-chat.js.map +1 -1
  72. package/dist/hooks/use-chat.mjs +4 -3
  73. package/dist/hooks/use-coagent-state-render.d.ts +2 -2
  74. package/dist/hooks/use-coagent-state-render.js +4 -0
  75. package/dist/hooks/use-coagent-state-render.js.map +1 -1
  76. package/dist/hooks/use-coagent-state-render.mjs +2 -2
  77. package/dist/hooks/use-coagent.d.ts +1 -1
  78. package/dist/hooks/use-coagent.js +514 -277
  79. package/dist/hooks/use-coagent.js.map +1 -1
  80. package/dist/hooks/use-coagent.mjs +9 -7
  81. package/dist/hooks/use-copilot-action.d.ts +12 -2
  82. package/dist/hooks/use-copilot-action.js +158 -16
  83. package/dist/hooks/use-copilot-action.js.map +1 -1
  84. package/dist/hooks/use-copilot-action.mjs +4 -2
  85. package/dist/hooks/use-copilot-chat.d.ts +1 -0
  86. package/dist/hooks/use-copilot-chat.js +487 -253
  87. package/dist/hooks/use-copilot-chat.js.map +1 -1
  88. package/dist/hooks/use-copilot-chat.mjs +8 -6
  89. package/dist/hooks/use-copilot-readable.js +4 -0
  90. package/dist/hooks/use-copilot-readable.js.map +1 -1
  91. package/dist/hooks/use-copilot-readable.mjs +2 -2
  92. package/dist/hooks/use-copilot-runtime-client.js +110 -4
  93. package/dist/hooks/use-copilot-runtime-client.js.map +1 -1
  94. package/dist/hooks/use-copilot-runtime-client.mjs +2 -2
  95. package/dist/hooks/use-make-copilot-document-readable.js +4 -0
  96. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  97. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  98. package/dist/index.d.ts +2 -2
  99. package/dist/index.js +630 -408
  100. package/dist/index.js.map +1 -1
  101. package/dist/index.mjs +14 -12
  102. package/dist/lib/copilot-task.d.ts +6 -1
  103. package/dist/lib/copilot-task.js +55 -17
  104. package/dist/lib/copilot-task.js.map +1 -1
  105. package/dist/lib/copilot-task.mjs +7 -5
  106. package/dist/lib/index.d.ts +1 -1
  107. package/dist/lib/index.js +55 -17
  108. package/dist/lib/index.js.map +1 -1
  109. package/dist/lib/index.mjs +7 -5
  110. package/dist/types/frontend-action.d.ts +21 -2
  111. package/dist/types/frontend-action.js +34 -0
  112. package/dist/types/frontend-action.js.map +1 -1
  113. package/dist/types/frontend-action.mjs +7 -0
  114. package/dist/types/index.d.ts +2 -1
  115. package/dist/types/index.js.map +1 -1
  116. package/dist/utils/extract.d.ts +3 -2
  117. package/dist/utils/extract.js +21 -3
  118. package/dist/utils/extract.js.map +1 -1
  119. package/dist/utils/extract.mjs +5 -4
  120. package/dist/utils/index.js +21 -3
  121. package/dist/utils/index.js.map +1 -1
  122. package/dist/utils/index.mjs +5 -4
  123. package/package.json +5 -5
  124. package/src/components/copilot-provider/copilotkit-props.tsx +6 -0
  125. package/src/components/copilot-provider/copilotkit.tsx +23 -1
  126. package/src/components/error-boundary/error-boundary.tsx +42 -0
  127. package/src/components/error-boundary/error-utils.tsx +95 -0
  128. package/src/components/toast/toast-provider.tsx +10 -49
  129. package/src/context/copilot-context.tsx +24 -8
  130. package/src/hooks/use-chat.ts +388 -279
  131. package/src/hooks/use-coagent-state-render.ts +2 -2
  132. package/src/hooks/use-coagent.ts +34 -28
  133. package/src/hooks/use-copilot-action.ts +50 -15
  134. package/src/hooks/use-copilot-chat.ts +30 -14
  135. package/src/hooks/use-copilot-runtime-client.ts +4 -0
  136. package/src/lib/copilot-task.ts +12 -9
  137. package/src/types/frontend-action.ts +55 -2
  138. package/src/types/index.ts +5 -1
  139. package/src/utils/extract.ts +4 -0
  140. package/dist/chunk-2JP64U3A.mjs.map +0 -1
  141. package/dist/chunk-2KCEHGSI.mjs.map +0 -1
  142. package/dist/chunk-3R4J2TPH.mjs.map +0 -1
  143. package/dist/chunk-6EN7J4V2.mjs +0 -317
  144. package/dist/chunk-6EN7J4V2.mjs.map +0 -1
  145. package/dist/chunk-6QKA3SNN.mjs.map +0 -1
  146. package/dist/chunk-CZMEZR6F.mjs.map +0 -1
  147. package/dist/chunk-JR55I3FL.mjs.map +0 -1
  148. package/dist/chunk-ODN4H66E.mjs.map +0 -1
  149. package/dist/chunk-SEPYQHH7.mjs.map +0 -1
  150. /package/dist/{chunk-USL3EHJB.mjs.map → chunk-6KHA5W5R.mjs.map} +0 -0
  151. /package/dist/{chunk-XUPO37VH.mjs.map → chunk-VPTC36RZ.mjs.map} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,16 +1,180 @@
1
1
  # ui
2
2
 
3
- ## 1.5.0-tyler-reset-chat.0
3
+ ## 1.5.1-custom-tag-pre.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 896c825: testing a prerelease
8
+ - 6dfa0d2: - feat: add temperature parameter support for LLM completions
9
+ - Updated dependencies [896c825]
10
+ - @copilotkit/runtime-client-gql@1.5.1-custom-tag-pre.0
11
+ - @copilotkit/shared@1.5.1-custom-tag-pre.0
12
+
13
+ ## 1.5.0
14
+
15
+ ### Minor Changes
16
+
17
+ - 1b47092: Synchronize LangGraph messages with CopilotKit
18
+
19
+ ### Patch Changes
20
+
21
+ - 00e9488: - fix: wait for renderAndWaitForResponse handler to be ready before rendering
22
+ - 1b47092: CoAgents v0.3 prerelease
23
+ - Updated dependencies [1b47092]
24
+ - Updated dependencies [1b47092]
25
+ - @copilotkit/runtime-client-gql@1.5.0
26
+ - @copilotkit/shared@1.5.0
27
+
28
+ ## 1.5.0-coagents-v0-3.0
4
29
 
5
30
  ### Minor Changes
6
31
 
7
- - Merging Markus' reset fixes with main + a new change for stopping an agent during execution of FE actions
32
+ - Synchronize LangGraph messages with CopilotKit
33
+
34
+ ### Patch Changes
35
+
36
+ - e66bce4: CoAgents v0.3 prerelease
37
+ - Updated dependencies
38
+ - Updated dependencies [e66bce4]
39
+ - @copilotkit/runtime-client-gql@1.5.0-coagents-v0-3.0
40
+ - @copilotkit/shared@1.5.0-coagents-v0-3.0
41
+
42
+ ## 1.4.8
43
+
44
+ ### Patch Changes
45
+
46
+ - - Better error handling
47
+ - Introduce new "EmptyLLMAdapter" for when using CoAgents
48
+ - Improve dev console help options
49
+ - Allow CopilotKit remote endpoint without agents
50
+ - Updated dependencies
51
+ - @copilotkit/runtime-client-gql@1.4.8
52
+ - @copilotkit/shared@1.4.8
53
+
54
+ ## 1.4.8-next.0
55
+
56
+ ### Patch Changes
57
+
58
+ - @copilotkit/runtime-client-gql@1.4.8-next.0
59
+ - @copilotkit/shared@1.4.8-next.0
60
+
61
+ ## 1.4.7
62
+
63
+ ### Patch Changes
64
+
65
+ - Fix broken build script before release
66
+ - Updated dependencies
67
+ - @copilotkit/runtime-client-gql@1.4.7
68
+ - @copilotkit/shared@1.4.7
69
+
70
+ ## 1.4.6
71
+
72
+ ### Patch Changes
73
+
74
+ - .
75
+
76
+ ## 1.4.5
77
+
78
+ ### Patch Changes
79
+
80
+ - testing release workflow
81
+ - Updated dependencies
82
+ - @copilotkit/runtime-client-gql@1.4.5
83
+ - @copilotkit/shared@1.4.5
84
+
85
+ ## 1.4.5-next.0
86
+
87
+ ### Patch Changes
88
+
89
+ - testing release workflow
90
+ - Updated dependencies
91
+ - @copilotkit/runtime-client-gql@1.4.5-next.0
92
+ - @copilotkit/shared@1.4.5-next.0
93
+
94
+ ## 1.4.4
95
+
96
+ ### Patch Changes
97
+
98
+ - e35e6ad: - test
99
+ - update config.json
100
+ - Merge remote-tracking branch 'origin/main' into feat/test-changeset-bot-1
101
+ - test
102
+ - test
103
+ - @copilotkit/runtime-client-gql@1.4.4
104
+ - @copilotkit/shared@1.4.4
105
+
106
+ ## 1.4.4-next.4
107
+
108
+ ### Patch Changes
109
+
110
+ - @copilotkit/runtime-client-gql@1.4.4-next.4
111
+ - @copilotkit/shared@1.4.4-next.4
112
+
113
+ ## 1.4.4-next.3
114
+
115
+ ### Patch Changes
116
+
117
+ - @copilotkit/runtime-client-gql@1.4.4-next.3
118
+ - @copilotkit/shared@1.4.4-next.3
119
+
120
+ ## 1.4.4-next.2
121
+
122
+ ### Patch Changes
123
+
124
+ - @copilotkit/runtime-client-gql@1.4.4-next.2
125
+ - @copilotkit/shared@1.4.4-next.2
126
+
127
+ ## 1.4.4-next.1
128
+
129
+ ### Patch Changes
130
+
131
+ - @copilotkit/runtime-client-gql@1.4.4-next.1
132
+ - @copilotkit/shared@1.4.4-next.1
133
+
134
+ ## 1.4.4-next.0
135
+
136
+ ### Patch Changes
137
+
138
+ - e35e6ad: - test
139
+ - update config.json
140
+ - Merge remote-tracking branch 'origin/main' into feat/test-changeset-bot-1
141
+ - test
142
+ - test
143
+ - @copilotkit/runtime-client-gql@1.4.4-next.0
144
+ - @copilotkit/shared@1.4.4-next.0
145
+
146
+ ## 1.4.3
147
+
148
+ ### Patch Changes
149
+
150
+ - c296282: - Better error surfacing when using LangGraph Platform streaming
151
+ - Ensure state is immediately set without using flushSync
152
+ - - Better error surfacing when using LangGraph Platform streaming
153
+ - Ensure state is immediately set without using flushSync
154
+ - Updated dependencies [c296282]
155
+ - Updated dependencies
156
+ - @copilotkit/runtime-client-gql@1.4.3
157
+ - @copilotkit/shared@1.4.3
158
+
159
+ ## 1.4.3-pre.0
160
+
161
+ ### Patch Changes
162
+
163
+ - - Better error surfacing when using LangGraph Platform streaming
164
+ - Ensure state is immediately set without using flushSync
165
+ - Updated dependencies
166
+ - @copilotkit/runtime-client-gql@1.4.3-pre.0
167
+ - @copilotkit/shared@1.4.3-pre.0
168
+
169
+ ## 1.4.2
8
170
 
9
171
  ### Patch Changes
10
172
 
173
+ - - Make sure agent state is set immediately (#1077)
174
+ - Support running an agent without messages (#1075)
11
175
  - Updated dependencies
12
- - @copilotkit/runtime-client-gql@1.5.0-tyler-reset-chat.0
13
- - @copilotkit/shared@1.5.0-tyler-reset-chat.0
176
+ - @copilotkit/runtime-client-gql@1.4.2
177
+ - @copilotkit/shared@1.4.2
14
178
 
15
179
  ## 1.4.1
16
180
 
package/README.md CHANGED
@@ -12,6 +12,8 @@
12
12
 
13
13
  <br/>
14
14
 
15
+ <!-- -->
16
+
15
17
  <div align="center">
16
18
  <a href="https://www.npmjs.com/package/@copilotkit/react-core" target="_blank">
17
19
  <img src="https://img.shields.io/npm/v/%40copilotkit%2Freact-core?logo=npm&logoColor=%23FFFFFF&label=Version&color=%236963ff" alt="NPM">
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  useCopilotContext
3
- } from "./chunk-2JP64U3A.mjs";
3
+ } from "./chunk-STZUYPHJ.mjs";
4
4
 
5
5
  // src/hooks/use-copilot-readable.ts
6
6
  import { useEffect, useRef } from "react";
@@ -25,4 +25,4 @@ function useCopilotReadable({ description, value, parentId, categories, convert
25
25
  export {
26
26
  useCopilotReadable
27
27
  };
28
- //# sourceMappingURL=chunk-USL3EHJB.mjs.map
28
+ //# sourceMappingURL=chunk-6KHA5W5R.mjs.map
@@ -1,6 +1,7 @@
1
1
  import {
2
+ useErrorToast,
2
3
  useToast
3
- } from "./chunk-2KCEHGSI.mjs";
4
+ } from "./chunk-SFPANIOY.mjs";
4
5
  import {
5
6
  __spreadProps,
6
7
  __spreadValues
@@ -13,11 +14,14 @@ import {
13
14
  import { useMemo } from "react";
14
15
  var useCopilotRuntimeClient = (options) => {
15
16
  const { addGraphQLErrorsToast } = useToast();
17
+ const addErrorToast = useErrorToast();
16
18
  const runtimeClient = useMemo(() => {
17
19
  return new CopilotRuntimeClient(__spreadProps(__spreadValues({}, options), {
18
20
  handleGQLErrors: (error) => {
19
21
  if (error.graphQLErrors.length) {
20
22
  addGraphQLErrorsToast(error.graphQLErrors);
23
+ } else {
24
+ addErrorToast([error]);
21
25
  }
22
26
  }
23
27
  }));
@@ -28,4 +32,4 @@ var useCopilotRuntimeClient = (options) => {
28
32
  export {
29
33
  useCopilotRuntimeClient
30
34
  };
31
- //# sourceMappingURL=chunk-ODN4H66E.mjs.map
35
+ //# sourceMappingURL=chunk-7LRDVJH5.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-copilot-runtime-client.ts"],"sourcesContent":["import {\n CopilotRuntimeClient,\n CopilotRuntimeClientOptions,\n GraphQLError,\n} from \"@copilotkit/runtime-client-gql\";\nimport { useToast } from \"../components/toast/toast-provider\";\nimport { useMemo } from \"react\";\nimport { useErrorToast } from \"../components/error-boundary/error-utils\";\n\nexport const useCopilotRuntimeClient = (options: CopilotRuntimeClientOptions) => {\n const { addGraphQLErrorsToast } = useToast();\n const addErrorToast = useErrorToast();\n\n const runtimeClient = useMemo(() => {\n return new CopilotRuntimeClient({\n ...options,\n handleGQLErrors: (error) => {\n if ((error as any).graphQLErrors.length) {\n addGraphQLErrorsToast((error as any).graphQLErrors as GraphQLError[]);\n } else {\n addErrorToast([error]);\n }\n },\n });\n }, [options, addGraphQLErrorsToast]);\n\n return runtimeClient;\n};\n"],"mappings":";;;;;;;;;;AAAA;AAAA,EACE;AAAA,OAGK;AAEP,SAAS,eAAe;AAGjB,IAAM,0BAA0B,CAAC,YAAyC;AAC/E,QAAM,EAAE,sBAAsB,IAAI,SAAS;AAC3C,QAAM,gBAAgB,cAAc;AAEpC,QAAM,gBAAgB,QAAQ,MAAM;AAClC,WAAO,IAAI,qBAAqB,iCAC3B,UAD2B;AAAA,MAE9B,iBAAiB,CAAC,UAAU;AAC1B,YAAK,MAAc,cAAc,QAAQ;AACvC,gCAAuB,MAAc,aAA+B;AAAA,QACtE,OAAO;AACL,wBAAc,CAAC,KAAK,CAAC;AAAA,QACvB;AAAA,MACF;AAAA,IACF,EAAC;AAAA,EACH,GAAG,CAAC,SAAS,qBAAqB,CAAC;AAEnC,SAAO;AACT;","names":[]}
@@ -0,0 +1,393 @@
1
+ import {
2
+ processActionsForRuntimeRequest
3
+ } from "./chunk-XERJQUHA.mjs";
4
+ import {
5
+ useCopilotRuntimeClient
6
+ } from "./chunk-7LRDVJH5.mjs";
7
+ import {
8
+ useAsyncCallback,
9
+ useToast
10
+ } from "./chunk-SFPANIOY.mjs";
11
+ import {
12
+ __async,
13
+ __spreadProps,
14
+ __spreadValues
15
+ } from "./chunk-SKC7AJIV.mjs";
16
+
17
+ // src/hooks/use-chat.ts
18
+ import { useRef } from "react";
19
+ import {
20
+ COPILOT_CLOUD_PUBLIC_API_KEY_HEADER
21
+ } from "@copilotkit/shared";
22
+ import {
23
+ TextMessage,
24
+ ResultMessage,
25
+ convertMessagesToGqlInput,
26
+ filterAdjacentAgentStateMessages,
27
+ filterAgentStateMessages,
28
+ convertGqlOutputToMessages,
29
+ MessageStatusCode,
30
+ MessageRole,
31
+ Role,
32
+ CopilotRequestType,
33
+ loadMessagesFromJsonRepresentation
34
+ } from "@copilotkit/runtime-client-gql";
35
+ function useChat(options) {
36
+ const {
37
+ messages,
38
+ setMessages,
39
+ makeSystemMessageCallback,
40
+ copilotConfig,
41
+ setIsLoading,
42
+ initialMessages,
43
+ isLoading,
44
+ actions,
45
+ onFunctionCall,
46
+ onCoAgentStateRender,
47
+ setCoagentStatesWithRef,
48
+ coagentStatesRef,
49
+ agentSession,
50
+ setAgentSession,
51
+ threadId,
52
+ setThreadId,
53
+ runId,
54
+ setRunId,
55
+ chatAbortControllerRef
56
+ } = options;
57
+ const { addGraphQLErrorsToast } = useToast();
58
+ const runChatCompletionRef = useRef();
59
+ const agentSessionRef = useRef(agentSession);
60
+ agentSessionRef.current = agentSession;
61
+ const threadIdRef = useRef(threadId);
62
+ threadIdRef.current = threadId;
63
+ const runIdRef = useRef(runId);
64
+ runIdRef.current = runId;
65
+ const publicApiKey = copilotConfig.publicApiKey;
66
+ const headers = __spreadValues(__spreadValues({}, copilotConfig.headers || {}), publicApiKey ? { [COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
67
+ const runtimeClient = useCopilotRuntimeClient({
68
+ url: copilotConfig.chatApiEndpoint,
69
+ publicApiKey: copilotConfig.publicApiKey,
70
+ headers,
71
+ credentials: copilotConfig.credentials
72
+ });
73
+ const runChatCompletion = useAsyncCallback(
74
+ (previousMessages) => __async(this, null, function* () {
75
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l;
76
+ setIsLoading(true);
77
+ let newMessages = [
78
+ new TextMessage({
79
+ content: "",
80
+ role: Role.Assistant
81
+ })
82
+ ];
83
+ chatAbortControllerRef.current = new AbortController();
84
+ setMessages([...previousMessages, ...newMessages]);
85
+ const systemMessage = makeSystemMessageCallback();
86
+ const messagesWithContext = [systemMessage, ...initialMessages || [], ...previousMessages];
87
+ const isAgentRun = agentSessionRef.current !== null;
88
+ const stream = runtimeClient.asStream(
89
+ runtimeClient.generateCopilotResponse({
90
+ data: __spreadProps(__spreadValues(__spreadProps(__spreadValues({
91
+ frontend: {
92
+ actions: processActionsForRuntimeRequest(actions),
93
+ url: window.location.href
94
+ },
95
+ threadId: threadIdRef.current,
96
+ runId: runIdRef.current,
97
+ messages: convertMessagesToGqlInput(filterAgentStateMessages(messagesWithContext))
98
+ }, copilotConfig.cloud ? {
99
+ cloud: __spreadValues({}, ((_c = (_b = (_a = copilotConfig.cloud.guardrails) == null ? void 0 : _a.input) == null ? void 0 : _b.restrictToTopic) == null ? void 0 : _c.enabled) ? {
100
+ guardrails: {
101
+ inputValidationRules: {
102
+ allowList: copilotConfig.cloud.guardrails.input.restrictToTopic.validTopics,
103
+ denyList: copilotConfig.cloud.guardrails.input.restrictToTopic.invalidTopics
104
+ }
105
+ }
106
+ } : {})
107
+ } : {}), {
108
+ metadata: {
109
+ requestType: CopilotRequestType.Chat
110
+ }
111
+ }), agentSessionRef.current ? {
112
+ agentSession: agentSessionRef.current
113
+ } : {}), {
114
+ agentStates: Object.values(coagentStatesRef.current).map((state) => ({
115
+ agentName: state.name,
116
+ state: JSON.stringify(state.state)
117
+ })),
118
+ forwardedParameters: options.forwardedParameters || {}
119
+ }),
120
+ properties: copilotConfig.properties,
121
+ signal: (_d = chatAbortControllerRef.current) == null ? void 0 : _d.signal
122
+ })
123
+ );
124
+ const guardrailsEnabled = ((_g = (_f = (_e = copilotConfig.cloud) == null ? void 0 : _e.guardrails) == null ? void 0 : _f.input) == null ? void 0 : _g.restrictToTopic.enabled) || false;
125
+ const reader = stream.getReader();
126
+ let executedCoAgentStateRenders = [];
127
+ let followUp = void 0;
128
+ let messages2 = [];
129
+ let syncedMessages = [];
130
+ try {
131
+ while (true) {
132
+ let done, value;
133
+ try {
134
+ const readResult = yield reader.read();
135
+ done = readResult.done;
136
+ value = readResult.value;
137
+ } catch (readError) {
138
+ break;
139
+ }
140
+ if (done) {
141
+ if (chatAbortControllerRef.current.signal.aborted) {
142
+ return [];
143
+ }
144
+ break;
145
+ }
146
+ if (!(value == null ? void 0 : value.generateCopilotResponse)) {
147
+ continue;
148
+ }
149
+ threadIdRef.current = value.generateCopilotResponse.threadId || null;
150
+ runIdRef.current = value.generateCopilotResponse.runId || null;
151
+ setThreadId(threadIdRef.current);
152
+ setRunId(runIdRef.current);
153
+ messages2 = convertGqlOutputToMessages(
154
+ filterAdjacentAgentStateMessages(value.generateCopilotResponse.messages)
155
+ );
156
+ if (messages2.length === 0) {
157
+ continue;
158
+ }
159
+ newMessages = [];
160
+ if (((_h = value.generateCopilotResponse.status) == null ? void 0 : _h.__typename) === "FailedResponseStatus" && value.generateCopilotResponse.status.reason === "GUARDRAILS_VALIDATION_FAILED") {
161
+ newMessages = [
162
+ new TextMessage({
163
+ role: MessageRole.Assistant,
164
+ content: ((_i = value.generateCopilotResponse.status.details) == null ? void 0 : _i.guardrailsReason) || ""
165
+ })
166
+ ];
167
+ setMessages([...previousMessages, ...newMessages]);
168
+ break;
169
+ } else {
170
+ newMessages = [...messages2];
171
+ for (const message of messages2) {
172
+ if (message.isAgentStateMessage() && !message.active && !executedCoAgentStateRenders.includes(message.id) && onCoAgentStateRender) {
173
+ if (guardrailsEnabled && value.generateCopilotResponse.status === void 0) {
174
+ break;
175
+ }
176
+ yield onCoAgentStateRender({
177
+ name: message.agentName,
178
+ nodeName: message.nodeName,
179
+ state: message.state
180
+ });
181
+ executedCoAgentStateRenders.push(message.id);
182
+ }
183
+ }
184
+ const lastAgentStateMessage = [...messages2].reverse().find((message) => message.isAgentStateMessage());
185
+ if (lastAgentStateMessage) {
186
+ if (lastAgentStateMessage.state.messages && lastAgentStateMessage.state.messages.length > 0) {
187
+ syncedMessages = loadMessagesFromJsonRepresentation(
188
+ lastAgentStateMessage.state.messages
189
+ );
190
+ }
191
+ setCoagentStatesWithRef((prevAgentStates) => __spreadProps(__spreadValues({}, prevAgentStates), {
192
+ [lastAgentStateMessage.agentName]: {
193
+ name: lastAgentStateMessage.agentName,
194
+ state: lastAgentStateMessage.state,
195
+ running: lastAgentStateMessage.running,
196
+ active: lastAgentStateMessage.active,
197
+ threadId: lastAgentStateMessage.threadId,
198
+ nodeName: lastAgentStateMessage.nodeName,
199
+ runId: lastAgentStateMessage.runId
200
+ }
201
+ }));
202
+ if (lastAgentStateMessage.running) {
203
+ setAgentSession({
204
+ threadId: lastAgentStateMessage.threadId,
205
+ agentName: lastAgentStateMessage.agentName,
206
+ nodeName: lastAgentStateMessage.nodeName
207
+ });
208
+ } else {
209
+ setAgentSession(null);
210
+ }
211
+ }
212
+ }
213
+ if (newMessages.length > 0) {
214
+ setMessages([...previousMessages, ...newMessages]);
215
+ }
216
+ }
217
+ const finalMessages = constructFinalMessages(syncedMessages, previousMessages, newMessages);
218
+ let didExecuteAction = false;
219
+ if (onFunctionCall) {
220
+ const lastMessages = [];
221
+ for (let i = finalMessages.length - 1; i >= 0; i--) {
222
+ const message = finalMessages[i];
223
+ if (message.isActionExecutionMessage() && message.status.code !== MessageStatusCode.Pending) {
224
+ lastMessages.unshift(message);
225
+ } else {
226
+ break;
227
+ }
228
+ }
229
+ for (const message of lastMessages) {
230
+ setMessages(finalMessages);
231
+ const action = actions.find((action2) => action2.name === message.name);
232
+ if (action) {
233
+ followUp = action.followUp;
234
+ let result;
235
+ try {
236
+ result = yield Promise.race([
237
+ onFunctionCall({
238
+ messages: previousMessages,
239
+ name: message.name,
240
+ args: message.arguments
241
+ }),
242
+ new Promise(
243
+ (resolve) => {
244
+ var _a2;
245
+ return (_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.addEventListener(
246
+ "abort",
247
+ () => resolve("Operation was aborted by the user")
248
+ );
249
+ }
250
+ ),
251
+ // if the user stopped generation, we also abort consecutive actions
252
+ new Promise((resolve) => {
253
+ var _a2;
254
+ if ((_a2 = chatAbortControllerRef.current) == null ? void 0 : _a2.signal.aborted) {
255
+ resolve("Operation was aborted by the user");
256
+ }
257
+ })
258
+ ]);
259
+ } catch (e) {
260
+ result = `Failed to execute action ${message.name}`;
261
+ console.error(`Failed to execute action ${message.name}: ${e}`);
262
+ }
263
+ didExecuteAction = true;
264
+ const messageIndex = finalMessages.findIndex((msg) => msg.id === message.id);
265
+ finalMessages.splice(
266
+ messageIndex + 1,
267
+ 0,
268
+ new ResultMessage({
269
+ id: "result-" + message.id,
270
+ result: ResultMessage.encodeResult(result),
271
+ actionExecutionId: message.id,
272
+ actionName: message.name
273
+ })
274
+ );
275
+ }
276
+ }
277
+ setMessages(finalMessages);
278
+ }
279
+ if (
280
+ // if followUp is not explicitly false
281
+ followUp !== false && // and we executed an action
282
+ (didExecuteAction || // the last message is a server side result
283
+ !isAgentRun && finalMessages.length && finalMessages[finalMessages.length - 1].isResultMessage()) && // the user did not stop generation
284
+ !((_j = chatAbortControllerRef.current) == null ? void 0 : _j.signal.aborted)
285
+ ) {
286
+ yield new Promise((resolve) => setTimeout(resolve, 10));
287
+ return yield runChatCompletionRef.current(finalMessages);
288
+ } else if ((_k = chatAbortControllerRef.current) == null ? void 0 : _k.signal.aborted) {
289
+ const repairedMessages = finalMessages.filter((message, actionExecutionIndex) => {
290
+ if (message.isActionExecutionMessage()) {
291
+ return finalMessages.find(
292
+ (msg, resultIndex) => msg.isResultMessage() && msg.actionExecutionId === message.id && resultIndex === actionExecutionIndex + 1
293
+ );
294
+ }
295
+ return true;
296
+ });
297
+ const repairedMessageIds = repairedMessages.map((message) => message.id);
298
+ setMessages(repairedMessages);
299
+ if ((_l = agentSessionRef.current) == null ? void 0 : _l.nodeName) {
300
+ setAgentSession({
301
+ threadId: agentSessionRef.current.threadId,
302
+ agentName: agentSessionRef.current.agentName,
303
+ nodeName: "__end__"
304
+ });
305
+ }
306
+ return newMessages.filter((message) => repairedMessageIds.includes(message.id));
307
+ } else {
308
+ return newMessages.slice();
309
+ }
310
+ } finally {
311
+ setIsLoading(false);
312
+ }
313
+ }),
314
+ [
315
+ messages,
316
+ setMessages,
317
+ makeSystemMessageCallback,
318
+ copilotConfig,
319
+ setIsLoading,
320
+ initialMessages,
321
+ isLoading,
322
+ actions,
323
+ onFunctionCall,
324
+ onCoAgentStateRender,
325
+ setCoagentStatesWithRef,
326
+ coagentStatesRef,
327
+ agentSession,
328
+ setAgentSession
329
+ ]
330
+ );
331
+ runChatCompletionRef.current = runChatCompletion;
332
+ const runChatCompletionAndHandleFunctionCall = useAsyncCallback(
333
+ (messages2) => __async(this, null, function* () {
334
+ yield runChatCompletionRef.current(messages2);
335
+ }),
336
+ [messages]
337
+ );
338
+ const append = useAsyncCallback(
339
+ (message) => __async(this, null, function* () {
340
+ if (isLoading) {
341
+ return;
342
+ }
343
+ const newMessages = [...messages, message];
344
+ setMessages(newMessages);
345
+ return runChatCompletionAndHandleFunctionCall(newMessages);
346
+ }),
347
+ [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]
348
+ );
349
+ const reload = useAsyncCallback(() => __async(this, null, function* () {
350
+ if (isLoading || messages.length === 0) {
351
+ return;
352
+ }
353
+ let newMessages = [...messages];
354
+ const lastMessage = messages[messages.length - 1];
355
+ if (lastMessage.isTextMessage() && lastMessage.role === "assistant") {
356
+ newMessages = newMessages.slice(0, -1);
357
+ }
358
+ setMessages(newMessages);
359
+ return runChatCompletionAndHandleFunctionCall(newMessages);
360
+ }), [isLoading, messages, setMessages, runChatCompletionAndHandleFunctionCall]);
361
+ const stop = () => {
362
+ var _a;
363
+ (_a = chatAbortControllerRef.current) == null ? void 0 : _a.abort("Stop was called");
364
+ };
365
+ return {
366
+ append,
367
+ reload,
368
+ stop,
369
+ runChatCompletion: () => runChatCompletionRef.current(messages)
370
+ };
371
+ }
372
+ function constructFinalMessages(syncedMessages, previousMessages, newMessages) {
373
+ const finalMessages = syncedMessages.length > 0 ? [...syncedMessages] : [...previousMessages, ...newMessages];
374
+ if (syncedMessages.length > 0) {
375
+ const messagesWithAgentState = [...previousMessages, ...newMessages];
376
+ let previousMessageId = void 0;
377
+ for (const message of messagesWithAgentState) {
378
+ if (message.isAgentStateMessage()) {
379
+ const index = finalMessages.findIndex((msg) => msg.id === previousMessageId);
380
+ if (index !== -1) {
381
+ finalMessages.splice(index + 1, 0, message);
382
+ }
383
+ }
384
+ previousMessageId = message.id;
385
+ }
386
+ }
387
+ return finalMessages;
388
+ }
389
+
390
+ export {
391
+ useChat
392
+ };
393
+ //# sourceMappingURL=chunk-M3SYKJ3W.mjs.map