model-mux 1.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/LICENSE +21 -0
- package/README.md +42 -0
- package/dist/eslint.config.d.ts +3 -0
- package/dist/eslint.config.d.ts.map +1 -0
- package/dist/eslint.config.js +52 -0
- package/dist/eslint.config.js.map +1 -0
- package/dist/src/adapter-factory.d.ts +5 -0
- package/dist/src/adapter-factory.d.ts.map +1 -0
- package/dist/src/adapter-factory.js +11 -0
- package/dist/src/adapter-factory.js.map +1 -0
- package/dist/src/adapter.d.ts +19 -0
- package/dist/src/adapter.d.ts.map +1 -0
- package/dist/src/adapter.js +19 -0
- package/dist/src/adapter.js.map +1 -0
- package/dist/src/anthropic/anthropic-adapter.d.ts +8 -0
- package/dist/src/anthropic/anthropic-adapter.d.ts.map +1 -0
- package/dist/src/anthropic/anthropic-adapter.js +17 -0
- package/dist/src/anthropic/anthropic-adapter.js.map +1 -0
- package/dist/src/index.d.ts +15 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +22 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/openai/openai-adapter.d.ts +10 -0
- package/dist/src/openai/openai-adapter.d.ts.map +1 -0
- package/dist/src/openai/openai-adapter.js +75 -0
- package/dist/src/openai/openai-adapter.js.map +1 -0
- package/dist/test/integration/openai/openai.it.spec.d.ts +2 -0
- package/dist/test/integration/openai/openai.it.spec.d.ts.map +1 -0
- package/dist/test/integration/openai/openai.it.spec.js +91 -0
- package/dist/test/integration/openai/openai.it.spec.js.map +1 -0
- package/dist/test/unit/adapter-factory.spec.d.ts +2 -0
- package/dist/test/unit/adapter-factory.spec.d.ts.map +1 -0
- package/dist/test/unit/adapter-factory.spec.js +89 -0
- package/dist/test/unit/adapter-factory.spec.js.map +1 -0
- package/dist/test/unit/adapter.spec.d.ts +2 -0
- package/dist/test/unit/adapter.spec.d.ts.map +1 -0
- package/dist/test/unit/adapter.spec.js +136 -0
- package/dist/test/unit/adapter.spec.js.map +1 -0
- package/dist/test/unit/index.spec.d.ts +2 -0
- package/dist/test/unit/index.spec.d.ts.map +1 -0
- package/dist/test/unit/index.spec.js +193 -0
- package/dist/test/unit/index.spec.js.map +1 -0
- package/dist/test/unit/openai/openai-adapter.spec.d.ts +2 -0
- package/dist/test/unit/openai/openai-adapter.spec.d.ts.map +1 -0
- package/dist/test/unit/openai/openai-adapter.spec.js +314 -0
- package/dist/test/unit/openai/openai-adapter.spec.js.map +1 -0
- package/package.json +70 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Nicholas Coutinho Checan
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Model Mux
|
|
2
|
+
|
|
3
|
+
**Model Mux** is a TypeScript library inspired by Python's LiteLLM, designed for the Google ADK ecosystem. It provides a unified interface to interact with multiple LLM providers (currently OpenAI, with Anthropic planned).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install model-mux
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## How to use
|
|
12
|
+
|
|
13
|
+
Getting started with Model Mux is easy! With just a few lines of code, you can connect to different LLM providers through a unified interface.
|
|
14
|
+
|
|
15
|
+
Example using **OpenAI** models:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { LlmAgent } from '@google/adk';
|
|
19
|
+
import { ModelMux } from 'model-mux';
|
|
20
|
+
|
|
21
|
+
const model = 'gpt-5.4-2026-03-05';
|
|
22
|
+
const baseUrl = 'https://api.openai.com/v1';
|
|
23
|
+
const apiKey = process.env.OPENAI_API_KEY;
|
|
24
|
+
|
|
25
|
+
const modelMux = new ModelMux({ model, baseUrl, apiKey });
|
|
26
|
+
|
|
27
|
+
const agent = new LlmAgent({
|
|
28
|
+
name: 'openai-agent',
|
|
29
|
+
description: 'An agent for testing purposes',
|
|
30
|
+
instruction: 'When in doubt, blame the user.',
|
|
31
|
+
model: modelMux,
|
|
32
|
+
});
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
Example using **Anthropic** models:
|
|
36
|
+
|
|
37
|
+
```typescript
|
|
38
|
+
...wip...
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
> [!TIP]
|
|
42
|
+
> ModelMux also allows the usage of custom headers. Check the [ModelMuxOptions](./src/index.ts) for more details.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eslint.config.d.ts","sourceRoot":"","sources":["../eslint.config.ts"],"names":[],"mappings":";AAOA,wBA4CG"}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import js from '@eslint/js';
|
|
2
|
+
import { defineConfig } from 'eslint/config';
|
|
3
|
+
import eslintConfigPrettier from 'eslint-config-prettier';
|
|
4
|
+
import importPlugin from 'eslint-plugin-import';
|
|
5
|
+
import globals from 'globals';
|
|
6
|
+
import tseslint from 'typescript-eslint';
|
|
7
|
+
export default defineConfig([
|
|
8
|
+
{ ignores: ['coverage/'] },
|
|
9
|
+
{
|
|
10
|
+
files: ['**/*.{js,mjs,cjs,ts,mts,cts}'],
|
|
11
|
+
plugins: { js },
|
|
12
|
+
extends: ['js/recommended'],
|
|
13
|
+
languageOptions: {
|
|
14
|
+
globals: globals.browser,
|
|
15
|
+
},
|
|
16
|
+
},
|
|
17
|
+
tseslint.configs.recommended,
|
|
18
|
+
{
|
|
19
|
+
plugins: {
|
|
20
|
+
import: importPlugin,
|
|
21
|
+
},
|
|
22
|
+
rules: {
|
|
23
|
+
eqeqeq: 'error',
|
|
24
|
+
'prefer-const': 'error',
|
|
25
|
+
'no-console': 'warn',
|
|
26
|
+
'@typescript-eslint/no-explicit-any': 'warn',
|
|
27
|
+
'@typescript-eslint/consistent-type-imports': 'error',
|
|
28
|
+
'@typescript-eslint/no-unused-vars': [
|
|
29
|
+
'error',
|
|
30
|
+
{ argsIgnorePattern: '^_', varsIgnorePattern: '^_' },
|
|
31
|
+
],
|
|
32
|
+
'import/order': [
|
|
33
|
+
'error',
|
|
34
|
+
{
|
|
35
|
+
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'],
|
|
36
|
+
'newlines-between': 'never',
|
|
37
|
+
alphabetize: { order: 'asc', caseInsensitive: true },
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
'import/first': 'error',
|
|
41
|
+
'import/no-duplicates': 'error',
|
|
42
|
+
'import/no-cycle': 'error',
|
|
43
|
+
'import/no-self-import': 'error',
|
|
44
|
+
'import/no-useless-path-segments': 'error',
|
|
45
|
+
'import/no-mutable-exports': 'error',
|
|
46
|
+
'import/no-named-as-default': 'warn',
|
|
47
|
+
'import/no-extraneous-dependencies': 'error',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
eslintConfigPrettier,
|
|
51
|
+
]);
|
|
52
|
+
//# sourceMappingURL=eslint.config.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"eslint.config.js","sourceRoot":"","sources":["../eslint.config.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,YAAY,CAAC;AAC5B,OAAO,EAAE,YAAY,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,oBAAoB,MAAM,wBAAwB,CAAC;AAC1D,OAAO,YAAY,MAAM,sBAAsB,CAAC;AAChD,OAAO,OAAO,MAAM,SAAS,CAAC;AAC9B,OAAO,QAAQ,MAAM,mBAAmB,CAAC;AAEzC,eAAe,YAAY,CAAC;IAC1B,EAAE,OAAO,EAAE,CAAC,WAAW,CAAC,EAAE;IAC1B;QACE,KAAK,EAAE,CAAC,8BAA8B,CAAC;QACvC,OAAO,EAAE,EAAE,EAAE,EAAE;QACf,OAAO,EAAE,CAAC,gBAAgB,CAAC;QAC3B,eAAe,EAAE;YACf,OAAO,EAAE,OAAO,CAAC,OAAO;SACzB;KACF;IACD,QAAQ,CAAC,OAAO,CAAC,WAAW;IAC5B;QACE,OAAO,EAAE;YACP,MAAM,EAAE,YAAY;SACrB;QACD,KAAK,EAAE;YACL,MAAM,EAAE,OAAO;YACf,cAAc,EAAE,OAAO;YACvB,YAAY,EAAE,MAAM;YACpB,oCAAoC,EAAE,MAAM;YAC5C,4CAA4C,EAAE,OAAO;YACrD,mCAAmC,EAAE;gBACnC,OAAO;gBACP,EAAE,iBAAiB,EAAE,IAAI,EAAE,iBAAiB,EAAE,IAAI,EAAE;aACrD;YACD,cAAc,EAAE;gBACd,OAAO;gBACP;oBACE,MAAM,EAAE,CAAC,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,QAAQ,EAAE,SAAS,EAAE,OAAO,CAAC;oBACzE,kBAAkB,EAAE,OAAO;oBAC3B,WAAW,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,eAAe,EAAE,IAAI,EAAE;iBACrD;aACF;YACD,cAAc,EAAE,OAAO;YACvB,sBAAsB,EAAE,OAAO;YAC/B,iBAAiB,EAAE,OAAO;YAC1B,uBAAuB,EAAE,OAAO;YAChC,iCAAiC,EAAE,OAAO;YAC1C,2BAA2B,EAAE,OAAO;YACpC,4BAA4B,EAAE,MAAM;YACpC,mCAAmC,EAAE,OAAO;SAC7C;KACF;IACD,oBAAoB;CACrB,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter-factory.d.ts","sourceRoot":"","sources":["../../src/adapter-factory.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAGhD,8BAAsB,cAAc;IAClC,MAAM,CAAC,aAAa,CAClB,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,EACf,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAC/B,MAAM,EAAE,MAAM,GACb,WAAW;CAMf"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { OpenAIAdapter } from './openai/openai-adapter.js';
|
|
2
|
+
export class AdapterFactory {
|
|
3
|
+
static createAdapter(model, baseUrl, headers, apiKey) {
|
|
4
|
+
if (model.startsWith('gpt-'))
|
|
5
|
+
return new OpenAIAdapter(model, baseUrl, headers, apiKey);
|
|
6
|
+
// if (model.startsWith("claude-"))
|
|
7
|
+
// return new AnthropicAdapter(model, baseUrl, headers, apiKey);
|
|
8
|
+
throw new Error(`Unsupported model/provider: ${model}`);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=adapter-factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter-factory.js","sourceRoot":"","sources":["../../src/adapter-factory.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAE3D,MAAM,OAAgB,cAAc;IAClC,MAAM,CAAC,aAAa,CAClB,KAAa,EACb,OAAe,EACf,OAA+B,EAC/B,MAAc;QAEd,IAAI,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC;YAAE,OAAO,IAAI,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QACxF,mCAAmC;QACnC,oEAAoE;QACpE,MAAM,IAAI,KAAK,CAAC,+BAA+B,KAAK,EAAE,CAAC,CAAC;IAC1D,CAAC;CACF"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { BaseLlmConnection, LlmRequest, LlmResponse } from '@google/adk';
|
|
2
|
+
interface AdapterInterface {
|
|
3
|
+
stream(llmRequest: LlmRequest): AsyncGenerator<LlmResponse, void>;
|
|
4
|
+
generate(llmRequest: LlmRequest): Promise<LlmResponse>;
|
|
5
|
+
connect(llmRequest: LlmRequest): Promise<BaseLlmConnection>;
|
|
6
|
+
}
|
|
7
|
+
export declare abstract class BaseAdapter implements AdapterInterface {
|
|
8
|
+
protected readonly model: string;
|
|
9
|
+
protected readonly baseUrl: string;
|
|
10
|
+
protected readonly headers: Record<string, string>;
|
|
11
|
+
protected readonly apiKey: string;
|
|
12
|
+
constructor(model: string, baseUrl: string, headers: Record<string, string>, apiKey: string);
|
|
13
|
+
protected mapInput(req: LlmRequest): string;
|
|
14
|
+
abstract stream(llmRequest: LlmRequest): AsyncGenerator<LlmResponse, void>;
|
|
15
|
+
abstract generate(llmRequest: LlmRequest): Promise<LlmResponse>;
|
|
16
|
+
abstract connect(llmRequest: LlmRequest): Promise<BaseLlmConnection>;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.d.ts","sourceRoot":"","sources":["../../src/adap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, apiKey) {
|
|
7
|
+
this.model = model;
|
|
8
|
+
this.baseUrl = baseUrl;
|
|
9
|
+
this.headers = headers;
|
|
10
|
+
this.apiKey = apiKey;
|
|
11
|
+
}
|
|
12
|
+
mapInput(req) {
|
|
13
|
+
return req.contents
|
|
14
|
+
.flatMap((content) => content.parts ?? [])
|
|
15
|
+
.map((part) => part.text ?? '')
|
|
16
|
+
.join('');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter.js","sourceRoot":"","sources":["../../src/adapter.ts"],"names":[],"mappings":"AAQA,MAAM,OAAgB,WAAW;IAEV;IACA;IACA;IACA;IAJrB,YACqB,KAAa,EACb,OAAe,EACf,OAA+B,EAC/B,MAAc;QAHd,UAAK,GAAL,KAAK,CAAQ;QACb,YAAO,GAAP,OAAO,CAAQ;QACf,YAAO,GAAP,OAAO,CAAwB;QAC/B,WAAM,GAAN,MAAM,CAAQ;IAChC,CAAC;IAEM,QAAQ,CAAC,GAAe;QAChC,OAAO,GAAG,CAAC,QAAQ;aAChB,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,CAAC;aACzC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC;aAC9B,IAAI,CAAC,EAAE,CAAC,CAAC;IACd,CAAC;CAKF"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { BaseLlmConnection, LlmRequest, LlmResponse } from '@google/adk';
|
|
2
|
+
import { BaseAdapter } from '../adapter.js';
|
|
3
|
+
export declare class AnthropicAdapter extends BaseAdapter {
|
|
4
|
+
stream(llmRequest: LlmRequest): AsyncGenerator<LlmResponse, void>;
|
|
5
|
+
generate(llmRequest: LlmRequest): Promise<LlmResponse>;
|
|
6
|
+
connect(llmRequest: LlmRequest): Promise<BaseLlmConnection>;
|
|
7
|
+
}
|
|
8
|
+
//# sourceMappingURL=anthropic-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-adapter.d.ts","sourceRoot":"","sources":["../../../src/anthropic/anthropic-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,qBAAa,gBAAiB,SAAQ,WAAW;IACxC,MAAM,CAAC,UAAU,EAAE,UAAU,GAAG,cAAc,CAAC,WAAW,EAAE,IAAI,CAAC;IAKlE,QAAQ,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC;IAKtD,OAAO,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,iBAAiB,CAAC;CAIlE"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { BaseAdapter } from '../adapter.js';
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export class AnthropicAdapter extends BaseAdapter {
|
|
4
|
+
async *stream(llmRequest) {
|
|
5
|
+
// Implementation for streaming responses from Anthropic
|
|
6
|
+
throw new Error('Streaming not implemented for Anthropic yet');
|
|
7
|
+
}
|
|
8
|
+
async generate(llmRequest) {
|
|
9
|
+
// Implementation for generating responses from Anthropic
|
|
10
|
+
throw new Error('One-shot generation not implemented for Anthropic yet');
|
|
11
|
+
}
|
|
12
|
+
async connect(llmRequest) {
|
|
13
|
+
// Implementation for connecting to Anthropic
|
|
14
|
+
throw new Error('Connection not implemented for Anthropic yet');
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=anthropic-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"anthropic-adapter.js","sourceRoot":"","sources":["../../../src/anthropic/anthropic-adapter.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,oBAAoB;AACpB,MAAM,OAAO,gBAAiB,SAAQ,WAAW;IAC/C,KAAK,CAAC,CAAC,MAAM,CAAC,UAAsB;QAClC,wDAAwD;QACxD,MAAM,IAAI,KAAK,CAAC,6CAA6C,CAAC,CAAC;IACjE,CAAC;IAED,KAAK,CAAC,QAAQ,CAAC,UAAsB;QACnC,yDAAyD;QACzD,MAAM,IAAI,KAAK,CAAC,uDAAuD,CAAC,CAAC;IAC3E,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,UAAsB;QAClC,6CAA6C;QAC7C,MAAM,IAAI,KAAK,CAAC,8CAA8C,CAAC,CAAC;IAClE,CAAC;CACF"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { BaseLlmConnection, LlmRequest, LlmResponse } from '@google/adk';
|
|
2
|
+
import { BaseLlm } from '@google/adk';
|
|
3
|
+
export interface ModelMuxOptions {
|
|
4
|
+
model: string;
|
|
5
|
+
baseUrl: string;
|
|
6
|
+
headers?: Record<string, string>;
|
|
7
|
+
apiKey?: string;
|
|
8
|
+
}
|
|
9
|
+
export declare class ModelMux extends BaseLlm {
|
|
10
|
+
private readonly adapter;
|
|
11
|
+
constructor(options: ModelMuxOptions);
|
|
12
|
+
generateContentAsync(llmRequest: LlmRequest, stream?: boolean): AsyncGenerator<LlmResponse, void>;
|
|
13
|
+
connect(_llmRequest: LlmRequest): Promise<BaseLlmConnection>;
|
|
14
|
+
}
|
|
15
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC9E,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AAItC,MAAM,WAAW,eAAe;IAC9B,KAAK,EAAE,MAAM,CAAC;IACd,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,qBAAa,QAAS,SAAQ,OAAO;IACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAc;gBAE1B,OAAO,EAAE,eAAe;IAUpB,oBAAoB,CAClC,UAAU,EAAE,UAAU,EACtB,MAAM,CAAC,EAAE,OAAO,GACf,cAAc,CAAC,WAAW,EAAE,IAAI,CAAC;IAU9B,OAAO,CAAC,WAAW,EAAE,UAAU,GAAG,OAAO,CAAC,iBAAiB,CAAC;CAInE"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { BaseLlm } from '@google/adk';
|
|
2
|
+
import { AdapterFactory } from './adapter-factory.js';
|
|
3
|
+
export class ModelMux extends BaseLlm {
|
|
4
|
+
adapter;
|
|
5
|
+
constructor(options) {
|
|
6
|
+
super({ model: options.model });
|
|
7
|
+
this.adapter = AdapterFactory.createAdapter(options.model, options.baseUrl, options.headers ?? {}, options.apiKey ?? 'sk-000000000000000000000000000000000000000000000000');
|
|
8
|
+
}
|
|
9
|
+
async *generateContentAsync(llmRequest, stream) {
|
|
10
|
+
if (stream) {
|
|
11
|
+
yield* this.adapter.stream(llmRequest);
|
|
12
|
+
return;
|
|
13
|
+
}
|
|
14
|
+
const oneShot = await this.adapter.generate(llmRequest);
|
|
15
|
+
yield oneShot;
|
|
16
|
+
}
|
|
17
|
+
async connect(_llmRequest) {
|
|
18
|
+
// For “live” sessions / bidirectional streaming
|
|
19
|
+
return this.adapter.connect(_llmRequest);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAUtD,MAAM,OAAO,QAAS,SAAQ,OAAO;IAClB,OAAO,CAAc;IAEtC,YAAY,OAAwB;QAClC,KAAK,CAAC,EAAE,KAAK,EAAE,OAAO,CAAC,KAAK,EAAE,CAAC,CAAC;QAChC,IAAI,CAAC,OAAO,GAAG,cAAc,CAAC,aAAa,CACzC,OAAO,CAAC,KAAK,EACb,OAAO,CAAC,OAAO,EACf,OAAO,CAAC,OAAO,IAAI,EAAE,EACrB,OAAO,CAAC,MAAM,IAAI,qDAAqD,CACxE,CAAC;IACJ,CAAC;IAEQ,KAAK,CAAC,CAAC,oBAAoB,CAClC,UAAsB,EACtB,MAAgB;QAEhB,IAAI,MAAM,EAAE,CAAC;YACX,KAAK,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;YACvC,OAAO;QACT,CAAC;QAED,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAC,CAAC;QACxD,MAAM,OAAO,CAAC;IAChB,CAAC;IAED,KAAK,CAAC,OAAO,CAAC,WAAuB;QACnC,gDAAgD;QAChD,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IAC3C,CAAC;CACF"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BaseLlmConnection, LlmRequest, LlmResponse } from '@google/adk';
|
|
2
|
+
import { BaseAdapter } from '../adapter.js';
|
|
3
|
+
export declare class OpenAIAdapter extends BaseAdapter {
|
|
4
|
+
private readonly client;
|
|
5
|
+
constructor(model: string, baseUrl: string, headers: Record<string, string>, apiKey: string);
|
|
6
|
+
stream(llmRequest: LlmRequest): AsyncGenerator<LlmResponse, void>;
|
|
7
|
+
generate(llmRequest: LlmRequest): Promise<LlmResponse>;
|
|
8
|
+
connect(llmRequest: LlmRequest): Promise<BaseLlmConnection>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=openai-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-adapter.d.ts","sourceRoot":"","sources":["../../../src/openai/openai-adapter.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAE9E,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,qBAAa,aAAc,SAAQ,WAAW;IAC5C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAS;gBAEpB,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,MAAM;IASpF,MAAM,CAAC,UAAU,EAAE,UAAU,GAAG,cAAc,CAAC,WAAW,EAAE,IAAI,CAAC;IAwClE,QAAQ,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,WAAW,CAAC;IAwBtD,OAAO,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO,CAAC,iBAAiB,CAAC;CAIlE"}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import OpenAI from 'openai';
|
|
2
|
+
import { BaseAdapter } from '../adapter.js';
|
|
3
|
+
export class OpenAIAdapter extends BaseAdapter {
|
|
4
|
+
client;
|
|
5
|
+
constructor(model, baseUrl, headers, apiKey) {
|
|
6
|
+
super(model, baseUrl, headers, apiKey);
|
|
7
|
+
this.client = new OpenAI({
|
|
8
|
+
apiKey: apiKey,
|
|
9
|
+
baseURL: baseUrl,
|
|
10
|
+
defaultHeaders: headers,
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
async *stream(llmRequest) {
|
|
14
|
+
let stream;
|
|
15
|
+
try {
|
|
16
|
+
stream = await this.client.responses.create({
|
|
17
|
+
model: this.model,
|
|
18
|
+
input: this.mapInput(llmRequest),
|
|
19
|
+
temperature: llmRequest.config?.temperature ?? undefined,
|
|
20
|
+
max_output_tokens: llmRequest.config?.maxOutputTokens ?? undefined,
|
|
21
|
+
stream: true,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
throw new Error(`OpenAI stream creation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
26
|
+
}
|
|
27
|
+
for await (const event of stream) {
|
|
28
|
+
if (event.type === 'response.output_text.delta' && event.delta) {
|
|
29
|
+
yield {
|
|
30
|
+
content: {
|
|
31
|
+
role: 'model',
|
|
32
|
+
parts: [{ text: event.delta }],
|
|
33
|
+
},
|
|
34
|
+
partial: true,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
if (event.type === 'response.completed') {
|
|
38
|
+
yield {
|
|
39
|
+
content: {
|
|
40
|
+
role: 'model',
|
|
41
|
+
parts: [{ text: '' }],
|
|
42
|
+
},
|
|
43
|
+
partial: false,
|
|
44
|
+
};
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
async generate(llmRequest) {
|
|
50
|
+
let response;
|
|
51
|
+
try {
|
|
52
|
+
response = await this.client.responses.create({
|
|
53
|
+
model: this.model,
|
|
54
|
+
input: this.mapInput(llmRequest),
|
|
55
|
+
temperature: llmRequest.config?.temperature ?? undefined,
|
|
56
|
+
max_output_tokens: llmRequest.config?.maxOutputTokens ?? undefined,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
catch (error) {
|
|
60
|
+
throw new Error(`OpenAI response creation failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
61
|
+
}
|
|
62
|
+
return {
|
|
63
|
+
content: {
|
|
64
|
+
role: 'model',
|
|
65
|
+
parts: [{ text: response.output_text }],
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
70
|
+
async connect(llmRequest) {
|
|
71
|
+
// Implementation for connecting to OpenAI
|
|
72
|
+
throw new Error('Not implemented');
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
//# sourceMappingURL=openai-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai-adapter.js","sourceRoot":"","sources":["../../../src/openai/openai-adapter.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAE5C,MAAM,OAAO,aAAc,SAAQ,WAAW;IAC3B,MAAM,CAAS;IAEhC,YAAY,KAAa,EAAE,OAAe,EAAE,OAA+B,EAAE,MAAc;QACzF,KAAK,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,MAAM,CAAC,CAAC;QACvC,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC;YACvB,MAAM,EAAE,MAAM;YACd,OAAO,EAAE,OAAO;YAChB,cAAc,EAAE,OAAO;SACxB,CAAC,CAAC;IACL,CAAC;IAED,KAAK,CAAC,CAAC,MAAM,CAAC,UAAsB;QAClC,IAAI,MAAM,CAAC;QACX,IAAI,CAAC;YACH,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC;gBAC1C,KAAK,EAAE,IAAI,CAAC,KAAK;gBACjB,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC;gBAChC,WAAW,EAAE,UAAU,CAAC,MAAM,EAAE,WAAW,IAAI,SAAS;gBACxD,iBAAiB,EAAE,UAAU,CAAC,MAAM,EAAE,eAAe,IAAI,SAAS;gBAClE,MAAM,EAAE,IAAI;aACb,CAAC,CAAC;QACL,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,IAAI,KAAK,CACb,kCAAkC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,M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CAAC;QAED,OAAO;YACL,OAAO,EAAE;gBACP,IAAI,EAAE,OAAO;gBACb,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,QAAQ,CAAC,WAAW,EAAE,CAAC;aACxC;SACoB,CAAC;IAC1B,CAAC;IAED,6DAA6D;IAC7D,KAAK,CAAC,OAAO,CAAC,UAAsB;QAClC,0CAA0C;QAC1C,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.it.spec.d.ts","sourceRoot":"","sources":["../../../../test/integration/openai/openai.it.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { InMemorySessionService, LlmAgent, Runner, StreamingMode, isFinalResponse, stringifyContent, } from '@google/adk';
|
|
2
|
+
import dotenv from 'dotenv';
|
|
3
|
+
import { ModelMux } from '../../../src/index.js';
|
|
4
|
+
const models = [
|
|
5
|
+
'gpt-4o-2024-11-20',
|
|
6
|
+
'gpt-5-2025-08-07',
|
|
7
|
+
'gpt-5.1-2025-11-13',
|
|
8
|
+
'gpt-5.2-2025-12-11',
|
|
9
|
+
'gpt-5.4-2026-03-05',
|
|
10
|
+
];
|
|
11
|
+
describe.each(models)('openai integration: %s', (model) => {
|
|
12
|
+
const baseUrl = 'https://api.openai.com/v1';
|
|
13
|
+
const appName = 'test-app';
|
|
14
|
+
const userId = 'test-user';
|
|
15
|
+
beforeAll(() => {
|
|
16
|
+
dotenv.config();
|
|
17
|
+
});
|
|
18
|
+
it('should generate an answer to a one-shot ad hoc request', async () => {
|
|
19
|
+
// Arrange
|
|
20
|
+
const apiKey = process.env.API_KEY_OPENAI || '';
|
|
21
|
+
const sessionId = `test-session-${model}`;
|
|
22
|
+
const sessionService = new InMemorySessionService();
|
|
23
|
+
await sessionService.createSession({ appName, userId, sessionId });
|
|
24
|
+
// Act 1
|
|
25
|
+
const modelMux = new ModelMux({ model, baseUrl, apiKey });
|
|
26
|
+
// Act 2
|
|
27
|
+
const agent = new LlmAgent({
|
|
28
|
+
name: 'test-agent',
|
|
29
|
+
description: 'An agent for testing purposes',
|
|
30
|
+
instruction: 'Return "yes" or "no", nothing else.',
|
|
31
|
+
model: modelMux,
|
|
32
|
+
});
|
|
33
|
+
// Act 3
|
|
34
|
+
const runner = new Runner({ appName, agent, sessionService });
|
|
35
|
+
const events = await runner.runAsync({
|
|
36
|
+
userId,
|
|
37
|
+
sessionId,
|
|
38
|
+
newMessage: {
|
|
39
|
+
role: 'user',
|
|
40
|
+
parts: [{ text: 'Is this thing working?' }],
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
// Act 4
|
|
44
|
+
let answer;
|
|
45
|
+
for await (const event of events) {
|
|
46
|
+
if (isFinalResponse(event))
|
|
47
|
+
answer = stringifyContent(event).trim();
|
|
48
|
+
}
|
|
49
|
+
// Assert
|
|
50
|
+
expect(answer).toBeTruthy();
|
|
51
|
+
});
|
|
52
|
+
it('should stream an answer in multiple chunks to an ad hoc request', async () => {
|
|
53
|
+
// Arrange
|
|
54
|
+
const apiKey = process.env.API_KEY_OPENAI || '';
|
|
55
|
+
const sessionId = `test-session-stream-${model}`;
|
|
56
|
+
const sessionService = new InMemorySessionService();
|
|
57
|
+
await sessionService.createSession({ appName, userId, sessionId });
|
|
58
|
+
// Act 1
|
|
59
|
+
const modelMux = new ModelMux({ model, baseUrl, apiKey });
|
|
60
|
+
// Act 2
|
|
61
|
+
const agent = new LlmAgent({
|
|
62
|
+
name: 'test-agent',
|
|
63
|
+
description: 'An agent for testing purposes',
|
|
64
|
+
instruction: 'Return "yes" or "no", nothing else.',
|
|
65
|
+
model: modelMux,
|
|
66
|
+
});
|
|
67
|
+
// Act 3
|
|
68
|
+
const runner = new Runner({ appName, agent, sessionService });
|
|
69
|
+
const events = await runner.runAsync({
|
|
70
|
+
userId,
|
|
71
|
+
sessionId,
|
|
72
|
+
newMessage: {
|
|
73
|
+
role: 'user',
|
|
74
|
+
parts: [{ text: 'Is this thing working?' }],
|
|
75
|
+
},
|
|
76
|
+
runConfig: { streamingMode: StreamingMode.SSE },
|
|
77
|
+
});
|
|
78
|
+
// Act 4
|
|
79
|
+
const chunks = [];
|
|
80
|
+
for await (const event of events) {
|
|
81
|
+
const text = stringifyContent(event).trim();
|
|
82
|
+
if (text)
|
|
83
|
+
chunks.push(text);
|
|
84
|
+
}
|
|
85
|
+
const answer = chunks.join('');
|
|
86
|
+
// Assert
|
|
87
|
+
expect(chunks.length).toBeGreaterThan(0);
|
|
88
|
+
expect(answer).toBeTruthy();
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
//# sourceMappingURL=openai.it.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.it.spec.js","sourceRoot":"","sources":["../../../../test/integration/openai/openai.it.spec.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,sBAAsB,EACtB,QAAQ,EACR,MAAM,EACN,aAAa,EACb,eAAe,EACf,gBAAgB,GACjB,MAAM,aAAa,CAAC;AACrB,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AAEjD,MAAM,MAAM,GAAG;IACb,mBAAmB;IACnB,kBAAkB;IAClB,oBAAoB;IACpB,oBAAoB;IACpB,oBAAoB;CACrB,CAAC;AAEF,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,wBAAwB,EAAE,CAAC,KAAK,EAAE,EAAE;IACxD,MAAM,OAAO,GAAG,2BAA2B,CAAC;IAC5C,MAAM,OAAO,GAAG,UAAU,CAAC;IAC3B,MAAM,MAAM,GAAG,WAAW,CAAC;IAE3B,SAAS,CAAC,GAAG,EAAE;QACb,MAAM,CAAC,MAAM,EAAE,CAAC;IAClB,CAAC,CAAC,CAAC;IAEH,EAAE,CAAC,wDAAwD,EAAE,KAAK,IAAI,EAAE;QACtE,UAAU;QACV,MAA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CAAC,EAAE,CAAC,CAAC;QAE/B,SAAS;QACT,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,CAAC;QACzC,MAAM,CAAC,MAAM,CAAC,CAAC,UAAU,EAAE,CAAC;IAC9B,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter-factory.spec.d.ts","sourceRoot":"","sources":["../../../test/unit/adapter-factory.spec.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import Chance from 'chance';
|
|
2
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
3
|
+
import { AdapterFactory } from '../../src/adapter-factory.js';
|
|
4
|
+
import { OpenAIAdapter } from '../../src/openai/openai-adapter.js';
|
|
5
|
+
const mockOpenAIConstructor = vi.fn();
|
|
6
|
+
vi.mock('../../src/openai/openai-adapter.js', () => {
|
|
7
|
+
class MockOpenAIAdapter {
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
|
+
constructor(...args) {
|
|
10
|
+
mockOpenAIConstructor(...args);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return { OpenAIAdapter: MockOpenAIAdapter };
|
|
14
|
+
});
|
|
15
|
+
describe('AdapterFactory', () => {
|
|
16
|
+
const chance = new Chance();
|
|
17
|
+
let baseUrl;
|
|
18
|
+
let headers;
|
|
19
|
+
let apiKey;
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
vi.clearAllMocks();
|
|
22
|
+
baseUrl = chance.url();
|
|
23
|
+
headers = { [chance.word()]: chance.string(), [chance.word()]: chance.string() };
|
|
24
|
+
apiKey = chance.string();
|
|
25
|
+
});
|
|
26
|
+
describe('createAdapter', () => {
|
|
27
|
+
it("should create an OpenAIAdapter when model starts with 'gpt-'", () => {
|
|
28
|
+
// Arrange
|
|
29
|
+
const model = `gpt-${chance.word()}`;
|
|
30
|
+
// Act
|
|
31
|
+
AdapterFactory.createAdapter(model, baseUrl, headers, apiKey);
|
|
32
|
+
// Assert
|
|
33
|
+
expect(mockOpenAIConstructor).toHaveBeenCalledOnce();
|
|
34
|
+
expect(mockOpenAIConstructor).toHaveBeenCalledWith(model, baseUrl, headers, apiKey);
|
|
35
|
+
});
|
|
36
|
+
it('should return an instance of OpenAIAdapter for gpt- models', () => {
|
|
37
|
+
// Arrange
|
|
38
|
+
const model = `gpt-${chance.word()}`;
|
|
39
|
+
// Act
|
|
40
|
+
const result = AdapterFactory.createAdapter(model, baseUrl, headers, apiKey);
|
|
41
|
+
// Assert
|
|
42
|
+
expect(result).toBeInstanceOf(OpenAIAdapter);
|
|
43
|
+
});
|
|
44
|
+
it('should pass model, baseUrl, headers, and apiKey to OpenAIAdapter', () => {
|
|
45
|
+
// Arrange
|
|
46
|
+
const model = `gpt-${chance.word()}`;
|
|
47
|
+
const specificBaseUrl = chance.url();
|
|
48
|
+
const specificHeaders = { [chance.word()]: chance.string() };
|
|
49
|
+
const specificApiKey = chance.string();
|
|
50
|
+
// Act
|
|
51
|
+
AdapterFactory.createAdapter(model, specificBaseUrl, specificHeaders, specificApiKey);
|
|
52
|
+
// Assert
|
|
53
|
+
expect(mockOpenAIConstructor).toHaveBeenCalledWith(model, specificBaseUrl, specificHeaders, specificApiKey);
|
|
54
|
+
});
|
|
55
|
+
it('should throw an error for unsupported model prefixes', () => {
|
|
56
|
+
// Arrange
|
|
57
|
+
const model = chance.word();
|
|
58
|
+
// Act & Assert
|
|
59
|
+
expect(() => AdapterFactory.createAdapter(model, baseUrl, headers, apiKey)).toThrow(`Unsupported model/provider: ${model}`);
|
|
60
|
+
});
|
|
61
|
+
it('should throw an error when model is an empty string', () => {
|
|
62
|
+
// Act & Assert
|
|
63
|
+
expect(() => AdapterFactory.createAdapter('', baseUrl, headers, apiKey)).toThrow('Unsupported model/provider: ');
|
|
64
|
+
});
|
|
65
|
+
it('should not instantiate OpenAIAdapter for unsupported models', () => {
|
|
66
|
+
// Arrange
|
|
67
|
+
const model = chance.word();
|
|
68
|
+
// Act
|
|
69
|
+
try {
|
|
70
|
+
AdapterFactory.createAdapter(model, baseUrl, headers, apiKey);
|
|
71
|
+
}
|
|
72
|
+
catch {
|
|
73
|
+
// expected
|
|
74
|
+
}
|
|
75
|
+
// Assert
|
|
76
|
+
expect(mockOpenAIConstructor).not.toHaveBeenCalled();
|
|
77
|
+
});
|
|
78
|
+
it("should handle model 'gpt-' with no suffix", () => {
|
|
79
|
+
// Arrange
|
|
80
|
+
const model = 'gpt-';
|
|
81
|
+
// Act
|
|
82
|
+
AdapterFactory.createAdapter(model, baseUrl, headers, apiKey);
|
|
83
|
+
// Assert
|
|
84
|
+
expect(mockOpenAIConstructor).toHaveBeenCalledOnce();
|
|
85
|
+
expect(mockOpenAIConstructor).toHaveBeenCalledWith(model, baseUrl, headers, apiKey);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
//# sourceMappingURL=adapter-factory.spec.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapter-factory.spec.js","sourceRoot":"","sources":["../../../test/unit/adapter-factory.spec.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAC5B,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AAC9D,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAC9D,OAAO,EAAE,aAAa,EAAE,MAAM,oCAAoC,CAAC;AAEnE,MAAM,qBAAqB,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC;AAEtC,EAAE,CAAC,IAAI,CAAC,oCAAoC,EAAE,GAAG,EAAE;IACjD,MAAM,iBAAiB;QACrB,8DAA8D;QAC9D,YAAY,GAAG,IAAW;YACxB,qBAAqB,CAAC,GAAG,IAAI,CAAC,CAAC;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