# dsh-csv-and-image-preview

English | [中文](README.md)

Preview images, SVG and CSV/TSV in DeepSeek Harness chat using native `<img>` and `<table>` elements. Tables have sticky headers, 320px internal scrolling and sortable columns.

## 0.1.3: dedicated inline preview rows

Version 0.1.3 registers dedicated keyed Chat nodes after turn/end, outside the collapsed process interval, preserving DSH's existing file cards. Native and nested preview calls and legacy metadata are replayed. Refreshing or reopening history reloads saved snapshots. Files presented only through DSH's `present` tool are outside this feature.

Run `npm test`; set `DSH_UPSTREAM` to a DSH source checkout to include the real conversation assembler integration. `npm run preview:test` provides a browser fixture with a completion button and refresh verification.

## Install and upgrade

Version 0.1.3 is tested with **DSH 0.1.5-rc.1** and requires its Connection RPC interface or a compatible newer version.

```sh
# Install the published version
dsh plugin --profile web add @jetecho/dsh-csv-and-image-preview

# Build and install this checkout without publishing to npm
npm ci
npm test
npm pack
dsh plugin --profile web add ./jetecho-dsh-csv-and-image-preview-0.1.3.tgz
```

Restart `dsh web` and refresh the browser after installation. Package name, bundle row ID and tool names are unchanged.

## Compatibility fixes

The plugin registers only the exact `/api/csv-and-image-preview/get` route, preserving the shared `/api` interceptor owned by DSH's Gateway so history and plugin lists load normally. Upgrade, restart DSH and refresh the page; the fix does not modify session files.

- Explicit client dependencies on `slots` and `connection`, and host dependencies on `tools` and `connection`. Late service startup parks the plugin; unloading cleans up registrations.
- **Native calls and `run_code` child calls both work.** Current DSH computes `presentationMeta` only for top-level calls. New previews use a separate snapshot channel.
- Inputs are read and parsed once, then saved before success is returned. Both the model and code program receive only a string summary, with no additional image bytes or table body.
- Separate generating, loading, tool failure, missing snapshot and image decode failure states. Includes retry, reconnection refresh and cancellation of stale requests when navigating.
- Legacy `block.meta.src` and `block.meta.rows` still render. Old nested calls that never persisted presentation data must be previewed again.

## Usage

```text
preview_image {"path":"D:/assets/logo.svg","label":"Logo preview"}
preview_csv {"path":"D:/data/sales.csv","label":"Sales","maxRows":100}
preview_csv {"content":"Name,Score\nAlice,90\nBob,85"}
```

Inline SVG text can be supplied as `content`. Relative paths resolve against the session working directory. `path` takes precedence over `content`.

Images support SVG, PNG, JPEG, GIF, WebP, BMP, ICO and AVIF. Inline non-SVG content requires standard base64 and a `mime` value. Image and CSV inputs are capped at 8 MiB; the inline base64 limit applies to encoded input size.

CSV files require UTF-8 (BOM supported); other encodings produce an explicit conversion message. Delimiter detection supports comma, semicolon, Tab and pipe, with a `delimiter` override; `.tsv` defaults to Tab. Defaults: 50 rows; limits: 500 rows including the header, 40 columns and 200 characters plus an ellipsis per cell. Hover shows retained text only; discarded text is not recoverable. Headers cycle ascending, descending and original order, with empty values last.

## Storage and replay

Snapshots live in `$DSH_HOME/storages/csv-and-image-preview/`, defaulting to `~/.dsh/storages/csv-and-image-preview/`. Records are keyed by session ID, call ID and tool name using hashed filenames. Reads use DSH Connection's authenticated `/api` RPC channel and verify record identity.

The same session can replay the original snapshot after refreshing, restarting DSH or changing/deleting the source file. Back up this directory alongside session data. Snapshots are not automatically pruned; deleting them removes new-format historical previews. Copying/forking into another session does not automatically migrate snapshots.

The returned “preview data generated” summary acknowledges persistence, not successful browser decoding. Tool errors are reported as real failures.

## Preview-first convention

Preview generated or modified images/SVG/CSV before changing target files, then wait for user approval. Previewing itself writes a snapshot in the plugin data directory. This is an agent convention, not an enforcement mechanism against other write tools. Add [Agent.md](Agent.md) to your agent instructions if desired.

## Development

```sh
npm ci
npm test              # Cordis / DSH dispatch, persistence and React regression tests
npm run preview:test  # Local browser verification page; prints its URL
npm run build         # lib/ is publishable JavaScript
npm pack --dry-run
```

- `lib/index.js`: tool definitions and input parsing.
- `lib/store.js`: atomic snapshots, bounded reads and authenticated channel handler.
- `lib/client.js`: toolviews, RPC reads, status/retry and legacy metadata support.
- `cordis.patch.yml`: Profile Bundle insertion.

MIT © [jetecho](LICENSE).
