Class: Terret::Titler

Inherits:
Hames::Service
  • Object
show all
Defined in:
lib/terret/titler.rb

Overview

ctx — one durable title per session (plan §6.2's titler role, §12 M6). Rides the first turn/end: generates through the :titler model role when the roles map has one, else falls back to the first user line truncated to 40 chars. session/titled is metadata — projection-invisible, like the approval and policy events — so titling can never disturb derived context. The :role knob is read per call, so reconfigure is live by construction.

Constant Summary collapse

PROMPT =
"Title this conversation in at most six words. Reply with the title only."

Instance Method Summary collapse

Instance Method Details

#reconfigure(_config) ⇒ Object



29
# File 'lib/terret/titler.rb', line 29

def reconfigure(_config); end

#start(ctx) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/terret/titler.rb', line 19

def start(ctx)
  @ctx = ctx
  ctx.on("session/event") do |ev|
    next unless ev.type == "turn/end"
    next if @ctx[:sessions].title(ev.session_id)

    title!(ev.session_id)
  end
end

#title!(session_id) ⇒ Object

Append a title now (re-titling is the caller's explicit choice; the listener above only ever titles once). Returns the event, or nil when there is nothing to title.



34
35
36
37
38
39
40
41
42
43
# File 'lib/terret/titler.rb', line 34

def title!(session_id)
  sessions = @ctx[:sessions]
  history = sessions.derive_messages(session_id)
  return if history.empty?

  title = (generate(history) || fallback(sessions.fetch(session_id).events)).to_s.strip
  return if title.empty?

  sessions.append(session_id, "session/titled", { title: title[0, 80] })
end