# Workpiece

A workpiece is a folder that holds a `pointer.json`. Stations pass it from bin to bin whole. See [Workpieces](/docs/method/workpieces).

## The `wp` object

`ctx.eachWorkpiece` and `ctx.seed` call your function with `wp`:

| Member | Type | What it is |
|---|---|---|
| `id` | string | The workpiece's id, its folder name |
| `dir` | string | The folder `fn` works in: the copy in `doing/` for `eachWorkpiece`, the hidden `output/.<id>/` for `seed` |
| `readText(name)` | `string` | Read a file in the folder, as UTF-8 |
| `readJson(name)` | any | Read and parse a JSON file in the folder |
| `writeText(name, text)` | | Write a file in the folder |
| `writeJson(name, value)` | | Write `value` as JSON, indented two spaces |
| `log(event, details)` | | Append an event to the folder's `log.jsonl`. `details` is optional. Never throws: a failed write prints a warning. |

`name` is relative to `wp.dir`. The read and write calls are synchronous and throw as `fs.readFileSync` and `fs.writeFileSync` do. For types: `@param {import('@assemblyline/lib-factory').Workpiece} wp`.

## Files the lib writes in a workpiece

| File | Written | Holds |
|---|---|---|
| `pointer.json` | by `ctx.seed`, when the workpiece is created | `{ workpiece_id, dedup_key, seeded_at, ...pointer }`; `dedup_key` only when given. A station never writes it. |
| `log.jsonl` | at every event | One JSON line per event: `{ ts, station, wr, event, ...details }` |
| `error.json` | when `fn` throws | `{ failed_at, name, message, stack }` and every other own property of the error, such as `status` or `body` |
| `error.txt` | when `fn` throws | The same, for reading: a long `body` is cut to 500 characters |

The events the lib logs:

| Event | When | Details |
|---|---|---|
| `workpiece_created` | `ctx.seed` created the workpiece | `dedup_key`, when given |
| `conveyed` | the conveyor moved it into a station's `input/` | `from`; no `wr` |
| `convey_failed` | the conveyor couldn't move it; logged where it stayed | `from`, `error`; no `wr` |
| `station_started` | `eachWorkpiece` copied it into `doing/` | |
| `station_complete` | `fn` returned | `sub_bin`, when given |
| `station_failed` | `fn` threw | |

## `StopError`

```js
import { StopError } from '@assemblyline/lib-factory';
throw new StopError('ledger_auth: the API key has expired');
```

Throw it from the function given to `eachWorkpiece` or `seed` when the station itself is broken, not the workpiece. The workpiece stays where it was, the batch stops, and the run fails as retryable: see [Station context](/docs/reference/station-context) and [Throw errors from a station](/docs/guides/station-errors).

## `listWorkpieces(bin_dir)`

Returns the names of the workpieces directly in `bin_dir`, sorted: folders that hold a `pointer.json` and don't start with `.`. Returns `[]` when the folder doesn't exist. Sub-bins hold no `pointer.json`, so they aren't listed.

```js
listWorkpieces(bin('IN1', 'failed'));   // ['inv-1001', 'inv-1004']
```

## `logEvent(dir, station, work_record_id, event, details)`

Appends `{ ts, station, wr: work_record_id, event, ...details }` to `<dir>/log.jsonl`, for a workpiece you hold outside `fn`. Inside `fn`, use `wp.log(event, details)`. Like `wp.log`, it never throws.
