@harrym1707/sdk 0.0.0-stage → 0.2.1
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 +98 -0
- package/README.md +48 -2
- package/bin/mesh-demo.mjs +105 -0
- package/bin/mesh-proxy.mjs +39 -0
- package/bin/mesh-quote.mjs +45 -0
- package/bin/mesh-report.mjs +47 -0
- package/index.cjs +6306 -0
- package/index.js +6204 -0
- package/package.json +39 -4
- package/types/blind-features.d.ts +28 -0
- package/types/cache-volatility-guard.d.ts +31 -0
- package/types/core/account-store.d.ts +7 -0
- package/types/core/accounts.d.ts +134 -0
- package/types/core/billing-worker.d.ts +113 -0
- package/types/core/budget.d.ts +216 -0
- package/types/core/cache-simulator.d.ts +271 -0
- package/types/core/confidence.d.ts +36 -0
- package/types/core/cost.d.ts +16 -0
- package/types/core/evaluator.d.ts +16 -0
- package/types/core/gateway.d.ts +75 -0
- package/types/core/geo-visibility.d.ts +25 -0
- package/types/core/github-oauth.d.ts +41 -0
- package/types/core/helicone.d.ts +6 -0
- package/types/core/index.d.ts +36 -0
- package/types/core/langfuse.d.ts +7 -0
- package/types/core/ledger-store.d.ts +62 -0
- package/types/core/litellm.d.ts +15 -0
- package/types/core/local-tier.d.ts +82 -0
- package/types/core/model-prices.d.ts +25 -0
- package/types/core/output-diagnostics.d.ts +39 -0
- package/types/core/price-catalogue.d.ts +168 -0
- package/types/core/provider-usage.d.ts +84 -0
- package/types/core/quote-report.d.ts +54 -0
- package/types/core/replay.d.ts +2 -0
- package/types/core/router.d.ts +9 -0
- package/types/core/routing.d.ts +282 -0
- package/types/core/savings-ledger.d.ts +349 -0
- package/types/core/savings-model.d.ts +132 -0
- package/types/core/stripe-billing.d.ts +67 -0
- package/types/core/stripe-client.d.ts +117 -0
- package/types/core/stripe-sink.d.ts +77 -0
- package/types/core/theo-coverage.d.ts +49 -0
- package/types/core/token-reduction.d.ts +219 -0
- package/types/core/tool-schema.d.ts +64 -0
- package/types/core/traffic.d.ts +182 -0
- package/types/core/triage.d.ts +70 -0
- package/types/core/types.d.ts +149 -0
- package/types/core/workflow-sessions.d.ts +50 -0
- package/types/cta.d.ts +60 -0
- package/types/evaluation-capture.d.ts +11 -0
- package/types/exact-cache.d.ts +20 -0
- package/types/graders.d.ts +53 -0
- package/types/index.d.ts +31 -0
- package/types/ledger-mirror.d.ts +38 -0
- package/types/openai.d.ts +23 -0
- package/types/proxy.d.ts +104 -0
- package/types/reduce-request.d.ts +272 -0
- package/types/replay-gate.d.ts +120 -0
- package/types/report.d.ts +33 -0
- package/types/routing.d.ts +11 -0
- package/types/savings-ledger.d.ts +22 -0
- package/types/savings-snapshot.d.ts +11 -0
- package/types/semantic-compaction.d.ts +84 -0
- package/types/telemetry.d.ts +6 -0
- package/types/theo-agent-store.d.ts +27 -0
- package/types/theo-agent.d.ts +247 -0
- package/types/theo-gate.d.ts +75 -0
- package/types/theo-policy.d.ts +219 -0
- package/types/types.d.ts +287 -0
- package/types/wrap.d.ts +60 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
Business Source License 1.1
|
|
2
|
+
|
|
3
|
+
License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
|
|
4
|
+
"Business Source License" is a trademark of MariaDB Corporation Ab.
|
|
5
|
+
|
|
6
|
+
-----------------------------------------------------------------------------
|
|
7
|
+
|
|
8
|
+
Parameters
|
|
9
|
+
|
|
10
|
+
Licensor: Flux AI Technologies Limited (company no. 17543982,
|
|
11
|
+
registered in England and Wales), trading as Mesh
|
|
12
|
+
|
|
13
|
+
Licensed Work: MESH
|
|
14
|
+
The Licensed Work is (c) 2026 Flux AI Technologies
|
|
15
|
+
Limited.
|
|
16
|
+
|
|
17
|
+
Additional Use Grant: You may make use of the Licensed Work for
|
|
18
|
+
non-production purposes: evaluation, development,
|
|
19
|
+
testing, and personal, non-commercial use. Any use of
|
|
20
|
+
the Licensed Work in production, or to provide a
|
|
21
|
+
commercial product or service, requires a commercial
|
|
22
|
+
agreement with the Licensor.
|
|
23
|
+
|
|
24
|
+
Change Date: 2030-10-19
|
|
25
|
+
|
|
26
|
+
Change License: Apache License, Version 2.0
|
|
27
|
+
|
|
28
|
+
For information about alternative licensing arrangements for the Licensed
|
|
29
|
+
Work, please contact the Licensor.
|
|
30
|
+
|
|
31
|
+
-----------------------------------------------------------------------------
|
|
32
|
+
|
|
33
|
+
Terms
|
|
34
|
+
|
|
35
|
+
The Licensor hereby grants you the right to copy, modify, create derivative
|
|
36
|
+
works, redistribute, and make non-production use of the Licensed Work. The
|
|
37
|
+
Licensor may make an Additional Use Grant, above, permitting limited
|
|
38
|
+
production use.
|
|
39
|
+
|
|
40
|
+
Effective on the Change Date, or the fourth anniversary of the first publicly
|
|
41
|
+
available distribution of a specific version of the Licensed Work under this
|
|
42
|
+
License, whichever comes first, the Licensor hereby grants you rights under
|
|
43
|
+
the terms of the Change License, and the rights granted in the paragraph
|
|
44
|
+
above terminate.
|
|
45
|
+
|
|
46
|
+
If your use of the Licensed Work does not comply with the requirements
|
|
47
|
+
currently in effect as described in this License, you must purchase a
|
|
48
|
+
commercial license from the Licensor, its affiliated entities, or authorized
|
|
49
|
+
resellers, or you must refrain from using the Licensed Work.
|
|
50
|
+
|
|
51
|
+
All copies of the original and modified Licensed Work, and derivative works
|
|
52
|
+
of the Licensed Work, are subject to this License. This License applies
|
|
53
|
+
separately for each version of the Licensed Work and the Change Date may vary
|
|
54
|
+
for each version of the Licensed Work released by Licensor.
|
|
55
|
+
|
|
56
|
+
You must conspicuously display this License on each original or modified copy
|
|
57
|
+
of the Licensed Work. If you receive the Licensed Work in original or
|
|
58
|
+
modified form from a third party, the terms and conditions set forth in this
|
|
59
|
+
License apply to your use of that work.
|
|
60
|
+
|
|
61
|
+
Any use of the Licensed Work in violation of this License will automatically
|
|
62
|
+
terminate your rights under this License for the current and all other
|
|
63
|
+
versions of the Licensed Work.
|
|
64
|
+
|
|
65
|
+
This License does not grant you any right in any trademark or logo of
|
|
66
|
+
Licensor or its affiliates (provided that you may use a trademark or logo of
|
|
67
|
+
Licensor as expressly required by this License).
|
|
68
|
+
|
|
69
|
+
TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
|
|
70
|
+
AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
|
|
71
|
+
EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
|
|
72
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
|
|
73
|
+
TITLE.
|
|
74
|
+
|
|
75
|
+
MariaDB hereby grants you permission to use this License's text to license
|
|
76
|
+
your works, and to refer to it using the trademark "Business Source License",
|
|
77
|
+
as long as you comply with the Covenants of Licensor below.
|
|
78
|
+
|
|
79
|
+
Covenants of Licensor
|
|
80
|
+
|
|
81
|
+
In consideration of the right to use this License's text and the "Business
|
|
82
|
+
Source License" name and trademark, Licensor covenants to MariaDB, and to all
|
|
83
|
+
other recipients of the licensed work to be provided by Licensor:
|
|
84
|
+
|
|
85
|
+
1. To specify as the Change License the GPL Version 2.0 or any later version,
|
|
86
|
+
or a license that is compatible with GPL Version 2.0 or a later version,
|
|
87
|
+
where "compatible" means that software provided under the Change License can
|
|
88
|
+
be included in a program with software provided under GPL Version 2.0 or a
|
|
89
|
+
later version. Licensor may specify additional Change Licenses without
|
|
90
|
+
limitation.
|
|
91
|
+
|
|
92
|
+
2. To either: (a) specify an additional grant of rights to use that does not
|
|
93
|
+
impose any additional restriction on the right granted in this License, as
|
|
94
|
+
the Additional Use Grant; or (b) insert the text "None".
|
|
95
|
+
|
|
96
|
+
3. To specify a Change Date.
|
|
97
|
+
|
|
98
|
+
4. Not to modify this License in any other way.
|
package/README.md
CHANGED
|
@@ -1,3 +1,49 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @harrym1707/sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Cut LLM API costs on Claude (Anthropic), OpenAI, and OpenAI-compatible APIs.
|
|
4
|
+
MESH reduces each request losslessly before it is sent and meters VERIFIED
|
|
5
|
+
savings into an append-only, hash-chained JSONL ledger you can recompute into
|
|
6
|
+
the invoice yourself. The fee is a share of verified savings; a period that
|
|
7
|
+
saved nothing says do_not_charge.
|
|
8
|
+
|
|
9
|
+
## Proxy mode: no code changes
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
npx mesh-proxy # then point your client's base_url at it
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
const anthropic = new Anthropic({ baseURL: "http://127.0.0.1:8787" });
|
|
17
|
+
const openai = new OpenAI({ baseURL: "http://127.0.0.1:8787/v1" });
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Your API key passes through verbatim (MESH never holds credentials), responses
|
|
21
|
+
— SSE streams included — return byte-identical, and every request appends one
|
|
22
|
+
ledger event. Prompts never leave your machine: the ledger records usage
|
|
23
|
+
numbers, lever names and hashes, not content.
|
|
24
|
+
|
|
25
|
+
## SDK mode: one line
|
|
26
|
+
|
|
27
|
+
```js
|
|
28
|
+
import { withMesh } from "@harrym1707/sdk"; // or require("@harrym1707/sdk")
|
|
29
|
+
|
|
30
|
+
const client = withMesh(anthropic, {
|
|
31
|
+
applicationId: "my-app",
|
|
32
|
+
tokenReduction: { toolSchemas: true, duplicateContext: true, repairCachePrefix: true },
|
|
33
|
+
ledger: { path: "./mesh-ledger.jsonl", provider: "anthropic" },
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Wraps Anthropic messages.create, OpenAI responses.create and
|
|
38
|
+
chat.completions.create on one shared hash chain. The wrapper never changes
|
|
39
|
+
your responses; the meter never throws into the request path.
|
|
40
|
+
|
|
41
|
+
## Quote from your gateway export
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
npx mesh-quote your-export.jsonl --format litellm # or helicone | langfuse
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
Includes the honest "do not pay us" verdict for already-optimised stacks.
|
|
48
|
+
Recompute any invoice from your own spool with `recomputeInvoice` /
|
|
49
|
+
`verifyLedgerChain`, both exported here.
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// The zero-key MESH demo: the ENTIRE loop — proxy, reduction, hash-chained
|
|
3
|
+
// ledger, independent recomputation — on your machine in ~10 seconds, with
|
|
4
|
+
// no API key, no account, and no network access (the "provider" is a local
|
|
5
|
+
// stub). DEMO TRAFFIC: the percentages below describe this synthetic run,
|
|
6
|
+
// not a claim about your workload — run the proxy on real traffic for that.
|
|
7
|
+
import { createServer } from "node:http";
|
|
8
|
+
import { mkdtempSync, readFileSync, readdirSync } from "node:fs";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { join } from "node:path";
|
|
11
|
+
import { recomputeInvoice, startMeshProxy, verifyLedgerChain } from "../index.js";
|
|
12
|
+
|
|
13
|
+
const stub = createServer((req, res) => {
|
|
14
|
+
const chunks = [];
|
|
15
|
+
req.on("data", (c) => chunks.push(c));
|
|
16
|
+
req.on("end", () => {
|
|
17
|
+
const body = Buffer.concat(chunks).toString("utf8");
|
|
18
|
+
const inputTokens = Math.ceil(body.length / 4);
|
|
19
|
+
res.writeHead(200, { "content-type": "application/json" });
|
|
20
|
+
res.end(JSON.stringify({
|
|
21
|
+
id: "msg_demo", type: "message", role: "assistant", model: "claude-sonnet-5-5",
|
|
22
|
+
content: [{ type: "text", text: "(stub answer)" }], stop_reason: "end_turn",
|
|
23
|
+
usage: { input_tokens: inputTokens, output_tokens: 40, cache_read_input_tokens: 0, cache_creation_input_tokens: 0 },
|
|
24
|
+
}));
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
await new Promise((ready) => stub.listen(0, "127.0.0.1", ready));
|
|
28
|
+
const stubUrl = `http://127.0.0.1:${stub.address().port}`;
|
|
29
|
+
|
|
30
|
+
const ledgerDirectory = mkdtempSync(join(tmpdir(), "mesh-demo-"));
|
|
31
|
+
const proxy = await startMeshProxy({
|
|
32
|
+
ledgerDirectory,
|
|
33
|
+
anthropicUpstream: stubUrl,
|
|
34
|
+
tokenReduction: { toolSchemas: true, duplicateContext: true, repairCachePrefix: true, cacheTtl: "5m" },
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
console.log("MESH zero-key demo — a local stub stands in for the provider; nothing leaves this machine.\n");
|
|
38
|
+
|
|
39
|
+
const PREAMBLE = "You are a careful assistant on a production team. Verify inputs, cite sources, prefer small reversible steps, and keep answers terse. ".repeat(12);
|
|
40
|
+
const TOOLS = Array.from({ length: 8 }, (_, i) => ({
|
|
41
|
+
name: `lookup_${i}`,
|
|
42
|
+
description: `Looks up records in subsystem ${i}. Validates input, retries once on transient failure, returns compact JSON.`,
|
|
43
|
+
input_schema: { $schema: "http://json-schema.org/draft-07/schema#", type: "object", additionalProperties: false, properties: { id: { type: "string" } }, required: ["id"] },
|
|
44
|
+
}));
|
|
45
|
+
const EXCERPT = "Clause 4 of the services agreement sets the notification window and assigns liability for third-party outages to the operating party. ".repeat(2);
|
|
46
|
+
|
|
47
|
+
async function send(name, request) {
|
|
48
|
+
const response = await fetch(`${proxy.url}/v1/messages`, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: { "content-type": "application/json", "x-api-key": "sk-demo-not-a-real-key", "x-mesh-application": name },
|
|
51
|
+
body: JSON.stringify(request),
|
|
52
|
+
});
|
|
53
|
+
await response.arrayBuffer();
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// 1. A multi-turn agent whose system prompt carries a per-turn clock — the
|
|
57
|
+
// exact shape that LOSES money when cache breakpoints are placed naively.
|
|
58
|
+
const history = [];
|
|
59
|
+
for (let turn = 0; turn < 5; turn += 1) {
|
|
60
|
+
history.push({ role: "user", content: `Turn ${turn}: continue the migration.` });
|
|
61
|
+
await send("demo-agent", {
|
|
62
|
+
model: "claude-sonnet-5-5", max_tokens: 300,
|
|
63
|
+
system: `Current time: 2026-10-24T09:0${turn}:00Z\n${PREAMBLE}`,
|
|
64
|
+
tools: TOOLS, messages: [...history],
|
|
65
|
+
});
|
|
66
|
+
history.push({ role: "assistant", content: `Turn ${turn} done.` });
|
|
67
|
+
}
|
|
68
|
+
console.log("1. five-turn agent, per-turn clock in the system prompt: schemas compacted, the clock relocated out of the cacheable prefix, cache_control placed on the stable text only");
|
|
69
|
+
|
|
70
|
+
// 2. A RAG request that retrieved the same excerpt twice.
|
|
71
|
+
await send("demo-rag", {
|
|
72
|
+
model: "claude-sonnet-5-5", max_tokens: 300, system: PREAMBLE,
|
|
73
|
+
messages: [{ role: "user", content: `Context:\n${EXCERPT}\n${EXCERPT}\nQuestion: what does clause 4 require?` }],
|
|
74
|
+
});
|
|
75
|
+
console.log("2. RAG request with a duplicated excerpt: the exact duplicate removed, question untouched");
|
|
76
|
+
|
|
77
|
+
// 3. Templated batch items.
|
|
78
|
+
for (let item = 0; item < 4; item += 1) {
|
|
79
|
+
await send("demo-batch", {
|
|
80
|
+
model: "claude-sonnet-5-5", max_tokens: 60, system: PREAMBLE, tools: TOOLS.slice(0, 4),
|
|
81
|
+
messages: [{ role: "user", content: `Classify ticket ${item}: printer error E${item}.` }],
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
console.log("3. four templated batch classifications: shared template stamped once, read thereafter");
|
|
85
|
+
|
|
86
|
+
// 4. An already-lean request MESH should leave alone.
|
|
87
|
+
await send("demo-lean", {
|
|
88
|
+
model: "claude-sonnet-5-5", max_tokens: 60,
|
|
89
|
+
messages: [{ role: "user", content: "Summarise: the meeting moved to 3pm." }],
|
|
90
|
+
});
|
|
91
|
+
console.log("4. an already-lean request: nothing worth removing\n");
|
|
92
|
+
|
|
93
|
+
await proxy.close();
|
|
94
|
+
stub.close();
|
|
95
|
+
|
|
96
|
+
for (const spool of readdirSync(ledgerDirectory).sort()) {
|
|
97
|
+
const records = readFileSync(join(ledgerDirectory, spool), "utf8").trim().split(/\r?\n/).map((line) => JSON.parse(line));
|
|
98
|
+
const chain = verifyLedgerChain(records);
|
|
99
|
+
const invoice = recomputeInvoice(records.map((r) => r.event));
|
|
100
|
+
console.log(`${spool}: ${records.length} event(s), chain ${chain.ok ? "VERIFIED" : "BROKEN"} — verdict ${invoice.verdict}, net verified savings $${invoice.netVerifiedSavingsUsd.toFixed(4)}`);
|
|
101
|
+
}
|
|
102
|
+
console.log(`\nThe spools live at ${ledgerDirectory} — inspect them, or re-verify independently with the MIT verifier:`);
|
|
103
|
+
console.log(` npx -y -p @harrym1707/verify mesh-verify ${join(ledgerDirectory, "demo-agent-anthropic.jsonl")}`);
|
|
104
|
+
console.log("\nDemo traffic only: these numbers describe this synthetic run, never your workload.");
|
|
105
|
+
console.log("Real traffic is one line: point your client baseURL at mesh-proxy and keep your own key.");
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// MESH proxy mode: point your client's base_url here, keep your own API key.
|
|
3
|
+
// Requests are reduced before they reach the provider, responses (including
|
|
4
|
+
// SSE streams) return byte-identical, and every request appends one
|
|
5
|
+
// hash-chained ledger event. Lossless levers only by default.
|
|
6
|
+
//
|
|
7
|
+
// MESH_PROXY_PORT default 8787
|
|
8
|
+
// MESH_PROXY_LEDGER_DIR default ./mesh-ledger
|
|
9
|
+
// MESH_PROXY_CACHE_TTL "5m" | "1h" to enable the Anthropic cache stamp
|
|
10
|
+
import { mkdirSync } from "node:fs";
|
|
11
|
+
import { join } from "node:path";
|
|
12
|
+
import { startMeshProxy } from "../index.js";
|
|
13
|
+
|
|
14
|
+
const port = Number(process.env.MESH_PROXY_PORT ?? 8787);
|
|
15
|
+
if (!Number.isInteger(port) || port < 0 || port > 65535) {
|
|
16
|
+
console.error(`MESH_PROXY_PORT must be a port number, got ${JSON.stringify(process.env.MESH_PROXY_PORT)}.`);
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
const ledgerDirectory = process.env.MESH_PROXY_LEDGER_DIR ?? join(process.cwd(), "mesh-ledger");
|
|
20
|
+
mkdirSync(ledgerDirectory, { recursive: true });
|
|
21
|
+
const cacheTtl = process.env.MESH_PROXY_CACHE_TTL;
|
|
22
|
+
if (cacheTtl !== undefined && cacheTtl !== "5m" && cacheTtl !== "1h") {
|
|
23
|
+
console.error(`MESH_PROXY_CACHE_TTL must be "5m" or "1h", got ${JSON.stringify(cacheTtl)}.`);
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
const proxy = await startMeshProxy({
|
|
27
|
+
port,
|
|
28
|
+
ledgerDirectory,
|
|
29
|
+
tokenReduction: {
|
|
30
|
+
toolSchemas: true,
|
|
31
|
+
duplicateContext: true,
|
|
32
|
+
repairCachePrefix: true,
|
|
33
|
+
...(cacheTtl === undefined ? {} : { cacheTtl }),
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
console.log(`MESH proxy listening on ${proxy.url}`);
|
|
37
|
+
console.log(`Ledger spools: ${ledgerDirectory}`);
|
|
38
|
+
console.log(`Anthropic SDK: new Anthropic({ baseURL: "${proxy.url}" })`);
|
|
39
|
+
console.log(`OpenAI SDK: new OpenAI({ baseURL: "${proxy.url}/v1" })`);
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Pre-contract savings quote from YOUR gateway export (LiteLLM, Helicone or
|
|
3
|
+
// Langfuse), including the honest "do not pay us" verdict.
|
|
4
|
+
//
|
|
5
|
+
// mesh-quote <export-file> [--format litellm|helicone|langfuse]
|
|
6
|
+
// [--at YYYY-MM-DD] [--out quote.json]
|
|
7
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
8
|
+
import { buildQuoteReport, canonicalJson, publishedModelPrices, renderQuoteReport } from "../index.js";
|
|
9
|
+
|
|
10
|
+
function fail(message) {
|
|
11
|
+
console.error(message);
|
|
12
|
+
process.exit(1);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const args = process.argv.slice(2);
|
|
16
|
+
if (args.length === 0 || args[0].startsWith("--")) {
|
|
17
|
+
fail("Usage: mesh-quote <export-file> [--format litellm|helicone|langfuse] [--at YYYY-MM-DD] [--out quote.json]");
|
|
18
|
+
}
|
|
19
|
+
const file = args[0];
|
|
20
|
+
let format = "litellm";
|
|
21
|
+
let at = new Date().toISOString().slice(0, 10);
|
|
22
|
+
let out;
|
|
23
|
+
for (let index = 1; index < args.length; index += 2) {
|
|
24
|
+
const flag = args[index];
|
|
25
|
+
const value = args[index + 1];
|
|
26
|
+
if (value === undefined) fail(`Missing value for ${flag}.`);
|
|
27
|
+
if (flag === "--format") {
|
|
28
|
+
if (!["litellm", "helicone", "langfuse"].includes(value)) fail(`Unknown format ${value}.`);
|
|
29
|
+
format = value;
|
|
30
|
+
} else if (flag === "--at") at = value;
|
|
31
|
+
else if (flag === "--out") out = value;
|
|
32
|
+
else fail(`Unknown flag ${flag}.`);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const text = readFileSync(file, "utf8").trim();
|
|
36
|
+
const rows = text.startsWith("[")
|
|
37
|
+
? JSON.parse(text)
|
|
38
|
+
: text.split(/\r?\n/).filter((line) => line.trim() !== "").map((line) => JSON.parse(line));
|
|
39
|
+
|
|
40
|
+
const report = buildQuoteReport(rows, { format, at, catalogue: publishedModelPrices });
|
|
41
|
+
console.log(renderQuoteReport(report));
|
|
42
|
+
if (out !== undefined) {
|
|
43
|
+
writeFileSync(out, `${canonicalJson(report)}\n`, "utf8");
|
|
44
|
+
console.error(`\nShareable report written to ${out}`);
|
|
45
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Render a MESH statement (self-contained HTML) from ledger spools.
|
|
3
|
+
// Usage: mesh-report <spool.jsonl...> [--period YYYY-MM] [--fee 1] [--carry-forward 0] [--out mesh-statement.html]
|
|
4
|
+
import { readFileSync, writeFileSync } from "node:fs";
|
|
5
|
+
import { basename } from "node:path";
|
|
6
|
+
import { renderLedgerReport } from "../index.js";
|
|
7
|
+
|
|
8
|
+
const args = process.argv.slice(2);
|
|
9
|
+
const files = [];
|
|
10
|
+
let period;
|
|
11
|
+
let feePercent = 1;
|
|
12
|
+
let carryForwardDeficitUsd = 0;
|
|
13
|
+
let out = "mesh-statement.html";
|
|
14
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
15
|
+
const arg = args[index];
|
|
16
|
+
if (arg === "--period") period = args[++index];
|
|
17
|
+
else if (arg === "--fee") feePercent = Number(args[++index]);
|
|
18
|
+
else if (arg === "--carry-forward") carryForwardDeficitUsd = Number(args[++index]);
|
|
19
|
+
else if (arg === "--out") out = args[++index];
|
|
20
|
+
else files.push(arg);
|
|
21
|
+
}
|
|
22
|
+
if (files.length === 0) {
|
|
23
|
+
console.error("Usage: mesh-report <spool.jsonl...> [--period YYYY-MM] [--fee 1] [--carry-forward 0] [--out mesh-statement.html]");
|
|
24
|
+
process.exit(1);
|
|
25
|
+
}
|
|
26
|
+
if (!Number.isFinite(feePercent) || feePercent < 0 || feePercent > 100) {
|
|
27
|
+
console.error(`--fee must be a percentage between 0 and 100, got ${JSON.stringify(feePercent)}.`);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
if (period !== undefined && !/^\d{4}-\d{2}$/.test(period)) {
|
|
31
|
+
console.error(`--period must be YYYY-MM, got ${JSON.stringify(period)}.`);
|
|
32
|
+
process.exit(1);
|
|
33
|
+
}
|
|
34
|
+
const spools = files.map((file) => ({
|
|
35
|
+
name: basename(file),
|
|
36
|
+
records: readFileSync(file, "utf8").trim().split(/\r?\n/).filter(Boolean).map((line) => JSON.parse(line)),
|
|
37
|
+
}));
|
|
38
|
+
if (!Number.isFinite(carryForwardDeficitUsd) || carryForwardDeficitUsd < 0) {
|
|
39
|
+
console.error("--carry-forward must be a non-negative USD amount.");
|
|
40
|
+
process.exit(1);
|
|
41
|
+
}
|
|
42
|
+
const html = renderLedgerReport(spools, { period, feePercent, carryForwardDeficitUsd });
|
|
43
|
+
writeFileSync(out, html, "utf8");
|
|
44
|
+
const verdict = html.includes("DO NOT PAY") ? "DO NOT PAY (chain broken)"
|
|
45
|
+
: html.includes("do_not_charge") ? "do_not_charge" : "charge";
|
|
46
|
+
console.log(`statement written: ${out} (${spools.length} spool(s), verdict: ${verdict})`);
|
|
47
|
+
console.log("Verify independently: npx -y -p @harrym1707/verify mesh-verify <spool.jsonl>");
|