@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.
- package/dist_ts_anthropic_messages/errors.d.ts +22 -0
- package/dist_ts_anthropic_messages/errors.js +100 -0
- package/dist_ts_anthropic_messages/index.d.ts +4 -0
- package/dist_ts_anthropic_messages/index.js +4 -0
- package/dist_ts_anthropic_messages/interfaces.d.ts +185 -0
- package/dist_ts_anthropic_messages/interfaces.js +2 -0
- package/dist_ts_anthropic_messages/openai.d.ts +19 -0
- package/dist_ts_anthropic_messages/openai.js +48 -0
- package/dist_ts_anthropic_messages/plugins.d.ts +4 -0
- package/dist_ts_anthropic_messages/plugins.js +4 -0
- package/dist_ts_anthropic_messages/request.d.ts +109 -0
- package/dist_ts_anthropic_messages/request.js +554 -0
- package/dist_ts_anthropic_messages/service.d.ts +8 -0
- package/dist_ts_anthropic_messages/service.js +274 -0
- package/dist_ts_anthropic_messages/signature.d.ts +16 -0
- package/dist_ts_anthropic_messages/signature.js +91 -0
- package/dist_ts_anthropic_messages/stream.d.ts +48 -0
- package/dist_ts_anthropic_messages/stream.js +220 -0
- package/dist_ts_anthropic_messages_node/index.d.ts +8 -0
- package/dist_ts_anthropic_messages_node/index.js +93 -0
- package/dist_ts_anthropic_messages_node/plugins.d.ts +6 -0
- package/dist_ts_anthropic_messages_node/plugins.js +4 -0
- package/dist_ts_openai_account/plugins.d.ts +2 -0
- package/dist_ts_openai_account/plugins.js +4 -1
- package/dist_ts_openai_account/smartai.providers.interfaces.d.ts +6 -22
- package/dist_ts_openai_account/smartai.providers.openai.js +15 -134
- package/dist_ts_openai_auth/index.d.ts +1 -2
- package/dist_ts_openai_auth/index.js +2 -3
- package/dist_ts_openai_auth/interfaces.d.ts +1 -1
- package/dist_ts_openai_auth/smartai.auth.openai.d.ts +3 -15
- package/dist_ts_openai_auth/smartai.auth.openai.js +3 -17
- package/dist_ts_openai_auth/smartai.connection.openai.d.ts +1 -1
- package/dist_ts_openai_auth/smartai.connection.openai.js +2 -3
- package/dist_ts_providers/anthropic/index.d.ts +1 -1
- package/dist_ts_providers/anthropic/index.js +1 -1
- package/dist_ts_providers/anthropic/plugins.d.ts +1 -1
- package/dist_ts_providers/google/index.d.ts +1 -1
- package/dist_ts_providers/google/index.js +1 -1
- package/dist_ts_providers/google/plugins.d.ts +1 -1
- package/dist_ts_providers/groq/index.d.ts +1 -1
- package/dist_ts_providers/groq/index.js +1 -1
- package/dist_ts_providers/groq/plugins.d.ts +1 -1
- package/dist_ts_providers/mistral/index.d.ts +1 -1
- package/dist_ts_providers/mistral/index.js +1 -1
- package/dist_ts_providers/mistral/plugins.d.ts +1 -1
- package/dist_ts_providers/openai/index.d.ts +5 -1
- package/dist_ts_providers/openai/index.js +5 -1
- package/dist_ts_providers/openai/plugins.d.ts +4 -4
- package/dist_ts_providers/openai/plugins.js +3 -3
- package/dist_ts_providers/openai/smartai.chatgpt.catalog.d.ts +61 -0
- package/dist_ts_providers/openai/smartai.chatgpt.catalog.js +252 -0
- package/{dist_ts_openai_auth/smartai.limits.openai.d.ts → dist_ts_providers/openai/smartai.chatgpt.limits.d.ts} +2 -2
- package/dist_ts_providers/openai/smartai.chatgpt.limits.js +49 -0
- package/dist_ts_providers/openai/smartai.chatgpt.middleware.d.ts +10 -0
- package/dist_ts_providers/openai/smartai.chatgpt.middleware.js +60 -0
- package/dist_ts_providers/openai/smartai.chatgpt.openai.d.ts +92 -0
- package/dist_ts_providers/openai/smartai.chatgpt.openai.js +160 -0
- package/dist_ts_providers/perplexity/index.d.ts +1 -1
- package/dist_ts_providers/perplexity/index.js +1 -1
- package/dist_ts_providers/perplexity/plugins.d.ts +1 -1
- package/dist_ts_providers/xai/index.d.ts +1 -1
- package/dist_ts_providers/xai/index.js +1 -1
- package/dist_ts_providers/xai/plugins.d.ts +1 -1
- package/package.json +14 -2
- package/readme.md +9 -0
- package/ts_anthropic_messages/errors.ts +124 -0
- package/ts_anthropic_messages/index.ts +24 -0
- package/ts_anthropic_messages/interfaces.ts +173 -0
- package/ts_anthropic_messages/openai.ts +58 -0
- package/ts_anthropic_messages/plugins.ts +26 -0
- package/ts_anthropic_messages/readme.md +129 -0
- package/ts_anthropic_messages/request.ts +652 -0
- package/ts_anthropic_messages/service.ts +312 -0
- package/ts_anthropic_messages/signature.ts +107 -0
- package/ts_anthropic_messages/stream.ts +260 -0
- package/ts_anthropic_messages_node/index.ts +99 -0
- package/ts_anthropic_messages_node/plugins.ts +7 -0
- package/ts_anthropic_messages_node/readme.md +50 -0
- package/ts_openai_account/plugins.ts +5 -0
- package/ts_openai_account/smartai.providers.interfaces.ts +11 -25
- package/ts_openai_account/smartai.providers.openai.ts +16 -135
- package/ts_openai_auth/index.ts +4 -2
- package/ts_openai_auth/interfaces.ts +1 -1
- package/ts_openai_auth/smartai.auth.openai.ts +12 -24
- package/ts_openai_auth/smartai.connection.openai.ts +5 -3
- package/ts_providers/anthropic/index.ts +1 -1
- package/ts_providers/anthropic/plugins.ts +1 -0
- package/ts_providers/google/index.ts +1 -1
- package/ts_providers/google/plugins.ts +1 -1
- package/ts_providers/groq/index.ts +1 -1
- package/ts_providers/groq/plugins.ts +1 -1
- package/ts_providers/mistral/index.ts +1 -1
- package/ts_providers/mistral/plugins.ts +1 -1
- package/ts_providers/openai/index.ts +30 -1
- package/ts_providers/openai/plugins.ts +5 -3
- package/ts_providers/openai/readme.md +60 -7
- package/ts_providers/openai/smartai.chatgpt.catalog.ts +319 -0
- package/{ts_openai_auth/smartai.limits.openai.ts → ts_providers/openai/smartai.chatgpt.limits.ts} +6 -7
- package/ts_providers/openai/smartai.chatgpt.middleware.ts +71 -0
- package/ts_providers/openai/smartai.chatgpt.openai.ts +271 -0
- package/ts_providers/perplexity/index.ts +1 -1
- package/ts_providers/perplexity/plugins.ts +1 -1
- package/ts_providers/readme.md +9 -0
- package/ts_providers/xai/index.ts +1 -1
- package/ts_providers/xai/plugins.ts +1 -1
- package/dist_ts_openai_auth/smartai.limits.openai.js +0 -49
- package/dist_ts_openai_auth/smartai.middleware.openai.d.ts +0 -6
- package/dist_ts_openai_auth/smartai.middleware.openai.js +0 -47
- package/ts_openai_auth/smartai.middleware.openai.ts +0 -55
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// OpenAI protocol portions are adapted in TypeScript; see third-party-notices.md.
|
|
2
|
+
import { classifyOpenAiChatGptLimit } from './smartai.chatgpt.limits.js';
|
|
3
|
+
import { createOpenAiChatGptInstructionsMiddleware } from './smartai.chatgpt.middleware.js';
|
|
4
|
+
export const OPENAI_CHATGPT_CODEX_BASE_URL = 'https://chatgpt.com/backend-api/codex';
|
|
5
|
+
export const OPENAI_CHATGPT_DEFAULT_ORIGINATOR = 'smartai';
|
|
6
|
+
export class OpenAiChatGptAuthError extends Error {
|
|
7
|
+
status;
|
|
8
|
+
body;
|
|
9
|
+
requestOutcome;
|
|
10
|
+
retryAfterMs;
|
|
11
|
+
constructor(message, options = {}) {
|
|
12
|
+
super(message);
|
|
13
|
+
this.name = 'OpenAiChatGptAuthError';
|
|
14
|
+
this.status = options.status;
|
|
15
|
+
this.body = options.body;
|
|
16
|
+
this.requestOutcome = options.requestOutcome;
|
|
17
|
+
this.retryAfterMs = options.retryAfterMs;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
const codexBaseUrl = new URL(OPENAI_CHATGPT_CODEX_BASE_URL);
|
|
21
|
+
const isNonEmptyString = (value) => typeof value === 'string' && value.length > 0;
|
|
22
|
+
const isGeneration = (value) => Number.isSafeInteger(value) && value >= 1;
|
|
23
|
+
function assertCodexRequestUrl(normalizedUrlArg) {
|
|
24
|
+
const url = new URL(normalizedUrlArg);
|
|
25
|
+
const isCodexPath = url.pathname === codexBaseUrl.pathname
|
|
26
|
+
|| url.pathname.startsWith(`${codexBaseUrl.pathname}/`);
|
|
27
|
+
if (url.origin !== codexBaseUrl.origin
|
|
28
|
+
|| url.username !== ''
|
|
29
|
+
|| url.password !== ''
|
|
30
|
+
|| !isCodexPath) {
|
|
31
|
+
throw new OpenAiChatGptAuthError('OpenAI ChatGPT inference URL is outside the Codex endpoint.');
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function requireWorkspaceIdentity(identityArg) {
|
|
35
|
+
if (!identityArg
|
|
36
|
+
|| !isNonEmptyString(identityArg.accountId)
|
|
37
|
+
|| typeof identityArg.isFedrampAccount !== 'boolean') {
|
|
38
|
+
throw new OpenAiChatGptAuthError('OpenAI ChatGPT workspace identity is invalid.');
|
|
39
|
+
}
|
|
40
|
+
return Object.freeze({
|
|
41
|
+
accountId: identityArg.accountId,
|
|
42
|
+
isFedrampAccount: identityArg.isFedrampAccount,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
function assertGrant(grantArg, expectedIdentityArg) {
|
|
46
|
+
if (!grantArg
|
|
47
|
+
|| !isNonEmptyString(grantArg.accessToken)
|
|
48
|
+
|| (grantArg.generation !== undefined && !isGeneration(grantArg.generation))) {
|
|
49
|
+
throw new OpenAiChatGptAuthError('OpenAI ChatGPT resolved access is invalid.');
|
|
50
|
+
}
|
|
51
|
+
if (grantArg.accountId !== expectedIdentityArg.accountId
|
|
52
|
+
|| grantArg.isFedrampAccount !== expectedIdentityArg.isFedrampAccount) {
|
|
53
|
+
throw new OpenAiChatGptAuthError('OpenAI ChatGPT resolved access identity does not match the connection.');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** The headers a ChatGPT backend request authenticates with. Shared by inference and the model catalog. */
|
|
57
|
+
export function createOpenAiChatGptAccessHeaders(accessArg, originatorArg = OPENAI_CHATGPT_DEFAULT_ORIGINATOR) {
|
|
58
|
+
return {
|
|
59
|
+
Authorization: `Bearer ${accessArg.accessToken}`,
|
|
60
|
+
originator: originatorArg,
|
|
61
|
+
...(accessArg.accountId ? { 'ChatGPT-Account-ID': accessArg.accountId } : {}),
|
|
62
|
+
...(accessArg.isFedrampAccount ? { 'X-OpenAI-Fedramp': 'true' } : {}),
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
const accessRejected = () => new OpenAiChatGptAuthError('OpenAI ChatGPT rejected the access token.', { status: 401 });
|
|
66
|
+
/**
|
|
67
|
+
* A ChatGPT connection that resolves its access before every provider HTTP request.
|
|
68
|
+
*
|
|
69
|
+
* The workspace and residency are pinned at construction; an access that states anything else stops the
|
|
70
|
+
* request before the transport is called. A provider 401 is answered once: when the rejected access carried
|
|
71
|
+
* a `generation`, the connection resolves again with `rejectedGeneration` and sends the request a second
|
|
72
|
+
* time with the new access. A 401 that cannot be retried, and a second 401, fail with an
|
|
73
|
+
* `OpenAiChatGptAuthError` whose `status` is 401.
|
|
74
|
+
*/
|
|
75
|
+
export const createOpenAiChatGptResolvingModelConnection = (optionsArg) => {
|
|
76
|
+
if (!optionsArg || typeof optionsArg.resolveAccess !== 'function') {
|
|
77
|
+
throw new OpenAiChatGptAuthError('OpenAI ChatGPT access resolver is required.');
|
|
78
|
+
}
|
|
79
|
+
const expectedIdentity = requireWorkspaceIdentity(optionsArg.expectedIdentity);
|
|
80
|
+
const resolveAccess = optionsArg.resolveAccess;
|
|
81
|
+
const originator = optionsArg.originator ?? OPENAI_CHATGPT_DEFAULT_ORIGINATOR;
|
|
82
|
+
const transport = optionsArg.fetch ?? globalThis.fetch;
|
|
83
|
+
if (typeof transport !== 'function') {
|
|
84
|
+
throw new OpenAiChatGptAuthError('fetch is not available for OpenAI ChatGPT inference.');
|
|
85
|
+
}
|
|
86
|
+
const resolveGrant = async (requestArg) => {
|
|
87
|
+
const grant = await resolveAccess(requestArg);
|
|
88
|
+
requestArg.signal.throwIfAborted();
|
|
89
|
+
assertGrant(grant, expectedIdentity);
|
|
90
|
+
return grant;
|
|
91
|
+
};
|
|
92
|
+
const send = async (requestArg, grantArg) => {
|
|
93
|
+
const headers = new Headers(requestArg.headers);
|
|
94
|
+
for (const [name, value] of Object.entries(createOpenAiChatGptAccessHeaders(grantArg, originator))) {
|
|
95
|
+
headers.set(name, value);
|
|
96
|
+
}
|
|
97
|
+
if (!grantArg.isFedrampAccount)
|
|
98
|
+
headers.delete('X-OpenAI-Fedramp');
|
|
99
|
+
const authenticatedRequest = new Request(requestArg, { headers });
|
|
100
|
+
authenticatedRequest.signal.throwIfAborted();
|
|
101
|
+
return transport(authenticatedRequest);
|
|
102
|
+
};
|
|
103
|
+
const fetchWithCurrentAccess = async (inputArg, initArg) => {
|
|
104
|
+
const request = new Request(inputArg, {
|
|
105
|
+
...initArg,
|
|
106
|
+
redirect: 'error',
|
|
107
|
+
});
|
|
108
|
+
assertCodexRequestUrl(request.url);
|
|
109
|
+
request.signal.throwIfAborted();
|
|
110
|
+
const grant = await resolveGrant({ signal: request.signal });
|
|
111
|
+
// A replay needs the body the first attempt consumes, so it is kept only when a replay can happen.
|
|
112
|
+
const replay = grant.generation === undefined ? undefined : request.clone();
|
|
113
|
+
const response = await send(request, grant);
|
|
114
|
+
if (response.status !== 401)
|
|
115
|
+
return response;
|
|
116
|
+
await response.body?.cancel().catch(() => undefined);
|
|
117
|
+
if (!replay)
|
|
118
|
+
throw accessRejected();
|
|
119
|
+
const renewed = await resolveGrant({ signal: replay.signal, rejectedGeneration: grant.generation });
|
|
120
|
+
const replayed = await send(replay, renewed);
|
|
121
|
+
if (replayed.status !== 401)
|
|
122
|
+
return replayed;
|
|
123
|
+
await replayed.body?.cancel().catch(() => undefined);
|
|
124
|
+
throw accessRejected();
|
|
125
|
+
};
|
|
126
|
+
return {
|
|
127
|
+
settings: {
|
|
128
|
+
apiKey: '',
|
|
129
|
+
baseURL: OPENAI_CHATGPT_CODEX_BASE_URL,
|
|
130
|
+
fetch: fetchWithCurrentAccess,
|
|
131
|
+
},
|
|
132
|
+
middleware: createOpenAiChatGptInstructionsMiddleware(),
|
|
133
|
+
// the ChatGPT Codex backend serves the Responses API only
|
|
134
|
+
apis: ['responses'],
|
|
135
|
+
classifyLimit: classifyOpenAiChatGptLimit,
|
|
136
|
+
};
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* A ChatGPT connection for one fixed access, with the workspace and residency its issuer stated.
|
|
140
|
+
*
|
|
141
|
+
* It sends exactly the access it was given, and a provider 401 fails with an `OpenAiChatGptAuthError`
|
|
142
|
+
* whose `status` is 401. Use `createOpenAiChatGptResolvingModelConnection` when an authority renews the
|
|
143
|
+
* access.
|
|
144
|
+
*/
|
|
145
|
+
export const createOpenAiChatGptAccessModelConnection = (accessArg, optionsArg = {}) => {
|
|
146
|
+
if (!accessArg || !isNonEmptyString(accessArg.accessToken)) {
|
|
147
|
+
throw new OpenAiChatGptAuthError('OpenAI ChatGPT access is invalid.');
|
|
148
|
+
}
|
|
149
|
+
const access = Object.freeze({
|
|
150
|
+
accessToken: accessArg.accessToken,
|
|
151
|
+
accountId: accessArg.accountId,
|
|
152
|
+
isFedrampAccount: accessArg.isFedrampAccount,
|
|
153
|
+
});
|
|
154
|
+
return createOpenAiChatGptResolvingModelConnection({
|
|
155
|
+
...optionsArg,
|
|
156
|
+
expectedIdentity: access,
|
|
157
|
+
resolveAccess: async () => access,
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoic21hcnRhaS5jaGF0Z3B0Lm9wZW5haS5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uL3RzX3Byb3ZpZGVycy9vcGVuYWkvc21hcnRhaS5jaGF0Z3B0Lm9wZW5haS50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxrRkFBa0Y7QUFDbEYsT0FBTyxFQUFFLDBCQUEwQixFQUFFLE1BQU0sNkJBQTZCLENBQUM7QUFDekUsT0FBTyxFQUFFLHlDQUF5QyxFQUFFLE1BQU0saUNBQWlDLENBQUM7QUFHNUYsTUFBTSxDQUFDLE1BQU0sNkJBQTZCLEdBQUcsdUNBQXVDLENBQUM7QUFDckYsTUFBTSxDQUFDLE1BQU0saUNB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NCQUFzQjtTQUM5QjtRQUNELFVBQVUsRUFBRSx5Q0FBeUMsRUFBRTtRQUN2RCwwREFBMEQ7UUFDMUQsSUFBSSxFQUFFLENBQUMsV0FBVyxDQUFDO1FBQ25CLGFBQWEsRUFBRSwwQkFBMEI7S0FDMUMsQ0FBQztBQUNKLENBQUMsQ0FBQztBQUVGOzs7Ozs7R0FNRztBQUNILE1BQU0sQ0FBQyxNQUFNLHdDQUF3QyxHQUFHLENBQ3RELFNBQStCLEVBQy9CLGFBQW9ELEVBQUUsRUFDOUIsRUFBRTtJQUMxQixJQUFJLENBQUMsU0FBUyxJQUFJLENBQUMsZ0JBQWdCLENBQUMsU0FBUyxDQUFDLFdBQVcsQ0FBQyxFQUFFLENBQUM7UUFDM0QsTUFBTSxJQUFJLHNCQUFzQixDQUFDLG1DQUFtQyxDQUFDLENBQUM7SUFDeEUsQ0FBQztJQUNELE1BQU0sTUFBTSxHQUF5QixNQUFNLENBQUMsTUFBTSxDQUFDO1FBQ2pELFdBQVcsRUFBRSxTQUFTLENBQUMsV0FBVztRQUNsQyxTQUFTLEVBQUUsU0FBUyxDQUFDLFNBQVM7UUFDOUIsZ0JBQWdCLEVBQUUsU0FBUyxDQUFDLGdCQUFnQjtLQUM3QyxDQUFDLENBQUM7SUFDSCxPQUFPLDJDQUEyQyxDQUFDO1FBQ2pELEdBQUcsVUFBVTtRQUNiLGdCQUFnQixFQUFFLE1BQU07UUFDeEIsYUFBYSxFQUFFLEtBQUssSUFBSSxFQUFFLENBQUMsTUFBTTtLQUNsQyxDQUFDLENBQUM7QUFDTCxDQUFDLENBQUMifQ==
|
|
@@ -2,4 +2,4 @@ import * as plugins from './plugins.js';
|
|
|
2
2
|
export interface IPerplexityModelOptions extends plugins.ISmartAiOptions {
|
|
3
3
|
provider: 'perplexity';
|
|
4
4
|
}
|
|
5
|
-
export declare const createPerplexityModelProvider: () => plugins.IModelProvider<IPerplexityModelOptions>;
|
|
5
|
+
export declare const createPerplexityModelProvider: () => plugins.IModelProvider<IPerplexityModelOptions, plugins.LanguageModelV4>;
|
|
@@ -3,4 +3,4 @@ export const createPerplexityModelProvider = () => ({
|
|
|
3
3
|
id: 'perplexity',
|
|
4
4
|
getModel: (options) => plugins.createPerplexity({ apiKey: options.apiKey })(options.model),
|
|
5
5
|
});
|
|
6
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
6
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi90c19wcm92aWRlcnMvcGVycGxleGl0eS9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSxPQUFPLEtBQUssT0FBTyxNQUFNLGNBQWMsQ0FBQztBQUl4QyxNQUFNLENBQUMsTUFBTSw2QkFBNkIsR0FBRyxHQUE2RSxFQUFFLENBQUMsQ0FBQztJQUM1SCxFQUFFLEVBQUUsWUFBWTtJQUNoQixRQUFRLEVBQUUsQ0FBQyxPQUFPLEVBQUUsRUFBRSxDQUFDLE9BQU8sQ0FBQyxnQkFBZ0IsQ0FBQyxFQUFFLE1BQU0sRUFBRSxPQUFPLENBQUMsTUFBTSxFQUFFLENBQUMsQ0FBQyxPQUFPLENBQUMsS0FBSyxDQUFDO0NBQzNGLENBQUMsQ0FBQyJ9
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { createPerplexity } from '@ai-sdk/perplexity';
|
|
2
2
|
export { createPerplexity };
|
|
3
|
-
export type { IModelProvider, ISmartAiOptions } from '@modelprofile.com/flexharness-models';
|
|
3
|
+
export type { IModelProvider, ISmartAiOptions, LanguageModelV4 } from '@modelprofile.com/flexharness-models';
|
|
@@ -2,4 +2,4 @@ import * as plugins from './plugins.js';
|
|
|
2
2
|
export interface IXaiModelOptions extends plugins.ISmartAiOptions {
|
|
3
3
|
provider: 'xai';
|
|
4
4
|
}
|
|
5
|
-
export declare const createXaiModelProvider: () => plugins.IModelProvider<IXaiModelOptions>;
|
|
5
|
+
export declare const createXaiModelProvider: () => plugins.IModelProvider<IXaiModelOptions, plugins.LanguageModelV4>;
|
|
@@ -3,4 +3,4 @@ export const createXaiModelProvider = () => ({
|
|
|
3
3
|
id: 'xai',
|
|
4
4
|
getModel: (options) => plugins.createXai({ apiKey: options.apiKey })(options.model),
|
|
5
5
|
});
|
|
6
|
-
//# sourceMappingURL=data:application/json;base64,
|
|
6
|
+
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi90c19wcm92aWRlcnMveGFpL2luZGV4LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE9BQU8sS0FBSyxPQUFPLE1BQU0sY0FBYyxDQUFDO0FBSXhDLE1BQU0sQ0FBQyxNQUFNLHNCQUFzQixHQUFHLEdBQXNFLEVBQUUsQ0FBQyxDQUFDO0lBQzlHLEVBQUUsRUFBRSxLQUFLO0lBQ1QsUUFBUSxFQUFFLENBQUMsT0FBTyxFQUFFLEVBQUUsQ0FBQyxPQUFPLENBQUMsU0FBUyxDQUFDLEVBQUUsTUFBTSxFQUFFLE9BQU8sQ0FBQyxNQUFNLEVBQUUsQ0FBQyxDQUFDLE9BQU8sQ0FBQyxLQUFLLENBQUM7Q0FDcEYsQ0FBQyxDQUFDIn0=
|
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.
|
|
5
|
+
"version": "9.2.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,6 +63,14 @@
|
|
|
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": {
|
|
@@ -75,7 +83,7 @@
|
|
|
75
83
|
"@ai-sdk/provider": "^4.0.8",
|
|
76
84
|
"@ai-sdk/xai": "^4.0.44",
|
|
77
85
|
"@anthropic-ai/sdk": "0.95.2",
|
|
78
|
-
"@modelprofile.com/flexharness-models": "9.
|
|
86
|
+
"@modelprofile.com/flexharness-models": "9.2.0",
|
|
79
87
|
"ai": "^7.0.79",
|
|
80
88
|
"openai": "^6.37.0",
|
|
81
89
|
"proper-lockfile": "^4.1.2"
|
|
@@ -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
|
|
@@ -42,6 +43,14 @@ instead; it resolves current access at every provider HTTP request while keeping
|
|
|
42
43
|
account and residency identity fixed for the connection. API-key inference
|
|
43
44
|
needs no authentication setup.
|
|
44
45
|
|
|
46
|
+
When an account authority such as authswitch owns the login, `/openai` runs
|
|
47
|
+
ChatGPT inference from access alone:
|
|
48
|
+
`createOpenAiChatGptResolvingModelConnection({ expectedIdentity, resolveAccess })`
|
|
49
|
+
resolves the access token, workspace and residency before every provider request
|
|
50
|
+
and resolves once more after a rejected token, and `listOpenAiChatGptModels()`
|
|
51
|
+
reads the model catalog with the same access. Neither parses the token; see
|
|
52
|
+
`openai/readme.md`.
|
|
53
|
+
|
|
45
54
|
The package includes the applicable OpenAI Codex license and third-party notices.
|
|
46
55
|
|
|
47
56
|
## License and Legal Information
|
|
@@ -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 } };
|