Mastodon Politics, Power, and Science: Implementation Specification: Agent cut and paste and Sharing

Saturday, August 9, 2025

Implementation Specification: Agent cut and paste and Sharing

 


1. Feature Overview

This specification outlines two new, related features:

  1. Paste to Create/Populate: Allows a user to paste a valid agent JSON string into the application.

    • If no agent is currently loaded in the editor, the application will treat the pasted JSON as a new agent, create it, and load it.

    • If an agent is loaded, the application will use the pasted JSON to populate the fields of the current editor, effectively "cloning" the data into the existing agent.

  2. Copy Agent JSON Button: A new button in the "Settings" tab of every agent editor that copies the complete, current JSON definition of that agent to the system clipboard.

These features will work together to create a seamless "copy-paste" workflow for duplicating, sharing, and modifying agents.

2. Implementation Details

The main App class will be responsible for handling the paste event.

  1. Event Binding:

    • In the App.__init__ method, a key binding for the paste event (<Control-v> or <Command-v>) will be attached to the main application window (self).

    • This binding will call a new method, self.handle_paste_event(event=None).

  2. New Method: 

    • This method will be the central logic for processing pasted text.

    • It will first try to get the content from the system clipboard using self.clipboard_get().

    • It will then attempt to parse the clipboard content as JSON using json.loads().

    • If the parsing fails, it will do nothing (or show a subtle toast message "Invalid JSON in clipboard").

    • Agent Validation: After successful parsing, it must validate that the JSON object is a valid agent definition. A helper function _is_valid_agent_json(data) will be created. This function checks for the presence of essential keys like "type""inputs""outputs", etc.

  3. Conditional Logic within 

    • Case 1: No Agent is Loaded (

      • If the pasted JSON is a valid agent, a new agent will be created.

      • The logic will check if a name is present in the pasted JSON. If not, or if the name already exists, it will use self.config_manager.generate_unique_name() to create a safe name.

      • The new agent data (from the clipboard) will be added to the config using self.config_manager.update_agent().

      • The application will then save the config, refresh the agent list, and automatically select and load the newly created agent using self.select_agent(new_name).

      • If the JSON is invalid, a messagebox.showerror() will be displayed.

    • Case 2: An Agent IS Loaded (

      • If the pasted JSON is a valid agent, it will populate the current editor's fields.

      • A new method will be added to the BaseEditorFrame class and all its subclasses (ProcEditorFrameTemplateEditorFrameWorkflowEditorFrame): populate_from_data(self, new_data).

      • handle_paste_event will call self.editor_frame_instance.populate_from_data(pasted_json_data).

      • This will update all the UI widgets with the new data, effectively "pasting" the state of another agent onto the current one without changing its name.

      • If the JSON is invalid, a messagebox.showerror() will be displayed.

This feature is simpler and will be implemented in the individual editor frames.

  1. Button Creation:

    • In the create_settings_tab method of ProcEditorFrameTemplateEditorFrame, and WorkflowEditorFrame, a new button will be added.

    • ctk.CTkButton(tab, text="Copy JSON to Clipboard", command=self.copy_agent_json).pack(...)

  2. New Method: 

    • This method will be added to ProcEditorFrameTemplateEditorFrame, and WorkflowEditorFrame.

    • It will first call self.get_data() to gather all the current, potentially unsaved data from the UI form widgets.

    • It will then format this data as a clean JSON string using json.dumps(current_data, indent=2).

    • Finally, it will clear the system clipboard and append the new JSON string using self.clipboard_clear() and self.clipboard_append(json_string).

    • A confirmation toast message will be shown using self.app_ref.show_toast("Agent JSON copied to clipboard.").

  3. New Method: 

    • This method, required for Part A, will be implemented in each editor frame.

    • It will take a dictionary (new_data) as input.

    • It will programmatically update the contents of each widget (self.help_textself.inputs_frameself.prompt_text, etc.) with the corresponding values from the new_data dictionary.

    • The self.name_entry will not be updated, as this action is intended to clone the contents of an agent, not rename the current one.

3. User Workflow

The new user workflow for cloning an agent will be:

  1. User opens an existing agent (Agent_A) that they want to clone.

  2. They click the "Copy JSON to Clipboard" button in the Settings tab.

  3. They navigate back to the main agent list.

  4. With no agent loaded, they press Ctrl+V.

  5. A new agent (Agent_A_1) appears in the list, an exact copy of Agent_A, and is automatically opened in the editor.

  6. The user can then rename it and make modifications.

No comments:

Post a Comment

Progress on the campaign manager

You can see that you can build tactical maps automatically from the world map data.  You can place roads, streams, buildings. The framework ...