feat: better context files (#222)

feat: context files
This commit is contained in:
Carlos Florêncio
2026-02-14 20:13:11 +00:00
committed by GitHub
parent ee612a42c5
commit f4d1dfd3a7
49 changed files with 3809 additions and 801 deletions
@@ -8,6 +8,7 @@ type UserMessageMeta struct {
PlanMode bool
HasReviewComments bool
Attachments []v1.MessageAttachment
ContextFiles []v1.ContextFileMeta
}
// NewUserMessageMeta creates a UserMessageMeta builder.
@@ -33,10 +34,16 @@ func (m *UserMessageMeta) WithAttachments(attachments []v1.MessageAttachment) *U
return m
}
// WithContextFiles sets the context files attached to the message.
func (m *UserMessageMeta) WithContextFiles(files []v1.ContextFileMeta) *UserMessageMeta {
m.ContextFiles = files
return m
}
// ToMap returns the metadata as a map suitable for message creation.
// Returns nil if no metadata fields are set.
func (m *UserMessageMeta) ToMap() map[string]interface{} {
if !m.PlanMode && !m.HasReviewComments && len(m.Attachments) == 0 {
if !m.PlanMode && !m.HasReviewComments && len(m.Attachments) == 0 && len(m.ContextFiles) == 0 {
return nil
}
meta := make(map[string]interface{})
@@ -49,5 +56,8 @@ func (m *UserMessageMeta) ToMap() map[string]interface{} {
if len(m.Attachments) > 0 {
meta["attachments"] = m.Attachments
}
if len(m.ContextFiles) > 0 {
meta["context_files"] = m.ContextFiles
}
return meta
}
@@ -61,18 +61,39 @@ func TestUserMessageMeta_ToMap_AttachmentsOnly(t *testing.T) {
}
}
func TestUserMessageMeta_ToMap_AllFields(t *testing.T) {
attachments := []v1.MessageAttachment{{Type: "image", Data: "data", MimeType: "image/jpeg"}}
meta := NewUserMessageMeta().
WithPlanMode(true).
WithReviewComments(true).
WithAttachments(attachments)
func TestUserMessageMeta_ToMap_ContextFilesOnly(t *testing.T) {
files := []v1.ContextFileMeta{{Path: "src/main.go", Name: "main.go"}}
meta := NewUserMessageMeta().WithContextFiles(files)
result := meta.ToMap()
if result == nil {
t.Fatal("expected non-nil map")
}
if len(result) != 3 {
t.Errorf("expected 3 keys, got %d", len(result))
cf, ok := result["context_files"]
if !ok {
t.Fatal("expected context_files key")
}
if len(cf.([]v1.ContextFileMeta)) != 1 {
t.Errorf("expected 1 context file, got %d", len(cf.([]v1.ContextFileMeta)))
}
if _, ok := result["plan_mode"]; ok {
t.Error("unexpected plan_mode key")
}
}
func TestUserMessageMeta_ToMap_AllFields(t *testing.T) {
attachments := []v1.MessageAttachment{{Type: "image", Data: "data", MimeType: "image/jpeg"}}
contextFiles := []v1.ContextFileMeta{{Path: "README.md", Name: "README.md"}}
meta := NewUserMessageMeta().
WithPlanMode(true).
WithReviewComments(true).
WithAttachments(attachments).
WithContextFiles(contextFiles)
result := meta.ToMap()
if result == nil {
t.Fatal("expected non-nil map")
}
if len(result) != 4 {
t.Errorf("expected 4 keys, got %d", len(result))
}
if result["plan_mode"] != true {
t.Error("expected plan_mode=true")
@@ -80,6 +101,12 @@ func TestUserMessageMeta_ToMap_AllFields(t *testing.T) {
if result["has_review_comments"] != true {
t.Error("expected has_review_comments=true")
}
if _, ok := result["attachments"]; !ok {
t.Error("expected attachments key")
}
if _, ok := result["context_files"]; !ok {
t.Error("expected context_files key")
}
}
func TestUserMessageMeta_ToMap_FalseValues(t *testing.T) {
@@ -106,4 +133,8 @@ func TestUserMessageMeta_Chaining(t *testing.T) {
if returned != meta {
t.Error("WithAttachments should return the same pointer for chaining")
}
returned = meta.WithContextFiles(nil)
if returned != meta {
t.Error("WithContextFiles should return the same pointer for chaining")
}
}
@@ -191,7 +191,7 @@ func (s *Service) StartCreatedSession(ctx context.Context, taskID, sessionID, ag
}
if execution.SessionID != "" {
s.recordInitialMessage(ctx, taskID, execution.SessionID, effectivePrompt, false)
s.updateTaskSessionState(ctx, taskID, execution.SessionID, models.TaskSessionStateRunning, "", true)
}
return execution, nil
@@ -125,6 +125,7 @@ type wsAddMessageRequest struct {
PlanMode bool `json:"plan_mode,omitempty"`
HasReviewComments bool `json:"has_review_comments,omitempty"`
Attachments []v1.MessageAttachment `json:"attachments,omitempty"`
ContextFiles []v1.ContextFileMeta `json:"context_files,omitempty"`
}
func (h *MessageHandlers) wsAddMessage(ctx context.Context, msg *ws.Message) (*ws.Message, error) {
@@ -185,11 +186,12 @@ func (h *MessageHandlers) wsAddMessage(ctx context.Context, msg *ws.Message) (*w
}
}
// Build metadata with attachments, plan mode, and review comments
// Build metadata with attachments, plan mode, review comments, and context files
meta := orchestrator.NewUserMessageMeta().
WithPlanMode(req.PlanMode).
WithReviewComments(req.HasReviewComments).
WithAttachments(req.Attachments)
WithAttachments(req.Attachments).
WithContextFiles(req.ContextFiles)
metadata := meta.ToMap()
message, err := h.messageController.CreateMessage(ctx, dto.CreateMessageRequest{
+6
View File
@@ -126,6 +126,12 @@ type MessageAttachment struct {
MimeType string `json:"mime_type"` // MIME type (e.g., "image/png")
}
// ContextFileMeta represents a context file reference attached to a message
type ContextFileMeta struct {
Path string `json:"path"`
Name string `json:"name"`
}
// Message represents a message in a task session (user or agent)
type Message struct {
ID string `json:"id"`