Download packages/agent-core-v2/src/app/web/webService.ts from SaylorTwift/kimi-code: direct link, hf CLI and curl.
- Browser
- Download file 3.61 kB
-
https://huggingface.co/SaylorTwift/kimi-code/resolve/main/packages/agent-core-v2/src/app/web/webService.ts
- Command line
-
hf download hf://SaylorTwift/kimi-code/packages/agent-core-v2/src/app/web/webService.ts
-
curl -L -o webService.ts https://huggingface.co/SaylorTwift/kimi-code/resolve/main/packages/agent-core-v2/src/app/web/webService.ts
3.61 kB
| import { | |
| KIMI_CODE_PROVIDER_NAME, | |
| kimiCodeBaseUrl, | |
| } from '@moonshot-ai/kimi-code-oauth'; | |
| import { LifecycleScope } from '#/app/scopes'; | |
| import { ScopeActivation, registerScopedService } from '#/_base/di/scope'; | |
| import { IOAuthService } from '#/app/auth/auth'; | |
| import { SERVICES_SECTION, type ServicesConfig } from '#/app/auth/configSection'; | |
| import { IAgentIdentity } from '#/app/agentIdentity/agentIdentity'; | |
| import { IBootstrapService } from '#/app/bootstrap/bootstrap'; | |
| import { IConfigService } from '#/app/config/config'; | |
| import { ITelemetryService } from '#/app/telemetry/telemetry'; | |
| import { IProviderService } from '#/llm-adapter/provider/provider'; | |
| import { isOAuthCatalogVendor } from '#/llm-adapter/provider/provider-definition'; | |
| import { LocalFetchURLProvider } from './providers/local-fetch-url'; | |
| import { MoonshotFetchURLProvider } from './providers/moonshot-fetch-url'; | |
| import type { UrlFetcher } from './tools/fetch-url-types'; | |
| import { IWebFetchService } from './web'; | |
| export class WebFetchService implements IWebFetchService { | |
| declare readonly _serviceBrand: undefined; | |
| private readonly localFetcher: UrlFetcher; | |
| constructor( | |
| private readonly providers: IProviderService, | |
| private readonly oauth: IOAuthService, | |
| private readonly bootstrap: IBootstrapService, | |
| private readonly config: IConfigService, | |
| private readonly identity: IAgentIdentity, | |
| private readonly telemetry: ITelemetryService, | |
| ) { | |
| this.localFetcher = new LocalFetchURLProvider(); | |
| } | |
| getUrlFetcher(): UrlFetcher { | |
| return this.fromServicesConfig() ?? this.fromManagedOAuth() ?? this.localFetcher; | |
| } | |
| private fromServicesConfig(): UrlFetcher | undefined { | |
| const fetchConfig = this.config.get<ServicesConfig>(SERVICES_SECTION)?.moonshotFetch; | |
| if (fetchConfig?.baseUrl === undefined) { | |
| return undefined; | |
| } | |
| const tokenProvider = | |
| fetchConfig.oauth === undefined | |
| ? undefined | |
| : this.oauth.resolveTokenProvider(KIMI_CODE_PROVIDER_NAME, fetchConfig.oauth); | |
| return new MoonshotFetchURLProvider({ | |
| baseUrl: fetchConfig.baseUrl, | |
| tokenProvider, | |
| apiKey: nonEmptyString(fetchConfig.apiKey), | |
| defaultHeaders: { ...this.identity.current().requestHeaders }, | |
| customHeaders: fetchConfig.customHeaders, | |
| localFallback: this.localFetcher, | |
| telemetry: this.telemetry, | |
| }); | |
| } | |
| private fromManagedOAuth(): UrlFetcher | undefined { | |
| const provider = this.providers.get(KIMI_CODE_PROVIDER_NAME); | |
| if (provider === undefined || !isOAuthCatalogVendor(provider.type) || provider.oauth === undefined) { | |
| return undefined; | |
| } | |
| const tokenProvider = this.oauth.resolveTokenProvider( | |
| KIMI_CODE_PROVIDER_NAME, | |
| provider.oauth, | |
| ); | |
| if (tokenProvider === undefined) { | |
| return undefined; | |
| } | |
| const baseUrl = `${(provider.baseUrl ?? kimiCodeBaseUrl()).replace(/\/+$/, '')}/fetch`; | |
| return new MoonshotFetchURLProvider({ | |
| baseUrl, | |
| tokenProvider, | |
| defaultHeaders: { ...this.bootstrap.args.requestHeaders }, | |
| customHeaders: provider.customHeaders, | |
| localFallback: this.localFetcher, | |
| telemetry: this.telemetry, | |
| }); | |
| } | |
| } | |
| function nonEmptyString(value: string | undefined): string | undefined { | |
| const trimmed = value?.trim(); | |
| return trimmed === undefined || trimmed.length === 0 ? undefined : trimmed; | |
| } | |
| registerScopedService( | |
| LifecycleScope.App, | |
| IWebFetchService, | |
| WebFetchService, | |
| ScopeActivation.OnScopeCreated, | |
| 'web', | |
| ); | |