@ttt-productions/moderation-core 0.8.2 → 0.8.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/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/likelihood.d.ts +1 -7
- package/dist/likelihood.d.ts.map +1 -1
- package/dist/likelihood.js +11 -16
- package/dist/likelihood.js.map +1 -1
- package/dist/server/image-moderation.d.ts +2 -12
- package/dist/server/image-moderation.d.ts.map +1 -1
- package/dist/server/image-moderation.js +7 -14
- package/dist/server/image-moderation.js.map +1 -1
- package/dist/server/index.d.ts +6 -6
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +3 -3
- package/dist/server/index.js.map +1 -1
- package/dist/server/text-moderation.d.ts +1 -11
- package/dist/server/text-moderation.d.ts.map +1 -1
- package/dist/server/text-moderation.js +15 -15
- package/dist/server/text-moderation.js.map +1 -1
- package/dist/server/video-moderation.d.ts +2 -12
- package/dist/server/video-moderation.d.ts.map +1 -1
- package/dist/server/video-moderation.js +17 -21
- package/dist/server/video-moderation.js.map +1 -1
- package/dist/types.d.ts +2 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/package.json +3 -4
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export type { Likelihood, MediaModerationScores, MediaModerationResult, TextModerationResult, ModerationLogger, } from
|
|
2
|
-
export { LIKELIHOOD_MAP, LIKELIHOOD_ORDER, likelihoodToString, isRejectionLikelihood, } from
|
|
1
|
+
export type { Likelihood, MediaModerationScores, MediaModerationResult, TextModerationResult, ModerationLogger, } from './types.js';
|
|
2
|
+
export { LIKELIHOOD_MAP, LIKELIHOOD_ORDER, likelihoodToString, isRejectionLikelihood, } from './likelihood.js';
|
|
3
3
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { LIKELIHOOD_MAP, LIKELIHOOD_ORDER, likelihoodToString, isRejectionLikelihood, } from
|
|
1
|
+
export { LIKELIHOOD_MAP, LIKELIHOOD_ORDER, likelihoodToString, isRejectionLikelihood, } from './likelihood.js';
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,iBAAiB,CAAC","sourcesContent":["export type {\n Likelihood,\n MediaModerationScores,\n MediaModerationResult,\n TextModerationResult,\n ModerationLogger,\n} from
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAQA,OAAO,EACL,cAAc,EACd,gBAAgB,EAChB,kBAAkB,EAClB,qBAAqB,GACtB,MAAM,iBAAiB,CAAC","sourcesContent":["export type {\n Likelihood,\n MediaModerationScores,\n MediaModerationResult,\n TextModerationResult,\n ModerationLogger,\n} from './types.js';\n\nexport {\n LIKELIHOOD_MAP,\n LIKELIHOOD_ORDER,\n likelihoodToString,\n isRejectionLikelihood,\n} from './likelihood.js';\n"]}
|
package/dist/likelihood.d.ts
CHANGED
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import type { Likelihood } from
|
|
2
|
-
/** Numeric enum value → string form, matching the Google APIs convention. */
|
|
1
|
+
import type { Likelihood } from './types.js';
|
|
3
2
|
export declare const LIKELIHOOD_MAP: Record<string | number, Likelihood>;
|
|
4
|
-
/** Comparison order for likelihoods (higher = more severe). */
|
|
5
3
|
export declare const LIKELIHOOD_ORDER: Record<Likelihood, number>;
|
|
6
4
|
export declare function likelihoodToString(value: string | number | null | undefined): Likelihood;
|
|
7
|
-
/**
|
|
8
|
-
* Returns true if `value` is at or above any of the `rejectionLikelihoods`.
|
|
9
|
-
* The caller provides the set — `moderation-core` does not bake in app-specific thresholds.
|
|
10
|
-
*/
|
|
11
5
|
export declare function isRejectionLikelihood(value: string | number | null | undefined, rejectionLikelihoods: ReadonlySet<string>): boolean;
|
|
12
6
|
//# sourceMappingURL=likelihood.d.ts.map
|
package/dist/likelihood.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"likelihood.d.ts","sourceRoot":"","sources":["../src/likelihood.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,
|
|
1
|
+
{"version":3,"file":"likelihood.d.ts","sourceRoot":"","sources":["../src/likelihood.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAE7C,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,UAAU,CAO9D,CAAC;AAEF,eAAO,MAAM,gBAAgB,EAAE,MAAM,CAAC,UAAU,EAAE,MAAM,CAOvD,CAAC;AAEF,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,UAAU,CAOxF;AAED,wBAAgB,qBAAqB,CACnC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,EACzC,oBAAoB,EAAE,WAAW,CAAC,MAAM,CAAC,GACxC,OAAO,CAGT"}
|
package/dist/likelihood.js
CHANGED
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
/** Numeric enum value → string form, matching the Google APIs convention. */
|
|
2
1
|
export const LIKELIHOOD_MAP = {
|
|
3
|
-
0:
|
|
4
|
-
1:
|
|
5
|
-
2:
|
|
6
|
-
3:
|
|
7
|
-
4:
|
|
8
|
-
5:
|
|
2
|
+
0: 'UNKNOWN',
|
|
3
|
+
1: 'VERY_UNLIKELY',
|
|
4
|
+
2: 'UNLIKELY',
|
|
5
|
+
3: 'POSSIBLE',
|
|
6
|
+
4: 'LIKELY',
|
|
7
|
+
5: 'VERY_LIKELY',
|
|
9
8
|
};
|
|
10
|
-
/** Comparison order for likelihoods (higher = more severe). */
|
|
11
9
|
export const LIKELIHOOD_ORDER = {
|
|
12
10
|
UNKNOWN: 0,
|
|
13
11
|
VERY_UNLIKELY: 1,
|
|
@@ -18,16 +16,13 @@ export const LIKELIHOOD_ORDER = {
|
|
|
18
16
|
};
|
|
19
17
|
export function likelihoodToString(value) {
|
|
20
18
|
if (value === null || value === undefined)
|
|
21
|
-
return
|
|
22
|
-
if (typeof value ===
|
|
23
|
-
return (LIKELIHOOD_MAP[value] ??
|
|
19
|
+
return 'UNKNOWN';
|
|
20
|
+
if (typeof value === 'string') {
|
|
21
|
+
return (LIKELIHOOD_MAP[value] ??
|
|
22
|
+
value);
|
|
24
23
|
}
|
|
25
|
-
return LIKELIHOOD_MAP[value] ??
|
|
24
|
+
return LIKELIHOOD_MAP[value] ?? 'UNKNOWN';
|
|
26
25
|
}
|
|
27
|
-
/**
|
|
28
|
-
* Returns true if `value` is at or above any of the `rejectionLikelihoods`.
|
|
29
|
-
* The caller provides the set — `moderation-core` does not bake in app-specific thresholds.
|
|
30
|
-
*/
|
|
31
26
|
export function isRejectionLikelihood(value, rejectionLikelihoods) {
|
|
32
27
|
const str = likelihoodToString(value);
|
|
33
28
|
return rejectionLikelihoods.has(str);
|
package/dist/likelihood.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"likelihood.js","sourceRoot":"","sources":["../src/likelihood.ts"],"names":[],"mappings":"AAEA,
|
|
1
|
+
{"version":3,"file":"likelihood.js","sourceRoot":"","sources":["../src/likelihood.ts"],"names":[],"mappings":"AAEA,MAAM,CAAC,MAAM,cAAc,GAAwC;IACjE,CAAC,EAAE,SAAS;IACZ,CAAC,EAAE,eAAe;IAClB,CAAC,EAAE,UAAU;IACb,CAAC,EAAE,UAAU;IACb,CAAC,EAAE,QAAQ;IACX,CAAC,EAAE,aAAa;CACjB,CAAC;AAEF,MAAM,CAAC,MAAM,gBAAgB,GAA+B;IAC1D,OAAO,EAAE,CAAC;IACV,aAAa,EAAE,CAAC;IAChB,QAAQ,EAAE,CAAC;IACX,QAAQ,EAAE,CAAC;IACX,MAAM,EAAE,CAAC;IACT,WAAW,EAAE,CAAC;CACf,CAAC;AAEF,MAAM,UAAU,kBAAkB,CAAC,KAAyC;IAC1E,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,SAAS,CAAC;IAC5D,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,CAAC,cAAc,CAAC,KAAoC,CAAC;YACzD,KAAoB,CAAe,CAAC;IACzC,CAAC;IACD,OAAO,cAAc,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC;AAC5C,CAAC;AAED,MAAM,UAAU,qBAAqB,CACnC,KAAyC,EACzC,oBAAyC;IAEzC,MAAM,GAAG,GAAG,kBAAkB,CAAC,KAAK,CAAC,CAAC;IACtC,OAAO,oBAAoB,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;AACvC,CAAC","sourcesContent":["import type { Likelihood } from './types.js';\n\nexport const LIKELIHOOD_MAP: Record<string | number, Likelihood> = {\n 0: 'UNKNOWN',\n 1: 'VERY_UNLIKELY',\n 2: 'UNLIKELY',\n 3: 'POSSIBLE',\n 4: 'LIKELY',\n 5: 'VERY_LIKELY',\n};\n\nexport const LIKELIHOOD_ORDER: Record<Likelihood, number> = {\n UNKNOWN: 0,\n VERY_UNLIKELY: 1,\n UNLIKELY: 2,\n POSSIBLE: 3,\n LIKELY: 4,\n VERY_LIKELY: 5,\n};\n\nexport function likelihoodToString(value: string | number | null | undefined): Likelihood {\n if (value === null || value === undefined) return 'UNKNOWN';\n if (typeof value === 'string') {\n return (LIKELIHOOD_MAP[value as keyof typeof LIKELIHOOD_MAP] ??\n (value as Likelihood)) as Likelihood;\n }\n return LIKELIHOOD_MAP[value] ?? 'UNKNOWN';\n}\n\nexport function isRejectionLikelihood(\n value: string | number | null | undefined,\n rejectionLikelihoods: ReadonlySet<string>,\n): boolean {\n const str = likelihoodToString(value);\n return rejectionLikelihoods.has(str);\n}\n"]}
|
|
@@ -1,20 +1,10 @@
|
|
|
1
|
-
import type { MediaModerationResult, ModerationLogger } from
|
|
2
|
-
import type * as vision from
|
|
1
|
+
import type { MediaModerationResult, ModerationLogger } from '../types.js';
|
|
2
|
+
import type * as vision from '@google-cloud/vision';
|
|
3
3
|
export interface ImageModerationOptions {
|
|
4
|
-
/** Set of likelihood strings that should cause rejection (e.g. new Set(["LIKELY","VERY_LIKELY"])). */
|
|
5
4
|
rejectionLikelihoods: ReadonlySet<string>;
|
|
6
|
-
/** Optional logger. */
|
|
7
5
|
logger?: ModerationLogger;
|
|
8
|
-
/** Lazy Vision client provider. Defaults to constructing a new ImageAnnotatorClient on first call. */
|
|
9
6
|
getClient?: () => Promise<vision.ImageAnnotatorClient>;
|
|
10
7
|
}
|
|
11
|
-
/**
|
|
12
|
-
* Moderate an image via Google Cloud Vision SafeSearch.
|
|
13
|
-
* Throws on system errors (quota, network, etc.); returns a result on policy decisions.
|
|
14
|
-
*
|
|
15
|
-
* @param imageSource - GCS URI ("gs://bucket/path") or HTTPS URL.
|
|
16
|
-
*/
|
|
17
8
|
export declare function moderateImage(imageSource: string, options: ImageModerationOptions): Promise<MediaModerationResult>;
|
|
18
|
-
/** Test-only: reset memoized default client. */
|
|
19
9
|
export declare function __resetDefaultVisionClient(): void;
|
|
20
10
|
//# sourceMappingURL=image-moderation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image-moderation.d.ts","sourceRoot":"","sources":["../../src/server/image-moderation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"image-moderation.d.ts","sourceRoot":"","sources":["../../src/server/image-moderation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAE3E,OAAO,KAAK,KAAK,MAAM,MAAM,sBAAsB,CAAC;AAEpD,MAAM,WAAW,sBAAsB;IACrC,oBAAoB,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC1C,MAAM,CAAC,EAAE,gBAAgB,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC;CACxD;AAUD,wBAAsB,aAAa,CACjC,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,qBAAqB,CAAC,CAkChC;AAED,wBAAgB,0BAA0B,SAEzC"}
|
|
@@ -1,18 +1,12 @@
|
|
|
1
|
-
import { isRejectionLikelihood, likelihoodToString
|
|
1
|
+
import { isRejectionLikelihood, likelihoodToString } from '../likelihood.js';
|
|
2
2
|
let _defaultClient = null;
|
|
3
3
|
async function defaultGetVisionClient() {
|
|
4
4
|
if (_defaultClient)
|
|
5
5
|
return _defaultClient;
|
|
6
|
-
const mod = await import(
|
|
6
|
+
const mod = await import('@google-cloud/vision');
|
|
7
7
|
_defaultClient = new mod.ImageAnnotatorClient();
|
|
8
8
|
return _defaultClient;
|
|
9
9
|
}
|
|
10
|
-
/**
|
|
11
|
-
* Moderate an image via Google Cloud Vision SafeSearch.
|
|
12
|
-
* Throws on system errors (quota, network, etc.); returns a result on policy decisions.
|
|
13
|
-
*
|
|
14
|
-
* @param imageSource - GCS URI ("gs://bucket/path") or HTTPS URL.
|
|
15
|
-
*/
|
|
16
10
|
export async function moderateImage(imageSource, options) {
|
|
17
11
|
const { rejectionLikelihoods, logger } = options;
|
|
18
12
|
const getClient = options.getClient ?? defaultGetVisionClient;
|
|
@@ -21,11 +15,11 @@ export async function moderateImage(imageSource, options) {
|
|
|
21
15
|
const [result] = await client.safeSearchDetection(imageSource);
|
|
22
16
|
const safeSearch = result.safeSearchAnnotation;
|
|
23
17
|
if (!safeSearch) {
|
|
24
|
-
logger?.warn?.(
|
|
18
|
+
logger?.warn?.('[moderation-core] SafeSearch returned no annotation; rejecting as precaution');
|
|
25
19
|
return {
|
|
26
20
|
safe: false,
|
|
27
|
-
reason:
|
|
28
|
-
scores: { adult:
|
|
21
|
+
reason: 'Content moderation check failed',
|
|
22
|
+
scores: { adult: 'UNKNOWN', violence: 'UNKNOWN', racy: 'UNKNOWN' },
|
|
29
23
|
};
|
|
30
24
|
}
|
|
31
25
|
const scores = {
|
|
@@ -38,12 +32,11 @@ export async function moderateImage(imageSource, options) {
|
|
|
38
32
|
const safe = !isAdult && !isViolent;
|
|
39
33
|
let reason;
|
|
40
34
|
if (isAdult)
|
|
41
|
-
reason =
|
|
35
|
+
reason = 'Adult or sexually explicit content is not allowed';
|
|
42
36
|
else if (isViolent)
|
|
43
|
-
reason =
|
|
37
|
+
reason = 'Violent or graphic content is not allowed';
|
|
44
38
|
return { safe, reason, scores };
|
|
45
39
|
}
|
|
46
|
-
/** Test-only: reset memoized default client. */
|
|
47
40
|
export function __resetDefaultVisionClient() {
|
|
48
41
|
_defaultClient = null;
|
|
49
42
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image-moderation.js","sourceRoot":"","sources":["../../src/server/image-moderation.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"image-moderation.js","sourceRoot":"","sources":["../../src/server/image-moderation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AAW7E,IAAI,cAAc,GAAuC,IAAI,CAAC;AAC9D,KAAK,UAAU,sBAAsB;IACnC,IAAI,cAAc;QAAE,OAAO,cAAc,CAAC;IAC1C,MAAM,GAAG,GAAG,MAAM,MAAM,CAAC,sBAAsB,CAAC,CAAC;IACjD,cAAc,GAAG,IAAI,GAAG,CAAC,oBAAoB,EAAE,CAAC;IAChD,OAAO,cAAc,CAAC;AACxB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU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MAAM,GAAG,2CAA2C,CAAC;IAEzE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,CAAC;AAClC,CAAC;AAED,MAAM,UAAU,0BAA0B;IACxC,cAAc,GAAG,IAAI,CAAC;AACxB,CAAC","sourcesContent":["import { isRejectionLikelihood, likelihoodToString } from '../likelihood.js';\nimport type { MediaModerationResult, ModerationLogger } from '../types.js';\n\nimport type * as vision from '@google-cloud/vision';\n\nexport interface ImageModerationOptions {\n rejectionLikelihoods: ReadonlySet<string>;\n logger?: ModerationLogger;\n getClient?: () => Promise<vision.ImageAnnotatorClient>;\n}\n\nlet _defaultClient: vision.ImageAnnotatorClient | null = null;\nasync function defaultGetVisionClient(): Promise<vision.ImageAnnotatorClient> {\n if (_defaultClient) return _defaultClient;\n const mod = await import('@google-cloud/vision');\n _defaultClient = new mod.ImageAnnotatorClient();\n return _defaultClient;\n}\n\nexport async function moderateImage(\n imageSource: string,\n options: ImageModerationOptions,\n): Promise<MediaModerationResult> {\n const { rejectionLikelihoods, logger } = options;\n const getClient = options.getClient ?? defaultGetVisionClient;\n\n logger?.info?.(`[moderation-core] Image moderation: ${imageSource}`);\n\n const client = await getClient();\n const [result] = await client.safeSearchDetection(imageSource);\n const safeSearch = result.safeSearchAnnotation;\n\n if (!safeSearch) {\n logger?.warn?.('[moderation-core] SafeSearch returned no annotation; rejecting as precaution');\n return {\n safe: false,\n reason: 'Content moderation check failed',\n scores: { adult: 'UNKNOWN', violence: 'UNKNOWN', racy: 'UNKNOWN' },\n };\n }\n\n const scores = {\n adult: likelihoodToString(safeSearch.adult),\n violence: likelihoodToString(safeSearch.violence),\n racy: likelihoodToString(safeSearch.racy),\n };\n\n const isAdult = isRejectionLikelihood(safeSearch.adult, rejectionLikelihoods);\n const isViolent = isRejectionLikelihood(safeSearch.violence, rejectionLikelihoods);\n\n const safe = !isAdult && !isViolent;\n let reason: string | undefined;\n if (isAdult) reason = 'Adult or sexually explicit content is not allowed';\n else if (isViolent) reason = 'Violent or graphic content is not allowed';\n\n return { safe, reason, scores };\n}\n\nexport function __resetDefaultVisionClient() {\n _defaultClient = null;\n}\n"]}
|
package/dist/server/index.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export { moderateImage, __resetDefaultVisionClient
|
|
2
|
-
export type { ImageModerationOptions } from
|
|
3
|
-
export { moderateVideo, DEFAULT_VIDEO_POLL_BACKOFF, __resetDefaultVideoClient, } from
|
|
4
|
-
export type { VideoModerationOptions, VideoPollBackoff
|
|
5
|
-
export { moderateText, quickWordFilter, createWordListCache
|
|
6
|
-
export type { WordListProvider, ModerateTextOptions
|
|
1
|
+
export { moderateImage, __resetDefaultVisionClient } from './image-moderation.js';
|
|
2
|
+
export type { ImageModerationOptions } from './image-moderation.js';
|
|
3
|
+
export { moderateVideo, DEFAULT_VIDEO_POLL_BACKOFF, __resetDefaultVideoClient, } from './video-moderation.js';
|
|
4
|
+
export type { VideoModerationOptions, VideoPollBackoff } from './video-moderation.js';
|
|
5
|
+
export { moderateText, quickWordFilter, createWordListCache } from './text-moderation.js';
|
|
6
|
+
export type { WordListProvider, ModerateTextOptions } from './text-moderation.js';
|
|
7
7
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAC;AAClF,YAAY,EAAE,sBAAsB,EAAE,MAAM,uBAAuB,CAAC;AAEpE,OAAO,EACL,aAAa,EACb,0BAA0B,EAC1B,yBAAyB,GAC1B,MAAM,uBAAuB,CAAC;AAC/B,YAAY,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAC;AAEtF,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC1F,YAAY,EAAE,gBAAgB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC"}
|
package/dist/server/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { moderateImage, __resetDefaultVisionClient
|
|
2
|
-
export { moderateVideo, DEFAULT_VIDEO_POLL_BACKOFF, __resetDefaultVideoClient, } from
|
|
3
|
-
export { moderateText, quickWordFilter, createWordListCache
|
|
1
|
+
export { moderateImage, __resetDefaultVisionClient } from './image-moderation.js';
|
|
2
|
+
export { moderateVideo, DEFAULT_VIDEO_POLL_BACKOFF, __resetDefaultVideoClient, } from './video-moderation.js';
|
|
3
|
+
export { moderateText, quickWordFilter, createWordListCache } from './text-moderation.js';
|
|
4
4
|
//# sourceMappingURL=index.js.map
|
package/dist/server/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/server/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,0BAA0B,EAAE,MAAM,uBAAuB,CAAC;AAGlF,OAAO,EACL,aAAa,EACb,0BAA0B,EAC1B,yBAAyB,GAC1B,MAAM,uBAAuB,CAAC;AAG/B,OAAO,EAAE,YAAY,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC","sourcesContent":["export { moderateImage, __resetDefaultVisionClient } from './image-moderation.js';\nexport type { ImageModerationOptions } from './image-moderation.js';\n\nexport {\n moderateVideo,\n DEFAULT_VIDEO_POLL_BACKOFF,\n __resetDefaultVideoClient,\n} from './video-moderation.js';\nexport type { VideoModerationOptions, VideoPollBackoff } from './video-moderation.js';\n\nexport { moderateText, quickWordFilter, createWordListCache } from './text-moderation.js';\nexport type { WordListProvider, ModerateTextOptions } from './text-moderation.js';\n"]}
|
|
@@ -1,12 +1,7 @@
|
|
|
1
|
-
import type { ModerationLogger, TextModerationResult } from
|
|
1
|
+
import type { ModerationLogger, TextModerationResult } from '../types.js';
|
|
2
2
|
export interface WordListProvider {
|
|
3
|
-
/** Returns the active profanity word list. May read from cache, Firestore, etc. */
|
|
4
3
|
getWords(): Promise<ReadonlySet<string>>;
|
|
5
4
|
}
|
|
6
|
-
/**
|
|
7
|
-
* Build a memoized word-list provider. Calls `loader` on first use and
|
|
8
|
-
* any time the cache is older than `ttlMs`.
|
|
9
|
-
*/
|
|
10
5
|
export declare function createWordListCache(args: {
|
|
11
6
|
loader: () => Promise<Iterable<string>>;
|
|
12
7
|
ttlMs: number;
|
|
@@ -20,13 +15,8 @@ export declare function quickWordFilter(text: string, wordList: WordListProvider
|
|
|
20
15
|
}>;
|
|
21
16
|
export interface ModerateTextOptions {
|
|
22
17
|
wordList: WordListProvider;
|
|
23
|
-
/** Minimum text length before any moderation runs. Default 0 (always run). */
|
|
24
18
|
minLength?: number;
|
|
25
19
|
logger?: ModerationLogger;
|
|
26
20
|
}
|
|
27
|
-
/**
|
|
28
|
-
* Word-list text moderation (cheap, local). Returns a generic result — the
|
|
29
|
-
* caller decides how to respond (throw HttpsError, log violation, etc.).
|
|
30
|
-
*/
|
|
31
21
|
export declare function moderateText(text: string, options: ModerateTextOptions): Promise<TextModerationResult>;
|
|
32
22
|
//# sourceMappingURL=text-moderation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text-moderation.d.ts","sourceRoot":"","sources":["../../src/server/text-moderation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAE1E,MAAM,WAAW,gBAAgB;IAC/B,
|
|
1
|
+
{"version":3,"file":"text-moderation.d.ts","sourceRoot":"","sources":["../../src/server/text-moderation.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAE1E,MAAM,WAAW,gBAAgB;IAC/B,QAAQ,IAAI,OAAO,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC;CAC1C;AAED,wBAAgB,mBAAmB,CAAC,IAAI,EAAE;IACxC,MAAM,EAAE,MAAM,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC;IACxC,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,gBAAgB,CAAC;CAC3B,GAAG,gBAAgB,GAAG;IAAE,OAAO,IAAI,IAAI,CAAA;CAAE,CAmBzC;AAyBD,wBAAsB,eAAe,CACnC,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,gBAAgB,GACzB,OAAO,CAAC;IAAE,IAAI,EAAE,OAAO,CAAC;IAAC,YAAY,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,CAYpD;AAED,MAAM,WAAW,mBAAmB;IAClC,QAAQ,EAAE,gBAAgB,CAAC;IAC3B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,MAAM,CAAC,EAAE,gBAAgB,CAAC;CAC3B;AAED,wBAAsB,YAAY,CAChC,IAAI,EAAE,MAAM,EACZ,OAAO,EAAE,mBAAmB,GAC3B,OAAO,CAAC,oBAAoB,CAAC,CAkB/B"}
|
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Build a memoized word-list provider. Calls `loader` on first use and
|
|
3
|
-
* any time the cache is older than `ttlMs`.
|
|
4
|
-
*/
|
|
5
1
|
export function createWordListCache(args) {
|
|
6
2
|
let cached = null;
|
|
7
3
|
let cachedAt = 0;
|
|
@@ -23,16 +19,24 @@ export function createWordListCache(args) {
|
|
|
23
19
|
};
|
|
24
20
|
}
|
|
25
21
|
const LEET_SPEAK_MAP = {
|
|
26
|
-
|
|
27
|
-
|
|
22
|
+
'0': 'o',
|
|
23
|
+
'1': 'i',
|
|
24
|
+
'3': 'e',
|
|
25
|
+
'4': 'a',
|
|
26
|
+
'5': 's',
|
|
27
|
+
'7': 't',
|
|
28
|
+
'8': 'b',
|
|
29
|
+
'@': 'a',
|
|
30
|
+
$: 's',
|
|
31
|
+
'!': 'i',
|
|
32
|
+
'+': 't',
|
|
28
33
|
};
|
|
29
34
|
function normalizeText(text) {
|
|
30
35
|
let normalized = text.toLowerCase();
|
|
31
36
|
for (const [leet, letter] of Object.entries(LEET_SPEAK_MAP)) {
|
|
32
37
|
normalized = normalized.split(leet).join(letter);
|
|
33
38
|
}
|
|
34
|
-
|
|
35
|
-
normalized = normalized.replace(/(.)\1{2,}/g, "$1$1");
|
|
39
|
+
normalized = normalized.replace(/(.)\1{2,}/g, '$1$1');
|
|
36
40
|
return normalized;
|
|
37
41
|
}
|
|
38
42
|
export async function quickWordFilter(text, wordList) {
|
|
@@ -43,16 +47,12 @@ export async function quickWordFilter(text, wordList) {
|
|
|
43
47
|
const tokens = normalized.split(/[\s.,!?;:'"()[\]{}<>/\\|`~@#$%^&*+=_-]+/);
|
|
44
48
|
const flagged = [];
|
|
45
49
|
for (const token of tokens) {
|
|
46
|
-
const clean = token.replace(/[^a-z0-9]/gi,
|
|
50
|
+
const clean = token.replace(/[^a-z0-9]/gi, '');
|
|
47
51
|
if (clean.length > 1 && words.has(clean))
|
|
48
52
|
flagged.push(clean);
|
|
49
53
|
}
|
|
50
54
|
return { pass: flagged.length === 0, flaggedWords: flagged };
|
|
51
55
|
}
|
|
52
|
-
/**
|
|
53
|
-
* Word-list text moderation (cheap, local). Returns a generic result — the
|
|
54
|
-
* caller decides how to respond (throw HttpsError, log violation, etc.).
|
|
55
|
-
*/
|
|
56
56
|
export async function moderateText(text, options) {
|
|
57
57
|
const { wordList, minLength = 0, logger } = options;
|
|
58
58
|
if (!text || text.trim().length === 0)
|
|
@@ -64,9 +64,9 @@ export async function moderateText(text, options) {
|
|
|
64
64
|
if (!wordCheck.pass) {
|
|
65
65
|
return {
|
|
66
66
|
safe: false,
|
|
67
|
-
reason:
|
|
67
|
+
reason: 'Content contains prohibited language',
|
|
68
68
|
flaggedWords: wordCheck.flaggedWords,
|
|
69
|
-
layer:
|
|
69
|
+
layer: 'word_filter',
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
72
|
return { safe: true };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"text-moderation.js","sourceRoot":"","sources":["../../src/server/text-moderation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"text-moderation.js","sourceRoot":"","sources":["../../src/server/text-moderation.ts"],"names":[],"mappings":"AAMA,MAAM,UAAU,mBAAmB,CAAC,IAInC;IACC,IAAI,MAAM,GAAuB,IAAI,CAAC;IACtC,IAAI,QAAQ,GAAG,CAAC,CAAC;IAEjB,OAAO;QACL,KAAK,CAAC,QAAQ;YACZ,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACvB,IAAI,MAAM,IAAI,GAAG,GAAG,QAAQ,GAAG,IAAI,CAAC,KAAK;gBAAE,OAAO,MAAM,CAAC;YACzD,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;YAClC,MAAM,GAAG,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;YACxB,QAAQ,GAAG,GAAG,CAAC;YACf,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,4BAA4B,MAAM,CAAC,IAAI,mBAAmB,CAAC,CAAC;YAChF,OAAO,MAAM,CAAC;QAChB,CAAC;QACD,OAAO;YACL,MAAM,GAAG,IAAI,CAAC;YACd,QAAQ,GAAG,CAAC,CAAC;QACf,CAAC;KACF,CAAC;AACJ,CAAC;AAED,MAAM,cAAc,GAA2B;IAC7C,GAAG,EAAE,GAAG;IACR,GAAG,EAAE,GAAG;IACR,GAAG,EAAE,GAAG;IACR,GAAG,EAAE,GAAG;IACR,GAAG,EAAE,GAAG;IACR,GAAG,EAAE,GAAG;IACR,GAAG,EAAE,GAA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C,EAAE,YAAY,EAAE,OAAO,EAAE,CAAC;AAC/D,CAAC;AAQD,MAAM,CAAC,KAAK,UAAU,YAAY,CAChC,IAAY,EACZ,OAA4B;IAE5B,MAAM,EAAE,QAAQ,EAAE,SAAS,GAAG,CAAC,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC;IACpD,IAAI,CAAC,IAAI,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IAC7D,IAAI,IAAI,CAAC,IAAI,EAAE,CAAC,MAAM,GAAG,SAAS;QAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;IAE1D,MAAM,EAAE,IAAI,EAAE,CAAC,sCAAsC,IAAI,CAAC,MAAM,QAAQ,CAAC,CAAC;IAE1E,MAAM,SAAS,GAAG,MAAM,eAAe,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IACxD,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,CAAC;QACpB,OAAO;YACL,IAAI,EAAE,KAAK;YACX,MAAM,EAAE,sCAAsC;YAC9C,YAAY,EAAE,SAAS,CAAC,YAAY;YACpC,KAAK,EAAE,aAAa;SACrB,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC;AACxB,CAAC","sourcesContent":["import type { ModerationLogger, TextModerationResult } from '../types.js';\n\nexport interface WordListProvider {\n getWords(): Promise<ReadonlySet<string>>;\n}\n\nexport function createWordListCache(args: {\n loader: () => Promise<Iterable<string>>;\n ttlMs: number;\n logger?: ModerationLogger;\n}): WordListProvider & { __reset(): void } {\n let cached: Set<string> | null = null;\n let cachedAt = 0;\n\n return {\n async getWords() {\n const now = Date.now();\n if (cached && now - cachedAt < args.ttlMs) return cached;\n const fresh = await args.loader();\n cached = new Set(fresh);\n cachedAt = now;\n args.logger?.info?.(`[moderation-core] Loaded ${cached.size} words into cache`);\n return cached;\n },\n __reset() {\n cached = null;\n cachedAt = 0;\n },\n };\n}\n\nconst LEET_SPEAK_MAP: Record<string, string> = {\n '0': 'o',\n '1': 'i',\n '3': 'e',\n '4': 'a',\n '5': 's',\n '7': 't',\n '8': 'b',\n '@': 'a',\n $: 's',\n '!': 'i',\n '+': 't',\n};\n\nfunction normalizeText(text: string): string {\n let normalized = text.toLowerCase();\n for (const [leet, letter] of Object.entries(LEET_SPEAK_MAP)) {\n normalized = normalized.split(leet).join(letter);\n }\n normalized = normalized.replace(/(.)\\1{2,}/g, '$1$1');\n return normalized;\n}\n\nexport async function quickWordFilter(\n text: string,\n wordList: WordListProvider,\n): Promise<{ pass: boolean; flaggedWords: string[] }> {\n const words = await wordList.getWords();\n if (words.size === 0) return { pass: true, flaggedWords: [] };\n\n const normalized = normalizeText(text);\n const tokens = normalized.split(/[\\s.,!?;:'\"()[\\]{}<>/\\\\|`~@#$%^&*+=_-]+/);\n const flagged: string[] = [];\n for (const token of tokens) {\n const clean = token.replace(/[^a-z0-9]/gi, '');\n if (clean.length > 1 && words.has(clean)) flagged.push(clean);\n }\n return { pass: flagged.length === 0, flaggedWords: flagged };\n}\n\nexport interface ModerateTextOptions {\n wordList: WordListProvider;\n minLength?: number;\n logger?: ModerationLogger;\n}\n\nexport async function moderateText(\n text: string,\n options: ModerateTextOptions,\n): Promise<TextModerationResult> {\n const { wordList, minLength = 0, logger } = options;\n if (!text || text.trim().length === 0) return { safe: true };\n if (text.trim().length < minLength) return { safe: true };\n\n logger?.info?.(`[moderation-core] Text moderation: ${text.length} chars`);\n\n const wordCheck = await quickWordFilter(text, wordList);\n if (!wordCheck.pass) {\n return {\n safe: false,\n reason: 'Content contains prohibited language',\n flaggedWords: wordCheck.flaggedWords,\n layer: 'word_filter',\n };\n }\n\n return { safe: true };\n}\n"]}
|
|
@@ -1,16 +1,10 @@
|
|
|
1
|
-
import type { MediaModerationResult, ModerationLogger } from
|
|
2
|
-
import type { VideoIntelligenceServiceClient } from
|
|
3
|
-
/** Poll cadence for the annotateVideo long-running operation. */
|
|
1
|
+
import type { MediaModerationResult, ModerationLogger } from '../types.js';
|
|
2
|
+
import type { VideoIntelligenceServiceClient } from '@google-cloud/video-intelligence';
|
|
4
3
|
export interface VideoPollBackoff {
|
|
5
4
|
initialDelayMillis: number;
|
|
6
5
|
delayMultiplier: number;
|
|
7
6
|
maxDelayMillis: number;
|
|
8
7
|
}
|
|
9
|
-
/**
|
|
10
|
-
* Without an explicit `longrunning` backoff, gax polls the operation starting at
|
|
11
|
-
* 100ms (×1.3), which burns ~20 'Requests per minute' of Video Intelligence quota
|
|
12
|
-
* per video. Scans take tens of seconds, so the first poll waits 10s.
|
|
13
|
-
*/
|
|
14
8
|
export declare const DEFAULT_VIDEO_POLL_BACKOFF: VideoPollBackoff;
|
|
15
9
|
export interface VideoModerationOptions {
|
|
16
10
|
rejectionLikelihoods: ReadonlySet<string>;
|
|
@@ -18,10 +12,6 @@ export interface VideoModerationOptions {
|
|
|
18
12
|
getClient?: () => Promise<VideoIntelligenceServiceClient>;
|
|
19
13
|
pollBackoff?: VideoPollBackoff;
|
|
20
14
|
}
|
|
21
|
-
/**
|
|
22
|
-
* Moderate a video via Google Cloud Video Intelligence.
|
|
23
|
-
* @param gcsUri - MUST be a GCS URI ("gs://bucket/path"); HTTPS URLs are not supported by the API.
|
|
24
|
-
*/
|
|
25
15
|
export declare function moderateVideo(gcsUri: string, options: VideoModerationOptions): Promise<MediaModerationResult>;
|
|
26
16
|
export declare function __resetDefaultVideoClient(): void;
|
|
27
17
|
//# sourceMappingURL=video-moderation.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"video-moderation.d.ts","sourceRoot":"","sources":["../../src/server/video-moderation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"video-moderation.d.ts","sourceRoot":"","sources":["../../src/server/video-moderation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAc,qBAAqB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEvF,OAAO,KAAK,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAEvF,MAAM,WAAW,gBAAgB;IAC/B,kBAAkB,EAAE,MAAM,CAAC;IAC3B,eAAe,EAAE,MAAM,CAAC;IACxB,cAAc,EAAE,MAAM,CAAC;CACxB;AAGD,eAAO,MAAM,0BAA0B,EAAE,gBAIxC,CAAC;AAEF,MAAM,WAAW,sBAAsB;IACrC,oBAAoB,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC1C,MAAM,CAAC,EAAE,gBAAgB,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,OAAO,CAAC,8BAA8B,CAAC,CAAC;IAC1D,WAAW,CAAC,EAAE,gBAAgB,CAAC;CAChC;AAWD,wBAAsB,aAAa,CACjC,MAAM,EAAE,MAAM,EACd,OAAO,EAAE,sBAAsB,GAC9B,OAAO,CAAC,qBAAqB,CAAC,CA+DhC;AAED,wBAAgB,yBAAyB,SAExC"}
|
|
@@ -1,9 +1,5 @@
|
|
|
1
|
-
import { isRejectionLikelihood, likelihoodToString, LIKELIHOOD_ORDER
|
|
2
|
-
|
|
3
|
-
* Without an explicit `longrunning` backoff, gax polls the operation starting at
|
|
4
|
-
* 100ms (×1.3), which burns ~20 'Requests per minute' of Video Intelligence quota
|
|
5
|
-
* per video. Scans take tens of seconds, so the first poll waits 10s.
|
|
6
|
-
*/
|
|
1
|
+
import { isRejectionLikelihood, likelihoodToString, LIKELIHOOD_ORDER } from '../likelihood.js';
|
|
2
|
+
// gax's default long-running poll (100ms, x1.3) spends Video Intelligence per-minute quota.
|
|
7
3
|
export const DEFAULT_VIDEO_POLL_BACKOFF = {
|
|
8
4
|
initialDelayMillis: 10_000,
|
|
9
5
|
delayMultiplier: 1.5,
|
|
@@ -13,26 +9,21 @@ let _defaultClient = null;
|
|
|
13
9
|
async function defaultGetVideoClient() {
|
|
14
10
|
if (_defaultClient)
|
|
15
11
|
return _defaultClient;
|
|
16
|
-
const mod = await import(
|
|
12
|
+
const mod = await import('@google-cloud/video-intelligence');
|
|
17
13
|
_defaultClient = new mod.VideoIntelligenceServiceClient();
|
|
18
14
|
return _defaultClient;
|
|
19
15
|
}
|
|
20
|
-
|
|
21
|
-
* Moderate a video via Google Cloud Video Intelligence.
|
|
22
|
-
* @param gcsUri - MUST be a GCS URI ("gs://bucket/path"); HTTPS URLs are not supported by the API.
|
|
23
|
-
*/
|
|
16
|
+
// Video Intelligence accepts only gs:// URIs, never HTTPS URLs.
|
|
24
17
|
export async function moderateVideo(gcsUri, options) {
|
|
25
18
|
const { rejectionLikelihoods, logger } = options;
|
|
26
19
|
const getClient = options.getClient ?? defaultGetVideoClient;
|
|
27
20
|
logger?.info?.(`[moderation-core] Video moderation: ${gcsUri}`);
|
|
28
|
-
const { protos } = await import(
|
|
21
|
+
const { protos } = await import('@google-cloud/video-intelligence');
|
|
29
22
|
const client = await getClient();
|
|
30
23
|
const pollBackoff = options.pollBackoff ?? DEFAULT_VIDEO_POLL_BACKOFF;
|
|
31
24
|
const [operation] = await client.annotateVideo({
|
|
32
25
|
inputUri: gcsUri,
|
|
33
|
-
features: [
|
|
34
|
-
protos.google.cloud.videointelligence.v1.Feature.EXPLICIT_CONTENT_DETECTION,
|
|
35
|
-
],
|
|
26
|
+
features: [protos.google.cloud.videointelligence.v1.Feature.EXPLICIT_CONTENT_DETECTION],
|
|
36
27
|
}, {
|
|
37
28
|
longrunning: {
|
|
38
29
|
initialRetryDelayMillis: pollBackoff.initialDelayMillis,
|
|
@@ -43,11 +34,16 @@ export async function moderateVideo(gcsUri, options) {
|
|
|
43
34
|
const [result] = await operation.promise();
|
|
44
35
|
const annotation = result.annotationResults?.[0];
|
|
45
36
|
const explicitAnnotation = annotation?.explicitAnnotation;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
37
|
+
// An error or an empty frame list means nothing was checked.
|
|
38
|
+
if (annotation?.error || !explicitAnnotation?.frames?.length) {
|
|
39
|
+
logger?.warn?.('[moderation-core] Video analysis returned no checked frames; rejecting as precaution');
|
|
40
|
+
return {
|
|
41
|
+
safe: false,
|
|
42
|
+
reason: 'Content moderation check failed',
|
|
43
|
+
scores: { adult: 'UNKNOWN', violence: 'UNKNOWN', racy: 'UNKNOWN' },
|
|
44
|
+
};
|
|
49
45
|
}
|
|
50
|
-
let maxAdult =
|
|
46
|
+
let maxAdult = 'VERY_UNLIKELY';
|
|
51
47
|
let foundExplicit = false;
|
|
52
48
|
for (const frame of explicitAnnotation.frames) {
|
|
53
49
|
if (isRejectionLikelihood(frame.pornographyLikelihood, rejectionLikelihoods)) {
|
|
@@ -61,11 +57,11 @@ export async function moderateVideo(gcsUri, options) {
|
|
|
61
57
|
}
|
|
62
58
|
}
|
|
63
59
|
const safe = !foundExplicit;
|
|
64
|
-
const reason = safe ? undefined :
|
|
60
|
+
const reason = safe ? undefined : 'Adult or sexually explicit content is not allowed';
|
|
65
61
|
return {
|
|
66
62
|
safe,
|
|
67
63
|
reason,
|
|
68
|
-
scores: { adult: maxAdult, violence:
|
|
64
|
+
scores: { adult: maxAdult, violence: 'NOT_CHECKED', racy: 'NOT_CHECKED' },
|
|
69
65
|
};
|
|
70
66
|
}
|
|
71
67
|
export function __resetDefaultVideoClient() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"video-moderation.js","sourceRoot":"","sources":["../../src/server/video-moderation.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"video-moderation.js","sourceRoot":"","sources":["../../src/server/video-moderation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,MAAM,kBAAkB,CAAC;AAW/F,4FAA4F;AAC5F,MAAM,CAAC,MAAM,0BAA0B,GAAqB;IAC1D,kBAAkB,EAAE,MAAM;IAC1B,eAAe,EAAE,GAAG;IACpB,cAAc,EAAE,MAAM;CACvB,CAAC;AASF,IAAI,cAAc,GAA0C,IAAI,CAAC;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C,cAAc,GAAG,IAAI,CAAC;AACxB,CAAC","sourcesContent":["import { isRejectionLikelihood, likelihoodToString, LIKELIHOOD_ORDER } from '../likelihood.js';\nimport type { Likelihood, MediaModerationResult, ModerationLogger } from '../types.js';\n\nimport type { VideoIntelligenceServiceClient } from '@google-cloud/video-intelligence';\n\nexport interface VideoPollBackoff {\n initialDelayMillis: number;\n delayMultiplier: number;\n maxDelayMillis: number;\n}\n\n// gax's default long-running poll (100ms, x1.3) spends Video Intelligence per-minute quota.\nexport const DEFAULT_VIDEO_POLL_BACKOFF: VideoPollBackoff = {\n initialDelayMillis: 10_000,\n delayMultiplier: 1.5,\n maxDelayMillis: 60_000,\n};\n\nexport interface VideoModerationOptions {\n rejectionLikelihoods: ReadonlySet<string>;\n logger?: ModerationLogger;\n getClient?: () => Promise<VideoIntelligenceServiceClient>;\n pollBackoff?: VideoPollBackoff;\n}\n\nlet _defaultClient: VideoIntelligenceServiceClient | null = null;\nasync function defaultGetVideoClient(): Promise<VideoIntelligenceServiceClient> {\n if (_defaultClient) return _defaultClient;\n const mod = await import('@google-cloud/video-intelligence');\n _defaultClient = new mod.VideoIntelligenceServiceClient();\n return _defaultClient;\n}\n\n// Video Intelligence accepts only gs:// URIs, never HTTPS URLs.\nexport async function moderateVideo(\n gcsUri: string,\n options: VideoModerationOptions,\n): Promise<MediaModerationResult> {\n const { rejectionLikelihoods, logger } = options;\n const getClient = options.getClient ?? defaultGetVideoClient;\n\n logger?.info?.(`[moderation-core] Video moderation: ${gcsUri}`);\n\n const { protos } = await import('@google-cloud/video-intelligence');\n const client = await getClient();\n\n const pollBackoff = options.pollBackoff ?? DEFAULT_VIDEO_POLL_BACKOFF;\n const [operation] = await client.annotateVideo(\n {\n inputUri: gcsUri,\n features: [protos.google.cloud.videointelligence.v1.Feature.EXPLICIT_CONTENT_DETECTION],\n },\n {\n longrunning: {\n initialRetryDelayMillis: pollBackoff.initialDelayMillis,\n retryDelayMultiplier: pollBackoff.delayMultiplier,\n maxRetryDelayMillis: pollBackoff.maxDelayMillis,\n },\n },\n );\n\n const [result] = await operation.promise();\n const annotation = result.annotationResults?.[0];\n const explicitAnnotation = annotation?.explicitAnnotation;\n\n // An error or an empty frame list means nothing was checked.\n if (annotation?.error || !explicitAnnotation?.frames?.length) {\n logger?.warn?.(\n '[moderation-core] Video analysis returned no checked frames; rejecting as precaution',\n );\n return {\n safe: false,\n reason: 'Content moderation check failed',\n scores: { adult: 'UNKNOWN', violence: 'UNKNOWN', racy: 'UNKNOWN' },\n };\n }\n\n let maxAdult: Likelihood = 'VERY_UNLIKELY';\n let foundExplicit = false;\n\n for (const frame of explicitAnnotation.frames) {\n if (isRejectionLikelihood(frame.pornographyLikelihood, rejectionLikelihoods)) {\n maxAdult = likelihoodToString(frame.pornographyLikelihood);\n foundExplicit = true;\n break;\n }\n const current = likelihoodToString(frame.pornographyLikelihood);\n if (LIKELIHOOD_ORDER[current] > LIKELIHOOD_ORDER[maxAdult]) {\n maxAdult = current;\n }\n }\n\n const safe = !foundExplicit;\n const reason = safe ? undefined : 'Adult or sexually explicit content is not allowed';\n\n return {\n safe,\n reason,\n scores: { adult: maxAdult, violence: 'NOT_CHECKED', racy: 'NOT_CHECKED' },\n };\n}\n\nexport function __resetDefaultVideoClient() {\n _defaultClient = null;\n}\n"]}
|
package/dist/types.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
export type Likelihood = "UNKNOWN" | "VERY_UNLIKELY" | "UNLIKELY" | "POSSIBLE" | "LIKELY" | "VERY_LIKELY";
|
|
1
|
+
export type Likelihood = 'UNKNOWN' | 'VERY_UNLIKELY' | 'UNLIKELY' | 'POSSIBLE' | 'LIKELY' | 'VERY_LIKELY';
|
|
3
2
|
export interface MediaModerationScores {
|
|
4
3
|
adult: string;
|
|
5
4
|
violence: string;
|
|
@@ -14,9 +13,8 @@ export interface TextModerationResult {
|
|
|
14
13
|
safe: boolean;
|
|
15
14
|
reason?: string;
|
|
16
15
|
flaggedWords?: string[];
|
|
17
|
-
layer?:
|
|
16
|
+
layer?: 'word_filter';
|
|
18
17
|
}
|
|
19
|
-
/** Generic logger used for diagnostic output. Consumers wire to their own. */
|
|
20
18
|
export interface ModerationLogger {
|
|
21
19
|
info?: (message: string, ...args: unknown[]) => void;
|
|
22
20
|
warn?: (message: string, ...args: unknown[]) => void;
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,UAAU,GACpB,SAAS,GAAG,eAAe,GAAG,UAAU,GAAG,UAAU,GAAG,QAAQ,GAAG,aAAa,CAAC;AAEnF,MAAM,WAAW,qBAAqB;IACpC,KAAK,EAAE,MAAM,CAAC;IACd,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,qBAAqB,CAAC;CAC/B;AAED,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,KAAK,CAAC,EAAE,aAAa,CAAC;CACvB;AAED,MAAM,WAAW,gBAAgB;IAC/B,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;IACrD,IAAI,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;IACrD,KAAK,CAAC,EAAE,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,OAAO,EAAE,KAAK,IAAI,CAAC;CACvD"}
|
package/dist/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"","sourcesContent":["export type Likelihood =\n 'UNKNOWN' | 'VERY_UNLIKELY' | 'UNLIKELY' | 'POSSIBLE' | 'LIKELY' | 'VERY_LIKELY';\n\nexport interface MediaModerationScores {\n adult: string;\n violence: string;\n racy: string;\n}\n\nexport interface MediaModerationResult {\n safe: boolean;\n reason?: string;\n scores: MediaModerationScores;\n}\n\nexport interface TextModerationResult {\n safe: boolean;\n reason?: string;\n flaggedWords?: string[];\n layer?: 'word_filter';\n}\n\nexport interface ModerationLogger {\n info?: (message: string, ...args: unknown[]) => void;\n warn?: (message: string, ...args: unknown[]) => void;\n error?: (message: string, ...args: unknown[]) => void;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ttt-productions/moderation-core",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.4",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/ttt-productions/ttt-packages.git",
|
|
@@ -43,9 +43,8 @@
|
|
|
43
43
|
}
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@google-cloud/vision": "^
|
|
47
|
-
"@google-cloud/video-intelligence": "^
|
|
48
|
-
"typescript": "^5.8.3"
|
|
46
|
+
"@google-cloud/vision": "^6.1.0",
|
|
47
|
+
"@google-cloud/video-intelligence": "^7.1.0"
|
|
49
48
|
},
|
|
50
49
|
"engines": {
|
|
51
50
|
"node": ">=22",
|