domaintrail 0.5.0 → 0.6.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/README.md CHANGED
@@ -1,209 +1,93 @@
1
1
  # DomainTrail
2
2
 
3
3
  A type-checkable domain modeling DSL shared by humans and AI.
4
- AI agents edit `.dt` files; humans watch the model update live in the browser and give feedback.
5
- The DSL is the single source of truth for domain **structure and contracts** — it generates TypeScript types, command
6
- contracts, checks of the invariants and requirements it states, and contract tests for your implementation, never
7
- business logic.
8
4
 
9
- ## Setup
5
+ DomainTrail models are `.dt` files that state a domain's **structure and contracts** — bounded contexts, aggregates
6
+ and their states, commands with what they require, change and emit, invariants — and are their single source of truth.
7
+ AI agents write the model and implement it; people watch it update live in the browser and give feedback on the model
8
+ rather than on the code. From the model, `dt` generates TypeScript types, command contracts, checks of the invariants
9
+ and requirements, and contract tests for your implementation — never business logic, which is written against the
10
+ generated types and held to the model by the compiler and the tests.
10
11
 
11
- Requires Node.js 22.12 or later.
12
-
13
- ```bash
14
- npm install -D domaintrail # then: npx dt <command>
15
- npx domaintrail <command> # or run it without installing
16
- ```
17
-
18
- ## CLI
19
-
20
- ```bash
21
- dt guide [topic] # quick reference and topics; one topic (dt guide requires); --all for everything
22
- dt -r <root> fmt # format .dt files in place in the canonical layout (--check for CI)
23
- dt -r <root> check # validate syntax, references, types and DDD rules
24
- dt -r <root> generate ts # generate TypeScript into <root>/generated/domain (--out to change)
25
- dt -r <root> view # Live View at http://127.0.0.1:4321 (--port, --host); runs until Ctrl+C
26
- dt lsp # language server over stdio: dt check's diagnostics, definitions, references, hover
27
- dt -r <root> show <name> # one element resolved: field sets, labels, commands' contracts, who uses it
28
- ```
29
-
30
- The package installs the same command under two names: `dt` for everyday use and `domaintrail`, its full name, for
31
- when `dt` clashes with another command or a script should spell it out (CI, for example).
32
-
33
- `<root>` defaults to the current directory; every `*.dt` file below it (except in `node_modules`, `.git`, `.domaintrail`
34
- and what `.gitignore` ignores) belongs to the workspace, unless a `domaintrail.jsonc` says otherwise. `dt --help` and `dt <command> --help` show the options with examples.
35
-
36
- `check` prints diagnostics as `file:line:col - error M00X: message` (with a `hint:` line on what to change, when there is
37
- one) and exits non-zero on errors; `check --format json` prints them as one JSON document (`schema/diagnostics.schema.json`).
38
- `generate ts` runs `check` first and generates nothing if there are errors.
12
+ ## Quick start
39
13
 
40
- Try it with the example in this repository (see [Development](#development)). [examples/shop](examples/shop/README.md)
41
- is a complete one: the model, the generated code, and the code an AI agent writes against it — the domain
42
- implementations and a small application layer.
43
-
44
- ```bash
45
- bun run dt -r examples/shop check
46
- bun run dt -r examples/shop generate ts # then examples/shop/src type-checks against it
47
- (cd examples/shop && bun test) # passes the generated contract tests and the application's tests
48
- (cd examples/shop && bun src/demo.ts) # a walk through the shop: orders, an invoice, a refund request
49
- bun run build && bun run dt -r examples/shop view
50
- ```
51
-
52
- ## Guide
53
-
54
- **[docs/guide.md](docs/guide.md)** is the reference for writing models and using the CLI. It starts with a
55
- [quick reference](docs/guide.md#quick-reference) (the workflow, the DSL at a glance, the commands), and its sections are
56
- **topics** that the CLI prints one at a time, so people and AI agents read only what they need without leaving the
57
- terminal:
14
+ Requires Node.js 22.12 or later.
58
15
 
59
16
  ```bash
60
- dt guide # the quick reference and the list of topics (about one screen)
61
- dt guide requires # one topic: a ## section with its ### sections, or one ### section
62
- dt guide M010 # one rule and the topics that explain it
63
- dt guide --all # the whole guide, the same text as docs/guide.md
17
+ npm i -D domaintrail
64
18
  ```
65
19
 
66
- The sections, in the guide's order:
67
-
68
- - [Workflow](docs/guide.md#workflow): `fmt` → `check` → `generate ts` → implement with `decide` → type-check your own
69
- code (`tsc --noEmit`) → run the generated contract tests
70
- - [File layout (recommended)](docs/guide.md#file-layout-recommended): `<context>/<aggregate>.dt` and
71
- `<context>/context.dt` (see [ADR 0009](docs/adr/0009-file-layout.md))
72
- - [DSL](docs/guide.md#dsl): contexts, aggregates, entities, values, states, commands, events, errors,
73
- [domain primitives](docs/guide.md#domain-primitives), [unions](docs/guide.md#unions),
74
- [shared contexts](docs/guide.md#shared-contexts), [field sets](docs/guide.md#field-sets),
75
- [domain and technical fields](docs/guide.md#domain-fields-and-technical-fields),
76
- [names (表示名)](docs/guide.md#names-labels-and-descriptions),
77
- [invariant expressions](docs/guide.md#invariant-expressions), [command requirements](docs/guide.md#command-requirements),
78
- [command changes](docs/guide.md#command-changes), [command events](docs/guide.md#command-events),
79
- [pure commands](docs/guide.md#pure-commands), [verified types](docs/guide.md#verified-types) (the evidence of a
80
- check outside the aggregate, as a command's parameter) and [reactions to events](docs/guide.md#reactions-to-events-policies)
81
- (policies: when an event happens, a command eventually runs; see [ADR 0022](docs/adr/0022-policy-declarations.md))
82
- - [Generated TypeScript](docs/guide.md#generated-typescript): [decisions and `decide`](docs/guide.md#decisions-and-decide),
83
- [what is generated and what you write](docs/guide.md#generated-code-and-your-code) (see
84
- [ADR 0011](docs/adr/0011-generated-code-boundary.md)), [generated checks](docs/guide.md#generated-checks) and
85
- [generated tests](docs/guide.md#generated-tests) (see [ADR 0020](docs/adr/0020-generated-checks-and-contract-tests.md)),
86
- [technical metadata](docs/guide.md#technical-metadata) (see [ADR 0013](docs/adr/0013-field-sets-and-technical-metadata.md))
87
- and [output directories](docs/guide.md#output-directories)
88
- - [Rules](docs/guide.md#rules): M001–M024, L001–L003
89
- - [Looking up one element](docs/guide.md#looking-up-one-element): `dt show`, as text or JSON
90
- - [Formatting](docs/guide.md#formatting): `dt fmt`, the one canonical layout of `.dt` files
91
- - [Output and exit codes](docs/guide.md#output-and-exit-codes): text, or JSON with `--format json`
92
- - [Configuration](docs/guide.md#configuration): `domaintrail.jsonc` (`sources`, `generate.ts.out` /
93
- `generate.ts.contexts` / `generate.ts.tests`, `view.port` / `view.host`; JSON Schema in `schema/domaintrail.schema.json`)
94
- - [Language server](docs/guide.md#language-server): `dt lsp`, and how to use it from an editor or Claude Code
95
- - [Editors](docs/guide.md#editors): the Zed extension in `editors/zed` (tree-sitter highlighting, outline, `dt lsp`),
96
- other editors through `dt lsp` and the tree-sitter grammar in `tree-sitter-domaintrail`
97
-
98
- A taste of the DSL:
20
+ Write a model, e.g. `booking/reservation.dt`:
99
21
 
100
22
  ```dt
101
- context Sales "販売" {
102
- id OrderId: UUID
103
- aggregate Order "注文" {
104
- id: OrderId
105
- state Draft
23
+ context Booking "会議室予約" {
24
+ id ReservationId: UUID
25
+ primitive Headcount "人数": Int { value in 1..50 }
26
+
27
+ aggregate Reservation "予約" {
28
+ id: ReservationId
29
+ attendees: Headcount
30
+ state Requested
106
31
  state Confirmed {
107
32
  confirmedAt: Timestamp
108
33
  }
109
- create command place(id: OrderId) -> Draft
110
- command confirm(at: Timestamp)
111
- Draft -> Confirmed
112
- emits OrderConfirmed
34
+
35
+ create command request(id: ReservationId, attendees: Headcount) -> Requested
36
+ command confirm(capacity: Headcount, at: Timestamp)
37
+ Requested -> Confirmed
38
+ requires roomFits { attendees <= capacity } else RoomTooSmall
39
+ emits ReservationConfirmed
113
40
  }
114
- persistent event OrderConfirmed {
115
- orderId: OrderId
41
+
42
+ event ReservationConfirmed {
43
+ reservationId: ReservationId
116
44
  confirmedAt: Timestamp
117
45
  }
46
+ error RoomTooSmall
118
47
  }
119
48
  ```
120
49
 
121
- The design decisions behind the DSL are in [docs/adr](docs/adr): shared contexts ([0004](docs/adr/0004-shared-contexts.md)),
122
- unions ([0005](docs/adr/0005-unions.md)), persistent events ([0008](docs/adr/0008-persistent-events.md)),
123
- command transitions ([0014](docs/adr/0014-command-transitions.md)), handlers per state
124
- ([0015](docs/adr/0015-handlers-per-state.md), superseded), collection constraints and declared invariants
125
- ([0016](docs/adr/0016-collection-constraints-and-declared-invariants.md)), command requirements
126
- ([0017](docs/adr/0017-command-requirements.md)), command events as an exact contract
127
- ([0018](docs/adr/0018-command-events-contract.md)), the fields a command changes and decisions
128
- ([0019](docs/adr/0019-command-changes-and-decisions.md)), generated checks and contract tests
129
- ([0020](docs/adr/0020-generated-checks-and-contract-tests.md)), event and error fields named after their source
130
- ([0021](docs/adr/0021-payload-field-names.md)), policy declarations ([0022](docs/adr/0022-policy-declarations.md)),
131
- verified types ([0023](docs/adr/0023-verified-types.md)) and more.
132
-
133
- ## Live View
134
-
135
- `dt view` watches the workspace, re-validates on every save and pushes updates over WebSocket.
136
- The screen is laid out like an IDE; every pane is closed and opened with the icon button at its top (a closed side
137
- pane leaves a narrow rail with the button that opens it) and resized by dragging (remembered per browser).
138
-
139
- - **Explorer (left)**: model tree — shared contexts first, then each context grouped by kind; aggregates expand to
140
- their states and commands — and the `.dt` files with error/warning counts. Click selects (a file too);
141
- double-click opens the item in the view or inspector tab that suits it.
142
- - **Main (center)**: views of the whole model, in tabs with a view-specific toolbar. Anything about one selected item
143
- is in the inspector. See [ADR 0007](docs/adr/0007-pane-roles.md).
144
- - *Context Map*: one node per bounded context and one edge per dependency between contexts, labeled with the
145
- number of ID references and borrowed types (solid when types are borrowed). Shared contexts and contexts that
146
- only declare IDs are hidden unless 「共有・外部の BC も表示」 is checked. Selecting a context lists every use in
147
- the inspector (依存先 / 依存元). See [ADR 0006](docs/adr/0006-context-map.md).
148
- - *Structure*: contexts, aggregate boundaries, entities, value objects, unions, `owns` / `references`. Pick bounded
149
- contexts in the toolbar (any number) to draw only them, with the elements they use and the elements that use them
150
- as stubs; the eye next to each other context hides its stubs and the edges to and from them (the toolbar shows how
151
- many are hidden). The first row, 「すべての BC」, checks or unchecks every context and shows or hides every stub.
152
- Three levels work in both, switched by the icons in the toolbar: 詳細 (every field), 概観 (the elements' names
153
- only) and 集約 (only the aggregates with their descriptions, and one edge per pair of aggregates that depend on
154
- each other, also through the entities, value objects and IDs they hold; shared contexts are left out, and a
155
- context without aggregates stays as a frame with its description).
156
- - *Catalog*: cards to look over the model, one section per kind; for now the domain primitives per context (shared
157
- first) with base type, invariants and usages.
158
- - **Inspector (right)**: the selected item — name, description, location (copy `file:line-line` for AI feedback),
159
- diagnostics, a back button naming the item it returns to (「〇〇 に戻る」), and buttons to show it in a view. Sub-tabs: 構造 / 状態遷移 / 振る舞い / 参照 /
160
- ソース. An event shows 永続化 / 非永続 next to its kind, as it does in an aggregate's 発生させる Event list;
161
- persistent events have a database icon in the explorer.
162
- - *状態遷移*: states and command transitions of the aggregate (`new` marks factory commands), for the aggregate
163
- and its states and commands; it pans and zooms like the main canvas and shows the selected state or command.
164
- - *ソース*: the whole file, read-only, syntax-coloured, diagnostics in the gutter, scrolled to the declaration;
165
- clicking a declaration selects it. A selected file shows here the same way.
166
- - **Problems / changes (bottom)**: diagnostics (click to locate) and additions / changes / removals since the previous model.
167
- - Selecting never switches the main view by itself; the item is highlighted when the current view shows it.
168
- - Added / changed elements are highlighted; removed ones appear as ghosts. On a syntax error the last diagram stays.
169
- - The language of the Live View (Japanese or English) follows the browser, or is forced in the settings menu (the gear
170
- at the right of the header). It covers the screen and the diagrams' own words (集約 / aggregate…); the model's names
171
- and labels are shown as written, and diagnostics are in English.
172
- - The theme follows the OS (自動), or is forced with ライト / ダーク in the settings menu; both choices are remembered
173
- per browser.
174
- - Names follow the 名前の表示 toggle in the header: ラベル (the string after a declaration's name, falling back to the
175
- name), 名前 or 両方. Layouts are cached in `<root>/.domaintrail/layout.json`. The canvas is read-only.
176
-
177
- See [docs/adr/0001-mvp-implementation-decisions.md](docs/adr/0001-mvp-implementation-decisions.md) for the design decisions.
178
-
179
- ## Development
180
-
181
- Requires [Bun](https://bun.sh) (version pinned in `mise.toml`). `bun run dt` runs the CLI from source.
50
+ Check it, and watch it in the browser:
182
51
 
183
52
  ```bash
184
- bun install
185
- bun run langium:generate # after editing src/language/domain.langium
186
- bun run typecheck
187
- bun run lint # oxlint --type-aware, no narrowing casts without a reason (after `bun run dt -r examples/shop generate ts`)
188
- bun run fmt # oxfmt (`fmt:check` in CI); the .dt files are formatted by `dt fmt`
189
- bun test # the repository's tests (bunfig.toml: test/ only)
190
- bun run build # dist/: the CLI bundled for Node and the Live View page (needed by `view` and the tests)
53
+ npx dt check # 1 file(s) checked: 0 error(s), 0 warning(s).
54
+ npx dt view # the Live View at http://127.0.0.1:4321, until Ctrl+C
191
55
  ```
192
56
 
193
- ### Release
194
-
195
- Run `npm version <patch|minor|major>` and `git push --follow-tags`. The Release workflow tests, builds and stages the
196
- package on npm; it is published only after you approve it on npmjs.com (Staged Packages) or with `npm stage approve`.
197
- See [docs/releasing.md](docs/releasing.md).
198
-
199
- | Path | Contents |
200
- |---|---|
201
- | `src/language` | Langium grammar, generated parser/AST, scoping |
202
- | `src/core` | analyzer, diagnostics, validation rules (`checker/`) |
203
- | `src/model` | Typed Model (serializable, resolved) |
204
- | `src/generator` | TypeScript generator and naming, generated checks (`checks.ts`) and contract tests (`tests.ts`, `testing/`) |
205
- | `src/graph`, `src/layout` | Graph IR, diff, ELK layout |
206
- | `src/server`, `web` | Live View server and React + tldraw client |
57
+ Then `npx dt generate ts` writes the TypeScript into `generated/domain`. The package installs the command as `dt` and
58
+ as `domaintrail`, its full name, for when `dt` clashes with another command.
59
+
60
+ ## Where to read next
61
+
62
+ - **[Getting started](docs/getting-started.md)** ([日本語](docs/getting-started.ja.md)): working with an AI agent,
63
+ a first model step by step, generating, implementing and testing, and the editors.
64
+ - **[The guide](docs/guide.md)**, also printed by `dt guide` one topic at a time: the reference for the DSL and the
65
+ CLI, written for people and AI agents. Start with its [quick reference](docs/guide.md#quick-reference).
66
+ - **[examples/shop](examples/shop/README.md)**: a complete example — the model, the generated code and the code an AI
67
+ agent wrote against it, with the contract tests and a demo.
68
+ - **[The Live View](docs/live-view.md)**: every pane and view of `dt view`.
69
+ - **[Changelog](CHANGELOG.md)** ([日本語](CHANGELOG.ja.md)): what changed in each version, and how to upgrade.
70
+ - **[Design decisions](docs/adr)**: the ADRs behind the language.
71
+
72
+ ## Features
73
+
74
+ - **The DSL**: contexts and shared contexts, aggregates with states and transitions, entities, value objects, domain
75
+ primitives, unions, field sets, labels (表示名) and terms; invariants, command requirements (`requires`), the fields
76
+ a command changes, the events it emits as an exact contract, verified types and policies.
77
+ - **`dt check`**: syntax, references, types and DDD rules, with a hint on what to change; text or JSON.
78
+ - **`dt fmt`**: one canonical layout for `.dt` files, keeping every comment.
79
+ - **`dt generate ts`**: types per context, command contracts and decisions for `decide`, checks of the invariants and
80
+ requirements, constructors of domain primitives, and contract tests with fast-check (bun, vitest, jest, node).
81
+ - **`dt view`**: the Live View — context map, structure, state transitions, behaviour, source and the changes since the
82
+ last save or a git revision, in Japanese or English.
83
+ - **`dt show`**: one element resolved, for people and agents, as text or JSON.
84
+ - **`dt lsp`**: a language server — diagnostics, definitions, references, hover, rename, formatting; a
85
+ [Zed extension](editors/zed) and a [tree-sitter grammar](tree-sitter-domaintrail).
86
+ - **`dt guide`**: the guide in the terminal, one topic at a time.
87
+
88
+ ## Contributing
89
+
90
+ See [CONTRIBUTING.md](CONTRIBUTING.md) for building, testing and releasing DomainTrail itself.
207
91
 
208
92
  ## License
209
93