Skip to content

Self-hosted workflows for coding-agent delivery. Orchestrate CodeBuddy, Codex and Claude Code with scripts and human approval gates. Includes Local workflows for coding assistants.

3.8 KB
Raw Blame
package httpapi

import (
	"context"
	"errors"
	"regexp"

	"agentflow/internal/runtime"
)

// freshPhasePrompt rebuilds the full transcript for a fresh native session.
// req.workspaceMode follows the current phase, so a fallback after a handoff
// describes the new mode rather than the one the turn started in.
func (h *assistantHandlers) freshPhasePrompt(req assistantRequest, sourceRoot string, switches int) string {
	prompt := h.turnPrompt(req, sourceRoot, false)
	if switches > 0 {
		// The fresh session has no record of the switch it continues from.
		prompt += "\n本轮之前的阶段已执行过操作。先核对工作区实际文件与状态,避免重复执行。"
		if req.workspaceMode {
			prompt += "本轮已经进入工作区模式,你现在就在工作区里:直接用原生文件与命令工具继续,不要再调用 workbench_workspace_open 或 workbench_platform_mode,除非任务确实需要换运行环境或回到平台管理。"
		} else {
			prompt += "本轮已经切回平台模式:直接用平台工具继续,不要为重做上一阶段的操作再切换模式。"
		}
	}
	return prompt
}

// assistantResumeRejectedRe matches providers refusing a replayed native
// journal: a 400/422, or the wording CodeBuddy and OpenAI-compatible
// gateways use for a transcript whose structure they reject.
var assistantResumeRejectedRe = regexp.MustCompile(`(?i)(must end with a user message|request parameters were rejected|invalid_request_error|invalid request|(?:status|status code|http)[ =:]*(?:400|422)\b|\b(?:400|422) bad request)`)

// assistantPhaseProgress reports whether an event shows the CLI got past
// replaying its journal: model output or a tool call. A phase that failed
// with none of these executed nothing and can safely start over.
func assistantPhaseProgress(ev assistantStreamEvent) bool {
	switch ev.Kind {
	case "tool", "toolDone", "toolEnd", "text", "answer", "agentText", "agentModel", "draftStart", "draftDelta", "thinking", "thinkingStart":
		return true
	}
	return false
}

// assistantResumeRecoverable reports whether resuming a native session failed
// because the journal itself is unusable (missing, or rejected by the
// provider) rather than for a reason a fresh session would hit again or that
// the user must fix: cancellation, credentials, an unavailable execution
// machine, a missing CLI, rate limits, quota or network failures.
func assistantResumeRecoverable(ctx context.Context, err error) bool {
	if err == nil || ctx.Err() != nil {
		return false
	}
	if errors.Is(err, errAssistantSessionMissing) {
		return true
	}
	var credential assistantCredentialError
	if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) ||
		errors.Is(err, runtime.ErrConversationNotStarted) || errors.Is(err, errAssistantExecutionUnavailable) ||
		errors.Is(err, errAssistantCredentialRejected) || errors.Is(err, errAssistantCLIMissing) ||
		errors.Is(err, runtime.ErrMaxTurns) || errors.As(err, &credential) {
		return false
	}
	failure := classifyAssistantFailure(ctx, err)
	if assistantResumeRejectedRe.MatchString(err.Error()) {
		// A rejection wording outranks generic exit codes, but never an
		// explicit provider cause such as authentication or rate limiting.
		return assistantFailureGeneric(failure.Code)
	}
	return failure.Code == "MISSING_RESULT" || failure.Code == "CLI_EXIT_NONZERO"
}

// assistantHandoffOutlivesPhaseError reports whether a phase error should be
// ignored because a mode handoff already succeeded in that phase. The old CLI
// may end without type=result once it has nothing left to do; failing the
// turn there would leave the session in the old mode.
func assistantHandoffOutlivesPhaseError(ctx context.Context, s *AgentToolSession, err error) bool {
	return err != nil && s != nil && s.requestedMode() != "" && ctx.Err() == nil && !errors.Is(err, runtime.ErrConversationNotStarted)
}