Skip to content

Commit c9434f0

Browse files
committed
session: add WithID option for custom session IDs
Apply options before generating UUID, allowing callers to set a custom session ID via WithID. If no ID is provided, a UUID is generated.
1 parent 528a4b3 commit c9434f0

1 file changed

Lines changed: 13 additions & 4 deletions

File tree

pkg/session/session.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,13 @@ func WithParentID(parentID string) Opt {
579579
}
580580
}
581581

582+
// WithID sets the session ID. If not set, a UUID will be generated.
583+
func WithID(id string) Opt {
584+
return func(s *Session) {
585+
s.ID = id
586+
}
587+
}
588+
582589
// WithExcludedTools sets tool names that should be filtered out of the agent's
583590
// tool list for this session. This prevents recursive tool calls in skill
584591
// sub-sessions.
@@ -647,11 +654,7 @@ func (s *Session) OwnCost() float64 {
647654

648655
// New creates a new agent session
649656
func New(opts ...Opt) *Session {
650-
sessionID := uuid.New().String()
651-
slog.Debug("Creating new session", "session_id", sessionID)
652-
653657
s := &Session{
654-
ID: sessionID,
655658
CreatedAt: time.Now(),
656659
SendUserMessage: true,
657660
}
@@ -660,6 +663,12 @@ func New(opts ...Opt) *Session {
660663
opt(s)
661664
}
662665

666+
if s.ID == "" {
667+
s.ID = uuid.New().String()
668+
}
669+
670+
slog.Debug("Creating new session", "session_id", s.ID)
671+
663672
return s
664673
}
665674

0 commit comments

Comments
 (0)