The platform for continuously improving AI agents.
from django.db import transaction
from overbae.core.errors import InputValidationError
from overbae.innermind import runs
from overbae.models import ExperimentAsset
from overbae.services import (
experiment_assets,
experiment_components,
experiment_predictions,
experiment_scoring,
research_context_authorship,
research_evidence,
research_team,
)
def invoke(action, request):
from overbae.services.research_tools import (
source_cell, # The native dispatcher calls this adapter.
)
assignment = action.agent.assignment
project = assignment.project
key = f"research:{action.pk}"
if action.tool == "asset":
if request.operation == "publish":
if research_evidence.can_read_final(assignment):
raise InputValidationError(
"Final-result observers cannot publish reusable adaptive assets"
)
with transaction.atomic():
research_team.current_turn(action.agent)
return research_context_authorship.project_asset(
assignment,
asset := experiment_assets.publish(
project,
assignment.created_by,
kind=request.kind,
name=request.name,
specification=request.definition,
parent=request.parent,
request_key=key,
),
experiment_assets.describe(asset),
)
if request.operation == "inspect":
asset = experiment_assets.get(project, request.asset, request.kind)
return research_context_authorship.project_asset(
assignment, asset, experiment_assets.describe(asset)
)
assets = ExperimentAsset.objects.filter(project=project)
if request.kind:
assets = assets.filter(kind=request.kind)
page = list(
assets.order_by("-created_at", "-pk")[
request.offset : request.offset + request.limit + 1
]
)
return {
"assets": [
research_context_authorship.project_asset(
assignment,
a,
{"id": str(a.pk), "name": a.name, "kind": a.kind, "fingerprint": a.fingerprint},
)
for a in page[: request.limit]
],
"next_offset": request.offset + request.limit if len(page) > request.limit else None,
}
if action.tool == "component":
return experiment_components.invoke(action, request)
if action.tool == "prediction":
if request.operation == "run":
if request.candidate is None or request.cell is None:
raise InputValidationError("Connect a candidate and exact data version")
with transaction.atomic():
research_team.current_turn(action.agent)
return experiment_predictions.describe(
experiment_predictions.create(assignment, request.candidate, request.cell, key)
)
predictions = experiment_predictions.get(project, request.prediction_set)
source_cell(assignment, predictions.cell_id)
if request.operation != "inspect" and predictions.assignment_id != assignment.pk:
raise InputValidationError("Only the owning assignment controls prediction execution")
if request.operation == "resume":
with transaction.atomic():
research_team.current_turn(action.agent)
return experiment_predictions.resume(predictions)
if request.operation == "cancel":
with transaction.atomic():
research_team.current_turn(action.agent)
return experiment_predictions.cancel(predictions)
return experiment_predictions.describe(predictions)
if request.operation == "native":
if request.evaluation is None or not request.participant or request.suite is None:
raise InputValidationError(
"Select a native evaluation, participant and benchmark suite"
)
with transaction.atomic():
research_team.current_turn(action.agent)
return experiment_scoring.describe(
experiment_scoring.score_native(
assignment, request.evaluation, request.participant, request.suite, key
)
)
if request.operation == "run":
if request.prediction_set is None or request.suite is None:
raise InputValidationError("Connect retained predictions and a benchmark suite")
with transaction.atomic():
research_team.current_turn(action.agent)
return experiment_scoring.describe(
experiment_scoring.create(assignment, request.prediction_set, request.suite, key)
)
evaluation = experiment_scoring.get(project, request.evaluation)
source_cell(assignment, evaluation.prediction_set.cell_id)
if request.operation == "cancel":
if evaluation.assignment_id != assignment.pk:
raise InputValidationError("Only the owning assignment controls scoring")
with transaction.atomic():
research_team.current_turn(action.agent)
runs.cancel(evaluation.run)
evaluation.run.refresh_from_db()
return experiment_scoring.describe(evaluation)