Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

amazon-bedrock.ts

101 lines3.3 KB

1/**
2 * Amazon Bedrock — first-party only (issue #153). Models come from the user
3 * guide's model cards; the runtime schema (Converse, ConverseStream,
4 * InvokeModel, InvokeModelWithResponseStream) is generated from the AWS
5 * SDK's Bedrock Runtime service model. Neither needs AWS credentials
6 * (ListFoundationModels does), and no aggregator is read.
7 */
8import type { Activity } from '#/db/schema.ts'
9​
10import { BEDROCK_CARDS_URL, bedrockCardListing } from '../bedrock-cards.ts'
11import {
12 BEDROCK_RUNTIME_PATHS,
13 BEDROCK_SDK_MODEL_URL,
14 bedrockConverseSpec,
15} from '../bedrock-sdk-spec.ts'
16import type { BedrockServiceModel } from '../bedrock-sdk-spec.ts'
17import { loadBedrockConverseWireMaps } from '../bedrock-converse-facts.ts'
18import { loadBedrockThinkingGuides } from '../bedrock-thinking-guides.ts'
19import { docsRun, docsReport } from '../model-facts.ts'
20import { fetchText, sha256Text } from '../types.ts'
21import type {
22 ListModelsResult,
23 ProviderConfig,
24 ProviderSecrets,
25 SpecFetchResult,
26} from '../types.ts'
27​
28async function fetchSpec(_env: ProviderSecrets): Promise<SpecFetchResult> {
29 const text = await fetchText(BEDROCK_SDK_MODEL_URL)
30 const hash = await sha256Text(text)
31 return {
32 specs: [bedrockConverseSpec(JSON.parse(text) as BedrockServiceModel)],
33 sources: [{ url: BEDROCK_SDK_MODEL_URL, hash }],
34 outputStrategy: 'post-200',
35 specRevision: hash,
36 }
38​
39async function listModels(
40 _env: ProviderSecrets,
41 kv?: KVNamespace,
42): Promise<ListModelsResult> {
43 const catalog = await bedrockCardListing(kv)
44 const run = docsRun()
45 const thinking = await loadBedrockThinkingGuides(
46 catalog.models,
47 catalog.groups,
48 run,
49 kv,
50 )
51 const models = await loadBedrockConverseWireMaps(thinking, run, kv)
52 return { models, docsFailures: docsReport(run) }
54​
55export const provider: ProviderConfig = {
56 id: 'amazon-bedrock',
57 // LLM Gateway route name (providers[].providerId).
58 modelNamespaces: ['aws-bedrock'],
59 displayName: 'Amazon Bedrock',
60 specSourceUrl: BEDROCK_SDK_MODEL_URL,
61 modelsEndpoint: BEDROCK_CARDS_URL,
62 defaultDerivation: 'generated',
63 // Converse is an AWS API: requests are SigV4-signed (service `bedrock`).
64 // A Bedrock API key (`AWS_BEARER_TOKEN_BEDROCK`) is the stated alternative.
65 connect: {
66 servers: [
67 {
68 url: 'https://bedrock-runtime.{region}.amazonaws.com',
69 variables: {
70 region: {
71 default: 'us-east-1',
72 description: 'The AWS Region you call.',
73 },
74 },
75 },
76 ],
77 securitySchemes: {
78 sigv4: {
79 type: 'apiKey',
80 in: 'header',
81 name: 'Authorization',
82 'x-amazon-apigateway-authtype': 'awsSigv4',
83 description:
84 'AWS Signature Version 4 with service `bedrock` and the Region in the URL; a generated HTTP client cannot sign this itself, so route through an AWS SDK or a SigV4 signer.',
85 },
86 bearerAuth: {
87 type: 'http',
88 scheme: 'bearer',
89 description:
90 'A Bedrock API key (`AWS_BEARER_TOKEN_BEDROCK`) as `Authorization: Bearer <key>`.',
91 },
92 },
93 security: [{ sigv4: [] }, { bearerAuth: [] }],
94 },
95 fetchSpec,
96 listModels,
97 classify: (path): Activity | null =>
98 (BEDROCK_RUNTIME_PATHS as ReadonlyArray<string>).includes(path)
99 ? 'chat'
100 : null,