@librechat/agents 4.2.0 → 4.2.2
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/cjs/graphs/Graph.cjs +41 -3
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/langfuse.cjs +11 -2
- package/dist/cjs/langfuse.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +51 -16
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +3 -1
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +41 -3
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/langfuse.mjs +11 -2
- package/dist/esm/langfuse.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +51 -16
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +3 -1
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/package.json +1 -1
- package/src/graphs/Graph.ts +70 -4
- package/src/langfuse.ts +25 -4
- package/src/llm/openai/index.ts +141 -41
- package/src/summarization/node.ts +23 -5
package/src/llm/openai/index.ts
CHANGED
|
@@ -264,9 +264,33 @@ type ResponsesUsageWithCacheWrite = Omit<
|
|
|
264
264
|
> & {
|
|
265
265
|
input_tokens_details?: OpenAIClient.Responses.ResponseUsage['input_tokens_details'] & {
|
|
266
266
|
cache_write_tokens?: number;
|
|
267
|
+
cache_write_tokens_details?: {
|
|
268
|
+
ttl_30m_tokens?: number;
|
|
269
|
+
ttl_12h_tokens?: number;
|
|
270
|
+
};
|
|
267
271
|
};
|
|
268
272
|
};
|
|
273
|
+
type ResponsesCacheWriteUsage = {
|
|
274
|
+
cache_creation?: number;
|
|
275
|
+
cache_creation_30m?: number;
|
|
276
|
+
cache_creation_12h?: number;
|
|
277
|
+
};
|
|
278
|
+
type ResponsesCacheWriteMetadata = {
|
|
279
|
+
usage?: ResponsesUsageWithCacheWrite | null;
|
|
280
|
+
metadata?: Record<string, string> | null;
|
|
281
|
+
};
|
|
269
282
|
const CACHE_WRITE_METADATA_KEY = '__librechat_cache_write_tokens';
|
|
283
|
+
const CACHE_WRITE_METADATA_KEYS = {
|
|
284
|
+
cache_creation: CACHE_WRITE_METADATA_KEY,
|
|
285
|
+
cache_creation_30m: '__librechat_cache_write_tokens_30m',
|
|
286
|
+
cache_creation_12h: '__librechat_cache_write_tokens_12h',
|
|
287
|
+
} as const;
|
|
288
|
+
const CACHE_WRITE_USAGE_KEYS = Object.keys(
|
|
289
|
+
CACHE_WRITE_METADATA_KEYS
|
|
290
|
+
) as (keyof ResponsesCacheWriteUsage)[];
|
|
291
|
+
const INTERNAL_CACHE_WRITE_METADATA_KEYS = Object.values(
|
|
292
|
+
CACHE_WRITE_METADATA_KEYS
|
|
293
|
+
);
|
|
270
294
|
|
|
271
295
|
function applyManagedRequestParams<T extends object>(
|
|
272
296
|
params: T,
|
|
@@ -286,6 +310,26 @@ function applyManagedRequestParams<T extends object>(
|
|
|
286
310
|
};
|
|
287
311
|
}
|
|
288
312
|
|
|
313
|
+
/** Deployment aliases must not determine the logical model's token parameter. */
|
|
314
|
+
function preserveReasoningTokenLimit(
|
|
315
|
+
params: ReturnType<OriginalChatOpenAICompletions['invocationParams']>,
|
|
316
|
+
model: string,
|
|
317
|
+
maxTokens: number | undefined
|
|
318
|
+
): ReturnType<OriginalChatOpenAICompletions['invocationParams']> {
|
|
319
|
+
if (!isReasoningModel(model) || isReasoningModel(params.model)) {
|
|
320
|
+
return params;
|
|
321
|
+
}
|
|
322
|
+
const normalized = {
|
|
323
|
+
...params,
|
|
324
|
+
max_completion_tokens:
|
|
325
|
+
maxTokens === -1
|
|
326
|
+
? undefined
|
|
327
|
+
: (maxTokens ?? params.max_completion_tokens),
|
|
328
|
+
};
|
|
329
|
+
delete normalized.max_tokens;
|
|
330
|
+
return normalized;
|
|
331
|
+
}
|
|
332
|
+
|
|
289
333
|
function isCacheableChatPart(part: unknown): part is CacheableChatPart {
|
|
290
334
|
if (typeof part !== 'object' || part == null || !('type' in part)) {
|
|
291
335
|
return false;
|
|
@@ -639,11 +683,48 @@ export function shouldIncludeEncryptedReasoning(
|
|
|
639
683
|
);
|
|
640
684
|
}
|
|
641
685
|
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
686
|
+
function cacheWriteTokenCount(
|
|
687
|
+
reported: number | undefined,
|
|
688
|
+
serialized: string | undefined
|
|
689
|
+
): number | undefined {
|
|
690
|
+
const value =
|
|
691
|
+
reported ??
|
|
692
|
+
(serialized == null || serialized.trim() === ''
|
|
693
|
+
? undefined
|
|
694
|
+
: Number(serialized));
|
|
695
|
+
return typeof value === 'number' && Number.isFinite(value) && value >= 0
|
|
696
|
+
? value
|
|
697
|
+
: undefined;
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function getCacheWriteUsage(
|
|
701
|
+
responseMetadata: ResponsesCacheWriteMetadata
|
|
702
|
+
): ResponsesCacheWriteUsage {
|
|
703
|
+
const details = responseMetadata.usage?.input_tokens_details;
|
|
704
|
+
const reported: ResponsesCacheWriteUsage = {
|
|
705
|
+
cache_creation: details?.cache_write_tokens,
|
|
706
|
+
cache_creation_30m: details?.cache_write_tokens_details?.ttl_30m_tokens,
|
|
707
|
+
cache_creation_12h: details?.cache_write_tokens_details?.ttl_12h_tokens,
|
|
646
708
|
};
|
|
709
|
+
const usage: ResponsesCacheWriteUsage = {};
|
|
710
|
+
for (const key of CACHE_WRITE_USAGE_KEYS) {
|
|
711
|
+
const value = cacheWriteTokenCount(
|
|
712
|
+
reported[key],
|
|
713
|
+
responseMetadata.metadata != null &&
|
|
714
|
+
Object.hasOwn(responseMetadata.metadata, CACHE_WRITE_METADATA_KEYS[key])
|
|
715
|
+
? responseMetadata.metadata[CACHE_WRITE_METADATA_KEYS[key]]
|
|
716
|
+
: undefined
|
|
717
|
+
);
|
|
718
|
+
if (value != null) {
|
|
719
|
+
usage[key] = value;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
return usage;
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
export function getCacheWriteTokens(message: BaseMessage): number | undefined {
|
|
726
|
+
const responseMetadata =
|
|
727
|
+
message.response_metadata as ResponsesCacheWriteMetadata;
|
|
647
728
|
const reported =
|
|
648
729
|
responseMetadata.usage?.input_tokens_details?.cache_write_tokens;
|
|
649
730
|
if (reported != null) {
|
|
@@ -658,47 +739,51 @@ export function getCacheWriteTokens(message: BaseMessage): number | undefined {
|
|
|
658
739
|
}
|
|
659
740
|
|
|
660
741
|
export function attachCacheWriteUsage(message: BaseMessage): void {
|
|
661
|
-
|
|
742
|
+
if (!isAIMessage(message)) {
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
const responseMetadata =
|
|
746
|
+
message.response_metadata as ResponsesCacheWriteMetadata;
|
|
747
|
+
const cacheWriteUsage = getCacheWriteUsage(responseMetadata);
|
|
662
748
|
if (
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
message.usage_metadata == null
|
|
749
|
+
message.usage_metadata != null &&
|
|
750
|
+
Object.keys(cacheWriteUsage).length > 0
|
|
666
751
|
) {
|
|
667
|
-
|
|
752
|
+
message.usage_metadata.input_token_details = {
|
|
753
|
+
...message.usage_metadata.input_token_details,
|
|
754
|
+
...cacheWriteUsage,
|
|
755
|
+
};
|
|
668
756
|
}
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
if (responseMetadata.metadata?.[CACHE_WRITE_METADATA_KEY] == null) {
|
|
757
|
+
const originalMetadata = responseMetadata.metadata;
|
|
758
|
+
if (
|
|
759
|
+
originalMetadata == null ||
|
|
760
|
+
!INTERNAL_CACHE_WRITE_METADATA_KEYS.some((key) =>
|
|
761
|
+
Object.hasOwn(originalMetadata, key)
|
|
762
|
+
)
|
|
763
|
+
) {
|
|
677
764
|
return;
|
|
678
765
|
}
|
|
679
|
-
const metadata = { ...
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
};
|
|
766
|
+
const metadata = { ...originalMetadata };
|
|
767
|
+
for (const key of INTERNAL_CACHE_WRITE_METADATA_KEYS) {
|
|
768
|
+
delete metadata[key];
|
|
769
|
+
}
|
|
770
|
+
message.response_metadata = { ...message.response_metadata, metadata };
|
|
685
771
|
}
|
|
686
772
|
|
|
687
773
|
export function attachCacheWriteMetadata(
|
|
688
774
|
response: OpenAIClient.Responses.Response
|
|
689
775
|
): OpenAIClient.Responses.Response {
|
|
690
|
-
const
|
|
691
|
-
|
|
692
|
-
if (cacheWriteTokens == null) {
|
|
776
|
+
const cacheWriteUsage = getCacheWriteUsage(response);
|
|
777
|
+
if (Object.keys(cacheWriteUsage).length === 0) {
|
|
693
778
|
return response;
|
|
694
779
|
}
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
};
|
|
780
|
+
const metadata = { ...(response.metadata ?? {}) };
|
|
781
|
+
for (const key of Object.keys(
|
|
782
|
+
cacheWriteUsage
|
|
783
|
+
) as (keyof ResponsesCacheWriteUsage)[]) {
|
|
784
|
+
metadata[CACHE_WRITE_METADATA_KEYS[key]] = String(cacheWriteUsage[key]);
|
|
785
|
+
}
|
|
786
|
+
return { ...response, metadata };
|
|
702
787
|
}
|
|
703
788
|
|
|
704
789
|
function isResponsesStream(
|
|
@@ -827,6 +912,7 @@ class ResponsesReplayAIMessageChunk extends AIMessageChunk {
|
|
|
827
912
|
this.additional_kwargs[OPENAI_RESPONSES_ACTIVE_REASONING_ID_KEY]
|
|
828
913
|
);
|
|
829
914
|
const combined = super.concat(chunk);
|
|
915
|
+
attachCacheWriteUsage(combined);
|
|
830
916
|
resealReasoningItemBoundary(combined, accumulated, incoming, activeItemId);
|
|
831
917
|
if (activeItemId != null) {
|
|
832
918
|
combined.additional_kwargs[OPENAI_RESPONSES_ACTIVE_REASONING_ID_KEY] =
|
|
@@ -1888,10 +1974,17 @@ class LibreChatOpenAICompletions extends OriginalChatOpenAICompletions {
|
|
|
1888
1974
|
return stripIntentFromStrictTools(
|
|
1889
1975
|
stripUnsupportedAstraParams(
|
|
1890
1976
|
this.astraRulesApply,
|
|
1891
|
-
applyManagedRequestParams(
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1977
|
+
applyManagedRequestParams(
|
|
1978
|
+
preserveReasoningTokenLimit(
|
|
1979
|
+
super.invocationParams(options, extra),
|
|
1980
|
+
this.model,
|
|
1981
|
+
this.maxTokens
|
|
1982
|
+
),
|
|
1983
|
+
{
|
|
1984
|
+
promptCacheExplicit: this.promptCacheExplicit,
|
|
1985
|
+
safetyIdentifier: this.safetyIdentifier,
|
|
1986
|
+
}
|
|
1987
|
+
),
|
|
1895
1988
|
'completions'
|
|
1896
1989
|
)
|
|
1897
1990
|
);
|
|
@@ -2475,10 +2568,17 @@ class LibreChatAzureOpenAICompletions extends OriginalAzureChatOpenAICompletions
|
|
|
2475
2568
|
return stripIntentFromStrictTools(
|
|
2476
2569
|
stripUnsupportedAstraParams(
|
|
2477
2570
|
this.astraRulesApply,
|
|
2478
|
-
applyManagedRequestParams(
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2571
|
+
applyManagedRequestParams(
|
|
2572
|
+
preserveReasoningTokenLimit(
|
|
2573
|
+
super.invocationParams(options, extra),
|
|
2574
|
+
this.model,
|
|
2575
|
+
this.maxTokens
|
|
2576
|
+
),
|
|
2577
|
+
{
|
|
2578
|
+
promptCacheExplicit: this.promptCacheExplicit,
|
|
2579
|
+
safetyIdentifier: this.safetyIdentifier,
|
|
2580
|
+
}
|
|
2581
|
+
),
|
|
2482
2582
|
'completions'
|
|
2483
2583
|
)
|
|
2484
2584
|
);
|
|
@@ -73,6 +73,7 @@ import { createRemoveAllMessage } from '@/messages/reducer';
|
|
|
73
73
|
import { getProviderFamily } from '@/llm/providerRegistry';
|
|
74
74
|
import { getProviderStopReasons } from '@/llm/truncation';
|
|
75
75
|
import { isProviderCancellation } from '@/utils/errors';
|
|
76
|
+
import { isReasoningModel } from '@/llm/openai/utils';
|
|
76
77
|
import { initializeModel } from '@/llm/init';
|
|
77
78
|
import { getChunkContent } from '@/stream';
|
|
78
79
|
import { executeHooks } from '@/hooks';
|
|
@@ -219,17 +220,34 @@ function usesCompletionTokenAlias(provider: t.ProviderName): boolean {
|
|
|
219
220
|
return getProviderFamily(provider) === 'openai' || provider === Providers.MOONSHOT;
|
|
220
221
|
}
|
|
221
222
|
|
|
222
|
-
function readSummaryOutputLimit(
|
|
223
|
+
function readSummaryOutputLimit(
|
|
224
|
+
provider: t.ProviderName,
|
|
225
|
+
options: Record<string, unknown>
|
|
226
|
+
): number | undefined {
|
|
223
227
|
const openai = usesCompletionTokenAlias(provider);
|
|
224
228
|
const native = (openai ? options.modelKwargs : options.invocationKwargs) as
|
|
225
|
-
|
|
226
|
-
|
|
229
|
+
| {
|
|
230
|
+
model?: string;
|
|
231
|
+
max_output_tokens?: number;
|
|
232
|
+
max_completion_tokens?: number;
|
|
233
|
+
max_tokens?: number;
|
|
234
|
+
}
|
|
235
|
+
| undefined;
|
|
236
|
+
const anthropic =
|
|
237
|
+
provider === Providers.ANTHROPIC ||
|
|
238
|
+
getProviderFamily(provider) === 'anthropic';
|
|
227
239
|
// Completions rewrites its active output field after merging modelKwargs.
|
|
228
240
|
let nativeLimit: number | undefined;
|
|
229
241
|
if (openai) nativeLimit = native?.max_output_tokens;
|
|
230
242
|
else if (anthropic) nativeLimit = native?.max_tokens;
|
|
231
|
-
|
|
232
|
-
|
|
243
|
+
const aliasedCompletionLimit =
|
|
244
|
+
openai && native?.model != null && !isReasoningModel(native.model)
|
|
245
|
+
? native.max_completion_tokens
|
|
246
|
+
: undefined;
|
|
247
|
+
return (nativeLimit ??
|
|
248
|
+
(openai ? options.maxCompletionTokens : undefined) ??
|
|
249
|
+
options[getMaxOutputTokensKey(provider)] ??
|
|
250
|
+
aliasedCompletionLimit) as number | undefined;
|
|
233
251
|
}
|
|
234
252
|
|
|
235
253
|
function clearSummaryOutputLimits(provider: t.ProviderName, options: Record<string, unknown>): void {
|