mirror of
https://github.com/MODSetter/SurfSense.git
synced 2026-05-12 09:12:40 +02:00
Persist optional relay metadata on tool-call and thinking-step state.
This commit is contained in:
parent
1761b60c16
commit
47e64d1861
1 changed files with 42 additions and 1 deletions
|
|
@ -5,6 +5,11 @@ export interface ThinkingStepData {
|
||||||
title: string;
|
title: string;
|
||||||
status: "pending" | "in_progress" | "completed";
|
status: "pending" | "in_progress" | "completed";
|
||||||
items: string[];
|
items: string[];
|
||||||
|
/**
|
||||||
|
* Optional relay fields from ``data-thinking-step`` when present on the wire
|
||||||
|
* (e.g. ``spanId``). Populated in a later slice; equality helpers ignore until wired.
|
||||||
|
*/
|
||||||
|
metadata?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type ContentPart =
|
export type ContentPart =
|
||||||
|
|
@ -42,6 +47,11 @@ export type ContentPart =
|
||||||
* ``data-action-log`` events.
|
* ``data-action-log`` events.
|
||||||
*/
|
*/
|
||||||
langchainToolCallId?: string;
|
langchainToolCallId?: string;
|
||||||
|
/**
|
||||||
|
* Relay correlation from tool SSE (e.g. ``spanId``, ``thinkingStepId``).
|
||||||
|
* Merged by ``mergeToolPartMetadata`` when events carry ``metadata``.
|
||||||
|
*/
|
||||||
|
metadata?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
type: "data-thinking-steps";
|
type: "data-thinking-steps";
|
||||||
|
|
@ -252,6 +262,23 @@ function _toolPasses(gate: ToolUIGate, toolName: string): boolean {
|
||||||
return gate === "all" || gate.has(toolName);
|
return gate === "all" || gate.has(toolName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shallow-merge relay ``metadata`` into a tool-call part (SSE → content part).
|
||||||
|
* Keys already set on ``into`` are left unchanged so chunk vs canonical tool
|
||||||
|
* events cannot reorder or overwrite ``spanId`` / ``thinkingStepId``.
|
||||||
|
* Matches server ``AssistantContentBuilder`` merge semantics.
|
||||||
|
*/
|
||||||
|
function mergeToolPartMetadata(
|
||||||
|
into: Record<string, unknown>,
|
||||||
|
incoming: Record<string, unknown> | undefined
|
||||||
|
): void {
|
||||||
|
if (!incoming) return;
|
||||||
|
for (const [k, v] of Object.entries(incoming)) {
|
||||||
|
if (k === "__proto__" || k === "constructor") continue;
|
||||||
|
if (!(k in into)) into[k] = v;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function addToolCall(
|
export function addToolCall(
|
||||||
state: ContentPartsState,
|
state: ContentPartsState,
|
||||||
toolsWithUI: ToolUIGate,
|
toolsWithUI: ToolUIGate,
|
||||||
|
|
@ -259,15 +286,19 @@ export function addToolCall(
|
||||||
toolName: string,
|
toolName: string,
|
||||||
args: Record<string, unknown>,
|
args: Record<string, unknown>,
|
||||||
force = false,
|
force = false,
|
||||||
langchainToolCallId?: string
|
langchainToolCallId?: string,
|
||||||
|
metadata?: Record<string, unknown>
|
||||||
): void {
|
): void {
|
||||||
if (force || _toolPasses(toolsWithUI, toolName)) {
|
if (force || _toolPasses(toolsWithUI, toolName)) {
|
||||||
|
const relayMeta: Record<string, unknown> = {};
|
||||||
|
mergeToolPartMetadata(relayMeta, metadata);
|
||||||
state.contentParts.push({
|
state.contentParts.push({
|
||||||
type: "tool-call",
|
type: "tool-call",
|
||||||
toolCallId,
|
toolCallId,
|
||||||
toolName,
|
toolName,
|
||||||
args,
|
args,
|
||||||
...(langchainToolCallId ? { langchainToolCallId } : {}),
|
...(langchainToolCallId ? { langchainToolCallId } : {}),
|
||||||
|
...(Object.keys(relayMeta).length > 0 ? { metadata: relayMeta } : {}),
|
||||||
});
|
});
|
||||||
state.toolCallIndices.set(toolCallId, state.contentParts.length - 1);
|
state.toolCallIndices.set(toolCallId, state.contentParts.length - 1);
|
||||||
state.currentTextPartIndex = -1;
|
state.currentTextPartIndex = -1;
|
||||||
|
|
@ -304,6 +335,7 @@ export function updateToolCall(
|
||||||
argsText?: string;
|
argsText?: string;
|
||||||
result?: unknown;
|
result?: unknown;
|
||||||
langchainToolCallId?: string;
|
langchainToolCallId?: string;
|
||||||
|
metadata?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
): void {
|
): void {
|
||||||
const index = state.toolCallIndices.get(toolCallId);
|
const index = state.toolCallIndices.get(toolCallId);
|
||||||
|
|
@ -323,6 +355,11 @@ export function updateToolCall(
|
||||||
if (update.langchainToolCallId && !tc.langchainToolCallId) {
|
if (update.langchainToolCallId && !tc.langchainToolCallId) {
|
||||||
tc.langchainToolCallId = update.langchainToolCallId;
|
tc.langchainToolCallId = update.langchainToolCallId;
|
||||||
}
|
}
|
||||||
|
if (update.metadata && Object.keys(update.metadata).length > 0) {
|
||||||
|
const md = (tc.metadata ?? {}) as Record<string, unknown>;
|
||||||
|
mergeToolPartMetadata(md, update.metadata);
|
||||||
|
tc.metadata = md;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -416,6 +453,8 @@ export type SSEEvent =
|
||||||
toolName: string;
|
toolName: string;
|
||||||
/** Authoritative LangChain ``tool_call.id``. Optional. */
|
/** Authoritative LangChain ``tool_call.id``. Optional. */
|
||||||
langchainToolCallId?: string;
|
langchainToolCallId?: string;
|
||||||
|
/** Optional JSON object from tool SSE (same keys as persisted tool-call metadata). */
|
||||||
|
metadata?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
/**
|
/**
|
||||||
|
|
@ -434,6 +473,7 @@ export type SSEEvent =
|
||||||
toolName: string;
|
toolName: string;
|
||||||
input: Record<string, unknown>;
|
input: Record<string, unknown>;
|
||||||
langchainToolCallId?: string;
|
langchainToolCallId?: string;
|
||||||
|
metadata?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
| {
|
| {
|
||||||
type: "tool-output-available";
|
type: "tool-output-available";
|
||||||
|
|
@ -443,6 +483,7 @@ export type SSEEvent =
|
||||||
* ``ToolMessage.tool_call_id`` at on_tool_end. Backfills cards
|
* ``ToolMessage.tool_call_id`` at on_tool_end. Backfills cards
|
||||||
* that didn't get the id at tool-input-start time. */
|
* that didn't get the id at tool-input-start time. */
|
||||||
langchainToolCallId?: string;
|
langchainToolCallId?: string;
|
||||||
|
metadata?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
| { type: "data-thinking-step"; data: ThinkingStepData }
|
| { type: "data-thinking-step"; data: ThinkingStepData }
|
||||||
| { type: "data-thread-title-update"; data: { threadId: number; title: string } }
|
| { type: "data-thread-title-update"; data: { threadId: number; title: string } }
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue