@@ -17,13 +17,28 @@ Create `providers/<name>.js`. The adapter is a plain JS object (no class syntax
1717``` js
1818' use strict' ;
1919
20- const { normalizeApiTarget , normalizeBasePath } = require (' ../proxy-utils' );
20+ const { createBaseAdapterConfig , createAdapterMethods } = require (' ../proxy-utils' );
2121
2222function createMyProviderAdapter (env , deps = {}) {
2323 // Read credentials and config from env at construction time
24- const apiKey = (env .MY_PROVIDER_API_KEY || ' ' ).trim () || undefined ;
25- const target = normalizeApiTarget (env .MY_PROVIDER_API_TARGET ) || ' api.myprovider.com' ;
26- const basePath = normalizeBasePath (env .MY_PROVIDER_API_BASE_PATH );
24+ const { apiKey , rawTarget: target , basePath } = createBaseAdapterConfig (env, {
25+ keyEnvVar: ' MY_PROVIDER_API_KEY' ,
26+ targetEnvVar: ' MY_PROVIDER_API_TARGET' ,
27+ basePathEnvVar: ' MY_PROVIDER_API_BASE_PATH' ,
28+ defaultTarget: ' api.myprovider.com' ,
29+ });
30+ const adapterMethods = createAdapterMethods ({
31+ apiKey,
32+ rawTarget: target,
33+ basePath,
34+ provider: ' my-provider' ,
35+ port: 10005 ,
36+ defaultTarget: ' api.myprovider.com' ,
37+ validationPath: ' /v1/models' ,
38+ validationHeaders : () => ({ ' Authorization' : ` Bearer ${ apiKey} ` }),
39+ modelsPath: ' /v1/models' ,
40+ modelsFetchHeaders : () => ({ ' Authorization' : ` Bearer ${ apiKey} ` }),
41+ });
2742
2843 const bodyTransform = deps .bodyTransform || null ; // model-alias rewriting etc.
2944
@@ -34,12 +49,9 @@ function createMyProviderAdapter(env, deps = {}) {
3449
3550 isManagementPort: false , // true only for port 10000 (OpenAI)
3651 alwaysBind: false , // set true to start a 503-stub when not configured
37- get participatesInValidation () { return this .isEnabled (); },
38-
3952 // ── Credentials ──────────────────────────────────────────────────────────
4053 isEnabled () { return !! apiKey; },
41- getTargetHost () { return target; },
42- getBasePath () { return basePath; },
54+ ... adapterMethods,
4355
4456 // ── Per-request auth headers ──────────────────────────────────────────────
4557 // `req` is the incoming http.IncomingMessage — inspect it for request-specific logic.
@@ -55,41 +67,13 @@ function createMyProviderAdapter(env, deps = {}) {
5567 // Return a function (body: Buffer) => Buffer|null, or null for no transform.
5668 getBodyTransform () { return bodyTransform; },
5769
58- // ── Startup: credential validation ───────────────────────────────────────
59- // Return a probe config, a skip config, or null if validation is not applicable.
60- getValidationProbe () {
61- if (! apiKey) return null ;
62- if (target !== ' api.myprovider.com' ) {
63- return { skip: true , reason: ` Custom target ${ target} ; validation skipped` };
64- }
65- return {
66- url: ` https://${ target} /v1/models` ,
67- opts: { method: ' GET' , headers: { ' Authorization' : ` Bearer ${ apiKey} ` } },
68- };
69- },
70-
71- // ── Startup: model listing ────────────────────────────────────────────────
72- // Return null to opt out of model fetching.
73- getModelsFetchConfig () {
74- if (! apiKey) return null ;
75- return {
76- url: ` https://${ target} /v1/models` ,
77- opts: { method: ' GET' , headers: { ' Authorization' : ` Bearer ${ apiKey} ` } },
78- cacheKey: ' my-provider' , // key in cachedModels; must match name
79- };
80- },
81-
82- // ── /reflect endpoint metadata ────────────────────────────────────────────
83- getReflectionInfo () {
84- return {
85- provider: ' my-provider' ,
86- port: 10005 ,
87- base_url: ' http://api-proxy:10005' ,
88- configured: !! apiKey,
89- models_cache_key: ' my-provider' , // null when models are not fetched
90- models_url: ' http://api-proxy:10005/v1/models' ,
91- };
92- },
70+ // createAdapterMethods provides:
71+ // - participatesInValidation (defaults to !!apiKey)
72+ // - getTargetHost()
73+ // - getBasePath()
74+ // - getValidationProbe()
75+ // - getModelsFetchConfig()
76+ // - getReflectionInfo()
9377 };
9478}
9579
0 commit comments