Skip to content

Instantly share code, notes, and snippets.

@leehanchung
Created March 8, 2025 23:14
Show Gist options
  • Save leehanchung/8d93257acda9ed0a83a5c69945c8171c to your computer and use it in GitHub Desktop.
Save leehanchung/8d93257acda9ed0a83a5c69945c8171c to your computer and use it in GitHub Desktop.
claude code using aws vertexai
var c9 = !!process.env.CLAUDE_CODE_USE_BEDROCK,
p9 = !!process.env.CLAUDE_CODE_USE_VERTEX,
Tl1 = {
bedrock: 'us.anthropic.claude-3-7-sonnet-20250219-v1:0',
vertex: 'claude-3-7-sonnet@20250219',
firstParty: 'claude-3-7-sonnet-20250219',
},
PB = c9
? 'us.anthropic.claude-3-5-haiku-20241022-v1:0'
: p9
? 'claude-3-5-haiku@20241022'
: 'claude-3-5-haiku-20241022'
async function qX4() {
try {
return await DJ('tengu-capable-model-config', Tl1)
} catch (I) {
return C0(I), Tl1
}
}
var f6 = s2(async () => {
let I = await qX4()
if (c9) {
if (process.env.ANTHROPIC_MODEL) return process.env.ANTHROPIC_MODEL
return I.bedrock
}
if (p9) {
if (process.env.ANTHROPIC_MODEL) return process.env.ANTHROPIC_MODEL
return I.vertex
}
return I.firstParty
})
async function i41() {
return !process.env.ANTHROPIC_MODEL || process.env.ANTHROPIC_MODEL === (await f6())
}
function ml1(I) {
if (I?.startsWith('claude-3-5-haiku')) return process.env.VERTEX_REGION_CLAUDE_3_5_HAIKU
else if (I?.startsWith('claude-3-5-sonnet')) return process.env.VERTEX_REGION_CLAUDE_3_5_SONNET
else if (I?.startsWith('claude-3-7-sonnet')) return process.env.VERTEX_REGION_CLAUDE_3_7_SONNET
}
var bl1 = {},
AM = null,
ez = s2(async () => {
if (f2.isCI) return null
let I = await Cj(),
G = {
networkConfig: { api: 'https://statsig.anthropic.com/v1/' },
environment: {
tier: f2.isCI || ['test', 'development'].includes('production') ? 'dev' : 'production',
},
logLevel: Bj.LogLevel.None,
storageProvider: new p41(),
}
return (
(AM = new Bj.StatsigClient($l1, I, G)),
AM.on('error', (Z) => {
C0(`Statsig error: ${Z}`)
}),
await AM.initializeAsync(),
process.on('exit', () => {
AM?.flush()
}),
AM
)
})
...
...
var Ai5 = 'bedrock-2023-05-31',
Xi5 = new Set(['/v1/complete', '/v1/messages', '/v1/messages?beta=true'])
class qr extends AK {
constructor({
baseURL: I = pW('ANTHROPIC_BEDROCK_BASE_URL'),
awsSecretKey: G = null,
awsAccessKey: Z = null,
awsRegion: d = pW('AWS_REGION') ?? 'us-east-1',
awsSessionToken: W = null,
...w
} = {}) {
let B = {
awsSecretKey: G,
awsAccessKey: Z,
awsRegion: d,
awsSessionToken: W,
...w,
baseURL: I || `https://bedrock-runtime.${d}.amazonaws.com`,
}
super({
baseURL: B.baseURL,
timeout: B.timeout ?? 600000,
httpAgent: B.httpAgent,
maxRetries: B.maxRetries,
fetch: B.fetch,
})
;(this.messages = Yi5(this)),
(this.completions = new n_(this)),
(this.beta = _i5(this)),
(this._options = B),
(this.awsSecretKey = G),
(this.awsAccessKey = Z),
(this.awsRegion = d),
(this.awsSessionToken = W)
}
defaultQuery() {
return this._options.defaultQuery
}
defaultHeaders(I) {
return { ...super.defaultHeaders(I), ...this._options.defaultHeaders }
}
async prepareRequest(I, { url: G, options: Z }) {
let d = this.awsRegion
if (!d)
throw new Error(
'Expected `awsRegion` option to be passed to the client or the `AWS_REGION` environment variable to be present',
)
let W = await Vp0(I, {
url: G,
regionName: d,
awsAccessKey: this.awsAccessKey,
awsSecretKey: this.awsSecretKey,
awsSessionToken: this.awsSessionToken,
})
I.headers = { ...I.headers, ...W }
}
buildRequest(I) {
if (((I.__streamClass = Qr), p_(I.body))) I.body = { ...I.body }
if (p_(I.body)) {
if (!I.body.anthropic_version) I.body.anthropic_version = Ai5
if (I.headers && !I.body.anthropic_beta) {
let G = nq(I.headers, 'anthropic-beta')
if (G != null) I.body.anthropic_beta = G.split(',')
}
}
if (Xi5.has(I.path) && I.method === 'post') {
if (!p_(I.body))
throw new Error('Expected request body to be an object for post /v1/messages')
let G = I.body.model
I.body.model = void 0
let Z = I.body.stream
if (((I.body.stream = void 0), Z)) I.path = `/model/${G}/invoke-with-response-stream`
else I.path = `/model/${G}/invoke`
}
return super.buildRequest(I)
}
}
function Yi5(I) {
let G = new fd(I)
return delete G.batches, delete G.countTokens, G
}
function _i5(I) {
let G = new wZ(I)
return delete G.promptCaching, delete G.messages.batches, delete G.messages.countTokens, G
}
var lB2 = X1(bB2(), 1),
xe5 = 'vertex-2023-10-16',
ce5 = new Set(['/v1/messages', '/v1/messages?beta=true'])
class Gs extends AK {
constructor({
baseURL: I = pW('ANTHROPIC_VERTEX_BASE_URL'),
region: G = pW('CLOUD_ML_REGION') ?? null,
projectId: Z = pW('ANTHROPIC_VERTEX_PROJECT_ID') ?? null,
...d
} = {}) {
if (!G)
throw new Error(
'No region was given. The client should be instantiated with the `region` option or the `CLOUD_ML_REGION` environment variable should be set.',
)
let W = { ...d, baseURL: I || `https://${G}-aiplatform.googleapis.com/v1` }
super({
baseURL: W.baseURL,
timeout: W.timeout ?? 600000,
httpAgent: W.httpAgent,
maxRetries: W.maxRetries,
fetch: W.fetch,
})
;(this.messages = pe5(this)),
(this.beta = ie5(this)),
(this._options = W),
(this.region = G),
(this.projectId = Z),
(this.accessToken = W.accessToken ?? null),
(this._auth =
W.googleAuth ??
new lB2.GoogleAuth({ scopes: 'https://www.googleapis.com/auth/cloud-platform' })),
(this._authClientPromise = this._auth.getClient())
}
defaultQuery() {
return this._options.defaultQuery
}
defaultHeaders(I) {
return { ...super.defaultHeaders(I), ...this._options.defaultHeaders }
}
async prepareOptions(I) {
let G = await this._authClientPromise,
Z = await G.getRequestHeaders(),
d = G.projectId ?? Z['x-goog-user-project']
if (!this.projectId && d) this.projectId = d
I.headers = { ...Z, ...I.headers }
}
buildRequest(I) {
if (p_(I.body)) I.body = { ...I.body }
if (p_(I.body)) {
if (!I.body.anthropic_version) I.body.anthropic_version = xe5
}
if (ce5.has(I.path) && I.method === 'post') {
if (!this.projectId)
throw new Error(
'No projectId was given and it could not be resolved from credentials. The client should be instantiated with the `projectId` option or the `ANTHROPIC_VERTEX_PROJECT_ID` environment variable should be set.',
)
if (!p_(I.body))
throw new Error('Expected request body to be an object for post /v1/messages')
let G = I.body.model
I.body.model = void 0
let d = (I.body.stream ?? !1) ? 'streamRawPredict' : 'rawPredict'
I.path = `/projects/${this.projectId}/locations/${this.region}/publishers/anthropic/models/${G}:${d}`
}
if (
I.path === '/v1/messages/count_tokens' ||
(I.path == '/v1/messages/count_tokens?beta=true' && I.method === 'post')
) {
if (!this.projectId)
throw new Error(
'No projectId was given and it could not be resolved from credentials. The client should be instantiated with the `projectId` option or the `ANTHROPIC_VERTEX_PROJECT_ID` environment variable should be set.',
)
I.path = `/projects/${this.projectId}/locations/${this.region}/publishers/anthropic/models/count-tokens:rawPredict`
}
return super.buildRequest(I)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment