@hahnpro/ai-sdk
v0.11.1
Published
```bash npm install @hahnpro/ai-sdk ```
Readme
ADAM AI SDK
Installation
npm install @hahnpro/ai-sdkUsage
In your Angular application, you can use the provideAiSdk() function to set up all necessary providers:
// 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:
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
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:
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
- Register Provider
import { provideAiSdk } from '@hahnpro/ai-sdk';
bootstrapApplication(AppComponent, {
providers: [
provideAiSdk(),
...AUTH_PROVIDERS, // AuthAdapter is provided here
],
});- 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:
export class AppComponent implements OnInit {
private readonly auth = inject(AUTH_ADAPTER);
ngOnInit() {
this.auth.init();
}
...
}