{
  "name": "edit_code",
  "title": "编辑代码",
  "description": "将代码写入文件并验证格式、运行测试",
  "group": "develop",
  "version": "1.0.0",
  "input_schema": {
    "type": "object",
    "properties": {
      "code": {
        "type": "string",
        "description": "要写入的代码内容"
      },
      "file_path": {
        "type": "string",
        "description": "目标文件路径"
      },
      "mode": {
        "type": "string",
        "description": "编辑模式: replace(替换)/insert(插入)，默认 insert",
        "enum": [
          "replace",
          "insert"
        ]
      },
      "start_line": {
        "type": "number",
        "description": "起始行号(从1开始)，insert 模式表示在此行之后插入"
      },
      "end_line": {
        "type": "number",
        "description": "结束行号(从1开始)，replace 模式使用"
      }
    },
    "required": [
      "code",
      "file_path"
    ]
  },
  "output_schema": {
    "type": "object",
    "properties": {
      "msg": {
        "type": "string",
        "description": "测试执行结果描述"
      },
      "stdout": {
        "type": "string",
        "description": "测试标准输出"
      },
      "stderr": {
        "type": "string",
        "description": "测试标准错误"
      },
      "exit_code": {
        "type": "number",
        "description": "测试退出码"
      }
    }
  },
  "steps": [
    {
      "id": "verify_format",
      "description": "验证代码语法和格式规范",
      "operator": "validator",
      "handler": "code",
      "params": {
        "code": "{{ $input.code }}"
      }
    },
    {
      "id": "check_valid",
      "description": "验证失败则中止，通过则写入文件",
      "operator": "dispatcher",
      "handler": "condition",
      "params": {
        "source": "{{ $steps.verify_format.valid }}",
        "operator": "truthy",
        "then_jump": "write_code",
        "else_jump": "end"
      }
    },
    {
      "id": "write_code",
      "description": "将代码写入文件",
      "operator": "manager",
      "handler": "text",
      "params": {
        "file_path": "{{ $input.file_path }}",
        "code": "{{ $input.code }}",
        "mode": "{{ $input.mode }}",
        "start_line": "{{ $input.start_line }}",
        "end_line": "{{ $input.end_line }}"
      }
    },
    {
      "id": "test_code",
      "description": "运行代码进行测试",
      "operator": "executor",
      "handler": "command",
      "params": {
        "command": "node {{ $input.file_path }}"
      }
    },
    {
      "id": "log_artifact",
      "description": "记录产出文件信息",
      "operator": "manager",
      "handler": "artifact",
      "params": {
        "work_name": "edit_code",
        "stage": "executed",
        "file_path": "{{ $input.file_path }}",
        "valid": "{{ $steps.verify_format.valid }}",
        "error_count": "{{ $steps.verify_format.error_count }}",
        "exit_code": "{{ $steps.test_code.exit_code }}",
        "line_count": "{{ $steps.verify_format.line_count }}"
      }
    },
    {
      "id": "log_call",
      "description": "记录调用日志",
      "operator": "manager",
      "handler": "call_log",
      "params": {
        "work_name": "edit_code",
        "stage": "executed",
        "status": "{{ $steps.verify_format.valid && $steps.test_code.exit_code === 0 ? 'success' : 'fail' }}",
        "input_preview": { "$expr": "String($input.code).slice(0, 80)" },
        "last_error": "{{ $last_error }}"
      }
    }
  ]
}
