@purpleschool/gptbot-tools 0.0.32 → 0.0.34

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.
@@ -4,6 +4,7 @@ exports.VideoCommand = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const command_response_schema_1 = require("../../common/models/command-response.schema");
6
6
  const video_job_schema_1 = require("../models/video-job.schema");
7
+ const models_1 = require("../models");
7
8
  var VideoCommand;
8
9
  (function (VideoCommand) {
9
10
  VideoCommand.RequestSchema = zod_1.z.object({
@@ -12,12 +13,7 @@ var VideoCommand;
12
13
  prompt: zod_1.z.string(),
13
14
  userBalance: zod_1.z.number(),
14
15
  modelId: zod_1.z.string(),
15
- params: zod_1.z.object({
16
- imageUrls: zod_1.z.string().array().optional(),
17
- duration: zod_1.z.number(),
18
- aspectRatio: zod_1.z.string().optional(),
19
- quality: zod_1.z.string().optional(),
20
- }),
16
+ params: models_1.VideoGenerationRequestParamsSchema,
21
17
  });
22
18
  VideoCommand.ResponseSchema = (0, command_response_schema_1.ICommandResponseSchema)(video_job_schema_1.VideoJobSchema);
23
19
  })(VideoCommand || (exports.VideoCommand = VideoCommand = {}));
@@ -22,3 +22,4 @@ __exportStar(require("./veo-aspect-ratio.enum"), exports);
22
22
  __exportStar(require("./veo-person-allow.enum"), exports);
23
23
  __exportStar(require("./veo-job-params-type.enum"), exports);
24
24
  __exportStar(require("./video-resolution.enum"), exports);
25
+ __exportStar(require("./video-pricing-rule-type.enum"), exports);
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VIDEO_PRICING_RULE_TYPE = void 0;
4
+ var VIDEO_PRICING_RULE_TYPE;
5
+ (function (VIDEO_PRICING_RULE_TYPE) {
6
+ VIDEO_PRICING_RULE_TYPE["FLAT"] = "flat";
7
+ })(VIDEO_PRICING_RULE_TYPE || (exports.VIDEO_PRICING_RULE_TYPE = VIDEO_PRICING_RULE_TYPE = {}));
@@ -19,3 +19,4 @@ __exportStar(require("./enums"), exports);
19
19
  __exportStar(require("./models"), exports);
20
20
  __exportStar(require("./queries"), exports);
21
21
  __exportStar(require("./routes"), exports);
22
+ __exportStar(require("./utils"), exports);
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.VideoModelSchema = exports.VideoModelParamsSchema = void 0;
3
+ exports.VideoModelSchema = exports.VideoModelPricingRulesSchema = exports.VideoModelPricingRuleConditionSchema = exports.VideoGenerationRequestParamsSchema = exports.VideoModelParamsSchema = void 0;
4
4
  const zod_1 = require("zod");
5
5
  const common_1 = require("../../common");
6
6
  const enums_1 = require("../enums");
@@ -19,6 +19,20 @@ exports.VideoModelParamsSchema = zod_1.z.object({
19
19
  .optional(),
20
20
  quality: zod_1.z.string().optional(),
21
21
  });
22
+ exports.VideoGenerationRequestParamsSchema = zod_1.z.object({
23
+ imageUrls: zod_1.z.string().array().optional(),
24
+ duration: zod_1.z.number(),
25
+ aspectRatio: zod_1.z.string().optional(),
26
+ quality: zod_1.z.string().optional(),
27
+ });
28
+ exports.VideoModelPricingRuleConditionSchema = zod_1.z.object({
29
+ aspectRatio: zod_1.z.string(),
30
+ });
31
+ exports.VideoModelPricingRulesSchema = zod_1.z.array(zod_1.z.object({
32
+ type: zod_1.z.nativeEnum(enums_1.VIDEO_PRICING_RULE_TYPE),
33
+ condition: exports.VideoModelPricingRuleConditionSchema,
34
+ value: zod_1.z.number(),
35
+ }));
22
36
  exports.VideoModelSchema = zod_1.z.object({
23
37
  uuid: zod_1.z.string(),
24
38
  title: zod_1.z.string(),
@@ -30,6 +44,7 @@ exports.VideoModelSchema = zod_1.z.object({
30
44
  strategy: zod_1.z.nativeEnum(enums_1.VIDEO_GENERATION_STRATEGY),
31
45
  maxInputLength: zod_1.z.number(),
32
46
  params: exports.VideoModelParamsSchema,
47
+ pricingRules: exports.VideoModelPricingRulesSchema,
33
48
  createdAt: zod_1.z.date(),
34
49
  updatedAt: zod_1.z.date(),
35
50
  });
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateVideoGenerationPrice = calculateVideoGenerationPrice;
4
+ const enums_1 = require("../enums");
5
+ function matchesCondition(condition, params) {
6
+ const keys = Object.keys(condition);
7
+ return keys.every((key) => {
8
+ const expected = condition[key];
9
+ if (expected === undefined || expected === null || expected === '') {
10
+ return true;
11
+ }
12
+ const value = params[key];
13
+ if (value === undefined || value === null) {
14
+ return false;
15
+ }
16
+ return value === expected;
17
+ });
18
+ }
19
+ function calculateVideoGenerationPrice({ pricePerSecond, params, rules, }) {
20
+ const base = pricePerSecond * params.duration;
21
+ const flatMarkup = rules.reduce((sum, r) => {
22
+ if (r.type !== enums_1.VIDEO_PRICING_RULE_TYPE.FLAT) {
23
+ return sum;
24
+ }
25
+ return matchesCondition(r.condition, params) ? sum + r.value : sum;
26
+ }, 0);
27
+ return Math.ceil(base + flatMarkup);
28
+ }
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./calculate-video-generation-price.util"), exports);
18
+ __exportStar(require("./calculate-video-generation-price.util"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@purpleschool/gptbot-tools",
3
- "version": "0.0.32",
3
+ "version": "0.0.34",
4
4
  "main": "build/index.js",
5
5
  "types": "build/index.d.ts",
6
6
  "scripts": {
@@ -1,6 +1,7 @@
1
1
  import { z } from 'zod';
2
2
  import { ICommandResponseSchema } from '../../common/models/command-response.schema';
3
3
  import { VideoJobSchema } from '../models/video-job.schema';
4
+ import { VideoGenerationRequestParamsSchema } from '../models';
4
5
 
5
6
  export namespace VideoCommand {
6
7
  export const RequestSchema = z.object({
@@ -9,12 +10,7 @@ export namespace VideoCommand {
9
10
  prompt: z.string(),
10
11
  userBalance: z.number(),
11
12
  modelId: z.string(),
12
- params: z.object({
13
- imageUrls: z.string().array().optional(),
14
- duration: z.number(),
15
- aspectRatio: z.string().optional(),
16
- quality: z.string().optional(),
17
- }),
13
+ params: VideoGenerationRequestParamsSchema,
18
14
  });
19
15
  export type Request = z.infer<typeof RequestSchema>;
20
16
 
@@ -6,3 +6,4 @@ export * from './veo-aspect-ratio.enum';
6
6
  export * from './veo-person-allow.enum';
7
7
  export * from './veo-job-params-type.enum';
8
8
  export * from './video-resolution.enum';
9
+ export * from './video-pricing-rule-type.enum';
@@ -0,0 +1,3 @@
1
+ export enum VIDEO_PRICING_RULE_TYPE {
2
+ FLAT = 'flat',
3
+ }
package/video/index.ts CHANGED
@@ -3,3 +3,4 @@ export * from './enums';
3
3
  export * from './models';
4
4
  export * from './queries';
5
5
  export * from './routes';
6
+ export * from './utils';
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod';
2
2
  import { IconVariantsSchema } from '../../common';
3
- import { VIDEO_GENERATION_STRATEGY } from '../enums';
3
+ import { VIDEO_GENERATION_STRATEGY, VIDEO_PRICING_RULE_TYPE } from '../enums';
4
4
 
5
5
  export const VideoModelParamsSchema = z.object({
6
6
  imageAttachment: z.object({
@@ -20,6 +20,28 @@ export const VideoModelParamsSchema = z.object({
20
20
 
21
21
  export type VideoModelParams = z.infer<typeof VideoModelParamsSchema>;
22
22
 
23
+ export const VideoGenerationRequestParamsSchema = z.object({
24
+ imageUrls: z.string().array().optional(),
25
+ duration: z.number(),
26
+ aspectRatio: z.string().optional(),
27
+ quality: z.string().optional(),
28
+ });
29
+ export type VideoGenerationRequestParams = z.infer<typeof VideoGenerationRequestParamsSchema>;
30
+
31
+ export const VideoModelPricingRuleConditionSchema = z.object({
32
+ aspectRatio: z.string(),
33
+ });
34
+ export type VideoModelPricingRuleCondition = z.infer<typeof VideoModelPricingRuleConditionSchema>;
35
+
36
+ export const VideoModelPricingRulesSchema = z.array(
37
+ z.object({
38
+ type: z.nativeEnum(VIDEO_PRICING_RULE_TYPE),
39
+ condition: VideoModelPricingRuleConditionSchema,
40
+ value: z.number(),
41
+ }),
42
+ );
43
+ export type VideoModelPricingRules = z.infer<typeof VideoModelPricingRulesSchema>;
44
+
23
45
  export const VideoModelSchema = z.object({
24
46
  uuid: z.string(),
25
47
  title: z.string(),
@@ -31,6 +53,7 @@ export const VideoModelSchema = z.object({
31
53
  strategy: z.nativeEnum(VIDEO_GENERATION_STRATEGY),
32
54
  maxInputLength: z.number(),
33
55
  params: VideoModelParamsSchema,
56
+ pricingRules: VideoModelPricingRulesSchema,
34
57
  createdAt: z.date(),
35
58
  updatedAt: z.date(),
36
59
  });
@@ -0,0 +1,50 @@
1
+ import { VIDEO_PRICING_RULE_TYPE } from '../enums';
2
+ import {
3
+ VideoGenerationRequestParams,
4
+ VideoModelPricingRuleCondition,
5
+ VideoModelPricingRules,
6
+ } from '../models';
7
+
8
+ function matchesCondition(
9
+ condition: VideoModelPricingRuleCondition,
10
+ params: VideoGenerationRequestParams,
11
+ ): boolean {
12
+ const keys = Object.keys(condition) as (keyof VideoModelPricingRuleCondition)[];
13
+
14
+ return keys.every((key) => {
15
+ const expected = condition[key];
16
+
17
+ if (expected === undefined || expected === null || expected === '') {
18
+ return true;
19
+ }
20
+
21
+ const value = params[key];
22
+ if (value === undefined || value === null) {
23
+ return false;
24
+ }
25
+
26
+ return value === expected;
27
+ });
28
+ }
29
+
30
+ export function calculateVideoGenerationPrice({
31
+ pricePerSecond,
32
+ params,
33
+ rules,
34
+ }: {
35
+ pricePerSecond: number;
36
+ params: VideoGenerationRequestParams;
37
+ rules: VideoModelPricingRules;
38
+ }) {
39
+ const base = pricePerSecond * params.duration;
40
+
41
+ const flatMarkup = rules.reduce((sum, r) => {
42
+ if (r.type !== VIDEO_PRICING_RULE_TYPE.FLAT) {
43
+ return sum;
44
+ }
45
+
46
+ return matchesCondition(r.condition, params) ? sum + r.value : sum;
47
+ }, 0);
48
+
49
+ return Math.ceil(base + flatMarkup);
50
+ }
@@ -0,0 +1,2 @@
1
+ export * from './calculate-video-generation-price.util';
2
+ export * from './calculate-video-generation-price.util';