opencode-translate 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (40) hide show
  1. package/README.md +1 -1
  2. package/package.json +6 -3
  3. package/src/activation/chat-message.ts +164 -0
  4. package/src/activation/index.ts +38 -0
  5. package/src/activation/logging.ts +11 -0
  6. package/src/activation/messages-transform.ts +38 -0
  7. package/src/activation/metadata.ts +42 -0
  8. package/src/activation/parts.ts +53 -0
  9. package/src/activation/question-hooks.ts +95 -0
  10. package/src/activation/state.ts +98 -0
  11. package/src/activation/text-complete.ts +51 -0
  12. package/src/activation/trigger.ts +57 -0
  13. package/src/activation/types.ts +41 -0
  14. package/src/activation.ts +1 -607
  15. package/src/anthropic-oauth.ts +3 -3
  16. package/src/auth/codex-request.ts +108 -0
  17. package/src/auth/codex-response.ts +78 -0
  18. package/src/auth/codex-shared.ts +3 -0
  19. package/src/auth/headers.ts +18 -0
  20. package/src/auth/index.ts +153 -0
  21. package/src/auth/oauth-fetch.ts +100 -0
  22. package/src/auth/refresh.ts +102 -0
  23. package/src/auth/retry.ts +70 -0
  24. package/src/auth/store.ts +45 -0
  25. package/src/auth/types.ts +27 -0
  26. package/src/auth.ts +1 -725
  27. package/src/constants/errors.ts +24 -0
  28. package/src/constants/guards.ts +34 -0
  29. package/src/constants/options.ts +37 -0
  30. package/src/constants/plugin.ts +10 -0
  31. package/src/constants/types.ts +147 -0
  32. package/src/constants.ts +5 -261
  33. package/src/labels.ts +0 -2
  34. package/src/question-tool.ts +45 -22
  35. package/src/translator/index.ts +125 -0
  36. package/src/translator/part-id.ts +43 -0
  37. package/src/translator/provider.ts +81 -0
  38. package/src/translator/retry.ts +62 -0
  39. package/src/translator/types.ts +17 -0
  40. package/src/translator.ts +1 -326
@@ -0,0 +1,45 @@
1
+ import { readFile, stat } from "node:fs/promises"
2
+ import os from "node:os"
3
+ import path from "node:path"
4
+ import { type AuthInfo, OAUTH_DUMMY_KEY, type OAuthInfo } from "../constants"
5
+ import type { AuthDependencies } from "./types"
6
+
7
+ // Mirrors opencode's xdg-basedir auth location; see packages/opencode/src/global/index.ts.
8
+ function authFilePath(): string {
9
+ const xdgDataHome = process.env.XDG_DATA_HOME
10
+ if (xdgDataHome) return path.join(xdgDataHome, "opencode", "auth.json")
11
+ if (process.platform === "win32") {
12
+ return path.join(process.env.LOCALAPPDATA || path.join(os.homedir(), "AppData", "Local"), "opencode", "auth.json")
13
+ }
14
+ return path.join(os.homedir(), ".local", "share", "opencode", "auth.json")
15
+ }
16
+
17
+ export function normalizeProviderKey(value: string | undefined): string | undefined {
18
+ if (!value || value === OAUTH_DUMMY_KEY) return undefined
19
+ return value
20
+ }
21
+
22
+ export function ensureOAuthInfo(value: AuthInfo | undefined): OAuthInfo | undefined {
23
+ return value && value.type === "oauth" ? value : undefined
24
+ }
25
+
26
+ export async function readAuthMap(deps: AuthDependencies): Promise<Record<string, AuthInfo> | undefined> {
27
+ if (process.env.OPENCODE_AUTH_CONTENT) {
28
+ try {
29
+ const parsed = JSON.parse(process.env.OPENCODE_AUTH_CONTENT) as Record<string, AuthInfo>
30
+ if (parsed && typeof parsed === "object") return parsed
31
+ } catch {}
32
+ return undefined
33
+ }
34
+
35
+ const filePath = authFilePath()
36
+ try {
37
+ const fileStat = await (deps.stat ?? stat)(filePath)
38
+ if ((fileStat.mode & 0o777) !== 0o600) return undefined
39
+ const raw = await (deps.readFile ?? readFile)(filePath, "utf8")
40
+ const parsed = JSON.parse(raw) as Record<string, AuthInfo>
41
+ return parsed && typeof parsed === "object" ? parsed : undefined
42
+ } catch {
43
+ return undefined
44
+ }
45
+ }
@@ -0,0 +1,27 @@
1
+ import type { FetchLike, OAuthInfo, ProviderInfo } from "../constants"
2
+
3
+ export interface ResolvedCredential {
4
+ providerID: string
5
+ provider?: ProviderInfo
6
+ apiKey?: string
7
+ fetch?: FetchLike
8
+ mode: "apiKey" | "oauth" | "default"
9
+ }
10
+
11
+ export interface AuthDependencies {
12
+ fetchImpl?: FetchLike
13
+ now?: () => number
14
+ sleep?: (ms: number) => Promise<void>
15
+ readFile?: (filePath: string, encoding: BufferEncoding) => Promise<string>
16
+ stat?: (filePath: string) => Promise<{ mode: number }>
17
+ packageVersion?: string
18
+ }
19
+
20
+ export type AuthRuntime = Required<Pick<AuthDependencies, "fetchImpl" | "sleep">>
21
+
22
+ export type OAuthResolver = (providerID: string) => Promise<OAuthInfo | undefined>
23
+
24
+ export interface CodexBodyRewrite {
25
+ body: BodyInit | null | undefined
26
+ originalStream: boolean
27
+ }