-
-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathswagger-postprocess.mjs
More file actions
174 lines (156 loc) · 5.48 KB
/
Copy pathswagger-postprocess.mjs
File metadata and controls
174 lines (156 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import fs from "node:fs";
const file = process.argv[2] || "cmd/gomodel/docs/docs.go";
const source = fs.readFileSync(file, "utf8");
const marker = "const docTemplate = `";
const start = source.indexOf(marker);
if (start < 0) {
throw new Error("missing docTemplate start");
}
const templateStart = start + marker.length;
const end = source.indexOf("`\n\n// SwaggerInfo", templateStart);
if (end < 0) {
throw new Error("missing docTemplate end");
}
const schemesMarker = "__GOMODEL_SWAGGER_SCHEMES__";
const template = source.slice(templateStart, end);
const rawBacktickJoin = "` + \"`\" + `";
const parseableTemplate = template.replace(
new RegExp(rawBacktickJoin.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "g"),
"`",
).replace(
"{{ marshal .Schemes }}",
`["${schemesMarker}"]`,
);
const spec = JSON.parse(parseableTemplate);
function schema(name) {
const result = spec.definitions?.[name];
if (!result) {
throw new Error(`missing Swagger definition: ${name}`);
}
return result;
}
function clone(value) {
return JSON.parse(JSON.stringify(value));
}
function anthropicContentSchema() {
return {
oneOf: [
{ type: "string" },
{
type: "array",
items: { $ref: "#/definitions/anthropicapi.ContentBlock" },
},
],
};
}
function freeFormObjectSchema() {
return {
type: "object",
additionalProperties: true,
};
}
function stringOrFreeFormObjectSchema() {
return {
oneOf: [
{ type: "string" },
freeFormObjectSchema(),
],
};
}
function applyResponseConversationOneOf(name) {
const properties = schema(name).properties;
if (!properties?.conversation) {
throw new Error(`missing conversation property on definition: ${name}`);
}
const conversation = {};
if (properties.conversation.description) {
conversation.description = properties.conversation.description;
}
conversation.oneOf = clone([
{ type: "string" },
{ $ref: "#/definitions/core.ResponsesConversationRef" },
]);
properties.conversation = conversation;
}
function ensureRequiredProperty(schemaName, propertyName) {
const target = schema(schemaName);
if (!target.properties?.[propertyName]) {
throw new Error(`missing ${propertyName} property on definition: ${schemaName}`);
}
const required = new Set(target.required || []);
required.add(propertyName);
target.required = Array.from(required).sort();
}
// Swagger 2 mirror of the bounds applied in openapi-postprocess.mjs so the
// runtime spec advertises the same array limits as the published OpenAPI.
function applyStringArrayPropertyBounds(schemaName, propertyName, maxItems, itemMaxLength) {
const target = schema(schemaName);
const property = target.properties?.[propertyName];
if (!property || property.type !== "array") {
throw new Error(`expected array property ${propertyName} on definition: ${schemaName}`);
}
property.maxItems = maxItems;
property.items = property.items || {};
property.items.maxLength = itemMaxLength;
}
function applyArrayMaxItems(operationPath, method, statusCode, maxItems) {
const op = spec.paths?.[operationPath]?.[method];
if (!op) {
throw new Error(`missing operation: ${method.toUpperCase()} ${operationPath}`);
}
const response = op.responses?.[statusCode];
// Swagger 2 carries the response schema directly under `schema`.
const schemaRef = response?.schema;
if (!schemaRef || schemaRef.type !== "array") {
throw new Error(`expected array schema on ${method.toUpperCase()} ${operationPath} ${statusCode}`);
}
schemaRef.maxItems = maxItems;
}
function ensureAnthropicContentBlockSchema() {
if (!spec.definitions) {
throw new Error("missing Swagger definitions");
}
spec.definitions["anthropicapi.ContentBlock"] = {
type: "object",
properties: {
content: anthropicContentSchema(),
id: { type: "string" },
input: freeFormObjectSchema(),
is_error: { type: "boolean" },
name: { type: "string" },
source: stringOrFreeFormObjectSchema(),
text: { type: "string" },
thinking: { type: "string" },
tool_use_id: { type: "string" },
type: { type: "string" },
},
};
}
function applyAnthropicMessageSchemas() {
ensureAnthropicContentBlockSchema();
schema("anthropicapi.Message").properties.content = anthropicContentSchema();
schema("anthropicapi.MessagesRequest").properties.system = anthropicContentSchema();
schema("anthropicapi.ResponseContentBlock").properties.input = freeFormObjectSchema();
schema("anthropicapi.Tool").properties.input_schema = freeFormObjectSchema();
}
applyAnthropicMessageSchemas();
ensureRequiredProperty("core.ResponsesConversationRef", "id");
// Virtual-models admin contract: mirror the required field and array bounds the
// OpenAPI postprocess applies, so the embedded Swagger 2 spec matches.
ensureRequiredProperty("admin.upsertVirtualModelRequest", "source");
ensureRequiredProperty("admin.deleteVirtualModelRequest", "source");
applyStringArrayPropertyBounds("admin.upsertVirtualModelRequest", "user_paths", 100, 1024);
applyArrayMaxItems("/admin/virtual-models", "get", "200", 10000);
for (const name of [
"core.ResponsesRequest",
"core.ResponseInputTokensRequest",
"core.ResponseCompactRequest",
]) {
applyResponseConversationOneOf(name);
}
let rendered = JSON.stringify(spec, null, 4);
rendered = rendered.replace(
`"schemes": [\n "${schemesMarker}"\n ]`,
`"schemes": {{ marshal .Schemes }}`,
).replace(/`/g, rawBacktickJoin);
fs.writeFileSync(file, `${source.slice(0, templateStart)}${rendered}${source.slice(end)}`);