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

bedrock-sdk-spec.ts

181 lines5.4 KB

1/**
2 * Bedrock Runtime Converse as OpenAPI (issue #153). AWS publishes no OpenAPI
3 * document, so the spec is generated at sync time from the service model the
4 * AWS SDK for Python ships (botocore `service-2.json`): every shape becomes a
5 * component schema and Converse becomes one POST path. InvokeModel is left
6 * out — its body is an opaque blob whose shape belongs to each model maker.
7 */
8import { BEDROCK_CONVERSE_PATH } from './bedrock-cards.ts'
9import { PROVENANCE_MARKER } from './types.ts'
10import type { OpenApiDocument } from './types.ts'
11​
12export const BEDROCK_SDK_MODEL_URL =
13 'https://raw.githubusercontent.com/boto/botocore/develop/botocore/data/bedrock-runtime/2023-09-30/service-2.json'
14​
15const REF = '#/components/schemas/'
16​
17interface Member {
18 shape: string
19 documentation?: string
20 /** `uri` / `header` / `querystring`: not part of the JSON body. */
21 location?: string
23​
24interface Shape {
25 type: string
26 members?: Record<string, Member>
27 required?: Array<string>
28 member?: Member
29 value?: Member
30 enum?: Array<string>
31 min?: number
32 max?: number
33 documentation?: string
34 /** Tagged union: exactly one member is set. */
35 union?: boolean
36 /** Free-form JSON value. */
37 document?: boolean
38 exception?: boolean
40​
41export interface BedrockServiceModel {
42 operations: Record<
43 string,
44 {
45 http: { method: string; requestUri: string }
46 input?: { shape: string }
47 output?: { shape: string }
48 documentation?: string
49 }
50 >
51 shapes: Record<string, Shape>
53​
54type Schema = Record<string, unknown>
55​
56function prose(html: string | undefined): string | undefined {
57 const text = html
58 ?.replace(/<[^>]+>/g, ' ')
59 .replace(/\s+/g, ' ')
60 .trim()
61 return text === '' ? undefined : text
63​
64function described(schema: Schema, html: string | undefined): Schema {
65 const description = prose(html)
66 return description ? { ...schema, description } : schema
68​
69const ref = (member: Member): Schema =>
70 described({ $ref: REF + member.shape }, member.documentation)
71​
72function range(shape: Shape, min: string, max: string): Schema {
73 return {
74 ...(shape.min !== undefined ? { [min]: shape.min } : {}),
75 ...(shape.max !== undefined ? { [max]: shape.max } : {}),
76 }
78​
79function shapeSchema(shape: Shape): Schema {
80 switch (shape.type) {
81 case 'structure': {
82 if (shape.document) return {}
83 const body = Object.entries(shape.members ?? {}).filter(
84 ([, member]) => member.location === undefined,
85 )
86 const required = (shape.required ?? []).filter((name) =>
87 body.some(([key]) => key === name),
88 )
89 return {
90 type: 'object',
91 properties: Object.fromEntries(
92 body.map(([name, member]) => [name, ref(member)]),
93 ),
94 ...(required.length > 0 ? { required } : {}),
95 ...(shape.union ? { minProperties: 1, maxProperties: 1 } : {}),
96 }
97 }
98 case 'list':
99 return {
100 type: 'array',
101 ...(shape.member ? { items: ref(shape.member) } : {}),
102 ...range(shape, 'minItems', 'maxItems'),
103 }
104 case 'map':
105 return {
106 type: 'object',
107 ...(shape.value ? { additionalProperties: ref(shape.value) } : {}),
108 }
109 case 'string':
110 // `pattern` is dropped: the SDK's regexes are not all ECMAScript.
111 return {
112 type: 'string',
113 ...(shape.enum ? { enum: shape.enum } : {}),
114 ...range(shape, 'minLength', 'maxLength'),
115 }
116 case 'blob':
117 return { type: 'string', contentEncoding: 'base64' }
118 case 'integer':
119 case 'long':
120 return { type: 'integer', ...range(shape, 'minimum', 'maximum') }
121 case 'float':
122 case 'double':
123 return { type: 'number', ...range(shape, 'minimum', 'maximum') }
124 case 'boolean':
125 return { type: 'boolean' }
126 case 'timestamp':
127 return { type: 'string', format: 'date-time' }
128 default:
129 throw new Error(`amazon-bedrock SDK model: shape type "${shape.type}"`)
130 }
132​
133/** The SDK service model → an OpenAPI document with the Converse path. */
134export function bedrockConverseSpec(
135 model: BedrockServiceModel,
136): OpenApiDocument {
137 const converse = Object.values(model.operations).find(
138 (op) => op.http.requestUri === BEDROCK_CONVERSE_PATH,
139 )
140 if (!converse?.input || !converse.output) {
141 throw new Error('amazon-bedrock SDK model: no Converse operation')
142 }
143 const json = (shape: string) => ({
144 'application/json': { schema: { $ref: REF + shape } },
145 })
146 return {
147 openapi: '3.1.0',
148 info: { title: 'Amazon Bedrock Runtime', version: '2023-09-30' },
149 paths: {
150 [BEDROCK_CONVERSE_PATH]: {
151 post: {
152 operationId: 'Converse',
153 ...described({}, converse.documentation),
154 [PROVENANCE_MARKER]: { derivation: 'generated' },
155 parameters: [
156 {
157 name: 'modelId',
158 in: 'path',
159 required: true,
160 schema: { type: 'string' },
161 },
162 ],
163 requestBody: { required: true, content: json(converse.input.shape) },
164 responses: {
165 '200': { description: 'OK', content: json(converse.output.shape) },
166 },
167 },
168 },
169 },
170 components: {
171 schemas: Object.fromEntries(
172 Object.entries(model.shapes)
173 .filter(([, shape]) => !shape.exception)
174 .map(([name, shape]) => [
175 name,
176 described(shapeSchema(shape), shape.documentation),
177 ]),
178 ),
179 },
180 }