@bpmsoftwaresolutions/ai-engine-client 1.1.83 → 1.1.86

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.
@@ -0,0 +1,457 @@
1
+ import { cleanText, isPlainObject } from '../utils/communication.js';
2
+
3
+ export function createPresenceDomain(client) {
4
+ return {
5
+ getPresenceBoard: (request) => getPresenceBoard(client, request),
6
+ getChannelPresence: (request) => getChannelPresence(client, request),
7
+ markParticipantOnline: (request) => markParticipantOnline(client, request),
8
+ markParticipantOffline: (request) => markParticipantOffline(client, request),
9
+ whoIsOnline: (request) => whoIsOnline(client, request),
10
+ findOnlineParticipant: (request) => findOnlineParticipant(client, request),
11
+ sendToParticipant: (request) => sendToParticipant(client, request),
12
+ sendToRole: (request) => sendToRole(client, request),
13
+ postPresenceHeartbeat: (request) => postPresenceHeartbeat(client, request),
14
+ };
15
+ }
16
+
17
+ export async function getPresenceBoard(client, {
18
+ workflowRunId,
19
+ workflow_run_id,
20
+ transferChannelId,
21
+ transfer_channel_id,
22
+ participantRole,
23
+ participant_role,
24
+ role,
25
+ capability,
26
+ includeStale,
27
+ include_stale,
28
+ } = {}) {
29
+ const normalizedTransferChannelId = cleanText(transfer_channel_id) || cleanText(transferChannelId);
30
+ const path = normalizedTransferChannelId
31
+ ? `/api/operator/projections/agent-communications/presence/${encodeURIComponent(normalizedTransferChannelId)}`
32
+ : '/api/operator/projections/agent-communications/presence';
33
+ return client._requestLogaProjection(path, {
34
+ query: {
35
+ workflow_run_id: cleanText(workflow_run_id) || cleanText(workflowRunId),
36
+ transfer_channel_id: normalizedTransferChannelId,
37
+ participant_role: cleanText(participant_role) || cleanText(participantRole) || cleanText(role),
38
+ capability: cleanText(capability),
39
+ include_stale: include_stale ?? includeStale,
40
+ },
41
+ });
42
+ }
43
+
44
+ export async function getChannelPresence(client, {
45
+ transferChannelId,
46
+ transfer_channel_id,
47
+ channelId,
48
+ channel_id,
49
+ workflowRunId,
50
+ workflow_run_id,
51
+ participantRole,
52
+ participant_role,
53
+ role,
54
+ capability,
55
+ includeStale,
56
+ include_stale,
57
+ } = {}) {
58
+ const normalizedTransferChannelId = cleanText(transfer_channel_id) || cleanText(transferChannelId) || cleanText(channel_id) || cleanText(channelId);
59
+ if (!normalizedTransferChannelId) {
60
+ throw new Error('transferChannelId is required.');
61
+ }
62
+ return client._requestLogaProjection(`/api/operator/projections/agent-communications/presence/${encodeURIComponent(normalizedTransferChannelId)}`, {
63
+ query: {
64
+ workflow_run_id: cleanText(workflow_run_id) || cleanText(workflowRunId),
65
+ participant_role: cleanText(participant_role) || cleanText(participantRole) || cleanText(role),
66
+ capability: cleanText(capability),
67
+ include_stale: include_stale ?? includeStale,
68
+ },
69
+ });
70
+ }
71
+
72
+ export async function postPresenceHeartbeat(client, {
73
+ transferChannelId,
74
+ transfer_channel_id,
75
+ channelId,
76
+ channel_id,
77
+ workTransferPacketId,
78
+ work_transfer_packet_id,
79
+ workflowRunId,
80
+ workflow_run_id,
81
+ participantRole,
82
+ participant_role,
83
+ activityState,
84
+ activity_state,
85
+ currentPhase,
86
+ current_phase,
87
+ currentTaskSummary,
88
+ current_task_summary,
89
+ isActive,
90
+ is_active,
91
+ agentSessionId,
92
+ agent_session_id,
93
+ actorSessionId,
94
+ actor_session_id,
95
+ observedAt,
96
+ observed_at,
97
+ metadata = {},
98
+ } = {}) {
99
+ const normalizedTransferChannelId = cleanText(transfer_channel_id) || cleanText(transferChannelId) || cleanText(channel_id) || cleanText(channelId);
100
+ if (!normalizedTransferChannelId) throw new Error('transferChannelId is required.');
101
+ return client._request(`/api/agent-communications/transfer-channels/${encodeURIComponent(normalizedTransferChannelId)}/heartbeats`, {
102
+ method: 'POST',
103
+ body: {
104
+ work_transfer_packet_id: cleanText(work_transfer_packet_id) || cleanText(workTransferPacketId),
105
+ workflow_run_id: cleanText(workflow_run_id) || cleanText(workflowRunId),
106
+ participant_role: cleanText(participant_role) || cleanText(participantRole),
107
+ activity_state: cleanText(activity_state) || cleanText(activityState) || 'active',
108
+ current_phase: cleanText(current_phase) || cleanText(currentPhase),
109
+ current_task_summary: cleanText(current_task_summary) || cleanText(currentTaskSummary),
110
+ is_active: Boolean(is_active ?? isActive ?? true),
111
+ agent_session_id: cleanText(agent_session_id) || cleanText(agentSessionId) || client.agentSessionId,
112
+ actor_session_id: cleanText(actor_session_id) || cleanText(actorSessionId),
113
+ observed_at: observed_at || observedAt,
114
+ metadata: isPlainObject(metadata) ? metadata : {},
115
+ },
116
+ });
117
+ }
118
+
119
+ async function resolvePresenceContext(client, request = {}) {
120
+ const {
121
+ transferChannelId,
122
+ transfer_channel_id,
123
+ channelId,
124
+ channel_id,
125
+ workTransferPacketId,
126
+ work_transfer_packet_id,
127
+ packetId,
128
+ packet_id,
129
+ workflowRunId,
130
+ workflow_run_id,
131
+ participantRole,
132
+ participant_role,
133
+ role,
134
+ } = request;
135
+ const normalizedTransferChannelId = cleanText(transfer_channel_id) || cleanText(transferChannelId) || cleanText(channel_id) || cleanText(channelId);
136
+ const normalizedParticipantRole = cleanText(participant_role) || cleanText(participantRole) || cleanText(role);
137
+ const normalizedWorkflowRunId = cleanText(workflow_run_id) || cleanText(workflowRunId);
138
+ if (normalizedTransferChannelId) {
139
+ let normalizedPacketId = cleanText(work_transfer_packet_id) || cleanText(workTransferPacketId) || cleanText(packet_id) || cleanText(packetId);
140
+ if (!normalizedPacketId) {
141
+ try {
142
+ const status = await client.getCommunicationChannelStatus({ transferChannelId: normalizedTransferChannelId });
143
+ normalizedPacketId = cleanText(status?.packet_id || status?.packetId || status?.work_transfer_packet_id);
144
+ } catch (error) {
145
+ void error;
146
+ }
147
+ }
148
+ return { channel_id: normalizedTransferChannelId, packet_id: normalizedPacketId, workflow_run_id: normalizedWorkflowRunId, stop_reason: null };
149
+ }
150
+ const presence = await client.whoIsOnline({ workflowRunId: normalizedWorkflowRunId, participantRole: normalizedParticipantRole, includeStale: true });
151
+ const directoryRows = Array.isArray(presence?.presence)
152
+ ? presence.presence.filter((row) => row && typeof row === 'object')
153
+ : Array.isArray(presence?.online_participants)
154
+ ? presence.online_participants.filter((row) => row && typeof row === 'object')
155
+ : Array.isArray(presence?.participants)
156
+ ? presence.participants.filter((row) => row && typeof row === 'object')
157
+ : [];
158
+ const filteredRows = normalizedParticipantRole
159
+ ? directoryRows.filter((row) => cleanText(row.participant_role) === normalizedParticipantRole)
160
+ : [];
161
+ const onlineRows = filteredRows.filter((row) => Boolean(row.is_online));
162
+ const chooseRows = onlineRows.length > 0 ? onlineRows : filteredRows;
163
+ if (chooseRows.length === 1) {
164
+ return {
165
+ channel_id: cleanText(chooseRows[0].transfer_channel_id),
166
+ packet_id: cleanText(chooseRows[0].work_transfer_packet_id) || cleanText(chooseRows[0].packet_id),
167
+ workflow_run_id: cleanText(chooseRows[0].workflow_run_id) || normalizedWorkflowRunId,
168
+ stop_reason: null,
169
+ };
170
+ }
171
+ const openChannels = await client.listOpenCommunicationChannels({ workflowRunId: normalizedWorkflowRunId });
172
+ const openChannelRows = Array.isArray(openChannels) ? openChannels.filter((row) => row && typeof row === 'object') : [];
173
+ if (openChannelRows.length === 1) {
174
+ return {
175
+ channel_id: cleanText(openChannelRows[0].channel_id || openChannelRows[0].transfer_channel_id),
176
+ packet_id: cleanText(openChannelRows[0].packet_id || openChannelRows[0].work_transfer_packet_id),
177
+ workflow_run_id: cleanText(openChannelRows[0].workflow_run_id) || normalizedWorkflowRunId,
178
+ stop_reason: null,
179
+ };
180
+ }
181
+ if (openChannelRows.length > 1) {
182
+ return {
183
+ channel_id: null,
184
+ packet_id: null,
185
+ workflow_run_id: normalizedWorkflowRunId,
186
+ stop_reason: {
187
+ code: 'multiple_open_channels_need_disambiguation',
188
+ message: 'Multiple open channels matched the requested coordination context.',
189
+ details: {
190
+ workflow_run_id: normalizedWorkflowRunId,
191
+ participant_role: normalizedParticipantRole,
192
+ candidate_count: openChannelRows.length,
193
+ candidate_channel_ids: openChannelRows.map((row) => cleanText(row.channel_id || row.transfer_channel_id)).filter(Boolean),
194
+ },
195
+ },
196
+ };
197
+ }
198
+ return {
199
+ channel_id: null,
200
+ packet_id: null,
201
+ workflow_run_id: normalizedWorkflowRunId,
202
+ stop_reason: {
203
+ code: 'active_channel_not_found',
204
+ message: 'Active transfer channel not found.',
205
+ details: { workflow_run_id: normalizedWorkflowRunId, participant_role: normalizedParticipantRole },
206
+ },
207
+ };
208
+ }
209
+
210
+ export async function markParticipantOnline(client, request = {}) {
211
+ const {
212
+ transferChannelId,
213
+ transfer_channel_id,
214
+ channelId,
215
+ channel_id,
216
+ workTransferPacketId,
217
+ work_transfer_packet_id,
218
+ packetId,
219
+ packet_id,
220
+ workflowRunId,
221
+ workflow_run_id,
222
+ participantRole,
223
+ participant_role,
224
+ currentPhase,
225
+ current_phase,
226
+ currentTaskSummary,
227
+ current_task_summary,
228
+ agentSessionId,
229
+ agent_session_id,
230
+ actorSessionId,
231
+ actor_session_id,
232
+ observedAt,
233
+ observed_at,
234
+ metadata = {},
235
+ } = request;
236
+ const normalizedTransferChannelId = cleanText(transfer_channel_id) || cleanText(transferChannelId) || cleanText(channel_id) || cleanText(channelId);
237
+ if (!normalizedTransferChannelId) throw new Error('transferChannelId is required.');
238
+ const resolved = await resolvePresenceContext(client, request);
239
+ return client._request(`/api/agent-communications/transfer-channels/${encodeURIComponent(normalizedTransferChannelId)}/presence/online`, {
240
+ method: 'POST',
241
+ body: {
242
+ transfer_channel_id: normalizedTransferChannelId,
243
+ work_transfer_packet_id: cleanText(work_transfer_packet_id) || cleanText(workTransferPacketId) || cleanText(packet_id) || cleanText(packetId) || resolved.packet_id,
244
+ workflow_run_id: cleanText(workflow_run_id) || cleanText(workflowRunId),
245
+ participant_role: cleanText(participant_role) || cleanText(participantRole),
246
+ current_phase: cleanText(current_phase) || cleanText(currentPhase),
247
+ current_task_summary: cleanText(current_task_summary) || cleanText(currentTaskSummary),
248
+ agent_session_id: cleanText(agent_session_id) || cleanText(agentSessionId) || client.agentSessionId,
249
+ actor_session_id: cleanText(actor_session_id) || cleanText(actorSessionId),
250
+ observed_at: cleanText(observed_at) || cleanText(observedAt),
251
+ metadata: isPlainObject(metadata) ? metadata : {},
252
+ is_active: true,
253
+ activity_state: 'active',
254
+ },
255
+ });
256
+ }
257
+
258
+ export async function markParticipantOffline(client, request = {}) {
259
+ const {
260
+ transferChannelId,
261
+ transfer_channel_id,
262
+ channelId,
263
+ channel_id,
264
+ workTransferPacketId,
265
+ work_transfer_packet_id,
266
+ packetId,
267
+ packet_id,
268
+ workflowRunId,
269
+ workflow_run_id,
270
+ participantRole,
271
+ participant_role,
272
+ currentPhase,
273
+ current_phase,
274
+ currentTaskSummary,
275
+ current_task_summary,
276
+ agentSessionId,
277
+ agent_session_id,
278
+ actorSessionId,
279
+ actor_session_id,
280
+ observedAt,
281
+ observed_at,
282
+ metadata = {},
283
+ } = request;
284
+ const normalizedTransferChannelId = cleanText(transfer_channel_id) || cleanText(transferChannelId) || cleanText(channel_id) || cleanText(channelId);
285
+ if (!normalizedTransferChannelId) throw new Error('transferChannelId is required.');
286
+ const resolved = await resolvePresenceContext(client, request);
287
+ return client._request(`/api/agent-communications/transfer-channels/${encodeURIComponent(normalizedTransferChannelId)}/presence/offline`, {
288
+ method: 'POST',
289
+ body: {
290
+ transfer_channel_id: normalizedTransferChannelId,
291
+ work_transfer_packet_id: cleanText(work_transfer_packet_id) || cleanText(workTransferPacketId) || cleanText(packet_id) || cleanText(packetId) || resolved.packet_id,
292
+ workflow_run_id: cleanText(workflow_run_id) || cleanText(workflowRunId),
293
+ participant_role: cleanText(participant_role) || cleanText(participantRole),
294
+ current_phase: cleanText(current_phase) || cleanText(currentPhase),
295
+ current_task_summary: cleanText(current_task_summary) || cleanText(currentTaskSummary),
296
+ agent_session_id: cleanText(agent_session_id) || cleanText(agentSessionId) || client.agentSessionId,
297
+ actor_session_id: cleanText(actor_session_id) || cleanText(actorSessionId),
298
+ observed_at: cleanText(observed_at) || cleanText(observedAt),
299
+ metadata: isPlainObject(metadata) ? metadata : {},
300
+ is_active: false,
301
+ activity_state: 'offline',
302
+ },
303
+ });
304
+ }
305
+
306
+ export async function whoIsOnline(client, {
307
+ workflowRunId,
308
+ workflow_run_id,
309
+ transferChannelId,
310
+ transfer_channel_id,
311
+ participantRole,
312
+ participant_role,
313
+ role,
314
+ capability,
315
+ includeStale,
316
+ include_stale,
317
+ } = {}) {
318
+ const normalizedTransferChannelId = cleanText(transfer_channel_id) || cleanText(transferChannelId);
319
+ const path = normalizedTransferChannelId
320
+ ? `/api/agent-communications/transfer-channels/${encodeURIComponent(normalizedTransferChannelId)}/presence`
321
+ : '/api/agent-communications/presence';
322
+ return client._request(path, {
323
+ query: {
324
+ workflow_run_id: cleanText(workflow_run_id) || cleanText(workflowRunId),
325
+ participant_role: cleanText(participant_role) || cleanText(participantRole) || cleanText(role),
326
+ capability: cleanText(capability),
327
+ include_stale: include_stale ?? includeStale,
328
+ },
329
+ });
330
+ }
331
+
332
+ export async function findOnlineParticipant(client, request = {}) {
333
+ const {
334
+ workflowRunId,
335
+ workflow_run_id,
336
+ transferChannelId,
337
+ transfer_channel_id,
338
+ participantRole,
339
+ participant_role,
340
+ role,
341
+ capability,
342
+ } = request;
343
+ const normalizedTransferChannelId = cleanText(transfer_channel_id) || cleanText(transferChannelId);
344
+ const body = {
345
+ workflow_run_id: cleanText(workflow_run_id) || cleanText(workflowRunId),
346
+ transfer_channel_id: normalizedTransferChannelId,
347
+ participant_role: cleanText(participant_role) || cleanText(participantRole) || cleanText(role),
348
+ capability: cleanText(capability),
349
+ };
350
+ const path = normalizedTransferChannelId
351
+ ? `/api/agent-communications/transfer-channels/${encodeURIComponent(normalizedTransferChannelId)}/presence/find`
352
+ : '/api/agent-communications/presence/find';
353
+ return client._request(path, {
354
+ method: 'POST',
355
+ body,
356
+ });
357
+ }
358
+
359
+ export async function sendToParticipant(client, {
360
+ transferChannelId,
361
+ transfer_channel_id,
362
+ channelId,
363
+ channel_id,
364
+ recipientParticipantId,
365
+ recipient_participant_id,
366
+ recipientAgentSessionId,
367
+ recipient_agent_session_id,
368
+ recipientActorSessionId,
369
+ recipient_actor_session_id,
370
+ senderAgentSessionId,
371
+ sender_agent_session_id,
372
+ senderRole,
373
+ sender_role,
374
+ recipientRole,
375
+ recipient_role,
376
+ role,
377
+ workflowRunId,
378
+ workflow_run_id,
379
+ messageKind,
380
+ message_kind,
381
+ expectedMessageKind,
382
+ expected_message_kind,
383
+ bodyMarkdown,
384
+ body_markdown,
385
+ payload = {},
386
+ metadata = {},
387
+ } = {}) {
388
+ const normalizedTransferChannelId = cleanText(transfer_channel_id) || cleanText(transferChannelId) || cleanText(channel_id) || cleanText(channelId);
389
+ const path = normalizedTransferChannelId
390
+ ? `/api/agent-communications/transfer-channels/${encodeURIComponent(normalizedTransferChannelId)}/messages/send-to-participant`
391
+ : '/api/agent-communications/messages/send-to-participant';
392
+ return client._request(path, {
393
+ method: 'POST',
394
+ body: {
395
+ transfer_channel_id: normalizedTransferChannelId,
396
+ workflow_run_id: cleanText(workflow_run_id) || cleanText(workflowRunId),
397
+ recipient_participant_id: cleanText(recipient_participant_id) || cleanText(recipientParticipantId),
398
+ recipient_agent_session_id: cleanText(recipient_agent_session_id) || cleanText(recipientAgentSessionId),
399
+ recipient_actor_session_id: cleanText(recipient_actor_session_id) || cleanText(recipientActorSessionId),
400
+ message_kind: cleanText(message_kind) || cleanText(messageKind),
401
+ expected_message_kind: cleanText(expected_message_kind) || cleanText(expectedMessageKind),
402
+ body_markdown: cleanText(body_markdown) || cleanText(bodyMarkdown),
403
+ sender_agent_session_id: cleanText(sender_agent_session_id) || cleanText(senderAgentSessionId) || client.agentSessionId,
404
+ sender_role: cleanText(sender_role) || cleanText(senderRole),
405
+ recipient_role: cleanText(recipient_role) || cleanText(recipientRole) || cleanText(role),
406
+ payload: isPlainObject(payload) ? payload : {},
407
+ metadata: isPlainObject(metadata) ? metadata : {},
408
+ },
409
+ });
410
+ }
411
+
412
+ export async function sendToRole(client, {
413
+ transferChannelId,
414
+ transfer_channel_id,
415
+ channelId,
416
+ channel_id,
417
+ recipientRole,
418
+ recipient_role,
419
+ role,
420
+ senderAgentSessionId,
421
+ sender_agent_session_id,
422
+ senderRole,
423
+ sender_role,
424
+ recipientAgentSessionId,
425
+ recipient_agent_session_id,
426
+ workflowRunId,
427
+ workflow_run_id,
428
+ messageKind,
429
+ message_kind,
430
+ expectedMessageKind,
431
+ expected_message_kind,
432
+ bodyMarkdown,
433
+ body_markdown,
434
+ payload = {},
435
+ metadata = {},
436
+ } = {}) {
437
+ const normalizedTransferChannelId = cleanText(transfer_channel_id) || cleanText(transferChannelId) || cleanText(channel_id) || cleanText(channelId);
438
+ const path = normalizedTransferChannelId
439
+ ? `/api/agent-communications/transfer-channels/${encodeURIComponent(normalizedTransferChannelId)}/messages/send-to-role`
440
+ : '/api/agent-communications/messages/send-to-role';
441
+ return client._request(path, {
442
+ method: 'POST',
443
+ body: {
444
+ transfer_channel_id: normalizedTransferChannelId,
445
+ workflow_run_id: cleanText(workflow_run_id) || cleanText(workflowRunId),
446
+ sender_agent_session_id: cleanText(sender_agent_session_id) || cleanText(senderAgentSessionId) || client.agentSessionId,
447
+ sender_role: cleanText(sender_role) || cleanText(senderRole),
448
+ recipient_agent_session_id: cleanText(recipient_agent_session_id) || cleanText(recipientAgentSessionId),
449
+ recipient_role: cleanText(recipient_role) || cleanText(recipientRole) || cleanText(role),
450
+ message_kind: cleanText(message_kind) || cleanText(messageKind),
451
+ expected_message_kind: cleanText(expected_message_kind) || cleanText(expectedMessageKind),
452
+ body_markdown: cleanText(body_markdown) || cleanText(bodyMarkdown),
453
+ payload: isPlainObject(payload) ? payload : {},
454
+ metadata: isPlainObject(metadata) ? metadata : {},
455
+ },
456
+ });
457
+ }