Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

1.6 KB
Raw Blame
import type { AgentRuntimeHost } from '../transport';
import type { AgentEvent, AgentInstruction, InstructionExecutor } from '../types';

/**
 * `finish` executor — terminates the operation.
 *
 * First executor migrated from the server into this package as part of the
 * agent-runtime IO transport port abstraction: it depends only on
 * the `StreamSink` + `OperationStore` transports and the operation context, so
 * the server just provides those adapters. Behavior mirrors the previous
 * server-local implementation exactly.
 */
export const finish =
  (host: AgentRuntimeHost): InstructionExecutor =>
  async (instruction, state) => {
    const { reason, reasonDetail } = instruction as Extract<AgentInstruction, { type: 'finish' }>;
    const { operation, transports } = host;

    // Clear the topic's running-operation mark so a reconnect doesn't
    // re-trigger after completion. Best-effort — the adapter swallows failures.
    await transports.operationStore?.clearRunningMark();

    // State snapshots are opt-in; internal done events always retain the state.
    await transports.stream.publishEvent({
      data: {
        ...(state.host?.includeFinalState === true && { finalState: { ...state, status: 'done' } }),
        phase: 'execution_complete',
        reason,
        reasonDetail,
      },
      stepIndex: operation.stepIndex,
      type: 'step_complete',
    });

    const newState = structuredClone(state);
    newState.lastModified = new Date().toISOString();
    newState.status = 'done';

    const events: AgentEvent[] = [{ finalState: newState, reason, reasonDetail, type: 'done' }];

    return { events, newState };
  };