Class: Chorus::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/chorus/context.rb

Overview

Represents the full shared state of a conversation, and knows how to carve out the slice of it that is relevant to a given agent and task.

This class is the heart of Chorus's concept: instead of replaying the entire conversation to every agent, slice_for builds a small, targeted view of the context. See the comments inside slice_for for the exact rules — this logic is intentionally simple in v0.1.0 (no LLM calls) and is the primary thing to refine in v0.2.0.

Defined Under Namespace

Classes: Message

Constant Summary collapse

SUBJECT_LENGTH =

How many characters of a task we keep when summarizing another agent's activity for a teammate. Kept short on purpose — it's a pointer, not a transcript.

60

Instance Method Summary collapse

Constructor Details

#initializeContext

Returns a new instance of Context.



20
21
22
# File 'lib/chorus/context.rb', line 20

def initialize
  @messages = []
end

Instance Method Details

#add_message(role:, content:, agent: nil) ⇒ void

This method returns an undefined value.

Parameters:

  • role (Symbol)

    :user or :assistant

  • content (String)

    the message text

  • agent (Symbol, nil) (defaults to: nil)

    which agent produced this message (nil for user messages)



28
29
30
# File 'lib/chorus/context.rb', line 28

def add_message(role:, content:, agent: nil)
  @messages << Message.new(role: role, content: content, agent: agent, timestamp: Time.now)
end

#full_historyArray<Message>

Returns the complete, unfiltered history — for debugging/logging only.

Returns:

  • (Array<Message>)

    the complete, unfiltered history — for debugging/logging only.



33
34
35
# File 'lib/chorus/context.rb', line 33

def full_history
  @messages.dup
end

#slice_for(agent_name, task) ⇒ Array<Hash>

Builds the context slice for agent_name working on task.

The slice always contains, in order:

1. A one-line summary of what OTHER agents have been doing, so this
 agent has situational awareness without the full transcript of
 their conversations. Built by concatenating short subjects
 (truncated prior task text) — no LLM call involved in v0.1.0.
2. This agent's own past (task, response) pairs, so it remembers what
 it has already said — continuity of memory per role.
3. The current task, which is always the final entry.

The Anthropic API only requires the conversation to start with a user turn; consecutive same-role turns are merged server-side, so we don't need to strictly alternate roles here.

Parameters:

  • agent_name (Symbol)

    the agent about to handle task, e.g. :coder

  • task (String)

    the current user message

Returns:

  • (Array<Hash>)

    messages shaped as {role:, content:}, ready for Chorus::Client#chat



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/chorus/context.rb', line 55

def slice_for(agent_name, task)
  slice = []

  summary = other_agents_summary(agent_name)
  slice << { role: "user", content: "[Other agents' context] #{summary}" } if summary

  slice.concat(own_history_for(agent_name))

  slice << { role: "user", content: task }
  slice
end