@nodaro/shared 3.14.0 → 3.18.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.
Files changed (153) hide show
  1. package/dist/index.cjs +1679 -81
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.d.cts +1822 -49
  4. package/dist/index.d.ts +1822 -49
  5. package/dist/index.js +1491 -82
  6. package/dist/index.js.map +1 -1
  7. package/package.json +1 -1
  8. package/src/__tests__/apply-edl-credit-id.test.ts +25 -0
  9. package/src/__tests__/camera-switch-contract.test.ts +98 -0
  10. package/src/__tests__/competitors.test.ts +49 -0
  11. package/src/__tests__/content-recipe-ideas.test.ts +96 -0
  12. package/src/__tests__/edl-multicam.test.ts +192 -0
  13. package/src/__tests__/exposed-field-keys.test.ts +78 -0
  14. package/src/__tests__/instagram-scrape.test.ts +62 -0
  15. package/src/__tests__/llm-models.test.ts +5 -2
  16. package/src/__tests__/music-provider.test.ts +17 -0
  17. package/src/__tests__/node-runtime-keys.test.ts +11 -0
  18. package/src/__tests__/parameter-node-value.test.ts +2 -1
  19. package/src/__tests__/per-unit-video-pricing.test.ts +97 -0
  20. package/src/__tests__/producer-types.test.ts +35 -0
  21. package/src/__tests__/saved-posts.test.ts +37 -0
  22. package/src/__tests__/script-settings.test.ts +61 -0
  23. package/src/__tests__/settings-input.test.ts +252 -0
  24. package/src/__tests__/social-search.test.ts +243 -0
  25. package/src/__tests__/studio-sequence-owner.test.ts +356 -0
  26. package/src/__tests__/studio-take-voice.test.ts +200 -0
  27. package/src/__tests__/studio-transient.test.ts +71 -0
  28. package/src/__tests__/sub-gates.test.ts +21 -0
  29. package/src/__tests__/telegram-account-trigger.test.ts +120 -0
  30. package/src/__tests__/text-to-audio-pricing.test.ts +71 -0
  31. package/src/__tests__/tts-capabilities.test.ts +237 -0
  32. package/src/__tests__/video-analysis-pricing.test.ts +27 -13
  33. package/src/__tests__/video-link.test.ts +2 -2
  34. package/src/__tests__/video-utils.test.ts +89 -83
  35. package/src/__tests__/workflow-export-strip.test.ts +37 -0
  36. package/src/camera-switch-contract.ts +166 -0
  37. package/src/catalog-projection.ts +7 -0
  38. package/src/competitors.ts +430 -0
  39. package/src/content-recipe-ideas.ts +69 -0
  40. package/src/credit-estimators/index.ts +21 -0
  41. package/src/credit-estimators/ltx-extend.ts +33 -0
  42. package/src/credit-estimators/ltx-retake.ts +30 -0
  43. package/src/credit-estimators/text-to-audio.ts +58 -0
  44. package/src/credit-estimators/video-sfx.ts +41 -0
  45. package/src/credit-estimators/video-utils.ts +38 -24
  46. package/src/credit-identifiers.ts +22 -0
  47. package/src/edl-multicam.ts +220 -0
  48. package/src/exposed-field-keys.ts +49 -0
  49. package/src/fan-out-rows.ts +7 -2
  50. package/src/i18n/action-fx.ko.ts +18 -18
  51. package/src/i18n/action-fx.pt-BR.ts +66 -66
  52. package/src/i18n/aesthetic.ko.ts +49 -49
  53. package/src/i18n/aesthetic.pt-BR.ts +14 -14
  54. package/src/i18n/animals.ko.ts +126 -126
  55. package/src/i18n/animals.pt-BR.ts +33 -33
  56. package/src/i18n/atmosphere.ko.ts +37 -37
  57. package/src/i18n/atmosphere.pt-BR.ts +29 -29
  58. package/src/i18n/backdrop.ko.ts +40 -40
  59. package/src/i18n/backdrop.pt-BR.ts +38 -38
  60. package/src/i18n/camera-format.ko.ts +31 -31
  61. package/src/i18n/camera-format.pt-BR.ts +12 -12
  62. package/src/i18n/camera-motions.ko.ts +66 -66
  63. package/src/i18n/camera-motions.pt-BR.ts +51 -51
  64. package/src/i18n/character-fx.ko.ts +19 -19
  65. package/src/i18n/character-fx.pt-BR.ts +64 -52
  66. package/src/i18n/character-motion.ko.ts +164 -164
  67. package/src/i18n/character-motion.pt-BR.ts +867 -858
  68. package/src/i18n/color-look.ko.ts +41 -41
  69. package/src/i18n/color-look.pt-BR.ts +23 -23
  70. package/src/i18n/composition-effects.ko.ts +18 -18
  71. package/src/i18n/composition-effects.pt-BR.ts +17 -17
  72. package/src/i18n/era.ko.ts +28 -28
  73. package/src/i18n/era.pt-BR.ts +20 -20
  74. package/src/i18n/exposure-settings.ko.ts +20 -20
  75. package/src/i18n/exposure-settings.pt-BR.ts +3 -3
  76. package/src/i18n/framing.ko.ts +66 -66
  77. package/src/i18n/framing.pt-BR.ts +50 -50
  78. package/src/i18n/furniture.ko.ts +78 -78
  79. package/src/i18n/furniture.pt-BR.ts +46 -46
  80. package/src/i18n/held-prop.ko.ts +60 -60
  81. package/src/i18n/held-prop.pt-BR.ts +30 -30
  82. package/src/i18n/instrumentation.ko.ts +62 -62
  83. package/src/i18n/instrumentation.pt-BR.ts +64 -64
  84. package/src/i18n/lens.ko.ts +16 -16
  85. package/src/i18n/lens.pt-BR.ts +5 -5
  86. package/src/i18n/lighting.ko.ts +68 -68
  87. package/src/i18n/lighting.pt-BR.ts +43 -43
  88. package/src/i18n/loop-subject.ko.ts +1 -1
  89. package/src/i18n/loop-subject.pt-BR.ts +31 -31
  90. package/src/i18n/materials.ko.ts +66 -66
  91. package/src/i18n/materials.pt-BR.ts +22 -22
  92. package/src/i18n/mood.ko.ts +50 -50
  93. package/src/i18n/mood.pt-BR.ts +12 -12
  94. package/src/i18n/music-genre.ko.ts +28 -28
  95. package/src/i18n/music-genre.pt-BR.ts +50 -50
  96. package/src/i18n/music-mood.ko.ts +27 -27
  97. package/src/i18n/music-mood.pt-BR.ts +20 -20
  98. package/src/i18n/person.ko.ts +396 -396
  99. package/src/i18n/person.pt-BR.ts +237 -237
  100. package/src/i18n/photo-genre.ko.ts +46 -46
  101. package/src/i18n/photo-genre.pt-BR.ts +32 -32
  102. package/src/i18n/photographer.ko.ts +63 -63
  103. package/src/i18n/photographer.pt-BR.ts +15 -15
  104. package/src/i18n/pose.ko.ts +81 -81
  105. package/src/i18n/pose.pt-BR.ts +65 -65
  106. package/src/i18n/post-process-effects.ko.ts +18 -18
  107. package/src/i18n/post-process-effects.pt-BR.ts +18 -18
  108. package/src/i18n/render-quality.ko.ts +24 -24
  109. package/src/i18n/render-quality.pt-BR.ts +12 -12
  110. package/src/i18n/setting.ko.ts +64 -64
  111. package/src/i18n/setting.pt-BR.ts +41 -41
  112. package/src/i18n/style.ko.ts +49 -49
  113. package/src/i18n/style.pt-BR.ts +19 -19
  114. package/src/i18n/styling.ko.ts +265 -265
  115. package/src/i18n/styling.pt-BR.ts +141 -141
  116. package/src/i18n/temporal.ko.ts +18 -18
  117. package/src/i18n/temporal.pt-BR.ts +12 -12
  118. package/src/i18n/transitions.ko.ts +14 -14
  119. package/src/i18n/transitions.pt-BR.ts +83 -71
  120. package/src/i18n/vehicles.ko.ts +107 -107
  121. package/src/i18n/vehicles.pt-BR.ts +55 -55
  122. package/src/i18n/voice-character.ko.ts +19 -19
  123. package/src/i18n/voice-character.pt-BR.ts +19 -19
  124. package/src/i18n/voice-delivery.ko.ts +22 -22
  125. package/src/i18n/voice-delivery.pt-BR.ts +22 -22
  126. package/src/i18n/weapons.ko.ts +85 -85
  127. package/src/i18n/weapons.pt-BR.ts +46 -46
  128. package/src/index.ts +236 -1
  129. package/src/instagram-scrape.ts +91 -16
  130. package/src/llm-models.ts +11 -0
  131. package/src/model-catalog.ts +151 -16
  132. package/src/model-constants.ts +54 -16
  133. package/src/node-mappable-fields.ts +21 -5
  134. package/src/node-runtime-keys.ts +45 -0
  135. package/src/oauth-scopes.ts +28 -0
  136. package/src/parameter-node-value.ts +13 -5
  137. package/src/pipeline-defaults.ts +14 -0
  138. package/src/pipeline-state-types.ts +14 -0
  139. package/src/presentation-utils.ts +5 -1
  140. package/src/producer-types.ts +85 -0
  141. package/src/recast-segment-packs.ts +13 -0
  142. package/src/resolve-field-mappings.ts +15 -5
  143. package/src/saved-posts.ts +106 -0
  144. package/src/script-settings.ts +70 -0
  145. package/src/settings-input.ts +366 -0
  146. package/src/social-search.ts +467 -0
  147. package/src/studio-transient.ts +479 -7
  148. package/src/telegram-account-send.ts +32 -0
  149. package/src/telegram-account-trigger.ts +108 -0
  150. package/src/tts-capabilities.ts +75 -0
  151. package/src/video-analysis-pricing.ts +17 -6
  152. package/src/video-link.ts +16 -4
  153. package/src/workflow-export.ts +7 -0
@@ -6,6 +6,7 @@
6
6
  import type { GenericNode, GenericEdge } from "./types.js"
7
7
  import type { PresentationItem } from "./presentation-types.js"
8
8
  import { AUDIO_PRODUCER_TYPES, VIDEO_PRODUCER_TYPES } from "./producer-types.js"
9
+ import { canonicalizeOverrideKeys } from "./exposed-field-keys.js"
9
10
 
10
11
  // ---------------------------------------------------------------------------
11
12
  // Node type sets
@@ -422,13 +423,16 @@ const MEDIA_INPUT_FIELD_TYPES: ReadonlySet<InputFieldSchema["type"]> = new Set([
422
423
  *
423
424
  * Schema-driven on purpose: a new media input node is covered by its
424
425
  * `INPUT_FIELD_MAP` row, with no list to remember here.
426
+ *
427
+ * An override sent under a renamed exposed-field key (an app published before
428
+ * the rename) lands on the node's current field — `canonicalizeOverrideKeys`.
425
429
  */
426
430
  export function mergeNodeInputOverrides(
427
431
  nodeType: string | undefined,
428
432
  data: Record<string, unknown>,
429
433
  overrides: Record<string, unknown>,
430
434
  ): Record<string, unknown> {
431
- const merged: Record<string, unknown> = { ...data, ...overrides }
435
+ const merged: Record<string, unknown> = { ...data, ...canonicalizeOverrideKeys(nodeType, overrides) }
432
436
  const schema = nodeType ? INPUT_FIELD_MAP[nodeType] : undefined
433
437
  if (
434
438
  schema &&
@@ -239,4 +239,89 @@ export const FAN_OUT_EACH_TYPES: ReadonlySet<string> = new Set([
239
239
  // to the scalar `edl` value (no fan-out) — the same graceful degradation
240
240
  // web-scrape relies on. See `unwrapEditPlanOutput` in `edit-plan-contract.ts`.
241
241
  "edit-plan",
242
+ // Content Ideas emits one creative brief per idea on `listResults`, so an
243
+ // edge leaving it defaults to "each" — the node after it (typically
244
+ // Generate Script) runs once per idea, on the server too.
245
+ "content-ideas",
242
246
  ])
247
+
248
+ /**
249
+ * Per-HANDLE "each" defaults, for a node whose outputs differ in kind: on the
250
+ * `each` handles an edge without an explicit `outputMode` defaults to "each",
251
+ * on its other handles to "last". `primary` is the handle an edge with no
252
+ * `sourceHandle` reads (agent-written workflow JSON often omits it).
253
+ *
254
+ * The list an "each" edge reads is the node's per-iteration results
255
+ * (`listResults`) — it exists only when the node itself ran once per item, so
256
+ * after a single run the edge degrades to the single value, exactly like a
257
+ * `FAN_OUT_EACH_TYPES` member with one item.
258
+ */
259
+ export const FAN_OUT_EACH_HANDLES: Readonly<Record<string, { readonly each: readonly string[]; readonly primary: string }>> = {
260
+ // Camera Switch run once per clip (Edit Plan in clips mode): each clip's
261
+ // switched EDL is its own Apply EDL render. Its transcript is the same for
262
+ // every clip (the master clock, renamed), so that handle stays one value
263
+ // (decided 2026-10-04).
264
+ "camera-switch": { each: ["edl"], primary: "edl" },
265
+ }
266
+
267
+ /** The `outputMode` an edge has when none is set on it — the ONE rule both
268
+ * engines, the credit estimate and the editor read. */
269
+ export function defaultEdgeOutputMode(
270
+ sourceType: string | null | undefined,
271
+ sourceHandle: string | null | undefined,
272
+ ): "each" | "last" {
273
+ if (typeof sourceType !== "string") return "last"
274
+ if (FAN_OUT_EACH_TYPES.has(sourceType)) return "each"
275
+ const entry = FAN_OUT_EACH_HANDLES[sourceType]
276
+ if (!entry) return "last"
277
+ return entry.each.includes(sourceHandle || entry.primary) ? "each" : "last"
278
+ }
279
+
280
+ /** Whether an edge from `sourceHandle` reads the node's per-iteration results
281
+ * (`listResults` — each iteration's PRIMARY output). False on the other
282
+ * handles of a `FAN_OUT_EACH_HANDLES` node: Camera Switch's transcript is one
283
+ * value, never the list of switched EDLs, whatever mode its edge is set to. */
284
+ export function listResultsServeHandle(
285
+ sourceType: string | null | undefined,
286
+ sourceHandle: string | null | undefined,
287
+ ): boolean {
288
+ const entry = typeof sourceType === "string" ? FAN_OUT_EACH_HANDLES[sourceType] : undefined
289
+ return !entry || entry.each.includes(sourceHandle || entry.primary)
290
+ }
291
+
292
+ /**
293
+ * FAN-IN targets: nodes that FOLD everything wired into them into ONE run,
294
+ * instead of running once per upstream item. Keyed by node type; the value
295
+ * names the target handles that fold (`"*"` = every handle). An edge into any
296
+ * other handle of such a node is routed normally — Content Ideas folds its
297
+ * recipes but reads its brand from a `field-brand` wire like any field.
298
+ *
299
+ * Single source of truth for both engines' input resolvers (the backend and
300
+ * the editor each used to keep a private `new Set(["reduce"])`). A fan-in
301
+ * node is never itself fanned out by an upstream list.
302
+ */
303
+ export const FAN_IN_TARGETS: Readonly<Record<string, "*" | readonly string[]>> = {
304
+ // Choose Best — every wire is a candidate.
305
+ reduce: "*",
306
+ // Content Ideas — one or more recipes, from several Content Recipe nodes
307
+ // and/or one that ran once per post.
308
+ "content-ideas": ["recipes"],
309
+ // Telegram Reply — one message per run, whatever is wired into its text
310
+ // input: a list (or a writer that ran once per item) arrives as one
311
+ // message, never as a burst of sends to the owner's chat.
312
+ "telegram-account-send": ["in"],
313
+ }
314
+
315
+ export function isFanInNodeType(nodeType: string | undefined | null): boolean {
316
+ return typeof nodeType === "string" && Object.prototype.hasOwnProperty.call(FAN_IN_TARGETS, nodeType)
317
+ }
318
+
319
+ /** True when an edge into `targetHandle` of a `targetType` node is folded.
320
+ * An edge with NO target handle (workflow JSON written by an agent often
321
+ * omits it) lands on the node's primary input, which is the folding one. */
322
+ export function isFanInEdge(targetType: string | undefined | null, targetHandle: string | null | undefined): boolean {
323
+ if (!isFanInNodeType(targetType)) return false
324
+ const spec = FAN_IN_TARGETS[targetType as string]!
325
+ if (spec === "*") return true
326
+ return targetHandle == null || targetHandle === "" || spec.includes(targetHandle)
327
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * How a recast render packs scenes into parts — the values `segmentSec` takes
3
+ * on `POST /v1/recast`, `/v1/recast/estimate` and `/v1/recast/:id/start`. A
4
+ * name, not seconds, despite the field's name:
5
+ *
6
+ * - `"scenes-max"` (Long): whole scenes grouped into as few parts as fit, the
7
+ * fewest seams.
8
+ * - `"scenes"` (Short): whole scenes in shorter parts.
9
+ * - `"max"`: the longest parts the model allows, not grouped by scene.
10
+ */
11
+ export const RECAST_SEGMENT_PACKS = ["max", "scenes-max", "scenes"] as const
12
+
13
+ export type RecastSegmentPack = (typeof RECAST_SEGMENT_PACKS)[number]
@@ -1,5 +1,11 @@
1
1
  import { injectUpstream } from "./inject-upstream.js"
2
2
 
3
+ /** A live wire into a field's handle: which node, and which of its outputs. */
4
+ export interface FieldEdgeSource {
5
+ readonly sourceNodeId: string
6
+ readonly sourceHandle?: string | null
7
+ }
8
+
3
9
  /**
4
10
  * Resolve fieldMappings + {} injection for all text fields on a node.
5
11
  *
@@ -19,23 +25,27 @@ import { injectUpstream } from "./inject-upstream.js"
19
25
  * from NodeExecutionState.output. Same pattern as ancestor-refs.ts.
20
26
  *
21
27
  * `edgeSourceForField` is optional: when omitted, behaviour is identical to
22
- * the prior fieldMappings-only resolver (existing callers stay valid).
28
+ * the prior fieldMappings-only resolver (existing callers stay valid). When
29
+ * it names the wire's source HANDLE too, the value is that handle's output —
30
+ * a Router's route, a trigger's named output — not the source's primary one.
23
31
  */
24
32
  export function resolveFieldMappings(
25
33
  data: Record<string, unknown>,
26
34
  upstreamText: string | undefined,
27
35
  mappableFieldNames: ReadonlyArray<string>,
28
- getSourceOutput: (sourceNodeId: string) => string | undefined,
29
- edgeSourceForField?: (field: string) => string | undefined,
36
+ getSourceOutput: (sourceNodeId: string, sourceHandle?: string | null) => string | undefined,
37
+ edgeSourceForField?: (field: string) => FieldEdgeSource | string | undefined,
30
38
  ): Record<string, unknown> {
31
39
  const fm = data.fieldMappings as Record<string, { sourceNodeId: string }> | undefined
32
40
  const resolved = { ...data }
33
41
 
34
42
  for (const field of mappableFieldNames) {
35
- const sourceNodeId = edgeSourceForField?.(field) ?? fm?.[field]?.sourceNodeId
43
+ const wired = edgeSourceForField?.(field)
44
+ const edge: FieldEdgeSource | undefined = typeof wired === "string" ? { sourceNodeId: wired } : wired
45
+ const sourceNodeId = edge?.sourceNodeId ?? fm?.[field]?.sourceNodeId
36
46
 
37
47
  if (sourceNodeId) {
38
- const output = getSourceOutput(sourceNodeId)
48
+ const output = getSourceOutput(sourceNodeId, edge?.sourceHandle)
39
49
  if (output != null) resolved[field] = output
40
50
  } else {
41
51
  const current = resolved[field]
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Saved posts — the inspiration wall's wire contract.
3
+ *
4
+ * A person saves a post they found (a Social Search result) with a note and
5
+ * tags, to come back to it or feed it into a workflow. The row keeps a
6
+ * snapshot of the post, because platform links and numbers change, and the
7
+ * post's still is copied into the person's storage so the wall outlives the
8
+ * platform's signed image links.
9
+ *
10
+ * Why public: `SavedPost` is what `GET /v1/saved-posts` returns and the SDK's
11
+ * `savedPosts` resource types.
12
+ */
13
+ import type { SocialPlatform, SocialPost } from "./social-search.js"
14
+
15
+ /** Where a save came from. */
16
+ export const SAVED_POST_SOURCES = ["picker", "competitors", "manual", "api"] as const
17
+ export type SavedPostSource = (typeof SAVED_POST_SOURCES)[number]
18
+
19
+ export const SAVED_POST_NOTE_MAX = 2000
20
+ export const SAVED_POST_MAX_TAGS = 10
21
+ export const SAVED_POST_TAG_MAX = 40
22
+ export const SAVED_POSTS_PAGE_MAX = 100
23
+ /** How many post ids one lookup may ask about. */
24
+ export const SAVED_POSTS_LOOKUP_MAX = 200
25
+
26
+ export interface SavedPost {
27
+ readonly id: string
28
+ /** The post's own id (`SocialPost.id`); one save per post per person. */
29
+ readonly postId: string
30
+ readonly platform: SocialPlatform
31
+ readonly url: string
32
+ /** The post as it was when saved. */
33
+ readonly post: SocialPost
34
+ /** The still, copied into the person's storage; null when the copy failed
35
+ * (the post's own `media.thumbnailUrl` is then the only, expiring, link). */
36
+ readonly thumbnailUrl: string | null
37
+ readonly note: string
38
+ readonly tags: readonly string[]
39
+ readonly source: SavedPostSource
40
+ readonly createdAt: string
41
+ readonly updatedAt: string
42
+ }
43
+
44
+ /** The body of `POST /v1/saved-posts`. Saving a post already saved updates
45
+ * its note and tags (when given) and returns the existing save. */
46
+ export type SavePostInput = {
47
+ readonly post: SocialPost
48
+ readonly note?: string
49
+ readonly tags?: readonly string[]
50
+ readonly source?: SavedPostSource
51
+ }
52
+
53
+ /** The body of `PATCH /v1/saved-posts/:id`. */
54
+ export type UpdateSavedPostInput = {
55
+ readonly note?: string
56
+ readonly tags?: readonly string[]
57
+ }
58
+
59
+ /** Query of `GET /v1/saved-posts`. */
60
+ export type ListSavedPostsParams = {
61
+ readonly platform?: SocialPlatform
62
+ /** Only saves carrying this tag. */
63
+ readonly tag?: string
64
+ /** Words to find in the note or the post's text. */
65
+ readonly q?: string
66
+ /** The `nextCursor` of the previous page. */
67
+ readonly cursor?: string
68
+ /** 1–100, default 40. */
69
+ readonly limit?: number
70
+ }
71
+
72
+ export interface ListSavedPostsResult {
73
+ readonly data: readonly SavedPost[]
74
+ readonly nextCursor: string | null
75
+ }
76
+
77
+ /** The answer of `POST /v1/saved-posts/lookup`: which of the asked posts are saved. */
78
+ export interface SavedPostsLookupResult {
79
+ readonly saved: ReadonlyArray<{ readonly postId: string; readonly id: string }>
80
+ }
81
+
82
+ /**
83
+ * Tags as stored: split on commas (`,` `,` `、`, so "hooks, openers" is two
84
+ * tags in any script), trimmed, inner whitespace collapsed, lower-cased, a
85
+ * leading `#` dropped, braces, quotes and backslashes removed (they would
86
+ * change a tag filter's meaning), each at most 40 characters, no repeats, at
87
+ * most 10.
88
+ */
89
+ export function normalizeSavedPostTags(tags: unknown): string[] {
90
+ if (!Array.isArray(tags)) return []
91
+ const clean = tags
92
+ .filter((t): t is string => typeof t === "string")
93
+ .flatMap((t) => t.split(/[,,、]/))
94
+ .map((t) =>
95
+ t
96
+ .replace(/[{}"\\]/g, "")
97
+ .trim()
98
+ .replace(/^#+/, "")
99
+ .replace(/\s+/g, " ")
100
+ .toLowerCase()
101
+ .slice(0, SAVED_POST_TAG_MAX)
102
+ .trim(),
103
+ )
104
+ .filter((t) => t.length > 0)
105
+ return Array.from(new Set(clean)).slice(0, SAVED_POST_MAX_TAGS)
106
+ }
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Generate Script's request settings: ONE reader for both DAG engines, and the
3
+ * limits the `/v1/generate-script` route validates against.
4
+ *
5
+ * A run reads the node's data AFTER field mappings rewrote it, so a value can
6
+ * arrive in a shape its field never had: a Scene Count or Duration node maps
7
+ * its number as TEXT ("6"), and a Tone mapped from a Text node can be a whole
8
+ * paragraph. Passing those on raw failed the editor's single-node run on the
9
+ * route's 400 and sent the server run's text as-is. This reader coerces instead
10
+ * of rejecting (the `normalizeModelInput` principle): a number-like value
11
+ * becomes a whole number clamped to the route's range, text is trimmed and cut
12
+ * to the route's length, and an unusable value is dropped so the generator's
13
+ * own default applies.
14
+ */
15
+ import { resolveNodeRefs } from "./node-refs.js"
16
+ import { LLM_TEXT_INPUT_MAX } from "./model-constants.js"
17
+
18
+ export const SCRIPT_SCENE_COUNT_RANGE = { min: 1, max: 20 } as const
19
+ export const SCRIPT_TARGET_DURATION_RANGE = { min: 5, max: 600 } as const
20
+ /** What the script generator uses when a request leaves the field out. */
21
+ export const SCRIPT_SCENE_COUNT_DEFAULT = 5
22
+ /** Seconds; what the script generator uses when a request leaves it out. */
23
+ export const SCRIPT_TARGET_DURATION_DEFAULT = 60
24
+ export const SCRIPT_TONE_MAX_LENGTH = 200
25
+ export const SCRIPT_STYLE_GUIDE_MAX_LENGTH = LLM_TEXT_INPUT_MAX
26
+
27
+ export interface ScriptSettings {
28
+ sceneCount?: number
29
+ tone?: string
30
+ /** Seconds. */
31
+ targetDuration?: number
32
+ styleGuide?: string
33
+ }
34
+
35
+ function clampInt(value: unknown, range: { readonly min: number; readonly max: number }): number | undefined {
36
+ const n = typeof value === "number"
37
+ ? value
38
+ : typeof value === "string" && value.trim().length > 0 ? Number(value) : Number.NaN
39
+ if (!Number.isFinite(n)) return undefined
40
+ return Math.min(range.max, Math.max(range.min, Math.round(n)))
41
+ }
42
+
43
+ function clampText(value: unknown, max: number): string | undefined {
44
+ if (typeof value !== "string") return undefined
45
+ const text = value.trim()
46
+ return text.length > 0 ? text.slice(0, max) : undefined
47
+ }
48
+
49
+ /**
50
+ * The settings a Generate Script node sends. `refMap` resolves `{Node Label}`
51
+ * references in the style guide, the one field here with a reference-aware
52
+ * editor; references with no matching node stay as typed.
53
+ */
54
+ export function readScriptSettings(
55
+ data: Record<string, unknown>,
56
+ refMap?: ReadonlyMap<string, string>,
57
+ ): ScriptSettings {
58
+ const styleGuide = typeof data.styleGuide === "string" && refMap && refMap.size > 0
59
+ ? resolveNodeRefs(data.styleGuide, refMap)
60
+ : data.styleGuide
61
+ return {
62
+ sceneCount: clampInt(data.sceneCount, SCRIPT_SCENE_COUNT_RANGE),
63
+ // The panel's own fields (the ones a mapping writes) come first. `style` is
64
+ // an older key for the tone, and `targetDuration` is the route's name for
65
+ // the length, which workflow JSON written by an agent can carry.
66
+ tone: clampText(data.tone ?? data.style, SCRIPT_TONE_MAX_LENGTH),
67
+ targetDuration: clampInt(data.targetLength ?? data.targetDuration, SCRIPT_TARGET_DURATION_RANGE),
68
+ styleGuide: clampText(styleGuide, SCRIPT_STYLE_GUIDE_MAX_LENGTH),
69
+ }
70
+ }