GeezerVison is showing github.com — add chat.headers hook, adjust codex and copilot plugins to use it · bpd1069/opencode@c89f6e7
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/plugin/codex.ts +9-2Lines changed: 9 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | import type { Hooks, PluginInput } from "@opencode-ai/plugin" | 2 | 2 | import { Log } from "../util/log" | 3 | | -import { OAUTH_DUMMY_KEY } from "../auth" | 4 | | -import { ProviderTransform } from "../provider/transform" | | 3 | +import { Installation } from "../installation" | | 4 | +import { Auth, OAUTH_DUMMY_KEY } from "../auth" | | 5 | +import os from "os" | 5 | 6 |
| 6 | 7 | const log = Log.create({ service: "plugin.codex" }) | 7 | 8 |
| @@ -489,5 +490,11 @@ export async function CodexAuthPlugin(input: PluginInput): Promise<Hooks> { | 489 | 490 | }, | 490 | 491 | ], | 491 | 492 | }, | | 493 | + "chat.headers": async (input, output) => { | | 494 | + if (input.model.providerID !== "openai") return | | 495 | + output.headers.originator = "opencode" | | 496 | + output.headers["User-Agent"] = `opencode/${Installation.VERSION} (${os.platform()} ${os.release()}; ${os.arch()})` | | 497 | + output.headers.session_id = input.sessionID | | 498 | + }, | 492 | 499 | } | 493 | 500 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/plugin/copilot.ts +16-2Lines changed: 16 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,7 +6,6 @@ const CLIENT_ID = "Ov23li8tweQw6odWQebz" | 6 | 6 | // Add a small safety buffer when polling to avoid hitting the server | 7 | 7 | // slightly too early due to clock skew / timer drift. | 8 | 8 | const OAUTH_POLLING_SAFETY_MARGIN_MS = 3000 // 3 seconds | 9 | | - | 10 | 9 | function normalizeDomain(url: string) { | 11 | 10 | return url.replace(/^https?:\/\//, "").replace(/\/$/, "") | 12 | 11 | } | @@ -19,6 +18,7 @@ function getUrls(domain: string) { | 19 | 18 | } | 20 | 19 |
| 21 | 20 | export async function CopilotAuthPlugin(input: PluginInput): Promise<Hooks> { | | 21 | + const sdk = input.client | 22 | 22 | return { | 23 | 23 | auth: { | 24 | 24 | provider: "github-copilot", | @@ -83,11 +83,11 @@ export async function CopilotAuthPlugin(input: PluginInput): Promise<Hooks> { | 83 | 83 | }) | 84 | 84 |
| 85 | 85 | const headers: Record<string, string> = { | | 86 | + "x-initiator": isAgent ? "agent" : "user", | 86 | 87 | ...(init?.headers as Record<string, string>), | 87 | 88 | "User-Agent": `opencode/${Installation.VERSION}`, | 88 | 89 | Authorization: `Bearer ${info.refresh}`, | 89 | 90 | "Openai-Intent": "conversation-edits", | 90 | | - "X-Initiator": isAgent ? "agent" : "user", | 91 | 91 | } | 92 | 92 |
| 93 | 93 | if (isVision) { | @@ -265,5 +265,19 @@ export async function CopilotAuthPlugin(input: PluginInput): Promise<Hooks> { | 265 | 265 | }, | 266 | 266 | ], | 267 | 267 | }, | | 268 | + "chat.headers": async (input, output) => { | | 269 | + if (!input.model.providerID.includes("github-copilot")) return | | 270 | + const session = await sdk.session | | 271 | + .get({ | | 272 | + path: { | | 273 | + id: input.sessionID, | | 274 | + }, | | 275 | + throwOnError: true, | | 276 | + }) | | 277 | + .catch(() => undefined) | | 278 | + if (!session || !session.data.parentID) return | | 279 | + // TODO: mark subagent sessions as agent initiated once copilot gives ok | | 280 | + // output.headers["x-initiator"] = "agent" | | 281 | + }, | 268 | 282 | } | 269 | 283 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/session/llm.ts +16-7Lines changed: 16 additions & 7 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -53,6 +53,7 @@ export namespace LLM { | 53 | 53 | .tag("sessionID", input.sessionID) | 54 | 54 | .tag("small", (input.small ?? false).toString()) | 55 | 55 | .tag("agent", input.agent.name) | | 56 | + .tag("mode", input.agent.mode) | 56 | 57 | l.info("stream", { | 57 | 58 | modelID: input.model.id, | 58 | 59 | providerID: input.model.providerID, | @@ -131,6 +132,20 @@ export namespace LLM { | 131 | 132 | }, | 132 | 133 | ) | 133 | 134 |
| | 135 | + const { headers } = await Plugin.trigger( | | 136 | + "chat.headers", | | 137 | + { | | 138 | + sessionID: input.sessionID, | | 139 | + agent: input.agent, | | 140 | + model: input.model, | | 141 | + provider, | | 142 | + message: input.user, | | 143 | + }, | | 144 | + { | | 145 | + headers: {}, | | 146 | + }, | | 147 | + ) | | 148 | + | 134 | 149 | const maxOutputTokens = isCodex | 135 | 150 | ? undefined | 136 | 151 | : ProviderTransform.maxOutputTokens( | @@ -198,13 +213,6 @@ export namespace LLM { | 198 | 213 | maxOutputTokens, | 199 | 214 | abortSignal: input.abort, | 200 | 215 | headers: { | 201 | | - ...(isCodex | 202 | | - ? { | 203 | | - originator: "opencode", | 204 | | - "User-Agent": `opencode/${Installation.VERSION} (${os.platform()} ${os.release()}; ${os.arch()})`, | 205 | | - session_id: input.sessionID, | 206 | | - } | 207 | | - : undefined), | 208 | 216 | ...(input.model.providerID.startsWith("opencode") | 209 | 217 | ? { | 210 | 218 | "x-opencode-project": Instance.project.id, | @@ -218,6 +226,7 @@ export namespace LLM { | 218 | 226 | } | 219 | 227 | : undefined), | 220 | 228 | ...input.model.headers, | | 229 | + ...headers, | 221 | 230 | }, | 222 | 231 | maxRetries: input.retries ?? 0, | 223 | 232 | messages: [ | |
Collapse file Copy file name to clipboardExpand all lines: packages/plugin/src/index.ts +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -172,6 +172,10 @@ export interface Hooks { | 172 | 172 | input: { sessionID: string; agent: string; model: Model; provider: ProviderContext; message: UserMessage }, | 173 | 173 | output: { temperature: number; topP: number; topK: number; options: Record<string, any> }, | 174 | 174 | ) => Promise<void> | | 175 | + "chat.headers"?: ( | | 176 | + input: { sessionID: string; agent: string; model: Model; provider: ProviderContext; message: UserMessage }, | | 177 | + output: { headers: Record<string, string> }, | | 178 | + ) => Promise<void> | 175 | 179 | "permission.ask"?: (input: Permission, output: { status: "ask" | "deny" | "allow" }) => Promise<void> | 176 | 180 | "command.execute.before"?: ( | 177 | 181 | input: { command: string; sessionID: string; arguments: string }, | |
|