librechat-data-provider 0.8.505 → 0.8.506

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,63 +1,18 @@
1
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
- const require_data_service = require("./data-service-CG5e1FOi.js");
2
+ const require_data_service = require("./data-service-CCdWvcRd.js");
3
3
  let zod = require("zod");
4
4
  let dayjs = require("dayjs");
5
5
  dayjs = require_data_service.__toESM(dayjs);
6
+ let dayjs_plugin_utc = require("dayjs/plugin/utc");
7
+ dayjs_plugin_utc = require_data_service.__toESM(dayjs_plugin_utc);
8
+ let dayjs_plugin_timezone = require("dayjs/plugin/timezone");
9
+ dayjs_plugin_timezone = require_data_service.__toESM(dayjs_plugin_timezone);
6
10
  let axios = require("axios");
7
11
  axios = require_data_service.__toESM(axios);
8
12
  let url = require("url");
9
13
  let crypto = require("crypto");
10
14
  crypto = require_data_service.__toESM(crypto);
11
15
  let js_yaml = require("js-yaml");
12
- //#region src/types/runs.ts
13
- let ContentTypes = /* @__PURE__ */ function(ContentTypes) {
14
- ContentTypes["TEXT"] = "text";
15
- ContentTypes["THINK"] = "think";
16
- ContentTypes["TEXT_DELTA"] = "text_delta";
17
- ContentTypes["TOOL_CALL"] = "tool_call";
18
- ContentTypes["IMAGE_FILE"] = "image_file";
19
- ContentTypes["IMAGE_URL"] = "image_url";
20
- ContentTypes["VIDEO_URL"] = "video_url";
21
- ContentTypes["INPUT_AUDIO"] = "input_audio";
22
- ContentTypes["AGENT_UPDATE"] = "agent_update";
23
- ContentTypes["SUMMARY"] = "summary";
24
- ContentTypes["ERROR"] = "error";
25
- return ContentTypes;
26
- }({});
27
- let StepTypes = /* @__PURE__ */ function(StepTypes) {
28
- StepTypes["TOOL_CALLS"] = "tool_calls";
29
- StepTypes["MESSAGE_CREATION"] = "message_creation";
30
- return StepTypes;
31
- }({});
32
- let ToolCallTypes = /* @__PURE__ */ function(ToolCallTypes) {
33
- ToolCallTypes["FUNCTION"] = "function";
34
- ToolCallTypes["RETRIEVAL"] = "retrieval";
35
- ToolCallTypes["FILE_SEARCH"] = "file_search";
36
- ToolCallTypes["CODE_INTERPRETER"] = "code_interpreter";
37
- ToolCallTypes["TOOL_CALL"] = "tool_call";
38
- return ToolCallTypes;
39
- }({});
40
- /** Event names dispatched by the agent graph and consumed by step handlers. */
41
- let StepEvents = /* @__PURE__ */ function(StepEvents) {
42
- StepEvents["ON_RUN_STEP"] = "on_run_step";
43
- StepEvents["ON_AGENT_UPDATE"] = "on_agent_update";
44
- StepEvents["ON_MESSAGE_DELTA"] = "on_message_delta";
45
- StepEvents["ON_REASONING_DELTA"] = "on_reasoning_delta";
46
- StepEvents["ON_RUN_STEP_DELTA"] = "on_run_step_delta";
47
- StepEvents["ON_RUN_STEP_COMPLETED"] = "on_run_step_completed";
48
- StepEvents["ON_SUMMARIZE_START"] = "on_summarize_start";
49
- StepEvents["ON_SUMMARIZE_DELTA"] = "on_summarize_delta";
50
- StepEvents["ON_SUMMARIZE_COMPLETE"] = "on_summarize_complete";
51
- StepEvents["ON_SUBAGENT_UPDATE"] = "on_subagent_update";
52
- return StepEvents;
53
- }({});
54
- /** Token-tracking event names streamed to the client (separate from StepEvents dispatch). */
55
- let UsageEvents = /* @__PURE__ */ function(UsageEvents) {
56
- UsageEvents["ON_CONTEXT_USAGE"] = "on_context_usage";
57
- UsageEvents["ON_TOKEN_USAGE"] = "on_token_usage";
58
- return UsageEvents;
59
- }({});
60
- //#endregion
61
16
  //#region src/bedrock.ts
62
17
  const DEFAULT_ENABLED_MAX_TOKENS = 8192;
63
18
  const DEFAULT_THINKING_BUDGET = 2e3;
@@ -217,6 +172,7 @@ const bedrockInputSchema = require_data_service.tConversationSchema.pick({
217
172
  thinkingDisplay: true,
218
173
  reasoning_effort: true,
219
174
  promptCache: true,
175
+ promptCacheTtl: true,
220
176
  topK: true,
221
177
  additionalModelRequestFields: true
222
178
  }).transform((obj) => {
@@ -255,6 +211,7 @@ const bedrockInputParser = require_data_service.tConversationSchema.pick({
255
211
  thinkingDisplay: true,
256
212
  reasoning_effort: true,
257
213
  promptCache: true,
214
+ promptCacheTtl: true,
258
215
  topK: true,
259
216
  additionalModelRequestFields: true
260
217
  }).catchall(zod.z.any()).transform((data) => {
@@ -275,7 +232,8 @@ const bedrockInputParser = require_data_service.tConversationSchema.pick({
275
232
  "temperature",
276
233
  "topP",
277
234
  "stop",
278
- "promptCache"
235
+ "promptCache",
236
+ "promptCacheTtl"
279
237
  ];
280
238
  const additionalFields = {};
281
239
  const typedData = data;
@@ -381,6 +339,12 @@ const bedrockInputParser = require_data_service.tConversationSchema.pick({
381
339
  if (typeof typedData.model === "string" && (typedData.model.includes("claude") || typedData.model.includes("nova"))) {
382
340
  if (typedData.promptCache === void 0) typedData.promptCache = true;
383
341
  } else if (typedData.promptCache === true) typedData.promptCache = void 0;
342
+ /**
343
+ * A cache TTL is meaningless without caching — tie it to promptCache. When
344
+ * caching is off or unsupported for the model (cleared above), drop the TTL
345
+ * so an unsupported `1h` is never sent on a non-caching Bedrock request.
346
+ */
347
+ if (typedData.promptCache !== true) typedData.promptCacheTtl = void 0;
384
348
  if (Object.keys(additionalFields).length > 0) typedData.additionalModelRequestFields = {
385
349
  ...typedData.additionalModelRequestFields || {},
386
350
  ...additionalFields
@@ -438,7 +402,100 @@ const bedrockOutputParser = (data) => {
438
402
  return result;
439
403
  };
440
404
  //#endregion
405
+ //#region src/types/runs.ts
406
+ let ContentTypes = /* @__PURE__ */ function(ContentTypes) {
407
+ ContentTypes["TEXT"] = "text";
408
+ ContentTypes["THINK"] = "think";
409
+ ContentTypes["TEXT_DELTA"] = "text_delta";
410
+ ContentTypes["TOOL_CALL"] = "tool_call";
411
+ ContentTypes["IMAGE_FILE"] = "image_file";
412
+ ContentTypes["IMAGE_URL"] = "image_url";
413
+ ContentTypes["VIDEO_URL"] = "video_url";
414
+ ContentTypes["INPUT_AUDIO"] = "input_audio";
415
+ ContentTypes["AGENT_UPDATE"] = "agent_update";
416
+ ContentTypes["SUMMARY"] = "summary";
417
+ ContentTypes["ERROR"] = "error";
418
+ return ContentTypes;
419
+ }({});
420
+ let StepTypes = /* @__PURE__ */ function(StepTypes) {
421
+ StepTypes["TOOL_CALLS"] = "tool_calls";
422
+ StepTypes["MESSAGE_CREATION"] = "message_creation";
423
+ return StepTypes;
424
+ }({});
425
+ let ToolCallTypes = /* @__PURE__ */ function(ToolCallTypes) {
426
+ ToolCallTypes["FUNCTION"] = "function";
427
+ ToolCallTypes["RETRIEVAL"] = "retrieval";
428
+ ToolCallTypes["FILE_SEARCH"] = "file_search";
429
+ ToolCallTypes["CODE_INTERPRETER"] = "code_interpreter";
430
+ ToolCallTypes["TOOL_CALL"] = "tool_call";
431
+ return ToolCallTypes;
432
+ }({});
433
+ /** Event names dispatched by the agent graph and consumed by step handlers. */
434
+ let StepEvents = /* @__PURE__ */ function(StepEvents) {
435
+ StepEvents["ON_RUN_STEP"] = "on_run_step";
436
+ StepEvents["ON_AGENT_UPDATE"] = "on_agent_update";
437
+ StepEvents["ON_MESSAGE_DELTA"] = "on_message_delta";
438
+ StepEvents["ON_REASONING_DELTA"] = "on_reasoning_delta";
439
+ StepEvents["ON_RUN_STEP_DELTA"] = "on_run_step_delta";
440
+ StepEvents["ON_RUN_STEP_COMPLETED"] = "on_run_step_completed";
441
+ StepEvents["ON_SUMMARIZE_START"] = "on_summarize_start";
442
+ StepEvents["ON_SUMMARIZE_DELTA"] = "on_summarize_delta";
443
+ StepEvents["ON_SUMMARIZE_COMPLETE"] = "on_summarize_complete";
444
+ StepEvents["ON_SUBAGENT_UPDATE"] = "on_subagent_update";
445
+ return StepEvents;
446
+ }({});
447
+ /** Token-tracking event names streamed to the client (separate from StepEvents dispatch). */
448
+ let UsageEvents = /* @__PURE__ */ function(UsageEvents) {
449
+ UsageEvents["ON_CONTEXT_USAGE"] = "on_context_usage";
450
+ UsageEvents["ON_TOKEN_USAGE"] = "on_token_usage";
451
+ return UsageEvents;
452
+ }({});
453
+ /**
454
+ * Full prompt token count for one completed model call — the EXACT context the
455
+ * model saw, provider-aware: additive providers (Bedrock) report `input_tokens`
456
+ * excluding cache, so cache reads/writes are added back; subset providers
457
+ * (Anthropic, OpenAI, …) already fold cache into `input_tokens`. When the
458
+ * provider is absent (custom/OpenAI-compatible payloads), fall back to the same
459
+ * magnitude heuristic `normalizeUsageUnits` uses — cache ≤ input means it's
460
+ * already included — so cached events aren't re-inflated. The ground truth the
461
+ * gauge reconciles its calibrated estimate to.
462
+ */
463
+ const promptTokensFromUsage = (event) => {
464
+ const input = event.input_tokens ?? 0;
465
+ const details = event.input_token_details ?? {};
466
+ const cacheRead = details.cache_read ?? 0;
467
+ const cacheCreation = details.cache_creation ?? 0;
468
+ return (event.provider != null ? require_data_service.inputTokensIncludesCache(event.provider) : cacheRead + cacheCreation <= input) ? input : input + cacheRead + cacheCreation;
469
+ };
470
+ /**
471
+ * Reconciles a pre-invoke context snapshot's CALIBRATED estimate to a call's
472
+ * ACTUAL prompt tokens. The SDK's calibration multiplier scales only
473
+ * `messageTokens` (instructions/summary are raw tiktoken counts), and it can
474
+ * over-shoot badly when a provider injects server-side content the SDK never
475
+ * counted (e.g. Anthropic web search) — pinning the gauge several× too high and
476
+ * persisting it. Trust the provider's own prompt count: keep the raw
477
+ * instruction/summary rows, set `messageTokens` to the remainder, and recompute
478
+ * the free space. No-op when `promptTokens` is unusable.
479
+ */
480
+ const reconcileContextUsage = (snapshot, promptTokens) => {
481
+ if (!Number.isFinite(promptTokens) || promptTokens <= 0) return snapshot;
482
+ const { breakdown } = snapshot;
483
+ const budget = snapshot.contextBudget ?? breakdown.maxContextTokens;
484
+ const nonMessageTokens = (breakdown.instructionTokens ?? 0) + (breakdown.summaryTokens ?? 0);
485
+ const messageTokens = Math.max(0, promptTokens - nonMessageTokens);
486
+ return {
487
+ ...snapshot,
488
+ breakdown: {
489
+ ...breakdown,
490
+ messageTokens
491
+ },
492
+ remainingContextTokens: budget != null ? Math.max(0, budget - promptTokens) : snapshot.remainingContextTokens
493
+ };
494
+ };
495
+ //#endregion
441
496
  //#region src/parsers.ts
497
+ dayjs.default.extend(dayjs_plugin_utc.default);
498
+ dayjs.default.extend(dayjs_plugin_timezone.default);
442
499
  const endpointSchemas = {
443
500
  ["openAI"]: require_data_service.openAISchema,
444
501
  ["azureOpenAI"]: require_data_service.openAISchema,
@@ -637,10 +694,24 @@ function findLastSeparatorIndex(text, separators = SEPARATORS) {
637
694
  }
638
695
  return lastIndex;
639
696
  }
640
- function replaceSpecialVars({ text, user, now: inputNow }) {
697
+ /**
698
+ * Anchors a dayjs instant to the user's IANA timezone when one is supplied,
699
+ * so local-time special vars reflect the user's wall clock rather than the
700
+ * server's. Falls back to the original instant for missing or invalid zones.
701
+ */
702
+ function applyTimezone(value, timezone) {
703
+ if (!timezone) return value;
704
+ try {
705
+ const zoned = value.tz(timezone);
706
+ return zoned.isValid() ? zoned : value;
707
+ } catch {
708
+ return value;
709
+ }
710
+ }
711
+ function replaceSpecialVars({ text, user, now: inputNow, timezone }) {
641
712
  let result = text;
642
713
  if (!result) return result;
643
- const now = inputNow != null ? (0, dayjs.default)(inputNow) : (0, dayjs.default)();
714
+ const now = applyTimezone(inputNow != null ? (0, dayjs.default)(inputNow) : (0, dayjs.default)(), timezone);
644
715
  const weekdayName = now.format("dddd");
645
716
  const currentDate = now.format("YYYY-MM-DD");
646
717
  result = result.replace(/{{\s*current_date\s*}}/gi, `${currentDate} (${weekdayName})`);
@@ -4992,6 +5063,14 @@ function validateAndParseOpenAPISpec(specString) {
4992
5063
  }
4993
5064
  //#endregion
4994
5065
  //#region src/createPayload.ts
5066
+ /** Resolves the browser's IANA timezone so the server can localize prompt variables. */
5067
+ function getUserTimezone() {
5068
+ try {
5069
+ return Intl.DateTimeFormat().resolvedOptions().timeZone || void 0;
5070
+ } catch {
5071
+ return;
5072
+ }
5073
+ }
4995
5074
  function createPayload(submission) {
4996
5075
  const { isEdited, addedConvo, userMessage, isContinued, isTemporary, isRegenerate, conversation, editedContent, ephemeralAgent, endpointOption, manualSkills } = submission;
4997
5076
  const { conversationId } = require_data_service.tConvoUpdateSchema.parse(conversation);
@@ -5010,7 +5089,8 @@ function createPayload(submission) {
5010
5089
  conversationId,
5011
5090
  isContinued: !!(isEdited && isContinued),
5012
5091
  ephemeralAgent: require_data_service.isAssistantsEndpoint(endpoint) ? void 0 : ephemeralAgent,
5013
- manualSkills: require_data_service.isAssistantsEndpoint(endpoint) ? void 0 : manualSkills
5092
+ manualSkills: require_data_service.isAssistantsEndpoint(endpoint) ? void 0 : manualSkills,
5093
+ timezone: getUserTimezone()
5014
5094
  };
5015
5095
  return {
5016
5096
  server,
@@ -5414,6 +5494,22 @@ const anthropic = {
5414
5494
  showDefault: false,
5415
5495
  columnSpan: 2
5416
5496
  },
5497
+ promptCacheTtl: {
5498
+ key: "promptCacheTtl",
5499
+ label: "com_endpoint_prompt_cache_ttl",
5500
+ labelCode: true,
5501
+ description: "com_endpoint_anthropic_prompt_cache_ttl",
5502
+ descriptionCode: true,
5503
+ type: "enum",
5504
+ default: require_data_service.anthropicSettings.promptCacheTtl.default,
5505
+ options: ["5m", "1h"],
5506
+ component: "dropdown",
5507
+ optionType: "conversation",
5508
+ showDefault: false,
5509
+ placeholder: "com_endpoint_prompt_cache_ttl_default",
5510
+ placeholderCode: true,
5511
+ columnSpan: 2
5512
+ },
5417
5513
  thinking: {
5418
5514
  key: "thinking",
5419
5515
  label: "com_endpoint_thinking",
@@ -5569,6 +5665,22 @@ const bedrock = {
5569
5665
  showDefault: false,
5570
5666
  columnSpan: 2
5571
5667
  },
5668
+ promptCacheTtl: {
5669
+ key: "promptCacheTtl",
5670
+ label: "com_endpoint_prompt_cache_ttl",
5671
+ labelCode: true,
5672
+ description: "com_endpoint_anthropic_prompt_cache_ttl",
5673
+ descriptionCode: true,
5674
+ type: "enum",
5675
+ default: void 0,
5676
+ options: ["5m", "1h"],
5677
+ component: "dropdown",
5678
+ optionType: "conversation",
5679
+ showDefault: false,
5680
+ placeholder: "com_endpoint_prompt_cache_ttl_default",
5681
+ placeholderCode: true,
5682
+ columnSpan: 2
5683
+ },
5572
5684
  reasoning_effort: {
5573
5685
  key: "reasoning_effort",
5574
5686
  label: "com_endpoint_reasoning_effort",
@@ -5822,7 +5934,11 @@ const openAI = [
5822
5934
  openAIParams.disableStreaming,
5823
5935
  librechat.fileTokenLimit
5824
5936
  ];
5825
- const openRouter = [...openAI, anthropic.promptCache];
5937
+ const openRouter = [
5938
+ ...openAI,
5939
+ anthropic.promptCache,
5940
+ anthropic.promptCacheTtl
5941
+ ];
5826
5942
  const openAICol1 = [
5827
5943
  baseDefinitions.model,
5828
5944
  librechat.modelLabel,
@@ -5856,6 +5972,7 @@ const anthropicConfig = [
5856
5972
  anthropic.topK,
5857
5973
  librechat.resendFiles,
5858
5974
  anthropic.promptCache,
5975
+ anthropic.promptCacheTtl,
5859
5976
  anthropic.thinking,
5860
5977
  anthropic.thinkingBudget,
5861
5978
  anthropic.effort,
@@ -5876,6 +5993,7 @@ const anthropicCol2 = [
5876
5993
  anthropic.topK,
5877
5994
  librechat.resendFiles,
5878
5995
  anthropic.promptCache,
5996
+ anthropic.promptCacheTtl,
5879
5997
  anthropic.thinking,
5880
5998
  anthropic.thinkingBudget,
5881
5999
  anthropic.effort,
@@ -5895,6 +6013,7 @@ const bedrockAnthropic = [
5895
6013
  librechat.resendFiles,
5896
6014
  bedrock.region,
5897
6015
  bedrock.promptCache,
6016
+ bedrock.promptCacheTtl,
5898
6017
  anthropic.thinking,
5899
6018
  anthropic.thinkingBudget,
5900
6019
  anthropic.effort,
@@ -5932,6 +6051,7 @@ const bedrockGeneral = [
5932
6051
  librechat.resendFiles,
5933
6052
  bedrock.region,
5934
6053
  bedrock.promptCache,
6054
+ bedrock.promptCacheTtl,
5935
6055
  librechat.fileTokenLimit
5936
6056
  ];
5937
6057
  const bedrockAnthropicCol1 = [
@@ -5949,6 +6069,7 @@ const bedrockAnthropicCol2 = [
5949
6069
  librechat.resendFiles,
5950
6070
  bedrock.region,
5951
6071
  bedrock.promptCache,
6072
+ bedrock.promptCacheTtl,
5952
6073
  anthropic.thinking,
5953
6074
  anthropic.thinkingBudget,
5954
6075
  anthropic.effort,
@@ -5995,6 +6116,7 @@ const bedrockGeneralCol2 = [
5995
6116
  librechat.resendFiles,
5996
6117
  bedrock.region,
5997
6118
  bedrock.promptCache,
6119
+ bedrock.promptCacheTtl,
5998
6120
  librechat.fileTokenLimit
5999
6121
  ];
6000
6122
  const bedrockZAI = [
@@ -6084,7 +6206,11 @@ const presetSettings = {
6084
6206
  ["custom"]: openAIColumns,
6085
6207
  ["openrouter"]: {
6086
6208
  col1: openAICol1,
6087
- col2: [...openAICol2, anthropic.promptCache]
6209
+ col2: [
6210
+ ...openAICol2,
6211
+ anthropic.promptCache,
6212
+ anthropic.promptCacheTtl
6213
+ ]
6088
6214
  },
6089
6215
  ["anthropic"]: {
6090
6216
  col1: anthropicCol1,
@@ -6515,9 +6641,11 @@ exports.presetSettings = presetSettings;
6515
6641
  exports.principalSchema = require_data_service.principalSchema;
6516
6642
  exports.progress = progress;
6517
6643
  exports.promptPermissionsSchema = promptPermissionsSchema;
6644
+ exports.promptTokensFromUsage = promptTokensFromUsage;
6518
6645
  exports.providerEndpointMap = require_data_service.providerEndpointMap;
6519
6646
  exports.radioGroup = radioGroup;
6520
6647
  exports.rateLimitSchema = require_data_service.rateLimitSchema;
6648
+ exports.reconcileContextUsage = reconcileContextUsage;
6521
6649
  exports.registerPage = require_data_service.registerPage;
6522
6650
  exports.remoteAgentsPermissionsSchema = remoteAgentsPermissionsSchema;
6523
6651
  exports.removeNullishValues = require_data_service.removeNullishValues;