Class: Ask::Agent::ContextSource

Inherits:
Object
  • Object
show all
Defined in:
lib/ask/agent/context_source.rb

Overview

A typed, independently observable piece of the system context.

Each source has a unique key, a load function that returns its current value, a baseline render for initial prompt inclusion, and an update render for mid-conversation change notifications.

Examples:

class DateSource < Ask::Agent::ContextSource
  key "core/date"

  def load
    Date.today.iso8601
  end

  def baseline(date)
    "Today is #{date}."
  end

  def update(prev, curr)
    "Earlier I said the date was #{prev}, but it is now #{curr}."
  end
end

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(subclass) ⇒ Object



39
40
41
42
43
44
# File 'lib/ask/agent/context_source.rb', line 39

def inherited(subclass)
  super
  # Register source type for discovery
  @registered_sources ||= []
  @registered_sources << subclass
end

.key(value = :__no_value__) ⇒ Object

Unique namespaced key for this source (e.g., "core/date"). Used for change detection and deterministic ordering.



31
32
33
34
35
36
37
# File 'lib/ask/agent/context_source.rb', line 31

def key(value = :__no_value__)
  if value == :__no_value__
    @key
  else
    @key = value
  end
end

.registered_sourcesObject



46
47
48
# File 'lib/ask/agent/context_source.rb', line 46

def registered_sources
  @registered_sources || []
end

Instance Method Details

#baseline(value) ⇒ String

Render the initial text for this source.

Parameters:

  • value (Object)

    the value returned by load

Returns:

  • (String)

Raises:

  • (NotImplementedError)


65
66
67
# File 'lib/ask/agent/context_source.rb', line 65

def baseline(value)
  raise NotImplementedError
end

#keyString

Returns unique key for this source.

Returns:

  • (String)

    unique key for this source



52
53
54
# File 'lib/ask/agent/context_source.rb', line 52

def key
  self.class.key
end

#loadObject

Load the current value of this source.

Returns:

  • (Object)

    any value that will be passed to baseline and update

Raises:

  • (NotImplementedError)


58
59
60
# File 'lib/ask/agent/context_source.rb', line 58

def load
  raise NotImplementedError
end

#update(previous, current) ⇒ String?

Render a mid-conversation update message. Called when load returns a value different from the previous one.

Parameters:

  • previous (Object)

    the value from the last load

  • current (Object)

    the current value

Returns:

  • (String, nil)

    nil if no update is needed



74
75
76
# File 'lib/ask/agent/context_source.rb', line 74

def update(previous, current)
  nil
end