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

    Type Alias AIActionAlpha

    Something the AI can do on the page, offered by an AIContextProvider through getActions.

    An action that creates, updates or deletes a Lime object must be an AIActionWithApproval, so the user sees what will change and approves it before it runs. Filtering and navigating need no approval.

    needsApproval is the discriminant. An action whose needsApproval can be true, including one typed as a plain boolean, does not compile without a prepare.

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

    interface Deal {
    id: number;
    name: string;
    value: number;
    timestamp: string;
    }

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

    const dealId = 1001;

    function readDeal(): Promise<Deal> {
    return http.get(`my_addon/deals/${dealId}`);
    }

    function readValue(input: JsonValue): number {
    const value =
    typeof input === 'object' && input !== null && !Array.isArray(input)
    ? input['value']
    : undefined;

    if (typeof value !== 'number' || value < 0) {
    throw new Error('value must be a number of zero or more');
    }

    return value;
    }

    const setDealValue: AIActionWithApproval = {
    id: 'deal.set-value',
    description: 'Set the value of the open deal, in SEK.',
    inputSchema: {
    type: 'object',
    properties: {
    value: { type: 'number', minimum: 0 },
    },
    required: ['value'],
    additionalProperties: false,
    },
    needsApproval: true,
    prepare: async (input) => {
    const value = readValue(input);
    const deal = await readDeal();

    return {
    preview: {
    operation: 'update',
    object: {
    limetype: 'deal',
    id: deal.id,
    descriptive: deal.name,
    },
    title: `Update deal ${deal.name}`,
    changes: [
    {
    label: 'Value',
    before: deal.value.toLocaleString('sv-SE'),
    after: value.toLocaleString('sv-SE'),
    },
    ],
    },
    run: async () => {
    try {
    const current = await readDeal();
    if (current.timestamp !== deal.timestamp) {
    return {
    status: 'failed',
    message: 'The deal has changed since the preview',
    };
    }

    await http.patch(`my_addon/deals/${deal.id}`, { value });
    } catch (error) {
    console.warn('Saving the deal failed:', error);

    return {
    status: 'failed',
    message: 'The deal could not be saved',
    };
    }

    return { status: 'done', result: { id: deal.id } };
    },
    };
    },
    };