Touch creates or updates a session. Returns the session. When conversationKey is non-empty it is used directly as the session ID (e.g. prompt_cache_key from Codex), bypassing the hash computation.
(apiKey, userAgent, format, conversationKey string)
| 69 | // When conversationKey is non-empty it is used directly as the session ID |
| 70 | // (e.g. prompt_cache_key from Codex), bypassing the hash computation. |
| 71 | func (m *Manager) Touch(apiKey, userAgent, format, conversationKey string) *Session { |
| 72 | var id string |
| 73 | if conversationKey != "" { |
| 74 | id = conversationKey |
| 75 | } else { |
| 76 | id = ComputeSessionID(apiKey, userAgent) |
| 77 | } |
| 78 | now := time.Now() |
| 79 | |
| 80 | m.mu.Lock() |
| 81 | defer m.mu.Unlock() |
| 82 | |
| 83 | if sess, ok := m.sessions[id]; ok { |
| 84 | sess.mu.Lock() |
| 85 | sess.LastActivity = now |
| 86 | if format != "" { |
| 87 | sess.Format = format |
| 88 | } |
| 89 | sess.mu.Unlock() |
| 90 | return sess |
| 91 | } |
| 92 | |
| 93 | sess := &Session{ |
| 94 | ID: id, |
| 95 | APIKeyHash: hashKey(apiKey), |
| 96 | UserAgent: userAgent, |
| 97 | Format: format, |
| 98 | CreatedAt: now, |
| 99 | LastActivity: now, |
| 100 | processedCallIDs: make(map[string]bool), |
| 101 | subscribers: make(map[string]chan *CommandResult), |
| 102 | observers: make(map[string]chan *ObserveEvent), |
| 103 | } |
| 104 | m.sessions[id] = sess |
| 105 | cb := m.onNewSession |
| 106 | m.mu.Unlock() |
| 107 | if cb != nil { |
| 108 | cb(sess) |
| 109 | } |
| 110 | m.mu.Lock() |
| 111 | return sess |
| 112 | } |
| 113 | |
| 114 | // Get returns a session by ID, or nil. |
| 115 | func (m *Manager) Get(id string) *Session { |