forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot-context.tsx
More file actions
99 lines (84 loc) · 2.82 KB
/
Copy pathcopilot-context.tsx
File metadata and controls
99 lines (84 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
"use client";
import { FunctionCallHandler, AnnotatedFunction, Function } from "@copilotkit/shared";
import React from "react";
import { TreeNodeId } from "../hooks/use-tree";
import { DocumentPointer } from "../types";
/**
* Interface for the configuration of the Copilot API.
*/
export interface CopilotApiConfig {
/**
* The endpoint for the chat API.
*/
chatApiEndpoint: string;
/**
* additional headers to be sent with the request
* @default {}
* @example
* ```
* {
* 'Authorization': 'Bearer your_token_here'
* }
* ```
*/
headers: Record<string, string>;
/**
* Additional body params to be sent with the request
* @default {}
* @example
* ```
* {
* 'message': 'Hello, world!'
* }
* ```
*/
body: Record<string, any>;
}
export interface CopilotContextParams {
// function-calling
entryPoints: Record<string, AnnotatedFunction<any[]>>;
setEntryPoint: (id: string, entryPoint: AnnotatedFunction<any[]>) => void;
removeEntryPoint: (id: string) => void;
getChatCompletionFunctionDescriptions: () => Function[];
getFunctionCallHandler: () => FunctionCallHandler;
// text context
addContext: (context: string, parentId?: string, categories?: string[]) => TreeNodeId;
removeContext: (id: TreeNodeId) => void;
getContextString: (documents: DocumentPointer[], categories: string[]) => string;
// document context
addDocumentContext: (documentPointer: DocumentPointer, categories?: string[]) => TreeNodeId;
removeDocumentContext: (documentId: string) => void;
getDocumentsContext: (categories: string[]) => DocumentPointer[];
// api endpoints
copilotApiConfig: CopilotApiConfig;
}
const emptyCopilotContext: CopilotContextParams = {
entryPoints: {},
setEntryPoint: () => {},
removeEntryPoint: () => {},
getChatCompletionFunctionDescriptions: () => returnAndThrowInDebug([]),
getFunctionCallHandler: () => returnAndThrowInDebug(async () => {}),
getContextString: (documents: DocumentPointer[], categories: string[]) =>
returnAndThrowInDebug(""),
addContext: () => "",
removeContext: () => {},
getDocumentsContext: (categories: string[]) => returnAndThrowInDebug([]),
addDocumentContext: () => returnAndThrowInDebug(""),
removeDocumentContext: () => {},
copilotApiConfig: new (class implements CopilotApiConfig {
get chatApiEndpoint(): string {
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
}
get headers(): Record<string, string> {
return {};
}
get body(): Record<string, any> {
return {};
}
})(),
};
export const CopilotContext = React.createContext<CopilotContextParams>(emptyCopilotContext);
function returnAndThrowInDebug<T>(value: T): T {
throw new Error("Remember to wrap your app in a `<CopilotKit> {...} </CopilotKit>` !!!");
return value;
}