惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
罗磊的独立博客
博客园 - 聂微东
T
The Blog of Author Tim Ferriss
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
A
About on SuperTechFans
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
B
Blog RSS Feed
IT之家
IT之家
V
Visual Studio Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
C
Check Point Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Vercel News
Vercel News
爱范儿
爱范儿
Microsoft Security Blog
Microsoft Security Blog
月光博客
月光博客
T
Tailwind CSS Blog
The Cloudflare Blog
Hugging Face - Blog
Hugging Face - Blog

Recent Commits to openclaw:main

test: merge chat side-result checks · openclaw/openclaw@ddd2c2a test: merge cron history checks · openclaw/openclaw@f7eb746 test: merge responsive navigation shell checks · openclaw/openclaw@c2e4b47 docs(changelog): add codex oauth fixes · openclaw/openclaw@628e6cd test: merge navigation routing cases · openclaw/openclaw@5d8cecb Tests: mock channel registry bundled fallback · openclaw/openclaw@2b08233 Secrets: avoid broad web search discovery for single plugin config · openclaw/openclaw@a464f59 test: merge config view browser checks · openclaw/openclaw@20cf511 fix(status): align oauth health with runtime · openclaw/openclaw@eed7116 feat: add macOS screen snapshots for monitor preview (#67954) thanks … · openclaw/openclaw@f377db1 fix: report shared auth scopes in hello-ok (#67810) thanks @BunsDev · openclaw/openclaw@0b6c39b Auto-reply: avoid eager bundled route fallback · openclaw/openclaw@3ea1bf4 Tests: narrow session binding contract setup · openclaw/openclaw@54e4e16 fix(macOS): enable undo/redo in webchat composer text input (#34962) · openclaw/openclaw@00951dc Tests: speed up channel setup promotion · openclaw/openclaw@82b529a Docs: refresh agent instructions · openclaw/openclaw@5775fe2 fix(auth): serialize OAuth refresh across agents to fix #26322 (#67876) · openclaw/openclaw@8e79080 test: allow ollama public surface boundary test · openclaw/openclaw@7d4f1a6 Docs: add test performance guardrails · openclaw/openclaw@89706d3 Tests: restore context-engine usage proof · openclaw/openclaw@e4c4f95 Tests: slim context engine runtime coverage · openclaw/openclaw@74c198f ci: retry failed custom checkouts · openclaw/openclaw@0ee5baf test: trim duplicate provider auth onboarding cases · openclaw/openclaw@1ffc02e matrix: fix sessions_spawn --thread subagent session spawning (#67643) · openclaw/openclaw@1ce2596 test: reduce auth choice fixture churn · openclaw/openclaw@857b9cd test: mock health status config boundaries · openclaw/openclaw@9d5ab4a test: mock onboard config io boundary · openclaw/openclaw@299694d test: mock legacy state plugin boundaries · openclaw/openclaw@2713089 test: mock channel install boundaries · openclaw/openclaw@b945248 test: mock doctor preview channel boundaries · openclaw/openclaw@b1a3ad4
refactor: reuse shared dedupe helpers · openclaw/openclaw...
steipete · 2026-04-29 · via Recent Commits to openclaw:main

@@ -1,169 +1,6 @@

1-

import path from "node:path";

2-

import { loadJsonFile, saveJsonFile } from "openclaw/plugin-sdk/json-store";

3-

import { resolveProviderEndpoint } from "openclaw/plugin-sdk/provider-model-shared";

4-

import { resolveStateDir } from "openclaw/plugin-sdk/state-paths";

5-

import { normalizeLowercaseStringOrEmpty } from "openclaw/plugin-sdk/string-coerce-runtime";

6-7-

const COPILOT_TOKEN_URL = "https://api.github.com/copilot_internal/v2/token";

8-

const COPILOT_EDITOR_VERSION = "vscode/1.96.2";

9-

const COPILOT_USER_AGENT = "GitHubCopilotChat/0.26.7";

10-

const COPILOT_EDITOR_PLUGIN_VERSION = "copilot-chat/0.35.0";

11-

const COPILOT_GITHUB_API_VERSION = "2025-04-01";

12-13-

export const DEFAULT_COPILOT_API_BASE_URL = "https://api.individual.githubcopilot.com";

14-15-

export type CachedCopilotToken = {

16-

token: string;

17-

expiresAt: number;

18-

updatedAt: number;

19-

};

20-21-

function buildCopilotIdeHeaders(

22-

params: {

23-

includeApiVersion?: boolean;

24-

} = {},

25-

): Record<string, string> {

26-

return {

27-

"Editor-Version": COPILOT_EDITOR_VERSION,

28-

"Editor-Plugin-Version": COPILOT_EDITOR_PLUGIN_VERSION,

29-

"User-Agent": COPILOT_USER_AGENT,

30-

...(params.includeApiVersion ? { "X-Github-Api-Version": COPILOT_GITHUB_API_VERSION } : {}),

31-

};

32-

}

33-34-

function resolveCopilotTokenCachePath(env: NodeJS.ProcessEnv = process.env) {

35-

return path.join(resolveStateDir(env), "credentials", "github-copilot.token.json");

36-

}

37-38-

function isTokenUsable(cache: CachedCopilotToken, now = Date.now()): boolean {

39-

return cache.expiresAt - now > 5 * 60 * 1000;

40-

}

41-42-

function parseCopilotTokenResponse(value: unknown): {

43-

token: string;

44-

expiresAt: number;

45-

} {

46-

if (!value || typeof value !== "object") {

47-

throw new Error("Unexpected response from GitHub Copilot token endpoint");

48-

}

49-

const asRecord = value as Record<string, unknown>;

50-

const token = asRecord.token;

51-

const expiresAt = asRecord.expires_at;

52-

if (typeof token !== "string" || token.trim().length === 0) {

53-

throw new Error("Copilot token response missing token");

54-

}

55-56-

let expiresAtMs: number;

57-

if (typeof expiresAt === "number" && Number.isFinite(expiresAt)) {

58-

expiresAtMs = expiresAt < 100_000_000_000 ? expiresAt * 1000 : expiresAt;

59-

} else if (typeof expiresAt === "string" && expiresAt.trim().length > 0) {

60-

const parsed = Number.parseInt(expiresAt, 10);

61-

if (!Number.isFinite(parsed)) {

62-

throw new Error("Copilot token response has invalid expires_at");

63-

}

64-

expiresAtMs = parsed < 100_000_000_000 ? parsed * 1000 : parsed;

65-

} else {

66-

throw new Error("Copilot token response missing expires_at");

67-

}

68-69-

return { token, expiresAt: expiresAtMs };

70-

}

71-72-

function resolveCopilotProxyHost(proxyEp: string): string | null {

73-

const trimmed = proxyEp.trim();

74-

if (!trimmed) {

75-

return null;

76-

}

77-78-

const urlText = /^https?:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`;

79-

try {

80-

const url = new URL(urlText);

81-

if (url.protocol !== "http:" && url.protocol !== "https:") {

82-

return null;

83-

}

84-

return normalizeLowercaseStringOrEmpty(url.hostname);

85-

} catch {

86-

return null;

87-

}

88-

}

89-90-

export function deriveCopilotApiBaseUrlFromToken(token: string): string | null {

91-

const trimmed = token.trim();

92-

if (!trimmed) {

93-

return null;

94-

}

95-96-

const match = trimmed.match(/(?:^|;)\s*proxy-ep=([^;\s]+)/i);

97-

const proxyEp = match?.[1]?.trim();

98-

if (!proxyEp) {

99-

return null;

100-

}

101-102-

const proxyHost = resolveCopilotProxyHost(proxyEp);

103-

if (!proxyHost) {

104-

return null;

105-

}

106-

const host = proxyHost.replace(/^proxy\./i, "api.");

107-108-

const baseUrl = `https://${host}`;

109-

return resolveProviderEndpoint(baseUrl).endpointClass === "invalid" ? null : baseUrl;

110-

}

111-112-

export async function resolveCopilotApiToken(params: {

113-

githubToken: string;

114-

env?: NodeJS.ProcessEnv;

115-

fetchImpl?: typeof fetch;

116-

cachePath?: string;

117-

loadJsonFileImpl?: (path: string) => unknown;

118-

saveJsonFileImpl?: (path: string, value: CachedCopilotToken) => void;

119-

}): Promise<{

120-

token: string;

121-

expiresAt: number;

122-

source: string;

123-

baseUrl: string;

124-

}> {

125-

const env = params.env ?? process.env;

126-

const cachePath = params.cachePath?.trim() || resolveCopilotTokenCachePath(env);

127-

const loadJsonFileFn = params.loadJsonFileImpl ?? loadJsonFile;

128-

const saveJsonFileFn = params.saveJsonFileImpl ?? saveJsonFile;

129-

const cached = loadJsonFileFn(cachePath) as CachedCopilotToken | undefined;

130-

if (cached && typeof cached.token === "string" && typeof cached.expiresAt === "number") {

131-

if (isTokenUsable(cached)) {

132-

return {

133-

token: cached.token,

134-

expiresAt: cached.expiresAt,

135-

source: `cache:${cachePath}`,

136-

baseUrl: deriveCopilotApiBaseUrlFromToken(cached.token) ?? DEFAULT_COPILOT_API_BASE_URL,

137-

};

138-

}

139-

}

140-141-

const fetchImpl = params.fetchImpl ?? fetch;

142-

const res = await fetchImpl(COPILOT_TOKEN_URL, {

143-

method: "GET",

144-

headers: {

145-

Accept: "application/json",

146-

Authorization: `Bearer ${params.githubToken}`,

147-

...buildCopilotIdeHeaders({ includeApiVersion: true }),

148-

},

149-

});

150-151-

if (!res.ok) {

152-

throw new Error(`Copilot token exchange failed: HTTP ${res.status}`);

153-

}

154-155-

const json = parseCopilotTokenResponse(await res.json());

156-

const payload: CachedCopilotToken = {

157-

token: json.token,

158-

expiresAt: json.expiresAt,

159-

updatedAt: Date.now(),

160-

};

161-

saveJsonFileFn(cachePath, payload);

162-163-

return {

164-

token: payload.token,

165-

expiresAt: payload.expiresAt,

166-

source: `fetched:${COPILOT_TOKEN_URL}`,

167-

baseUrl: deriveCopilotApiBaseUrlFromToken(payload.token) ?? DEFAULT_COPILOT_API_BASE_URL,

168-

};

169-

}

1+

export {

2+

DEFAULT_COPILOT_API_BASE_URL,

3+

deriveCopilotApiBaseUrlFromToken,

4+

resolveCopilotApiToken,

5+

type CachedCopilotToken,

6+

} from "openclaw/plugin-sdk/provider-auth";