@botonic/core
Version:
Runtime and APIs for Botonic bots: actions, context, messaging, and integration hooks used by the **current** framework line.
80 lines • 3.24 kB
JavaScript
/**
* v2 `PATCH …/session/` success body: stored slice only (not full BotonicSessionBuilder JSON).
* @see openspec/specs/BE-botonic-v2-botonic-session-endpoints/spec.md
*/
import { mergeWebchatClientSettings, WebchatClientSettings, } from '@botonic/shared';
function isPlainObject(v) {
return typeof v === 'object' && v !== null && !Array.isArray(v);
}
function deepMergeRecords(target, patch) {
const merged = Object.assign({}, target);
for (const [key, patchValue] of Object.entries(patch)) {
const targetValue = merged[key];
if (isPlainObject(patchValue) && isPlainObject(targetValue)) {
merged[key] = deepMergeRecords(targetValue, patchValue);
}
else {
merged[key] = patchValue;
}
}
return merged;
}
/**
* Mutates `session` with fields from the PATCH session response (slice), deep-merging object fields
* to mirror server `deep_merge` behavior for nested dicts.
*/
export function mergeBotonicSessionPatchSlice(session, slice) {
var _a, _b, _c;
if (slice.shadowing !== undefined)
session.shadowing = slice.shadowing;
if (slice.flow_thread_id !== undefined)
session.flow_thread_id = slice.flow_thread_id;
if (slice.is_first_interaction !== undefined) {
session.is_first_interaction = slice.is_first_interaction;
}
if (slice.custom !== undefined) {
session.custom = deepMergeRecords(((_a = session.custom) !== null && _a !== void 0 ? _a : {}), slice.custom);
}
if (slice.webviews_custom !== undefined) {
session.webviews_custom = deepMergeRecords(((_b = session.webviews_custom) !== null && _b !== void 0 ? _b : {}), slice.webviews_custom);
}
if (slice.webchat_client_settings !== undefined) {
const current = (_c = session.webchat_client_settings) !== null && _c !== void 0 ? _c : new WebchatClientSettings();
const incoming = slice.webchat_client_settings instanceof WebchatClientSettings
? slice.webchat_client_settings
: new WebchatClientSettings(slice.webchat_client_settings);
session.webchat_client_settings = mergeWebchatClientSettings(current, incoming);
}
if (slice.capture_user_input_node_id !== undefined) {
const id = slice.capture_user_input_node_id;
if (id === null || id === '') {
delete session.capture_user_input_node_id;
}
else {
session.capture_user_input_node_id = id;
}
}
}
export function mergeChatUserPatchResponseIntoSessionUser(user, response) {
if (response.override_name !== undefined) {
user.override_name = response.override_name;
}
if (response.locale !== undefined) {
user.locale = response.locale;
}
if (response.country !== undefined) {
user.country = response.country;
}
if (response.system_locale !== undefined) {
user.system_locale = response.system_locale;
}
if (response.extra_data !== undefined) {
if (response.extra_data === null) {
user.extra_data = undefined;
}
else {
user.extra_data = response.extra_data;
}
}
}
//# sourceMappingURL=botonic-session-patch-slice.js.map