mesauth-angular 1.20.0 → 1.21.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -4,6 +4,21 @@ Angular helper library to connect to a backend API and SignalR hub to surface th
4
4
 
5
5
  ## Changelog
6
6
 
7
+ ### v1.21.0 (2026-05-16) — **AI Assistant integration docs**
8
+ - Documentation only: full integration guide for the AI Assistant module added below ([jump](#ai-assistant)). CHANGELOG backfilled for v1.19.0 + v1.20.0 entries.
9
+
10
+ ### v1.20.0 (2026-05-16) — **Ollama + Markdown + same-layer panel**
11
+ - **Ollama is the default LLM provider**. Backend `OpenAiCompatibleLlmClient` POSTs to `${BaseUrl}/chat/completions` — works against Ollama (`http://localhost:11434/v1`), OpenAI, LM Studio, vLLM, Anthropic's OpenAI-compat endpoint, OpenRouter. Tool-call `arguments` parsed in both JSON-string (spec) and parsed-object (Ollama) shapes.
12
+ - **Markdown rendering inside assistant bubbles** via new `MaAiMarkdownPipe`. Supports GFM pipe tables, fenced + inline code, lists, blockquotes, bold/italic, sanitized links. URL allowlist blocks `javascript:` / `data:`. Pipe applies only to assistant bubbles — user input stays plain text.
13
+ - **`quickPrompts` manifest field**: hosted-manifest can override the AI panel empty-state suggestions per deployment without republishing the npm package.
14
+ - **AI chat panel is now same-layer with the host app** — backdrop removed; `z-index: 1040` (above page chrome, below CoreUI modals at 1055). Page stays fully interactive while the panel is open.
15
+
16
+ ### v1.19.0 (2026-05-16) — **AI Assistant module**
17
+ - New `src/ai/` module: `MaAiButtonComponent`, `MaAiChatPanelComponent` (right-side resizable drawer, 320–800 px), `MaAiService` (SSE via `fetch` + `ReadableStream`), `MaAiToolsRegistry`, `provideMesAuthAi(config)` provider.
18
+ - Auto-wired into `ma-user` next to the bell/approval buttons. Toggle off with `[showAi]="false"` on `<ma-user>`.
19
+ - Built-in client tools shipped: `navigate`, `toggle_theme`, `show_toast`, `who_am_i_client`, `reload_page`. Consumer apps register additional tools (incl. write-class) via `provideMesAuthAi({ tools: [...] })` — write tools surface an in-panel approval card.
20
+ - Backend: `POST /ai/chat` SSE endpoint in MesAuth.Api with 7 curated server tools + 3 universal tools (`list_apps`, `list_app_functions`, `call_app_endpoint`) that proxy to consumer apps using each `Application.HostUrl`.
21
+
7
22
  ### v1.6.8 (2026-04-01) - **Permission Header Support**
8
23
  - **New `withXMaPerm()` RxJS operator**: Extracts `X-MA-PERM` permission header from HTTP responses, returning `{ data, allowedActions }`. Use with `observe: 'response'` on any `HttpClient` call for zero-overhead permission-gated UI.
9
24
  - **New `xMaResource()` helper**: Combines `rxResource` with automatic permission extraction for GET endpoints. Returns a signal-based resource with `.value().allowedActions`.
@@ -72,6 +87,7 @@ Angular helper library to connect to a backend API and SignalR hub to surface th
72
87
  - 🔐 **Authentication**: User login/logout with API integration
73
88
  - 🔔 **Real-time Notifications**: SignalR integration for live notifications
74
89
  - ✅ **Approval Workflows**: `<ma-approval-panel>`, `<ma-arv-container>`, `MaApprovalService` for multi-step document approval
90
+ - 🤖 **AI Assistant**: in-browser chat panel (Ollama by default) with markdown rendering, agentic tool loop, server + client tools — see [AI Assistant](#ai-assistant)
75
91
  - 🎨 **Dark/Light Theme**: Automatic theme detection and support
76
92
  - 🖼️ **Avatar Support**: Direct API-based avatar loading
77
93
  - 🍞 **Toast Notifications**: In-app notification toasts
@@ -250,6 +266,138 @@ A standalone component for displaying a slide-out notification panel with real-t
250
266
  notificationPanel.open();
251
267
  ```
252
268
 
269
+ ## AI Assistant
270
+
271
+ Since **v1.19.0** the library ships an in-browser AI chat panel that lives inside `<ma-user>`. It calls MesAuth.Api's `/ai/chat` SSE endpoint, which fronts an LLM (Ollama by default) and runs an agentic loop with two kinds of tools: **server tools** (data queries — `whoami`, `list_my_approvals`, etc.) and **client tools** (UI actions the model triggers in the browser — navigation, dialogs, custom logic the consumer registers).
272
+
273
+ ### 1. Backend prerequisite
274
+
275
+ MesAuth.Api must be running v10.x with the `Ai:*` config block populated. Default points at local Ollama:
276
+
277
+ ```jsonc
278
+ // appsettings.json (MesAuth.Api)
279
+ "Ai": {
280
+ "Enabled": true,
281
+ "Provider": "ollama",
282
+ "BaseUrl": "http://localhost:11434/v1", // any OpenAI-compatible base URL
283
+ "ApiKey": "", // optional; required for OpenAI / Anthropic compat
284
+ "Model": "qwen2.5:7b", // any tool-calling capable model
285
+ "MaxToolCallsPerTurn": 12,
286
+ "MaxTokensPerResponse": 4096,
287
+ "SessionTtlMinutes": 120
288
+ }
289
+ ```
290
+
291
+ Install Ollama (`brew install ollama` / `winget install Ollama.Ollama`) then `ollama pull qwen2.5:7b` (or `llama3.1:8b`, `mistral-nemo`, etc.).
292
+
293
+ ### 2. Frontend setup
294
+
295
+ ```ts
296
+ // app.config.ts
297
+ import { ApplicationConfig, inject } from '@angular/core';
298
+ import { Router } from '@angular/router';
299
+ import { provideHttpClient, withInterceptors } from '@angular/common/http';
300
+ import { provideMesAuth, provideMesAuthAi, mesAuthInterceptor } from 'mesauth-angular';
301
+
302
+ export const appConfig: ApplicationConfig = {
303
+ providers: [
304
+ provideHttpClient(withInterceptors([mesAuthInterceptor])),
305
+ provideMesAuth({
306
+ apiBaseUrl: 'https://mes.kefico.vn/auth',
307
+ userBaseUrl: 'https://mes.kefico.vn/x'
308
+ }),
309
+ provideMesAuthAi({
310
+ enabled: true,
311
+ appName: 'MesExtensionSite', // sent to the AI as host context each turn
312
+ systemPromptExtensions: [
313
+ 'You are inside the MES Extension app. Use SPC tools when the user asks about lines or charts.'
314
+ ],
315
+ tools: [
316
+ // example custom client tool — read-only, no approval card
317
+ {
318
+ name: 'spc_open_chart',
319
+ description: 'Open the SPC chart for a given product line',
320
+ parameters: {
321
+ type: 'object',
322
+ properties: { lineId: { type: 'string', description: 'Production line id' } },
323
+ required: ['lineId']
324
+ },
325
+ readOnly: true,
326
+ handler: (args: { lineId: string }) => {
327
+ inject(Router).navigateByUrl(`/spc/${args.lineId}`);
328
+ return `Opened SPC chart for line ${args.lineId}`;
329
+ }
330
+ }
331
+ ]
332
+ })
333
+ ]
334
+ };
335
+ ```
336
+
337
+ That's all that's needed — the AI button appears in the existing `<ma-user>` header and the chat panel slides in from the right when clicked. No template changes required in the consumer app.
338
+
339
+ ### 3. Built-in client tools
340
+
341
+ | Tool | What it does |
342
+ |---|---|
343
+ | `navigate` | `router.navigateByUrl(path)` |
344
+ | `toggle_theme` | Flip between light/dark via `ThemeService` |
345
+ | `show_toast` | Surface a toast via `ToastService` |
346
+ | `who_am_i_client` | Returns the current user from the browser (synchronous, no API call) |
347
+ | `reload_page` | `window.location.reload()` (write-class — surfaces approval card) |
348
+
349
+ Override / extend by passing your own tools in `provideMesAuthAi({ tools })`. Consumer tools always take precedence over built-ins of the same name.
350
+
351
+ ### 4. Server-tool catalogue (provided by MesAuth.Api)
352
+
353
+ | Tool | Description |
354
+ |---|---|
355
+ | `whoami` | Identity, roles, department of the signed-in user |
356
+ | `list_users` | Search users by name / department / employee code |
357
+ | `list_my_roles` | Roles assigned to the caller |
358
+ | `list_my_approvals` | Pending approval documents waiting on the caller |
359
+ | `list_my_notifications` | Caller's recent notifications (unread by default) |
360
+ | `list_health_endpoints` | Registered health endpoints + latest probe status |
361
+ | `current_time` | Server time (UTC + Vietnam local) |
362
+ | `list_apps` | Apps the caller has any permission in |
363
+ | `list_app_functions` | Paginated endpoints in an app the caller can call (driven by the Authorizer-registered permission catalogue) |
364
+ | `call_app_endpoint` | **Universal proxy** — calls any registered endpoint in any consumer app on the caller's behalf. Permission is re-checked before the call. Requires `Application.HostUrl` to be set in *Auth → Client Apps*. |
365
+
366
+ ### 5. Customising the empty-state suggestions
367
+
368
+ Edit `wwwroot/mesauth-angular/v1/manifest.json` on the MesAuth.Api server (no npm republish needed):
369
+
370
+ ```json
371
+ {
372
+ "version": "1.21.0",
373
+ "quickPrompts": [
374
+ "Show my pending approvals",
375
+ "Who is the supervisor of line A1?",
376
+ "How do I add a new user role?"
377
+ ]
378
+ }
379
+ ```
380
+
381
+ ### 6. Write tools + approval cards
382
+
383
+ A consumer-registered tool with `readOnly: false` (or omitted) surfaces an in-panel approval card showing the constructed `args` before the call runs. The user can choose **Approve**, **Always** (remembers the verb for this session), or **Decline**. Built-in writes (`reload_page`) follow the same flow.
384
+
385
+ ### 7. Disabling the AI feature
386
+
387
+ Three knobs:
388
+
389
+ - **Per app**: `provideMesAuthAi({ enabled: false })` hides the button and the panel.
390
+ - **Per server**: set `Ai:Enabled = false` in MesAuth.Api appsettings — the endpoint returns 503 and the panel shows a friendly error.
391
+ - **Per `<ma-user>`**: `<ma-user [showAi]="false">` hides the button but keeps the service available for programmatic use.
392
+
393
+ ### 8. Cost / safety controls
394
+
395
+ - Server-side cap on tool calls per turn (`Ai:MaxToolCallsPerTurn`, default 12).
396
+ - 8 KB truncation on every tool result fed back to the LLM (prevents context blow-up).
397
+ - Write client tools always require user approval before executing.
398
+ - Server tools re-check user permissions via `IFuncPermService` before running — the AI cannot bypass authorization.
399
+ - `call_app_endpoint` forwards the caller's JWT to consumer apps, so the consumer's `MesAuth.Authorizer` middleware enforces the same permission gate it would for a normal request.
400
+
253
401
  ## Migration Guide
254
402
 
255
403
  ### Upgrading from v0.x to v1.1.0+
@@ -10,7 +10,7 @@ import { DomSanitizer } from '@angular/platform-browser';
10
10
  import { DatePipe, JsonPipe } from '@angular/common';
11
11
 
12
12
  /** Current installed package version — keep in sync with package.json. */
13
- const PACKAGE_VERSION = '1.19.0';
13
+ const PACKAGE_VERSION = '1.21.0';
14
14
  /**
15
15
  * Provides server-driven UI configuration loaded from the hosted manifest.
16
16
  * Components read `labels()` and `features()` signals instead of hardcoded strings.