forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_message_transform_e2e_test.go
More file actions
189 lines (165 loc) · 5 KB
/
Copy pathsystem_message_transform_e2e_test.go
File metadata and controls
189 lines (165 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
package e2e
import (
"os"
"path/filepath"
"strings"
"sync"
"testing"
copilot "github.com/github/copilot-sdk/go"
"github.com/github/copilot-sdk/go/internal/e2e/testharness"
)
func TestSystemMessageTransformE2E(t *testing.T) {
ctx := testharness.NewTestContext(t)
client := ctx.NewClient()
t.Cleanup(func() { client.ForceStop() })
t.Run("should_invoke_transform_callbacks_with_section_content", func(t *testing.T) {
ctx.ConfigureForTest(t)
var identityContent string
var toneContent string
var mu sync.Mutex
identityCalled := false
toneCalled := false
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
SystemMessage: &copilot.SystemMessageConfig{
Mode: "customize",
Sections: map[string]copilot.SectionOverride{
"identity": {
Transform: func(currentContent string) (string, error) {
mu.Lock()
identityCalled = true
identityContent = currentContent
mu.Unlock()
return currentContent, nil
},
},
"tone": {
Transform: func(currentContent string) (string, error) {
mu.Lock()
toneCalled = true
toneContent = currentContent
mu.Unlock()
return currentContent, nil
},
},
},
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
testFile := filepath.Join(ctx.WorkDir, "test.txt")
err = os.WriteFile(testFile, []byte("Hello transform!"), 0644)
if err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
_, err = session.SendAndWait(t.Context(), copilot.MessageOptions{
Prompt: "Read the contents of test.txt and tell me what it says",
})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
mu.Lock()
defer mu.Unlock()
if !identityCalled {
t.Error("Expected identity transform callback to be invoked")
}
if !toneCalled {
t.Error("Expected tone transform callback to be invoked")
}
if identityContent == "" {
t.Error("Expected identity transform to receive non-empty content")
}
if toneContent == "" {
t.Error("Expected tone transform to receive non-empty content")
}
})
t.Run("should_apply_transform_modifications_to_section_content", func(t *testing.T) {
ctx.ConfigureForTest(t)
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
SystemMessage: &copilot.SystemMessageConfig{
Mode: "customize",
Sections: map[string]copilot.SectionOverride{
"identity": {
Transform: func(currentContent string) (string, error) {
return currentContent + "\nAlways end your reply with TRANSFORM_MARKER", nil
},
},
},
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
testFile := filepath.Join(ctx.WorkDir, "hello.txt")
err = os.WriteFile(testFile, []byte("Hello!"), 0644)
if err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
assistantMessage, err := session.SendAndWait(t.Context(), copilot.MessageOptions{
Prompt: "Read the contents of hello.txt",
})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
// Verify the transform result was actually applied to the system message
traffic, err := ctx.GetExchanges()
if err != nil {
t.Fatalf("Failed to get exchanges: %v", err)
}
if len(traffic) == 0 {
t.Fatal("Expected at least one exchange")
}
systemMessage := getSystemMessage(traffic[0])
if !strings.Contains(systemMessage, "TRANSFORM_MARKER") {
t.Errorf("Expected system message to contain TRANSFORM_MARKER, got %q", systemMessage)
}
_ = assistantMessage
})
t.Run("should_work_with_static_overrides_and_transforms_together", func(t *testing.T) {
ctx.ConfigureForTest(t)
var mu sync.Mutex
transformCalled := false
session, err := client.CreateSession(t.Context(), &copilot.SessionConfig{
OnPermissionRequest: copilot.PermissionHandler.ApproveAll,
SystemMessage: &copilot.SystemMessageConfig{
Mode: "customize",
Sections: map[string]copilot.SectionOverride{
"safety": {
Action: copilot.SectionActionRemove,
},
"identity": {
Transform: func(currentContent string) (string, error) {
mu.Lock()
transformCalled = true
mu.Unlock()
return currentContent, nil
},
},
},
},
})
if err != nil {
t.Fatalf("Failed to create session: %v", err)
}
testFile := filepath.Join(ctx.WorkDir, "combo.txt")
err = os.WriteFile(testFile, []byte("Combo test!"), 0644)
if err != nil {
t.Fatalf("Failed to write test file: %v", err)
}
_, err = session.SendAndWait(t.Context(), copilot.MessageOptions{
Prompt: "Read the contents of combo.txt and tell me what it says",
})
if err != nil {
t.Fatalf("Failed to send message: %v", err)
}
mu.Lock()
defer mu.Unlock()
if !transformCalled {
t.Error("Expected identity transform callback to be invoked")
}
})
}