UNPKG

@mastra/core

Version:
122 lines • 5.91 kB
import type { MessageList } from '../../../agent/message-list/index.js'; import type { IMastraLogger } from '../../../logger/index.js'; import type { ProviderMetadata } from '../../../stream/types.js'; /** Completion payload delivered by the background-task manager's onResult hook. */ export interface BackgroundToolResultParams { status: string; result?: unknown; error?: { message?: string; }; toolCallId: string; toolName: string; runId: string; startedAt?: Date; completedAt?: Date; taskId?: string; } /** * Shared background-task result injector: when a background * tool finishes, replace the "Background task started..." placeholder in the * transcript with the real result (or failure), recompute the model-facing * output, and flush to memory. Engines call this from their per-task onResult * hook; transport-specific chunk emission stays engine-side (onChunk). * * Unified behaviors: * - `toModelOutput` recompute + `mastra.modelOutput` overwrite: previously * main-only. The dispatch turn stored `mastra.modelOutput` derived from the * placeholder (or, on durable, no mapping at all), and `llmPrompt()` prefers * that field over `toolInvocation.result` when building the tool message. * Every path below overwrites `mastra.modelOutput`, including the ones that * produce nothing: a tool with no `toModelOutput`, a mapping that returns * nullish, and a mapping that throws. A null `modelOutput` is the * established "no mapping, use the raw result" signal `MessageList` keys * off by value. Deliberately not shared with tool-result-commit-core's * `computeModelOutputProviderMetadata`, which omits the key on nullish * (presence-keyed) and runs under a MAPPING span — the policies differ on * purpose. * - Transcript payload transforms remain an engine-supplied hook. Main * supplies `transformForTranscript` from its run scope (dispatch-time * capture, valid for its request-bound lifetime); durable supplies it too, * resolving the policy and tool-level transform * at completion time from the live run registry because the entry may be * rebuilt after a process restart. The run-level policy carries a closure * and does not survive a restart on durable — only tool-level transforms * do, via registry re-resolution. * - Idempotency/conflict scan: keyed by (toolCallId, taskId), not toolCallId * alone. The scan set is the full db message list, which includes * memory-loaded history on both engines, and providers reuse tool-call ids * across turns — so a same-toolCallId part with a different taskId is an * earlier dispatch's record and is skipped. Status conflicts only throw for * the part this dispatch owns (matching taskId). */ export declare function applyBackgroundToolResult(deps: { params: BackgroundToolResultParams; /** The run currently executing the tool-call step (not params.runId, which * is the run that originally dispatched the task). */ currentRunId: string; /** Whether this leg of the run is itself a resume; gates the fallback * tool-call append so a same-run replay re-records the invocation. */ hasResumeData: boolean; /** Tool args with `_background` removed. */ args: unknown; messageList: MessageList; /** Approval decision for an approved approval-gated tool, preserved so it * round-trips on recall, matching the sync path. */ approvalGrant?: Record<string, unknown>; baseProviderMetadata: ProviderMetadata | undefined; /** Optional transcript payload transforms (supplied by both engines; see docblock). */ transformForTranscript?: (result: unknown) => Promise<{ transcriptArgs: unknown; transcriptResult: unknown; providerMetadata: ProviderMetadata | undefined; }>; toModelOutput?: (output: unknown) => unknown; generateId?: () => string; logger?: IMastraLogger; /** Engine-specific memory flush (save-queue wiring differs per engine). */ flush: () => Promise<void>; }): Promise<void>; /** Lifecycle payload delivered by the background-task manager's onExecution hook. */ export interface BackgroundToolExecutionParams { toolCallId: string; toolName: string; runId: string; taskId: string; startedAt: Date; suspendedAt?: Date; suspendPayload?: unknown; } /** A `tool-call-suspended` chunk, as handed to the run's tool payload transform. */ export interface BackgroundSuspensionChunk { type: 'tool-call-suspended'; runId: string; payload: { toolCallId: string; toolName: string; args: unknown; suspendPayload: unknown; }; metadata?: Record<string, any>; } /** * Shared background-task execution injector: records the task's lifecycle on * the assistant message that holds the tool call. While the task is suspended * the tool call carries a `suspendedTools[toolCallId]` entry (the same shape * as a foreground suspension, plus `taskId` and `suspendedAt`), so a client * that reloads the thread can still see and answer the open request. The * entry's args and payload go through the run's transcript transform, as a * foreground suspension's do. When the manager reports the task running again * (resume) or cancels it, the entry this task wrote is removed. Returns * whether that entry changed, so an engine that doesn't flush every lifecycle * update knows it must flush now. */ export declare function applyBackgroundToolExecution(deps: { params: BackgroundToolExecutionParams; /** Tool args with `_background` removed. */ args: unknown; messageList: MessageList; /** Applies the run's tool payload transform to a chunk. */ transformChunk: (chunk: BackgroundSuspensionChunk) => Promise<BackgroundSuspensionChunk>; }): Promise<boolean>; //# sourceMappingURL=background-task-result-core.d.ts.map