import { readRuntimeMetadata } from '../../runtime-metadata.js';
import { loadRoutingInstructionStateSync } from '../../../router/virtual-router/sticky-session-store.js';
import { logContinueExecution } from '../../../servertool/continue-execution/log.js';
import { buildContinueExecutionOperationsWithNative, injectContinueExecutionDirectiveWithNative, planContinueExecutionOperationsWithNative, isStopMessageStateActiveWithNative, resolveStopMessageSessionScopeWithNative } from '../../../router/virtual-router/engine-selection/native-chat-process-servertool-orchestration-semantics.js';
import { isClientInjectReady } from './client-inject-readiness.js';
const CONTINUE_EXECUTION_INJECTION_MARKER = '[routecodex:continue_execution_injection]';
const CONTINUE_EXECUTION_INJECTION_TEXT = '继续执行';
export function buildContinueExecutionOperations(metadata, options) {
    const rt = (readRuntimeMetadata(metadata) ?? {});
    const sessionId = readString(metadata.sessionId);
    const hasSessionId = Boolean(sessionId && sessionId.trim());
    if (!isClientInjectReady(metadata)) {
        logContinueExecution('skip_schema_due_client_inject_unready', { hasSessionId });
        return [];
    }
    const hasActiveStopMessage = options && typeof options.hasActiveStopMessage === 'boolean'
        ? options.hasActiveStopMessage
        : hasActiveStopMessageStateForContinueExecution(metadata);
    const plan = options?.precomputedPlan ??
        planContinueExecutionOperationsWithNative(rt, hasActiveStopMessage);
    const operations = buildContinueExecutionOperationsWithNative(Boolean(plan.shouldInject));
    if (operations.length === 0) {
        if (hasActiveStopMessage) {
            logContinueExecution('skip_schema_due_stopmessage', { hasSessionId });
        }
        return [];
    }
    logContinueExecution('inject_schema', { hasSessionId });
    return operations;
}
export function injectContinueExecutionDirectiveIntoUserMessage(request, metadata) {
    const rt = (readRuntimeMetadata(metadata) ?? {});
    if (!isClientInjectReady(metadata)) {
        return request;
    }
    if (rt?.serverToolFollowup === true) {
        return request;
    }
    if (hasActiveStopMessageStateForContinueExecution(metadata)) {
        const sessionId = readString(metadata.sessionId);
        const hasSessionId = Boolean(sessionId && sessionId.trim());
        logContinueExecution('skip_user_directive_due_stopmessage', { hasSessionId });
        return request;
    }
    const messages = Array.isArray(request.messages) ? request.messages : [];
    if (messages.length === 0) {
        return request;
    }
    const injected = injectContinueExecutionDirectiveWithNative(messages, CONTINUE_EXECUTION_INJECTION_MARKER, CONTINUE_EXECUTION_INJECTION_TEXT);
    if (!injected.changed) {
        return request;
    }
    const sessionId = readString(metadata.sessionId);
    const hasSessionId = Boolean(sessionId && sessionId.trim());
    logContinueExecution('inject_user_directive', { hasSessionId });
    return {
        ...request,
        messages: injected.messages
    };
}
function hasActiveStopMessageStateForContinueExecution(metadata) {
    if (!isClientInjectReady(metadata)) {
        return false;
    }
    const persistedState = resolvePersistedStopMessageState(metadata);
    return isStopMessageStateActiveWithNative(persistedState);
}
export function resolveHasActiveStopMessageForContinueExecution(metadata) {
    return hasActiveStopMessageStateForContinueExecution(metadata);
}
function resolvePersistedStopMessageState(metadata) {
    const sessionScope = resolveStopMessageSessionScope(metadata);
    if (!sessionScope) {
        return undefined;
    }
    return loadRoutingInstructionStateSync(sessionScope);
}
function resolveStopMessageSessionScope(metadata) {
    return resolveStopMessageSessionScopeWithNative(metadata);
}
function readString(value) {
    return typeof value === 'string' ? value : undefined;
}
