vidspotai-shared 1.0.146 → 1.0.148

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.
Files changed (67) hide show
  1. package/lib/globals/plans.d.ts +17 -0
  2. package/lib/globals/plans.d.ts.map +1 -1
  3. package/lib/globals/plans.js +11 -1
  4. package/lib/globals/types.d.ts +2 -1
  5. package/lib/globals/types.d.ts.map +1 -1
  6. package/lib/globals/types.js +5 -0
  7. package/lib/models/agent.model.d.ts +6 -0
  8. package/lib/models/agent.model.d.ts.map +1 -1
  9. package/lib/models/demoVideoProject.model.d.ts +6 -0
  10. package/lib/models/demoVideoProject.model.d.ts.map +1 -1
  11. package/lib/models/image.model.d.ts +6 -0
  12. package/lib/models/image.model.d.ts.map +1 -1
  13. package/lib/models/musicJob.model.d.ts +6 -0
  14. package/lib/models/musicJob.model.d.ts.map +1 -1
  15. package/lib/models/upscale.model.d.ts +6 -0
  16. package/lib/models/upscale.model.d.ts.map +1 -1
  17. package/lib/models/user.model.d.ts +68 -1
  18. package/lib/models/user.model.d.ts.map +1 -1
  19. package/lib/models/video.model.d.ts +43 -0
  20. package/lib/models/video.model.d.ts.map +1 -1
  21. package/lib/models/videoTool.model.d.ts +6 -0
  22. package/lib/models/videoTool.model.d.ts.map +1 -1
  23. package/lib/services/accountStatus/index.d.ts +35 -0
  24. package/lib/services/accountStatus/index.d.ts.map +1 -1
  25. package/lib/services/accountStatus/index.js +54 -0
  26. package/lib/services/agent/repair/engine.d.ts.map +1 -1
  27. package/lib/services/agent/repair/engine.js +18 -0
  28. package/lib/services/agent/repair/strategies/llmRepair.strategy.d.ts.map +1 -1
  29. package/lib/services/agent/repair/strategies/llmRepair.strategy.js +20 -8
  30. package/lib/services/bullmq.service.d.ts +18 -1
  31. package/lib/services/bullmq.service.d.ts.map +1 -1
  32. package/lib/services/bullmq.service.js +13 -0
  33. package/lib/services/credit.service.d.ts +61 -1
  34. package/lib/services/credit.service.d.ts.map +1 -1
  35. package/lib/services/credit.service.js +124 -52
  36. package/lib/services/credits/grant.d.ts +105 -0
  37. package/lib/services/credits/grant.d.ts.map +1 -0
  38. package/lib/services/credits/grant.js +129 -0
  39. package/lib/services/demo/demoPrefill.d.ts.map +1 -1
  40. package/lib/services/demo/demoPrefill.js +101 -84
  41. package/lib/services/index.d.ts +1 -0
  42. package/lib/services/index.d.ts.map +1 -1
  43. package/lib/services/index.js +1 -0
  44. package/lib/services/moderation/generationModeration.service.d.ts +52 -0
  45. package/lib/services/moderation/generationModeration.service.d.ts.map +1 -0
  46. package/lib/services/moderation/generationModeration.service.js +260 -0
  47. package/lib/services/moderation/index.d.ts +4 -0
  48. package/lib/services/moderation/index.d.ts.map +1 -1
  49. package/lib/services/moderation/index.js +14 -1
  50. package/lib/services/moderation/moderationStrike.service.d.ts +67 -0
  51. package/lib/services/moderation/moderationStrike.service.d.ts.map +1 -0
  52. package/lib/services/moderation/moderationStrike.service.js +134 -0
  53. package/lib/services/rateLimiter/index.d.ts +1 -0
  54. package/lib/services/rateLimiter/index.d.ts.map +1 -1
  55. package/lib/services/rateLimiter/index.js +1 -0
  56. package/lib/services/rateLimiter/userRateLimiter.service.d.ts +55 -0
  57. package/lib/services/rateLimiter/userRateLimiter.service.d.ts.map +1 -0
  58. package/lib/services/rateLimiter/userRateLimiter.service.js +220 -0
  59. package/lib/services/trialGuard/trialSignals.d.ts.map +1 -1
  60. package/lib/services/trialGuard/trialSignals.js +15 -0
  61. package/lib/utils/index.d.ts +1 -0
  62. package/lib/utils/index.d.ts.map +1 -1
  63. package/lib/utils/index.js +1 -0
  64. package/lib/utils/ssrfGuard.d.ts +59 -0
  65. package/lib/utils/ssrfGuard.d.ts.map +1 -0
  66. package/lib/utils/ssrfGuard.js +253 -0
  67. package/package.json +1 -1
@@ -0,0 +1,129 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readCreditState = readCreditState;
4
+ exports.computeCreditGrant = computeCreditGrant;
5
+ exports.computeCreditClawback = computeCreditClawback;
6
+ exports.creditStateUpdate = creditStateUpdate;
7
+ exports.applyCreditGrantInTx = applyCreditGrantInTx;
8
+ exports.applyCreditClawbackInTx = applyCreditClawbackInTx;
9
+ function num(v) {
10
+ return typeof v === "number" && Number.isFinite(v) ? v : 0;
11
+ }
12
+ /** Reads a user doc's `credits` map defensively (missing fields → 0). */
13
+ function readCreditState(userData) {
14
+ const c = (userData?.credits ?? {});
15
+ return {
16
+ total: Math.max(0, num(c.total)),
17
+ consumed: Math.max(0, num(c.consumed)),
18
+ owed: Math.max(0, num(c.owed)),
19
+ topUpBalance: Math.max(0, num(c.topUpBalance)),
20
+ };
21
+ }
22
+ function remaining(s) {
23
+ return Math.max(0, s.total - s.consumed);
24
+ }
25
+ /** Moves as much of `owed` as the balance allows into `consumed`. */
26
+ function payOwed(s) {
27
+ const paid = Math.min(s.owed, remaining(s));
28
+ return {
29
+ state: { ...s, consumed: s.consumed + paid, owed: s.owed - paid },
30
+ paid,
31
+ };
32
+ }
33
+ function clampTopUp(s) {
34
+ return { ...s, topUpBalance: Math.min(Math.max(0, s.topUpBalance), remaining(s)) };
35
+ }
36
+ /** Pure: the ledger after a grant. Unit-testable without Firestore. */
37
+ function computeCreditGrant(before, grant) {
38
+ const amount = Math.max(0, Math.floor(grant.kind === "plan_reset" ? grant.planCredits : grant.credits));
39
+ let next;
40
+ switch (grant.kind) {
41
+ case "plan_reset": {
42
+ // Remaining top-up credits survive the reset; plan credits are spent
43
+ // first, so at most the whole remaining balance is carried.
44
+ const carried = Math.min(before.topUpBalance, remaining(before));
45
+ next = { total: amount + carried, consumed: 0, owed: before.owed, topUpBalance: carried };
46
+ break;
47
+ }
48
+ case "top_up":
49
+ next = {
50
+ ...before,
51
+ total: before.total + amount,
52
+ topUpBalance: before.topUpBalance + amount,
53
+ };
54
+ break;
55
+ case "plan_delta":
56
+ case "bonus":
57
+ next = { ...before, total: before.total + amount };
58
+ break;
59
+ }
60
+ // Debt is paid before anything becomes spendable. topUpBalance is clamped
61
+ // afterwards, so owed is paid out of plan credits first, then top-ups.
62
+ const { state, paid } = payOwed(next);
63
+ return { before, after: clampTopUp(state), paidOwed: paid, revoked: 0 };
64
+ }
65
+ /** Pure: the ledger after a refund/dispute clawback. */
66
+ function computeCreditClawback(before, clawback) {
67
+ const rem = remaining(before);
68
+ const carriedTopUp = Math.min(before.topUpBalance, rem);
69
+ let revoked;
70
+ switch (clawback.kind) {
71
+ case "revoke_all":
72
+ revoked = rem;
73
+ break;
74
+ case "revoke_plan":
75
+ revoked = rem - carriedTopUp;
76
+ break;
77
+ case "revoke_top_up":
78
+ revoked = Math.min(Math.max(0, Math.floor(clawback.credits)), rem);
79
+ break;
80
+ }
81
+ const total = before.total - revoked;
82
+ let topUpBalance;
83
+ switch (clawback.kind) {
84
+ case "revoke_all":
85
+ topUpBalance = 0;
86
+ break;
87
+ case "revoke_plan":
88
+ topUpBalance = carriedTopUp;
89
+ break;
90
+ case "revoke_top_up":
91
+ topUpBalance = Math.max(0, carriedTopUp - revoked);
92
+ break;
93
+ }
94
+ const after = clampTopUp({ ...before, total, topUpBalance });
95
+ return { before, after, paidOwed: 0, revoked };
96
+ }
97
+ /** Dot-path update for the ledger fields only (never clobbers `reserved`). */
98
+ function creditStateUpdate(s) {
99
+ return {
100
+ "credits.total": s.total,
101
+ "credits.consumed": s.consumed,
102
+ "credits.owed": s.owed,
103
+ "credits.topUpBalance": s.topUpBalance,
104
+ };
105
+ }
106
+ /**
107
+ * Transactional grant. The caller passes the user doc's data if it already
108
+ * read it inside `tx` (Firestore requires all reads before writes); otherwise
109
+ * it is read here. `extra` is merged into the same `tx.update` (dot paths),
110
+ * so a grant and the subscription fields it belongs to commit atomically.
111
+ */
112
+ async function applyCreditGrantInTx(tx, ref, grant, opts = {}) {
113
+ const data = opts.userData ?? (await tx.get(ref)).data();
114
+ const result = computeCreditGrant(readCreditState(data), grant);
115
+ tx.update(ref, { ...creditStateUpdate(result.after), ...(opts.extra ?? {}) });
116
+ return result;
117
+ }
118
+ /** Transactional clawback; same read/write contract as applyCreditGrantInTx. */
119
+ async function applyCreditClawbackInTx(tx, ref, clawback, opts = {}) {
120
+ const data = opts.userData ?? (await tx.get(ref)).data();
121
+ const result = computeCreditClawback(readCreditState(data), clawback);
122
+ if (result.revoked > 0 || result.after.topUpBalance !== result.before.topUpBalance) {
123
+ tx.update(ref, { ...creditStateUpdate(result.after), ...(opts.extra ?? {}) });
124
+ }
125
+ else if (opts.extra && Object.keys(opts.extra).length > 0) {
126
+ tx.update(ref, opts.extra);
127
+ }
128
+ return result;
129
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"demoPrefill.d.ts","sourceRoot":"","sources":["../../../src/services/demo/demoPrefill.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAE/C,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAC;AA0BhE,MAAM,WAAW,iBAAiB;IAChC,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,EAAE,MAAM,CAAC;CACnB;AAED,MAAM,WAAW,iBAAiB;IAChC,MAAM,EAAE,iBAAiB,CAAC;IAC1B,+EAA+E;IAC/E,MAAM,EAAE,KAAK,GAAG,UAAU,CAAC;IAC3B,SAAS,CAAC,EAAE,WAAW,CAAC;IACxB,KAAK,CAAC,EAAE;QAAE,YAAY,CAAC,EAAE,MAAM,CAAC;QAAC,gBAAgB,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;CAC9D;AAED,wEAAwE;AACxE,MAAM,MAAM,oBAAoB,GAC5B,aAAa,GACb,cAAc,GACd,aAAa,GACb,YAAY,CAAC;AAEjB,qBAAa,gBAAiB,SAAQ,KAAK;aAEvB,IAAI,EAAE,oBAAoB;gBAA1B,IAAI,EAAE,oBAAoB,EAC1C,OAAO,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B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAC;CACd;AA+ED,wBAAgB,eAAe,CAAC,IAAI,EAAE,MAAM,GAAG,YAAY,CAc1D;AA4ED;;;;;;;GAOG;AACH,MAAM,WAAW,eAAe;IAC9B,oEAAoE;IACpE,MAAM,EAAE,iBAAiB,CAAC;IAC1B,+EAA+E;IAC/E,OAAO,EAAE,MAAM,CAAC;CACjB;AAKD;;;;;;GAMG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,MAAM,EACd,IAAI,GAAE,IAAI,CAAC,eAAe,EAAE,WAAW,GAAG,cAAc,GAAG,UAAU,CAAM,GAC1E,OAAO,CAAC,eAAe,CAAC,CAqC1B;AAED;;;;;GAKG;AACH,wBAAsB,iBAAiB,CACrC,MAAM,EAAE,MAAM,EACd,IAAI,GAAE,eAAoB,GACzB,OAAO,CAAC,iBAAiB,CAAC,CA8C5B"}
@@ -4,9 +4,11 @@ exports.DemoPrefillError = void 0;
4
4
  exports.extractPageMeta = extractPageMeta;
5
5
  exports.gatherPageSignals = gatherPageSignals;
6
6
  exports.prefillDemoBasics = prefillDemoBasics;
7
- const promises_1 = require("dns/promises");
8
- const net_1 = require("net");
7
+ // REMOVED FOR v1 — kept for reference: guard moved to utils/ssrfGuard.ts
8
+ // import { lookup } from "dns/promises";
9
+ // import { isIP } from "net";
9
10
  const zod_1 = require("zod");
11
+ const ssrfGuard_1 = require("../../utils/ssrfGuard");
10
12
  const llmCallerRegistry_1 = require("../agent/llmCallerRegistry");
11
13
  const modelRouter_1 = require("../agent/modelRouter");
12
14
  /**
@@ -40,94 +42,109 @@ class DemoPrefillError extends Error {
40
42
  }
41
43
  exports.DemoPrefillError = DemoPrefillError;
42
44
  // ── SSRF guard ───────────────────────────────────────────────────────────────
43
- /** IPv4/IPv6 ranges we must never fetch (cloud metadata, loopback, LAN, etc.). */
44
- function isPrivateAddress(ip) {
45
- const v = (0, net_1.isIP)(ip);
46
- if (v === 4) {
47
- const p = ip.split(".").map(Number);
48
- if (p.length !== 4 || p.some((n) => Number.isNaN(n)))
49
- return true;
50
- const a = p[0];
51
- const b = p[1];
52
- if (a === 10)
53
- return true; // 10.0.0.0/8
54
- if (a === 127)
55
- return true; // loopback
56
- if (a === 0)
57
- return true; // "this" network
58
- if (a === 169 && b === 254)
59
- return true; // link-local incl. 169.254.169.254
60
- if (a === 172 && b >= 16 && b <= 31)
61
- return true; // 172.16.0.0/12
62
- if (a === 192 && b === 168)
63
- return true; // 192.168.0.0/16
64
- if (a === 100 && b >= 64 && b <= 127)
65
- return true; // CGNAT 100.64.0.0/10
66
- if (a >= 224)
67
- return true; // multicast / reserved
68
- return false;
69
- }
70
- if (v === 6) {
71
- const lower = ip.toLowerCase();
72
- if (lower === "::1" || lower === "::")
73
- return true; // loopback / unspecified
74
- if (lower.startsWith("fc") || lower.startsWith("fd"))
75
- return true; // ULA fc00::/7
76
- if (lower.startsWith("fe80"))
77
- return true; // link-local
78
- // IPv4-mapped (::ffff:a.b.c.d) — extract and re-check as v4.
79
- const mapped = lower.match(/::ffff:(\d+\.\d+\.\d+\.\d+)$/);
80
- if (mapped?.[1])
81
- return isPrivateAddress(mapped[1]);
82
- return false;
83
- }
84
- return true; // not an IP literal ⇒ treat as unsafe
85
- }
86
- /**
87
- * Validate a URL is a public http(s) target: parseable, http/https, not a
88
- * localhost-ish hostname, and every resolved IP is public. Throws a
89
- * DemoPrefillError otherwise. Called on the initial URL AND every redirect hop.
90
- */
45
+ // WHY (2026-11-25): the guard below was extracted to utils/ssrfGuard.ts so the
46
+ // worker's headless-browser capture, browser sessions, video-tool downloads
47
+ // and webhook delivery share ONE implementation (it also now handles the
48
+ // hex-normalised IPv4-mapped form `[::ffff:7f00:1]` that WHATWG URL produces,
49
+ // which the old regex missed). This thin wrapper keeps DemoPrefillError codes.
91
50
  async function assertPublicHttpUrl(raw) {
92
- let url;
93
51
  try {
94
- url = new URL(raw);
95
- }
96
- catch {
97
- throw new DemoPrefillError("invalid_url", "That doesn't look like a valid URL.");
98
- }
99
- if (url.protocol !== "http:" && url.protocol !== "https:") {
100
- throw new DemoPrefillError("invalid_url", "Only http(s) URLs are supported.");
52
+ return await (0, ssrfGuard_1.assertPublicUrl)(raw);
101
53
  }
102
- const host = url.hostname.replace(/^\[|\]$/g, ""); // strip IPv6 brackets
103
- // Obvious internal hostnames — reject before any DNS.
104
- const lowered = host.toLowerCase();
105
- if (lowered === "localhost" ||
106
- lowered.endsWith(".localhost") ||
107
- lowered.endsWith(".local") ||
108
- lowered.endsWith(".internal") ||
109
- lowered === "metadata.google.internal") {
110
- throw new DemoPrefillError("blocked_host", "That host isn't reachable.");
111
- }
112
- // Literal IP ⇒ check directly; hostname ⇒ resolve every address and check all.
113
- if ((0, net_1.isIP)(host)) {
114
- if (isPrivateAddress(host)) {
115
- throw new DemoPrefillError("blocked_host", "That host isn't reachable.");
54
+ catch (err) {
55
+ if (err instanceof ssrfGuard_1.SsrfBlockedError) {
56
+ if (err.reason === "unresolvable") {
57
+ throw new DemoPrefillError("unreachable", "We couldn't reach that site.");
58
+ }
59
+ if (err.reason === "blocked_host") {
60
+ throw new DemoPrefillError("blocked_host", "That host isn't reachable.");
61
+ }
62
+ throw new DemoPrefillError("invalid_url", err.message);
116
63
  }
117
- return url;
118
- }
119
- let addresses;
120
- try {
121
- addresses = await (0, promises_1.lookup)(host, { all: true });
122
- }
123
- catch {
124
- throw new DemoPrefillError("unreachable", "We couldn't reach that site.");
125
- }
126
- if (addresses.length === 0 || addresses.some((a) => isPrivateAddress(a.address))) {
127
- throw new DemoPrefillError("blocked_host", "That host isn't reachable.");
64
+ throw err;
128
65
  }
129
- return url;
130
66
  }
67
+ // REMOVED FOR v1 — kept for reference (superseded by utils/ssrfGuard.ts)
68
+ // // ── SSRF guard ───────────────────────────────────────────────────────────────
69
+ //
70
+ // /** IPv4/IPv6 ranges we must never fetch (cloud metadata, loopback, LAN, etc.). */
71
+ // function isPrivateAddress(ip: string): boolean {
72
+ // const v = isIP(ip);
73
+ // if (v === 4) {
74
+ // const p = ip.split(".").map(Number);
75
+ // if (p.length !== 4 || p.some((n) => Number.isNaN(n))) return true;
76
+ // const a = p[0]!;
77
+ // const b = p[1]!;
78
+ // if (a === 10) return true; // 10.0.0.0/8
79
+ // if (a === 127) return true; // loopback
80
+ // if (a === 0) return true; // "this" network
81
+ // if (a === 169 && b === 254) return true; // link-local incl. 169.254.169.254
82
+ // if (a === 172 && b >= 16 && b <= 31) return true; // 172.16.0.0/12
83
+ // if (a === 192 && b === 168) return true; // 192.168.0.0/16
84
+ // if (a === 100 && b >= 64 && b <= 127) return true; // CGNAT 100.64.0.0/10
85
+ // if (a >= 224) return true; // multicast / reserved
86
+ // return false;
87
+ // }
88
+ // if (v === 6) {
89
+ // const lower = ip.toLowerCase();
90
+ // if (lower === "::1" || lower === "::") return true; // loopback / unspecified
91
+ // if (lower.startsWith("fc") || lower.startsWith("fd")) return true; // ULA fc00::/7
92
+ // if (lower.startsWith("fe80")) return true; // link-local
93
+ // // IPv4-mapped (::ffff:a.b.c.d) — extract and re-check as v4.
94
+ // const mapped = lower.match(/::ffff:(\d+\.\d+\.\d+\.\d+)$/);
95
+ // if (mapped?.[1]) return isPrivateAddress(mapped[1]);
96
+ // return false;
97
+ // }
98
+ // return true; // not an IP literal ⇒ treat as unsafe
99
+ // }
100
+ //
101
+ // /**
102
+ // * Validate a URL is a public http(s) target: parseable, http/https, not a
103
+ // * localhost-ish hostname, and every resolved IP is public. Throws a
104
+ // * DemoPrefillError otherwise. Called on the initial URL AND every redirect hop.
105
+ // */
106
+ // async function assertPublicHttpUrl(raw: string): Promise<URL> {
107
+ // let url: URL;
108
+ // try {
109
+ // url = new URL(raw);
110
+ // } catch {
111
+ // throw new DemoPrefillError("invalid_url", "That doesn't look like a valid URL.");
112
+ // }
113
+ // if (url.protocol !== "http:" && url.protocol !== "https:") {
114
+ // throw new DemoPrefillError("invalid_url", "Only http(s) URLs are supported.");
115
+ // }
116
+ // const host = url.hostname.replace(/^\[|\]$/g, ""); // strip IPv6 brackets
117
+ //
118
+ // // Obvious internal hostnames — reject before any DNS.
119
+ // const lowered = host.toLowerCase();
120
+ // if (
121
+ // lowered === "localhost" ||
122
+ // lowered.endsWith(".localhost") ||
123
+ // lowered.endsWith(".local") ||
124
+ // lowered.endsWith(".internal") ||
125
+ // lowered === "metadata.google.internal"
126
+ // ) {
127
+ // throw new DemoPrefillError("blocked_host", "That host isn't reachable.");
128
+ // }
129
+ //
130
+ // // Literal IP ⇒ check directly; hostname ⇒ resolve every address and check all.
131
+ // if (isIP(host)) {
132
+ // if (isPrivateAddress(host)) {
133
+ // throw new DemoPrefillError("blocked_host", "That host isn't reachable.");
134
+ // }
135
+ // return url;
136
+ // }
137
+ // let addresses: { address: string }[];
138
+ // try {
139
+ // addresses = await lookup(host, { all: true });
140
+ // } catch {
141
+ // throw new DemoPrefillError("unreachable", "We couldn't reach that site.");
142
+ // }
143
+ // if (addresses.length === 0 || addresses.some((a) => isPrivateAddress(a.address))) {
144
+ // throw new DemoPrefillError("blocked_host", "That host isn't reachable.");
145
+ // }
146
+ // return url;
147
+ // }
131
148
  // ── Fetch (SSRF-safe, manual redirects) ──────────────────────────────────────
132
149
  async function safeFetchHtml(initialUrl, deps) {
133
150
  let target = initialUrl;
@@ -11,6 +11,7 @@ export * from "./credit.service";
11
11
  export * from "./notification.service";
12
12
  export * from "./credits/pricing";
13
13
  export * from "./credits/unlimitedPlanImageLimiter";
14
+ export * from "./credits/grant";
14
15
  export * from "./analytics.service";
15
16
  export * from "./providerHealth.service";
16
17
  export * from "./tts";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/services/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC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AAChC,cAAc,iBAAiB,CAAC;AAChC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,gBAAgB,CAAC;AAC/B,cAAc,YAAY,CAAC;AAC3B,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,YAAY,CAAC;AAC3B,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,8BAA8B,CAAC;AAC7C,cAAc,oBAAoB,CAAC;AACnC,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC;AAC5B,cAAc,6CAA6C,CAAC;AAC5D,cAAc,0BAA0B,CAAC;AACzC,cAAc,oBAAoB,CAAC;AACnC,cAAc,mBAAmB,CAAC;AAClC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,wBAAwB,CAAC;AACvC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC"}
@@ -27,6 +27,7 @@ __exportStar(require("./credit.service"), exports);
27
27
  __exportStar(require("./notification.service"), exports);
28
28
  __exportStar(require("./credits/pricing"), exports);
29
29
  __exportStar(require("./credits/unlimitedPlanImageLimiter"), exports);
30
+ __exportStar(require("./credits/grant"), exports);
30
31
  __exportStar(require("./analytics.service"), exports);
31
32
  __exportStar(require("./providerHealth.service"), exports);
32
33
  __exportStar(require("./tts"), exports);
@@ -0,0 +1,52 @@
1
+ /**
2
+ * Pre-generation moderation gate (2026-11-25).
3
+ *
4
+ * WHY: an abuser submitted ~950 generations, 289 of them "undressing" prompts
5
+ * (Korean) against uploaded photos of real people. Every generation surface was
6
+ * reactive-only — the provider rejected mid-flight after credits moved — and
7
+ * nothing tied repeat attempts to the account. This runs BEFORE any charge /
8
+ * enqueue on every generation entry point (REST route middleware, MCP tools,
9
+ * agent execute) and blocks ONLY:
10
+ * - sexual content involving minors (OpenAI `sexual/minors`, any score),
11
+ * - sexual intent combined with an uploaded/reference image (real-person
12
+ * sexualization / "nudify"),
13
+ * - unambiguous text-only sexual content (`sexual` score above a high bar).
14
+ *
15
+ * Deliberately NOT blocked (owner's standing rule, 2026-11-01): violence,
16
+ * horror, action, brands/franchises/IP. Do not add those categories here.
17
+ *
18
+ * FAIL-OPEN: an OpenAI outage / timeout must never take generation down, so any
19
+ * error resolves to `{ blocked: false }` with a logger.warn. The keyword
20
+ * prefilter below still runs without OpenAI, so the exact abuse pattern we saw
21
+ * (undress-intent + photo) is blocked even while OpenAI is unreachable.
22
+ */
23
+ export interface GenerationModerationInput {
24
+ texts: string[];
25
+ imageUrls?: string[];
26
+ }
27
+ export interface GenerationModerationResult {
28
+ blocked: boolean;
29
+ /** Categories that drove the decision (e.g. "sexual/minors", "keyword:undress"). */
30
+ categories: string[];
31
+ /** Internal reason (logged / stored on the strike); not shown verbatim to users. */
32
+ reason?: string;
33
+ /** True when OpenAI could not be consulted and the verdict is prefilter-only. */
34
+ failedOpen?: boolean;
35
+ }
36
+ /** User-facing copy for a blocked generation (422 content_policy_violation). */
37
+ export declare const CONTENT_POLICY_VIOLATION_MESSAGE = "This request can't be generated: it appears to involve sexual content involving minors or sexualizing a real person, which our content policy doesn't allow. Repeated attempts will suspend your account.";
38
+ export declare const CONTENT_POLICY_VIOLATION_CODE = "content_policy_violation";
39
+ export declare function hasUndressIntent(text: string): boolean;
40
+ /**
41
+ * Moderate a generation request's text + image inputs. Never throws.
42
+ */
43
+ export declare function moderateGenerationInput(input: GenerationModerationInput): Promise<GenerationModerationResult>;
44
+ /**
45
+ * Walk an arbitrary request body / plan / tool-args object and collect the
46
+ * creative texts and image URLs that a generation would consume.
47
+ */
48
+ export declare function collectModerationInputs(...roots: unknown[]): {
49
+ texts: string[];
50
+ imageUrls: string[];
51
+ };
52
+ //# sourceMappingURL=generationModeration.service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"generationModeration.service.d.ts","sourceRoot":"","sources":["../../../src/services/moderation/generationModeration.service.ts"],"names":[],"mappings":"AAOA;;;;;;;;;;;;;;;;;;;;;GAqBG;AAEH,MAAM,WAAW,yBAAyB;IACxC,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,CAAC,EAAE,MAAM,EAAE,CAAC;CACtB;AAED,MAAM,WAAW,0BAA0B;IACzC,OAAO,EAAE,OAAO,CAAC;IACjB,oFAAoF;IACpF,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,oFAAoF;IACpF,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,iFAAiF;IACjF,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,gFAAgF;AAChF,eAAO,MAAM,gCAAgC,8MACgK,CAAC;AAE9M,eAAO,MAAM,6BAA6B,6BAA6B,CAAC;AA+CxE,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAGtD;AA8CD;;GAEG;AACH,wBAAsB,uBAAuB,CAC3C,KAAK,EAAE,yBAAyB,GAC/B,OAAO,CAAC,0BAA0B,CAAC,CAyGrC;AAuBD;;;GAGG;AACH,wBAAgB,uBAAuB,CAAC,GAAG,KAAK,EAAE,OAAO,EAAE,GAAG;IAC5D,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,EAAE,MAAM,EAAE,CAAC;CACrB,CA0CA"}