@healthcloudai/hc-ams 3.6.5 → 3.7.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/dist/index.js CHANGED
@@ -1,11 +1,28 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
+ var __privateWrapper = (obj, member, setter, getter) => ({
10
+ set _(value) {
11
+ __privateSet(obj, member, value, setter);
12
+ },
13
+ get _() {
14
+ return __privateGet(obj, member, getter);
15
+ }
16
+ });
17
+
1
18
  // src/config/SDKConfig.ts
2
19
  var SERVICE_NAME = "ams";
3
20
  var DEFAULT_TIMEOUT_MS = 3e4;
4
21
  function resolveAmsOrigin(environment) {
5
22
  assertEnvironment(environment);
6
23
  if (environment === "dev") return "https://dev-ams-services.health.cloud";
7
- const prefix = environment === "prod" ? "" : `${environment}-`;
8
- return `https://${prefix}amsservices.health.cloud`;
24
+ if (environment === "prod") return "https://ams-services.health.cloud";
25
+ return "https://uat-amsservices.health.cloud";
9
26
  }
10
27
  function resolveBaseUrl(environment) {
11
28
  return `${resolveAmsOrigin(environment)}/api/ams`;
@@ -942,6 +959,414 @@ var WssClient = class {
942
959
  }
943
960
  };
944
961
 
962
+ // src/v2/validation.ts
963
+ function invalid(message) {
964
+ return new SDKError({ message, statusCode: 0 });
965
+ }
966
+ function record(value) {
967
+ return typeof value === "object" && value !== null && !Array.isArray(value);
968
+ }
969
+ function uuid(value) {
970
+ return typeof value === "string" && /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/.test(value);
971
+ }
972
+ function identity(value) {
973
+ return typeof value === "string" && /^[A-Za-z0-9_-]{1,128}$/.test(value);
974
+ }
975
+ function protocol(value) {
976
+ return typeof value === "string" && [
977
+ "dbos_signin_v1",
978
+ "dbos_signin_cards_v1",
979
+ "dbos_signin_actions_v1",
980
+ "dbos_signin_booking_v1",
981
+ "dbos_signin_cta_v1"
982
+ ].includes(value);
983
+ }
984
+ function cursor(value) {
985
+ return Number.isSafeInteger(value) && value >= 0;
986
+ }
987
+ function ownedRun(value) {
988
+ if (!record(value) || !uuid(value.run_id) || !protocol(value.protocol) || typeof value.run_secret !== "string" || value.run_secret.length < 32 || value.run_secret.length > 128) {
989
+ throw invalid("Invalid V2 run credentials or protocol.");
990
+ }
991
+ }
992
+ function onlyKeys(value, allowed) {
993
+ return Object.keys(value).every((key) => allowed.includes(key));
994
+ }
995
+ function capabilities(value) {
996
+ if (!record(value) || !onlyKeys(value, [
997
+ "platform",
998
+ "supported_actions",
999
+ "patient_flags_confirmation",
1000
+ "intake_confirmation",
1001
+ "encounter_confirmation",
1002
+ "locale",
1003
+ "time_zone"
1004
+ ])) return false;
1005
+ if (value.platform !== void 0 && !["web", "ios", "android", "terminal", "unknown"].includes(value.platform)) return false;
1006
+ if (value.supported_actions !== void 0 && (!Array.isArray(value.supported_actions) || value.supported_actions.length > 16 || !value.supported_actions.every((action) => ["confirm", "insurance_scan", "vitals_import", "slot_picker", "login", "symptom_save", "telehealth_join"].includes(action)))) return false;
1007
+ for (const key of ["patient_flags_confirmation", "intake_confirmation", "encounter_confirmation"]) {
1008
+ if (value[key] !== void 0 && typeof value[key] !== "boolean") return false;
1009
+ }
1010
+ return ["locale", "time_zone"].every((key) => value[key] === void 0 || typeof value[key] === "string" && value[key].length > 0 && value[key].length <= 128);
1011
+ }
1012
+
1013
+ // src/v2/NativeV2Client.ts
1014
+ var NativeV2Client = class {
1015
+ constructor(http) {
1016
+ this.http = http;
1017
+ }
1018
+ async listPublicAgents(tenantId, options = {}) {
1019
+ if (!identity(tenantId)) throw invalid("Invalid V2 tenant identity.");
1020
+ const result = await this.request("GET", "/public/tenants/{tenant_id}/agents", tenantId, options);
1021
+ if (!record(result) || !Array.isArray(result.agents) || !result.agents.every((agent) => record(agent) && identity(agent.id) && typeof agent.name === "string" && protocol(agent.protocol))) {
1022
+ throw invalid("Invalid V2 public discovery response.");
1023
+ }
1024
+ return { agents: result.agents.map((agent) => ({ id: agent.id, name: agent.name, protocol: agent.protocol })) };
1025
+ }
1026
+ async createRun(tenantId, agentId, options = {}) {
1027
+ if (!identity(tenantId) || !identity(agentId)) throw invalid("Invalid V2 tenant or agent identity.");
1028
+ const result = await this.request("POST", "/public/tenants/{tenant_id}/agents/{agent_id}/runs", tenantId, options, agentId);
1029
+ ownedRun(result);
1030
+ return { run_id: result.run_id, run_secret: result.run_secret, protocol: result.protocol };
1031
+ }
1032
+ async request(method, path, tenantId, options, agentId) {
1033
+ if (options.origin !== void 0) {
1034
+ try {
1035
+ const parsed = new URL(options.origin);
1036
+ if (!["https:", "http:"].includes(parsed.protocol) || parsed.origin !== options.origin) throw invalid("Invalid V2 admission origin.");
1037
+ } catch {
1038
+ throw invalid("Invalid V2 admission origin.");
1039
+ }
1040
+ }
1041
+ try {
1042
+ return await this.http.request(method, path, {
1043
+ pathParams: { tenant_id: tenantId, ...agentId ? { agent_id: agentId } : {} },
1044
+ auth: false,
1045
+ ...method === "POST" ? { body: {} } : {},
1046
+ ...options.origin !== void 0 ? { headers: { Origin: options.origin } } : {}
1047
+ });
1048
+ } catch (error) {
1049
+ throw new SDKError({
1050
+ message: "V2 public admission request failed.",
1051
+ statusCode: error instanceof SDKError ? error.statusCode : 0
1052
+ });
1053
+ }
1054
+ }
1055
+ };
1056
+
1057
+ // src/v2/NativeV2WssClient.ts
1058
+ var _options, _socket, _ctor, _base, _authenticated, _lastTurn, _cursor, _sentCommands, _generation, _cleanup, _cancelConnect, _listeners, _NativeV2WssClient_instances, requireConnected_fn, validAuth_fn, receive_fn, dispatch_fn;
1059
+ var NativeV2WssClient = class {
1060
+ constructor(options) {
1061
+ __privateAdd(this, _NativeV2WssClient_instances);
1062
+ __privateAdd(this, _options);
1063
+ __privateAdd(this, _socket);
1064
+ __privateAdd(this, _ctor);
1065
+ __privateAdd(this, _base);
1066
+ __privateAdd(this, _authenticated, false);
1067
+ __privateAdd(this, _lastTurn, null);
1068
+ __privateAdd(this, _cursor);
1069
+ __privateAdd(this, _sentCommands, /* @__PURE__ */ new Map());
1070
+ __privateAdd(this, _generation, 0);
1071
+ __privateAdd(this, _cleanup);
1072
+ __privateAdd(this, _cancelConnect);
1073
+ __privateAdd(this, _listeners, /* @__PURE__ */ new Map());
1074
+ var _a, _b, _c, _d, _e, _f, _g;
1075
+ ownedRun(options.run);
1076
+ const owner = options.identity.kind === "anonymous" ? options.identity.sessionId : options.identity.patientId;
1077
+ if (!identity(options.tenantId) || !identity(options.agentId) || !identity(owner) || options.identity.kind !== "anonymous" && options.identity.kind !== "patient" || options.identity.kind === "patient" && typeof options.identity.getAccessToken !== "function" || !cursor((_a = options.replaySince) != null ? _a : 0)) throw invalid("Invalid V2 connection identity or replay cursor.");
1078
+ if (!capabilities((_b = options.capabilities) != null ? _b : {}) || options.lang !== void 0 && (typeof options.lang !== "string" || !options.lang || options.lang.length > 128)) throw invalid("Invalid V2 client capabilities or language.");
1079
+ assertTimeout((_c = options.authTimeoutMs) != null ? _c : 1e4, "authTimeoutMs");
1080
+ __privateSet(this, _base, normalizeWssBaseUrl((_d = options.baseUrlOverride) != null ? _d : resolveWssBaseUrl(options.environment)));
1081
+ const ctor = (_e = options.WebSocketImpl) != null ? _e : globalThis.WebSocket;
1082
+ if (!ctor) throw invalid("No WebSocket implementation available. Pass WebSocketImpl.");
1083
+ __privateSet(this, _ctor, ctor);
1084
+ __privateSet(this, _cursor, (_f = options.replaySince) != null ? _f : 0);
1085
+ __privateSet(this, _options, {
1086
+ ...options,
1087
+ run: { ...options.run },
1088
+ identity: { ...options.identity },
1089
+ capabilities: JSON.parse(JSON.stringify((_g = options.capabilities) != null ? _g : {}))
1090
+ });
1091
+ }
1092
+ get lastTurnId() {
1093
+ return __privateGet(this, _lastTurn);
1094
+ }
1095
+ get replaySince() {
1096
+ return __privateGet(this, _cursor);
1097
+ }
1098
+ on(type, listener) {
1099
+ if (!__privateGet(this, _listeners).has(type)) __privateGet(this, _listeners).set(type, /* @__PURE__ */ new Set());
1100
+ const entry = listener;
1101
+ __privateGet(this, _listeners).get(type).add(entry);
1102
+ return () => {
1103
+ var _a;
1104
+ (_a = __privateGet(this, _listeners).get(type)) == null ? void 0 : _a.delete(entry);
1105
+ };
1106
+ }
1107
+ connect() {
1108
+ var _a;
1109
+ if (__privateGet(this, _socket) && __privateGet(this, _socket).readyState < 2) return Promise.reject(invalid("V2 connection is already active."));
1110
+ (_a = __privateGet(this, _cleanup)) == null ? void 0 : _a.call(this);
1111
+ __privateSet(this, _authenticated, false);
1112
+ const generation = ++__privateWrapper(this, _generation)._;
1113
+ const options = __privateGet(this, _options);
1114
+ const owner = options.identity.kind === "anonymous" ? options.identity.sessionId : options.identity.patientId;
1115
+ const url = new URL(joinServiceUrl(__privateGet(this, _base), `/ws/agents/${encodeURIComponent(options.agentId)}/chat`));
1116
+ url.searchParams.set("session_id", `${options.tenantId}/${owner}`);
1117
+ url.searchParams.set("transport", options.run.protocol);
1118
+ url.searchParams.set("run_id", options.run.run_id);
1119
+ url.searchParams.set("replay_since", String(__privateGet(this, _cursor)));
1120
+ if (options.identity.kind === "anonymous") url.searchParams.set("anonymous", "1");
1121
+ if (options.lang) url.searchParams.set("lang", options.lang);
1122
+ return new Promise((resolve, reject) => {
1123
+ var _a2;
1124
+ let socket;
1125
+ try {
1126
+ socket = new (__privateGet(this, _ctor))(url.toString());
1127
+ } catch {
1128
+ reject(invalid("V2 connection could not be opened."));
1129
+ return;
1130
+ }
1131
+ __privateSet(this, _socket, socket);
1132
+ let settled = false;
1133
+ let authSent = false;
1134
+ const current = () => generation === __privateGet(this, _generation) && socket === __privateGet(this, _socket);
1135
+ const timer = setTimeout(() => fail("Authentication timed out."), (_a2 = options.authTimeoutMs) != null ? _a2 : 1e4);
1136
+ const cleanup = () => {
1137
+ clearTimeout(timer);
1138
+ socket.removeEventListener("open", onOpen);
1139
+ socket.removeEventListener("message", onMessage);
1140
+ socket.removeEventListener("close", onClose);
1141
+ socket.removeEventListener("error", onError);
1142
+ };
1143
+ const fail = (message) => {
1144
+ if (!current()) return;
1145
+ clearTimeout(timer);
1146
+ if (!settled) {
1147
+ settled = true;
1148
+ reject(invalid(message));
1149
+ } else __privateMethod(this, _NativeV2WssClient_instances, dispatch_fn).call(this, "error", { code: "v2_transport_error", message });
1150
+ __privateSet(this, _authenticated, false);
1151
+ cleanup();
1152
+ __privateSet(this, _cancelConnect, void 0);
1153
+ socket.close();
1154
+ };
1155
+ const onOpen = () => {
1156
+ void (async () => {
1157
+ try {
1158
+ const token = options.identity.kind === "patient" ? await options.identity.getAccessToken() : "";
1159
+ if (!current() || settled || socket.readyState !== 1) return;
1160
+ if (typeof token !== "string" || token.length > 4096 || options.identity.kind === "patient" && !token) throw invalid("Invalid V2 access token.");
1161
+ socket.send(JSON.stringify({
1162
+ type: "auth",
1163
+ token,
1164
+ run_id: options.run.run_id,
1165
+ run_secret: options.run.run_secret,
1166
+ client: options.capabilities,
1167
+ ...options.identity.kind === "patient" ? { patient_id: options.identity.patientId } : {}
1168
+ }));
1169
+ authSent = true;
1170
+ } catch {
1171
+ fail("V2 authentication credentials could not be resolved.");
1172
+ }
1173
+ })();
1174
+ };
1175
+ const onMessage = (event) => {
1176
+ if (!current()) return;
1177
+ let frame;
1178
+ try {
1179
+ if (typeof event.data !== "string" || event.data.length > 2 * 1024 * 1024) throw invalid("Invalid V2 frame.");
1180
+ frame = JSON.parse(event.data);
1181
+ if (!record(frame) || typeof frame.type !== "string" || !record(frame.payload)) throw invalid("Invalid V2 frame.");
1182
+ } catch {
1183
+ fail("Invalid V2 server message.");
1184
+ return;
1185
+ }
1186
+ if (!__privateGet(this, _authenticated)) {
1187
+ if (frame.type === "error") {
1188
+ fail("V2 authentication failed.");
1189
+ return;
1190
+ }
1191
+ if (frame.type !== "auth_ok") return;
1192
+ if (!authSent || !__privateMethod(this, _NativeV2WssClient_instances, validAuth_fn).call(this, frame.payload)) {
1193
+ fail("Invalid V2 authentication response.");
1194
+ return;
1195
+ }
1196
+ const metadata = frame.payload;
1197
+ __privateSet(this, _lastTurn, metadata.last_turn_id);
1198
+ __privateSet(this, _authenticated, true);
1199
+ clearTimeout(timer);
1200
+ settled = true;
1201
+ __privateSet(this, _cancelConnect, void 0);
1202
+ __privateMethod(this, _NativeV2WssClient_instances, dispatch_fn).call(this, "auth_ok", metadata);
1203
+ resolve(metadata);
1204
+ return;
1205
+ }
1206
+ if (frame.type === "auth_ok") {
1207
+ fail("Unexpected V2 authentication response.");
1208
+ return;
1209
+ }
1210
+ __privateMethod(this, _NativeV2WssClient_instances, receive_fn).call(this, frame.type, frame.payload);
1211
+ };
1212
+ const onClose = (event) => {
1213
+ if (!current()) return;
1214
+ cleanup();
1215
+ __privateSet(this, _authenticated, false);
1216
+ __privateSet(this, _cancelConnect, void 0);
1217
+ if (!settled) {
1218
+ settled = true;
1219
+ reject(invalid("V2 connection closed before authentication."));
1220
+ }
1221
+ __privateMethod(this, _NativeV2WssClient_instances, dispatch_fn).call(this, "close", { code: event.code });
1222
+ };
1223
+ const onError = () => fail("V2 connection failed.");
1224
+ __privateSet(this, _cancelConnect, () => {
1225
+ if (!settled) {
1226
+ settled = true;
1227
+ reject(invalid("V2 connection closed before authentication."));
1228
+ }
1229
+ });
1230
+ __privateSet(this, _cleanup, cleanup);
1231
+ socket.addEventListener("open", onOpen);
1232
+ socket.addEventListener("message", onMessage);
1233
+ socket.addEventListener("close", onClose);
1234
+ socket.addEventListener("error", onError);
1235
+ });
1236
+ }
1237
+ close() {
1238
+ var _a, _b, _c;
1239
+ (_a = __privateGet(this, _cancelConnect)) == null ? void 0 : _a.call(this);
1240
+ __privateSet(this, _cancelConnect, void 0);
1241
+ ++__privateWrapper(this, _generation)._;
1242
+ (_b = __privateGet(this, _cleanup)) == null ? void 0 : _b.call(this);
1243
+ __privateSet(this, _cleanup, void 0);
1244
+ __privateSet(this, _authenticated, false);
1245
+ (_c = __privateGet(this, _socket)) == null ? void 0 : _c.close();
1246
+ __privateSet(this, _socket, void 0);
1247
+ }
1248
+ sendChat(payload) {
1249
+ const command = { type: "chat", payload: { ...payload } };
1250
+ this.sendCommand(command);
1251
+ return command;
1252
+ }
1253
+ sendActionResult(payload) {
1254
+ this.sendCommand({ type: "action_result", payload });
1255
+ }
1256
+ /** Offered-login socket binding; ordinary app sign-in uses its separate HTTP contract. */
1257
+ bindPatient(payload) {
1258
+ this.sendCommand({ type: "bind_patient", payload });
1259
+ }
1260
+ /** Explicit retries resend the caller's original command identity and terms. */
1261
+ sendCommand(command) {
1262
+ var _a, _b, _c, _d, _e, _f;
1263
+ __privateMethod(this, _NativeV2WssClient_instances, requireConnected_fn).call(this);
1264
+ if (!record(command) || !onlyKeys(command, ["type", "payload"]) || !record(command.payload)) {
1265
+ throw invalid("Invalid V2 command.");
1266
+ }
1267
+ const payload = command.payload;
1268
+ const serialized = JSON.stringify(command);
1269
+ const original = __privateGet(this, _sentCommands).get(payload.turn_id);
1270
+ if (!uuid(payload.turn_id) || !(payload.previous_turn_id === null || uuid(payload.previous_turn_id)) || payload.turn_id === payload.previous_turn_id || (original !== void 0 ? original !== serialized : payload.previous_turn_id !== __privateGet(this, _lastTurn))) {
1271
+ throw invalid("Invalid V2 command identity or predecessor.");
1272
+ }
1273
+ if (command.type === "chat") {
1274
+ if (!onlyKeys(payload, ["content", "turn_id", "previous_turn_id", "cta_confirmation"]) || typeof payload.content !== "string" || !payload.content.length || payload.content.length > 16384) throw invalid("Invalid V2 chat command.");
1275
+ if (payload.cta_confirmation !== void 0) {
1276
+ const choice = payload.cta_confirmation;
1277
+ if (__privateGet(this, _options).run.protocol !== "dbos_signin_cta_v1" || payload.previous_turn_id === null || !record(choice) || !onlyKeys(choice, ["offer_id", "action", "option_id"]) || typeof choice.offer_id !== "string" || !/^[0-9a-f]{32}$/.test(choice.offer_id) || typeof choice.action !== "string" || !/^[a-z0-9_-]{1,128}$/.test(choice.action) || ![`${choice.offer_id}:yes`, `${choice.offer_id}:no`].includes(choice.option_id)) throw invalid("Invalid V2 CTA confirmation.");
1278
+ }
1279
+ } else if (command.type === "bind_patient") {
1280
+ if (!((_b = (_a = __privateGet(this, _options).capabilities) == null ? void 0 : _a.supported_actions) == null ? void 0 : _b.includes("login"))) throw invalid("V2 login was not advertised.");
1281
+ if (!onlyKeys(payload, ["action_id", "tenant_id", "patient_id", "token", "run_secret", "identity_proof", "turn_id", "previous_turn_id"]) || !uuid(payload.action_id) || payload.previous_turn_id === null || payload.tenant_id !== __privateGet(this, _options).tenantId || !identity(payload.patient_id) || payload.run_secret !== __privateGet(this, _options).run.run_secret || typeof payload.token !== "string" || !payload.token || payload.token.length > 4096 || typeof payload.identity_proof !== "string" || !payload.identity_proof || payload.identity_proof.length > 2048) throw invalid("Invalid V2 binding command.");
1282
+ } else if (command.type === "action_result") {
1283
+ if (!onlyKeys(payload, ["action_id", "status", "data", "operation", "turn_id", "previous_turn_id"]) || !uuid(payload.action_id) || payload.previous_turn_id === null || !["completed", "cancelled", "failed"].includes(payload.status) || !record(payload.data)) throw invalid("Invalid V2 action command.");
1284
+ const data = payload.data;
1285
+ if ("proposal_id" in data) {
1286
+ if (!((_d = (_c = __privateGet(this, _options).capabilities) == null ? void 0 : _c.supported_actions) == null ? void 0 : _d.includes("confirm"))) throw invalid("V2 confirmation was not advertised.");
1287
+ if (__privateGet(this, _options).run.protocol !== "dbos_signin_booking_v1" || !onlyKeys(data, ["proposal_id", "approved"]) || !uuid(data.proposal_id) || ![void 0, "confirm", "status"].includes(payload.operation) || (payload.operation === "status" ? payload.status !== "completed" || data.approved !== null : typeof data.approved !== "boolean" || payload.status !== "completed" && data.approved)) throw invalid("Invalid V2 booking result.");
1288
+ } else if (!((_f = (_e = __privateGet(this, _options).capabilities) == null ? void 0 : _e.supported_actions) == null ? void 0 : _f.includes("slot_picker")) || !["dbos_signin_actions_v1", "dbos_signin_booking_v1", "dbos_signin_cta_v1"].includes(__privateGet(this, _options).run.protocol) || payload.operation !== void 0 || (payload.status === "completed" ? !onlyKeys(data, ["slot_id"]) || typeof data.slot_id !== "string" || !data.slot_id.length || data.slot_id.length > 256 : Object.keys(data).length > 0)) {
1289
+ throw invalid("Invalid V2 slot result.");
1290
+ }
1291
+ } else throw invalid("Unsupported V2 command.");
1292
+ __privateGet(this, _socket).send(serialized);
1293
+ __privateGet(this, _sentCommands).set(payload.turn_id, serialized);
1294
+ }
1295
+ };
1296
+ _options = new WeakMap();
1297
+ _socket = new WeakMap();
1298
+ _ctor = new WeakMap();
1299
+ _base = new WeakMap();
1300
+ _authenticated = new WeakMap();
1301
+ _lastTurn = new WeakMap();
1302
+ _cursor = new WeakMap();
1303
+ _sentCommands = new WeakMap();
1304
+ _generation = new WeakMap();
1305
+ _cleanup = new WeakMap();
1306
+ _cancelConnect = new WeakMap();
1307
+ _listeners = new WeakMap();
1308
+ _NativeV2WssClient_instances = new WeakSet();
1309
+ requireConnected_fn = function() {
1310
+ var _a;
1311
+ if (!__privateGet(this, _authenticated) || ((_a = __privateGet(this, _socket)) == null ? void 0 : _a.readyState) !== 1) throw invalid("V2 connection is not authenticated.");
1312
+ };
1313
+ validAuth_fn = function(value) {
1314
+ const expectedPatient = __privateGet(this, _options).identity.kind === "patient" ? __privateGet(this, _options).identity.patientId : null;
1315
+ return value.runtime === "dbos" && value.run_id === __privateGet(this, _options).run.run_id && value.protocol === __privateGet(this, _options).run.protocol && value.patient_id === expectedPatient && (value.last_turn_id === null || uuid(value.last_turn_id)) && cursor(value.last_event_seq) && value.last_event_seq >= __privateGet(this, _cursor) && Array.isArray(value.pending_actions) && value.pending_actions.every((action) => record(action) && uuid(action.action_id) && ["login", "slot_picker", "confirm"].includes(action.type) && record(action.payload)) && Array.isArray(value.agent_ids) && value.agent_ids.every(identity) && record(value.agent_names) && Object.values(value.agent_names).every((name) => typeof name === "string");
1316
+ };
1317
+ receive_fn = function(type, payload) {
1318
+ if (type === "ping") {
1319
+ __privateGet(this, _socket).send(JSON.stringify({ type: "pong" }));
1320
+ __privateMethod(this, _NativeV2WssClient_instances, dispatch_fn).call(this, "ping", payload);
1321
+ return;
1322
+ }
1323
+ if (type === "event_replay") {
1324
+ if (!Array.isArray(payload.events) || typeof payload.complete !== "boolean") return;
1325
+ const events = [];
1326
+ for (const frame of payload.events) {
1327
+ if (record(frame) && typeof frame.type === "string" && record(frame.payload) && !["auth_ok", "history", "event_replay", "ping", "close"].includes(frame.type) && cursor(frame.payload.event_seq) && frame.payload.event_seq > __privateGet(this, _cursor)) {
1328
+ events.push(frame);
1329
+ __privateMethod(this, _NativeV2WssClient_instances, receive_fn).call(this, frame.type, frame.payload);
1330
+ }
1331
+ }
1332
+ __privateMethod(this, _NativeV2WssClient_instances, dispatch_fn).call(this, "event_replay", { events, complete: payload.complete });
1333
+ return;
1334
+ }
1335
+ if (type === "turn_ack") {
1336
+ if (!uuid(payload.turn_id) || !cursor(payload.event_seq) || payload.event_seq < __privateGet(this, _cursor)) return;
1337
+ __privateSet(this, _lastTurn, payload.turn_id);
1338
+ __privateSet(this, _cursor, payload.event_seq);
1339
+ } else if (payload.event_seq !== void 0) {
1340
+ if (!cursor(payload.event_seq) || payload.event_seq <= __privateGet(this, _cursor)) return;
1341
+ __privateSet(this, _cursor, payload.event_seq);
1342
+ }
1343
+ if ([
1344
+ "history",
1345
+ "chat",
1346
+ "action_request",
1347
+ "turn_ack",
1348
+ "turn_started",
1349
+ "turn_completed",
1350
+ "activity",
1351
+ "transfer",
1352
+ "artifact_block",
1353
+ "notification",
1354
+ "response_suggestions",
1355
+ "error"
1356
+ ].includes(type)) {
1357
+ __privateMethod(this, _NativeV2WssClient_instances, dispatch_fn).call(this, type, payload);
1358
+ }
1359
+ };
1360
+ dispatch_fn = function(type, payload) {
1361
+ var _a;
1362
+ for (const listener of [...(_a = __privateGet(this, _listeners).get(type)) != null ? _a : []]) {
1363
+ try {
1364
+ listener(payload);
1365
+ } catch {
1366
+ }
1367
+ }
1368
+ };
1369
+
945
1370
  // src/HCAmsSDK.ts
946
1371
  var HCAmsSDK = class {
947
1372
  constructor(config) {
@@ -988,6 +1413,7 @@ var HCAmsSDK = class {
988
1413
  };
989
1414
  this.authHttp = new HttpClient(authState);
990
1415
  this.ams = new AmsClient(this.http);
1416
+ this.v2 = new NativeV2Client(this.http);
991
1417
  this.agents = new AgentsClient(this.http);
992
1418
  this.departments = new DepartmentsClient(this.http);
993
1419
  this.teams = new TeamsClient(this.http);
@@ -1020,6 +1446,18 @@ var HCAmsSDK = class {
1020
1446
  tenantId
1021
1447
  });
1022
1448
  }
1449
+ /** Native V2 transport. The existing connectAgentChat remains the V1 API. */
1450
+ connectV2AgentChat(options) {
1451
+ var _a, _b;
1452
+ const tenantId = (_a = options.tenantId) != null ? _a : this.state.tenantId;
1453
+ if (!tenantId) throw new Error("connectV2AgentChat requires a tenantId.");
1454
+ return new NativeV2WssClient({
1455
+ ...options,
1456
+ environment: this.state.environment,
1457
+ tenantId,
1458
+ baseUrlOverride: (_b = options.baseUrlOverride) != null ? _b : this.wssBaseUrl
1459
+ });
1460
+ }
1023
1461
  /** Get the current runtime environment. */
1024
1462
  get environment() {
1025
1463
  return this.state.environment;
@@ -1046,6 +1484,8 @@ export {
1046
1484
  DepartmentsClient,
1047
1485
  GuardrailsClient,
1048
1486
  HCAmsSDK,
1487
+ NativeV2Client,
1488
+ NativeV2WssClient,
1049
1489
  PoliciesClient,
1050
1490
  SDKError,
1051
1491
  SERVICE_NAME,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@healthcloudai/hc-ams",
3
- "version": "3.6.5",
3
+ "version": "3.7.0",
4
4
  "description": "HealthCloud AMS SDK \u2014 application-agent discovery for AMS Services.",
5
5
  "author": "Healthcheck Systems Inc",
6
6
  "license": "MIT",