kimi-code / packages /agent-core-v2 /src /app /web /webService.ts
SaylorTwift's picture
SaylorTwift HF Staff
Add files using upload-large-folder tool
68d7816 verified
Raw History Blame Contribute Delete
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(
@IProviderService private readonly providers: IProviderService,
@IOAuthService private readonly oauth: IOAuthService,
@IBootstrapService private readonly bootstrap: IBootstrapService,
@IConfigService private readonly config: IConfigService,
@IAgentIdentity private readonly identity: IAgentIdentity,
@ITelemetryService 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',
);