-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathsession_codec.go
More file actions
83 lines (72 loc) · 1.8 KB
/
Copy pathsession_codec.go
File metadata and controls
83 lines (72 loc) · 1.8 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
package codebuddy
import (
"crypto/sha256"
"fmt"
"strings"
"github.com/agent-dance/agent-adaptor/driver"
)
type sessionCodec struct{}
func (adapter) SessionCodec() driver.SessionCodec {
return sessionCodec{}
}
func (sessionCodec) Name() string { return DriverType }
func (sessionCodec) ToParams(state *driver.SessionState) driver.SessionParams {
if state == nil {
return driver.SessionParams{}
}
return driver.SessionParams{
ResumeID: state.ResumeID,
DisplayID: displayID(state),
Values: cloneData(state.Data),
}
}
func (sessionCodec) FromParams(params driver.SessionParams) *driver.SessionState {
if params.ResumeID == "" && params.DisplayID == "" && len(params.Values) == 0 {
return nil
}
display := params.DisplayID
if display == "" {
display = params.ResumeID
}
return &driver.SessionState{
ResumeID: params.ResumeID,
DisplayID: display,
Data: cloneData(params.Values),
}
}
func (sessionCodec) GuardFingerprint(params driver.SessionParams) string {
return guardHash(params.Values,
driver.SessionParamCWD,
driver.SessionParamWorkspaceID,
driver.SessionParamProfileFingerprint,
)
}
func displayID(state *driver.SessionState) string {
if state.DisplayID != "" {
return state.DisplayID
}
return state.ResumeID
}
func cloneData(values map[string]string) map[string]string {
if len(values) == 0 {
return nil
}
out := make(map[string]string, len(values))
for key, value := range values {
out[key] = value
}
return out
}
func guardHash(values map[string]string, keys ...string) string {
builder := strings.Builder{}
for index, key := range keys {
if index > 0 {
builder.WriteByte(0)
}
builder.WriteString(key)
builder.WriteByte('=')
builder.WriteString(values[key])
}
sum := sha256.Sum256([]byte(builder.String()))
return fmt.Sprintf("%x", sum[:])
}