npm package: @hahnpro/ai-sdk
Description: ```bash npm install @hahnpro/ai-sdk ```
Latest version: 0.11.1

# ADAM AI SDK

## Installation

```bash
npm install @hahnpro/ai-sdk
```

## Usage

In your Angular application, you can use the `provideAiSdk()` function to set up all necessary providers:

```typescript
// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { provideAiSdk } from '@hahnpro/ai-sdk';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, {
  providers: [
    provideAiSdk(),
    // other providers...
  ],
}).catch((err) => console.error(err));
```

The `provideAiSdk()` function includes the following configurations:

- Transloco module for internationalization
- Markdown support (ngx-markdown)
- Logger configuration (ngx-logger)

For more granular control, you can also use the individual providers:

```typescript
import { bootstrapApplication } from '@angular/platform-browser';
import { importProvidersFrom } from '@angular/core';
import { AiTranslocoModule, provideAiMarkdown } from '@hahnpro/ai-sdk';
import { LoggerModule, NgxLoggerLevel } from 'ngx-logger';
import { AppComponent } from './app/app.component';

bootstrapApplication(AppComponent, {
  providers: [
    importProvidersFrom(
      AiTranslocoModule,
      LoggerModule.forRoot({
        level: NgxLoggerLevel.DEBUG,
        disableConsoleLogging: false,
      }),
    ),
    provideAiMarkdown(),
    // other providers...
  ],
}).catch((err) => console.error(err));
```

## User Authentication

### Overview

The `AuthAdapter` is an interface that provides a unified API for different authentication libraries. It abstracts the specific authentication implementation and allows using various auth providers.

### Interface Definition

``` typescript
export interface AuthAdapter {
  init(): Promise<void>;
  isAuthenticated(): Promise<boolean>;
  getAccessToken(): Promise<string>;
  loginRedirect(options?: { redirectUri?: string }): Promise<void>;
  loginPopup?(): Promise<void>;
  logout?(): Promise<void>;
}
```

### Required Fields

`init()`: Initializes the auth provider (e.g., checks existing sessions)

`isAuthenticated()`: Checks if the user is authenticated

`getAccessToken()`: Returns the current access token

`loginRedirect()`: Starts the login flow with redirect

### Optional Fields
`loginPopup()`: Starts the login flow in a popup window

`logout()`: Logs out the user

### Implementation with OIDC
Here's an example implementation using angular-auth-oidc-client:
``` typescript

import { AUTH_ADAPTER, AuthAdapter } from '@hahnpro/ai-sdk';
import { OidcSecurityService } from 'angular-auth-oidc-client';
import { lastValueFrom } from 'rxjs';

export const AUTH_PROVIDERS = [
  // OIDC Provider configuration...
  {
    provide: AUTH_ADAPTER,
    deps: [OidcSecurityService],
    useFactory: (oidc: OidcSecurityService): AuthAdapter => ({
      init: async () => {
        // Automatically checks existing authentication
        await lastValueFrom(oidc.checkAuth());
      },
      isAuthenticated: () => {
        return lastValueFrom(oidc.isAuthenticated());
      },
      getAccessToken(): Promise<string> {
        return lastValueFrom(oidc.getAccessToken());
      },
      loginRedirect: async (options) => {
        // Optional: use redirect URL
        oidc.authorize(options?.redirectUri);
      },
      loginPopup: async () => {
        await lastValueFrom(oidc.authorizeWithPopUp());
      },
      logout: async () => {
        await lastValueFrom(oidc.logoff());
      },
    }),
  },
];
```

### Injection in Angular
1. Register Provider
``` typescript
import { provideAiSdk } from '@hahnpro/ai-sdk';

bootstrapApplication(AppComponent, {
  providers: [
    provideAiSdk(),
    ...AUTH_PROVIDERS, // AuthAdapter is provided here
  ],
});
```
2. Initalize Auth Adapter
At some point before using the Auth Adapter, you need to initialize it. This can be done by calling the `init()` method of the Auth Adapter:
``` typescript
export class AppComponent implements OnInit {
  private readonly auth = inject(AUTH_ADAPTER);

  ngOnInit() {
    this.auth.init();
  }
  ...
}
```