# Angular InlineAIAssist Component

InlineAIAssist rewrites the selected text of a field with AI: improve, fix, shorten, change the tone or follow your own instructions, with the answer streamed into a preview to replace, retry or discard. It works with any model through a small adapter to your own backend, and the replacement is undone with Ctrl+Z.

## Basic

Give the field to for . Select some text, or none to work on the whole field, then press the button, the small button that appears next to a selection, or Ctrl+J ( ⌘J on a Mac) in the field. Pick an action or type your own instructions: the answer streams into a preview, to Replace the text, Try again or Discard . The replacement is an edit of the field, undone with Ctrl+Z . The demos of this page answer offline with fakeAIAssistAdapter and canned rewrites; the Adapter section plugs a real model.

```typescript
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { fakeAIAssistAdapter } from '@exhaustive-ui/core/aiassist';
import { InlineAIAssistModule } from '@exhaustive-ui/core/inlineaiassist';
import { TextareaModule } from '@exhaustive-ui/core/textarea';

@Component({
    template: `
        <div class="card flex flex-col gap-3">
            <label for="ia-message" class="font-medium">Message</label>
            <textarea #message id="ia-message" pTextarea [(ngModel)]="text" rows="5" class="w-full"></textarea>
            <p-inline-ai-assist [for]="message" [adapter]="adapter" />
        </div>
    `,
    standalone: true,
    imports: [InlineAIAssistModule, TextareaModule, FormsModule]
})
export class InlineaiassistBasicDemo {
    text = 'hi team, i wanted to let you know that teh release is delayed to friday because we found a bug in the payment page , sorry for the trouble.';

    adapter = fakeAIAssistAdapter({
        respond: ({ task, text, context }) => {
            if (task !== 'inline') {
                return '';
            }
            const rewrites: Record<string, string> = {
                improve: 'Hi team, the release moves to Friday: we found a bug in the payment page and are fixing it. Sorry for the trouble.',
                fix: 'Hi team, I wanted to let you know that the release is delayed to Friday because we found a bug in the payment page. Sorry for the trouble.',
                shorten: 'Hi team, the release moves to Friday because of a payment page bug.',
                expand: 'Hi team, I wanted to let you know that the release is delayed to Friday. During the final checks we found a bug in the payment page that could charge some customers twice, so we are fixing it and testing again. Sorry for the trouble, and thank you for your patience.',
                simplify: 'Hi team, the release is late. It is now on Friday. We found a bug in the payment page.',
                professional: 'Dear team, please note that the release has been postponed to Friday, as an issue was identified in the payment page. We apologize for any inconvenience.',
                friendly: 'Hey team! Quick heads-up: the release is moving to Friday, we caught a little bug in the payment page. Sorry about that, and thanks for bearing with us!'
            };
            if (context.action === 'prompt') {
                return `${text.trim()} (rewritten for: ${context.prompt})`;
            }
            return context.wholeField ? rewrites[context.action] : this.rewriteSelection(context.action, text.trim());
        }
    });

    /** Canned edits of a selection, for the demo. */
    rewriteSelection(action: string, text: string): string {
        const fixed = text
            .replace(/\bteh\b/g, 'the')
            .replace(/\bi\b/g, 'I')
            .replace(/\s+([,.])/g, '$1');
        const words = fixed.split(/\s+/);
        switch (action) {
            case 'fix':
                return fixed.charAt(0).toUpperCase() + fixed.slice(1);
            case 'shorten':
                return words.slice(0, Math.max(2, Math.ceil(words.length / 2))).join(' ');
            case 'expand':
                return `${fixed}, as we discussed in the last meeting`;
            case 'professional':
                return `please note that ${fixed}`;
            case 'friendly':
                return `${fixed} 🙂`;
            default:
                return fixed;
        }
    }
}
```

## Directive

The pInlineAIAssist directive adds an assistant to an input, a textarea, a contenteditable element or a component that holds one, such as an Editor, with the adapter given to provideAIAssist . It has no button: the shortcut and the small button next to a selection open it. To configure it, give it an InlineAIAssist: [pInlineAIAssist]="assist" .

```typescript
import { Component } from '@angular/core';
import { fakeAIAssistAdapter, provideAIAssist } from '@exhaustive-ui/core/aiassist';
import { InlineAIAssistModule } from '@exhaustive-ui/core/inlineaiassist';
import { InputTextModule } from '@exhaustive-ui/core/inputtext';

@Component({
    template: `
        <div class="card flex flex-col gap-4">
            <div class="flex flex-col gap-2">
                <label for="ia-subject" class="font-medium">Subject</label>
                <input pInputText id="ia-subject" pInlineAIAssist value="Teh plan for next week" />
            </div>
            <div class="flex flex-col gap-2">
                <span id="ia-notes-label" class="font-medium">Notes</span>
                <div contenteditable="true" role="textbox" aria-multiline="true" aria-labelledby="ia-notes-label" pInlineAIAssist class="min-h-24 rounded-md border border-surface-300 dark:border-surface-600 p-3">
                    <p class="m-0">We need to finish teh wrok on the dashboard before the demo.</p>
                </div>
            </div>
        </div>
    `,
    standalone: true,
    imports: [InlineAIAssistModule, InputTextModule],
    providers: [provideAIAssist(
            fakeAIAssistAdapter({
                respond: (request) => {
                    const fixed = request.text
                        .replace(/\bteh\b/g, 'the')
                        .replace(/\bTeh\b/g, 'The')
                        .replace(/\bwrok\b/g, 'work');
                    return request.task === 'inline' && request.context.action === 'fix' ? fixed : `${fixed.trim()}, reviewed.`;
                }
            })
        )]
})
export class InlineaiassistDirectiveDemo {
}
```

## Adapter and Streaming

InlineAIAssist sends no request of its own and needs no SDK: an adapter connects it to the model of your choice. Its complete function receives a typed request, with the task ( inline here), the selected text , ready-made instructions , a context (the action, the free prompt, the text around the selection) and an AbortSignal . It returns the new text, a promise of it, or a stream of chunks: an Observable or an AsyncIterable of strings, appended to the preview as they arrive. Give the adapter to one component with adapter , or to all of them with provideAIAssist , in the providers of the application or of a component. The same adapter serves InlineAIAssist, SmartPasteButton and SmartTextarea: switch on request.task if your backend needs to. Never put the API key of a model provider in the browser. Call an endpoint of your own application, here /api/ai , that holds the key and streams the answer back. The signal cancels the request when the user stops, retries, discards or closes the assistant. The endpoint sends the instructions and the text to the model and streams its text back, here with plain fetch calls to an OpenAI-compatible chat completions API; adapt it to your provider. In tests and offline demos, fakeAIAssistAdapter answers with canned texts streamed word by word, or throws to simulate an error.

## Customization

actions lists the actions, in order: the identifiers of the built-in ones, improve , fix , shorten , expand , simplify , professional and friendly , and your own actions with an id , a label , the instructions sent to the model and optional search keywords . freePrompt allows typed instructions, shortcut changes the keyboard shortcut ( Mod is Ctrl, or ⌘ on Apple devices; null disables it), iconOnly , variant and size style the button, and labels translates the texts.

```typescript
import { Component } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { fakeAIAssistAdapter } from '@exhaustive-ui/core/aiassist';
import { InlineAIAssistAction, InlineAIAssistDefaultAction, InlineAIAssistModule } from '@exhaustive-ui/core/inlineaiassist';
import { TextareaModule } from '@exhaustive-ui/core/textarea';

@Component({
    template: `
        <div class="card flex flex-col gap-3">
            <label for="ia-product" class="font-medium">Product description</label>
            <textarea #product id="ia-product" pTextarea [(ngModel)]="text" rows="3" class="w-full"></textarea>
            <div class="flex items-center gap-2">
                <p-inline-ai-assist [for]="product" [adapter]="adapter" [actions]="actions" shortcut="Alt+Shift+A" [iconOnly]="true" variant="text" [labels]="{ trigger: 'Rewrite with AI', promptPlaceholder: 'Describe the change…' }" />
                <span class="text-sm text-surface-600 dark:text-surface-300">Or press Alt+Shift+A in the field.</span>
            </div>
        </div>
    `,
    standalone: true,
    imports: [InlineAIAssistModule, TextareaModule, FormsModule]
})
export class InlineaiassistCustomizationDemo {
    text = 'Lightweight running shoes with a breathable mesh and a cushioned sole for long distances.';

    actions: (InlineAIAssistDefaultAction | InlineAIAssistAction)[] = [
        'fix',
        'shorten',
        { id: 'seo', label: 'Optimize for search engines', instructions: 'Rewrite the product description for search engines, with the main keywords early, under 160 characters.', keywords: ['seo'] },
        { id: 'french', label: 'Translate into French', instructions: 'Translate the text into French.', keywords: ['translate'] }
    ];

    adapter = fakeAIAssistAdapter({
        respond: (request) => {
            if (request.task !== 'inline') {
                return '';
            }
            const answers: Record<string, string> = {
                fix: request.text,
                shorten: 'Light, breathable running shoes with a cushioned sole.',
                seo: 'Lightweight running shoes: breathable mesh, cushioned sole, made for long-distance running.',
                french: 'Chaussures de course légères avec un mesh respirant et une semelle amortie pour les longues distances.'
            };
            return answers[request.context.action] ?? `${request.text} (${request.context.prompt})`;
        }
    });
}
```

## Accessibility

Screen Reader The button is a native button named by its text, "Ask AI", which becomes its aria-label with iconOnly ; aria-expanded and aria-controls tell that it opens the assistant. The field gets an aria-keyshortcuts attribute with the shortcut, so that screen readers can announce it. The small button shown next to a selection is a pointer shortcut only: it is hidden from assistive technologies and out of the tab order, and the keyboard shortcut does the same. The assistant is a non-modal dialog named "AI assistant". Its prompt is a combobox that controls a listbox of actions: the active action is given by aria-activedescendant , so the focus stays in the prompt while the arrows move through the list, and typing filters it. While the answer is written, the result region is aria-busy and the focus is on the stop button; once it is ready, the focus moves to the replace button, which is described by the preview, and a polite live region announces "Suggestion ready". The replacement, the discard and the stop are announced the same way, without moving the focus. Closing the assistant gives the focus back to the field with the same selection, and after a replacement the new text is selected. The replacement is a native edit of the field, so Ctrl+Z undoes it. The texts are translated with labels , and the aiAssist , aiAssistant and aiPrompt keys of the aria translation.

## Inline A I Assist

InlineAIAssist rewrites the selected text of an input, a textarea, a contenteditable element or an Editor with the model of your choice: improve, fix the grammar, shorten, expand, change the tone, your own actions or a free prompt. The answer streams into a preview, to replace the selection, try again or discard.

### Props

| Name | Type | Default | Description |
|------|------|---------|-------------|
| dt | Object | undefined | Defines scoped design tokens of the component. |
| unstyled | boolean | undefined | Indicates whether the component should be rendered without styles. |
| pt | InlineAIAssistPassThrough | undefined | Used to pass attributes to DOM elements inside the component. |
| ptOptions | PassThroughOptions | undefined | Used to configure passthrough(pt) options of the component. |
| for | InlineAIAssistTarget | - | The element the assistant works on: an input, a textarea, a contenteditable element or an element that holds one, such as an Editor; a template reference, an ElementRef, a CSS selector or an element with the pInlineAIAssist directive. |
| adapter | AIAssistAdapter \| AIAssistCompleteFn | - | Adapter that asks the model, or its complete function. Defaults to the one given to provideAIAssist. |
| actions | (InlineAIAssistDefaultAction \| InlineAIAssistAction)[] | - | Actions of the list: identifiers of built-in actions (improve, fix, shorten, expand, simplify, professional, friendly) and your own actions with their instructions. |
| freePrompt | boolean | true | Whether what is typed in the prompt input can be sent as free instructions. |
| shortcut | string | 'Mod+J' | Keyboard shortcut that opens the assistant from the field, such as  `Mod+J`  (Ctrl+J, or ⌘J on Apple devices),  `Alt+Shift+A` . null disables it. |
| button | boolean | true | Whether the button that opens the assistant is shown. |
| selectionButton | boolean | true | Whether a small button appears next to a selection made with the mouse, to open the assistant. Keyboard users open it with the shortcut. |
| iconOnly | boolean | false | Whether the button only shows its icon. Its text is then its accessible name. |
| variant | "text" \| "outlined" | 'outlined' | Variant of the button. |
| size | "small" \| "large" | - | Size of the button. |
| disabled | boolean | false | Whether the assistant cannot be opened. |
| contextLength | number | CONTEXT_LENGTH | Number of characters before and after the selection sent to the model as context. |
| announce | boolean | true | Whether the progress, the result and the replacement are announced to screen readers through a polite live region. |
| labels | Partial<InlineAIAssistLabels> | - | Texts of the component, merged with the translation and the defaults. |

### Emits

| Name | Parameters | Description |
|------|------------|-------------|
| onOpen | event: InlineAIAssistOpenEvent | Callback to invoke when the assistant opens. |
| onClose | value: void | Callback to invoke when the assistant closes. |
| onAccept | event: InlineAIAssistAcceptEvent | Callback to invoke when a result replaces the text. |
| onError | event: InlineAIAssistErrorEvent | Callback to invoke when the adapter fails. |

### Methods

| Name | Parameters | Return Type | Description |
|------|------------|-------------|-------------|
| open |  | void | Opens the assistant on the selection of the element, or on its whole text when nothing is selected, and moves the focus to the prompt input. |
| toggle |  | void | Opens the assistant, or closes it when it is open. |
| run | action: string \| InlineAIAssistAction, prompt: string | void | Runs an action, or free instructions, on the text, without the list:  `run('shorten')` ,  `run(null, 'Translate into French')` . |
| accept |  | void | Replaces the text with the result, as an edit that Ctrl+Z or ⌘Z undoes where the field allows it, then closes the assistant and gives the focus back to the field. |
| retry |  | void | Runs the last action again. |
| stop |  | void | Stops the generation; the text received so far stays in the preview. |
| discard |  | void | Drops the result, closes the assistant and gives the focus and the selection back to the field. |
| close | restoreFocus: boolean | void | Closes the assistant and cancels the request in progress. |

## Pass Through Options

| Name | Type | Description |
|------|------|-------------|
| host | PassThroughOption<HTMLElement, I> | Used to pass attributes to the host's DOM element. |
| root | PassThroughOption<HTMLElement, I> | Used to pass attributes to the root's DOM element. |
| pcTriggerButton | ButtonPassThrough | Used to pass attributes to the button that opens the assistant. |
| selectionButton | PassThroughOption<HTMLButtonElement, I> | Used to pass attributes to the small button shown next to a selection. |
| panel | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the panel, the non-modal dialog of the assistant. |
| promptBox | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the element around the prompt input. |
| prompt | PassThroughOption<HTMLInputElement, I> | Used to pass attributes to the prompt input, a combobox. |
| list | PassThroughOption<HTMLUListElement, I> | Used to pass attributes to the list of actions. |
| option | PassThroughOption<HTMLLIElement, I> | Used to pass attributes to an action. |
| result | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the result area. |
| resultTitle | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the title of the result. |
| preview | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the preview of the new text. |
| error | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the error message. |
| footer | PassThroughOption<HTMLDivElement, I> | Used to pass attributes to the buttons under the result. |
| pcActionButton | ButtonPassThrough | Used to pass attributes to the Replace, Try again, Discard and Stop buttons. |
| liveRegion | PassThroughOption<HTMLSpanElement, I> | Used to pass attributes to the live region. |

## Theming

### CSS Classes

| Class | Description |
|-------|-------------|
| p-inlineaiassist | Class name of the root element |
| p-inlineaiassist-open | Class name of the root element while the panel is open |
| p-inlineaiassist-trigger-button | Class name of the button that opens the assistant |
| p-inlineaiassist-icon | Class name of the icons |
| p-inlineaiassist-selection-button | Class name of the small button shown next to a selection |
| p-inlineaiassist-panel | Class name of the panel |
| p-inlineaiassist-panel-busy | Class name of the panel while a text is generated |
| p-inlineaiassist-prompt-box | Class name of the element around the prompt input |
| p-inlineaiassist-prompt-icon | Class name of the icon of the prompt input |
| p-inlineaiassist-prompt | Class name of the prompt input |
| p-inlineaiassist-list | Class name of the list of actions |
| p-inlineaiassist-option | Class name of an action |
| p-inlineaiassist-option-custom | Class name of the option that sends the free prompt |
| p-inlineaiassist-result | Class name of the result area |
| p-inlineaiassist-result-title | Class name of the title of the result |
| p-inlineaiassist-preview | Class name of the preview of the new text |
| p-inlineaiassist-preview-streaming | Class name of the preview while the text streams |
| p-inlineaiassist-error | Class name of the error message |
| p-inlineaiassist-footer | Class name of the buttons under the result |

### Design Tokens

| Token | CSS Variable | Description |
|-------|--------------|-------------|
| inlineaiassist.transition.duration | --p-inlineaiassist-transition-duration | Transition duration of root |
| inlineaiassist.icon.size | --p-inlineaiassist-icon-size | Size of the icons |
| inlineaiassist.selection.button.gap | --p-inlineaiassist-selection-button-gap | Gap of the small button shown next to a selection |
| inlineaiassist.selection.button.padding | --p-inlineaiassist-selection-button-padding | Padding of the small button shown next to a selection |
| inlineaiassist.selection.button.border.radius | --p-inlineaiassist-selection-button-border-radius | Border radius of the small button shown next to a selection |
| inlineaiassist.selection.button.background | --p-inlineaiassist-selection-button-background | Background of the small button shown next to a selection |
| inlineaiassist.selection.button.hover.background | --p-inlineaiassist-selection-button-hover-background | Hover background of the small button shown next to a selection |
| inlineaiassist.selection.button.border.color | --p-inlineaiassist-selection-button-border-color | Border color of the small button shown next to a selection |
| inlineaiassist.selection.button.color | --p-inlineaiassist-selection-button-color | Color of the small button shown next to a selection |
| inlineaiassist.selection.button.shadow | --p-inlineaiassist-selection-button-shadow | Shadow of the small button shown next to a selection |
| inlineaiassist.selection.button.font.size | --p-inlineaiassist-selection-button-font-size | Font size of the small button shown next to a selection |
| inlineaiassist.panel.width | --p-inlineaiassist-panel-width | Width of the panel of the assistant |
| inlineaiassist.panel.background | --p-inlineaiassist-panel-background | Background of the panel of the assistant |
| inlineaiassist.panel.border.color | --p-inlineaiassist-panel-border-color | Border color of the panel of the assistant |
| inlineaiassist.panel.border.radius | --p-inlineaiassist-panel-border-radius | Border radius of the panel of the assistant |
| inlineaiassist.panel.color | --p-inlineaiassist-panel-color | Color of the panel of the assistant |
| inlineaiassist.panel.shadow | --p-inlineaiassist-panel-shadow | Shadow of the panel of the assistant |
| inlineaiassist.prompt.gap | --p-inlineaiassist-prompt-gap | Gap of the prompt input |
| inlineaiassist.prompt.padding | --p-inlineaiassist-prompt-padding | Padding of the prompt input |
| inlineaiassist.prompt.icon.color | --p-inlineaiassist-prompt-icon-color | Icon color of the prompt input |
| inlineaiassist.prompt.placeholder.color | --p-inlineaiassist-prompt-placeholder-color | Placeholder color of the prompt input |
| inlineaiassist.prompt.focus.border.color | --p-inlineaiassist-prompt-focus-border-color | Focus border color of the prompt input |
| inlineaiassist.list.padding | --p-inlineaiassist-list-padding | Padding of the list of actions |
| inlineaiassist.list.gap | --p-inlineaiassist-list-gap | Gap of the list of actions |
| inlineaiassist.list.max.height | --p-inlineaiassist-list-max-height | Max height of the list of actions |
| inlineaiassist.option.padding | --p-inlineaiassist-option-padding | Padding of an action |
| inlineaiassist.option.border.radius | --p-inlineaiassist-option-border-radius | Border radius of an action |
| inlineaiassist.option.color | --p-inlineaiassist-option-color | Color of an action |
| inlineaiassist.option.focus.background | --p-inlineaiassist-option-focus-background | Focus background of an action |
| inlineaiassist.option.focus.color | --p-inlineaiassist-option-focus-color | Focus color of an action |
| inlineaiassist.result.gap | --p-inlineaiassist-result-gap | Gap of the result area |
| inlineaiassist.result.padding | --p-inlineaiassist-result-padding | Padding of the result area |
| inlineaiassist.result.title.color | --p-inlineaiassist-result-title-color | Title color of the result area |
| inlineaiassist.result.title.font.size | --p-inlineaiassist-result-title-font-size | Title font size of the result area |
| inlineaiassist.result.title.font.weight | --p-inlineaiassist-result-title-font-weight | Title font weight of the result area |
| inlineaiassist.preview.max.height | --p-inlineaiassist-preview-max-height | Max height of the preview of the new text |
| inlineaiassist.preview.padding | --p-inlineaiassist-preview-padding | Padding of the preview of the new text |
| inlineaiassist.preview.border.radius | --p-inlineaiassist-preview-border-radius | Border radius of the preview of the new text |
| inlineaiassist.preview.background | --p-inlineaiassist-preview-background | Background of the preview of the new text |
| inlineaiassist.preview.color | --p-inlineaiassist-preview-color | Color of the preview of the new text |
| inlineaiassist.preview.focus.ring.width | --p-inlineaiassist-preview-focus-ring-width | Focus ring width of the preview of the new text |
| inlineaiassist.preview.focus.ring.style | --p-inlineaiassist-preview-focus-ring-style | Focus ring style of the preview of the new text |
| inlineaiassist.preview.focus.ring.color | --p-inlineaiassist-preview-focus-ring-color | Focus ring color of the preview of the new text |
| inlineaiassist.preview.focus.ring.offset | --p-inlineaiassist-preview-focus-ring-offset | Focus ring offset of the preview of the new text |
| inlineaiassist.error.color | --p-inlineaiassist-error-color | Color of the error message |
| inlineaiassist.footer.gap | --p-inlineaiassist-footer-gap | Gap of the buttons under the result |

