@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.
- 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_media_openai/index.js +2 -3
- package/dist_ts_media_openai/plugins.d.ts +2 -0
- package/dist_ts_media_openai/plugins.js +5 -1
- package/dist_ts_openai_account/index.d.ts +1 -1
- package/dist_ts_openai_account/index.js +2 -2
- package/dist_ts_openai_account/plugins.d.ts +3 -0
- package/dist_ts_openai_account/plugins.js +3 -1
- package/dist_ts_openai_account/smartai.providers.credentials.d.ts +2 -2
- package/dist_ts_openai_account/smartai.providers.credentials.js +3 -4
- package/dist_ts_openai_account/smartai.providers.interfaces.d.ts +6 -7
- package/dist_ts_openai_account/smartai.providers.openai.js +15 -16
- package/dist_ts_openai_auth/index.d.ts +1 -1
- package/dist_ts_openai_auth/index.js +2 -2
- package/dist_ts_openai_auth/interfaces.d.ts +1 -1
- package/dist_ts_openai_auth/plugins.d.ts +3 -0
- package/dist_ts_openai_auth/plugins.js +4 -0
- package/dist_ts_openai_auth/smartai.auth.openai.d.ts +1 -2
- package/dist_ts_openai_auth/smartai.auth.openai.js +27 -27
- package/dist_ts_openai_auth/smartai.connection.openai.d.ts +3 -3
- package/dist_ts_openai_auth/smartai.connection.openai.js +6 -6
- package/dist_ts_openai_auth_files/index.d.ts +6 -6
- package/dist_ts_openai_auth_files/index.js +3 -4
- package/dist_ts_openai_auth_files/plugins.d.ts +3 -0
- package/dist_ts_openai_auth_files/plugins.js +4 -1
- 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 +4 -3
- package/dist_ts_providers/anthropic/plugins.js +3 -3
- package/dist_ts_providers/anthropic/smartai.middleware.anthropic.d.ts +2 -3
- package/dist_ts_providers/anthropic/smartai.middleware.anthropic.js +3 -3
- 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/ollama/index.d.ts +2 -2
- package/dist_ts_providers/ollama/index.js +1 -1
- package/dist_ts_providers/ollama/interfaces.d.ts +2 -2
- package/dist_ts_providers/ollama/plugins.d.ts +2 -0
- package/dist_ts_providers/ollama/plugins.js +2 -0
- package/dist_ts_providers/ollama/smartai.provider.ollama.d.ts +2 -2
- package/dist_ts_providers/ollama/smartai.provider.ollama.js +1 -1
- package/dist_ts_providers/openai/index.d.ts +1 -1
- package/dist_ts_providers/openai/index.js +1 -1
- package/dist_ts_providers/openai/plugins.d.ts +1 -1
- package/dist_ts_providers/openai/smartai.chatgpt.catalog.d.ts +2 -0
- package/dist_ts_providers/openai/smartai.chatgpt.catalog.js +8 -1
- package/dist_ts_providers/openai/smartai.chatgpt.middleware.d.ts +5 -1
- package/dist_ts_providers/openai/smartai.chatgpt.middleware.js +25 -12
- package/dist_ts_providers/openai/smartai.limits.openai.d.ts +4 -1
- package/dist_ts_providers/openai/smartai.limits.openai.js +10 -5
- 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 +23 -11
- package/readme.md +1 -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_media_openai/index.ts +1 -2
- package/ts_media_openai/plugins.ts +6 -0
- package/ts_openai_account/index.ts +1 -1
- package/ts_openai_account/plugins.ts +37 -0
- package/ts_openai_account/smartai.providers.credentials.ts +3 -5
- package/ts_openai_account/smartai.providers.interfaces.ts +6 -12
- package/ts_openai_account/smartai.providers.openai.ts +18 -31
- package/ts_openai_auth/index.ts +1 -1
- package/ts_openai_auth/interfaces.ts +1 -1
- package/ts_openai_auth/plugins.ts +17 -0
- package/ts_openai_auth/smartai.auth.openai.ts +28 -33
- package/ts_openai_auth/smartai.connection.openai.ts +7 -11
- package/ts_openai_auth_files/index.ts +19 -28
- package/ts_openai_auth_files/plugins.ts +10 -0
- package/ts_providers/anthropic/index.ts +1 -1
- package/ts_providers/anthropic/plugins.ts +11 -1
- package/ts_providers/anthropic/smartai.middleware.anthropic.ts +3 -5
- 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/ollama/index.ts +2 -2
- package/ts_providers/ollama/interfaces.ts +2 -2
- package/ts_providers/ollama/plugins.ts +12 -0
- package/ts_providers/ollama/smartai.provider.ollama.ts +11 -21
- package/ts_providers/openai/index.ts +1 -1
- package/ts_providers/openai/plugins.ts +1 -0
- package/ts_providers/openai/readme.md +9 -6
- package/ts_providers/openai/smartai.chatgpt.catalog.ts +11 -0
- package/ts_providers/openai/smartai.chatgpt.middleware.ts +27 -11
- package/ts_providers/openai/smartai.limits.openai.ts +12 -4
- package/ts_providers/perplexity/index.ts +1 -1
- package/ts_providers/perplexity/plugins.ts +1 -1
- package/ts_providers/readme.md +1 -0
- package/ts_providers/xai/index.ts +1 -1
- package/ts_providers/xai/plugins.ts +1 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
|
|
3
|
+
const bodylessMethods: ReadonlySet<string> = new Set(['GET', 'HEAD']);
|
|
4
|
+
|
|
5
|
+
const toRequest = (incomingArg: plugins.http.IncomingMessage, signalArg: AbortSignal): Request => {
|
|
6
|
+
const headers = new Headers();
|
|
7
|
+
for (let index = 0; index + 1 < incomingArg.rawHeaders.length; index += 2) {
|
|
8
|
+
headers.append(incomingArg.rawHeaders[index], incomingArg.rawHeaders[index + 1]);
|
|
9
|
+
}
|
|
10
|
+
const method = incomingArg.method ?? 'GET';
|
|
11
|
+
// Only the path and query are taken from the request line; the Host header stays a header, for the
|
|
12
|
+
// service's `authenticate` to judge.
|
|
13
|
+
const url = new URL(incomingArg.url ?? '/', 'http://localhost');
|
|
14
|
+
return new Request(url, {
|
|
15
|
+
method,
|
|
16
|
+
headers,
|
|
17
|
+
signal: signalArg,
|
|
18
|
+
...(bodylessMethods.has(method)
|
|
19
|
+
? {}
|
|
20
|
+
// `Readable.toWeb` is typed with Node's own stream/web types, which the global `BodyInit` does not name.
|
|
21
|
+
: { body: plugins.Readable.toWeb(incomingArg) as ReadableStream<Uint8Array>, duplex: 'half' as const }),
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const writeResponse = async (
|
|
26
|
+
responseArg: Response,
|
|
27
|
+
outgoingArg: plugins.http.ServerResponse,
|
|
28
|
+
signalArg: AbortSignal,
|
|
29
|
+
): Promise<void> => {
|
|
30
|
+
if (signalArg.aborted) {
|
|
31
|
+
// The client left while the answer was prepared; cancelling the body releases what it holds, such as the
|
|
32
|
+
// model call and the ping timer of a stream.
|
|
33
|
+
await responseArg.body?.cancel(signalArg.reason);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
outgoingArg.writeHead(responseArg.status, Object.fromEntries(responseArg.headers));
|
|
37
|
+
outgoingArg.flushHeaders();
|
|
38
|
+
if (!responseArg.body) {
|
|
39
|
+
outgoingArg.end();
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
const reader = responseArg.body.getReader();
|
|
43
|
+
const onAbort = (): void => {
|
|
44
|
+
void reader.cancel(signalArg.reason).catch(() => undefined);
|
|
45
|
+
};
|
|
46
|
+
signalArg.addEventListener('abort', onAbort, { once: true });
|
|
47
|
+
let finished = false;
|
|
48
|
+
try {
|
|
49
|
+
while (!signalArg.aborted) {
|
|
50
|
+
const { done, value } = await reader.read();
|
|
51
|
+
if (done) {
|
|
52
|
+
finished = true;
|
|
53
|
+
break;
|
|
54
|
+
}
|
|
55
|
+
// A socket that closes while full aborts the signal, which ends this wait.
|
|
56
|
+
if (!outgoingArg.write(value)) await plugins.once(outgoingArg, 'drain', { signal: signalArg });
|
|
57
|
+
}
|
|
58
|
+
} finally {
|
|
59
|
+
signalArg.removeEventListener('abort', onAbort);
|
|
60
|
+
// A write that failed leaves the body unread; cancelling it releases the model call and the ping timer.
|
|
61
|
+
if (!finished) await reader.cancel(signalArg.reason).catch(() => undefined);
|
|
62
|
+
reader.releaseLock();
|
|
63
|
+
}
|
|
64
|
+
if (!signalArg.aborted) outgoingArg.end();
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Hosts an Anthropic Messages service on a Node HTTP server: `http.createServer(listener)`.
|
|
69
|
+
*
|
|
70
|
+
* A streamed answer is written as it is produced and waits for the socket when it is full. A client that
|
|
71
|
+
* disconnects aborts its request, which stops the model call behind it.
|
|
72
|
+
*/
|
|
73
|
+
export const createAnthropicMessagesNodeListener = (
|
|
74
|
+
serviceArg: plugins.IAnthropicMessagesService,
|
|
75
|
+
): ((incomingArg: plugins.http.IncomingMessage, outgoingArg: plugins.http.ServerResponse) => void) => (
|
|
76
|
+
incomingArg,
|
|
77
|
+
outgoingArg,
|
|
78
|
+
) => {
|
|
79
|
+
const abort = new AbortController();
|
|
80
|
+
outgoingArg.once('close', () => {
|
|
81
|
+
if (!outgoingArg.writableFinished) abort.abort(new Error('The client closed the connection.'));
|
|
82
|
+
});
|
|
83
|
+
void (async () => {
|
|
84
|
+
const response = await serviceArg.handle(toRequest(incomingArg, abort.signal));
|
|
85
|
+
await writeResponse(response, outgoingArg, abort.signal);
|
|
86
|
+
})().catch((errorArg: unknown) => {
|
|
87
|
+
// A client that left has no one to tell; its abort already ended the answer.
|
|
88
|
+
if (abort.signal.aborted) return;
|
|
89
|
+
if (!outgoingArg.headersSent) {
|
|
90
|
+
outgoingArg.writeHead(500, { 'content-type': 'application/json' });
|
|
91
|
+
outgoingArg.end(JSON.stringify({
|
|
92
|
+
type: 'error',
|
|
93
|
+
error: { type: 'api_error', message: `The request could not be served: ${errorArg instanceof Error ? errorArg.message : String(errorArg)}` },
|
|
94
|
+
}));
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
outgoingArg.destroy(errorArg instanceof Error ? errorArg : new Error(String(errorArg)));
|
|
98
|
+
});
|
|
99
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type * as http from 'node:http';
|
|
2
|
+
import { once } from 'node:events';
|
|
3
|
+
import { Readable } from 'node:stream';
|
|
4
|
+
|
|
5
|
+
export { once, Readable };
|
|
6
|
+
export type { http };
|
|
7
|
+
export type { IAnthropicMessagesService } from '@modelprofile.com/flexharness-providers/anthropic-messages';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
Hosts an Anthropic Messages service on a Node HTTP server.
|
|
2
|
+
|
|
3
|
+
Part of the [FlexHarness toolbox](https://code.foss.global/modelprofile.com/flexharness).
|
|
4
|
+
Install the components your project uses. Provider adapters are registered explicitly;
|
|
5
|
+
the agent and harness do not install or register vendor SDKs.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
pnpm add @modelprofile.com/flexharness-providers
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
See the [toolbox usage and migration guide](https://code.foss.global/modelprofile.com/flexharness#readme)
|
|
12
|
+
for composition examples and the SmartAI/SmartAgent migration map.
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
import * as http from 'node:http';
|
|
16
|
+
import { createAnthropicMessagesNodeListener } from '@modelprofile.com/flexharness-providers/anthropic-messages/node';
|
|
17
|
+
|
|
18
|
+
const server = http.createServer(createAnthropicMessagesNodeListener(service));
|
|
19
|
+
server.listen(0, '127.0.0.1');
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
A streamed answer is written as the model produces it and waits for the socket when it is full. A client that
|
|
23
|
+
disconnects aborts its request, which stops the model call behind it; one that leaves before the answer
|
|
24
|
+
begins gets no model call, or has the answer that was already prepared cancelled with its model call. The request line gives the path and
|
|
25
|
+
query only; the `Host` header stays a header for the service's `authenticate` to judge.
|
|
26
|
+
|
|
27
|
+
## Issue Reporting and Security
|
|
28
|
+
|
|
29
|
+
Report issues and security concerns through [community.foss.global](https://community.foss.global).
|
|
30
|
+
|
|
31
|
+
## License and Legal Information
|
|
32
|
+
|
|
33
|
+
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the repository license file.
|
|
34
|
+
|
|
35
|
+
**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.
|
|
36
|
+
|
|
37
|
+
### Trademarks
|
|
38
|
+
|
|
39
|
+
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.
|
|
40
|
+
|
|
41
|
+
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.
|
|
42
|
+
|
|
43
|
+
### Company Information
|
|
44
|
+
|
|
45
|
+
Task Venture Capital GmbH<br>
|
|
46
|
+
Registered at District Court Bremen HRB 35230 HB, Germany
|
|
47
|
+
|
|
48
|
+
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
|
49
|
+
|
|
50
|
+
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.
|
package/ts_media_openai/index.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import * as plugins from './plugins.js';
|
|
2
|
-
import { Readable } from 'node:stream';
|
|
3
2
|
|
|
4
3
|
export interface IOpenAiTtsOptions {
|
|
5
4
|
apiKey: string;
|
|
@@ -20,7 +19,7 @@ export async function textToSpeech(options: IOpenAiTtsOptions): Promise<NodeJS.R
|
|
|
20
19
|
speed: options.speed ?? 1,
|
|
21
20
|
});
|
|
22
21
|
const stream = result.body;
|
|
23
|
-
return Readable.fromWeb(stream as any);
|
|
22
|
+
return plugins.Readable.fromWeb(stream as any);
|
|
24
23
|
}
|
|
25
24
|
|
|
26
25
|
export * from './image.js';
|
|
@@ -12,3 +12,40 @@ export type { Server, Socket };
|
|
|
12
12
|
import * as openAi from '@modelprofile.com/flexharness-providers/openai';
|
|
13
13
|
|
|
14
14
|
export { openAi };
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
OPENAI_CHATGPT_AUTH_ISSUER,
|
|
18
|
+
OPENAI_CHATGPT_CLIENT_ID,
|
|
19
|
+
OPENAI_CHATGPT_CODEX_BASE_URL,
|
|
20
|
+
OPENAI_CHATGPT_DEFAULT_ORIGINATOR,
|
|
21
|
+
OpenAiChatGptAuthError,
|
|
22
|
+
completeOpenAiChatGptDeviceCodeLogin,
|
|
23
|
+
createOpenAiChatGptBrowserAuthorization,
|
|
24
|
+
exchangeOpenAiChatGptBrowserAuthorizationCode,
|
|
25
|
+
parseOpenAiChatGptBrowserCallback,
|
|
26
|
+
parseOpenAiChatGptTokenInfo,
|
|
27
|
+
refreshOpenAiChatGptTokenData,
|
|
28
|
+
requestOpenAiChatGptDeviceCode,
|
|
29
|
+
revokeOpenAiChatGptToken,
|
|
30
|
+
} from '@modelprofile.com/flexharness-providers/auth';
|
|
31
|
+
|
|
32
|
+
export {
|
|
33
|
+
OPENAI_CHATGPT_AUTH_ISSUER,
|
|
34
|
+
OPENAI_CHATGPT_CLIENT_ID,
|
|
35
|
+
OPENAI_CHATGPT_CODEX_BASE_URL,
|
|
36
|
+
OPENAI_CHATGPT_DEFAULT_ORIGINATOR,
|
|
37
|
+
OpenAiChatGptAuthError,
|
|
38
|
+
completeOpenAiChatGptDeviceCodeLogin,
|
|
39
|
+
createOpenAiChatGptBrowserAuthorization,
|
|
40
|
+
exchangeOpenAiChatGptBrowserAuthorizationCode,
|
|
41
|
+
parseOpenAiChatGptBrowserCallback,
|
|
42
|
+
parseOpenAiChatGptTokenInfo,
|
|
43
|
+
refreshOpenAiChatGptTokenData,
|
|
44
|
+
requestOpenAiChatGptDeviceCode,
|
|
45
|
+
revokeOpenAiChatGptToken,
|
|
46
|
+
};
|
|
47
|
+
export type {
|
|
48
|
+
IOpenAiChatGptAuthCredentials,
|
|
49
|
+
IOpenAiChatGptTokenData,
|
|
50
|
+
TOpenAiChatGptRequestOutcome,
|
|
51
|
+
} from '@modelprofile.com/flexharness-providers/auth';
|
|
@@ -1,7 +1,5 @@
|
|
|
1
1
|
import * as plugins from './plugins.js';
|
|
2
|
-
import { parseOpenAiChatGptTokenInfo } from '@modelprofile.com/flexharness-providers/auth';
|
|
3
2
|
import { createSmartAiProviderError } from './smartai.providers.error.js';
|
|
4
|
-
import type { IOpenAiChatGptAuthCredentials } from '@modelprofile.com/flexharness-providers/auth';
|
|
5
3
|
import type {
|
|
6
4
|
IOpenAiChatGptOAuthCredential,
|
|
7
5
|
TSmartAiProviderCredential,
|
|
@@ -65,7 +63,7 @@ const parseOAuthCredential = (
|
|
|
65
63
|
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
66
64
|
}
|
|
67
65
|
try {
|
|
68
|
-
parseOpenAiChatGptTokenInfo(valueArg.idToken);
|
|
66
|
+
plugins.parseOpenAiChatGptTokenInfo(valueArg.idToken);
|
|
69
67
|
} catch {
|
|
70
68
|
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
71
69
|
}
|
|
@@ -143,11 +141,11 @@ export const parseProviderCredential = (
|
|
|
143
141
|
|
|
144
142
|
export const toOpenAiChatGptAuthCredentials = (
|
|
145
143
|
credentialArg: IOpenAiChatGptOAuthCredential,
|
|
146
|
-
): IOpenAiChatGptAuthCredentials => {
|
|
144
|
+
): plugins.IOpenAiChatGptAuthCredentials => {
|
|
147
145
|
const credential = parseOAuthCredential(credentialArg as unknown as Record<string, unknown>);
|
|
148
146
|
let tokenInfo;
|
|
149
147
|
try {
|
|
150
|
-
tokenInfo = parseOpenAiChatGptTokenInfo(credential.idToken);
|
|
148
|
+
tokenInfo = plugins.parseOpenAiChatGptTokenInfo(credential.idToken);
|
|
151
149
|
} catch {
|
|
152
150
|
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
153
151
|
}
|
|
@@ -1,14 +1,8 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import type {
|
|
3
|
-
IOpenAiChatGptModel,
|
|
4
|
-
IOpenAiChatGptModelReasoningEffort,
|
|
5
|
-
IOpenAiChatGptModelServiceTier,
|
|
6
|
-
TOpenAiChatGptInputModality,
|
|
7
|
-
} from '@modelprofile.com/flexharness-providers/openai';
|
|
1
|
+
import type * as plugins from './plugins.js';
|
|
8
2
|
|
|
9
3
|
export type TSmartAiProviderId = string;
|
|
10
4
|
export type TSmartAiProviderLoginFlow = 'browser' | 'device';
|
|
11
|
-
export type TSmartAiProviderInputModality = TOpenAiChatGptInputModality;
|
|
5
|
+
export type TSmartAiProviderInputModality = plugins.openAi.TOpenAiChatGptInputModality;
|
|
12
6
|
|
|
13
7
|
export interface ISmartAiProviderDescriptor {
|
|
14
8
|
providerId: TSmartAiProviderId;
|
|
@@ -94,11 +88,11 @@ export interface ISmartAiProviderOperationOptions {
|
|
|
94
88
|
}
|
|
95
89
|
|
|
96
90
|
/** The catalog shapes are defined once, by the access-only catalog read in `/openai`. */
|
|
97
|
-
export interface ISmartAiProviderModelReasoningEffort extends IOpenAiChatGptModelReasoningEffort {}
|
|
91
|
+
export interface ISmartAiProviderModelReasoningEffort extends plugins.openAi.IOpenAiChatGptModelReasoningEffort {}
|
|
98
92
|
|
|
99
|
-
export interface ISmartAiProviderModelServiceTier extends IOpenAiChatGptModelServiceTier {}
|
|
93
|
+
export interface ISmartAiProviderModelServiceTier extends plugins.openAi.IOpenAiChatGptModelServiceTier {}
|
|
100
94
|
|
|
101
|
-
export interface ISmartAiProviderModel extends IOpenAiChatGptModel {}
|
|
95
|
+
export interface ISmartAiProviderModel extends plugins.openAi.IOpenAiChatGptModel {}
|
|
102
96
|
|
|
103
97
|
export interface ISmartAiProviderModelListOptions extends ISmartAiProviderOperationOptions {
|
|
104
98
|
cursor?: string;
|
|
@@ -121,7 +115,7 @@ export interface ISmartAiProviderRefreshFailure {
|
|
|
121
115
|
credential: IOpenAiChatGptOAuthCredential;
|
|
122
116
|
errorCode: TSmartAiProviderCredentialOperationErrorCode | 'REFRESH_FAILED';
|
|
123
117
|
/** Never replay a rotating grant when this is outcomeUnknown. */
|
|
124
|
-
requestOutcome: TOpenAiChatGptRequestOutcome;
|
|
118
|
+
requestOutcome: plugins.TOpenAiChatGptRequestOutcome;
|
|
125
119
|
/** The response status, when an HTTP response was observed. */
|
|
126
120
|
responseStatus?: number;
|
|
127
121
|
/** Bounded Retry-After delay from the observed response. */
|
|
@@ -1,17 +1,5 @@
|
|
|
1
1
|
// OpenAI protocol portions are adapted in TypeScript; see third-party-notices.md.
|
|
2
2
|
import * as plugins from './plugins.js';
|
|
3
|
-
import {
|
|
4
|
-
OPENAI_CHATGPT_DEFAULT_ORIGINATOR,
|
|
5
|
-
OpenAiChatGptAuthError,
|
|
6
|
-
completeOpenAiChatGptDeviceCodeLogin,
|
|
7
|
-
createOpenAiChatGptBrowserAuthorization,
|
|
8
|
-
exchangeOpenAiChatGptBrowserAuthorizationCode,
|
|
9
|
-
parseOpenAiChatGptBrowserCallback,
|
|
10
|
-
parseOpenAiChatGptTokenInfo,
|
|
11
|
-
refreshOpenAiChatGptTokenData,
|
|
12
|
-
requestOpenAiChatGptDeviceCode,
|
|
13
|
-
revokeOpenAiChatGptToken,
|
|
14
|
-
} from '@modelprofile.com/flexharness-providers/auth';
|
|
15
3
|
import { createSmartAiProviderError, SmartAiProviderError } from './smartai.providers.error.js';
|
|
16
4
|
import type {
|
|
17
5
|
IOpenAiChatGptOAuthCredential,
|
|
@@ -36,7 +24,6 @@ import type {
|
|
|
36
24
|
TSmartAiProviderModelListResult,
|
|
37
25
|
TSmartAiProviderRefreshResult,
|
|
38
26
|
} from './smartai.providers.interfaces.js';
|
|
39
|
-
import type { IOpenAiChatGptTokenData } from '@modelprofile.com/flexharness-providers/auth';
|
|
40
27
|
|
|
41
28
|
const defaultBrowserLoginTimeoutMs = 11 * 60 * 1000;
|
|
42
29
|
const defaultDeviceLoginTimeoutMs = 15 * 60 * 1000;
|
|
@@ -67,7 +54,7 @@ interface ISharedRefresh {
|
|
|
67
54
|
interface IBrowserCallbackServer {
|
|
68
55
|
port: number;
|
|
69
56
|
waitForCallback(
|
|
70
|
-
authorization: Awaited<ReturnType<typeof createOpenAiChatGptBrowserAuthorization>>,
|
|
57
|
+
authorization: Awaited<ReturnType<typeof plugins.createOpenAiChatGptBrowserAuthorization>>,
|
|
71
58
|
): Promise<string>;
|
|
72
59
|
close(): Promise<void>;
|
|
73
60
|
}
|
|
@@ -156,7 +143,7 @@ const classifyOperationError = <TFallback extends
|
|
|
156
143
|
return fallbackArg;
|
|
157
144
|
};
|
|
158
145
|
|
|
159
|
-
const toTokenData = (credentialArg: IOpenAiChatGptOAuthCredential): IOpenAiChatGptTokenData => {
|
|
146
|
+
const toTokenData = (credentialArg: IOpenAiChatGptOAuthCredential): plugins.IOpenAiChatGptTokenData => {
|
|
160
147
|
if (
|
|
161
148
|
credentialArg.kind !== 'chatgptOAuth'
|
|
162
149
|
|| credentialArg.providerId !== 'openai'
|
|
@@ -168,7 +155,7 @@ const toTokenData = (credentialArg: IOpenAiChatGptOAuthCredential): IOpenAiChatG
|
|
|
168
155
|
}
|
|
169
156
|
let tokenInfo;
|
|
170
157
|
try {
|
|
171
|
-
tokenInfo = parseOpenAiChatGptTokenInfo(credentialArg.idToken);
|
|
158
|
+
tokenInfo = plugins.parseOpenAiChatGptTokenInfo(credentialArg.idToken);
|
|
172
159
|
} catch {
|
|
173
160
|
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
174
161
|
}
|
|
@@ -216,7 +203,7 @@ const toReadAccess = (
|
|
|
216
203
|
};
|
|
217
204
|
|
|
218
205
|
const toCredential = (
|
|
219
|
-
tokenDataArg: IOpenAiChatGptTokenData,
|
|
206
|
+
tokenDataArg: plugins.IOpenAiChatGptTokenData,
|
|
220
207
|
lastRefreshAtArg?: string,
|
|
221
208
|
): IOpenAiChatGptOAuthCredential => {
|
|
222
209
|
if (!tokenDataArg.idToken) throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
@@ -281,7 +268,7 @@ const bindBrowserCallbackServer = async (
|
|
|
281
268
|
for (let index = 0; index < portsArg.length; index++) {
|
|
282
269
|
const port = portsArg[index];
|
|
283
270
|
const sockets = new Set<plugins.Socket>();
|
|
284
|
-
let authorization: Awaited<ReturnType<typeof createOpenAiChatGptBrowserAuthorization>> | undefined;
|
|
271
|
+
let authorization: Awaited<ReturnType<typeof plugins.createOpenAiChatGptBrowserAuthorization>> | undefined;
|
|
285
272
|
let settled = false;
|
|
286
273
|
let resolveCallback!: (url: string) => void;
|
|
287
274
|
let rejectCallback!: (error: SmartAiProviderError) => void;
|
|
@@ -320,7 +307,7 @@ const bindBrowserCallbackServer = async (
|
|
|
320
307
|
const callbackUrl = `http://${host}${requestUrl}`;
|
|
321
308
|
let providerDenied = false;
|
|
322
309
|
try {
|
|
323
|
-
const callback = parseOpenAiChatGptBrowserCallback(callbackUrl, {
|
|
310
|
+
const callback = plugins.parseOpenAiChatGptBrowserCallback(callbackUrl, {
|
|
324
311
|
expectedState: authorization.state,
|
|
325
312
|
expectedRedirectUri: authorization.redirectUri,
|
|
326
313
|
});
|
|
@@ -645,7 +632,7 @@ export class OpenAiProviderAdapter implements ISmartAiProviderAdapter {
|
|
|
645
632
|
constructor(optionsArg: IOpenAiProviderAdapterOptions = {}) {
|
|
646
633
|
this.fetchImplementation = optionsArg.fetch ?? globalThis.fetch;
|
|
647
634
|
if (!this.fetchImplementation) throw createSmartAiProviderError('INVALID_ARGUMENT');
|
|
648
|
-
this.originator = optionsArg.originator ?? OPENAI_CHATGPT_DEFAULT_ORIGINATOR;
|
|
635
|
+
this.originator = optionsArg.originator ?? plugins.OPENAI_CHATGPT_DEFAULT_ORIGINATOR;
|
|
649
636
|
this.now = optionsArg.now ?? (() => new Date());
|
|
650
637
|
}
|
|
651
638
|
|
|
@@ -712,13 +699,13 @@ export class OpenAiProviderAdapter implements ISmartAiProviderAdapter {
|
|
|
712
699
|
let prompt: ISmartAiProviderLoginHandle['prompt'];
|
|
713
700
|
let completion: Promise<ISmartAiProviderLoginResult>;
|
|
714
701
|
if (optionsArg.flow === 'device') {
|
|
715
|
-
const deviceCode = await requestOpenAiChatGptDeviceCode(this.authOptions(abortController.signal));
|
|
702
|
+
const deviceCode = await plugins.requestOpenAiChatGptDeviceCode(this.authOptions(abortController.signal));
|
|
716
703
|
prompt = {
|
|
717
704
|
flow: 'device',
|
|
718
705
|
verificationUrl: deviceCode.verificationUrl,
|
|
719
706
|
userCode: deviceCode.userCode,
|
|
720
707
|
};
|
|
721
|
-
completion = completeOpenAiChatGptDeviceCodeLogin(deviceCode, {
|
|
708
|
+
completion = plugins.completeOpenAiChatGptDeviceCodeLogin(deviceCode, {
|
|
722
709
|
...this.authOptions(abortController.signal),
|
|
723
710
|
timeoutMs,
|
|
724
711
|
}).then((tokenData) => {
|
|
@@ -727,21 +714,21 @@ export class OpenAiProviderAdapter implements ISmartAiProviderAdapter {
|
|
|
727
714
|
});
|
|
728
715
|
} else {
|
|
729
716
|
callbackServer = await bindBrowserCallbackServer([1455, 1457], abortController.signal);
|
|
730
|
-
const authorization = await createOpenAiChatGptBrowserAuthorization({
|
|
717
|
+
const authorization = await plugins.createOpenAiChatGptBrowserAuthorization({
|
|
731
718
|
...this.authOptions(abortController.signal),
|
|
732
719
|
redirectUri: `http://localhost:${callbackServer.port}/auth/callback`,
|
|
733
720
|
originator: this.originator,
|
|
734
721
|
});
|
|
735
722
|
prompt = { flow: 'browser', authUrl: authorization.authUrl };
|
|
736
723
|
completion = callbackServer.waitForCallback(authorization).then(async (callbackUrl) => {
|
|
737
|
-
const callback = parseOpenAiChatGptBrowserCallback(callbackUrl, {
|
|
724
|
+
const callback = plugins.parseOpenAiChatGptBrowserCallback(callbackUrl, {
|
|
738
725
|
expectedState: authorization.state,
|
|
739
726
|
expectedRedirectUri: authorization.redirectUri,
|
|
740
727
|
});
|
|
741
728
|
if ('providerError' in callback) {
|
|
742
729
|
throw createSmartAiProviderError('LOGIN_CANCELED');
|
|
743
730
|
}
|
|
744
|
-
const tokenData = await exchangeOpenAiChatGptBrowserAuthorizationCode(
|
|
731
|
+
const tokenData = await plugins.exchangeOpenAiChatGptBrowserAuthorizationCode(
|
|
745
732
|
callback.authorizationCode,
|
|
746
733
|
{
|
|
747
734
|
...this.authOptions(abortController.signal),
|
|
@@ -855,7 +842,7 @@ export class OpenAiProviderAdapter implements ISmartAiProviderAdapter {
|
|
|
855
842
|
const operation = createOperation(optionsArg, defaultOperationTimeoutMs);
|
|
856
843
|
this.operations.add(operation.controller);
|
|
857
844
|
try {
|
|
858
|
-
const refreshed = await refreshOpenAiChatGptTokenData(input, this.authOptions(operation.signal));
|
|
845
|
+
const refreshed = await plugins.refreshOpenAiChatGptTokenData(input, this.authOptions(operation.signal));
|
|
859
846
|
const tokenChanged = refreshed.accessToken !== input.accessToken
|
|
860
847
|
|| refreshed.refreshToken !== input.refreshToken;
|
|
861
848
|
if (!tokenChanged || !refreshed.idToken) {
|
|
@@ -879,13 +866,13 @@ export class OpenAiProviderAdapter implements ISmartAiProviderAdapter {
|
|
|
879
866
|
success: false,
|
|
880
867
|
credential: credentialArg,
|
|
881
868
|
errorCode: classifyOperationError(errorArg, operation, 'REFRESH_FAILED'),
|
|
882
|
-
requestOutcome: errorArg instanceof OpenAiChatGptAuthError
|
|
869
|
+
requestOutcome: errorArg instanceof plugins.OpenAiChatGptAuthError
|
|
883
870
|
? errorArg.requestOutcome ?? 'outcomeUnknown'
|
|
884
871
|
: 'outcomeUnknown',
|
|
885
|
-
...(errorArg instanceof OpenAiChatGptAuthError && errorArg.status !== undefined
|
|
872
|
+
...(errorArg instanceof plugins.OpenAiChatGptAuthError && errorArg.status !== undefined
|
|
886
873
|
? { responseStatus: errorArg.status }
|
|
887
874
|
: {}),
|
|
888
|
-
...(errorArg instanceof OpenAiChatGptAuthError && errorArg.retryAfterMs !== undefined
|
|
875
|
+
...(errorArg instanceof plugins.OpenAiChatGptAuthError && errorArg.retryAfterMs !== undefined
|
|
889
876
|
? { retryAfterMs: errorArg.retryAfterMs }
|
|
890
877
|
: {}),
|
|
891
878
|
};
|
|
@@ -907,7 +894,7 @@ export class OpenAiProviderAdapter implements ISmartAiProviderAdapter {
|
|
|
907
894
|
});
|
|
908
895
|
} catch (errorArg) {
|
|
909
896
|
if (signalArg.aborted) throw createSmartAiProviderError('ABORTED');
|
|
910
|
-
if (errorArg instanceof OpenAiChatGptAuthError && errorArg.status === 401) {
|
|
897
|
+
if (errorArg instanceof plugins.OpenAiChatGptAuthError && errorArg.status === 401) {
|
|
911
898
|
throw new ModelHttpError(401);
|
|
912
899
|
}
|
|
913
900
|
if (errorArg instanceof plugins.openAi.OpenAiChatGptModelListError) {
|
|
@@ -1104,7 +1091,7 @@ export class OpenAiProviderAdapter implements ISmartAiProviderAdapter {
|
|
|
1104
1091
|
const operation = createOperation(optionsArg, defaultOperationTimeoutMs);
|
|
1105
1092
|
this.operations.add(operation.controller);
|
|
1106
1093
|
try {
|
|
1107
|
-
await revokeOpenAiChatGptToken(credentialArg.refreshToken, {
|
|
1094
|
+
await plugins.revokeOpenAiChatGptToken(credentialArg.refreshToken, {
|
|
1108
1095
|
...this.authOptions(operation.signal),
|
|
1109
1096
|
tokenTypeHint: 'refresh_token',
|
|
1110
1097
|
});
|
package/ts_openai_auth/index.ts
CHANGED
|
@@ -70,7 +70,7 @@ export interface IOpenAiChatGptAuthOptions {
|
|
|
70
70
|
}
|
|
71
71
|
|
|
72
72
|
/** A failed refresh is replayable only when no request was handed to fetch. */
|
|
73
|
-
export type { TOpenAiChatGptRequestOutcome } from '
|
|
73
|
+
export type { TOpenAiChatGptRequestOutcome } from './plugins.js';
|
|
74
74
|
|
|
75
75
|
export interface IOpenAiChatGptBrowserAuthorizationOptions extends IOpenAiChatGptAuthOptions {
|
|
76
76
|
redirectUri: string;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// @modelprofile.com scope
|
|
2
|
+
import {
|
|
3
|
+
classifyOpenAiChatGptLimit,
|
|
4
|
+
createOpenAiChatGptInstructionsMiddleware,
|
|
5
|
+
OPENAI_CHATGPT_CODEX_BASE_URL,
|
|
6
|
+
OPENAI_CHATGPT_DEFAULT_ORIGINATOR,
|
|
7
|
+
OpenAiChatGptAuthError,
|
|
8
|
+
} from '@modelprofile.com/flexharness-providers/openai';
|
|
9
|
+
|
|
10
|
+
export {
|
|
11
|
+
classifyOpenAiChatGptLimit,
|
|
12
|
+
createOpenAiChatGptInstructionsMiddleware,
|
|
13
|
+
OPENAI_CHATGPT_CODEX_BASE_URL,
|
|
14
|
+
OPENAI_CHATGPT_DEFAULT_ORIGINATOR,
|
|
15
|
+
OpenAiChatGptAuthError,
|
|
16
|
+
};
|
|
17
|
+
export type { IOpenAiModelConnection, TOpenAiChatGptRequestOutcome } from '@modelprofile.com/flexharness-providers/openai';
|