{"version":3,"file":"AgentContext.mjs","names":[],"sources":["../../../src/agent/context/AgentContext.ts"],"sourcesContent":["import { InjectionSymbols } from '../../constants'\nimport type { DependencyManager, InjectionToken } from '../../plugins'\nimport { AgentConfig } from '../AgentConfig'\nimport type { AgentContextProvider } from './AgentContextProvider'\n\nexport class AgentContext {\n  /**\n   * Dependency manager holds all dependencies for the current context. Possibly a child of a parent dependency manager,\n   * in which case all singleton dependencies from the parent context are also available to this context.\n   */\n  public readonly dependencyManager: DependencyManager\n\n  /**\n   * An identifier that allows to correlate this context across sessions. This identifier is created by the `AgentContextProvider`\n   * and should only be meaningful to the `AgentContextProvider`. The `contextCorrelationId` MUST uniquely identity the context and\n   * should be enough to start a new session.\n   *\n   * An example of the `contextCorrelationId` is for example the id of the `TenantRecord` that is associated with this context when using the tenant module.\n   * The `TenantAgentContextProvider` will set the `contextCorrelationId` to the `TenantRecord` id when creating the context, and will be able to create a context\n   * for a specific tenant using the `contextCorrelationId`.\n   */\n  public readonly contextCorrelationId: string\n\n  public readonly isRootAgentContext: boolean\n\n  private _isInitialized = false\n\n  public constructor({\n    dependencyManager,\n    contextCorrelationId,\n    isRootAgentContext = false,\n  }: {\n    dependencyManager: DependencyManager\n    contextCorrelationId: string\n    isRootAgentContext?: boolean\n  }) {\n    this.dependencyManager = dependencyManager\n    this.contextCorrelationId = contextCorrelationId\n    this.isRootAgentContext = isRootAgentContext\n  }\n\n  public get isInitialized(): boolean {\n    return this._isInitialized\n  }\n\n  /** @internal */\n  public set isInitialized(value: boolean) {\n    this._isInitialized = value\n  }\n\n  /**\n   * Convenience method to access the agent config for the current context.\n   */\n  public get config() {\n    return this.dependencyManager.resolve(AgentConfig)\n  }\n\n  /**\n   * End session the current agent context\n   */\n  public async endSession() {\n    // TODO: we need to create a custom agent context per sesion\n    // and then track if it has already been ended, because it's quite\n    // easy to mess up the session count at the moment\n    const agentContextProvider = this.dependencyManager.resolve<AgentContextProvider>(\n      InjectionSymbols.AgentContextProvider\n    )\n\n    await agentContextProvider.endSessionForAgentContext(this)\n  }\n\n  public toJSON() {\n    return {\n      contextCorrelationId: this.contextCorrelationId,\n    }\n  }\n\n  /**\n   * Resolve a dependency\n   */\n  public resolve<T>(token: InjectionToken<T>): T {\n    return this.dependencyManager.resolve(token)\n  }\n}\n"],"mappings":";;;AAKA,IAAa,eAAb,MAA0B;CAsBxB,YAAmB,EACjB,mBACA,sBACA,qBAAqB,SAKpB;EAVH,KAAQ,iBAAiB;EAWvB,KAAK,oBAAoB;EACzB,KAAK,uBAAuB;EAC5B,KAAK,qBAAqB;CAC5B;CAEA,IAAW,gBAAyB;EAClC,OAAO,KAAK;CACd;;CAGA,IAAW,cAAc,OAAgB;EACvC,KAAK,iBAAiB;CACxB;;;;CAKA,IAAW,SAAS;EAClB,OAAO,KAAK,kBAAkB,QAAQ,WAAW;CACnD;;;;CAKA,MAAa,aAAa;EAQxB,MAJ6B,KAAK,kBAAkB,QAClD,iBAAiB,oBAGM,CAAC,CAAC,0BAA0B,IAAI;CAC3D;CAEA,SAAgB;EACd,OAAO,EACL,sBAAsB,KAAK,qBAC7B;CACF;;;;CAKA,QAAkB,OAA6B;EAC7C,OAAO,KAAK,kBAAkB,QAAQ,KAAK;CAC7C;AACF"}