Class: ActionAgent::Api::InteractionsController

Inherits:
BaseController show all
Defined in:
app/controllers/action_agent/api/interactions_controller.rb

Overview

Read API for agent conversation contexts (solid_agent persistence), backing the dashboard Interactions view.

A context is one agent's interaction stream (AgentContext + AgentMessages + AgentGenerations, recorded by SolidAgent::HasContext during execution), scoped to the current user's agents.

Constant Summary collapse

DEFAULT_LIMIT =
50
MAX_WINDOW_MINUTES =
60 * 24 * 90

Instance Method Summary collapse

Methods inherited from ActionAgent::ApplicationController

allow_unauthenticated_access

Instance Method Details

#indexObject

GET /api/interactions



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/action_agent/api/interactions_controller.rb', line 18

def index
  limit = params.fetch(:limit, DEFAULT_LIMIT).to_i.clamp(1, 200)

  contexts = interactions_scope
    .includes(:contextable)
    .then { |scope| window_minutes ? scope.where(updated_at: window_minutes.minutes.ago..) : scope }
    .recent
    .limit(limit)

  message_counts = AgentMessage.where(agent_context_id: contexts.map(&:id)).group(:agent_context_id).count
  generation_counts = AgentGeneration.where(agent_context_id: contexts.map(&:id)).group(:agent_context_id).count
  # Latest model per context, so list rows can gauge context-window
  # pressure without loading generations.
  # DISTINCT ON would do this in one pass on PostgreSQL but has no
  # portable equivalent, so take the newest generation id per context
  # (they are only ever appended) and read those rows.
  newest_ids = AgentGeneration
    .where(agent_context_id: contexts.map(&:id))
    .group(:agent_context_id)
    .maximum(:id)
  latest_models = AgentGeneration.where(id: newest_ids.values)
    .pluck(:agent_context_id, :model).to_h

  previews = message_previews(contexts.map(&:id))

  persisted = contexts.map do |context|
    serialize_context(context).merge(
      source: "platform",
      model: latest_models[context.id],
      message_count: message_counts[context.id] || 0,
      generation_count: generation_counts[context.id] || 0,
      preview: previews[context.id] || { input: nil, output: nil }
    )
  end

  reported = reported_traces(limit).map { |trace| TraceInteractionSerializer.summary(trace) }

  render json: {
    interactions: (persisted + reported).sort_by { |row| row[:last_activity_at].to_s }.reverse.first(limit)
  }
end

#showObject

GET /api/interactions/:id



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/action_agent/api/interactions_controller.rb', line 61

def show
  if (trace_id = params[:id].to_s[/\Atrace-(\d+)\z/, 1])
    trace = owned_traces.find(trace_id)
    return render json: { interaction: TraceInteractionSerializer.detail(trace) }
  end

  context = interactions_scope.find(params[:id])

  render json: {
    interaction: serialize_context(context).merge(
      source: "platform",
      instructions: context.instructions,
      messages: context.messages.chronological.map { |message| serialize_message(message) },
      generations: context.generations.order(created_at: :asc).map { |generation| serialize_generation(generation) }
    )
  }
end