@5minds/processcube_app_sdk 8.4.0-insiders.2 → 8.5.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
@@ -1,58 +1,949 @@
1
- # ProcessCube.App.SDK
1
+ # ProcessCube App SDK
2
2
 
3
- Das SDK beinhaltet Komponenten und Funktionen für Frontend und Backend (Client/Server) zur einfachen und schnellen Entwicklung einer ProcessCube App auf Basis von [Next.js](https://nextjs.org/).
3
+ Das `@5minds/processcube_app_sdk` ist ein TypeScript-SDK zur Entwicklung von ProcessCube-Anwendungen mit [Next.js](https://nextjs.org/). Es stellt React-Komponenten, Server-Funktionen und gemeinsam genutzte Utilities für Prozessvisualisierung (BPMN), dynamische Formulare und die Integration mit der ProcessCube-Engine bereit.
4
4
 
5
- ## Installation zur Verwendung
5
+ ## Überblick
6
+
7
+ ### Features
8
+
9
+ - **Next.js 15+** App Router mit React 19 Server Components
10
+ - **Vollständig typisiert** durch TypeScript-Support
11
+ - **OAuth 2.0 Authentifizierung** via Authority-Integration (NextAuth + Client Credentials)
12
+ - **Ready-to-use React-Komponenten** für BPMN-Viewer, Prozess-Inspektion und dynamische Formulare
13
+ - **Sichere Engine-Kommunikation** über Server-Side Functions und Server Actions
14
+ - **Automatische Worker-Registrierung** für External Tasks
15
+ - **Authority Client** für Server-to-Server User-Administration
16
+
17
+ ### Architektur
18
+
19
+ Das SDK hat eine Drei-Schichten-Modulstruktur mit strikter Trennung:
20
+
21
+ ```mermaid
22
+ graph TD
23
+ subgraph SDK["@5minds/processcube_app_sdk"]
24
+ direction TB
25
+ Common["Common<br/><code>@5minds/processcube_app_sdk</code><br/><br/>Gemeinsame Typen, Komponenten<br/>und Funktionen"]
26
+
27
+ Server["Server<br/><code>@5minds/processcube_app_sdk/server</code><br/><br/>Engine-Client, Server Actions,<br/>External Tasks, Auth, Authority Client"]
28
+
29
+ Client["Client<br/><code>@5minds/processcube_app_sdk/client</code><br/><br/>React-Komponenten:<br/>BPMNViewer, DynamicUi,<br/>ProcessInstanceInspector"]
30
+ end
31
+
32
+ Server -->|importiert| Common
33
+ Client -->|importiert| Common
34
+
35
+ Engine["ProcessCube Engine"]
36
+ Authority["ProcessCube Authority"]
37
+ Browser["Browser / React"]
38
+
39
+ Server -->|HTTP| Engine
40
+ Server -->|OAuth2 / HTTP| Authority
41
+ Client --> Browser
42
+ ```
43
+
44
+ ### Export-Map
45
+
46
+ Das Paket hat drei Einstiegspunkte — jeder mit ESM- und CJS-Support:
47
+
48
+ ```mermaid
49
+ graph LR
50
+ subgraph Imports
51
+ I1["import _ from<br/>'@5minds/processcube_app_sdk'"]
52
+ I2["import _ from<br/>'@5minds/processcube_app_sdk/server'"]
53
+ I3["import _ from<br/>'@5minds/processcube_app_sdk/client'"]
54
+ I4["import<br/>'…/client/components/*.css'"]
55
+ end
56
+
57
+ I1 --> Common["common/index.mjs<br/><small>.cjs</small>"]
58
+ I2 --> Server["server/index.mjs<br/><small>.cjs</small>"]
59
+ I3 --> Client["client/index.mjs<br/><small>.cjs</small>"]
60
+ I4 --> CSS["components/*.css"]
61
+ ```
62
+
63
+ | Import-Pfad | Inhalt | Umgebung |
64
+ | ------------------------------------ | ------------------------------------------------------------------------- | --------------- |
65
+ | `@5minds/processcube_app_sdk` | Gemeinsame Typen, `RemoteUserTask`, `hasClaim`, Auth-Callbacks | Client + Server |
66
+ | `@5minds/processcube_app_sdk/server` | Engine-Funktionen, Server Actions, Auth, Authority Client, External Tasks | Nur Server |
67
+ | `@5minds/processcube_app_sdk/client` | React-Komponenten (BPMNViewer, DynamicUi, ProcessInstanceInspector, …) | Nur Client |
68
+ | `…/client/components/*.css` | Komponent-spezifische Stylesheets | CSS-Import |
69
+
70
+ ## Installation
6
71
 
7
72
  ### Voraussetzungen
8
73
 
9
- - NodeJS `>= v24`
74
+ - Node.js >= 24
75
+ - Next.js >= 15
76
+ - React >= 19
10
77
 
11
- ```shell
12
- npm i @5minds/processcube_app_sdk
78
+ ### Paket installieren
79
+
80
+ ```bash
81
+ npm install @5minds/processcube_app_sdk
13
82
  ```
14
83
 
15
- ## Benutzung
84
+ ### Next.js konfigurieren
16
85
 
17
- Das NPM Paket hat _drei_ Exports.
86
+ Die einfachste Konfiguration mit dem SDK-Plugin:
18
87
 
19
- ### Default/Common
88
+ ```typescript
89
+ // next.config.ts
90
+ import { withApplicationSdk } from '@5minds/processcube_app_sdk/server';
20
91
 
21
- Hier werden Komponenten und Funktionen exportiert, die im Client und Server genutzt werden können.
92
+ export default withApplicationSdk({
93
+ applicationSdk: {
94
+ useExternalTasks: true,
95
+ },
96
+ });
97
+ ```
22
98
 
23
- Zum Beispiel die React Komponente RemoteUserTask:
99
+ Ohne External Tasks reicht:
24
100
 
25
- ```javascript
26
- import { RemoteUserTask } from '@5minds/processcube_app_sdk';
101
+ ```typescript
102
+ // next.config.ts
103
+ import type { NextConfig } from 'next';
104
+
105
+ const nextConfig: NextConfig = {
106
+ serverExternalPackages: ['esbuild'],
107
+ };
108
+
109
+ export default nextConfig;
110
+ ```
111
+
112
+ ### CSS einbinden
113
+
114
+ Client-Komponenten bringen eigene Stylesheets mit, die im Layout oder auf der Seite importiert werden müssen:
115
+
116
+ ```typescript
117
+ // Beispiel: ProcessInstanceInspector CSS
118
+ import '@5minds/processcube_app_sdk/client/components/ProcessInstanceInspector/ProcessInstanceInspector.css';
119
+ import '@5minds/processcube_app_sdk/client/components/BPMNViewer.css';
120
+ ```
121
+
122
+ ### Umgebungsvariablen
123
+
124
+ Erstelle eine `.env.local` im Projekt-Root:
125
+
126
+ ```bash
127
+ # Pflicht
128
+ PROCESSCUBE_ENGINE_URL=http://localhost:10560
129
+
130
+ # Für Authentifizierung (optional)
131
+ PROCESSCUBE_AUTHORITY_URL=http://localhost:11560
132
+ NEXTAUTH_CLIENT_ID=my_client_id
133
+ NEXTAUTH_SECRET=my_secret
134
+ ```
135
+
136
+ Die vollständige Liste aller Umgebungsvariablen findest du im Abschnitt [Konfiguration](#konfiguration).
137
+
138
+ ## Authentifizierung
139
+
140
+ Das SDK bietet zwei Authentifizierungs-Strategien für unterschiedliche Anwendungsfälle:
141
+
142
+ ```mermaid
143
+ flowchart TD
144
+ Start["Authentifizierung benötigt"] --> Q1{"Wer handelt?"}
145
+
146
+ Q1 -->|"Eingeloggter Benutzer<br/>(Browser-Session)"| UserPath["User-Identity<br/><code>getIdentity()</code>"]
147
+ Q1 -->|"Server/Service<br/>(kein Benutzerkontext)"| ServerPath["Server-Identity<br/><code>getServerIdentity()</code>"]
148
+
149
+ UserPath --> U1["NextAuth JWT aus Cookie"]
150
+ U1 --> U2["Automatischer Token-Refresh"]
151
+ U2 --> U3["Identity: { token, userId }"]
152
+
153
+ ServerPath --> S1["Client Credentials Grant"]
154
+ S1 --> S2["Token-Cache mit Auto-Refresh"]
155
+ S2 --> S3["Identity: { token, userId }"]
156
+
157
+ U3 --> UseCase1["Server Components<br/>Server Actions<br/>API Routes"]
158
+ S3 --> UseCase2["External Tasks<br/>Cron-Jobs<br/>Authority Admin API"]
159
+
160
+ style UserPath fill:#e0f0ff
161
+ style ServerPath fill:#fff0e0
162
+ ```
163
+
164
+ ### User-Identity (NextAuth-basiert)
165
+
166
+ Für Server Components und Server Actions, die im Kontext eines eingeloggten Benutzers laufen:
167
+
168
+ #### getIdentity()
169
+
170
+ Gibt die Identity des aktuell eingeloggten Benutzers zurück. Nutzt das NextAuth-JWT aus dem Session-Cookie.
171
+
172
+ ```typescript
173
+ import { getIdentity } from '@5minds/processcube_app_sdk/server';
174
+
175
+ export default async function MyPage() {
176
+ const identity = await getIdentity();
177
+ // identity.token — Access Token
178
+ // identity.userId — User ID (sub Claim)
179
+ }
180
+ ```
181
+
182
+ Der Token wird automatisch erneuert, wenn er abgelaufen ist. Voraussetzung: `PROCESSCUBE_AUTHORITY_URL`, `NEXTAUTH_CLIENT_ID` und `NEXTAUTH_SECRET` sind gesetzt.
183
+
184
+ #### hasClaim()
185
+
186
+ Prüft, ob der aktuelle Benutzer einen bestimmten Claim besitzt. Funktioniert sowohl in Server Components als auch im Client.
187
+
188
+ ```typescript
189
+ import { hasClaim } from '@5minds/processcube_app_sdk';
190
+
191
+ const isAdmin = await hasClaim('admin');
192
+ ```
193
+
194
+ #### Auth-Callbacks für NextAuth
195
+
196
+ Das SDK exportiert vorkonfigurierte Callbacks für die NextAuth-Konfiguration:
197
+
198
+ ```typescript
199
+ import { authConfigJwtCallback, authConfigSessionCallback } from '@5minds/processcube_app_sdk';
200
+
201
+ export const authOptions = {
202
+ callbacks: {
203
+ jwt: authConfigJwtCallback,
204
+ session: authConfigSessionCallback,
205
+ },
206
+ // ... weitere NextAuth-Konfiguration
207
+ };
208
+ ```
209
+
210
+ | Callback | Aufgabe |
211
+ | --------------------------- | ------------------------------------------------------------------------------------------------------- |
212
+ | `authConfigJwtCallback` | Überträgt Access Token, ID Token und Refresh Token in das JWT. Erneuert abgelaufene Tokens automatisch. |
213
+ | `authConfigSessionCallback` | Extrahiert Claims aus dem Access Token und stellt sie in `session.user.claims` bereit. |
214
+
215
+ ### Server-Identity (Client Credentials)
216
+
217
+ Für Server-to-Server-Kommunikation ohne Benutzerkontext — typischerweise in External Tasks, Cron-Jobs oder Authority-Admin-Operationen:
218
+
219
+ #### getServerAccessToken()
220
+
221
+ Holt einen Access Token über den OAuth2 Client Credentials Grant. Der Token wird gecacht und automatisch vor Ablauf erneuert.
222
+
223
+ ```typescript
224
+ import { getServerAccessToken } from '@5minds/processcube_app_sdk/server';
225
+
226
+ const token = await getServerAccessToken();
227
+ ```
228
+
229
+ ```mermaid
230
+ sequenceDiagram
231
+ participant App as Anwendung
232
+ participant Cache as Token-Cache
233
+ participant Auth as Authority
234
+
235
+ App->>Cache: getServerAccessToken()
236
+ alt Token im Cache und gültig
237
+ Cache-->>App: Gecachter Token
238
+ else Kein Token oder abgelaufen
239
+ Cache->>Auth: POST /token<br/>(client_credentials)
240
+ alt Erfolg
241
+ Auth-->>Cache: { access_token, expires_in }
242
+ Note over Cache: Token speichern<br/>(Ablauf × 0.85 Buffer)
243
+ Cache-->>App: Neuer Token
244
+ else Fehler (401/403 — Client gesperrt)
245
+ Auth-->>Cache: HTTP Error
246
+ Cache-->>App: Error wird geworfen
247
+ end
248
+ end
249
+ ```
250
+
251
+ **Optionen:**
252
+
253
+ ```typescript
254
+ interface ServerAccessTokenOptions {
255
+ clientId?: string; // Default: PROCESSCUBE_SERVER_CLIENT_ID
256
+ clientSecret?: string; // Default: PROCESSCUBE_SERVER_CLIENT_SECRET
257
+ scopes?: string; // Default: PROCESSCUBE_SERVER_SCOPES oder 'upe_admin engine_read engine_write'
258
+ authorityUrl?: string; // Default: PROCESSCUBE_AUTHORITY_URL
259
+ skipCache?: boolean; // Cache umgehen und frischen Token holen
260
+ }
261
+ ```
262
+
263
+ #### getServerIdentity()
264
+
265
+ Wrapper um `getServerAccessToken()`, der direkt ein `Identity`-Objekt zurückgibt:
266
+
267
+ ```typescript
268
+ import { getServerIdentity } from '@5minds/processcube_app_sdk/server';
269
+
270
+ const identity = await getServerIdentity();
271
+ // identity.token — Access Token
272
+ // identity.userId — Service-Account User ID
273
+ ```
274
+
275
+ ### Authority Client
276
+
277
+ Der `AuthorityClient` bietet authentifizierten Zugriff auf die ProcessCube Authority API — in zwei Stufen:
278
+
279
+ ```mermaid
280
+ classDiagram
281
+ class AuthorityClient {
282
+ -authorityUrl: string
283
+ -tokenOptions: ServerAccessTokenOptions
284
+ +constructor(options?: AuthorityClientOptions)
285
+ +request(method, path, body?) AuthorityResponse~T~
286
+ +updateClaim(username, claimName, claimValue) AuthorityResponse
287
+ +addScope(username, scopeName) AuthorityResponse
288
+ +addGroup(username, groupName) AuthorityResponse
289
+ +deleteUser(username, options?) AuthorityResponse
290
+ }
291
+
292
+ class AuthorityResponse~T~ {
293
+ +ok: boolean
294
+ +status: number
295
+ +data: T
296
+ }
297
+
298
+ AuthorityClient --> AuthorityResponse : gibt zurück
299
+ ```
300
+
301
+ #### Stufe 1 — Allgemein: request()
302
+
303
+ Für beliebige Authority-API-Aufrufe. Der Token wird automatisch beschafft und als Cookie gesendet:
304
+
305
+ ```typescript
306
+ import { AuthorityClient } from '@5minds/processcube_app_sdk/server';
307
+
308
+ const authority = new AuthorityClient();
309
+
310
+ // Beliebiger API-Aufruf
311
+ const response = await authority.request('GET', '/acr/username_password/admin/users');
312
+ if (response.ok) {
313
+ console.log(response.data);
314
+ }
315
+ ```
316
+
317
+ #### Stufe 2 — Convenience-Methoden
318
+
319
+ Vordefinierte Methoden für häufige User-Admin-Operationen:
320
+
321
+ ```typescript
322
+ import { AuthorityClient } from '@5minds/processcube_app_sdk/server';
323
+
324
+ const authority = new AuthorityClient();
325
+
326
+ // Claim setzen
327
+ await authority.updateClaim('user@example.com', 'email_verified', true);
328
+
329
+ // Scope hinzufügen
330
+ await authority.addScope('user@example.com', 'premium_content');
331
+
332
+ // Gruppe zuweisen
333
+ await authority.addGroup('user@example.com', 'Partner-Netzwerk');
334
+
335
+ // User soft-deleten
336
+ const result = await authority.deleteUser('user@example.com', { fullDelete: false });
337
+ ```
338
+
339
+ | Methode | HTTP | Beschreibung |
340
+ | ---------------------------------------------- | ------ | -------------------------------------- |
341
+ | `updateClaim(username, claimName, claimValue)` | PATCH | Setzt einen Claim auf dem User-Account |
342
+ | `addScope(username, scopeName)` | PATCH | Fügt einen Scope zum User hinzu |
343
+ | `addGroup(username, groupName)` | PATCH | Fügt den User einer Gruppe hinzu |
344
+ | `deleteUser(username, { fullDelete? })` | DELETE | Löscht oder soft-deleted einen User |
345
+
346
+ Alle Methoden geben ein `AuthorityResponse<T>` zurück mit `ok`, `status` und `data`.
347
+
348
+ #### Beispiel: External Task mit Authority Client
349
+
350
+ Vorher (ohne SDK-Helper):
351
+
352
+ ```typescript
353
+ export default async function (payload: any) {
354
+ const response = await fetch(`${process.env.PROCESSCUBE_AUTHORITY_URL}/token`, {
355
+ method: 'POST',
356
+ headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
357
+ body: new URLSearchParams({
358
+ grant_type: 'client_credentials',
359
+ client_id: process.env.PROCESSCUBE_SERVER_CLIENT_ID,
360
+ client_secret: process.env.PROCESSCUBE_SERVER_CLIENT_SECRET,
361
+ scope: 'upe_admin engine_read engine_write',
362
+ }).toString(),
363
+ });
364
+ const { access_token } = await response.json();
365
+
366
+ const result = await fetch(`${process.env.PROCESSCUBE_AUTHORITY_URL}/acr/username_password/admin/user/${payload.email}/delete`, {
367
+ method: 'DELETE',
368
+ headers: { Cookie: `access_token=${access_token}`, 'Content-Type': 'application/json' },
369
+ body: JSON.stringify({ fullDelete: false }),
370
+ });
371
+ return { completed_successful: result.ok };
372
+ }
373
+ ```
374
+
375
+ Nachher (mit Authority Client):
376
+
377
+ ```typescript
378
+ import { AuthorityClient } from '@5minds/processcube_app_sdk/server';
379
+
380
+ export default async function (payload: any) {
381
+ const authority = new AuthorityClient();
382
+ const result = await authority.deleteUser(payload.email, { fullDelete: false });
383
+ return { completed_successful: result.ok };
384
+ }
385
+ ```
386
+
387
+ ## Konfiguration
388
+
389
+ ### withApplicationSdk Plugin
390
+
391
+ Das SDK-Plugin für Next.js konfiguriert die Anwendung automatisch und startet bei Bedarf External Task Worker:
392
+
393
+ ```typescript
394
+ import { withApplicationSdk } from '@5minds/processcube_app_sdk/server';
395
+
396
+ export default withApplicationSdk({
397
+ applicationSdk: {
398
+ useExternalTasks: true,
399
+ customExternalTasksDirPath: './my-tasks', // Optional
400
+ },
401
+ // Weitere Next.js-Optionen hier
402
+ });
403
+ ```
404
+
405
+ | Option | Typ | Standard | Beschreibung |
406
+ | ---------------------------- | --------- | ------------------------ | ------------------------------------- |
407
+ | `useExternalTasks` | `boolean` | `false` | External Task Worker aktivieren |
408
+ | `customExternalTasksDirPath` | `string` | `./app` oder `./src/app` | Verzeichnis für External Task Handler |
409
+
410
+ Das Plugin fügt automatisch `esbuild` zu `serverComponentsExternalPackages` hinzu und startet Worker nur im Server-Modus (nicht während `next build`).
411
+
412
+ ### Umgebungsvariablen
413
+
414
+ #### Allgemein
415
+
416
+ | Variable | Pflicht | Standard | Beschreibung |
417
+ | --------------------------- | ------- | ------------------------ | -------------------------------------------------------------------------- |
418
+ | `PROCESSCUBE_ENGINE_URL` | Nein | `http://localhost:10560` | URL der ProcessCube Engine |
419
+ | `PROCESSCUBE_AUTHORITY_URL` | Nein | — | URL der ProcessCube Authority. Aktiviert Token-basierte Authentifizierung. |
420
+
421
+ #### User-Identity (NextAuth)
422
+
423
+ | Variable | Pflicht | Standard | Beschreibung |
424
+ | -------------------- | -------------- | ----------- | -------------------------------------------- |
425
+ | `NEXTAUTH_CLIENT_ID` | Wenn Authority | — | OAuth2 Client ID für den Benutzer-Login |
426
+ | `NEXTAUTH_SECRET` | Wenn Authority | — | Secret für NextAuth JWT-Verschlüsselung |
427
+ | `NEXTAUTH_URL` | Nein | Auto-Detect | URL der NextAuth-Instanz (für Token-Refresh) |
428
+
429
+ #### Server-Identity (Client Credentials)
430
+
431
+ | Variable | Pflicht | Standard | Beschreibung |
432
+ | ---------------------------------- | -------------------- | ------------------------------------ | ------------------------------------------ |
433
+ | `PROCESSCUBE_SERVER_CLIENT_ID` | Wenn Server-Identity | — | OAuth2 Client ID für Server-to-Server Auth |
434
+ | `PROCESSCUBE_SERVER_CLIENT_SECRET` | Wenn Server-Identity | — | OAuth2 Client Secret |
435
+ | `PROCESSCUBE_SERVER_SCOPES` | Nein | `upe_admin engine_read engine_write` | Scopes für den Server-Token |
436
+
437
+ #### External Task Worker
438
+
439
+ | Variable | Pflicht | Standard | Beschreibung |
440
+ | ------------------------------------------------ | -------------- | -------- | ---------------------------------------------- |
441
+ | `PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_ID` | Wenn Authority | — | Client ID für External Task Worker Auth |
442
+ | `PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_SECRET` | Wenn Authority | — | Client Secret für External Task Worker Auth |
443
+ | `PROCESSCUBE_APP_SDK_ETW_RETRY` | Nein | `6` | Max. Reconnect-Versuche bei Verbindungsfehlern |
444
+
445
+ ## Server-Funktionen
446
+
447
+ ### Übersicht
448
+
449
+ Alle Server-Funktionen werden aus `@5minds/processcube_app_sdk/server` importiert und können in Server Components, Server Actions und API Routes verwendet werden.
450
+
451
+ | Kategorie | Funktionen |
452
+ | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
453
+ | **Authentifizierung** | `getIdentity`, `getServerAccessToken`, `getServerIdentity` |
454
+ | **Authority** | `AuthorityClient` |
455
+ | **Process-Instanzen** | `getProcessInstanceById`, `getActiveProcessInstances`, `retryProcessInstance`, `terminateProcessInstance`, `waitForProcessEnd`, `getFlowNodeInstancesByProcessInstanceId`, `getFlowNodeInstancesTriggeredByFlowNodeInstanceIds` |
456
+ | **User Tasks** | `getUserTasks`, `getWaitingUserTasks`, `getWaitingUserTasksByProcessInstanceId`, `getWaitingUserTasksByFlowNodeId`, `getWaitingUserTaskByFlowNodeInstanceId`, `getWaitingUserTasksByCorrelationId`, `getReservedUserTasksByIdentity`, `getAssignedUserTasksByIdentity`, `waitForUserTask`, `finishUserTaskAndGetNext`, `reserveUserTask`, `cancelReservedUserTask` |
457
+ | **Server Actions** | `startProcess`, `finishUserTask`, `finishManualTask`, `finishUntypedTask`, `finishTask`, `navigateToUrl` |
458
+ | **Engine** | `getEngineClient` |
459
+ | **Plugin** | `withApplicationSdk` |
460
+ | **External Tasks** | `ExternalTaskConfig` (Type) |
461
+
462
+ ### Process-Instanzen
463
+
464
+ #### getProcessInstanceById
465
+
466
+ Gibt die Prozessinstanz mit der angegebenen ID zurück (inkl. BPMN-XML).
467
+
468
+ ```typescript
469
+ import { getProcessInstanceById } from '@5minds/processcube_app_sdk/server';
470
+
471
+ const instance = await getProcessInstanceById('process-instance-id');
472
+ ```
473
+
474
+ #### getActiveProcessInstances
475
+
476
+ Gibt alle laufenden Prozessinstanzen zurück. Unterstützt Filterung und Paginierung.
477
+
478
+ ```typescript
479
+ import { getActiveProcessInstances } from '@5minds/processcube_app_sdk/server';
480
+
481
+ // Alle laufenden Instanzen
482
+ const result = await getActiveProcessInstances();
483
+
484
+ // Mit Filter
485
+ const filtered = await getActiveProcessInstances({
486
+ query: { processModelId: 'OrderProcess' },
487
+ options: { limit: 10, offset: 0 },
488
+ });
27
489
  ```
28
490
 
29
- ### Server
491
+ #### retryProcessInstance
492
+
493
+ Startet eine Prozessinstanz von einem bestimmten FlowNode neu.
494
+
495
+ ```typescript
496
+ import { retryProcessInstance } from '@5minds/processcube_app_sdk/server';
30
497
 
31
- Hier steht alles ausschließlich für eine serverseitige Umgebung zur Verfügung. Dazu zählen Funktionen, die mit der Engine arbeiten, oder React Komponenten, die Serverseitig gerendert werden können.
498
+ // Gesamten Prozess neu starten
499
+ await retryProcessInstance('process-instance-id');
32
500
 
33
- Beispiel:
501
+ // Ab einem bestimmten FlowNode
502
+ await retryProcessInstance('process-instance-id', 'flow-node-instance-id');
34
503
 
35
- ```javascript
36
- import { startProcess } from '@5minds/processcube_app_sdk/server';
504
+ // Mit neuem Start-Token
505
+ await retryProcessInstance('process-instance-id', 'flow-node-instance-id', { orderId: 'new-123' });
37
506
  ```
38
507
 
39
- Um die Engine URL anzupassen, die von den exportierten Funktionen genutzt wird, muss `PROCESSCUBE_ENGINE_URL` als Umgebungsvariable gesetzt werden. Andernfalls wird localhost mit dem Standardport der Engine genutzt `10560`.
508
+ #### terminateProcessInstance
40
509
 
41
- ### Client
510
+ Beendet eine Prozessinstanz sofort.
42
511
 
43
- Es können nur Komponenten und Funktionen importiert werden, die im Browser funktionieren. Zum Beispiel React Komponenten, die einen clientseitigen Router und dessen React Hooks nutzen oder Funktionen, die auf `window` oder generell globale Browser APIs zugreifen möchten.
512
+ ```typescript
513
+ import { terminateProcessInstance } from '@5minds/processcube_app_sdk/server';
44
514
 
45
- ```javascript
46
- import { DynamicLink } from '@5minds/processcube_app_sdk/client';
515
+ await terminateProcessInstance('process-instance-id');
47
516
  ```
48
517
 
49
- ### External Tasks
518
+ #### waitForProcessEnd
519
+
520
+ Wartet, bis eine Prozessinstanz beendet, terminiert oder fehlerhaft ist. Nutzt Engine-Notifications (kein Polling).
521
+
522
+ ```typescript
523
+ import { waitForProcessEnd } from '@5minds/processcube_app_sdk/server';
524
+
525
+ const finishedInstance = await waitForProcessEnd({
526
+ processInstanceId: 'process-instance-id',
527
+ });
528
+ ```
529
+
530
+ #### getFlowNodeInstancesByProcessInstanceId
531
+
532
+ Gibt alle FlowNode-Instanzen einer Prozessinstanz zurück (sortiert nach Erstellungsdatum).
533
+
534
+ ```typescript
535
+ import { getFlowNodeInstancesByProcessInstanceId } from '@5minds/processcube_app_sdk/server';
536
+
537
+ const flowNodes = await getFlowNodeInstancesByProcessInstanceId('process-instance-id');
538
+ ```
539
+
540
+ #### getFlowNodeInstancesTriggeredByFlowNodeInstanceIds
541
+
542
+ Gibt FlowNode-Instanzen zurück, die von den angegebenen FlowNode-Instanzen ausgelöst wurden.
543
+
544
+ ```typescript
545
+ import { getFlowNodeInstancesTriggeredByFlowNodeInstanceIds } from '@5minds/processcube_app_sdk/server';
546
+
547
+ const triggered = await getFlowNodeInstancesTriggeredByFlowNodeInstanceIds(['fni-1', 'fni-2']);
548
+ ```
549
+
550
+ ### User Tasks
551
+
552
+ #### getWaitingUserTasks
553
+
554
+ Gibt alle wartenden User Tasks zurück.
555
+
556
+ ```typescript
557
+ import { getWaitingUserTasks } from '@5minds/processcube_app_sdk/server';
558
+
559
+ const { userTasks, totalCount } = await getWaitingUserTasks();
560
+ ```
561
+
562
+ #### getWaitingUserTasksByProcessInstanceId
563
+
564
+ Filtert wartende User Tasks nach Prozessinstanz.
565
+
566
+ ```typescript
567
+ import { getWaitingUserTasksByProcessInstanceId } from '@5minds/processcube_app_sdk/server';
568
+
569
+ const { userTasks } = await getWaitingUserTasksByProcessInstanceId('process-instance-id');
570
+
571
+ // Auch mit Array von IDs
572
+ const { userTasks: multiple } = await getWaitingUserTasksByProcessInstanceId(['id-1', 'id-2']);
573
+ ```
574
+
575
+ #### getWaitingUserTaskByFlowNodeInstanceId
576
+
577
+ Gibt eine einzelne wartende User Task nach FlowNode-Instanz-ID zurück (oder `null`).
578
+
579
+ ```typescript
580
+ import { getWaitingUserTaskByFlowNodeInstanceId } from '@5minds/processcube_app_sdk/server';
581
+
582
+ const userTask = await getWaitingUserTaskByFlowNodeInstanceId('flow-node-instance-id');
583
+ ```
584
+
585
+ #### waitForUserTask
586
+
587
+ Wartet auf die nächste wartende User Task. Falls bereits eine existiert, wird sie sofort zurückgegeben.
588
+
589
+ ```typescript
590
+ import { waitForUserTask } from '@5minds/processcube_app_sdk/server';
591
+
592
+ // Auf beliebige User Task warten
593
+ const userTask = await waitForUserTask();
594
+
595
+ // Mit Filter
596
+ const filtered = await waitForUserTask({
597
+ processInstanceId: 'process-instance-id',
598
+ flowNodeId: 'UserTask_Approve',
599
+ });
600
+ ```
601
+
602
+ #### finishUserTaskAndGetNext
603
+
604
+ Schließt eine User Task ab und gibt die nächste wartende User Task zurück (falls vorhanden).
605
+
606
+ ```typescript
607
+ import { finishUserTaskAndGetNext } from '@5minds/processcube_app_sdk/server';
608
+
609
+ const nextTask = await finishUserTaskAndGetNext('flow-node-instance-id', { processInstanceId: 'process-instance-id' }, { approved: true });
610
+ ```
611
+
612
+ #### reserveUserTask / cancelReservedUserTask
613
+
614
+ Reserviert eine User Task für einen bestimmten Benutzer oder hebt die Reservierung auf.
615
+
616
+ ```typescript
617
+ import { reserveUserTask, cancelReservedUserTask, getIdentity } from '@5minds/processcube_app_sdk/server';
618
+
619
+ const identity = await getIdentity();
620
+ await reserveUserTask(identity, 'flow-node-instance-id');
621
+
622
+ // Reservierung aufheben
623
+ await cancelReservedUserTask(identity, 'flow-node-instance-id');
624
+ ```
625
+
626
+ #### Weitere User-Task-Funktionen
627
+
628
+ | Funktion | Beschreibung |
629
+ | ------------------------------------------------------------- | ------------------------------------------ |
630
+ | `getUserTasks(query, options?)` | Generische Abfrage mit vollem Query-Objekt |
631
+ | `getWaitingUserTasksByFlowNodeId(flowNodeId, options?)` | Filter nach BPMN-FlowNode-ID |
632
+ | `getWaitingUserTasksByCorrelationId(correlationId, options?)` | Filter nach Correlation-ID |
633
+ | `getReservedUserTasksByIdentity(identity?, options?)` | Vom aktuellen User reservierte Tasks |
634
+ | `getAssignedUserTasksByIdentity(identity?, options?)` | Dem aktuellen User zugewiesene Tasks |
635
+
636
+ Alle Funktionen unterstützen einen `identity`-Parameter: `true` (Default) nutzt die implizite User-Identity, `false` deaktiviert Auth, oder ein explizites `Identity`-Objekt.
637
+
638
+ ### Server Actions
639
+
640
+ Server Actions sind für den Aufruf aus Client Components oder Formularen gedacht:
641
+
642
+ ```typescript
643
+ import { startProcess, finishUserTask, navigateToUrl } from '@5minds/processcube_app_sdk/server';
644
+ ```
645
+
646
+ | Action | Beschreibung |
647
+ | ------------------------------------------------------- | --------------------------------------------------------- |
648
+ | `startProcess(...)` | Startet eine Prozessinstanz (Argumente wie Engine-Client) |
649
+ | `finishUserTask(flowNodeInstanceId, result, identity?)` | Schließt eine User Task ab |
650
+ | `finishManualTask(flowNodeInstanceId, identity?)` | Schließt eine Manual Task ab |
651
+ | `finishUntypedTask(flowNodeInstanceId, identity?)` | Schließt eine generische bpmn:Task ab |
652
+ | `finishTask(flowNodeInstanceId, flowNodeType)` | Universelle Finish-Funktion (erkennt Task-Typ) |
653
+ | `navigateToUrl(url)` | Server-seitige Weiterleitung via Next.js `redirect()` |
654
+
655
+ **Beispiel: Server Action in einem Formular**
656
+
657
+ ```typescript
658
+ // app/tasks/page.tsx
659
+ import { finishUserTask, getWaitingUserTasks } from '@5minds/processcube_app_sdk/server';
660
+ import { revalidatePath } from 'next/cache';
661
+
662
+ export default async function TasksPage() {
663
+ const { userTasks } = await getWaitingUserTasks();
664
+
665
+ async function handleFinish(flowNodeInstanceId: string) {
666
+ 'use server';
667
+ await finishUserTask(flowNodeInstanceId, { approved: true });
668
+ revalidatePath('/tasks');
669
+ }
670
+
671
+ return (
672
+ <ul>
673
+ {userTasks.map((task) => (
674
+ <li key={task.flowNodeInstanceId}>
675
+ {task.flowNodeName}
676
+ <form action={handleFinish.bind(null, task.flowNodeInstanceId)}>
677
+ <button type="submit">Abschließen</button>
678
+ </form>
679
+ </li>
680
+ ))}
681
+ </ul>
682
+ );
683
+ }
684
+ ```
685
+
686
+ ### Engine Client
687
+
688
+ Für direkte Zugriffe auf die Engine-API, die nicht durch die SDK-Funktionen abgedeckt sind:
689
+
690
+ ```typescript
691
+ import { getEngineClient } from '@5minds/processcube_app_sdk/server';
692
+
693
+ const client = getEngineClient();
694
+ // Zugriff auf alle Engine-Client-APIs
695
+ const models = await client.processModels.getProcessModels();
696
+ ```
697
+
698
+ Der Client ist ein Singleton und nutzt die `PROCESSCUBE_ENGINE_URL`.
699
+
700
+ ## Client-Komponenten
701
+
702
+ Alle Client-Komponenten werden aus `@5minds/processcube_app_sdk/client` importiert und müssen in Dateien mit `'use client'` verwendet werden.
703
+
704
+ ### Übersicht
705
+
706
+ | Komponente | Beschreibung | CSS erforderlich |
707
+ | ---------------------------- | ------------------------------------------------------- | ---------------- |
708
+ | **BPMNViewer** | BPMN-Diagramm-Rendering mit Overlays und Markern | Ja |
709
+ | **ProcessInstanceInspector** | Interaktive Prozessinstanz-Ansicht mit Token-Inspektion | Ja |
710
+ | **DynamicUi** | Dynamischer Formular-Builder aus UserTask-Schemas | Ja |
711
+ | **ProcessModelInspector** | Prozessmodell mit Heatmap-Visualisierung | Ja |
712
+ | **DocumentationViewer** | Markdown-Dokumentation mit Syntax-Highlighting | Ja |
713
+ | **SplitterLayout** | Größenveränderbares Panel-Layout | Ja |
714
+ | **DropdownMenu** | Dropdown-Menü (Headless UI) | Ja |
715
+ | **RemoteUserTask** | iFrame-basierte Remote User Task (Common) | Nein |
716
+
717
+ ### BPMNViewer
718
+
719
+ Rendert BPMN-Diagramme und bietet eine Ref-API für Overlays, Marker und Heatmaps.
720
+
721
+ ```typescript
722
+ 'use client';
723
+
724
+ import { BPMNViewerNextJS, BPMNViewerFunctions } from '@5minds/processcube_app_sdk/client';
725
+ import '@5minds/processcube_app_sdk/client/components/BPMNViewer.css';
726
+ import { useRef } from 'react';
727
+
728
+ export default function DiagramPage({ xml }: { xml: string }) {
729
+ const viewerRef = useRef<BPMNViewerFunctions>(null);
730
+
731
+ return (
732
+ <BPMNViewerNextJS
733
+ xml={xml}
734
+ viewerRef={viewerRef}
735
+ onSelectionChanged={(elements) => console.log('Ausgewählt:', elements)}
736
+ onImportDone={() => console.log('Diagramm geladen')}
737
+ />
738
+ );
739
+ }
740
+ ```
741
+
742
+ **Props:**
743
+
744
+ | Prop | Typ | Pflicht | Beschreibung |
745
+ | ----------------------- | ----------------------------------- | ------- | ---------------------------------- |
746
+ | `xml` | `string` | Ja | BPMN-XML des Diagramms |
747
+ | `className` | `string` | Nein | CSS-Klasse für den Container |
748
+ | `preselectedElementIds` | `string[]` | Nein | Vorausgewählte BPMN-Elemente |
749
+ | `onSelectionChanged` | `(elements) => void` | Nein | Callback bei Auswahländerung |
750
+ | `onImportDone` | `() => void` | Nein | Callback nach erfolgreichem Import |
751
+ | `viewerRef` | `ForwardedRef<BPMNViewerFunctions>` | Nein | Nur bei `BPMNViewerNextJS` |
752
+
753
+ **Ref-API (BPMNViewerFunctions):**
754
+
755
+ | Methode | Beschreibung |
756
+ | ------------------------------------ | ----------------------------------------------- |
757
+ | `getOverlays()` | Gibt das Overlay-Modul zurück |
758
+ | `getElementRegistry()` | Gibt die Element-Registry zurück |
759
+ | `addMarker(elementId, className)` | Fügt einen CSS-Marker zu einem Element hinzu |
760
+ | `removeMarker(elementId, className)` | Entfernt einen CSS-Marker |
761
+ | `hasMarker(elementId, className)` | Prüft ob ein Marker gesetzt ist |
762
+ | `showHeatmap(data)` | Zeigt Heatmap-Daten an (`{ elementId: color }`) |
763
+ | `clearHeatmap(data)` | Entfernt Heatmap-Daten |
764
+
765
+ ### ProcessInstanceInspector
766
+
767
+ Interaktive Ansicht einer Prozessinstanz mit BPMN-Diagramm, Token-Inspektion, Retry und Kommandopalette.
768
+
769
+ ```typescript
770
+ 'use client';
771
+
772
+ import { ProcessInstanceInspectorNextJS } from '@5minds/processcube_app_sdk/client';
773
+ import '@5minds/processcube_app_sdk/client/components/ProcessInstanceInspector/ProcessInstanceInspector.css';
774
+ import '@5minds/processcube_app_sdk/client/components/BPMNViewer.css';
775
+
776
+ export default function InspectorPage({ processInstanceId }: { processInstanceId: string }) {
777
+ return (
778
+ <ProcessInstanceInspectorNextJS
779
+ processInstanceId={processInstanceId}
780
+ showFinishTaskButton
781
+ showProcessRetryButton
782
+ showProcessTerminateButton
783
+ showTokenInspectorButton
784
+ onFinish={async ({ flowNodeInstanceId, taskType }) => {
785
+ console.log(`Task ${flowNodeInstanceId} (${taskType}) abgeschlossen`);
786
+ }}
787
+ />
788
+ );
789
+ }
790
+ ```
791
+
792
+ **Props:**
793
+
794
+ | Prop | Typ | Pflicht | Beschreibung |
795
+ | --------------------------------- | ----------------------- | ------- | ------------------------------------- |
796
+ | `processInstanceId` | `string` | Ja | ID der Prozessinstanz |
797
+ | `showFinishTaskButton` | `boolean` | Nein | Play-Button zum Abschließen von Tasks |
798
+ | `showFlowNodeExecutionCount` | `boolean` | Nein | Ausführungszähler auf FlowNodes |
799
+ | `showFlowNodeInstancesListButton` | `boolean` | Nein | Liste der FlowNode-Instanzen |
800
+ | `showGoToFlowNodeButton` | `boolean` | Nein | Navigation zu FlowNodes |
801
+ | `showRetryFlowNodeInstanceButton` | `boolean` | Nein | Retry-Button auf FlowNode-Ebene |
802
+ | `showProcessRefreshButton` | `boolean` | Nein | Daten-Refresh-Button |
803
+ | `showProcessRetryButton` | `boolean` | Nein | Prozess-Retry-Button |
804
+ | `showProcessTerminateButton` | `boolean` | Nein | Prozess-Terminate-Button |
805
+ | `showTokenInspectorButton` | `boolean` | Nein | Token-Inspektor öffnen |
806
+ | `loadingComponent` | `ReactNode` | Nein | Custom Loading-Indikator |
807
+ | `onFinish` | `(taskContext) => void` | Nein | Callback nach Task-Abschluss |
808
+
809
+ ### DynamicUi
810
+
811
+ Rendert dynamische Formulare basierend auf UserTask-FormField-Definitionen. Unterstützt 25+ Feldtypen und Custom Fields.
812
+
813
+ ```typescript
814
+ 'use client';
815
+
816
+ import { DynamicUi } from '@5minds/processcube_app_sdk/client';
817
+ import '@5minds/processcube_app_sdk/client/components/DynamicUi/DynamicUi.css';
818
+
819
+ export default function TaskForm({ task }) {
820
+ return (
821
+ <DynamicUi
822
+ task={task}
823
+ onSubmit={async (result, rawFormData, task) => {
824
+ console.log('Ergebnis:', result);
825
+ }}
826
+ title="Aufgabe bearbeiten"
827
+ />
828
+ );
829
+ }
830
+ ```
831
+
832
+ **Props:**
833
+
834
+ | Prop | Typ | Pflicht | Beschreibung |
835
+ | ----------------------- | -------------------------------------------------------------------------------- | ------- | ---------------------------------------- |
836
+ | `task` | `UserTaskInstance` | Ja | Die User Task mit FormField-Definitionen |
837
+ | `onSubmit` | `(result, formData, task) => Promise<void>` | Ja | Submit-Handler |
838
+ | `headerComponent` | `JSX.Element` | Nein | Custom Header über dem Formular |
839
+ | `className` | `string` | Nein | CSS-Klasse für den Root-Container |
840
+ | `classNames` | `Partial<Record<'wrapper' \| 'base' \| 'header' \| 'body' \| 'footer', string>>` | Nein | CSS-Klassen für Unterelemente |
841
+ | `title` | `ReactNode` | Nein | Titel des Formulars |
842
+ | `customFieldComponents` | `DynamicUiFormFieldComponentMap` | Nein | Custom-Renderer für Feldtypen |
843
+ | `state` | `FormState` | Nein | Externer Formular-State |
844
+ | `onStateChange` | `(newValue, formFieldId, formState) => void` | Nein | Callback bei Wertänderung |
845
+ | `darkMode` | `true` | Nein | Dark-Mode aktivieren |
846
+
847
+ ```mermaid
848
+ graph TD
849
+ Task["UserTaskInstance<br/>(mit FormField-Definitionen)"] --> DynUi["DynamicUi"]
850
+ DynUi --> Fields["FormField-Renderer<br/>(25+ Typen)"]
851
+ Custom["customFieldComponents<br/>(optional)"] --> DynUi
852
+ State["state / onStateChange<br/>(optional)"] --> DynUi
853
+ Fields --> Form["HTML-Formular"]
854
+ Form -->|Submit| Handler["onSubmit(result, formData, task)"]
855
+ ```
856
+
857
+ **Unterstützte Feldtypen:** Boolean, Checkbox, Color, Date, Datetime-Local, Decimal, Email, Enum, File, Header, Hidden, Integer, Month, Paragraph, Password, Radio, Range, Select, Confirm, String, Tel, Textarea, Time, URL, Week, Custom
858
+
859
+ ### Weitere Komponenten
860
+
861
+ #### ProcessModelInspector
862
+
863
+ Zeigt ein Prozessmodell mit optionaler Heatmap-Visualisierung.
864
+
865
+ ```typescript
866
+ import { ProcessModelInspectorNextJS } from '@5minds/processcube_app_sdk/client';
867
+ ```
868
+
869
+ | Prop | Typ | Pflicht | Beschreibung |
870
+ | -------------------------- | ---------------------------------------------------- | ------- | -------------------------- |
871
+ | `processModel` | `any` | Nein | Das Prozessmodell-Objekt |
872
+ | `getInstancesFromDatabase` | `(processModelId, hash, options?) => Promise<any[]>` | Nein | Callback für Heatmap-Daten |
873
+
874
+ #### DocumentationViewer
875
+
876
+ Rendert Markdown-Dokumentation.
877
+
878
+ ```typescript
879
+ import { DocumentationViewer } from '@5minds/processcube_app_sdk/client';
880
+ import '@5minds/processcube_app_sdk/client/components/DocumentationViewer.css';
881
+
882
+ <DocumentationViewer documentation="# Titel\n\nBeschreibung..." />
883
+ ```
884
+
885
+ | Prop | Typ | Pflicht | Beschreibung |
886
+ | --------------- | -------- | ------- | --------------- |
887
+ | `documentation` | `string` | Ja | Markdown-String |
888
+
889
+ #### SplitterLayout
890
+
891
+ Größenveränderbares Zwei-Panel-Layout.
892
+
893
+ ```typescript
894
+ import { SplitterLayout } from '@5minds/processcube_app_sdk/client';
895
+ import '@5minds/processcube_app_sdk/client/components/SplitterLayout.css';
896
+
897
+ <SplitterLayout vertical={false} primaryIndex={0} secondaryMinSize={200}>
898
+ <div>Linkes Panel</div>
899
+ <div>Rechtes Panel</div>
900
+ </SplitterLayout>
901
+ ```
902
+
903
+ | Prop | Typ | Standard | Beschreibung |
904
+ | ---------------------- | ------------------------- | -------- | ---------------------------------- |
905
+ | `vertical` | `boolean` | `false` | Vertikale Teilung |
906
+ | `percentage` | `boolean` | `false` | Größen in Prozent |
907
+ | `primaryIndex` | `number` | `0` | Index des primären Panels |
908
+ | `primaryMinSize` | `number` | `0` | Minimalgröße des primären Panels |
909
+ | `secondaryMinSize` | `number` | `0` | Minimalgröße des sekundären Panels |
910
+ | `secondaryDefaultSize` | `number \| null` | `null` | Standard-Größe |
911
+ | `customClassName` | `string` | `''` | CSS-Klasse |
912
+ | `onSizeChanged` | `(size) => void` | `null` | Callback bei Größenänderung |
913
+ | `onDragStart` | `(prev) => void` | `null` | Callback bei Drag-Start |
914
+ | `onDragEnd` | `(prev, current) => void` | `null` | Callback bei Drag-Ende |
915
+
916
+ #### DropdownMenu
917
+
918
+ Dropdown-Menü basierend auf Headless UI.
919
+
920
+ ```typescript
921
+ import { DropdownMenu, DropdownMenuItem } from '@5minds/processcube_app_sdk/client';
922
+ import '@5minds/processcube_app_sdk/client/components/DropdownMenu.css';
923
+
924
+ <DropdownMenu>
925
+ <DropdownMenuItem title="Bearbeiten" onClick={() => {}} />
926
+ <DropdownMenuItem title="Löschen" onClick={() => {}} isDanger />
927
+ </DropdownMenu>
928
+ ```
929
+
930
+ #### RemoteUserTask (Common)
931
+
932
+ Zeigt eine User Task als iFrame an. Importiert aus dem Common-Modul.
933
+
934
+ ```typescript
935
+ import { RemoteUserTask } from '@5minds/processcube_app_sdk';
936
+
937
+ <RemoteUserTask url="https://my-task-ui.example.com/task/123" />
938
+ ```
939
+
940
+ ## External Tasks
50
941
 
51
942
  External Tasks ermöglichen es, eigene Geschäftslogik in einer Next.js App auszuführen, die von der ProcessCube Engine als Aufgabe vergeben wird. Erreicht ein BPMN-Prozess einen External Service Task, veröffentlicht die Engine diesen unter einem **Topic**. Ein passender Worker in der App holt sich den Task ab, verarbeitet ihn und gibt das Ergebnis zurück.
52
943
 
53
944
  Das App SDK übernimmt dabei die komplette Infrastruktur: Worker-Prozesse werden automatisch gestartet, überwacht und bei Fehlern neu gestartet. Der Entwickler schreibt nur die eigentliche Handler-Funktion.
54
945
 
55
- #### Architektur
946
+ ### Architektur
56
947
 
57
948
  Das folgende Diagramm zeigt die drei beteiligten Schichten und ihre Kommunikation:
58
949
 
@@ -91,7 +982,7 @@ graph LR
91
982
  | **ExternalTaskWorkerProcess** | Eigenständiger Node.js-Kindprozess (einer pro Topic). Lädt den transpilierten Handler, verbindet sich per HTTP-Long-Polling mit der Engine und verarbeitet Tasks. |
92
983
  | **ProcessCube Engine** | Verwaltet BPMN-Prozesse und vergibt External Tasks an Worker über das Fetch-and-Lock-Protokoll. |
93
984
 
94
- #### Lebenszyklus eines External Tasks
985
+ ### Lebenszyklus eines External Tasks
95
986
 
96
987
  Das folgende Sequenzdiagramm zeigt, wie ein External Task von der Engine zum Worker gelangt, verarbeitet und abgeschlossen wird:
97
988
 
@@ -130,7 +1021,7 @@ sequenceDiagram
130
1021
  4. Während der Verarbeitung verlängert der Worker automatisch den Lock, damit die Engine den Task nicht vorzeitig freigibt.
131
1022
  5. Nach Abschluss meldet der Worker das Ergebnis (Finish) oder einen Fehler (Error) an die Engine.
132
1023
 
133
- #### Worker-Startup und IPC-Kommunikation
1024
+ ### Worker-Startup und IPC-Kommunikation
134
1025
 
135
1026
  Das App SDK startet pro `external_task.ts`-Datei einen eigenen Node.js-Kindprozess. Die Kommunikation zwischen Hauptprozess (Adapter) und Kindprozess (Worker) läuft über IPC (Inter-Process Communication):
136
1027
 
@@ -179,7 +1070,7 @@ sequenceDiagram
179
1070
  | `restart` | Adapter → Worker | Hot-Reload: Stoppt den alten Worker und startet mit neuem Code (gleiche Worker-ID) |
180
1071
  | `updateIdentity` | Adapter → Worker | Aktualisiert den Auth-Token auf dem laufenden Worker |
181
1072
 
182
- #### Fehlerbehandlung und Restart-Strategie
1073
+ ### Fehlerbehandlung und Restart-Strategie
183
1074
 
184
1075
  Das System hat zwei Ebenen der Fehlerbehandlung: im Worker-Prozess selbst und im Adapter (Hauptprozess).
185
1076
 
@@ -229,17 +1120,17 @@ flowchart TD
229
1120
  - Wird das Limit erreicht, bleibt der Worker gestoppt — ein manueller Eingriff (z.B. App-Neustart) ist nötig.
230
1121
  - Nach Ablauf des 5-Minuten-Fensters wird der Zähler zurückgesetzt.
231
1122
 
232
- #### Setup und Konfiguration
1123
+ ### Setup und Konfiguration
233
1124
 
234
- ##### 1. Next.js Plugin aktivieren
1125
+ #### 1. Next.js Plugin aktivieren
235
1126
 
236
- In der `next.config.js` wird das SDK-Plugin eingebunden und External Tasks aktiviert:
1127
+ In der `next.config.ts` wird das SDK-Plugin eingebunden und External Tasks aktiviert:
237
1128
 
238
- ```javascript
239
- // next.config.js
240
- const { withApplicationSdk } = require('@5minds/processcube_app_sdk/server');
1129
+ ```typescript
1130
+ // next.config.ts
1131
+ import { withApplicationSdk } from '@5minds/processcube_app_sdk/server';
241
1132
 
242
- module.exports = withApplicationSdk({
1133
+ export default withApplicationSdk({
243
1134
  applicationSdk: {
244
1135
  useExternalTasks: true,
245
1136
  // Optional: Eigenes Verzeichnis für External Tasks
@@ -250,7 +1141,7 @@ module.exports = withApplicationSdk({
250
1141
 
251
1142
  Das Plugin erkennt automatisch, ob die App im Development- oder Production-Modus läuft, und startet die Worker entsprechend. Während des Build-Prozesses (`next build`) werden keine Worker gestartet.
252
1143
 
253
- ##### 2. Handler-Datei anlegen
1144
+ #### 2. Handler-Datei anlegen
254
1145
 
255
1146
  External Tasks werden durch Dateien mit dem Namen `external_task.ts` (oder `.js`) definiert. Das Verzeichnis, in dem die Datei liegt, bestimmt automatisch das **Topic**, unter dem sich der Worker bei der Engine registriert.
256
1147
 
@@ -271,7 +1162,7 @@ Das SDK sucht Handler-Dateien standardmäßig in `./app` oder `./src/app`. Ein e
271
1162
 
272
1163
  > **Wichtig:** Pro Verzeichnis darf nur eine `external_task.ts` oder `external_task.js` existieren. Beide Dateien im selben Verzeichnis führen zu einem Fehler.
273
1164
 
274
- #### Handler-Signatur
1165
+ ### Handler-Signatur
275
1166
 
276
1167
  Der Handler wird als **Default-Export** der Datei definiert. Er erhält bis zu drei Parameter:
277
1168
 
@@ -290,7 +1181,7 @@ export default async function handleExternalTask(payload: any, task: ExternalTas
290
1181
 
291
1182
  **Rückgabewert:** Das zurückgegebene Objekt wird als Ergebnis an die Engine gemeldet und steht im BPMN-Prozess als Variable zur Verfügung.
292
1183
 
293
- #### Worker-Konfiguration
1184
+ ### Worker-Konfiguration
294
1185
 
295
1186
  Über einen benannten `config`-Export können Worker-Einstellungen pro Handler angepasst werden:
296
1187
 
@@ -308,7 +1199,7 @@ export const config: ExternalTaskConfig = {
308
1199
  | `lockDuration` | `number` | `30000` | Dauer in Millisekunden, für die ein Task gesperrt wird. Bestimmt auch das Intervall der Lock-Verlängerung und die maximale Verzögerung bei Abort-Signalen. |
309
1200
  | `maxTasks` | `number` | `10` | Maximale Anzahl gleichzeitig abgeholter Tasks pro Polling-Zyklus. |
310
1201
 
311
- #### Abort-Handling bei Boundary Events
1202
+ ### Abort-Handling bei Boundary Events
312
1203
 
313
1204
  Wenn ein BPMN Boundary Event (z.B. ein Timer oder Signal) einen External Task abbricht, löst die Engine den Abbruch beim nächsten Lock-Renewal aus. Das `AbortSignal` im Handler wird daraufhin ausgelöst.
314
1205
 
@@ -354,7 +1245,7 @@ export default async function handleExternalTask(payload: any, _task: any, signa
354
1245
  }
355
1246
  ```
356
1247
 
357
- #### Authentifizierung und Token-Management
1248
+ ### ETW-Authentifizierung und Token-Management
358
1249
 
359
1250
  Ist eine ProcessCube Authority konfiguriert, holt der Adapter automatisch Tokens per **OpenID Connect Client Credentials Grant** und verteilt sie an alle Worker.
360
1251
 
@@ -386,17 +1277,7 @@ sequenceDiagram
386
1277
  - Der periodische Token-Refresh versucht es **unbegrenzt** mit Backoff (max. 60s).
387
1278
  - Ist keine Authority konfiguriert, wird eine Dummy-Identity verwendet (für lokale Entwicklung ohne Auth).
388
1279
 
389
- #### Umgebungsvariablen
390
-
391
- | Variable | Pflicht | Standard | Beschreibung |
392
- | ------------------------------------------------ | -------------- | ------------------------ | ---------------------------------------------------------------------------------------- |
393
- | `PROCESSCUBE_ENGINE_URL` | Nein | `http://localhost:10560` | URL der ProcessCube Engine |
394
- | `PROCESSCUBE_AUTHORITY_URL` | Nein | — | URL des OpenID-Providers. Wenn gesetzt, wird Token-basierte Authentifizierung aktiviert. |
395
- | `PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_ID` | Wenn Authority | — | Client-ID für den OpenID Client Credentials Grant |
396
- | `PROCESSCUBE_EXTERNAL_TASK_WORKER_CLIENT_SECRET` | Wenn Authority | — | Client-Secret für den OpenID Client Credentials Grant |
397
- | `PROCESSCUBE_APP_SDK_ETW_RETRY` | Nein | `6` | Maximale Anzahl der Reconnect-Versuche im Worker-Prozess bei Verbindungsfehlern |
398
-
399
- #### Vollständiges Beispiel
1280
+ ### Vollständiges External-Task-Beispiel
400
1281
 
401
1282
  Eine `external_task.ts` mit allen Features — Konfiguration, typisiertem Payload, Fehlerbehandlung und Abort-Support:
402
1283
 
@@ -451,44 +1332,70 @@ export default async function handleExternalTask(payload: OrderPayload, task: an
451
1332
  }
452
1333
  ```
453
1334
 
454
- #### Hot-Reload
1335
+ ### Hot-Reload
455
1336
 
456
1337
  Im Development-Modus überwacht das SDK die Handler-Dateien per File-Watcher. Änderungen an einer `external_task.ts` werden automatisch erkannt: Die Datei wird neu transpiliert und der Worker per IPC-Nachricht (`restart`) mit dem neuen Code neu gestartet — ohne die App neu starten zu müssen. Die Worker-ID bleibt dabei erhalten.
457
1338
 
458
- ## Wie kann ich das Projekt aufsetzen?
1339
+ ## Entwicklung
459
1340
 
460
- ### Setup/Installation
1341
+ ### Setup
461
1342
 
462
- Das SDK wird über den Node Paketmanager `npm` gebaut.
1343
+ Das SDK wird über `npm` gebaut:
463
1344
 
464
- Für das Installieren und Bauen können folgende Befehle benutzt werden:
465
-
466
- ```shell
1345
+ ```bash
467
1346
  npm ci
468
1347
  npm run build
469
1348
  ```
470
1349
 
471
- Für ein Productionbuild:
1350
+ Für ein Production-Build:
472
1351
 
473
- ```shell
1352
+ ```bash
474
1353
  npm run build:prod
475
1354
  ```
476
1355
 
477
- Um mit dem Paket lokal zu arbeiten, kann es mit npm in ein anderes Projekt verlinkt werden:
1356
+ ### Verfügbare Befehle
1357
+
1358
+ | Befehl | Beschreibung |
1359
+ | ---------------------- | ----------------------------------------------- |
1360
+ | `npm run build` | Development-Build (Code + Types + CSS parallel) |
1361
+ | `npm run build:prod` | Production-Build (clean + minify + tree-shake) |
1362
+ | `npm run build:types` | Nur `.d.ts`-Dateien generieren |
1363
+ | `npm run watch` | Watch-Modus (alle Build-Prozesse) |
1364
+ | `npm run format` | Code mit Prettier formatieren |
1365
+ | `npm run format:check` | Formatierung prüfen |
1366
+ | `npm run clean` | `build/`-Verzeichnis löschen |
1367
+
1368
+ ### Lokale Entwicklung mit npm link
478
1369
 
479
- ```shell
1370
+ Um das SDK lokal in einer Consumer-App zu testen:
1371
+
1372
+ ```bash
1373
+ # Im SDK-Verzeichnis
480
1374
  npm link
481
1375
  npm run watch
1376
+
1377
+ # Im Zielprojekt
1378
+ npm link @5minds/processcube_app_sdk
482
1379
  ```
483
1380
 
484
- Im Zielprojekt anschließend:
1381
+ Bei Problemen mit React (doppelte React-Instanz):
485
1382
 
486
- ```shell
487
- npm link @5minds/processcube_app_sdk
1383
+ ```bash
1384
+ npm link <pfad-zum-zielprojekt>/node_modules/react
488
1385
  ```
489
1386
 
490
- Bei Problemen mit React muss ggf. noch die React Dependency des Zielprojekts zurück in das SDK gelinkt werden, damit nur eine React Instanz zur Laufzeit existiert:
1387
+ ### Test-App
1388
+
1389
+ Im Verzeichnis `test-app/` liegt eine Next.js-Beispielanwendung:
1390
+
1391
+ ```bash
1392
+ cd test-app
491
1393
 
492
- ```shell
493
- npm link <path-to-project>/node_modules/react
1394
+ # Docker-Infrastruktur starten (Engine, Authority, PostgreSQL)
1395
+ docker compose up
1396
+
1397
+ # App starten
1398
+ npm run dev
494
1399
  ```
1400
+
1401
+ Die Test-App enthält drei External Task Handler (`test-task`, `doit`, `dothis`) und BPMN-Beispielprozesse.