11// 启动组装:--provider 参数 → Provider 实例 + 注册表 + 路由表。
22// 默认 provider 是 gateway(任务要求);显式路由之外的模型名直通默认
33// provider 并由其模型解析器裁决(这是路由策略,不是故障 fallback)。
4- import { GatewayProvider } from "./providers/gateway/index.js" ;
5- import { CliProvider } from "./providers/cli/index.js" ;
6- import { CloudAgentsClient , SessionStore } from "./providers/cloudAgents/client.js" ;
7- import { CloudAgentsProvider } from "./providers/cloudAgents/index.js" ;
8- import { loadCliConfig } from "./providers/cli/config.js" ;
9- import { GATEWAY_REGIONS } from "./providers/gateway/auth.js" ;
10- import { createProviderRegistry } from "./routing/providerRegistry.js" ;
11- import { parseModelRoutes } from "./routing/modelRouter.js" ;
12- import { loadConfig } from "./config.js" ;
4+ import { GatewayProvider } from "./providers/gateway/index.ts" ;
5+ import { CliProvider } from "./providers/cli/index.ts" ;
6+ import { CloudAgentsClient , SessionStore } from "./providers/cloudAgents/client.ts" ;
7+ import { CloudAgentsProvider } from "./providers/cloudAgents/index.ts" ;
8+ import { loadCliConfig } from "./providers/cli/config.ts" ;
9+ import { GATEWAY_REGIONS } from "./providers/gateway/auth.ts" ;
10+ import { createProviderRegistry } from "./routing/providerRegistry.ts" ;
11+ import { parseModelRoutes } from "./routing/modelRouter.ts" ;
12+ import { loadConfig } from "./config.ts" ;
13+ import type { AppConfig } from "./config.ts" ;
14+ import type { QoderProvider } from "./core/provider.ts" ;
15+ import type { ProviderRegistry } from "./routing/providerRegistry.ts" ;
16+ import type { ModelRoutes } from "./routing/modelRouter.ts" ;
1317
14- export const PROVIDER_IDS = [ "gateway" , "cli" , "cloudAgents" ] ;
18+ export const PROVIDER_IDS = [ "gateway" , "cli" , "cloudAgents" ] as const ;
1519
16- export function resolveProviderId ( argv = process . argv , env = process . env ) {
17- const normalize = ( value ) => ( value === "cloud-agents" ? "cloudAgents" : value ) ;
20+ export type ProviderId = ( typeof PROVIDER_IDS ) [ number ] ;
21+
22+ export function resolveProviderId (
23+ argv : readonly string [ ] = process . argv ,
24+ env : NodeJS . ProcessEnv = process . env ,
25+ ) : ProviderId {
26+ const normalize = ( value : string ) : string => ( value === "cloud-agents" ? "cloudAgents" : value ) ;
1827 for ( const arg of argv ) {
1928 const match = String ( arg ) . match ( / ^ - - p r o v i d e r = ( .+ ) $ / ) ;
2029 if ( match ) {
2130 const id = normalize ( match [ 1 ] . trim ( ) ) ;
22- if ( ! PROVIDER_IDS . includes ( id ) ) {
31+ if ( ! ( PROVIDER_IDS as readonly string [ ] ) . includes ( id ) ) {
2332 throw new Error ( `unknown provider "${ match [ 1 ] } "; valid: gateway, cli, cloud-agents` ) ;
2433 }
25- return id ;
34+ return id as ProviderId ;
2635 }
2736 }
2837 const fromEnv = String ( env . QODER_DEFAULT_PROVIDER || "" ) . trim ( ) ;
2938 if ( fromEnv ) {
3039 const id = normalize ( fromEnv ) ;
31- if ( ! PROVIDER_IDS . includes ( id ) ) {
40+ if ( ! ( PROVIDER_IDS as readonly string [ ] ) . includes ( id ) ) {
3241 throw new Error (
3342 `unknown QODER_DEFAULT_PROVIDER "${ fromEnv } "; valid: gateway, cli, cloud-agents` ,
3443 ) ;
3544 }
36- return id ;
45+ return id as ProviderId ;
3746 }
3847 return "gateway" ;
3948}
4049
41- export function createApp ( { providerId, env = process . env , config = loadConfig ( env ) } = { } ) {
50+ export interface CreateAppOptions {
51+ providerId ?: ProviderId ;
52+ env ?: NodeJS . ProcessEnv ;
53+ config ?: AppConfig ;
54+ }
55+
56+ export interface CreateAppResult {
57+ config : AppConfig ;
58+ registry : ProviderRegistry ;
59+ modelRoutes : ModelRoutes ;
60+ defaultProviderId : ProviderId | undefined ;
61+ }
62+
63+ export function createApp ( {
64+ providerId,
65+ env = process . env ,
66+ config = loadConfig ( env ) ,
67+ } : CreateAppOptions = { } ) : CreateAppResult {
4268 const registry = createProviderRegistry ( ) ;
4369
70+ // Provider 类的 id 字段被声明为拓宽的 string;运行时即注册 id 本身,
71+ // 在组装点按 QoderProvider 契约收窄(纯编译期,无行为变化)。
4472 if ( providerId === "gateway" ) {
73+ const regionKey = ( env . QODER_GATEWAY_REGION || "cn" ) as keyof typeof GATEWAY_REGIONS ;
4574 registry . register (
4675 new GatewayProvider ( {
4776 pat : env . QODER_GATEWAY_PAT ,
48- region : GATEWAY_REGIONS [ env . QODER_GATEWAY_REGION || "cn" ] || GATEWAY_REGIONS . cn ,
77+ region : GATEWAY_REGIONS [ regionKey ] || GATEWAY_REGIONS . cn ,
4978 requestTimeoutMs : config . requestTimeoutMs ,
5079 modelCacheTtlMs : Number ( env . QODER_GATEWAY_MODEL_CACHE_TTL_MS ) || 600000 ,
5180 defaultModel : env . QODER_GATEWAY_DEFAULT_MODEL || "" ,
52- } ) ,
81+ } ) as unknown as QoderProvider ,
5382 ) ;
5483 } else if ( providerId === "cli" ) {
55- registry . register ( new CliProvider ( { cliConfig : loadCliConfig ( env ) } ) ) ;
84+ registry . register (
85+ new CliProvider ( { cliConfig : loadCliConfig ( env ) } ) as unknown as QoderProvider ,
86+ ) ;
5687 } else if ( providerId === "cloudAgents" ) {
5788 const client = new CloudAgentsClient ( {
5889 baseUrl : config . qoderApiBaseUrl ,
@@ -65,7 +96,7 @@ export function createApp({ providerId, env = process.env, config = loadConfig(e
6596 sessionStore : new SessionStore ( { ttlMs : config . sessionTtlMs } ) ,
6697 modelMap : config . modelMap ,
6798 archiveEphemeralSessions : config . archiveEphemeralSessions ,
68- } ) ,
99+ } ) as unknown as QoderProvider ,
69100 ) ;
70101 }
71102
0 commit comments