-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathgatherContext.ts
113 lines (105 loc) · 3.28 KB
/
gatherContext.ts
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { createAsyncThunk } from "@reduxjs/toolkit";
import { JSONContent } from "@tiptap/core";
import {
ContextItemWithId,
InputModifiers,
MessageContent,
RangeInFile,
} from "core";
import * as URI from "uri-js";
import resolveEditorContent from "../../components/mainInput/resolveInput";
import { selectDefaultModel } from "../slices/configSlice";
import { ThunkApiType } from "../store";
export const gatherContext = createAsyncThunk<
{
selectedContextItems: ContextItemWithId[];
selectedCode: RangeInFile[];
content: MessageContent;
},
{
editorState: JSONContent;
modifiers: InputModifiers;
promptPreamble?: string;
},
ThunkApiType
>(
"chat/gatherContext",
async (
{ modifiers, editorState, promptPreamble },
{ dispatch, extra, getState },
) => {
const state = getState();
const defaultModel = selectDefaultModel(state);
const defaultContextProviders =
state.config.config.experimental?.defaultContext ?? [];
if (!defaultModel) {
console.error(
"gatherContext thunk: Cannot gather context, no model selected",
);
throw new Error("No chat model selected");
}
// Resolve context providers and construct new history
let [selectedContextItems, selectedCode, content] =
await resolveEditorContent({
editorState,
modifiers,
ideMessenger: extra.ideMessenger,
defaultContextProviders,
dispatch,
selectedModelTitle: defaultModel.title,
});
// Automatically use currently open file
if (!modifiers.noContext) {
const usingFreeTrial = defaultModel.provider === "free-trial";
const currentFileResponse = await extra.ideMessenger.request(
"context/getContextItems",
{
name: "currentFile",
query: "non-mention-usage",
fullInput: "",
selectedCode: [],
selectedModelTitle: defaultModel.title,
},
);
if (currentFileResponse.status === "success") {
const items = currentFileResponse.content;
if (items.length > 0) {
const currentFile = items[0];
const uri = currentFile.uri?.value;
// don't add the file if it's already in the context items
if (
uri &&
!selectedContextItems.find(
(item) => item.uri?.value && URI.equal(item.uri.value, uri),
)
) {
// Limit to 1000 lines if using free trial
if (usingFreeTrial) {
currentFile.content = currentFile.content
.split("\n")
.slice(0, 1000)
.join("\n");
if (!currentFile.content.endsWith("```")) {
currentFile.content += "\n```";
}
}
currentFile.id = {
providerTitle: "file",
itemId: uri,
};
selectedContextItems.unshift(currentFile);
}
}
}
}
if (promptPreamble) {
if (typeof content === "string") {
content = promptPreamble + content;
} else if (content[0].type === "text") {
content[0].text = promptPreamble + content[0].text;
}
}
// dispatch(addContextItems(contextItems));
return { selectedContextItems, selectedCode, content };
},
);