@copilotkit/react-core 0.3.0 → 0.4.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 +69 -11
- package/CHANGELOG.md +6 -0
- package/LICENSE +21 -0
- package/dist/chunk-7IPFT6UY.mjs +115 -0
- package/dist/chunk-7IPFT6UY.mjs.map +1 -0
- package/dist/chunk-7TCGEDGA.mjs +19 -0
- package/dist/chunk-7TCGEDGA.mjs.map +1 -0
- package/dist/chunk-BVQRDAR7.mjs +3 -0
- package/dist/chunk-BVQRDAR7.mjs.map +1 -0
- package/dist/chunk-EFZPSZWO.mjs +3 -0
- package/dist/chunk-EFZPSZWO.mjs.map +1 -0
- package/dist/chunk-GJUAS6I7.mjs +80 -0
- package/dist/chunk-GJUAS6I7.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-OMI7JPW5.mjs +30 -0
- package/dist/chunk-OMI7JPW5.mjs.map +1 -0
- package/dist/chunk-UABEP4UT.mjs +22 -0
- package/dist/chunk-UABEP4UT.mjs.map +1 -0
- package/dist/chunk-VNRDQJXW.mjs +3 -0
- package/dist/chunk-VNRDQJXW.mjs.map +1 -0
- package/dist/chunk-YVXQVXMS.mjs +111 -0
- package/dist/chunk-YVXQVXMS.mjs.map +1 -0
- package/dist/components/copilot-provider.d.ts +7 -0
- package/dist/components/copilot-provider.mjs +6 -0
- package/dist/components/copilot-provider.mjs.map +1 -0
- package/dist/components/index.d.ts +2 -0
- package/dist/components/index.mjs +7 -0
- package/dist/components/index.mjs.map +1 -0
- package/dist/context/copilot-context.d.ts +19 -0
- package/dist/context/copilot-context.mjs +4 -0
- package/dist/context/copilot-context.mjs.map +1 -0
- package/dist/context/index.d.ts +6 -0
- package/dist/context/index.mjs +5 -0
- package/dist/context/index.mjs.map +1 -0
- package/dist/hooks/index.d.ts +5 -0
- package/dist/hooks/index.mjs +8 -0
- package/dist/hooks/index.mjs.map +1 -0
- package/dist/hooks/use-copilot-chat.d.ts +18 -0
- package/dist/hooks/use-copilot-chat.mjs +5 -0
- package/dist/hooks/use-copilot-chat.mjs.map +1 -0
- package/dist/hooks/use-make-copilot-actionable.d.ts +5 -0
- package/dist/hooks/use-make-copilot-actionable.mjs +5 -0
- package/dist/hooks/use-make-copilot-actionable.mjs.map +1 -0
- package/dist/hooks/use-make-copilot-readable.d.ts +3 -0
- package/dist/hooks/use-make-copilot-readable.mjs +5 -0
- package/dist/hooks/use-make-copilot-readable.mjs.map +1 -0
- package/dist/hooks/use-tree.d.ts +17 -0
- package/dist/hooks/use-tree.mjs +4 -0
- package/dist/hooks/use-tree.mjs.map +1 -0
- package/dist/index.d.ts +10 -55
- package/dist/index.mjs +13 -26
- package/dist/index.mjs.map +1 -0
- package/dist/types/annotated-function.d.ts +24 -0
- package/dist/types/annotated-function.mjs +3 -0
- package/dist/types/annotated-function.mjs.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.mjs +3 -0
- package/dist/types/index.mjs.map +1 -0
- package/package.json +8 -8
- package/src/components/copilot-provider.tsx +60 -55
- package/src/components/index.ts +1 -1
- package/src/context/copilot-context.tsx +20 -20
- package/src/context/index.ts +2 -2
- package/src/hooks/index.ts +5 -5
- package/src/hooks/use-copilot-chat.ts +43 -39
- package/src/hooks/use-make-copilot-actionable.ts +13 -13
- package/src/hooks/use-make-copilot-readable.ts +11 -11
- package/src/hooks/use-tree.ts +87 -76
- package/src/index.tsx +4 -4
- package/src/types/annotated-function.ts +23 -10
- package/src/types/index.ts +2 -2
- package/tsup.config.ts +3 -5
- package/dist/index.js +0 -32
|
@@ -1,33 +1,33 @@
|
|
|
1
|
-
|
|
1
|
+
"use client";
|
|
2
2
|
|
|
3
|
-
import React from
|
|
4
|
-
import { AnnotatedFunction } from
|
|
5
|
-
import { TreeNodeId } from
|
|
6
|
-
import { ChatCompletionFunctions } from
|
|
7
|
-
import { FunctionCallHandler } from
|
|
3
|
+
import React from "react";
|
|
4
|
+
import { AnnotatedFunction } from "../types/annotated-function";
|
|
5
|
+
import { TreeNodeId } from "../hooks/use-tree";
|
|
6
|
+
import { ChatCompletionFunctions } from "openai-edge/types/api";
|
|
7
|
+
import { FunctionCallHandler } from "ai";
|
|
8
8
|
|
|
9
9
|
export interface CopilotContextParams {
|
|
10
|
-
entryPoints: Record<string, AnnotatedFunction<any[]
|
|
11
|
-
|
|
12
|
-
getFunctionCallHandler: () => FunctionCallHandler
|
|
13
|
-
setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void
|
|
14
|
-
removeEntryPoint: (id: string) => void
|
|
10
|
+
entryPoints: Record<string, AnnotatedFunction<any[]>>;
|
|
11
|
+
getChatCompletionFunctionDescriptions: () => ChatCompletionFunctions[];
|
|
12
|
+
getFunctionCallHandler: () => FunctionCallHandler;
|
|
13
|
+
setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;
|
|
14
|
+
removeEntryPoint: (id: string) => void;
|
|
15
15
|
|
|
16
|
-
getContextString: () => string
|
|
17
|
-
addContext: (context: string, parentId?: string) => TreeNodeId
|
|
18
|
-
removeContext: (id: TreeNodeId) => void
|
|
16
|
+
getContextString: () => string;
|
|
17
|
+
addContext: (context: string, parentId?: string) => TreeNodeId;
|
|
18
|
+
removeContext: (id: TreeNodeId) => void;
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
const emptyCopilotContext: CopilotContextParams = {
|
|
22
22
|
entryPoints: {},
|
|
23
|
-
|
|
23
|
+
getChatCompletionFunctionDescriptions: () => [],
|
|
24
24
|
getFunctionCallHandler: () => async () => {},
|
|
25
25
|
setEntryPoint: () => {},
|
|
26
26
|
removeEntryPoint: () => {},
|
|
27
|
-
getContextString: () =>
|
|
28
|
-
addContext: () =>
|
|
29
|
-
removeContext: () => {}
|
|
30
|
-
}
|
|
27
|
+
getContextString: () => "",
|
|
28
|
+
addContext: () => "",
|
|
29
|
+
removeContext: () => {},
|
|
30
|
+
};
|
|
31
31
|
|
|
32
32
|
export const CopilotContext =
|
|
33
|
-
React.createContext<CopilotContextParams>(emptyCopilotContext)
|
|
33
|
+
React.createContext<CopilotContextParams>(emptyCopilotContext);
|
package/src/context/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { CopilotContext } from
|
|
2
|
-
export type { CopilotContextParams } from
|
|
1
|
+
export { CopilotContext } from "./copilot-context";
|
|
2
|
+
export type { CopilotContextParams } from "./copilot-context";
|
package/src/hooks/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export { useCopilotChat } from
|
|
2
|
-
export type { UseCopilotChatOptions } from
|
|
3
|
-
export type { UseCopilotChatReturn } from
|
|
1
|
+
export { useCopilotChat } from "./use-copilot-chat";
|
|
2
|
+
export type { UseCopilotChatOptions } from "./use-copilot-chat";
|
|
3
|
+
export type { UseCopilotChatReturn } from "./use-copilot-chat";
|
|
4
4
|
|
|
5
|
-
export { useMakeCopilotActionable } from
|
|
6
|
-
export { useMakeCopilotReadable } from
|
|
5
|
+
export { useMakeCopilotActionable } from "./use-make-copilot-actionable";
|
|
6
|
+
export { useMakeCopilotReadable } from "./use-make-copilot-readable";
|
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
import { useMemo, useContext } from
|
|
1
|
+
import { useMemo, useContext } from "react";
|
|
2
2
|
import {
|
|
3
3
|
CopilotContext,
|
|
4
|
-
CopilotContextParams
|
|
5
|
-
} from
|
|
6
|
-
import { useChat } from
|
|
7
|
-
import { ChatRequestOptions, CreateMessage, Message } from
|
|
8
|
-
import { UseChatOptions } from
|
|
4
|
+
CopilotContextParams,
|
|
5
|
+
} from "../context/copilot-context";
|
|
6
|
+
import { useChat } from "ai/react";
|
|
7
|
+
import { ChatRequestOptions, CreateMessage, Message } from "ai";
|
|
8
|
+
import { UseChatOptions } from "ai";
|
|
9
9
|
|
|
10
10
|
export interface UseCopilotChatOptions extends UseChatOptions {
|
|
11
|
-
makeSystemMessage?: (contextString: string) => string
|
|
11
|
+
makeSystemMessage?: (contextString: string) => string;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
14
|
export interface UseCopilotChatReturn {
|
|
15
|
-
visibleMessages: Message[]
|
|
15
|
+
visibleMessages: Message[];
|
|
16
16
|
append: (
|
|
17
17
|
message: Message | CreateMessage,
|
|
18
18
|
chatRequestOptions?: ChatRequestOptions
|
|
19
|
-
) => Promise<string | null | undefined
|
|
19
|
+
) => Promise<string | null | undefined>;
|
|
20
20
|
reload: (
|
|
21
21
|
chatRequestOptions?: ChatRequestOptions
|
|
22
|
-
) => Promise<string | null | undefined
|
|
23
|
-
stop: () => void
|
|
24
|
-
isLoading: boolean
|
|
25
|
-
input: string
|
|
26
|
-
setInput: React.Dispatch<React.SetStateAction<string
|
|
22
|
+
) => Promise<string | null | undefined>;
|
|
23
|
+
stop: () => void;
|
|
24
|
+
isLoading: boolean;
|
|
25
|
+
input: string;
|
|
26
|
+
setInput: React.Dispatch<React.SetStateAction<string>>;
|
|
27
27
|
}
|
|
28
28
|
|
|
29
29
|
export function useCopilotChat({
|
|
@@ -32,28 +32,28 @@ export function useCopilotChat({
|
|
|
32
32
|
}: UseCopilotChatOptions): UseCopilotChatReturn {
|
|
33
33
|
const {
|
|
34
34
|
getContextString,
|
|
35
|
-
|
|
36
|
-
getFunctionCallHandler
|
|
37
|
-
} = useContext(CopilotContext)
|
|
35
|
+
getChatCompletionFunctionDescriptions,
|
|
36
|
+
getFunctionCallHandler,
|
|
37
|
+
} = useContext(CopilotContext);
|
|
38
38
|
|
|
39
39
|
const systemMessage: Message = useMemo(() => {
|
|
40
|
-
const systemMessageMaker = makeSystemMessage || defaultSystemMessage
|
|
41
|
-
const contextString = getContextString()
|
|
40
|
+
const systemMessageMaker = makeSystemMessage || defaultSystemMessage;
|
|
41
|
+
const contextString = getContextString();
|
|
42
42
|
|
|
43
43
|
return {
|
|
44
|
-
id:
|
|
44
|
+
id: "system",
|
|
45
45
|
content: systemMessageMaker(contextString),
|
|
46
|
-
role:
|
|
47
|
-
}
|
|
48
|
-
}, [getContextString, makeSystemMessage])
|
|
46
|
+
role: "system",
|
|
47
|
+
};
|
|
48
|
+
}, [getContextString, makeSystemMessage]);
|
|
49
49
|
|
|
50
50
|
const initialMessagesWithContext = [systemMessage].concat(
|
|
51
51
|
options.initialMessages || []
|
|
52
|
-
)
|
|
52
|
+
);
|
|
53
53
|
|
|
54
|
-
const
|
|
55
|
-
return
|
|
56
|
-
}, [
|
|
54
|
+
const functionDescriptions = useMemo(() => {
|
|
55
|
+
return getChatCompletionFunctionDescriptions();
|
|
56
|
+
}, [getChatCompletionFunctionDescriptions]);
|
|
57
57
|
|
|
58
58
|
const { messages, append, reload, stop, isLoading, input, setInput } =
|
|
59
59
|
useChat({
|
|
@@ -63,13 +63,13 @@ export function useCopilotChat({
|
|
|
63
63
|
body: {
|
|
64
64
|
id: options.id,
|
|
65
65
|
previewToken,
|
|
66
|
-
|
|
67
|
-
}
|
|
68
|
-
})
|
|
66
|
+
copilotkit_manually_passed_function_descriptions: functionDescriptions,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
69
|
|
|
70
70
|
const visibleMessages = messages.filter(
|
|
71
|
-
message => message.role ===
|
|
72
|
-
)
|
|
71
|
+
(message) => message.role === "user" || message.role === "assistant"
|
|
72
|
+
);
|
|
73
73
|
|
|
74
74
|
return {
|
|
75
75
|
visibleMessages,
|
|
@@ -78,16 +78,17 @@ export function useCopilotChat({
|
|
|
78
78
|
stop,
|
|
79
79
|
isLoading,
|
|
80
80
|
input,
|
|
81
|
-
setInput
|
|
82
|
-
}
|
|
81
|
+
setInput,
|
|
82
|
+
};
|
|
83
83
|
}
|
|
84
84
|
|
|
85
|
-
const previewToken =
|
|
85
|
+
const previewToken = "TODO123";
|
|
86
86
|
|
|
87
87
|
export function defaultSystemMessage(contextString: string): string {
|
|
88
88
|
return `
|
|
89
|
-
Please act as
|
|
90
|
-
|
|
89
|
+
Please act as an efficient, competent, conscientious, and industrious professional assistant.
|
|
90
|
+
|
|
91
|
+
Help 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.
|
|
91
92
|
Always be polite and respectful, and prefer brevity over verbosity.
|
|
92
93
|
|
|
93
94
|
The user has provided you with the following context:
|
|
@@ -98,6 +99,9 @@ ${contextString}
|
|
|
98
99
|
They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
|
|
99
100
|
|
|
100
101
|
Please assist them as best you can.
|
|
101
|
-
|
|
102
|
-
|
|
102
|
+
|
|
103
|
+
You 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.
|
|
104
|
+
|
|
105
|
+
If you would like to call a function, call it without saying anything else.
|
|
106
|
+
`;
|
|
103
107
|
}
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
|
|
1
|
+
"use client";
|
|
2
2
|
|
|
3
|
-
import { useRef, useContext, useEffect, useMemo } from
|
|
4
|
-
import { CopilotContext } from
|
|
5
|
-
import { AnnotatedFunction } from
|
|
6
|
-
import { nanoid } from
|
|
3
|
+
import { useRef, useContext, useEffect, useMemo } from "react";
|
|
4
|
+
import { CopilotContext } from "../context/copilot-context";
|
|
5
|
+
import { AnnotatedFunction } from "../types/annotated-function";
|
|
6
|
+
import { nanoid } from "nanoid";
|
|
7
7
|
|
|
8
8
|
export function useMakeCopilotActionable<ActionInput extends any[]>(
|
|
9
9
|
annotatedFunction: AnnotatedFunction<ActionInput>,
|
|
10
10
|
dependencies: any[]
|
|
11
11
|
) {
|
|
12
|
-
const idRef = useRef(nanoid()) // generate a unique id
|
|
13
|
-
const { setEntryPoint, removeEntryPoint } = useContext(CopilotContext)
|
|
12
|
+
const idRef = useRef(nanoid()); // generate a unique id
|
|
13
|
+
const { setEntryPoint, removeEntryPoint } = useContext(CopilotContext);
|
|
14
14
|
|
|
15
15
|
const memoizedAnnotatedFunction: AnnotatedFunction<ActionInput> = useMemo(
|
|
16
16
|
() => ({
|
|
17
17
|
name: annotatedFunction.name,
|
|
18
18
|
description: annotatedFunction.description,
|
|
19
19
|
argumentAnnotations: annotatedFunction.argumentAnnotations,
|
|
20
|
-
implementation: annotatedFunction.implementation
|
|
20
|
+
implementation: annotatedFunction.implementation,
|
|
21
21
|
}),
|
|
22
22
|
dependencies
|
|
23
|
-
)
|
|
23
|
+
);
|
|
24
24
|
|
|
25
25
|
useEffect(() => {
|
|
26
26
|
setEntryPoint(
|
|
27
27
|
idRef.current,
|
|
28
28
|
memoizedAnnotatedFunction as AnnotatedFunction<any[]>
|
|
29
|
-
)
|
|
29
|
+
);
|
|
30
30
|
|
|
31
31
|
return () => {
|
|
32
|
-
removeEntryPoint(idRef.current)
|
|
33
|
-
}
|
|
34
|
-
}, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint])
|
|
32
|
+
removeEntryPoint(idRef.current);
|
|
33
|
+
};
|
|
34
|
+
}, [memoizedAnnotatedFunction, setEntryPoint, removeEntryPoint]);
|
|
35
35
|
}
|
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
|
|
1
|
+
"use client";
|
|
2
2
|
|
|
3
|
-
import { useRef, useContext, useEffect } from
|
|
4
|
-
import { CopilotContext } from
|
|
3
|
+
import { useRef, useContext, useEffect } from "react";
|
|
4
|
+
import { CopilotContext } from "../context/copilot-context";
|
|
5
5
|
|
|
6
6
|
export function useMakeCopilotReadable(
|
|
7
7
|
information: string,
|
|
8
8
|
parentId?: string
|
|
9
9
|
): string | undefined {
|
|
10
|
-
const { addContext, removeContext } = useContext(CopilotContext)
|
|
11
|
-
const idRef = useRef<string>()
|
|
10
|
+
const { addContext, removeContext } = useContext(CopilotContext);
|
|
11
|
+
const idRef = useRef<string>();
|
|
12
12
|
|
|
13
13
|
useEffect(() => {
|
|
14
|
-
const id = addContext(information, parentId)
|
|
15
|
-
idRef.current = id
|
|
14
|
+
const id = addContext(information, parentId);
|
|
15
|
+
idRef.current = id;
|
|
16
16
|
|
|
17
17
|
return () => {
|
|
18
|
-
removeContext(id)
|
|
19
|
-
}
|
|
20
|
-
}, [information, parentId, addContext, removeContext])
|
|
18
|
+
removeContext(id);
|
|
19
|
+
};
|
|
20
|
+
}, [information, parentId, addContext, removeContext]);
|
|
21
21
|
|
|
22
|
-
return idRef.current
|
|
22
|
+
return idRef.current;
|
|
23
23
|
}
|
package/src/hooks/use-tree.ts
CHANGED
|
@@ -1,79 +1,97 @@
|
|
|
1
|
-
import { nanoid } from
|
|
2
|
-
import { useReducer, useCallback } from
|
|
1
|
+
import { nanoid } from "nanoid";
|
|
2
|
+
import { useReducer, useCallback } from "react";
|
|
3
3
|
|
|
4
|
-
export type TreeNodeId = string
|
|
4
|
+
export type TreeNodeId = string;
|
|
5
5
|
|
|
6
6
|
export interface TreeNode {
|
|
7
|
-
id: TreeNodeId
|
|
8
|
-
value: string
|
|
9
|
-
children: TreeNode[]
|
|
10
|
-
parentId?: TreeNodeId
|
|
7
|
+
id: TreeNodeId;
|
|
8
|
+
value: string;
|
|
9
|
+
children: TreeNode[];
|
|
10
|
+
parentId?: TreeNodeId;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
export type Tree = TreeNode[]
|
|
13
|
+
export type Tree = TreeNode[];
|
|
14
14
|
|
|
15
15
|
export interface UseTreeReturn {
|
|
16
|
-
tree: Tree
|
|
17
|
-
addElement: (value: string, parentId?: TreeNodeId) => TreeNodeId
|
|
18
|
-
printTree: () => string
|
|
19
|
-
removeElement: (id: TreeNodeId) => void
|
|
16
|
+
tree: Tree;
|
|
17
|
+
addElement: (value: string, parentId?: TreeNodeId) => TreeNodeId;
|
|
18
|
+
printTree: () => string;
|
|
19
|
+
removeElement: (id: TreeNodeId) => void;
|
|
20
20
|
}
|
|
21
21
|
|
|
22
22
|
const findNode = (nodes: Tree, id: TreeNodeId): TreeNode | undefined => {
|
|
23
23
|
for (const node of nodes) {
|
|
24
24
|
if (node.id === id) {
|
|
25
|
-
return node
|
|
25
|
+
return node;
|
|
26
26
|
}
|
|
27
|
-
const result = findNode(node.children, id)
|
|
27
|
+
const result = findNode(node.children, id);
|
|
28
28
|
if (result) {
|
|
29
|
-
return result
|
|
29
|
+
return result;
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
|
-
return undefined
|
|
33
|
-
}
|
|
32
|
+
return undefined;
|
|
33
|
+
};
|
|
34
34
|
|
|
35
35
|
const removeNode = (nodes: Tree, id: TreeNodeId): Tree => {
|
|
36
36
|
return nodes.reduce((result: Tree, node) => {
|
|
37
37
|
if (node.id !== id) {
|
|
38
|
-
const newNode = { ...node, children: removeNode(node.children, id) }
|
|
39
|
-
result.push(newNode)
|
|
38
|
+
const newNode = { ...node, children: removeNode(node.children, id) };
|
|
39
|
+
result.push(newNode);
|
|
40
40
|
}
|
|
41
|
-
return result
|
|
42
|
-
}, [])
|
|
43
|
-
}
|
|
41
|
+
return result;
|
|
42
|
+
}, []);
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
const addNode = (
|
|
46
|
+
nodes: Tree,
|
|
47
|
+
newNode: TreeNode,
|
|
48
|
+
parentId?: TreeNodeId
|
|
49
|
+
): Tree => {
|
|
50
|
+
if (!parentId) {
|
|
51
|
+
return [...nodes, newNode];
|
|
52
|
+
}
|
|
53
|
+
return nodes.map((node) => {
|
|
54
|
+
if (node.id === parentId) {
|
|
55
|
+
return { ...node, children: [...node.children, newNode] };
|
|
56
|
+
} else if (node.children.length) {
|
|
57
|
+
return { ...node, children: addNode(node.children, newNode, parentId) };
|
|
58
|
+
}
|
|
59
|
+
return node;
|
|
60
|
+
});
|
|
61
|
+
};
|
|
44
62
|
|
|
45
63
|
const treeIndentationRepresentation = (
|
|
46
64
|
index: number,
|
|
47
65
|
indentLevel: number
|
|
48
66
|
): string => {
|
|
49
67
|
if (indentLevel === 0) {
|
|
50
|
-
return (index + 1).toString()
|
|
68
|
+
return (index + 1).toString();
|
|
51
69
|
} else if (indentLevel === 1) {
|
|
52
|
-
return String.fromCharCode(65 + index) // 65 is the ASCII value for 'A'
|
|
70
|
+
return String.fromCharCode(65 + index); // 65 is the ASCII value for 'A'
|
|
53
71
|
} else if (indentLevel === 2) {
|
|
54
|
-
return String.fromCharCode(97 + index) // 97 is the ASCII value for 'a'
|
|
72
|
+
return String.fromCharCode(97 + index); // 97 is the ASCII value for 'a'
|
|
55
73
|
} else {
|
|
56
|
-
throw new Error(
|
|
74
|
+
throw new Error("Indentation level not supported");
|
|
57
75
|
}
|
|
58
|
-
}
|
|
76
|
+
};
|
|
59
77
|
|
|
60
|
-
const printNode = (node: TreeNode, prefix =
|
|
61
|
-
const indent =
|
|
78
|
+
const printNode = (node: TreeNode, prefix = "", indentLevel = 0): string => {
|
|
79
|
+
const indent = " ".repeat(3).repeat(indentLevel);
|
|
62
80
|
|
|
63
|
-
const prefixPlusIndentLength = prefix.length + indent.length
|
|
64
|
-
const subsequentLinesPrefix =
|
|
81
|
+
const prefixPlusIndentLength = prefix.length + indent.length;
|
|
82
|
+
const subsequentLinesPrefix = " ".repeat(prefixPlusIndentLength);
|
|
65
83
|
|
|
66
|
-
const valueLines = node.value.split(
|
|
84
|
+
const valueLines = node.value.split("\n");
|
|
67
85
|
|
|
68
|
-
const outputFirstLine = `${indent}${prefix}${valueLines[0]}
|
|
86
|
+
const outputFirstLine = `${indent}${prefix}${valueLines[0]}`;
|
|
69
87
|
const outputSubsequentLines = valueLines
|
|
70
88
|
.slice(1)
|
|
71
|
-
.map(line => `${subsequentLinesPrefix}${line}`)
|
|
72
|
-
.join(
|
|
89
|
+
.map((line) => `${subsequentLinesPrefix}${line}`)
|
|
90
|
+
.join("\n");
|
|
73
91
|
|
|
74
|
-
let output = `${outputFirstLine}\n
|
|
92
|
+
let output = `${outputFirstLine}\n`;
|
|
75
93
|
if (outputSubsequentLines) {
|
|
76
|
-
output += `${outputSubsequentLines}\n
|
|
94
|
+
output += `${outputSubsequentLines}\n`;
|
|
77
95
|
}
|
|
78
96
|
|
|
79
97
|
node.children.forEach(
|
|
@@ -83,77 +101,70 @@ const printNode = (node: TreeNode, prefix = '', indentLevel = 0): string => {
|
|
|
83
101
|
`${prefix}${treeIndentationRepresentation(index, indentLevel + 1)}. `,
|
|
84
102
|
indentLevel + 1
|
|
85
103
|
))
|
|
86
|
-
)
|
|
87
|
-
return output
|
|
88
|
-
}
|
|
104
|
+
);
|
|
105
|
+
return output;
|
|
106
|
+
};
|
|
89
107
|
|
|
90
108
|
// Action types
|
|
91
109
|
type Action =
|
|
92
|
-
| { type:
|
|
93
|
-
| { type:
|
|
110
|
+
| { type: "ADD_NODE"; value: string; parentId?: string; id: string }
|
|
111
|
+
| { type: "REMOVE_NODE"; id: string };
|
|
94
112
|
|
|
95
113
|
// Reducer function
|
|
96
114
|
function treeReducer(state: Tree, action: Action): Tree {
|
|
97
115
|
switch (action.type) {
|
|
98
|
-
case
|
|
99
|
-
const { value, parentId, id: newNodeId } = action
|
|
116
|
+
case "ADD_NODE": {
|
|
117
|
+
const { value, parentId, id: newNodeId } = action;
|
|
100
118
|
const newNode: TreeNode = {
|
|
101
119
|
id: newNodeId,
|
|
102
120
|
value,
|
|
103
|
-
children: []
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
} else {
|
|
112
|
-
throw new Error(`Parent with id ${parentId} not found`)
|
|
113
|
-
}
|
|
114
|
-
} else {
|
|
115
|
-
return [...state, newNode]
|
|
121
|
+
children: [],
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
return addNode(state, newNode, parentId);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
console.error(`Error while adding node with id ${newNodeId}: ${error}`);
|
|
128
|
+
return state;
|
|
116
129
|
}
|
|
117
|
-
|
|
118
|
-
return state
|
|
119
130
|
}
|
|
120
|
-
case
|
|
121
|
-
return removeNode(state, action.id)
|
|
131
|
+
case "REMOVE_NODE":
|
|
132
|
+
return removeNode(state, action.id);
|
|
122
133
|
default:
|
|
123
|
-
return state
|
|
134
|
+
return state;
|
|
124
135
|
}
|
|
125
136
|
}
|
|
126
137
|
|
|
127
138
|
// useTree hook
|
|
128
139
|
const useTree = (): UseTreeReturn => {
|
|
129
|
-
const [tree, dispatch] = useReducer(treeReducer, [])
|
|
140
|
+
const [tree, dispatch] = useReducer(treeReducer, []);
|
|
130
141
|
|
|
131
142
|
const addElement = useCallback(
|
|
132
143
|
(value: string, parentId?: string): TreeNodeId => {
|
|
133
|
-
const newNodeId = nanoid() // Generate new ID outside of dispatch
|
|
134
|
-
dispatch({ type:
|
|
135
|
-
return newNodeId // Return the new ID
|
|
144
|
+
const newNodeId = nanoid(); // Generate new ID outside of dispatch
|
|
145
|
+
dispatch({ type: "ADD_NODE", value, parentId, id: newNodeId });
|
|
146
|
+
return newNodeId; // Return the new ID
|
|
136
147
|
},
|
|
137
148
|
[]
|
|
138
|
-
)
|
|
149
|
+
);
|
|
139
150
|
|
|
140
151
|
const removeElement = useCallback((id: TreeNodeId): void => {
|
|
141
|
-
dispatch({ type:
|
|
142
|
-
}, [])
|
|
152
|
+
dispatch({ type: "REMOVE_NODE", id });
|
|
153
|
+
}, []);
|
|
143
154
|
|
|
144
155
|
const printTree = (): string => {
|
|
145
|
-
let output =
|
|
156
|
+
let output = "";
|
|
146
157
|
tree.forEach(
|
|
147
158
|
(node, index) =>
|
|
148
159
|
(output += printNode(
|
|
149
160
|
node,
|
|
150
161
|
`${treeIndentationRepresentation(index, 0)}. `
|
|
151
162
|
))
|
|
152
|
-
)
|
|
153
|
-
return output
|
|
154
|
-
}
|
|
163
|
+
);
|
|
164
|
+
return output;
|
|
165
|
+
};
|
|
155
166
|
|
|
156
|
-
return { tree, addElement, printTree, removeElement }
|
|
157
|
-
}
|
|
167
|
+
return { tree, addElement, printTree, removeElement };
|
|
168
|
+
};
|
|
158
169
|
|
|
159
|
-
export default useTree
|
|
170
|
+
export default useTree;
|
package/src/index.tsx
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
1
|
+
export * from "./components";
|
|
2
|
+
export * from "./context";
|
|
3
|
+
export * from "./hooks";
|
|
4
|
+
export * from "./types";
|
|
@@ -1,14 +1,27 @@
|
|
|
1
|
-
export interface
|
|
2
|
-
name: string
|
|
3
|
-
type: string
|
|
4
|
-
description: string
|
|
5
|
-
|
|
6
|
-
required: boolean
|
|
1
|
+
export interface AnnotatedFunctionSimpleArgument {
|
|
2
|
+
name: string;
|
|
3
|
+
type: "string" | "number" | "boolean" | "object"; // Add or change types according to your needs.
|
|
4
|
+
description: string;
|
|
5
|
+
required: boolean;
|
|
7
6
|
}
|
|
8
7
|
|
|
8
|
+
export interface AnnotatedFunctionArrayArgument {
|
|
9
|
+
name: string;
|
|
10
|
+
type: "array";
|
|
11
|
+
items: {
|
|
12
|
+
type: string;
|
|
13
|
+
};
|
|
14
|
+
description: string;
|
|
15
|
+
required: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export type AnnotatedFunctionArgument =
|
|
19
|
+
| AnnotatedFunctionSimpleArgument
|
|
20
|
+
| AnnotatedFunctionArrayArgument;
|
|
21
|
+
|
|
9
22
|
export interface AnnotatedFunction<Inputs extends any[]> {
|
|
10
|
-
name: string
|
|
11
|
-
description: string
|
|
12
|
-
argumentAnnotations: AnnotatedFunctionArgument[]
|
|
13
|
-
implementation: (...args: Inputs) => Promise<void
|
|
23
|
+
name: string;
|
|
24
|
+
description: string;
|
|
25
|
+
argumentAnnotations: AnnotatedFunctionArgument[];
|
|
26
|
+
implementation: (...args: Inputs) => Promise<void>;
|
|
14
27
|
}
|
package/src/types/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export type { AnnotatedFunctionArgument } from
|
|
2
|
-
export type { AnnotatedFunction } from
|
|
1
|
+
export type { AnnotatedFunctionArgument } from "./annotated-function";
|
|
2
|
+
export type { AnnotatedFunction } from "./annotated-function";
|
package/tsup.config.ts
CHANGED
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import { defineConfig, Options } from "tsup";
|
|
2
2
|
|
|
3
3
|
export default defineConfig((options: Options) => ({
|
|
4
|
-
|
|
5
|
-
splitting: true,
|
|
6
|
-
entry: ["src/**/*.tsx"],
|
|
4
|
+
entry: ["src/**/*.{ts,tsx}"],
|
|
7
5
|
format: ["esm"],
|
|
8
6
|
dts: true,
|
|
9
|
-
minify:
|
|
7
|
+
minify: false,
|
|
10
8
|
clean: true,
|
|
11
9
|
external: ["react"],
|
|
10
|
+
sourcemap: true,
|
|
12
11
|
...options,
|
|
13
12
|
}));
|
|
14
|
-
|
package/dist/index.js
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var V = require('react');
|
|
4
|
-
var nanoid = require('nanoid');
|
|
5
|
-
var jsxRuntime = require('react/jsx-runtime');
|
|
6
|
-
var react = require('ai/react');
|
|
7
|
-
|
|
8
|
-
var k=Object.defineProperty,q=Object.defineProperties;var U=Object.getOwnPropertyDescriptors;var x=Object.getOwnPropertySymbols;var P=Object.prototype.hasOwnProperty,A=Object.prototype.propertyIsEnumerable;var N=(e,t,n)=>t in e?k(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n,h=(e,t)=>{for(var n in t||(t={}))P.call(t,n)&&N(e,n,t[n]);if(x)for(var n of x(t))A.call(t,n)&&N(e,n,t[n]);return e},T=(e,t)=>q(e,U(t));var E=(e,t)=>{var n={};for(var o in e)P.call(e,o)&&t.indexOf(o)<0&&(n[o]=e[o]);if(e!=null&&x)for(var o of x(e))t.indexOf(o)<0&&A.call(e,o)&&(n[o]=e[o]);return n};var v=(e,t,n)=>new Promise((o,r)=>{var i=u=>{try{a(n.next(u));}catch(c){r(c);}},s=u=>{try{a(n.throw(u));}catch(c){r(c);}},a=u=>u.done?o(u.value):Promise.resolve(u.value).then(i,s);a((n=n.apply(e,t)).next());});var I=(e,t)=>{for(let n of e){if(n.id===t)return n;let o=I(n.children,t);if(o)return o}},M=(e,t)=>e.reduce((n,o)=>{if(o.id!==t){let r=T(h({},o),{children:M(o.children,t)});n.push(r);}return n},[]),b=(e,t)=>{if(t===0)return (e+1).toString();if(t===1)return String.fromCharCode(65+e);if(t===2)return String.fromCharCode(97+e);throw new Error("Indentation level not supported")},O=(e,t="",n=0)=>{let o=" ".repeat(3).repeat(n),r=t.length+o.length,i=" ".repeat(r),s=e.value.split(`
|
|
9
|
-
`),a=`${o}${t}${s[0]}`,u=s.slice(1).map(l=>`${i}${l}`).join(`
|
|
10
|
-
`),c=`${a}
|
|
11
|
-
`;return u&&(c+=`${u}
|
|
12
|
-
`),e.children.forEach((l,g)=>c+=O(l,`${t}${b(g,n+1)}. `,n+1)),c};function j(e,t){switch(t.type){case"ADD_NODE":{let{value:n,parentId:o,id:r}=t,i={id:r,value:n,children:[]};if(o){let s=I(e,o);if(s)i.parentId=o,s.children.push(i);else throw new Error(`Parent with id ${o} not found`)}else return [...e,i];return e}case"REMOVE_NODE":return M(e,t.id);default:return e}}var L=()=>{let[e,t]=V.useReducer(j,[]),n=V.useCallback((i,s)=>{let a=nanoid.nanoid();return t({type:"ADD_NODE",value:i,parentId:s,id:a}),a},[]),o=V.useCallback(i=>{t({type:"REMOVE_NODE",id:i});},[]);return {tree:e,addElement:n,printTree:()=>{let i="";return e.forEach((s,a)=>i+=O(s,`${b(a,0)}. `)),i},removeElement:o}},w=L;var J={entryPoints:{},getChatCompletionFunctions:()=>[],getFunctionCallHandler:()=>()=>v(void 0,null,function*(){}),setEntryPoint:()=>{},removeEntryPoint:()=>{},getContextString:()=>"",addContext:()=>"",removeContext:()=>{}},d=V.createContext(J);function B({children:e}){let[t,n]=V.useState({}),{addElement:o,removeElement:r,printTree:i}=w(),s=V.useCallback((p,m)=>{n(C=>T(h({},C),{[p]:m}));},[]),a=V.useCallback(p=>{n(m=>{let C=h({},m);return delete C[p],C});},[]),u=V.useCallback(()=>i(),[i]),c=V.useCallback((p,m)=>o(p,m),[o]),l=V.useCallback(p=>{r(p);},[r]),g=V.useCallback(()=>X(Object.values(t)),[t]),F=V.useCallback(()=>W(Object.values(t)),[t]);return jsxRuntime.jsx(d.Provider,{value:{entryPoints:t,getChatCompletionFunctions:g,getFunctionCallHandler:F,setEntryPoint:s,removeEntryPoint:a,getContextString:u,addContext:c,removeContext:l},children:e})}function W(e){return (t,n)=>v(this,null,function*(){let o={};for(let i of e)o[i.name]=i;let r=o[n.name||""];if(r){let i=[];n.arguments&&(i=JSON.parse(n.arguments));let s=[];for(let a of r.argumentAnnotations)s.push(i[a.name]);yield r.implementation(...s);}})}function X(e){return e.map(Y)}function Y(e){let t={};for(let r of e.argumentAnnotations)t[r.name]={type:r.type,description:r.description};let n=[];for(let r of e.argumentAnnotations)r.required&&n.push(r.name);return {name:e.name,description:e.description,parameters:{type:"object",properties:t,required:n}}}function Z(n){var o=n,{makeSystemMessage:e}=o,t=E(o,["makeSystemMessage"]);let{getContextString:r,getChatCompletionFunctions:i,getFunctionCallHandler:s}=V.useContext(d),u=[V.useMemo(()=>{let y=e||te,$=r();return {id:"system",content:y($),role:"system"}},[r,e])].concat(t.initialMessages||[]),c=V.useMemo(()=>i(),[i]),{messages:l,append:g,reload:F,stop:p,isLoading:m,input:C,setInput:D}=react.useChat({id:t.id,initialMessages:u,experimental_onFunctionCall:s(),body:{id:t.id,previewToken:ee,functions:c}});return {visibleMessages:l.filter(y=>y.role==="user"||y.role==="assistant"),append:g,reload:F,stop:p,isLoading:m,input:C,setInput:D}}var ee="TODO123";function te(e){return `
|
|
13
|
-
Please act as a efficient, competent, and conscientious professional assistant.
|
|
14
|
-
You help 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.
|
|
15
|
-
Always be polite and respectful, and prefer brevity over verbosity.
|
|
16
|
-
|
|
17
|
-
The user has provided you with the following context:
|
|
18
|
-
\`\`\`
|
|
19
|
-
${e}
|
|
20
|
-
\`\`\`
|
|
21
|
-
|
|
22
|
-
They have also provided you with functions you can call to initiate actions on their behalf, or functions you can call to receive more information.
|
|
23
|
-
|
|
24
|
-
Please assist them as best you can.
|
|
25
|
-
If you are not sure how to proceed to best fulfill their requests, please ask them for more information.
|
|
26
|
-
`}function ae(e,t){let n=V.useRef(nanoid.nanoid()),{setEntryPoint:o,removeEntryPoint:r}=V.useContext(d),i=V.useMemo(()=>({name:e.name,description:e.description,argumentAnnotations:e.argumentAnnotations,implementation:e.implementation}),t);V.useEffect(()=>(o(n.current,i),()=>{r(n.current);}),[i,o,r]);}function de(e,t){let{addContext:n,removeContext:o}=V.useContext(d),r=V.useRef();return V.useEffect(()=>{let i=n(e,t);return r.current=i,()=>{o(i);}},[e,t,n,o]),r.current}
|
|
27
|
-
|
|
28
|
-
exports.CopilotContext = d;
|
|
29
|
-
exports.CopilotProvider = B;
|
|
30
|
-
exports.useCopilotChat = Z;
|
|
31
|
-
exports.useMakeCopilotActionable = ae;
|
|
32
|
-
exports.useMakeCopilotReadable = de;
|