Skip to content

Commit 987cab5

Browse files
authored
Merge pull request #1329 from wakatime/bugfix/copilot
Fix copilot parser for string variable values
2 parents 1c29303 + 9853e0c commit 987cab5

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

pkg/ai/copilot.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,33 @@ type (
149149
}
150150
)
151151

152+
func (v *copilotVariable) UnmarshalJSON(data []byte) error {
153+
type alias struct {
154+
Kind string `json:"kind"`
155+
ID string `json:"id"`
156+
Value json.RawMessage `json:"value"`
157+
}
158+
159+
var decoded alias
160+
if err := json.Unmarshal(data, &decoded); err != nil {
161+
return err
162+
}
163+
164+
v.Kind = decoded.Kind
165+
v.ID = decoded.ID
166+
167+
if len(decoded.Value) == 0 || string(decoded.Value) == "null" {
168+
return nil
169+
}
170+
171+
var pathValue copilotPathValue
172+
if err := json.Unmarshal(decoded.Value, &pathValue); err == nil {
173+
v.Value = &pathValue
174+
}
175+
176+
return nil
177+
}
178+
152179
func (m *copilotMessageWithURIs) UnmarshalJSON(data []byte) error {
153180
if data == nil || string(data) == "null" {
154181
return nil

pkg/ai/copilot_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,40 @@ func TestCopilotParseJSONLSession(t *testing.T) {
221221
assert.Equal(t, "Copilot session-2", got[2].Entity)
222222
assert.Equal(t, heartbeat.AppType, got[2].EntityType)
223223
}
224+
225+
func TestCopilotParseJSONLSession_IgnoresStringVariableValues(t *testing.T) {
226+
home := t.TempDir()
227+
t.Setenv("HOME", home)
228+
t.Setenv("USERPROFILE", home)
229+
230+
workspaceDir := filepath.Join(home, "Library", "Application Support", "Code", "User", "workspaceStorage", "workspace-3")
231+
require.NoError(t, os.MkdirAll(filepath.Join(workspaceDir, "chatSessions"), 0o755))
232+
233+
sessionPath := filepath.Join(workspaceDir, "chatSessions", "session-3.jsonl")
234+
lines := []string{
235+
strings.Join([]string{
236+
`{"kind":0,"v":{"version":3,"creationDate":1771000000000,`,
237+
`"sessionId":"session-3","requests":[{"requestId":"request-b",`,
238+
`"timestamp":1771000005000,`,
239+
`"agent":{"extensionVersion":"0.43.0"},`,
240+
`"message":{"text":"Find bugs in this repo"},`,
241+
`"variableData":{"variables":[{"id":"vscode.customizations.index",`,
242+
`"kind":"promptText","value":"<skills>...</skills>"}]},`,
243+
`"response":[]}]}}`,
244+
}, ""),
245+
`{"kind":1,"k":["requests",0,"modelState"],"v":{"value":1,"completedAt":1771000009000}}`,
246+
}
247+
require.NoError(t, os.WriteFile(sessionPath, []byte(strings.Join(lines, "\n")+"\n"), 0o644))
248+
249+
got, err := ai.Copilot{
250+
After: time.Unix(1771000000, 0),
251+
FallbackUserAgent: "editor/1.0.0",
252+
}.Parse(t.Context())
253+
require.NoError(t, err)
254+
require.Len(t, got, 2)
255+
256+
assert.Equal(t, "Copilot session-3", got[0].Entity)
257+
assert.Equal(t, heartbeat.AppType, got[0].EntityType)
258+
assert.Equal(t, "Copilot session-3", got[1].Entity)
259+
assert.Equal(t, heartbeat.AppType, got[1].EntityType)
260+
}

0 commit comments

Comments
 (0)