agent-zero/python/tools/persona_create.py
nic 37e696b104
Agent persona profiles (#7)
* Add survey profile DB, parser, and background refiner

Co-authored-by: nic <nicsins@users.noreply.github.com>

* Block external survey domains unless allowlisted

Co-authored-by: nic <nicsins@users.noreply.github.com>

* Add persona creation tool for survey personas

Co-authored-by: nic <nicsins@users.noreply.github.com>

* Add local survey demo and documentation

Co-authored-by: nic <nicsins@users.noreply.github.com>

* Add profile update tool and email env fallback

Co-authored-by: nic <nicsins@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: nic <nicsins@users.noreply.github.com>
2026-03-17 02:41:34 -05:00

65 lines
2.2 KiB
Python

import json
import uuid
from python.helpers.tool import Tool, Response
from python.surveys.db import SurveyDB
from python.surveys.schemas import Persona
class PersonaCreate(Tool):
async def execute(
self,
name: str = "",
description: str = "",
constraints_json: str = "",
generate: bool = False,
seed: str = "",
**kwargs,
):
"""
Create (and store) a persona used for survey answering.
If generate=true, the utility model will draft name/description/constraints using 'seed'.
"""
db = SurveyDB.for_agent(self.agent)
try:
constraints = {}
if constraints_json:
constraints = json.loads(constraints_json)
if generate:
system = (
"You design a survey-answering persona for testing.\n"
"Output ONLY valid JSON with keys: name, description, constraints.\n"
"Constraints must be a JSON object with stable fields (demographics, preferences, traits).\n"
)
msg = f"seed: {seed}\nexisting_name: {name}\nexisting_description: {description}\n"
out = await self.agent.call_utility_model(system=system, message=msg, background=False)
data = json.loads(out)
if isinstance(data, dict):
name = str(data.get("name") or name or "Persona")
description = str(data.get("description") or description or "")
if isinstance(data.get("constraints"), dict):
constraints = data["constraints"]
if not name:
name = "Persona"
persona = Persona(
id=str(uuid.uuid4()),
name=name,
description=description or "",
constraints=constraints or {},
)
db.upsert_persona(persona)
return Response(
message=json.dumps(
{"persona_id": persona.id, "name": persona.name},
ensure_ascii=False,
indent=2,
),
break_loop=False,
)
finally:
db.close()