2.26.5.33. 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.

Module Attributes

DEFAULT_OUTPUT_DIR

Folder used when no output_dir is given.

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) – Folder for the script and its .validation.json report; defaults to DEFAULT_OUTPUT_DIR (pycsamt_agent_output).

  • 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.\nGenerate or edit a clean Python script for the user's complete request.\nWorkflow configurations and templates are references, not fixed plans.\nPreserve requested selections, quantities, units, filenames, formats, and\nexclusions. For an edit, preserve the prior script except for requested changes.\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. Never invent missing APIs or scientific parameters.\nOutput only valid Python code, or CLARIFY: and one focused question when\nrequired information is missing. Do not claim to have executed the script.\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