@modelprofile.com/flexharness-providers 7.0.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_media_openai/image.d.ts +46 -0
- package/dist_ts_media_openai/image.js +110 -0
- package/dist_ts_media_openai/index.d.ts +10 -0
- package/dist_ts_media_openai/index.js +16 -0
- package/dist_ts_media_openai/plugins.d.ts +3 -0
- package/dist_ts_media_openai/plugins.js +4 -0
- package/dist_ts_openai_account/index.d.ts +6 -0
- package/dist_ts_openai_account/index.js +7 -0
- package/dist_ts_openai_account/plugins.d.ts +7 -0
- package/dist_ts_openai_account/plugins.js +6 -0
- package/dist_ts_openai_account/smartai.providers.credentials.d.ts +7 -0
- package/dist_ts_openai_account/smartai.providers.credentials.js +126 -0
- package/dist_ts_openai_account/smartai.providers.error.d.ts +11 -0
- package/dist_ts_openai_account/smartai.providers.error.js +30 -0
- package/dist_ts_openai_account/smartai.providers.interfaces.d.ts +161 -0
- package/dist_ts_openai_account/smartai.providers.interfaces.js +2 -0
- package/dist_ts_openai_account/smartai.providers.openai.d.ts +29 -0
- package/dist_ts_openai_account/smartai.providers.openai.js +1075 -0
- package/dist_ts_openai_account/smartai.providers.registry.d.ts +11 -0
- package/dist_ts_openai_account/smartai.providers.registry.js +53 -0
- package/dist_ts_openai_auth/index.d.ts +6 -0
- package/dist_ts_openai_auth/index.js +10 -0
- package/dist_ts_openai_auth/interfaces.d.ts +71 -0
- package/dist_ts_openai_auth/interfaces.js +2 -0
- package/dist_ts_openai_auth/smartai.auth.openai.d.ts +34 -0
- package/dist_ts_openai_auth/smartai.auth.openai.js +603 -0
- package/dist_ts_openai_auth/smartai.middleware.openai.d.ts +6 -0
- package/dist_ts_openai_auth/smartai.middleware.openai.js +47 -0
- package/dist_ts_openai_auth_files/index.d.ts +43 -0
- package/dist_ts_openai_auth_files/index.js +377 -0
- package/dist_ts_openai_auth_files/plugins.d.ts +9 -0
- package/dist_ts_openai_auth_files/plugins.js +10 -0
- package/dist_ts_providers/anthropic/index.d.ts +8 -0
- package/dist_ts_providers/anthropic/index.js +13 -0
- package/dist_ts_providers/anthropic/plugins.d.ts +4 -0
- package/dist_ts_providers/anthropic/plugins.js +4 -0
- package/dist_ts_providers/anthropic/smartai.middleware.anthropic.d.ts +8 -0
- package/dist_ts_providers/anthropic/smartai.middleware.anthropic.js +10 -0
- package/dist_ts_providers/google/index.d.ts +5 -0
- package/dist_ts_providers/google/index.js +6 -0
- package/dist_ts_providers/google/plugins.d.ts +3 -0
- package/dist_ts_providers/google/plugins.js +3 -0
- package/dist_ts_providers/groq/index.d.ts +5 -0
- package/dist_ts_providers/groq/index.js +6 -0
- package/dist_ts_providers/groq/plugins.d.ts +3 -0
- package/dist_ts_providers/groq/plugins.js +3 -0
- package/dist_ts_providers/index.d.ts +8 -0
- package/dist_ts_providers/index.js +9 -0
- package/dist_ts_providers/mistral/index.d.ts +5 -0
- package/dist_ts_providers/mistral/index.js +6 -0
- package/dist_ts_providers/mistral/plugins.d.ts +3 -0
- package/dist_ts_providers/mistral/plugins.js +3 -0
- package/dist_ts_providers/ollama/index.d.ts +6 -0
- package/dist_ts_providers/ollama/index.js +7 -0
- package/dist_ts_providers/ollama/interfaces.d.ts +26 -0
- package/dist_ts_providers/ollama/interfaces.js +2 -0
- package/dist_ts_providers/ollama/smartai.provider.ollama.d.ts +8 -0
- package/dist_ts_providers/ollama/smartai.provider.ollama.js +378 -0
- package/dist_ts_providers/openai/index.d.ts +15 -0
- package/dist_ts_providers/openai/index.js +14 -0
- package/dist_ts_providers/openai/plugins.d.ts +6 -0
- package/dist_ts_providers/openai/plugins.js +4 -0
- package/dist_ts_providers/perplexity/index.d.ts +5 -0
- package/dist_ts_providers/perplexity/index.js +6 -0
- package/dist_ts_providers/perplexity/plugins.d.ts +3 -0
- package/dist_ts_providers/perplexity/plugins.js +3 -0
- package/dist_ts_providers/xai/index.d.ts +5 -0
- package/dist_ts_providers/xai/index.js +6 -0
- package/dist_ts_providers/xai/plugins.d.ts +3 -0
- package/dist_ts_providers/xai/plugins.js +3 -0
- package/dist_ts_research/index.d.ts +19 -0
- package/dist_ts_research/index.js +98 -0
- package/dist_ts_research/plugins.d.ts +2 -0
- package/dist_ts_research/plugins.js +3 -0
- package/license.md +21 -0
- package/openai-codex-license.txt +201 -0
- package/package.json +105 -0
- package/readme.md +62 -0
- package/third-party-notices.md +19 -0
- package/ts_media_openai/image.ts +147 -0
- package/ts_media_openai/index.ts +26 -0
- package/ts_media_openai/plugins.ts +3 -0
- package/ts_media_openai/readme.md +37 -0
- package/ts_openai_account/index.ts +11 -0
- package/ts_openai_account/plugins.ts +9 -0
- package/ts_openai_account/readme.md +37 -0
- package/ts_openai_account/smartai.providers.credentials.ts +163 -0
- package/ts_openai_account/smartai.providers.error.ts +49 -0
- package/ts_openai_account/smartai.providers.interfaces.ts +222 -0
- package/ts_openai_account/smartai.providers.openai.ts +1209 -0
- package/ts_openai_account/smartai.providers.registry.ts +57 -0
- package/ts_openai_auth/index.ts +12 -0
- package/ts_openai_auth/interfaces.ts +89 -0
- package/ts_openai_auth/readme.md +37 -0
- package/ts_openai_auth/smartai.auth.openai.ts +789 -0
- package/ts_openai_auth/smartai.middleware.openai.ts +55 -0
- package/ts_openai_auth_files/index.ts +499 -0
- package/ts_openai_auth_files/plugins.ts +13 -0
- package/ts_openai_auth_files/readme.md +37 -0
- package/ts_providers/anthropic/index.ts +20 -0
- package/ts_providers/anthropic/plugins.ts +4 -0
- package/ts_providers/anthropic/readme.md +37 -0
- package/ts_providers/anthropic/smartai.middleware.anthropic.ts +12 -0
- package/ts_providers/google/index.ts +8 -0
- package/ts_providers/google/plugins.ts +3 -0
- package/ts_providers/google/readme.md +37 -0
- package/ts_providers/groq/index.ts +8 -0
- package/ts_providers/groq/plugins.ts +3 -0
- package/ts_providers/groq/readme.md +37 -0
- package/ts_providers/index.ts +8 -0
- package/ts_providers/mistral/index.ts +8 -0
- package/ts_providers/mistral/plugins.ts +3 -0
- package/ts_providers/mistral/readme.md +37 -0
- package/ts_providers/ollama/index.ts +8 -0
- package/ts_providers/ollama/interfaces.ts +28 -0
- package/ts_providers/ollama/readme.md +37 -0
- package/ts_providers/ollama/smartai.provider.ollama.ts +426 -0
- package/ts_providers/openai/index.ts +29 -0
- package/ts_providers/openai/plugins.ts +6 -0
- package/ts_providers/openai/readme.md +37 -0
- package/ts_providers/perplexity/index.ts +8 -0
- package/ts_providers/perplexity/plugins.ts +3 -0
- package/ts_providers/perplexity/readme.md +37 -0
- package/ts_providers/readme.md +62 -0
- package/ts_providers/xai/index.ts +8 -0
- package/ts_providers/xai/plugins.ts +3 -0
- package/ts_providers/xai/readme.md +37 -0
- package/ts_research/index.ts +120 -0
- package/ts_research/plugins.ts +2 -0
- package/ts_research/readme.md +37 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
import { Readable } from 'node:stream';
|
|
3
|
+
|
|
4
|
+
export interface IOpenAiTtsOptions {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
text: string;
|
|
7
|
+
voice?: 'alloy' | 'echo' | 'fable' | 'onyx' | 'nova' | 'shimmer';
|
|
8
|
+
model?: 'tts-1' | 'tts-1-hd';
|
|
9
|
+
responseFormat?: 'mp3' | 'opus' | 'aac' | 'flac';
|
|
10
|
+
speed?: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function textToSpeech(options: IOpenAiTtsOptions): Promise<NodeJS.ReadableStream> {
|
|
14
|
+
const client = new plugins.OpenAI({ apiKey: options.apiKey });
|
|
15
|
+
const result = await client.audio.speech.create({
|
|
16
|
+
model: options.model ?? 'tts-1',
|
|
17
|
+
voice: options.voice ?? 'alloy',
|
|
18
|
+
input: options.text,
|
|
19
|
+
response_format: options.responseFormat ?? 'mp3',
|
|
20
|
+
speed: options.speed ?? 1,
|
|
21
|
+
});
|
|
22
|
+
const stream = result.body;
|
|
23
|
+
return Readable.fromWeb(stream as any);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export * from './image.js';
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
OpenAI audio and image generation using the OpenAI client SDK.
|
|
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
|
+
## Issue Reporting and Security
|
|
15
|
+
|
|
16
|
+
Report issues and security concerns through [community.foss.global](https://community.foss.global).
|
|
17
|
+
|
|
18
|
+
## License and Legal Information
|
|
19
|
+
|
|
20
|
+
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the repository license file.
|
|
21
|
+
|
|
22
|
+
**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.
|
|
23
|
+
|
|
24
|
+
### Trademarks
|
|
25
|
+
|
|
26
|
+
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.
|
|
27
|
+
|
|
28
|
+
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.
|
|
29
|
+
|
|
30
|
+
### Company Information
|
|
31
|
+
|
|
32
|
+
Task Venture Capital GmbH<br>
|
|
33
|
+
Registered at District Court Bremen HRB 35230 HB, Germany
|
|
34
|
+
|
|
35
|
+
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
|
36
|
+
|
|
37
|
+
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.
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export * from './smartai.providers.interfaces.js';
|
|
2
|
+
export * from './smartai.providers.error.js';
|
|
3
|
+
export * from './smartai.providers.credentials.js';
|
|
4
|
+
export * from './smartai.providers.openai.js';
|
|
5
|
+
export * from './smartai.providers.registry.js';
|
|
6
|
+
export {
|
|
7
|
+
OPENAI_CHATGPT_AUTH_ISSUER,
|
|
8
|
+
OPENAI_CHATGPT_CLIENT_ID,
|
|
9
|
+
OPENAI_CHATGPT_CODEX_BASE_URL,
|
|
10
|
+
OPENAI_CHATGPT_DEFAULT_ORIGINATOR,
|
|
11
|
+
} from '@modelprofile.com/flexharness-providers/auth';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// node native scope
|
|
2
|
+
import * as crypto from 'node:crypto';
|
|
3
|
+
import * as http from 'node:http';
|
|
4
|
+
import { Buffer } from 'node:buffer';
|
|
5
|
+
import type { Server } from 'node:http';
|
|
6
|
+
import type { Socket } from 'node:net';
|
|
7
|
+
|
|
8
|
+
export { Buffer, crypto, http };
|
|
9
|
+
export type { Server, Socket };
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
OpenAI account lifecycle, authenticated model catalogs, rate limits and credential envelopes.
|
|
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
|
+
## Issue Reporting and Security
|
|
15
|
+
|
|
16
|
+
Report issues and security concerns through [community.foss.global](https://community.foss.global).
|
|
17
|
+
|
|
18
|
+
## License and Legal Information
|
|
19
|
+
|
|
20
|
+
This repository contains open-source code licensed under the MIT License. A copy of the license can be found in the repository license file.
|
|
21
|
+
|
|
22
|
+
**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.
|
|
23
|
+
|
|
24
|
+
### Trademarks
|
|
25
|
+
|
|
26
|
+
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.
|
|
27
|
+
|
|
28
|
+
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.
|
|
29
|
+
|
|
30
|
+
### Company Information
|
|
31
|
+
|
|
32
|
+
Task Venture Capital GmbH<br>
|
|
33
|
+
Registered at District Court Bremen HRB 35230 HB, Germany
|
|
34
|
+
|
|
35
|
+
For any legal inquiries or further information, please contact us via email at hello@task.vc.
|
|
36
|
+
|
|
37
|
+
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.
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import * as plugins from './plugins.js';
|
|
2
|
+
import { parseOpenAiChatGptTokenInfo } from '@modelprofile.com/flexharness-providers/auth';
|
|
3
|
+
import { createSmartAiProviderError } from './smartai.providers.error.js';
|
|
4
|
+
import type { IOpenAiChatGptAuthCredentials } from '@modelprofile.com/flexharness-providers/auth';
|
|
5
|
+
import type {
|
|
6
|
+
IOpenAiChatGptOAuthCredential,
|
|
7
|
+
TSmartAiProviderCredential,
|
|
8
|
+
} from './smartai.providers.interfaces.js';
|
|
9
|
+
|
|
10
|
+
export const providerCredentialEnvelopeVersion = 1 as const;
|
|
11
|
+
export const providerCredentialEnvelopeMaxBytes = 16_384;
|
|
12
|
+
|
|
13
|
+
interface IProviderCredentialEnvelopeV1 {
|
|
14
|
+
schemaVersion: typeof providerCredentialEnvelopeVersion;
|
|
15
|
+
credential: TSmartAiProviderCredential;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const isPlainObject = (valueArg: unknown): valueArg is Record<string, unknown> =>
|
|
19
|
+
typeof valueArg === 'object'
|
|
20
|
+
&& valueArg !== null
|
|
21
|
+
&& !Array.isArray(valueArg)
|
|
22
|
+
&& (
|
|
23
|
+
Object.getPrototypeOf(valueArg) === Object.prototype
|
|
24
|
+
|| Object.getPrototypeOf(valueArg) === null
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
const hasExactKeys = (valueArg: Record<string, unknown>, keysArg: readonly string[]): boolean => {
|
|
28
|
+
const keys = Object.keys(valueArg);
|
|
29
|
+
return keys.length === keysArg.length && keysArg.every((key) => Object.hasOwn(valueArg, key));
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const boundedString = (valueArg: unknown, maximumBytesArg: number): valueArg is string =>
|
|
33
|
+
typeof valueArg === 'string'
|
|
34
|
+
&& valueArg.length > 0
|
|
35
|
+
&& plugins.Buffer.byteLength(valueArg, 'utf8') <= maximumBytesArg;
|
|
36
|
+
|
|
37
|
+
const parseOAuthCredential = (
|
|
38
|
+
valueArg: Record<string, unknown>,
|
|
39
|
+
): IOpenAiChatGptOAuthCredential => {
|
|
40
|
+
const required = [
|
|
41
|
+
'kind',
|
|
42
|
+
'providerId',
|
|
43
|
+
'accessToken',
|
|
44
|
+
'refreshToken',
|
|
45
|
+
'idToken',
|
|
46
|
+
];
|
|
47
|
+
const optional = ['lastRefreshAt'];
|
|
48
|
+
const keys = Object.keys(valueArg);
|
|
49
|
+
if (
|
|
50
|
+
!required.every((key) => Object.hasOwn(valueArg, key))
|
|
51
|
+
|| keys.some((key) => !required.includes(key) && !optional.includes(key))
|
|
52
|
+
|| valueArg.kind !== 'chatgptOAuth'
|
|
53
|
+
|| valueArg.providerId !== 'openai'
|
|
54
|
+
|| !boundedString(valueArg.accessToken, providerCredentialEnvelopeMaxBytes)
|
|
55
|
+
|| !boundedString(valueArg.refreshToken, providerCredentialEnvelopeMaxBytes)
|
|
56
|
+
|| !boundedString(valueArg.idToken, providerCredentialEnvelopeMaxBytes)
|
|
57
|
+
|| (
|
|
58
|
+
valueArg.lastRefreshAt !== undefined
|
|
59
|
+
&& (
|
|
60
|
+
!boundedString(valueArg.lastRefreshAt, 128)
|
|
61
|
+
|| !Number.isFinite(Date.parse(valueArg.lastRefreshAt))
|
|
62
|
+
)
|
|
63
|
+
)
|
|
64
|
+
) {
|
|
65
|
+
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
66
|
+
}
|
|
67
|
+
try {
|
|
68
|
+
parseOpenAiChatGptTokenInfo(valueArg.idToken);
|
|
69
|
+
} catch {
|
|
70
|
+
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
kind: 'chatgptOAuth',
|
|
74
|
+
providerId: 'openai',
|
|
75
|
+
accessToken: valueArg.accessToken,
|
|
76
|
+
refreshToken: valueArg.refreshToken,
|
|
77
|
+
idToken: valueArg.idToken,
|
|
78
|
+
...(typeof valueArg.lastRefreshAt === 'string'
|
|
79
|
+
? { lastRefreshAt: valueArg.lastRefreshAt }
|
|
80
|
+
: {}),
|
|
81
|
+
};
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const parseCredential = (valueArg: unknown): TSmartAiProviderCredential => {
|
|
85
|
+
if (!isPlainObject(valueArg)) throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
86
|
+
if (valueArg.kind === 'apiKey') {
|
|
87
|
+
if (
|
|
88
|
+
!hasExactKeys(valueArg, ['kind', 'providerId', 'apiKey'])
|
|
89
|
+
|| !boundedString(valueArg.providerId, 256)
|
|
90
|
+
|| !boundedString(valueArg.apiKey, providerCredentialEnvelopeMaxBytes)
|
|
91
|
+
) {
|
|
92
|
+
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
93
|
+
}
|
|
94
|
+
return { kind: 'apiKey', providerId: valueArg.providerId, apiKey: valueArg.apiKey };
|
|
95
|
+
}
|
|
96
|
+
return parseOAuthCredential(valueArg);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export const serializeProviderCredential = (
|
|
100
|
+
credentialArg: TSmartAiProviderCredential,
|
|
101
|
+
): Uint8Array => {
|
|
102
|
+
const credential = parseCredential(credentialArg);
|
|
103
|
+
const envelope: IProviderCredentialEnvelopeV1 = {
|
|
104
|
+
schemaVersion: providerCredentialEnvelopeVersion,
|
|
105
|
+
credential,
|
|
106
|
+
};
|
|
107
|
+
const bytes = plugins.Buffer.from(JSON.stringify(envelope), 'utf8');
|
|
108
|
+
if (bytes.byteLength > providerCredentialEnvelopeMaxBytes) {
|
|
109
|
+
bytes.fill(0);
|
|
110
|
+
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
111
|
+
}
|
|
112
|
+
return Uint8Array.from(bytes);
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
export const parseProviderCredential = (
|
|
116
|
+
envelopeBytesArg: Uint8Array,
|
|
117
|
+
): TSmartAiProviderCredential => {
|
|
118
|
+
if (
|
|
119
|
+
!(envelopeBytesArg instanceof Uint8Array)
|
|
120
|
+
|| envelopeBytesArg.byteLength < 1
|
|
121
|
+
|| envelopeBytesArg.byteLength > providerCredentialEnvelopeMaxBytes
|
|
122
|
+
) {
|
|
123
|
+
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
124
|
+
}
|
|
125
|
+
const copiedBytes = plugins.Buffer.from(envelopeBytesArg);
|
|
126
|
+
let parsed: unknown;
|
|
127
|
+
try {
|
|
128
|
+
parsed = JSON.parse(copiedBytes.toString('utf8')) as unknown;
|
|
129
|
+
} catch {
|
|
130
|
+
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
131
|
+
} finally {
|
|
132
|
+
copiedBytes.fill(0);
|
|
133
|
+
}
|
|
134
|
+
if (
|
|
135
|
+
!isPlainObject(parsed)
|
|
136
|
+
|| !hasExactKeys(parsed, ['schemaVersion', 'credential'])
|
|
137
|
+
|| parsed.schemaVersion !== providerCredentialEnvelopeVersion
|
|
138
|
+
) {
|
|
139
|
+
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
140
|
+
}
|
|
141
|
+
return parseCredential(parsed.credential);
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
export const toOpenAiChatGptAuthCredentials = (
|
|
145
|
+
credentialArg: IOpenAiChatGptOAuthCredential,
|
|
146
|
+
): IOpenAiChatGptAuthCredentials => {
|
|
147
|
+
const credential = parseOAuthCredential(credentialArg as unknown as Record<string, unknown>);
|
|
148
|
+
let tokenInfo;
|
|
149
|
+
try {
|
|
150
|
+
tokenInfo = parseOpenAiChatGptTokenInfo(credential.idToken);
|
|
151
|
+
} catch {
|
|
152
|
+
throw createSmartAiProviderError('CREDENTIAL_INVALID');
|
|
153
|
+
}
|
|
154
|
+
return {
|
|
155
|
+
accessToken: credential.accessToken,
|
|
156
|
+
refreshToken: credential.refreshToken,
|
|
157
|
+
idToken: credential.idToken,
|
|
158
|
+
...(tokenInfo.chatgptAccountId ? { accountId: tokenInfo.chatgptAccountId } : {}),
|
|
159
|
+
tokenInfo: {
|
|
160
|
+
...tokenInfo,
|
|
161
|
+
},
|
|
162
|
+
};
|
|
163
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export type TSmartAiProviderErrorCode =
|
|
2
|
+
| 'ABORTED'
|
|
3
|
+
| 'CLOSED'
|
|
4
|
+
| 'CREDENTIAL_INVALID'
|
|
5
|
+
| 'INVALID_ARGUMENT'
|
|
6
|
+
| 'LOGIN_BUSY'
|
|
7
|
+
| 'LOGIN_CANCELED'
|
|
8
|
+
| 'LOGIN_FAILED'
|
|
9
|
+
| 'MODEL_LIST_FAILED'
|
|
10
|
+
| 'NETWORK_FAILED'
|
|
11
|
+
| 'PROTOCOL_ERROR'
|
|
12
|
+
| 'REFRESH_FAILED'
|
|
13
|
+
| 'TIMEOUT';
|
|
14
|
+
|
|
15
|
+
const codes = new Set<TSmartAiProviderErrorCode>([
|
|
16
|
+
'ABORTED',
|
|
17
|
+
'CLOSED',
|
|
18
|
+
'CREDENTIAL_INVALID',
|
|
19
|
+
'INVALID_ARGUMENT',
|
|
20
|
+
'LOGIN_BUSY',
|
|
21
|
+
'LOGIN_CANCELED',
|
|
22
|
+
'LOGIN_FAILED',
|
|
23
|
+
'MODEL_LIST_FAILED',
|
|
24
|
+
'NETWORK_FAILED',
|
|
25
|
+
'PROTOCOL_ERROR',
|
|
26
|
+
'REFRESH_FAILED',
|
|
27
|
+
'TIMEOUT',
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
export class SmartAiProviderError extends Error {
|
|
31
|
+
public readonly code: TSmartAiProviderErrorCode;
|
|
32
|
+
|
|
33
|
+
constructor(codeArg: TSmartAiProviderErrorCode) {
|
|
34
|
+
const code = codes.has(codeArg) ? codeArg : 'PROTOCOL_ERROR';
|
|
35
|
+
super(`SmartAI provider operation failed (${code}).`);
|
|
36
|
+
this.name = 'SmartAiProviderError';
|
|
37
|
+
this.code = code;
|
|
38
|
+
Object.setPrototypeOf(this, new.target.prototype);
|
|
39
|
+
this.stack = `${this.name}: ${this.message}`;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
public toJSON(): { name: string; code: TSmartAiProviderErrorCode; message: string } {
|
|
43
|
+
return { name: this.name, code: this.code, message: this.message };
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const createSmartAiProviderError = (
|
|
48
|
+
codeArg: TSmartAiProviderErrorCode,
|
|
49
|
+
): SmartAiProviderError => new SmartAiProviderError(codeArg);
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
export type TSmartAiProviderId = string;
|
|
2
|
+
export type TSmartAiProviderLoginFlow = 'browser' | 'device';
|
|
3
|
+
export type TSmartAiProviderInputModality = 'text' | 'image' | 'audio';
|
|
4
|
+
|
|
5
|
+
export interface ISmartAiProviderDescriptor {
|
|
6
|
+
providerId: TSmartAiProviderId;
|
|
7
|
+
displayName: string;
|
|
8
|
+
loginFlows: readonly TSmartAiProviderLoginFlow[];
|
|
9
|
+
supportsApiKey: boolean;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface ISmartAiProviderApiKeyCredential {
|
|
13
|
+
kind: 'apiKey';
|
|
14
|
+
providerId: TSmartAiProviderId;
|
|
15
|
+
apiKey: string;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface IOpenAiChatGptOAuthCredential {
|
|
19
|
+
kind: 'chatgptOAuth';
|
|
20
|
+
providerId: 'openai';
|
|
21
|
+
accessToken: string;
|
|
22
|
+
refreshToken: string;
|
|
23
|
+
idToken: string;
|
|
24
|
+
lastRefreshAt?: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type TSmartAiProviderCredential =
|
|
28
|
+
| ISmartAiProviderApiKeyCredential
|
|
29
|
+
| IOpenAiChatGptOAuthCredential;
|
|
30
|
+
|
|
31
|
+
export interface ISmartAiProviderAccountSummary {
|
|
32
|
+
providerId: TSmartAiProviderId;
|
|
33
|
+
authKind: TSmartAiProviderCredential['kind'];
|
|
34
|
+
accountId?: string;
|
|
35
|
+
email?: string;
|
|
36
|
+
plan?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface ISmartAiProviderLoginResult {
|
|
40
|
+
credential: IOpenAiChatGptOAuthCredential;
|
|
41
|
+
account: ISmartAiProviderAccountSummary;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export interface ISmartAiProviderBrowserLoginPrompt {
|
|
45
|
+
flow: 'browser';
|
|
46
|
+
authUrl: string;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface ISmartAiProviderDeviceLoginPrompt {
|
|
50
|
+
flow: 'device';
|
|
51
|
+
verificationUrl: string;
|
|
52
|
+
userCode: string;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export type TSmartAiProviderLoginPrompt =
|
|
56
|
+
| ISmartAiProviderBrowserLoginPrompt
|
|
57
|
+
| ISmartAiProviderDeviceLoginPrompt;
|
|
58
|
+
|
|
59
|
+
export type TSmartAiProviderCancelStatus = 'canceled' | 'notFound';
|
|
60
|
+
|
|
61
|
+
export interface ISmartAiProviderLoginHandle {
|
|
62
|
+
readonly prompt: TSmartAiProviderLoginPrompt;
|
|
63
|
+
readonly completion: Promise<ISmartAiProviderLoginResult>;
|
|
64
|
+
cancel(): Promise<TSmartAiProviderCancelStatus>;
|
|
65
|
+
close(): Promise<void>;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export interface ISmartAiProviderOperationOptions {
|
|
69
|
+
signal?: AbortSignal;
|
|
70
|
+
timeoutMs?: number;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface ISmartAiProviderModelReasoningEffort {
|
|
74
|
+
effort: string;
|
|
75
|
+
description: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export interface ISmartAiProviderModelServiceTier {
|
|
79
|
+
id: string;
|
|
80
|
+
name: string;
|
|
81
|
+
description: string;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface ISmartAiProviderModel {
|
|
85
|
+
providerId: 'openai';
|
|
86
|
+
modelId: string;
|
|
87
|
+
wireModel: string;
|
|
88
|
+
displayName: string;
|
|
89
|
+
description: string;
|
|
90
|
+
hidden: boolean;
|
|
91
|
+
reasoningEfforts: ISmartAiProviderModelReasoningEffort[];
|
|
92
|
+
defaultReasoningEffort: string;
|
|
93
|
+
inputModalities: TSmartAiProviderInputModality[];
|
|
94
|
+
supportsPersonality: boolean;
|
|
95
|
+
serviceTiers: ISmartAiProviderModelServiceTier[];
|
|
96
|
+
defaultServiceTier?: string;
|
|
97
|
+
isDefault: boolean;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export interface ISmartAiProviderModelListOptions extends ISmartAiProviderOperationOptions {
|
|
101
|
+
cursor?: string;
|
|
102
|
+
limit?: number;
|
|
103
|
+
includeHidden?: boolean;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export type TSmartAiProviderCredentialOperationErrorCode =
|
|
107
|
+
| 'ABORTED'
|
|
108
|
+
| 'TIMEOUT';
|
|
109
|
+
|
|
110
|
+
export interface ISmartAiProviderRefreshSuccess {
|
|
111
|
+
success: true;
|
|
112
|
+
credential: IOpenAiChatGptOAuthCredential;
|
|
113
|
+
account: ISmartAiProviderAccountSummary;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export interface ISmartAiProviderRefreshFailure {
|
|
117
|
+
success: false;
|
|
118
|
+
credential: IOpenAiChatGptOAuthCredential;
|
|
119
|
+
errorCode: TSmartAiProviderCredentialOperationErrorCode | 'REFRESH_FAILED';
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export type TSmartAiProviderRefreshResult =
|
|
123
|
+
| ISmartAiProviderRefreshSuccess
|
|
124
|
+
| ISmartAiProviderRefreshFailure;
|
|
125
|
+
|
|
126
|
+
export interface ISmartAiProviderModelListSuccess {
|
|
127
|
+
success: true;
|
|
128
|
+
models: ISmartAiProviderModel[];
|
|
129
|
+
nextCursor?: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
export interface ISmartAiProviderModelListFailure {
|
|
133
|
+
success: false;
|
|
134
|
+
errorCode: TSmartAiProviderCredentialOperationErrorCode
|
|
135
|
+
| 'CREDENTIAL_REFRESH_REQUIRED'
|
|
136
|
+
| 'MODEL_LIST_FAILED';
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export type TSmartAiProviderModelListResult =
|
|
140
|
+
| ISmartAiProviderModelListSuccess
|
|
141
|
+
| ISmartAiProviderModelListFailure;
|
|
142
|
+
|
|
143
|
+
export interface ISmartAiProviderRateLimitWindow {
|
|
144
|
+
usedPercent: number;
|
|
145
|
+
limitWindowSeconds: number;
|
|
146
|
+
resetAfterSeconds: number;
|
|
147
|
+
/** Unix timestamp in seconds. */
|
|
148
|
+
resetsAt: number;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export interface ISmartAiProviderRateLimitDetails {
|
|
152
|
+
allowed: boolean;
|
|
153
|
+
limitReached: boolean;
|
|
154
|
+
primaryWindow?: ISmartAiProviderRateLimitWindow;
|
|
155
|
+
secondaryWindow?: ISmartAiProviderRateLimitWindow;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
export interface ISmartAiProviderAdditionalRateLimit {
|
|
159
|
+
limitName: string;
|
|
160
|
+
meteredFeature: string;
|
|
161
|
+
rateLimit?: ISmartAiProviderRateLimitDetails;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
export interface ISmartAiProviderAccountRateLimits {
|
|
165
|
+
providerId: 'openai';
|
|
166
|
+
plan: string;
|
|
167
|
+
observedAt: string;
|
|
168
|
+
rateLimit?: ISmartAiProviderRateLimitDetails;
|
|
169
|
+
additionalRateLimits: ISmartAiProviderAdditionalRateLimit[];
|
|
170
|
+
rateLimitReachedType?: string;
|
|
171
|
+
rateLimitResetCreditsAvailableCount?: number;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export interface ISmartAiProviderAccountRateLimitsSuccess {
|
|
175
|
+
success: true;
|
|
176
|
+
rateLimits: ISmartAiProviderAccountRateLimits;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
export interface ISmartAiProviderAccountRateLimitsFailure {
|
|
180
|
+
success: false;
|
|
181
|
+
errorCode: TSmartAiProviderCredentialOperationErrorCode
|
|
182
|
+
| 'CREDENTIAL_REFRESH_REQUIRED'
|
|
183
|
+
| 'ACCOUNT_RATE_LIMITS_FAILED';
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
export type TSmartAiProviderAccountRateLimitsResult =
|
|
187
|
+
| ISmartAiProviderAccountRateLimitsSuccess
|
|
188
|
+
| ISmartAiProviderAccountRateLimitsFailure;
|
|
189
|
+
|
|
190
|
+
export interface ISmartAiProviderLoginOptions extends ISmartAiProviderOperationOptions {
|
|
191
|
+
flow: TSmartAiProviderLoginFlow;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
export interface ISmartAiProviderAdapter {
|
|
195
|
+
readonly descriptor: ISmartAiProviderDescriptor;
|
|
196
|
+
createApiKeyCredential(apiKey: string): ISmartAiProviderApiKeyCredential;
|
|
197
|
+
inspectCredential(credential: TSmartAiProviderCredential): ISmartAiProviderAccountSummary;
|
|
198
|
+
beginLogin(options: ISmartAiProviderLoginOptions): Promise<ISmartAiProviderLoginHandle>;
|
|
199
|
+
refreshCredential(
|
|
200
|
+
credential: IOpenAiChatGptOAuthCredential,
|
|
201
|
+
options?: ISmartAiProviderOperationOptions,
|
|
202
|
+
): Promise<TSmartAiProviderRefreshResult>;
|
|
203
|
+
listModels(
|
|
204
|
+
credential: IOpenAiChatGptOAuthCredential,
|
|
205
|
+
options?: ISmartAiProviderModelListOptions,
|
|
206
|
+
): Promise<TSmartAiProviderModelListResult>;
|
|
207
|
+
getAccountRateLimits(
|
|
208
|
+
credential: IOpenAiChatGptOAuthCredential,
|
|
209
|
+
options?: ISmartAiProviderOperationOptions,
|
|
210
|
+
): Promise<TSmartAiProviderAccountRateLimitsResult>;
|
|
211
|
+
logoutCredential(
|
|
212
|
+
credential: IOpenAiChatGptOAuthCredential,
|
|
213
|
+
options?: ISmartAiProviderOperationOptions,
|
|
214
|
+
): Promise<void>;
|
|
215
|
+
dispose(): Promise<void>;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export interface IOpenAiProviderAdapterOptions {
|
|
219
|
+
fetch?: typeof fetch;
|
|
220
|
+
originator?: string;
|
|
221
|
+
now?: () => Date;
|
|
222
|
+
}
|