Skip to content

Commit ed96fe3

Browse files
authored
fix(adapters): reduce copilot premium request consumption (#1738)
1 parent f160cba commit ed96fe3

1 file changed

Lines changed: 10 additions & 2 deletions

File tree

‎lua/codecompanion/adapters/copilot.lua‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ local openai = require("codecompanion.adapters.openai")
55
local utils = require("codecompanion.utils.adapters")
66

77
-- Reference: https://github.com/yetone/avante.nvim/blob/22418bff8bcac4377ebf975cd48f716823867979/lua/avante/providers/copilot.lua#L5-L26
8-
---
98
---@class CopilotToken
109
---@field annotations_enabled boolean
1110
---@field chat_enabled boolean
@@ -361,6 +360,7 @@ return {
361360
formatted_name = "Copilot",
362361
roles = {
363362
llm = "assistant",
363+
tool = "tool",
364364
user = "user",
365365
},
366366
opts = {
@@ -426,13 +426,21 @@ return {
426426
return openai.handlers.form_parameters(self, params, messages)
427427
end,
428428
form_messages = function(self, messages)
429-
-- Ensure we send over the correct headers for image requests
430429
for _, m in ipairs(messages) do
431430
if m.opts and m.opts.tag == "image" and m.opts.mimetype then
431+
self.headers["X-Initiator"] = "user"
432432
self.headers["Copilot-Vision-Request"] = "true"
433433
break
434434
end
435435
end
436+
437+
local last_msg = messages[#messages]
438+
if last_msg and last_msg.role == self.roles.tool then
439+
-- NOTE: The inclusion of this header reduces premium token usage when
440+
-- sending tool output back to the LLM (#1717)
441+
self.headers["X-Initiator"] = "agent"
442+
end
443+
436444
return openai.handlers.form_messages(self, messages)
437445
end,
438446
form_tools = function(self, tools)

0 commit comments

Comments
 (0)