@librechat/agents 3.7.8 → 3.7.10
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 +3 -2
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/constants.cjs +2 -0
- package/dist/cjs/common/constants.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +40 -6
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -1
- package/dist/cjs/hooks/executeHooks.cjs +8 -0
- package/dist/cjs/hooks/executeHooks.cjs.map +1 -1
- package/dist/cjs/hooks/index.cjs +2 -0
- package/dist/cjs/hooks/index.cjs.map +1 -1
- package/dist/cjs/hooks/types.cjs +1 -0
- package/dist/cjs/hooks/types.cjs.map +1 -1
- package/dist/cjs/langfuse.cjs +43 -3
- package/dist/cjs/langfuse.cjs.map +1 -1
- package/dist/cjs/llm/invoke.cjs +2 -6
- package/dist/cjs/llm/invoke.cjs.map +1 -1
- package/dist/cjs/llm/request.cjs +6 -0
- package/dist/cjs/llm/request.cjs.map +1 -1
- package/dist/cjs/main.cjs +19 -11
- package/dist/cjs/messages/format.cjs +95 -18
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/index.cjs +1 -1
- package/dist/cjs/run.cjs +201 -46
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/session/AgentSession.cjs +1 -1
- package/dist/cjs/session/JsonlSessionStore.cjs +1 -1
- package/dist/cjs/summarization/index.cjs +1 -0
- package/dist/cjs/summarization/index.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +24 -85
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/summarization/semanticIndex.cjs +7 -3
- package/dist/cjs/summarization/semanticIndex.cjs.map +1 -1
- package/dist/cjs/summarization/shared.cjs +83 -0
- package/dist/cjs/summarization/shared.cjs.map +1 -0
- package/dist/cjs/tools/ToolNode.cjs +5 -5
- package/dist/cjs/tools/local/CompileCheckTool.cjs +1 -1
- package/dist/cjs/tools/local/LocalCodingTools.cjs +1 -1
- package/dist/cjs/tools/local/LocalExecutionEngine.cjs +1 -1
- package/dist/cjs/tools/runStepResume.cjs +1 -1
- package/dist/cjs/tools/runStepResume.cjs.map +1 -1
- package/dist/cjs/tools/search/metrics.cjs +234 -0
- package/dist/cjs/tools/search/metrics.cjs.map +1 -0
- package/dist/cjs/tools/search/rerankers.cjs +80 -68
- package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
- package/dist/cjs/tools/search/search.cjs +95 -45
- package/dist/cjs/tools/search/search.cjs.map +1 -1
- package/dist/cjs/tools/search/tool.cjs +77 -49
- package/dist/cjs/tools/search/tool.cjs.map +1 -1
- package/dist/cjs/tools/search/utils.cjs.map +1 -1
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +8 -12
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -1
- package/dist/cjs/utils/tokens.cjs +8 -1
- package/dist/cjs/utils/tokens.cjs.map +1 -1
- package/dist/esm/agents/AgentContext.mjs +3 -2
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/constants.mjs +2 -1
- package/dist/esm/common/constants.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +40 -6
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +1 -1
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -1
- package/dist/esm/hooks/executeHooks.mjs +8 -1
- package/dist/esm/hooks/executeHooks.mjs.map +1 -1
- package/dist/esm/hooks/index.mjs +2 -1
- package/dist/esm/hooks/index.mjs.map +1 -1
- package/dist/esm/hooks/types.mjs +1 -0
- package/dist/esm/hooks/types.mjs.map +1 -1
- package/dist/esm/langfuse.mjs +43 -3
- package/dist/esm/langfuse.mjs.map +1 -1
- package/dist/esm/llm/invoke.mjs +2 -6
- package/dist/esm/llm/invoke.mjs.map +1 -1
- package/dist/esm/llm/request.mjs +6 -1
- package/dist/esm/llm/request.mjs.map +1 -1
- package/dist/esm/main.mjs +13 -12
- package/dist/esm/messages/format.mjs +96 -19
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/index.mjs +1 -1
- package/dist/esm/run.mjs +201 -46
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/session/AgentSession.mjs +1 -1
- package/dist/esm/session/JsonlSessionStore.mjs +1 -1
- package/dist/esm/summarization/index.mjs +1 -0
- package/dist/esm/summarization/index.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +21 -82
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/summarization/semanticIndex.mjs +7 -4
- package/dist/esm/summarization/semanticIndex.mjs.map +1 -1
- package/dist/esm/summarization/shared.mjs +79 -0
- package/dist/esm/summarization/shared.mjs.map +1 -0
- package/dist/esm/tools/ToolNode.mjs +5 -5
- package/dist/esm/tools/local/CompileCheckTool.mjs +1 -1
- package/dist/esm/tools/local/LocalCodingTools.mjs +1 -1
- package/dist/esm/tools/local/LocalExecutionEngine.mjs +1 -1
- package/dist/esm/tools/runStepResume.mjs +1 -1
- package/dist/esm/tools/runStepResume.mjs.map +1 -1
- package/dist/esm/tools/search/metrics.mjs +234 -0
- package/dist/esm/tools/search/metrics.mjs.map +1 -0
- package/dist/esm/tools/search/rerankers.mjs +80 -68
- package/dist/esm/tools/search/rerankers.mjs.map +1 -1
- package/dist/esm/tools/search/search.mjs +96 -46
- package/dist/esm/tools/search/search.mjs.map +1 -1
- package/dist/esm/tools/search/tool.mjs +77 -49
- package/dist/esm/tools/search/tool.mjs.map +1 -1
- package/dist/esm/tools/search/utils.mjs.map +1 -1
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +8 -12
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -1
- package/dist/esm/utils/tokens.mjs +8 -2
- package/dist/esm/utils/tokens.mjs.map +1 -1
- package/dist/types/common/constants.d.ts +7 -0
- package/dist/types/graphs/Graph.d.ts +9 -0
- package/dist/types/hooks/executeHooks.d.ts +5 -2
- package/dist/types/hooks/index.d.ts +7 -2
- package/dist/types/hooks/types.d.ts +45 -7
- package/dist/types/langfuse.d.ts +3 -1
- package/dist/types/llm/request.d.ts +10 -0
- package/dist/types/messages/format.d.ts +8 -2
- package/dist/types/run.d.ts +2 -1
- package/dist/types/summarization/index.d.ts +6 -0
- package/dist/types/summarization/node.d.ts +0 -4
- package/dist/types/summarization/semanticIndex.d.ts +2 -0
- package/dist/types/summarization/shared.d.ts +25 -0
- package/dist/types/tools/search/metrics.d.ts +15 -0
- package/dist/types/tools/search/rerankers.d.ts +30 -5
- package/dist/types/tools/search/tool.d.ts +7 -1
- package/dist/types/tools/search/types.d.ts +88 -4
- package/dist/types/tools/search/utils.d.ts +2 -10
- package/dist/types/types/run.d.ts +8 -0
- package/dist/types/types/stream.d.ts +6 -0
- package/dist/types/types/summarize.d.ts +6 -0
- package/dist/types/utils/tokens.d.ts +9 -0
- package/package.json +1 -1
- package/src/agents/AgentContext.ts +2 -5
- package/src/common/constants.ts +8 -0
- package/src/graphs/Graph.ts +45 -0
- package/src/hooks/HookRegistry.ts +3 -1
- package/src/hooks/executeHooks.ts +20 -2
- package/src/hooks/index.ts +12 -1
- package/src/hooks/types.ts +47 -4
- package/src/langfuse.ts +70 -0
- package/src/llm/invoke.ts +13 -29
- package/src/llm/request.ts +24 -0
- package/src/messages/format.ts +213 -27
- package/src/run.ts +366 -186
- package/src/summarization/index.ts +11 -0
- package/src/summarization/node.ts +117 -130
- package/src/summarization/semanticIndex.ts +16 -7
- package/src/summarization/shared.ts +130 -0
- package/src/tools/runStepResume.ts +9 -0
- package/src/tools/search/metrics.ts +400 -0
- package/src/tools/search/rerankers.ts +160 -97
- package/src/tools/search/search.ts +139 -56
- package/src/tools/search/tool.ts +126 -62
- package/src/tools/search/types.ts +104 -4
- package/src/tools/search/utils.ts +2 -10
- package/src/tools/subagent/SubagentExecutor.ts +4 -27
- package/src/types/run.ts +8 -0
- package/src/types/stream.ts +6 -0
- package/src/types/summarize.ts +7 -0
- package/src/utils/tokens.ts +31 -8
package/src/hooks/types.ts
CHANGED
|
@@ -22,6 +22,7 @@ export const HOOK_EVENTS = [
|
|
|
22
22
|
'SubagentStart',
|
|
23
23
|
'SubagentStop',
|
|
24
24
|
'Stop',
|
|
25
|
+
'StopFinalize',
|
|
25
26
|
'StopFailure',
|
|
26
27
|
'PreCompact',
|
|
27
28
|
'PostCompact',
|
|
@@ -209,7 +210,34 @@ export interface StopHookInput extends BaseHookInput {
|
|
|
209
210
|
hook_event_name: 'Stop';
|
|
210
211
|
messages: BaseMessage[];
|
|
211
212
|
stopReason?: string;
|
|
213
|
+
/** True after this hook has already kept the current Run warm once. */
|
|
212
214
|
stopHookActive: boolean;
|
|
215
|
+
/** Number of terminal continuations already admitted in this Run. */
|
|
216
|
+
continuationCount: number;
|
|
217
|
+
/**
|
|
218
|
+
* Remaining continuations the SDK can honor. A host that durably claims
|
|
219
|
+
* queued work MUST seal admission instead when this reaches zero.
|
|
220
|
+
*/
|
|
221
|
+
continuationBudgetRemaining: number;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Serialized terminal admission phase. Unlike ordinary `Stop` hooks (which
|
|
226
|
+
* execute in parallel), this fires after their outputs have been folded so a
|
|
227
|
+
* durable host can make one final claim-or-seal decision with full knowledge
|
|
228
|
+
* of whether another hook already kept the Run warm.
|
|
229
|
+
*/
|
|
230
|
+
export interface StopFinalizeHookInput extends BaseHookInput {
|
|
231
|
+
hook_event_name: 'StopFinalize';
|
|
232
|
+
messages: BaseMessage[];
|
|
233
|
+
stopReason?: string;
|
|
234
|
+
stopHookActive: boolean;
|
|
235
|
+
continuationCount: number;
|
|
236
|
+
continuationBudgetRemaining: number;
|
|
237
|
+
/** A regular Stop hook already supplied a valid warm continuation. */
|
|
238
|
+
continuationPlanned: boolean;
|
|
239
|
+
/** A halt reason or Stop output forbids another graph segment. */
|
|
240
|
+
continuationPrevented: boolean;
|
|
213
241
|
}
|
|
214
242
|
|
|
215
243
|
export interface StopFailureHookInput extends BaseHookInput {
|
|
@@ -259,6 +287,7 @@ export type HookInput =
|
|
|
259
287
|
| SubagentStartHookInput
|
|
260
288
|
| SubagentStopHookInput
|
|
261
289
|
| StopHookInput
|
|
290
|
+
| StopFinalizeHookInput
|
|
262
291
|
| StopFailureHookInput
|
|
263
292
|
| PreCompactHookInput
|
|
264
293
|
| PostCompactHookInput;
|
|
@@ -276,6 +305,7 @@ export type HookInputByEvent = {
|
|
|
276
305
|
SubagentStart: SubagentStartHookInput;
|
|
277
306
|
SubagentStop: SubagentStopHookInput;
|
|
278
307
|
Stop: StopHookInput;
|
|
308
|
+
StopFinalize: StopFinalizeHookInput;
|
|
279
309
|
StopFailure: StopFailureHookInput;
|
|
280
310
|
PreCompact: PreCompactHookInput;
|
|
281
311
|
PostCompact: PostCompactHookInput;
|
|
@@ -298,10 +328,11 @@ export interface BaseHookOutput {
|
|
|
298
328
|
* mid-run steering message). Accumulated across hooks in registration
|
|
299
329
|
* order.
|
|
300
330
|
*
|
|
301
|
-
* Consumed at
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
* Every other event ignores the
|
|
331
|
+
* Consumed at three dispatch sites, all of which run the same converter so
|
|
332
|
+
* the emitted shapes cannot drift: `PostToolBatch` (the tool boundary),
|
|
333
|
+
* `PreemptBoundary` (a cooperative mid-generation seal), and a blocking
|
|
334
|
+
* `Stop` hook (terminal Run continuation). Every other event ignores the
|
|
335
|
+
* field.
|
|
305
336
|
*/
|
|
306
337
|
injectedMessages?: InjectedMessage[];
|
|
307
338
|
/** True to prevent the next model turn. Any hook can set this. */
|
|
@@ -435,10 +466,18 @@ export interface SubagentStartHookOutput extends BaseHookOutput {
|
|
|
435
466
|
export type SubagentStopHookOutput = BaseHookOutput;
|
|
436
467
|
|
|
437
468
|
export interface StopHookOutput extends BaseHookOutput {
|
|
469
|
+
/**
|
|
470
|
+
* `block` keeps a naturally terminal Run warm only when the output also
|
|
471
|
+
* supplies non-empty `injectedMessages` or `additionalContext`. The SDK
|
|
472
|
+
* starts another graph segment inside the same `processStream` call; no
|
|
473
|
+
* RunStart/UserPromptSubmit hooks or terminal cleanup occur between them.
|
|
474
|
+
*/
|
|
438
475
|
decision?: StopDecision;
|
|
439
476
|
reason?: string;
|
|
440
477
|
}
|
|
441
478
|
|
|
479
|
+
export type StopFinalizeHookOutput = StopHookOutput;
|
|
480
|
+
|
|
442
481
|
export type StopFailureHookOutput = BaseHookOutput;
|
|
443
482
|
|
|
444
483
|
export type PreCompactHookOutput = BaseHookOutput;
|
|
@@ -458,6 +497,7 @@ export type HookOutputByEvent = {
|
|
|
458
497
|
SubagentStart: SubagentStartHookOutput;
|
|
459
498
|
SubagentStop: SubagentStopHookOutput;
|
|
460
499
|
Stop: StopHookOutput;
|
|
500
|
+
StopFinalize: StopFinalizeHookOutput;
|
|
461
501
|
StopFailure: StopFailureHookOutput;
|
|
462
502
|
PreCompact: PreCompactHookOutput;
|
|
463
503
|
PostCompact: PostCompactHookOutput;
|
|
@@ -476,6 +516,7 @@ export type HookOutput =
|
|
|
476
516
|
| SubagentStartHookOutput
|
|
477
517
|
| SubagentStopHookOutput
|
|
478
518
|
| StopHookOutput
|
|
519
|
+
| StopFinalizeHookOutput
|
|
479
520
|
| StopFailureHookOutput
|
|
480
521
|
| PreCompactHookOutput
|
|
481
522
|
| PostCompactHookOutput;
|
|
@@ -593,6 +634,8 @@ export interface AggregatedHookResult {
|
|
|
593
634
|
* `preventContinuation` do not overwrite the reason.
|
|
594
635
|
*/
|
|
595
636
|
stopReason?: string;
|
|
637
|
+
/** True when any hook failed, including internal hooks with hidden diagnostics. */
|
|
638
|
+
hasHookFailures?: true;
|
|
596
639
|
/** Error messages from hooks that threw; always present (possibly empty). */
|
|
597
640
|
errors: string[];
|
|
598
641
|
}
|
package/src/langfuse.ts
CHANGED
|
@@ -79,6 +79,8 @@ type LangfuseHandlerParams = {
|
|
|
79
79
|
* only adopted when stamped with the same run (see
|
|
80
80
|
* `LangfuseRuntimeContext.runId`). */
|
|
81
81
|
runId?: string;
|
|
82
|
+
/** Keep this root open while one processStream call executes graph segments. */
|
|
83
|
+
deferRootRunId?: string;
|
|
82
84
|
/** The run's resolved tool-output policy — for multi-agent streams the
|
|
83
85
|
* conservative aggregate across agents, which `this.langfuse` (the
|
|
84
86
|
* primary agent's config) cannot reproduce. Applied when a foreign
|
|
@@ -293,9 +295,19 @@ class ScopedLangfuseCallbackHandler extends CallbackHandler {
|
|
|
293
295
|
private readonly agentId?: string;
|
|
294
296
|
private readonly parentSpanContext?: SpanContext;
|
|
295
297
|
private readonly runId?: string;
|
|
298
|
+
private readonly deferredRootRunId?: string;
|
|
296
299
|
private readonly identity: HandlerIdentity;
|
|
297
300
|
private readonly toolOutputTracing?: ResolvedLangfuseToolOutputTracingConfig;
|
|
298
301
|
private readonly trackedRunIds = new Set<string>();
|
|
302
|
+
private deferredRootStarted = false;
|
|
303
|
+
private deferredRootOutcome:
|
|
304
|
+
| {
|
|
305
|
+
type: 'end';
|
|
306
|
+
output: Parameters<CallbackHandler['handleChainEnd']>[0];
|
|
307
|
+
parentRunId?: string;
|
|
308
|
+
}
|
|
309
|
+
| { type: 'error'; error: Error; parentRunId?: string }
|
|
310
|
+
| undefined;
|
|
299
311
|
|
|
300
312
|
constructor(params?: AgentLangfuseHandlerParams) {
|
|
301
313
|
const {
|
|
@@ -306,6 +318,7 @@ class ScopedLangfuseCallbackHandler extends CallbackHandler {
|
|
|
306
318
|
parentSpanContext,
|
|
307
319
|
inheritTraceIdentity,
|
|
308
320
|
runId,
|
|
321
|
+
deferRootRunId,
|
|
309
322
|
toolOutputTracing,
|
|
310
323
|
traceName,
|
|
311
324
|
...handlerParams
|
|
@@ -326,6 +339,10 @@ class ScopedLangfuseCallbackHandler extends CallbackHandler {
|
|
|
326
339
|
this.agentId = agentId;
|
|
327
340
|
this.parentSpanContext = parentSpanContext;
|
|
328
341
|
this.runId = runId;
|
|
342
|
+
this.deferredRootRunId = deferRootRunId;
|
|
343
|
+
if (deferRootRunId != null) {
|
|
344
|
+
this.awaitHandlers = true;
|
|
345
|
+
}
|
|
329
346
|
this.toolOutputTracing = toolOutputTracing;
|
|
330
347
|
this.identity = {
|
|
331
348
|
userId: inheritTraceIdentity === true ? undefined : handlerParams.userId,
|
|
@@ -528,6 +545,17 @@ class ScopedLangfuseCallbackHandler extends CallbackHandler {
|
|
|
528
545
|
override handleChainStart(
|
|
529
546
|
...args: Parameters<CallbackHandler['handleChainStart']>
|
|
530
547
|
): ReturnType<CallbackHandler['handleChainStart']> {
|
|
548
|
+
const [, , runId, parentRunId] = args;
|
|
549
|
+
if (
|
|
550
|
+
runId === this.deferredRootRunId &&
|
|
551
|
+
parentRunId == null &&
|
|
552
|
+
this.deferredRootStarted
|
|
553
|
+
) {
|
|
554
|
+
return Promise.resolve();
|
|
555
|
+
}
|
|
556
|
+
if (runId === this.deferredRootRunId && parentRunId == null) {
|
|
557
|
+
this.deferredRootStarted = true;
|
|
558
|
+
}
|
|
531
559
|
return this.withRuntimeContext(
|
|
532
560
|
() => super.handleChainStart(...args),
|
|
533
561
|
this.startsDetachedRun(args[2], args[3]),
|
|
@@ -539,6 +567,13 @@ class ScopedLangfuseCallbackHandler extends CallbackHandler {
|
|
|
539
567
|
...args: Parameters<CallbackHandler['handleChainError']>
|
|
540
568
|
): ReturnType<CallbackHandler['handleChainError']> {
|
|
541
569
|
const [error, runId, parentRunId] = args;
|
|
570
|
+
if (runId === this.deferredRootRunId && parentRunId == null) {
|
|
571
|
+
this.deferredRootOutcome = {
|
|
572
|
+
type: 'error',
|
|
573
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
574
|
+
};
|
|
575
|
+
return Promise.resolve();
|
|
576
|
+
}
|
|
542
577
|
if (error != null && parentRunId != null && isGraphInterrupt(error)) {
|
|
543
578
|
return super.handleChainEnd(
|
|
544
579
|
GRAPH_INTERRUPT_CONTROL_FLOW,
|
|
@@ -556,6 +591,36 @@ class ScopedLangfuseCallbackHandler extends CallbackHandler {
|
|
|
556
591
|
return super.handleChainError(...args);
|
|
557
592
|
}
|
|
558
593
|
|
|
594
|
+
override handleChainEnd(
|
|
595
|
+
...args: Parameters<CallbackHandler['handleChainEnd']>
|
|
596
|
+
): ReturnType<CallbackHandler['handleChainEnd']> {
|
|
597
|
+
const [output, runId, parentRunId] = args;
|
|
598
|
+
if (runId === this.deferredRootRunId && parentRunId == null) {
|
|
599
|
+
this.deferredRootOutcome = { type: 'end', output };
|
|
600
|
+
return Promise.resolve();
|
|
601
|
+
}
|
|
602
|
+
return super.handleChainEnd(...args);
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
async finishDeferredRoot(): Promise<void> {
|
|
606
|
+
const runId = this.deferredRootRunId;
|
|
607
|
+
const outcome = this.deferredRootOutcome;
|
|
608
|
+
if (!this.deferredRootStarted || runId == null || outcome == null) {
|
|
609
|
+
return;
|
|
610
|
+
}
|
|
611
|
+
this.deferredRootStarted = false;
|
|
612
|
+
this.deferredRootOutcome = undefined;
|
|
613
|
+
if (outcome.type === 'error') {
|
|
614
|
+
await super.handleChainError(
|
|
615
|
+
outcome.error,
|
|
616
|
+
runId,
|
|
617
|
+
outcome.parentRunId
|
|
618
|
+
);
|
|
619
|
+
return;
|
|
620
|
+
}
|
|
621
|
+
await super.handleChainEnd(outcome.output, runId, outcome.parentRunId);
|
|
622
|
+
}
|
|
623
|
+
|
|
559
624
|
override handleAgentAction(
|
|
560
625
|
...args: Parameters<CallbackHandler['handleAgentAction']>
|
|
561
626
|
): ReturnType<CallbackHandler['handleAgentAction']> {
|
|
@@ -792,6 +857,7 @@ export function createLangfuseHandler({
|
|
|
792
857
|
parentSpanContext,
|
|
793
858
|
inheritTraceIdentity,
|
|
794
859
|
runId,
|
|
860
|
+
deferRootRunId,
|
|
795
861
|
toolOutputTracing,
|
|
796
862
|
traceName,
|
|
797
863
|
}: AgentLangfuseHandlerParams): CallbackHandler | undefined {
|
|
@@ -813,6 +879,7 @@ export function createLangfuseHandler({
|
|
|
813
879
|
parentSpanContext,
|
|
814
880
|
inheritTraceIdentity,
|
|
815
881
|
runId,
|
|
882
|
+
deferRootRunId,
|
|
816
883
|
toolOutputTracing,
|
|
817
884
|
traceName,
|
|
818
885
|
});
|
|
@@ -865,6 +932,9 @@ export function isLangfuseCallbackHandler(value: unknown): boolean {
|
|
|
865
932
|
}
|
|
866
933
|
|
|
867
934
|
export async function disposeLangfuseHandler(value: unknown): Promise<void> {
|
|
935
|
+
if (value instanceof ScopedLangfuseCallbackHandler) {
|
|
936
|
+
await value.finishDeferredRoot();
|
|
937
|
+
}
|
|
868
938
|
if (
|
|
869
939
|
value == null ||
|
|
870
940
|
parseBooleanEnv(process.env[LANGFUSE_FORCE_FLUSH_ON_DISPOSE]) !== true
|
package/src/llm/invoke.ts
CHANGED
|
@@ -27,6 +27,12 @@ import {
|
|
|
27
27
|
STREAM_LIMIT_REDISPATCH_KEY,
|
|
28
28
|
STREAM_LIMIT_ATTEMPT_KEY,
|
|
29
29
|
} from '@/llm/streamLimits';
|
|
30
|
+
import {
|
|
31
|
+
inspectProviderMessageProjection,
|
|
32
|
+
ProviderMessageProjectionInvariantError,
|
|
33
|
+
resolveProviderMessageProjectionInvariantMode,
|
|
34
|
+
modifyDeltaProperties,
|
|
35
|
+
} from '@/messages';
|
|
30
36
|
import {
|
|
31
37
|
assertPreparedProviderRequestFor,
|
|
32
38
|
prepareProviderRequest,
|
|
@@ -38,15 +44,10 @@ import {
|
|
|
38
44
|
import { ChatModelStreamHandler, dispatchesChatModelStream } from '@/stream';
|
|
39
45
|
import { Constants, ContentTypes, GraphEvents, Providers } from '@/common';
|
|
40
46
|
import { assertNotTruncatedToolCall } from '@/llm/truncation';
|
|
47
|
+
import { resolveClientOptionsModel } from '@/llm/request';
|
|
41
48
|
import { safeDispatchCustomEvent } from '@/utils/events';
|
|
42
49
|
import { getContextOverflowInfo } from '@/utils/errors';
|
|
43
50
|
import { appendCallbacks } from '@/utils/callbacks';
|
|
44
|
-
import {
|
|
45
|
-
inspectProviderMessageProjection,
|
|
46
|
-
ProviderMessageProjectionInvariantError,
|
|
47
|
-
resolveProviderMessageProjectionInvariantMode,
|
|
48
|
-
modifyDeltaProperties,
|
|
49
|
-
} from '@/messages';
|
|
50
51
|
import { canSealPreempt } from '@/llm/preempt';
|
|
51
52
|
import { initializeModel } from '@/llm/init';
|
|
52
53
|
|
|
@@ -715,8 +716,7 @@ async function attemptInvokeBody(
|
|
|
715
716
|
resolveProviderMessageProjectionInvariantMode();
|
|
716
717
|
let sealedRunId: string | undefined;
|
|
717
718
|
let invocationConfig = config;
|
|
718
|
-
const captureModelRunId =
|
|
719
|
-
model.stream != null && context?.preemption != null;
|
|
719
|
+
const captureModelRunId = model.stream != null && context?.preemption != null;
|
|
720
720
|
if (projectionInvariantMode !== 'off' || captureModelRunId) {
|
|
721
721
|
invocationConfig = withModelStartHandler({
|
|
722
722
|
config,
|
|
@@ -934,7 +934,10 @@ async function attemptInvokeBody(
|
|
|
934
934
|
return { messages: [finalChunk as AIMessageChunk] };
|
|
935
935
|
}
|
|
936
936
|
|
|
937
|
-
const finalMessage = await model.invoke(
|
|
937
|
+
const finalMessage = await model.invoke(
|
|
938
|
+
messagesForProvider,
|
|
939
|
+
invocationConfig
|
|
940
|
+
);
|
|
938
941
|
if ((finalMessage.tool_calls?.length ?? 0) > 0) {
|
|
939
942
|
finalMessage.tool_calls = finalMessage.tool_calls?.filter(
|
|
940
943
|
(tool_call: ToolCall) => !!tool_call.name
|
|
@@ -997,25 +1000,6 @@ export function getFallbackOverflowCandidates(
|
|
|
997
1000
|
return [...(fallbackOverflowCandidates.get(error) ?? [])];
|
|
998
1001
|
}
|
|
999
1002
|
|
|
1000
|
-
/**
|
|
1001
|
-
* Best-effort read of the configured model name from client options.
|
|
1002
|
-
* Providers disagree on the key (`model` vs `modelName`).
|
|
1003
|
-
*/
|
|
1004
|
-
function extractClientOptionsModel(
|
|
1005
|
-
clientOptions: t.ClientOptions | undefined
|
|
1006
|
-
): string | undefined {
|
|
1007
|
-
const options = clientOptions as
|
|
1008
|
-
| { model?: unknown; modelName?: unknown }
|
|
1009
|
-
| undefined;
|
|
1010
|
-
if (typeof options?.model === 'string' && options.model !== '') {
|
|
1011
|
-
return options.model;
|
|
1012
|
-
}
|
|
1013
|
-
if (typeof options?.modelName === 'string' && options.modelName !== '') {
|
|
1014
|
-
return options.modelName;
|
|
1015
|
-
}
|
|
1016
|
-
return undefined;
|
|
1017
|
-
}
|
|
1018
|
-
|
|
1019
1003
|
/**
|
|
1020
1004
|
* Attempts each fallback provider in order until one succeeds.
|
|
1021
1005
|
*
|
|
@@ -1106,7 +1090,7 @@ export async function tryFallbackProviders({
|
|
|
1106
1090
|
* `ls_model_name`. The serving provider is stamped uniformly by
|
|
1107
1091
|
* `attemptInvoke` (`INVOKED_PROVIDER`).
|
|
1108
1092
|
*/
|
|
1109
|
-
const fbModelName =
|
|
1093
|
+
const fbModelName = resolveClientOptionsModel(fb.clientOptions);
|
|
1110
1094
|
const fbConfig: RunnableConfig | undefined =
|
|
1111
1095
|
fbModelName == null
|
|
1112
1096
|
? config
|
package/src/llm/request.ts
CHANGED
|
@@ -43,6 +43,30 @@ export function isThinkingEnabled(
|
|
|
43
43
|
return false;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
+
/**
|
|
47
|
+
* Model configured on client options, under whichever of the two keys carries
|
|
48
|
+
* it. LangChain accepts `modelName` as an alias for `model` and this package
|
|
49
|
+
* writes both (see `buildSummarizationClientConfig`), while hosts configure
|
|
50
|
+
* agents through either. Reading only one key therefore does not fail loudly:
|
|
51
|
+
* it reports an unconfigured model, and every caller here treats that as a cue
|
|
52
|
+
* to fall back to a default, so a Claude agent configured through the alias is
|
|
53
|
+
* silently handled as something else.
|
|
54
|
+
*/
|
|
55
|
+
export function resolveClientOptionsModel(
|
|
56
|
+
clientOptions: t.ClientOptions | undefined
|
|
57
|
+
): string | undefined {
|
|
58
|
+
const options = clientOptions as
|
|
59
|
+
| { model?: unknown; modelName?: unknown }
|
|
60
|
+
| undefined;
|
|
61
|
+
if (typeof options?.model === 'string' && options.model !== '') {
|
|
62
|
+
return options.model;
|
|
63
|
+
}
|
|
64
|
+
if (typeof options?.modelName === 'string' && options.modelName !== '') {
|
|
65
|
+
return options.modelName;
|
|
66
|
+
}
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
|
|
46
70
|
/**
|
|
47
71
|
* Returns the correct key for setting max output tokens on the model
|
|
48
72
|
* constructor options. Google/Vertex use `maxOutputTokens`, all others
|