@modelprofile.com/flexharness-providers 9.1.0 → 9.3.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 (136) 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_media_openai/index.js +2 -3
  24. package/dist_ts_media_openai/plugins.d.ts +2 -0
  25. package/dist_ts_media_openai/plugins.js +5 -1
  26. package/dist_ts_openai_account/index.d.ts +1 -1
  27. package/dist_ts_openai_account/index.js +2 -2
  28. package/dist_ts_openai_account/plugins.d.ts +3 -0
  29. package/dist_ts_openai_account/plugins.js +3 -1
  30. package/dist_ts_openai_account/smartai.providers.credentials.d.ts +2 -2
  31. package/dist_ts_openai_account/smartai.providers.credentials.js +3 -4
  32. package/dist_ts_openai_account/smartai.providers.interfaces.d.ts +6 -7
  33. package/dist_ts_openai_account/smartai.providers.openai.js +15 -16
  34. package/dist_ts_openai_auth/index.d.ts +1 -1
  35. package/dist_ts_openai_auth/index.js +2 -2
  36. package/dist_ts_openai_auth/interfaces.d.ts +1 -1
  37. package/dist_ts_openai_auth/plugins.d.ts +3 -0
  38. package/dist_ts_openai_auth/plugins.js +4 -0
  39. package/dist_ts_openai_auth/smartai.auth.openai.d.ts +1 -2
  40. package/dist_ts_openai_auth/smartai.auth.openai.js +27 -27
  41. package/dist_ts_openai_auth/smartai.connection.openai.d.ts +3 -3
  42. package/dist_ts_openai_auth/smartai.connection.openai.js +6 -6
  43. package/dist_ts_openai_auth_files/index.d.ts +6 -6
  44. package/dist_ts_openai_auth_files/index.js +3 -4
  45. package/dist_ts_openai_auth_files/plugins.d.ts +3 -0
  46. package/dist_ts_openai_auth_files/plugins.js +4 -1
  47. package/dist_ts_providers/anthropic/index.d.ts +1 -1
  48. package/dist_ts_providers/anthropic/index.js +1 -1
  49. package/dist_ts_providers/anthropic/plugins.d.ts +4 -3
  50. package/dist_ts_providers/anthropic/plugins.js +3 -3
  51. package/dist_ts_providers/anthropic/smartai.middleware.anthropic.d.ts +2 -3
  52. package/dist_ts_providers/anthropic/smartai.middleware.anthropic.js +3 -3
  53. package/dist_ts_providers/google/index.d.ts +1 -1
  54. package/dist_ts_providers/google/index.js +1 -1
  55. package/dist_ts_providers/google/plugins.d.ts +1 -1
  56. package/dist_ts_providers/groq/index.d.ts +1 -1
  57. package/dist_ts_providers/groq/index.js +1 -1
  58. package/dist_ts_providers/groq/plugins.d.ts +1 -1
  59. package/dist_ts_providers/mistral/index.d.ts +1 -1
  60. package/dist_ts_providers/mistral/index.js +1 -1
  61. package/dist_ts_providers/mistral/plugins.d.ts +1 -1
  62. package/dist_ts_providers/ollama/index.d.ts +2 -2
  63. package/dist_ts_providers/ollama/index.js +1 -1
  64. package/dist_ts_providers/ollama/interfaces.d.ts +2 -2
  65. package/dist_ts_providers/ollama/plugins.d.ts +2 -0
  66. package/dist_ts_providers/ollama/plugins.js +2 -0
  67. package/dist_ts_providers/ollama/smartai.provider.ollama.d.ts +2 -2
  68. package/dist_ts_providers/ollama/smartai.provider.ollama.js +1 -1
  69. package/dist_ts_providers/openai/index.d.ts +1 -1
  70. package/dist_ts_providers/openai/index.js +1 -1
  71. package/dist_ts_providers/openai/plugins.d.ts +1 -1
  72. package/dist_ts_providers/openai/smartai.chatgpt.catalog.d.ts +2 -0
  73. package/dist_ts_providers/openai/smartai.chatgpt.catalog.js +8 -1
  74. package/dist_ts_providers/openai/smartai.chatgpt.middleware.d.ts +5 -1
  75. package/dist_ts_providers/openai/smartai.chatgpt.middleware.js +25 -12
  76. package/dist_ts_providers/openai/smartai.limits.openai.d.ts +4 -1
  77. package/dist_ts_providers/openai/smartai.limits.openai.js +10 -5
  78. package/dist_ts_providers/perplexity/index.d.ts +1 -1
  79. package/dist_ts_providers/perplexity/index.js +1 -1
  80. package/dist_ts_providers/perplexity/plugins.d.ts +1 -1
  81. package/dist_ts_providers/xai/index.d.ts +1 -1
  82. package/dist_ts_providers/xai/index.js +1 -1
  83. package/dist_ts_providers/xai/plugins.d.ts +1 -1
  84. package/package.json +23 -11
  85. package/readme.md +1 -0
  86. package/ts_anthropic_messages/errors.ts +124 -0
  87. package/ts_anthropic_messages/index.ts +24 -0
  88. package/ts_anthropic_messages/interfaces.ts +173 -0
  89. package/ts_anthropic_messages/openai.ts +58 -0
  90. package/ts_anthropic_messages/plugins.ts +26 -0
  91. package/ts_anthropic_messages/readme.md +129 -0
  92. package/ts_anthropic_messages/request.ts +652 -0
  93. package/ts_anthropic_messages/service.ts +312 -0
  94. package/ts_anthropic_messages/signature.ts +107 -0
  95. package/ts_anthropic_messages/stream.ts +260 -0
  96. package/ts_anthropic_messages_node/index.ts +99 -0
  97. package/ts_anthropic_messages_node/plugins.ts +7 -0
  98. package/ts_anthropic_messages_node/readme.md +50 -0
  99. package/ts_media_openai/index.ts +1 -2
  100. package/ts_media_openai/plugins.ts +6 -0
  101. package/ts_openai_account/index.ts +1 -1
  102. package/ts_openai_account/plugins.ts +37 -0
  103. package/ts_openai_account/smartai.providers.credentials.ts +3 -5
  104. package/ts_openai_account/smartai.providers.interfaces.ts +6 -12
  105. package/ts_openai_account/smartai.providers.openai.ts +18 -31
  106. package/ts_openai_auth/index.ts +1 -1
  107. package/ts_openai_auth/interfaces.ts +1 -1
  108. package/ts_openai_auth/plugins.ts +17 -0
  109. package/ts_openai_auth/smartai.auth.openai.ts +28 -33
  110. package/ts_openai_auth/smartai.connection.openai.ts +7 -11
  111. package/ts_openai_auth_files/index.ts +19 -28
  112. package/ts_openai_auth_files/plugins.ts +10 -0
  113. package/ts_providers/anthropic/index.ts +1 -1
  114. package/ts_providers/anthropic/plugins.ts +11 -1
  115. package/ts_providers/anthropic/smartai.middleware.anthropic.ts +3 -5
  116. package/ts_providers/google/index.ts +1 -1
  117. package/ts_providers/google/plugins.ts +1 -1
  118. package/ts_providers/groq/index.ts +1 -1
  119. package/ts_providers/groq/plugins.ts +1 -1
  120. package/ts_providers/mistral/index.ts +1 -1
  121. package/ts_providers/mistral/plugins.ts +1 -1
  122. package/ts_providers/ollama/index.ts +2 -2
  123. package/ts_providers/ollama/interfaces.ts +2 -2
  124. package/ts_providers/ollama/plugins.ts +12 -0
  125. package/ts_providers/ollama/smartai.provider.ollama.ts +11 -21
  126. package/ts_providers/openai/index.ts +1 -1
  127. package/ts_providers/openai/plugins.ts +1 -0
  128. package/ts_providers/openai/readme.md +9 -6
  129. package/ts_providers/openai/smartai.chatgpt.catalog.ts +11 -0
  130. package/ts_providers/openai/smartai.chatgpt.middleware.ts +27 -11
  131. package/ts_providers/openai/smartai.limits.openai.ts +12 -4
  132. package/ts_providers/perplexity/index.ts +1 -1
  133. package/ts_providers/perplexity/plugins.ts +1 -1
  134. package/ts_providers/readme.md +1 -0
  135. package/ts_providers/xai/index.ts +1 -1
  136. package/ts_providers/xai/plugins.ts +1 -1
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Task Venture Capital GmbH",
3
3
  "license": "MIT",
4
4
  "name": "@modelprofile.com/flexharness-providers",
5
- "version": "9.1.0",
5
+ "version": "9.3.0",
6
6
  "type": "module",
7
7
  "description": "Model providers, authentication, account management, media and research integrations in one package.",
8
8
  "main": "./dist_ts_providers/index.js",
@@ -63,20 +63,28 @@
63
63
  "./research": {
64
64
  "types": "./dist_ts_research/index.d.ts",
65
65
  "import": "./dist_ts_research/index.js"
66
+ },
67
+ "./anthropic-messages": {
68
+ "types": "./dist_ts_anthropic_messages/index.d.ts",
69
+ "import": "./dist_ts_anthropic_messages/index.js"
70
+ },
71
+ "./anthropic-messages/node": {
72
+ "types": "./dist_ts_anthropic_messages_node/index.d.ts",
73
+ "import": "./dist_ts_anthropic_messages_node/index.js"
66
74
  }
67
75
  },
68
76
  "dependencies": {
69
- "@ai-sdk/anthropic": "^4.0.42",
70
- "@ai-sdk/google": "^4.0.51",
71
- "@ai-sdk/groq": "^4.0.31",
72
- "@ai-sdk/mistral": "^4.0.33",
73
- "@ai-sdk/openai": "^4.0.47",
74
- "@ai-sdk/perplexity": "^4.0.32",
75
- "@ai-sdk/provider": "^4.0.8",
76
- "@ai-sdk/xai": "^4.0.44",
77
+ "@ai-sdk/anthropic": "^4.0.58",
78
+ "@ai-sdk/google": "^4.0.76",
79
+ "@ai-sdk/groq": "^4.0.46",
80
+ "@ai-sdk/mistral": "^4.0.48",
81
+ "@ai-sdk/openai": "^4.0.71",
82
+ "@ai-sdk/perplexity": "^4.0.48",
83
+ "@ai-sdk/provider": "^4.0.17",
84
+ "@ai-sdk/xai": "^4.0.59",
77
85
  "@anthropic-ai/sdk": "0.95.2",
78
- "@modelprofile.com/flexharness-models": "9.1.0",
79
- "ai": "^7.0.79",
86
+ "@modelprofile.com/flexharness-models": "9.3.0",
87
+ "ai": "^7.0.107",
80
88
  "openai": "^6.37.0",
81
89
  "proper-lockfile": "^4.1.2"
82
90
  },
@@ -97,6 +105,10 @@
97
105
  "dist_ts_media_openai",
98
106
  "ts_research",
99
107
  "dist_ts_research",
108
+ "ts_anthropic_messages",
109
+ "dist_ts_anthropic_messages",
110
+ "ts_anthropic_messages_node",
111
+ "dist_ts_anthropic_messages_node",
100
112
  "openai-codex-license.txt",
101
113
  "third-party-notices.md",
102
114
  "readme.md",
package/readme.md CHANGED
@@ -31,6 +31,7 @@ factories; use a provider subpath to import just that implementation.
31
31
  | `/auth/files` | Explicit interoperability with external credential files |
32
32
  | `/media` | OpenAI audio and image generation |
33
33
  | `/research` | Anthropic research utilities |
34
+ | `/anthropic-messages`, `/anthropic-messages/node` | Serves the Anthropic Messages API over FlexHarness models, for Anthropic clients such as Claude Code |
34
35
 
35
36
  Provider SDKs are installed together. Authentication, file access and vendor media
36
37
  are separate entrypoints and are not imported by the provider factory root. For
@@ -0,0 +1,124 @@
1
+ import * as plugins from './plugins.js';
2
+ import type {
3
+ IAnthropicMessagesFailure,
4
+ IAnthropicMessagesModel,
5
+ TAnthropicMessagesErrorType,
6
+ } from './interfaces.js';
7
+
8
+ /**
9
+ * A request this service refuses, in the Anthropic error shape. Callbacks throw it to answer with their
10
+ * own status and message, for example an `authenticate` that refuses a credential with 401.
11
+ */
12
+ export class AnthropicMessagesError extends Error {
13
+ public readonly failure: Readonly<IAnthropicMessagesFailure>;
14
+
15
+ constructor(
16
+ statusArg: number,
17
+ typeArg: TAnthropicMessagesErrorType,
18
+ messageArg: string,
19
+ detailsArg: { retryAfterSeconds?: number; shouldRetry?: boolean } = {},
20
+ optionsArg?: ErrorOptions,
21
+ ) {
22
+ super(messageArg, optionsArg);
23
+ this.name = 'AnthropicMessagesError';
24
+ this.failure = Object.freeze({ status: statusArg, type: typeArg, message: messageArg, ...detailsArg });
25
+ }
26
+ }
27
+
28
+ export const invalidRequest = (messageArg: string): AnthropicMessagesError =>
29
+ new AnthropicMessagesError(400, 'invalid_request_error', messageArg);
30
+
31
+ /** The one wording an Anthropic client compacts its conversation on. */
32
+ export const promptTooLong = (detailArg: string): IAnthropicMessagesFailure => ({
33
+ status: 400,
34
+ type: 'invalid_request_error',
35
+ message: `prompt is too long: ${detailArg}`,
36
+ });
37
+
38
+ const maximumMessageLength = 1024;
39
+
40
+ const bounded = (textArg: string): string => textArg.length > maximumMessageLength
41
+ ? `${textArg.slice(0, maximumMessageLength)}...`
42
+ : textArg;
43
+
44
+ const isAbort = (errorArg: unknown): boolean =>
45
+ errorArg instanceof Error && (errorArg.name === 'AbortError' || errorArg.name === 'TimeoutError');
46
+
47
+ const fromLimit = (errorArg: unknown): IAnthropicMessagesFailure | undefined => {
48
+ if (!plugins.isModelLimitError(errorArg)) return undefined;
49
+ const limit = errorArg.limit;
50
+ const waitMs = limit.retryAfterMs ?? (limit.resetsAt === undefined ? undefined : limit.resetsAt - Date.now());
51
+ return {
52
+ status: 429,
53
+ type: 'rate_limit_error',
54
+ message: errorArg.message,
55
+ ...(waitMs === undefined ? {} : { retryAfterSeconds: Math.max(1, Math.ceil(waitMs / 1000)) }),
56
+ shouldRetry: limit.kind === 'rate_limit',
57
+ };
58
+ };
59
+
60
+ const fromApiCall = (errorArg: unknown): IAnthropicMessagesFailure | undefined => {
61
+ if (!plugins.APICallError.isInstance(errorArg)) return undefined;
62
+ const status = errorArg.statusCode;
63
+ const detail = bounded(errorArg.message);
64
+ if (status === 400 || status === 422) {
65
+ return { status: 400, type: 'invalid_request_error', message: `The model provider refused the request: ${detail}` };
66
+ }
67
+ if (status === 401 || status === 403) {
68
+ return {
69
+ status: 403,
70
+ type: 'permission_error',
71
+ message: `The model provider refused this account's credentials: ${detail}`,
72
+ shouldRetry: false,
73
+ };
74
+ }
75
+ if (status === 404) {
76
+ return { status: 404, type: 'not_found_error', message: `The model provider does not know this model: ${detail}` };
77
+ }
78
+ if (status === 413) {
79
+ return { status: 413, type: 'request_too_large', message: `The model provider refused the request size: ${detail}` };
80
+ }
81
+ if (status === 429) {
82
+ return { status: 429, type: 'rate_limit_error', message: `The model provider is rate limiting: ${detail}`, shouldRetry: true };
83
+ }
84
+ return {
85
+ status: 500,
86
+ type: 'api_error',
87
+ message: `The model provider failed${status === undefined ? '' : ` with HTTP ${status}`}: ${detail}`,
88
+ shouldRetry: errorArg.isRetryable,
89
+ };
90
+ };
91
+
92
+ /**
93
+ * States a failure of a request in the Anthropic shape: a refusal a callback threw, the model's own dialect,
94
+ * a usage or rate limit, a provider HTTP failure, and anything else as an `api_error` naming the error.
95
+ */
96
+ export const describeFailure = (
97
+ errorArg: unknown,
98
+ modelArg?: IAnthropicMessagesModel,
99
+ ): IAnthropicMessagesFailure => {
100
+ if (errorArg instanceof AnthropicMessagesError) return errorArg.failure;
101
+ return modelArg?.classifyFailure?.(errorArg)
102
+ ?? fromLimit(errorArg)
103
+ ?? fromApiCall(errorArg)
104
+ ?? (isAbort(errorArg)
105
+ ? { status: 499, type: 'api_error', message: 'The request was aborted.' }
106
+ : {
107
+ status: 500,
108
+ type: 'api_error',
109
+ message: `The model call failed: ${bounded(errorArg instanceof Error
110
+ ? `${errorArg.name}: ${errorArg.message}`
111
+ : String(errorArg))}`,
112
+ });
113
+ };
114
+
115
+ export const failureBody = (failureArg: IAnthropicMessagesFailure, requestIdArg: string): string => JSON.stringify({
116
+ type: 'error',
117
+ error: { type: failureArg.type, message: failureArg.message },
118
+ request_id: requestIdArg,
119
+ });
120
+
121
+ export const failureHeaders = (failureArg: IAnthropicMessagesFailure): Record<string, string> => ({
122
+ ...(failureArg.retryAfterSeconds === undefined ? {} : { 'retry-after': String(failureArg.retryAfterSeconds) }),
123
+ ...(failureArg.shouldRetry === undefined ? {} : { 'x-should-retry': String(failureArg.shouldRetry) }),
124
+ });
@@ -0,0 +1,24 @@
1
+ export { AnthropicMessagesError } from './errors.js';
2
+ export {
3
+ classifyOpenAiResponsesMessagesFailure,
4
+ createOpenAiResponsesMessagesDialect,
5
+ type IOpenAiResponsesMessagesDialectOptions,
6
+ } from './openai.js';
7
+ export { createAnthropicMessagesService } from './service.js';
8
+ export type {
9
+ TAnthropicMessagesContentBlock,
10
+ IAnthropicMessagesControls,
11
+ IAnthropicMessagesFailure,
12
+ IAnthropicMessagesMessage,
13
+ IAnthropicMessagesModel,
14
+ IAnthropicMessagesModelListing,
15
+ IAnthropicMessagesService,
16
+ IAnthropicMessagesServiceOptions,
17
+ IAnthropicMessagesUsage,
18
+ IAnthropicMessagesUsageEvent,
19
+ IAnthropicMessagesUsageReporter,
20
+ TAnthropicMessagesEffort,
21
+ TAnthropicMessagesErrorType,
22
+ TAnthropicMessagesStopReason,
23
+ TAnthropicMessagesStreamEvent,
24
+ } from './interfaces.js';
@@ -0,0 +1,173 @@
1
+ import type * as plugins from './plugins.js';
2
+
3
+ /** The Anthropic error types this service answers with. */
4
+ export type TAnthropicMessagesErrorType =
5
+ | 'invalid_request_error'
6
+ | 'authentication_error'
7
+ | 'permission_error'
8
+ | 'not_found_error'
9
+ | 'request_too_large'
10
+ | 'rate_limit_error'
11
+ | 'api_error'
12
+ | 'overloaded_error';
13
+
14
+ /** A failure as the Anthropic Messages API states it: an HTTP status, an error type and a message. */
15
+ export interface IAnthropicMessagesFailure {
16
+ status: number;
17
+ type: TAnthropicMessagesErrorType;
18
+ message: string;
19
+ /** Seconds a client waits before the next attempt, sent as `retry-after`. */
20
+ retryAfterSeconds?: number;
21
+ /** Sent as `x-should-retry`. */
22
+ shouldRetry?: boolean;
23
+ }
24
+
25
+ /** The effort levels an Anthropic client asks for in `output_config.effort`. */
26
+ export type TAnthropicMessagesEffort = 'low' | 'medium' | 'high' | 'xhigh' | 'max';
27
+
28
+ /**
29
+ * What one request asks of the model beyond its prompt and tools, in provider-independent terms.
30
+ * A model's `providerOptions` turns these into its provider's own options.
31
+ */
32
+ export interface IAnthropicMessagesControls {
33
+ /** The reasoning effort to send, already fitted to the model's `reasoningEfforts`; absent for the model's default. */
34
+ reasoningEffort?: string;
35
+ /** Whether the client asked for thinking blocks; without it no reasoning text is requested or returned. */
36
+ thinking: boolean;
37
+ /** False when the client set `disable_parallel_tool_use`. */
38
+ parallelToolCalls: boolean;
39
+ /** The client's session id (`x-claude-code-session-id`), for a provider prompt cache key. */
40
+ sessionId?: string;
41
+ }
42
+
43
+ /** One model this service serves, as the consumer resolves it for a request. */
44
+ export interface IAnthropicMessagesModel {
45
+ /** The language model that answers the request. */
46
+ model: plugins.LanguageModelV4;
47
+ /** The model's provider options for one request. */
48
+ providerOptions: (controls: IAnthropicMessagesControls) => plugins.SharedV4ProviderOptions;
49
+ /**
50
+ * The reasoning efforts the model accepts, lowest first. A requested effort the model lacks is fitted to
51
+ * the nearest lower one it has, and the response names the substitution in `x-flexharness-effort-fitted`.
52
+ * Unset, the requested effort is sent as it is.
53
+ */
54
+ reasoningEfforts?: readonly string[];
55
+ /**
56
+ * Names the account and provider a thinking signature belongs to. A signature from another scope, for
57
+ * example one written while the conversation ran on another account, is not sent back to the model.
58
+ */
59
+ signatureScope: string;
60
+ /**
61
+ * Whether `max_tokens` is sent as the model's output limit. A backend that takes no output limit, such as
62
+ * ChatGPT's Codex backend, is served without one, and the response says so in `x-flexharness-max-tokens`.
63
+ */
64
+ sendsMaxTokens: boolean;
65
+ /** The longest tool name the model accepts; longer names are sent under a stable alias and mapped back. */
66
+ toolNameLimit?: number;
67
+ /** Reads the model provider's own failures first; the generic rules handle the rest. */
68
+ classifyFailure?: (error: unknown) => IAnthropicMessagesFailure | undefined;
69
+ }
70
+
71
+ /** A model as `GET /v1/models` lists it. */
72
+ export interface IAnthropicMessagesModelListing {
73
+ id: string;
74
+ displayName?: string;
75
+ description?: string;
76
+ }
77
+
78
+ /** Token usage of one answered request, as the model reported it. */
79
+ export interface IAnthropicMessagesUsageEvent<TContext> {
80
+ context: TContext;
81
+ modelId: string;
82
+ usage: plugins.LanguageModelV4Usage;
83
+ }
84
+
85
+ /**
86
+ * Where the usage of answered requests goes. `record` runs after the answer is complete, so bookkeeping can
87
+ * never change an answer; a `record` that throws or rejects is handed to `failed` with its event, which the
88
+ * host must handle. A `failed` that throws itself is an unhandled rejection of the host.
89
+ */
90
+ export interface IAnthropicMessagesUsageReporter<TContext> {
91
+ record: (event: IAnthropicMessagesUsageEvent<TContext>) => void | Promise<void>;
92
+ failed: (error: unknown, event: IAnthropicMessagesUsageEvent<TContext>) => void;
93
+ }
94
+
95
+ export interface IAnthropicMessagesServiceOptions<TContext> {
96
+ /**
97
+ * Authenticates a request and returns what the other callbacks need to serve it. Throw an
98
+ * `AnthropicMessagesError` to refuse it, for example with status 401 for a wrong credential.
99
+ */
100
+ authenticate: (request: Request) => Promise<TContext>;
101
+ /**
102
+ * The model a request names, or `undefined` when this service does not serve that id. `signal` aborts when
103
+ * the client leaves, so a slow resolution (such as renewing access) can stop early.
104
+ */
105
+ resolveModel: (modelId: string, context: TContext, signal: AbortSignal) => Promise<IAnthropicMessagesModel | undefined>;
106
+ /** The models `GET /v1/models` lists and an unknown model id's refusal names. */
107
+ listModels: (context: TContext) => Promise<readonly IAnthropicMessagesModelListing[]>;
108
+ /**
109
+ * The HMAC-SHA256 key thinking signatures are written and verified with: at least 32 random bytes, kept
110
+ * secret by the host and used for nothing else. A signature this key did not write is not sent back to the
111
+ * model, so a host that keeps the key across restarts keeps the reasoning of conversations it answered.
112
+ */
113
+ signatureKey: Uint8Array;
114
+ /** Receives the usage of every request the model finished, once its answer has been delivered. */
115
+ usage?: IAnthropicMessagesUsageReporter<TContext>;
116
+ /** Interval of `ping` events while a stream is otherwise silent. Defaults to 15 seconds. */
117
+ pingIntervalMs?: number;
118
+ /** Largest accepted request body. Defaults to 32 MiB, the Messages API limit. */
119
+ maxRequestBytes?: number;
120
+ }
121
+
122
+ export interface IAnthropicMessagesService {
123
+ /** Answers one HTTP request of the Anthropic Messages API. */
124
+ handle: (request: Request) => Promise<Response>;
125
+ }
126
+
127
+ /** Usage in the Anthropic shape: input tokens exclude the ones read from cache. */
128
+ export interface IAnthropicMessagesUsage {
129
+ input_tokens: number;
130
+ output_tokens: number;
131
+ cache_creation_input_tokens: number;
132
+ cache_read_input_tokens: number;
133
+ }
134
+
135
+ export type TAnthropicMessagesStopReason = 'end_turn' | 'max_tokens' | 'stop_sequence' | 'tool_use' | 'refusal';
136
+
137
+ export type TAnthropicMessagesContentBlock =
138
+ | { type: 'text'; text: string }
139
+ | { type: 'thinking'; thinking: string; signature: string }
140
+ | { type: 'tool_use'; id: string; name: string; input: unknown };
141
+
142
+ /** A complete answer, as a non-streaming request receives it. */
143
+ export interface IAnthropicMessagesMessage {
144
+ id: string;
145
+ type: 'message';
146
+ role: 'assistant';
147
+ model: string;
148
+ content: TAnthropicMessagesContentBlock[];
149
+ stop_reason: TAnthropicMessagesStopReason | null;
150
+ stop_sequence: string | null;
151
+ usage: IAnthropicMessagesUsage;
152
+ }
153
+
154
+ export type TAnthropicMessagesContentDelta =
155
+ | { type: 'text_delta'; text: string }
156
+ | { type: 'thinking_delta'; thinking: string }
157
+ | { type: 'signature_delta'; signature: string }
158
+ | { type: 'input_json_delta'; partial_json: string };
159
+
160
+ /** One server-sent event of a streamed answer. */
161
+ export type TAnthropicMessagesStreamEvent =
162
+ | { type: 'message_start'; message: IAnthropicMessagesMessage }
163
+ | { type: 'content_block_start'; index: number; content_block: TAnthropicMessagesContentBlock }
164
+ | { type: 'content_block_delta'; index: number; delta: TAnthropicMessagesContentDelta }
165
+ | { type: 'content_block_stop'; index: number }
166
+ | {
167
+ type: 'message_delta';
168
+ delta: { stop_reason: TAnthropicMessagesStopReason; stop_sequence: string | null };
169
+ usage: IAnthropicMessagesUsage;
170
+ }
171
+ | { type: 'message_stop' }
172
+ | { type: 'ping' }
173
+ | { type: 'error'; error: { type: TAnthropicMessagesErrorType; message: string } };
@@ -0,0 +1,58 @@
1
+ import * as plugins from './plugins.js';
2
+ import { promptTooLong } from './errors.js';
3
+ import type { IAnthropicMessagesControls, IAnthropicMessagesFailure, IAnthropicMessagesModel } from './interfaces.js';
4
+
5
+ export interface IOpenAiResponsesMessagesDialectOptions {
6
+ /**
7
+ * Whether the model reasons. A reasoning model is asked for its encrypted reasoning, which a thinking
8
+ * signature carries back to it, so a conversation keeps its reasoning across turns and tool calls.
9
+ */
10
+ reasoning: boolean;
11
+ }
12
+
13
+ const isRecord = (valueArg: unknown): valueArg is Record<string, unknown> =>
14
+ typeof valueArg === 'object' && valueArg !== null && !Array.isArray(valueArg);
15
+
16
+ /** The OpenAI error of a failure: an HTTP error body, a failed response, or an error frame of a stream. */
17
+ const readOpenAiError = (valueArg: unknown): { code?: unknown; message?: unknown } | undefined => {
18
+ if (!isRecord(valueArg)) return undefined;
19
+ if (isRecord(valueArg.error)) return valueArg.error;
20
+ if (isRecord(valueArg.response) && isRecord(valueArg.response.error)) return valueArg.response.error;
21
+ if ('code' in valueArg) return valueArg;
22
+ return undefined;
23
+ };
24
+
25
+ /**
26
+ * Reads the OpenAI Responses failures the generic rules cannot: an input beyond the model's context window
27
+ * answers with the wording an Anthropic client compacts its conversation on.
28
+ */
29
+ export const classifyOpenAiResponsesMessagesFailure = (errorArg: unknown): IAnthropicMessagesFailure | undefined => {
30
+ const error = plugins.APICallError.isInstance(errorArg) ? readOpenAiError(errorArg.data) : readOpenAiError(errorArg);
31
+ if (error?.code !== 'context_length_exceeded') return undefined;
32
+ return promptTooLong(typeof error.message === 'string' ? error.message : 'the input exceeds the model\'s context window.');
33
+ };
34
+
35
+ /**
36
+ * The OpenAI Responses dialect of a served model: nothing is stored at the provider, reasoning returns
37
+ * encrypted so a thinking signature can carry it, reasoning summaries are requested only when the client
38
+ * asked for thinking, and the client's session id keys the provider's prompt cache.
39
+ */
40
+ export const createOpenAiResponsesMessagesDialect = (
41
+ optionsArg: IOpenAiResponsesMessagesDialectOptions,
42
+ ): Pick<IAnthropicMessagesModel, 'providerOptions' | 'classifyFailure'> => ({
43
+ providerOptions: (controlsArg: IAnthropicMessagesControls) => ({
44
+ openai: {
45
+ store: false,
46
+ ...(optionsArg.reasoning
47
+ ? {
48
+ forceReasoning: true,
49
+ reasoningSummary: controlsArg.thinking ? 'auto' : null,
50
+ ...(controlsArg.reasoningEffort === undefined ? {} : { reasoningEffort: controlsArg.reasoningEffort }),
51
+ }
52
+ : {}),
53
+ ...(controlsArg.parallelToolCalls ? {} : { parallelToolCalls: false }),
54
+ ...(controlsArg.sessionId === undefined ? {} : { promptCacheKey: controlsArg.sessionId }),
55
+ },
56
+ }),
57
+ classifyFailure: classifyOpenAiResponsesMessagesFailure,
58
+ });
@@ -0,0 +1,26 @@
1
+ import { APICallError } from '@ai-sdk/provider';
2
+ import { isModelLimitError } from '@modelprofile.com/flexharness-models';
3
+
4
+ export { APICallError, isModelLimitError };
5
+ export type {
6
+ JSONObject,
7
+ JSONValue,
8
+ LanguageModelV4,
9
+ LanguageModelV4CallOptions,
10
+ LanguageModelV4FilePart,
11
+ LanguageModelV4FinishReason,
12
+ LanguageModelV4FunctionTool,
13
+ LanguageModelV4Message,
14
+ LanguageModelV4Prompt,
15
+ LanguageModelV4ReasoningPart,
16
+ LanguageModelV4StreamPart,
17
+ LanguageModelV4TextPart,
18
+ LanguageModelV4ToolCallPart,
19
+ LanguageModelV4ToolChoice,
20
+ LanguageModelV4ToolResultOutput,
21
+ LanguageModelV4ToolResultPart,
22
+ LanguageModelV4Usage,
23
+ SharedV4ProviderMetadata,
24
+ SharedV4ProviderOptions,
25
+ SharedV4Warning,
26
+ } from '@ai-sdk/provider';
@@ -0,0 +1,129 @@
1
+ Serves the Anthropic Messages API over FlexHarness models, so an Anthropic client such as Claude Code can
2
+ run on another provider's model.
3
+
4
+ Part of the [FlexHarness toolbox](https://code.foss.global/modelprofile.com/flexharness).
5
+ Install the components your project uses. Provider adapters are registered explicitly;
6
+ the agent and harness do not install or register vendor SDKs.
7
+
8
+ ```sh
9
+ pnpm add @modelprofile.com/flexharness-providers
10
+ ```
11
+
12
+ See the [toolbox usage and migration guide](https://code.foss.global/modelprofile.com/flexharness#readme)
13
+ for composition examples and the SmartAI/SmartAgent migration map.
14
+
15
+ ## Serving a model
16
+
17
+ `createAnthropicMessagesService()` answers WHATWG `Request`s with `Response`s, so any HTTP server can host
18
+ it; `/anthropic-messages/node` hosts it on a Node HTTP server. It serves `POST /v1/messages`, streamed as the
19
+ Anthropic event sequence or whole, and `GET /v1/models`. `POST /v1/messages/count_tokens` answers 404, on
20
+ which an Anthropic client estimates token counts itself, and `HEAD /api/hello`, a client's connection
21
+ warm-up, answers 200 without a credential.
22
+
23
+ ```typescript
24
+ import {
25
+ AnthropicMessagesError,
26
+ createAnthropicMessagesService,
27
+ createOpenAiResponsesMessagesDialect,
28
+ } from '@modelprofile.com/flexharness-providers/anthropic-messages';
29
+ import {
30
+ createOpenAiChatGptResolvingModelConnection,
31
+ createOpenAiModelProvider,
32
+ } from '@modelprofile.com/flexharness-providers/openai';
33
+
34
+ const service = createAnthropicMessagesService({
35
+ authenticate: async (request) => {
36
+ if (request.headers.get('authorization') !== `Bearer ${sessionToken}`) {
37
+ throw new AnthropicMessagesError(401, 'authentication_error', 'The session token is not valid.');
38
+ }
39
+ return session;
40
+ },
41
+ resolveModel: async (modelId, session) => {
42
+ const entry = session.catalog.find((model) => model.modelId === modelId);
43
+ if (!entry) return undefined;
44
+ const connection = createOpenAiChatGptResolvingModelConnection({ expectedIdentity, resolveAccess });
45
+ return {
46
+ model: createOpenAiModelProvider().getModel({ provider: 'openai', model: modelId, connection }),
47
+ ...createOpenAiResponsesMessagesDialect({ reasoning: entry.reasoningEfforts.length > 0 }),
48
+ reasoningEfforts: entry.reasoningEfforts.map((effort) => effort.effort),
49
+ signatureScope: `openai:${expectedIdentity.accountId}`,
50
+ sendsMaxTokens: false,
51
+ toolNameLimit: 64,
52
+ };
53
+ },
54
+ listModels: async (session) => session.catalog.map((model) => ({ id: model.modelId, displayName: model.displayName })),
55
+ signatureKey, // 32 or more random bytes from the host's secret store
56
+ usage: {
57
+ record: async (event) => ledger.add(event),
58
+ failed: (error, event) => log.error('usage bookkeeping failed', { error, modelId: event.modelId }),
59
+ },
60
+ });
61
+ ```
62
+
63
+ `usage.record` runs after an answer is complete and outside its path, so bookkeeping never changes an
64
+ answer. A `record` that throws or rejects is handed to `usage.failed` with its event; the host handles it
65
+ there, and a `failed` that throws itself is an unhandled rejection of the host.
66
+
67
+ ## What is translated
68
+
69
+ System prompts, text, images, PDF and text documents, tool definitions, tool calls, tool results with text
70
+ and images, tool choice (including `disable_parallel_tool_use`), mid-conversation system messages,
71
+ thinking, `output_config.effort` and a JSON schema output format.
72
+
73
+ Thinking comes back with a signature that carries the reasoning metadata of the model's own reasoning part
74
+ (for OpenAI, the reasoning item and its encrypted content). A client returns thinking blocks unchanged, so
75
+ the reasoning reaches the model again on the next turn. The client holds the signature, so it is
76
+ authenticated with an HMAC-SHA256 under `signatureKey`, which the host provides: at least 32 random bytes,
77
+ kept secret and used for nothing else. A shorter key is refused when the service is created. A signature the
78
+ key did not write or that was changed, one written under another `signatureScope`, such as another account,
79
+ and a redacted thinking block are not sent back; the response counts them in
80
+ `x-flexharness-thinking-dropped`. A host that keeps its key across restarts keeps the reasoning of the
81
+ conversations it answered; a new key drops the thinking signed under the old one.
82
+
83
+ Response headers name what a request received differently from what it asked for:
84
+
85
+ | Header | Meaning |
86
+ | --- | --- |
87
+ | `x-flexharness-effort-fitted` | The requested effort the model lacks and the one sent instead, as `max=>xhigh` |
88
+ | `x-flexharness-max-tokens` | `not-sent` when the model takes no output limit (`sendsMaxTokens: false`) |
89
+ | `x-flexharness-thinking-dropped` | The number of thinking blocks not sent back to the model |
90
+ | `x-flexharness-model-warnings` | The model's own warnings, such as an unsupported sampling setting |
91
+
92
+ ## What is refused
93
+
94
+ A field that cannot be translated faithfully is refused with a 400 that names it: server-side context
95
+ management, code execution containers, remote MCP servers, deferred tool loading and server tools. The
96
+ server tool refusal names the tool type after `Input tag`, the wording on which an Anthropic client continues
97
+ without that tool. Prompt cache markers, citations, metadata and the service tier are hints to Anthropic's own
98
+ service and are accepted without effect; the provider caches prompts by the session id instead.
99
+
100
+ Failures answer in the Anthropic error shape. A usage or rate limit (`ModelLimitError`) is a 429 with
101
+ `retry-after` in seconds until the reset. An input beyond the model's context window is the
102
+ `prompt is too long` refusal an Anthropic client compacts its conversation on. A provider that refuses the
103
+ account's credentials is a 403. A failure after a stream began is an `error` event, and a stream that stays
104
+ silent sends `ping` events.
105
+
106
+ ## Issue Reporting and Security
107
+
108
+ Report issues and security concerns through [community.foss.global](https://community.foss.global).
109
+
110
+ ## License and Legal Information
111
+
112
+ This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the repository license file.
113
+
114
+ **Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
115
+
116
+ ### Trademarks
117
+
118
+ This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH or third parties, and are not included within the scope of the MIT license granted herein.
119
+
120
+ Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines or the guidelines of the respective third-party owners, and any usage must be approved in writing. Third-party trademarks used herein are the property of their respective owners and used only in a descriptive manner, e.g. for an implementation of an API or similar.
121
+
122
+ ### Company Information
123
+
124
+ Task Venture Capital GmbH<br>
125
+ Registered at District Court Bremen HRB 35230 HB, Germany
126
+
127
+ For any legal inquiries or further information, please contact us via email at hello@task.vc.
128
+
129
+ By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.