@particle-academy/fancy-chat-js 0.0.0-stage → 0.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AGENTS.md ADDED
@@ -0,0 +1,56 @@
1
+ # Using @particle-academy/fancy-chat-js — guide for agents
2
+
3
+ How an agent installs and uses this package in a consumer app. What the package
4
+ is, is in [README.md](./README.md). Notes for working ON this repo's code are in
5
+ [CONTRIBUTING.md](./CONTRIBUTING.md).
6
+
7
+ ## Install
8
+
9
+ **Not yet published to npm.** Until then, install from git over HTTPS, pinned to a
10
+ commit:
11
+
12
+ ```bash
13
+ npm install "git+https://github.com/Particle-Academy/fancy-chat-js.git#<sha>"
14
+ ```
15
+
16
+ `dist/` is committed, so a git install builds nothing. After installing,
17
+ `npx fancy-chat-check-install` checks every `@particle-academy` package's entry
18
+ files. Node `>=22`. Zero runtime dependencies.
19
+
20
+ ## Find it on the Fancy MCP server
21
+
22
+ The catalog server is `https://ui.particle.academy/mcp` (streamable HTTP, no API
23
+ key; MCP id `fancy-ui`). This package is not yet listed there; it appears once
24
+ published. Find it then with `search-backend-packages` (stack `node`) or
25
+ `search-components`.
26
+
27
+ ## API essentials
28
+
29
+ - **Replay:** `createSeqLog()` + `ingestEvents(log, batch)` drop duplicates, hold
30
+ events past a gap and report it (`r.missing.after`), so the transport re-requests
31
+ from `lastSeq`.
32
+ - **Projection:** `emptyThreadState()`, `applyEvent(state, event)`,
33
+ `projectThread(events)`, `rowsOf(state)`. The state is plain JSON and is the
34
+ snapshot format.
35
+ - **Validation:** `validateEvent`, `validatePart`, `partToText`,
36
+ `createPartRegistry`. Apps add their own event types as `x-<app>.<name>`.
37
+ - **Machines:** `transitionTurn`, `transitionDelivery`, `transitionApproval`,
38
+ driven by `contract/machines.json` (exported as
39
+ `@particle-academy/fancy-chat-js/contract/machines.json`).
40
+ - **HITL:** `canDecide`, `canSubmitAnswer`, `isReservedToolName`, `pickActionable`,
41
+ `optionTone`. **Status:** `deriveAgentStatus(snapshot, config)`.
42
+ - **Opt-in helpers:** `matchTypedApproval` (off, no phrases by default),
43
+ `undoWindow`, `checkPlanUpdate`.
44
+
45
+ The usage example in the README shows the ingest, apply and render loop.
46
+
47
+ ## Traps
48
+
49
+ - **Only `turn.settled` settles a turn.** Never infer a settled turn from rows.
50
+ - **A decision or answer counts only from a human.** `approval.decided` and
51
+ `question.answered` from any other actor are counted as illegal and change
52
+ nothing.
53
+ - **Approval and question parts come only from server records.** An agent-origin
54
+ part of either kind fails validation, and so do raw ECharts options: an agent may
55
+ only send the neutral chart spec.
56
+ - `seq` is per thread, monotonic and gapless, and is the only ordering.
package/CHANGELOG.md ADDED
@@ -0,0 +1,140 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here, in
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) format.
5
+
6
+ **This package is pre-1.0, so breaking changes land in MINOR releases.** The
7
+ version number is not a stability promise until 1.0; read the entry.
8
+
9
+ ## [Unreleased]
10
+
11
+ ### Changed
12
+
13
+ - **Docs:** `AGENTS.md` is now the guide to using this package (install, how to find it on the Fancy MCP catalog server once it is listed, API essentials, traps), and it ships in the package. The contributor notes it held moved to `CONTRIBUTING.md`, and `CLAUDE.md` is now a symlink to it. The README points to the MCP server and to `AGENTS.md`. No code changed; nothing to do.
14
+
15
+ ## [0.2.0] - 2026-11-16
16
+
17
+ Never published; 0.2.0 because an export path is removed.
18
+
19
+ ### Changed
20
+
21
+ - **Reframed: built on Prism, not provider-neutral.** The owner's ruling on
22
+ CC-fancy-0042 was misread as "no Prism in the core". It meant the extras
23
+ (prism-graph, prism-memory, the browser) are optional; the chat itself is
24
+ built on Prism Harness (owner, 2026-11-16). This package is the thread log a
25
+ server projects Harness runs into, and the one contract every server
26
+ implements. The code is unchanged: it never read a Prism type, so there was
27
+ nothing to swap for `@particle-academy/prism-harness`'s. It keeps zero runtime
28
+ dependencies, because the UI reads it in the browser. Nothing to do.
29
+
30
+ ### Removed
31
+
32
+ - **BREAKING: `./scripts/check-no-prism.mjs`, its export path, `npm run
33
+ check:no-prism`, its packaging fixtures and its CI step.** The rule it enforced
34
+ is withdrawn. If a package called it (fancy-chat-ui 0.1 did), drop the call.
35
+
36
+ ## [0.1.0] - 2026-11-16
37
+
38
+ ### Fixed
39
+
40
+ - **`applyEvent` never throws.** A `plan.updated` with a `null` step threw from
41
+ `checkPlanUpdate` (`step.status` on null). A throw escapes into the client's
42
+ transport, which reads it as a failed request and retries the same batch, so
43
+ one bad event stalled an open thread for good while every poll received it.
44
+ `checkPlanUpdate` now refuses a step that is not an object (`plan.step`, as
45
+ fancy-chat-php already did), and `applyEvent` is total: anything else it cannot
46
+ read is counted illegal and the cursor moves past it, as for an illegal
47
+ transition. Nothing to do.
48
+
49
+ ### Changed
50
+
51
+ - **Conformance fixture: three new cases, re-pinned in both legs.** A malformed
52
+ plan step projects as illegal with the cursor advanced, and `planStep` refuses
53
+ a null or non-object step. fancy-chat-php passes them unchanged. The SHA-256 is
54
+ now `295439897db175e8040f4a48effd5f55a18817aac342d2d39335dc030c39040b`.
55
+ - **`dist/` is committed and there is no `prepare`.** A git install now builds
56
+ nothing and installs no devDependencies, so it is quick and unaffected by
57
+ `ignore-scripts`. Before, every git install ran `prepare`, which installs the
58
+ package's devDependencies and builds it, and a sibling whose devDependencies are git
59
+ pins nested those builds (impactium measured 20+ minutes). CI checks the committed
60
+ `dist/` is the build of the commit. Nothing to do; reinstall to pick it up.
61
+
62
+ ### Added
63
+
64
+ - **A turn links to the message that started it.** `turn.started` may carry
65
+ `data.messageId`; when that message is in the log, `TurnEntry.messageId` names it
66
+ and the message's `turnId` is set (unless the message already named a turn).
67
+ `TurnEntry` gains `messageId` (null when no message started it), so a host can
68
+ retry a failed turn with its original input. Fixture `38c2c9b3...`; fancy-chat-php
69
+ takes it.
70
+ - **Several thinking blocks per run, in order.** A run that thinks, calls a tool
71
+ and thinks again now projects two thinking rows around the tool row, instead of
72
+ one block merged above it. Each `ThinkingEntry` has a `thinkingId`: the event's
73
+ `data.thinkingId` when it sends one, else the run id for the first block and
74
+ `<runId>#2`, `#3`... after. Without an explicit id, a `thinking.started` or
75
+ `thinking.delta` after the run's block completed opens the next block. A
76
+ non-string `thinkingId` is illegal. Rows key thinking by `thinkingId`.
77
+ **Changed shape:** the first block's id is still the run id, so a host that
78
+ looked a block up by run id still finds the first one; a host that relied on one
79
+ merged block per run now sees the later ones as their own rows.
80
+ - **`durationExact` on a tool row.** `false` when the backend could only bound the
81
+ duration (`tool.result` `durationExact: false`), so a UI can show "at most";
82
+ anything else, including absence, is `true`.
83
+ - **`fancy-chat-check-install`** (a bin, committed source, needs no build). A git
84
+ install under `ignore-scripts=true` skips `prepare` and arrives with no `dist`;
85
+ no postinstall can warn, because `ignore-scripts` skips that too. Run
86
+ `npx fancy-chat-check-install` from the host's root: it checks every installed
87
+ `@particle-academy` package's `main`, `module` and `exports` files, names each
88
+ missing one with the cause, and exits 1. Finding no packages fails too.
89
+ - Fixture `7d382bf7...`; fancy-chat-php takes it.
90
+
91
+ - **First release: the neutral agent-chat wire contract v1** (council change
92
+ CC-fancy-0042). Nothing to migrate; this is new.
93
+ - The v1 event envelope with a closed core type set and `x-<app>.<name>`
94
+ extensions; an unknown major is refused and unknown fields are ignored.
95
+ - Resumable replay on the client: `createSeqLog` / `ingestEvents` drop
96
+ duplicates, hold events past a gap and report it, and honour
97
+ `thread.reset`.
98
+ - Typed message parts (`text`, `code`, `table`, `chart`, `file`, `image`,
99
+ `tool-call`, `tool-result`, `card`, `question`, `approval`) with an app part
100
+ registry, plain-text forms and configurable size, count and depth caps.
101
+ Charts take the neutral spec; raw ECharts options are accepted only from
102
+ server code.
103
+ - The turn, delivery and approval machines in one shared table,
104
+ `contract/machines.json`.
105
+ - HITL helpers: human-only `canDecide`, reserved confirmation tool names,
106
+ `canSubmitAnswer`, single-actionable `pickActionable`, and button tone from
107
+ the option kind.
108
+ - `deriveAgentStatus`, one pure status derivation with host-overridable
109
+ recovery actions.
110
+ - `projectThread` / `applyEvent`: a pure, idempotent projection into rows.
111
+ Decisions and answers count only from a human actor.
112
+ - The shared conformance fixture `conformance/chat-v1.json`, which
113
+ `particle-academy/fancy-chat-php` passes to the same output.
114
+ - A packaging check that fails on any Prism dependency, import or bundled
115
+ module, with a negative control that proves it fires.
116
+ - Typed approval (`matchTypedApproval`), opt-in per app: off and with no
117
+ phrases until the host sets them. A typed phrase picks a one-time option
118
+ only.
119
+ - The undo window (`undoWindow`, and `undo.*` rows in the projection). Only
120
+ a human reverts, and a window closes once.
121
+ - Plan-step HITL (`checkPlanUpdate`): a held step releases only through its
122
+ approval, and the projection refuses any other release.
123
+ - Edit and resend: the projection records each superseded branch in
124
+ `state.branches` for the host's policy (branch by default, or truncate).
125
+ - `isTimestamp`, the one timestamp check every `at` and deadline uses.
126
+ - The `decided` delivery state: the server took the message as a typed
127
+ decision (`sent` or `queued` --decide--> `decided`, final), so it was never
128
+ delivered to the agent.
129
+ - `turnsToCancelForBranch(state, supersededId)`: the unsettled turns a
130
+ truncated branch must cancel, through its messages or its approvals.
131
+ `ApprovalRecord` gains an optional `turnId`.
132
+ - Tool output inline: `tool.result` may carry `output` (the result text, or a
133
+ failure's own words) and `outputLanguage` (code, e.g. `json`). The projection
134
+ keeps both on the tool row (`null` when absent or not text). It is plain text:
135
+ render it as text or code, never HTML. The server bounds it and sets
136
+ `truncated`; `outputRef` points at the rest.
137
+ - Installable from git: `prepare` builds `dist/`, so
138
+ `npm install github:Particle-Academy/fancy-chat-js#<sha>` works before a release. It ships as
139
+ `@particle-academy/fancy-chat-js/scripts/check-no-prism.mjs`, so every other
140
+ core chat package runs this one checker rather than a copy.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Particle Academy
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,94 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
1
+ # @particle-academy/fancy-chat-js
2
+
3
+ The wire contract of the Fancy agent chat (council change CC-fancy-0042), which is
4
+ built on the Prism ecosystem. A chat turn runs on Prism Harness, and the server
5
+ projects the run (Harness's `g282.1` RunSteps and its run events) into this thread
6
+ log. A server stores and replays the log, and a chat UI projects it into rows. Zero
7
+ runtime dependencies and isomorphic (browser, worker, Node), because the UI reads it
8
+ in the browser.
9
+
10
+ It holds no copy of a Prism type. It never reads a RunStep: the server maps them
11
+ (`FancyChat\Prism\HarnessBackend` in fancy-chat-php), and the types those come in
12
+ are `@particle-academy/prism-harness`'s. prism-graph, prism-memory and the other
13
+ extras are optional, and nothing here needs them.
14
+
15
+ This package is the ONE contract every server implements. The PHP server,
16
+ `particle-academy/fancy-chat-php`, passes the same conformance fixture (`conformance/chat-v1.json`) to the same output, and vendors the same
17
+ transition table (`contract/machines.json`). Both files are pinned by SHA-256 in
18
+ both repositories. A Node server twin, on `@particle-academy/prism-harness` and
19
+ `@particle-academy/prism-acp`, is next and will run the same fixtures.
20
+
21
+ **Not yet published.** Design: `.ai/plans/agent-chat-on-prism.md` in the Fancy envelope.
22
+
23
+ <!-- fancy-agents-note:start -->
24
+ > **For agents.** This package is not listed on the Fancy MCP catalog server
25
+ > yet. The server, `https://ui.particle.academy/mcp` (streamable HTTP, no API key; MCP id
26
+ > `fancy-ui`), lists the released Fancy packages with install commands and
27
+ > docs. How an agent installs and uses this package is in [AGENTS.md](https://github.com/Particle-Academy/fancy-chat-js/blob/main/AGENTS.md).
28
+ <!-- fancy-agents-note:end -->
29
+
30
+ ## What it holds
31
+
32
+ | Module | What |
33
+ |---|---|
34
+ | Events | v1 envelope (`validateEvent`): `seq` is per thread, monotonic and gapless, and is the only ordering. Core types are a closed set; apps add `x-<app>.<name>`. An unknown major is refused, unknown fields are ignored. |
35
+ | Resumable replay | `createSeqLog` / `ingestEvents`: drop duplicates, hold events past a gap and report it, so the transport re-requests `after = lastSeq`. `thread.reset` jumps the cursor. A missed broadcast costs nothing. |
36
+ | Parts | `validatePart`, `partToText`, `createPartRegistry`: `text`, `code`, `table`, `chart`, `file`, `image`, `tool-call`, `tool-result`, `card`, `question`, `approval`, plus app kinds. Size, count and depth caps are configurable. |
37
+ | Turn machine | `transitionTurn`, from `contract/machines.json`. Only `turn.settled` settles a turn; no client infers it from rows. |
38
+ | Delivery | `transitionDelivery`: `sending`, `sent`, `queued`, `steered`, `delivered`, `failed`, `dropped`, `unsent`. |
39
+ | HITL | `transitionApproval`, `optionTone` (tone from the option KIND, never its label), `canDecide` (humans only), `isReservedToolName`, `canSubmitAnswer`, `pickActionable` (one actionable item, in-flight lock). |
40
+ | Status | `deriveAgentStatus(snapshot, config)`: one pure derivation of `not-started`, `starting`, `working`, `cooling`, `idle`, `sleeping`, `parked`, `paused-limit`, `signed-out`, `broken`, each with recovery action ids the host can override. |
41
+ | Typed approval | `matchTypedApproval(text, approval, config)`: OPT-IN per app, off with no phrases by default (`ENGLISH_TYPED_APPROVAL_PHRASES` is there to opt into). Exact phrases after NFKC, case, spacing and trailing-punctuation folding; picks a one-time option only, never "always", never edit. |
42
+ | Undo window | `undoWindow(entry, now)`: time left and whether a revert may still land. Shut at the deadline and on an unreadable one. The projection lets only a human revert. |
43
+ | Plan-step HITL | `checkPlanUpdate(prev, next, approvals)`: a held step releases only through its approval; refused approvals allow only skipped or failed. The projection refuses any other release. |
44
+ | Edit and resend | The projection records each superseded branch in `state.branches`; the HOST's policy decides (branch, the default, keeps it; truncate retracts it on the server). |
45
+ | Projection | `projectThread(events)` / `applyEvent(state, event)`: messages, one merged thinking block per run, tool rows, plans and HITL placements. The state is plain JSON and is the snapshot format. |
46
+
47
+ ## Two rules the projection enforces
48
+
49
+ - **A decision or answer counts only from a human.** `approval.decided` and
50
+ `question.answered` are applied only when the event's actor is a human and the
51
+ recorded decider is that same human. Anything else is counted as illegal and
52
+ changes nothing.
53
+ - **Approval and question parts come only from server records.** An agent-origin
54
+ part of either kind fails validation, and so do raw ECharts options: an agent
55
+ may only send the neutral chart spec.
56
+
57
+ ## Use
58
+
59
+ ```ts
60
+ import { createSeqLog, ingestEvents, emptyThreadState, applyEvent, rowsOf } from "@particle-academy/fancy-chat-js";
61
+
62
+ let log = createSeqLog();
63
+ const state = emptyThreadState();
64
+
65
+ function onEvents(batch) {
66
+ const r = ingestEvents(log, batch);
67
+ log = r.log;
68
+ for (const e of r.applied) applyEvent(state, e);
69
+ if (r.missing) refetch({ after: r.missing.after });
70
+ render(rowsOf(state));
71
+ }
72
+ ```
73
+
74
+ ## Installing from git
75
+
76
+ Until the first npm publish, install from git over HTTPS, pinned to a commit
77
+ (`git+https://github.com/Particle-Academy/fancy-chat-js.git#<sha>`). **`dist/` is
78
+ committed**, so a git install builds nothing: there is no `prepare`, npm installs none
79
+ of the devDependencies, and `ignore-scripts=true` changes nothing. CI fails a commit
80
+ whose `dist/` is not the build of that commit. `npx fancy-chat-check-install` still
81
+ checks every `@particle-academy` package's entry files after an install. npm records
82
+ the dependency in the lockfile as `git+ssh://git@github.com/...`; a machine without a
83
+ GitHub key still installs, over HTTPS, because the repository is public.
84
+
85
+ ## Develop
86
+
87
+ ```bash
88
+ npm install
89
+ npm test # vitest: the conformance fixture, resume, parts, packaging (zero deps)
90
+ npm run lint # tsc --noEmit
91
+ npm run sabotage # breaks one clause at a time; the suite must notice each
92
+ ```
93
+
94
+ MIT licence.