pycsamt.agents.code_gen#

pycsamt.agents.code_gen#

CodeGenerationAgent — Emit a reproducible Python script from a completed workflow configuration and agent results.

The generated script imports pycsamt, replicates every processing step that was executed, and is self-contained (no agent infrastructure needed to run it). It is the “save your workflow as code” button.

Classes

CodeGenerationAgent(*[, api_key, model, ...])

Generate a reproducible Python script from a completed workflow.

class pycsamt.agents.code_gen.CodeGenerationAgent(*, api_key=None, model=None, llm_provider='claude', script_title='pycsamt MT Processing Workflow')[source]#

Bases: BaseAgent

Generate a reproducible Python script from a completed workflow.

Parameters:
  • api_key (str) – When an API key is provided the LLM refines and annotates the generated code. Otherwise the agent uses static templates.

  • model (str) – When an API key is provided the LLM refines and annotates the generated code. Otherwise the agent uses static templates.

  • llm_provider (str) – When an API key is provided the LLM refines and annotates the generated code. Otherwise the agent uses static templates.

  • script_title (str)

  • keys (Output data)

  • ----------

  • workflow_config (dict) – The config dict produced by ContextInputAgent.

  • results (dict) – The agent results dict from AgentCoordinator.

  • output_dir (str, optional)

  • keys

  • ----------------

  • code (code str — Python source)

  • file (script_path str or None — path to saved .py)

Examples

>>> agent  = CodeGenerationAgent()
>>> result = agent.execute({
...     "workflow_config": cfg,
...     "results":         coord_results,
...     "output_dir":      "/out",
... })
>>> print(result["script_path"])
/out/workflow_script.py
SYSTEM_PROMPT: str = 'You are an expert Python developer specialising in geophysics scripting.\nGiven a pycsamt workflow configuration and execution log, generate a clean,\nwell-commented Python script that reproduces the workflow step by step.\nUse pycsamt v2 public API only.  Add a one-line comment above each major\nblock.  Do not use the agents/ subpackage call emtools, forward, and\nsite functions directly.  Output only valid Python code.\n'#

Override in subclasses to give the LLM its domain expertise.

execute(input_data)[source]#

Run this agent on input_data and return an AgentResult.

Subclasses must implement this method. The contract:

  • Reset self._last_cost = 0.0 at the top.

  • Record wall-clock time with t0 = time.time().

  • Return AgentResult(elapsed_seconds=time.time()-t0, cost_estimate_usd=self._last_cost, ...).

Parameters:

input_data (dict[str, Any])

Return type:

AgentResult