Skip to content

Commit 8c45768

Browse files
committed
update go sdk by ai
1 parent f0ebb97 commit 8c45768

File tree

6 files changed

+334
-20
lines changed

6 files changed

+334
-20
lines changed

src/client/memobase-go/core/client_test.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package core
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/google/uuid"
@@ -12,18 +13,29 @@ const (
1213
TestAPIKey = "secret"
1314
)
1415

16+
func skipIfNoServer(t *testing.T) {
17+
if os.Getenv("MEMOBASE_TEST_SERVER") != "true" {
18+
t.Skip("Skipping test that requires live server - set MEMOBASE_TEST_SERVER=true to run")
19+
}
20+
}
21+
1522
func TestNewMemoBaseClient(t *testing.T) {
1623
client, err := NewMemoBaseClient(TestProjectURL, TestAPIKey)
1724
assert.NoError(t, err)
1825
assert.NotNil(t, client)
26+
assert.Equal(t, TestProjectURL, client.ProjectURL)
27+
assert.Equal(t, TestAPIKey, client.APIKey)
28+
assert.Equal(t, "api/v1", client.APIVersion)
1929
}
2030

2131
func TestPing(t *testing.T) {
32+
skipIfNoServer(t)
2233
client, _ := NewMemoBaseClient(TestProjectURL, TestAPIKey)
2334
assert.True(t, client.Ping())
2435
}
2536

2637
func TestConfig(t *testing.T) {
38+
skipIfNoServer(t)
2739
client, _ := NewMemoBaseClient(TestProjectURL, TestAPIKey)
2840
config := "language: en"
2941
err := client.UpdateConfig(config)
@@ -35,6 +47,7 @@ func TestConfig(t *testing.T) {
3547
}
3648

3749
func TestUserCRUD(t *testing.T) {
50+
skipIfNoServer(t)
3851
client, _ := NewMemoBaseClient(TestProjectURL, TestAPIKey)
3952

4053
// Add User
@@ -67,6 +80,7 @@ func TestUserCRUD(t *testing.T) {
6780
}
6881

6982
func TestGetOrCreateUser(t *testing.T) {
83+
skipIfNoServer(t)
7084
client, _ := NewMemoBaseClient(TestProjectURL, TestAPIKey)
7185
userID := uuid.New().String()
7286

@@ -86,8 +100,9 @@ func TestGetOrCreateUser(t *testing.T) {
86100
}
87101

88102
func TestGetAllUsers(t *testing.T) {
103+
skipIfNoServer(t)
89104
client, _ := NewMemoBaseClient(TestProjectURL, TestAPIKey)
90-
userID, err := client.AddUser(map[string]interface{}{}, "")
105+
userID, err := client.AddUser(map[string]interface{}{}, "")
91106

92107
users, err := client.GetAllUsers("", "updated_at", true, 10, 0)
93108
assert.NoError(t, err)
@@ -97,13 +112,15 @@ func TestGetAllUsers(t *testing.T) {
97112
}
98113

99114
func TestGetUsage(t *testing.T) {
115+
skipIfNoServer(t)
100116
client, _ := NewMemoBaseClient(TestProjectURL, TestAPIKey)
101117
usage, err := client.GetUsage()
102118
assert.NoError(t, err)
103119
assert.NotNil(t, usage)
104120
}
105121

106122
func TestGetDailyUsage(t *testing.T) {
123+
skipIfNoServer(t)
107124
client, _ := NewMemoBaseClient(TestProjectURL, TestAPIKey)
108125
usage, err := client.GetDailyUsage(7)
109126
assert.NoError(t, err)

src/client/memobase-go/core/types.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,32 @@ type UserEventData struct {
4040
UpdatedAt time.Time `json:"updated_at"`
4141
Similarity float64 `json:"similarity,omitempty"`
4242
}
43+
44+
// UserGistEventData represents a gist event with minimal data
45+
type UserGistEventData struct {
46+
ID uuid.UUID `json:"id"`
47+
GistData struct {
48+
Content string `json:"content"`
49+
} `json:"gist_data"`
50+
CreatedAt time.Time `json:"created_at"`
51+
UpdatedAt time.Time `json:"updated_at"`
52+
Similarity float64 `json:"similarity,omitempty"`
53+
}
54+
55+
// BufferStatus represents the status of buffer operations
56+
type BufferStatus string
57+
58+
const (
59+
BufferStatusPending BufferStatus = "pending"
60+
BufferStatusProcessing BufferStatus = "processing"
61+
BufferStatusCompleted BufferStatus = "completed"
62+
BufferStatusFailed BufferStatus = "failed"
63+
)
64+
65+
// BufferCapacity represents buffer capacity information
66+
type BufferCapacity struct {
67+
IDs []string `json:"ids"`
68+
Status BufferStatus `json:"status"`
69+
Count int `json:"count"`
70+
Capacity int `json:"capacity"`
71+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package core
2+
3+
import (
4+
"encoding/json"
5+
"testing"
6+
"time"
7+
8+
"github.com/google/uuid"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestUserGistEventData(t *testing.T) {
13+
// Test UserGistEventData structure
14+
eventID := uuid.New()
15+
now := time.Now()
16+
17+
event := UserGistEventData{
18+
ID: eventID,
19+
GistData: struct {
20+
Content string `json:"content"`
21+
}{
22+
Content: "test gist content",
23+
},
24+
CreatedAt: now,
25+
UpdatedAt: now,
26+
Similarity: 0.95,
27+
}
28+
29+
// Test JSON marshaling
30+
jsonData, err := json.Marshal(event)
31+
assert.NoError(t, err)
32+
assert.Contains(t, string(jsonData), "test gist content")
33+
assert.Contains(t, string(jsonData), eventID.String())
34+
35+
// Test JSON unmarshaling
36+
var unmarshaledEvent UserGistEventData
37+
err = json.Unmarshal(jsonData, &unmarshaledEvent)
38+
assert.NoError(t, err)
39+
assert.Equal(t, event.ID, unmarshaledEvent.ID)
40+
assert.Equal(t, event.GistData.Content, unmarshaledEvent.GistData.Content)
41+
assert.Equal(t, event.Similarity, unmarshaledEvent.Similarity)
42+
}
43+
44+
func TestBufferStatus(t *testing.T) {
45+
// Test BufferStatus constants
46+
assert.Equal(t, BufferStatus("pending"), BufferStatusPending)
47+
assert.Equal(t, BufferStatus("processing"), BufferStatusProcessing)
48+
assert.Equal(t, BufferStatus("completed"), BufferStatusCompleted)
49+
assert.Equal(t, BufferStatus("failed"), BufferStatusFailed)
50+
}
51+
52+
func TestBufferCapacity(t *testing.T) {
53+
// Test BufferCapacity structure
54+
capacity := BufferCapacity{
55+
IDs: []string{"id1", "id2", "id3"},
56+
Status: BufferStatusCompleted,
57+
Count: 3,
58+
Capacity: 10,
59+
}
60+
61+
// Test JSON marshaling
62+
jsonData, err := json.Marshal(capacity)
63+
assert.NoError(t, err)
64+
assert.Contains(t, string(jsonData), "completed")
65+
assert.Contains(t, string(jsonData), "id1")
66+
67+
// Test JSON unmarshaling
68+
var unmarshaledCapacity BufferCapacity
69+
err = json.Unmarshal(jsonData, &unmarshaledCapacity)
70+
assert.NoError(t, err)
71+
assert.Equal(t, capacity.IDs, unmarshaledCapacity.IDs)
72+
assert.Equal(t, capacity.Status, unmarshaledCapacity.Status)
73+
assert.Equal(t, capacity.Count, unmarshaledCapacity.Count)
74+
assert.Equal(t, capacity.Capacity, unmarshaledCapacity.Capacity)
75+
}
76+
77+
func TestUserEventData(t *testing.T) {
78+
// Test existing UserEventData structure
79+
eventID := uuid.New()
80+
now := time.Now()
81+
82+
event := UserEventData{
83+
ID: eventID,
84+
EventData: EventData{
85+
ProfileDelta: []ProfileDelta{
86+
{
87+
Content: "test content",
88+
Attributes: map[string]interface{}{
89+
"topic": "test",
90+
},
91+
},
92+
},
93+
EventTip: "test tip",
94+
EventTags: []EventTag{
95+
{Tag: "type", Value: "test"},
96+
},
97+
},
98+
CreatedAt: now,
99+
UpdatedAt: now,
100+
Similarity: 0.85,
101+
}
102+
103+
// Test JSON marshaling
104+
jsonData, err := json.Marshal(event)
105+
assert.NoError(t, err)
106+
assert.Contains(t, string(jsonData), "test content")
107+
assert.Contains(t, string(jsonData), "test tip")
108+
109+
// Test JSON unmarshaling
110+
var unmarshaledEvent UserEventData
111+
err = json.Unmarshal(jsonData, &unmarshaledEvent)
112+
assert.NoError(t, err)
113+
assert.Equal(t, event.ID, unmarshaledEvent.ID)
114+
assert.Equal(t, event.EventData.EventTip, unmarshaledEvent.EventData.EventTip)
115+
assert.Equal(t, len(event.EventData.ProfileDelta), len(unmarshaledEvent.EventData.ProfileDelta))
116+
assert.Equal(t, event.Similarity, unmarshaledEvent.Similarity)
117+
}

src/client/memobase-go/core/user.go

Lines changed: 68 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ func (u *User) Buffer(blobType blob.BlobType, status string) ([]string, error) {
228228
}
229229

230230
type ProfileOptions struct {
231-
MaxTokenSize int `json:"max_token_size,omitempty"`
232-
PreferTopics []string `json:"prefer_topics,omitempty"`
233-
OnlyTopics []string `json:"only_topics,omitempty"`
234-
MaxSubtopicSize *int `json:"max_subtopic_size,omitempty"`
235-
TopicLimits map[string]int `json:"topic_limits,omitempty"`
231+
MaxTokenSize int `json:"max_token_size,omitempty"`
232+
PreferTopics []string `json:"prefer_topics,omitempty"`
233+
OnlyTopics []string `json:"only_topics,omitempty"`
234+
MaxSubtopicSize *int `json:"max_subtopic_size,omitempty"`
235+
TopicLimits map[string]int `json:"topic_limits,omitempty"`
236236
Chats []blob.OpenAICompatibleMessage `json:"chats,omitempty"`
237237
}
238238

@@ -536,16 +536,70 @@ func (u *User) SearchEvent(query string, topk int, similarityThreshold float64,
536536
return result, nil
537537
}
538538

539+
// SearchEventGist searches for event gists with similarity threshold
540+
func (u *User) SearchEventGist(query string, topk int, similarityThreshold float64, timeRangeInDays int) ([]UserGistEventData, error) {
541+
params := url.Values{}
542+
params.Add("query", query)
543+
params.Add("topk", fmt.Sprintf("%d", topk))
544+
params.Add("similarity_threshold", fmt.Sprintf("%f", similarityThreshold))
545+
params.Add("time_range_in_days", fmt.Sprintf("%d", timeRangeInDays))
546+
547+
resp, err := u.ProjectClient.HTTPClient.Get(
548+
fmt.Sprintf("%s/users/event_gist/search/%s?%s", u.ProjectClient.BaseURL, u.UserID, params.Encode()),
549+
)
550+
if err != nil {
551+
return nil, err
552+
}
553+
defer resp.Body.Close()
554+
555+
baseResp, err := network.UnpackResponse(resp)
556+
if err != nil {
557+
return nil, err
558+
}
559+
560+
dataMap, ok := baseResp.Data.(map[string]interface{})
561+
if !ok {
562+
return nil, fmt.Errorf("unexpected response format for SearchEventGist")
563+
}
564+
565+
events, ok := dataMap["events"].([]interface{})
566+
if !ok {
567+
return nil, fmt.Errorf("unexpected response format for events")
568+
}
569+
570+
var result []UserGistEventData
571+
for _, e := range events {
572+
eventMap, ok := e.(map[string]interface{})
573+
if !ok {
574+
continue
575+
}
576+
577+
var event UserGistEventData
578+
jsonData, err := json.Marshal(eventMap)
579+
if err != nil {
580+
continue
581+
}
582+
if err := json.Unmarshal(jsonData, &event); err != nil {
583+
fmt.Printf("Error unmarshaling event gist: %v\nData: %s\n", err, jsonData)
584+
continue
585+
}
586+
587+
result = append(result, event)
588+
}
589+
590+
return result, nil
591+
}
592+
539593
type ContextOptions struct {
540-
MaxTokenSize int `json:"max_token_size,omitempty"`
541-
PreferTopics []string `json:"prefer_topics,omitempty"`
542-
OnlyTopics []string `json:"only_topics,omitempty"`
543-
MaxSubtopicSize *int `json:"max_subtopic_size,omitempty"`
544-
TopicLimits map[string]int `json:"topic_limits,omitempty"`
545-
ProfileEventRatio *float64 `json:"profile_event_ratio,omitempty"`
546-
RequireEventSummary *bool `json:"require_event_summary,omitempty"`
547-
Chats []blob.OpenAICompatibleMessage `json:"chats,omitempty"`
548-
EventSimilarityThreshold *float64 `json:"event_similarity_threshold,omitempty"`
594+
MaxTokenSize int `json:"max_token_size,omitempty"`
595+
PreferTopics []string `json:"prefer_topics,omitempty"`
596+
OnlyTopics []string `json:"only_topics,omitempty"`
597+
MaxSubtopicSize *int `json:"max_subtopic_size,omitempty"`
598+
TopicLimits map[string]int `json:"topic_limits,omitempty"`
599+
ProfileEventRatio *float64 `json:"profile_event_ratio,omitempty"`
600+
RequireEventSummary *bool `json:"require_event_summary,omitempty"`
601+
Chats []blob.OpenAICompatibleMessage `json:"chats,omitempty"`
602+
EventSimilarityThreshold *float64 `json:"event_similarity_threshold,omitempty"`
549603
}
550604

551605
func (u *User) Context(options *ContextOptions) (string, error) {

0 commit comments

Comments
 (0)