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

推荐订阅源

D
Docker
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Last Week in AI
Last Week in AI
博客园_首页
Microsoft Security Blog
Microsoft Security Blog
Blog — PlanetScale
Blog — PlanetScale
M
MIT News - Artificial intelligence
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
A
About on SuperTechFans
aimingoo的专栏
aimingoo的专栏
V
Visual Studio Blog
Jina AI
Jina AI
N
Netflix TechBlog - Medium
量子位
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
InfoQ
J
Java Code Geeks
T
Tailwind CSS Blog
博客园 - 司徒正美
Stack Overflow Blog
Stack Overflow Blog
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
腾讯CDC

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(anthropic-vertex): move SDK runtime to plugin (#...
vincentkoc · 2026-04-25 · via Recent Commits to openclaw:main

@@ -0,0 +1,199 @@

1+

import { AnthropicVertex } from "@anthropic-ai/vertex-sdk";

2+

import type { StreamFn } from "@mariozechner/pi-agent-core";

3+

import { streamAnthropic, type AnthropicOptions, type Model } from "@mariozechner/pi-ai";

4+

import {

5+

applyAnthropicPayloadPolicyToParams,

6+

resolveAnthropicPayloadPolicy,

7+

} from "openclaw/plugin-sdk/provider-stream-shared";

8+

import { resolveAnthropicVertexClientRegion, resolveAnthropicVertexProjectId } from "./region.js";

9+10+

type AnthropicVertexEffort = NonNullable<AnthropicOptions["effort"]>;

11+

type AnthropicVertexAdaptiveEffort = AnthropicVertexEffort | "xhigh";

12+13+

function isClaudeOpus47Model(modelId: string): boolean {

14+

return modelId.includes("opus-4-7") || modelId.includes("opus-4.7");

15+

}

16+17+

function isClaudeOpus46Model(modelId: string): boolean {

18+

return modelId.includes("opus-4-6") || modelId.includes("opus-4.6");

19+

}

20+21+

function supportsAdaptiveThinking(modelId: string): boolean {

22+

return (

23+

isClaudeOpus47Model(modelId) ||

24+

isClaudeOpus46Model(modelId) ||

25+

modelId.includes("sonnet-4-6") ||

26+

modelId.includes("sonnet-4.6")

27+

);

28+

}

29+30+

function mapAnthropicAdaptiveEffort(

31+

reasoning: string,

32+

modelId: string,

33+

): AnthropicVertexAdaptiveEffort {

34+

const effortMap: Record<string, AnthropicVertexAdaptiveEffort> = {

35+

minimal: "low",

36+

low: "low",

37+

medium: "medium",

38+

high: "high",

39+

xhigh: isClaudeOpus47Model(modelId) ? "xhigh" : isClaudeOpus46Model(modelId) ? "max" : "high",

40+

};

41+

return effortMap[reasoning] ?? "high";

42+

}

43+44+

function resolveAnthropicVertexMaxTokens(params: {

45+

modelMaxTokens: number | undefined;

46+

requestedMaxTokens: number | undefined;

47+

}): number | undefined {

48+

const modelMax =

49+

typeof params.modelMaxTokens === "number" &&

50+

Number.isFinite(params.modelMaxTokens) &&

51+

params.modelMaxTokens > 0

52+

? Math.floor(params.modelMaxTokens)

53+

: undefined;

54+

const requested =

55+

typeof params.requestedMaxTokens === "number" &&

56+

Number.isFinite(params.requestedMaxTokens) &&

57+

params.requestedMaxTokens > 0

58+

? Math.floor(params.requestedMaxTokens)

59+

: undefined;

60+61+

if (modelMax !== undefined && requested !== undefined) {

62+

return Math.min(requested, modelMax);

63+

}

64+

return requested ?? modelMax;

65+

}

66+67+

function createAnthropicVertexOnPayload(params: {

68+

model: { api: string; baseUrl?: string; provider: string };

69+

cacheRetention: AnthropicOptions["cacheRetention"] | undefined;

70+

onPayload: AnthropicOptions["onPayload"] | undefined;

71+

}): NonNullable<AnthropicOptions["onPayload"]> {

72+

const policy = resolveAnthropicPayloadPolicy({

73+

provider: params.model.provider,

74+

api: params.model.api,

75+

baseUrl: params.model.baseUrl,

76+

cacheRetention: params.cacheRetention,

77+

enableCacheControl: true,

78+

});

79+80+

function applyPolicy(payload: unknown): unknown {

81+

if (payload && typeof payload === "object" && !Array.isArray(payload)) {

82+

applyAnthropicPayloadPolicyToParams(payload as Record<string, unknown>, policy);

83+

}

84+

return payload;

85+

}

86+87+

return async (payload, model) => {

88+

const shapedPayload = applyPolicy(payload);

89+

const nextPayload = await params.onPayload?.(shapedPayload, model);

90+

if (nextPayload === undefined || nextPayload === shapedPayload) {

91+

return shapedPayload;

92+

}

93+

return applyPolicy(nextPayload);

94+

};

95+

}

96+97+

/**

98+

* Create a StreamFn that routes through pi-ai's `streamAnthropic` with an

99+

* injected `AnthropicVertex` client. All streaming, message conversion, and

100+

* event handling is handled by pi-ai — we only supply the GCP-authenticated

101+

* client and map SimpleStreamOptions → AnthropicOptions.

102+

*/

103+

export function createAnthropicVertexStreamFn(

104+

projectId: string | undefined,

105+

region: string,

106+

baseURL?: string,

107+

): StreamFn {

108+

const client = new AnthropicVertex({

109+

region,

110+

...(baseURL ? { baseURL } : {}),

111+

...(projectId ? { projectId } : {}),

112+

});

113+114+

return (model, context, options) => {

115+

const transportModel = model as Model<"anthropic-messages"> & {

116+

api: string;

117+

baseUrl?: string;

118+

provider: string;

119+

};

120+

const maxTokens = resolveAnthropicVertexMaxTokens({

121+

modelMaxTokens: transportModel.maxTokens,

122+

requestedMaxTokens: options?.maxTokens,

123+

});

124+

const opts: AnthropicOptions = {

125+

client: client as unknown as AnthropicOptions["client"],

126+

temperature: options?.temperature,

127+

...(maxTokens !== undefined ? { maxTokens } : {}),

128+

signal: options?.signal,

129+

cacheRetention: options?.cacheRetention,

130+

sessionId: options?.sessionId,

131+

headers: options?.headers,

132+

onPayload: createAnthropicVertexOnPayload({

133+

model: transportModel,

134+

cacheRetention: options?.cacheRetention,

135+

onPayload: options?.onPayload,

136+

}),

137+

maxRetryDelayMs: options?.maxRetryDelayMs,

138+

metadata: options?.metadata,

139+

};

140+141+

if (options?.reasoning) {

142+

if (supportsAdaptiveThinking(model.id)) {

143+

opts.thinkingEnabled = true;

144+

opts.effort = mapAnthropicAdaptiveEffort(

145+

options.reasoning,

146+

model.id,

147+

) as AnthropicVertexEffort;

148+

} else {

149+

opts.thinkingEnabled = true;

150+

const budgets = options.thinkingBudgets;

151+

opts.thinkingBudgetTokens =

152+

(budgets && options.reasoning in budgets

153+

? budgets[options.reasoning as keyof typeof budgets]

154+

: undefined) ?? 10000;

155+

}

156+

} else {

157+

opts.thinkingEnabled = false;

158+

}

159+160+

return streamAnthropic(transportModel, context, opts);

161+

};

162+

}

163+164+

function resolveAnthropicVertexSdkBaseUrl(baseUrl?: string): string | undefined {

165+

const trimmed = baseUrl?.trim();

166+

if (!trimmed) {

167+

return undefined;

168+

}

169+170+

try {

171+

const url = new URL(trimmed);

172+

const normalizedPath = url.pathname.replace(/\/+$/, "");

173+

if (!normalizedPath || normalizedPath === "") {

174+

url.pathname = "/v1";

175+

return url.toString().replace(/\/$/, "");

176+

}

177+

if (!normalizedPath.endsWith("/v1")) {

178+

url.pathname = `${normalizedPath}/v1`;

179+

return url.toString().replace(/\/$/, "");

180+

}

181+

return trimmed;

182+

} catch {

183+

return trimmed;

184+

}

185+

}

186+187+

export function createAnthropicVertexStreamFnForModel(

188+

model: { baseUrl?: string },

189+

env: NodeJS.ProcessEnv = process.env,

190+

): StreamFn {

191+

return createAnthropicVertexStreamFn(

192+

resolveAnthropicVertexProjectId(env),

193+

resolveAnthropicVertexClientRegion({

194+

baseUrl: model.baseUrl,

195+

env,

196+

}),

197+

resolveAnthropicVertexSdkBaseUrl(model.baseUrl),

198+

);

199+

}