@librechat/agents 4.2.3 → 4.2.5
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/harness/index.cjs +123 -0
- package/dist/cjs/harness/index.cjs.map +1 -0
- package/dist/cjs/llm/google/index.cjs +27 -6
- package/dist/cjs/llm/google/index.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/common.cjs +27 -14
- package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
- package/dist/cjs/llm/google/utils/signatures.cjs +137 -0
- package/dist/cjs/llm/google/utils/signatures.cjs.map +1 -0
- package/dist/cjs/llm/providerTextBoundary.cjs +3 -0
- package/dist/cjs/llm/providerTextBoundary.cjs.map +1 -1
- package/dist/cjs/llm/vertexai/index.cjs +90 -7
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
- package/dist/cjs/main.cjs +8 -0
- package/dist/cjs/messages/content.cjs +4 -1
- package/dist/cjs/messages/content.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +11 -8
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/esm/harness/index.mjs +123 -0
- package/dist/esm/harness/index.mjs.map +1 -0
- package/dist/esm/llm/google/index.mjs +23 -7
- package/dist/esm/llm/google/index.mjs.map +1 -1
- package/dist/esm/llm/google/utils/common.mjs +27 -14
- package/dist/esm/llm/google/utils/common.mjs.map +1 -1
- package/dist/esm/llm/google/utils/signatures.mjs +132 -0
- package/dist/esm/llm/google/utils/signatures.mjs.map +1 -0
- package/dist/esm/llm/providerTextBoundary.mjs +3 -0
- package/dist/esm/llm/providerTextBoundary.mjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs +91 -8
- package/dist/esm/llm/vertexai/index.mjs.map +1 -1
- package/dist/esm/main.mjs +3 -1
- package/dist/esm/messages/content.mjs +4 -1
- package/dist/esm/messages/content.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +11 -8
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/types/harness/index.d.ts +56 -0
- package/dist/types/index.d.ts +4 -1
- package/dist/types/llm/google/index.d.ts +4 -2
- package/dist/types/llm/google/utils/common.d.ts +5 -1
- package/dist/types/llm/google/utils/signatures.d.ts +23 -0
- package/dist/types/llm/vertexai/index.d.ts +2 -17
- package/dist/types/messages/format.d.ts +3 -1
- package/dist/types/types/llm.d.ts +2 -2
- package/package.json +1 -1
- package/src/harness/index.ts +226 -0
- package/src/index.ts +16 -1
- package/src/llm/google/index.ts +20 -6
- package/src/llm/google/utils/common.ts +23 -10
- package/src/llm/google/utils/signatures.ts +215 -0
- package/src/llm/providerTextBoundary.ts +3 -0
- package/src/llm/vertexai/index.ts +127 -39
- package/src/messages/content.ts +14 -6
- package/src/messages/format.ts +42 -29
- package/src/types/llm.ts +10 -2
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import type { EventHandler, LCTool, ToolCallRequest, ToolExecuteBatchRequest, ToolExecuteResult } from '@/types';
|
|
2
|
+
/** Established by the authenticated host, never inferred from model arguments. */
|
|
3
|
+
export interface ExecutionIdentity {
|
|
4
|
+
runtimeId: string;
|
|
5
|
+
agentId: string;
|
|
6
|
+
runId: string;
|
|
7
|
+
userId: string;
|
|
8
|
+
tenantId?: string;
|
|
9
|
+
conversationId?: string;
|
|
10
|
+
}
|
|
11
|
+
/** A host reference selects an authorized implementation, not a URL or executable name. */
|
|
12
|
+
export interface HostToolDescriptor {
|
|
13
|
+
definition: LCTool;
|
|
14
|
+
reference: string;
|
|
15
|
+
}
|
|
16
|
+
export interface HostToolInvocation {
|
|
17
|
+
reference: string;
|
|
18
|
+
call: ToolCallRequest;
|
|
19
|
+
}
|
|
20
|
+
export interface HostToolRequest {
|
|
21
|
+
version: 1;
|
|
22
|
+
identity: Readonly<ExecutionIdentity>;
|
|
23
|
+
calls: HostToolInvocation[];
|
|
24
|
+
/** SDK execution subject; distinct from the authenticated root binding. */
|
|
25
|
+
executingAgentId?: string;
|
|
26
|
+
executionContext?: ToolExecuteBatchRequest['executionContext'];
|
|
27
|
+
callerCapabilityProjection?: ToolExecuteBatchRequest['callerCapabilityProjection'];
|
|
28
|
+
}
|
|
29
|
+
export interface HostToolSessionUpdate {
|
|
30
|
+
toolCallId: string;
|
|
31
|
+
context: Pick<ToolCallRequest, 'codeSessionContext' | 'codeSessionBaselineId' | 'retainCodeSessionInputs' | 'runtimeSessionHint'>;
|
|
32
|
+
}
|
|
33
|
+
export interface HostToolEvents {
|
|
34
|
+
onResult?: (result: ToolExecuteResult) => void;
|
|
35
|
+
onSessionUpdate: (update: HostToolSessionUpdate) => void;
|
|
36
|
+
}
|
|
37
|
+
/** Implementations authenticate the transport, authorize every reference, and validate arguments. */
|
|
38
|
+
export interface HostToolService {
|
|
39
|
+
version: 1;
|
|
40
|
+
invoke(request: HostToolRequest, signal?: AbortSignal, events?: HostToolEvents): Promise<ToolExecuteResult[]>;
|
|
41
|
+
}
|
|
42
|
+
export interface HostToolBindings {
|
|
43
|
+
toolDefinitions: LCTool[];
|
|
44
|
+
customHandlers: Record<string, EventHandler>;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Binds model-visible schemas and host execution as one capability. The SDK's existing ToolNode
|
|
48
|
+
* owns policy, approvals, edited arguments, result ordering and lifecycle events in both modes.
|
|
49
|
+
* This adapter never retries execution; transports may deduplicate using execution identity and
|
|
50
|
+
* tool-call identity, but must reconcile uncertain external effects rather than blindly rerun.
|
|
51
|
+
*/
|
|
52
|
+
export declare function createHostToolBindings(options: {
|
|
53
|
+
identity: Readonly<ExecutionIdentity>;
|
|
54
|
+
tools: readonly HostToolDescriptor[];
|
|
55
|
+
service: HostToolService;
|
|
56
|
+
}): HostToolBindings;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -62,5 +62,8 @@ export { DEFAULT_MAX_TOOL_CALL_ARG_BYTES, StreamLimitExceededError, resolveStrea
|
|
|
62
62
|
export type { StreamedToolCallArgTally, ResolvedStreamLimits, StreamLimitState, StreamLimitKind, } from './llm/streamLimits';
|
|
63
63
|
export * from './decisions';
|
|
64
64
|
export * from './rerank';
|
|
65
|
-
export { PROVIDER_TEXT_PROTECTION_VERSION, ProviderTextProtectionError } from './protection/providerText';
|
|
65
|
+
export { PROVIDER_TEXT_PROTECTION_VERSION, ProviderTextProtectionError, } from './protection/providerText';
|
|
66
66
|
export type { ProviderTextProtection, ProviderTextProtectionResult, ProviderTextProtectionErrorCode, } from './protection/providerText';
|
|
67
|
+
export { GEMINI_PARTS, GEMINI_REPLAY_UNSUPPORTED, captureGeminiReplayParts, restoreGeminiReplayParts, isGeminiReplayParts, } from './llm/google/utils/signatures';
|
|
68
|
+
export type { GeminiReplayPart } from './llm/google/utils/signatures';
|
|
69
|
+
export * from './harness';
|
|
@@ -3,10 +3,10 @@ import { ChatGoogleGenerativeAI } from '@langchain/google-genai';
|
|
|
3
3
|
import type { GenerateContentRequest } from '@google/generative-ai';
|
|
4
4
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
5
5
|
import type { BaseMessage } from '@langchain/core/messages';
|
|
6
|
-
import type { GoogleClientOptions
|
|
6
|
+
import type { GoogleClientOptions } from '@/types';
|
|
7
7
|
export declare class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
|
|
8
8
|
_lc_stream_delay: number;
|
|
9
|
-
thinkingConfig?:
|
|
9
|
+
thinkingConfig?: ChatGoogleGenerativeAI['thinkingConfig'];
|
|
10
10
|
includeServerSideToolInvocations?: boolean;
|
|
11
11
|
/**
|
|
12
12
|
* Override to add gemini-3 model support for multimodal and function calling thought signatures
|
|
@@ -25,3 +25,5 @@ export declare class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI
|
|
|
25
25
|
_streamResponseChunks(messages: BaseMessage[], options: this['ParsedCallOptions'], runManager?: CallbackManagerForLLMRun): AsyncGenerator<ChatGenerationChunk>;
|
|
26
26
|
private _streamProviderChunks;
|
|
27
27
|
}
|
|
28
|
+
export { GEMINI_PARTS, GEMINI_REPLAY_UNSUPPORTED, captureGeminiReplayParts, restoreGeminiReplayParts, isGeminiReplayParts, } from './utils/signatures';
|
|
29
|
+
export type { GeminiReplayPart } from './utils/signatures';
|
|
@@ -31,15 +31,19 @@ export declare function rejectsModelTurnPrefill(model?: string): boolean;
|
|
|
31
31
|
* HTTP 400 for it, so we drop it and let the model generate fresh from the
|
|
32
32
|
* preceding user turn. No-op for every other model, preserving working prefill.
|
|
33
33
|
*/
|
|
34
|
-
export declare function dropUnsupportedModelTurnPrefill
|
|
34
|
+
export declare function dropUnsupportedModelTurnPrefill<T extends {
|
|
35
|
+
role: string;
|
|
36
|
+
}>(contents: T[] | undefined, model?: string): T[] | undefined;
|
|
35
37
|
export declare function convertResponseContentToChatGenerationChunk(response: EnhancedGenerateContentResponse, extra: {
|
|
36
38
|
usageMetadata?: UsageMetadata | undefined;
|
|
37
39
|
index: number;
|
|
40
|
+
model?: string;
|
|
38
41
|
}): ChatGenerationChunk | null;
|
|
39
42
|
/**
|
|
40
43
|
* Maps a Google GenerateContentResult to a LangChain ChatResult
|
|
41
44
|
*/
|
|
42
45
|
export declare function mapGenerateContentResultToChatResult(response: EnhancedGenerateContentResponse, extra?: {
|
|
43
46
|
usageMetadata: UsageMetadata | undefined;
|
|
47
|
+
model?: string;
|
|
44
48
|
}): ChatResult;
|
|
45
49
|
export declare function convertToGenerativeAITools(tools: GoogleGenerativeAIToolType[]): GoogleGenerativeAIFunctionDeclarationsTool[];
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
2
|
+
import type { GeminiPart } from '@langchain/google-common';
|
|
3
|
+
import type { Part } from '@google/generative-ai';
|
|
4
|
+
export declare const GEMINI_PARTS = "__gemini_parts__";
|
|
5
|
+
export declare const GEMINI_REPLAY_UNSUPPORTED = "__gemini_replay_unsupported__";
|
|
6
|
+
export declare const GEMINI_SIGNATURES = "__gemini_function_call_thought_signatures__";
|
|
7
|
+
export type GeminiReplayPart = {
|
|
8
|
+
type: 'text';
|
|
9
|
+
length: number;
|
|
10
|
+
hash: string;
|
|
11
|
+
thought?: boolean;
|
|
12
|
+
thoughtSignature?: string;
|
|
13
|
+
} | {
|
|
14
|
+
type: 'function_call';
|
|
15
|
+
id: string;
|
|
16
|
+
hash: string;
|
|
17
|
+
thoughtSignature?: string;
|
|
18
|
+
};
|
|
19
|
+
export declare function isGeminiReplayParts(value: unknown): value is GeminiReplayPart[];
|
|
20
|
+
/** Captures native boundaries without duplicating prose. Known Gemini 1/2 models retain their existing signature formats for cross-model compatibility. */
|
|
21
|
+
export declare function captureGeminiReplayParts(message: BaseMessage, parts: readonly (Part | GeminiPart)[], model?: string): void;
|
|
22
|
+
/** Returns original parts only when the current message still matches every text hash and call id. */
|
|
23
|
+
export declare function restoreGeminiReplayParts(message: BaseMessage, replay?: unknown): GeminiPart[] | undefined;
|
|
@@ -2,8 +2,8 @@ import { ChatGoogle } from '@langchain/google-gauth';
|
|
|
2
2
|
import { AIMessageChunk } from '@langchain/core/messages';
|
|
3
3
|
import type { GeminiContent, GoogleAIModelRequestParams, GoogleAbstractedClient } from '@langchain/google-common';
|
|
4
4
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
5
|
-
import type { BaseMessage, UsageMetadata } from '@langchain/core/messages';
|
|
6
5
|
import type { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
6
|
+
import type { BaseMessage, UsageMetadata } from '@langchain/core/messages';
|
|
7
7
|
import type { GoogleThinkingConfig, VertexAIClientOptions } from '@/types';
|
|
8
8
|
/**
|
|
9
9
|
* `@langchain/google-common`'s `_streamResponseChunks` emits usage on TWO
|
|
@@ -39,22 +39,7 @@ export declare function repairStreamUsageMetadata(current: UsageMetadata | undef
|
|
|
39
39
|
* the parse check is left unstamped and falls back to the lazy path.
|
|
40
40
|
*/
|
|
41
41
|
export declare function sealCompleteStreamedToolCalls(message: AIMessageChunk): void;
|
|
42
|
-
/**
|
|
43
|
-
* Fixes thought signatures on functionCall parts in the formatted Gemini request.
|
|
44
|
-
*
|
|
45
|
-
* `@langchain/google-common` stores signatures as a flat array in
|
|
46
|
-
* `additional_kwargs.signatures` (one per response part) and re-attaches them
|
|
47
|
-
* by index only when `signatures.length === parts.length`. This fails when:
|
|
48
|
-
* - The API omits a signature (length mismatch)
|
|
49
|
-
* - Streaming chunks merge with different part counts
|
|
50
|
-
* - The signature for a functionCall part is an empty string
|
|
51
|
-
*
|
|
52
|
-
* This function correlates each "model" content block in the formatted request
|
|
53
|
-
* back to its originating AI message by *position*, then re-attaches non-empty
|
|
54
|
-
* signatures that the library failed to apply. AI messages without signatures
|
|
55
|
-
* still consume their slot — filtering them out shifted later messages onto
|
|
56
|
-
* the wrong content block and dropped real signatures on the floor.
|
|
57
|
-
*/
|
|
42
|
+
/** Restores native part order. Legacy text with merged positional slots has lost its boundaries and requires regeneration. */
|
|
58
43
|
export declare function fixThoughtSignatures(contents: GeminiContent[], input: BaseMessage[]): void;
|
|
59
44
|
/**
|
|
60
45
|
* Integration with Google Vertex AI chat models.
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { AIMessage, ToolMessage, BaseMessage, HumanMessage, SystemMessage } from '@langchain/core/messages';
|
|
2
2
|
import type { MessageContentImageUrl } from '@langchain/core/messages';
|
|
3
3
|
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
4
|
-
import type { ToolHistoryPreparation } from './toolHistoryProjection';
|
|
5
4
|
import type { MessageContentComplex, TPayload, ProviderName, CompactionSemanticIndex, CompactionSemanticIndexSnapshot } from '@/types';
|
|
5
|
+
import type { ToolHistoryPreparation } from './toolHistoryProjection';
|
|
6
6
|
import { Providers } from '@/common';
|
|
7
7
|
interface MediaMessageParams {
|
|
8
8
|
message: {
|
|
@@ -102,6 +102,8 @@ export interface FormatAgentMessagesOptions {
|
|
|
102
102
|
* tool-call messages. Explicit opt-in for OpenAI-compatible endpoints that
|
|
103
103
|
* replay reasoning across turns; defaults to on for DeepSeek thinking-mode. */
|
|
104
104
|
preserveReasoningContent?: boolean;
|
|
105
|
+
/** Preserve exact assistant text bytes for provider signature replay. */
|
|
106
|
+
preserveTextContent?: boolean;
|
|
105
107
|
/** Skill names already primed fresh this turn (manual/always-apply). Their
|
|
106
108
|
* historical `skill` tool_calls are not reconstructed into a HumanMessage,
|
|
107
109
|
* so the same SKILL.md body is not injected twice in one request. */
|
|
@@ -51,7 +51,7 @@ export type AnthropicReasoning = {
|
|
|
51
51
|
export type GoogleThinkingConfig = {
|
|
52
52
|
thinkingBudget?: number;
|
|
53
53
|
includeThoughts?: boolean;
|
|
54
|
-
thinkingLevel?: 'THINKING_LEVEL_UNSPECIFIED' | 'LOW' | 'MEDIUM' | 'HIGH';
|
|
54
|
+
thinkingLevel?: 'THINKING_LEVEL_UNSPECIFIED' | 'LOW' | 'MEDIUM' | 'HIGH' | 'XHIGH';
|
|
55
55
|
};
|
|
56
56
|
/** GPT-5.6 managed-request passthrough fields, shared by the OpenAI and
|
|
57
57
|
* Azure wrappers that both read them. */
|
|
@@ -122,7 +122,7 @@ export type BedrockAnthropicInput = ChatBedrockConverseInput & StreamSmoothingOp
|
|
|
122
122
|
};
|
|
123
123
|
export type BedrockConverseClientOptions = BedrockAnthropicInput;
|
|
124
124
|
export type BedrockAnthropicClientOptions = BedrockAnthropicInput;
|
|
125
|
-
export type GoogleClientOptions = GoogleGenerativeAIChatInput & StreamSmoothingOptions & {
|
|
125
|
+
export type GoogleClientOptions = Omit<GoogleGenerativeAIChatInput, 'thinkingConfig'> & StreamSmoothingOptions & {
|
|
126
126
|
customHeaders?: RequestOptions['customHeaders'];
|
|
127
127
|
thinkingConfig?: GoogleThinkingConfig;
|
|
128
128
|
includeServerSideToolInvocations?: boolean;
|
package/package.json
CHANGED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
EventHandler,
|
|
3
|
+
LCTool,
|
|
4
|
+
ToolCallRequest,
|
|
5
|
+
ToolExecuteBatchRequest,
|
|
6
|
+
ToolExecuteResult,
|
|
7
|
+
} from '@/types';
|
|
8
|
+
import { GraphEvents } from '@/common';
|
|
9
|
+
|
|
10
|
+
/** Established by the authenticated host, never inferred from model arguments. */
|
|
11
|
+
export interface ExecutionIdentity {
|
|
12
|
+
runtimeId: string;
|
|
13
|
+
agentId: string;
|
|
14
|
+
runId: string;
|
|
15
|
+
userId: string;
|
|
16
|
+
tenantId?: string;
|
|
17
|
+
conversationId?: string;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A host reference selects an authorized implementation, not a URL or executable name. */
|
|
21
|
+
export interface HostToolDescriptor {
|
|
22
|
+
definition: LCTool;
|
|
23
|
+
reference: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface HostToolInvocation {
|
|
27
|
+
reference: string;
|
|
28
|
+
call: ToolCallRequest;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export interface HostToolRequest {
|
|
32
|
+
version: 1;
|
|
33
|
+
identity: Readonly<ExecutionIdentity>;
|
|
34
|
+
calls: HostToolInvocation[];
|
|
35
|
+
/** SDK execution subject; distinct from the authenticated root binding. */
|
|
36
|
+
executingAgentId?: string;
|
|
37
|
+
executionContext?: ToolExecuteBatchRequest['executionContext'];
|
|
38
|
+
callerCapabilityProjection?: ToolExecuteBatchRequest['callerCapabilityProjection'];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface HostToolSessionUpdate {
|
|
42
|
+
toolCallId: string;
|
|
43
|
+
context: Pick<
|
|
44
|
+
ToolCallRequest,
|
|
45
|
+
| 'codeSessionContext'
|
|
46
|
+
| 'codeSessionBaselineId'
|
|
47
|
+
| 'retainCodeSessionInputs'
|
|
48
|
+
| 'runtimeSessionHint'
|
|
49
|
+
>;
|
|
50
|
+
}
|
|
51
|
+
export interface HostToolEvents {
|
|
52
|
+
onResult?: (result: ToolExecuteResult) => void;
|
|
53
|
+
onSessionUpdate: (update: HostToolSessionUpdate) => void;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** Implementations authenticate the transport, authorize every reference, and validate arguments. */
|
|
57
|
+
export interface HostToolService {
|
|
58
|
+
version: 1;
|
|
59
|
+
invoke(
|
|
60
|
+
request: HostToolRequest,
|
|
61
|
+
signal?: AbortSignal,
|
|
62
|
+
events?: HostToolEvents
|
|
63
|
+
): Promise<ToolExecuteResult[]>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export interface HostToolBindings {
|
|
67
|
+
toolDefinitions: LCTool[];
|
|
68
|
+
customHandlers: Record<string, EventHandler>;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Binds model-visible schemas and host execution as one capability. The SDK's existing ToolNode
|
|
73
|
+
* owns policy, approvals, edited arguments, result ordering and lifecycle events in both modes.
|
|
74
|
+
* This adapter never retries execution; transports may deduplicate using execution identity and
|
|
75
|
+
* tool-call identity, but must reconcile uncertain external effects rather than blindly rerun.
|
|
76
|
+
*/
|
|
77
|
+
export function createHostToolBindings(options: {
|
|
78
|
+
identity: Readonly<ExecutionIdentity>;
|
|
79
|
+
tools: readonly HostToolDescriptor[];
|
|
80
|
+
service: HostToolService;
|
|
81
|
+
}): HostToolBindings {
|
|
82
|
+
if (Number(options.service.version) !== 1)
|
|
83
|
+
throw new Error('Unsupported host tool service version');
|
|
84
|
+
const source = options.identity;
|
|
85
|
+
const identity = Object.freeze({
|
|
86
|
+
runtimeId: source.runtimeId,
|
|
87
|
+
agentId: source.agentId,
|
|
88
|
+
runId: source.runId,
|
|
89
|
+
userId: source.userId,
|
|
90
|
+
tenantId: source.tenantId,
|
|
91
|
+
conversationId: source.conversationId,
|
|
92
|
+
});
|
|
93
|
+
for (const key of ['runtimeId', 'agentId', 'runId', 'userId'] as const) {
|
|
94
|
+
if (identity[key].trim().length === 0)
|
|
95
|
+
throw new Error('Incomplete execution identity');
|
|
96
|
+
}
|
|
97
|
+
const references = new Map<string, string>();
|
|
98
|
+
const toolDefinitions: LCTool[] = [];
|
|
99
|
+
for (const descriptor of options.tools) {
|
|
100
|
+
const { definition, reference } = descriptor;
|
|
101
|
+
if (
|
|
102
|
+
!definition.name.trim() ||
|
|
103
|
+
!reference.trim() ||
|
|
104
|
+
references.has(definition.name)
|
|
105
|
+
) {
|
|
106
|
+
throw new Error('Invalid or duplicate host tool descriptor');
|
|
107
|
+
}
|
|
108
|
+
references.set(definition.name, reference);
|
|
109
|
+
toolDefinitions.push(structuredClone(definition));
|
|
110
|
+
}
|
|
111
|
+
const service = options.service;
|
|
112
|
+
return {
|
|
113
|
+
toolDefinitions,
|
|
114
|
+
customHandlers: {
|
|
115
|
+
[GraphEvents.ON_TOOL_EXECUTE]: {
|
|
116
|
+
handle: async (_event, value): Promise<void> => {
|
|
117
|
+
const batch = value as ToolExecuteBatchRequest;
|
|
118
|
+
try {
|
|
119
|
+
batch.signal?.throwIfAborted();
|
|
120
|
+
const ids = new Set<string>();
|
|
121
|
+
const calls = batch.toolCalls.map((call): HostToolInvocation => {
|
|
122
|
+
const reference = references.get(call.name);
|
|
123
|
+
if (
|
|
124
|
+
reference === undefined ||
|
|
125
|
+
call.id.length === 0 ||
|
|
126
|
+
ids.has(call.id)
|
|
127
|
+
) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
'Host tool invocation is outside the bound capability'
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
ids.add(call.id);
|
|
133
|
+
return { reference, call: structuredClone(call) };
|
|
134
|
+
});
|
|
135
|
+
let accepting = true;
|
|
136
|
+
const earlyState = { invalid: false };
|
|
137
|
+
const earlyIds = new Set<string>();
|
|
138
|
+
const receipt = (result: ToolExecuteResult): ToolExecuteResult => {
|
|
139
|
+
if (
|
|
140
|
+
!ids.has(result.toolCallId) ||
|
|
141
|
+
!['success', 'error'].includes(result.status)
|
|
142
|
+
)
|
|
143
|
+
throw new Error('Invalid host tool result identity');
|
|
144
|
+
const copy = { ...result };
|
|
145
|
+
delete copy.received_at;
|
|
146
|
+
return copy;
|
|
147
|
+
};
|
|
148
|
+
const publishResult = (result: ToolExecuteResult): void => {
|
|
149
|
+
if (!accepting || batch.signal?.aborted === true) return;
|
|
150
|
+
try {
|
|
151
|
+
const value = receipt(result);
|
|
152
|
+
if (earlyIds.has(value.toolCallId)) return;
|
|
153
|
+
earlyIds.add(value.toolCallId);
|
|
154
|
+
batch.onResult?.(value);
|
|
155
|
+
} catch {
|
|
156
|
+
earlyState.invalid = true;
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
const onResult = batch.onResult ? publishResult : undefined;
|
|
160
|
+
const byCallId = new Map(calls.map(({ call }) => [call.id, call]));
|
|
161
|
+
const reconcile = (
|
|
162
|
+
target: ToolCallRequest,
|
|
163
|
+
context: HostToolSessionUpdate['context']
|
|
164
|
+
): void => {
|
|
165
|
+
for (const key of [
|
|
166
|
+
'codeSessionContext',
|
|
167
|
+
'codeSessionBaselineId',
|
|
168
|
+
'retainCodeSessionInputs',
|
|
169
|
+
'runtimeSessionHint',
|
|
170
|
+
] as const) {
|
|
171
|
+
if (key in context)
|
|
172
|
+
Object.assign(target, {
|
|
173
|
+
[key]: structuredClone(context[key]),
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
};
|
|
177
|
+
const onSessionUpdate = (update: HostToolSessionUpdate): void => {
|
|
178
|
+
if (!accepting) return;
|
|
179
|
+
const call = byCallId.get(update.toolCallId);
|
|
180
|
+
if (!call) {
|
|
181
|
+
earlyState.invalid = true;
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
reconcile(call, update.context);
|
|
185
|
+
};
|
|
186
|
+
let results: ToolExecuteResult[];
|
|
187
|
+
try {
|
|
188
|
+
results = await service.invoke(
|
|
189
|
+
{
|
|
190
|
+
version: 1,
|
|
191
|
+
identity,
|
|
192
|
+
calls,
|
|
193
|
+
executingAgentId: batch.agentId,
|
|
194
|
+
executionContext: batch.executionContext,
|
|
195
|
+
callerCapabilityProjection: batch.callerCapabilityProjection,
|
|
196
|
+
},
|
|
197
|
+
batch.signal,
|
|
198
|
+
{ onResult, onSessionUpdate }
|
|
199
|
+
);
|
|
200
|
+
} finally {
|
|
201
|
+
accepting = false;
|
|
202
|
+
for (const original of batch.toolCalls) {
|
|
203
|
+
const updated = byCallId.get(original.id);
|
|
204
|
+
if (updated) reconcile(original, updated);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
if (earlyState.invalid)
|
|
208
|
+
throw new Error('Invalid incremental host result');
|
|
209
|
+
batch.signal?.throwIfAborted();
|
|
210
|
+
if (results.length !== ids.size)
|
|
211
|
+
throw new Error('Incomplete host tool results');
|
|
212
|
+
const byId = new Map<string, ToolExecuteResult>();
|
|
213
|
+
for (const result of results) {
|
|
214
|
+
if (byId.has(result.toolCallId))
|
|
215
|
+
throw new Error('Duplicate host tool result');
|
|
216
|
+
byId.set(result.toolCallId, receipt(result));
|
|
217
|
+
}
|
|
218
|
+
batch.resolve(calls.map(({ call }) => byId.get(call.id)!));
|
|
219
|
+
} catch {
|
|
220
|
+
batch.reject(new Error('Host tool execution failed'));
|
|
221
|
+
}
|
|
222
|
+
},
|
|
223
|
+
},
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -123,9 +123,24 @@ export type {
|
|
|
123
123
|
export * from './decisions';
|
|
124
124
|
export * from './rerank';
|
|
125
125
|
|
|
126
|
-
export {
|
|
126
|
+
export {
|
|
127
|
+
PROVIDER_TEXT_PROTECTION_VERSION,
|
|
128
|
+
ProviderTextProtectionError,
|
|
129
|
+
} from './protection/providerText';
|
|
127
130
|
export type {
|
|
128
131
|
ProviderTextProtection,
|
|
129
132
|
ProviderTextProtectionResult,
|
|
130
133
|
ProviderTextProtectionErrorCode,
|
|
131
134
|
} from './protection/providerText';
|
|
135
|
+
|
|
136
|
+
export {
|
|
137
|
+
GEMINI_PARTS,
|
|
138
|
+
GEMINI_REPLAY_UNSUPPORTED,
|
|
139
|
+
captureGeminiReplayParts,
|
|
140
|
+
restoreGeminiReplayParts,
|
|
141
|
+
isGeminiReplayParts,
|
|
142
|
+
} from './llm/google/utils/signatures';
|
|
143
|
+
export type { GeminiReplayPart } from './llm/google/utils/signatures';
|
|
144
|
+
|
|
145
|
+
/* Host execution harness */
|
|
146
|
+
export * from './harness';
|
package/src/llm/google/index.ts
CHANGED
|
@@ -15,7 +15,7 @@ import type {
|
|
|
15
15
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
16
16
|
import type { BaseMessage, UsageMetadata } from '@langchain/core/messages';
|
|
17
17
|
import type { GeminiApiUsageMetadata, InputTokenDetails } from './types';
|
|
18
|
-
import type { GoogleClientOptions
|
|
18
|
+
import type { GoogleClientOptions } from '@/types';
|
|
19
19
|
import {
|
|
20
20
|
convertResponseContentToChatGenerationChunk,
|
|
21
21
|
convertBaseMessagesToContent,
|
|
@@ -40,7 +40,7 @@ type GoogleToolConfigWithServerSideInvocations = ToolConfig & {
|
|
|
40
40
|
|
|
41
41
|
export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
|
|
42
42
|
_lc_stream_delay: number;
|
|
43
|
-
thinkingConfig?:
|
|
43
|
+
thinkingConfig?: ChatGoogleGenerativeAI['thinkingConfig'];
|
|
44
44
|
includeServerSideToolInvocations?: boolean;
|
|
45
45
|
|
|
46
46
|
/**
|
|
@@ -58,7 +58,10 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
|
|
|
58
58
|
}
|
|
59
59
|
|
|
60
60
|
constructor(fields: GoogleClientOptions) {
|
|
61
|
-
|
|
61
|
+
// The upstream type omits Hydrogen's XHIGH level; the API accepts it unchanged.
|
|
62
|
+
const thinkingConfig =
|
|
63
|
+
fields.thinkingConfig as ChatGoogleGenerativeAI['thinkingConfig'];
|
|
64
|
+
super({ ...fields, thinkingConfig });
|
|
62
65
|
|
|
63
66
|
this._lc_stream_delay = resolveStreamDelay(fields._lc_stream_delay);
|
|
64
67
|
this.model = fields.model.replace(/^models\//, '');
|
|
@@ -115,7 +118,7 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
|
|
|
115
118
|
}
|
|
116
119
|
}
|
|
117
120
|
|
|
118
|
-
this.thinkingConfig =
|
|
121
|
+
this.thinkingConfig = thinkingConfig ?? this.thinkingConfig;
|
|
119
122
|
this.includeServerSideToolInvocations =
|
|
120
123
|
fields.includeServerSideToolInvocations ??
|
|
121
124
|
this.includeServerSideToolInvocations;
|
|
@@ -172,7 +175,7 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
|
|
|
172
175
|
total_tokens: usageMetadata.totalTokenCount ?? 0,
|
|
173
176
|
};
|
|
174
177
|
|
|
175
|
-
if (usageMetadata.cachedContentTokenCount) {
|
|
178
|
+
if ((usageMetadata.cachedContentTokenCount ?? 0) > 0) {
|
|
176
179
|
output.input_token_details ??= {};
|
|
177
180
|
output.input_token_details.cache_read =
|
|
178
181
|
usageMetadata.cachedContentTokenCount;
|
|
@@ -188,7 +191,7 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
|
|
|
188
191
|
0,
|
|
189
192
|
(usageMetadata.cachedContentTokenCount ?? 0) - 200000
|
|
190
193
|
);
|
|
191
|
-
if (over200k) {
|
|
194
|
+
if (over200k > 0) {
|
|
192
195
|
output.input_token_details = {
|
|
193
196
|
...output.input_token_details,
|
|
194
197
|
over_200k: over200k,
|
|
@@ -285,6 +288,7 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
|
|
|
285
288
|
/** @ts-ignore */
|
|
286
289
|
const generationResult = mapGenerateContentResultToChatResult(response, {
|
|
287
290
|
usageMetadata,
|
|
291
|
+
model: this.model,
|
|
288
292
|
});
|
|
289
293
|
|
|
290
294
|
await runManager?.handleLLMNewToken(
|
|
@@ -359,6 +363,7 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
|
|
|
359
363
|
|
|
360
364
|
const chunk = convertResponseContentToChatGenerationChunk(response, {
|
|
361
365
|
usageMetadata: undefined,
|
|
366
|
+
model: this.model,
|
|
362
367
|
index,
|
|
363
368
|
});
|
|
364
369
|
index += 1;
|
|
@@ -380,3 +385,12 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
|
|
|
380
385
|
}
|
|
381
386
|
}
|
|
382
387
|
}
|
|
388
|
+
|
|
389
|
+
export {
|
|
390
|
+
GEMINI_PARTS,
|
|
391
|
+
GEMINI_REPLAY_UNSUPPORTED,
|
|
392
|
+
captureGeminiReplayParts,
|
|
393
|
+
restoreGeminiReplayParts,
|
|
394
|
+
isGeminiReplayParts,
|
|
395
|
+
} from './utils/signatures';
|
|
396
|
+
export type { GeminiReplayPart } from './utils/signatures';
|
|
@@ -40,6 +40,12 @@ import {
|
|
|
40
40
|
STREAMED_TOOL_CALL_ADAPTER_METADATA_KEY,
|
|
41
41
|
GOOGLE_STREAMED_TOOL_CALL_ADAPTER,
|
|
42
42
|
} from '@/tools/streamedToolCallSeals';
|
|
43
|
+
import {
|
|
44
|
+
GEMINI_PARTS,
|
|
45
|
+
GEMINI_REPLAY_UNSUPPORTED,
|
|
46
|
+
captureGeminiReplayParts,
|
|
47
|
+
restoreGeminiReplayParts,
|
|
48
|
+
} from './signatures';
|
|
43
49
|
import {
|
|
44
50
|
jsonSchemaToGeminiParameters,
|
|
45
51
|
schemaToGenerativeAIParameters,
|
|
@@ -519,6 +525,9 @@ export function convertMessageContentToParts(
|
|
|
519
525
|
];
|
|
520
526
|
}
|
|
521
527
|
|
|
528
|
+
const replayParts = restoreGeminiReplayParts(message);
|
|
529
|
+
if (replayParts != null) return replayParts as Part[];
|
|
530
|
+
|
|
522
531
|
let functionCalls: FunctionCallPart[] = [];
|
|
523
532
|
const messageParts: Part[] = [];
|
|
524
533
|
|
|
@@ -534,11 +543,9 @@ export function convertMessageContentToParts(
|
|
|
534
543
|
);
|
|
535
544
|
}
|
|
536
545
|
|
|
537
|
-
const
|
|
538
|
-
|
|
539
|
-
)
|
|
540
|
-
| Record<string, string>
|
|
541
|
-
| undefined;
|
|
546
|
+
const storedSignatures = message.additional_kwargs[_FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY] as Record<string, string> | undefined;
|
|
547
|
+
let functionThoughtSignatures = storedSignatures != null && Object.keys(storedSignatures).length > 0 ? storedSignatures : undefined;
|
|
548
|
+
if (Object.hasOwn(message.additional_kwargs, GEMINI_PARTS) && message.additional_kwargs[GEMINI_REPLAY_UNSUPPORTED] !== true) functionThoughtSignatures = {};
|
|
542
549
|
|
|
543
550
|
if (isAIMessage(message) && (message.tool_calls?.length ?? 0) > 0) {
|
|
544
551
|
functionCalls = (message.tool_calls ?? []).map((tc) => {
|
|
@@ -549,7 +556,7 @@ export function convertMessageContentToParts(
|
|
|
549
556
|
return signature;
|
|
550
557
|
}
|
|
551
558
|
}
|
|
552
|
-
if (isGemini3PlusModel(model)) {
|
|
559
|
+
if (isGemini3PlusModel(model) && functionThoughtSignatures == null) {
|
|
553
560
|
return DUMMY_SIGNATURE;
|
|
554
561
|
}
|
|
555
562
|
return '';
|
|
@@ -700,6 +707,7 @@ const GEMINI_FLASH_VERSION_PATTERN = /^gemini-(\d+)(?:\.(\d+))?-flash(?:$|-)/;
|
|
|
700
707
|
const NO_PREFILL_GEMINI_MODELS = [
|
|
701
708
|
'gemini-3.5-flash-lite',
|
|
702
709
|
'gemini-4-argon',
|
|
710
|
+
'gemini-4-hydrogen',
|
|
703
711
|
] as const;
|
|
704
712
|
|
|
705
713
|
export function rejectsModelTurnPrefill(model?: string): boolean {
|
|
@@ -732,10 +740,10 @@ export function rejectsModelTurnPrefill(model?: string): boolean {
|
|
|
732
740
|
* HTTP 400 for it, so we drop it and let the model generate fresh from the
|
|
733
741
|
* preceding user turn. No-op for every other model, preserving working prefill.
|
|
734
742
|
*/
|
|
735
|
-
export function dropUnsupportedModelTurnPrefill(
|
|
736
|
-
contents:
|
|
743
|
+
export function dropUnsupportedModelTurnPrefill<T extends { role: string }>(
|
|
744
|
+
contents: T[] | undefined,
|
|
737
745
|
model?: string
|
|
738
|
-
):
|
|
746
|
+
): T[] | undefined {
|
|
739
747
|
if (
|
|
740
748
|
contents == null ||
|
|
741
749
|
contents.length === 0 ||
|
|
@@ -755,6 +763,7 @@ export function convertResponseContentToChatGenerationChunk(
|
|
|
755
763
|
extra: {
|
|
756
764
|
usageMetadata?: UsageMetadata | undefined;
|
|
757
765
|
index: number;
|
|
766
|
+
model?: string;
|
|
758
767
|
}
|
|
759
768
|
): ChatGenerationChunk | null {
|
|
760
769
|
if (!response.candidates || response.candidates.length === 0) {
|
|
@@ -906,7 +915,7 @@ export function convertResponseContentToChatGenerationChunk(
|
|
|
906
915
|
}
|
|
907
916
|
: undefined;
|
|
908
917
|
|
|
909
|
-
|
|
918
|
+
const chunk = new ChatGenerationChunk({
|
|
910
919
|
text,
|
|
911
920
|
message: new AIMessageChunk({
|
|
912
921
|
content: content,
|
|
@@ -920,6 +929,8 @@ export function convertResponseContentToChatGenerationChunk(
|
|
|
920
929
|
}),
|
|
921
930
|
generationInfo,
|
|
922
931
|
});
|
|
932
|
+
captureGeminiReplayParts(chunk.message, candidateContent?.parts ?? [], extra.model);
|
|
933
|
+
return chunk;
|
|
923
934
|
}
|
|
924
935
|
|
|
925
936
|
/**
|
|
@@ -929,6 +940,7 @@ export function mapGenerateContentResultToChatResult(
|
|
|
929
940
|
response: EnhancedGenerateContentResponse,
|
|
930
941
|
extra?: {
|
|
931
942
|
usageMetadata: UsageMetadata | undefined;
|
|
943
|
+
model?: string;
|
|
932
944
|
}
|
|
933
945
|
): ChatResult {
|
|
934
946
|
if (!response.candidates || response.candidates.length === 0) {
|
|
@@ -1060,6 +1072,7 @@ export function mapGenerateContentResultToChatResult(
|
|
|
1060
1072
|
}),
|
|
1061
1073
|
generationInfo,
|
|
1062
1074
|
};
|
|
1075
|
+
captureGeminiReplayParts(generation.message, candidateContent?.parts ?? [], extra?.model);
|
|
1063
1076
|
return {
|
|
1064
1077
|
generations: [generation],
|
|
1065
1078
|
llmOutput: {
|