@invompt/invoml 1.0.0-alpha.4
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/LICENSE +190 -0
- package/README.md +320 -0
- package/dist/cli/invoml.d.ts +2 -0
- package/dist/cli/invoml.js +78 -0
- package/dist/src/calculator.d.ts +3 -0
- package/dist/src/calculator.js +164 -0
- package/dist/src/discounts.d.ts +6 -0
- package/dist/src/discounts.js +46 -0
- package/dist/src/format.d.ts +3 -0
- package/dist/src/format.js +14 -0
- package/dist/src/html-css.d.ts +4 -0
- package/dist/src/html-css.js +300 -0
- package/dist/src/html-renderer.d.ts +5 -0
- package/dist/src/html-renderer.js +365 -0
- package/dist/src/index.d.ts +13 -0
- package/dist/src/index.js +8 -0
- package/dist/src/markdown.d.ts +8 -0
- package/dist/src/markdown.js +89 -0
- package/dist/src/parser.d.ts +10 -0
- package/dist/src/parser.js +16 -0
- package/dist/src/rounding.d.ts +5 -0
- package/dist/src/rounding.js +24 -0
- package/dist/src/schema.d.ts +6 -0
- package/dist/src/schema.js +33 -0
- package/dist/src/serializer.d.ts +8 -0
- package/dist/src/serializer.js +181 -0
- package/dist/src/style.d.ts +21 -0
- package/dist/src/style.js +70 -0
- package/dist/src/tax.d.ts +3 -0
- package/dist/src/tax.js +40 -0
- package/dist/src/types.d.ts +132 -0
- package/dist/src/types.js +10 -0
- package/invoml-v1.0.schema.json +258 -0
- package/package.json +65 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship made available under
|
|
36
|
+
the License, as indicated by a copyright notice that is included in
|
|
37
|
+
or attached to the work (an example is provided in the Appendix below).
|
|
38
|
+
|
|
39
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
40
|
+
form, that is based on (or derived from) the Work and for which the
|
|
41
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
42
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
43
|
+
of this License, Derivative Works shall not include works that remain
|
|
44
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
45
|
+
the Work and Derivative Works thereof.
|
|
46
|
+
|
|
47
|
+
"Contribution" shall mean, as submitted to the Licensor for inclusion
|
|
48
|
+
in the Work by the copyright owner or by an individual or Legal Entity
|
|
49
|
+
authorized to submit on behalf of the copyright owner. For the purposes
|
|
50
|
+
of this definition, "submitted" means any form of electronic, verbal,
|
|
51
|
+
or written communication sent to the Licensor or its representatives,
|
|
52
|
+
including but not limited to communication on electronic mailing lists,
|
|
53
|
+
source code control systems, and issue tracking systems that are managed
|
|
54
|
+
by, or on behalf of, the Licensor for the purpose of discussing and
|
|
55
|
+
improving the Work, but excluding communication that is conspicuously
|
|
56
|
+
marked or designated in writing by the copyright owner as "Not a
|
|
57
|
+
Contribution."
|
|
58
|
+
|
|
59
|
+
"Contributor" shall mean Licensor and any Legal Entity on behalf of
|
|
60
|
+
whom a Contribution has been received by the Licensor and included
|
|
61
|
+
within the Work.
|
|
62
|
+
|
|
63
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
64
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
65
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
66
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
67
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
68
|
+
Work and such Derivative Works in Source or Object form.
|
|
69
|
+
|
|
70
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
71
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
72
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
73
|
+
(except as stated in this section) patent license to make, have made,
|
|
74
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
75
|
+
where such license applies only to those patent claims licensable
|
|
76
|
+
by such Contributor that are necessarily infringed by their
|
|
77
|
+
Contribution(s) alone or by the combination of their Contribution(s)
|
|
78
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
79
|
+
institute patent litigation against any entity (including a cross-claim
|
|
80
|
+
or counterclaim in a lawsuit) alleging that the Work or any such
|
|
81
|
+
Contribution constitutes direct or contributory patent infringement,
|
|
82
|
+
then any patent licenses granted to You under this License for that
|
|
83
|
+
Work shall terminate as of the date such litigation is filed.
|
|
84
|
+
|
|
85
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
86
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
87
|
+
modifications, and in Source or Object form, provided that You
|
|
88
|
+
meet the following conditions:
|
|
89
|
+
|
|
90
|
+
(a) You must give any other recipients of the Work or Derivative
|
|
91
|
+
Works a copy of this License; and
|
|
92
|
+
|
|
93
|
+
(b) You must cause any modified files to carry prominent notices
|
|
94
|
+
stating that You changed the files; and
|
|
95
|
+
|
|
96
|
+
(c) You must retain, in the Source form of any Work that You
|
|
97
|
+
distribute, all copyright, patent, trademark, and attribution
|
|
98
|
+
notices from the Source form of the Work, excluding those notices
|
|
99
|
+
that do not pertain to any part of the Derivative Works; and
|
|
100
|
+
|
|
101
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
102
|
+
distribution, You must include a readable copy of the attribution
|
|
103
|
+
notices contained within such NOTICE file, in at least one of
|
|
104
|
+
the following places: within a NOTICE text file distributed as
|
|
105
|
+
part of the Derivative Works; within the Source form or
|
|
106
|
+
documentation, if provided along with the Derivative Works; or,
|
|
107
|
+
within a display generated by the Derivative Works, if and
|
|
108
|
+
wherever such third-party notices normally appear. The contents
|
|
109
|
+
of the NOTICE file are for informational purposes only and do not
|
|
110
|
+
modify the License. You may add Your own attribution notices
|
|
111
|
+
within Derivative Works that You distribute, alongside or as an
|
|
112
|
+
addendum to the NOTICE text from the Work, provided that such
|
|
113
|
+
additional attribution notices cannot be construed as modifying
|
|
114
|
+
the License.
|
|
115
|
+
|
|
116
|
+
You may add Your own license statement for Your modifications and
|
|
117
|
+
may provide additional grant of rights to use, copy, modify, merge,
|
|
118
|
+
publish, distribute, sublicense, and/or sell copies of the Work,
|
|
119
|
+
and to permit persons to whom the Work is provided to do so, subject
|
|
120
|
+
to the following terms and conditions.
|
|
121
|
+
|
|
122
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
123
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
124
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
125
|
+
this License, without any additional terms or conditions.
|
|
126
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
127
|
+
the terms of any separate license agreement you may have executed
|
|
128
|
+
with Licensor regarding such Contributions.
|
|
129
|
+
|
|
130
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
131
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
132
|
+
except as required for reasonable and customary use in describing the
|
|
133
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
134
|
+
|
|
135
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
136
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
137
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
138
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
139
|
+
implied, including, without limitation, any conditions of TITLE,
|
|
140
|
+
NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR
|
|
141
|
+
PURPOSE. You are solely responsible for determining the
|
|
142
|
+
appropriateness of using or reproducing the Work and assume any
|
|
143
|
+
risks associated with Your exercise of permissions under this License.
|
|
144
|
+
|
|
145
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
146
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
147
|
+
unless required by applicable law (such as deliberate and grossly
|
|
148
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
149
|
+
liable to You for damages, including any direct, indirect, special,
|
|
150
|
+
incidental, or exemplary damages of any character arising as a
|
|
151
|
+
result of this License or out of the use or inability to use the
|
|
152
|
+
Work (even if such Contributor has been advised of the possibility
|
|
153
|
+
of such damages).
|
|
154
|
+
|
|
155
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
156
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
157
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
158
|
+
or other liability obligations and/or rights consistent with this
|
|
159
|
+
License. However, in accepting such obligations, You may offer such
|
|
160
|
+
conditions only on Your own behalf and on Your sole responsibility,
|
|
161
|
+
not on behalf of any other Contributor, and only if You agree to
|
|
162
|
+
indemnify, defend, and hold each Contributor harmless for any
|
|
163
|
+
liability incurred by, or claims asserted against, such Contributor
|
|
164
|
+
by reason of your accepting any such warranty or additional liability.
|
|
165
|
+
|
|
166
|
+
END OF TERMS AND CONDITIONS
|
|
167
|
+
|
|
168
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
169
|
+
|
|
170
|
+
To apply the Apache License to your work, attach the following
|
|
171
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
172
|
+
replaced with your own identifying information. (Don't include
|
|
173
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
174
|
+
comment syntax for the format in question. Please also include a
|
|
175
|
+
"NOTICE" or "LICENSE" file in the project root to inform users
|
|
176
|
+
of the License terms.
|
|
177
|
+
|
|
178
|
+
Copyright 2026 Invompt
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
# InvoML — Invoice Markup Language
|
|
2
|
+
|
|
3
|
+
**A specification for AI-generated invoice documents. Deterministic math, human-readable format, international tax coverage.**
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@invompt/invoml)
|
|
6
|
+
[](./LICENSE)
|
|
7
|
+
[](https://github.com/invompt/InvoML/actions)
|
|
8
|
+
[](https://www.typescriptlang.org/)
|
|
9
|
+
[](./test-vectors/)
|
|
10
|
+
|
|
11
|
+
InvoML is a **format specification** for invoice documents designed from the ground up for AI structured output and human authoring. `@invompt/invoml` is the official TypeScript reference implementation.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## The Problem
|
|
16
|
+
|
|
17
|
+
Every time an AI model generates an invoice today, it faces the same dilemma: which format?
|
|
18
|
+
|
|
19
|
+
**Traditional business document formats were designed for enterprise systems, not AI.** They use verbose XML with deeply nested structures — a simple invoice can require hundreds of tokens of boilerplate before the first data field appears. They handle data exchange between machines but leave human presentation entirely to the renderer — with no way for the document to carry visual intent.
|
|
20
|
+
|
|
21
|
+
**The same invoice in InvoML:**
|
|
22
|
+
|
|
23
|
+
```json
|
|
24
|
+
{
|
|
25
|
+
"$invoml": "1.0",
|
|
26
|
+
"meta": { "documentType": "invoice", "number": "INV-2026-0001",
|
|
27
|
+
"issueDate": "2026-03-28", "currency": "USD",
|
|
28
|
+
"tax": { "label": "Tax", "rate": 10 } },
|
|
29
|
+
"from": { "name": "Acme Corp", "content": "**Acme Corp**\n123 Main St" },
|
|
30
|
+
"to": { "name": "Client LLC" },
|
|
31
|
+
"items": [
|
|
32
|
+
{ "description": "Consulting", "quantity": 10, "unitPrice": 200.00 }
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
**Ad-hoc JSON doesn't solve it either:** totals are pre-calculated by the model (floating-point errors, hallucinated numbers), there is no validation contract, and renderers must guess at structure.
|
|
38
|
+
|
|
39
|
+
InvoML solves all three problems. It is compact enough for token-sensitive LLM calls, carries a JSON Schema for structured output APIs, and defines deterministic calculation rules that guarantee byte-identical totals across every runtime and language.
|
|
40
|
+
|
|
41
|
+
---
|
|
42
|
+
|
|
43
|
+
## How It Works
|
|
44
|
+
|
|
45
|
+
```
|
|
46
|
+
┌─────────────────────┐ ┌──────────────────────┐ ┌─────────────────────┐
|
|
47
|
+
│ │ │ │ │ │
|
|
48
|
+
│ AI generates │ ───▶ │ Runtime calculates │ ───▶ │ Renderer displays │
|
|
49
|
+
│ InvoML document │ │ all arithmetic │ │ the final invoice │
|
|
50
|
+
│ │ │ │ │ │
|
|
51
|
+
│ data only, no │ │ arbitrary-precision │ │ PDF, HTML, email, │
|
|
52
|
+
│ pre-computed sums │ │ decimal math │ │ Markdown, any UI │
|
|
53
|
+
│ │ │ │ │ │
|
|
54
|
+
└─────────────────────┘ └──────────────────────┘ └─────────────────────┘
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
The spec separates three responsibilities that every other format conflates:
|
|
58
|
+
|
|
59
|
+
1. **Data** — what the invoice says (items, parties, tax rates, discounts)
|
|
60
|
+
2. **Math** — deterministic rules the runtime always executes (subtotals, rounding, tax cascades)
|
|
61
|
+
3. **Presentation** — how it looks. The optional `style` field carries visual intent: block ordering, named templates, CSS-compatible properties. Compact enough for AI to include (18–60 tokens), expressive enough for real invoices.
|
|
62
|
+
|
|
63
|
+
AI models only produce layer 1. The runtime owns layer 2. Renderers own layer 3.
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## Quick Example
|
|
68
|
+
|
|
69
|
+
A minimal invoice with a single line and 10% VAT:
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{
|
|
73
|
+
"$invoml": "1.0",
|
|
74
|
+
"meta": {
|
|
75
|
+
"documentType": "invoice",
|
|
76
|
+
"number": "INV-001",
|
|
77
|
+
"issueDate": "2026-03-28",
|
|
78
|
+
"currency": "EUR",
|
|
79
|
+
"tax": { "label": "VAT", "rate": 10 }
|
|
80
|
+
},
|
|
81
|
+
"from": { "name": "Studio Craft", "content": "**Studio Craft**\nBerlin, Germany" },
|
|
82
|
+
"to": { "name": "Client GmbH", "content": "**Client GmbH**\nMunich, Germany" },
|
|
83
|
+
"items": [
|
|
84
|
+
{ "description": "Logo Design", "quantity": 1, "unitPrice": 1200.00 }
|
|
85
|
+
]
|
|
86
|
+
}
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
The runtime calculates:
|
|
90
|
+
|
|
91
|
+
| Field | Value |
|
|
92
|
+
|--------------|-----------|
|
|
93
|
+
| Subtotal | €1,200.00 |
|
|
94
|
+
| VAT (10%) | €120.00 |
|
|
95
|
+
| **Total** | **€1,320.00** |
|
|
96
|
+
|
|
97
|
+
Numbers are computed from spec rules, never guessed by the model.
|
|
98
|
+
|
|
99
|
+
### With visual intent
|
|
100
|
+
|
|
101
|
+
The same invoice, with the AI expressing how it should look:
|
|
102
|
+
|
|
103
|
+
```json
|
|
104
|
+
{
|
|
105
|
+
"$invoml": "1.0",
|
|
106
|
+
"meta": {
|
|
107
|
+
"documentType": "invoice",
|
|
108
|
+
"number": "INV-001",
|
|
109
|
+
"issueDate": "2026-03-28",
|
|
110
|
+
"currency": "EUR",
|
|
111
|
+
"tax": { "label": "VAT", "rate": 10 }
|
|
112
|
+
},
|
|
113
|
+
"from": { "content": "**Studio Craft**\nBerlin, Germany" },
|
|
114
|
+
"to": { "content": "**Client GmbH**\nMunich, Germany" },
|
|
115
|
+
"items": [
|
|
116
|
+
{ "description": "Logo Design", "quantity": 1, "unitPrice": 1200.00 }
|
|
117
|
+
],
|
|
118
|
+
"style": {
|
|
119
|
+
"template": "modern",
|
|
120
|
+
"properties": { "--accent": "#e63946", "font-family": "Georgia, serif" },
|
|
121
|
+
"blocks": {
|
|
122
|
+
"header": { "text-align": "center", "border-bottom": "2px solid #e63946" }
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
The `style` object adds ~30 tokens. The renderer uses it to produce a visually distinct document — centered header, serif font, red accent — while the math and data remain unchanged. Without `style`, the renderer applies its own defaults. This is optional: most invoices don't need it.
|
|
129
|
+
|
|
130
|
+
---
|
|
131
|
+
|
|
132
|
+
## Reference Implementation
|
|
133
|
+
|
|
134
|
+
`@invompt/invoml` is the official TypeScript implementation of the InvoML v1.0 specification.
|
|
135
|
+
|
|
136
|
+
```bash
|
|
137
|
+
npm install @invompt/invoml
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Parse and validate
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
import { parse, calculate, toMarkdown } from '@invompt/invoml'
|
|
144
|
+
|
|
145
|
+
const result = parse(jsonString)
|
|
146
|
+
if (!result.success) {
|
|
147
|
+
console.error(result.errors)
|
|
148
|
+
process.exit(1)
|
|
149
|
+
}
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Calculate totals
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
const totals = calculate(result.document)
|
|
156
|
+
|
|
157
|
+
console.log(totals.subtotal) // 1200
|
|
158
|
+
console.log(totals.taxTotal) // 120
|
|
159
|
+
console.log(totals.total) // 1320
|
|
160
|
+
console.log(totals.amountDue) // 1320
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
All arithmetic uses arbitrary-precision decimal math (`decimal.js`). No floating-point rounding errors. Results are byte-identical across Node.js versions and operating systems.
|
|
164
|
+
|
|
165
|
+
### Render to Markdown
|
|
166
|
+
|
|
167
|
+
```typescript
|
|
168
|
+
// Attach totals to the document, then render
|
|
169
|
+
const doc = { ...result.document, totals }
|
|
170
|
+
const md = toMarkdown(doc)
|
|
171
|
+
// Ready for chat interfaces, email previews, plain-text pipelines
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### API surface
|
|
175
|
+
|
|
176
|
+
| Function | Description |
|
|
177
|
+
|---|---|
|
|
178
|
+
| `parse(json)` | Parse and type-validate a JSON string into `InvoMLDocument` |
|
|
179
|
+
| `calculate(doc)` | Compute all totals with arbitrary-precision math |
|
|
180
|
+
| `validateSchema(value)` | Validate against the JSON Schema (useful before `parse`) |
|
|
181
|
+
| `toJSON(doc, options?)` | Serialize to JSON. Pass `{ compact: true }` for minified output |
|
|
182
|
+
| `toMarkdown(doc)` | Render as a human-readable Markdown table (reads `doc.totals`) |
|
|
183
|
+
| `toHTML(doc)` | Render as a self-contained HTML document |
|
|
184
|
+
| `validateStyle(style, sectionNames?)` | Validate a style object against normative rules |
|
|
185
|
+
| `resolveOrder(doc)` | Resolve effective block rendering order for a document |
|
|
186
|
+
| `resolveStyle(doc)` | Resolve the full style object with defaults applied |
|
|
187
|
+
|
|
188
|
+
---
|
|
189
|
+
|
|
190
|
+
## Ecosystem
|
|
191
|
+
|
|
192
|
+
### JSON Schema for LLM structured output
|
|
193
|
+
|
|
194
|
+
The InvoML v1.0 JSON Schema is at [`invoml-v1.0.schema.json`](./invoml-v1.0.schema.json). Pass it directly to any LLM structured-output API:
|
|
195
|
+
|
|
196
|
+
```typescript
|
|
197
|
+
import schema from '@invompt/invoml/invoml-v1.0.schema.json' assert { type: 'json' }
|
|
198
|
+
|
|
199
|
+
// OpenAI structured outputs
|
|
200
|
+
const completion = await openai.beta.chat.completions.parse({
|
|
201
|
+
model: 'gpt-4o',
|
|
202
|
+
response_format: { type: 'json_schema', json_schema: { schema } },
|
|
203
|
+
messages: [{ role: 'user', content: 'Generate an invoice for ...' }]
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
// Anthropic tool use
|
|
207
|
+
const message = await anthropic.messages.create({
|
|
208
|
+
tools: [{ name: 'generate_invoice', input_schema: schema }],
|
|
209
|
+
// ...
|
|
210
|
+
})
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
### CLI
|
|
214
|
+
|
|
215
|
+
```bash
|
|
216
|
+
# Validate a document against the spec
|
|
217
|
+
npx @invompt/invoml validate invoice.json
|
|
218
|
+
|
|
219
|
+
# Calculate and print totals
|
|
220
|
+
npx @invompt/invoml calculate invoice.json
|
|
221
|
+
|
|
222
|
+
# Serialize to canonical JSON with computed totals
|
|
223
|
+
npx @invompt/invoml serialize invoice.json
|
|
224
|
+
|
|
225
|
+
# Render as a self-contained HTML file
|
|
226
|
+
npx @invompt/invoml html invoice.json > invoice.html
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
### Test vectors for conformance
|
|
230
|
+
|
|
231
|
+
The [`test-vectors/`](./test-vectors/) directory contains 18 canonical input/expected pairs. Any implementation claiming InvoML v1.0 conformance must pass all 18 vectors.
|
|
232
|
+
|
|
233
|
+
| # | Scenario |
|
|
234
|
+
|---|---|
|
|
235
|
+
| 01 | Minimal invoice (no tax) |
|
|
236
|
+
| 02 | Basic VAT |
|
|
237
|
+
| 03 | Multi-rate EU VAT |
|
|
238
|
+
| 04 | Invoice-level proportional discount |
|
|
239
|
+
| 05 | Line-level discounts |
|
|
240
|
+
| 06 | Cascading discounts |
|
|
241
|
+
| 07 | Compound tax (Canada GST + PST) |
|
|
242
|
+
| 08 | Inclusive tax (Australia GST) |
|
|
243
|
+
| 09 | Reverse charge (zero-rate VAT) |
|
|
244
|
+
| 10 | Withholding tax |
|
|
245
|
+
| 11 | Rounding edge case |
|
|
246
|
+
| 12 | Zero subtotal |
|
|
247
|
+
| 13 | Credit note |
|
|
248
|
+
| 14 | Inclusive tax with invoice-level discount |
|
|
249
|
+
| 15 | Compound withholding |
|
|
250
|
+
| 16 | Error — unknown tax category |
|
|
251
|
+
| 17 | Error — no default tax |
|
|
252
|
+
| 18 | Proportional discount tie-breaking |
|
|
253
|
+
|
|
254
|
+
---
|
|
255
|
+
|
|
256
|
+
## International Coverage
|
|
257
|
+
|
|
258
|
+
InvoML covers 15+ countries out of the box. The [`examples/`](./examples/) directory contains real-world documents for:
|
|
259
|
+
|
|
260
|
+
**Tax models supported:**
|
|
261
|
+
- Flat-rate VAT/GST (UK, EU, UAE, Switzerland, New Zealand, Australia)
|
|
262
|
+
- Multi-rate VAT (EU — standard 19%/7%, zero-rate, exempt within the same document)
|
|
263
|
+
- Compound taxes (Canada — GST + PST calculated on different bases)
|
|
264
|
+
- Inclusive taxes (Australia GST — tax embedded in the listed price)
|
|
265
|
+
- Reverse charge (EU cross-border — tax shifts to buyer)
|
|
266
|
+
- Withholding taxes (Nigeria, Mexico — issuer reports, buyer remits)
|
|
267
|
+
- CFDI-compatible (Mexico IVA with SAT registration fields)
|
|
268
|
+
- ZUGFeRD-compatible field coverage (Germany)
|
|
269
|
+
- IGST/CGST/SGST model (India)
|
|
270
|
+
- Consumption tax (Japan 10%)
|
|
271
|
+
|
|
272
|
+
**Document types:** invoices, quotes, estimates, receipts, credit notes
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## For AI Developers
|
|
277
|
+
|
|
278
|
+
Building an invoice generation feature? See [`docs/LLM-INTEGRATION.md`](./docs/LLM-INTEGRATION.md) for:
|
|
279
|
+
|
|
280
|
+
- How to pass the InvoML schema to structured output APIs (OpenAI, Anthropic, Google)
|
|
281
|
+
- System prompt patterns that produce valid InvoML reliably
|
|
282
|
+
- How to validate AI output before calling `calculate`
|
|
283
|
+
- Handling edge cases: unknown tax categories, missing currency symbols, model hallucinations
|
|
284
|
+
|
|
285
|
+
---
|
|
286
|
+
|
|
287
|
+
## Specification
|
|
288
|
+
|
|
289
|
+
The InvoML v1.0 specification (currently Draft) lives in [`SPEC.md`](./SPEC.md). It defines:
|
|
290
|
+
|
|
291
|
+
- Document structure and required fields
|
|
292
|
+
- Tax resolution rules (simple, full, compound, inclusive, withholding, reverse charge)
|
|
293
|
+
- Discount application order (line-level before invoice-level, proportional allocation)
|
|
294
|
+
- Rounding rules (half-up, currency-precision)
|
|
295
|
+
- Style model: block ordering, named templates, freeform CSS-compatible properties
|
|
296
|
+
- Conformance requirements for independent implementations
|
|
297
|
+
|
|
298
|
+
The `@invompt/invoml` reference implementation is normative for any case where the spec text is ambiguous. If `@invompt/invoml` and `SPEC.md` disagree, file an issue — both will be corrected.
|
|
299
|
+
|
|
300
|
+
---
|
|
301
|
+
|
|
302
|
+
## Why InvoML?
|
|
303
|
+
|
|
304
|
+
Existing invoice standards were built for enterprise systems — they handle data exchange between machines but say nothing about how the document should look. Ad-hoc JSON handles neither reliably. InvoML covers the full pipeline: AI generates structured data with optional visual intent, the runtime computes deterministic totals, and renderers produce polished output for humans.
|
|
305
|
+
|
|
306
|
+
See [`docs/WHY-INVOML.md`](./docs/WHY-INVOML.md) for a detailed comparison with UBL, ad-hoc JSON, and platform-specific formats.
|
|
307
|
+
|
|
308
|
+
---
|
|
309
|
+
|
|
310
|
+
## Contributing
|
|
311
|
+
|
|
312
|
+
See [CONTRIBUTING.md](./CONTRIBUTING.md) for development setup, test vector authoring, and the pull request process.
|
|
313
|
+
|
|
314
|
+
Implementations in other languages are welcome. Any implementation that passes all 18 test vectors and implements the arithmetic rules in `SPEC.md` is a conforming InvoML v1.0 implementation.
|
|
315
|
+
|
|
316
|
+
---
|
|
317
|
+
|
|
318
|
+
## License
|
|
319
|
+
|
|
320
|
+
Apache-2.0 — see [LICENSE](./LICENSE)
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from 'fs';
|
|
3
|
+
import { parse } from '../src/parser.js';
|
|
4
|
+
import { calculate } from '../src/calculator.js';
|
|
5
|
+
import { validateSchema } from '../src/schema.js';
|
|
6
|
+
import { toJSON, toMarkdown } from '../src/serializer.js';
|
|
7
|
+
import { toHTML } from '../src/html-renderer.js';
|
|
8
|
+
const [, , command, file, format] = process.argv;
|
|
9
|
+
if (!command || !file) {
|
|
10
|
+
console.log('Usage: invoml <validate|calculate|serialize|html> <file.json> [format]');
|
|
11
|
+
process.exit(1);
|
|
12
|
+
}
|
|
13
|
+
let content;
|
|
14
|
+
try {
|
|
15
|
+
content = readFileSync(file, 'utf8');
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
console.error(`Error reading file "${file}": ${e.message}`);
|
|
19
|
+
process.exit(1);
|
|
20
|
+
}
|
|
21
|
+
if (command === 'validate') {
|
|
22
|
+
let doc;
|
|
23
|
+
try {
|
|
24
|
+
doc = JSON.parse(content);
|
|
25
|
+
}
|
|
26
|
+
catch (e) {
|
|
27
|
+
console.error(`Invalid JSON in "${file}": ${e.message}`);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
const result = validateSchema(doc);
|
|
31
|
+
if (result.valid) {
|
|
32
|
+
console.log('Valid InvoML document.');
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
console.error('Validation errors:');
|
|
36
|
+
result.errors.forEach(e => console.error(` - ${e}`));
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if (command === 'calculate') {
|
|
41
|
+
const result = parse(content);
|
|
42
|
+
if (!result.success) {
|
|
43
|
+
console.error('Parse errors:');
|
|
44
|
+
result.errors.forEach(e => console.error(` - ${e}`));
|
|
45
|
+
process.exit(1);
|
|
46
|
+
}
|
|
47
|
+
const totals = calculate(result.document);
|
|
48
|
+
console.log(JSON.stringify(totals, null, 2));
|
|
49
|
+
}
|
|
50
|
+
else if (command === 'serialize') {
|
|
51
|
+
const result = parse(content);
|
|
52
|
+
if (!result.success) {
|
|
53
|
+
console.error('Parse errors:');
|
|
54
|
+
result.errors.forEach(e => console.error(` - ${e}`));
|
|
55
|
+
process.exit(1);
|
|
56
|
+
}
|
|
57
|
+
const doc = { ...result.document, totals: calculate(result.document) };
|
|
58
|
+
if (format === 'md' || format === 'markdown') {
|
|
59
|
+
console.log(toMarkdown(doc));
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
console.log(toJSON(doc));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else if (command === 'html') {
|
|
66
|
+
const result = parse(content);
|
|
67
|
+
if (!result.success) {
|
|
68
|
+
console.error('Parse errors:');
|
|
69
|
+
result.errors.forEach(e => console.error(` - ${e}`));
|
|
70
|
+
process.exit(1);
|
|
71
|
+
}
|
|
72
|
+
const doc = { ...result.document, totals: calculate(result.document) };
|
|
73
|
+
console.log(toHTML(doc));
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
console.error(`Unknown command: ${command}`);
|
|
77
|
+
process.exit(1);
|
|
78
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { InvoMLDocument, InvoMLTotals } from './types.js';
|
|
2
|
+
/** Compute all totals for an InvoML document using arbitrary-precision decimal arithmetic. Returns subtotal, per-category tax breakdowns, discount details, and amount due. */
|
|
3
|
+
export declare function calculate(doc: InvoMLDocument): InvoMLTotals;
|