@librechat/agents 4.2.13 → 4.2.15
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/agents/AgentContext.cjs +75 -19
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +52 -6
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/harness/providerText.cjs +1 -0
- package/dist/cjs/harness/providerText.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/signatures.cjs +22 -14
- package/dist/cjs/llm/google/utils/signatures.cjs.map +1 -1
- package/dist/cjs/llm/invoke.cjs +122 -13
- package/dist/cjs/llm/invoke.cjs.map +1 -1
- package/dist/cjs/llm/openai/index.cjs +8 -2
- package/dist/cjs/llm/openai/index.cjs.map +1 -1
- package/dist/cjs/llm/openai/typedContent.cjs +94 -0
- package/dist/cjs/llm/openai/typedContent.cjs.map +1 -0
- package/dist/cjs/llm/prepareProviderRequest.cjs +102 -7
- package/dist/cjs/llm/prepareProviderRequest.cjs.map +1 -1
- package/dist/cjs/llm/providerTextBatch.cjs +128 -0
- package/dist/cjs/llm/providerTextBatch.cjs.map +1 -0
- package/dist/cjs/llm/providerTextBoundary.cjs +81 -12
- package/dist/cjs/llm/providerTextBoundary.cjs.map +1 -1
- package/dist/cjs/main.cjs +3 -0
- package/dist/cjs/messages/content.cjs +3 -3
- package/dist/cjs/messages/content.cjs.map +1 -1
- package/dist/cjs/messages/vision.cjs +77 -0
- package/dist/cjs/messages/vision.cjs.map +1 -0
- package/dist/cjs/protection/providerText.cjs +56 -6
- package/dist/cjs/protection/providerText.cjs.map +1 -1
- package/dist/cjs/protection/providerTextAdmission.cjs +5 -0
- package/dist/cjs/protection/providerTextAdmission.cjs.map +1 -1
- package/dist/cjs/run.cjs +7 -2
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/runtime/client.cjs +2 -0
- package/dist/cjs/runtime/client.cjs.map +1 -1
- package/dist/cjs/stream.cjs +9 -2
- package/dist/cjs/stream.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +6 -2
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/handlers.cjs +47 -1
- package/dist/cjs/tools/handlers.cjs.map +1 -1
- package/dist/cjs/tools/search/citations.cjs +194 -0
- package/dist/cjs/tools/search/citations.cjs.map +1 -0
- package/dist/cjs/tools/search/firecrawl.cjs +4 -9
- package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
- package/dist/cjs/tools/search/google.cjs +68 -0
- package/dist/cjs/tools/search/google.cjs.map +1 -0
- package/dist/cjs/tools/search/openai.cjs +80 -0
- package/dist/cjs/tools/search/openai.cjs.map +1 -0
- package/dist/cjs/utils/tokens.cjs +6 -0
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +76 -20
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +53 -7
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/harness/providerText.mjs +1 -0
- package/dist/esm/harness/providerText.mjs.map +1 -1
- package/dist/esm/llm/google/utils/signatures.mjs +22 -14
- package/dist/esm/llm/google/utils/signatures.mjs.map +1 -1
- package/dist/esm/llm/invoke.mjs +124 -15
- package/dist/esm/llm/invoke.mjs.map +1 -1
- package/dist/esm/llm/openai/index.mjs +8 -2
- package/dist/esm/llm/openai/index.mjs.map +1 -1
- package/dist/esm/llm/openai/typedContent.mjs +93 -0
- package/dist/esm/llm/openai/typedContent.mjs.map +1 -0
- package/dist/esm/llm/prepareProviderRequest.mjs +102 -7
- package/dist/esm/llm/prepareProviderRequest.mjs.map +1 -1
- package/dist/esm/llm/providerTextBatch.mjs +128 -0
- package/dist/esm/llm/providerTextBatch.mjs.map +1 -0
- package/dist/esm/llm/providerTextBoundary.mjs +81 -12
- package/dist/esm/llm/providerTextBoundary.mjs.map +1 -1
- package/dist/esm/main.mjs +4 -4
- package/dist/esm/messages/content.mjs +3 -3
- package/dist/esm/messages/content.mjs.map +1 -1
- package/dist/esm/messages/vision.mjs +73 -0
- package/dist/esm/messages/vision.mjs.map +1 -0
- package/dist/esm/protection/providerText.mjs +55 -7
- package/dist/esm/protection/providerText.mjs.map +1 -1
- package/dist/esm/protection/providerTextAdmission.mjs +5 -0
- package/dist/esm/protection/providerTextAdmission.mjs.map +1 -1
- package/dist/esm/run.mjs +7 -2
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/runtime/client.mjs +2 -0
- package/dist/esm/runtime/client.mjs.map +1 -1
- package/dist/esm/stream.mjs +9 -2
- package/dist/esm/stream.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +6 -2
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/handlers.mjs +47 -2
- package/dist/esm/tools/handlers.mjs.map +1 -1
- package/dist/esm/tools/search/citations.mjs +190 -0
- package/dist/esm/tools/search/citations.mjs.map +1 -0
- package/dist/esm/tools/search/firecrawl.mjs +4 -9
- package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
- package/dist/esm/tools/search/google.mjs +67 -0
- package/dist/esm/tools/search/google.mjs.map +1 -0
- package/dist/esm/tools/search/openai.mjs +80 -0
- package/dist/esm/tools/search/openai.mjs.map +1 -0
- package/dist/esm/utils/tokens.mjs +6 -1
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +20 -1
- package/dist/types/graphs/Graph.d.ts +14 -0
- package/dist/types/index.d.ts +2 -2
- package/dist/types/llm/openai/typedContent.d.ts +14 -0
- package/dist/types/llm/prepareProviderRequest.d.ts +7 -1
- package/dist/types/llm/providerTextBatch.d.ts +26 -0
- package/dist/types/llm/providerTextBoundary.d.ts +7 -1
- package/dist/types/messages/content.d.ts +2 -1
- package/dist/types/messages/vision.d.ts +20 -0
- package/dist/types/protection/providerText.d.ts +29 -1
- package/dist/types/protection/providerTextAdmission.d.ts +1 -0
- package/dist/types/tools/handlers.d.ts +13 -0
- package/dist/types/tools/search/citations.d.ts +88 -0
- package/dist/types/tools/search/firecrawl.d.ts +3 -1
- package/dist/types/tools/search/google.d.ts +11 -0
- package/dist/types/tools/search/openai.d.ts +3 -0
- package/dist/types/tools/search/types.d.ts +24 -0
- package/dist/types/types/graph.d.ts +8 -0
- package/dist/types/types/stream.d.ts +12 -0
- package/dist/types/utils/tokens.d.ts +3 -0
- package/package.json +1 -1
- package/src/agents/AgentContext.ts +188 -78
- package/src/graphs/Graph.ts +117 -8
- package/src/harness/providerText.ts +1 -0
- package/src/index.ts +2 -0
- package/src/llm/google/utils/signatures.ts +52 -36
- package/src/llm/invoke.ts +160 -16
- package/src/llm/openai/index.ts +18 -5
- package/src/llm/openai/typedContent.ts +110 -0
- package/src/llm/prepareProviderRequest.ts +206 -7
- package/src/llm/providerTextBatch.ts +158 -0
- package/src/llm/providerTextBoundary.ts +119 -13
- package/src/messages/content.ts +10 -4
- package/src/messages/vision.ts +109 -0
- package/src/protection/providerText.ts +95 -6
- package/src/protection/providerTextAdmission.ts +6 -0
- package/src/run.ts +22 -1
- package/src/runtime/client.ts +3 -0
- package/src/stream.ts +18 -2
- package/src/summarization/node.ts +4 -0
- package/src/tools/handlers.ts +110 -5
- package/src/tools/search/citations.ts +375 -0
- package/src/tools/search/firecrawl.ts +13 -16
- package/src/tools/search/google.ts +122 -0
- package/src/tools/search/openai.ts +118 -0
- package/src/tools/search/types.ts +21 -0
- package/src/types/graph.ts +8 -0
- package/src/types/stream.ts +9 -0
- package/src/utils/tokens.ts +13 -1
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import type { GoogleGroundingMetadata, ProcessedOrganic } from './types';
|
|
2
|
+
import type { NativeSearchResults, CitationSpan } from './citations';
|
|
3
|
+
import { byteToCharOffset } from './citations';
|
|
4
|
+
import { getAttribution } from './utils';
|
|
5
|
+
|
|
6
|
+
type GroundedChunk = {
|
|
7
|
+
additional_kwargs?: Record<string, unknown>;
|
|
8
|
+
response_metadata?: Record<string, unknown>;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
function isGroundingMetadata(value: unknown): value is GoogleGroundingMetadata {
|
|
12
|
+
return (
|
|
13
|
+
value != null &&
|
|
14
|
+
typeof value === 'object' &&
|
|
15
|
+
Array.isArray((value as GoogleGroundingMetadata).groundingChunks)
|
|
16
|
+
);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Gemini API chunks carry it in `additional_kwargs`, Vertex AI chunks in `response_metadata`. */
|
|
20
|
+
export function getGoogleGroundingMetadata(
|
|
21
|
+
chunk: GroundedChunk
|
|
22
|
+
): GoogleGroundingMetadata | undefined {
|
|
23
|
+
const candidates = [
|
|
24
|
+
chunk.additional_kwargs?.groundingMetadata,
|
|
25
|
+
chunk.response_metadata?.groundingMetadata,
|
|
26
|
+
chunk.response_metadata?.grounding_metadata,
|
|
27
|
+
];
|
|
28
|
+
return candidates.find(isGroundingMetadata);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Finds where a grounded segment ends in the call text. The API measures
|
|
33
|
+
* offsets in UTF-8 bytes of one response part, and streamed text carries no
|
|
34
|
+
* part boundaries, so the byte offset is trusted only for the first part and
|
|
35
|
+
* when the segment text sits right before it. A first-part segment is
|
|
36
|
+
* otherwise searched from the previous segment's end; a later part's segment
|
|
37
|
+
* is placed only when its text occurs once, never at a guessed occurrence.
|
|
38
|
+
*/
|
|
39
|
+
function locateSegmentEnd(
|
|
40
|
+
text: string,
|
|
41
|
+
segment: { partIndex?: number; endIndex?: number; text?: string },
|
|
42
|
+
cursor: number
|
|
43
|
+
): number | undefined {
|
|
44
|
+
const value = segment.text;
|
|
45
|
+
const firstPart = (segment.partIndex ?? 0) === 0;
|
|
46
|
+
const end =
|
|
47
|
+
firstPart && typeof segment.endIndex === 'number'
|
|
48
|
+
? byteToCharOffset(text, segment.endIndex)
|
|
49
|
+
: undefined;
|
|
50
|
+
if (value == null || value === '') {
|
|
51
|
+
return end;
|
|
52
|
+
}
|
|
53
|
+
if (end != null && text.slice(end - value.length, end) === value) {
|
|
54
|
+
return end;
|
|
55
|
+
}
|
|
56
|
+
if (!firstPart) {
|
|
57
|
+
const index = text.indexOf(value);
|
|
58
|
+
return index !== -1 && text.indexOf(value, index + 1) === -1
|
|
59
|
+
? index + value.length
|
|
60
|
+
: undefined;
|
|
61
|
+
}
|
|
62
|
+
let index = text.indexOf(value, cursor);
|
|
63
|
+
if (index === -1) {
|
|
64
|
+
index = text.indexOf(value);
|
|
65
|
+
}
|
|
66
|
+
return index === -1 ? undefined : index + value.length;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Turns Gemini grounding into organic results and the spans each support cites. */
|
|
70
|
+
export function coerceGoogleGroundingResults(
|
|
71
|
+
grounding: GoogleGroundingMetadata,
|
|
72
|
+
text: string
|
|
73
|
+
): NativeSearchResults | undefined {
|
|
74
|
+
const organic: ProcessedOrganic[] = [];
|
|
75
|
+
const sourceByChunk = new Map<number, number>();
|
|
76
|
+
grounding.groundingChunks?.forEach((chunk, index) => {
|
|
77
|
+
const link = chunk.web?.uri;
|
|
78
|
+
if (link == null || link === '') {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
sourceByChunk.set(index, organic.length);
|
|
82
|
+
organic.push({
|
|
83
|
+
link,
|
|
84
|
+
position: organic.length + 1,
|
|
85
|
+
title: chunk.web?.title,
|
|
86
|
+
attribution:
|
|
87
|
+
chunk.web?.domain ?? chunk.web?.title ?? getAttribution(link),
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
if (organic.length === 0) {
|
|
91
|
+
return undefined;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const sourcesByEnd = new Map<number, number[]>();
|
|
95
|
+
let cursor = 0;
|
|
96
|
+
for (const support of grounding.groundingSupports ?? []) {
|
|
97
|
+
const sources = [
|
|
98
|
+
...new Set(
|
|
99
|
+
(support.groundingChunkIndices ?? []).flatMap(
|
|
100
|
+
(index) => sourceByChunk.get(index) ?? []
|
|
101
|
+
)
|
|
102
|
+
),
|
|
103
|
+
];
|
|
104
|
+
if (sources.length === 0 || support.segment == null) {
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
const end = locateSegmentEnd(text, support.segment, cursor);
|
|
108
|
+
if (end == null) {
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
cursor = end;
|
|
112
|
+
const cited = sourcesByEnd.get(end) ?? [];
|
|
113
|
+
sourcesByEnd.set(end, [...new Set([...cited, ...sources])]);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const spans: CitationSpan[] = [...sourcesByEnd].map(([end, sources]) => ({
|
|
117
|
+
start: end,
|
|
118
|
+
end,
|
|
119
|
+
sources,
|
|
120
|
+
}));
|
|
121
|
+
return { organic, spans, queries: grounding.webSearchQueries };
|
|
122
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
NativeSearchResults,
|
|
3
|
+
CitationSpan,
|
|
4
|
+
UrlCitation,
|
|
5
|
+
} from './citations';
|
|
6
|
+
import type { ProcessedOrganic } from './types';
|
|
7
|
+
import { getAttribution, getDomainName } from './utils';
|
|
8
|
+
|
|
9
|
+
/** A markdown link, optionally wrapped in the parentheses OpenAI puts around citations. */
|
|
10
|
+
const MARKDOWN_LINK = /^\(?\[[^\]\n]+\]\((https?:\/\/\S+?)\)\)?$/;
|
|
11
|
+
|
|
12
|
+
function linksTo(span: string, url: string): boolean {
|
|
13
|
+
const target = MARKDOWN_LINK.exec(span)?.[1];
|
|
14
|
+
if (target == null) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
return target === url || getDomainName(target) === getDomainName(url);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function matchLink(
|
|
21
|
+
text: string,
|
|
22
|
+
start: number,
|
|
23
|
+
end: number,
|
|
24
|
+
url: string
|
|
25
|
+
): { start: number; end: number } | undefined {
|
|
26
|
+
for (const close of [end, end + 1]) {
|
|
27
|
+
if (!linksTo(text.slice(start, close), url)) {
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
const wrapped = text[start - 1] === '(' && text[close] === ')';
|
|
31
|
+
return wrapped
|
|
32
|
+
? { start: start - 1, end: close + 1 }
|
|
33
|
+
: { start, end: close };
|
|
34
|
+
}
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** UTF-16 index of every code point from `from` to the end of `text`, then the end itself. */
|
|
39
|
+
function getCodePointOffsets(text: string, from: number): number[] {
|
|
40
|
+
const offsets: number[] = [];
|
|
41
|
+
let index = from;
|
|
42
|
+
while (index < text.length) {
|
|
43
|
+
offsets.push(index);
|
|
44
|
+
index += (text.codePointAt(index) ?? 0) > 0xffff ? 2 : 1;
|
|
45
|
+
}
|
|
46
|
+
offsets.push(index);
|
|
47
|
+
return offsets;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Finds the inline markdown link a `url_citation` covers. Its indices count
|
|
52
|
+
* characters of one output text block; they are read as UTF-16 first, then as
|
|
53
|
+
* code points, and the end is tried as exclusive and inclusive. A span that is
|
|
54
|
+
* not a link to the cited host is left alone.
|
|
55
|
+
*/
|
|
56
|
+
function locateLink(
|
|
57
|
+
text: string,
|
|
58
|
+
blockStart: number,
|
|
59
|
+
citation: UrlCitation,
|
|
60
|
+
getOffsets: () => number[]
|
|
61
|
+
): { start: number; end: number } | undefined {
|
|
62
|
+
const { start, end, url } = citation;
|
|
63
|
+
if (start == null || end == null) {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
const direct = matchLink(text, blockStart + start, blockStart + end, url);
|
|
67
|
+
if (direct != null) {
|
|
68
|
+
return direct;
|
|
69
|
+
}
|
|
70
|
+
const offsets = getOffsets();
|
|
71
|
+
const from = offsets.at(start);
|
|
72
|
+
const to = offsets.at(end);
|
|
73
|
+
return from == null || to == null
|
|
74
|
+
? undefined
|
|
75
|
+
: matchLink(text, from, to, url);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** Turns OpenAI `url_citation` annotations into organic results and the links they replace. */
|
|
79
|
+
export function coerceOpenAICitationResults(
|
|
80
|
+
citations: readonly UrlCitation[],
|
|
81
|
+
text: string,
|
|
82
|
+
blocks: ReadonlyMap<number, number>
|
|
83
|
+
): NativeSearchResults | undefined {
|
|
84
|
+
const organic: ProcessedOrganic[] = [];
|
|
85
|
+
const sourceByUrl = new Map<string, number>();
|
|
86
|
+
const offsetsByBlock = new Map<number, number[]>();
|
|
87
|
+
const spans: CitationSpan[] = [];
|
|
88
|
+
for (const citation of citations) {
|
|
89
|
+
let source = sourceByUrl.get(citation.url);
|
|
90
|
+
if (source == null) {
|
|
91
|
+
source = organic.length;
|
|
92
|
+
sourceByUrl.set(citation.url, source);
|
|
93
|
+
organic.push({
|
|
94
|
+
link: citation.url,
|
|
95
|
+
position: source + 1,
|
|
96
|
+
title: citation.title,
|
|
97
|
+
attribution: getAttribution(citation.url),
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
const blockStart =
|
|
101
|
+
citation.block == null ? undefined : blocks.get(citation.block);
|
|
102
|
+
const span =
|
|
103
|
+
blockStart == null
|
|
104
|
+
? undefined
|
|
105
|
+
: locateLink(text, blockStart, citation, () => {
|
|
106
|
+
let offsets = offsetsByBlock.get(blockStart);
|
|
107
|
+
if (offsets == null) {
|
|
108
|
+
offsets = getCodePointOffsets(text, blockStart);
|
|
109
|
+
offsetsByBlock.set(blockStart, offsets);
|
|
110
|
+
}
|
|
111
|
+
return offsets;
|
|
112
|
+
});
|
|
113
|
+
if (span != null) {
|
|
114
|
+
spans.push({ ...span, sources: [source] });
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return organic.length > 0 ? { organic, spans } : undefined;
|
|
118
|
+
}
|
|
@@ -75,6 +75,27 @@ export interface SearchResultData {
|
|
|
75
75
|
error?: string;
|
|
76
76
|
}
|
|
77
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Gemini's `groundingMetadata` as the API returns it. The
|
|
80
|
+
* `@google/generative-ai` declarations type `segment` as a string and
|
|
81
|
+
* misspell `groundingChunkIndices`, so they cannot describe it.
|
|
82
|
+
*/
|
|
83
|
+
export type GoogleGroundingMetadata = {
|
|
84
|
+
webSearchQueries?: string[];
|
|
85
|
+
groundingChunks?: {
|
|
86
|
+
web?: { uri?: string; title?: string; domain?: string };
|
|
87
|
+
}[];
|
|
88
|
+
groundingSupports?: {
|
|
89
|
+
segment?: {
|
|
90
|
+
partIndex?: number;
|
|
91
|
+
startIndex?: number;
|
|
92
|
+
endIndex?: number;
|
|
93
|
+
text?: string;
|
|
94
|
+
};
|
|
95
|
+
groundingChunkIndices?: number[];
|
|
96
|
+
}[];
|
|
97
|
+
};
|
|
98
|
+
|
|
78
99
|
export interface SearchResult {
|
|
79
100
|
data?: SearchResultData;
|
|
80
101
|
error?: string;
|
package/src/types/graph.ts
CHANGED
|
@@ -1043,6 +1043,14 @@ interface AgentInputFields {
|
|
|
1043
1043
|
reasoningKey?: 'reasoning_content' | 'reasoning';
|
|
1044
1044
|
/** Format content blocks as strings (for legacy compatibility i.e. Ollama/Azure Serverless) */
|
|
1045
1045
|
useLegacyContent?: boolean;
|
|
1046
|
+
/**
|
|
1047
|
+
* Whether this agent's model reads image input. `false` replaces every
|
|
1048
|
+
* image in the agent's provider requests (its own calls, fallbacks and
|
|
1049
|
+
* summarization) with a short text marker, so a text-only model in a
|
|
1050
|
+
* multi-agent run never receives images the other agents can read. Unset
|
|
1051
|
+
* sends images unchanged.
|
|
1052
|
+
*/
|
|
1053
|
+
vision?: boolean;
|
|
1046
1054
|
/**
|
|
1047
1055
|
* Tool definitions for all tools, including deferred and programmatic.
|
|
1048
1056
|
* Used for tool search and programmatic tool calling.
|
package/src/types/stream.ts
CHANGED
|
@@ -395,12 +395,21 @@ export interface ReasoningDelta {
|
|
|
395
395
|
content?: MessageContentComplex[];
|
|
396
396
|
}
|
|
397
397
|
|
|
398
|
+
/** Replaces `[start, end)` of a text part's accumulated text; an insertion when both match. */
|
|
399
|
+
export type TextEdit = { start: number; end: number; text: string };
|
|
400
|
+
|
|
398
401
|
export type MessageDeltaUpdate = {
|
|
399
402
|
type: ContentTypes.TEXT;
|
|
400
403
|
text: string;
|
|
401
404
|
tool_call_ids?: string[];
|
|
402
405
|
/** Provider-supplied source citations, accumulated across deltas. */
|
|
403
406
|
citations?: Anthropic.TextCitation[];
|
|
407
|
+
/**
|
|
408
|
+
* Rewrites of text this part already holds, for citations a provider sends
|
|
409
|
+
* after the text they cite (Google grounding, OpenAI `url_citation`). Never
|
|
410
|
+
* stored on the part; a delta carrying them has no `text`.
|
|
411
|
+
*/
|
|
412
|
+
edits?: TextEdit[];
|
|
404
413
|
};
|
|
405
414
|
export type ReasoningDeltaUpdate = { type: ContentTypes.THINK; think: string };
|
|
406
415
|
|
package/src/utils/tokens.ts
CHANGED
|
@@ -302,6 +302,7 @@ function estimateBase64DocumentTokens(
|
|
|
302
302
|
* and Anthropic format (`type: 'document'` with `source`).
|
|
303
303
|
*
|
|
304
304
|
* - Plain text: tokenized directly via `getTokenCount`.
|
|
305
|
+
* - Audio and video: priced by duration, as {@link estimateTimedMediaBlockTokens}.
|
|
305
306
|
* - Base64 PDF: page count estimated from base64 length × per-page cost.
|
|
306
307
|
* - Other base64 documents: decoded text, or decoded size when binary.
|
|
307
308
|
* - URL reference: conservative flat estimate.
|
|
@@ -327,6 +328,14 @@ export function estimateDocumentBlockTokens(
|
|
|
327
328
|
return getTokenCount(block.text as string);
|
|
328
329
|
}
|
|
329
330
|
|
|
331
|
+
const mediaMimeType = normalizeMediaMimeType(mimeType);
|
|
332
|
+
if (
|
|
333
|
+
mediaMimeType.startsWith('audio/') ||
|
|
334
|
+
mediaMimeType.startsWith('video/')
|
|
335
|
+
) {
|
|
336
|
+
return estimateTimedMediaBlockTokens({ ...block, type: 'media' });
|
|
337
|
+
}
|
|
338
|
+
|
|
330
339
|
if (sourceType === 'base64' && typeof block.data === 'string') {
|
|
331
340
|
if (mimeType === 'application/pdf' || mimeType === '') {
|
|
332
341
|
const pageEstimate = Math.max(
|
|
@@ -507,7 +516,10 @@ function timedMediaKind(
|
|
|
507
516
|
return null;
|
|
508
517
|
}
|
|
509
518
|
|
|
510
|
-
|
|
519
|
+
/** The normalized MIME type a media or file content block declares, if any. */
|
|
520
|
+
export function getMediaMimeType(
|
|
521
|
+
block: Record<string, unknown>
|
|
522
|
+
): string | undefined {
|
|
511
523
|
if (typeof block.mimeType === 'string') {
|
|
512
524
|
return normalizeMediaMimeType(block.mimeType);
|
|
513
525
|
}
|