forked from trpc-group/trpc-agent-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.go
More file actions
599 lines (537 loc) · 20.5 KB
/
Copy pathutil.go
File metadata and controls
599 lines (537 loc) · 20.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//
// Tencent is pleased to support the open source community by making trpc-agent-go available.
//
// Copyright (C) 2025 Tencent. All rights reserved.
//
// trpc-agent-go is licensed under the Apache License Version 2.0.
//
//
// Package util provides utility functions for memory examples.
package util
import (
"database/sql"
"fmt"
"os"
"strconv"
"strings"
"time"
"trpc.group/trpc-go/trpc-agent-go/agent/llmagent"
openaiembedder "trpc.group/trpc-go/trpc-agent-go/knowledge/embedder/openai"
"trpc.group/trpc-go/trpc-agent-go/memory"
"trpc.group/trpc-go/trpc-agent-go/memory/extractor"
memoryinmemory "trpc.group/trpc-go/trpc-agent-go/memory/inmemory"
memorymysql "trpc.group/trpc-go/trpc-agent-go/memory/mysql"
memorymysqlvec "trpc.group/trpc-go/trpc-agent-go/memory/mysqlvec"
memorypgvector "trpc.group/trpc-go/trpc-agent-go/memory/pgvector"
memorypostgres "trpc.group/trpc-go/trpc-agent-go/memory/postgres"
memoryredis "trpc.group/trpc-go/trpc-agent-go/memory/redis"
memorysqlite "trpc.group/trpc-go/trpc-agent-go/memory/sqlite"
"trpc.group/trpc-go/trpc-agent-go/model"
"trpc.group/trpc-go/trpc-agent-go/model/openai"
"trpc.group/trpc-go/trpc-agent-go/runner"
sessioninmemory "trpc.group/trpc-go/trpc-agent-go/session/inmemory"
)
// MemoryType defines the type of memory service.
type MemoryType string
// Memory type constants.
const (
MemoryInMemory MemoryType = "inmemory"
MemorySQLite MemoryType = "sqlite"
MemorySQLiteVec MemoryType = "sqlitevec"
MemoryRedis MemoryType = "redis"
MemoryPostgres MemoryType = "postgres"
MemoryPGVector MemoryType = "pgvector"
MemoryMySQL MemoryType = "mysql"
MemoryMySQLVec MemoryType = "mysqlvec"
)
// MemoryServiceConfig holds configuration for creating a memory service.
type MemoryServiceConfig struct {
// Soft delete configuration.
SoftDelete bool
// Extractor configuration for auto memory mode.
Extractor extractor.MemoryExtractor
// Async memory worker configuration.
AsyncMemoryNum int
MemoryQueueSize int
MemoryJobTimeout time.Duration
}
// RunnerConfig holds configuration for creating a runner.
type RunnerConfig struct {
AppName string
AgentName string
ModelName string
Instruction string
MaxTokens int
Temperature float64
Streaming bool
}
// DefaultRunnerConfig returns a default runner configuration.
func DefaultRunnerConfig() RunnerConfig {
return RunnerConfig{
AppName: "memory-chat",
AgentName: "memory-assistant",
ModelName: GetEnvOrDefault("MODEL_NAME", "deepseek-v4-flash"),
Instruction: "You are a helpful AI assistant with memory capabilities.",
MaxTokens: 2000,
Temperature: 0.7,
Streaming: true,
}
}
// NewMemoryServiceByType creates a memory service based on the specified type.
//
// This function supports both manual memory mode and auto memory mode:
// - Manual mode: cfg.Extractor == nil, uses explicit memory tool calls
// - Auto mode: cfg.Extractor != nil, automatically extracts memories from conversations
//
// Parameters:
// - memoryType: one of inmemory, sqlite, sqlitevec, redis, postgres,
// pgvector, mysql, mysqlvec
// - cfg: memory service configuration
// - SoftDelete: enable soft delete for SQL backends
// - Extractor: memory extractor for auto mode (nil = manual mode)
// - AsyncMemoryNum: number of async workers for auto mode (default 1)
// - MemoryQueueSize: queue size for memory jobs in auto mode (default 10)
// - MemoryJobTimeout: timeout for each memory job in auto mode (default 30s)
//
// Environment variables by memory type:
//
// sqlite: SQLITE_MEMORY_DSN (default: file:memories.db?_busy_timeout=5000)
// sqlitevec: SQLITEVEC_MEMORY_DSN (default: file:memories_vec.db?_busy_timeout=5000)
// redis: REDIS_ADDR (default: localhost:6379)
// postgres: PG_HOST, PG_PORT, PG_USER, PG_PASSWORD, PG_DATABASE
// pgvector: PGVECTOR_HOST, PGVECTOR_PORT, PGVECTOR_USER, PGVECTOR_PASSWORD, PGVECTOR_DATABASE, PGVECTOR_EMBEDDER_MODEL
// mysql: MYSQL_HOST, MYSQL_PORT, MYSQL_USER, MYSQL_PASSWORD, MYSQL_DATABASE
// mysqlvec: MYSQLVEC_HOST, MYSQLVEC_PORT, MYSQLVEC_USER, MYSQLVEC_PASSWORD, MYSQLVEC_DATABASE, MYSQLVEC_EMBEDDER_MODEL
func NewMemoryServiceByType(memoryType MemoryType, cfg MemoryServiceConfig) (memory.Service, error) {
switch memoryType {
case MemorySQLite:
return newSQLiteMemoryService(cfg)
case MemorySQLiteVec:
return newSQLiteVecMemoryService(cfg)
case MemoryRedis:
return newRedisMemoryService(cfg)
case MemoryPostgres:
return newPostgresMemoryService(cfg)
case MemoryPGVector:
return newPGVectorMemoryService(cfg)
case MemoryMySQL:
return newMySQLMemoryService(cfg)
case MemoryMySQLVec:
return newMySQLVecMemoryService(cfg)
case MemoryInMemory:
fallthrough
default:
return newInMemoryMemoryService(cfg), nil
}
}
const (
sqliteMemoryDSNEnvKey = "SQLITE_MEMORY_DSN"
defaultSQLiteMemoryDBDSN = "file:memories.db?_busy_timeout=5000"
sqliteDriverName = "sqlite3"
defaultSQLiteMaxOpenConns = 1
defaultSQLiteMaxIdleConns = 1
)
func newSQLiteMemoryService(cfg MemoryServiceConfig) (memory.Service, error) {
dsn := GetEnvOrDefault(sqliteMemoryDSNEnvKey, defaultSQLiteMemoryDBDSN)
db, err := sql.Open(sqliteDriverName, dsn)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(defaultSQLiteMaxOpenConns)
db.SetMaxIdleConns(defaultSQLiteMaxIdleConns)
opts := []memorysqlite.ServiceOpt{
memorysqlite.WithSoftDelete(cfg.SoftDelete),
}
if cfg.Extractor != nil {
opts = append(opts, memorysqlite.WithExtractor(cfg.Extractor))
if cfg.AsyncMemoryNum > 0 {
opts = append(
opts,
memorysqlite.WithAsyncMemoryNum(cfg.AsyncMemoryNum),
)
}
if cfg.MemoryQueueSize > 0 {
opts = append(
opts,
memorysqlite.WithMemoryQueueSize(cfg.MemoryQueueSize),
)
}
if cfg.MemoryJobTimeout > 0 {
opts = append(
opts,
memorysqlite.WithMemoryJobTimeout(cfg.MemoryJobTimeout),
)
}
}
svc, err := memorysqlite.NewService(db, opts...)
if err != nil {
_ = db.Close()
return nil, err
}
return svc, nil
}
const (
sqliteVecMemoryDSNEnvKey = "SQLITEVEC_MEMORY_DSN"
defaultSQLiteVecMemoryDBDSN = "file:memories_vec.db?_busy_timeout=5000"
sqliteVecEmbedderModelEnvKey = "SQLITEVEC_EMBEDDER_MODEL"
openAIEmbeddingAPIKeyEnvKey = "OPENAI_EMBEDDING_API_KEY"
openAIEmbeddingBaseURLEnvKey = "OPENAI_EMBEDDING_BASE_URL"
openAIEmbeddingModelEnvKey = "OPENAI_EMBEDDING_MODEL"
)
func getEmbeddingModel(defaultModel string) string {
if env := os.Getenv(openAIEmbeddingModelEnvKey); env != "" {
return env
}
return defaultModel
}
func newOpenAIEmbedder(defaultModel string) *openaiembedder.Embedder {
modelName := getEmbeddingModel(defaultModel)
opts := []openaiembedder.Option{
openaiembedder.WithModel(modelName),
}
if apiKey := os.Getenv(openAIEmbeddingAPIKeyEnvKey); apiKey != "" {
opts = append(opts, openaiembedder.WithAPIKey(apiKey))
}
baseURL := os.Getenv(openAIEmbeddingBaseURLEnvKey)
if baseURL == "" {
baseURL = os.Getenv("OPENAI_BASE_URL")
}
if baseURL != "" {
opts = append(opts, openaiembedder.WithBaseURL(baseURL))
}
return openaiembedder.New(opts...)
}
// newInMemoryMemoryService creates an in-memory memory service.
// Supports both manual mode (cfg.Extractor == nil) and auto mode (cfg.Extractor != nil).
func newInMemoryMemoryService(cfg MemoryServiceConfig) memory.Service {
opts := []memoryinmemory.ServiceOpt{}
// Configure extractor for auto memory mode if provided.
if cfg.Extractor != nil {
opts = append(opts, memoryinmemory.WithExtractor(cfg.Extractor))
if cfg.AsyncMemoryNum > 0 {
opts = append(opts, memoryinmemory.WithAsyncMemoryNum(cfg.AsyncMemoryNum))
}
if cfg.MemoryQueueSize > 0 {
opts = append(opts, memoryinmemory.WithMemoryQueueSize(cfg.MemoryQueueSize))
}
if cfg.MemoryJobTimeout > 0 {
opts = append(opts, memoryinmemory.WithMemoryJobTimeout(cfg.MemoryJobTimeout))
}
}
return memoryinmemory.NewMemoryService(opts...)
}
// newRedisMemoryService creates a Redis memory service.
// Supports both manual mode (cfg.Extractor == nil) and auto mode (cfg.Extractor != nil).
// Environment variables:
// - REDIS_ADDR: Redis server address (default: localhost:6379)
func newRedisMemoryService(cfg MemoryServiceConfig) (memory.Service, error) {
addr := GetEnvOrDefault("REDIS_ADDR", "localhost:6379")
redisURL := fmt.Sprintf("redis://%s", addr)
opts := []memoryredis.ServiceOpt{
memoryredis.WithRedisClientURL(redisURL),
}
// Configure extractor for auto memory mode if provided.
if cfg.Extractor != nil {
opts = append(opts, memoryredis.WithExtractor(cfg.Extractor))
if cfg.AsyncMemoryNum > 0 {
opts = append(opts, memoryredis.WithAsyncMemoryNum(cfg.AsyncMemoryNum))
}
if cfg.MemoryQueueSize > 0 {
opts = append(opts, memoryredis.WithMemoryQueueSize(cfg.MemoryQueueSize))
}
if cfg.MemoryJobTimeout > 0 {
opts = append(opts, memoryredis.WithMemoryJobTimeout(cfg.MemoryJobTimeout))
}
}
return memoryredis.NewService(opts...)
}
// newPostgresMemoryService creates a PostgreSQL memory service.
// Supports both manual mode (cfg.Extractor == nil) and auto mode (cfg.Extractor != nil).
// Environment variables:
// - PG_HOST: PostgreSQL host (default: localhost)
// - PG_PORT: PostgreSQL port (default: 5432)
// - PG_USER: PostgreSQL user (default: postgres)
// - PG_PASSWORD: PostgreSQL password (default: empty)
// - PG_DATABASE: PostgreSQL database (default: trpc-agent-go-pgmemory)
func newPostgresMemoryService(cfg MemoryServiceConfig) (memory.Service, error) {
host := GetEnvOrDefault("PG_HOST", "localhost")
portStr := GetEnvOrDefault("PG_PORT", "5432")
port := 5432
if portStr != "" {
if p, err := strconv.Atoi(portStr); err == nil {
port = p
}
}
user := GetEnvOrDefault("PG_USER", "postgres")
password := GetEnvOrDefault("PG_PASSWORD", "")
database := GetEnvOrDefault("PG_DATABASE", "trpc-agent-go-pgmemory")
opts := []memorypostgres.ServiceOpt{
memorypostgres.WithHost(host),
memorypostgres.WithPort(port),
memorypostgres.WithUser(user),
memorypostgres.WithPassword(password),
memorypostgres.WithDatabase(database),
memorypostgres.WithSoftDelete(cfg.SoftDelete),
}
// Configure extractor for auto memory mode if provided.
if cfg.Extractor != nil {
opts = append(opts, memorypostgres.WithExtractor(cfg.Extractor))
if cfg.AsyncMemoryNum > 0 {
opts = append(opts, memorypostgres.WithAsyncMemoryNum(cfg.AsyncMemoryNum))
}
if cfg.MemoryQueueSize > 0 {
opts = append(opts, memorypostgres.WithMemoryQueueSize(cfg.MemoryQueueSize))
}
if cfg.MemoryJobTimeout > 0 {
opts = append(opts, memorypostgres.WithMemoryJobTimeout(cfg.MemoryJobTimeout))
}
}
return memorypostgres.NewService(opts...)
}
// newPGVectorMemoryService creates a pgvector memory service.
// Supports both manual mode (cfg.Extractor == nil) and auto mode (cfg.Extractor != nil).
// Environment variables:
// - PGVECTOR_HOST: PostgreSQL host (default: localhost)
// - PGVECTOR_PORT: PostgreSQL port (default: 5432)
// - PGVECTOR_USER: PostgreSQL user (default: postgres)
// - PGVECTOR_PASSWORD: PostgreSQL password (default: empty)
// - PGVECTOR_DATABASE: PostgreSQL database (default: trpc-agent-go-pgmemory)
// - PGVECTOR_EMBEDDER_MODEL: Embedder model name (default: text-embedding-3-small)
func newPGVectorMemoryService(cfg MemoryServiceConfig) (memory.Service, error) {
host := GetEnvOrDefault("PGVECTOR_HOST", "localhost")
portStr := GetEnvOrDefault("PGVECTOR_PORT", "5432")
port := 5432
if portStr != "" {
if p, err := strconv.Atoi(portStr); err == nil {
port = p
}
}
user := GetEnvOrDefault("PGVECTOR_USER", "postgres")
password := GetEnvOrDefault("PGVECTOR_PASSWORD", "")
database := GetEnvOrDefault("PGVECTOR_DATABASE", "trpc-agent-go-pgmemory")
embedderModel := GetEnvOrDefault("PGVECTOR_EMBEDDER_MODEL", "text-embedding-3-small")
// Create embedder - for simplicity, we'll use OpenAI embedder
embedder := newOpenAIEmbedder(embedderModel)
opts := []memorypgvector.ServiceOpt{
memorypgvector.WithHost(host),
memorypgvector.WithPort(port),
memorypgvector.WithUser(user),
memorypgvector.WithPassword(password),
memorypgvector.WithDatabase(database),
memorypgvector.WithEmbedder(embedder),
memorypgvector.WithSoftDelete(cfg.SoftDelete),
}
// Configure extractor for auto memory mode if provided.
if cfg.Extractor != nil {
opts = append(opts, memorypgvector.WithExtractor(cfg.Extractor))
if cfg.AsyncMemoryNum > 0 {
opts = append(opts, memorypgvector.WithAsyncMemoryNum(cfg.AsyncMemoryNum))
}
if cfg.MemoryQueueSize > 0 {
opts = append(opts, memorypgvector.WithMemoryQueueSize(cfg.MemoryQueueSize))
}
if cfg.MemoryJobTimeout > 0 {
opts = append(opts, memorypgvector.WithMemoryJobTimeout(cfg.MemoryJobTimeout))
}
}
return memorypgvector.NewService(opts...)
}
// newMySQLMemoryService creates a MySQL memory service.
// Supports both manual mode (cfg.Extractor == nil) and auto mode (cfg.Extractor != nil).
// Environment variables:
// - MYSQL_HOST: MySQL host (default: localhost)
// - MYSQL_PORT: MySQL port (default: 3306)
// - MYSQL_USER: MySQL user (default: root)
// - MYSQL_PASSWORD: MySQL password (default: empty)
// - MYSQL_DATABASE: MySQL database (default: trpc_agent_go)
func newMySQLMemoryService(cfg MemoryServiceConfig) (memory.Service, error) {
host := GetEnvOrDefault("MYSQL_HOST", "localhost")
port := GetEnvOrDefault("MYSQL_PORT", "3306")
user := GetEnvOrDefault("MYSQL_USER", "root")
password := GetEnvOrDefault("MYSQL_PASSWORD", "")
database := GetEnvOrDefault("MYSQL_DATABASE", "trpc_agent_go")
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true&charset=utf8mb4",
user, password, host, port, database)
opts := []memorymysql.ServiceOpt{
memorymysql.WithMySQLClientDSN(dsn),
memorymysql.WithSoftDelete(cfg.SoftDelete),
}
// Configure extractor for auto memory mode if provided.
if cfg.Extractor != nil {
opts = append(opts, memorymysql.WithExtractor(cfg.Extractor))
if cfg.AsyncMemoryNum > 0 {
opts = append(opts, memorymysql.WithAsyncMemoryNum(cfg.AsyncMemoryNum))
}
if cfg.MemoryQueueSize > 0 {
opts = append(opts, memorymysql.WithMemoryQueueSize(cfg.MemoryQueueSize))
}
if cfg.MemoryJobTimeout > 0 {
opts = append(opts, memorymysql.WithMemoryJobTimeout(cfg.MemoryJobTimeout))
}
}
return memorymysql.NewService(opts...)
}
// newMySQLVecMemoryService creates a MySQL vector memory service.
// Supports both manual mode (cfg.Extractor == nil) and auto mode (cfg.Extractor != nil).
// Environment variables:
// - MYSQLVEC_HOST: MySQL host (default: localhost)
// - MYSQLVEC_PORT: MySQL port (default: 3306)
// - MYSQLVEC_USER: MySQL user (default: root)
// - MYSQLVEC_PASSWORD: MySQL password (default: empty)
// - MYSQLVEC_DATABASE: MySQL database (default: trpc_agent_go)
// - MYSQLVEC_EMBEDDER_MODEL: Embedder model name (default: text-embedding-3-small)
func newMySQLVecMemoryService(cfg MemoryServiceConfig) (memory.Service, error) {
host := GetEnvOrDefault("MYSQLVEC_HOST", "localhost")
port := GetEnvOrDefault("MYSQLVEC_PORT", "3306")
user := GetEnvOrDefault("MYSQLVEC_USER", "root")
password := GetEnvOrDefault("MYSQLVEC_PASSWORD", "")
database := GetEnvOrDefault("MYSQLVEC_DATABASE", "trpc_agent_go")
embedderModel := GetEnvOrDefault("MYSQLVEC_EMBEDDER_MODEL", "text-embedding-3-small")
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true&charset=utf8mb4",
user, password, host, port, database)
embedder := newOpenAIEmbedder(embedderModel)
opts := []memorymysqlvec.ServiceOpt{
memorymysqlvec.WithMySQLClientDSN(dsn),
memorymysqlvec.WithEmbedder(embedder),
memorymysqlvec.WithSoftDelete(cfg.SoftDelete),
}
// Configure extractor for auto memory mode if provided.
if cfg.Extractor != nil {
opts = append(opts, memorymysqlvec.WithExtractor(cfg.Extractor))
if cfg.AsyncMemoryNum > 0 {
opts = append(opts, memorymysqlvec.WithAsyncMemoryNum(cfg.AsyncMemoryNum))
}
if cfg.MemoryQueueSize > 0 {
opts = append(opts, memorymysqlvec.WithMemoryQueueSize(cfg.MemoryQueueSize))
}
if cfg.MemoryJobTimeout > 0 {
opts = append(opts, memorymysqlvec.WithMemoryJobTimeout(cfg.MemoryJobTimeout))
}
}
return memorymysqlvec.NewService(opts...)
}
// NewRunner creates a runner with the given memory service and configuration.
func NewRunner(memoryService memory.Service, cfg RunnerConfig) runner.Runner {
modelInstance := openai.New(cfg.ModelName)
genConfig := model.GenerationConfig{
MaxTokens: IntPtr(cfg.MaxTokens),
Temperature: FloatPtr(cfg.Temperature),
Stream: cfg.Streaming,
}
agentOpts := []llmagent.Option{
llmagent.WithModel(modelInstance),
llmagent.WithInstruction(cfg.Instruction),
llmagent.WithGenerationConfig(genConfig),
llmagent.WithTools(memoryService.Tools()),
}
llmAgent := llmagent.New(cfg.AgentName, agentOpts...)
return runner.NewRunner(
cfg.AppName,
llmAgent,
runner.WithSessionService(sessioninmemory.NewSessionService()),
runner.WithMemoryService(memoryService),
)
}
// GetEnvOrDefault retrieves the value of an environment variable or returns a default value if not set.
func GetEnvOrDefault(key, defaultValue string) string {
if value := os.Getenv(key); value != "" {
return value
}
return defaultValue
}
// IntPtr returns a pointer to an int.
func IntPtr(v int) *int {
return &v
}
// FloatPtr returns a pointer to a float64.
func FloatPtr(v float64) *float64 {
return &v
}
// PrintMemoryInfo prints memory service information based on type.
func PrintMemoryInfo(memoryType MemoryType, softDelete bool) {
switch memoryType {
case MemorySQLite:
dsn := GetEnvOrDefault(sqliteMemoryDSNEnvKey, defaultSQLiteMemoryDBDSN)
fmt.Printf("SQLite: %s\n", dsn)
fmt.Printf("Soft delete: %t\n", softDelete)
case MemorySQLiteVec:
dsn := GetEnvOrDefault(
sqliteVecMemoryDSNEnvKey,
defaultSQLiteVecMemoryDBDSN,
)
embedderModel := GetEnvOrDefault(
sqliteVecEmbedderModelEnvKey,
openaiembedder.DefaultModel,
)
fmt.Printf("SQLiteVec: %s\n", dsn)
fmt.Printf("Embedder model: %s\n", getEmbeddingModel(embedderModel))
fmt.Printf("Soft delete: %t\n", softDelete)
case MemoryRedis:
addr := GetEnvOrDefault("REDIS_ADDR", "localhost:6379")
fmt.Printf("Redis: %s\n", addr)
case MemoryPostgres:
host := GetEnvOrDefault("PG_HOST", "localhost")
port := GetEnvOrDefault("PG_PORT", "5432")
database := GetEnvOrDefault("PG_DATABASE", "trpc-agent-go-pgmemory")
fmt.Printf("PostgreSQL: %s:%s/%s\n", host, port, database)
fmt.Printf("Soft delete: %t\n", softDelete)
case MemoryPGVector:
host := GetEnvOrDefault("PGVECTOR_HOST", "localhost")
port := GetEnvOrDefault("PGVECTOR_PORT", "5432")
database := GetEnvOrDefault("PGVECTOR_DATABASE", "trpc-agent-go-pgmemory")
embedderModel := GetEnvOrDefault(
"PGVECTOR_EMBEDDER_MODEL",
"text-embedding-3-small",
)
fmt.Printf("pgvector: %s:%s/%s\n", host, port, database)
fmt.Printf("Embedder model: %s\n", getEmbeddingModel(embedderModel))
fmt.Printf("Soft delete: %t\n", softDelete)
case MemoryMySQL:
host := GetEnvOrDefault("MYSQL_HOST", "localhost")
port := GetEnvOrDefault("MYSQL_PORT", "3306")
database := GetEnvOrDefault("MYSQL_DATABASE", "trpc_agent_go")
fmt.Printf("MySQL: %s:%s/%s\n", host, port, database)
fmt.Printf("Soft delete: %t\n", softDelete)
case MemoryMySQLVec:
host := GetEnvOrDefault("MYSQLVEC_HOST", "localhost")
port := GetEnvOrDefault("MYSQLVEC_PORT", "3306")
database := GetEnvOrDefault("MYSQLVEC_DATABASE", "trpc_agent_go")
embedderModel := GetEnvOrDefault(
"MYSQLVEC_EMBEDDER_MODEL",
"text-embedding-3-small",
)
fmt.Printf("MySQL Vector: %s:%s/%s\n", host, port, database)
fmt.Printf("Embedder model: %s\n", getEmbeddingModel(embedderModel))
fmt.Printf("Soft delete: %t\n", softDelete)
default:
fmt.Printf("In-memory\n")
}
}
// GetAvailableToolsString returns a string describing available memory tools.
func GetAvailableToolsString() string {
return "memory_add, memory_update, memory_search, memory_load\n" +
"(memory_delete, memory_clear disabled by default, can be enabled or customized)"
}
// FormatToolCalls formats tool calls for display.
func FormatToolCalls(toolCalls []model.ToolCall) string {
var builder strings.Builder
for _, toolCall := range toolCalls {
fmt.Fprintf(&builder, " • %s (ID: %s)\n", toolCall.Function.Name, toolCall.ID)
if len(toolCall.Function.Arguments) > 0 {
fmt.Fprintf(&builder, " Args: %s\n", string(toolCall.Function.Arguments))
}
}
return builder.String()
}
// FormatToolResponses formats tool responses for display.
func FormatToolResponses(choices []model.Choice) string {
var builder strings.Builder
for _, choice := range choices {
if choice.Message.Role == model.RoleTool && choice.Message.ToolID != "" {
fmt.Fprintf(&builder, "✅ Memory tool response (ID: %s): %s\n",
choice.Message.ToolID,
strings.TrimSpace(choice.Message.Content))
}
}
return builder.String()
}