pika-shared 1.3.0 → 1.4.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.
Files changed (35) hide show
  1. package/dist/types/chatbot/chatbot-types.d.mts +446 -14
  2. package/dist/types/chatbot/chatbot-types.d.ts +446 -14
  3. package/dist/types/chatbot/chatbot-types.js +4 -2
  4. package/dist/types/chatbot/chatbot-types.js.map +1 -1
  5. package/dist/types/chatbot/chatbot-types.mjs +4 -2
  6. package/dist/types/chatbot/chatbot-types.mjs.map +1 -1
  7. package/dist/util/api-gateway-utils.js +14 -1
  8. package/dist/util/api-gateway-utils.js.map +1 -1
  9. package/dist/util/api-gateway-utils.mjs +14 -1
  10. package/dist/util/api-gateway-utils.mjs.map +1 -1
  11. package/dist/util/bad-request-error.d.mts +7 -0
  12. package/dist/util/bad-request-error.d.ts +7 -0
  13. package/dist/util/bad-request-error.js +20 -0
  14. package/dist/util/bad-request-error.js.map +1 -0
  15. package/dist/util/bad-request-error.mjs +18 -0
  16. package/dist/util/bad-request-error.mjs.map +1 -0
  17. package/dist/util/forbidden-error.d.mts +7 -0
  18. package/dist/util/forbidden-error.d.ts +7 -0
  19. package/dist/util/forbidden-error.js +20 -0
  20. package/dist/util/forbidden-error.js.map +1 -0
  21. package/dist/util/forbidden-error.mjs +18 -0
  22. package/dist/util/forbidden-error.mjs.map +1 -0
  23. package/dist/util/server-utils.d.mts +41 -1
  24. package/dist/util/server-utils.d.ts +41 -1
  25. package/dist/util/server-utils.js +303 -0
  26. package/dist/util/server-utils.js.map +1 -1
  27. package/dist/util/server-utils.mjs +301 -1
  28. package/dist/util/server-utils.mjs.map +1 -1
  29. package/dist/util/unauthorized-error.d.mts +7 -0
  30. package/dist/util/unauthorized-error.d.ts +7 -0
  31. package/dist/util/unauthorized-error.js +20 -0
  32. package/dist/util/unauthorized-error.js.map +1 -0
  33. package/dist/util/unauthorized-error.mjs +18 -0
  34. package/dist/util/unauthorized-error.mjs.map +1 -0
  35. package/package.json +1 -1
@@ -1,5 +1,11 @@
1
1
  import { gunzipSync, gzipSync } from 'zlib';
2
2
 
3
+ // src/util/server-utils.ts
4
+
5
+ // src/types/chatbot/chatbot-types.ts
6
+ var DEFAULT_MAX_MEMORY_RECORDS_PER_PROMPT = 25;
7
+ var DEFAULT_MAX_K_MATCHES_PER_STRATEGY = 5;
8
+
3
9
  // src/util/server-utils.ts
4
10
  function gunzipBase64EncodedString(base64EncodedString) {
5
11
  const gzippedHexEncodedString = Buffer.from(base64EncodedString, "base64").toString("hex");
@@ -11,7 +17,301 @@ function gzipAndBase64EncodeString(string) {
11
17
  const gzippedBase64EncodedString = Buffer.from(gzippedHexEncodedString, "hex").toString("base64");
12
18
  return gzippedBase64EncodedString;
13
19
  }
20
+ function getOverridableFeatures(siteFeatures, chatApp, user) {
21
+ const result = {
22
+ entity: {
23
+ enabled: false
24
+ },
25
+ verifyResponse: {
26
+ enabled: false
27
+ },
28
+ traces: {
29
+ enabled: false,
30
+ detailedTraces: false
31
+ },
32
+ fileUpload: {
33
+ mimeTypesAllowed: []
34
+ },
35
+ suggestions: {
36
+ suggestions: [],
37
+ randomize: false,
38
+ randomizeAfter: 0,
39
+ maxToShow: 5
40
+ },
41
+ promptInputFieldLabel: {
42
+ label: void 0
43
+ },
44
+ uiCustomization: {
45
+ showUserRegionInLeftNav: false,
46
+ showChatHistoryInStandaloneMode: false
47
+ },
48
+ chatDisclaimerNotice: void 0,
49
+ logout: {
50
+ enabled: false,
51
+ menuItemTitle: "Logout",
52
+ dialogTitle: "Logout",
53
+ dialogDescription: "Are you sure you want to logout?"
54
+ },
55
+ siteAdmin: {
56
+ websiteEnabled: false
57
+ },
58
+ tags: {
59
+ tagsEnabled: []
60
+ },
61
+ agentInstructionAssistance: {
62
+ enabled: false,
63
+ includeOutputFormattingRequirements: false,
64
+ includeInstructionsForTags: false,
65
+ completeExampleInstructionEnabled: false,
66
+ completeExampleInstructionLine: void 0,
67
+ jsonOnlyImperativeInstructionEnabled: false,
68
+ jsonOnlyImperativeInstructionLine: void 0
69
+ },
70
+ instructionAugmentation: {
71
+ enabled: false,
72
+ type: void 0
73
+ },
74
+ userMemory: {
75
+ enabled: false,
76
+ maxMemoryRecordsPerPrompt: DEFAULT_MAX_MEMORY_RECORDS_PER_PROMPT,
77
+ maxKMatchesPerStrategy: DEFAULT_MAX_K_MATCHES_PER_STRATEGY
78
+ }
79
+ };
80
+ const effectiveVerifyResponseFeature = chatApp.override?.features?.verifyResponse || chatApp.features?.verifyResponse;
81
+ result.verifyResponse = handleAccessRuleFeature(
82
+ "verifyResponse",
83
+ effectiveVerifyResponseFeature,
84
+ siteFeatures?.verifyResponse,
85
+ result.verifyResponse,
86
+ user,
87
+ (feature, enabled) => ({
88
+ enabled,
89
+ autoRepromptThreshold: feature.autoRepromptThreshold
90
+ })
91
+ );
92
+ const effectiveTracesFeature = chatApp.override?.features?.traces || chatApp.features?.traces;
93
+ result.traces = handleAccessRuleFeature("traces", effectiveTracesFeature, siteFeatures?.traces, result.traces, user, (feature, enabled) => {
94
+ let detailedTraces = false;
95
+ if (enabled && feature.detailedTraces) {
96
+ const siteDetailedTracesRule = siteFeatures?.traces?.detailedTraces || { enabled: false };
97
+ if (siteDetailedTracesRule.enabled) {
98
+ detailedTraces = checkUserAccessToFeature(user, feature.detailedTraces);
99
+ }
100
+ } else if (enabled) {
101
+ const siteDetailedTracesRule = siteFeatures?.traces?.detailedTraces || { enabled: false };
102
+ detailedTraces = checkUserAccessToFeature(user, siteDetailedTracesRule);
103
+ }
104
+ return {
105
+ enabled,
106
+ detailedTraces
107
+ };
108
+ });
109
+ const effectiveLogoutFeature = chatApp.override?.features?.logout || chatApp.features?.logout;
110
+ result.logout = handleAccessRuleFeature("logout", effectiveLogoutFeature, siteFeatures?.logout, result.logout, user, (feature, enabled) => ({
111
+ enabled,
112
+ menuItemTitle: feature.menuItemTitle ?? "Logout",
113
+ dialogTitle: feature.dialogTitle ?? "Logout",
114
+ dialogDescription: feature.dialogDescription ?? "Are you sure you want to logout?"
115
+ }));
116
+ const effectiveFileUploadFeature = chatApp.override?.features?.fileUpload || chatApp.features?.fileUpload;
117
+ result.fileUpload = handleSimpleFeature("fileUpload", effectiveFileUploadFeature, siteFeatures?.fileUpload, result.fileUpload, (feature) => ({
118
+ mimeTypesAllowed: feature.mimeTypesAllowed || []
119
+ }));
120
+ const effectiveSuggestionsFeature = chatApp.override?.features?.suggestions || chatApp.features?.suggestions;
121
+ result.suggestions = handleSimpleFeature("suggestions", effectiveSuggestionsFeature, siteFeatures?.suggestions, result.suggestions, (feature) => ({
122
+ suggestions: feature.suggestions || [],
123
+ randomize: feature.randomize ?? false,
124
+ randomizeAfter: feature.randomizeAfter ?? 0,
125
+ maxToShow: feature.maxToShow ?? 5
126
+ }));
127
+ const effectivePromptInputFieldLabelFeature = chatApp.override?.features?.promptInputFieldLabel || chatApp.features?.promptInputFieldLabel;
128
+ result.promptInputFieldLabel = handleEnabledOnlyFeature(
129
+ "promptInputFieldLabel",
130
+ effectivePromptInputFieldLabelFeature,
131
+ siteFeatures?.promptInputFieldLabel,
132
+ { },
133
+ // Default return shape
134
+ (feature, enabled) => ({
135
+ label: enabled ? feature.promptInputFieldLabel ?? "Ready to chat" : void 0
136
+ })
137
+ );
138
+ const effectiveUiCustomizationFeature = chatApp.override?.features?.uiCustomization || chatApp.features?.uiCustomization;
139
+ result.uiCustomization = handleSimpleFeature("uiCustomization", effectiveUiCustomizationFeature, siteFeatures?.uiCustomization, result.uiCustomization, (feature) => ({
140
+ showUserRegionInLeftNav: feature.showUserRegionInLeftNav ?? false,
141
+ showChatHistoryInStandaloneMode: feature.showChatHistoryInStandaloneMode ?? false
142
+ }));
143
+ const effectiveChatDisclaimerNoticeFeature = chatApp.override?.features?.chatDisclaimerNotice || chatApp.features?.chatDisclaimerNotice;
144
+ const disclaimerResult = handleEnabledOnlyFeature(
145
+ "chatDisclaimerNotice",
146
+ effectiveChatDisclaimerNoticeFeature,
147
+ siteFeatures?.chatDisclaimerNotice,
148
+ { },
149
+ (feature, enabled) => ({
150
+ notice: enabled ? feature.notice : void 0
151
+ })
152
+ );
153
+ result.chatDisclaimerNotice = disclaimerResult.notice;
154
+ const effectiveTagsFeature = chatApp.override?.features?.tags || chatApp.features?.tags;
155
+ result.tags = handleSimpleFeature("tags", effectiveTagsFeature, siteFeatures?.tags, result.tags, (feature) => ({
156
+ tagsEnabled: feature.tagsEnabled ?? []
157
+ }));
158
+ const effectiveAgentInstructionAssistanceFeature = chatApp.override?.features?.agentInstructionAssistance || chatApp.features?.agentInstructionAssistance;
159
+ result.agentInstructionAssistance = handleSimpleFeature(
160
+ "agentInstructionAssistance",
161
+ effectiveAgentInstructionAssistanceFeature,
162
+ siteFeatures?.agentInstructionAssistance,
163
+ result.agentInstructionAssistance,
164
+ (feature) => ({
165
+ enabled: feature.enabled ?? false,
166
+ includeOutputFormattingRequirements: feature.includeOutputFormattingRequirements?.enabled ?? false,
167
+ includeInstructionsForTags: feature.includeInstructionsForTags?.enabled ?? false,
168
+ completeExampleInstructionEnabled: feature.completeExampleInstructionLine?.enabled ?? false,
169
+ completeExampleInstructionLine: feature.completeExampleInstructionLine?.mdLine ?? void 0,
170
+ jsonOnlyImperativeInstructionEnabled: feature.jsonOnlyImperativeInstructionLine?.enabled ?? false,
171
+ jsonOnlyImperativeInstructionLine: feature.jsonOnlyImperativeInstructionLine?.line ?? void 0
172
+ })
173
+ );
174
+ const effectiveInstructionAugmentationFeature = chatApp.override?.features?.instructionAugmentation || chatApp.features?.instructionAugmentation;
175
+ result.instructionAugmentation = handleSimpleFeature(
176
+ "instructionAugmentation",
177
+ effectiveInstructionAugmentationFeature,
178
+ siteFeatures?.instructionAugmentation,
179
+ result.instructionAugmentation,
180
+ (feature) => ({
181
+ enabled: feature.enabled ?? false,
182
+ type: feature.type ?? "llm-semantic-directive-search"
183
+ })
184
+ );
185
+ const effectiveUserMemoryFeature = chatApp.override?.features?.userMemory || chatApp.features?.userMemory;
186
+ result.userMemory = handleSimpleFeature("userMemory", effectiveUserMemoryFeature, siteFeatures?.userMemory, result.userMemory, (feature) => ({
187
+ enabled: feature.enabled ?? false,
188
+ maxMemoryRecordsPerPrompt: feature.maxMemoryRecordsPerPrompt ?? DEFAULT_MAX_MEMORY_RECORDS_PER_PROMPT,
189
+ maxKMatchesPerStrategy: feature.maxKMatchesPerStrategy ?? DEFAULT_MAX_K_MATCHES_PER_STRATEGY
190
+ }));
191
+ const effectiveEntityFeature = chatApp.override?.features?.entity || chatApp.features?.entity;
192
+ if (siteFeatures?.entity?.enabled) {
193
+ if (effectiveEntityFeature?.enabled === false) {
194
+ result.entity.enabled = false;
195
+ } else {
196
+ result.entity.enabled = true;
197
+ if (!siteFeatures?.entity?.attributeName) {
198
+ throw new Error("Entity feature is enabled at the site level but the site features entity object does not have an attributeName set");
199
+ }
200
+ result.entity.attributeName = siteFeatures.entity.attributeName;
201
+ }
202
+ } else {
203
+ result.entity.enabled = false;
204
+ }
205
+ return result;
206
+ }
207
+ function handleSimpleFeature(featureName, appFeature, siteFeature, defaults, propertyExtractor) {
208
+ if (isSimpleFeatureOverrideValid(appFeature, featureName) && appFeature) {
209
+ return propertyExtractor(appFeature);
210
+ } else {
211
+ const siteRule = siteFeature || defaults;
212
+ return propertyExtractor(siteRule);
213
+ }
214
+ }
215
+ function handleEnabledOnlyFeature(featureName, appFeature, siteFeature, defaults, propertyExtractor) {
216
+ const overrideStatus = isFeatureOverrideValid(appFeature, featureName);
217
+ if (overrideStatus === "enabled" && appFeature) {
218
+ const siteRule = siteFeature || { enabled: false };
219
+ if (siteRule.enabled) {
220
+ return propertyExtractor(appFeature, true);
221
+ }
222
+ return propertyExtractor({}, false);
223
+ } else if (overrideStatus === "disabled") {
224
+ return propertyExtractor({}, false);
225
+ } else {
226
+ const siteRule = siteFeature || { enabled: true };
227
+ return propertyExtractor(siteRule, siteRule.enabled);
228
+ }
229
+ }
230
+ function isFeatureOverrideValid(appFeature, featureName) {
231
+ if (!appFeature) return "none";
232
+ if (Object.keys(appFeature).length === 0) {
233
+ console.error(`Invalid empty feature override for ${featureName}. Falling back to site level.`);
234
+ return "invalid";
235
+ }
236
+ if (!("enabled" in appFeature)) {
237
+ console.error(`Invalid feature override for ${featureName}: missing 'enabled' property. Falling back to site level.`);
238
+ return "invalid";
239
+ }
240
+ return appFeature.enabled ? "enabled" : "disabled";
241
+ }
242
+ function isSimpleFeatureOverrideValid(appFeature, featureName) {
243
+ if (!appFeature) return false;
244
+ if (Object.keys(appFeature).length === 0) {
245
+ console.error(`Invalid empty feature override for ${featureName}. Falling back to site level.`);
246
+ return false;
247
+ }
248
+ return true;
249
+ }
250
+ function handleAccessRuleFeature(featureName, appFeature, siteFeature, defaults, user, propertyExtractor) {
251
+ const overrideStatus = isFeatureOverrideValid(appFeature, featureName);
252
+ if (overrideStatus === "enabled" && appFeature) {
253
+ const siteRule = siteFeature || { enabled: false };
254
+ if (siteRule.enabled) {
255
+ const enabled = checkUserAccessToFeature(user, appFeature);
256
+ return propertyExtractor(appFeature, enabled);
257
+ }
258
+ return propertyExtractor(defaults, false);
259
+ } else if (overrideStatus === "disabled") {
260
+ return propertyExtractor(defaults, false);
261
+ } else {
262
+ const siteRule = siteFeature || { enabled: false };
263
+ const enabled = checkUserAccessToFeature(user, siteRule);
264
+ return propertyExtractor(siteRule, enabled);
265
+ }
266
+ }
267
+ function checkUserAccessToFeature(user, feature) {
268
+ let { enabled, userTypes, userRoles, applyRulesAs = "and" } = feature;
269
+ if (userTypes && userTypes.length > 0 && userRoles && userRoles.length === 0) {
270
+ userRoles = void 0;
271
+ }
272
+ if (userRoles && userRoles.length > 0 && (!userTypes || userTypes.length === 0)) {
273
+ userTypes = void 0;
274
+ }
275
+ if (!enabled) {
276
+ return false;
277
+ }
278
+ if (!userTypes && !userRoles) {
279
+ return false;
280
+ }
281
+ const userTypeMatches = userTypes ? userTypes.includes(user.userType ?? "external-user") : true;
282
+ const userRoleMatches = userRoles ? (user.roles ?? []).some((role) => userRoles.includes(role)) : true;
283
+ if (applyRulesAs === "and") {
284
+ return userTypeMatches && userRoleMatches;
285
+ } else {
286
+ return userTypeMatches || userRoleMatches;
287
+ }
288
+ }
289
+ function getEntityIdForUser(user, overrideDataForThisChatApp, entityAttributeName) {
290
+ let customUserData = overrideDataForThisChatApp || user.customData;
291
+ if (!entityAttributeName || !customUserData) {
292
+ return void 0;
293
+ }
294
+ let currentObject = customUserData;
295
+ const attributeParts = entityAttributeName.split(".");
296
+ let currentValue;
297
+ for (let i = 0; i < attributeParts.length; i++) {
298
+ const part = attributeParts[i];
299
+ if (!(part in currentObject)) {
300
+ return void 0;
301
+ }
302
+ if (i === attributeParts.length - 1) {
303
+ currentValue = currentObject[part];
304
+ } else {
305
+ if (typeof currentObject[part] === "object" && currentObject[part] !== null) {
306
+ currentObject = currentObject[part];
307
+ } else {
308
+ return void 0;
309
+ }
310
+ }
311
+ }
312
+ return currentValue;
313
+ }
14
314
 
15
- export { gunzipBase64EncodedString, gzipAndBase64EncodeString };
315
+ export { checkUserAccessToFeature, getEntityIdForUser, getOverridableFeatures, gunzipBase64EncodedString, gzipAndBase64EncodeString };
16
316
  //# sourceMappingURL=server-utils.mjs.map
17
317
  //# sourceMappingURL=server-utils.mjs.map