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.

46.6 KB
Raw Blame
package httpapi

import (
	"archive/zip"
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"log"
	"mime"
	"net/http"
	"os"
	"path/filepath"
	"regexp"
	"slices"
	"sort"
	"strconv"
	"strings"
	"time"

	"agentflow/internal/domain"
	"agentflow/internal/runtime"

	"github.com/gin-gonic/gin"
)

const workbenchAttachmentLimit = 10 << 20
const workbenchAttachmentCount = 8

type workbenchAttachmentRecord struct {
	domain.AssistantAttachment
	ProjectID string `json:"projectId"`
}

// Open legacy projects retain the room membership policy. Private projects
// require an explicit member or owner; a reference never grants access.
func (h *assistantHandlers) workbenchProjectAllowed(c *gin.Context, projectID string, userID int64) bool {
	if projectID == "" {
		return true
	}
	p, ok := h.repo.Project(projectID)
	if !ok {
		return false
	}
	return workbenchProjectAllowsUser(p, userID, h.isPlatformAdmin(c.Request))
}

func workbenchProjectAllowsUser(p domain.Project, userID int64, platformAdmin bool) bool {
	if platformAdmin || p.OwnerID == userID || (p.OwnerID == 0 && len(p.Members) == 0) {
		return true
	}
	for _, m := range p.Members {
		if m.UserID == userID {
			return true
		}
	}
	return false
}

func (h *assistantHandlers) workbenchReference(ref domain.AssistantReference, project string) (domain.AssistantReference, string, error) {
	allowed := func(id string, shared bool) bool { return project != "" && (id == project || shared) }
	var data any
	switch ref.Type {
	case "issue":
		v, ok := h.repo.Issue(ref.ID)
		if !ok || v.ProjectID != project || project == "" {
			break
		}
		ref.Name = v.Title
		data = gin.H{"id": v.ID, "title": v.Title, "description": v.Description, "status": v.Status, "projectId": v.ProjectID,
			"repositoryId": v.RepositoryID, "repositoryIds": v.RepositoryIDs}
	case "sop":
		v, ok := h.repo.Workflow(ref.ID)
		if !ok || !(v.Generic && project != "" || allowed(v.ProjectID, v.Shared)) {
			break
		}
		ref.Name = v.Name
		data = gin.H{"id": v.ID, "name": v.Name, "status": v.Status, "nodes": v.Nodes, "edges": v.Edges}
	case "agent":
		v, ok := h.repo.Agent(ref.ID)
		if !ok || !allowed(v.ProjectID, v.Shared) {
			break
		}
		ref.Name = v.Name
		data = gin.H{"id": v.ID, "name": v.Name, "description": v.Description, "prompt": v.Prompt}
	case "skill":
		v, ok := h.repo.Skill(ref.ID)
		if !ok || !allowed(v.ProjectID, v.Shared) {
			break
		}
		ref.Name = v.Name
		data = gin.H{"id": v.ID, "name": v.Name, "description": v.Description, "skill": runtime.SkillName(v.ID)}
	case "repository":
		v, ok := h.repo.Repository(ref.ID)
		if !ok || v.ProjectID != project || project == "" {
			break
		}
		ref.Name = v.Name
		data = gin.H{"id": v.ID, "name": v.Name, "projectId": v.ProjectID}
	case "run":
		v, ok := h.repo.RunState(ref.ID)
		if !ok || v.ExecutionContext.ProjectID != project || project == "" {
			break
		}
		ref.Name = v.WorkflowSnapshot.Name + " · " + v.ID
		nodes := []gin.H{}
		for id, n := range v.Nodes {
			if n == nil {
				continue
			}
			nodes = append(nodes, gin.H{"id": id, "agentId": runNodeAgentID(v, id), "status": n.Status, "attempt": n.Attempt, "reworkCount": n.ReworkCount, "output": truncateRunes(n.Output, 4000), "error": truncateRunes(n.Error, 2000)})
		}
		data = gin.H{"id": v.ID, "status": v.Status, "input": v.Input, "nodes": nodes, "pauseRequested": v.PauseRequested, "cancelRequested": v.CancelRequested}
	}
	if data == nil {
		return ref, "", fmt.Errorf("引用对象不存在或不属于当前项目:%s/%s", ref.Type, ref.ID)
	}
	raw, _ := json.Marshal(data)
	return ref, truncateRunes(string(raw), 18000), nil
}

// Resolve project ownership from the stored object, never client metadata.
func (h *assistantHandlers) accessibleWorkbenchReference(c *gin.Context, userID int64, ref domain.AssistantReference, currentProject string) (domain.AssistantReference, string, error) {
	project := currentProject
	shared := false
	switch ref.Type {
	case "issue":
		if v, ok := h.repo.Issue(ref.ID); ok {
			project = v.ProjectID
		}
	case "run":
		if v, ok := h.repo.RunState(ref.ID); ok {
			project = v.ExecutionContext.ProjectID
		}
	case "repository":
		if v, ok := h.repo.Repository(ref.ID); ok {
			project = v.ProjectID
		}
	case "agent":
		if v, ok := h.repo.Agent(ref.ID); ok {
			project, shared = v.ProjectID, v.Shared
		}
	case "skill":
		if v, ok := h.repo.Skill(ref.ID); ok {
			project, shared = v.ProjectID, v.Shared
		}
	case "sop":
		if v, ok := h.repo.Workflow(ref.ID); ok {
			project, shared = v.ProjectID, v.Shared || v.Generic
		}
	}
	if !shared && !h.workbenchProjectAllowed(c, project, userID) {
		return ref, "", fmt.Errorf("无权访问引用对象")
	}
	// A project conversation only sees its own project's objects plus shared
	// roles and SOPs; a personal conversation keeps cross-project references.
	if currentProject != "" && !shared && project != currentProject {
		return ref, "", errForeignReference
	}
	resolveProject := project
	if shared {
		resolveProject = currentProject
		if resolveProject == "" {
			resolveProject = project
		}
		if resolveProject == "" {
			resolveProject = "shared"
		}
	}
	canonical, data, err := h.workbenchReference(ref, resolveProject)
	canonical.ProjectID, canonical.ProjectName = project, ""
	if p, ok := h.repo.Project(project); ok {
		canonical.ProjectName = p.Name
	}
	return canonical, data, err
}

var errForeignReference = fmt.Errorf("只能引用当前项目的需求、角色、技能、SOP、仓库和运行;其他项目的对象请到对应项目的对话里引用")

var errNotProjectMember = fmt.Errorf("你还不是这个项目的成员,不能在项目里对话。可以把项目切换为「个人对话」直接聊,或请项目 Owner 把你加入项目")

func (h *assistantHandlers) prepareWorkbenchContext(c *gin.Context, req *assistantRequest, user domain.User) error {
	if !h.workbenchProjectAllowed(c, req.ProjectID, user.ID) {
		return errNotProjectMember
	}
	if req.SessionID != "" {
		if old, ok := h.repo.AssistantSession(assistantStorageOwner(h.repo, req.SessionID, user.ID), req.SessionID); ok && old.ProjectID != req.ProjectID {
			return fmt.Errorf("会话属于其他项目,请新建会话")
		}
	}
	// Existing personal attachments remain usable when an unassigned conversation
	// is explicitly bound to an authorized project. Never infer another user's files.
	historicalAttachments := map[string]bool{}
	if old, ok := h.repo.AssistantSession(assistantStorageOwner(h.repo, req.SessionID, user.ID), req.SessionID); ok && !old.Shared && old.ProjectID == req.ProjectID {
		for _, message := range old.Messages {
			for _, attachment := range message.Attachments {
				historicalAttachments[attachment.ID] = true
			}
		}
	}
	req.attachmentIDs = map[string]bool{}
	req.attachmentOwners = map[string]int64{}
	if len(req.Messages) > 200 {
		return fmt.Errorf("会话过长,请开启新会话")
	}
	var context strings.Builder
	if s, ok := h.repo.AssistantSession(assistantStorageOwner(h.repo, req.SessionID, user.ID), req.SessionID); ok && s.IssueID != "" && req.RunID == "" {
		if err := h.validateSessionIssue(c, req.ProjectID, s.IssueID, user.ID); err != nil {
			return err
		}
		_, data, err := h.workbenchReference(domain.AssistantReference{Type: "issue", ID: s.IssueID}, req.ProjectID)
		if err != nil {
			return fmt.Errorf("关联需求不可用,请更换或解除关联:%w", err)
		}
		context.WriteString("当前会话关联的主需求(每轮从平台读取的最新数据):" + data + "\n这是持续的需求上下文;其他 @需求只供本条消息参考。关联不会自动创建或派发运行,也不会改变会话可见范围。\n")
	}
	context.WriteString("当前任务以 runId 为准;普通会话引用只提供上下文。@角色表示让该角色作为子 Agent 来做这件事。\n")
	if branch, ok := h.repo.AssistantSession(assistantStorageOwner(h.repo, req.SessionID, user.ID), req.SessionID); ok && branch.ParentSessionID != "" {
		context.WriteString("这是编辑消息后的讨论分支。原会话后续执行过的文件修改和平台变更不会回退,可能已经存在;写入前先读取真实状态,避免重复导入、创建或派发。历史 inherited 草稿仅供参考,不可认为它们在当前分支重新执行。\n")
	}
	if req.RunID != "" {
		_, text, err := h.workbenchReference(domain.AssistantReference{Type: "run", ID: req.RunID}, req.ProjectID)
		if err != nil {
			return err
		}
		r, _ := h.repo.RunState(req.RunID)
		if !h.projectWriterAllowed(c, domain.RunProjectID(r, ""), user.ID) {
			return fmt.Errorf("只读成员不能在任务会话中执行 Agent 或修改工作区")
		}
		context.WriteString("当前 SOP 运行:" + text + "\nSOP 推进沿用既有暂停、审批、拒绝和重试接口。工作区工具用于停止后的修改与验证。\n")
	}
	var mentioned []domain.AgentDefinition
	var mentionedSkills []domain.SkillDefinition
	addSkill := func(skill domain.SkillDefinition) {
		for _, known := range req.skills {
			if known.ID == skill.ID {
				return
			}
		}
		req.skills = append(req.skills, skill)
	}
	addRole := func(role domain.AgentDefinition) {
		for _, known := range req.roles {
			if known.Role.ID == role.ID {
				return
			}
		}
		req.roles = append(req.roles, h.resolveRoleSubagent(role, req.ProjectID, user.ID))
	}
	// Every project role is a subagent Main Bot may dispatch on its own
	// judgment; shared roles join once mentioned.
	if req.ProjectID != "" {
		for _, role := range h.repo.Agents() {
			if role.ProjectID == req.ProjectID {
				addRole(role)
			}
		}
	}
	for i := range req.Messages {
		m := &req.Messages[i]
		if len(m.References) > 12 || len(m.Attachments) > workbenchAttachmentCount {
			return fmt.Errorf("单条消息最多引用 12 个对象、附加 8 个文件")
		}
		for j, ref := range m.References {
			if i != len(req.Messages)-1 {
				// Shared roles and skills mentioned earlier stay available for follow-ups.
				if ref.Type == "agent" {
					if role, ok := h.repo.Agent(ref.ID); ok && req.ProjectID != "" && (role.ProjectID == req.ProjectID || role.Shared) {
						addRole(role)
					}
				}
				if ref.Type == "skill" {
					if skill, ok := h.repo.Skill(ref.ID); ok && (req.ProjectID == "" || skill.ProjectID == req.ProjectID || skill.Shared) {
						addSkill(skill)
					}
				}
				continue
			}
			if ref.Type == pageElementReference {
				canonical, text, err := pageElementContext(ref)
				if err != nil {
					return err
				}
				m.References[j] = canonical
				context.WriteString("引用(数据):" + text + "\n")
				continue
			}
			if req.RunID != "" {
				r, ok := h.repo.RunState(req.RunID)
				if !ok || !h.taskReferenceAllowed(r, ref.Type, ref.ID) {
					return fmt.Errorf("只能引用当前任务的角色、需求、SOP 和仓库")
				}
			}
			canonical, text, err := h.accessibleWorkbenchReference(c, user.ID, ref, req.ProjectID)
			if err != nil {
				return err
			}
			m.References[j] = canonical
			context.WriteString("引用(数据):" + text + "\n")
			if ref.Type == "agent" {
				if role, ok := h.repo.Agent(ref.ID); ok {
					addRole(role)
					mentioned = append(mentioned, role)
				}
			}
			if ref.Type == "skill" {
				if skill, ok := h.repo.Skill(ref.ID); ok {
					addSkill(skill)
					mentionedSkills = append(mentionedSkills, skill)
				}
			}
		}
		for j, a := range m.Attachments {
			attachmentOwner := user.ID
			if stored, ok := h.repo.AssistantSession(assistantStorageOwner(h.repo, req.SessionID, user.ID), req.SessionID); ok && i < len(stored.Messages) {
				attachmentOwner = stored.Messages[i].AuthorID
				if attachmentOwner == 0 {
					attachmentOwner = stored.UserID
				}
			}
			record, _, err := loadWorkbenchAttachment(attachmentOwner, a.ID)
			if err != nil || (record.ProjectID != req.ProjectID && !(record.ProjectID == "" && historicalAttachments[a.ID])) {
				return fmt.Errorf("附件不存在或不属于当前用户和项目")
			}
			m.Attachments[j] = record.AssistantAttachment
			req.attachmentIDs[a.ID] = true
			req.attachmentOwners[a.ID] = attachmentOwner
		}
	}
	prefs := h.repo.AssistantPreferences(user.ID)
	if len(req.roles) > 0 {
		context.WriteString("本项目的角色已作为子 Agent 提供(Agent 工具的 subagent_type=role-<角色ID>),各自带着角色的 prompt、规则和技能。")
		if !prefs.ProactiveRoles {
			context.WriteString("任务明显属于某个角色的职责、或需要它的规则和技能时,可以自行派给它;简单问题自己处理。")
		}
		context.WriteString("子 Agent 看不到本对话,派发的 prompt 要写清背景、要做什么、工作区里相关的仓库和文件路径。同一角色在本对话里已经派过(前几轮,或本轮早先 Agent 结果里的 Agent ID),默认传 resume=<agent ID> 复用那个子 Agent,让它带着之前的上下文继续;只有用户要求新开、新任务与它之前做的事无关、或要与它并行时才不传 resume。resume 失败时直接新开。\n")
	}
	hasSOP := false
	for _, wf := range h.repo.Workflows() {
		if req.ProjectID != "" && wf.ProjectID == req.ProjectID {
			hasSOP = true
			break
		}
	}
	context.WriteString(delegationContext(req, prefs, normalizeAssistantApprovalMode(req.ApprovalMode), hasSOP))
	context.WriteString(assistantInstructionsContext(prefs))
	if len(req.roles) > 0 {
		if stored, ok := h.repo.AssistantSession(assistantStorageOwner(h.repo, req.SessionID, user.ID), req.SessionID); ok {
			ids := roleAgentIDs(stored.Messages)
			var reuse strings.Builder
			for _, role := range req.roles {
				name := roleSubagentName(role.Role.ID)
				if id := ids[name]; id != "" {
					reuse.WriteString("「" + role.Role.Name + "」subagent_type=" + name + " → agent ID " + id + ";")
				}
			}
			if reuse.Len() > 0 {
				context.WriteString("这些角色在本对话里已有子 Agent:" + reuse.String() + "再派给这些角色时按上面的规则默认传 resume 复用。\n")
			}
		}
	}
	if len(mentioned) > 0 {
		var dispatch strings.Builder
		for _, role := range mentioned {
			dispatch.WriteString("「" + role.Name + "」→ subagent_type=" + roleSubagentName(role.ID) + ";")
		}
		context.WriteString("用户本轮 @ 了角色:" + dispatch.String() + "用 Agent 工具把用户要做的事派发给对应子 Agent 执行,prompt 写清任务、仓库/工作区和期望产物;多个角色互不依赖时可并行派发。子 Agent 返回后,由你核对结果并向用户汇报。\n")
	}
	if len(mentionedSkills) > 0 {
		var use strings.Builder
		for _, skill := range mentionedSkills {
			use.WriteString("「" + skill.Name + "」→ skill=" + runtime.SkillName(skill.ID) + ";")
		}
		context.WriteString("用户本轮 @ 了技能:" + use.String() + "用 Skill 工具按名称调用并照它的说明完成用户要做的事;如果把这件事派给子 Agent,在派发的 prompt 里写明要用这个技能。\n")
	}
	if req.RunID == "" {
		if note := h.linkMentionedIssue(req, user); note != "" {
			context.WriteString(note + "\n")
		}
		if note := h.workspaceTurnContext(req, user); note != "" {
			context.WriteString(note + "\n")
		}
	}
	if req.RunID != "" {
		context.WriteString("你是 Main Bot。先核对当前任务运行状态;SOP 执行中只读查看,停止后才能接管工作区。需要编程时先调用 workbench_workspace_open;handoff=true 时只回复一句「好的。」并结束本阶段,不向用户解释交接等内部机制,让平台自动续接 CLI,handoff=false 时先用 runs_pause 或 runs_cancel 停下 SOP,之后使用原生文件、补丁和命令工具修改并验证。工作区不可用时如实说明,不要假装修改成功。\n")
		context.WriteString("当前任务有一个真实 Chromium(Agent 浏览器),用户在任务面板实时看到并可接管,阶段 Agent 也用同一个浏览器:workbench_browser_navigate 打开网页,workbench_browser_snapshot 读取页面文本和带 ref 的可交互元素,workbench_browser_click/type/press 操作,workbench_browser_screenshot 查看画面,workbench_browser_tabs 管理标签页。用户让你打开或操作网页时直接使用它们,不要声称没有浏览器,也不要用 curl 抓取代替;页面被用户改动时以最新快照为准。\n")
	}
	req.contextText = beijingTimestamps(context.String())
	return nil
}

func (h *assistantHandlers) workbenchReferences(c *gin.Context) {
	u, ok := h.requestUser(c.Request, true)
	if !ok {
		c.JSON(401, gin.H{"error": "请先登录"})
		return
	}
	project := strings.TrimSpace(c.Query("projectId"))
	if !h.workbenchProjectAllowed(c, project, u.ID) {
		c.JSON(403, gin.H{"error": "无权访问该项目"})
		return
	}
	out := []domain.AssistantReference{}
	var task *domain.Run
	if runID := strings.TrimSpace(c.Query("runId")); runID != "" {
		r, exists := h.repo.RunState(runID)
		if !exists || r.IsPersonalWorkspace() || r.ExecutionContext.ProjectID != project || !h.workbenchProjectAllowed(c, r.ExecutionContext.ProjectID, u.ID) {
			c.JSON(403, gin.H{"error": "无权访问任务"})
			return
		}
		task = &r
	}
	global := c.Query("scope") == "all"
	q := strings.ToLower(strings.TrimSpace(c.Query("q")))
	kindFilter := c.Query("type")
	allowedProjects := map[string]bool{}
	projectNames := map[string]string{}
	for _, p := range h.repo.Projects() {
		allowedProjects[p.ID] = h.workbenchProjectAllowed(c, p.ID, u.ID)
		projectNames[p.ID] = p.Name
	}
	add := func(kind, id, name, objectProject string, shared bool) {

		if task != nil && !h.taskReferenceAllowed(*task, kind, id) {
			return
		}
		if kindFilter != "" && kind != kindFilter {
			return
		}
		if global {
			if !shared && !allowedProjects[objectProject] {
				return
			}
		} else if project == "" || (objectProject != project && !shared) {
			return
		}
		if q != "" && !strings.Contains(strings.ToLower(name+" "+id+" "+projectNames[objectProject]), q) {
			return
		}
		out = append(out, domain.AssistantReference{Type: kind, ID: id, Name: name, ProjectID: objectProject, ProjectName: projectNames[objectProject]})
	}
	for _, v := range h.repo.Agents() {
		add("agent", v.ID, v.Name, v.ProjectID, v.Shared)
	}
	for _, v := range h.repo.Workflows() {
		add("sop", v.ID, v.Name, v.ProjectID, v.Shared || v.Generic)
	}
	for _, v := range h.repo.Skills() {
		add("skill", v.ID, v.Name, v.ProjectID, v.Shared)
	}
	for _, v := range h.repo.Issues() {
		add("issue", v.ID, v.Title, v.ProjectID, false)
	}
	filter := domain.AnalyticsFilter{}
	if !global {
		filter.ProjectID = project
	}
	runs, err := h.repo.RunSummariesFor(filter, "")
	if err != nil {
		c.JSON(500, gin.H{"error": "读取运行失败"})
		return
	}
	for _, v := range runs {
		add("run", v.ID, v.WorkflowSnapshot.Name+" · "+v.ID, v.ExecutionContext.ProjectID, false)
	}
	for _, v := range h.repo.Repositories() {
		add("repository", v.ID, v.Name, v.ProjectID, false)
	}
	sortAssistantReferences(out)
	if global {
		offset, _ := strconv.Atoi(c.Query("offset"))
		items, next := pageAssistantReferences(out, offset, workbenchReferencePageSize)
		c.JSON(200, gin.H{"items": items, "nextOffset": next})
		return
	}
	c.JSON(200, out)
}

// workbenchReferencePageSize is the page size of the global reference picker.
const workbenchReferencePageSize = 30

// sortAssistantReferences orders references by name, then type, then ID.
func sortAssistantReferences(refs []domain.AssistantReference) {
	sort.Slice(refs, func(i, j int) bool {
		if refs[i].Name != refs[j].Name {
			return refs[i].Name < refs[j].Name
		}
		if refs[i].Type != refs[j].Type {
			return refs[i].Type < refs[j].Type
		}
		return refs[i].ID < refs[j].ID
	})
}

// pageAssistantReferences returns the page starting at offset (clamped into
// range) and the offset of the next page, or 0 when this is the last page.
func pageAssistantReferences(refs []domain.AssistantReference, offset, size int) ([]domain.AssistantReference, int) {
	offset = max(0, min(offset, len(refs)))
	end := min(offset+size, len(refs))
	next := 0
	if end < len(refs) {
		next = end
	}
	return refs[offset:end], next
}

func workbenchAttachmentDir(userID int64) string {
	return filepath.Join(assistantSessionRoot(userID), "attachments")
}

var localMediaPath = regexp.MustCompile(`(?:^|[\s` + "`" + `((::])(/[^\s` + "`" + `))]+\.(?:png|jpe?g|gif|webp))`)

func stripLocalMediaPaths(message string) string {
	cleaned := regexp.MustCompile(`!\[[^\]]*\]\((?:/|file://)[^)]*\)`).ReplaceAllString(message, "")
	cleaned = localMediaPath.ReplaceAllString(cleaned, "")
	var lines []string
	for _, line := range strings.Split(cleaned, "\n") {
		plain := strings.Trim(strings.TrimSpace(line), "`:: \t*")
		if plain == "文件位置" || plain == "图片位置" || plain == "路径" || plain == "文件路径" {
			continue
		}
		lines = append(lines, strings.TrimRight(line, " \t"))
	}
	return strings.TrimSpace(strings.Join(lines, "\n"))
}

func saveWorkbenchImage(userID int64, name, mime string, data []byte, projectIDs ...string) (domain.AssistantAttachment, error) {
	token := make([]byte, 16)
	if _, err := rand.Read(token); err != nil {
		return domain.AssistantAttachment{}, err
	}
	id := hex.EncodeToString(token)
	if len(name) > 240 {
		name = name[:240]
	}
	record := workbenchAttachmentRecord{AssistantAttachment: domain.AssistantAttachment{ID: id, Name: name, MimeType: mime, Size: int64(len(data))}}
	if len(projectIDs) > 0 {
		record.ProjectID = projectIDs[0]
	}
	dir := workbenchAttachmentDir(userID)
	if err := os.MkdirAll(dir, 0700); err != nil {
		return domain.AssistantAttachment{}, err
	}
	if err := os.WriteFile(filepath.Join(dir, id+".bin"), data, 0600); err != nil {
		return domain.AssistantAttachment{}, err
	}
	raw, _ := json.Marshal(record)
	if err := os.WriteFile(filepath.Join(dir, id+".json"), raw, 0600); err != nil {
		os.Remove(filepath.Join(dir, id+".bin"))
		return domain.AssistantAttachment{}, err
	}
	return record.AssistantAttachment, nil
}

// saveAgentReplyFiles stores what Main Bot handed the user during the turn as
// attachments kept with the conversation's media owner.
func saveAgentReplyFiles(owner int64, files []agentReplyFile, projectID string) []domain.AssistantAttachment {
	var out []domain.AssistantAttachment
	for _, f := range files {
		item, err := saveWorkbenchImage(owner, f.Name, f.MimeType, f.Data, projectID)
		if err != nil {
			log.Printf("assistant reply file %s: %v", f.Name, err)
			continue
		}
		out = append(out, item)
	}
	return out
}

func workspaceImages(userID int64, since, until time.Time, projectIDs ...string) []domain.AssistantAttachment {
	return workspaceImagesIn(assistantCLIWorkspaceForUser(userID), userID, since, until, projectIDs...)
}

// workspaceImagesIn imports fresh generated images from a CLI workspace into
// the attachment store of owner.
func workspaceImagesIn(workspace string, owner int64, since, until time.Time, projectIDs ...string) []domain.AssistantAttachment {
	userID := owner
	dir := filepath.Join(workspace, "generated-images")
	entries, err := os.ReadDir(dir)
	if err != nil {
		return nil
	}
	var out []domain.AssistantAttachment
	for _, entry := range entries {
		if entry.IsDir() || len(out) >= 4 {
			continue
		}
		name := entry.Name()
		if name != filepath.Base(name) || strings.Contains(name, "..") {
			continue
		}
		ext := strings.ToLower(filepath.Ext(name))
		mime := map[string]string{".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg", ".gif": "image/gif", ".webp": "image/webp"}[ext]
		if mime == "" {
			continue
		}
		info, err := entry.Info()
		if err != nil || info.Size() == 0 || info.Size() > 8<<20 {
			continue
		}
		if !since.IsZero() && info.ModTime().Before(since.Add(-3*time.Second)) {
			continue
		}
		if !until.IsZero() && info.ModTime().After(until) {
			continue
		}
		data, err := os.ReadFile(filepath.Join(dir, name))
		if err != nil || !strings.HasPrefix(http.DetectContentType(data), "image/") {
			continue
		}
		item, err := saveWorkbenchImage(userID, name, mime, data, projectIDs...)
		if err != nil {
			continue
		}
		out = append(out, item)
	}
	return out
}

type workspaceDeck struct {
	Files  []domain.AssistantAttachment
	Slides string
}

var (
	workbuddyLink = regexp.MustCompile(`\[[^\]\n]*\]\(https?://[^)\s]*workbuddy\.cn[^)\s]*\)|https?://[^\s)]*workbuddy\.cn[^\s)]*`)
	pptxTextRun   = regexp.MustCompile(`<a:t[^>]*>([^<]*)</a:t>`)
	pptxParagraph = regexp.MustCompile(`(?s)<a:p(?:\s[^>]*)?>.*?</a:p>`)
	slideXMLName  = regexp.MustCompile(`(?:^|/)slide(\d+)\.xml$`)
)

func workspaceDecks(userID int64, since, until time.Time) workspaceDeck {
	return workspaceDecksIn(assistantSessionRoot(userID), userID, since, until)
}

// workspaceDecksIn imports fresh decks produced under a CLI session root
// (its ws and home/tmp) into the attachment store of owner.
func workspaceDecksIn(root string, owner int64, since, until time.Time) workspaceDeck {
	userID := owner
	var deck workspaceDeck
	paths := recentDeckPathsIn(root, since, until)
	if len(paths) == 0 {
		return deck
	}
	sort.Slice(paths, func(i, j int) bool {
		ai, _ := os.Stat(paths[i])
		aj, _ := os.Stat(paths[j])
		if ai == nil || aj == nil {
			return paths[i] < paths[j]
		}
		return ai.ModTime().After(aj.ModTime())
	})
	for _, path := range paths {
		if len(deck.Files) >= 2 {
			break
		}
		info, err := os.Lstat(path)
		if err != nil || !info.Mode().IsRegular() || info.Size() == 0 || info.Size() > 8<<20 {
			continue
		}
		data, err := os.ReadFile(path)
		if err != nil {
			continue
		}
		name := filepath.Base(path)
		item, err := saveWorkbenchImage(userID, name, "application/vnd.openxmlformats-officedocument.presentationml.presentation", data)
		if err != nil {
			continue
		}
		deck.Files = append(deck.Files, item)
		if deck.Slides == "" {
			deck.Slides = pptxSlides(data)
		}
	}
	return deck
}

func recentDeckPathsIn(root string, since, until time.Time) []string {
	var out []string
	out = append(out, deckFilesUnder(filepath.Join(root, "ws"), 0)...)
	out = append(out, deckFilesUnder(filepath.Join(root, "home", "tmp"), 0)...)
	tmp, err := tempDirDeckFiles()
	if err != nil {
		return uniquePaths(out)
	}
	out = append(out, tmp...)
	return filterRecentRegularFiles(uniquePaths(out), since, until)
}

// tempDirDeckFiles collects pptx files directly under the system temp dir and
// inside temp subdirectories whose names look deck-related.
func tempDirDeckFiles() ([]string, error) {
	entries, err := os.ReadDir(os.TempDir())
	if err != nil {
		return nil, err
	}
	var out []string
	for _, entry := range entries {
		name := entry.Name()
		path := filepath.Join(os.TempDir(), name)
		if !entry.IsDir() {
			if strings.EqualFold(filepath.Ext(name), ".pptx") {
				out = append(out, path)
			}
			continue
		}
		lower := strings.ToLower(name)
		if !strings.Contains(lower, "ppt") && !strings.Contains(lower, "slide") && !strings.Contains(lower, "loopforge") {
			continue
		}
		out = append(out, deckFilesUnder(path, 0)...)
	}
	return out, nil
}

// filterRecentRegularFiles keeps regular files modified within
// [since-30s, until]; a zero since/until leaves that side unbounded.
func filterRecentRegularFiles(paths []string, since, until time.Time) []string {
	var fresh []string
	for _, path := range paths {
		info, err := os.Lstat(path)
		if err != nil || !info.Mode().IsRegular() {
			continue
		}
		if !since.IsZero() && info.ModTime().Before(since.Add(-30*time.Second)) {
			continue
		}
		if !until.IsZero() && info.ModTime().After(until) {
			continue
		}
		fresh = append(fresh, path)
	}
	return fresh
}

func deckFilesUnder(root string, depth int) []string {
	if depth > 4 {
		return nil
	}
	entries, err := os.ReadDir(root)
	if err != nil {
		return nil
	}
	var out []string
	for _, entry := range entries {
		if len(out) >= 8 {
			break
		}
		name := entry.Name()
		if name == "" || strings.HasPrefix(name, ".") {
			continue
		}
		path := filepath.Join(root, name)
		info, err := entry.Info()
		if err != nil || info.Mode()&os.ModeSymlink != 0 {
			continue
		}
		if entry.IsDir() {
			out = append(out, deckFilesUnder(path, depth+1)...)
			continue
		}
		if strings.EqualFold(filepath.Ext(name), ".pptx") {
			out = append(out, path)
		}
	}
	return out
}

func uniquePaths(paths []string) []string {
	seen := map[string]bool{}
	var out []string
	for _, path := range paths {
		if path == "" || seen[path] {
			continue
		}
		seen[path] = true
		out = append(out, path)
	}
	return out
}

func pptxSlides(data []byte) string {
	reader, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
	if err != nil {
		return ""
	}
	type slide struct {
		n    int
		text string
	}
	var slides []slide
	for _, file := range reader.File {
		m := slideXMLName.FindStringSubmatch(file.Name)
		if m == nil || strings.Contains(file.Name, "/_rels/") {
			continue
		}
		n, _ := strconv.Atoi(m[1])
		rc, err := file.Open()
		if err != nil {
			continue
		}
		body, err := io.ReadAll(io.LimitReader(rc, 1<<20))
		rc.Close()
		if err != nil {
			continue
		}
		text := pptxSlideText(string(body))
		if text == "" {
			continue
		}
		slides = append(slides, slide{n, text})
	}
	sort.Slice(slides, func(i, j int) bool { return slides[i].n < slides[j].n })
	if len(slides) > 12 {
		slides = slides[:12]
	}
	parts := make([]string, 0, len(slides))
	for _, slide := range slides {
		parts = append(parts, slide.text)
	}
	return strings.Join(parts, "\n---\n")
}

func pptxSlideText(xml string) string {
	var lines []string
	for _, paragraph := range pptxParagraph.FindAllString(xml, -1) {
		var b strings.Builder
		for _, run := range pptxTextRun.FindAllStringSubmatch(paragraph, -1) {
			b.WriteString(run[1])
		}
		line := strings.TrimSpace(b.String())
		if line != "" {
			lines = append(lines, line)
		}
	}
	if len(lines) == 0 {
		return ""
	}
	if len(lines) == 1 {
		return lines[0]
	}
	return lines[0] + "\n" + strings.Join(lines[1:], "\n")
}

func previewSlidesMessage(message, slides string) string {
	message = stripWorkbuddyLinks(message)
	slides = strings.TrimSpace(slides)
	if slides == "" || strings.Contains(message, "```slides") || strings.Contains(message, "```ppt") {
		return strings.TrimSpace(message)
	}
	return strings.TrimSpace(message) + "\n\n```slides\n" + slides + "\n```"
}

func stripWorkbuddyLinks(message string) string {
	cleaned := workbuddyLink.ReplaceAllString(message, "")
	var lines []string
	for _, line := range strings.Split(cleaned, "\n") {
		plain := strings.Trim(strings.TrimSpace(line), "`::* \t-")
		if plain == "已上传到你的资料库" || plain == "点击打开" {
			continue
		}
		lines = append(lines, strings.TrimRight(line, " \t"))
	}
	return strings.TrimSpace(strings.Join(lines, "\n"))
}

func materializeWorkspaceImages(ownerID int64, conv *assistantConversation) bool {
	if conv == nil {
		return false
	}
	seen := map[string]bool{}
	changed := false
	for i := range conv.Messages {
		message := &conv.Messages[i]
		hasImage := false
		for _, item := range message.Attachments {
			seen[item.Name] = true
			if strings.HasPrefix(item.MimeType, "image/") {
				hasImage = true
			}
		}
		if message.Role != "assistant" {
			continue
		}
		if hasImage {
			if cleaned := stripMediaPathsInContent(message.Content); cleaned != message.Content {
				message.Content = cleaned
				changed = true
			}
			continue
		}
		if attachWorkspaceDeck(ownerID, message, seen) {
			changed = true
		}
		if !strings.Contains(message.Content, "generated-images") && !strings.Contains(message.Content, ".png") && !strings.Contains(message.Content, ".jpg") && !strings.Contains(message.Content, ".webp") {
			continue
		}
		since := message.CreatedAt.Add(-3 * time.Minute)
		if message.StartedAt != nil && !message.StartedAt.IsZero() {
			since = message.StartedAt.Add(-3 * time.Second)
		}
		until := message.CreatedAt.Add(2 * time.Minute)
		var fresh []domain.AssistantAttachment
		for _, item := range workspaceImages(ownerID, since, until, conv.ProjectID) {
			if seen[item.Name] {
				continue
			}
			seen[item.Name] = true
			fresh = append(fresh, item)
		}
		if len(fresh) == 0 {
			continue
		}
		message.Attachments = append(message.Attachments, fresh...)
		message.Content = stripMediaPathsInContent(message.Content)
		changed = true
	}
	return changed
}

func attachWorkspaceDeck(ownerID int64, message *assistantMessage, seen map[string]bool) bool {
	if message == nil || strings.Contains(message.Content, "```slides") || strings.Contains(message.Content, "```ppt") {
		return false
	}
	plain := message.Content
	var payload map[string]json.RawMessage
	var text string
	if json.Unmarshal([]byte(message.Content), &payload) == nil {
		if raw, ok := payload["message"]; ok {
			_ = json.Unmarshal(raw, &text)
			plain = text
		}
	}
	lower := strings.ToLower(plain)
	if !strings.Contains(lower, "ppt") && !strings.Contains(plain, "幻灯") && !strings.Contains(lower, "workbuddy") && !strings.Contains(lower, "slides") {
		return false
	}
	since := message.CreatedAt.Add(-3 * time.Minute)
	if message.StartedAt != nil && !message.StartedAt.IsZero() {
		since = message.StartedAt.Add(-3 * time.Second)
	}
	deck := workspaceDecks(ownerID, since, message.CreatedAt.Add(2*time.Minute))
	if deck.Slides == "" && len(deck.Files) == 0 {
		return false
	}
	next := previewSlidesMessage(plain, deck.Slides)
	changed := next != plain
	if changed {
		if payload != nil {
			encoded, err := json.Marshal(next)
			if err != nil {
				return false
			}
			payload["message"] = encoded
			out, err := json.Marshal(payload)
			if err != nil {
				return false
			}
			message.Content = string(out)
		} else {
			message.Content = next
		}
	}
	for _, item := range deck.Files {
		if seen[item.Name] {
			continue
		}
		seen[item.Name] = true
		message.Attachments = append(message.Attachments, item)
		changed = true
	}
	return changed
}

func stripMediaPathsInContent(content string) string {
	var payload map[string]json.RawMessage
	if json.Unmarshal([]byte(content), &payload) != nil {
		return content
	}
	raw, ok := payload["message"]
	if !ok {
		return content
	}
	var message string
	if json.Unmarshal(raw, &message) != nil {
		return content
	}
	cleaned := stripLocalMediaPaths(message)
	if cleaned == message {
		return content
	}
	encoded, err := json.Marshal(cleaned)
	if err != nil {
		return content
	}
	payload["message"] = encoded
	out, err := json.Marshal(payload)
	if err != nil {
		return content
	}
	return string(out)
}
func validWorkbenchAttachmentID(id string) bool {
	if len(id) != 32 {
		return false
	}
	_, err := hex.DecodeString(id)
	return err == nil
}
func loadWorkbenchAttachment(userID int64, id string) (workbenchAttachmentRecord, string, error) {
	var record workbenchAttachmentRecord
	if !validWorkbenchAttachmentID(id) {
		return record, "", os.ErrNotExist
	}
	dir := workbenchAttachmentDir(userID)
	root, err := os.OpenRoot(dir)
	if err != nil {
		return record, "", err
	}
	defer root.Close()
	// Keep metadata reads inside the owner's attachment directory, including
	// when a stored file has been replaced by a symbolic link.
	raw, err := root.ReadFile(id + ".json")
	if err != nil {
		return record, "", err
	}
	if err = json.Unmarshal(raw, &record); err != nil {
		return record, "", err
	}
	return record, filepath.Join(dir, id+".bin"), nil
}

func openWorkbenchAttachment(userID int64, id string) (workbenchAttachmentRecord, *os.File, error) {
	record, path, err := loadWorkbenchAttachment(userID, id)
	if err != nil {
		return record, nil, err
	}
	file, err := os.OpenInRoot(workbenchAttachmentDir(userID), filepath.Base(path))
	return record, file, err
}

// removeWorkbenchAttachment deletes the file and metadata of one attachment
// from the owner's attachment directory and reports whether anything was
// removed. Only well-formed ids are accepted and removal goes through
// os.Root, so nothing outside that directory is touched.
func removeWorkbenchAttachment(userID int64, id string) bool {
	if userID <= 0 || !validWorkbenchAttachmentID(id) {
		return false
	}
	root, err := os.OpenRoot(workbenchAttachmentDir(userID))
	if err != nil {
		return false
	}
	defer root.Close()
	removed := false
	for _, name := range []string{id + ".bin", id + ".json"} {
		if err := root.Remove(name); err == nil {
			removed = true
		} else if !errors.Is(err, os.ErrNotExist) {
			log.Printf("remove attachment %s of user %d: %v", name, userID, err)
		}
	}
	return removed
}

func (h *assistantHandlers) uploadAttachment(c *gin.Context) {
	u, ok := h.requestUser(c.Request, true)
	if !ok {
		c.JSON(401, gin.H{"error": "请先登录"})
		return
	}
	c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, workbenchAttachmentLimit+(1<<20))
	if err := c.Request.ParseMultipartForm(workbenchAttachmentLimit); err != nil {
		c.JSON(413, gin.H{"error": "附件不能超过 10 MB"})
		return
	}
	defer c.Request.MultipartForm.RemoveAll()
	project := strings.TrimSpace(c.PostForm("projectId"))
	if !h.workbenchProjectAllowed(c, project, u.ID) {
		c.JSON(403, gin.H{"error": "无权访问该项目"})
		return
	}
	f, header, err := c.Request.FormFile("file")
	if err != nil {
		c.JSON(400, gin.H{"error": "请选择文件"})
		return
	}
	defer f.Close()
	data, err := io.ReadAll(io.LimitReader(f, workbenchAttachmentLimit+1))
	if err != nil || len(data) > workbenchAttachmentLimit {
		c.JSON(413, gin.H{"error": "附件不能超过 10 MB"})
		return
	}
	if len(data) == 0 {
		c.JSON(400, gin.H{"error": "不能上传空文件"})
		return
	}
	token := make([]byte, 16)
	if _, err = rand.Read(token); err != nil {
		c.JSON(500, gin.H{"error": "创建附件失败"})
		return
	}
	id := hex.EncodeToString(token)
	name := filepath.Base(strings.ReplaceAll(header.Filename, "\\", "/"))
	if len(name) > 240 {
		name = name[:240]
	}
	record := workbenchAttachmentRecord{AssistantAttachment: domain.AssistantAttachment{ID: id, Name: name, MimeType: http.DetectContentType(data), Size: int64(len(data))}, ProjectID: project}
	dir := workbenchAttachmentDir(u.ID)
	if err = os.MkdirAll(dir, 0700); err == nil {
		err = os.WriteFile(filepath.Join(dir, id+".bin"), data, 0600)
	}
	raw, _ := json.Marshal(record)
	if err == nil {
		err = os.WriteFile(filepath.Join(dir, id+".json"), raw, 0600)
	}
	if err != nil {
		os.Remove(filepath.Join(dir, id+".bin"))
		c.JSON(500, gin.H{"error": "保存附件失败"})
		return
	}
	c.JSON(201, record.AssistantAttachment)
}

// transcriptAttachmentOwner reports who uploaded attachmentID according to the
// session transcript: the author of the message listing it, falling back to the
// session owner for messages without an author. The last listing wins.
func transcriptAttachmentOwner(session domain.AssistantSession, attachmentID string) (int64, bool) {
	var owner int64
	found := false
	for _, message := range session.Messages {
		for _, a := range message.Attachments {
			if a.ID == attachmentID {
				owner = message.AuthorID
				if owner == 0 {
					owner = session.UserID
				}
				found = true
			}
		}
	}
	return owner, found
}

func (h *assistantHandlers) downloadAttachment(c *gin.Context) {
	u, ok := h.requestUser(c.Request, true)
	if !ok {
		c.JSON(401, gin.H{"error": "请先登录"})
		return
	}
	ownerID := u.ID
	readOnly, sessionProject := false, ""
	if sessionID := c.Query("sessionId"); sessionID != "" {
		owner, shared, viewOnly, err := h.conversationReadScope(c, sessionID, u.ID)
		if err != nil {
			c.JSON(403, gin.H{"error": err.Error()})
			return
		}
		if shared {
			readOnly = viewOnly
			session, _ := h.repo.AssistantSession(owner, sessionID)
			sessionProject = session.ProjectID
			transcriptOwner, found := transcriptAttachmentOwner(session, c.Param("id"))
			if found {
				ownerID = transcriptOwner
			}
			// The uploader may fetch a file the transcript does not list yet: a
			// just-sent message renders its attachments while the generation request
			// that persists them is still in flight. Project scope stays enforced by
			// the permission checks below.
			if !found {
				record, _, loadErr := loadWorkbenchAttachment(u.ID, c.Param("id"))
				if loadErr != nil || (sessionProject != "" && record.ProjectID != "" && record.ProjectID != sessionProject) {
					c.JSON(404, gin.H{"error": "附件不在此共享会话中"})
					return
				}
				ownerID = u.ID
			}
		}
	}
	record, file, err := openWorkbenchAttachment(ownerID, c.Param("id"))
	if err != nil {
		c.JSON(404, gin.H{"error": "附件不存在"})
		return
	}
	defer file.Close()
	// A viewer may only fetch files that are part of the transcript itself.
	if readOnly && record.ProjectID != "" && record.ProjectID != sessionProject || !readOnly && !h.workbenchProjectAllowed(c, record.ProjectID, u.ID) {
		c.JSON(403, gin.H{"error": "无权访问该项目"})
		return
	}
	info, err := file.Stat()
	if err != nil || !info.Mode().IsRegular() {
		c.JSON(404, gin.H{"error": "附件不存在"})
		return
	}
	c.Header("Content-Type", record.MimeType)
	c.Header("X-Content-Type-Options", "nosniff")
	c.Header("Cache-Control", "private, no-store")
	c.Header("Content-Disposition", mime.FormatMediaType("attachment", map[string]string{"filename": record.Name}))
	http.ServeContent(c.Writer, c.Request, record.Name, info.ModTime(), file)
}

// taskReferenceAllowed extends taskWorkbenchReferenceAllowed to skills: a
// task discussion may only name skills bound to the task's roles.
func (h *assistantHandlers) taskReferenceAllowed(r domain.Run, kind, id string) bool {
	if kind != "skill" {
		return taskWorkbenchReferenceAllowed(r, kind, id)
	}
	for _, n := range r.WorkflowSnapshot.Nodes {
		if role, ok := h.repo.Agent(n.AgentID); ok && n.AgentID != "" && slices.Contains(role.Skills, id) {
			return true
		}
	}
	return false
}

// A task discussion cannot recruit roles or context from unrelated workflows.
func taskWorkbenchReferenceAllowed(r domain.Run, kind, id string) bool {
	switch kind {
	case "agent":
		for _, n := range r.WorkflowSnapshot.Nodes {
			if n.AgentID == id && id != "" {
				return true
			}
		}
	case "run":
		return id == r.ID
	case "sop":
		return id == r.WorkflowID
	case "issue":
		return id != "" && id == r.ExecutionContext.IssueID
	case "repository":
		for _, v := range r.RepositoryContexts() {
			if v.RepositoryID == id && id != "" {
				return true
			}
		}
	}
	return false
}

func runNodeAgentID(r domain.Run, id string) string {
	for _, n := range r.WorkflowSnapshot.Nodes {
		if n.ID == id {
			return n.AgentID
		}
	}
	return ""
}

// workspaceTurnContext tells Main Bot which workspace the conversation is
// bound to, which environments it used before, and what the requirements in
// play already delivered, so it resumes the right work instead of following @
// mentions mechanically.
func (h *assistantHandlers) workspaceTurnContext(req *assistantRequest, user domain.User) string {
	session, ok := h.repo.AssistantSession(assistantStorageOwner(h.repo, req.SessionID, user.ID), req.SessionID)
	if !ok || session.Shared || session.RunID != "" {
		return ""
	}
	var b strings.Builder
	current, bound := h.repo.RunState(session.WorkspaceRunID)
	bound = bound && conversationMayBind(session, current, req.ProjectID)
	if bound {
		fmt.Fprintf(&b, "本对话当前绑定的工作区:%s。", describeConversationWorkspace(current))
		if !current.IsWorkspace() {
			b.WriteString(borrowedSOPNotice(current))
		}
		b.WriteString("\n")
	}
	others := []string{}
	for _, id := range ownWorkspaceRunIDs(session) {
		if run, ok := h.repo.RunState(id); ok && id != session.WorkspaceRunID && run.IsWorkspace() && conversationMayBind(session, run, req.ProjectID) {
			others = append(others, "- "+describeConversationWorkspace(run))
		}
	}
	if len(others) > 0 {
		b.WriteString("本对话之前在其他运行环境开过的工作区(传它的 sandboxProfileId 或 workspaceRunId 切回,文件和已克隆的仓库都在):\n" + strings.Join(others, "\n") + "\n")
	}
	issues := []string{}
	for _, issue := range turnIssues(h.deps, req.ProjectID, session.IssueID, lastMessageReferences(req.Messages)) {
		line := fmt.Sprintf("- 需求 %s「%s」", issue.ID, truncateRunes(issue.Title, 60))
		if run, ok := issueSOPRun(h.deps, req.ProjectID, issue.ID); ok {
			line += fmt.Sprintf(":跑过 SOP,最近一次运行 %s(%s)", run.ID, run.Status)
			if branches := sopWorkBranches(run); branches != "" {
				line += ",工作分支 " + branches
			}
		} else {
			line += ":没跑过 SOP"
		}
		issues = append(issues, line)
	}
	if len(issues) > 0 {
		b.WriteString("本条消息 @ 的需求和本会话关联的主需求:\n" + strings.Join(issues, "\n") + "\n")
	}
	if b.Len() == 0 {
		return ""
	}
	b.WriteString("按用户这条消息实际要处理的事决定(@ 只是线索):只在要看或改代码、跑命令时才进入工作区;工作区按运行环境区分,当前环境合适就直接用,需要别的语言或工具链再传 sandboxProfileId 切换。创建文件和运行命令可直接在工作区完成。需要已有仓库时在工作区内自行 git clone 和切换分支。要直接恢复并接着某次 SOP 运行的现场继续干活(保留未推送修改和产物,或改完后回 SOP 重跑测试节点/继续执行),打开工作区时传入该 SOP 的 workspaceRunId(与当前对话在同一执行机器时直接复用原工作区,已回收时自动恢复快照;不在同一执行机器时如实告知用户需要在哪台执行机器下新建对话)。")
	return b.String()
}

// sopWorkBranches lists the work branches an SOP run pushed, as repo:branch.
func sopWorkBranches(run domain.Run) string {
	out := []string{}
	for _, rc := range run.RepositoryContexts() {
		if branch := strings.TrimSpace(rc.WorkBranch); branch != "" {
			out = append(out, rc.Name+":"+branch)
		}
	}
	return strings.Join(out, "、")
}

func describeConversationWorkspace(run domain.Run) string {
	if !run.IsWorkspace() {
		return fmt.Sprintf("需求 %s 的 SOP 运行 %s(%s)", run.ExecutionContext.IssueID, run.ID, run.Status)
	}
	state := "在线"
	if !run.WorkspaceCleanedAt.IsZero() {
		state = "已回收,切回时恢复快照"
	}
	extra := ""
	if repos := run.RepositoryContexts(); len(repos) > 0 {
		names := []string{}
		for _, rc := range repos {
			names = append(names, rc.Name)
		}
		extra = ",预检出仓库 " + strings.Join(names, "、")
	}
	return fmt.Sprintf("对话工作区 %s(运行环境 %s%s,%s)", run.ID, run.WorkflowSnapshot.SandboxProfileID, extra, state)
}

// linkMentionedIssue links an ordinary project conversation without a
// linked issue to an issue the current message mentions, preferring the one
// with the newest SOP run, so later turns that mention no issue still offer it.
// It returns the note for Main Bot, or "" when nothing changed.
func (h *assistantHandlers) linkMentionedIssue(req *assistantRequest, user domain.User) string {
	if len(req.Messages) == 0 {
		return ""
	}
	ownerID := assistantStorageOwner(h.repo, req.SessionID, user.ID)
	session, ok := h.repo.AssistantSession(ownerID, req.SessionID)
	if !ok || session.Shared || session.RunID != "" || session.IssueID != "" {
		return ""
	}
	issueID := ""
	var newest domain.Run
	for _, issue := range turnIssues(h.deps, req.ProjectID, "", lastMessageReferences(req.Messages)) {
		if issueID == "" {
			issueID = issue.ID
		}
		if run, ok := issueSOPRun(h.deps, req.ProjectID, issue.ID); ok && (newest.ID == "" || run.CreatedAt.After(newest.CreatedAt)) {
			newest, issueID = run, issue.ID
		}
	}
	if issueID == "" {
		return ""
	}
	linked, err := h.repo.LinkAssistantSessionIssue(ownerID, req.SessionID, issueID)
	if err != nil {
		log.Printf("link conversation %s to issue %s: %v", req.SessionID, issueID, err)
	}
	if !linked {
		return ""
	}
	req.linkedIssueID = issueID
	return "需求 " + issueID + " 已设为本会话的关联主需求。"
}