@copilotkit/react-ui 0.36.0-mme-push-to-talk.0 → 0.36.0-mme-pre.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +174 -167
- package/CHANGELOG.md +12 -0
- package/dist/{chunk-DMAQBCTX.mjs → chunk-4MKP23AD.mjs} +6 -4
- package/dist/chunk-4MKP23AD.mjs.map +1 -0
- package/dist/{chunk-UVMROYDT.mjs → chunk-6XLZXLM5.mjs} +5 -5
- package/dist/{chunk-UVMROYDT.mjs.map → chunk-6XLZXLM5.mjs.map} +1 -1
- package/dist/{chunk-XYM43AHP.mjs → chunk-7FES2IQA.mjs} +3 -3
- package/dist/chunk-ANO23V2M.mjs +135 -0
- package/dist/chunk-ANO23V2M.mjs.map +1 -0
- package/dist/{chunk-DRNCOXZO.mjs → chunk-BL65ZC6L.mjs} +28 -7
- package/dist/chunk-BL65ZC6L.mjs.map +1 -0
- package/dist/{chunk-PGZDQT74.mjs → chunk-CE7PJAAO.mjs} +2 -2
- package/dist/{chunk-FWTPMPSN.mjs → chunk-FZC7X5PK.mjs} +23 -3
- package/dist/{chunk-FWTPMPSN.mjs.map → chunk-FZC7X5PK.mjs.map} +1 -1
- package/dist/{chunk-Z45ZEXJW.mjs → chunk-LTCJCXCP.mjs} +5 -8
- package/dist/chunk-LTCJCXCP.mjs.map +1 -0
- package/dist/chunk-MRFF7GSQ.mjs +1 -0
- package/dist/{chunk-SKC7AJIV.mjs → chunk-MRXNTQOX.mjs} +1 -3
- package/dist/{chunk-MWFHYCQB.mjs → chunk-PAQWLSA4.mjs} +2 -2
- package/dist/chunk-RT2XG2T7.mjs +25 -0
- package/dist/chunk-RT2XG2T7.mjs.map +1 -0
- package/dist/chunk-T3JTSIHT.mjs +93 -0
- package/dist/chunk-T3JTSIHT.mjs.map +1 -0
- package/dist/{chunk-A7J4KGLP.mjs → chunk-UPTB2MVO.mjs} +2 -2
- package/dist/{chunk-KZME7C5S.mjs → chunk-VUZA5AFH.mjs} +8 -11
- package/dist/chunk-VUZA5AFH.mjs.map +1 -0
- package/dist/{chunk-XWWMYJJF.mjs → chunk-XRODMID5.mjs} +5 -5
- package/dist/{chunk-XWWMYJJF.mjs.map → chunk-XRODMID5.mjs.map} +1 -1
- package/dist/{chunk-ZKLK3M77.mjs → chunk-YQ3D5IQV.mjs} +3 -3
- package/dist/{chunk-WM6BS77F.mjs → chunk-YQFVRDNC.mjs} +2 -2
- package/dist/{chunk-WM6BS77F.mjs.map → chunk-YQFVRDNC.mjs.map} +1 -1
- package/dist/chunk-ZO3GLN23.mjs +137 -0
- package/dist/chunk-ZO3GLN23.mjs.map +1 -0
- package/dist/components/chat/Button.d.ts +1 -1
- package/dist/components/chat/Button.js +2 -30
- package/dist/components/chat/Button.js.map +1 -1
- package/dist/components/chat/Button.mjs +4 -4
- package/dist/components/chat/Chat.d.ts +66 -47
- package/dist/components/chat/Chat.js +274 -430
- package/dist/components/chat/Chat.js.map +1 -1
- package/dist/components/chat/Chat.mjs +16 -17
- package/dist/components/chat/ChatContext.d.ts +17 -22
- package/dist/components/chat/ChatContext.js +23 -8
- package/dist/components/chat/ChatContext.js.map +1 -1
- package/dist/components/chat/ChatContext.mjs +3 -3
- package/dist/components/chat/CodeBlock.js.map +1 -1
- package/dist/components/chat/CodeBlock.mjs +3 -3
- package/dist/components/chat/Header.js.map +1 -1
- package/dist/components/chat/Header.mjs +4 -4
- package/dist/components/chat/Icons.d.ts +6 -5
- package/dist/components/chat/Icons.js +21 -0
- package/dist/components/chat/Icons.js.map +1 -1
- package/dist/components/chat/Icons.mjs +4 -2
- package/dist/components/chat/Input.js +147 -9
- package/dist/components/chat/Input.js.map +1 -1
- package/dist/components/chat/Input.mjs +6 -5
- package/dist/components/chat/Markdown.js.map +1 -1
- package/dist/components/chat/Markdown.mjs +4 -4
- package/dist/components/chat/Messages.js.map +1 -1
- package/dist/components/chat/Messages.mjs +6 -6
- package/dist/components/chat/Modal.d.ts +50 -0
- package/dist/components/chat/Modal.js +1584 -0
- package/dist/components/chat/Modal.js.map +1 -0
- package/dist/components/chat/Modal.mjs +23 -0
- package/dist/components/chat/Popup.d.ts +6 -5
- package/dist/components/chat/Popup.js +288 -249
- package/dist/components/chat/Popup.js.map +1 -1
- package/dist/components/chat/Popup.mjs +16 -15
- package/dist/components/chat/Response.js.map +1 -1
- package/dist/components/chat/Response.mjs +4 -4
- package/dist/components/chat/Sidebar.d.ts +6 -5
- package/dist/components/chat/Sidebar.js +290 -251
- package/dist/components/chat/Sidebar.js.map +1 -1
- package/dist/components/chat/Sidebar.mjs +16 -15
- package/dist/components/chat/Suggestion.d.ts +1 -2
- package/dist/components/chat/Suggestion.js.map +1 -1
- package/dist/components/chat/Suggestion.mjs +3 -3
- package/dist/components/chat/Textarea.d.ts +4 -4
- package/dist/components/chat/Textarea.js +1 -1
- package/dist/components/chat/Textarea.js.map +1 -1
- package/dist/components/chat/Textarea.mjs +2 -2
- package/dist/components/chat/Window.mjs +1 -1
- package/dist/components/chat/index.d.ts +2 -1
- package/dist/components/chat/index.js +294 -253
- package/dist/components/chat/index.js.map +1 -1
- package/dist/components/chat/index.mjs +23 -19
- package/dist/components/chat/props.d.ts +1 -3
- package/dist/components/chat/props.js.map +1 -1
- package/dist/components/index.d.ts +2 -1
- package/dist/components/index.js +294 -253
- package/dist/components/index.js.map +1 -1
- package/dist/components/index.mjs +23 -19
- package/dist/hooks/index.d.ts +0 -1
- package/dist/hooks/index.js +6 -31
- package/dist/hooks/index.js.map +1 -1
- package/dist/hooks/index.mjs +2 -22
- package/dist/hooks/use-copilot-chat-suggestions.d.ts +26 -4
- package/dist/hooks/use-copilot-chat-suggestions.js +6 -31
- package/dist/hooks/use-copilot-chat-suggestions.js.map +1 -1
- package/dist/hooks/use-copilot-chat-suggestions.mjs +2 -22
- package/dist/hooks/use-copy-to-clipboard.mjs +1 -1
- package/dist/hooks/use-push-to-talk.d.ts +19 -0
- package/dist/hooks/use-push-to-talk.js +177 -0
- package/dist/hooks/use-push-to-talk.js.map +1 -0
- package/dist/hooks/use-push-to-talk.mjs +12 -0
- package/dist/hooks/use-push-to-talk.mjs.map +1 -0
- package/dist/index.css +60 -8
- package/dist/index.css.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +300 -258
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +28 -24
- package/dist/lib/utils.mjs +1 -1
- package/dist/types/suggestions.d.ts +1 -21
- package/dist/types/suggestions.js.map +1 -1
- package/package.json +6 -6
- package/src/components/chat/Button.tsx +2 -35
- package/src/components/chat/Chat.tsx +126 -255
- package/src/components/chat/ChatContext.tsx +8 -22
- package/src/components/chat/Icons.tsx +17 -0
- package/src/components/chat/Input.tsx +37 -5
- package/src/components/chat/Modal.tsx +115 -0
- package/src/components/chat/Popup.tsx +3 -3
- package/src/components/chat/Sidebar.tsx +4 -4
- package/src/components/chat/Suggestion.tsx +6 -2
- package/src/components/chat/Textarea.tsx +1 -1
- package/src/components/chat/index.tsx +1 -0
- package/src/components/chat/props.ts +1 -3
- package/src/css/input.css +18 -8
- package/src/css/panel.css +38 -0
- package/src/css/window.css +3 -1
- package/src/hooks/use-copilot-chat-suggestions.tsx +31 -5
- package/src/hooks/use-push-to-talk.tsx +162 -0
- package/src/styles.css +1 -0
- package/src/types/suggestions.ts +0 -24
- package/dist/chunk-5ASYNEHX.mjs +0 -53
- package/dist/chunk-5ASYNEHX.mjs.map +0 -1
- package/dist/chunk-DMAQBCTX.mjs.map +0 -1
- package/dist/chunk-DRNCOXZO.mjs.map +0 -1
- package/dist/chunk-JPX5ODUX.mjs +0 -266
- package/dist/chunk-JPX5ODUX.mjs.map +0 -1
- package/dist/chunk-KZME7C5S.mjs.map +0 -1
- package/dist/chunk-PEDSZYHE.mjs +0 -36
- package/dist/chunk-PEDSZYHE.mjs.map +0 -1
- package/dist/chunk-UGQQ4WEQ.mjs +0 -1
- package/dist/chunk-Z45ZEXJW.mjs.map +0 -1
- package/dist/components/chat/audio.d.ts +0 -7
- package/dist/components/chat/audio.js +0 -77
- package/dist/components/chat/audio.js.map +0 -1
- package/dist/components/chat/audio.mjs +0 -10
- package/src/components/chat/audio.ts +0 -26
- /package/dist/{chunk-XYM43AHP.mjs.map → chunk-7FES2IQA.mjs.map} +0 -0
- /package/dist/{chunk-PGZDQT74.mjs.map → chunk-CE7PJAAO.mjs.map} +0 -0
- /package/dist/{chunk-SKC7AJIV.mjs.map → chunk-MRFF7GSQ.mjs.map} +0 -0
- /package/dist/{chunk-UGQQ4WEQ.mjs.map → chunk-MRXNTQOX.mjs.map} +0 -0
- /package/dist/{chunk-MWFHYCQB.mjs.map → chunk-PAQWLSA4.mjs.map} +0 -0
- /package/dist/{chunk-A7J4KGLP.mjs.map → chunk-UPTB2MVO.mjs.map} +0 -0
- /package/dist/{chunk-ZKLK3M77.mjs.map → chunk-YQ3D5IQV.mjs.map} +0 -0
- /package/dist/components/chat/{audio.mjs.map → Modal.mjs.map} +0 -0
package/dist/hooks/index.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// src/hooks/index.ts
|
|
@@ -35,41 +25,26 @@ __export(hooks_exports, {
|
|
|
35
25
|
module.exports = __toCommonJS(hooks_exports);
|
|
36
26
|
|
|
37
27
|
// src/hooks/use-copilot-chat-suggestions.tsx
|
|
38
|
-
var
|
|
39
|
-
|
|
40
|
-
// src/components/chat/ChatContext.tsx
|
|
41
|
-
var import_react = __toESM(require("react"));
|
|
42
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
43
|
-
var ChatContext = import_react.default.createContext(void 0);
|
|
44
|
-
function useChatContext() {
|
|
45
|
-
const context = import_react.default.useContext(ChatContext);
|
|
46
|
-
if (context === void 0) {
|
|
47
|
-
throw new Error(
|
|
48
|
-
"Context not found. Did you forget to wrap your app in a <ChatContextProvider> component?"
|
|
49
|
-
);
|
|
50
|
-
}
|
|
51
|
-
return context;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// src/hooks/use-copilot-chat-suggestions.tsx
|
|
28
|
+
var import_react = require("react");
|
|
55
29
|
var import_nanoid = require("nanoid");
|
|
30
|
+
var import_react_core = require("@copilotkit/react-core");
|
|
56
31
|
function useCopilotChatSuggestions({
|
|
57
32
|
instructions,
|
|
58
33
|
className,
|
|
59
34
|
minSuggestions = 1,
|
|
60
35
|
maxSuggestions = 3
|
|
61
36
|
}, dependencies = []) {
|
|
62
|
-
const
|
|
63
|
-
(0,
|
|
37
|
+
const context = (0, import_react_core.useCopilotContext)();
|
|
38
|
+
(0, import_react.useEffect)(() => {
|
|
64
39
|
const id = (0, import_nanoid.nanoid)();
|
|
65
|
-
|
|
40
|
+
context.addChatSuggestionConfiguration(id, {
|
|
66
41
|
instructions,
|
|
67
42
|
minSuggestions,
|
|
68
43
|
maxSuggestions,
|
|
69
44
|
className
|
|
70
45
|
});
|
|
71
46
|
return () => {
|
|
72
|
-
|
|
47
|
+
context.removeChatSuggestionConfiguration(id);
|
|
73
48
|
};
|
|
74
49
|
}, dependencies);
|
|
75
50
|
}
|
package/dist/hooks/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/index.ts","../../src/hooks/use-copilot-chat-suggestions.tsx"
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/index.ts","../../src/hooks/use-copilot-chat-suggestions.tsx"],"sourcesContent":["export * from \"./use-copilot-chat-suggestions\";\n","/**\n * A hook for providing suggestions to the user in the Copilot chat.\n *\n * <Warning>\n * useCopilotChatSuggestions is experimental. The interface is not final and\n * can change without further notice.\n * </Warning>\n *\n * <img src=\"/images/useCopilotChatSuggestions/use-copilot-chat-suggestions.gif\" width=\"500\" />\n *\n * <img referrerPolicy=\"no-referrer-when-downgrade\" src=\"https://static.scarf.sh/a.png?x-pxid=a9b290bb-38f9-4518-ac3b-8f54fdbf43be\" />\n *\n * `useCopilotChatSuggestions` integrates auto-generated chat suggestions into your application in the context of your\n * app's state. It dynamically manages suggestions based on provided configurations and\n * dependencies.\n *\n * <RequestExample>\n * ```jsx useCopilotChatSuggestions Example\n * import { useCopilotChatSuggestions }\n * from \"@copilotkit/react-ui\";\n *\n * useCopilotChatSuggestions({\n * instructions: \"Your instructions for suggestions.\",\n * })\n * ```\n * </RequestExample>\n *\n * ## Basic Setup\n *\n * To incorporate this hook into your React components, start by importing it:\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n * ```\n *\n * Then, use it in your component to initiate suggestion functionality:\n *\n * ```tsx\n * useCopilotChatSuggestions({\n * instructions: \"Your instructions for suggestions.\",\n * });\n * ```\n *\n * ## Dependency Management\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n *\n * useCopilotChatSuggestions(\n * {\n * instructions: \"Suggest the most relevant next actions.\",\n * },\n * [appState],\n * );\n * ```\n *\n * In the example above, the suggestions are generated based on the given instructions.\n * The hook monitors `appState`, and updates suggestions accordingly whenever it changes.\n *\n * ## Behavior and Lifecycle\n *\n * The hook registers the configuration with the chat context upon component mount and\n * removes it on unmount, ensuring a clean and efficient lifecycle management.\n */\n\nimport { useEffect } from \"react\";\nimport { useChatContext } from \"../components\";\nimport { nanoid } from \"nanoid\";\nimport { CopilotChatSuggestionConfiguration, useCopilotContext } from \"@copilotkit/react-core\";\n\ninterface UseCopilotChatSuggestionsConfiguration {\n /**\n * A prompt or instructions for the GPT to generate suggestions.\n */\n instructions: string;\n /**\n * The minimum number of suggestions to generate. Defaults to `1`.\n * @default 1\n */\n minSuggestions?: number;\n /**\n * The maximum number of suggestions to generate. Defaults to `3`.\n * @default 1\n */\n maxSuggestions?: number;\n /**\n * An optional class name to apply to the suggestions.\n */\n className?: string;\n}\n\nexport function useCopilotChatSuggestions(\n {\n instructions,\n className,\n minSuggestions = 1,\n maxSuggestions = 3,\n }: UseCopilotChatSuggestionsConfiguration,\n dependencies: any[] = [],\n) {\n const context = useCopilotContext();\n\n useEffect(() => {\n const id = nanoid();\n\n context.addChatSuggestionConfiguration(id, {\n instructions,\n minSuggestions,\n maxSuggestions,\n className,\n });\n\n return () => {\n context.removeChatSuggestionConfiguration(id);\n };\n }, dependencies);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACiEA,mBAA0B;AAE1B,oBAAuB;AACvB,wBAAsE;AAuB/D,SAAS,0BACd;AAAA,EACE;AAAA,EACA;AAAA,EACA,iBAAiB;AAAA,EACjB,iBAAiB;AACnB,GACA,eAAsB,CAAC,GACvB;AACA,QAAM,cAAU,qCAAkB;AAElC,8BAAU,MAAM;AACd,UAAM,SAAK,sBAAO;AAElB,YAAQ,+BAA+B,IAAI;AAAA,MACzC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,WAAO,MAAM;AACX,cAAQ,kCAAkC,EAAE;AAAA,IAC9C;AAAA,EACF,GAAG,YAAY;AACjB;","names":[]}
|
package/dist/hooks/index.mjs
CHANGED
|
@@ -1,28 +1,8 @@
|
|
|
1
1
|
import "../chunk-T26KLXLH.mjs";
|
|
2
2
|
import {
|
|
3
3
|
useCopilotChatSuggestions
|
|
4
|
-
} from "../chunk-
|
|
5
|
-
import "../chunk-
|
|
6
|
-
import "../chunk-UGQQ4WEQ.mjs";
|
|
7
|
-
import "../chunk-WB3YULQ4.mjs";
|
|
8
|
-
import "../chunk-XWWMYJJF.mjs";
|
|
9
|
-
import "../chunk-UVMROYDT.mjs";
|
|
10
|
-
import "../chunk-JPX5ODUX.mjs";
|
|
11
|
-
import "../chunk-B7DNOYVQ.mjs";
|
|
12
|
-
import "../chunk-PEDSZYHE.mjs";
|
|
13
|
-
import "../chunk-DRNCOXZO.mjs";
|
|
14
|
-
import "../chunk-WM6BS77F.mjs";
|
|
15
|
-
import "../chunk-XYM43AHP.mjs";
|
|
16
|
-
import "../chunk-ZKLK3M77.mjs";
|
|
17
|
-
import "../chunk-PGZDQT74.mjs";
|
|
18
|
-
import "../chunk-DMAQBCTX.mjs";
|
|
19
|
-
import "../chunk-5ASYNEHX.mjs";
|
|
20
|
-
import "../chunk-A7J4KGLP.mjs";
|
|
21
|
-
import "../chunk-MWFHYCQB.mjs";
|
|
22
|
-
import "../chunk-KZME7C5S.mjs";
|
|
23
|
-
import "../chunk-FWTPMPSN.mjs";
|
|
24
|
-
import "../chunk-54JAUBUJ.mjs";
|
|
25
|
-
import "../chunk-SKC7AJIV.mjs";
|
|
4
|
+
} from "../chunk-LTCJCXCP.mjs";
|
|
5
|
+
import "../chunk-MRXNTQOX.mjs";
|
|
26
6
|
export {
|
|
27
7
|
useCopilotChatSuggestions
|
|
28
8
|
};
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { CopilotChatSuggestionConfiguration } from '../types/suggestions.js';
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
2
|
* A hook for providing suggestions to the user in the Copilot chat.
|
|
5
3
|
*
|
|
4
|
+
* <Warning>
|
|
5
|
+
* useCopilotChatSuggestions is experimental. The interface is not final and
|
|
6
|
+
* can change without further notice.
|
|
7
|
+
* </Warning>
|
|
8
|
+
*
|
|
6
9
|
* <img src="/images/useCopilotChatSuggestions/use-copilot-chat-suggestions.gif" width="500" />
|
|
7
10
|
*
|
|
8
11
|
* <img referrerPolicy="no-referrer-when-downgrade" src="https://static.scarf.sh/a.png?x-pxid=a9b290bb-38f9-4518-ac3b-8f54fdbf43be" />
|
|
@@ -59,7 +62,26 @@ import { CopilotChatSuggestionConfiguration } from '../types/suggestions.js';
|
|
|
59
62
|
* The hook registers the configuration with the chat context upon component mount and
|
|
60
63
|
* removes it on unmount, ensuring a clean and efficient lifecycle management.
|
|
61
64
|
*/
|
|
62
|
-
|
|
63
|
-
|
|
65
|
+
interface UseCopilotChatSuggestionsConfiguration {
|
|
66
|
+
/**
|
|
67
|
+
* A prompt or instructions for the GPT to generate suggestions.
|
|
68
|
+
*/
|
|
69
|
+
instructions: string;
|
|
70
|
+
/**
|
|
71
|
+
* The minimum number of suggestions to generate. Defaults to `1`.
|
|
72
|
+
* @default 1
|
|
73
|
+
*/
|
|
74
|
+
minSuggestions?: number;
|
|
75
|
+
/**
|
|
76
|
+
* The maximum number of suggestions to generate. Defaults to `3`.
|
|
77
|
+
* @default 1
|
|
78
|
+
*/
|
|
79
|
+
maxSuggestions?: number;
|
|
80
|
+
/**
|
|
81
|
+
* An optional class name to apply to the suggestions.
|
|
82
|
+
*/
|
|
83
|
+
className?: string;
|
|
84
|
+
}
|
|
85
|
+
declare function useCopilotChatSuggestions({ instructions, className, minSuggestions, maxSuggestions, }: UseCopilotChatSuggestionsConfiguration, dependencies?: any[]): void;
|
|
64
86
|
|
|
65
87
|
export { useCopilotChatSuggestions };
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// src/hooks/use-copilot-chat-suggestions.tsx
|
|
@@ -33,41 +23,26 @@ __export(use_copilot_chat_suggestions_exports, {
|
|
|
33
23
|
useCopilotChatSuggestions: () => useCopilotChatSuggestions
|
|
34
24
|
});
|
|
35
25
|
module.exports = __toCommonJS(use_copilot_chat_suggestions_exports);
|
|
36
|
-
var
|
|
37
|
-
|
|
38
|
-
// src/components/chat/ChatContext.tsx
|
|
39
|
-
var import_react = __toESM(require("react"));
|
|
40
|
-
var import_jsx_runtime = require("react/jsx-runtime");
|
|
41
|
-
var ChatContext = import_react.default.createContext(void 0);
|
|
42
|
-
function useChatContext() {
|
|
43
|
-
const context = import_react.default.useContext(ChatContext);
|
|
44
|
-
if (context === void 0) {
|
|
45
|
-
throw new Error(
|
|
46
|
-
"Context not found. Did you forget to wrap your app in a <ChatContextProvider> component?"
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
return context;
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
// src/hooks/use-copilot-chat-suggestions.tsx
|
|
26
|
+
var import_react = require("react");
|
|
53
27
|
var import_nanoid = require("nanoid");
|
|
28
|
+
var import_react_core = require("@copilotkit/react-core");
|
|
54
29
|
function useCopilotChatSuggestions({
|
|
55
30
|
instructions,
|
|
56
31
|
className,
|
|
57
32
|
minSuggestions = 1,
|
|
58
33
|
maxSuggestions = 3
|
|
59
34
|
}, dependencies = []) {
|
|
60
|
-
const
|
|
61
|
-
(0,
|
|
35
|
+
const context = (0, import_react_core.useCopilotContext)();
|
|
36
|
+
(0, import_react.useEffect)(() => {
|
|
62
37
|
const id = (0, import_nanoid.nanoid)();
|
|
63
|
-
|
|
38
|
+
context.addChatSuggestionConfiguration(id, {
|
|
64
39
|
instructions,
|
|
65
40
|
minSuggestions,
|
|
66
41
|
maxSuggestions,
|
|
67
42
|
className
|
|
68
43
|
});
|
|
69
44
|
return () => {
|
|
70
|
-
|
|
45
|
+
context.removeChatSuggestionConfiguration(id);
|
|
71
46
|
};
|
|
72
47
|
}, dependencies);
|
|
73
48
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/hooks/use-copilot-chat-suggestions.tsx"
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-copilot-chat-suggestions.tsx"],"sourcesContent":["/**\n * A hook for providing suggestions to the user in the Copilot chat.\n *\n * <Warning>\n * useCopilotChatSuggestions is experimental. The interface is not final and\n * can change without further notice.\n * </Warning>\n *\n * <img src=\"/images/useCopilotChatSuggestions/use-copilot-chat-suggestions.gif\" width=\"500\" />\n *\n * <img referrerPolicy=\"no-referrer-when-downgrade\" src=\"https://static.scarf.sh/a.png?x-pxid=a9b290bb-38f9-4518-ac3b-8f54fdbf43be\" />\n *\n * `useCopilotChatSuggestions` integrates auto-generated chat suggestions into your application in the context of your\n * app's state. It dynamically manages suggestions based on provided configurations and\n * dependencies.\n *\n * <RequestExample>\n * ```jsx useCopilotChatSuggestions Example\n * import { useCopilotChatSuggestions }\n * from \"@copilotkit/react-ui\";\n *\n * useCopilotChatSuggestions({\n * instructions: \"Your instructions for suggestions.\",\n * })\n * ```\n * </RequestExample>\n *\n * ## Basic Setup\n *\n * To incorporate this hook into your React components, start by importing it:\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n * ```\n *\n * Then, use it in your component to initiate suggestion functionality:\n *\n * ```tsx\n * useCopilotChatSuggestions({\n * instructions: \"Your instructions for suggestions.\",\n * });\n * ```\n *\n * ## Dependency Management\n *\n * ```tsx\n * import { useCopilotChatSuggestions } from \"@copilotkit/react-ui\";\n *\n * useCopilotChatSuggestions(\n * {\n * instructions: \"Suggest the most relevant next actions.\",\n * },\n * [appState],\n * );\n * ```\n *\n * In the example above, the suggestions are generated based on the given instructions.\n * The hook monitors `appState`, and updates suggestions accordingly whenever it changes.\n *\n * ## Behavior and Lifecycle\n *\n * The hook registers the configuration with the chat context upon component mount and\n * removes it on unmount, ensuring a clean and efficient lifecycle management.\n */\n\nimport { useEffect } from \"react\";\nimport { useChatContext } from \"../components\";\nimport { nanoid } from \"nanoid\";\nimport { CopilotChatSuggestionConfiguration, useCopilotContext } from \"@copilotkit/react-core\";\n\ninterface UseCopilotChatSuggestionsConfiguration {\n /**\n * A prompt or instructions for the GPT to generate suggestions.\n */\n instructions: string;\n /**\n * The minimum number of suggestions to generate. Defaults to `1`.\n * @default 1\n */\n minSuggestions?: number;\n /**\n * The maximum number of suggestions to generate. Defaults to `3`.\n * @default 1\n */\n maxSuggestions?: number;\n /**\n * An optional class name to apply to the suggestions.\n */\n className?: string;\n}\n\nexport function useCopilotChatSuggestions(\n {\n instructions,\n className,\n minSuggestions = 1,\n maxSuggestions = 3,\n }: UseCopilotChatSuggestionsConfiguration,\n dependencies: any[] = [],\n) {\n const context = useCopilotContext();\n\n useEffect(() => {\n const id = nanoid();\n\n context.addChatSuggestionConfiguration(id, {\n instructions,\n minSuggestions,\n maxSuggestions,\n className,\n });\n\n return () => {\n context.removeChatSuggestionConfiguration(id);\n };\n }, dependencies);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAiEA,mBAA0B;AAE1B,oBAAuB;AACvB,wBAAsE;AAuB/D,SAAS,0BACd;AAAA,EACE;AAAA,EACA;AAAA,EACA,iBAAiB;AAAA,EACjB,iBAAiB;AACnB,GACA,eAAsB,CAAC,GACvB;AACA,QAAM,cAAU,qCAAkB;AAElC,8BAAU,MAAM;AACd,UAAM,SAAK,sBAAO;AAElB,YAAQ,+BAA+B,IAAI;AAAA,MACzC;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IACF,CAAC;AAED,WAAO,MAAM;AACX,cAAQ,kCAAkC,EAAE;AAAA,IAC9C;AAAA,EACF,GAAG,YAAY;AACjB;","names":[]}
|
|
@@ -1,27 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
useCopilotChatSuggestions
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
5
|
-
import "../chunk-UGQQ4WEQ.mjs";
|
|
6
|
-
import "../chunk-WB3YULQ4.mjs";
|
|
7
|
-
import "../chunk-XWWMYJJF.mjs";
|
|
8
|
-
import "../chunk-UVMROYDT.mjs";
|
|
9
|
-
import "../chunk-JPX5ODUX.mjs";
|
|
10
|
-
import "../chunk-B7DNOYVQ.mjs";
|
|
11
|
-
import "../chunk-PEDSZYHE.mjs";
|
|
12
|
-
import "../chunk-DRNCOXZO.mjs";
|
|
13
|
-
import "../chunk-WM6BS77F.mjs";
|
|
14
|
-
import "../chunk-XYM43AHP.mjs";
|
|
15
|
-
import "../chunk-ZKLK3M77.mjs";
|
|
16
|
-
import "../chunk-PGZDQT74.mjs";
|
|
17
|
-
import "../chunk-DMAQBCTX.mjs";
|
|
18
|
-
import "../chunk-5ASYNEHX.mjs";
|
|
19
|
-
import "../chunk-A7J4KGLP.mjs";
|
|
20
|
-
import "../chunk-MWFHYCQB.mjs";
|
|
21
|
-
import "../chunk-KZME7C5S.mjs";
|
|
22
|
-
import "../chunk-FWTPMPSN.mjs";
|
|
23
|
-
import "../chunk-54JAUBUJ.mjs";
|
|
24
|
-
import "../chunk-SKC7AJIV.mjs";
|
|
3
|
+
} from "../chunk-LTCJCXCP.mjs";
|
|
4
|
+
import "../chunk-MRXNTQOX.mjs";
|
|
25
5
|
export {
|
|
26
6
|
useCopilotChatSuggestions
|
|
27
7
|
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { Message } from '@copilotkit/shared';
|
|
3
|
+
|
|
4
|
+
declare const checkMicrophonePermission: () => Promise<boolean | undefined>;
|
|
5
|
+
declare const requestMicAndPlaybackPermission: () => Promise<{
|
|
6
|
+
stream: MediaStream;
|
|
7
|
+
audioContext: AudioContext;
|
|
8
|
+
} | null>;
|
|
9
|
+
type PushToTalkState = "idle" | "recording" | "transcribing";
|
|
10
|
+
type SendFunction = (text: string) => Promise<Message>;
|
|
11
|
+
declare const usePushToTalk: ({ sendFunction, inProgress, }: {
|
|
12
|
+
sendFunction: SendFunction;
|
|
13
|
+
inProgress: boolean;
|
|
14
|
+
}) => {
|
|
15
|
+
pushToTalkState: PushToTalkState;
|
|
16
|
+
setPushToTalkState: React.Dispatch<React.SetStateAction<PushToTalkState>>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { PushToTalkState, SendFunction, checkMicrophonePermission, requestMicAndPlaybackPermission, usePushToTalk };
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __async = (__this, __arguments, generator) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
var fulfilled = (value) => {
|
|
22
|
+
try {
|
|
23
|
+
step(generator.next(value));
|
|
24
|
+
} catch (e) {
|
|
25
|
+
reject(e);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var rejected = (value) => {
|
|
29
|
+
try {
|
|
30
|
+
step(generator.throw(value));
|
|
31
|
+
} catch (e) {
|
|
32
|
+
reject(e);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
36
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/hooks/use-push-to-talk.tsx
|
|
41
|
+
var use_push_to_talk_exports = {};
|
|
42
|
+
__export(use_push_to_talk_exports, {
|
|
43
|
+
checkMicrophonePermission: () => checkMicrophonePermission,
|
|
44
|
+
requestMicAndPlaybackPermission: () => requestMicAndPlaybackPermission,
|
|
45
|
+
usePushToTalk: () => usePushToTalk
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(use_push_to_talk_exports);
|
|
48
|
+
var import_react_core = require("@copilotkit/react-core");
|
|
49
|
+
var import_react = require("react");
|
|
50
|
+
var checkMicrophonePermission = () => __async(void 0, null, function* () {
|
|
51
|
+
try {
|
|
52
|
+
const permissionStatus = yield navigator.permissions.query({
|
|
53
|
+
name: "microphone"
|
|
54
|
+
});
|
|
55
|
+
if (permissionStatus.state === "granted") {
|
|
56
|
+
return true;
|
|
57
|
+
} else {
|
|
58
|
+
return false;
|
|
59
|
+
}
|
|
60
|
+
} catch (err) {
|
|
61
|
+
console.error("Error checking microphone permission", err);
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
var requestMicAndPlaybackPermission = () => __async(void 0, null, function* () {
|
|
65
|
+
try {
|
|
66
|
+
const stream = yield navigator.mediaDevices.getUserMedia({ audio: true });
|
|
67
|
+
const audioContext = new window.AudioContext();
|
|
68
|
+
yield audioContext.resume();
|
|
69
|
+
return { stream, audioContext };
|
|
70
|
+
} catch (err) {
|
|
71
|
+
console.error("Error requesting microphone and playback permissions", err);
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
var startRecording = (mediaStreamRef, mediaRecorderRef, audioContextRef, recordedChunks, onStop) => __async(void 0, null, function* () {
|
|
76
|
+
if (!mediaStreamRef.current || !audioContextRef.current) {
|
|
77
|
+
mediaStreamRef.current = yield navigator.mediaDevices.getUserMedia({ audio: true });
|
|
78
|
+
audioContextRef.current = new window.AudioContext();
|
|
79
|
+
yield audioContextRef.current.resume();
|
|
80
|
+
}
|
|
81
|
+
mediaRecorderRef.current = new MediaRecorder(mediaStreamRef.current);
|
|
82
|
+
mediaRecorderRef.current.start(1e3);
|
|
83
|
+
mediaRecorderRef.current.ondataavailable = (event) => {
|
|
84
|
+
recordedChunks.push(event.data);
|
|
85
|
+
};
|
|
86
|
+
mediaRecorderRef.current.onstop = onStop;
|
|
87
|
+
});
|
|
88
|
+
var stopRecording = (mediaRecorderRef) => {
|
|
89
|
+
if (mediaRecorderRef.current && mediaRecorderRef.current.state !== "inactive") {
|
|
90
|
+
mediaRecorderRef.current.stop();
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
var transcribeAudio = (recordedChunks, transcribeAudioUrl) => __async(void 0, null, function* () {
|
|
94
|
+
const completeBlob = new Blob(recordedChunks, { type: "audio/mp4" });
|
|
95
|
+
const formData = new FormData();
|
|
96
|
+
formData.append("file", completeBlob, "recording.mp4");
|
|
97
|
+
const response = yield fetch(transcribeAudioUrl, {
|
|
98
|
+
method: "POST",
|
|
99
|
+
body: formData
|
|
100
|
+
});
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
throw new Error(`Error: ${response.statusText}`);
|
|
103
|
+
}
|
|
104
|
+
const transcription = yield response.json();
|
|
105
|
+
return transcription.text;
|
|
106
|
+
});
|
|
107
|
+
var playAudioResponse = (text, textToSpeechUrl, audioContext) => {
|
|
108
|
+
const encodedText = encodeURIComponent(text);
|
|
109
|
+
const url = `${textToSpeechUrl}?text=${encodedText}`;
|
|
110
|
+
fetch(url).then((response) => response.arrayBuffer()).then((arrayBuffer) => audioContext.decodeAudioData(arrayBuffer)).then((audioBuffer) => {
|
|
111
|
+
const source = audioContext.createBufferSource();
|
|
112
|
+
source.buffer = audioBuffer;
|
|
113
|
+
source.connect(audioContext.destination);
|
|
114
|
+
source.start(0);
|
|
115
|
+
}).catch((error) => {
|
|
116
|
+
console.error("Error with decoding audio data", error);
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
var usePushToTalk = ({
|
|
120
|
+
sendFunction,
|
|
121
|
+
inProgress
|
|
122
|
+
}) => {
|
|
123
|
+
const [pushToTalkState, setPushToTalkState] = (0, import_react.useState)("idle");
|
|
124
|
+
const mediaStreamRef = (0, import_react.useRef)(null);
|
|
125
|
+
const audioContextRef = (0, import_react.useRef)(null);
|
|
126
|
+
const mediaRecorderRef = (0, import_react.useRef)(null);
|
|
127
|
+
const recordedChunks = (0, import_react.useRef)([]);
|
|
128
|
+
const context = (0, import_react_core.useCopilotContext)();
|
|
129
|
+
const [startReadingFromMessageId, setStartReadingFromMessageId] = (0, import_react.useState)(null);
|
|
130
|
+
(0, import_react.useEffect)(() => {
|
|
131
|
+
if (pushToTalkState === "recording") {
|
|
132
|
+
startRecording(
|
|
133
|
+
mediaStreamRef,
|
|
134
|
+
mediaRecorderRef,
|
|
135
|
+
audioContextRef,
|
|
136
|
+
recordedChunks.current,
|
|
137
|
+
() => {
|
|
138
|
+
setPushToTalkState("transcribing");
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
} else {
|
|
142
|
+
stopRecording(mediaRecorderRef);
|
|
143
|
+
if (pushToTalkState === "transcribing") {
|
|
144
|
+
transcribeAudio(recordedChunks.current, context.copilotApiConfig.transcribeAudioUrl).then(
|
|
145
|
+
(transcription) => __async(void 0, null, function* () {
|
|
146
|
+
recordedChunks.current = [];
|
|
147
|
+
setPushToTalkState("idle");
|
|
148
|
+
const message = yield sendFunction(transcription);
|
|
149
|
+
setStartReadingFromMessageId(message.id);
|
|
150
|
+
})
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
return () => {
|
|
155
|
+
stopRecording(mediaRecorderRef);
|
|
156
|
+
};
|
|
157
|
+
}, [pushToTalkState]);
|
|
158
|
+
(0, import_react.useEffect)(() => {
|
|
159
|
+
if (inProgress === false && startReadingFromMessageId) {
|
|
160
|
+
const lastMessageIndex = context.messages.findIndex(
|
|
161
|
+
(message) => message.id === startReadingFromMessageId
|
|
162
|
+
);
|
|
163
|
+
const messagesAfterLast = context.messages.slice(lastMessageIndex + 1).filter((message) => message.role === "assistant" && message.content);
|
|
164
|
+
const text = messagesAfterLast.map((message) => message.content).join("\n");
|
|
165
|
+
playAudioResponse(text, context.copilotApiConfig.textToSpeechUrl, audioContextRef.current);
|
|
166
|
+
setStartReadingFromMessageId(null);
|
|
167
|
+
}
|
|
168
|
+
}, [startReadingFromMessageId, inProgress]);
|
|
169
|
+
return { pushToTalkState, setPushToTalkState };
|
|
170
|
+
};
|
|
171
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
172
|
+
0 && (module.exports = {
|
|
173
|
+
checkMicrophonePermission,
|
|
174
|
+
requestMicAndPlaybackPermission,
|
|
175
|
+
usePushToTalk
|
|
176
|
+
});
|
|
177
|
+
//# sourceMappingURL=use-push-to-talk.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/hooks/use-push-to-talk.tsx"],"sourcesContent":["import { CopilotContextParams, useCopilotContext } from \"@copilotkit/react-core\";\nimport { Message } from \"@copilotkit/shared\";\nimport { MutableRefObject, useEffect, useRef, useState } from \"react\";\n\nexport const checkMicrophonePermission = async () => {\n try {\n const permissionStatus = await navigator.permissions.query({\n name: \"microphone\" as PermissionName,\n });\n if (permissionStatus.state === \"granted\") {\n return true;\n } else {\n return false;\n }\n } catch (err) {\n console.error(\"Error checking microphone permission\", err);\n }\n};\n\nexport const requestMicAndPlaybackPermission = async () => {\n try {\n const stream = await navigator.mediaDevices.getUserMedia({ audio: true });\n const audioContext = new window.AudioContext();\n await audioContext.resume();\n return { stream, audioContext };\n } catch (err) {\n console.error(\"Error requesting microphone and playback permissions\", err);\n return null;\n }\n};\n\nconst startRecording = async (\n mediaStreamRef: MutableRefObject<MediaStream | null>,\n mediaRecorderRef: MutableRefObject<MediaRecorder | null>,\n audioContextRef: MutableRefObject<AudioContext | null>,\n recordedChunks: Blob[],\n onStop: () => void,\n) => {\n if (!mediaStreamRef.current || !audioContextRef.current) {\n mediaStreamRef.current = await navigator.mediaDevices.getUserMedia({ audio: true });\n audioContextRef.current = new window.AudioContext();\n await audioContextRef.current.resume();\n }\n\n mediaRecorderRef.current = new MediaRecorder(mediaStreamRef.current!);\n mediaRecorderRef.current.start(1000);\n mediaRecorderRef.current.ondataavailable = (event) => {\n recordedChunks.push(event.data);\n };\n mediaRecorderRef.current.onstop = onStop;\n};\n\nconst stopRecording = (mediaRecorderRef: MutableRefObject<MediaRecorder | null>) => {\n if (mediaRecorderRef.current && mediaRecorderRef.current.state !== \"inactive\") {\n mediaRecorderRef.current.stop();\n }\n};\n\nconst transcribeAudio = async (recordedChunks: Blob[], transcribeAudioUrl: string) => {\n const completeBlob = new Blob(recordedChunks, { type: \"audio/mp4\" });\n const formData = new FormData();\n formData.append(\"file\", completeBlob, \"recording.mp4\");\n\n const response = await fetch(transcribeAudioUrl, {\n method: \"POST\",\n body: formData,\n });\n\n if (!response.ok) {\n throw new Error(`Error: ${response.statusText}`);\n }\n\n const transcription = await response.json();\n return transcription.text;\n};\n\nconst playAudioResponse = (text: string, textToSpeechUrl: string, audioContext: AudioContext) => {\n const encodedText = encodeURIComponent(text);\n const url = `${textToSpeechUrl}?text=${encodedText}`;\n\n fetch(url)\n .then((response) => response.arrayBuffer())\n .then((arrayBuffer) => audioContext.decodeAudioData(arrayBuffer))\n .then((audioBuffer) => {\n const source = audioContext.createBufferSource();\n source.buffer = audioBuffer;\n source.connect(audioContext.destination);\n source.start(0);\n })\n .catch((error) => {\n console.error(\"Error with decoding audio data\", error);\n });\n};\n\nexport type PushToTalkState = \"idle\" | \"recording\" | \"transcribing\";\n\nexport type SendFunction = (text: string) => Promise<Message>;\n\nexport const usePushToTalk = ({\n sendFunction,\n inProgress,\n}: {\n sendFunction: SendFunction;\n inProgress: boolean;\n}) => {\n const [pushToTalkState, setPushToTalkState] = useState<PushToTalkState>(\"idle\");\n const mediaStreamRef = useRef<MediaStream | null>(null);\n const audioContextRef = useRef<AudioContext | null>(null);\n const mediaRecorderRef = useRef<MediaRecorder | null>(null);\n const recordedChunks = useRef<Blob[]>([]);\n const context = useCopilotContext();\n const [startReadingFromMessageId, setStartReadingFromMessageId] = useState<string | null>(null);\n\n useEffect(() => {\n if (pushToTalkState === \"recording\") {\n startRecording(\n mediaStreamRef,\n mediaRecorderRef,\n audioContextRef,\n recordedChunks.current,\n () => {\n setPushToTalkState(\"transcribing\");\n },\n );\n } else {\n stopRecording(mediaRecorderRef);\n if (pushToTalkState === \"transcribing\") {\n transcribeAudio(recordedChunks.current, context.copilotApiConfig.transcribeAudioUrl!).then(\n async (transcription) => {\n recordedChunks.current = [];\n setPushToTalkState(\"idle\");\n const message = await sendFunction(transcription);\n setStartReadingFromMessageId(message.id);\n },\n );\n }\n }\n\n return () => {\n stopRecording(mediaRecorderRef);\n };\n }, [pushToTalkState]);\n\n useEffect(() => {\n if (inProgress === false && startReadingFromMessageId) {\n const lastMessageIndex = context.messages.findIndex(\n (message) => message.id === startReadingFromMessageId,\n );\n\n const messagesAfterLast = context.messages\n .slice(lastMessageIndex + 1)\n .filter((message) => message.role === \"assistant\" && message.content);\n\n const text = messagesAfterLast.map((message) => message.content).join(\"\\n\");\n playAudioResponse(text, context.copilotApiConfig.textToSpeechUrl!, audioContextRef.current!);\n\n setStartReadingFromMessageId(null);\n }\n }, [startReadingFromMessageId, inProgress]);\n\n return { pushToTalkState, setPushToTalkState };\n};\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,wBAAwD;AAExD,mBAA8D;AAEvD,IAAM,4BAA4B,MAAY;AACnD,MAAI;AACF,UAAM,mBAAmB,MAAM,UAAU,YAAY,MAAM;AAAA,MACzD,MAAM;AAAA,IACR,CAAC;AACD,QAAI,iBAAiB,UAAU,WAAW;AACxC,aAAO;AAAA,IACT,OAAO;AACL,aAAO;AAAA,IACT;AAAA,EACF,SAAS,KAAP;AACA,YAAQ,MAAM,wCAAwC,GAAG;AAAA,EAC3D;AACF;AAEO,IAAM,kCAAkC,MAAY;AACzD,MAAI;AACF,UAAM,SAAS,MAAM,UAAU,aAAa,aAAa,EAAE,OAAO,KAAK,CAAC;AACxE,UAAM,eAAe,IAAI,OAAO,aAAa;AAC7C,UAAM,aAAa,OAAO;AAC1B,WAAO,EAAE,QAAQ,aAAa;AAAA,EAChC,SAAS,KAAP;AACA,YAAQ,MAAM,wDAAwD,GAAG;AACzE,WAAO;AAAA,EACT;AACF;AAEA,IAAM,iBAAiB,CACrB,gBACA,kBACA,iBACA,gBACA,WACG;AACH,MAAI,CAAC,eAAe,WAAW,CAAC,gBAAgB,SAAS;AACvD,mBAAe,UAAU,MAAM,UAAU,aAAa,aAAa,EAAE,OAAO,KAAK,CAAC;AAClF,oBAAgB,UAAU,IAAI,OAAO,aAAa;AAClD,UAAM,gBAAgB,QAAQ,OAAO;AAAA,EACvC;AAEA,mBAAiB,UAAU,IAAI,cAAc,eAAe,OAAQ;AACpE,mBAAiB,QAAQ,MAAM,GAAI;AACnC,mBAAiB,QAAQ,kBAAkB,CAAC,UAAU;AACpD,mBAAe,KAAK,MAAM,IAAI;AAAA,EAChC;AACA,mBAAiB,QAAQ,SAAS;AACpC;AAEA,IAAM,gBAAgB,CAAC,qBAA6D;AAClF,MAAI,iBAAiB,WAAW,iBAAiB,QAAQ,UAAU,YAAY;AAC7E,qBAAiB,QAAQ,KAAK;AAAA,EAChC;AACF;AAEA,IAAM,kBAAkB,CAAO,gBAAwB,uBAA+B;AACpF,QAAM,eAAe,IAAI,KAAK,gBAAgB,EAAE,MAAM,YAAY,CAAC;AACnE,QAAM,WAAW,IAAI,SAAS;AAC9B,WAAS,OAAO,QAAQ,cAAc,eAAe;AAErD,QAAM,WAAW,MAAM,MAAM,oBAAoB;AAAA,IAC/C,QAAQ;AAAA,IACR,MAAM;AAAA,EACR,CAAC;AAED,MAAI,CAAC,SAAS,IAAI;AAChB,UAAM,IAAI,MAAM,UAAU,SAAS,YAAY;AAAA,EACjD;AAEA,QAAM,gBAAgB,MAAM,SAAS,KAAK;AAC1C,SAAO,cAAc;AACvB;AAEA,IAAM,oBAAoB,CAAC,MAAc,iBAAyB,iBAA+B;AAC/F,QAAM,cAAc,mBAAmB,IAAI;AAC3C,QAAM,MAAM,GAAG,wBAAwB;AAEvC,QAAM,GAAG,EACN,KAAK,CAAC,aAAa,SAAS,YAAY,CAAC,EACzC,KAAK,CAAC,gBAAgB,aAAa,gBAAgB,WAAW,CAAC,EAC/D,KAAK,CAAC,gBAAgB;AACrB,UAAM,SAAS,aAAa,mBAAmB;AAC/C,WAAO,SAAS;AAChB,WAAO,QAAQ,aAAa,WAAW;AACvC,WAAO,MAAM,CAAC;AAAA,EAChB,CAAC,EACA,MAAM,CAAC,UAAU;AAChB,YAAQ,MAAM,kCAAkC,KAAK;AAAA,EACvD,CAAC;AACL;AAMO,IAAM,gBAAgB,CAAC;AAAA,EAC5B;AAAA,EACA;AACF,MAGM;AACJ,QAAM,CAAC,iBAAiB,kBAAkB,QAAI,uBAA0B,MAAM;AAC9E,QAAM,qBAAiB,qBAA2B,IAAI;AACtD,QAAM,sBAAkB,qBAA4B,IAAI;AACxD,QAAM,uBAAmB,qBAA6B,IAAI;AAC1D,QAAM,qBAAiB,qBAAe,CAAC,CAAC;AACxC,QAAM,cAAU,qCAAkB;AAClC,QAAM,CAAC,2BAA2B,4BAA4B,QAAI,uBAAwB,IAAI;AAE9F,8BAAU,MAAM;AACd,QAAI,oBAAoB,aAAa;AACnC;AAAA,QACE;AAAA,QACA;AAAA,QACA;AAAA,QACA,eAAe;AAAA,QACf,MAAM;AACJ,6BAAmB,cAAc;AAAA,QACnC;AAAA,MACF;AAAA,IACF,OAAO;AACL,oBAAc,gBAAgB;AAC9B,UAAI,oBAAoB,gBAAgB;AACtC,wBAAgB,eAAe,SAAS,QAAQ,iBAAiB,kBAAmB,EAAE;AAAA,UACpF,CAAO,kBAAkB;AACvB,2BAAe,UAAU,CAAC;AAC1B,+BAAmB,MAAM;AACzB,kBAAM,UAAU,MAAM,aAAa,aAAa;AAChD,yCAA6B,QAAQ,EAAE;AAAA,UACzC;AAAA,QACF;AAAA,MACF;AAAA,IACF;AAEA,WAAO,MAAM;AACX,oBAAc,gBAAgB;AAAA,IAChC;AAAA,EACF,GAAG,CAAC,eAAe,CAAC;AAEpB,8BAAU,MAAM;AACd,QAAI,eAAe,SAAS,2BAA2B;AACrD,YAAM,mBAAmB,QAAQ,SAAS;AAAA,QACxC,CAAC,YAAY,QAAQ,OAAO;AAAA,MAC9B;AAEA,YAAM,oBAAoB,QAAQ,SAC/B,MAAM,mBAAmB,CAAC,EAC1B,OAAO,CAAC,YAAY,QAAQ,SAAS,eAAe,QAAQ,OAAO;AAEtE,YAAM,OAAO,kBAAkB,IAAI,CAAC,YAAY,QAAQ,OAAO,EAAE,KAAK,IAAI;AAC1E,wBAAkB,MAAM,QAAQ,iBAAiB,iBAAkB,gBAAgB,OAAQ;AAE3F,mCAA6B,IAAI;AAAA,IACnC;AAAA,EACF,GAAG,CAAC,2BAA2B,UAAU,CAAC;AAE1C,SAAO,EAAE,iBAAiB,mBAAmB;AAC/C;","names":[]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {
|
|
2
|
+
checkMicrophonePermission,
|
|
3
|
+
requestMicAndPlaybackPermission,
|
|
4
|
+
usePushToTalk
|
|
5
|
+
} from "../chunk-ANO23V2M.mjs";
|
|
6
|
+
import "../chunk-MRXNTQOX.mjs";
|
|
7
|
+
export {
|
|
8
|
+
checkMicrophonePermission,
|
|
9
|
+
requestMicAndPlaybackPermission,
|
|
10
|
+
usePushToTalk
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=use-push-to-talk.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|