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; constructor( @IConfigService config: IConfigService, @IBootstrapService bootstrap: IBootstrapService, ) { this.frozen = config.ready .catch(() => undefined) .then(() => { const section = config.get(IDENTITY_SECTION) ?? {}; this.snapshot = buildAgentIdentitySnapshot({ name: section.name, slug: section.slug, hostDisplayName: bootstrap.args.displayName, hostRequestHeaders: bootstrap.args.requestHeaders, }); return this.snapshot; }); } resolved(): Promise { 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', );