# Angular Service

The [`InlineAIPromptService`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptservice) is an [Angular service](https://angular.dev/guide/di/creating-and-using-services#) that provides APIs to create InlineAIPrompt instances dynamically.

This means that the service helps create InlineAIPrompt instances based on user interactions and without the need to define the components in a template.

> For a practical example of using the service approach to integrate the InlineAIPrompt capabilities within existing components, see the [Angular Data Grid AI Column Assistant](https://www.telerik.com/kendo-angular-ui/components/grid/smart-grid/ai-column-assistant) article.

## Getting Started

[Angular dependency injection](https://angular.dev/guide/di%22) injects the `InlineAIPromptService` automatically in the constructor of the component.

To create new InlineAIPrompt instances using the service:

1. Inject the `InlineAIPromptService` in the constructor of your Angular component:

   ```ts
   constructor(private inlineAIPromptService: InlineAIPromptService) {}
   ```

2. To open the InlineAIPrompt, use the [`open`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptservice#open) method of the service and pass the configuration of the newly created InlineAIPrompt instance as an [`InlineAIPromptSettings`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings) parameter object:

   ```ts
   const popupRef: PopupRef = this.inlineAIPromptService.open({
       popupSettings: { anchor: this.anchorElement },
       placeholder: 'Ask AI to assist you...',
       promptCommands: [
           { id: 1, text: 'Summarize' },
           { id: 2, text: 'Explain' }
       ]
       ...
   });
   ```

3. To handle user interactions, subscribe to the events of the InlineAIPrompt instance by obtaining it through the popup reference:

   ```ts
   const promptInstance = popupRef.content?.instance;

   if (promptInstance) {
       promptInstance.promptRequest.subscribe((event: InlineAIPromptRequestEvent) => {
           // Handle the prompt request
       });

       promptInstance.commandExecute.subscribe((command: InlineAIPromptCommand) => {
           // Handle the command execution
       });
   }
   ```

The following example demonstrates how to use the `InlineAIPromptService` to dynamically create InlineAIPrompt instances.

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/conversational-ui/inlineaiprompt/service/source.txt)

## Configuration Options

The `InlineAIPromptService` supports all the configuration options available in the template-based approach through the [`InlineAIPromptSettings`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings) configuration object. This provides you with the same level of flexibility and customization capabilities when creating InlineAIPrompt instances dynamically.

> For detailed information about all available configuration options, refer to the [Angular InlineAIPrompt Configuration](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/inline-aiprompt/configuration) article.

### Basic Configuration

When opening an InlineAIPrompt through the service, you can configure essential properties such as [`promptValue`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings#promptvalue), [`placeholder`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings#placeholder), [`width`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings#width), and [`maxHeight`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings#maxheight):

```ts
const popupRef = this.inlineAIPromptService.open({
    popupSettings: { anchor: this.anchorElement },
    placeholder: 'Ask AI about this content...',
    width: 600,
    maxHeight: 400
});
```

### Interactive Elements

The service supports configuring [`promptCommands`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings#promptcommands) for predefined actions and [`outputActions`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings#outputactions) for managing AI responses. These properties enable users to quickly apply common AI operations without typing custom prompts:

```ts
const popupRef = this.inlineAIPromptService.open({
    promptCommands: [
        { id: 'summarize', text: 'Summarize' },
        { id: 'explain', text: 'Explain' }
    ],
    outputActions: [
        { name: 'copy' },
        { name: 'retry' }
    ]
});
```

### Advanced Features

You can enable advanced features such as [`streaming`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings#streaming) for real-time responses and [`enableSpeechToTextButton`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings#enablespeechtotextbutton) for voice input. The [`popupSettings`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings#popupsettings) property allows you to control the positioning and behavior of the popup container:

```ts
const popupRef = this.inlineAIPromptService.open({
    popupSettings: {
        anchor: this.anchorElement,
        anchorAlign: { horizontal: 'left', vertical: 'bottom' },
        popupAlign: { horizontal: 'left', vertical: 'top' }
    },
    streaming: true,
    enableSpeechToTextButton: true,
});
```

## Dynamic Response Management

After opening an InlineAIPrompt through the `InlineAIPromptService`, you can access the component instance through the popup reference, which exposes all the events and properties needed to create intuitive AI-powered user experiences.

You can update the [`promptOutput`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptcomponent#promptoutput) property in real-time and control the [`streaming`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptcomponent#streaming) state to provide users with live feedback as AI responses are generated. This creates a more engaging and responsive user experience compared to traditional request-response patterns.

> For a runnable demo of the streaming functionality, refer to the [Streaming AI Responses with InlineAIPrompt](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/inline-aiprompt/streaming) article.

```ts
constructor(public aiService: MyAIService) {}

private processAIRequest(userPrompt: string) {
    const promptInstance = popupRef.content?.instance;
    promptInstance.streaming = true;
    
    this.aiService.streamResponse(userPrompt).subscribe({
        next: (chunk) => {
            // Progressive response updates
            if (!promptInstance.promptOutput) {
                promptInstance.promptOutput = {
                    id: Date.now().toString(),
                    prompt: userPrompt,
                    output: chunk
                };
            } else {
                promptInstance.promptOutput.output += chunk;
            }
        },
        complete: () => promptInstance.streaming = false,
    });
}
```

## Event Handling

To handle events when using the service approach, you need to access the InlineAIPrompt component instance through the popup reference and programmatically subscribe to the events you need.

> For a complete list of all available events and their descriptions, refer to the [Angular InlineAIPrompt Events](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/inline-aiprompt/events) article.

```ts
const popupRef = this.inlineAIPromptService.open({...});
const promptInstance = popupRef.content?.instance;

if (promptInstance) {
    // Subscribe to events based on your requirements
    promptInstance.promptRequest.subscribe((event: InlineAIPromptRequestEvent) => {
        this.processAIRequest(event.prompt);
    });

    promptInstance.commandExecute.subscribe((command: InlineAIPromptCommand) => {
        this.processAICommand(command);
    });

    promptInstance.outputActionClick.subscribe((event: InlineAIPromptOutputActionClickEvent) => {
        this.handleOutputAction(event);
    });
}
```

## Programmatic Control

The [`open`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptservice#open) method of the `InlineAIPromptService` returns a `PopupRef` that provides direct control over the popup lifecycle. This reference allows you to programmatically manage when and how InlineAIPrompt instances are displayed and closed within your application.

The popup reference exposes a [`close`](https://www.telerik.com/kendo-angular-ui/components/popup/api/popupref#close) method that enables you to programmatically close the InlineAIPrompt popup at any time:

```ts
private popupRef: PopupRef | null = null;

public openInlineAIPrompt(anchorElement: HTMLElement): void {
    // Store the popup reference for later control
    this.popupRef = this.inlineAIPromptService.open({...});
}

public closePopupRef(): void {
    // Programmatically close the popup
    if (this.popupRef) {
        this.popupRef.close();
        this.popupRef = null;
    }
}
```

The popup reference is essential for creating dynamic AI interactions where prompts need to appear and disappear based on user actions or specific business logic.

The following example demonstrates how you can store the InlineAIPrompt popup reference and use it to programmatically control when the popup appears:

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/conversational-ui/inlineaiprompt/service/programmatic-control/source.txt)

## Custom Styling and Containers

The `InlineAIPromptService` supports advanced customization through the [`popupSettings`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptsettings#popupsettings) property. This property accepts an object of type [`InlineAIPromptPopupSettings`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptpopupsettings), which allows you to control various aspects of the popup's behavior, positioning, and appearance.

For example, you can utilize the [`appendTo`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptpopupsettings#appendto) setting to append the InlineAIPrompt popup to a custom container in the DOM hierarchy. Additionally, you can apply custom classes through the [`popupClass`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptpopupsettings#popupclass) property and enhance the appearance of the popup with custom CSS styling.

```ts
const popupRef = this.inlineAIPromptService.open({
    popupSettings: {
        anchor: this.anchorElement,
        appendTo: this.customViewContainer,
        popupClass: 'custom-popup-class'
    }
});
```

[Demo Source](https://demos.telerik.com/kendo-angular-ui/demos/conversational-ui/inlineaiprompt/service/custom-popup/source.txt)

## Suggested Links

* [Angular InlineAIPrompt Configuration](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/inline-aiprompt/configuration)
* [Overview of the InlineAIPrompt](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/inline-aiprompt)
* [API Reference of the `InlineAIPromptComponent`](https://www.telerik.com/kendo-angular-ui/components/conversational-ui/api/inlineaipromptcomponent)
