@curless/hotel-storefront 0.69.4 → 0.70.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 +11 -0
- package/dist/account/index.d.ts +21 -0
- package/dist/account/index.d.ts.map +1 -1
- package/dist/account/index.js +63 -4
- package/dist/account/index.js.map +1 -1
- package/dist/config.d.ts +12 -0
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/huazhu/client.d.ts +3 -1
- package/dist/huazhu/client.d.ts.map +1 -1
- package/dist/huazhu/client.js +26 -0
- package/dist/huazhu/client.js.map +1 -1
- package/dist/otaless/backend.d.ts.map +1 -1
- package/dist/otaless/backend.js +4 -0
- package/dist/otaless/backend.js.map +1 -1
- package/dist/otaless/booking.d.ts +3 -2
- package/dist/otaless/booking.d.ts.map +1 -1
- package/dist/otaless/booking.js +8 -2
- package/dist/otaless/booking.js.map +1 -1
- package/dist/otaless/places.d.ts.map +1 -1
- package/dist/otaless/places.js +16 -2
- package/dist/otaless/places.js.map +1 -1
- package/dist/regions.d.ts.map +1 -1
- package/dist/regions.js +25 -2
- package/dist/regions.js.map +1 -1
- package/dist/server.d.ts +4 -1
- package/dist/server.d.ts.map +1 -1
- package/dist/server.js +245 -8
- package/dist/server.js.map +1 -1
- package/dist/usage/boot.d.ts +32 -0
- package/dist/usage/boot.d.ts.map +1 -0
- package/dist/usage/boot.js +140 -0
- package/dist/usage/boot.js.map +1 -0
- package/dist/usage/geo.d.ts +48 -0
- package/dist/usage/geo.d.ts.map +1 -0
- package/dist/usage/geo.js +347 -0
- package/dist/usage/geo.js.map +1 -0
- package/dist/usage/maintain.d.ts +26 -0
- package/dist/usage/maintain.d.ts.map +1 -0
- package/dist/usage/maintain.js +177 -0
- package/dist/usage/maintain.js.map +1 -0
- package/dist/usage/platform.d.ts +69 -0
- package/dist/usage/platform.d.ts.map +1 -0
- package/dist/usage/platform.js +124 -0
- package/dist/usage/platform.js.map +1 -0
- package/dist/usage/pseudonym.d.ts +28 -0
- package/dist/usage/pseudonym.d.ts.map +1 -0
- package/dist/usage/pseudonym.js +73 -0
- package/dist/usage/pseudonym.js.map +1 -0
- package/dist/usage/range.d.ts +24 -0
- package/dist/usage/range.d.ts.map +1 -0
- package/dist/usage/range.js +102 -0
- package/dist/usage/range.js.map +1 -0
- package/dist/usage/recorder.d.ts +115 -0
- package/dist/usage/recorder.d.ts.map +1 -0
- package/dist/usage/recorder.js +227 -0
- package/dist/usage/recorder.js.map +1 -0
- package/dist/usage/report.d.ts +207 -0
- package/dist/usage/report.d.ts.map +1 -0
- package/dist/usage/report.js +730 -0
- package/dist/usage/report.js.map +1 -0
- package/dist/usage/shadow-import.d.ts +28 -0
- package/dist/usage/shadow-import.d.ts.map +1 -0
- package/dist/usage/shadow-import.js +107 -0
- package/dist/usage/shadow-import.js.map +1 -0
- package/dist/usage/store.d.ts +215 -0
- package/dist/usage/store.d.ts.map +1 -0
- package/dist/usage/store.js +430 -0
- package/dist/usage/store.js.map +1 -0
- package/dist/usage-cli.d.ts +3 -0
- package/dist/usage-cli.d.ts.map +1 -0
- package/dist/usage-cli.js +152 -0
- package/dist/usage-cli.js.map +1 -0
- package/package.json +3 -2
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// Which platform a request came through, whose address - if anyone's - it
|
|
2
|
+
// carries, and the country that address is in. The same rules as
|
|
3
|
+
// @curless/shopify-storefront src/usage/platform.ts and where.ts (0.77.2), on
|
|
4
|
+
// this package's own ranges (source-ranges.ts, the allowlist's) and parser
|
|
5
|
+
// (cidr.ts) - so a hotel's events and a shop's count the same way.
|
|
6
|
+
//
|
|
7
|
+
// ⚠️ THE ADDRESS NEVER LEAVES `geoFields`. It is read, looked up in the local
|
|
8
|
+
// database (geo.ts), and the answer - two letters and a word - is all that comes
|
|
9
|
+
// back. Nothing here stores, logs or sends it.
|
|
10
|
+
import { inCidr, parseAddress, parseCidr } from '../cidr.js';
|
|
11
|
+
import { PLATFORM_RANGES } from '../source-ranges.js';
|
|
12
|
+
export const PLATFORMS = ['chatgpt', 'claude', 'manus', 'muse', 'other'];
|
|
13
|
+
/** A set of ranges, asked one address at a time. */
|
|
14
|
+
export class Ranges {
|
|
15
|
+
cidrs;
|
|
16
|
+
constructor(list) {
|
|
17
|
+
this.cidrs = list.map((c) => {
|
|
18
|
+
const cidr = parseCidr(c);
|
|
19
|
+
if (!cidr)
|
|
20
|
+
throw new Error(`usage: ${JSON.stringify(c)} is not a range`);
|
|
21
|
+
return cidr;
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
has(ip) {
|
|
25
|
+
const a = parseAddress(ip);
|
|
26
|
+
return !!a && this.cidrs.some((c) => inCidr(a, c));
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** A platform's published outbound ranges - the allowlist's own list. */
|
|
30
|
+
export const rangesOf = (id) => new Ranges((PLATFORM_RANGES.find((r) => r.id === id)?.sources ?? []).flatMap((s) => s.cidrs));
|
|
31
|
+
/** OpenAI's published ChatGPT connector ranges: the only source a guest's address is taken from. */
|
|
32
|
+
export const OPENAI = rangesOf('openai');
|
|
33
|
+
/** Anthropic's: claude.ai's calls come from them. */
|
|
34
|
+
const ANTHROPIC = rangesOf('anthropic');
|
|
35
|
+
/**
|
|
36
|
+
* By the marks the connector already goes by (sessions.ts CARD_HOSTS): ChatGPT's
|
|
37
|
+
* `openai-mcp` agent and its session header; claude.ai by its client name or
|
|
38
|
+
* Anthropic's ranges; Manus and Muse by name only - neither publishes an address
|
|
39
|
+
* (docs/mcp-source-allowlist.md). Self-reported, which is fine for a count.
|
|
40
|
+
* Claude Code counts as Claude; Codex is `other`.
|
|
41
|
+
*/
|
|
42
|
+
export const platformOf = (s) => {
|
|
43
|
+
const client = s.client ?? '';
|
|
44
|
+
const ua = s.userAgent ?? '';
|
|
45
|
+
if (s.openaiSession || /^openai-mcp\b/i.test(ua) || /^(openai-mcp|chatgpt)\b/i.test(client)) {
|
|
46
|
+
return 'chatgpt';
|
|
47
|
+
}
|
|
48
|
+
const named = `${client} ${ua}`;
|
|
49
|
+
if (/\bmanus\b/i.test(named))
|
|
50
|
+
return 'manus';
|
|
51
|
+
if (/\bmuse\b/i.test(named))
|
|
52
|
+
return 'muse';
|
|
53
|
+
if (/^(claude|anthropic)/i.test(client) || /\b(claude|anthropic)/i.test(ua))
|
|
54
|
+
return 'claude';
|
|
55
|
+
if (s.peer && ANTHROPIC.has(s.peer))
|
|
56
|
+
return 'claude';
|
|
57
|
+
return 'other';
|
|
58
|
+
};
|
|
59
|
+
/** Ranges that are nobody's location: private, shared, documentation, multicast. */
|
|
60
|
+
const NOT_PUBLIC = new Ranges([
|
|
61
|
+
'0.0.0.0/8',
|
|
62
|
+
'10.0.0.0/8',
|
|
63
|
+
'100.64.0.0/10',
|
|
64
|
+
'127.0.0.0/8',
|
|
65
|
+
'169.254.0.0/16',
|
|
66
|
+
'172.16.0.0/12',
|
|
67
|
+
'192.0.0.0/24',
|
|
68
|
+
'192.0.2.0/24',
|
|
69
|
+
'192.168.0.0/16',
|
|
70
|
+
'198.18.0.0/15',
|
|
71
|
+
'198.51.100.0/24',
|
|
72
|
+
'203.0.113.0/24',
|
|
73
|
+
'224.0.0.0/3',
|
|
74
|
+
'::/127',
|
|
75
|
+
'64:ff9b::/96',
|
|
76
|
+
'100::/64',
|
|
77
|
+
'2001:db8::/32',
|
|
78
|
+
'fc00::/7',
|
|
79
|
+
'fe80::/10',
|
|
80
|
+
'ff00::/8',
|
|
81
|
+
]);
|
|
82
|
+
/** A routable public address, as sent - or null. */
|
|
83
|
+
export const publicIp = (raw) => {
|
|
84
|
+
if (typeof raw !== 'string')
|
|
85
|
+
return null;
|
|
86
|
+
const ip = raw.trim().replace(/^\[|\]$/g, '');
|
|
87
|
+
if (!ip || ip.length > 64 || !parseAddress(ip) || NOT_PUBLIC.has(ip))
|
|
88
|
+
return null;
|
|
89
|
+
return ip;
|
|
90
|
+
};
|
|
91
|
+
/**
|
|
92
|
+
* Whose address this request carries.
|
|
93
|
+
*
|
|
94
|
+
* ⚠️ ONLY CHATGPT, AND ONLY FROM OPENAI'S OWN ADDRESSES. ChatGPT puts the person's
|
|
95
|
+
* address in the `X-Forwarded-For` it sends (first entry); every other host calls
|
|
96
|
+
* from its own machines, so its address is the platform's, not the person's -
|
|
97
|
+
* recorded as such, never as where they are (robin, 2026-11-12). A request that
|
|
98
|
+
* merely CLAIMS to be ChatGPT, from anywhere else, is `unknown`: the header is
|
|
99
|
+
* the caller's to write.
|
|
100
|
+
*
|
|
101
|
+
* `forwarded` is the X-Forwarded-For exactly as the caller sent it - the copy
|
|
102
|
+
* Caddy is asked to make (`X-Caller-Forwarded-For`, recorder.ts), because Caddy
|
|
103
|
+
* itself rewrites X-Forwarded-For to the peer it saw.
|
|
104
|
+
*/
|
|
105
|
+
export const originOf = (platform, peer, forwarded, openai) => {
|
|
106
|
+
if (platform !== 'chatgpt')
|
|
107
|
+
return { kind: 'platform' };
|
|
108
|
+
if (!peer || !openai.has(peer))
|
|
109
|
+
return { kind: 'unknown' };
|
|
110
|
+
const first = forwarded?.split(',')[0];
|
|
111
|
+
const ip = publicIp(first);
|
|
112
|
+
return ip ? { kind: 'user', ip } : { kind: 'unknown' };
|
|
113
|
+
};
|
|
114
|
+
/** The country (and network type) an event carries - looked up here, the address dropped here. */
|
|
115
|
+
export const geoFields = (platform, peer, forwarded, openai, geo) => {
|
|
116
|
+
const origin = originOf(platform, peer, forwarded, openai);
|
|
117
|
+
if (origin.kind === 'platform')
|
|
118
|
+
return { g: 'platform' };
|
|
119
|
+
if (origin.kind === 'unknown')
|
|
120
|
+
return { g: 'unknown' };
|
|
121
|
+
const { cc, net } = geo.lookup(origin.ip);
|
|
122
|
+
return cc ? { g: cc, n: net } : { g: 'unknown', ...(net !== 'unknown' ? { n: net } : {}) };
|
|
123
|
+
};
|
|
124
|
+
//# sourceMappingURL=platform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"platform.js","source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EAAE,CAAC;AACzD,CAAC,CAAC;AAIF,kGAAkG;AAClG,MAAM,CAAC,MAAM,SAAS,GAAG,CACvB,QAAkB,EAClB,IAAwB,EACxB,SAA6B,EAC7B,MAA2B,EAC3B,GAA0B,EACf,EAAE;IACb,MAAM,MAAM,GAAG,QAAQ,CAAC,QAAQ,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,CAAC,CAAC;IAC3D,IAAI,MAAM,CAAC,IAAI,KAAK,UAAU;QAAE,OAAO,EAAE,CAAC,EAAE,UAAU,EAAE,CAAC;IACzD,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS;QAAE,OAAO,EAAE,CAAC,EAAE,SAAS,EAAE,CAAC;IACvD,MAAM,EAAE,EAAE,EAAE,GAAG,EAAE,GAAG,GAAG,CAAC,MAAM,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;IAC1C,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,SAAS,EAAE,GAAG,CAAC,GAAG,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC;AAC7F,CAAC,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Hex characters kept: 64 bits - no collision worth counting at our volume. */
|
|
2
|
+
export declare const PSEUDONYM_HEX = 16;
|
|
3
|
+
/**
|
|
4
|
+
* The shortest key taken: 32 characters (`openssl rand -hex 32` gives 64). A
|
|
5
|
+
* short key is one a guess can find, and with it every pseudonym can be tested
|
|
6
|
+
* against a candidate id (security review of 0.77.0, P3).
|
|
7
|
+
*/
|
|
8
|
+
export declare const MIN_KEY_CHARS = 32;
|
|
9
|
+
export type ConversationSource =
|
|
10
|
+
/** ChatGPT's `X-OpenAI-Session`, as sent. */
|
|
11
|
+
'oai'
|
|
12
|
+
/** An `Mcp-Session-Id` - ours, or a conversation's own key from the cart binding. */
|
|
13
|
+
| 'mcp';
|
|
14
|
+
export declare class Pseudonyms {
|
|
15
|
+
private readonly key;
|
|
16
|
+
/** `secret` is the deployment's own; the MAC key is derived from it, labelled - the label shopify's, so both write one format. */
|
|
17
|
+
constructor(secret: string);
|
|
18
|
+
private mac;
|
|
19
|
+
/** A platform's own user id - today only ChatGPT's `X-OpenAI-Subject`. */
|
|
20
|
+
user(platformUserId: unknown): string | undefined;
|
|
21
|
+
/** A conversation. Never an address-keyed one (`ip:…`): see the top. */
|
|
22
|
+
conversation(source: ConversationSource, id: unknown): string | undefined;
|
|
23
|
+
/** A guest signed in to the deployment's own account: their Otaless member id (`memberUserId`) - nothing else of theirs. */
|
|
24
|
+
shopper(customerId: unknown): string | undefined;
|
|
25
|
+
/** One of our booking ids - never the hotel's confirmation code. */
|
|
26
|
+
ref(curlessRef: unknown): string | undefined;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=pseudonym.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pseudonym.d.ts","sourceRoot":"","sources":["../../src/usage/pseudonym.ts"],"names":[],"mappings":"AAmBA,gFAAgF;AAChF,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC;;;;GAIG;AACH,eAAO,MAAM,aAAa,KAAK,CAAC;AAWhC,MAAM,MAAM,kBAAkB;AAC5B,6CAA6C;AAC3C,KAAK;AACP,qFAAqF;GACnF,KAAK,CAAC;AAEV,qBAAa,UAAU;IACrB,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAS;IAE7B,kIAAkI;gBACtH,MAAM,EAAE,MAAM;IAO1B,OAAO,CAAC,GAAG;IAOX,0EAA0E;IAC1E,IAAI,CAAC,cAAc,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;IAKjD,wEAAwE;IACxE,YAAY,CAAC,MAAM,EAAE,kBAAkB,EAAE,EAAE,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;IAMzE,4HAA4H;IAC5H,OAAO,CAAC,UAAU,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;IAKhD,oEAAoE;IACpE,GAAG,CAAC,UAAU,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS;CAI7C"}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Copied from @curless/shopify-storefront src/usage/pseudonym.ts (0.77.2), the
|
|
2
|
+
// derivation unchanged (docs/usage-events.md §6) - only the key's variable is
|
|
3
|
+
// this package's: HOTEL_USAGE_KEY.
|
|
4
|
+
//
|
|
5
|
+
// Pseudonyms for the usage statistics: a keyed MAC of an identifier a platform
|
|
6
|
+
// or the account backend gave us, never the identifier itself.
|
|
7
|
+
//
|
|
8
|
+
// ⚠️ KEYED PER DEPLOYMENT (`HOTEL_USAGE_KEY`). Two deployments see the same
|
|
9
|
+
// ChatGPT user under different names, so one deployment's records cannot be joined to
|
|
10
|
+
// another's; and without the key nobody can test a guess against them - an
|
|
11
|
+
// unkeyed hash of an OpenAI subject or an Otaless member id could be.
|
|
12
|
+
//
|
|
13
|
+
// ⚠️ ONLY WHAT A PLATFORM OR THE SHOP HANDED US. A request with no platform user
|
|
14
|
+
// id gets no user pseudonym at all - never one made from its address or anything
|
|
15
|
+
// else to stand in (robin, 2026-11-12). An address-keyed conversation (`ip:…`)
|
|
16
|
+
// gets no conversation pseudonym for the same reason: an address MAC'd is still
|
|
17
|
+
// an address to whoever holds the key, and IPv4 is small enough to walk.
|
|
18
|
+
import { createHmac } from 'node:crypto';
|
|
19
|
+
/** Hex characters kept: 64 bits - no collision worth counting at our volume. */
|
|
20
|
+
export const PSEUDONYM_HEX = 16;
|
|
21
|
+
/**
|
|
22
|
+
* The shortest key taken: 32 characters (`openssl rand -hex 32` gives 64). A
|
|
23
|
+
* short key is one a guess can find, and with it every pseudonym can be tested
|
|
24
|
+
* against a candidate id (security review of 0.77.0, P3).
|
|
25
|
+
*/
|
|
26
|
+
export const MIN_KEY_CHARS = 32;
|
|
27
|
+
/** The longest identifier taken - anything longer is not a platform's id. */
|
|
28
|
+
const MAX_ID = 256;
|
|
29
|
+
const clean = (raw) => {
|
|
30
|
+
if (typeof raw !== 'string')
|
|
31
|
+
return undefined;
|
|
32
|
+
const v = raw.trim();
|
|
33
|
+
return v && v.length <= MAX_ID ? v : undefined;
|
|
34
|
+
};
|
|
35
|
+
export class Pseudonyms {
|
|
36
|
+
key;
|
|
37
|
+
/** `secret` is the deployment's own; the MAC key is derived from it, labelled - the label shopify's, so both write one format. */
|
|
38
|
+
constructor(secret) {
|
|
39
|
+
if (!secret || secret.length < MIN_KEY_CHARS) {
|
|
40
|
+
throw new Error(`the pseudonym key must be at least ${MIN_KEY_CHARS} characters`);
|
|
41
|
+
}
|
|
42
|
+
this.key = createHmac('sha256', secret).update('storefront-usage-pseudonyms-v1').digest();
|
|
43
|
+
}
|
|
44
|
+
mac(kind, value) {
|
|
45
|
+
return createHmac('sha256', this.key)
|
|
46
|
+
.update(`${kind}|${value}`)
|
|
47
|
+
.digest('hex')
|
|
48
|
+
.slice(0, PSEUDONYM_HEX);
|
|
49
|
+
}
|
|
50
|
+
/** A platform's own user id - today only ChatGPT's `X-OpenAI-Subject`. */
|
|
51
|
+
user(platformUserId) {
|
|
52
|
+
const v = clean(platformUserId);
|
|
53
|
+
return v ? this.mac('user', v) : undefined;
|
|
54
|
+
}
|
|
55
|
+
/** A conversation. Never an address-keyed one (`ip:…`): see the top. */
|
|
56
|
+
conversation(source, id) {
|
|
57
|
+
const v = clean(id);
|
|
58
|
+
if (!v || v.startsWith('ip:'))
|
|
59
|
+
return undefined;
|
|
60
|
+
return this.mac(`conversation:${source}`, v);
|
|
61
|
+
}
|
|
62
|
+
/** A guest signed in to the deployment's own account: their Otaless member id (`memberUserId`) - nothing else of theirs. */
|
|
63
|
+
shopper(customerId) {
|
|
64
|
+
const v = typeof customerId === 'number' ? String(customerId) : clean(customerId);
|
|
65
|
+
return v ? this.mac('shopper', v) : undefined;
|
|
66
|
+
}
|
|
67
|
+
/** One of our booking ids - never the hotel's confirmation code. */
|
|
68
|
+
ref(curlessRef) {
|
|
69
|
+
const v = clean(curlessRef);
|
|
70
|
+
return v ? this.mac('ref', v) : undefined;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=pseudonym.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pseudonym.js","sourceRoot":"","sources":["../../src/usage/pseudonym.ts"],"names":[],"mappings":"AAAA,+EAA+E;AAC/E,8EAA8E;AAC9E,mCAAmC;AACnC,EAAE;AACF,+EAA+E;AAC/E,+DAA+D;AAC/D,EAAE;AACF,4EAA4E;AAC5E,sFAAsF;AACtF,2EAA2E;AAC3E,sEAAsE;AACtE,EAAE;AACF,iFAAiF;AACjF,iFAAiF;AACjF,+EAA+E;AAC/E,gFAAgF;AAChF,yEAAyE;AACzE,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAEzC,gFAAgF;AAChF,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,CAAC;AAEhC;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,CAAC;AAEhC,6EAA6E;AAC7E,MAAM,MAAM,GAAG,GAAG,CAAC;AAEnB,MAAM,KAAK,GAAG,CAAC,GAAY,EAAsB,EAAE;IACjD,IAAI,OAAO,GAAG,KAAK,QAAQ;QAAE,OAAO,SAAS,CAAC;IAC9C,MAAM,CAAC,GAAG,GAAG,CAAC,IAAI,EAAE,CAAC;IACrB,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;AACjD,CAAC,CAAC;AAQF,MAAM,OAAO,UAAU;IACJ,GAAG,CAAS;IAE7B,kIAAkI;IAClI,YAAY,MAAc;QACxB,IAAI,CAAC,MAAM,IAAI,MAAM,CAAC,MAAM,GAAG,aAAa,EAAE,CAAC;YAC7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AAC,CAAC,SAAS,CAAC;IAChD,CAAC;IAED,oEAAoE;IACpE,GAAG,CAAC,UAAmB;QACrB,MAAM,CAAC,GAAG,KAAK,CAAC,UAAU,CAAC,CAAC;QAC5B,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAC;IAC5C,CAAC;CACF"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { type Report, type Tz } from './report.js';
|
|
2
|
+
import { type UsageStore } from './store.js';
|
|
3
|
+
export declare const RANGES: readonly ["today", "yesterday", "7d", "30d", "90d", "all"];
|
|
4
|
+
export type Range = (typeof RANGES)[number];
|
|
5
|
+
export declare const isRange: (s: unknown) => s is Range;
|
|
6
|
+
export declare const windowOf: (store: UsageStore, range: Range, now: number, tz: Tz) => Promise<{
|
|
7
|
+
from: string;
|
|
8
|
+
to: string;
|
|
9
|
+
}>;
|
|
10
|
+
/**
|
|
11
|
+
* The most distinct ids an exact report holds before it gives up and sums the
|
|
12
|
+
* days instead (总线程's review of 0.77.1, P3: `--range all` over a flooded
|
|
13
|
+
* store held every conversation and connection at once).
|
|
14
|
+
*/
|
|
15
|
+
export declare const MAX_DISTINCT = 1000000;
|
|
16
|
+
export declare const reportFor: (store: UsageStore, deployment: string, window: {
|
|
17
|
+
from: string;
|
|
18
|
+
to: string;
|
|
19
|
+
}, opts?: {
|
|
20
|
+
now?: number;
|
|
21
|
+
tz?: Tz;
|
|
22
|
+
maxDistinct?: number;
|
|
23
|
+
}) => Promise<Report>;
|
|
24
|
+
//# sourceMappingURL=range.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"range.d.ts","sourceRoot":"","sources":["../../src/usage/range.ts"],"names":[],"mappings":"AAMA,OAAO,EAGL,KAAK,MAAM,EAEX,KAAK,EAAE,EAKR,MAAM,aAAa,CAAC;AACrB,OAAO,EAAE,KAAK,UAAU,EAAuB,MAAM,YAAY,CAAC;AAElE,eAAO,MAAM,MAAM,4DAA6D,CAAC;AACjF,MAAM,MAAM,KAAK,GAAG,CAAC,OAAO,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC;AAE5C,eAAO,MAAM,OAAO,GAAI,GAAG,OAAO,KAAG,CAAC,IAAI,KAAoC,CAAC;AAc/E,eAAO,MAAM,QAAQ,GACnB,OAAO,UAAU,EACjB,OAAO,KAAK,EACZ,KAAK,MAAM,EACX,IAAI,EAAE,KACL,OAAO,CAAC;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,CAgBtC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,YAAY,UAAY,CAAC;AAItC,eAAO,MAAM,SAAS,GACpB,OAAO,UAAU,EACjB,YAAY,MAAM,EAClB,QAAQ;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAA;CAAE,EACpC,OAAM;IAAE,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,EAAE,CAAC,EAAE,EAAE,CAAC;IAAC,WAAW,CAAC,EAAE,MAAM,CAAA;CAAO,KACzD,OAAO,CAAC,MAAM,CA+ChB,CAAC"}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// The six windows robin asked for (11-13): today, yesterday, 7, 30 and 90
|
|
2
|
+
// days, and everything - in Beijing days unless asked for UTC. Inside the 13
|
|
3
|
+
// months of event lines a window is counted exactly; one that reaches past them
|
|
4
|
+
// is the sum of the kept daily totals (report.ts sumDays), with the days not
|
|
5
|
+
// yet summed (today, say) summed from their lines.
|
|
6
|
+
import { Seen, readDaily } from './maintain.js';
|
|
7
|
+
import { BEIJING, ReportBuilder, addDays, dailyTotals, dayIn, sumDays, } from './report.js';
|
|
8
|
+
import { dayOf, firstKeptDay } from './store.js';
|
|
9
|
+
export const RANGES = ['today', 'yesterday', '7d', '30d', '90d', 'all'];
|
|
10
|
+
export const isRange = (s) => RANGES.includes(s);
|
|
11
|
+
/** The first local day anything is known for: the oldest totals or line. */
|
|
12
|
+
const earliestDay = async (store, tz) => {
|
|
13
|
+
const daily = await readDaily(store, '0000-00-00', '9999-99-99');
|
|
14
|
+
const firstTotals = daily[0]?.day ?? null;
|
|
15
|
+
let firstLine = null;
|
|
16
|
+
for await (const e of store.read()) {
|
|
17
|
+
firstLine = dayIn(e.t, tz);
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
return [firstTotals, firstLine].filter((d) => !!d).sort()[0] ?? null;
|
|
21
|
+
};
|
|
22
|
+
export const windowOf = async (store, range, now, tz) => {
|
|
23
|
+
const today = dayIn(now, tz);
|
|
24
|
+
switch (range) {
|
|
25
|
+
case 'today':
|
|
26
|
+
return { from: today, to: today };
|
|
27
|
+
case 'yesterday':
|
|
28
|
+
return { from: addDays(today, -1), to: addDays(today, -1) };
|
|
29
|
+
case '7d':
|
|
30
|
+
return { from: addDays(today, -6), to: today };
|
|
31
|
+
case '30d':
|
|
32
|
+
return { from: addDays(today, -29), to: today };
|
|
33
|
+
case '90d':
|
|
34
|
+
return { from: addDays(today, -89), to: today };
|
|
35
|
+
case 'all':
|
|
36
|
+
return { from: (await earliestDay(store, tz)) ?? today, to: today };
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
/**
|
|
40
|
+
* The most distinct ids an exact report holds before it gives up and sums the
|
|
41
|
+
* days instead (总线程's review of 0.77.1, P3: `--range all` over a flooded
|
|
42
|
+
* store held every conversation and connection at once).
|
|
43
|
+
*/
|
|
44
|
+
export const MAX_DISTINCT = 1_000_000;
|
|
45
|
+
class TooMany extends Error {
|
|
46
|
+
}
|
|
47
|
+
export const reportFor = async (store, deployment, window, opts = {}) => {
|
|
48
|
+
const now = opts.now ?? Date.now();
|
|
49
|
+
const tz = opts.tz ?? BEIJING;
|
|
50
|
+
const max = opts.maxDistinct ?? MAX_DISTINCT;
|
|
51
|
+
const { from, to } = window;
|
|
52
|
+
const firstKept = firstKeptDay(dayOf(now));
|
|
53
|
+
// The first local day whose lines are all still here.
|
|
54
|
+
const whole = addDays(firstKept, 1);
|
|
55
|
+
// Who is new: the maintenance's index when there is one - then only the
|
|
56
|
+
// window's own files are read; without it, every kept line before the window.
|
|
57
|
+
const seen = (await Seen.load(store)) ?? undefined;
|
|
58
|
+
const sum = async (a, b, bounded) => {
|
|
59
|
+
const r = new ReportBuilder(a, b, deployment, tz, seen);
|
|
60
|
+
let n = 0;
|
|
61
|
+
const check = () => {
|
|
62
|
+
if (bounded && ++n % 10_000 === 0 && r.distinct() > max)
|
|
63
|
+
throw new TooMany();
|
|
64
|
+
};
|
|
65
|
+
for await (const e of store.read(addDays(a, -1), addDays(b, 1))) {
|
|
66
|
+
r.place(e);
|
|
67
|
+
check();
|
|
68
|
+
}
|
|
69
|
+
for await (const e of store.read(seen ? addDays(a, -1) : firstKept, addDays(b, 1))) {
|
|
70
|
+
r.add(e);
|
|
71
|
+
check();
|
|
72
|
+
}
|
|
73
|
+
if (bounded && r.distinct() > max)
|
|
74
|
+
throw new TooMany();
|
|
75
|
+
return r.build();
|
|
76
|
+
};
|
|
77
|
+
const byDays = async (reason) => {
|
|
78
|
+
const kept = await readDaily(store, from, to);
|
|
79
|
+
const have = new Set(kept.map((d) => d.day));
|
|
80
|
+
const fill = [];
|
|
81
|
+
for (let d = whole > from ? whole : from; d <= to; d = addDays(d, 1)) {
|
|
82
|
+
if (have.has(d))
|
|
83
|
+
continue;
|
|
84
|
+
// One day at a time - a day's file is capped (store.ts), so this is bounded.
|
|
85
|
+
const one = dailyTotals(await sum(d, d, false), d);
|
|
86
|
+
if (one)
|
|
87
|
+
fill.push(one);
|
|
88
|
+
}
|
|
89
|
+
return sumDays([...kept, ...fill], from, to, deployment, tz, reason);
|
|
90
|
+
};
|
|
91
|
+
if (from < whole)
|
|
92
|
+
return byDays('past-13-months');
|
|
93
|
+
try {
|
|
94
|
+
return await sum(from, to, true);
|
|
95
|
+
}
|
|
96
|
+
catch (err) {
|
|
97
|
+
if (err instanceof TooMany)
|
|
98
|
+
return byDays('too-many');
|
|
99
|
+
throw err;
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
//# sourceMappingURL=range.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"range.js","sourceRoot":"","sources":["../../src/usage/range.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,6EAA6E;AAC7E,gFAAgF;AAChF,6EAA6E;AAC7E,mDAAmD;AACnD,OAAO,EAAE,IAAI,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAChD,OAAO,EACL,OAAO,EAGP,aAAa,EAEb,OAAO,EACP,WAAW,EACX,KAAK,EACL,OAAO,GACR,MAAM,aAAa,CAAC;AACrB,OAAO,EAAmB,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAElE,MAAM,CAAC,MAAM,MAAM,GAAG,CAAC,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,CAAU,CAAC;AAGjF,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,CAAU,EAAc,EAAE,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAU,CAAC,CAAC;AAE/E,4EAA4E;AAC5E,MAAM,WAAW,GAAG,KAAK,EAAE,KAAiB,EAAE,EAAM,EAA0B,EAAE;IAC9E,MAAM,KAAK,GAAG,MAAM,SAAS,CAAC,KAAK,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;IACjE,MAAM,WAAW,GAAG,KAAK,CAAC,CAAC,CAAC,EAAE,GAAG,IAAI,IAAI,CAAC;IAC1C,IAAI,SAAS,GAAkB,IAAI,CAAC;IACpC,IAAI,KAAK,EAAE,MAAM,CAAC,IAAI,KAAK,CAAC,IAAI,EAAE,EAAE,CAAC;QACnC,SAAS,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;QAC3B,MAAM;IACR,CAAC;IACD,OAAO,CAAC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C,EAAE,CAAC,EAAE,KAAK,CAAC,EAAE,CAAC,CAAC,CAAC;YACnD,IAAI,GAAG;gBAAE,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;QAC1B,CAAC;QACD,OAAO,OAAO,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,IAAI,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC;IACvE,CAAC,CAAC;IACF,IAAI,IAAI,GAAG,KAAK;QAAE,OAAO,MAAM,CAAC,gBAAgB,CAAC,CAAC;IAClD,IAAI,CAAC;QACH,OAAO,MAAM,GAAG,CAAC,IAAI,EAAE,EAAE,EAAE,IAAI,CAAC,CAAC;IACnC,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,IAAI,GAAG,YAAY,OAAO;YAAE,OAAO,MAAM,CAAC,UAAU,CAAC,CAAC;QACtD,MAAM,GAAG,CAAC;IACZ,CAAC;AACH,CAAC,CAAC"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import type { GeoDb, NetKind } from './geo.js';
|
|
2
|
+
import { type Platform, type Ranges } from './platform.js';
|
|
3
|
+
import type { Pseudonyms } from './pseudonym.js';
|
|
4
|
+
import type { AccountStep, ToolRole, UsageStore } from './store.js';
|
|
5
|
+
/** What an event may carry about who and where - and nothing else. */
|
|
6
|
+
export type Facts = {
|
|
7
|
+
p: Platform;
|
|
8
|
+
u?: string;
|
|
9
|
+
c?: string;
|
|
10
|
+
k?: string;
|
|
11
|
+
g: string;
|
|
12
|
+
n?: NetKind;
|
|
13
|
+
};
|
|
14
|
+
export type HeaderSource = {
|
|
15
|
+
get(name: string): string | null | undefined;
|
|
16
|
+
};
|
|
17
|
+
export type RecorderDeps = {
|
|
18
|
+
deployment: string;
|
|
19
|
+
store: UsageStore;
|
|
20
|
+
pseudo: Pseudonyms;
|
|
21
|
+
/** The country database in use now (read again daily). */
|
|
22
|
+
geo: () => Pick<GeoDb, 'lookup'>;
|
|
23
|
+
/** OpenAI's published addresses - the only source a guest's address is taken from. */
|
|
24
|
+
openai: Pick<Ranges, 'has'>;
|
|
25
|
+
/** What a tool is for. */
|
|
26
|
+
roleOf: (tool: string) => ToolRole;
|
|
27
|
+
now?: () => number;
|
|
28
|
+
};
|
|
29
|
+
/** The header Caddy is asked to copy the caller's own X-Forwarded-For into. */
|
|
30
|
+
export declare const CALLER_FORWARDED = "x-caller-forwarded-for";
|
|
31
|
+
/** A failure's category for the record: a short code, never free text. */
|
|
32
|
+
export declare const whyCode: (raw: unknown) => string | undefined;
|
|
33
|
+
/** What a client called itself, printable and at most 60 characters. */
|
|
34
|
+
export declare const clientLabel: (raw: unknown) => string | undefined;
|
|
35
|
+
/** A group's code as an event may carry it: `[a-z0-9-]`, the backend's own providerCode. */
|
|
36
|
+
export declare const shopCode: (raw: unknown) => string | undefined;
|
|
37
|
+
export declare class UsageRecorder {
|
|
38
|
+
private readonly deps;
|
|
39
|
+
private readonly keys;
|
|
40
|
+
private readonly refs;
|
|
41
|
+
constructor(deps: RecorderDeps);
|
|
42
|
+
private now;
|
|
43
|
+
private safe;
|
|
44
|
+
private emit;
|
|
45
|
+
/**
|
|
46
|
+
* Who and where, for this request. `connection` is the MCP session it came
|
|
47
|
+
* in on; `conversation` is the conversation it acts for - for a client that
|
|
48
|
+
* draws no card, carried across its connections (server.ts `own`), for a card,
|
|
49
|
+
* the conversation that drew it. `peer` is the address our proxy saw.
|
|
50
|
+
*/
|
|
51
|
+
facts(h: HeaderSource, opts?: {
|
|
52
|
+
peer?: string | undefined;
|
|
53
|
+
client?: string | undefined;
|
|
54
|
+
connection?: string | undefined;
|
|
55
|
+
conversation?: string | undefined;
|
|
56
|
+
}): Facts;
|
|
57
|
+
init(f: Facts, x: {
|
|
58
|
+
client?: string | undefined;
|
|
59
|
+
cards: 'extension' | 'host' | 'none';
|
|
60
|
+
}): void;
|
|
61
|
+
call(f: Facts, x: {
|
|
62
|
+
tool: string;
|
|
63
|
+
ok: boolean;
|
|
64
|
+
ms: number;
|
|
65
|
+
why?: string | undefined;
|
|
66
|
+
shop?: string;
|
|
67
|
+
}): void;
|
|
68
|
+
/** Stays went into the trip: how many more lines it holds after the call. */
|
|
69
|
+
cartAdd(f: Facts, x: {
|
|
70
|
+
tool: string;
|
|
71
|
+
added: number;
|
|
72
|
+
shop?: string;
|
|
73
|
+
}): void;
|
|
74
|
+
/** A booking's reference, pseudonymised - and whether it was seen before (once per reference). */
|
|
75
|
+
private ref;
|
|
76
|
+
/**
|
|
77
|
+
* A booking asked for, one stay of it: its outcome (`booking`) - and, made or
|
|
78
|
+
* being confirmed, the funnel's last step (`checkout`). Once per booking id:
|
|
79
|
+
* a booking asked again by the same words comes back as the one made before.
|
|
80
|
+
* Never an amount, a currency, a hotel or a confirmation code.
|
|
81
|
+
*/
|
|
82
|
+
booking(f: Facts, x: {
|
|
83
|
+
res: 'ok' | 'fail' | 'checking';
|
|
84
|
+
id?: string | undefined;
|
|
85
|
+
kind: 'cart' | 'item';
|
|
86
|
+
via: 'model' | 'card';
|
|
87
|
+
signedIn?: boolean | undefined;
|
|
88
|
+
why?: string | undefined;
|
|
89
|
+
shop?: string | undefined;
|
|
90
|
+
}): void;
|
|
91
|
+
/** The guest's own account. `member` is the backend's member id - pseudonymised here, never kept as it came. */
|
|
92
|
+
account(f: Facts, x: {
|
|
93
|
+
step: AccountStep;
|
|
94
|
+
why?: string | undefined;
|
|
95
|
+
member?: string | undefined;
|
|
96
|
+
signUp?: boolean | undefined;
|
|
97
|
+
}): void;
|
|
98
|
+
/** A hotel group's membership linked to the account: the group in `shop`, the account as `s`. */
|
|
99
|
+
member(f: Facts, x: {
|
|
100
|
+
step: AccountStep;
|
|
101
|
+
group?: string | undefined;
|
|
102
|
+
why?: string | undefined;
|
|
103
|
+
member?: string | undefined;
|
|
104
|
+
signUp?: boolean | undefined;
|
|
105
|
+
}): void;
|
|
106
|
+
/**
|
|
107
|
+
* Remember whose conversation `key` is, so an account step finished on our
|
|
108
|
+
* own page - a request from the guest's browser, not the platform - is
|
|
109
|
+
* counted against the conversation that began it.
|
|
110
|
+
*/
|
|
111
|
+
bindKey(key: string | undefined, f: Facts): void;
|
|
112
|
+
/** The facts last bound to a conversation - or a platform unknown, nowhere. */
|
|
113
|
+
factsOf(key: string): Facts;
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=recorder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recorder.d.ts","sourceRoot":"","sources":["../../src/usage/recorder.ts"],"names":[],"mappings":"AAaA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAC/C,OAAO,EAAE,KAAK,QAAQ,EAAE,KAAK,MAAM,EAAyB,MAAM,eAAe,CAAC;AAClF,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACjD,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAc,UAAU,EAAE,MAAM,YAAY,CAAC;AAEhF,sEAAsE;AACtE,MAAM,MAAM,KAAK,GAAG;IAClB,CAAC,EAAE,QAAQ,CAAC;IACZ,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,CAAC,CAAC,EAAE,MAAM,CAAC;IACX,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,CAAC,EAAE,OAAO,CAAC;CACb,CAAC;AAEF,MAAM,MAAM,YAAY,GAAG;IAAE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,CAAA;CAAE,CAAC;AAE5E,MAAM,MAAM,YAAY,GAAG;IACzB,UAAU,EAAE,MAAM,CAAC;IACnB,KAAK,EAAE,UAAU,CAAC;IAClB,MAAM,EAAE,UAAU,CAAC;IACnB,0DAA0D;IAC1D,GAAG,EAAE,MAAM,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,CAAC;IACjC,sFAAsF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