Lime Web Components API Documentation - v7.11.0
    Preparing search index...

    Interface AIContextRegistryAlpha

    Registry for AI context providers.

    The AIContextRegistry manages context providers that contribute information to the AI chat. Components register providers to share their state, and the AI chat gathers context from all providers when needed.

    import {
    createLogger,
    PlatformServiceName,
    } from '@limetech/lime-web-components';

    const logger = createLogger('my-component');

    const registry = platform.get(PlatformServiceName.AIContextRegistry);
    const contributions = registry.gatherContext();

    for (const contribution of contributions) {
    logger.debug('Gathered context', {
    type: contribution.type,
    limetype: contribution.ancestorContext.limetype,
    purpose: contribution.purpose,
    data: contribution.data,
    });
    }
    interface AIContextRegistry {
        gatherContext(): GatheredAIContext[];
        gatherContext(
            options: GatherContextOptions<"full">,
        ): GatheredAIContextFull[];
        gatherContext(
            options: GatherContextOptions<"summary">,
        ): GatheredAISummary[];
        getProviders(): AIContextProvider[];
        prepareAction?(id: string, input: JsonValue): Promise<PreparedAction>;
        register(
            provider: AIContextProvider,
            element: HTMLElement & LimeWebComponent,
        ): () => void;
    }
    Index

    Methods

    • Alpha

      Gather context from all registered providers.

      Calls getContext() on each provider and collects the results. Providers that return null (indicating no context available) and providers that throw errors are skipped (errors log a warning).

      Each contribution is enriched with the component's LimeWebComponentContext.

      Summaries and actions are not included.

      Returns GatheredAIContext[]

      An array of enriched context contributions, one from each provider that returned a non-null contribution.

    • Alpha

      Gather the full context, including actions, from all registered providers.

      Gathers the same contributions as gatherContext() without options, and adds the actions from each provider's getActions. A provider that returns null from getContext() but has actions is included with just its actions, and one whose getActions throws is included without actions, with a warning. Action ids are page-wide, so a provider registered more than once lists each action once, and at most 20 actions per provider and 50 per page are listed, the rest dropped with a warning.

      A registry offers this option together with prepareAction, so pass it only when typeof registry.prepareAction === 'function'.

      Parameters

      Returns GatheredAIContextFull[]

      An array of enriched context contributions, one from each provider that returned a non-null contribution or has actions.

    • Alpha

      Gather the summaries and action ids from all registered providers.

      This is the short form of the context, sent with every message. Providers without a summary or actions are skipped, as are providers whose getSummary throws; a provider whose getActions throws is listed without actions. Both log a warning. Action ids are page-wide, so a provider registered more than once lists each action once, and at most 20 actions per provider and 50 per page are listed, the rest dropped with a warning.

      A registry offers this option together with prepareAction, so pass it only when typeof registry.prepareAction === 'function'.

      Parameters

      Returns GatheredAISummary[]

      An array of summaries, one from each provider that has a summary or actions.

      import { PlatformServiceName } from '@limetech/lime-web-components';

      const registry = platform.get(PlatformServiceName.AIContextRegistry);
      const http = platform.get(PlatformServiceName.Http);

      await http.post('my_addon/chat', {
      text: 'Which deals here are about to close?',
      context:
      typeof registry.prepareAction === 'function'
      ? registry.gatherContext({ detail: 'summary' })
      : registry.gatherContext(),
      });
    • Alpha

      Get all registered providers.

      Useful for debugging or introspection.

      Returns AIContextProvider[]

      Array of all registered providers.

    • Alpha

      Prepare an action for running.

      Finds the action with the id among the connected providers' getActions. When several offer the id, the most recently registered provider wins. Action ids are page-wide, and an action dropped by the limit of 20 per provider and 50 per page, with a warning, is 'unavailable'. For an action that needs approval, it calls the action's prepare to get the preview to show the user. When prepare rejects, the registry logs a warning, as it does for a provider that throws while gathering, and returns 'invalid' with the rejection's message.

      Check that it exists before offering actions to the AI.

      Parameters

      • id: string

        The action's id.

      • input: JsonValue

        The input the AI sent for the action.

      Returns Promise<PreparedAction>

      'ready' with a run function for an action without approval, 'needs-approval' with a preview and a run function for one with approval, 'unavailable' when no connected provider offers the id, or 'invalid' when prepare rejected the input or resolved without a preview that is an object with a title. An action that needs approval never comes back as 'ready'.

      import {
      AIActionResult,
      ApprovalPreview,
      JsonValue,
      PlatformServiceName,
      } from '@limetech/lime-web-components';

      type ActionOutcome = AIActionResult | { status: 'denied' };

      declare function askUserToApprove(preview: ApprovalPreview): Promise<boolean>;

      const registry = platform.get(PlatformServiceName.AIContextRegistry);

      async function performAction(
      id: string,
      input: JsonValue
      ): Promise<ActionOutcome> {
      if (!registry.prepareAction) {
      return { status: 'failed', message: 'This page has no actions' };
      }

      const prepared = await registry.prepareAction(id, input);

      if (prepared.status === 'unavailable') {
      return { status: 'failed', message: `${id} is not on this page` };
      }

      if (prepared.status === 'invalid') {
      return { status: 'failed', message: prepared.message };
      }

      if (
      prepared.status === 'needs-approval' &&
      !(await askUserToApprove(prepared.preview))
      ) {
      return { status: 'denied' };
      }

      return prepared.run();
      }
    • Alpha

      Register a context provider.

      The provider will be called when context is gathered via gatherContext(). Multiple providers with the same contextType can coexist - each registration gets a unique internal key.

      Parameters

      • provider: AIContextProvider

        The provider to register.

      • element: HTMLElement & LimeWebComponent

        The host element of the component registering this provider. Enables the registry to:

        • Read the component's LimeWebComponentContext to enrich contributions
        • Skip providers whose elements are not currently connected to the DOM

      Returns () => void

      A function to unregister the provider. Call this when the component is disconnected or the context is no longer relevant.

      import { Element } from '@stencil/core';
      import {
      LimeObject,
      LimeWebComponent,
      PlatformServiceName,
      } from '@limetech/lime-web-components';

      class DealCard {
      @Element()
      private host: HTMLElement & LimeWebComponent;

      private deal: LimeObject | null = null;

      private unregister?: () => void;

      public async connectedCallback() {
      const repository = platform.get(
      PlatformServiceName.LimeObjectRepository
      );

      if (context.limetype && context.id) {
      this.deal = await repository.loadObject(
      context.limetype,
      context.id,
      { properties: ['descriptive'] }
      );
      }

      const registry = platform.get(PlatformServiceName.AIContextRegistry);

      this.unregister = registry.register(
      {
      contextType: 'deal-card',
      getContext: () => ({
      purpose:
      'Shows full details of a deal record. ' +
      'Users can edit fields and manage related records.',
      data: {
      limetype: 'deal',
      id: context.id,
      descriptive: this.deal?.getValue('descriptive') ?? null,
      },
      }),
      },
      this.host
      );
      }

      public disconnectedCallback() {
      this.unregister?.();
      }
      }