@librechat/agents 4.2.14 → 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 +7 -3
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/llm/prepareProviderRequest.cjs +24 -6
- package/dist/cjs/llm/prepareProviderRequest.cjs.map +1 -1
- package/dist/cjs/main.cjs +1 -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/summarization/node.cjs +6 -2
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/utils/tokens.cjs +1 -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 +7 -3
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/llm/prepareProviderRequest.mjs +24 -6
- package/dist/esm/llm/prepareProviderRequest.mjs.map +1 -1
- package/dist/esm/main.mjs +2 -2
- 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/summarization/node.mjs +6 -2
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/utils/tokens.mjs +1 -1
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/agents/AgentContext.d.ts +20 -1
- package/dist/types/llm/prepareProviderRequest.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/types/graph.d.ts +8 -0
- package/dist/types/utils/tokens.d.ts +2 -0
- package/package.json +1 -1
- package/src/agents/AgentContext.ts +188 -78
- package/src/graphs/Graph.ts +14 -5
- package/src/llm/prepareProviderRequest.ts +52 -3
- package/src/messages/content.ts +10 -4
- package/src/messages/vision.ts +109 -0
- package/src/summarization/node.ts +4 -0
- package/src/types/graph.ts +8 -0
- package/src/utils/tokens.ts +4 -1
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
/* eslint-disable no-console */
|
|
2
2
|
import { RunnableLambda } from '@langchain/core/runnables';
|
|
3
3
|
import {
|
|
4
|
+
ToolMessage,
|
|
4
5
|
HumanMessage,
|
|
5
6
|
SystemMessage,
|
|
6
7
|
coerceMessageLikeToMessage,
|
|
@@ -10,6 +11,7 @@ import type {
|
|
|
10
11
|
BaseMessage,
|
|
11
12
|
BaseMessageFields,
|
|
12
13
|
BaseMessageLike,
|
|
14
|
+
MessageContentComplex,
|
|
13
15
|
} from '@langchain/core/messages';
|
|
14
16
|
import type { RunnableConfig, Runnable } from '@langchain/core/runnables';
|
|
15
17
|
import type { ExactTokenCountCache } from '@/llm/contextPressureMeter';
|
|
@@ -56,11 +58,17 @@ import {
|
|
|
56
58
|
Constants,
|
|
57
59
|
Providers,
|
|
58
60
|
} from '@/common';
|
|
61
|
+
import {
|
|
62
|
+
hasComputerCallOutputMarker,
|
|
63
|
+
cloneToolMessageWithContent,
|
|
64
|
+
} from '@/utils/toolContent';
|
|
59
65
|
import { isTokenCounterCacheCompatible } from '@/llm/tokenCounterCacheCompatibility';
|
|
60
66
|
import { normalizeToBashIdentifier } from '@/tools/BashProgrammaticToolCalling';
|
|
61
67
|
import { snapshotCompactionSemanticIndex } from '@/summarization/semanticIndex';
|
|
68
|
+
import { flattenLegacyContent, isLegacyConvertible } from '@/messages/content';
|
|
62
69
|
import { normalizeToPythonIdentifier } from '@/tools/ProgrammaticToolCalling';
|
|
63
70
|
import { createExactTokenCountCache } from '@/llm/contextPressureMeter';
|
|
71
|
+
import { omitImageBlocks, omitArtifactImages } from '@/messages/vision';
|
|
64
72
|
import { markProviderTextInput } from '@/protection/providerTextInput';
|
|
65
73
|
import { buildSummaryCarrierText } from '@/summarization/shared';
|
|
66
74
|
import { createSchemaOnlyTools } from '@/tools/schema';
|
|
@@ -127,6 +135,7 @@ export class AgentContext {
|
|
|
127
135
|
maxContextTokens,
|
|
128
136
|
reasoningKey,
|
|
129
137
|
useLegacyContent,
|
|
138
|
+
vision,
|
|
130
139
|
discoveredTools,
|
|
131
140
|
summarizationEnabled,
|
|
132
141
|
summarizeOnly,
|
|
@@ -163,6 +172,7 @@ export class AgentContext {
|
|
|
163
172
|
instructionTokens: 0,
|
|
164
173
|
tokenCounter,
|
|
165
174
|
useLegacyContent,
|
|
175
|
+
vision,
|
|
166
176
|
discoveredTools,
|
|
167
177
|
summarizationEnabled,
|
|
168
178
|
summarizeOnly,
|
|
@@ -323,6 +333,8 @@ export class AgentContext {
|
|
|
323
333
|
private providerProjectedMessages?: BaseMessage[];
|
|
324
334
|
/** Canonical object identities corresponding to the projected slots. */
|
|
325
335
|
private providerProjectionSources?: BaseMessage[];
|
|
336
|
+
/** Without image input, the image-free history the pruner rewrites the projection from. */
|
|
337
|
+
private providerProjectionCanonical?: BaseMessage[];
|
|
326
338
|
/** Canonical messages by ID, used to distinguish identity replay from replacement. */
|
|
327
339
|
private providerProjectionSourcesById?: Map<string, BaseMessage>;
|
|
328
340
|
/** Conservative counts retained by canonical identity across a rebuild. */
|
|
@@ -414,6 +426,8 @@ export class AgentContext {
|
|
|
414
426
|
tokenCalculationPromise?: Promise<void>;
|
|
415
427
|
/** Format content blocks as strings (for legacy compatibility) */
|
|
416
428
|
useLegacyContent: boolean = false;
|
|
429
|
+
/** `false` when this agent's model cannot read images (see `AgentInputs.vision`) */
|
|
430
|
+
vision?: boolean;
|
|
417
431
|
/** Enables graph-level summarization for this agent */
|
|
418
432
|
summarizationEnabled?: boolean;
|
|
419
433
|
/** Summarize-only run: request a summary on the first model step and end after it */
|
|
@@ -478,7 +492,11 @@ export class AgentContext {
|
|
|
478
492
|
private correctedContextBudgetMode?: t.ContextBudgetMode;
|
|
479
493
|
|
|
480
494
|
get summarizationBudgetMode(): t.ContextBudgetMode {
|
|
481
|
-
return
|
|
495
|
+
return (
|
|
496
|
+
this.correctedContextBudgetMode ??
|
|
497
|
+
this.summarizationConfig?.contextBudgetMode ??
|
|
498
|
+
'input'
|
|
499
|
+
);
|
|
482
500
|
}
|
|
483
501
|
/**
|
|
484
502
|
* Prompt size, normalized into the local counter's uncalibrated units, at
|
|
@@ -520,6 +538,7 @@ export class AgentContext {
|
|
|
520
538
|
toolEnd,
|
|
521
539
|
instructionTokens,
|
|
522
540
|
useLegacyContent,
|
|
541
|
+
vision,
|
|
523
542
|
discoveredTools,
|
|
524
543
|
summarizationEnabled,
|
|
525
544
|
summarizeOnly,
|
|
@@ -549,6 +568,7 @@ export class AgentContext {
|
|
|
549
568
|
toolEnd?: boolean;
|
|
550
569
|
instructionTokens?: number;
|
|
551
570
|
useLegacyContent?: boolean;
|
|
571
|
+
vision?: boolean;
|
|
552
572
|
discoveredTools?: string[];
|
|
553
573
|
summarizationEnabled?: boolean;
|
|
554
574
|
summarizeOnly?: boolean;
|
|
@@ -595,6 +615,7 @@ export class AgentContext {
|
|
|
595
615
|
}
|
|
596
616
|
|
|
597
617
|
this.useLegacyContent = useLegacyContent ?? false;
|
|
618
|
+
this.vision = vision;
|
|
598
619
|
this.summarizationEnabled = summarizationEnabled;
|
|
599
620
|
this.summarizeOnly = summarizeOnly;
|
|
600
621
|
this.summarizationConfig = summarizationConfig;
|
|
@@ -1061,45 +1082,47 @@ export class AgentContext {
|
|
|
1061
1082
|
: 0;
|
|
1062
1083
|
}
|
|
1063
1084
|
|
|
1064
|
-
return markProviderTextInput(
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
if (
|
|
1092
|
-
promptCacheProvider != null &&
|
|
1093
|
-
dynamicTail.length === 0 &&
|
|
1094
|
-
body.length >= 2
|
|
1095
|
-
) {
|
|
1096
|
-
body = addTailCacheControl(
|
|
1097
|
-
body,
|
|
1098
|
-
this.getPromptCacheTtl(promptCacheProvider)
|
|
1085
|
+
return markProviderTextInput(
|
|
1086
|
+
RunnableLambda.from((messages: BaseMessage[]) => {
|
|
1087
|
+
const prefix: BaseMessage[] = systemMessage ? [systemMessage] : [];
|
|
1088
|
+
|
|
1089
|
+
// Build the non-system portion (summary + conversation), then apply
|
|
1090
|
+
// cache markers separately so addCacheControl doesn't strip the
|
|
1091
|
+
// SystemMessage's own cache_control breakpoint set above.
|
|
1092
|
+
const hasSummaryBody =
|
|
1093
|
+
this._summaryLocation === 'user_message' &&
|
|
1094
|
+
this.summaryText != null &&
|
|
1095
|
+
this.summaryText !== '';
|
|
1096
|
+
|
|
1097
|
+
const bodyWithSummary =
|
|
1098
|
+
hasSummaryBody && promptCacheProvider == null
|
|
1099
|
+
? [this.buildSummaryHumanMessage(promptCacheProvider), ...messages]
|
|
1100
|
+
: messages;
|
|
1101
|
+
const dynamicTail = this.buildPromptCacheDynamicTail({
|
|
1102
|
+
dynamicInstructions,
|
|
1103
|
+
hasSummaryBody,
|
|
1104
|
+
promptCacheProvider,
|
|
1105
|
+
shouldMoveDynamicInstructions,
|
|
1106
|
+
});
|
|
1107
|
+
let body = this.buildBodyWithPromptCacheDynamicTail(
|
|
1108
|
+
bodyWithSummary,
|
|
1109
|
+
dynamicTail,
|
|
1110
|
+
promptCacheProvider
|
|
1099
1111
|
);
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1112
|
+
|
|
1113
|
+
if (
|
|
1114
|
+
promptCacheProvider != null &&
|
|
1115
|
+
dynamicTail.length === 0 &&
|
|
1116
|
+
body.length >= 2
|
|
1117
|
+
) {
|
|
1118
|
+
body = addTailCacheControl(
|
|
1119
|
+
body,
|
|
1120
|
+
this.getPromptCacheTtl(promptCacheProvider)
|
|
1121
|
+
);
|
|
1122
|
+
}
|
|
1123
|
+
return [...prefix, ...body];
|
|
1124
|
+
})
|
|
1125
|
+
).withConfig({ runName: 'prompt' });
|
|
1103
1126
|
}
|
|
1104
1127
|
|
|
1105
1128
|
private buildSummaryHumanMessage(
|
|
@@ -1881,31 +1904,50 @@ export class AgentContext {
|
|
|
1881
1904
|
this.pendingOriginalToolContent = undefined;
|
|
1882
1905
|
this.prepareProviderProjectionRecount(sources);
|
|
1883
1906
|
}
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
);
|
|
1896
|
-
|
|
1897
|
-
|
|
1898
|
-
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1907
|
+
const counter = this.tokenCounter;
|
|
1908
|
+
const recount = this.providerProjectionRequiresRecount && counter != null;
|
|
1909
|
+
const floors = this.providerProjectionRecountFloors;
|
|
1910
|
+
const hostCounts = this.indexTokenCountMap;
|
|
1911
|
+
/** A rewrite recounts every message; otherwise host counts stand, and the first replaced image copies them so other agents' maps stay intact. */
|
|
1912
|
+
let counts: Record<string, number | undefined> = recount
|
|
1913
|
+
? {}
|
|
1914
|
+
: hostCounts;
|
|
1915
|
+
const canonical: BaseMessage[] | undefined =
|
|
1916
|
+
this.vision === false ? [] : undefined;
|
|
1917
|
+
const nextSources: BaseMessage[] = [];
|
|
1918
|
+
const sourcesById = new Map<string, BaseMessage>();
|
|
1919
|
+
projection = messages.map((message, index) => {
|
|
1920
|
+
nextSources.push(message);
|
|
1921
|
+
if (message.id != null) {
|
|
1922
|
+
sourcesById.set(message.id, message);
|
|
1923
|
+
}
|
|
1924
|
+
const projected = this.projectMessage(message);
|
|
1925
|
+
canonical?.push(projected.canonical);
|
|
1926
|
+
if (counter == null) {
|
|
1927
|
+
return projected.message;
|
|
1928
|
+
}
|
|
1929
|
+
if (projected.omittedImages) {
|
|
1930
|
+
if (counts === hostCounts) {
|
|
1931
|
+
counts = { ...hostCounts };
|
|
1932
|
+
}
|
|
1933
|
+
/** Priced as the marker its request sends, never as the image a host count or floor remembers. */
|
|
1934
|
+
counts[index] = counter(projected.message);
|
|
1935
|
+
} else if (recount) {
|
|
1936
|
+
const localCount = counter(message);
|
|
1937
|
+
const conservativeFloor = floors?.get(message);
|
|
1938
|
+
counts[index] =
|
|
1904
1939
|
conservativeFloor == null
|
|
1905
1940
|
? localCount
|
|
1906
1941
|
: Math.max(localCount, conservativeFloor);
|
|
1907
1942
|
}
|
|
1908
|
-
|
|
1943
|
+
return projected.message;
|
|
1944
|
+
});
|
|
1945
|
+
this.providerProjectedMessages = projection;
|
|
1946
|
+
this.providerProjectionCanonical = canonical;
|
|
1947
|
+
this.providerProjectionSources = nextSources;
|
|
1948
|
+
this.providerProjectionSourcesById = sourcesById;
|
|
1949
|
+
this.indexTokenCountMap = counts;
|
|
1950
|
+
if (recount) {
|
|
1909
1951
|
this.providerProjectionRequiresRecount = false;
|
|
1910
1952
|
this.providerProjectionRecountFloors = undefined;
|
|
1911
1953
|
}
|
|
@@ -1918,15 +1960,75 @@ export class AgentContext {
|
|
|
1918
1960
|
if (message.id != null) {
|
|
1919
1961
|
this.providerProjectionSourcesById?.set(message.id, message);
|
|
1920
1962
|
}
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
: message
|
|
1925
|
-
);
|
|
1963
|
+
const projected = this.projectMessage(message);
|
|
1964
|
+
projection.push(projected.message);
|
|
1965
|
+
this.providerProjectionCanonical?.push(projected.canonical);
|
|
1926
1966
|
}
|
|
1927
1967
|
return projection;
|
|
1928
1968
|
}
|
|
1929
1969
|
|
|
1970
|
+
/**
|
|
1971
|
+
* The canonical history the pruner rewrites the projection from: for a model
|
|
1972
|
+
* without image input, the messages last given to
|
|
1973
|
+
* `getProviderProjectedMessages` with each image as its marker, so fading,
|
|
1974
|
+
* masking and input caps never bring an image back; otherwise `messages`.
|
|
1975
|
+
*/
|
|
1976
|
+
getProviderCanonicalMessages(messages: BaseMessage[]): BaseMessage[] {
|
|
1977
|
+
return this.providerProjectionCanonical ?? messages;
|
|
1978
|
+
}
|
|
1979
|
+
|
|
1980
|
+
/**
|
|
1981
|
+
* The projection's copy of `message` and the canonical message it is
|
|
1982
|
+
* rewritten from; without image input, both read each image as its marker,
|
|
1983
|
+
* also in a tool artifact the request expands later. `omittedImages` marks a
|
|
1984
|
+
* content change that needs a new count.
|
|
1985
|
+
*/
|
|
1986
|
+
private projectMessage(message: BaseMessage): {
|
|
1987
|
+
message: BaseMessage;
|
|
1988
|
+
canonical: BaseMessage;
|
|
1989
|
+
omittedImages: boolean;
|
|
1990
|
+
} {
|
|
1991
|
+
const content = Array.isArray(message.content)
|
|
1992
|
+
? (message.content as MessageContentComplex[])
|
|
1993
|
+
: undefined;
|
|
1994
|
+
const omits =
|
|
1995
|
+
this.vision === false && !hasComputerCallOutputMarker(message);
|
|
1996
|
+
const omitted =
|
|
1997
|
+
omits && content != null ? omitImageBlocks(content) : undefined;
|
|
1998
|
+
const artifact =
|
|
1999
|
+
omits && message instanceof ToolMessage
|
|
2000
|
+
? omitArtifactImages(message.artifact)
|
|
2001
|
+
: undefined;
|
|
2002
|
+
if (omitted == null && artifact == null) {
|
|
2003
|
+
return {
|
|
2004
|
+
message:
|
|
2005
|
+
content == null ? message : cloneMessage(message, [...content]),
|
|
2006
|
+
canonical: message,
|
|
2007
|
+
omittedImages: false,
|
|
2008
|
+
};
|
|
2009
|
+
}
|
|
2010
|
+
let readable = message.content as string | MessageContentComplex[];
|
|
2011
|
+
if (omitted != null) {
|
|
2012
|
+
readable =
|
|
2013
|
+
this.useLegacyContent && isLegacyConvertible(message, omitted)
|
|
2014
|
+
? flattenLegacyContent(omitted)
|
|
2015
|
+
: omitted;
|
|
2016
|
+
}
|
|
2017
|
+
const copy = (value: string | MessageContentComplex[]): BaseMessage =>
|
|
2018
|
+
artifact == null
|
|
2019
|
+
? cloneMessage(message, value)
|
|
2020
|
+
: cloneToolMessageWithContent(
|
|
2021
|
+
message as ToolMessage,
|
|
2022
|
+
value as BaseMessage['content'],
|
|
2023
|
+
artifact
|
|
2024
|
+
);
|
|
2025
|
+
return {
|
|
2026
|
+
message: copy(typeof readable === 'string' ? readable : [...readable]),
|
|
2027
|
+
canonical: copy(readable),
|
|
2028
|
+
omittedImages: omitted != null,
|
|
2029
|
+
};
|
|
2030
|
+
}
|
|
2031
|
+
|
|
1930
2032
|
/**
|
|
1931
2033
|
* Invalidates the provider projection when a graph reducer update rewrites
|
|
1932
2034
|
* canonical history. Reducer updates expose replacements and removals before
|
|
@@ -1978,6 +2080,7 @@ export class AgentContext {
|
|
|
1978
2080
|
this.providerProjectedMessages = undefined;
|
|
1979
2081
|
this.providerProjectionSources = undefined;
|
|
1980
2082
|
this.providerProjectionSourcesById = undefined;
|
|
2083
|
+
this.providerProjectionCanonical = undefined;
|
|
1981
2084
|
}
|
|
1982
2085
|
|
|
1983
2086
|
private prepareProviderProjectionRecount(
|
|
@@ -2189,19 +2292,26 @@ export class AgentContext {
|
|
|
2189
2292
|
/** Refresh stale system overhead (handoff/summary changes) so instruction
|
|
2190
2293
|
* tokens match the prompt a real call would send. */
|
|
2191
2294
|
this.initializeSystemRunnable();
|
|
2192
|
-
/**
|
|
2193
|
-
*
|
|
2194
|
-
* history
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
)
|
|
2200
|
-
|
|
2201
|
-
|
|
2202
|
-
|
|
2203
|
-
|
|
2204
|
-
|
|
2295
|
+
/** The projection's copies: the pruner unshifts reasoning blocks into AI
|
|
2296
|
+
* content arrays in place, which would otherwise corrupt the caller's
|
|
2297
|
+
* history, and a model without image input reads each image as its
|
|
2298
|
+
* marker. (Slot replacements land on the mapped array, not the caller's.) */
|
|
2299
|
+
const supplied = opts?.indexTokenCountMap;
|
|
2300
|
+
let indexTokenCountMap: Record<string, number | undefined> = supplied ?? {};
|
|
2301
|
+
const projected: BaseMessage[] = [];
|
|
2302
|
+
for (let i = 0; i < messages.length; i++) {
|
|
2303
|
+
const { message, omittedImages } = this.projectMessage(messages[i]);
|
|
2304
|
+
projected.push(message);
|
|
2305
|
+
if (supplied == null) {
|
|
2306
|
+
indexTokenCountMap[String(i)] = tokenCounter(
|
|
2307
|
+
omittedImages ? message : messages[i]
|
|
2308
|
+
);
|
|
2309
|
+
} else if (omittedImages) {
|
|
2310
|
+
/** A supplied count priced the image the marker replaces; the copy keeps the caller's map intact. */
|
|
2311
|
+
if (indexTokenCountMap === supplied) {
|
|
2312
|
+
indexTokenCountMap = { ...supplied };
|
|
2313
|
+
}
|
|
2314
|
+
indexTokenCountMap[String(i)] = tokenCounter(message);
|
|
2205
2315
|
}
|
|
2206
2316
|
}
|
|
2207
2317
|
const prune = createPruneMessages({
|
package/src/graphs/Graph.ts
CHANGED
|
@@ -3516,7 +3516,6 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3516
3516
|
}
|
|
3517
3517
|
this.config = config;
|
|
3518
3518
|
|
|
3519
|
-
let messagesToUse = messages;
|
|
3520
3519
|
let contextUsage: t.ContextUsageEvent | null = null;
|
|
3521
3520
|
/**
|
|
3522
3521
|
* Held outside the prune block so overflow recovery — which detours to
|
|
@@ -3527,9 +3526,14 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3527
3526
|
const canProjectContext =
|
|
3528
3527
|
agentContext.tokenCounter != null &&
|
|
3529
3528
|
agentContext.maxContextTokens != null;
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3529
|
+
/** A model without image input reads history through the projection even
|
|
3530
|
+
* without a pruner, so no later transform (tool folding included) sees
|
|
3531
|
+
* image bytes before the request replaces them. */
|
|
3532
|
+
const providerMessages =
|
|
3533
|
+
canProjectContext || agentContext.vision === false
|
|
3534
|
+
? agentContext.getProviderProjectedMessages(messages)
|
|
3535
|
+
: messages;
|
|
3536
|
+
let messagesToUse = providerMessages;
|
|
3533
3537
|
if (
|
|
3534
3538
|
!agentContext.pruneMessages &&
|
|
3535
3539
|
agentContext.tokenCounter &&
|
|
@@ -3576,7 +3580,8 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
3576
3580
|
effectiveInstructionTokens,
|
|
3577
3581
|
} = agentContext.pruneMessages({
|
|
3578
3582
|
messages: providerMessages,
|
|
3579
|
-
canonicalMessages:
|
|
3583
|
+
canonicalMessages:
|
|
3584
|
+
agentContext.getProviderCanonicalMessages(messages),
|
|
3580
3585
|
canonicalPrefixStable: true,
|
|
3581
3586
|
usageMetadata: agentContext.currentUsage,
|
|
3582
3587
|
lastCallUsage: agentContext.lastCallUsage,
|
|
@@ -4335,6 +4340,8 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
4335
4340
|
config,
|
|
4336
4341
|
maxToolResultChars: maxProviderToolResultChars,
|
|
4337
4342
|
toolHistory,
|
|
4343
|
+
vision: agentContext.vision,
|
|
4344
|
+
useLegacyContent: agentContext.useLegacyContent,
|
|
4338
4345
|
measure: (preparedMessages) =>
|
|
4339
4346
|
measureProviderPayload(
|
|
4340
4347
|
trackProviderMessageOrigins(
|
|
@@ -4896,6 +4903,8 @@ export class StandardGraph extends Graph<t.BaseGraphState, t.GraphNode> {
|
|
|
4896
4903
|
config: fallbackConfig,
|
|
4897
4904
|
maxToolResultChars: fallbackToolResultChars,
|
|
4898
4905
|
toolHistory: fallbackToolHistory,
|
|
4906
|
+
vision: agentContext.vision,
|
|
4907
|
+
useLegacyContent: agentContext.useLegacyContent,
|
|
4899
4908
|
measure: (prepared) => {
|
|
4900
4909
|
projection = measureFallback(
|
|
4901
4910
|
trackProviderMessageOrigins(cached, prepared)
|
|
@@ -33,10 +33,17 @@ import {
|
|
|
33
33
|
appendInstructionlessHandoffCue,
|
|
34
34
|
removePredecessorHandoffCue,
|
|
35
35
|
} from '@/messages';
|
|
36
|
+
import {
|
|
37
|
+
omitImageBlock,
|
|
38
|
+
isImageContentBlock,
|
|
39
|
+
toOmittedImageBlock,
|
|
40
|
+
} from '@/messages/vision';
|
|
36
41
|
import { createToolHistoryPreparation } from '@/messages/toolHistoryProjection';
|
|
42
|
+
import { flattenLegacyContent, isLegacyConvertible } from '@/messages/content';
|
|
37
43
|
import { isAnthropicLike, isGoogleLike, isOpenAILike } from '@/utils/llm';
|
|
38
44
|
import { annotateMessagesForLLM } from '@/tools/toolOutputReferences';
|
|
39
45
|
import { providerRequiresStrictAlternation } from '@/llm/providers';
|
|
46
|
+
import { hasComputerCallOutputMarker } from '@/utils/toolContent';
|
|
40
47
|
import { getProviderFamily } from '@/llm/providerRegistry';
|
|
41
48
|
import { Providers } from '@/common';
|
|
42
49
|
|
|
@@ -132,6 +139,10 @@ export interface PrepareProviderRequestParams {
|
|
|
132
139
|
maxToolResultChars?: number;
|
|
133
140
|
measure?: (messages: BaseMessage[]) => ProviderPayloadMeasurement;
|
|
134
141
|
toolHistory?: ToolHistoryPreparation;
|
|
142
|
+
/** `false` replaces images with a text marker for a model without image input. */
|
|
143
|
+
vision?: boolean;
|
|
144
|
+
/** Flattens messages the image replacement leaves text-only, as `formatContentStrings` does. */
|
|
145
|
+
useLegacyContent?: boolean;
|
|
135
146
|
}
|
|
136
147
|
|
|
137
148
|
export function usesNativeOpenAIResponses(
|
|
@@ -211,6 +222,12 @@ function resolveProviderMessageProjectionMode(
|
|
|
211
222
|
: 'chat-messages';
|
|
212
223
|
}
|
|
213
224
|
|
|
225
|
+
/** How a request for a model without image input treats images. */
|
|
226
|
+
interface ImageOmission {
|
|
227
|
+
/** Flattens a turn left with only text, as `formatContentStrings` does. */
|
|
228
|
+
legacyContent: boolean;
|
|
229
|
+
}
|
|
230
|
+
|
|
214
231
|
interface ProjectMessagesForProviderParams {
|
|
215
232
|
model: t.ChatModel;
|
|
216
233
|
messages: BaseMessage[];
|
|
@@ -219,6 +236,8 @@ interface ProjectMessagesForProviderParams {
|
|
|
219
236
|
callOptions?: unknown;
|
|
220
237
|
toolHistory?: ToolHistoryPreparation;
|
|
221
238
|
resolve?: ResolveReference;
|
|
239
|
+
vision?: boolean;
|
|
240
|
+
useLegacyContent?: boolean;
|
|
222
241
|
}
|
|
223
242
|
|
|
224
243
|
function isSerializedBuffer(value: object): value is SerializedBuffer {
|
|
@@ -505,7 +524,8 @@ function projectAttachmentsForProvider(
|
|
|
505
524
|
messages: BaseMessage[],
|
|
506
525
|
provider: t.ProviderName,
|
|
507
526
|
projectionMode: ProviderMessageProjectionMode,
|
|
508
|
-
resolve?: ResolveReference
|
|
527
|
+
resolve?: ResolveReference,
|
|
528
|
+
imageOmission?: ImageOmission
|
|
509
529
|
): BaseMessage[] {
|
|
510
530
|
const native =
|
|
511
531
|
provider === Providers.BEDROCK || getProviderFamily(provider) === 'bedrock';
|
|
@@ -517,11 +537,24 @@ function projectAttachmentsForProvider(
|
|
|
517
537
|
continue;
|
|
518
538
|
}
|
|
519
539
|
const projectsTimedMedia = message.getType() !== 'tool';
|
|
540
|
+
/** A native computer-call output keeps its screenshot: the Responses API takes no other output. */
|
|
541
|
+
const omitsImages =
|
|
542
|
+
imageOmission != null && !hasComputerCallOutputMarker(message);
|
|
520
543
|
const sourceContent = message.content as ProviderContentBlock[];
|
|
521
544
|
let content: ProviderContentBlock[] | undefined;
|
|
522
545
|
let displaced: ReturnType<typeof getCacheControl>;
|
|
546
|
+
let omittedImage = false;
|
|
523
547
|
for (let blockIndex = 0; blockIndex < sourceContent.length; blockIndex++) {
|
|
524
548
|
const block = sourceContent[blockIndex];
|
|
549
|
+
const omitted = omitsImages
|
|
550
|
+
? omitImageBlock(block as MessageContentComplex)
|
|
551
|
+
: undefined;
|
|
552
|
+
if (omitted != null && omitted !== block) {
|
|
553
|
+
content ??= copyUsableContentPrefix(sourceContent, blockIndex);
|
|
554
|
+
content.push(omitted);
|
|
555
|
+
omittedImage = true;
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
525
558
|
const resolved =
|
|
526
559
|
resolve != null && isIDContentBlock(block) ? resolve(block) : undefined;
|
|
527
560
|
if (resolved !== undefined) {
|
|
@@ -529,6 +562,9 @@ function projectAttachmentsForProvider(
|
|
|
529
562
|
const marker = getCacheControl(block);
|
|
530
563
|
if (resolved === null) {
|
|
531
564
|
displaced = marker ?? displaced;
|
|
565
|
+
} else if (omitsImages && isImageContentBlock(resolved)) {
|
|
566
|
+
content.push(toOmittedImageBlock(block as MessageContentComplex));
|
|
567
|
+
omittedImage = true;
|
|
532
568
|
} else {
|
|
533
569
|
content.push(
|
|
534
570
|
marker == null ? resolved : withCacheControl(resolved, marker)
|
|
@@ -595,10 +631,15 @@ function projectAttachmentsForProvider(
|
|
|
595
631
|
if (displaced != null) {
|
|
596
632
|
anchorCacheControl(content as MessageContentComplex[], displaced);
|
|
597
633
|
}
|
|
634
|
+
const projectedContent = content as MessageContentComplex[];
|
|
598
635
|
projected ??= [...messages];
|
|
599
636
|
projected[messageIndex] = cloneMessage(
|
|
600
637
|
message,
|
|
601
|
-
|
|
638
|
+
omittedImage &&
|
|
639
|
+
imageOmission?.legacyContent === true &&
|
|
640
|
+
isLegacyConvertible(message, projectedContent)
|
|
641
|
+
? flattenLegacyContent(projectedContent)
|
|
642
|
+
: projectedContent
|
|
602
643
|
);
|
|
603
644
|
}
|
|
604
645
|
return projected ?? messages;
|
|
@@ -611,11 +652,14 @@ function projectMessagesForProviderMode(
|
|
|
611
652
|
maxToolResultChars,
|
|
612
653
|
toolHistory,
|
|
613
654
|
resolve,
|
|
655
|
+
vision,
|
|
656
|
+
useLegacyContent,
|
|
614
657
|
}: ProjectMessagesForProviderParams,
|
|
615
658
|
projectionMode: ProviderMessageProjectionMode
|
|
616
659
|
): BaseMessage[] {
|
|
617
660
|
const providerFamily = getProviderFamily(provider);
|
|
618
661
|
const nativeOpenAIResponses = projectionMode === 'openai-responses';
|
|
662
|
+
/** Images go before tool results are serialized, so none lands in them as base64 text. */
|
|
619
663
|
const providerInputMessages = projectAttachmentsForProvider(
|
|
620
664
|
projectToolStreamContentForProvider(
|
|
621
665
|
messages,
|
|
@@ -625,7 +669,8 @@ function projectMessagesForProviderMode(
|
|
|
625
669
|
),
|
|
626
670
|
provider,
|
|
627
671
|
projectionMode,
|
|
628
|
-
resolve
|
|
672
|
+
resolve,
|
|
673
|
+
vision === false ? { legacyContent: useLegacyContent === true } : undefined
|
|
629
674
|
);
|
|
630
675
|
if (nativeOpenAIResponses) {
|
|
631
676
|
return projectOpenAIResponsesToolMessageContent(
|
|
@@ -736,6 +781,8 @@ export function prepareProviderRequest({
|
|
|
736
781
|
maxToolResultChars,
|
|
737
782
|
measure,
|
|
738
783
|
toolHistory = createToolHistoryPreparation(),
|
|
784
|
+
vision,
|
|
785
|
+
useLegacyContent,
|
|
739
786
|
}: PrepareProviderRequestParams): PreparedProviderRequest {
|
|
740
787
|
const projectionMode = resolveProviderMessageProjectionMode(
|
|
741
788
|
model,
|
|
@@ -752,6 +799,8 @@ export function prepareProviderRequest({
|
|
|
752
799
|
maxToolResultChars,
|
|
753
800
|
callOptions: config,
|
|
754
801
|
toolHistory,
|
|
802
|
+
vision,
|
|
803
|
+
useLegacyContent,
|
|
755
804
|
resolve:
|
|
756
805
|
resolveAttachment == null
|
|
757
806
|
? undefined
|
package/src/messages/content.ts
CHANGED
|
@@ -12,18 +12,22 @@ import { cloneMessage } from './cache';
|
|
|
12
12
|
/**
|
|
13
13
|
* Whether {@link formatContentStrings} will flatten this message's content:
|
|
14
14
|
* a human/ai/system message whose content is an array of text-only blocks.
|
|
15
|
+
* Pass `content` to judge replacement content before cloning the message.
|
|
15
16
|
*/
|
|
16
|
-
export const isLegacyConvertible = (
|
|
17
|
+
export const isLegacyConvertible = (
|
|
18
|
+
message: BaseMessage,
|
|
19
|
+
content: BaseMessage['content'] | MessageContentComplex[] = message.content
|
|
20
|
+
): boolean => {
|
|
17
21
|
const messageType = message.getType();
|
|
18
22
|
const isValidMessage =
|
|
19
23
|
messageType === 'human' || messageType === 'ai' || messageType === 'system';
|
|
20
24
|
if (!isValidMessage) {
|
|
21
25
|
return false;
|
|
22
26
|
}
|
|
23
|
-
if (!Array.isArray(
|
|
27
|
+
if (!Array.isArray(content)) {
|
|
24
28
|
return false;
|
|
25
29
|
}
|
|
26
|
-
return
|
|
30
|
+
return content.every((block) => block.type === ContentTypes.TEXT);
|
|
27
31
|
};
|
|
28
32
|
|
|
29
33
|
/** Joins the text of {@link isLegacyConvertible} content blocks into the exact
|
|
@@ -63,7 +67,9 @@ export const formatContentStrings = (
|
|
|
63
67
|
isGeminiReplayParts(message.additional_kwargs[GEMINI_PARTS]);
|
|
64
68
|
const content = preserveNativeText
|
|
65
69
|
? blocks
|
|
66
|
-
.map((block) =>
|
|
70
|
+
.map((block) =>
|
|
71
|
+
'text' in block && typeof block.text === 'string' ? block.text : ''
|
|
72
|
+
)
|
|
67
73
|
.join('')
|
|
68
74
|
: flattenLegacyContent(blocks);
|
|
69
75
|
result.push(cloneMessage(message, content));
|