Просмотр исходного кода

improve anthropic oauth token caching and authentication handling

🤖 Generated with [opencode](https://opencode.ai)

Co-Authored-By: opencode <[email protected]>
Dax Raad 8 месяцев назад
Родитель
Сommit
1c58023df9

+ 3 - 0
bun.lock

@@ -45,6 +45,7 @@
         "zod-openapi": "4.2.4",
       },
       "devDependencies": {
+        "@ai-sdk/anthropic": "1.2.12",
         "@tsconfig/bun": "1.0.7",
         "@types/bun": "latest",
         "@types/turndown": "5.0.5",
@@ -99,6 +100,8 @@
     "zod": "3.24.2",
   },
   "packages": {
+    "@ai-sdk/anthropic": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "1.1.3", "@ai-sdk/provider-utils": "2.2.8" }, "peerDependencies": { "zod": "^3.0.0" } }, "sha512-YSzjlko7JvuiyQFmI9RN1tNZdEiZxc+6xld/0tq/VkJaHpEzGAb1yiNxxvmYVcjvfu/PcvCxAAYXmTYQQ63IHQ=="],
+
     "@ai-sdk/provider": ["@ai-sdk/[email protected]", "", { "dependencies": { "json-schema": "^0.4.0" } }, "sha512-qZMxYJ0qqX/RfnuIaab+zp8UAeJn/ygXXAffR5I4N0n1IrvA6qBsjc8hXLmBiMV2zoXlifkacF7sEFnYnjBcqg=="],
 
     "@ai-sdk/provider-utils": ["@ai-sdk/[email protected]", "", { "dependencies": { "@ai-sdk/provider": "1.1.3", "nanoid": "^3.3.8", "secure-json-parse": "^2.7.0" }, "peerDependencies": { "zod": "^3.23.8" } }, "sha512-fqhG+4sCVv8x7nFzYnFo19ryhAa3w096Kmc3hWxMQfW/TubPOmt3A6tYZhl4mUfQWWQMsuSkLrtjlWuXBVSGQA=="],

+ 2 - 1
packages/opencode/package.json

@@ -20,7 +20,8 @@
     "@types/turndown": "5.0.5",
     "@types/yargs": "17.0.33",
     "typescript": "catalog:",
-    "zod-to-json-schema": "3.24.5"
+    "zod-to-json-schema": "3.24.5",
+    "@ai-sdk/anthropic": "1.2.12"
   },
   "dependencies": {
     "@clack/prompts": "0.11.0",

+ 3 - 0
packages/opencode/src/auth/anthropic.ts

@@ -48,6 +48,7 @@ export namespace AuthAnthropic {
     await Auth.set("anthropic", {
       type: "oauth",
       refresh: json.refresh_token as string,
+      access: json.access_token as string,
       expires: Date.now() + json.expires_in * 1000,
     })
   }
@@ -55,6 +56,7 @@ export namespace AuthAnthropic {
   export async function access() {
     const info = await Auth.get("anthropic")
     if (!info || info.type !== "oauth") return
+    if (info.access && info.expires > Date.now()) return info.access
     const response = await fetch(
       "https://console.anthropic.com/v1/oauth/token",
       {
@@ -74,6 +76,7 @@ export namespace AuthAnthropic {
     await Auth.set("anthropic", {
       type: "oauth",
       refresh: json.refresh_token as string,
+      access: json.access_token as string,
       expires: Date.now() + json.expires_in * 1000,
     })
     return json.access_token as string

+ 1 - 0
packages/opencode/src/auth/index.ts

@@ -7,6 +7,7 @@ export namespace Auth {
   export const Oauth = z.object({
     type: z.literal("oauth"),
     refresh: z.string(),
+    access: z.string(),
     expires: z.number(),
   })
 

+ 12 - 3
packages/opencode/src/provider/provider.ts

@@ -50,9 +50,18 @@ export namespace Provider {
       }
       return {
         apiKey: "",
-        headers: {
-          authorization: `Bearer ${access}`,
-          "anthropic-beta": "oauth-2025-04-20",
+        async fetch(input: any, init: any) {
+          const access = await AuthAnthropic.access()
+          const headers = {
+            ...init.headers,
+            authorization: `Bearer ${access}`,
+            "anthropic-beta": "oauth-2025-04-20",
+          }
+          delete headers["x-api-key"]
+          return fetch(input, {
+            ...init,
+            headers,
+          })
         },
       }
     },