kimi-code / packages /agent-core-v2 /src /app /agentIdentity /agentIdentityService.ts
SaylorTwift's picture
SaylorTwift HF Staff
Add files using upload-large-folder tool
68d7816 verified
Raw History Blame Contribute Delete
1.78 kB
import { LifecycleScope } from '#/app/scopes';
import { ScopeActivation, registerScopedService } from '#/_base/di/scope';
import { CoreErrors } from '#/_base/errors/codes';
import { Error2 } from '#/_base/errors/errors';
import { IBootstrapService } from '#/app/bootstrap/bootstrap';
import { IConfigService } from '#/app/config/config';
import {
buildAgentIdentitySnapshot,
IAgentIdentity,
type AgentIdentitySnapshot,
} from './agentIdentity';
import { IDENTITY_SECTION, type IdentityConfig } from './configSection';
export class AgentIdentityService implements IAgentIdentity {
declare readonly _serviceBrand: undefined;
private snapshot: AgentIdentitySnapshot | undefined;
private readonly frozen: Promise<AgentIdentitySnapshot>;
constructor(
@IConfigService config: IConfigService,
@IBootstrapService bootstrap: IBootstrapService,
) {
this.frozen = config.ready
.catch(() => undefined)
.then(() => {
const section = config.get<IdentityConfig | undefined>(IDENTITY_SECTION) ?? {};
this.snapshot = buildAgentIdentitySnapshot({
name: section.name,
slug: section.slug,
hostDisplayName: bootstrap.args.displayName,
hostRequestHeaders: bootstrap.args.requestHeaders,
});
return this.snapshot;
});
}
resolved(): Promise<AgentIdentitySnapshot> {
return this.frozen;
}
current(): AgentIdentitySnapshot {
if (this.snapshot === undefined) {
throw new Error2(
CoreErrors.codes.INTERNAL,
'agent identity read before config load completed',
);
}
return this.snapshot;
}
}
registerScopedService(
LifecycleScope.App,
IAgentIdentity,
AgentIdentityService,
ScopeActivation.OnScopeCreated,
'agentIdentity',
);