@kriya-ai/harness 0.0.0-stage → 0.2.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 (185) hide show
  1. package/README.md +379 -2
  2. package/SAFETY.md +162 -0
  3. package/THIRD_PARTY_NOTICES.md +7911 -0
  4. package/bin/kriya.js +577 -0
  5. package/bundle/cordis.patch.yml +397 -0
  6. package/contract/SOURCE.md +15 -0
  7. package/contract/contract.json +1355 -0
  8. package/package.json +57 -4
  9. package/plugins/anchor/index.js +56 -0
  10. package/plugins/anchor/package.json +13 -0
  11. package/plugins/approvals/index.js +183 -0
  12. package/plugins/approvals/package.json +13 -0
  13. package/plugins/brand/DESIGN.md +112 -0
  14. package/plugins/brand/client.js +826 -0
  15. package/plugins/brand/index.js +15 -0
  16. package/plugins/brand/package.json +25 -0
  17. package/plugins/brand/src/client.js +823 -0
  18. package/plugins/browser/index.js +218 -0
  19. package/plugins/browser/package.json +13 -0
  20. package/plugins/budget/index.js +108 -0
  21. package/plugins/budget/package.json +13 -0
  22. package/plugins/changes/client.js +245 -0
  23. package/plugins/changes/index.js +75 -0
  24. package/plugins/changes/package.json +22 -0
  25. package/plugins/edit/client.js +113 -0
  26. package/plugins/edit/index.js +58 -0
  27. package/plugins/edit/package.json +23 -0
  28. package/plugins/fs-freshness/index.js +28 -0
  29. package/plugins/fs-freshness/package.json +13 -0
  30. package/plugins/graph/client.js +178 -0
  31. package/plugins/graph/index.js +4 -0
  32. package/plugins/graph/package.json +22 -0
  33. package/plugins/kriya-handoff/index.js +194 -0
  34. package/plugins/kriya-handoff/package.json +13 -0
  35. package/plugins/kriya-tools/index.js +189 -0
  36. package/plugins/kriya-tools/package.json +13 -0
  37. package/plugins/loop/index.js +743 -0
  38. package/plugins/loop/package.json +13 -0
  39. package/plugins/mirror/index.js +235 -0
  40. package/plugins/mirror/package.json +13 -0
  41. package/plugins/overview/client.js +276 -0
  42. package/plugins/overview/index.js +95 -0
  43. package/plugins/overview/package.json +22 -0
  44. package/plugins/preview/client.js +337 -0
  45. package/plugins/preview/index.js +4 -0
  46. package/plugins/preview/package.json +22 -0
  47. package/plugins/progress/client.js +290 -0
  48. package/plugins/progress/index.js +100 -0
  49. package/plugins/progress/package.json +22 -0
  50. package/plugins/projects/client.js +249 -0
  51. package/plugins/projects/index.js +30 -0
  52. package/plugins/projects/package.json +24 -0
  53. package/plugins/rea/index.js +256 -0
  54. package/plugins/rea/package.json +10 -0
  55. package/plugins/rea/skill/LICENSE +21 -0
  56. package/plugins/rea/skill/SKILL.md +360 -0
  57. package/plugins/rea/skill/references/android-applications.md +36 -0
  58. package/plugins/rea/skill/references/connection-and-recovery.md +127 -0
  59. package/plugins/rea/skill/references/evidence-workflows.md +71 -0
  60. package/plugins/rea/skill/references/javascript-applications.md +218 -0
  61. package/plugins/rea/skill/references/native-and-artifacts.md +297 -0
  62. package/plugins/rea/skill/references/runtime-observation.md +78 -0
  63. package/plugins/remote/index.js +297 -0
  64. package/plugins/remote/package.json +10 -0
  65. package/plugins/router/index.js +84 -0
  66. package/plugins/router/package.json +13 -0
  67. package/plugins/sessions/client.js +68 -0
  68. package/plugins/sessions/index.js +79 -0
  69. package/plugins/sessions/package.json +20 -0
  70. package/plugins/skills/client.js +504 -0
  71. package/plugins/skills/index.js +166 -0
  72. package/plugins/skills/package.json +23 -0
  73. package/src/api.js +97 -0
  74. package/src/browser.js +286 -0
  75. package/src/changes.js +191 -0
  76. package/src/checkpoints.js +313 -0
  77. package/src/daemon.js +503 -0
  78. package/src/doctor.js +137 -0
  79. package/src/edit/core/anchors.js +102 -0
  80. package/src/edit/core/audio.js +158 -0
  81. package/src/edit/core/faces.js +99 -0
  82. package/src/edit/core/ffmpeg.js +258 -0
  83. package/src/edit/core/handoff.js +220 -0
  84. package/src/edit/core/history.js +90 -0
  85. package/src/edit/core/layout.js +92 -0
  86. package/src/edit/core/mix.js +115 -0
  87. package/src/edit/core/ops.js +602 -0
  88. package/src/edit/core/plan.js +266 -0
  89. package/src/edit/core/project.d.ts +134 -0
  90. package/src/edit/core/project.js +307 -0
  91. package/src/edit/core/reframe.js +125 -0
  92. package/src/edit/core/solver.js +416 -0
  93. package/src/edit/core/time.js +110 -0
  94. package/src/edit/core/tonemap.js +57 -0
  95. package/src/edit/director/ask.js +120 -0
  96. package/src/edit/director/gateway.js +16 -0
  97. package/src/edit/director/revise.js +186 -0
  98. package/src/edit/engine/capabilities.js +33 -0
  99. package/src/edit/engine/captions.js +154 -0
  100. package/src/edit/engine/color.js +269 -0
  101. package/src/edit/engine/confine.js +67 -0
  102. package/src/edit/engine/dna.js +148 -0
  103. package/src/edit/engine/index.js +473 -0
  104. package/src/edit/engine/loudness.js +43 -0
  105. package/src/edit/engine/media.js +57 -0
  106. package/src/edit/engine/motion.js +281 -0
  107. package/src/edit/engine/packs.js +87 -0
  108. package/src/edit/engine/probe.js +101 -0
  109. package/src/edit/engine/receipt.js +123 -0
  110. package/src/edit/engine/tool.js +101 -0
  111. package/src/edit/gates/copy.js +113 -0
  112. package/src/edit/gates/index.js +430 -0
  113. package/src/edit/gates/picture.js +130 -0
  114. package/src/edit/gates/sound.js +58 -0
  115. package/src/edit/gates/speech.js +127 -0
  116. package/src/edit/gates/text.js +196 -0
  117. package/src/edit/gates/text.swift +22 -0
  118. package/src/edit/graph/index.js +139 -0
  119. package/src/edit/graph/journal.js +69 -0
  120. package/src/edit/graph/scheduler.js +79 -0
  121. package/src/edit/host/client.js +58 -0
  122. package/src/edit/host/deliver.js +119 -0
  123. package/src/edit/host/jobs.js +84 -0
  124. package/src/edit/host/main.js +135 -0
  125. package/src/edit/host/paths.js +11 -0
  126. package/src/edit/host/projects.js +415 -0
  127. package/src/edit/host/server.js +391 -0
  128. package/src/edit/host/taste.js +135 -0
  129. package/src/edit/host/tavi.js +417 -0
  130. package/src/edit/host/watch.js +141 -0
  131. package/src/edit/index/db.js +109 -0
  132. package/src/edit/index/index.js +236 -0
  133. package/src/edit/index/layers/faces.js +78 -0
  134. package/src/edit/index/layers/faces.swift +18 -0
  135. package/src/edit/index/layers/meta.js +35 -0
  136. package/src/edit/index/layers/music.js +146 -0
  137. package/src/edit/index/layers/quality.js +120 -0
  138. package/src/edit/index/layers/retakes.js +132 -0
  139. package/src/edit/index/layers/screen.js +99 -0
  140. package/src/edit/index/layers/shots.js +29 -0
  141. package/src/edit/index/layers/sound.js +42 -0
  142. package/src/edit/index/layers/words.js +64 -0
  143. package/src/edit/index/story.js +248 -0
  144. package/src/edit/index/views.js +71 -0
  145. package/src/edit/loop/governor.js +112 -0
  146. package/src/edit/loop/loop.js +69 -0
  147. package/src/edit/loop/make.js +115 -0
  148. package/src/edit/loop/repair.js +97 -0
  149. package/src/edit/loop/replay.js +32 -0
  150. package/src/edit/recipes/multicam.js +461 -0
  151. package/src/edit/recipes/reel.js +202 -0
  152. package/src/edit/recipes/short-from-long.js +237 -0
  153. package/src/edit/recipes/tutorial.js +137 -0
  154. package/src/edit/recipes/youtube.js +155 -0
  155. package/src/edit/runtime/cli.js +126 -0
  156. package/src/edit/runtime/doctor.js +122 -0
  157. package/src/edit/runtime/fetch.js +15 -0
  158. package/src/edit/runtime/licences.js +88 -0
  159. package/src/edit/runtime/manifest.js +73 -0
  160. package/src/edit/runtime/setup.js +252 -0
  161. package/src/failure.js +69 -0
  162. package/src/gateway-proxy.js +213 -0
  163. package/src/intent.js +130 -0
  164. package/src/launch.js +315 -0
  165. package/src/live.js +81 -0
  166. package/src/local-skills.js +284 -0
  167. package/src/login.js +82 -0
  168. package/src/loop.js +428 -0
  169. package/src/modes.js +128 -0
  170. package/src/overview.js +71 -0
  171. package/src/paths.js +30 -0
  172. package/src/profiles.js +92 -0
  173. package/src/projects.js +104 -0
  174. package/src/recon.js +244 -0
  175. package/src/rules.js +139 -0
  176. package/src/runner.bundle.js +21172 -0
  177. package/src/sentinel.js +248 -0
  178. package/src/skill-switches.js +130 -0
  179. package/src/store.js +58 -0
  180. package/src/strict.js +124 -0
  181. package/src/verify.js +659 -0
  182. package/src/world.js +268 -0
  183. package/studio/dist/index.html +14 -0
  184. package/studio/dist/studio.css +163 -0
  185. package/studio/dist/studio.js +1 -0
@@ -0,0 +1,602 @@
1
+ // Operations (EDIT_ENGINE §4): the only way anything changes. Each is validated
2
+ // for shape, then for meaning, and a batch applies whole or not at all. Undo needs
3
+ // no inverse per operation: history moves HEAD and replays (history.js).
4
+
5
+ import { anchorSource, openIndex, wordAfter, wordAt, wordBefore } from "./anchors.js"
6
+ import { check, EFFECTS, SHAPES, validateProject } from "./project.js"
7
+
8
+ class Refused extends Error {
9
+ constructor(code, message) {
10
+ super(message)
11
+ this.code = code
12
+ }
13
+ }
14
+
15
+ const refuse = (code, message) => {
16
+ throw new Refused(code, message)
17
+ }
18
+
19
+ const ROLE_ORDER = ["base", "broll", "matte", "overlay", "caption", "dialogue", "sfx", "music", "ambience"]
20
+ const SEQUENCED = ["video", "audio", "image"]
21
+ const PICTURE = ["video", "image"]
22
+ const ONE = { n: 1, d: 1 }
23
+ const SPAN = { "until?": "ref", "duration?": "flicks" }
24
+
25
+ const sequenceOf = (s) => s.project.sequences.find((q) => q.id === s.project.active)
26
+ const trackOf = (s, clip) => sequenceOf(s).tracks.find((t) => t.id === clip.track)
27
+ const isBase = (s, clip) => trackOf(s, clip).role === "base"
28
+ const newId = (s, prefix) => `${prefix}${s.project.nextId++}`
29
+
30
+ function ensureTrack(s, role) {
31
+ const sequence = sequenceOf(s)
32
+ let track = sequence.tracks.find((t) => t.role === role)
33
+ if (!track) {
34
+ track = { id: `t-${role}`, role, order: ROLE_ORDER.indexOf(role), muted: false, locked: false }
35
+ sequence.tracks.push(track)
36
+ }
37
+ if (track.locked) refuse("track_locked", `the ${role} track is locked, so nothing can change on it`)
38
+ return track
39
+ }
40
+
41
+ function clipOf(s, id) {
42
+ const clip = sequenceOf(s).clips.find((c) => c.id === id)
43
+ if (!clip) refuse("unknown_clip", `there is no clip "${id}" in this sequence`)
44
+ return clip
45
+ }
46
+
47
+ function writable(s, id) {
48
+ const clip = clipOf(s, id)
49
+ if (trackOf(s, clip).locked) refuse("track_locked", `clip "${id}" is on a locked track, so it can't change`)
50
+ return clip
51
+ }
52
+
53
+ function assetOf(s, id, kinds) {
54
+ const asset = s.project.assets.find((a) => a.id === id)
55
+ if (!asset) refuse("unknown_asset", `there is no asset "${id}" in this project`)
56
+ if (kinds && !kinds.includes(asset.kind)) refuse("wrong_asset_kind", `asset "${id}" is ${asset.kind}; this needs ${kinds.join(" or ")}`)
57
+ return asset
58
+ }
59
+
60
+ // Every anchor must point at something real before it enters the project
61
+ function known(s, ref) {
62
+ if (typeof ref === "number") return
63
+ if (ref.word !== undefined && !s.index.word.has(ref.word)) refuse("unknown_word", `word "${ref.word}" isn't in the transcript`)
64
+ if (ref.shot !== undefined && !s.index.shot.has(ref.shot)) refuse("unknown_shot", `shot "${ref.shot}" isn't in the index`)
65
+ if (ref.beat !== undefined && !s.index.beat.has(ref.beat)) refuse("unknown_beat", `beat "${ref.beat}" isn't in the index`)
66
+ if (ref.marker !== undefined && !sequenceOf(s).markers.some((m) => m.name === ref.marker || m.id === ref.marker)) refuse("unknown_marker", `there is no marker "${ref.marker}"`)
67
+ if (ref.clip !== undefined) clipOf(s, ref.clip)
68
+ }
69
+
70
+ /** A reference as a time inside an asset. */
71
+ function sourceEdge(s, ref, asset, edge) {
72
+ if (typeof ref === "number") return ref
73
+ known(s, ref)
74
+ const place = anchorSource(ref, s.index, edge)
75
+ if (!place) refuse("not_a_source_time", "this needs a word, a shot or a time inside the asset")
76
+ if (place.asset !== asset) refuse("wrong_asset", `that anchor is in asset "${place.asset}", not "${asset}"`)
77
+ return place.t
78
+ }
79
+
80
+ const clipRange = (s, clip) => [sourceEdge(s, clip.source.in, clip.source.asset, "start"), sourceEdge(s, clip.source.out, clip.source.asset, "end")]
81
+
82
+ function checkRange(s, asset, from, to) {
83
+ if (to <= from) refuse("range_inverted", "the range ends before it starts; give the earlier point first")
84
+ if (from < 0 || (asset.facts.duration !== undefined && to > asset.facts.duration)) refuse("longer_than_source", `that range runs past the end of asset "${asset.id}"`)
85
+ }
86
+
87
+ function anchorAsset(s, op, fallback) {
88
+ if (op.asset) return op.asset
89
+ for (const ref of [op.from, op.to]) {
90
+ if (typeof ref === "object") {
91
+ known(s, ref)
92
+ const place = anchorSource(ref, s.index)
93
+ if (place) return place.asset
94
+ }
95
+ }
96
+ if (fallback) return fallback
97
+ refuse("needs_asset", "a range given as plain times needs an asset")
98
+ }
99
+
100
+ function addClip(s, role, fields) {
101
+ const clip = { id: newId(s, "c"), track: ensureTrack(s, role).id, source: null, at: null, effects: [], speed: ONE, createdBy: s.author, ...fields }
102
+ sequenceOf(s).clips.push(clip)
103
+ return clip
104
+ }
105
+
106
+ function span(s, op) {
107
+ if (op.until === undefined && op.duration === undefined) refuse("needs_duration", "say how long it lasts, with duration or until")
108
+ if (op.until !== undefined) known(s, op.until)
109
+ return op.until !== undefined ? { until: op.until } : { duration: op.duration }
110
+ }
111
+
112
+ /** Sets an effect, merging into one of the same type so each clip has one of each. */
113
+ function setEffect(clip, effect) {
114
+ const i = clip.effects.findIndex((e) => e.type === effect.type)
115
+ if (i >= 0) clip.effects[i] = { ...clip.effects[i], ...effect }
116
+ else clip.effects.push(effect)
117
+ }
118
+
119
+ // Optional fields of an operation, left out when not given
120
+ const given = (op, keys) => Object.fromEntries(keys.filter((key) => op[key] !== undefined).map((key) => [key, op[key]]))
121
+
122
+ function musicClip(s) {
123
+ const clip = sequenceOf(s).clips.find((c) => trackOf(s, c).role === "music")
124
+ if (!clip) refuse("no_music", "there is no music yet; use set_music first")
125
+ return writable(s, clip.id)
126
+ }
127
+
128
+ function block(s, op, blockId, version, variables) {
129
+ known(s, op.at)
130
+ return addClip(s, "overlay", { at: op.at, ...span(s, op), effects: [{ type: "block", blockId, version, variables }] })
131
+ }
132
+
133
+ function splitClip(s, clip, cut, cutTime) {
134
+ const second = { ...structuredClone(clip), id: newId(s, "c") }
135
+ clip.source.out = cut.out
136
+ second.source.in = cut.in
137
+ const inFirst = (effect) => sourceEdge(s, effect.at, clip.source.asset, "start") < cutTime
138
+ clip.effects = clip.effects.filter((e) => e.type !== "freeze" || inFirst(e))
139
+ second.effects = second.effects.filter((e) => (e.type !== "freeze" || !inFirst(e)) && e.type !== "transition")
140
+ const clips = sequenceOf(s).clips
141
+ clips.splice(clips.indexOf(clip) + 1, 0, second)
142
+ return second
143
+ }
144
+
145
+ function removeFrom(s, op, asset, from, to) {
146
+ const words = s.index.words.get(asset) ?? []
147
+ // Keep word anchors at the words either side, so what's left follows them too
148
+ const outRef = (start) => {
149
+ if (op.from.word === undefined) return op.from
150
+ const word = wordBefore(words, from)
151
+ return word && word.end > start ? { word: word.id, edge: "end" } : null
152
+ }
153
+ const inRef = (end) => {
154
+ if (op.to.word === undefined) return op.to
155
+ const word = wordAfter(words, to)
156
+ return word && word.start < end ? { word: word.id, edge: "start" } : null
157
+ }
158
+ let touched = false
159
+ for (const clip of [...sequenceOf(s).clips]) {
160
+ if (!clip.source || clip.source.asset !== asset || !isBase(s, clip)) continue
161
+ const [start, end] = clipRange(s, clip)
162
+ if (end <= from || start >= to) continue
163
+ writable(s, clip.id)
164
+ touched = true
165
+ const left = start < from ? outRef(start) : null
166
+ const right = end > to ? inRef(end) : null
167
+ if (left !== null && right !== null) splitClip(s, clip, { out: left, in: right }, from)
168
+ else if (left !== null) clip.source.out = left
169
+ else if (right !== null) clip.source.in = right
170
+ else sequenceOf(s).clips.splice(sequenceOf(s).clips.indexOf(clip), 1)
171
+ }
172
+ if (!touched) refuse("range_not_on_timeline", "that range isn't on the timeline, so there is nothing to remove")
173
+ }
174
+
175
+ const OPS = {
176
+ // Selection
177
+ keep_range: {
178
+ fields: { asset: "id", from: "ref", to: "ref", "after?": { nullable: "id" } },
179
+ run(op, s) {
180
+ const asset = assetOf(s, op.asset, SEQUENCED)
181
+ checkRange(s, asset, sourceEdge(s, op.from, asset.id, "start"), sourceEdge(s, op.to, asset.id, "end"))
182
+ const clip = addClip(s, "base", { source: { asset: asset.id, in: op.from, out: op.to } })
183
+ if (op.after === undefined) return
184
+ const clips = sequenceOf(s).clips
185
+ clips.pop()
186
+ clips.splice(op.after === null ? 0 : clips.indexOf(clipOf(s, op.after)) + 1, 0, clip)
187
+ },
188
+ },
189
+ remove_range: {
190
+ fields: { "asset?": "id", from: "ref", to: "ref" },
191
+ run(op, s) {
192
+ const asset = anchorAsset(s, op)
193
+ const from = sourceEdge(s, op.from, asset, "start")
194
+ const to = sourceEdge(s, op.to, asset, "end")
195
+ if (to <= from) refuse("range_inverted", "the range ends before it starts; give the earlier point first")
196
+ removeFrom(s, op, asset, from, to)
197
+ },
198
+ },
199
+ use_take: {
200
+ fields: { clip: "id", "asset?": "id", from: "ref", to: "ref" },
201
+ run(op, s) {
202
+ const clip = writable(s, op.clip)
203
+ if (!clip.source) refuse("no_source", `clip "${op.clip}" has no source to replace`)
204
+ const asset = assetOf(s, anchorAsset(s, op, clip.source.asset), SEQUENCED)
205
+ checkRange(s, asset, sourceEdge(s, op.from, asset.id, "start"), sourceEdge(s, op.to, asset.id, "end"))
206
+ clip.source = { asset: asset.id, in: op.from, out: op.to }
207
+ },
208
+ },
209
+ reorder: {
210
+ fields: { clip: "id", after: { nullable: "id" } },
211
+ run(op, s) {
212
+ const clip = writable(s, op.clip)
213
+ if (!isBase(s, clip)) refuse("not_on_base", "only clips on the main picture have an order; other clips follow their anchors")
214
+ if (op.after === op.clip) refuse("same_clip", "a clip can't go after itself")
215
+ if (op.after !== null && !isBase(s, clipOf(s, op.after))) refuse("not_on_base", `clip "${op.after}" isn't on the main picture`)
216
+ const clips = sequenceOf(s).clips
217
+ clips.splice(clips.indexOf(clip), 1)
218
+ clips.splice(op.after === null ? 0 : clips.indexOf(clipOf(s, op.after)) + 1, 0, clip)
219
+ },
220
+ },
221
+ trim: {
222
+ fields: { clip: "id", "in?": "ref", "out?": "ref", "audioLead?": "flicks", "audioLag?": "flicks" },
223
+ run(op, s) {
224
+ const clip = writable(s, op.clip)
225
+ if (!clip.source) refuse("no_source", `clip "${op.clip}" has no source to trim`)
226
+ const source = { ...clip.source, ...given(op, ["in", "out"]) }
227
+ checkRange(s, assetOf(s, source.asset), ...clipRange(s, { source }))
228
+ clip.source = source
229
+ Object.assign(clip, given(op, ["audioLead", "audioLag"]))
230
+ },
231
+ },
232
+ split: {
233
+ fields: { clip: "id", at: "ref" },
234
+ run(op, s) {
235
+ const clip = writable(s, op.clip)
236
+ if (!clip.source) refuse("no_source", `clip "${op.clip}" has no source to split`)
237
+ const t = sourceEdge(s, op.at, clip.source.asset, "start")
238
+ const [start, end] = clipRange(s, clip)
239
+ if (t <= start || t >= end) refuse("outside_clip", `that point isn't inside clip "${op.clip}"`)
240
+ if (typeof op.at === "number" && wordAt(s.index.words.get(clip.source.asset) ?? [], t)) refuse("cut_inside_word", "that time is inside a word; split at a word's edge instead")
241
+ // Exact: a split is one continuous cut, so neither side takes a pad
242
+ const cut = typeof op.at === "number" ? op.at : { ...op.at, exact: true }
243
+ splitClip(s, clip, { out: cut, in: cut }, t)
244
+ },
245
+ },
246
+ ripple_delete: {
247
+ fields: { clip: "id" },
248
+ run(op, s) {
249
+ const clips = sequenceOf(s).clips
250
+ clips.splice(clips.indexOf(writable(s, op.clip)), 1)
251
+ },
252
+ },
253
+
254
+ // Picture
255
+ punch_in: {
256
+ // x, y (ED11): where to zoom, in thousandths of the framed picture; both or neither. ease "smooth" eases in and out
257
+ fields: { at: "ref", ...SPAN, scale: "num", "ease?": "str", "x?": "int", "y?": "int" },
258
+ run(op, s) {
259
+ known(s, op.at)
260
+ if (op.scale <= 1) refuse("bad_scale", "a punch-in's scale must be above 1")
261
+ if ((op.x === undefined) !== (op.y === undefined) || [op.x, op.y].some((v) => v !== undefined && (v < 0 || v > 1000))) refuse("bad_focus", "a punch-in's focus needs both x and y, each from 0 to 1000")
262
+ if (op.until !== undefined) known(s, op.until)
263
+ addClip(s, "overlay", { at: op.at, ...given(op, ["until", "duration"]), effects: [{ type: "zoom", scale: op.scale, ...given(op, ["ease", "x", "y"]) }] })
264
+ },
265
+ },
266
+ reframe: {
267
+ fields: { clip: "id", mode: { enum: ["subject", "fixed"] }, "x?": "int", "y?": "int" },
268
+ run(op, s) {
269
+ setEffect(writable(s, op.clip), { type: "reframe", mode: op.mode, ...given(op, ["x", "y"]) })
270
+ },
271
+ },
272
+ place_broll: {
273
+ fields: { asset: "id", at: "ref", "in?": "flicks", "out?": "flicks", ...SPAN },
274
+ run(op, s) {
275
+ const asset = assetOf(s, op.asset, PICTURE)
276
+ known(s, op.at)
277
+ const start = op.in ?? 0
278
+ const out = op.out ?? (op.duration === undefined ? asset.facts.duration : start + op.duration)
279
+ if (out === undefined || (op.out === undefined && op.until === undefined && op.duration === undefined)) refuse("needs_duration", "say how long it lasts, with duration, until or out")
280
+ checkRange(s, asset, start, out)
281
+ if (op.until !== undefined) known(s, op.until)
282
+ addClip(s, "broll", { source: { asset: asset.id, in: start, out }, at: op.at, ...given(op, ["until"]) })
283
+ },
284
+ },
285
+ speed: {
286
+ fields: { clip: "id", rate: "rate" },
287
+ run(op, s) {
288
+ writable(s, op.clip).speed = op.rate
289
+ },
290
+ },
291
+ freeze: {
292
+ fields: { clip: "id", at: "ref", duration: "flicks" },
293
+ run(op, s) {
294
+ const clip = writable(s, op.clip)
295
+ if (!clip.source) refuse("no_source", `clip "${op.clip}" has no picture to freeze`)
296
+ const t = sourceEdge(s, op.at, clip.source.asset, "start")
297
+ const [start, end] = clipRange(s, clip)
298
+ if (t < start || t >= end) refuse("outside_clip", `that point isn't inside clip "${op.clip}"`)
299
+ setEffect(clip, { type: "freeze", at: op.at, duration: op.duration })
300
+ },
301
+ },
302
+ transition: {
303
+ fields: { clip: "id", kind: "str", duration: "flicks" },
304
+ run(op, s) {
305
+ setEffect(writable(s, op.clip), { type: "transition", kind: op.kind, duration: op.duration })
306
+ },
307
+ },
308
+ stabilize: {
309
+ fields: { clip: "id", "strength?": "num" },
310
+ run(op, s) {
311
+ setEffect(writable(s, op.clip), { type: "stabilize", ...given(op, ["strength"]) })
312
+ },
313
+ },
314
+ set_camera: {
315
+ fields: { clip: "id", asset: "id", "offset?": "signed" },
316
+ run(op, s) {
317
+ const clip = writable(s, op.clip)
318
+ if (!clip.source) refuse("no_source", `clip "${op.clip}" has no source to switch`)
319
+ const asset = assetOf(s, op.asset, PICTURE)
320
+ const [start, end] = clipRange(s, clip).map((t) => t + (op.offset ?? 0))
321
+ checkRange(s, asset, start, end)
322
+ clip.source = { asset: asset.id, in: start, out: end }
323
+ },
324
+ },
325
+
326
+ // Words on screen
327
+ caption_style: {
328
+ fields: { look: "str", "words?": { list: "id" }, "placement?": EFFECTS.caption["placement?"] },
329
+ run(op, s) {
330
+ if (op.placement) {
331
+ const { x, y, w, h } = op.placement
332
+ const { width, height } = s.project.canvas
333
+ if (x < 0 || y < 0 || x + w > width || y + h > height) refuse("outside_frame", `that placement falls outside the ${width}×${height} frame`)
334
+ }
335
+ if (!op.words?.length) {
336
+ s.project.look.caption = { ...s.project.look.caption, style: op.look }
337
+ return
338
+ }
339
+ for (const word of op.words) known(s, { word })
340
+ addClip(s, "caption", {
341
+ at: { word: op.words[0], edge: "start" },
342
+ until: { word: op.words.at(-1), edge: "end" },
343
+ effects: [{ type: "caption", words: op.words, look: op.look, emphasis: [], ...given(op, ["placement"]) }],
344
+ })
345
+ },
346
+ },
347
+ caption_edit: {
348
+ fields: { caption: "id", text: "str" },
349
+ run(op, s) {
350
+ captionOf(s, op.caption).text = op.text
351
+ },
352
+ },
353
+ caption_emphasis: {
354
+ fields: { caption: "id", word: "id" },
355
+ run(op, s) {
356
+ const caption = captionOf(s, op.caption)
357
+ if (!caption.words.includes(op.word)) refuse("word_not_in_caption", `word "${op.word}" isn't in caption "${op.caption}"`)
358
+ if (!caption.emphasis.includes(op.word)) caption.emphasis.push(op.word)
359
+ },
360
+ },
361
+ add_title: {
362
+ fields: { text: "str", at: "ref", ...SPAN },
363
+ run(op, s) {
364
+ block(s, op, "title", 1, { text: op.text })
365
+ },
366
+ },
367
+ add_lower_third: {
368
+ fields: { text: "str", "subtitle?": "str", at: "ref", ...SPAN },
369
+ run(op, s) {
370
+ block(s, op, "lower-third", 1, { text: op.text, ...given(op, ["subtitle"]) })
371
+ },
372
+ },
373
+ add_motion: {
374
+ fields: { block: "id", version: "pos", variables: "plain", at: "ref", ...SPAN },
375
+ run(op, s) {
376
+ block(s, op, op.block, op.version, op.variables)
377
+ },
378
+ },
379
+ // Text behind the speaker (EDIT_ENGINE §6, ED8): the clip moves under the matte track,
380
+ // and a matte clip spans the same anchors, so it follows its words through a re-cut
381
+ text_behind: {
382
+ fields: { clip: "id" },
383
+ run(op, s) {
384
+ const clip = writable(s, op.clip)
385
+ const kind = clip.effects.find((e) => e.type === "caption" || e.type === "block")
386
+ if (!kind) refuse("not_text", `clip "${op.clip}" isn't a caption, title or card`)
387
+ const matte = ensureTrack(s, "matte")
388
+ const sequence = sequenceOf(s)
389
+ let under = sequence.tracks.find((t) => t.id === "t-behind")
390
+ if (!under) {
391
+ under = { id: "t-behind", role: "overlay", order: matte.order - 1, muted: false, locked: false }
392
+ sequence.tracks.push(under)
393
+ }
394
+ if (clip.track === under.id) refuse("already_behind", `clip "${op.clip}" is already behind the speaker`)
395
+ clip.track = under.id
396
+ addClip(s, "matte", { at: clip.at, ...(clip.until !== undefined ? { until: clip.until } : { duration: clip.duration }), effects: [{ type: "matte", model: "face-box" }] })
397
+ },
398
+ },
399
+
400
+ // Sound
401
+ add_sfx: {
402
+ fields: { asset: "id", peak_at: "ref" },
403
+ run(op, s) {
404
+ const asset = assetOf(s, op.asset, ["audio"])
405
+ known(s, op.peak_at)
406
+ if (asset.facts.duration === undefined) refuse("not_probed", `asset "${op.asset}" hasn't been probed yet`)
407
+ addClip(s, "sfx", { source: { asset: asset.id, in: 0, out: asset.facts.duration }, at: op.peak_at, peak: asset.facts.peak ?? 0 })
408
+ },
409
+ },
410
+ set_music: {
411
+ fields: { asset: "id", "in?": "flicks", "out?": "flicks" },
412
+ run(op, s) {
413
+ const asset = assetOf(s, op.asset, ["audio"])
414
+ const start = op.in ?? 0
415
+ const out = op.out ?? asset.facts.duration
416
+ if (out === undefined) refuse("not_probed", `asset "${op.asset}" hasn't been probed yet`)
417
+ checkRange(s, asset, start, out)
418
+ const track = ensureTrack(s, "music")
419
+ const sequence = sequenceOf(s)
420
+ sequence.clips = sequence.clips.filter((c) => c.track !== track.id)
421
+ addClip(s, "music", { source: { asset: asset.id, in: start, out }, at: 0 })
422
+ },
423
+ },
424
+ music_shape: {
425
+ fields: { "fadeIn?": "flicks", "fadeOut?": "flicks", "fit?": "bool" },
426
+ run(op, s) {
427
+ if (op.fadeIn === undefined && op.fadeOut === undefined && op.fit === undefined) refuse("needs_a_value", "give fadeIn, fadeOut or fit")
428
+ const clip = musicClip(s)
429
+ if (op.fadeIn !== undefined || op.fadeOut !== undefined) setEffect(clip, { type: "fade", ...(op.fadeIn === undefined ? {} : { in: op.fadeIn }), ...(op.fadeOut === undefined ? {} : { out: op.fadeOut }) })
430
+ // fit: the song is edited on its downbeats to the timeline's length (ED9)
431
+ if (op.fit === true) setEffect(clip, { type: "fit" })
432
+ if (op.fit === false) clip.effects = clip.effects.filter((e) => e.type !== "fit")
433
+ },
434
+ },
435
+ duck: {
436
+ fields: { "depth?": "num", "attack?": "flicks", "release?": "flicks" },
437
+ run(op, s) {
438
+ setEffect(musicClip(s), { type: "duck", ...given(op, ["depth", "attack", "release"]) })
439
+ },
440
+ },
441
+ clean_voice: {
442
+ fields: { clip: "id", "strength?": "num" },
443
+ run(op, s) {
444
+ const clip = writable(s, op.clip)
445
+ if (!clip.source || assetOf(s, clip.source.asset).facts.hasAudio === false) refuse("no_audio", `clip "${op.clip}" has no voice to clean`)
446
+ setEffect(clip, { type: "eq", preset: "voice-clean", ...given(op, ["strength"]) })
447
+ },
448
+ },
449
+ set_level: {
450
+ fields: { clip: "id", db: "num" },
451
+ run(op, s) {
452
+ setEffect(writable(s, op.clip), { type: "level", db: op.db })
453
+ },
454
+ },
455
+ room_tone: {
456
+ fields: { asset: "id", from: "ref", to: "ref" },
457
+ run(op, s) {
458
+ const asset = assetOf(s, op.asset, ["video", "audio"])
459
+ checkRange(s, asset, sourceEdge(s, op.from, asset.id, "start"), sourceEdge(s, op.to, asset.id, "end"))
460
+ addClip(s, "ambience", { source: { asset: asset.id, in: op.from, out: op.to }, at: 0, loop: true })
461
+ },
462
+ },
463
+
464
+ // Colour
465
+ grade: {
466
+ fields: { clip: "id", "lift?": "num", "gamma?": "num", "gain?": "num", "saturation?": "num" },
467
+ run(op, s) {
468
+ const values = given(op, ["lift", "gamma", "gain", "saturation"])
469
+ if (Object.keys(values).length === 0) refuse("needs_a_value", "give at least one of lift, gamma, gain or saturation")
470
+ setEffect(writable(s, op.clip), { type: "grade", ...values })
471
+ },
472
+ },
473
+ match_color: {
474
+ fields: { clip: "id", reference: "id" },
475
+ run(op, s) {
476
+ clipOf(s, op.reference)
477
+ setEffect(writable(s, op.clip), { type: "grade", match: op.reference })
478
+ },
479
+ },
480
+ apply_lut: {
481
+ fields: { clip: "id", asset: "id" },
482
+ run(op, s) {
483
+ setEffect(writable(s, op.clip), { type: "lut", asset: assetOf(s, op.asset, ["lut"]).id })
484
+ },
485
+ },
486
+ fix_exposure: {
487
+ fields: { clip: "id", ev: "num" },
488
+ run(op, s) {
489
+ setEffect(writable(s, op.clip), { type: "grade", ev: op.ev })
490
+ },
491
+ },
492
+
493
+ // Project
494
+ set_format: {
495
+ fields: { "width?": "pos", "height?": "pos", "fps?": "rate" },
496
+ run(op, s) {
497
+ const change = given(op, ["width", "height", "fps"])
498
+ const odd = [op.width, op.height].some((side) => side !== undefined && side % 2 !== 0)
499
+ if (Object.keys(change).length === 0 || odd) refuse("bad_format", "give width, height or fps; width and height must be even, as video encoders need")
500
+ Object.assign(s.project.canvas, change)
501
+ },
502
+ },
503
+ add_deliverable: {
504
+ fields: {
505
+ preset: "str",
506
+ size: { width: "pos", height: "pos" },
507
+ fps: "rate",
508
+ codec: "str",
509
+ audio: "str",
510
+ loudness: { lufs: "num", tp: "num" },
511
+ burnCaptions: "bool",
512
+ "gates?": { list: "str" },
513
+ },
514
+ run({ op: _, gates = [], ...fields }, s) {
515
+ s.project.deliverables.push({ id: newId(s, "d"), ...fields, gates })
516
+ },
517
+ },
518
+ set_look: {
519
+ fields: { "caption?": "plain", "color?": "plain", "motion?": "plain", "sound?": "plain", "brand?": "plain" },
520
+ run({ op: _, ...parts }, s) {
521
+ for (const [key, part] of Object.entries(parts)) s.project.look[key] = { ...s.project.look[key], ...part }
522
+ },
523
+ },
524
+ mark: {
525
+ fields: { name: "str", at: "ref" },
526
+ run(op, s) {
527
+ known(s, op.at)
528
+ sequenceOf(s).markers.push({ id: newId(s, "k"), name: op.name, at: op.at })
529
+ },
530
+ },
531
+ // Names the history entry (history.js): the project itself doesn't change
532
+ name_version: { fields: { name: "name" }, run() {} },
533
+ }
534
+
535
+ // Not an edit: the Host records what was brought in, so a replay sees the same assets.
536
+ // An agent or a person can't name a file through an operation (ES-01).
537
+ const CODE_OPS = {
538
+ add_asset: {
539
+ fields: Object.fromEntries(Object.entries(SHAPES.project.assets.list)),
540
+ run({ op: _, ...asset }, s) {
541
+ if (s.author !== "code") refuse("code_only", "assets are added by the Host, when a file is brought in")
542
+ if (s.project.assets.some((a) => a.id === asset.id)) refuse("duplicate_asset", `there is already an asset "${asset.id}"`)
543
+ s.project.assets.push(asset)
544
+ },
545
+ },
546
+ // The brief (DIRECTOR_AND_AGENT §3): what the edit is for, so QG-12 can check it (ED7).
547
+ // A setting, not one of the 37 edits; history records it like any other entry
548
+ set_brief: {
549
+ fields: SHAPES.project["brief?"],
550
+ run({ op: _, ...brief }, s) {
551
+ if (brief.targetDuration <= 0) refuse("bad_brief", "a brief's target length must be above zero")
552
+ s.project.brief = brief
553
+ },
554
+ },
555
+ }
556
+
557
+ const ALL_OPS = { ...OPS, ...CODE_OPS }
558
+
559
+ function captionOf(s, id) {
560
+ const caption = writable(s, id).effects.find((e) => e.type === "caption")
561
+ if (!caption) refuse("not_a_caption", `clip "${id}" isn't a caption`)
562
+ return caption
563
+ }
564
+
565
+ export const OPERATIONS = Object.keys(OPS)
566
+
567
+ /** Shape only, with no project: what reading the log back can check. */
568
+ export function opShapeErrors(op) {
569
+ if (op === null || typeof op !== "object" || !Object.hasOwn(ALL_OPS, op.op)) return [{ code: "unknown_op", message: `unknown operation; use one of ${OPERATIONS.join(", ")}` }]
570
+ const { op: name, ...fields } = op
571
+ const errors = []
572
+ check(fields, ALL_OPS[name].fields, "", errors)
573
+ return errors.map(({ path, reason }) => ({ code: "bad_field", message: `${name}.${path} ${reason}` }))
574
+ }
575
+
576
+ /**
577
+ * Applies operations as one unit. Pure: the input is never changed, and on any
578
+ * refusal the same project comes back with the reason.
579
+ * @param {object} project
580
+ * @param {object[]} ops
581
+ * @param {{ index?: object, author?: "person" | "agent" | "code" }} [ctx]
582
+ * @returns {{ project: object, applied: boolean, diagnostics: { code: string, message: string, op: number }[] }}
583
+ */
584
+ export function apply(project, ops, { index, author = "agent" } = {}) {
585
+ if (!Array.isArray(ops)) return { project, applied: false, diagnostics: [{ code: "unknown_op", message: "operations must be a list", op: 0 }] }
586
+ const s = { project: structuredClone(project), index: openIndex(index), author }
587
+ for (const [i, op] of ops.entries()) {
588
+ const shape = opShapeErrors(op)
589
+ if (shape.length) return { project, applied: false, diagnostics: shape.map((d) => ({ ...d, op: i })) }
590
+ try {
591
+ ALL_OPS[op.op].run(op, s)
592
+ } catch (error) {
593
+ if (!(error instanceof Refused)) throw error
594
+ return { project, applied: false, diagnostics: [{ code: error.code, message: error.message, op: i }] }
595
+ }
596
+ }
597
+ const errors = validateProject(s.project)
598
+ if (errors.length) return { project, applied: false, diagnostics: errors.map(({ path, reason }) => ({ code: "invalid_result", message: `${path} ${reason}`, op: ops.length - 1 })) }
599
+ return { project: s.project, applied: true, diagnostics: [] }
600
+ }
601
+
602
+ export const validateOps = (project, ops, ctx) => apply(project, ops, ctx).diagnostics