Skip to content

Refactor actions to use output JSON responses#43

Merged
zfoong merged 3 commits intofeature/abstracted-executionfrom
codex/refactor-action-scripts-to-return-json
Dec 27, 2025
Merged

Refactor actions to use output JSON responses#43
zfoong merged 3 commits intofeature/abstracted-executionfrom
codex/refactor-action-scripts-to-return-json

Conversation

@zfoong
Copy link
Collaborator

@zfoong zfoong commented Dec 27, 2025

Summary

  • update action scripts to assign their JSON responses to the output variable instead of printing
  • ensure success and error paths consistently set structured outputs across CLI and GUI actions

Testing

  • not run (not requested)

Codex Task

Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

"execution_mode": "sandboxed",
"mode": "ALL",
"code": "import os, sys, json, importlib, subprocess\nfrom typing import List\n\n# Ensure dependencies\n\ndef _ensure(pkg):\n try:\n importlib.import_module(pkg)\n except ImportError:\n subprocess.check_call([sys.executable, '-m', 'pip', 'install', pkg, '--quiet'])\n\n_ensure('python-docx')\n\nfrom docx import Document\n\n# ───────────────────────────────────────────────\n# Read file contents\n# ───────────────────────────────────────────────\n\ndef read_txt(path: str) -> str:\n try:\n with open(path, 'r', encoding='utf-8', errors='ignore') as f:\n return f.read()\n except Exception:\n return ''\n\n\ndef read_md(path: str) -> str:\n return read_txt(path)\n\n\ndef read_docx(path: str) -> str:\n try:\n doc = Document(path)\n return '\\n'.join([p.text for p in doc.paragraphs])\n except Exception:\n return ''\n\n# ───────────────────────────────────────────────\n# Entrypoint\n# ───────────────────────────────────────────────\n\ndef _execute_action():\n input_data = globals().get('input_data', {}) or {}\n\n directory = input_data.get('directory_path')\n output_path = input_data.get('output_path')\n\n if not directory or not os.path.isdir(directory):\n print(json.dumps({'error': \"'directory_path' must be a valid directory.\"}))\n return\n if not output_path:\n print(json.dumps({'error': \"'output_path' is required and must be a file path ending in .md\"}))\n return\n\n supported_ext = {'.txt', '.md', '.docx'}\n collected = []\n\n for fname in os.listdir(directory):\n ext = os.path.splitext(fname)[1].lower()\n if ext in supported_ext:\n collected.append(os.path.join(directory, fname))\n\n if not collected:\n print(json.dumps({'error': 'No supported text files found in directory.'}))\n return\n\n content_blocks = []\n included_files = []\n\n for path in collected:\n ext = os.path.splitext(path)[1].lower()\n if ext == '.txt': text = read_txt(path)\n elif ext == '.md': text = read_md(path)\n elif ext == '.docx': text = read_docx(path)\n else: continue\n\n if text.strip():\n content_blocks.append(f\"# File: {os.path.basename(path)}\\n\\n{text}\\n\\n---\\n\")\n included_files.append(os.path.basename(path))\n\n # Ensure directory for output exists\n os.makedirs(os.path.dirname(output_path), exist_ok=True)\n\n final_md = '\\n'.join(content_blocks)\n with open(output_path, 'w', encoding='utf-8') as f:\n f.write(final_md)\n\n print(json.dumps({\n 'message': 'Draft created successfully.',\n 'output_file': output_path,\n 'files_included': included_files\n }, ensure_ascii=False))\n\n_execute_action()",
"code": "import os, sys, json, importlib, subprocess\nfrom typing import List\n\n# Ensure dependencies\n\ndef _ensure(pkg):\n try:\n importlib.import_module(pkg)\n except ImportError:\n subprocess.check_call([sys.executable, '-m', 'pip', 'install', pkg, '--quiet'])\n\n_ensure('python-docx')\n\nfrom docx import Document\n\n# ───────────────────────────────────────────────\n# Read file contents\n# ───────────────────────────────────────────────\n\ndef read_txt(path: str) -> str:\n try:\n with open(path, 'r', encoding='utf-8', errors='ignore') as f:\n return f.read()\n except Exception:\n return ''\n\n\ndef read_md(path: str) -> str:\n return read_txt(path)\n\n\ndef read_docx(path: str) -> str:\n try:\n doc = Document(path)\n return '\\n'.join([p.text for p in doc.paragraphs])\n except Exception:\n return ''\n\n# ───────────────────────────────────────────────\n# Entrypoint\n# ───────────────────────────────────────────────\n\ndef _execute_action():\n input_data = globals().get('input_data', {}) or {}\n\n directory = input_data.get('directory_path')\n output_path = input_data.get('output_path')\n\n if not directory or not os.path.isdir(directory):\n output = json.dumps({'error': \"'directory_path' must be a valid directory.\"})\n return\n if not output_path:\n output = json.dumps({'error': \"'output_path' is required and must be a file path ending in .md\"})\n return\n\n supported_ext = {'.txt', '.md', '.docx'}\n collected = []\n\n for fname in os.listdir(directory):\n ext = os.path.splitext(fname)[1].lower()\n if ext in supported_ext:\n collected.append(os.path.join(directory, fname))\n\n if not collected:\n output = json.dumps({'error': 'No supported text files found in directory.'})\n return\n\n content_blocks = []\n included_files = []\n\n for path in collected:\n ext = os.path.splitext(path)[1].lower()\n if ext == '.txt': text = read_txt(path)\n elif ext == '.md': text = read_md(path)\n elif ext == '.docx': text = read_docx(path)\n else: continue\n\n if text.strip():\n content_blocks.append(f\"# File: {os.path.basename(path)}\\n\\n{text}\\n\\n---\\n\")\n included_files.append(os.path.basename(path))\n\n # Ensure directory for output exists\n os.makedirs(os.path.dirname(output_path), exist_ok=True)\n\n final_md = '\\n'.join(content_blocks)\n with open(output_path, 'w', encoding='utf-8') as f:\n f.write(final_md)\n\n output = json.dumps({\n 'message': 'Draft created successfully.',\n 'output_file': output_path,\n 'files_included': included_files\n }, ensure_ascii=False)\n\n_execute_action()",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Restore stdout emission for sandboxed action outputs

Sandboxed actions now assign their responses to output without printing them. In combine text documents, the code runs inside _atomic_action_venv_process, which only returns captured stdout; because nothing is printed, ActionExecutor hands back an empty raw_stdout and _parse_action_output returns {}, so callers see a “successful” action with no data and no error. The same regression applies to the other sandboxed actions touched in this commit (e.g., convert to md, create folder, create and run python script, trace mouse, window close/focus/maximize/minimize).

Useful? React with 👍 / 👎.

@zfoong zfoong merged commit 4228b1f into feature/abstracted-execution Dec 27, 2025
@zfoong zfoong deleted the codex/refactor-action-scripts-to-return-json branch December 27, 2025 05:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant