# Local File System

> 在宿主机上读写文件。

Source: https://docs.sommatic.ai/zh/nodes/io.local.file/

- 标识: `io.local.file`
- 类别: 输入与输出
- 版本: 1.0.0
- 状态: 可用
- 执行方式: async

在宿主机上读写文件。

## 配置

- `path` (string, 必填): 绝对路径
- `operation` (string, 必填): 默认值: `read`. 可选值: `read`, `write`, `append`, `delete`, `list`.
- `content` (string, 可选): 要写入/追加的内容
- `encoding` (string, 可选): 默认值: `utf8`.

## 输出端口

- `out` (默认): 文件系统操作成功
- `error`: 文件系统操作失败

## 输入契约

- `path` (string, 必填)
- `operation` (string, 必填)
- `content` (string, 可选)
- `encoding` (string, 可选)

## 输出契约

- `result` (string, 可选): 文件内容或操作成功状态

## 在表达式中

后续节点可通过 `$("Local File System").item.json.<路径>` 读取此节点输出中的内容（请把 `Local File System` 换成你的节点标签）。**Editor** 标记工作流编辑器在 Previous Nodes 中提示的路径（输出契约）；**Worker** 标记 worker 实际通过 `out` 返回的路径，依据“端口”一节所示的输出。

- `$("Local File System").item.json.result` (string) = `{…}`: 文件内容或操作成功状态
- `$("Local File System").item.json.result.written` (boolean) = `true` (编辑器不提示)
- `$("Local File System").item.json.result.path` (string) = `"/data/exports/orders-2026-11-25.csv"` (编辑器不提示)


## 工作原理

在运行 worker 的主机文件系统上读取、写入、追加、删除或列出指定路径。

1. 解析配置。`path`、`operation`（默认 `read`）、`content`（默认为空）和 `encoding`（默认 `utf8`）缺失时回退到输入 payload 中的同名键。
2. `path` 为空时从 `error` 失败输出。
3. 执行操作：`read` 以 `encoding` 解码并返回文件内容；`write` 用 `content` 覆盖文件；`append` 在末尾追加 `content`；`delete` 删除文件；`list` 返回目录中的条目名称。
4. 从 `out` 返回 `{ result }`；任何文件系统错误都从 `error` 输出。

## 在工作流中

### 节点

```json
{
  "node_id": "node-write-export",
  "label": "Write export",
  "operator_slug": "io.local.file",
  "operator_version": "1.0.0",
  "config": {
    "path": "/data/exports/orders-{{ $json.date }}.csv",
    "operation": "write",
    "content": "{{ $json.csv }}"
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": {
      "written": true,
      "path": "/data/exports/orders-2026-11-25.csv"
    }
  },
  "port": "out"
}
```

## 输出端口 (代码中的行为)

### `out`

操作成功。`read` 时 `result` 为文本，`list` 时为名称数组，其他操作为 `{ written | appended | deleted: true, path }`。

```json
{
  "result": {
    "written": true,
    "path": "/data/exports/orders-2026-11-25.csv"
  }
}
```

### `error`

缺少 `path`、操作未知，或文件系统拒绝了该调用。

```json
{
  "error": "ENOENT: no such file or directory, open '/data/exports/missing.csv'"
}
```

## 代码中的行为

- `encoding`: 仅对 `read` 生效。`write` 和 `append` 始终写入 UTF-8 文本。
- `path`: 相对路径以编排器进程的工作目录为基准解析。

## 错误

| 消息 | 端口 | 原因与处理 |
| --- | --- | --- |
| `path is required` | `error` | 配置和输入 payload 中都没有路径。请使用绝对路径。 |
| `Unsupported operation: <operation>` | `error` | `operation` 不是 `read`、`write`、`append`、`delete` 或 `list`。 |
| `ENOENT: no such file or directory, <syscall> '<path>'` | `error` | 主机上不存在该文件或其父目录。`write` 和 `append` 不会创建缺失的目录。 |
| `EACCES: permission denied, <syscall> '<path>'` | `error` | 编排器进程对该路径没有权限。 |

## 说明与限制

- 路径指向运行 worker 的主机或容器，其环境可能是临时的；写入的文件在重新部署后不会保留。
- `write` 会直接覆盖且不提示确认，`delete` 不可恢复。`list` 不递归。


## 契约（JSON Schema）

### All (`*.definition.js`)

```json
{
  "slug": "io.local.file",
  "name": "Local File System",
  "category": "io",
  "version": "1.0.0",
  "description": "Read/Write files on host machine.",
  "capabilities": [
    "fs",
    "local",
    "file"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "path": {
        "type": "string",
        "description": "Absolute path"
      },
      "operation": {
        "type": "string",
        "enum": [
          "read",
          "write",
          "append",
          "delete",
          "list"
        ],
        "default": "read"
      },
      "content": {
        "type": "string",
        "description": "Content to write/append"
      },
      "encoding": {
        "type": "string",
        "default": "utf8"
      }
    },
    "required": [
      "path",
      "operation"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "path": {
        "type": "string"
      },
      "operation": {
        "type": "string"
      },
      "content": {
        "type": "string"
      },
      "encoding": {
        "type": "string"
      }
    },
    "required": [
      "path",
      "operation"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "result": {
        "type": "string",
        "description": "File content or operation success status"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Result",
      "description": "FS operation succeeded",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "FS operation failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "desktop-agent",
    "local-agent"
  ],
  "tags": [
    "local",
    "files"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "path": {
      "type": "string",
      "description": "Absolute path"
    },
    "operation": {
      "type": "string",
      "enum": [
        "read",
        "write",
        "append",
        "delete",
        "list"
      ],
      "default": "read"
    },
    "content": {
      "type": "string",
      "description": "Content to write/append"
    },
    "encoding": {
      "type": "string",
      "default": "utf8"
    }
  },
  "required": [
    "path",
    "operation"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "path": {
      "type": "string"
    },
    "operation": {
      "type": "string"
    },
    "content": {
      "type": "string"
    },
    "encoding": {
      "type": "string"
    }
  },
  "required": [
    "path",
    "operation"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "result": {
      "type": "string",
      "description": "File content or operation success status"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Result",
    "description": "FS operation succeeded",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "FS operation failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
