@modelprofile.com/flexharness-providers 9.0.0 → 9.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (109) hide show
  1. package/dist_ts_anthropic_messages/errors.d.ts +22 -0
  2. package/dist_ts_anthropic_messages/errors.js +100 -0
  3. package/dist_ts_anthropic_messages/index.d.ts +4 -0
  4. package/dist_ts_anthropic_messages/index.js +4 -0
  5. package/dist_ts_anthropic_messages/interfaces.d.ts +185 -0
  6. package/dist_ts_anthropic_messages/interfaces.js +2 -0
  7. package/dist_ts_anthropic_messages/openai.d.ts +19 -0
  8. package/dist_ts_anthropic_messages/openai.js +48 -0
  9. package/dist_ts_anthropic_messages/plugins.d.ts +4 -0
  10. package/dist_ts_anthropic_messages/plugins.js +4 -0
  11. package/dist_ts_anthropic_messages/request.d.ts +109 -0
  12. package/dist_ts_anthropic_messages/request.js +554 -0
  13. package/dist_ts_anthropic_messages/service.d.ts +8 -0
  14. package/dist_ts_anthropic_messages/service.js +274 -0
  15. package/dist_ts_anthropic_messages/signature.d.ts +16 -0
  16. package/dist_ts_anthropic_messages/signature.js +91 -0
  17. package/dist_ts_anthropic_messages/stream.d.ts +48 -0
  18. package/dist_ts_anthropic_messages/stream.js +220 -0
  19. package/dist_ts_anthropic_messages_node/index.d.ts +8 -0
  20. package/dist_ts_anthropic_messages_node/index.js +93 -0
  21. package/dist_ts_anthropic_messages_node/plugins.d.ts +6 -0
  22. package/dist_ts_anthropic_messages_node/plugins.js +4 -0
  23. package/dist_ts_openai_account/plugins.d.ts +2 -0
  24. package/dist_ts_openai_account/plugins.js +4 -1
  25. package/dist_ts_openai_account/smartai.providers.interfaces.d.ts +6 -22
  26. package/dist_ts_openai_account/smartai.providers.openai.js +15 -134
  27. package/dist_ts_openai_auth/index.d.ts +1 -2
  28. package/dist_ts_openai_auth/index.js +2 -3
  29. package/dist_ts_openai_auth/interfaces.d.ts +1 -1
  30. package/dist_ts_openai_auth/smartai.auth.openai.d.ts +3 -15
  31. package/dist_ts_openai_auth/smartai.auth.openai.js +3 -17
  32. package/dist_ts_openai_auth/smartai.connection.openai.d.ts +1 -1
  33. package/dist_ts_openai_auth/smartai.connection.openai.js +2 -3
  34. package/dist_ts_providers/anthropic/index.d.ts +1 -1
  35. package/dist_ts_providers/anthropic/index.js +1 -1
  36. package/dist_ts_providers/anthropic/plugins.d.ts +1 -1
  37. package/dist_ts_providers/google/index.d.ts +1 -1
  38. package/dist_ts_providers/google/index.js +1 -1
  39. package/dist_ts_providers/google/plugins.d.ts +1 -1
  40. package/dist_ts_providers/groq/index.d.ts +1 -1
  41. package/dist_ts_providers/groq/index.js +1 -1
  42. package/dist_ts_providers/groq/plugins.d.ts +1 -1
  43. package/dist_ts_providers/mistral/index.d.ts +1 -1
  44. package/dist_ts_providers/mistral/index.js +1 -1
  45. package/dist_ts_providers/mistral/plugins.d.ts +1 -1
  46. package/dist_ts_providers/openai/index.d.ts +5 -1
  47. package/dist_ts_providers/openai/index.js +5 -1
  48. package/dist_ts_providers/openai/plugins.d.ts +4 -4
  49. package/dist_ts_providers/openai/plugins.js +3 -3
  50. package/dist_ts_providers/openai/smartai.chatgpt.catalog.d.ts +61 -0
  51. package/dist_ts_providers/openai/smartai.chatgpt.catalog.js +252 -0
  52. package/{dist_ts_openai_auth/smartai.limits.openai.d.ts → dist_ts_providers/openai/smartai.chatgpt.limits.d.ts} +2 -2
  53. package/dist_ts_providers/openai/smartai.chatgpt.limits.js +49 -0
  54. package/dist_ts_providers/openai/smartai.chatgpt.middleware.d.ts +10 -0
  55. package/dist_ts_providers/openai/smartai.chatgpt.middleware.js +60 -0
  56. package/dist_ts_providers/openai/smartai.chatgpt.openai.d.ts +92 -0
  57. package/dist_ts_providers/openai/smartai.chatgpt.openai.js +160 -0
  58. package/dist_ts_providers/perplexity/index.d.ts +1 -1
  59. package/dist_ts_providers/perplexity/index.js +1 -1
  60. package/dist_ts_providers/perplexity/plugins.d.ts +1 -1
  61. package/dist_ts_providers/xai/index.d.ts +1 -1
  62. package/dist_ts_providers/xai/index.js +1 -1
  63. package/dist_ts_providers/xai/plugins.d.ts +1 -1
  64. package/package.json +14 -2
  65. package/readme.md +9 -0
  66. package/ts_anthropic_messages/errors.ts +124 -0
  67. package/ts_anthropic_messages/index.ts +24 -0
  68. package/ts_anthropic_messages/interfaces.ts +173 -0
  69. package/ts_anthropic_messages/openai.ts +58 -0
  70. package/ts_anthropic_messages/plugins.ts +26 -0
  71. package/ts_anthropic_messages/readme.md +129 -0
  72. package/ts_anthropic_messages/request.ts +652 -0
  73. package/ts_anthropic_messages/service.ts +312 -0
  74. package/ts_anthropic_messages/signature.ts +107 -0
  75. package/ts_anthropic_messages/stream.ts +260 -0
  76. package/ts_anthropic_messages_node/index.ts +99 -0
  77. package/ts_anthropic_messages_node/plugins.ts +7 -0
  78. package/ts_anthropic_messages_node/readme.md +50 -0
  79. package/ts_openai_account/plugins.ts +5 -0
  80. package/ts_openai_account/smartai.providers.interfaces.ts +11 -25
  81. package/ts_openai_account/smartai.providers.openai.ts +16 -135
  82. package/ts_openai_auth/index.ts +4 -2
  83. package/ts_openai_auth/interfaces.ts +1 -1
  84. package/ts_openai_auth/smartai.auth.openai.ts +12 -24
  85. package/ts_openai_auth/smartai.connection.openai.ts +5 -3
  86. package/ts_providers/anthropic/index.ts +1 -1
  87. package/ts_providers/anthropic/plugins.ts +1 -0
  88. package/ts_providers/google/index.ts +1 -1
  89. package/ts_providers/google/plugins.ts +1 -1
  90. package/ts_providers/groq/index.ts +1 -1
  91. package/ts_providers/groq/plugins.ts +1 -1
  92. package/ts_providers/mistral/index.ts +1 -1
  93. package/ts_providers/mistral/plugins.ts +1 -1
  94. package/ts_providers/openai/index.ts +30 -1
  95. package/ts_providers/openai/plugins.ts +5 -3
  96. package/ts_providers/openai/readme.md +60 -7
  97. package/ts_providers/openai/smartai.chatgpt.catalog.ts +319 -0
  98. package/{ts_openai_auth/smartai.limits.openai.ts → ts_providers/openai/smartai.chatgpt.limits.ts} +6 -7
  99. package/ts_providers/openai/smartai.chatgpt.middleware.ts +71 -0
  100. package/ts_providers/openai/smartai.chatgpt.openai.ts +271 -0
  101. package/ts_providers/perplexity/index.ts +1 -1
  102. package/ts_providers/perplexity/plugins.ts +1 -1
  103. package/ts_providers/readme.md +9 -0
  104. package/ts_providers/xai/index.ts +1 -1
  105. package/ts_providers/xai/plugins.ts +1 -1
  106. package/dist_ts_openai_auth/smartai.limits.openai.js +0 -49
  107. package/dist_ts_openai_auth/smartai.middleware.openai.d.ts +0 -6
  108. package/dist_ts_openai_auth/smartai.middleware.openai.js +0 -47
  109. package/ts_openai_auth/smartai.middleware.openai.ts +0 -55
@@ -0,0 +1,652 @@
1
+ import type * as plugins from './plugins.js';
2
+ import { invalidRequest } from './errors.js';
3
+ import type {
4
+ IAnthropicMessagesControls,
5
+ IAnthropicMessagesModel,
6
+ TAnthropicMessagesEffort,
7
+ } from './interfaces.js';
8
+ import type { ThinkingSignatures } from './signature.js';
9
+
10
+ /*
11
+ * Request policy. A field that changes what the model does and has a faithful translation is translated.
12
+ * A field that is only a hint to Anthropic's own service (prompt cache markers, citations, metadata, the
13
+ * service tier, eager tool input streaming) is accepted without effect. Every other field is refused with a
14
+ * 400 that names it, so a capability this service cannot provide never silently disappears.
15
+ */
16
+
17
+ type TParsedFile = {
18
+ kind: 'file';
19
+ mediaType: string;
20
+ data: { type: 'data'; data: string } | { type: 'url'; url: URL };
21
+ };
22
+ type TParsedText = { kind: 'text'; text: string };
23
+ type TParsedToolResult = {
24
+ kind: 'tool_result';
25
+ toolUseId: string;
26
+ isError: boolean;
27
+ content: string | Array<TParsedText | TParsedFile>;
28
+ };
29
+ type TParsedUserBlock = TParsedText | TParsedFile | TParsedToolResult;
30
+ type TParsedAssistantBlock =
31
+ | TParsedText
32
+ | { kind: 'thinking'; thinking: string; signature: string }
33
+ | { kind: 'redacted_thinking' }
34
+ | { kind: 'tool_use'; id: string; name: string; input: unknown };
35
+
36
+ type TParsedMessage =
37
+ | { role: 'system'; text: string }
38
+ | { role: 'user'; blocks: TParsedUserBlock[] }
39
+ | { role: 'assistant'; blocks: TParsedAssistantBlock[] };
40
+
41
+ interface IParsedTool {
42
+ name: string;
43
+ description?: string;
44
+ inputSchema: plugins.LanguageModelV4FunctionTool['inputSchema'];
45
+ strict?: boolean;
46
+ inputExamples?: Array<{ input: plugins.JSONObject }>;
47
+ }
48
+
49
+ type TParsedToolChoice =
50
+ | { type: 'auto' }
51
+ | { type: 'any' }
52
+ | { type: 'none' }
53
+ | { type: 'tool'; name: string };
54
+
55
+ /** A request of the Messages API, checked and reduced to what this service translates. */
56
+ export interface IAnthropicMessagesParsedRequest {
57
+ modelId: string;
58
+ stream: boolean;
59
+ maxTokens: number;
60
+ system: string[];
61
+ messages: TParsedMessage[];
62
+ tools: IParsedTool[];
63
+ toolChoice?: TParsedToolChoice;
64
+ parallelToolCalls: boolean;
65
+ thinking: false | { display: 'summarized' | 'omitted' };
66
+ effort?: TAnthropicMessagesEffort;
67
+ responseFormat?: { type: 'json'; schema: plugins.LanguageModelV4FunctionTool['inputSchema'] };
68
+ temperature?: number;
69
+ topP?: number;
70
+ topK?: number;
71
+ stopSequences?: string[];
72
+ }
73
+
74
+ const efforts: readonly TAnthropicMessagesEffort[] = ['low', 'medium', 'high', 'xhigh', 'max'];
75
+ /** Every effort name either side uses, in ascending order, for fitting one to a model's list. */
76
+ const isEffort = (valueArg: unknown): valueArg is TAnthropicMessagesEffort =>
77
+ efforts.some((effort) => effort === valueArg);
78
+ const effortRanks: readonly string[] = ['none', 'minimal', 'low', 'medium', 'high', 'xhigh', 'max', 'ultra'];
79
+ const toolNamePattern = /^[a-zA-Z0-9_-]{1,128}$/u;
80
+ const imageMediaTypes: ReadonlySet<string> = new Set(['image/jpeg', 'image/png', 'image/gif', 'image/webp']);
81
+
82
+ const isPlainObject = (valueArg: unknown): valueArg is Record<string, unknown> =>
83
+ typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg);
84
+
85
+ const describePath = (pathArg: string, keyArg: string): string => (pathArg ? `${pathArg}.${keyArg}` : keyArg);
86
+
87
+ /** Refuses any key that is neither translated nor a known hint, naming it. */
88
+ const checkKeys = (
89
+ valueArg: Record<string, unknown>,
90
+ pathArg: string,
91
+ translatedArg: readonly string[],
92
+ hintsArg: readonly string[] = [],
93
+ ): void => {
94
+ for (const key of Object.keys(valueArg)) {
95
+ if (!translatedArg.includes(key) && !hintsArg.includes(key)) {
96
+ throw invalidRequest(`${describePath(pathArg, key)}: this field is not supported by this service.`);
97
+ }
98
+ }
99
+ };
100
+
101
+ const requireObject = (valueArg: unknown, pathArg: string): Record<string, unknown> => {
102
+ if (!isPlainObject(valueArg)) throw invalidRequest(`${pathArg}: must be an object.`);
103
+ return valueArg;
104
+ };
105
+
106
+ const requireString = (valueArg: unknown, pathArg: string, allowEmptyArg = true): string => {
107
+ if (typeof valueArg !== 'string' || (!allowEmptyArg && valueArg.length === 0)) {
108
+ throw invalidRequest(`${pathArg}: must be a${allowEmptyArg ? '' : ' non-empty'} string.`);
109
+ }
110
+ return valueArg;
111
+ };
112
+
113
+ const requireArray = (valueArg: unknown, pathArg: string): unknown[] => {
114
+ if (!Array.isArray(valueArg)) throw invalidRequest(`${pathArg}: must be an array.`);
115
+ return valueArg;
116
+ };
117
+
118
+ const optionalNumber = (valueArg: unknown, pathArg: string): number | undefined => {
119
+ if (valueArg === undefined) return undefined;
120
+ if (typeof valueArg !== 'number' || !Number.isFinite(valueArg)) throw invalidRequest(`${pathArg}: must be a number.`);
121
+ return valueArg;
122
+ };
123
+
124
+ const blockHints = ['cache_control', 'citations'];
125
+
126
+ const parseText = (blockArg: Record<string, unknown>, pathArg: string): TParsedText => {
127
+ checkKeys(blockArg, pathArg, ['type', 'text'], blockHints);
128
+ return { kind: 'text', text: requireString(blockArg.text, `${pathArg}.text`) };
129
+ };
130
+
131
+ const parseSource = (
132
+ sourceArg: unknown,
133
+ pathArg: string,
134
+ mediaTypeCheckArg: (mediaTypeArg: string) => boolean,
135
+ ): TParsedFile['data'] & { mediaType?: string } => {
136
+ const source = requireObject(sourceArg, pathArg);
137
+ if (source.type === 'base64') {
138
+ checkKeys(source, pathArg, ['type', 'media_type', 'data']);
139
+ const mediaType = requireString(source.media_type, `${pathArg}.media_type`, false);
140
+ if (!mediaTypeCheckArg(mediaType)) throw invalidRequest(`${pathArg}.media_type: "${mediaType}" is not supported.`);
141
+ return { type: 'data', data: requireString(source.data, `${pathArg}.data`, false), mediaType };
142
+ }
143
+ if (source.type === 'url') {
144
+ checkKeys(source, pathArg, ['type', 'url']);
145
+ let url: URL;
146
+ try {
147
+ url = new URL(requireString(source.url, `${pathArg}.url`, false));
148
+ } catch {
149
+ throw invalidRequest(`${pathArg}.url: must be an absolute URL.`);
150
+ }
151
+ return { type: 'url', url };
152
+ }
153
+ throw invalidRequest(`${pathArg}.type: source type "${String(source.type)}" is not supported.`);
154
+ };
155
+
156
+ const parseImage = (blockArg: Record<string, unknown>, pathArg: string): TParsedFile => {
157
+ checkKeys(blockArg, pathArg, ['type', 'source'], blockHints);
158
+ const { mediaType, ...data } = parseSource(blockArg.source, `${pathArg}.source`, (typeArg) => imageMediaTypes.has(typeArg));
159
+ return { kind: 'file', mediaType: mediaType ?? 'image', data };
160
+ };
161
+
162
+ const parseDocument = (blockArg: Record<string, unknown>, pathArg: string): TParsedFile | TParsedText => {
163
+ checkKeys(blockArg, pathArg, ['type', 'source', 'title', 'context'], blockHints);
164
+ const title = blockArg.title === undefined || blockArg.title === null
165
+ ? undefined
166
+ : requireString(blockArg.title, `${pathArg}.title`);
167
+ const context = blockArg.context === undefined || blockArg.context === null
168
+ ? undefined
169
+ : requireString(blockArg.context, `${pathArg}.context`);
170
+ const source = requireObject(blockArg.source, `${pathArg}.source`);
171
+ if (source.type === 'text') {
172
+ checkKeys(source, `${pathArg}.source`, ['type', 'media_type', 'data']);
173
+ if (source.media_type !== 'text/plain') {
174
+ throw invalidRequest(`${pathArg}.source.media_type: a text document must be "text/plain".`);
175
+ }
176
+ const text = requireString(source.data, `${pathArg}.source.data`);
177
+ return { kind: 'text', text: [title, context, text].filter((entry) => entry !== undefined).join('\n\n') };
178
+ }
179
+ if (title !== undefined || context !== undefined) {
180
+ throw invalidRequest(`${pathArg}: a title or context is supported on text documents only.`);
181
+ }
182
+ const { mediaType, ...data } = parseSource(source, `${pathArg}.source`, (typeArg) => typeArg === 'application/pdf');
183
+ return { kind: 'file', mediaType: mediaType ?? 'application/pdf', data };
184
+ };
185
+
186
+ const parseToolResultContent = (valueArg: unknown, pathArg: string): TParsedToolResult['content'] => {
187
+ if (valueArg === undefined) return '';
188
+ if (typeof valueArg === 'string') return valueArg;
189
+ return requireArray(valueArg, pathArg).map((entry, index) => {
190
+ const path = `${pathArg}[${index}]`;
191
+ const block = requireObject(entry, path);
192
+ switch (block.type) {
193
+ case 'text': return parseText(block, path);
194
+ case 'image': return parseImage(block, path);
195
+ case 'document': return parseDocument(block, path);
196
+ default: throw invalidRequest(`${path}.type: "${String(block.type)}" is not supported in a tool result.`);
197
+ }
198
+ });
199
+ };
200
+
201
+ const parseUserBlock = (valueArg: unknown, pathArg: string): TParsedUserBlock => {
202
+ const block = requireObject(valueArg, pathArg);
203
+ switch (block.type) {
204
+ case 'text': return parseText(block, pathArg);
205
+ case 'image': return parseImage(block, pathArg);
206
+ case 'document': return parseDocument(block, pathArg);
207
+ case 'tool_result': {
208
+ checkKeys(block, pathArg, ['type', 'tool_use_id', 'content', 'is_error'], blockHints);
209
+ if (block.is_error !== undefined && typeof block.is_error !== 'boolean') {
210
+ throw invalidRequest(`${pathArg}.is_error: must be a boolean.`);
211
+ }
212
+ return {
213
+ kind: 'tool_result',
214
+ toolUseId: requireString(block.tool_use_id, `${pathArg}.tool_use_id`, false),
215
+ isError: block.is_error === true,
216
+ content: parseToolResultContent(block.content, `${pathArg}.content`),
217
+ };
218
+ }
219
+ default: throw invalidRequest(`${pathArg}.type: "${String(block.type)}" is not supported in a user message.`);
220
+ }
221
+ };
222
+
223
+ const parseAssistantBlock = (valueArg: unknown, pathArg: string): TParsedAssistantBlock => {
224
+ const block = requireObject(valueArg, pathArg);
225
+ switch (block.type) {
226
+ case 'text': return parseText(block, pathArg);
227
+ case 'thinking':
228
+ checkKeys(block, pathArg, ['type', 'thinking', 'signature'], blockHints);
229
+ return {
230
+ kind: 'thinking',
231
+ thinking: requireString(block.thinking, `${pathArg}.thinking`),
232
+ signature: requireString(block.signature, `${pathArg}.signature`),
233
+ };
234
+ case 'redacted_thinking':
235
+ checkKeys(block, pathArg, ['type', 'data'], blockHints);
236
+ return { kind: 'redacted_thinking' };
237
+ case 'tool_use': {
238
+ checkKeys(block, pathArg, ['type', 'id', 'name', 'input'], [...blockHints, 'caller']);
239
+ const name = requireString(block.name, `${pathArg}.name`, false);
240
+ if (!toolNamePattern.test(name)) throw invalidRequest(`${pathArg}.name: "${name}" is not a valid tool name.`);
241
+ return {
242
+ kind: 'tool_use',
243
+ id: requireString(block.id, `${pathArg}.id`, false),
244
+ name,
245
+ input: block.input ?? {},
246
+ };
247
+ }
248
+ default: throw invalidRequest(`${pathArg}.type: "${String(block.type)}" is not supported in an assistant message.`);
249
+ }
250
+ };
251
+
252
+ const parseSystemText = (valueArg: unknown, pathArg: string): string[] => {
253
+ if (typeof valueArg === 'string') return [valueArg];
254
+ return requireArray(valueArg, pathArg).map((entry, index) => {
255
+ const path = `${pathArg}[${index}]`;
256
+ const block = requireObject(entry, path);
257
+ if (block.type !== 'text') throw invalidRequest(`${path}.type: system content must be text.`);
258
+ return parseText(block, path).text;
259
+ });
260
+ };
261
+
262
+ const parseMessage = (valueArg: unknown, pathArg: string): TParsedMessage => {
263
+ const message = requireObject(valueArg, pathArg);
264
+ checkKeys(message, pathArg, ['role', 'content']);
265
+ const content = typeof message.content === 'string'
266
+ ? [{ type: 'text', text: message.content }]
267
+ : requireArray(message.content, `${pathArg}.content`);
268
+ switch (message.role) {
269
+ case 'system':
270
+ return { role: 'system', text: parseSystemText(message.content, `${pathArg}.content`).join('\n') };
271
+ case 'user':
272
+ return { role: 'user', blocks: content.map((entry, index) => parseUserBlock(entry, `${pathArg}.content[${index}]`)) };
273
+ case 'assistant':
274
+ return {
275
+ role: 'assistant',
276
+ blocks: content.map((entry, index) => parseAssistantBlock(entry, `${pathArg}.content[${index}]`)),
277
+ };
278
+ default:
279
+ throw invalidRequest(`${pathArg}.role: "${String(message.role)}" is not a message role.`);
280
+ }
281
+ };
282
+
283
+ const isJsonSchemaObject = (
284
+ valueArg: unknown,
285
+ ): valueArg is plugins.LanguageModelV4FunctionTool['inputSchema'] => isPlainObject(valueArg);
286
+
287
+ const parseTool = (valueArg: unknown, pathArg: string): IParsedTool => {
288
+ const tool = requireObject(valueArg, pathArg);
289
+ if (tool.type !== undefined && tool.type !== 'custom') {
290
+ // The wording names the tool type after `Input tag`, which is how an Anthropic client recognizes a
291
+ // server tool the endpoint does not offer and continues without it.
292
+ throw invalidRequest(`${pathArg}: Input tag '${String(tool.type)}' found using 'type' does not match any of the expected tags: 'custom'`);
293
+ }
294
+ if (tool.defer_loading === true) {
295
+ throw invalidRequest(`${pathArg}.defer_loading: deferred tool loading (tool search) is not supported by this service.`);
296
+ }
297
+ checkKeys(tool, pathArg, ['type', 'name', 'description', 'input_schema', 'strict', 'input_examples', 'defer_loading'], [
298
+ 'cache_control',
299
+ 'eager_input_streaming',
300
+ ]);
301
+ const name = requireString(tool.name, `${pathArg}.name`, false);
302
+ if (!toolNamePattern.test(name)) throw invalidRequest(`${pathArg}.name: "${name}" is not a valid tool name.`);
303
+ if (!isJsonSchemaObject(tool.input_schema)) throw invalidRequest(`${pathArg}.input_schema: must be a JSON schema object.`);
304
+ if (tool.strict !== undefined && typeof tool.strict !== 'boolean') throw invalidRequest(`${pathArg}.strict: must be a boolean.`);
305
+ const inputExamples = tool.input_examples === undefined
306
+ ? undefined
307
+ : requireArray(tool.input_examples, `${pathArg}.input_examples`).map((example, index) => {
308
+ if (!isPlainObject(example)) throw invalidRequest(`${pathArg}.input_examples[${index}]: must be an object.`);
309
+ return { input: example as plugins.JSONObject };
310
+ });
311
+ return {
312
+ name,
313
+ ...(tool.description === undefined ? {} : { description: requireString(tool.description, `${pathArg}.description`) }),
314
+ inputSchema: tool.input_schema,
315
+ ...(tool.strict === undefined ? {} : { strict: tool.strict }),
316
+ ...(inputExamples === undefined ? {} : { inputExamples }),
317
+ };
318
+ };
319
+
320
+ const parseToolChoice = (valueArg: unknown): { choice?: TParsedToolChoice; parallelToolCalls: boolean } => {
321
+ if (valueArg === undefined) return { parallelToolCalls: true };
322
+ const choice = requireObject(valueArg, 'tool_choice');
323
+ checkKeys(choice, 'tool_choice', ['type', 'name', 'disable_parallel_tool_use']);
324
+ if (choice.disable_parallel_tool_use !== undefined && typeof choice.disable_parallel_tool_use !== 'boolean') {
325
+ throw invalidRequest('tool_choice.disable_parallel_tool_use: must be a boolean.');
326
+ }
327
+ const parallelToolCalls = choice.disable_parallel_tool_use !== true;
328
+ switch (choice.type) {
329
+ case 'auto':
330
+ case 'any':
331
+ case 'none':
332
+ if (choice.name !== undefined) throw invalidRequest(`tool_choice.name: only a "tool" choice names a tool.`);
333
+ return { choice: { type: choice.type }, parallelToolCalls };
334
+ case 'tool':
335
+ return { choice: { type: 'tool', name: requireString(choice.name, 'tool_choice.name', false) }, parallelToolCalls };
336
+ default:
337
+ throw invalidRequest(`tool_choice.type: "${String(choice.type)}" is not a tool choice.`);
338
+ }
339
+ };
340
+
341
+ const parseThinking = (valueArg: unknown): IAnthropicMessagesParsedRequest['thinking'] => {
342
+ if (valueArg === undefined) return false;
343
+ const thinking = requireObject(valueArg, 'thinking');
344
+ const display = thinking.display ?? 'summarized';
345
+ if (display !== 'summarized' && display !== 'omitted') {
346
+ throw invalidRequest(`thinking.display: "${String(display)}" is not supported.`);
347
+ }
348
+ switch (thinking.type) {
349
+ case 'disabled':
350
+ checkKeys(thinking, 'thinking', ['type']);
351
+ return false;
352
+ case 'adaptive':
353
+ checkKeys(thinking, 'thinking', ['type', 'display']);
354
+ return { display };
355
+ case 'enabled':
356
+ // The reasoning depth of the model is set by effort; a token budget has no equivalent to translate to.
357
+ checkKeys(thinking, 'thinking', ['type', 'display'], ['budget_tokens']);
358
+ return { display };
359
+ default:
360
+ throw invalidRequest(`thinking.type: "${String(thinking.type)}" is not supported.`);
361
+ }
362
+ };
363
+
364
+ const parseOutputConfig = (valueArg: unknown): Pick<IAnthropicMessagesParsedRequest, 'effort' | 'responseFormat'> => {
365
+ if (valueArg === undefined) return {};
366
+ const config = requireObject(valueArg, 'output_config');
367
+ checkKeys(config, 'output_config', ['effort', 'format']);
368
+ const effort = config.effort;
369
+ if (effort !== undefined && !isEffort(effort)) {
370
+ throw invalidRequest(`output_config.effort: "${String(effort)}" is not an effort level.`);
371
+ }
372
+ let responseFormat: IAnthropicMessagesParsedRequest['responseFormat'];
373
+ if (config.format !== undefined && config.format !== null) {
374
+ const format = requireObject(config.format, 'output_config.format');
375
+ checkKeys(format, 'output_config.format', ['type', 'schema']);
376
+ if (format.type !== 'json_schema' || !isJsonSchemaObject(format.schema)) {
377
+ throw invalidRequest('output_config.format: only a "json_schema" format with a schema is supported.');
378
+ }
379
+ responseFormat = { type: 'json', schema: format.schema };
380
+ }
381
+ return {
382
+ ...(isEffort(effort) ? { effort } : {}),
383
+ ...(responseFormat ? { responseFormat } : {}),
384
+ };
385
+ };
386
+
387
+ const refusedFeatures: Readonly<Record<string, string>> = {
388
+ context_management: 'server-side context management',
389
+ container: 'code execution containers',
390
+ mcp_servers: 'remote MCP servers',
391
+ };
392
+
393
+ /** Checks a Messages API request body and reduces it to what this service translates. */
394
+ export const parseAnthropicMessagesRequest = (bodyArg: unknown): IAnthropicMessagesParsedRequest => {
395
+ const body = requireObject(bodyArg, 'body');
396
+ for (const [key, feature] of Object.entries(refusedFeatures)) {
397
+ if (body[key] !== undefined) throw invalidRequest(`${key}: ${feature} is not supported by this service.`);
398
+ }
399
+ checkKeys(body, '', [
400
+ 'model', 'messages', 'max_tokens', 'system', 'stream', 'tools', 'tool_choice', 'thinking', 'output_config',
401
+ 'temperature', 'top_p', 'top_k', 'stop_sequences',
402
+ ], ['metadata', 'service_tier', 'cache_control']);
403
+ const maxTokens = body.max_tokens;
404
+ if (typeof maxTokens !== 'number' || !Number.isSafeInteger(maxTokens) || maxTokens < 1) {
405
+ throw invalidRequest('max_tokens: must be a positive integer.');
406
+ }
407
+ if (body.stream !== undefined && typeof body.stream !== 'boolean') throw invalidRequest('stream: must be a boolean.');
408
+ const messages = requireArray(body.messages, 'messages').map((entry, index) => parseMessage(entry, `messages[${index}]`));
409
+ if (messages.length === 0) throw invalidRequest('messages: at least one message is required.');
410
+ const tools = body.tools === undefined
411
+ ? []
412
+ : requireArray(body.tools, 'tools').map((entry, index) => parseTool(entry, `tools[${index}]`));
413
+ if (new Set(tools.map((tool) => tool.name)).size !== tools.length) throw invalidRequest('tools: tool names must be unique.');
414
+ const { choice, parallelToolCalls } = parseToolChoice(body.tool_choice);
415
+ if (choice?.type === 'tool' && !tools.some((tool) => tool.name === choice.name)) {
416
+ throw invalidRequest(`tool_choice.name: no tool is named "${choice.name}".`);
417
+ }
418
+ const stopSequences = body.stop_sequences === undefined
419
+ ? undefined
420
+ : requireArray(body.stop_sequences, 'stop_sequences').map((entry, index) => requireString(entry, `stop_sequences[${index}]`, false));
421
+ const topK = optionalNumber(body.top_k, 'top_k');
422
+ return {
423
+ modelId: requireString(body.model, 'model', false),
424
+ stream: body.stream === true,
425
+ maxTokens,
426
+ system: body.system === undefined ? [] : parseSystemText(body.system, 'system'),
427
+ messages,
428
+ tools,
429
+ ...(choice ? { toolChoice: choice } : {}),
430
+ parallelToolCalls,
431
+ thinking: parseThinking(body.thinking),
432
+ ...parseOutputConfig(body.output_config),
433
+ ...(body.temperature === undefined ? {} : { temperature: optionalNumber(body.temperature, 'temperature') }),
434
+ ...(body.top_p === undefined ? {} : { topP: optionalNumber(body.top_p, 'top_p') }),
435
+ ...(topK === undefined ? {} : { topK }),
436
+ ...(stopSequences === undefined || stopSequences.length === 0 ? {} : { stopSequences }),
437
+ };
438
+ };
439
+
440
+ /** Maps tool names to the names the model is sent, for a model with a shorter tool name limit. */
441
+ export class ToolNames {
442
+ private readonly toModel = new Map<string, string>();
443
+ private readonly fromModel = new Map<string, string>();
444
+
445
+ public static async create(namesArg: Iterable<string>, limitArg: number | undefined): Promise<ToolNames> {
446
+ const names = new ToolNames();
447
+ const unique = [...new Set(namesArg)];
448
+ for (const name of unique) {
449
+ let sent = name;
450
+ if (limitArg !== undefined && name.length > limitArg) {
451
+ if (limitArg < 16) throw invalidRequest(`tools: the model's tool name limit of ${limitArg} is too short to alias "${name}".`);
452
+ const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', new TextEncoder().encode(name)));
453
+ const suffix = [...digest.subarray(0, 4)].map((byte) => byte.toString(16).padStart(2, '0')).join('');
454
+ sent = `${name.slice(0, limitArg - suffix.length - 1)}_${suffix}`;
455
+ }
456
+ if (names.fromModel.has(sent)) {
457
+ throw invalidRequest(`tools: "${name}" and "${names.fromModel.get(sent)}" share the name the model is sent.`);
458
+ }
459
+ names.toModel.set(name, sent);
460
+ names.fromModel.set(sent, name);
461
+ }
462
+ return names;
463
+ }
464
+
465
+ public sent(nameArg: string): string {
466
+ const sent = this.toModel.get(nameArg);
467
+ if (sent === undefined) throw new Error(`Tool name "${nameArg}" was not registered.`);
468
+ return sent;
469
+ }
470
+
471
+ /** The client's name for a tool the model called, or `undefined` for a name the request never offered. */
472
+ public received(nameArg: string): string | undefined {
473
+ return this.fromModel.get(nameArg);
474
+ }
475
+ }
476
+
477
+ /** Fits a requested effort to the efforts a model accepts, preferring the nearest lower one. */
478
+ const fitEffort = (
479
+ requestedArg: string,
480
+ acceptedArg: readonly string[] | undefined,
481
+ ): string | undefined => {
482
+ if (!acceptedArg || acceptedArg.includes(requestedArg)) return requestedArg;
483
+ const requestedRank = effortRanks.indexOf(requestedArg);
484
+ const ranked = acceptedArg
485
+ .map((effort) => ({ effort, rank: effortRanks.indexOf(effort) }))
486
+ .filter((entry) => entry.rank !== -1)
487
+ .sort((left, right) => left.rank - right.rank);
488
+ const lower = ranked.filter((entry) => entry.rank <= requestedRank);
489
+ return (lower.at(-1) ?? ranked[0])?.effort;
490
+ };
491
+
492
+ export interface IAnthropicMessagesModelCall {
493
+ options: Omit<plugins.LanguageModelV4CallOptions, 'abortSignal'>;
494
+ toolNames: ToolNames;
495
+ /** Response headers that name what this request received differently from what it asked for. */
496
+ notices: Record<string, string>;
497
+ }
498
+
499
+ const toFilePart = (fileArg: TParsedFile): plugins.LanguageModelV4FilePart => ({
500
+ type: 'file',
501
+ mediaType: fileArg.mediaType,
502
+ data: fileArg.data,
503
+ });
504
+
505
+ const toToolResultOutput = (
506
+ resultArg: TParsedToolResult,
507
+ pathArg: string,
508
+ ): plugins.LanguageModelV4ToolResultOutput => {
509
+ if (typeof resultArg.content === 'string') {
510
+ return resultArg.isError ? { type: 'error-text', value: resultArg.content } : { type: 'text', value: resultArg.content };
511
+ }
512
+ if (resultArg.isError) {
513
+ const texts = resultArg.content.filter((entry): entry is TParsedText => entry.kind === 'text');
514
+ if (texts.length !== resultArg.content.length) {
515
+ throw invalidRequest(`${pathArg}: a tool result marked as an error can carry text only.`);
516
+ }
517
+ return { type: 'error-text', value: texts.map((entry) => entry.text).join('\n') };
518
+ }
519
+ return {
520
+ type: 'content',
521
+ value: resultArg.content.map((entry) => entry.kind === 'text'
522
+ ? { type: 'text' as const, text: entry.text }
523
+ : { type: 'file' as const, mediaType: entry.mediaType, data: entry.data }),
524
+ };
525
+ };
526
+
527
+ /** Translates a parsed request into the call of one model. */
528
+ export const createAnthropicMessagesModelCall = async (
529
+ requestArg: IAnthropicMessagesParsedRequest,
530
+ modelArg: IAnthropicMessagesModel,
531
+ signaturesArg: ThinkingSignatures,
532
+ sessionIdArg: string | undefined,
533
+ ): Promise<IAnthropicMessagesModelCall> => {
534
+ const notices: Record<string, string> = {};
535
+ const toolNamesInUse: string[] = requestArg.tools.map((tool) => tool.name);
536
+ const toolUseNames = new Map<string, string>();
537
+ for (const message of requestArg.messages) {
538
+ if (message.role !== 'assistant') continue;
539
+ for (const block of message.blocks) {
540
+ if (block.kind !== 'tool_use') continue;
541
+ toolNamesInUse.push(block.name);
542
+ toolUseNames.set(block.id, block.name);
543
+ }
544
+ }
545
+ const toolNames = await ToolNames.create(toolNamesInUse, modelArg.toolNameLimit);
546
+
547
+ const prompt: plugins.LanguageModelV4Prompt = requestArg.system.map((text) => ({ role: 'system', content: text }));
548
+ let droppedThinking = 0;
549
+ for (const [messageIndex, message] of requestArg.messages.entries()) {
550
+ if (message.role === 'system') {
551
+ prompt.push({ role: 'system', content: message.text });
552
+ continue;
553
+ }
554
+ if (message.role === 'assistant') {
555
+ const content: Extract<plugins.LanguageModelV4Message, { role: 'assistant' }>['content'] = [];
556
+ for (const block of message.blocks) {
557
+ if (block.kind === 'text') {
558
+ if (block.text.length > 0) content.push({ type: 'text', text: block.text });
559
+ } else if (block.kind === 'thinking') {
560
+ const providerOptions = await signaturesArg.verify(block.signature, modelArg.signatureScope);
561
+ if (providerOptions) content.push({ type: 'reasoning', text: block.thinking, providerOptions });
562
+ else droppedThinking++;
563
+ } else if (block.kind === 'redacted_thinking') {
564
+ droppedThinking++;
565
+ } else {
566
+ content.push({ type: 'tool-call', toolCallId: block.id, toolName: toolNames.sent(block.name), input: block.input });
567
+ }
568
+ }
569
+ if (content.length > 0) prompt.push({ role: 'assistant', content });
570
+ continue;
571
+ }
572
+ // A user message's tool results answer the preceding tool calls, so each run of them becomes a tool
573
+ // message and every other run of content a user message, in the order the client wrote them.
574
+ let toolParts: plugins.LanguageModelV4ToolResultPart[] = [];
575
+ let userParts: Array<plugins.LanguageModelV4TextPart | plugins.LanguageModelV4FilePart> = [];
576
+ const flushTool = (): void => {
577
+ if (toolParts.length > 0) prompt.push({ role: 'tool', content: toolParts });
578
+ toolParts = [];
579
+ };
580
+ const flushUser = (): void => {
581
+ if (userParts.length > 0) prompt.push({ role: 'user', content: userParts });
582
+ userParts = [];
583
+ };
584
+ for (const [blockIndex, block] of message.blocks.entries()) {
585
+ if (block.kind === 'tool_result') {
586
+ flushUser();
587
+ const name = toolUseNames.get(block.toolUseId);
588
+ const path = `messages[${messageIndex}].content[${blockIndex}]`;
589
+ if (name === undefined) throw invalidRequest(`${path}.tool_use_id: no tool_use block has the id "${block.toolUseId}".`);
590
+ toolParts.push({
591
+ type: 'tool-result',
592
+ toolCallId: block.toolUseId,
593
+ toolName: toolNames.sent(name),
594
+ output: toToolResultOutput(block, path),
595
+ });
596
+ continue;
597
+ }
598
+ flushTool();
599
+ userParts.push(block.kind === 'text' ? { type: 'text', text: block.text } : toFilePart(block));
600
+ }
601
+ flushTool();
602
+ flushUser();
603
+ }
604
+ if (droppedThinking > 0) notices['x-flexharness-thinking-dropped'] = String(droppedThinking);
605
+
606
+ let reasoningEffort: string | undefined;
607
+ if (requestArg.effort !== undefined) {
608
+ reasoningEffort = fitEffort(requestArg.effort, modelArg.reasoningEfforts);
609
+ if (reasoningEffort !== requestArg.effort) {
610
+ notices['x-flexharness-effort-fitted'] = `${requestArg.effort}=>${reasoningEffort ?? 'model-default'}`;
611
+ }
612
+ }
613
+ if (!modelArg.sendsMaxTokens) notices['x-flexharness-max-tokens'] = 'not-sent';
614
+
615
+ const controls: IAnthropicMessagesControls = {
616
+ ...(reasoningEffort === undefined ? {} : { reasoningEffort }),
617
+ thinking: requestArg.thinking !== false,
618
+ parallelToolCalls: requestArg.parallelToolCalls,
619
+ ...(sessionIdArg ? { sessionId: sessionIdArg } : {}),
620
+ };
621
+ const toolChoice: plugins.LanguageModelV4ToolChoice | undefined = requestArg.toolChoice === undefined
622
+ ? undefined
623
+ : requestArg.toolChoice.type === 'tool'
624
+ ? { type: 'tool', toolName: toolNames.sent(requestArg.toolChoice.name) }
625
+ : { type: requestArg.toolChoice.type === 'any' ? 'required' : requestArg.toolChoice.type };
626
+ return {
627
+ toolNames,
628
+ notices,
629
+ options: {
630
+ prompt,
631
+ ...(modelArg.sendsMaxTokens ? { maxOutputTokens: requestArg.maxTokens } : {}),
632
+ ...(requestArg.temperature === undefined ? {} : { temperature: requestArg.temperature }),
633
+ ...(requestArg.topP === undefined ? {} : { topP: requestArg.topP }),
634
+ ...(requestArg.topK === undefined ? {} : { topK: requestArg.topK }),
635
+ ...(requestArg.stopSequences === undefined ? {} : { stopSequences: requestArg.stopSequences }),
636
+ ...(requestArg.responseFormat === undefined ? {} : { responseFormat: requestArg.responseFormat }),
637
+ ...(requestArg.tools.length === 0 ? {} : {
638
+ tools: requestArg.tools.map((tool) => ({
639
+ type: 'function' as const,
640
+ name: toolNames.sent(tool.name),
641
+ ...(tool.description === undefined ? {} : { description: tool.description }),
642
+ inputSchema: tool.inputSchema,
643
+ ...(tool.strict === undefined ? {} : { strict: tool.strict }),
644
+ ...(tool.inputExamples === undefined ? {} : { inputExamples: tool.inputExamples }),
645
+ })),
646
+ }),
647
+ ...(toolChoice === undefined ? {} : { toolChoice }),
648
+ providerOptions: modelArg.providerOptions(controls),
649
+ },
650
+ };
651
+ };
652
+