forked from CopilotC-Nvim/CopilotChat.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutilities.lua
More file actions
97 lines (92 loc) · 4.7 KB
/
Copy pathutilities.lua
File metadata and controls
97 lines (92 loc) · 4.7 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
PROMPTS = require("prompts")
-- A function to generate a random hexadecimal string of a given length
-- Default length is 65
function random_hex(length)
length = length or 65 -- Use 65 if length is not provided
local hex = "" -- Initialize an empty string
local choices = "0123456789abcdef" -- The possible characters
for _ = 1, length do -- Loop for the given length
local index = math.random(#choices) -- Pick a random index
hex = hex .. choices:sub(index, index) -- Append the character at that index
end
return hex -- Return the hexadecimal string
end
-- A function to generate a request for the copilot-chat model
-- Takes a chat history, a code excerpt, and an optional language
function generate_request(chat_history, code_excerpt, language)
language = language or "" -- Use an empty string if language is not provided
local messages = { -- Initialize a table of messages
{
content = PROMPTS.COPILOT_INSTRUCTIONS, -- The instructions for the user
role = "system" -- The role of the system
}
}
for _, message in ipairs(chat_history) do -- Loop through the chat history
table.insert(messages, { -- Append each message to the table
content = message.content, -- The content of the message
role = message.role -- The role of the sender
})
end
if code_excerpt ~= "" then -- If there is a code excerpt
table.insert(messages, #messages, { -- Insert it before the last message
content = "\nActive selection:\n```" .. language .. "\n" .. code_excerpt .. "\n```", -- The formatted code excerpt
role = "system" -- The role of the system
})
end
return { -- Return a table with the request parameters
intent = true,
model = "copilot-chat",
n = 1,
stream = true,
temperature = 0.1,
top_p = 1,
messages = messages
}
end
-- A function to cache a token for a user
-- Writes to ~/.config/github-copilot/hosts.json
function cache_token(user, token)
local home = os.getenv("HOME") -- Get the home directory
local config_dir = home .. "/.config/github-copilot" -- The config directory
local lfs = require("lfs") -- Require the Lua file system module
if not lfs.attributes(config_dir) then -- If the config directory does not exist
lfs.mkdir(config_dir) -- Create it
end
local hosts_file = config_dir .. "/hosts.json" -- The hosts file
local json = require("json") -- Require the json module
local hosts = { -- Create a table with the host information
["github.com"] = {
user = user,
oauth_token = token
}
}
local f = io.open(hosts_file, "w") -- Open the file for writing
if not f then -- If the file could not be opened
return -- Return
end
f:write(json.encode(hosts)) -- Write the json-encoded table
f:close() -- Close the file
end
-- A function to get the cached token
-- Reads from ~/.config/github-copilot/hosts.json
function get_cached_token()
local home = os.getenv("HOME") -- Get the home directory
local config_dir = home .. "/.config/github-copilot" -- The config directory
local hosts_file = config_dir .. "/hosts.json" -- The hosts file
local lfs = require("lfs") -- Require the Lua file system module
if not lfs.attributes(hosts_file) then -- If the hosts file does not exist
return nil -- Return nil
end
local f = io.open(hosts_file, "r") -- Open the file for reading
if not f then -- If the file could not be opened
return nil -- Return nil
end
local json = require("json") -- Require the json module
local hosts = json.decode(f:read("*a")) -- Decode the json string
f:close() -- Close the file
if hosts["github.com"] then -- If there is a host for github.com
return hosts["github.com"].oauth_token -- Return the token
else
return nil -- Return nil
end
end