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.

【架构优化】从 downloadAttachment 中拆出转录附件上传者查找 transcriptAttachmentOwner (#816)

fx-kurumifef89e15202c
diff --git a/internal/httpapi/assistant_transcript_attachment_owner_test.go b/internal/httpapi/assistant_transcript_attachment_owner_test.go
new file mode 100644
index 00000000..275e4c74
--- /dev/null
+++ b/internal/httpapi/assistant_transcript_attachment_owner_test.go
@@ -0,0 +1,49 @@
+package httpapi
+
+import (
+ "testing"
+
+ "agentflow/internal/domain"
+)
+
+func TestTranscriptAttachmentOwner(t *testing.T) {
+ attachments := func(ids ...string) []domain.AssistantAttachment {
+ out := make([]domain.AssistantAttachment, 0, len(ids))
+ for _, id := range ids {
+ out = append(out, domain.AssistantAttachment{ID: id})
+ }
+ return out
+ }
+ session := domain.AssistantSession{
+ UserID: 1,
+ Messages: []domain.AssistantMessage{
+ {Role: "user", AuthorID: 7, Attachments: attachments("a", "b")},
+ {Role: "user", Attachments: attachments("c")},
+ {Role: "assistant", Content: "no files"},
+ {Role: "user", AuthorID: 9, Attachments: attachments("b")},
+ },
+ }
+ cases := []struct {
+ name string
+ id string
+ owner int64
+ found bool
+ }{
+ {name: "message author", id: "a", owner: 7, found: true},
+ {name: "authorless message falls back to session owner", id: "c", owner: 1, found: true},
+ {name: "last listing wins", id: "b", owner: 9, found: true},
+ {name: "not in transcript", id: "missing", owner: 0, found: false},
+ {name: "empty id", id: "", owner: 0, found: false},
+ }
+ for _, tc := range cases {
+ t.Run(tc.name, func(t *testing.T) {
+ owner, found := transcriptAttachmentOwner(session, tc.id)
+ if owner != tc.owner || found != tc.found {
+ t.Fatalf("transcriptAttachmentOwner(%q) = (%d, %v), want (%d, %v)", tc.id, owner, found, tc.owner, tc.found)
+ }
+ })
+ }
+ if owner, found := transcriptAttachmentOwner(domain.AssistantSession{UserID: 3}, "a"); owner != 0 || found {
+ t.Fatalf("empty transcript = (%d, %v), want (0, false)", owner, found)
+ }
+}
diff --git a/internal/httpapi/assistant_workbench.go b/internal/httpapi/assistant_workbench.go
index dbb3a52b..4c44514a 100644
--- a/internal/httpapi/assistant_workbench.go
+++ b/internal/httpapi/assistant_workbench.go
@@ -1099,6 +1099,27 @@ func (h *assistantHandlers) uploadAttachment(c *gin.Context) {
}
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 {
@@ -1117,17 +1138,9 @@ func (h *assistantHandlers) downloadAttachment(c *gin.Context) {
readOnly = viewOnly
session, _ := h.repo.AssistantSession(owner, sessionID)
sessionProject = session.ProjectID
- found := false
- for _, message := range session.Messages {
- for _, a := range message.Attachments {
- if a.ID == c.Param("id") {
- ownerID = message.AuthorID
- if ownerID == 0 {
- ownerID = session.UserID
- }
- found = true
- }
- }
+ 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