Class: Insika::ContextFragment

Inherits:
Data
  • Object
show all
Defined in:
lib/insika/context/fragment.rb

Overview

Unit of context produced by a provider. SHARED type (Insika::, not Insika::Context::). placement: :system | :history | :tool_context priority: Integer; higher = more important (survives cuts) tokens: Integer | nil; estimated by the Builder when nil source: String — provider id (audit) pinned: true -> uncuttable in the budget (e.g. identity) labels: [{ "name" =>, "reason" => }] — WHAT this fragment carries and WHY, as ids. Content-FREE by contract, so the context trace can report which skills a turn injected without storing a byte of the bodies. [] = nothing to name (the default for every provider that has no natural id, e.g. the identity prompt).

         The REASON is the point. A name alone answers "was something
         injected"; the operator's actual question is "which skill did I
         trigger, and why is it here" — `eager` (the agent always wants it),
         `trigger:<matched phrase>` (this message asked for it), or absent
         for a body a plugin supplied. String keys because these labels are
         written to the context trace and to events as JSON: the round-trip
         is then the identity, and no reader has to defend against both.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#contentObject (readonly)

Returns the value of attribute content

Returns:

  • (Object)

    the current value of content



24
25
26
# File 'lib/insika/context/fragment.rb', line 24

def content
  @content
end

#labelsObject (readonly)

Returns the value of attribute labels

Returns:

  • (Object)

    the current value of labels



24
25
26
# File 'lib/insika/context/fragment.rb', line 24

def labels
  @labels
end

#pinnedObject (readonly)

Returns the value of attribute pinned

Returns:

  • (Object)

    the current value of pinned



24
25
26
# File 'lib/insika/context/fragment.rb', line 24

def pinned
  @pinned
end

#placementObject (readonly)

Returns the value of attribute placement

Returns:

  • (Object)

    the current value of placement



24
25
26
# File 'lib/insika/context/fragment.rb', line 24

def placement
  @placement
end

#priorityObject (readonly)

Returns the value of attribute priority

Returns:

  • (Object)

    the current value of priority



24
25
26
# File 'lib/insika/context/fragment.rb', line 24

def priority
  @priority
end

#sourceObject (readonly)

Returns the value of attribute source

Returns:

  • (Object)

    the current value of source



24
25
26
# File 'lib/insika/context/fragment.rb', line 24

def source
  @source
end

#tokensObject (readonly)

Returns the value of attribute tokens

Returns:

  • (Object)

    the current value of tokens



24
25
26
# File 'lib/insika/context/fragment.rb', line 24

def tokens
  @tokens
end

Class Method Details

.build(content:, placement:, source:, priority: 50, tokens: nil, pinned: false, labels: []) ⇒ Object



26
27
28
29
30
31
# File 'lib/insika/context/fragment.rb', line 26

def self.build(content:, placement:, source:, priority: 50, tokens: nil,
               pinned: false, labels: [])
  new(content: content, placement: placement, priority: priority,
      tokens: tokens, source: source, pinned: pinned,
      labels: Array(labels).map { |l| label(l) })
end

.label(raw) ⇒ Object

A bare String is still a valid label (a provider that has an id but no reason to give) — it normalizes to a reason-less entry rather than being rejected.



35
36
37
38
39
40
41
# File 'lib/insika/context/fragment.rb', line 35

def self.label(raw)
  return { "name" => raw.to_s }.freeze unless raw.is_a?(Hash)

  name = (raw[:name] || raw["name"]).to_s
  reason = raw[:reason] || raw["reason"]
  { "name" => name, "reason" => reason&.to_s }.compact.freeze
end