@copilotkit/react-core 0.17.0-alpha.5 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +160 -160
- package/CHANGELOG.md +12 -0
- package/dist/chunk-5UGLWBZJ.mjs +3 -0
- package/dist/chunk-5UGLWBZJ.mjs.map +1 -0
- package/dist/chunk-6A4PCNMZ.mjs +48 -0
- package/dist/chunk-6A4PCNMZ.mjs.map +1 -0
- package/dist/chunk-6SOQYBNX.mjs +44 -0
- package/dist/chunk-6SOQYBNX.mjs.map +1 -0
- package/dist/chunk-7GFKOIO7.mjs +3 -0
- package/dist/chunk-7GFKOIO7.mjs.map +1 -0
- package/dist/chunk-7P4OYNP3.mjs +19 -0
- package/dist/chunk-7P4OYNP3.mjs.map +1 -0
- package/dist/chunk-BABVSMJR.mjs +3 -0
- package/dist/chunk-BABVSMJR.mjs.map +1 -0
- package/dist/chunk-EFZPSZWO.mjs +3 -0
- package/dist/chunk-EFZPSZWO.mjs.map +1 -0
- package/dist/chunk-F2JIAPZQ.mjs +13 -0
- package/dist/chunk-F2JIAPZQ.mjs.map +1 -0
- package/dist/chunk-FP4EXCGS.mjs +27 -0
- package/dist/chunk-FP4EXCGS.mjs.map +1 -0
- package/dist/chunk-FRAKUJWH.mjs +3 -0
- package/dist/chunk-FRAKUJWH.mjs.map +1 -0
- package/dist/chunk-HW5IH6PV.mjs +19 -0
- package/dist/chunk-HW5IH6PV.mjs.map +1 -0
- package/dist/chunk-JD7BAH7U.mjs +3 -0
- package/dist/chunk-JD7BAH7U.mjs.map +1 -0
- package/dist/chunk-MRXNTQOX.mjs +55 -0
- package/dist/chunk-MRXNTQOX.mjs.map +1 -0
- package/dist/chunk-MZ5UN3BY.mjs +28 -0
- package/dist/chunk-MZ5UN3BY.mjs.map +1 -0
- package/dist/chunk-PUFR5OAI.mjs +3 -0
- package/dist/chunk-PUFR5OAI.mjs.map +1 -0
- package/dist/chunk-QTET3FBV.mjs +165 -0
- package/dist/chunk-QTET3FBV.mjs.map +1 -0
- package/dist/chunk-SPCZTZCY.mjs +3 -0
- package/dist/chunk-SPCZTZCY.mjs.map +1 -0
- package/dist/chunk-TKXPMMND.mjs +177 -0
- package/dist/chunk-TKXPMMND.mjs.map +1 -0
- package/dist/chunk-TU4DNDO7.mjs +125 -0
- package/dist/chunk-TU4DNDO7.mjs.map +1 -0
- package/dist/chunk-VK25FIML.mjs +144 -0
- package/dist/chunk-VK25FIML.mjs.map +1 -0
- package/dist/chunk-VNRDQJXW.mjs +3 -0
- package/dist/chunk-VNRDQJXW.mjs.map +1 -0
- package/dist/chunk-VUY2K2DI.mjs +135 -0
- package/dist/chunk-VUY2K2DI.mjs.map +1 -0
- package/dist/chunk-WMXA2TIX.mjs +102 -0
- package/dist/chunk-WMXA2TIX.mjs.map +1 -0
- package/dist/chunk-XSS45JC5.mjs +77 -0
- package/dist/chunk-XSS45JC5.mjs.map +1 -0
- package/dist/chunk-YULKJPY3.mjs +70 -0
- package/dist/chunk-YULKJPY3.mjs.map +1 -0
- package/dist/components/copilot-provider/copilot-provider-props.mjs +2 -1
- package/dist/components/copilot-provider/copilot-provider-props.mjs.map +1 -1
- package/dist/components/copilot-provider/copilot-provider.mjs +7 -468
- package/dist/components/copilot-provider/copilot-provider.mjs.map +1 -1
- package/dist/components/copilot-provider/index.mjs +8 -467
- package/dist/components/copilot-provider/index.mjs.map +1 -1
- package/dist/components/copilot-provider/standard-copilot-api-config.mjs +3 -12
- package/dist/components/copilot-provider/standard-copilot-api-config.mjs.map +1 -1
- package/dist/components/index.mjs +9 -467
- package/dist/components/index.mjs.map +1 -1
- package/dist/context/copilot-context.mjs +3 -70
- package/dist/context/copilot-context.mjs.map +1 -1
- package/dist/context/index.mjs +4 -69
- package/dist/context/index.mjs.map +1 -1
- package/dist/hooks/index.mjs +17 -646
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/hooks/use-chat.mjs +5 -452
- package/dist/hooks/use-chat.mjs.map +1 -1
- package/dist/hooks/use-copilot-chat.mjs +13 -591
- package/dist/hooks/use-copilot-chat.mjs.map +1 -1
- package/dist/hooks/use-flat-category-store.mjs +3 -68
- package/dist/hooks/use-flat-category-store.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-actionable.mjs +4 -95
- package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-document-readable.mjs +4 -87
- package/dist/hooks/use-make-copilot-document-readable.mjs.map +1 -1
- package/dist/hooks/use-make-copilot-readable.mjs +4 -87
- package/dist/hooks/use-make-copilot-readable.mjs.map +1 -1
- package/dist/hooks/use-tree.mjs +3 -153
- package/dist/hooks/use-tree.mjs.map +1 -1
- package/dist/index.mjs +26 -1194
- package/dist/index.mjs.map +1 -1
- package/dist/openai/chat-completion-client.mjs +4 -353
- package/dist/openai/chat-completion-client.mjs.map +1 -1
- package/dist/openai/chat-completion-stream.mjs +4 -217
- package/dist/openai/chat-completion-stream.mjs.map +1 -1
- package/dist/openai/chat-completion-transport.mjs +3 -179
- package/dist/openai/chat-completion-transport.mjs.map +1 -1
- package/dist/openai/index.mjs +5 -217
- package/dist/openai/index.mjs.map +1 -1
- package/dist/openai-assistants/hooks/index.mjs +14 -235
- package/dist/openai-assistants/hooks/index.mjs.map +1 -1
- package/dist/openai-assistants/hooks/use-assistants.mjs +8 -52
- package/dist/openai-assistants/hooks/use-assistants.mjs.map +1 -1
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs +13 -236
- package/dist/openai-assistants/hooks/use-copilot-chat-v2.mjs.map +1 -1
- package/dist/openai-assistants/index.mjs +15 -236
- package/dist/openai-assistants/index.mjs.map +1 -1
- package/dist/openai-assistants/utils/index.mjs +4 -46
- package/dist/openai-assistants/utils/index.mjs.map +1 -1
- package/dist/openai-assistants/utils/process-message-stream.mjs +3 -46
- package/dist/openai-assistants/utils/process-message-stream.mjs.map +1 -1
- package/dist/types/annotated-function.mjs +2 -0
- package/dist/types/annotated-function.mjs.map +1 -1
- package/dist/types/base.mjs +2 -0
- package/dist/types/base.mjs.map +1 -1
- package/dist/types/document-pointer.mjs +2 -0
- package/dist/types/document-pointer.mjs.map +1 -1
- package/dist/types/index.mjs +2 -0
- package/dist/types/index.mjs.map +1 -1
- package/dist/types/message.mjs +2 -0
- package/dist/types/message.mjs.map +1 -1
- package/dist/utils/utils.mjs +2 -0
- package/dist/utils/utils.mjs.map +1 -1
- package/dist/utils/utils.test.mjs +1 -0
- package/dist/utils/utils.test.mjs.map +1 -1
- package/package.json +3 -3
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { CopilotContext } from './chunk-6A4PCNMZ.mjs';
|
|
2
|
+
import { useRef, useContext, useMemo, useEffect } from 'react';
|
|
3
|
+
import { nanoid } from 'nanoid';
|
|
4
|
+
|
|
5
|
+
function useMakeCopilotActionable(annotatedFunction, dependencies) {
|
|
6
|
+
const idRef = useRef(nanoid());
|
|
7
|
+
const { setEntryPoint, removeEntryPoint } = useContext(CopilotContext);
|
|
8
|
+
const memoizedAnnotatedFunction = useMemo(
|
|
9
|
+
() => ({
|
|
10
|
+
name: annotatedFunction.name,
|
|
11
|
+
description: annotatedFunction.description,
|
|
12
|
+
argumentAnnotations: annotatedFunction.argumentAnnotations,
|
|
13
|
+
implementation: annotatedFunction.implementation
|
|
14
|
+
}),
|
|
15
|
+
dependencies
|
|
16
|
+
);
|
|
17
|
+
useEffect(() => {
|
|
18
|
+
setEntryPoint(idRef.current, memoizedAnnotatedFunction);
|
|
19
|
+
return () => {
|
|
20
|
+
removeEntryPoint(idRef.current);
|
|
21
|
+
};
|
|
22
|
+
}, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { useMakeCopilotActionable };
|
|
26
|
+
//# sourceMappingURL=out.js.map
|
|
27
|
+
//# sourceMappingURL=chunk-FP4EXCGS.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-make-copilot-actionable.ts"],"names":[],"mappings":";;;;;AAEA,SAAS,QAAQ,YAAY,WAAW,eAAe;AAGvD,SAAS,cAAc;AAEhB,SAAS,yBACd,mBACA,cACA;AACA,QAAM,QAAQ,OAAO,OAAO,CAAC;AAC7B,QAAM,EAAE,eAAe,iBAAiB,IAAI,WAAW,cAAc;AAErE,QAAM,4BAA4D;AAAA,IAChE,OAAO;AAAA,MACL,MAAM,kBAAkB;AAAA,MACxB,aAAa,kBAAkB;AAAA,MAC/B,qBAAqB,kBAAkB;AAAA,MACvC,gBAAgB,kBAAkB;AAAA,IACpC;AAAA,IACA;AAAA,EACF;AAEA,YAAU,MAAM;AACd,kBAAc,MAAM,SAAS,yBAAqD;AAElF,WAAO,MAAM;AACX,uBAAiB,MAAM,OAAO;AAAA,IAChC;AAAA,EACF,GAAG,CAAC,2BAA2B,eAAe,gBAAgB,CAAC;AACjE","sourcesContent":["\"use client\";\n\nimport { useRef, useContext, useEffect, useMemo } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\nimport { AnnotatedFunction } from \"../types/annotated-function\";\nimport { nanoid } from \"nanoid\";\n\nexport function useMakeCopilotActionable<ActionInput extends any[]>(\n annotatedFunction: AnnotatedFunction<ActionInput>,\n dependencies: any[],\n) {\n const idRef = useRef(nanoid()); // generate a unique id\n const { setEntryPoint, removeEntryPoint } = useContext(CopilotContext);\n\n const memoizedAnnotatedFunction: AnnotatedFunction<ActionInput> = useMemo(\n () => ({\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n argumentAnnotations: annotatedFunction.argumentAnnotations,\n implementation: annotatedFunction.implementation,\n }),\n dependencies,\n );\n\n useEffect(() => {\n setEntryPoint(idRef.current, memoizedAnnotatedFunction as AnnotatedFunction<any[]>);\n\n return () => {\n removeEntryPoint(idRef.current);\n };\n }, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { CopilotContext } from './chunk-6A4PCNMZ.mjs';
|
|
2
|
+
import { useContext, useRef, useEffect } from 'react';
|
|
3
|
+
|
|
4
|
+
function useMakeCopilotReadable(information, parentId, categories) {
|
|
5
|
+
const { addContext, removeContext } = useContext(CopilotContext);
|
|
6
|
+
const idRef = useRef();
|
|
7
|
+
useEffect(() => {
|
|
8
|
+
const id = addContext(information, parentId, categories);
|
|
9
|
+
idRef.current = id;
|
|
10
|
+
return () => {
|
|
11
|
+
removeContext(id);
|
|
12
|
+
};
|
|
13
|
+
}, [information, parentId, addContext, removeContext]);
|
|
14
|
+
return idRef.current;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { useMakeCopilotReadable };
|
|
18
|
+
//# sourceMappingURL=out.js.map
|
|
19
|
+
//# sourceMappingURL=chunk-HW5IH6PV.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/hooks/use-make-copilot-readable.ts"],"names":[],"mappings":";;;;;AAEA,SAAS,YAAY,WAAW,cAAc;AAUvC,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","sourcesContent":["\"use client\";\n\nimport { useContext, useEffect, useRef } from \"react\";\nimport { CopilotContext } from \"../context/copilot-context\";\n\n/**\n * Adds the given information to the Copilot context to make it readable by Copilot.\n * @param information - The information to be added to the Copilot context.\n * @param parentId - The ID of the parent context, if any.\n * @param categories - An array of categories to control which context are visible where. Particularly useful with CopilotTextarea (see `useMakeAutosuggestionFunction`)\n * @returns The ID of the added context.\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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defProps = Object.defineProperties;
|
|
3
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
4
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
7
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
8
|
+
var __spreadValues = (a, b) => {
|
|
9
|
+
for (var prop in b || (b = {}))
|
|
10
|
+
if (__hasOwnProp.call(b, prop))
|
|
11
|
+
__defNormalProp(a, prop, b[prop]);
|
|
12
|
+
if (__getOwnPropSymbols)
|
|
13
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
14
|
+
if (__propIsEnum.call(b, prop))
|
|
15
|
+
__defNormalProp(a, prop, b[prop]);
|
|
16
|
+
}
|
|
17
|
+
return a;
|
|
18
|
+
};
|
|
19
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
20
|
+
var __objRest = (source, exclude) => {
|
|
21
|
+
var target = {};
|
|
22
|
+
for (var prop in source)
|
|
23
|
+
if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
|
|
24
|
+
target[prop] = source[prop];
|
|
25
|
+
if (source != null && __getOwnPropSymbols)
|
|
26
|
+
for (var prop of __getOwnPropSymbols(source)) {
|
|
27
|
+
if (exclude.indexOf(prop) < 0 && __propIsEnum.call(source, prop))
|
|
28
|
+
target[prop] = source[prop];
|
|
29
|
+
}
|
|
30
|
+
return target;
|
|
31
|
+
};
|
|
32
|
+
var __async = (__this, __arguments, generator) => {
|
|
33
|
+
return new Promise((resolve, reject) => {
|
|
34
|
+
var fulfilled = (value) => {
|
|
35
|
+
try {
|
|
36
|
+
step(generator.next(value));
|
|
37
|
+
} catch (e) {
|
|
38
|
+
reject(e);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var rejected = (value) => {
|
|
42
|
+
try {
|
|
43
|
+
step(generator.throw(value));
|
|
44
|
+
} catch (e) {
|
|
45
|
+
reject(e);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
49
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
50
|
+
});
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export { __async, __objRest, __spreadProps, __spreadValues };
|
|
54
|
+
//# sourceMappingURL=out.js.map
|
|
55
|
+
//# sourceMappingURL=chunk-MRXNTQOX.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":"","sourcesContent":[]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { __async } from './chunk-MRXNTQOX.mjs';
|
|
2
|
+
|
|
3
|
+
// src/openai-assistants/utils/process-message-stream.ts
|
|
4
|
+
function processMessageStream(reader, processMessage) {
|
|
5
|
+
return __async(this, null, function* () {
|
|
6
|
+
const decoder = new TextDecoder();
|
|
7
|
+
let buffer = "";
|
|
8
|
+
while (true) {
|
|
9
|
+
const { done, value } = yield reader.read();
|
|
10
|
+
if (done) {
|
|
11
|
+
if (buffer.length > 0) {
|
|
12
|
+
processMessage(buffer);
|
|
13
|
+
}
|
|
14
|
+
break;
|
|
15
|
+
}
|
|
16
|
+
buffer += decoder.decode(value, { stream: true });
|
|
17
|
+
let endIndex;
|
|
18
|
+
while ((endIndex = buffer.indexOf("\n")) !== -1) {
|
|
19
|
+
processMessage(buffer.substring(0, endIndex).trim());
|
|
20
|
+
buffer = buffer.substring(endIndex + 1);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { processMessageStream };
|
|
27
|
+
//# sourceMappingURL=out.js.map
|
|
28
|
+
//# sourceMappingURL=chunk-MZ5UN3BY.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/openai-assistants/utils/process-message-stream.ts"],"names":[],"mappings":";;;;;AAAA,SAAsB,qBACpB,QACA,gBACA;AAAA;AACA,UAAM,UAAU,IAAI,YAAY;AAChC,QAAI,SAAS;AACb,WAAO,MAAM;AACX,YAAM,EAAE,MAAM,MAAM,IAAI,MAAM,OAAO,KAAK;AAE1C,UAAI,MAAM;AACR,YAAI,OAAO,SAAS,GAAG;AACrB,yBAAe,MAAM;AAAA,QACvB;AACA;AAAA,MACF;AAEA,gBAAU,QAAQ,OAAO,OAAO,EAAE,QAAQ,KAAK,CAAC;AAEhD,UAAI;AACJ,cAAQ,WAAW,OAAO,QAAQ,IAAI,OAAO,IAAI;AAC/C,uBAAe,OAAO,UAAU,GAAG,QAAQ,EAAE,KAAK,CAAC;AACnD,iBAAS,OAAO,UAAU,WAAW,CAAC;AAAA,MACxC;AAAA,IACF;AAAA,EACF;AAAA","sourcesContent":["export async function processMessageStream(\n reader: ReadableStreamDefaultReader<Uint8Array>,\n processMessage: (message: string) => void | Promise<void>,\n) {\n const decoder = new TextDecoder();\n let buffer = \"\";\n while (true) {\n const { done, value } = await reader.read();\n\n if (done) {\n if (buffer.length > 0) {\n processMessage(buffer);\n }\n break;\n }\n\n buffer += decoder.decode(value, { stream: true });\n\n let endIndex: number;\n while ((endIndex = buffer.indexOf(\"\\n\")) !== -1) {\n processMessage(buffer.substring(0, endIndex).trim());\n buffer = buffer.substring(endIndex + 1); // Remove the processed instruction + delimiter\n }\n }\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { StandardCopilotApiConfig } from './chunk-F2JIAPZQ.mjs';
|
|
2
|
+
import { use_tree_default } from './chunk-VUY2K2DI.mjs';
|
|
3
|
+
import { CopilotContext } from './chunk-6A4PCNMZ.mjs';
|
|
4
|
+
import { use_flat_category_store_default } from './chunk-YULKJPY3.mjs';
|
|
5
|
+
import { __objRest, __spreadProps, __spreadValues, __async } from './chunk-MRXNTQOX.mjs';
|
|
6
|
+
import { useState, useCallback } from 'react';
|
|
7
|
+
import { jsx } from 'react/jsx-runtime';
|
|
8
|
+
|
|
9
|
+
function CopilotProvider(_a) {
|
|
10
|
+
var _b = _a, { children } = _b, props = __objRest(_b, ["children"]);
|
|
11
|
+
const [entryPoints, setEntryPoints] = useState({});
|
|
12
|
+
const { addElement, removeElement, printTree } = use_tree_default();
|
|
13
|
+
const {
|
|
14
|
+
addElement: addDocument,
|
|
15
|
+
removeElement: removeDocument,
|
|
16
|
+
allElements: allDocuments
|
|
17
|
+
} = use_flat_category_store_default();
|
|
18
|
+
const setEntryPoint = useCallback((id, entryPoint) => {
|
|
19
|
+
setEntryPoints((prevPoints) => {
|
|
20
|
+
return __spreadProps(__spreadValues({}, prevPoints), {
|
|
21
|
+
[id]: entryPoint
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
}, []);
|
|
25
|
+
const removeEntryPoint = useCallback((id) => {
|
|
26
|
+
setEntryPoints((prevPoints) => {
|
|
27
|
+
const newPoints = __spreadValues({}, prevPoints);
|
|
28
|
+
delete newPoints[id];
|
|
29
|
+
return newPoints;
|
|
30
|
+
});
|
|
31
|
+
}, []);
|
|
32
|
+
const getContextString = useCallback(
|
|
33
|
+
(documents, categories) => {
|
|
34
|
+
const documentsString = documents.map((document) => {
|
|
35
|
+
return `${document.name} (${document.sourceApplication}):
|
|
36
|
+
${document.getContents()}`;
|
|
37
|
+
}).join("\n\n");
|
|
38
|
+
const nonDocumentStrings = printTree(categories);
|
|
39
|
+
return `${documentsString}
|
|
40
|
+
|
|
41
|
+
${nonDocumentStrings}`;
|
|
42
|
+
},
|
|
43
|
+
[printTree]
|
|
44
|
+
);
|
|
45
|
+
const addContext = useCallback(
|
|
46
|
+
(context, parentId, categories = defaultCopilotContextCategories) => {
|
|
47
|
+
return addElement(context, categories, parentId);
|
|
48
|
+
},
|
|
49
|
+
[addElement]
|
|
50
|
+
);
|
|
51
|
+
const removeContext = useCallback(
|
|
52
|
+
(id) => {
|
|
53
|
+
removeElement(id);
|
|
54
|
+
},
|
|
55
|
+
[removeElement]
|
|
56
|
+
);
|
|
57
|
+
const getChatCompletionFunctionDescriptions = useCallback(() => {
|
|
58
|
+
return entryPointsToChatCompletionFunctions(Object.values(entryPoints));
|
|
59
|
+
}, [entryPoints]);
|
|
60
|
+
const getFunctionCallHandler = useCallback(() => {
|
|
61
|
+
return entryPointsToFunctionCallHandler(Object.values(entryPoints));
|
|
62
|
+
}, [entryPoints]);
|
|
63
|
+
const getDocumentsContext = useCallback(
|
|
64
|
+
(categories) => {
|
|
65
|
+
return allDocuments(categories);
|
|
66
|
+
},
|
|
67
|
+
[allDocuments]
|
|
68
|
+
);
|
|
69
|
+
const addDocumentContext = useCallback(
|
|
70
|
+
(documentPointer, categories = defaultCopilotContextCategories) => {
|
|
71
|
+
return addDocument(documentPointer, categories);
|
|
72
|
+
},
|
|
73
|
+
[addDocument]
|
|
74
|
+
);
|
|
75
|
+
const removeDocumentContext = useCallback(
|
|
76
|
+
(documentId) => {
|
|
77
|
+
removeDocument(documentId);
|
|
78
|
+
},
|
|
79
|
+
[removeDocument]
|
|
80
|
+
);
|
|
81
|
+
let copilotApiConfig;
|
|
82
|
+
if ("chatApiEndpoint" in props) {
|
|
83
|
+
copilotApiConfig = new StandardCopilotApiConfig(
|
|
84
|
+
props.chatApiEndpoint,
|
|
85
|
+
props.chatApiEndpointV2 || `${props.chatApiEndpoint}/v2`,
|
|
86
|
+
{},
|
|
87
|
+
{}
|
|
88
|
+
);
|
|
89
|
+
} else {
|
|
90
|
+
copilotApiConfig = props.chatApiConfig;
|
|
91
|
+
}
|
|
92
|
+
return /* @__PURE__ */ jsx(
|
|
93
|
+
CopilotContext.Provider,
|
|
94
|
+
{
|
|
95
|
+
value: {
|
|
96
|
+
entryPoints,
|
|
97
|
+
getChatCompletionFunctionDescriptions,
|
|
98
|
+
getFunctionCallHandler,
|
|
99
|
+
setEntryPoint,
|
|
100
|
+
removeEntryPoint,
|
|
101
|
+
getContextString,
|
|
102
|
+
addContext,
|
|
103
|
+
removeContext,
|
|
104
|
+
getDocumentsContext,
|
|
105
|
+
addDocumentContext,
|
|
106
|
+
removeDocumentContext,
|
|
107
|
+
copilotApiConfig
|
|
108
|
+
},
|
|
109
|
+
children
|
|
110
|
+
}
|
|
111
|
+
);
|
|
112
|
+
}
|
|
113
|
+
var defaultCopilotContextCategories = ["global"];
|
|
114
|
+
function entryPointsToFunctionCallHandler(entryPoints) {
|
|
115
|
+
return (chatMessages, functionCall) => __async(this, null, function* () {
|
|
116
|
+
let entrypointsByFunctionName = {};
|
|
117
|
+
for (let entryPoint of entryPoints) {
|
|
118
|
+
entrypointsByFunctionName[entryPoint.name] = entryPoint;
|
|
119
|
+
}
|
|
120
|
+
const entryPointFunction = entrypointsByFunctionName[functionCall.name || ""];
|
|
121
|
+
if (entryPointFunction) {
|
|
122
|
+
let functionCallArguments = [];
|
|
123
|
+
if (functionCall.arguments) {
|
|
124
|
+
functionCallArguments = JSON.parse(functionCall.arguments);
|
|
125
|
+
}
|
|
126
|
+
const paramsInCorrectOrder = [];
|
|
127
|
+
for (let arg of entryPointFunction.argumentAnnotations) {
|
|
128
|
+
paramsInCorrectOrder.push(
|
|
129
|
+
functionCallArguments[arg.name]
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
yield entryPointFunction.implementation(...paramsInCorrectOrder);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
function entryPointsToChatCompletionFunctions(entryPoints) {
|
|
137
|
+
return entryPoints.map(annotatedFunctionToChatCompletionFunction);
|
|
138
|
+
}
|
|
139
|
+
function annotatedFunctionToChatCompletionFunction(annotatedFunction) {
|
|
140
|
+
let parameters = {};
|
|
141
|
+
for (let arg of annotatedFunction.argumentAnnotations) {
|
|
142
|
+
let _a = arg, forwardedArgs = __objRest(_a, ["name", "required"]);
|
|
143
|
+
parameters[arg.name] = forwardedArgs;
|
|
144
|
+
}
|
|
145
|
+
let requiredParameterNames = [];
|
|
146
|
+
for (let arg of annotatedFunction.argumentAnnotations) {
|
|
147
|
+
if (arg.required) {
|
|
148
|
+
requiredParameterNames.push(arg.name);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
let chatCompletionFunction = {
|
|
152
|
+
name: annotatedFunction.name,
|
|
153
|
+
description: annotatedFunction.description,
|
|
154
|
+
parameters: {
|
|
155
|
+
type: "object",
|
|
156
|
+
properties: parameters,
|
|
157
|
+
required: requiredParameterNames
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
return chatCompletionFunction;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export { CopilotProvider, defaultCopilotContextCategories };
|
|
164
|
+
//# sourceMappingURL=out.js.map
|
|
165
|
+
//# sourceMappingURL=chunk-QTET3FBV.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/components/copilot-provider/copilot-provider.tsx"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAEA,SAAS,aAAa,gBAAgB;AA0JlC;AA3GG,SAAS,gBAAgB,IAA2D;AAA3D,eAAE,WAjDlC,IAiDgC,IAAe,kBAAf,I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pD,UAAI,aAAa,WAAW;AAC1B,gCAAwB,KAAK,MAAM,aAAa,SAAS;AAAA,MAC3D;AAEA,YAAM,uBAA8B,CAAC;AACrC,eAAS,OAAO,mBAAmB,qBAAqB;AACtD,6BAAqB;AAAA,UACnB,sBAAsB,IAAI,IAA0C;AAAA,QACtE;AAAA,MACF;AAEA,YAAM,mBAAmB,eAAe,GAAG,oBAAoB;AAAA,IAmBjE;AAAA,EACF;AACF;AAEA,SAAS,qCACP,aACuC;AACvC,SAAO,YAAY,IAAI,yCAAyC;AAClE;AAEA,SAAS,0CACP,mBACqC;AAErC,MAAI,aAAqC,CAAC;AAC1C,WAAS,OAAO,kBAAkB,qBAAqB;AAErD,QAA2C,UAArC,QAAM,SA9OhB,IA8O+C,IAAlB,0BAAkB,IAAlB,CAAnB,QAAM;AACZ,eAAW,IAAI,IAAI,IAAI;AAAA,EACzB;AAEA,MAAI,yBAAmC,CAAC;AACxC,WAAS,OAAO,kBAAkB,qBAAqB;AACrD,QAAI,IAAI,UAAU;AAChB,6BAAuB,KAAK,IAAI,IAAI;AAAA,IACtC;AAAA,EACF;AAGA,MAAI,yBAA8D;AAAA,IAChE,MAAM,kBAAkB;AAAA,IACxB,aAAa,kBAAkB;AAAA,IAC/B,YAAY;AAAA,MACV,MAAM;AAAA,MACN,YAAY;AAAA,MACZ,UAAU;AAAA,IACZ;AAAA,EACF;AAEA,SAAO;AACT","sourcesContent":["\"use client\";\n\nimport { useCallback, useState } from \"react\";\nimport { CopilotContext, CopilotApiConfig } from \"../../context/copilot-context\";\nimport useTree from \"../../hooks/use-tree\";\nimport { AnnotatedFunction } from \"../../types/annotated-function\";\nimport { ChatCompletionCreateParams } from \"openai/resources/chat\";\nimport { DocumentPointer, FunctionCallHandler } from \"../../types\";\nimport useFlatCategoryStore from \"../../hooks/use-flat-category-store\";\nimport { StandardCopilotApiConfig } from \"./standard-copilot-api-config\";\nimport { CopilotProviderProps } from \"./copilot-provider-props\";\n\n/**\n * The CopilotProvider 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 * <CopilotProvider chatApiEndpoint=\"https://your.copilotkit.api\">\n * <App />\n * </CopilotProvider>\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 * <CopilotProvider chatApiConfig={copilotApiConfig}>\n * <App />\n * </CopilotProvider>\n * ```\n *\n * @param props - The props for the component.\n * @returns The CopilotProvider component.\n */\nexport function CopilotProvider({ children, ...props }: CopilotProviderProps): JSX.Element {\n // Compute all the functions and properties that we need to pass\n // to the CopilotContext.\n\n const [entryPoints, setEntryPoints] = useState<Record<string, AnnotatedFunction<any[]>>>({});\n\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: AnnotatedFunction<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 return entryPointsToChatCompletionFunctions(Object.values(entryPoints));\n }, [entryPoints]);\n\n const getFunctionCallHandler = useCallback(() => {\n return entryPointsToFunctionCallHandler(Object.values(entryPoints));\n }, [entryPoints]);\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 let copilotApiConfig: CopilotApiConfig;\n if (\"chatApiEndpoint\" in props) {\n copilotApiConfig = new StandardCopilotApiConfig(\n props.chatApiEndpoint,\n props.chatApiEndpointV2 || `${props.chatApiEndpoint}/v2`,\n {},\n {},\n );\n } else {\n copilotApiConfig = props.chatApiConfig;\n }\n\n return (\n <CopilotContext.Provider\n value={{\n entryPoints,\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 entryPointsToFunctionCallHandler(\n entryPoints: AnnotatedFunction<any[]>[],\n): FunctionCallHandler {\n return async (chatMessages, functionCall) => {\n let entrypointsByFunctionName: Record<string, AnnotatedFunction<any[]>> = {};\n for (let entryPoint of entryPoints) {\n entrypointsByFunctionName[entryPoint.name] = entryPoint;\n }\n\n const entryPointFunction = entrypointsByFunctionName[functionCall.name || \"\"];\n if (entryPointFunction) {\n let functionCallArguments: Record<string, any>[] = [];\n if (functionCall.arguments) {\n functionCallArguments = JSON.parse(functionCall.arguments);\n }\n\n const paramsInCorrectOrder: any[] = [];\n for (let arg of entryPointFunction.argumentAnnotations) {\n paramsInCorrectOrder.push(\n functionCallArguments[arg.name as keyof typeof functionCallArguments],\n );\n }\n\n await entryPointFunction.implementation(...paramsInCorrectOrder);\n\n // commented out becasue for now we don't want to return anything\n // const result = await entryPointFunction.implementation(\n // ...parsedFunctionCallArguments\n // );\n // const functionResponse: ChatRequest = {\n // messages: [\n // ...chatMessages,\n // {\n // id: nanoid(),\n // name: functionCall.name,\n // role: 'function' as const,\n // content: JSON.stringify(result),\n // },\n // ],\n // };\n\n // return functionResponse;\n }\n };\n}\n\nfunction entryPointsToChatCompletionFunctions(\n entryPoints: AnnotatedFunction<any[]>[],\n): ChatCompletionCreateParams.Function[] {\n return entryPoints.map(annotatedFunctionToChatCompletionFunction);\n}\n\nfunction annotatedFunctionToChatCompletionFunction(\n annotatedFunction: AnnotatedFunction<any[]>,\n): ChatCompletionCreateParams.Function {\n // Create the parameters object based on the argumentAnnotations\n let parameters: { [key: string]: any } = {};\n for (let arg of annotatedFunction.argumentAnnotations) {\n // isolate the args we should forward inline\n let { name, required, ...forwardedArgs } = arg;\n parameters[arg.name] = forwardedArgs;\n }\n\n let requiredParameterNames: string[] = [];\n for (let arg of annotatedFunction.argumentAnnotations) {\n if (arg.required) {\n requiredParameterNames.push(arg.name);\n }\n }\n\n // Create the ChatCompletionFunctions object\n let chatCompletionFunction: ChatCompletionCreateParams.Function = {\n name: annotatedFunction.name,\n description: annotatedFunction.description,\n parameters: {\n type: \"object\",\n properties: parameters,\n required: requiredParameterNames,\n },\n };\n\n return chatCompletionFunction;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"names":[],"mappings":""}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { ChatCompletionTransport } from './chunk-VK25FIML.mjs';
|
|
2
|
+
import { __async, __spreadValues } from './chunk-MRXNTQOX.mjs';
|
|
3
|
+
import EventEmitter from 'eventemitter3';
|
|
4
|
+
|
|
5
|
+
var DEFAULT_MAX_TOKENS = 8192;
|
|
6
|
+
var ChatCompletionClient = class extends EventEmitter {
|
|
7
|
+
constructor(params) {
|
|
8
|
+
super();
|
|
9
|
+
this.chatCompletionTransport = null;
|
|
10
|
+
this.mode = null;
|
|
11
|
+
this.functionCallName = "";
|
|
12
|
+
this.functionCallArguments = "";
|
|
13
|
+
this.onData = (data) => {
|
|
14
|
+
if (this.mode === "function" && !data.choices[0].delta.function_call) {
|
|
15
|
+
const success = this.tryFlushFunctionCall();
|
|
16
|
+
if (!success) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
this.mode = data.choices[0].delta.function_call ? "function" : "message";
|
|
21
|
+
if (this.mode === "message") {
|
|
22
|
+
if (data.choices[0].delta.content) {
|
|
23
|
+
this.emit("content", data.choices[0].delta.content);
|
|
24
|
+
}
|
|
25
|
+
return;
|
|
26
|
+
} else if (this.mode === "function") {
|
|
27
|
+
if (data.choices[0].delta.function_call.name) {
|
|
28
|
+
this.functionCallName = data.choices[0].delta.function_call.name;
|
|
29
|
+
}
|
|
30
|
+
if (data.choices[0].delta.function_call.arguments) {
|
|
31
|
+
this.functionCallArguments += data.choices[0].delta.function_call.arguments;
|
|
32
|
+
}
|
|
33
|
+
this.emit("partial", this.functionCallName, this.functionCallArguments);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
this.onError = (error) => {
|
|
38
|
+
this.emit("error", error);
|
|
39
|
+
this.cleanup();
|
|
40
|
+
};
|
|
41
|
+
this.onEnd = () => {
|
|
42
|
+
if (this.mode === "function") {
|
|
43
|
+
const success = this.tryFlushFunctionCall();
|
|
44
|
+
if (!success) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
this.emit("end");
|
|
49
|
+
this.cleanup();
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
fetch(params) {
|
|
53
|
+
return __async(this, null, function* () {
|
|
54
|
+
params = __spreadValues({}, params);
|
|
55
|
+
if (params.model && params.model in maxTokensByModel) {
|
|
56
|
+
params.maxTokens || (params.maxTokens = maxTokensByModel[params.model]);
|
|
57
|
+
} else {
|
|
58
|
+
params.maxTokens || (params.maxTokens = DEFAULT_MAX_TOKENS);
|
|
59
|
+
}
|
|
60
|
+
params.functions || (params.functions = []);
|
|
61
|
+
params.messages = this.buildPrompt(params);
|
|
62
|
+
return yield this.runPrompt(params);
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
buildPrompt(params) {
|
|
66
|
+
let maxTokens = params.maxTokens;
|
|
67
|
+
const messages = params.messages;
|
|
68
|
+
const functions = params.functions;
|
|
69
|
+
const functionsNumTokens = countFunctionsTokens(functions);
|
|
70
|
+
if (functionsNumTokens > maxTokens) {
|
|
71
|
+
throw new Error(`Too many tokens in function calls: ${functionsNumTokens} > ${maxTokens}`);
|
|
72
|
+
}
|
|
73
|
+
maxTokens -= functionsNumTokens;
|
|
74
|
+
for (const message of messages) {
|
|
75
|
+
if (message.role === "system") {
|
|
76
|
+
const numTokens = this.countTokens(message);
|
|
77
|
+
maxTokens -= numTokens;
|
|
78
|
+
if (maxTokens < 0) {
|
|
79
|
+
throw new Error("Not enough tokens for system message.");
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
const result = [];
|
|
84
|
+
let cutoff = false;
|
|
85
|
+
const reversedMessages = [...messages].reverse();
|
|
86
|
+
for (const message of reversedMessages) {
|
|
87
|
+
if (message.role === "system") {
|
|
88
|
+
result.unshift(message);
|
|
89
|
+
continue;
|
|
90
|
+
} else if (cutoff) {
|
|
91
|
+
continue;
|
|
92
|
+
}
|
|
93
|
+
let numTokens = this.countTokens(message);
|
|
94
|
+
if (maxTokens < numTokens) {
|
|
95
|
+
cutoff = true;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
result.unshift(message);
|
|
99
|
+
maxTokens -= numTokens;
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
runPrompt(params) {
|
|
104
|
+
return __async(this, null, function* () {
|
|
105
|
+
this.chatCompletionTransport = new ChatCompletionTransport({});
|
|
106
|
+
this.chatCompletionTransport.on("data", this.onData);
|
|
107
|
+
this.chatCompletionTransport.on("error", this.onError);
|
|
108
|
+
this.chatCompletionTransport.on("end", this.onEnd);
|
|
109
|
+
yield this.chatCompletionTransport.fetch(params);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
tryFlushFunctionCall() {
|
|
113
|
+
let args = null;
|
|
114
|
+
try {
|
|
115
|
+
args = JSON.parse(this.functionCallArguments);
|
|
116
|
+
} catch (error) {
|
|
117
|
+
this.emit("error", error);
|
|
118
|
+
this.cleanup();
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
this.emit("function", {
|
|
122
|
+
name: this.functionCallName,
|
|
123
|
+
arguments: args
|
|
124
|
+
});
|
|
125
|
+
this.mode = null;
|
|
126
|
+
this.functionCallName = "";
|
|
127
|
+
this.functionCallArguments = "";
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
cleanup() {
|
|
131
|
+
if (this.chatCompletionTransport) {
|
|
132
|
+
this.chatCompletionTransport.off("data", this.onData);
|
|
133
|
+
this.chatCompletionTransport.off("error", this.onError);
|
|
134
|
+
this.chatCompletionTransport.off("end", this.onEnd);
|
|
135
|
+
}
|
|
136
|
+
this.chatCompletionTransport = null;
|
|
137
|
+
this.mode = null;
|
|
138
|
+
this.functionCallName = "";
|
|
139
|
+
this.functionCallArguments = "";
|
|
140
|
+
}
|
|
141
|
+
countTokens(message) {
|
|
142
|
+
if (message.content) {
|
|
143
|
+
return estimateTokens(message.content);
|
|
144
|
+
} else if (message.function_call) {
|
|
145
|
+
return estimateTokens(JSON.stringify(message.function_call));
|
|
146
|
+
}
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
var maxTokensByModel = {
|
|
151
|
+
"gpt-3.5-turbo": 4097,
|
|
152
|
+
"gpt-3.5-turbo-16k": 16385,
|
|
153
|
+
"gpt-4": 8192,
|
|
154
|
+
"gpt-4-1106-preview": 8192,
|
|
155
|
+
"gpt-4-32k": 32768,
|
|
156
|
+
"gpt-3.5-turbo-0301": 4097,
|
|
157
|
+
"gpt-4-0314": 8192,
|
|
158
|
+
"gpt-4-32k-0314": 32768,
|
|
159
|
+
"gpt-3.5-turbo-0613": 4097,
|
|
160
|
+
"gpt-4-0613": 8192,
|
|
161
|
+
"gpt-4-32k-0613": 32768,
|
|
162
|
+
"gpt-3.5-turbo-16k-0613": 16385
|
|
163
|
+
};
|
|
164
|
+
function estimateTokens(text) {
|
|
165
|
+
return text.length / 3;
|
|
166
|
+
}
|
|
167
|
+
function countFunctionsTokens(functions) {
|
|
168
|
+
if (functions.length === 0) {
|
|
169
|
+
return 0;
|
|
170
|
+
}
|
|
171
|
+
const json = JSON.stringify(functions);
|
|
172
|
+
return estimateTokens(json);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export { ChatCompletionClient };
|
|
176
|
+
//# sourceMappingURL=out.js.map
|
|
177
|
+
//# sourceMappingURL=chunk-TKXPMMND.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/openai/chat-completion-client.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,kBAAkB;AAiCzB,IAAM,qBAAqB;AAEpB,IAAM,uBAAN,cAAmC,aAAyC;AAAA,EAMjF,YAAY,QAA2C;AACrD,UAAM;AANR,SAAQ,0BAA0D;AAClE,SAAQ,OAAsC;AAC9C,SAAQ,mBAA2B;AACnC,SAAQ,wBAAgC;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,MAAI,UAAU,WAAW,GAAG;AAC1B,WAAO;AAAA,EACT;AACA,QAAM,OAAO,KAAK,UAAU,SAAS;AACrC,SAAO,eAAe,IAAI;AAC5B","sourcesContent":["import EventEmitter from \"eventemitter3\";\nimport { Function, Message, Role } from \"../types\";\nimport {\n ChatCompletionTransport,\n ChatCompletionTransportFetchParams,\n} from \"./chat-completion-transport\";\n\ninterface ChatCompletionClientConfiguration {}\n\ninterface ChatCompletionClientEvents {\n content: string;\n partial: [string, string];\n error: any;\n function: {\n name: string;\n arguments: any;\n };\n end: void;\n}\n\nexport interface ChatCompletionChunk {\n choices: {\n delta: {\n role: Role;\n content?: string | null;\n function_call?: {\n name?: string;\n arguments?: string;\n };\n };\n }[];\n}\n\nconst DEFAULT_MAX_TOKENS = 8192;\n\nexport class ChatCompletionClient extends EventEmitter<ChatCompletionClientEvents> {\n private chatCompletionTransport: ChatCompletionTransport | null = null;\n private mode: \"function\" | \"message\" | null = null;\n private functionCallName: string = \"\";\n private functionCallArguments: string = \"\";\n\n constructor(params: ChatCompletionClientConfiguration) {\n super();\n }\n\n public async fetch(params: ChatCompletionTransportFetchParams) {\n params = { ...params };\n if (params.model && params.model in maxTokensByModel) {\n params.maxTokens ||= maxTokensByModel[params.model];\n } else {\n params.maxTokens ||= DEFAULT_MAX_TOKENS;\n }\n\n params.functions ||= [];\n params.messages = this.buildPrompt(params);\n return await this.runPrompt(params);\n }\n\n private buildPrompt(params: ChatCompletionTransportFetchParams): Message[] {\n let maxTokens = params.maxTokens!;\n const messages = params.messages!;\n const functions = params.functions!;\n const functionsNumTokens = countFunctionsTokens(functions);\n if (functionsNumTokens > maxTokens) {\n throw new Error(`Too many tokens in function calls: ${functionsNumTokens} > ${maxTokens}`);\n }\n maxTokens -= functionsNumTokens;\n\n for (const message of messages) {\n if (message.role === \"system\") {\n const numTokens = this.countTokens(message);\n maxTokens -= numTokens;\n\n if (maxTokens < 0) {\n throw new Error(\"Not enough tokens for system message.\");\n }\n }\n }\n\n const result: Message[] = [];\n let cutoff: boolean = false;\n\n const reversedMessages = [...messages].reverse();\n for (const message of reversedMessages) {\n if (message.role === \"system\") {\n result.unshift(message);\n continue;\n } else if (cutoff) {\n continue;\n }\n let numTokens = this.countTokens(message);\n if (maxTokens < numTokens) {\n cutoff = true;\n continue;\n }\n result.unshift(message);\n maxTokens -= numTokens;\n }\n\n return result;\n }\n\n private async runPrompt(params: ChatCompletionTransportFetchParams): Promise<void> {\n this.chatCompletionTransport = new ChatCompletionTransport({});\n\n this.chatCompletionTransport.on(\"data\", this.onData);\n this.chatCompletionTransport.on(\"error\", this.onError);\n this.chatCompletionTransport.on(\"end\", this.onEnd);\n\n await this.chatCompletionTransport.fetch(params);\n }\n\n private onData = (data: ChatCompletionChunk) => {\n // In case we are in a function call but the next message is not a function call, flush it.\n if (this.mode === \"function\" && !data.choices[0].delta.function_call) {\n const success = this.tryFlushFunctionCall();\n if (!success) {\n return;\n }\n }\n\n this.mode = data.choices[0].delta.function_call ? \"function\" : \"message\";\n\n if (this.mode === \"message\") {\n // if we get a message, emit the content and return;\n\n if (data.choices[0].delta.content) {\n this.emit(\"content\", data.choices[0].delta.content);\n }\n\n return;\n } else if (this.mode === \"function\") {\n // if we get a function call, we buffer the name and arguments, then emit a partial event.\n\n if (data.choices[0].delta.function_call!.name) {\n this.functionCallName = data.choices[0].delta.function_call!.name!;\n }\n if (data.choices[0].delta.function_call!.arguments) {\n this.functionCallArguments += data.choices[0].delta.function_call!.arguments!;\n }\n this.emit(\"partial\", this.functionCallName, this.functionCallArguments);\n\n return;\n }\n };\n\n private onError = (error: any) => {\n this.emit(\"error\", error);\n this.cleanup();\n };\n\n private onEnd = () => {\n if (this.mode === \"function\") {\n const success = this.tryFlushFunctionCall();\n if (!success) {\n return;\n }\n }\n this.emit(\"end\");\n this.cleanup();\n };\n\n private tryFlushFunctionCall(): boolean {\n let args: any = null;\n try {\n args = JSON.parse(this.functionCallArguments);\n } catch (error) {\n this.emit(\"error\", error);\n this.cleanup();\n return false;\n }\n this.emit(\"function\", {\n name: this.functionCallName,\n arguments: args,\n });\n this.mode = null;\n this.functionCallName = \"\";\n this.functionCallArguments = \"\";\n return true;\n }\n\n private cleanup() {\n if (this.chatCompletionTransport) {\n this.chatCompletionTransport.off(\"data\", this.onData);\n this.chatCompletionTransport.off(\"error\", this.onError);\n this.chatCompletionTransport.off(\"end\", this.onEnd);\n }\n this.chatCompletionTransport = null;\n this.mode = null;\n this.functionCallName = \"\";\n this.functionCallArguments = \"\";\n }\n\n public countTokens(message: Message): number {\n if (message.content) {\n return estimateTokens(message.content);\n } else if (message.function_call) {\n return estimateTokens(JSON.stringify(message.function_call));\n }\n return 0;\n }\n}\n\nconst maxTokensByModel: { [key: string]: number } = {\n \"gpt-3.5-turbo\": 4097,\n \"gpt-3.5-turbo-16k\": 16385,\n \"gpt-4\": 8192,\n \"gpt-4-1106-preview\": 8192,\n \"gpt-4-32k\": 32768,\n \"gpt-3.5-turbo-0301\": 4097,\n \"gpt-4-0314\": 8192,\n \"gpt-4-32k-0314\": 32768,\n \"gpt-3.5-turbo-0613\": 4097,\n \"gpt-4-0613\": 8192,\n \"gpt-4-32k-0613\": 32768,\n \"gpt-3.5-turbo-16k-0613\": 16385,\n};\n\nfunction estimateTokens(text: string): number {\n return text.length / 3;\n}\n\nfunction countFunctionsTokens(functions: Function[]): number {\n if (functions.length === 0) {\n return 0;\n }\n const json = JSON.stringify(functions);\n return estimateTokens(json);\n}\n"]}
|