@copilotkit/react-core 0.25.0-mme-cloud.4 → 0.25.0-multi-feature-usecopilotreadable.3

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 (137) hide show
  1. package/.turbo/turbo-build.log +237 -216
  2. package/CHANGELOG.md +12 -23
  3. package/dist/{chunk-CGT3AVYT.mjs → chunk-3WKJ25L3.mjs} +3 -3
  4. package/dist/chunk-3WKJ25L3.mjs.map +1 -0
  5. package/dist/chunk-5L5PJJNT.mjs +28 -0
  6. package/dist/chunk-5L5PJJNT.mjs.map +1 -0
  7. package/dist/{chunk-IR5I4Q6R.mjs → chunk-75RLNHDB.mjs} +5 -5
  8. package/dist/chunk-75RLNHDB.mjs.map +1 -0
  9. package/dist/{chunk-SFV4PSJM.mjs → chunk-BCKOSVRB.mjs} +7 -11
  10. package/dist/chunk-BCKOSVRB.mjs.map +1 -0
  11. package/dist/{chunk-6ARSX7BW.mjs → chunk-BGB5G33N.mjs} +2 -2
  12. package/dist/{chunk-NQKBX6UJ.mjs → chunk-EABGYUTZ.mjs} +2 -2
  13. package/dist/chunk-EABGYUTZ.mjs.map +1 -0
  14. package/dist/chunk-EYKDJBDE.mjs +1 -0
  15. package/dist/chunk-F2JIAPZQ.mjs +14 -0
  16. package/dist/chunk-F2JIAPZQ.mjs.map +1 -0
  17. package/dist/{chunk-DW3HRHJB.mjs → chunk-FCEPEM4B.mjs} +3 -3
  18. package/dist/{chunk-VYD7RO32.mjs → chunk-H3FQWB4B.mjs} +13 -44
  19. package/dist/chunk-H3FQWB4B.mjs.map +1 -0
  20. package/dist/{chunk-PC6FY3LD.mjs → chunk-H6V2C2AN.mjs} +2 -2
  21. package/dist/chunk-ISKBWE4O.mjs +13 -0
  22. package/dist/chunk-ISKBWE4O.mjs.map +1 -0
  23. package/dist/{chunk-O35YOIHC.mjs → chunk-KIKHLN4X.mjs} +1 -3
  24. package/dist/chunk-KIKHLN4X.mjs.map +1 -0
  25. package/dist/{chunk-UNYZRMB2.mjs → chunk-L45P674M.mjs} +6 -6
  26. package/dist/chunk-L45P674M.mjs.map +1 -0
  27. package/dist/{chunk-MESZST5T.mjs → chunk-X3FZ6GTK.mjs} +16 -8
  28. package/dist/chunk-X3FZ6GTK.mjs.map +1 -0
  29. package/dist/components/copilot-provider/copilotkit-props.d.ts +1 -12
  30. package/dist/components/copilot-provider/copilotkit-props.js.map +1 -1
  31. package/dist/components/copilot-provider/copilotkit.d.ts +17 -0
  32. package/dist/components/copilot-provider/copilotkit.js +17 -40
  33. package/dist/components/copilot-provider/copilotkit.js.map +1 -1
  34. package/dist/components/copilot-provider/copilotkit.mjs +3 -2
  35. package/dist/components/copilot-provider/index.js +17 -40
  36. package/dist/components/copilot-provider/index.js.map +1 -1
  37. package/dist/components/copilot-provider/index.mjs +3 -2
  38. package/dist/components/copilot-provider/standard-copilot-api-config.d.ts +23 -0
  39. package/dist/components/copilot-provider/standard-copilot-api-config.js +38 -0
  40. package/dist/components/copilot-provider/standard-copilot-api-config.js.map +1 -0
  41. package/dist/components/copilot-provider/standard-copilot-api-config.mjs +8 -0
  42. package/dist/components/copilot-provider/standard-copilot-api-config.mjs.map +1 -0
  43. package/dist/components/index.js +17 -40
  44. package/dist/components/index.js.map +1 -1
  45. package/dist/components/index.mjs +3 -2
  46. package/dist/context/copilot-context.d.ts +1 -11
  47. package/dist/context/copilot-context.js +0 -2
  48. package/dist/context/copilot-context.js.map +1 -1
  49. package/dist/context/copilot-context.mjs +1 -1
  50. package/dist/context/index.js +0 -2
  51. package/dist/context/index.js.map +1 -1
  52. package/dist/context/index.mjs +1 -1
  53. package/dist/hooks/index.d.ts +1 -0
  54. package/dist/hooks/index.js +47 -17
  55. package/dist/hooks/index.js.map +1 -1
  56. package/dist/hooks/index.mjs +20 -14
  57. package/dist/hooks/use-chat.d.ts +2 -8
  58. package/dist/hooks/use-chat.js +15 -6
  59. package/dist/hooks/use-chat.js.map +1 -1
  60. package/dist/hooks/use-chat.mjs +2 -2
  61. package/dist/hooks/use-copilot-action-implementation.d.ts +7 -0
  62. package/dist/hooks/use-copilot-action-implementation.js +140 -0
  63. package/dist/hooks/use-copilot-action-implementation.js.map +1 -0
  64. package/dist/hooks/use-copilot-action-implementation.mjs +9 -0
  65. package/dist/hooks/use-copilot-action-implementation.mjs.map +1 -0
  66. package/dist/hooks/use-copilot-action.d.ts +1 -1
  67. package/dist/hooks/use-copilot-action.js +9 -4
  68. package/dist/hooks/use-copilot-action.js.map +1 -1
  69. package/dist/hooks/use-copilot-action.mjs +3 -2
  70. package/dist/hooks/use-copilot-chat.js +18 -15
  71. package/dist/hooks/use-copilot-chat.js.map +1 -1
  72. package/dist/hooks/use-copilot-chat.mjs +6 -5
  73. package/dist/hooks/use-copilot-readable.d.ts +33 -0
  74. package/dist/hooks/use-copilot-readable.js +121 -0
  75. package/dist/hooks/use-copilot-readable.js.map +1 -0
  76. package/dist/hooks/use-copilot-readable.mjs +9 -0
  77. package/dist/hooks/use-copilot-readable.mjs.map +1 -0
  78. package/dist/hooks/use-make-copilot-actionable.js +0 -2
  79. package/dist/hooks/use-make-copilot-actionable.js.map +1 -1
  80. package/dist/hooks/use-make-copilot-actionable.mjs +2 -2
  81. package/dist/hooks/use-make-copilot-document-readable.js +0 -2
  82. package/dist/hooks/use-make-copilot-document-readable.js.map +1 -1
  83. package/dist/hooks/use-make-copilot-document-readable.mjs +2 -2
  84. package/dist/hooks/use-make-copilot-readable.d.ts +1 -5
  85. package/dist/hooks/use-make-copilot-readable.js +0 -2
  86. package/dist/hooks/use-make-copilot-readable.js.map +1 -1
  87. package/dist/hooks/use-make-copilot-readable.mjs +2 -2
  88. package/dist/index.d.ts +1 -0
  89. package/dist/index.js +75 -66
  90. package/dist/index.js.map +1 -1
  91. package/dist/index.mjs +25 -19
  92. package/dist/lib/copilot-task.d.ts +7 -2
  93. package/dist/lib/copilot-task.js +5 -5
  94. package/dist/lib/copilot-task.js.map +1 -1
  95. package/dist/lib/copilot-task.mjs +5 -4
  96. package/dist/lib/index.js +5 -5
  97. package/dist/lib/index.js.map +1 -1
  98. package/dist/lib/index.mjs +5 -4
  99. package/dist/openai-assistants/hooks/index.js +0 -2
  100. package/dist/openai-assistants/hooks/index.js.map +1 -1
  101. package/dist/openai-assistants/hooks/index.mjs +4 -3
  102. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js +0 -2
  103. package/dist/openai-assistants/hooks/use-copilot-chat-v2.js.map +1 -1
  104. package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +4 -3
  105. package/dist/openai-assistants/index.js +0 -2
  106. package/dist/openai-assistants/index.js.map +1 -1
  107. package/dist/openai-assistants/index.mjs +4 -3
  108. package/dist/utils/fetch-chat-completion.js +2 -2
  109. package/dist/utils/fetch-chat-completion.js.map +1 -1
  110. package/dist/utils/fetch-chat-completion.mjs +1 -1
  111. package/package.json +4 -4
  112. package/src/components/copilot-provider/copilotkit-props.tsx +1 -14
  113. package/src/components/copilot-provider/copilotkit.tsx +25 -47
  114. package/src/components/copilot-provider/standard-copilot-api-config.tsx +28 -0
  115. package/src/context/copilot-context.tsx +1 -21
  116. package/src/hooks/index.ts +1 -0
  117. package/src/hooks/use-chat.ts +15 -16
  118. package/src/hooks/use-copilot-action-implementation.ts +60 -0
  119. package/src/hooks/use-copilot-action.ts +10 -58
  120. package/src/hooks/use-copilot-chat.ts +1 -5
  121. package/src/hooks/use-copilot-readable.ts +60 -0
  122. package/src/hooks/use-make-copilot-readable.ts +1 -5
  123. package/src/lib/copilot-task.ts +12 -5
  124. package/src/utils/fetch-chat-completion.ts +0 -1
  125. package/dist/chunk-CGT3AVYT.mjs.map +0 -1
  126. package/dist/chunk-IOP6JX34.mjs +0 -1
  127. package/dist/chunk-IR5I4Q6R.mjs.map +0 -1
  128. package/dist/chunk-MESZST5T.mjs.map +0 -1
  129. package/dist/chunk-NQKBX6UJ.mjs.map +0 -1
  130. package/dist/chunk-O35YOIHC.mjs.map +0 -1
  131. package/dist/chunk-SFV4PSJM.mjs.map +0 -1
  132. package/dist/chunk-UNYZRMB2.mjs.map +0 -1
  133. package/dist/chunk-VYD7RO32.mjs.map +0 -1
  134. /package/dist/{chunk-6ARSX7BW.mjs.map → chunk-BGB5G33N.mjs.map} +0 -0
  135. /package/dist/{chunk-IOP6JX34.mjs.map → chunk-EYKDJBDE.mjs.map} +0 -0
  136. /package/dist/{chunk-DW3HRHJB.mjs.map → chunk-FCEPEM4B.mjs.map} +0 -0
  137. /package/dist/{chunk-PC6FY3LD.mjs.map → chunk-H6V2C2AN.mjs.map} +0 -0
package/CHANGELOG.md CHANGED
@@ -1,59 +1,48 @@
1
1
  # ui
2
2
 
3
- ## 0.25.0-mme-cloud.4
3
+ ## 0.25.0-multi-feature-usecopilotreadable.3
4
4
 
5
5
  ### Minor Changes
6
6
 
7
- - Include reason in guardrails response
7
+ - multiple new features, including new useCopilotReadable
8
8
 
9
9
  ### Patch Changes
10
10
 
11
11
  - Updated dependencies
12
- - @copilotkit/shared@0.9.0-mme-cloud.4
12
+ - @copilotkit/shared@0.9.0-multi-feature-usecopilotreadable.3
13
13
 
14
- ## 0.25.0-mme-cloud.3
14
+ ## 0.25.0-function-calling-fixes.2
15
15
 
16
16
  ### Minor Changes
17
17
 
18
- - Retrieve public API key
18
+ - fix backend function calling return values
19
19
 
20
20
  ### Patch Changes
21
21
 
22
22
  - Updated dependencies
23
- - @copilotkit/shared@0.9.0-mme-cloud.3
23
+ - @copilotkit/shared@0.9.0-function-calling-fixes.2
24
24
 
25
- ## 0.25.0-mme-cloud.2
25
+ ## 0.25.0-function-calling-fixes.1
26
26
 
27
27
  ### Minor Changes
28
28
 
29
- - streamline cloud configuration
29
+ - gpt-4-turbo-april-2024 function calling fixes
30
30
 
31
31
  ### Patch Changes
32
32
 
33
33
  - Updated dependencies
34
- - @copilotkit/shared@0.9.0-mme-cloud.2
34
+ - @copilotkit/shared@0.9.0-function-calling-fixes.1
35
35
 
36
- ## 0.25.0-mme-cloud.1
36
+ ## 0.25.0-alpha.0
37
37
 
38
38
  ### Minor Changes
39
39
 
40
- - Unify api key handling
40
+ - gpt-4-turbo-april-2024
41
41
 
42
42
  ### Patch Changes
43
43
 
44
44
  - Updated dependencies
45
- - @copilotkit/shared@0.9.0-mme-cloud.1
46
-
47
- ## 0.25.0-mme-cloud.0
48
-
49
- ### Minor Changes
50
-
51
- - Support input guardrails (cloud)
52
-
53
- ### Patch Changes
54
-
55
- - Updated dependencies
56
- - @copilotkit/shared@0.9.0-mme-cloud.0
45
+ - @copilotkit/shared@0.9.0-alpha.0
57
46
 
58
47
  ## 0.24.0
59
48
 
@@ -32,11 +32,11 @@ function fetchChatCompletion(_0) {
32
32
  headers: __spreadValues(__spreadValues({
33
33
  "Content-Type": "application/json"
34
34
  }, copilotConfig.headers), headers ? __spreadValues({}, headers) : {}),
35
- body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
35
+ body: JSON.stringify(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues(__spreadValues({
36
36
  model,
37
37
  messages: cleanedMessages,
38
38
  stream: true
39
- }, tools.length ? { tools } : {}), temperature ? { temperature } : {}), tools.length != 0 ? { tool_choice: "auto" } : {}), copilotConfig.body), copilotConfig.backendOnlyProps), excludeBackendOnlyProps(copilotConfig)), body ? __spreadValues({}, body) : {}), copilotConfig.cloud ? { cloud: copilotConfig.cloud } : {})),
39
+ }, tools.length ? { tools } : {}), temperature ? { temperature } : {}), tools.length != 0 ? { tool_choice: "auto" } : {}), copilotConfig.body), copilotConfig.backendOnlyProps), excludeBackendOnlyProps(copilotConfig)), body ? __spreadValues({}, body) : {})),
40
40
  signal
41
41
  });
42
42
  return response;
@@ -85,4 +85,4 @@ export {
85
85
  fetchAndDecodeChatCompletion,
86
86
  fetchAndDecodeChatCompletionAsText
87
87
  };
88
- //# sourceMappingURL=chunk-CGT3AVYT.mjs.map
88
+ //# sourceMappingURL=chunk-3WKJ25L3.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils/fetch-chat-completion.ts"],"sourcesContent":["import {\n Message,\n ToolDefinition,\n ChatCompletionEvent,\n decodeChatCompletion,\n parseChatCompletion,\n decodeChatCompletionAsText,\n EXCLUDE_FROM_FORWARD_PROPS_KEYS,\n} from \"@copilotkit/shared\";\nimport { CopilotApiConfig } from \"../context\";\n\nexport interface FetchChatCompletionParams {\n copilotConfig: CopilotApiConfig;\n model?: string;\n messages: Message[];\n tools?: ToolDefinition[];\n temperature?: number;\n maxTokens?: number;\n headers?: Record<string, string> | Headers;\n body?: object;\n signal?: AbortSignal;\n}\n\nexport async function fetchChatCompletion({\n copilotConfig,\n model,\n messages,\n tools,\n temperature,\n headers,\n body,\n signal,\n}: FetchChatCompletionParams): Promise<Response> {\n temperature ||= 0.5;\n tools ||= [];\n\n // clean up any extra properties from messages\n const cleanedMessages = messages.map((message) => {\n const { content, role, name, function_call } = message;\n return { content, role, name, function_call };\n });\n\n const response = await fetch(copilotConfig.chatApiEndpoint, {\n method: \"POST\",\n headers: {\n \"Content-Type\": \"application/json\",\n ...copilotConfig.headers,\n ...(headers ? { ...headers } : {}),\n },\n body: JSON.stringify({\n model,\n messages: cleanedMessages,\n stream: true,\n ...(tools.length ? { tools } : {}),\n ...(temperature ? { temperature } : {}),\n ...(tools.length != 0 ? { tool_choice: \"auto\" } : {}),\n ...copilotConfig.body,\n ...copilotConfig.backendOnlyProps,\n ...excludeBackendOnlyProps(copilotConfig),\n ...(body ? { ...body } : {}),\n }),\n signal,\n });\n\n return response;\n}\n\nfunction excludeBackendOnlyProps(copilotConfig: any) {\n const backendOnlyProps = copilotConfig.backendOnlyProps ?? {};\n if (Object.keys(backendOnlyProps).length > 0) {\n return {\n [EXCLUDE_FROM_FORWARD_PROPS_KEYS]: Object.keys(backendOnlyProps),\n };\n } else {\n return {};\n }\n}\n\nexport interface DecodedChatCompletionResponse extends Response {\n events: ReadableStream<ChatCompletionEvent> | null;\n}\n\nexport async function fetchAndDecodeChatCompletion(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponse> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n (response as any).events = null;\n } else {\n const events = await decodeChatCompletion(parseChatCompletion(response.body));\n (response as any).events = events;\n }\n return response as any;\n}\n\nexport interface DecodedChatCompletionResponseAsText extends Response {\n events: ReadableStream<string> | null;\n}\n\nexport async function fetchAndDecodeChatCompletionAsText(\n params: FetchChatCompletionParams,\n): Promise<DecodedChatCompletionResponseAsText> {\n const response = await fetchChatCompletion(params);\n if (!response.ok || !response.body) {\n (response as any).events = null;\n } else {\n const events = await decodeChatCompletionAsText(\n decodeChatCompletion(parseChatCompletion(response.body)),\n );\n (response as any).events = events;\n }\n\n return response as any;\n}\n"],"mappings":";;;;;;AAAA;AAAA,EAIE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AAeP,SAAsB,oBAAoB,IASO;AAAA,6CATP;AAAA,IACxC;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,GAAiD;AAC/C,kCAAgB;AAChB,sBAAU,CAAC;AAGX,UAAM,kBAAkB,SAAS,IAAI,CAAC,YAAY;AAChD,YAAM,EAAE,SAAS,MAAM,MAAM,cAAc,IAAI;AAC/C,aAAO,EAAE,SAAS,MAAM,MAAM,cAAc;AAAA,IAC9C,CAAC;AAED,UAAM,WAAW,MAAM,MAAM,cAAc,iBAAiB;AAAA,MAC1D,QAAQ;AAAA,MACR,SAAS;AAAA,QACP,gBAAgB;AAAA,SACb,cAAc,UACb,UAAU,mBAAK,WAAY,CAAC;AAAA,MAElC,MAAM,KAAK,UAAU;AAAA,QACnB;AAAA,QACA,UAAU;AAAA,QACV,QAAQ;AAAA,SACJ,MAAM,SAAS,EAAE,MAAM,IAAI,CAAC,IAC5B,cAAc,EAAE,YAAY,IAAI,CAAC,IACjC,MAAM,UAAU,IAAI,EAAE,aAAa,OAAO,IAAI,CAAC,IAChD,cAAc,OACd,cAAc,mBACd,wBAAwB,aAAa,IACpC,OAAO,mBAAK,QAAS,CAAC,EAC3B;AAAA,MACD;AAAA,IACF,CAAC;AAED,WAAO;AAAA,EACT;AAAA;AAEA,SAAS,wBAAwB,eAAoB;AAnErD;AAoEE,QAAM,oBAAmB,mBAAc,qBAAd,YAAkC,CAAC;AAC5D,MAAI,OAAO,KAAK,gBAAgB,EAAE,SAAS,GAAG;AAC5C,WAAO;AAAA,MACL,CAAC,+BAA+B,GAAG,OAAO,KAAK,gBAAgB;AAAA,IACjE;AAAA,EACF,OAAO;AACL,WAAO,CAAC;AAAA,EACV;AACF;AAMA,SAAsB,6BACpB,QACwC;AAAA;AACxC,UAAM,WAAW,MAAM,oBAAoB,MAAM;AACjD,QAAI,CAAC,SAAS,MAAM,CAAC,SAAS,MAAM;AAClC,MAAC,SAAiB,SAAS;AAAA,IAC7B,OAAO;AACL,YAAM,SAAS,MAAM,qBAAqB,oBAAoB,SAAS,IAAI,CAAC;AAC5E,MAAC,SAAiB,SAAS;AAAA,IAC7B;AACA,WAAO;AAAA,EACT;AAAA;AAMA,SAAsB,mCACpB,QAC8C;AAAA;AAC9C,UAAM,WAAW,MAAM,oBAAoB,MAAM;AACjD,QAAI,CAAC,SAAS,MAAM,CAAC,SAAS,MAAM;AAClC,MAAC,SAAiB,SAAS;AAAA,IAC7B,OAAO;AACL,YAAM,SAAS,MAAM;AAAA,QACnB,qBAAqB,oBAAoB,SAAS,IAAI,CAAC;AAAA,MACzD;AACA,MAAC,SAAiB,SAAS;AAAA,IAC7B;AAEA,WAAO;AAAA,EACT;AAAA;","names":[]}
@@ -0,0 +1,28 @@
1
+ import {
2
+ CopilotContext
3
+ } from "./chunk-KIKHLN4X.mjs";
4
+
5
+ // src/hooks/use-copilot-readable.ts
6
+ import { useContext, useEffect, useRef } from "react";
7
+ function convertToJSON(description, value) {
8
+ return `${description}: ${typeof value === "string" ? value : JSON.stringify(value)}`;
9
+ }
10
+ function useCopilotReadable({ description, value, parentId, categories, convert }, dependencies) {
11
+ const { addContext, removeContext } = useContext(CopilotContext);
12
+ const idRef = useRef();
13
+ convert = convert || convertToJSON;
14
+ const information = convert(description, value);
15
+ useEffect(() => {
16
+ const id = addContext(information, parentId, categories);
17
+ idRef.current = id;
18
+ return () => {
19
+ removeContext(id);
20
+ };
21
+ }, [information, parentId, addContext, removeContext, ...dependencies || []]);
22
+ return idRef.current;
23
+ }
24
+
25
+ export {
26
+ useCopilotReadable
27
+ };
28
+ //# sourceMappingURL=chunk-5L5PJJNT.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-copilot-readable.ts"],"sourcesContent":["import { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\n\n/**\n * Options for the useCopilotReadable hook.\n */\nexport interface UseCopilotReadableOptions {\n /**\n * The description of the information to be added to the Copilot context.\n */\n description: string;\n /**\n * The value to be added to the Copilot context.\n */\n value: any;\n /**\n * The ID of the parent context, if any.\n */\n parentId?: string;\n /**\n * An array of categories to control which context are visible where. Particularly useful\n * with CopilotTextarea (see `useMakeAutosuggestionFunction`)\n */\n categories?: string[];\n\n /**\n * A custom conversion function to use to serialize the value to a string. If not provided, the value\n * will be serialized using `JSON.stringify`.\n */\n convert?: (description: string, value: any) => string;\n}\n\nfunction convertToJSON(description: string, value: any): string {\n return `${description}: ${typeof value === \"string\" ? value : JSON.stringify(value)}`;\n}\n\n/**\n * Adds the given information to the Copilot context to make it readable by Copilot.\n */\nexport function useCopilotReadable(\n { description, value, parentId, categories, convert }: UseCopilotReadableOptions,\n dependencies?: any[],\n): string | undefined {\n const { addContext, removeContext } = useContext(CopilotContext);\n const idRef = useRef<string>();\n convert = convert || convertToJSON;\n\n const information = convert(description, value);\n\n useEffect(() => {\n const id = addContext(information, parentId, categories);\n idRef.current = id;\n\n return () => {\n removeContext(id);\n };\n }, [information, parentId, addContext, removeContext, ...(dependencies || [])]);\n\n return idRef.current;\n}\n"],"mappings":";;;;;AAAA,SAAS,YAAY,WAAW,cAAc;AAgC9C,SAAS,cAAc,aAAqB,OAAoB;AAC9D,SAAO,GAAG,gBAAgB,OAAO,UAAU,WAAW,QAAQ,KAAK,UAAU,KAAK;AACpF;AAKO,SAAS,mBACd,EAAE,aAAa,OAAO,UAAU,YAAY,QAAQ,GACpD,cACoB;AACpB,QAAM,EAAE,YAAY,cAAc,IAAI,WAAW,cAAc;AAC/D,QAAM,QAAQ,OAAe;AAC7B,YAAU,WAAW;AAErB,QAAM,cAAc,QAAQ,aAAa,KAAK;AAE9C,YAAU,MAAM;AACd,UAAM,KAAK,WAAW,aAAa,UAAU,UAAU;AACvD,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,oBAAc,EAAE;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,aAAa,UAAU,YAAY,eAAe,GAAI,gBAAgB,CAAC,CAAE,CAAC;AAE9E,SAAO,MAAM;AACf;","names":[]}
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  CopilotContext
3
- } from "./chunk-O35YOIHC.mjs";
3
+ } from "./chunk-KIKHLN4X.mjs";
4
4
 
5
- // src/hooks/use-copilot-action.ts
5
+ // src/hooks/use-copilot-action-implementation.ts
6
6
  import { useRef, useContext, useEffect } from "react";
7
7
  import { nanoid } from "nanoid";
8
- function useCopilotAction(action, dependencies) {
8
+ function useCopilotActionImplementation(action, dependencies) {
9
9
  const { setEntryPoint, removeEntryPoint, entryPoints, chatComponentsCache } = useContext(CopilotContext);
10
10
  const idRef = useRef(nanoid());
11
11
  if (dependencies === void 0) {
@@ -42,6 +42,6 @@ function useCopilotAction(action, dependencies) {
42
42
  }
43
43
 
44
44
  export {
45
- useCopilotAction
45
+ useCopilotActionImplementation
46
46
  };
47
- //# sourceMappingURL=chunk-IR5I4Q6R.mjs.map
47
+ //# sourceMappingURL=chunk-75RLNHDB.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-copilot-action-implementation.ts"],"sourcesContent":["import { useRef, useContext, useEffect } from \"react\";\nimport { FrontendAction } from \"../types/frontend-action\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { nanoid } from \"nanoid\";\n\n// We implement useCopilotActionImplementation dependency handling so that\n// the developer has the option to not provide any dependencies.\n// In this case, we assume they want to update the handler on each rerender.\n// To avoid getting stuck in an infinite loop, we update the handler directly,\n// skipping React state updates.\n// This is ok in this case, because the handler is not part of any UI that\n// needs to be updated.\n// useCallback, useMemo or other memoization techniques are not suitable here,\n// because they will cause a infinite rerender loop.\nexport function useCopilotActionImplementation<T extends Array<any> = []>(\n action: FrontendAction<T>,\n dependencies?: any[],\n): void {\n const { setEntryPoint, removeEntryPoint, entryPoints, chatComponentsCache } =\n useContext(CopilotContext);\n const idRef = useRef<string>(nanoid());\n\n // If the developer doesn't provide dependencies, we assume they want to\n // update handler and render function when the action object changes.\n // This ensures that any captured variables in the handler are up to date.\n if (dependencies === undefined) {\n if (entryPoints[idRef.current]) {\n entryPoints[idRef.current].handler = action.handler;\n if (typeof action.render === \"function\") {\n if (chatComponentsCache.current !== null) {\n chatComponentsCache.current[action.name] = action.render;\n }\n }\n }\n }\n\n useEffect(() => {\n setEntryPoint(idRef.current, action);\n if (chatComponentsCache.current !== null && action.render !== undefined) {\n chatComponentsCache.current[action.name] = action.render;\n }\n return () => {\n // NOTE: For now, we don't remove the chatComponentsCache entry when the action is removed.\n // This is because we currently don't have access to the messages array in CopilotContext.\n removeEntryPoint(idRef.current);\n };\n }, [\n setEntryPoint,\n removeEntryPoint,\n action.description,\n action.name,\n // This should be faster than deep equality checking\n // In addition, all major JS engines guarantee the order of object keys\n JSON.stringify(action.parameters),\n // include render only if it's a string\n typeof action.render === \"string\" ? action.render : undefined,\n // dependencies set by the developer\n ...(dependencies || []),\n ]);\n}\n"],"mappings":";;;;;AAAA,SAAS,QAAQ,YAAY,iBAAiB;AAG9C,SAAS,cAAc;AAWhB,SAAS,+BACd,QACA,cACM;AACN,QAAM,EAAE,eAAe,kBAAkB,aAAa,oBAAoB,IACxE,WAAW,cAAc;AAC3B,QAAM,QAAQ,OAAe,OAAO,CAAC;AAKrC,MAAI,iBAAiB,QAAW;AAC9B,QAAI,YAAY,MAAM,OAAO,GAAG;AAC9B,kBAAY,MAAM,OAAO,EAAE,UAAU,OAAO;AAC5C,UAAI,OAAO,OAAO,WAAW,YAAY;AACvC,YAAI,oBAAoB,YAAY,MAAM;AACxC,8BAAoB,QAAQ,OAAO,IAAI,IAAI,OAAO;AAAA,QACpD;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,YAAU,MAAM;AACd,kBAAc,MAAM,SAAS,MAAM;AACnC,QAAI,oBAAoB,YAAY,QAAQ,OAAO,WAAW,QAAW;AACvE,0BAAoB,QAAQ,OAAO,IAAI,IAAI,OAAO;AAAA,IACpD;AACA,WAAO,MAAM;AAGX,uBAAiB,MAAM,OAAO;AAAA,IAChC;AAAA,EACF,GAAG;AAAA,IACD;AAAA,IACA;AAAA,IACA,OAAO;AAAA,IACP,OAAO;AAAA;AAAA;AAAA,IAGP,KAAK,UAAU,OAAO,UAAU;AAAA;AAAA,IAEhC,OAAO,OAAO,WAAW,WAAW,OAAO,SAAS;AAAA;AAAA,IAEpD,GAAI,gBAAgB,CAAC;AAAA,EACvB,CAAC;AACH;","names":[]}
@@ -1,12 +1,12 @@
1
1
  import {
2
2
  defaultCopilotContextCategories
3
- } from "./chunk-VYD7RO32.mjs";
3
+ } from "./chunk-H3FQWB4B.mjs";
4
4
  import {
5
5
  useChat
6
- } from "./chunk-MESZST5T.mjs";
6
+ } from "./chunk-X3FZ6GTK.mjs";
7
7
  import {
8
8
  CopilotContext
9
- } from "./chunk-O35YOIHC.mjs";
9
+ } from "./chunk-KIKHLN4X.mjs";
10
10
  import {
11
11
  __objRest,
12
12
  __spreadProps,
@@ -27,9 +27,7 @@ function useCopilotChat(_a) {
27
27
  getContextString,
28
28
  getChatCompletionFunctionDescriptions,
29
29
  getFunctionCallHandler,
30
- copilotApiConfig,
31
- messages,
32
- setMessages
30
+ copilotApiConfig
33
31
  } = useContext(CopilotContext);
34
32
  const systemMessage = useMemo(() => {
35
33
  const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
@@ -43,16 +41,14 @@ function useCopilotChat(_a) {
43
41
  const functionDescriptions = useMemo(() => {
44
42
  return getChatCompletionFunctionDescriptions();
45
43
  }, [getChatCompletionFunctionDescriptions]);
46
- const { append, reload, stop, isLoading, input, setInput } = useChat(__spreadProps(__spreadValues({}, options), {
44
+ const { messages, append, reload, stop, isLoading, input, setInput } = useChat(__spreadProps(__spreadValues({}, options), {
47
45
  copilotConfig: copilotApiConfig,
48
46
  id: options.id,
49
47
  initialMessages: [systemMessage].concat(options.initialMessages || []),
50
48
  tools: functionDescriptions,
51
49
  onFunctionCall: getFunctionCallHandler(),
52
50
  headers: __spreadValues({}, options.headers),
53
- body: __spreadValues({}, options.body),
54
- messages,
55
- setMessages
51
+ body: __spreadValues({}, options.body)
56
52
  }));
57
53
  const visibleMessages = messages.filter(
58
54
  (message) => message.role === "user" || message.role === "assistant" || message.role === "function"
@@ -95,4 +91,4 @@ export {
95
91
  useCopilotChat,
96
92
  defaultSystemMessage
97
93
  };
98
- //# sourceMappingURL=chunk-SFV4PSJM.mjs.map
94
+ //# sourceMappingURL=chunk-BCKOSVRB.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-copilot-chat.ts"],"sourcesContent":["import { useMemo, useContext } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { Message, ToolDefinition } from \"@copilotkit/shared\";\nimport { SystemMessageFunction } from \"../types\";\nimport { UseChatOptions, useChat } from \"./use-chat\";\nimport { defaultCopilotContextCategories } from \"../components\";\n\nexport interface UseCopilotChatOptions extends UseChatOptions {\n makeSystemMessage?: SystemMessageFunction;\n additionalInstructions?: string;\n}\n\nexport interface UseCopilotChatReturn {\n visibleMessages: Message[];\n append: (message: Message) => Promise<void>;\n reload: () => Promise<void>;\n stop: () => void;\n isLoading: boolean;\n input: string;\n setInput: React.Dispatch<React.SetStateAction<string>>;\n}\n\nexport function useCopilotChat({\n makeSystemMessage,\n additionalInstructions,\n ...options\n}: UseCopilotChatOptions): UseCopilotChatReturn {\n const {\n getContextString,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n copilotApiConfig,\n } = useContext(CopilotContext);\n\n const systemMessage: Message = useMemo(() => {\n const systemMessageMaker = makeSystemMessage || defaultSystemMessage;\n const contextString = getContextString([], defaultCopilotContextCategories); // TODO: make the context categories configurable\n\n return {\n id: \"system\",\n content: systemMessageMaker(contextString, additionalInstructions),\n role: \"system\",\n };\n }, [getContextString, makeSystemMessage, additionalInstructions]);\n\n const functionDescriptions: ToolDefinition[] = useMemo(() => {\n return getChatCompletionFunctionDescriptions();\n }, [getChatCompletionFunctionDescriptions]);\n\n const { messages, append, reload, stop, isLoading, input, setInput } = useChat({\n ...options,\n copilotConfig: copilotApiConfig,\n id: options.id,\n initialMessages: [systemMessage].concat(options.initialMessages || []),\n tools: functionDescriptions,\n onFunctionCall: getFunctionCallHandler(),\n headers: { ...options.headers },\n body: {\n ...options.body,\n },\n });\n\n const visibleMessages = messages.filter(\n (message) =>\n message.role === \"user\" || message.role === \"assistant\" || message.role === \"function\",\n );\n\n return {\n visibleMessages,\n append,\n reload,\n stop,\n isLoading,\n input,\n setInput,\n };\n}\n\nexport function defaultSystemMessage(\n contextString: string,\n additionalInstructions?: string,\n): string {\n return (\n `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.\n\nPlease assist them as best you can.\n\nYou can ask them for clarifying questions if needed, but don't be annoying about it. If you can reasonably 'fill in the blanks' yourself, do so.\n\nIf you would like to call a function, call it without saying anything else.\n` + (additionalInstructions ? `\\n\\n${additionalInstructions}` : \"\")\n );\n}\n"],"mappings":";;;;;;;;;;;;;;;;AAAA,SAAS,SAAS,kBAAkB;AAsB7B,SAAS,eAAe,IAIiB;AAJjB,eAC7B;AAAA;AAAA,IACA;AAAA,EAxBF,IAsB+B,IAG1B,oBAH0B,IAG1B;AAAA,IAFH;AAAA,IACA;AAAA;AAGA,QAAM;AAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF,IAAI,WAAW,cAAc;AAE7B,QAAM,gBAAyB,QAAQ,MAAM;AAC3C,UAAM,qBAAqB,qBAAqB;AAChD,UAAM,gBAAgB,iBAAiB,CAAC,GAAG,+BAA+B;AAE1E,WAAO;AAAA,MACL,IAAI;AAAA,MACJ,SAAS,mBAAmB,eAAe,sBAAsB;AAAA,MACjE,MAAM;AAAA,IACR;AAAA,EACF,GAAG,CAAC,kBAAkB,mBAAmB,sBAAsB,CAAC;AAEhE,QAAM,uBAAyC,QAAQ,MAAM;AAC3D,WAAO,sCAAsC;AAAA,EAC/C,GAAG,CAAC,qCAAqC,CAAC;AAE1C,QAAM,EAAE,UAAU,QAAQ,QAAQ,MAAM,WAAW,OAAO,SAAS,IAAI,QAAQ,iCAC1E,UAD0E;AAAA,IAE7E,eAAe;AAAA,IACf,IAAI,QAAQ;AAAA,IACZ,iBAAiB,CAAC,aAAa,EAAE,OAAO,QAAQ,mBAAmB,CAAC,CAAC;AAAA,IACrE,OAAO;AAAA,IACP,gBAAgB,uBAAuB;AAAA,IACvC,SAAS,mBAAK,QAAQ;AAAA,IACtB,MAAM,mBACD,QAAQ;AAAA,EAEf,EAAC;AAED,QAAM,kBAAkB,SAAS;AAAA,IAC/B,CAAC,YACC,QAAQ,SAAS,UAAU,QAAQ,SAAS,eAAe,QAAQ,SAAS;AAAA,EAChF;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,SAAS,qBACd,eACA,wBACQ;AACR,SACE;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,KAUG,yBAAyB;AAAA;AAAA,EAAO,2BAA2B;AAEhE;","names":[]}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  CopilotContext
3
- } from "./chunk-O35YOIHC.mjs";
3
+ } from "./chunk-KIKHLN4X.mjs";
4
4
 
5
5
  // src/hooks/use-make-copilot-actionable.ts
6
6
  import { useRef, useContext, useEffect, useMemo } from "react";
@@ -30,4 +30,4 @@ function useMakeCopilotActionable(annotatedFunction, dependencies) {
30
30
  export {
31
31
  useMakeCopilotActionable
32
32
  };
33
- //# sourceMappingURL=chunk-6ARSX7BW.mjs.map
33
+ //# sourceMappingURL=chunk-BGB5G33N.mjs.map
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  CopilotContext
3
- } from "./chunk-O35YOIHC.mjs";
3
+ } from "./chunk-KIKHLN4X.mjs";
4
4
 
5
5
  // src/hooks/use-make-copilot-readable.ts
6
6
  import { useContext, useEffect, useRef } from "react";
@@ -20,4 +20,4 @@ function useMakeCopilotReadable(information, parentId, categories) {
20
20
  export {
21
21
  useMakeCopilotReadable
22
22
  };
23
- //# sourceMappingURL=chunk-NQKBX6UJ.mjs.map
23
+ //# sourceMappingURL=chunk-EABGYUTZ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-make-copilot-readable.ts"],"sourcesContent":["import { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\n\n/**\n * @deprecated Use the useCopilotReadable function instead.\n */\nexport function useMakeCopilotReadable(\n information: string,\n parentId?: string,\n categories?: string[],\n): string | undefined {\n const { addContext, removeContext } = useContext(CopilotContext);\n const idRef = useRef<string>();\n\n useEffect(() => {\n const id = addContext(information, parentId, categories);\n idRef.current = id;\n\n return () => {\n removeContext(id);\n };\n }, [information, parentId, addContext, removeContext]);\n\n return idRef.current;\n}\n"],"mappings":";;;;;AAAA,SAAS,YAAY,WAAW,cAAc;AAMvC,SAAS,uBACd,aACA,UACA,YACoB;AACpB,QAAM,EAAE,YAAY,cAAc,IAAI,WAAW,cAAc;AAC/D,QAAM,QAAQ,OAAe;AAE7B,YAAU,MAAM;AACd,UAAM,KAAK,WAAW,aAAa,UAAU,UAAU;AACvD,UAAM,UAAU;AAEhB,WAAO,MAAM;AACX,oBAAc,EAAE;AAAA,IAClB;AAAA,EACF,GAAG,CAAC,aAAa,UAAU,YAAY,aAAa,CAAC;AAErD,SAAO,MAAM;AACf;","names":[]}
@@ -0,0 +1 @@
1
+ //# sourceMappingURL=chunk-EYKDJBDE.mjs.map
@@ -0,0 +1,14 @@
1
+ // src/components/copilot-provider/standard-copilot-api-config.tsx
2
+ var StandardCopilotApiConfig = class {
3
+ constructor(chatApiEndpoint, chatApiEndpointV2, headers, body) {
4
+ this.chatApiEndpoint = chatApiEndpoint;
5
+ this.chatApiEndpointV2 = chatApiEndpointV2;
6
+ this.headers = headers;
7
+ this.body = body;
8
+ }
9
+ };
10
+
11
+ export {
12
+ StandardCopilotApiConfig
13
+ };
14
+ //# sourceMappingURL=chunk-F2JIAPZQ.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/components/copilot-provider/standard-copilot-api-config.tsx"],"sourcesContent":["import { CopilotApiConfig } from \"../../context/copilot-context\";\n\n/**\n * A standard implementation of the CopilotApiConfig interface.\n *\n * Pass in the base URL of the chat API, the headers to be sent with each request, and the body to be sent with each request.\n * The rest of the CopilotApiConfig interface is implemented by default.\n *\n */\n\nexport class StandardCopilotApiConfig implements CopilotApiConfig {\n chatApiEndpoint: string;\n chatApiEndpointV2: string;\n headers: Record<string, string>;\n body: Record<string, any>;\n\n constructor(\n chatApiEndpoint: string,\n chatApiEndpointV2: string,\n headers: Record<string, string>,\n body: Record<string, any>,\n ) {\n this.chatApiEndpoint = chatApiEndpoint;\n this.chatApiEndpointV2 = chatApiEndpointV2;\n this.headers = headers;\n this.body = body;\n }\n}\n"],"mappings":";AAUO,IAAM,2BAAN,MAA2D;AAAA,EAMhE,YACE,iBACA,mBACA,SACA,MACA;AACA,SAAK,kBAAkB;AACvB,SAAK,oBAAoB;AACzB,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACd;AACF;","names":[]}
@@ -3,10 +3,10 @@ import {
3
3
  } from "./chunk-MZ5UN3BY.mjs";
4
4
  import {
5
5
  defaultCopilotContextCategories
6
- } from "./chunk-VYD7RO32.mjs";
6
+ } from "./chunk-H3FQWB4B.mjs";
7
7
  import {
8
8
  CopilotContext
9
- } from "./chunk-O35YOIHC.mjs";
9
+ } from "./chunk-KIKHLN4X.mjs";
10
10
  import {
11
11
  __async,
12
12
  __spreadValues
@@ -134,4 +134,4 @@ export {
134
134
  useCopilotChatV2,
135
135
  defaultSystemMessage
136
136
  };
137
- //# sourceMappingURL=chunk-DW3HRHJB.mjs.map
137
+ //# sourceMappingURL=chunk-FCEPEM4B.mjs.map
@@ -1,3 +1,6 @@
1
+ import {
2
+ StandardCopilotApiConfig
3
+ } from "./chunk-F2JIAPZQ.mjs";
1
4
  import {
2
5
  use_flat_category_store_default
3
6
  } from "./chunk-YULKJPY3.mjs";
@@ -6,7 +9,7 @@ import {
6
9
  } from "./chunk-VUY2K2DI.mjs";
7
10
  import {
8
11
  CopilotContext
9
- } from "./chunk-O35YOIHC.mjs";
12
+ } from "./chunk-KIKHLN4X.mjs";
10
13
  import {
11
14
  __async,
12
15
  __objRest,
@@ -16,22 +19,13 @@ import {
16
19
 
17
20
  // src/components/copilot-provider/copilotkit.tsx
18
21
  import { useCallback, useRef, useState } from "react";
19
- import {
20
- COPILOT_CLOUD_CHAT_URL,
21
- actionToChatCompletionFunction
22
- } from "@copilotkit/shared";
22
+ import { actionToChatCompletionFunction } from "@copilotkit/shared";
23
23
  import { jsx } from "react/jsx-runtime";
24
24
  function CopilotKit(_a) {
25
25
  var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
26
- var _a2, _b2;
27
- if (!props.url && !props.publicApiKey) {
28
- throw new Error("Please provide either a url or a publicApiKey to the CopilotKit component.");
29
- }
30
- const chatApiEndpoint = props.url || COPILOT_CLOUD_CHAT_URL;
31
26
  const [entryPoints, setEntryPoints] = useState({});
32
27
  const chatComponentsCache = useRef({});
33
28
  const { addElement, removeElement, printTree } = use_tree_default();
34
- const [messages, setMessages] = useState([]);
35
29
  const {
36
30
  addElement: addDocument,
37
31
  removeElement: removeDocument,
@@ -106,35 +100,12 @@ ${nonDocumentStrings}`;
106
100
  },
107
101
  [removeDocument]
108
102
  );
109
- if (!props.publicApiKey) {
110
- if (props.cloudRestrictToTopic) {
111
- throw new Error(
112
- "To use the cloudRestrictToTopic feature, please sign up at https://copilotkit.ai and provide a publicApiKey."
113
- );
114
- }
115
- }
116
- let cloud = void 0;
117
- if (props.publicApiKey) {
118
- cloud = {
119
- guardrails: {
120
- input: {
121
- restrictToTopic: {
122
- enabled: props.cloudRestrictToTopic ? true : false,
123
- validTopics: ((_a2 = props.cloudRestrictToTopic) == null ? void 0 : _a2.validTopics) || [],
124
- invalidTopics: ((_b2 = props.cloudRestrictToTopic) == null ? void 0 : _b2.invalidTopics) || []
125
- }
126
- }
127
- }
128
- };
129
- }
130
- const copilotApiConfig = __spreadProps(__spreadValues({
131
- publicApiKey: props.publicApiKey
132
- }, cloud ? { cloud } : {}), {
133
- chatApiEndpoint,
134
- chatApiEndpointV2: `${props.url}/v2`,
135
- headers: props.headers || {},
136
- body: __spreadValues(__spreadValues({}, props.body), props.backendOnlyProps)
137
- });
103
+ const copilotApiConfig = new StandardCopilotApiConfig(
104
+ props.url,
105
+ `${props.url}/v2`,
106
+ props.headers || {},
107
+ __spreadValues(__spreadValues({}, props.body), props.backendOnlyProps)
108
+ );
138
109
  return /* @__PURE__ */ jsx(
139
110
  CopilotContext.Provider,
140
111
  {
@@ -151,9 +122,7 @@ ${nonDocumentStrings}`;
151
122
  getDocumentsContext,
152
123
  addDocumentContext,
153
124
  removeDocumentContext,
154
- copilotApiConfig,
155
- messages,
156
- setMessages
125
+ copilotApiConfig
157
126
  },
158
127
  children
159
128
  }
@@ -184,4 +153,4 @@ export {
184
153
  CopilotKit,
185
154
  defaultCopilotContextCategories
186
155
  };
187
- //# sourceMappingURL=chunk-VYD7RO32.mjs.map
156
+ //# sourceMappingURL=chunk-H3FQWB4B.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/components/copilot-provider/copilotkit.tsx"],"sourcesContent":["import { Ref, useCallback, useRef, useState } from \"react\";\nimport {\n CopilotContext,\n CopilotApiConfig,\n InChatRenderFunction,\n} from \"../../context/copilot-context\";\nimport useTree from \"../../hooks/use-tree\";\nimport { DocumentPointer } from \"../../types\";\nimport { FunctionCallHandler, actionToChatCompletionFunction } from \"@copilotkit/shared\";\nimport { FrontendAction } from \"../../types/frontend-action\";\nimport useFlatCategoryStore from \"../../hooks/use-flat-category-store\";\nimport { StandardCopilotApiConfig } from \"./standard-copilot-api-config\";\nimport { CopilotKitProps } from \"./copilotkit-props\";\nimport { ToolDefinition } from \"@copilotkit/shared\";\n\n/**\n * The CopilotKit component.\n * This component provides the Copilot context to its children.\n * It can be configured either with a chat API endpoint or a CopilotApiConfig.\n *\n * NOTE: The backend can use OpenAI, or you can bring your own LLM.\n * For examples of the backend api implementation, see `examples/next-openai` usage (under `src/api/copilotkit`),\n * or read the documentation at https://docs.copilotkit.ai\n * In particular, Getting-Started > Quickstart-Backend: https://docs.copilotkit.ai/getting-started/quickstart-backend\n *\n * Example usage:\n * ```\n * <CopilotKit url=\"https://your.copilotkit.api\">\n * <App />\n * </CopilotKit>\n * ```\n *\n * or\n *\n * ```\n * const copilotApiConfig = new StandardCopilotApiConfig(\n * \"https://your.copilotkit.api/v1\",\n * \"https://your.copilotkit.api/v2\",\n * {},\n * {}\n * );\n *\n * // ...\n *\n * <CopilotKit chatApiConfig={copilotApiConfig}>\n * <App />\n * </CopilotKit>\n * ```\n *\n * @param props - The props for the component.\n * @returns The CopilotKit component.\n */\nexport function CopilotKit({ children, ...props }: CopilotKitProps) {\n // Compute all the functions and properties that we need to pass\n // to the CopilotContext.\n\n const [entryPoints, setEntryPoints] = useState<Record<string, FrontendAction<any>>>({});\n const chatComponentsCache = useRef<Record<string, InChatRenderFunction | string>>({});\n const { addElement, removeElement, printTree } = useTree();\n\n const {\n addElement: addDocument,\n removeElement: removeDocument,\n allElements: allDocuments,\n } = useFlatCategoryStore<DocumentPointer>();\n\n const setEntryPoint = useCallback((id: string, entryPoint: FrontendAction<any>) => {\n setEntryPoints((prevPoints) => {\n return {\n ...prevPoints,\n [id]: entryPoint,\n };\n });\n }, []);\n\n const removeEntryPoint = useCallback((id: string) => {\n setEntryPoints((prevPoints) => {\n const newPoints = { ...prevPoints };\n delete newPoints[id];\n return newPoints;\n });\n }, []);\n\n const getContextString = useCallback(\n (documents: DocumentPointer[], categories: string[]) => {\n const documentsString = documents\n .map((document) => {\n return `${document.name} (${document.sourceApplication}):\\n${document.getContents()}`;\n })\n .join(\"\\n\\n\");\n\n const nonDocumentStrings = printTree(categories);\n\n return `${documentsString}\\n\\n${nonDocumentStrings}`;\n },\n [printTree],\n );\n\n const addContext = useCallback(\n (\n context: string,\n parentId?: string,\n categories: string[] = defaultCopilotContextCategories,\n ) => {\n return addElement(context, categories, parentId);\n },\n [addElement],\n );\n\n const removeContext = useCallback(\n (id: string) => {\n removeElement(id);\n },\n [removeElement],\n );\n\n const getChatCompletionFunctionDescriptions = useCallback(\n (customEntryPoints?: Record<string, FrontendAction<any>>) => {\n return entryPointsToChatCompletionFunctions(Object.values(customEntryPoints || entryPoints));\n },\n [entryPoints],\n );\n\n const getFunctionCallHandler = useCallback(\n (customEntryPoints?: Record<string, FrontendAction<any>>) => {\n return entryPointsToFunctionCallHandler(Object.values(customEntryPoints || entryPoints));\n },\n [entryPoints],\n );\n\n const getDocumentsContext = useCallback(\n (categories: string[]) => {\n return allDocuments(categories);\n },\n [allDocuments],\n );\n\n const addDocumentContext = useCallback(\n (documentPointer: DocumentPointer, categories: string[] = defaultCopilotContextCategories) => {\n return addDocument(documentPointer, categories);\n },\n [addDocument],\n );\n\n const removeDocumentContext = useCallback(\n (documentId: string) => {\n removeDocument(documentId);\n },\n [removeDocument],\n );\n\n // get the appropriate CopilotApiConfig from the props\n const copilotApiConfig: CopilotApiConfig = new StandardCopilotApiConfig(\n props.url,\n `${props.url}/v2`,\n props.headers || {},\n {\n ...props.body,\n ...props.backendOnlyProps,\n },\n );\n\n return (\n <CopilotContext.Provider\n value={{\n entryPoints,\n chatComponentsCache,\n getChatCompletionFunctionDescriptions,\n getFunctionCallHandler,\n setEntryPoint,\n removeEntryPoint,\n getContextString,\n addContext,\n removeContext,\n getDocumentsContext,\n addDocumentContext,\n removeDocumentContext,\n copilotApiConfig: copilotApiConfig,\n }}\n >\n {children}\n </CopilotContext.Provider>\n );\n}\n\nexport const defaultCopilotContextCategories = [\"global\"];\n\nfunction entryPointsToChatCompletionFunctions(actions: FrontendAction<any>[]): ToolDefinition[] {\n return actions.map(actionToChatCompletionFunction);\n}\n\nfunction entryPointsToFunctionCallHandler(actions: FrontendAction<any>[]): FunctionCallHandler {\n return async (chatMessages, functionCall) => {\n let actionsByFunctionName: Record<string, FrontendAction<any>> = {};\n for (let action of actions) {\n actionsByFunctionName[action.name] = action;\n }\n\n const action = actionsByFunctionName[functionCall.name || \"\"];\n if (action) {\n let functionCallArguments: Record<string, any>[] = [];\n if (functionCall.arguments) {\n functionCallArguments = JSON.parse(functionCall.arguments);\n }\n return await action.handler(functionCallArguments);\n }\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,SAAc,aAAa,QAAQ,gBAAgB;AAQnD,SAA8B,sCAAsC;AA2JhE;AA/GG,SAAS,WAAW,IAAyC;AAAzC,eAAE,WApD7B,IAoD2B,IAAe,kBAAf,IAAe,CAAb;AAI3B,QAAM,CAAC,aAAa,cAAc,IAAI,SAA8C,CAAC,CAAC;AACtF,QAAM,sBAAsB,OAAsD,CAAC,CAAC;AACpF,QAAM,EAAE,YAAY,eAAe,UAAU,IAAI,iBAAQ;AAEzD,QAAM;AAAA,IACJ,YAAY;AAAA,IACZ,eAAe;AAAA,IACf,aAAa;AAAA,EACf,IAAI,gCAAsC;AAE1C,QAAM,gBAAgB,YAAY,CAAC,IAAY,eAAoC;AACjF,mBAAe,CAAC,eAAe;AAC7B,aAAO,iCACF,aADE;AAAA,QAEL,CAAC,EAAE,GAAG;AAAA,MACR;AAAA,IACF,CAAC;AAAA,EACH,GAAG,CAAC,CAAC;AAEL,QAAM,mBAAmB,YAAY,CAAC,OAAe;AACnD,mBAAe,CAAC,eAAe;AAC7B,YAAM,YAAY,mBAAK;AACvB,aAAO,UAAU,EAAE;AACnB,aAAO;AAAA,IACT,CAAC;AAAA,EACH,GAAG,CAAC,CAAC;AAEL,QAAM,mBAAmB;AAAA,IACvB,CAAC,WAA8B,eAAyB;AACtD,YAAM,kBAAkB,UACrB,IAAI,CAAC,aAAa;AACjB,eAAO,GAAG,SAAS,SAAS,SAAS;AAAA,EAAwB,SAAS,YAAY;AAAA,MACpF,CAAC,EACA,KAAK,MAAM;AAEd,YAAM,qBAAqB,UAAU,UAAU;AAE/C,aAAO,GAAG;AAAA;AAAA,EAAsB;AAAA,IAClC;AAAA,IACA,CAAC,SAAS;AAAA,EACZ;AAEA,QAAM,aAAa;AAAA,IACjB,CACE,SACA,UACA,aAAuB,oCACpB;AACH,aAAO,WAAW,SAAS,YAAY,QAAQ;AAAA,IACjD;AAAA,IACA,CAAC,UAAU;AAAA,EACb;AAEA,QAAM,gBAAgB;AAAA,IACpB,CAAC,OAAe;AACd,oBAAc,EAAE;AAAA,IAClB;AAAA,IACA,CAAC,aAAa;AAAA,EAChB;AAEA,QAAM,wCAAwC;AAAA,IAC5C,CAAC,sBAA4D;AAC3D,aAAO,qCAAqC,OAAO,OAAO,qBAAqB,WAAW,CAAC;AAAA,IAC7F;AAAA,IACA,CAAC,WAAW;AAAA,EACd;AAEA,QAAM,yBAAyB;AAAA,IAC7B,CAAC,sBAA4D;AAC3D,aAAO,iCAAiC,OAAO,OAAO,qBAAqB,WAAW,CAAC;AAAA,IACzF;AAAA,IACA,CAAC,WAAW;AAAA,EACd;AAEA,QAAM,sBAAsB;AAAA,IAC1B,CAAC,eAAyB;AACxB,aAAO,aAAa,UAAU;AAAA,IAChC;AAAA,IACA,CAAC,YAAY;AAAA,EACf;AAEA,QAAM,qBAAqB;AAAA,IACzB,CAAC,iBAAkC,aAAuB,oCAAoC;AAC5F,aAAO,YAAY,iBAAiB,UAAU;AAAA,IAChD;AAAA,IACA,CAAC,WAAW;AAAA,EACd;AAEA,QAAM,wBAAwB;AAAA,IAC5B,CAAC,eAAuB;AACtB,qBAAe,UAAU;AAAA,IAC3B;AAAA,IACA,CAAC,cAAc;AAAA,EACjB;AAGA,QAAM,mBAAqC,IAAI;AAAA,IAC7C,MAAM;AAAA,IACN,GAAG,MAAM;AAAA,IACT,MAAM,WAAW,CAAC;AAAA,IAClB,kCACK,MAAM,OACN,MAAM;AAAA,EAEb;AAEA,SACE;AAAA,IAAC,eAAe;AAAA,IAAf;AAAA,MACC,OAAO;AAAA,QACL;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MACF;AAAA,MAEC;AAAA;AAAA,EACH;AAEJ;AAEO,IAAM,kCAAkC,CAAC,QAAQ;AAExD,SAAS,qCAAqC,SAAkD;AAC9F,SAAO,QAAQ,IAAI,8BAA8B;AACnD;AAEA,SAAS,iCAAiC,SAAqD;AAC7F,SAAO,CAAO,cAAc,iBAAiB;AAC3C,QAAI,wBAA6D,CAAC;AAClE,aAASA,WAAU,SAAS;AAC1B,4BAAsBA,QAAO,IAAI,IAAIA;AAAA,IACvC;AAEA,UAAM,SAAS,sBAAsB,aAAa,QAAQ,EAAE;AAC5D,QAAI,QAAQ;AACV,UAAI,wBAA+C,CAAC;AACpD,UAAI,aAAa,WAAW;AAC1B,gCAAwB,KAAK,MAAM,aAAa,SAAS;AAAA,MAC3D;AACA,aAAO,MAAM,OAAO,QAAQ,qBAAqB;AAAA,IACnD;AAAA,EACF;AACF;","names":["action"]}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  CopilotContext
3
- } from "./chunk-O35YOIHC.mjs";
3
+ } from "./chunk-KIKHLN4X.mjs";
4
4
 
5
5
  // src/hooks/use-make-copilot-document-readable.ts
6
6
  import { useContext, useEffect, useRef } from "react";
@@ -20,4 +20,4 @@ function useMakeCopilotDocumentReadable(document, categories, dependencies = [])
20
20
  export {
21
21
  useMakeCopilotDocumentReadable
22
22
  };
23
- //# sourceMappingURL=chunk-PC6FY3LD.mjs.map
23
+ //# sourceMappingURL=chunk-H6V2C2AN.mjs.map
@@ -0,0 +1,13 @@
1
+ import {
2
+ useCopilotActionImplementation
3
+ } from "./chunk-75RLNHDB.mjs";
4
+
5
+ // src/hooks/use-copilot-action.ts
6
+ function useCopilotAction(action, dependencies) {
7
+ return useCopilotActionImplementation(action, dependencies);
8
+ }
9
+
10
+ export {
11
+ useCopilotAction
12
+ };
13
+ //# sourceMappingURL=chunk-ISKBWE4O.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/use-copilot-action.ts"],"sourcesContent":["import { Parameter } from \"@copilotkit/shared\";\nimport { FrontendAction } from \"../types/frontend-action\";\nimport { useCopilotActionImplementation } from \"./use-copilot-action-implementation\";\n\n // Prettier chokes on the `const` in the function signature\n // To have the main implementation checked by prettier, we split \n // this into a separate file\n // prettier-ignore\nexport function useCopilotAction<const T extends Parameter[] | [] = []>(action: FrontendAction<T>, dependencies?: any[]): void {\n return useCopilotActionImplementation(action, dependencies);\n}\n\n// Usage Example:\n// useCopilotAction({\n// name: \"myAction\",\n// parameters: [\n// { name: \"arg1\", type: \"string\", enum: [\"option1\", \"option2\", \"option3\"], required: false },\n// { name: \"arg2\", type: \"number\" },\n// {\n// name: \"arg3\",\n// type: \"object\",\n// attributes: [\n// { name: \"nestedArg1\", type: \"boolean\" },\n// { name: \"xyz\", required: false },\n// ],\n// },\n// { name: \"arg4\", type: \"number[]\" },\n// ],\n// handler: ({ arg1, arg2, arg3, arg4 }) => {\n// const x = arg3.nestedArg1;\n// const z = arg3.xyz;\n// console.log(arg1, arg2, arg3);\n// },\n// });\n\n// useCopilotAction({\n// name: \"myAction\",\n// handler: () => {\n// console.log(\"No parameters provided.\");\n// },\n// });\n\n// https://community.openai.com/t/function-call-complex-arrays-as-parameters/295648/3\n"],"mappings":";;;;;AAQO,SAAS,iBAAwD,QAA2B,cAA4B;AAC7H,SAAO,+BAA+B,QAAQ,YAAY;AAC5D;","names":[]}
@@ -18,8 +18,6 @@ var emptyCopilotContext = {
18
18
  addContext: () => "",
19
19
  removeContext: () => {
20
20
  },
21
- messages: [],
22
- setMessages: () => returnAndThrowInDebug([]),
23
21
  getDocumentsContext: (categories) => returnAndThrowInDebug([]),
24
22
  addDocumentContext: () => returnAndThrowInDebug(""),
25
23
  removeDocumentContext: () => {
@@ -52,4 +50,4 @@ export {
52
50
  CopilotContext,
53
51
  useCopilotContext
54
52
  };
55
- //# sourceMappingURL=chunk-O35YOIHC.mjs.map
53
+ //# sourceMappingURL=chunk-KIKHLN4X.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/context/copilot-context.tsx"],"sourcesContent":["import { FunctionCallHandler, ToolDefinition } from \"@copilotkit/shared\";\nimport { ActionRenderProps, FrontendAction } from \"../types/frontend-action\";\nimport React, { Ref } from \"react\";\nimport { TreeNodeId } from \"../hooks/use-tree\";\nimport { DocumentPointer } from \"../types\";\n\n/**\n * Interface for the configuration of the Copilot API.\n */\nexport interface CopilotApiConfig {\n /**\n * The endpoint for the chat API.\n */\n chatApiEndpoint: string;\n\n /**\n * The endpoint for the chat API v2.\n */\n chatApiEndpointV2: string;\n\n /**\n * additional headers to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'Authorization': 'Bearer your_token_here'\n * }\n * ```\n */\n headers: Record<string, string>;\n\n /**\n * Additional body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'message': 'Hello, world!'\n * }\n * ```\n */\n body: Record<string, any>;\n\n /**\n * Backend only props that will be combined to body params to be sent with the request\n * @default {}\n * @example\n * ```\n * {\n * 'user_id': 'user_id'\n * }\n * ```\n */\n backendOnlyProps?: Record<string, any>;\n}\n\nexport type InChatRenderFunction = (props: ActionRenderProps<any>) => string | JSX.Element;\n\nexport interface CopilotContextParams {\n // function-calling\n entryPoints: Record<string, FrontendAction<any>>;\n setEntryPoint: (id: string, entryPoint: FrontendAction<any>) => void;\n removeEntryPoint: (id: string) => void;\n chatComponentsCache: React.RefObject<Record<string, InChatRenderFunction | string>>;\n getChatCompletionFunctionDescriptions: (\n customEntryPoints?: Record<string, FrontendAction<any>>,\n ) => ToolDefinition[];\n getFunctionCallHandler: (\n customEntryPoints?: Record<string, FrontendAction<any>>,\n ) => FunctionCallHandler;\n\n // text context\n addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;\n removeContext: (id: TreeNodeId) => void;\n getContextString: (documents: DocumentPointer[], categories: string[]) => string;\n\n // document context\n addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;\n removeDocumentContext: (documentId: string) => void;\n getDocumentsContext: (categories: string[]) => DocumentPointer[];\n\n // api endpoints\n copilotApiConfig: CopilotApiConfig;\n}\n\nconst emptyCopilotContext: CopilotContextParams = {\n entryPoints: {},\n setEntryPoint: () => {},\n removeEntryPoint: () => {},\n getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),\n getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),\n chatComponentsCache: { current: {} },\n getContextString: (documents: DocumentPointer[], categories: string[]) =>\n returnAndThrowInDebug(\"\"),\n addContext: () => \"\",\n removeContext: () => {},\n\n getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),\n addDocumentContext: () => returnAndThrowInDebug(\"\"),\n removeDocumentContext: () => {},\n\n copilotApiConfig: new (class implements CopilotApiConfig {\n get chatApiEndpoint(): string {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n get chatApiEndpointV2(): string {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n }\n get headers(): Record<string, string> {\n return {};\n }\n get body(): Record<string, any> {\n return {};\n }\n })(),\n};\n\nexport const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);\n\nexport function useCopilotContext(): CopilotContextParams {\n return React.useContext(CopilotContext);\n}\n\nfunction returnAndThrowInDebug<T>(value: T): T {\n throw new Error(\"Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!\");\n return value;\n}\n"],"mappings":";;;;;AAEA,OAAO,WAAoB;AAoF3B,IAAM,sBAA4C;AAAA,EAChD,aAAa,CAAC;AAAA,EACd,eAAe,MAAM;AAAA,EAAC;AAAA,EACtB,kBAAkB,MAAM;AAAA,EAAC;AAAA,EACzB,uCAAuC,MAAM,sBAAsB,CAAC,CAAC;AAAA,EACrE,wBAAwB,MAAM,sBAAsB,MAAY;AAAA,EAAC,EAAC;AAAA,EAClE,qBAAqB,EAAE,SAAS,CAAC,EAAE;AAAA,EACnC,kBAAkB,CAAC,WAA8B,eAC/C,sBAAsB,EAAE;AAAA,EAC1B,YAAY,MAAM;AAAA,EAClB,eAAe,MAAM;AAAA,EAAC;AAAA,EAEtB,qBAAqB,CAAC,eAAyB,sBAAsB,CAAC,CAAC;AAAA,EACvE,oBAAoB,MAAM,sBAAsB,EAAE;AAAA,EAClD,uBAAuB,MAAM;AAAA,EAAC;AAAA,EAE9B,kBAAkB,IAAK,MAAkC;AAAA,IACvD,IAAI,kBAA0B;AAC5B,YAAM,IAAI,MAAM,uEAAuE;AAAA,IACzF;AAAA,IACA,IAAI,oBAA4B;AAC9B,YAAM,IAAI,MAAM,uEAAuE;AAAA,IACzF;AAAA,IACA,IAAI,UAAkC;AACpC,aAAO,CAAC;AAAA,IACV;AAAA,IACA,IAAI,OAA4B;AAC9B,aAAO,CAAC;AAAA,IACV;AAAA,EACF,EAAG;AACL;AAEO,IAAM,iBAAiB,MAAM,cAAoC,mBAAmB;AAEpF,SAAS,oBAA0C;AACxD,SAAO,MAAM,WAAW,cAAc;AACxC;AAEA,SAAS,sBAAyB,OAAa;AAC7C,QAAM,IAAI,MAAM,uEAAuE;AACvF,SAAO;AACT;","names":[]}
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  defaultCopilotContextCategories
3
- } from "./chunk-VYD7RO32.mjs";
3
+ } from "./chunk-H3FQWB4B.mjs";
4
4
  import {
5
5
  fetchAndDecodeChatCompletion
6
- } from "./chunk-CGT3AVYT.mjs";
6
+ } from "./chunk-3WKJ25L3.mjs";
7
7
  import {
8
8
  __async
9
9
  } from "./chunk-MRXNTQOX.mjs";
@@ -13,12 +13,12 @@ var CopilotTask = class {
13
13
  constructor(config) {
14
14
  this.instructions = config.instructions;
15
15
  this.actions = config.actions || [];
16
- this.includeCopilotReadable = config.includeCopilotReadable || true;
17
- this.includeCopilotActionable = config.includeCopilotActionable || true;
16
+ this.includeCopilotReadable = config.includeCopilotReadable !== false;
17
+ this.includeCopilotActions = config.includeCopilotActions !== false && config.includeCopilotActionable !== false;
18
18
  }
19
19
  run(context, data) {
20
20
  return __async(this, null, function* () {
21
- const entryPoints = this.includeCopilotActionable ? Object.assign({}, context.entryPoints) : {};
21
+ const entryPoints = this.includeCopilotActions ? Object.assign({}, context.entryPoints) : {};
22
22
  for (const fn of this.actions) {
23
23
  entryPoints[fn.name] = fn;
24
24
  }
@@ -99,4 +99,4 @@ ${instructions}
99
99
  export {
100
100
  CopilotTask
101
101
  };
102
- //# sourceMappingURL=chunk-UNYZRMB2.mjs.map
102
+ //# sourceMappingURL=chunk-L45P674M.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/lib/copilot-task.ts"],"sourcesContent":["import { FunctionCall, Message } from \"@copilotkit/shared\";\nimport { FrontendAction } from \"../types/frontend-action\";\nimport { CopilotContextParams } from \"../context\";\nimport { defaultCopilotContextCategories } from \"../components\";\nimport { fetchAndDecodeChatCompletion } from \"../utils/fetch-chat-completion\";\n\nexport interface CopilotTaskConfig {\n /**\n * The instructions to be given to the assistant.\n */\n instructions: string;\n /**\n * Action definitions to be sent to the API.\n */\n actions?: FrontendAction<any>[];\n /**\n * Whether to include the copilot readable context in the task.\n */\n includeCopilotReadable?: boolean;\n\n /**\n * Whether to include actions defined via useCopilotAction in the task.\n * @deprecated Use the `includeCopilotActions` property instead.\n */\n includeCopilotActionable?: boolean;\n\n /**\n * Whether to include actions defined via useCopilotAction in the task.\n */\n includeCopilotActions?: boolean;\n}\n\nexport class CopilotTask<T = any> {\n private instructions: string;\n private actions: FrontendAction<any>[];\n private includeCopilotReadable: boolean;\n private includeCopilotActions: boolean;\n\n constructor(config: CopilotTaskConfig) {\n this.instructions = config.instructions;\n this.actions = config.actions || [];\n this.includeCopilotReadable = config.includeCopilotReadable !== false;\n this.includeCopilotActions =\n config.includeCopilotActions !== false && config.includeCopilotActionable !== false;\n }\n\n async run(context: CopilotContextParams, data?: T): Promise<void> {\n const entryPoints = this.includeCopilotActions ? Object.assign({}, context.entryPoints) : {};\n\n // merge functions into entry points\n for (const fn of this.actions) {\n entryPoints[fn.name] = fn;\n }\n\n let contextString = \"\";\n\n if (data) {\n contextString = (typeof data === \"string\" ? data : JSON.stringify(data)) + \"\\n\\n\";\n }\n\n if (this.includeCopilotReadable) {\n contextString += context.getContextString([], defaultCopilotContextCategories);\n }\n\n const systemMessage: Message = {\n id: \"system\",\n content: taskSystemMessage(contextString, this.instructions),\n role: \"system\",\n };\n\n const messages = [systemMessage];\n\n const response = await fetchAndDecodeChatCompletion({\n copilotConfig: context.copilotApiConfig,\n messages: messages,\n tools: context.getChatCompletionFunctionDescriptions(entryPoints),\n headers: context.copilotApiConfig.headers,\n body: context.copilotApiConfig.body,\n });\n\n if (!response.events) {\n throw new Error(\"Failed to execute task\");\n }\n\n const reader = response.events.getReader();\n let functionCalls: FunctionCall[] = [];\n\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n break;\n }\n\n if (value.type === \"function\") {\n functionCalls.push({\n name: value.name,\n arguments: JSON.stringify(value.arguments),\n });\n break;\n }\n }\n\n if (!functionCalls.length) {\n throw new Error(\"No function call occurred\");\n }\n\n const functionCallHandler = context.getFunctionCallHandler(entryPoints);\n for (const functionCall of functionCalls) {\n await functionCallHandler(messages, functionCall);\n }\n }\n}\n\nfunction taskSystemMessage(contextString: string, instructions: string): string {\n return `\nPlease act as an efficient, competent, conscientious, and industrious professional assistant.\n\nHelp the user achieve their goals, and you do so in a way that is as efficient as possible, without unnecessary fluff, but also without sacrificing professionalism.\nAlways be polite and respectful, and prefer brevity over verbosity.\n\nThe user has provided you with the following context:\n\\`\\`\\`\n${contextString}\n\\`\\`\\`\n\nThey have also provided you with functions you can call to initiate actions on their behalf.\n\nPlease assist them as best you can.\n\nThis is not a conversation, so please do not ask questions. Just call a function without saying anything else.\n\nThe user has given you the following task to complete:\n\n\\`\\`\\`\n${instructions}\n\\`\\`\\`\n`;\n}\n"],"mappings":";;;;;;;;;;;AAgCO,IAAM,cAAN,MAA2B;AAAA,EAMhC,YAAY,QAA2B;AACrC,SAAK,eAAe,OAAO;AAC3B,SAAK,UAAU,OAAO,WAAW,CAAC;AAClC,SAAK,yBAAyB,OAAO,2BAA2B;AAChE,SAAK,wBACH,OAAO,0BAA0B,SAAS,OAAO,6BAA6B;AAAA,EAClF;AAAA,EAEM,IAAI,SAA+B,MAAyB;AAAA;AAChE,YAAM,cAAc,KAAK,wBAAwB,OAAO,OAAO,CAAC,GAAG,QAAQ,WAAW,IAAI,CAAC;AAG3F,iBAAW,MAAM,KAAK,SAAS;AAC7B,oBAAY,GAAG,IAAI,IAAI;AAAA,MACzB;AAEA,UAAI,gBAAgB;AAEpB,UAAI,MAAM;AACR,yBAAiB,OAAO,SAAS,WAAW,OAAO,KAAK,UAAU,IAAI,KAAK;AAAA,MAC7E;AAEA,UAAI,KAAK,wBAAwB;AAC/B,yBAAiB,QAAQ,iBAAiB,CAAC,GAAG,+BAA+B;AAAA,MAC/E;AAEA,YAAM,gBAAyB;AAAA,QAC7B,IAAI;AAAA,QACJ,SAAS,kBAAkB,eAAe,KAAK,YAAY;AAAA,QAC3D,MAAM;AAAA,MACR;AAEA,YAAM,WAAW,CAAC,aAAa;AAE/B,YAAM,WAAW,MAAM,6BAA6B;AAAA,QAClD,eAAe,QAAQ;AAAA,QACvB;AAAA,QACA,OAAO,QAAQ,sCAAsC,WAAW;AAAA,QAChE,SAAS,QAAQ,iBAAiB;AAAA,QAClC,MAAM,QAAQ,iBAAiB;AAAA,MACjC,CAAC;AAED,UAAI,CAAC,SAAS,QAAQ;AACpB,cAAM,IAAI,MAAM,wBAAwB;AAAA,MAC1C;AAEA,YAAM,SAAS,SAAS,OAAO,UAAU;AACzC,UAAI,gBAAgC,CAAC;AAErC,aAAO,MAAM;AACX,cAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAE1C,YAAI,MAAM;AACR;AAAA,QACF;AAEA,YAAI,MAAM,SAAS,YAAY;AAC7B,wBAAc,KAAK;AAAA,YACjB,MAAM,MAAM;AAAA,YACZ,WAAW,KAAK,UAAU,MAAM,SAAS;AAAA,UAC3C,CAAC;AACD;AAAA,QACF;AAAA,MACF;AAEA,UAAI,CAAC,cAAc,QAAQ;AACzB,cAAM,IAAI,MAAM,2BAA2B;AAAA,MAC7C;AAEA,YAAM,sBAAsB,QAAQ,uBAAuB,WAAW;AACtE,iBAAW,gBAAgB,eAAe;AACxC,cAAM,oBAAoB,UAAU,YAAY;AAAA,MAClD;AAAA,IACF;AAAA;AACF;AAEA,SAAS,kBAAkB,eAAuB,cAA8B;AAC9E,SAAO;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQP;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA;AAAA;AAAA;AAGF;","names":[]}
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  fetchAndDecodeChatCompletion
3
- } from "./chunk-CGT3AVYT.mjs";
3
+ } from "./chunk-3WKJ25L3.mjs";
4
4
  import {
5
5
  __async,
6
6
  __spreadProps,
@@ -10,20 +10,17 @@ import {
10
10
  // src/hooks/use-chat.ts
11
11
  import { useRef, useState } from "react";
12
12
  import {
13
- encodeResult,
14
- COPILOT_CLOUD_PUBLIC_API_KEY_HEADER
13
+ encodeResult
15
14
  } from "@copilotkit/shared";
16
15
  import { nanoid } from "nanoid";
17
16
  import untruncateJson from "untruncate-json";
18
17
  function useChat(options) {
19
- const { messages, setMessages } = options;
18
+ const [messages, setMessages] = useState([]);
20
19
  const [input, setInput] = useState("");
21
20
  const [isLoading, setIsLoading] = useState(false);
22
21
  const abortControllerRef = useRef();
23
22
  const threadIdRef = useRef(null);
24
23
  const runIdRef = useRef(null);
25
- const publicApiKey = options.copilotConfig.publicApiKey;
26
- const headers = __spreadValues(__spreadValues({}, options.headers || {}), publicApiKey ? { [COPILOT_CLOUD_PUBLIC_API_KEY_HEADER]: publicApiKey } : {});
27
24
  const runChatCompletion = (messages2) => __async(this, null, function* () {
28
25
  setIsLoading(true);
29
26
  const newMessages = [
@@ -49,7 +46,7 @@ function useChat(options) {
49
46
  copilotConfig: __spreadProps(__spreadValues({}, options.copilotConfig), { body: copilotConfigBody }),
50
47
  messages: messagesWithContext,
51
48
  tools: options.tools,
52
- headers,
49
+ headers: options.headers,
53
50
  signal: abortController.signal
54
51
  });
55
52
  if (response.headers.get("threadid")) {
@@ -59,6 +56,16 @@ function useChat(options) {
59
56
  runIdRef.current = response.headers.get("runid");
60
57
  }
61
58
  if (!response.events) {
59
+ setMessages([
60
+ ...messages2,
61
+ {
62
+ id: nanoid(),
63
+ createdAt: /* @__PURE__ */ new Date(),
64
+ content: response.statusText,
65
+ role: "assistant"
66
+ }
67
+ ]);
68
+ setIsLoading(false);
62
69
  throw new Error("Failed to fetch chat completion");
63
70
  }
64
71
  const reader = response.events.getReader();
@@ -182,6 +189,7 @@ function useChat(options) {
182
189
  (_a = abortControllerRef.current) == null ? void 0 : _a.abort();
183
190
  };
184
191
  return {
192
+ messages,
185
193
  append,
186
194
  reload,
187
195
  stop,
@@ -194,4 +202,4 @@ function useChat(options) {
194
202
  export {
195
203
  useChat
196
204
  };
197
- //# sourceMappingURL=chunk-MESZST5T.mjs.map
205
+ //# sourceMappingURL=chunk-X3FZ6GTK.mjs.map