@tora-editor/tora 0.2.0 → 0.3.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.
- package/README.md +5 -6
- package/bin/tora.mjs +255 -29
- package/crates/packer/trusted/native-3d-meta.json +3 -3
- package/crates/packer/trusted/native-3d.js +180 -180
- package/crates/packer/trusted/native-meta.json +3 -3
- package/crates/packer/trusted/native.js +5 -5
- package/crates/packer/trusted/release-manifest.json +17 -17
- package/crates/packer/trusted/review-runtime-meta.json +3 -3
- package/crates/packer/trusted/review-runtime.js +182 -182
- package/docs/MIGRATION_0_3.md +39 -0
- package/docs/NATIVE_AGENT_TOOLS.md +12 -363
- package/docs/TORA_AGENT_SETUP.md +24 -106
- package/docs/TORA_CREATION_API.md +63 -365
- package/docs/reference/CORE_AUTHORING.md +8 -2
- package/docs/reference/PLAYABLE_STARTERS.md +1 -1
- package/docs/reference/packs/3d.md +2 -0
- package/package.json +6 -6
- package/packages/creative-project/project-v21.schema.json +108 -1
- package/scripts/lean/editor/entry.js +11 -6
- package/scripts/lean/editor/project-launcher.js +116 -0
- package/scripts/lean/editor/quick-edit-panel.js +479 -0
- package/scripts/lean/editor/review-editor.css +246 -68
- package/scripts/lean/editor/review-editor.js +1731 -1099
- package/scripts/lean/editor-state.mjs +6 -0
- package/scripts/lean/editor.mjs +6 -3
- package/scripts/lean/matrix.mjs +5 -0
- package/scripts/lean/native/agent-tools.mjs +35 -58
- package/scripts/lean/native/api.mjs +148 -134
- package/scripts/lean/native/audio-assets.mjs +30 -0
- package/scripts/lean/native/browser-pool.mjs +34 -0
- package/scripts/lean/native/build-cache.mjs +36 -5
- package/scripts/lean/native/contracts.mjs +8 -6
- package/scripts/lean/native/creative-qa.mjs +66 -12
- package/scripts/lean/native/demos.mjs +11 -20
- package/scripts/lean/native/document.mjs +14 -2
- package/scripts/lean/native/local-bundle.mjs +4 -2
- package/scripts/lean/native/mcp-server.mjs +16 -35
- package/scripts/lean/native/modules/harvest-restore.mjs +3 -284
- package/scripts/lean/native/modules/sort-puzzle.mjs +51 -0
- package/scripts/lean/native/modules/star-salvage.mjs +3 -368
- package/scripts/lean/native/project-bundle.mjs +3 -2
- package/scripts/lean/native/project-index.mjs +0 -2
- package/scripts/lean/native/qa-adapters.mjs +10 -0
- package/scripts/lean/native/quick-edit.mjs +328 -0
- package/scripts/lean/native/review.mjs +203 -572
- package/scripts/lean/native/schema-vocabulary.mjs +10 -0
- package/scripts/lean/native/schema.mjs +1 -1
- package/scripts/lean/native/scope.mjs +45 -0
- package/scripts/lean/native/service.mjs +242 -70
- package/scripts/lean/native/starters.mjs +135 -8
- package/scripts/lean/native/variant-adapters.mjs +8 -0
- package/scripts/lean/native/variants-core.mjs +186 -0
- package/scripts/lean/native/variants.mjs +63 -0
- package/scripts/lean/native/win-cues.mjs +1 -1
- package/skills/tora-playables/SKILL.md +1 -1
- package/scripts/lean/editor/review-intake.js +0 -837
- package/scripts/lean/model-client.mjs +0 -119
- package/scripts/lean/native/author-expansion-proofs.mjs +0 -143
- package/scripts/lean/native/author-fps.mjs +0 -545
- package/scripts/lean/native/author-games.mjs +0 -160
- package/scripts/lean/native/author-harvest.mjs +0 -74
- package/scripts/lean/native/author-primitive-proofs.mjs +0 -875
- package/scripts/lean/native/author-salvage.mjs +0 -71
- package/scripts/lean/native/chat-compose.mjs +0 -287
- package/scripts/lean/native/check-harvest-outcome-audio.mjs +0 -190
- package/scripts/lean/native/check-salvage.mjs +0 -200
- package/scripts/lean/native/compatibility.mjs +0 -34
- package/scripts/lean/native/concept.mjs +0 -633
- package/scripts/lean/native/draw-stroke.mjs +0 -26
- package/scripts/lean/native/erase-mask.mjs +0 -18
- package/scripts/lean/native/glb-loader.mjs +0 -468
- package/scripts/lean/native/harvest-recipe.mjs +0 -21
- package/scripts/lean/native/instance-steering.mjs +0 -24
- package/scripts/lean/native/intake-creator-agent.mjs +0 -98
- package/scripts/lean/native/intake-creator-tools.mjs +0 -218
- package/scripts/lean/native/interaction-gestures.mjs +0 -45
- package/scripts/lean/native/match-board.mjs +0 -138
- package/scripts/lean/native/modules/index.mjs +0 -54
- package/scripts/lean/native/modules/tap-collect.mjs +0 -54
- package/scripts/lean/native/path-network.mjs +0 -132
- package/scripts/lean/native/physics-2d.mjs +0 -154
- package/scripts/lean/native/physics-3d.mjs +0 -259
- package/scripts/lean/native/prepare-audio-collections.mjs +0 -112
- package/scripts/lean/native/prepare-fps-assets.mjs +0 -330
- package/scripts/lean/native/prepare-game-audio.mjs +0 -372
- package/scripts/lean/native/prepare-golf-3d-assets.py +0 -177
- package/scripts/lean/native/prepare-golf-assets.py +0 -211
- package/scripts/lean/native/prepare-harvest-audio.mjs +0 -83
- package/scripts/lean/native/prepare-harvest-images.mjs +0 -31
- package/scripts/lean/native/prepare-primitive-images.py +0 -25
- package/scripts/lean/native/prepare-space-salvage-images.mjs +0 -89
- package/scripts/lean/native/prepare-traffic-assets.py +0 -329
- package/scripts/lean/native/presentation.mjs +0 -335
- package/scripts/lean/native/review-agent-tools.mjs +0 -561
- package/scripts/lean/native/review-connection.mjs +0 -303
- package/scripts/lean/native/review-fields.mjs +0 -24
- package/scripts/lean/native/review-intake.mjs +0 -385
- package/scripts/lean/native/review-intents.mjs +0 -217
- package/scripts/lean/native/review-provider.mjs +0 -580
- package/scripts/lean/native/review-receipts.mjs +0 -76
- package/scripts/lean/native/review-resolver.mjs +0 -429
- package/scripts/lean/native/review-targets.mjs +0 -35
- package/scripts/lean/native/runtime.mjs +0 -1809
- package/scripts/lean/native/salvage-recipe.mjs +0 -20
- package/scripts/lean/native/stage-3d.mjs +0 -1121
- package/scripts/lean/native/verify.mjs +0 -4
- package/scripts/lean/scene-document.mjs +0 -48
|
@@ -1,658 +1,289 @@
|
|
|
1
|
-
import {randomUUID} from 'node:crypto';
|
|
2
1
|
import {isDeepStrictEqual} from 'node:util';
|
|
3
2
|
import {Buffer} from 'node:buffer';
|
|
4
3
|
import {resolve} from 'node:path';
|
|
5
|
-
import {check,exact} from './document.mjs';
|
|
4
|
+
import {check,exact,identifier,references} from './document.mjs';
|
|
6
5
|
import {lowerAssets} from './assets.mjs';
|
|
7
6
|
import {exportProjectFile} from './local-bundle.mjs';
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import {feedbackModel,paidFeedbackModel} from './review-resolver.mjs';
|
|
11
|
-
import {ProjectIndexCache} from './project-index.mjs';
|
|
12
|
-
import {canTransferOutcomeCue,compileDeterministicIntent,deterministicIntents,eligibleOutcomeCueChoices,interpretationOptions} from './review-intents.mjs';
|
|
13
|
-
import {loadCatalog} from './catalog-cache.mjs';
|
|
14
|
-
import {applyReviewReceiptEvent,createReviewReceipt} from './review-receipts.mjs';
|
|
7
|
+
import {requireCreativeQa} from './service.mjs';
|
|
8
|
+
import {assertNativeScope,changedNativeRecords,nativeCollections} from './scope.mjs';
|
|
15
9
|
import {projectWinCueChoices} from './win-cues.mjs';
|
|
10
|
+
import {summarizeQuickEdit} from './quick-edit.mjs';
|
|
16
11
|
|
|
17
|
-
const
|
|
18
|
-
const
|
|
12
|
+
export const REVIEW_JOURNAL_FORMAT='tora-review-journal-2';
|
|
13
|
+
const uuid=value=>typeof value==='string'&&/^[a-f0-9]{8}(?:-[a-f0-9]{4}){3}-[a-f0-9]{12}$/.test(value);
|
|
14
|
+
const identity=current=>({revisionId:current.revisionId,contentHash:current.contentHash});
|
|
15
|
+
const empty=()=>({format:REVIEW_JOURNAL_FORMAT,requests:[]});
|
|
19
16
|
const withoutFrame=({frame,...note})=>({...note,...(frame?{hasFrame:true}:{})});
|
|
20
|
-
|
|
21
|
-
function find(state,requestId)
|
|
17
|
+
|
|
18
|
+
function find(state,requestId){
|
|
22
19
|
check(uuid(requestId),'E_REVIEW_ARGUMENT');
|
|
23
20
|
const request=state.requests.find(item=>item.id===requestId);
|
|
24
21
|
check(request,'E_REVIEW_NOT_FOUND');
|
|
25
22
|
return request;
|
|
26
23
|
}
|
|
27
|
-
function publicRequest(request)
|
|
28
|
-
const
|
|
29
|
-
delete
|
|
30
|
-
delete
|
|
31
|
-
delete
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
delete details.agentProgress;
|
|
35
|
-
if(details.notes)details.notes=details.notes.map(withoutFrame);
|
|
36
|
-
return details;
|
|
24
|
+
function publicRequest(request){
|
|
25
|
+
const result={...request};
|
|
26
|
+
delete result.operations;
|
|
27
|
+
delete result.previewDigest;
|
|
28
|
+
delete result.proposals;
|
|
29
|
+
result.notes=request.notes.map(withoutFrame);
|
|
30
|
+
return result;
|
|
37
31
|
}
|
|
38
|
-
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
});
|
|
32
|
+
function legacyRequest(request){
|
|
33
|
+
const result={...request,readOnly:true};
|
|
34
|
+
delete result.operations;
|
|
35
|
+
delete result.previewDigest;
|
|
36
|
+
delete result.proposals;
|
|
37
|
+
delete result.modelRun;
|
|
38
|
+
delete result.agentProgress;
|
|
39
|
+
if(result.notes)result.notes=result.notes.map(withoutFrame);
|
|
40
|
+
return result;
|
|
48
41
|
}
|
|
49
|
-
function
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
:
|
|
42
|
+
function scopeRecords(project){
|
|
43
|
+
const records=nativeCollections.flatMap(collection=>project[collection].map(record=>({
|
|
44
|
+
id:record.id,collection,label:record.label??record.kind??record.id,
|
|
45
|
+
...(record.sceneId?{sceneId:record.sceneId}:{}),...(record.componentId?{componentId:record.componentId}:{}),
|
|
46
|
+
...(record.parentId?{parentId:record.parentId}:{}),...(record.contract?{contract:record.contract}:{}),
|
|
47
|
+
...(record.kind?{kind:record.kind}:{}),
|
|
48
|
+
references:[...new Set(references(record))],
|
|
49
|
+
})));
|
|
50
|
+
return [{id:'$project.base',collection:'base',label:'Project settings',references:[]},...records];
|
|
53
51
|
}
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
let expected=project;
|
|
60
|
-
for(const [id,note] of scoped){
|
|
61
|
-
const result=reviewOperations(expected,note.target,note.fields,changes[id],project);
|
|
62
|
-
for(const [field,value] of Object.entries(changes[id])){
|
|
63
|
-
if(field==='shared-art'){
|
|
64
|
-
const previous=cropReplacements.get(note.target);
|
|
65
|
-
check(previous===value||previous===undefined&&![...cropReplacements.values()].includes(value),'E_REVIEW_CONFLICT');
|
|
66
|
-
cropReplacements.set(note.target,value);
|
|
67
|
-
}else{
|
|
68
|
-
check(!proposed.has(field)||proposed.get(field)===value,'E_REVIEW_CONFLICT');
|
|
69
|
-
proposed.set(field,value);
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
operations.push(...result.operations);
|
|
73
|
-
expected=result.expected;
|
|
74
|
-
}
|
|
75
|
-
return {operations,expected};
|
|
52
|
+
function validateFrame(frame){
|
|
53
|
+
if(frame===undefined)return;
|
|
54
|
+
check(typeof frame==='string'&&frame.length<=250000&&frame.length>=8,'E_REVIEW_FRAME');
|
|
55
|
+
const bytes=Buffer.from(frame,'base64');
|
|
56
|
+
check(bytes.toString('base64')===frame&&bytes[0]===255&&bytes[1]===216&&bytes.at(-2)===255&&bytes.at(-1)===217,'E_REVIEW_FRAME');
|
|
76
57
|
}
|
|
77
58
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
59
|
+
function reviewMoments(project){
|
|
60
|
+
const hasMatchBoard=project.gameplay.some(behavior=>behavior.kind==='match-board')&&project.variables.some(variable=>variable.id==='harvest.phase');
|
|
61
|
+
if(hasMatchBoard){
|
|
62
|
+
return {
|
|
63
|
+
moments:[
|
|
64
|
+
{id:'opening',label:'Opening'},
|
|
65
|
+
{id:'first-harvest',label:'First harvest'},
|
|
66
|
+
{id:'end-card',label:'End card'},
|
|
67
|
+
...(project.gameplay.some(behavior=>behavior.id==='harvest.deadline')?[{id:'timeout',label:'Timeout'}]:[]),
|
|
68
|
+
],
|
|
69
|
+
momentRules:{phaseVariable:'harvest.phase',progressVariable:'harvest.moves',goal:3,progressMoment:'first-harvest'},
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const hasSalvage=project.components.some(component=>component.id==='salvage.ship')&&project.variables.some(variable=>variable.id==='salvage.phase');
|
|
73
|
+
if(hasSalvage){
|
|
74
|
+
return {
|
|
75
|
+
moments:[
|
|
76
|
+
{id:'opening',label:'Opening'},
|
|
77
|
+
{id:'first-catch',label:'First catch'},
|
|
78
|
+
{id:'end-card',label:'End card'},
|
|
79
|
+
...(project.gameplay.some(behavior=>behavior.id==='salvage.deadline')?[{id:'timeout',label:'Timeout'}]:[]),
|
|
80
|
+
],
|
|
81
|
+
momentRules:{phaseVariable:'salvage.phase',progressVariable:'salvage.catches',goal:3,progressMoment:'first-catch'},
|
|
82
|
+
};
|
|
90
83
|
}
|
|
84
|
+
const hasEndCard=project.scenes.some(scene=>scene.role==='end-card');
|
|
85
|
+
const hasTimeout=hasEndCard&&(project.scenes.some(scene=>scene.role==='end-card'&&/fail|loss|timeout/i.test(scene.id))||project.gameplay.some(behavior=>behavior.kind==='timer'&&!behavior.parameters?.repeat));
|
|
86
|
+
const hasPrimaryInput=project.gameplay.some(behavior=>behavior.kind==='aim-launch'||behavior.kind==='aim-launch-3d')||project.events.some(event=>event.kind==='click'&&project.eventBindings.some(binding=>binding.eventId===event.id));
|
|
87
|
+
return {
|
|
88
|
+
moments:[
|
|
89
|
+
{id:'opening',label:'Opening'},
|
|
90
|
+
...(hasPrimaryInput?[{id:'first-input',label:'First input'}]:[]),
|
|
91
|
+
...(hasEndCard?[{id:'end-card',label:'End card'}]:[]),
|
|
92
|
+
...(hasTimeout?[{id:'timeout',label:'Timeout'}]:[]),
|
|
93
|
+
],
|
|
94
|
+
};
|
|
95
|
+
}
|
|
91
96
|
|
|
92
|
-
|
|
93
|
-
|
|
97
|
+
export class NativeReview{
|
|
98
|
+
constructor(service){this.service=service;}
|
|
94
99
|
|
|
95
|
-
async
|
|
96
|
-
|
|
97
|
-
const journal=await this.service.
|
|
98
|
-
const review=journal.
|
|
99
|
-
const
|
|
100
|
-
const
|
|
101
|
-
const
|
|
102
|
-
const outcomeCueChoices=this.deterministic&&canTransferOutcomeCue(this.indexes.get(project,expected.contentHash))?eligibleOutcomeCueChoices(project,await loadCatalog()):[];
|
|
103
|
-
const activeModel=this.connectionInfo?.model??feedbackModel;
|
|
104
|
-
const showPaidModel=Boolean(this.agent)&&(!this.connectionInfo||this.connectionInfo.provider==='orca');
|
|
100
|
+
async journal(id){return this.service.history.readRevisionJournal(id);}
|
|
101
|
+
async read(id){
|
|
102
|
+
const [current,journal]=await Promise.all([this.service.read(id),this.journal(id)]);
|
|
103
|
+
const review=journal.reviewV2??empty();
|
|
104
|
+
const scopes=scopeRecords(current.project);
|
|
105
|
+
const [variants,winCue]=await Promise.all([this.service.listVariants(id),projectWinCueChoices(current.project)]);
|
|
106
|
+
const {moments,momentRules}=reviewMoments(current.project);
|
|
105
107
|
return {
|
|
106
|
-
expected,
|
|
107
|
-
|
|
108
|
-
|
|
108
|
+
format:REVIEW_JOURNAL_FORMAT,expected:identity(current),baseline:identity(current),accepted:current.accepted,
|
|
109
|
+
canUndo:journal.index>0,canRedo:journal.index<journal.history.length-1,
|
|
110
|
+
name:current.project.creativeBrief?.goal??id,storeUrl:current.project.base.storeUrl,scopes,moments,
|
|
111
|
+
...(momentRules?{momentRules}:{}),variants,quickEdit:summarizeQuickEdit(current.project),
|
|
109
112
|
...(winCue?{winCue}:{}),
|
|
110
|
-
requests:review.requests.map(
|
|
111
|
-
|
|
112
|
-
const canResume=request.status==='resolving'&&isDeepStrictEqual(request.expected,expected)&&
|
|
113
|
-
(Object.hasOwn(run,'answer')||((this.configured||this.deterministic&&run.route==='deterministic')&&!(run.until>now)));
|
|
114
|
-
return {...publicRequest(request),...(canResume?{canResume:true}:{})};
|
|
115
|
-
}),
|
|
116
|
-
...(this.configured?{feedbackModel:activeModel}:{}),
|
|
117
|
-
...(showPaidModel?{paidFeedbackModel}:{}),
|
|
118
|
-
...(this.connectionInfo?{connection:this.connectionInfo}:{}),
|
|
119
|
-
...(this.deterministic?{deterministicAuthoring:{intents:['replaceText','setTiming','alignCaption','replaceSharedArtwork',
|
|
120
|
-
...(outcomeCueChoices.length?['transferOutcomeCue']:[])],outcomeCueChoices}}:{}),
|
|
113
|
+
requests:[...(journal.review?.requests??[]).map(legacyRequest),...review.requests.map(publicRequest)],
|
|
114
|
+
handoff:{projectId:id,pendingRequestIds:review.requests.filter(request=>['waiting','claimed','building','candidate'].includes(request.status)).map(request=>request.id)},
|
|
121
115
|
};
|
|
122
116
|
}
|
|
123
|
-
|
|
124
|
-
|
|
117
|
+
async summaries(id){
|
|
118
|
+
const journal=await this.journal(id),review=journal.reviewV2??empty();
|
|
119
|
+
return review.requests.filter(request=>['waiting','claimed','building','candidate','needs-scope','rejected'].includes(request.status)).map(request=>({
|
|
120
|
+
requestId:request.id,status:request.status,baseline:request.baseline,noteCount:request.notes.length,
|
|
121
|
+
...(request.candidate?{candidate:{summary:request.candidate.summary,artifactHash:request.candidate.artifactHash,qaOk:request.candidate.qa?.ok===true}}:{}),
|
|
122
|
+
}));
|
|
123
|
+
}
|
|
124
|
+
async update(id,fn){
|
|
125
125
|
const current=await this.service.read(id);
|
|
126
126
|
return this.service.history.commitRevision(id,current.revisionId,async journal=>{
|
|
127
|
-
journal.
|
|
128
|
-
|
|
127
|
+
journal.reviewV2??=empty();
|
|
128
|
+
check(journal.reviewV2.format===REVIEW_JOURNAL_FORMAT,'E_REVIEW_JOURNAL');
|
|
129
|
+
return {state:journal,value:await fn(journal.reviewV2,current)};
|
|
129
130
|
});
|
|
130
131
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const {expected,requestId,replacesRequestId,paid=false}=input;
|
|
143
|
-
check(typeof paid==='boolean','E_REVIEW_ARGUMENT');
|
|
144
|
-
const batched=Object.hasOwn(input,'notes');
|
|
145
|
-
const submitted=batched?input.notes:[{target:input.target,fields:input.fields,moment:input.moment,note:input.note,frame:input.frame,intent:input.intent}];
|
|
146
|
-
check(uuid(requestId)&&Array.isArray(submitted)&&submitted.length>0&&submitted.length<=8,'E_REVIEW_ARGUMENT');
|
|
147
|
-
check(submitted.length===1||submitted.every(item=>item.intent===undefined),'E_REVIEW_INTENT_BATCH');
|
|
148
|
-
const notes=submitted.map(item=>{
|
|
149
|
-
check(exact(item,['target','fields','moment','note','frame','intent']),'E_REVIEW_ARGUMENT');
|
|
150
|
-
const {target,fields,moment,note,frame,intent}=item;
|
|
151
|
-
check(typeof note==='string'&¬e.trim()&¬e.length<=2000&&Array.isArray(fields),'E_REVIEW_ARGUMENT');
|
|
152
|
-
if(intent!==undefined)check(intent&&typeof intent==='object'&&!Array.isArray(intent)&&JSON.stringify(intent).length<=4096,'E_REVIEW_INTENT');
|
|
153
|
-
if(frame!==undefined){
|
|
154
|
-
check(typeof frame==='string'&&frame.length<=250000&&frame.length>=8,'E_REVIEW_FRAME');
|
|
155
|
-
const bytes=Buffer.from(frame,'base64');
|
|
156
|
-
check(bytes.toString('base64')===frame&&bytes[0]===255&&bytes[1]===216&&bytes.at(-2)===255&&bytes.at(-1)===217,'E_REVIEW_FRAME');
|
|
157
|
-
}
|
|
158
|
-
return {target,fields,moment,note:note.trim(),...(frame?{frame}:{}),...(intent?{intent:globalThis.structuredClone(intent)}:{})};
|
|
132
|
+
async request(id,input){
|
|
133
|
+
check(exact(input,['requestId','baseline','notes','replacesRequestId'])&&uuid(input.requestId)&&Array.isArray(input.notes)&&input.notes.length>=1&&input.notes.length<=8,'E_REVIEW_ARGUMENT');
|
|
134
|
+
const notes=input.notes.map(note=>{
|
|
135
|
+
check(exact(note,['noteId','text','frame','moment','sceneId','scope','allowCreates'])&&uuid(note.noteId)&&typeof note.text==='string'&¬e.text.trim()&¬e.text.length<=2000,'E_REVIEW_ARGUMENT');
|
|
136
|
+
check(Array.isArray(note.scope)&¬e.scope.length>=1&¬e.scope.length<=64&&new Set(note.scope).size===note.scope.length&¬e.scope.every(scope=>scope==='$project.base'||identifier(scope)),'E_REVIEW_SCOPE');
|
|
137
|
+
check(note.allowCreates===undefined||typeof note.allowCreates==='boolean','E_REVIEW_ARGUMENT');
|
|
138
|
+
check(note.moment===undefined||typeof note.moment==='string'&¬e.moment.length<=64,'E_REVIEW_ARGUMENT');
|
|
139
|
+
check(note.sceneId===undefined||identifier(note.sceneId),'E_REVIEW_ARGUMENT');
|
|
140
|
+
validateFrame(note.frame);
|
|
141
|
+
return {noteId:note.noteId,text:note.text.trim(),scope:[...note.scope],allowCreates:note.allowCreates===true,
|
|
142
|
+
...(note.frame?{frame:note.frame}:{}),...(note.moment?{moment:note.moment}:{}),...(note.sceneId?{sceneId:note.sceneId}:{})};
|
|
159
143
|
});
|
|
144
|
+
check(new Set(notes.map(note=>note.noteId)).size===notes.length,'E_REVIEW_ARGUMENT');
|
|
160
145
|
return this.update(id,(state,current)=>{
|
|
161
|
-
const prior=state.requests.find(
|
|
146
|
+
const prior=state.requests.find(request=>request.id===input.requestId);
|
|
162
147
|
if(prior){
|
|
163
|
-
|
|
164
|
-
const saved=prior.notes??[{target,fields,moment,note,...(frame?{frame}:{}),...(intent?{intent}:{})}];
|
|
165
|
-
check(prior.replacesRequestId===replacesRequestId&&Boolean(prior.paid)===paid&&isDeepStrictEqual([prior.expected,saved],[expected,notes]),'E_REVIEW_EXISTS');
|
|
148
|
+
check(isDeepStrictEqual([prior.baseline,prior.notes,prior.replacesRequestId],[input.baseline,notes,input.replacesRequestId]),'E_REVIEW_EXISTS');
|
|
166
149
|
return publicRequest(prior);
|
|
167
150
|
}
|
|
168
|
-
check(isDeepStrictEqual(
|
|
169
|
-
const
|
|
170
|
-
for(const
|
|
171
|
-
check(
|
|
172
|
-
|
|
173
|
-
check(target===null?fields.length===0:selected&&selected.moment===moment&&fields.length>0&&new Set(fields).size===fields.length&&fields.every(key=>selected.fields.some(field=>field.id===key)),'E_REVIEW_SCOPE');
|
|
151
|
+
check(isDeepStrictEqual(input.baseline,identity(current)),'E_NATIVE_STALE');
|
|
152
|
+
const available=new Set(scopeRecords(current.project).map(record=>record.id));
|
|
153
|
+
for(const note of notes){
|
|
154
|
+
check(note.scope.every(scope=>available.has(scope)),'E_REVIEW_SCOPE');
|
|
155
|
+
if(note.sceneId)check(current.project.scenes.some(scene=>scene.id===note.sceneId),'E_REVIEW_ARGUMENT');
|
|
174
156
|
}
|
|
175
|
-
if(replacesRequestId!==undefined){
|
|
176
|
-
const replaced=find(state,replacesRequestId);
|
|
177
|
-
check(isDeepStrictEqual(replaced.
|
|
178
|
-
check(['waiting','claimed','rejected','needs-scope','
|
|
157
|
+
if(input.replacesRequestId!==undefined){
|
|
158
|
+
const replaced=find(state,input.replacesRequestId);
|
|
159
|
+
check(isDeepStrictEqual(replaced.baseline,input.baseline),'E_NATIVE_STALE');
|
|
160
|
+
check(['waiting','claimed','rejected','needs-scope','discarded'].includes(replaced.status),'E_REVIEW_STATUS');
|
|
179
161
|
replaced.status='superseded';
|
|
180
|
-
delete replaced.agentProgress;
|
|
181
162
|
}
|
|
182
|
-
const request={id:requestId,
|
|
183
|
-
|
|
163
|
+
const request={id:input.requestId,baseline:input.baseline,notes,status:'waiting',proposals:[],
|
|
164
|
+
...(input.replacesRequestId?{replacesRequestId:input.replacesRequestId}:{})};
|
|
184
165
|
state.requests.push(request);
|
|
185
166
|
return publicRequest(request);
|
|
186
167
|
});
|
|
187
168
|
}
|
|
188
|
-
|
|
189
|
-
async store(id,{expected,url}) {
|
|
190
|
-
const envelope={format:'tora-transaction-1',expected,operations:[{kind:'setStore',url}]};
|
|
191
|
-
const preview=await this.service.preview(id,envelope);
|
|
192
|
-
return this.service.transact(id,{...envelope,previewDigest:preview.previewDigest});
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
async claim(id,requestId) {
|
|
169
|
+
async claim(id,requestId){
|
|
196
170
|
return this.update(id,(state,current)=>{
|
|
197
171
|
const request=find(state,requestId);
|
|
198
|
-
check(isDeepStrictEqual(request.
|
|
172
|
+
check(isDeepStrictEqual(request.baseline,identity(current)),'E_NATIVE_STALE');
|
|
173
|
+
if(request.status==='candidate')return {...publicRequest(request),authorizedScopes:[...new Set(request.notes.flatMap(note=>note.scope))],allowCreates:request.notes.some(note=>note.allowCreates),notes:request.notes.map(withoutFrame),frames:request.notes.flatMap(note=>note.frame?[{noteId:note.noteId,data:note.frame}]:[])};
|
|
199
174
|
if(request.status==='building'){
|
|
200
|
-
const
|
|
201
|
-
return {...
|
|
175
|
+
const proposal=request.proposals.at(-1);
|
|
176
|
+
return {...publicRequest(request),authorizedScopes:[...new Set(request.notes.flatMap(note=>note.scope))],allowCreates:request.notes.some(note=>note.allowCreates),pendingProposal:{proposalId:proposal.id,operations:proposal.operations,addressedNoteIds:proposal.addressedNoteIds,summary:proposal.summary},frames:request.notes.flatMap(note=>note.frame?[{noteId:note.noteId,data:note.frame}]:[])};
|
|
202
177
|
}
|
|
203
|
-
check(['waiting','claimed','rejected'].includes(request.status),'E_REVIEW_STATUS');
|
|
204
|
-
check(request.proposals.length<2,'E_REVIEW_LIMIT');
|
|
178
|
+
check(['waiting','claimed','rejected','needs-scope'].includes(request.status),'E_REVIEW_STATUS');
|
|
205
179
|
request.status='claimed';
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
const target=targets.find(item=>item.id===note.target);
|
|
209
|
-
return {authorizedFields:target?.fields.filter(field=>note.fields.includes(field.id))??[],...(target?.imageChoices?{imageChoices:target.imageChoices,affectedComponentIds:target.uses}:{})};
|
|
210
|
-
};
|
|
211
|
-
if(request.notes)return {...briefing(request),notes:request.notes.map((note,index)=>({
|
|
212
|
-
id:String(index+1),...withoutFrame(note),...describe(note),
|
|
213
|
-
})),frames:request.notes.flatMap((note,index)=>note.frame?[{noteId:String(index+1),data:note.frame}]:[])};
|
|
214
|
-
return {...briefing(request),frame:request.frame,...describe(request)};
|
|
180
|
+
return {...publicRequest(request),authorizedScopes:[...new Set(request.notes.flatMap(note=>note.scope))],allowCreates:request.notes.some(note=>note.allowCreates),
|
|
181
|
+
frames:request.notes.flatMap(note=>note.frame?[{noteId:note.noteId,data:note.frame}]:[])};
|
|
215
182
|
});
|
|
216
183
|
}
|
|
217
|
-
|
|
218
|
-
async needsScope(id,requestId,reason) {
|
|
184
|
+
async needsScope(id,requestId,reason){
|
|
219
185
|
check(typeof reason==='string'&&reason.trim()&&reason.length<=500,'E_REVIEW_ARGUMENT');
|
|
220
186
|
return this.update(id,(state,current)=>{
|
|
221
187
|
const request=find(state,requestId);
|
|
222
|
-
check(isDeepStrictEqual(request.
|
|
223
|
-
check(
|
|
224
|
-
request.status='needs-scope';
|
|
225
|
-
request.reason=reason.trim();
|
|
226
|
-
request.receipt??=createReviewReceipt(requestId,request.expected);
|
|
227
|
-
applyReviewReceiptEvent(request.receipt,{type:'final',disposition:'needs-scope'});
|
|
188
|
+
check(isDeepStrictEqual(request.baseline,identity(current)),'E_NATIVE_STALE');
|
|
189
|
+
check(['claimed','rejected'].includes(request.status),'E_REVIEW_STATUS');
|
|
190
|
+
request.status='needs-scope';request.reason=reason.trim();
|
|
228
191
|
return publicRequest(request);
|
|
229
192
|
});
|
|
230
193
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
// alongside the working revision, so reconnecting cannot restore a pass.
|
|
194
|
+
async propose(id,input){
|
|
195
|
+
check(exact(input,['requestId','proposalId','operations','addressedNoteIds','summary'])&&uuid(input.proposalId)&&Array.isArray(input.operations)&&input.operations.length>0&&
|
|
196
|
+
Array.isArray(input.addressedNoteIds)&&input.addressedNoteIds.every(uuid)&&typeof input.summary==='string'&&input.summary.trim()&&input.summary.length<=500,'E_REVIEW_ARGUMENT');
|
|
197
|
+
const payload={operations:input.operations,addressedNoteIds:[...input.addressedNoteIds].sort(),summary:input.summary.trim()};
|
|
236
198
|
const pending=await this.update(id,(state,current)=>{
|
|
237
|
-
const request=find(state,requestId);
|
|
238
|
-
const prior=request.proposals.find(item=>item.id===proposalId);
|
|
199
|
+
const request=find(state,input.requestId),prior=request.proposals.find(proposal=>proposal.id===input.proposalId);
|
|
239
200
|
if(prior){
|
|
240
|
-
check(isDeepStrictEqual(prior.
|
|
201
|
+
check(isDeepStrictEqual({operations:prior.operations,addressedNoteIds:[...prior.addressedNoteIds].sort(),summary:prior.summary},payload),'E_REVIEW_EXISTS');
|
|
241
202
|
if(request.status==='building'&&request.proposals.at(-1)===prior){
|
|
242
|
-
check(isDeepStrictEqual(request.
|
|
243
|
-
return {request:
|
|
203
|
+
check(isDeepStrictEqual(request.baseline,identity(current)),'E_NATIVE_STALE');
|
|
204
|
+
return {resume:true,request:globalThis.structuredClone(request),project:current.project};
|
|
244
205
|
}
|
|
245
206
|
return {duplicate:true,request:publicRequest(request)};
|
|
246
207
|
}
|
|
247
|
-
check(isDeepStrictEqual(request.
|
|
248
|
-
check(['claimed','rejected'].includes(request.status)
|
|
249
|
-
check(request.
|
|
250
|
-
|
|
251
|
-
request.
|
|
252
|
-
request.
|
|
253
|
-
applyReviewReceiptEvent(request.receipt,{type:'route',route:'manual-agent'});
|
|
254
|
-
request.proposals.push({id:proposalId,changes,summary:summary.trim()});
|
|
255
|
-
return {request:publicRequest(request),project:current.project};
|
|
208
|
+
check(isDeepStrictEqual(request.baseline,identity(current)),'E_NATIVE_STALE');
|
|
209
|
+
check(['claimed','rejected','needs-scope'].includes(request.status),'E_REVIEW_STATUS');
|
|
210
|
+
check(isDeepStrictEqual([...new Set(input.addressedNoteIds)].sort(),request.notes.map(note=>note.noteId).sort())&&new Set(input.addressedNoteIds).size===input.addressedNoteIds.length,'E_REVIEW_NOTE_COVERAGE');
|
|
211
|
+
request.status='building';delete request.reason;delete request.detail;delete request.creativeQa;
|
|
212
|
+
request.proposals.push({id:input.proposalId,...payload});
|
|
213
|
+
return {request:globalThis.structuredClone(request),project:current.project};
|
|
256
214
|
});
|
|
257
215
|
if(pending.duplicate)return pending.request;
|
|
258
|
-
const {request,project}=pending;
|
|
259
|
-
let outcome;
|
|
260
|
-
try {
|
|
261
|
-
const {operations,expected}=request.notes
|
|
262
|
-
? reviewBatchOperations(project,request.notes,changes)
|
|
263
|
-
: reviewOperations(project,request.target,request.fields,changes);
|
|
264
|
-
outcome=await this.buildCandidate(id,{request,proposalId,project,operations,expected,summary:summary.trim()});
|
|
265
|
-
}catch(e){
|
|
266
|
-
outcome={status:'rejected',reason:/^E_[A-Z_]+$/.test(e.message)?e.message:'E_REVIEW_BUILD',...(e.creativeQa?{creativeQa:e.creativeQa}:{})};
|
|
267
|
-
}
|
|
268
|
-
return this.update(id,state=>{
|
|
269
|
-
const current=find(state,requestId);
|
|
270
|
-
if(current.status==='building'&¤t.proposals.at(-1)?.id===proposalId){
|
|
271
|
-
delete current.reason;
|
|
272
|
-
Object.assign(current,outcome);
|
|
273
|
-
applyReviewReceiptEvent(current.receipt,{type:'final',disposition:outcome.status,candidateId:outcome.status==='candidate'?proposalId:undefined});
|
|
274
|
-
}
|
|
275
|
-
return publicRequest(current);
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
async buildCandidate(id,{request,proposalId,project,operations,expected,summary}){
|
|
280
|
-
let started=Date.now();
|
|
281
|
-
const preview=await this.service.preview(id,{format:'tora-transaction-1',expected:request.expected,operations});
|
|
282
|
-
await this.recordReceipt(id,request.id,{type:'tool',name:'preview',status:'completed',responseBytes:0,durationMs:Date.now()-started});
|
|
283
|
-
check(isDeepStrictEqual(preview.project,expected),'E_REVIEW_SCOPE');
|
|
284
|
-
check(!isDeepStrictEqual(project,expected),'E_REVIEW_NO_CHANGE');
|
|
285
|
-
preview.project.revisionId='rev-'+proposalId;
|
|
286
|
-
started=Date.now();
|
|
287
|
-
const artifact=await this.service.build(id,preview.project,'unity-ads-ua-playable');
|
|
288
|
-
await this.recordReceipt(id,request.id,{type:'compilation',durationMs:Date.now()-started});
|
|
289
|
-
return {status:'candidate',operations,previewDigest:preview.previewDigest,candidate:{bytes:artifact.manifest.bytes,summary}};
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
async buildIntentCandidate({id,request,project,proposalId,intents,onReceipt}){
|
|
293
|
-
const index=this.indexes.get(project,request.expected.contentHash),notes=request.notes??[request];
|
|
294
|
-
const edits=[];
|
|
295
|
-
for(const intent of intents){
|
|
296
|
-
const note=notes[Number(intent.noteId)-1];
|
|
297
|
-
edits.push((await compileDeterministicIntent(project,intent,index,note?.target)).edit);
|
|
298
|
-
}
|
|
299
|
-
const intent=intents.length===1?intents[0]:undefined;
|
|
300
|
-
const toolsSession=createReviewTools({service:this.service,id,request,project,proposalId,intent,typed:true,onReceipt,exercise:this.exercise,qaRunner:this.qaRunner});
|
|
301
|
-
try{
|
|
302
|
-
const preview=await toolsSession.callTool('preview',{edits});
|
|
303
|
-
const {checks}=await toolsSession.callTool('exercise',{previewDigest:preview.previewDigest});
|
|
304
|
-
const completed=new Set(checks.map(item=>item.id));
|
|
305
|
-
return await toolsSession.finalize({previewDigest:preview.previewDigest,noteResults:intents.map(({noteId,requiredChecks})=>{
|
|
306
|
-
check(requiredChecks.every(key=>completed.has(key)),'E_REVIEW_VERIFICATION');
|
|
307
|
-
return {noteId,records:preview.notes[noteId],verification:requiredChecks};
|
|
308
|
-
})});
|
|
309
|
-
}finally{await toolsSession.dispose();}
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
async resolveDeterministic(id,requestId){
|
|
313
|
-
check(this.deterministic,'E_REVIEW_INTENT_DISABLED');
|
|
314
|
-
const runId=randomUUID();
|
|
315
|
-
const start=await this.update(id,(state,current)=>{
|
|
316
|
-
const request=find(state,requestId);
|
|
317
|
-
const intents=deterministicIntents(request);
|
|
318
|
-
if(!intents)return {};
|
|
319
|
-
check(isDeepStrictEqual(request.expected,identity(current)),'E_NATIVE_STALE');
|
|
320
|
-
if(request.status==='candidate')return {request:publicRequest(request)};
|
|
321
|
-
const now=Date.now();
|
|
322
|
-
if(request.status==='resolving'&&request.modelRun?.until>now)return {request:publicRequest(request)};
|
|
323
|
-
check(!state.requests.some(item=>item.id!==requestId&&item.status==='resolving'&&item.modelRun?.until>now),'E_REVIEW_MODEL_BUSY');
|
|
324
|
-
check(['waiting','claimed','needs-scope','rejected','model-error','resolving'].includes(request.status),'E_REVIEW_STATUS');
|
|
325
|
-
const proposalId=request.modelRun?.proposalId??runId;
|
|
326
|
-
request.status='resolving';
|
|
327
|
-
request.modelRun={id:runId,proposalId,route:'deterministic',until:now+90000};
|
|
328
|
-
request.receipt??=createReviewReceipt(requestId,request.expected);
|
|
329
|
-
applyReviewReceiptEvent(request.receipt,{type:'route',route:'deterministic'});
|
|
330
|
-
delete request.reason;
|
|
331
|
-
return {request:publicRequest(request),project:current.project,intents,proposalId};
|
|
332
|
-
});
|
|
333
|
-
const {request,project,intents,proposalId}=start;
|
|
334
|
-
if(!project)return request;
|
|
335
216
|
let outcome;
|
|
336
|
-
try{
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
const reason=/^E_[A-Z0-9_]+$/.test(error.message)?error.message:'E_REVIEW_BUILD';
|
|
341
|
-
outcome={status:reason==='E_NATIVE_STALE'?'model-error':'rejected',reason,...(error.creativeQa?{creativeQa:error.creativeQa}:{})};
|
|
217
|
+
try{outcome=await this.buildCandidate(id,pending.request,input.proposalId,pending.project);}
|
|
218
|
+
catch(error){
|
|
219
|
+
const qa=error.creativeQa?{ok:false,blockers:error.creativeQa.blockers??[],warnings:error.creativeQa.warnings??[],...(error.creativeQa.outputDir?{outputDir:error.creativeQa.outputDir}:{})}:undefined;
|
|
220
|
+
outcome={status:'rejected',reason:/^E_[A-Z0-9_]+$/.test(error.message)?error.message:'E_REVIEW_BUILD',...(error.detail?{detail:error.detail}:{}),...(qa?{creativeQa:qa}:{})};
|
|
342
221
|
}
|
|
343
222
|
return this.update(id,(state,current)=>{
|
|
344
|
-
const
|
|
345
|
-
if(
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
Object.assign(active,result);
|
|
349
|
-
applyReviewReceiptEvent(active.receipt,{type:'final',disposition:result.status,candidateId:result.status==='candidate'?proposalId:undefined});
|
|
223
|
+
const request=find(state,input.requestId);
|
|
224
|
+
if(request.status==='building'&&request.proposals.at(-1)?.id===input.proposalId){
|
|
225
|
+
const result=isDeepStrictEqual(request.baseline,identity(current))?outcome:{status:'rejected',reason:'E_NATIVE_STALE'};
|
|
226
|
+
Object.assign(request,result);
|
|
350
227
|
}
|
|
351
|
-
return publicRequest(
|
|
352
|
-
});
|
|
353
|
-
}
|
|
354
|
-
|
|
355
|
-
async resolve(id,requestId){
|
|
356
|
-
if(this.deterministic){
|
|
357
|
-
const direct=await this.resolveDeterministic(id,requestId);
|
|
358
|
-
if(direct)return direct;
|
|
359
|
-
}
|
|
360
|
-
if(this.agent)return this.resolveAgent(id,requestId);
|
|
361
|
-
let runId=randomUUID();
|
|
362
|
-
const {request,project,savedAnswer}=await this.update(id,(state,current)=>{
|
|
363
|
-
const request=find(state,requestId);
|
|
364
|
-
check(isDeepStrictEqual(request.expected,identity(current)),'E_NATIVE_STALE');
|
|
365
|
-
if(request.status==='candidate')return {request:publicRequest(request)};
|
|
366
|
-
const now=Date.now();
|
|
367
|
-
if(request.status==='resolving'){
|
|
368
|
-
if(Object.hasOwn(request.modelRun??{},'answer')){
|
|
369
|
-
runId=request.modelRun.id;
|
|
370
|
-
return {request:publicRequest(request),project:current.project,savedAnswer:request.modelRun.answer};
|
|
371
|
-
}
|
|
372
|
-
if(request.modelRun?.until>now)return {request:publicRequest(request)};
|
|
373
|
-
}
|
|
374
|
-
check(this.resolver,'E_REVIEW_MODEL_NOT_CONFIGURED');
|
|
375
|
-
check(['waiting','claimed','needs-scope','rejected','model-error','resolving'].includes(request.status),'E_REVIEW_STATUS');
|
|
376
|
-
check(!state.requests.some(item=>item.id!==requestId&&item.status==='resolving'&&item.modelRun?.until>now),'E_REVIEW_MODEL_BUSY');
|
|
377
|
-
request.status='resolving';
|
|
378
|
-
request.modelRun={id:runId,until:now+90000};
|
|
379
|
-
request.receipt??=createReviewReceipt(requestId,request.expected);
|
|
380
|
-
applyReviewReceiptEvent(request.receipt,{type:'route',route:'field-resolver'});
|
|
381
|
-
delete request.reason;
|
|
382
|
-
delete request.retryAfterSeconds;
|
|
383
|
-
delete request.retryAt;
|
|
384
|
-
return {request:publicRequest(request),project:current.project};
|
|
385
|
-
});
|
|
386
|
-
if(!project)return request;
|
|
387
|
-
|
|
388
|
-
let outcome;
|
|
389
|
-
try{
|
|
390
|
-
const {targets}=reviewTargets(project);
|
|
391
|
-
const notes=(request.notes??[request]).map(({target,fields,moment,note},index)=>{
|
|
392
|
-
const allowed=targets.filter(option=>target===null?(moment==='live'||option.moment===moment):option.id===target)
|
|
393
|
-
.map(({id,label,moment:targetMoment,fields:options,imageChoices})=>({id,label,moment:targetMoment,
|
|
394
|
-
fields:target===null?options.filter(field=>!field.confirm):options.filter(field=>fields.includes(field.id)),
|
|
395
|
-
...(target!==null&&imageChoices?{imageChoices}:{}),
|
|
396
|
-
})).filter(option=>option.fields.length);
|
|
397
|
-
return {id:String(index+1),note,moment,pinned:target!==null,allowed};
|
|
398
|
-
});
|
|
399
|
-
let answer=savedAnswer;
|
|
400
|
-
if(answer===undefined){
|
|
401
|
-
const started=Date.now();
|
|
402
|
-
await this.recordReceipt(id,requestId,{type:'provider-start',model:feedbackModel});
|
|
403
|
-
try{
|
|
404
|
-
answer=await this.resolver({project:project.creativeBrief.goal,notes});
|
|
405
|
-
await this.recordReceipt(id,requestId,{type:'provider-success',durationMs:Date.now()-started,usage:null});
|
|
406
|
-
}catch(error){
|
|
407
|
-
await this.recordReceipt(id,requestId,{type:'provider-error',durationMs:Date.now()-started,code:error.message,retryAfterSeconds:error.retryAfterSeconds});
|
|
408
|
-
throw error;
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
if(exact(answer,['reason'])&&typeof answer.reason==='string'&&answer.reason.trim()&&answer.reason.length<=500){
|
|
412
|
-
outcome={status:'needs-scope',reason:answer.reason.trim()};
|
|
413
|
-
}else{
|
|
414
|
-
check(validProposal(answer),'E_REVIEW_MODEL_RESPONSE');
|
|
415
|
-
// Journal the answer with the pass before building. A reconnect can then
|
|
416
|
-
// retry the same deterministic build without another provider call.
|
|
417
|
-
if(savedAnswer===undefined)await this.update(id,(state,current)=>{
|
|
418
|
-
const active=find(state,requestId);
|
|
419
|
-
check(active.status==='resolving'&&active.modelRun?.id===runId,'E_REVIEW_STATUS');
|
|
420
|
-
check(isDeepStrictEqual(request.expected,identity(current)),'E_NATIVE_STALE');
|
|
421
|
-
state.passesUsed++;
|
|
422
|
-
active.modelRun.answer=answer;
|
|
423
|
-
});
|
|
424
|
-
check(exact(answer.edits,notes.map(note=>note.id))&&Object.keys(answer.edits).length===notes.length,'E_REVIEW_SCOPE');
|
|
425
|
-
const resolved=[];
|
|
426
|
-
const changes={};
|
|
427
|
-
const changedMoments=new Set();
|
|
428
|
-
for(const note of notes){
|
|
429
|
-
const value=answer.edits[note.id];
|
|
430
|
-
check(!note.pinned||!Array.isArray(value),'E_REVIEW_SCOPE');
|
|
431
|
-
for(const edit of editList(value)){
|
|
432
|
-
const allowed=note.allowed.find(target=>target.id===edit.target);
|
|
433
|
-
check(allowed,'E_REVIEW_SCOPE');
|
|
434
|
-
resolved.push({target:edit.target,fields:allowed.fields.map(field=>field.id)});
|
|
435
|
-
changes[String(resolved.length)]=edit.changes;
|
|
436
|
-
for(const field of Object.keys(edit.changes))changedMoments.add(field==='timeout-caption'?'timeout':allowed.moment);
|
|
437
|
-
}
|
|
438
|
-
}
|
|
439
|
-
const {operations,expected}=reviewBatchOperations(project,resolved,changes);
|
|
440
|
-
outcome=await this.buildCandidate(id,{request,proposalId:runId,project,operations,expected,summary:answer.summary.trim()});
|
|
441
|
-
outcome.candidate.moments=[...changedMoments];
|
|
442
|
-
}
|
|
443
|
-
}catch(error){
|
|
444
|
-
const code=/^E_[A-Z0-9_]+$/.test(error.message)?error.message:'E_REVIEW_MODEL_RESPONSE';
|
|
445
|
-
const retryAfterSeconds=error.retryAfterSeconds;
|
|
446
|
-
outcome={status:'model-error',reason:code,...(error.creativeQa?{creativeQa:error.creativeQa}:{})};
|
|
447
|
-
if(Number.isSafeInteger(retryAfterSeconds)&&retryAfterSeconds>=0){
|
|
448
|
-
outcome.retryAfterSeconds=retryAfterSeconds;
|
|
449
|
-
const retryAt=Date.now()+retryAfterSeconds*1000;
|
|
450
|
-
if(Number.isFinite(new Date(retryAt).getTime()))outcome.retryAt=retryAt;
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
return this.update(id,(state,current)=>{
|
|
455
|
-
const active=find(state,requestId);
|
|
456
|
-
if(active.status==='resolving'&&active.modelRun?.id===runId){
|
|
457
|
-
const charged=Object.hasOwn(active.modelRun,'answer');
|
|
458
|
-
delete active.modelRun;
|
|
459
|
-
if(outcome.status==='model-error'&&charged)state.passesUsed=Math.max(0,state.passesUsed-1);
|
|
460
|
-
const result=isDeepStrictEqual(request.expected,identity(current))?outcome:{status:'model-error',reason:'E_NATIVE_STALE'};
|
|
461
|
-
Object.assign(active,result);
|
|
462
|
-
if(result.status==='needs-clarification')applyReviewReceiptEvent(active.receipt,{type:'clarification'});
|
|
463
|
-
applyReviewReceiptEvent(active.receipt,{type:'final',disposition:result.status,candidateId:result.status==='candidate'?runId:undefined});
|
|
464
|
-
}
|
|
465
|
-
return publicRequest(active);
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
async answer(id,requestId,answer) {
|
|
470
|
-
check(typeof answer==='string'&&answer.trim()&&answer.length<=2000,'E_REVIEW_ARGUMENT');
|
|
471
|
-
await this.update(id,(state,current)=>{
|
|
472
|
-
const request=find(state,requestId);
|
|
473
|
-
check(isDeepStrictEqual(request.expected,identity(current)),'E_NATIVE_STALE');
|
|
474
|
-
check(request.status==='needs-clarification','E_REVIEW_STATUS');
|
|
475
|
-
check(this.configured,'E_REVIEW_MODEL_NOT_CONFIGURED');
|
|
476
|
-
request.clarification={question:request.question,answer:answer.trim()};
|
|
477
|
-
delete request.question;
|
|
478
|
-
request.status='waiting';
|
|
228
|
+
return publicRequest(request);
|
|
479
229
|
});
|
|
480
|
-
return this.resolve(id,requestId);
|
|
481
230
|
}
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
const
|
|
485
|
-
|
|
486
|
-
const
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
item.expected.contentHash===request.expected.contentHash||item.revision?.contentHash===request.expected.contentHash
|
|
504
|
-
)&&['promoted','discarded'].includes(item.status)).slice(-2).map(item=>({
|
|
505
|
-
notes:(item.notes??[item]).slice(0,3).map(note=>note.note.slice(0,500)),
|
|
506
|
-
status:item.status,changes:item.candidate?.summary,
|
|
507
|
-
}));
|
|
508
|
-
return {request:publicRequest(request),project:current.project,progress:request.agentProgress,proposalId,history};
|
|
509
|
-
});
|
|
510
|
-
const {request,project,progress,proposalId,history}=start;
|
|
511
|
-
if(!project)return request;
|
|
512
|
-
const checkpoint=async next=>this.update(id,(state,current)=>{
|
|
513
|
-
const active=find(state,requestId);
|
|
514
|
-
check(active.status==='resolving'&&active.modelRun?.id===runId,'E_REVIEW_STATUS');
|
|
515
|
-
check(isDeepStrictEqual(request.expected,identity(current)),'E_NATIVE_STALE');
|
|
516
|
-
active.agentProgress=next;
|
|
517
|
-
active.modelRun.until=Date.now()+lease;
|
|
518
|
-
});
|
|
519
|
-
const notes=(request.notes??[request]).map(({target,fields,moment,note},index)=>({
|
|
520
|
-
id:String(index+1),note,target,fields,moment,pinned:target!==null,
|
|
521
|
-
}));
|
|
522
|
-
let outcome;
|
|
523
|
-
const toolsSession=createReviewTools({service:this.service,id,request,project,notes,proposalId,progress,exercise:this.exercise,qaRunner:this.qaRunner,onReceipt:event=>this.recordReceipt(id,requestId,event,runId)});
|
|
524
|
-
try {
|
|
525
|
-
let interpreterCalls=request.receipt?.providerCalls.filter(call=>call.phase==='interpretation').length??0;
|
|
526
|
-
const onReceipt=event=>{
|
|
527
|
-
if(event.type==='provider-start'&&event.phase==='interpretation')interpreterCalls++;
|
|
528
|
-
return this.recordReceipt(id,requestId,event,runId);
|
|
529
|
-
};
|
|
530
|
-
const {tools,callTool,finalize}=toolsSession;
|
|
531
|
-
const pinnedTargets=reviewTargets(project).targets.filter(target=>notes.some(note=>note.pinned&¬e.target===target.id));
|
|
532
|
-
const frames=(request.notes??[request]).filter(note=>typeof note.frame==='string'&¬e.frame).slice(0,4).map(note=>({mimeType:'image/jpeg',data:note.frame}));
|
|
533
|
-
const input={project:project.creativeBrief.goal,notes,history,pinnedTargets,
|
|
534
|
-
...(frames.length?{screenshots:frames}:{}),
|
|
535
|
-
...(!progress&&request.clarification?{clarification:request.clarification}:{})};
|
|
536
|
-
const previousInterpretation=request.receipt?.providerCalls.some(call=>call.phase==='interpretation'&&call.status==='completed');
|
|
537
|
-
const options=this.interpreter&&!progress&&!request.clarification&&!previousInterpretation
|
|
538
|
-
?await interpretationOptions(request,this.indexes.get(project,request.expected.contentHash),project):[];
|
|
539
|
-
if(options.length){
|
|
540
|
-
await onReceipt({type:'route',route:'interpretation'});
|
|
541
|
-
let interpreted;
|
|
542
|
-
try{
|
|
543
|
-
interpreted=await this.interpreter({input,options,onReceipt,paid:request.paid===true});
|
|
544
|
-
}catch(error){
|
|
545
|
-
if(!/^E_(REVIEW_INTENT|REVIEW_MODEL_RESPONSE|MODEL_(INCOMPLETE|JSON|INPUT_LIMIT))$/.test(error.message))throw error;
|
|
546
|
-
interpreted={reason:'The intent could not be validated.'};
|
|
547
|
-
}
|
|
548
|
-
if(interpreted.question){
|
|
549
|
-
outcome=await finalize({question:interpreted.question});
|
|
550
|
-
}else if(interpreted.intent){
|
|
551
|
-
try{
|
|
552
|
-
outcome=await this.buildIntentCandidate({id,request,project,proposalId,intents:[interpreted.intent],onReceipt});
|
|
553
|
-
}catch(error){
|
|
554
|
-
if(!/^E_REVIEW_(INTENT|VALUE|SCOPE|TEXT_FIT|ROLE_UNRESOLVED|OUTCOME_AUDIO_AMBIGUOUS|ASSET_CHOICE|WIN_AUDIO_MUST_BE_NEW)$/.test(error.message))throw error;
|
|
555
|
-
await onReceipt({type:'route',route:'general-agent'});
|
|
556
|
-
}
|
|
557
|
-
}else{
|
|
558
|
-
await onReceipt({type:'route',route:'general-agent'});
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
outcome??=await this.agent({
|
|
562
|
-
input,
|
|
563
|
-
tools,callTool,progress,checkpoint,paid:request.paid===true,onReceipt,
|
|
564
|
-
...(interpreterCalls?{turnBudget:Math.max(1,8-interpreterCalls)}:{}),
|
|
565
|
-
answer:request.clarification?.answer,
|
|
566
|
-
validateFinal:async answer=>{
|
|
567
|
-
check(!Object.hasOwn(answer,'question')||!request.clarification,'E_REVIEW_CLARIFICATION');
|
|
568
|
-
return finalize(answer);
|
|
569
|
-
},
|
|
570
|
-
});
|
|
571
|
-
}catch(error){
|
|
572
|
-
const reason=/^E_[A-Z0-9_]+$/.test(error.message)?error.message:'E_REVIEW_MODEL_RESPONSE';
|
|
573
|
-
outcome={status:'model-error',reason,...(error.creativeQa?{creativeQa:error.creativeQa}:{})};
|
|
574
|
-
const retryAfterSeconds=error.retryAfterSeconds??(reason==='E_MODEL_RATE_LIMIT'?30:undefined);
|
|
575
|
-
if(Number.isSafeInteger(retryAfterSeconds)&&retryAfterSeconds>=0){
|
|
576
|
-
outcome.retryAfterSeconds=retryAfterSeconds;
|
|
577
|
-
outcome.retryAt=Date.now()+Math.min(retryAfterSeconds,86400)*1000;
|
|
578
|
-
}
|
|
579
|
-
}finally{await toolsSession.dispose();}
|
|
580
|
-
return this.update(id,(state,current)=>{
|
|
581
|
-
const active=find(state,requestId);
|
|
582
|
-
if(active.status==='resolving'&&active.modelRun?.id===runId){
|
|
583
|
-
delete active.modelRun;
|
|
584
|
-
if(!isDeepStrictEqual(request.expected,identity(current))){
|
|
585
|
-
delete active.agentProgress;
|
|
586
|
-
Object.assign(active,{status:'model-error',reason:'E_NATIVE_STALE'});
|
|
587
|
-
}else{
|
|
588
|
-
if(outcome.status==='candidate')state.passesUsed++;
|
|
589
|
-
if(!['model-error','needs-clarification'].includes(outcome.status))delete active.agentProgress;
|
|
590
|
-
Object.assign(active,outcome);
|
|
591
|
-
if(outcome.status==='needs-clarification')applyReviewReceiptEvent(active.receipt,{type:'clarification'});
|
|
592
|
-
applyReviewReceiptEvent(active.receipt,{type:'final',disposition:outcome.status,candidateId:outcome.status==='candidate'?proposalId:undefined});
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
return publicRequest(active);
|
|
596
|
-
});
|
|
231
|
+
async buildCandidate(id,request,proposalId,baselineProject){
|
|
232
|
+
check(request.operations===undefined,'E_REVIEW_JOURNAL');
|
|
233
|
+
const proposal=request.proposals.find(item=>item.id===proposalId);
|
|
234
|
+
check(proposal&&proposal.operations.every(operation=>!['registerAsset','updateAsset'].includes(operation.kind)),'E_REVIEW_ASSET_INGEST');
|
|
235
|
+
const preview=await this.service.preview(id,{format:'tora-transaction-1',expected:request.baseline,operations:proposal.operations});
|
|
236
|
+
const changed=changedNativeRecords(baselineProject,preview.project);
|
|
237
|
+
check(changed.length>0,'E_REVIEW_NO_CHANGE');
|
|
238
|
+
const scope=[...new Set(request.notes.flatMap(note=>note.scope))];
|
|
239
|
+
const allowCreates=request.notes.some(note=>note.allowCreates);
|
|
240
|
+
const createScope=[...new Set(request.notes.filter(note=>note.allowCreates).flatMap(note=>note.scope))];
|
|
241
|
+
assertNativeScope(baselineProject,preview.project,changed,scope,allowCreates,createScope);
|
|
242
|
+
const candidateProject={...preview.project,revisionId:'rev-'+proposalId};
|
|
243
|
+
const artifact=await this.service.build(id,candidateProject,'standalone-browser-draft');
|
|
244
|
+
const outputDir=resolve(this.service.store.root,id,'qa','reviews',proposalId);
|
|
245
|
+
const report=await this.service.runQa(id,{revisionId:request.baseline.revisionId,target:'standalone-browser-draft',project: candidateProject,artifact,outputDir,motionFilmstrip:true,recordVideo:false});
|
|
246
|
+
requireCreativeQa(report);
|
|
247
|
+
return {status:'candidate',operations:proposal.operations,previewDigest:preview.previewDigest,candidate:{
|
|
248
|
+
id:proposalId,summary:proposal.summary,addressedNoteIds:proposal.addressedNoteIds,bytes:artifact.manifest.bytes,
|
|
249
|
+
artifactHash:artifact.manifest.artifactHash,changedIds:changed,
|
|
250
|
+
qa:{ok:true,format:report.format,artifactHash:report.artifactHash,outputDir:report.immutableOutputDir??report.outputDir,warnings:report.warnings??[],blockers:[]},
|
|
251
|
+
}};
|
|
597
252
|
}
|
|
598
|
-
|
|
599
|
-
async candidate(id,requestId)
|
|
600
|
-
const journal=await this.
|
|
601
|
-
const request=find(journal.review??empty(),requestId);
|
|
253
|
+
envelope(request){return {format:'tora-transaction-1',expected:request.baseline,operations:request.operations,previewDigest:request.previewDigest};}
|
|
254
|
+
async candidate(id,requestId){
|
|
255
|
+
const journal=await this.journal(id),request=find(journal.reviewV2??empty(),requestId);
|
|
602
256
|
check(request.status==='candidate','E_REVIEW_STATUS');
|
|
257
|
+
const current=await this.service.read(id);
|
|
258
|
+
check(isDeepStrictEqual(request.baseline,identity(current)),'E_NATIVE_STALE');
|
|
603
259
|
const {project,previewDigest}=await this.service.preview(id,this.envelope(request));
|
|
604
260
|
check(previewDigest===request.previewDigest,'E_NATIVE_PREVIEW_STALE');
|
|
605
261
|
const assets=await lowerAssets(resolve(this.service.store.root,id),project.assets);
|
|
606
|
-
return {project,assets};
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
envelope(request) {
|
|
610
|
-
return {format:'tora-transaction-1',expected:request.expected,operations:request.operations,previewDigest:request.previewDigest};
|
|
262
|
+
return {project,assets,artifactHash:request.candidate.artifactHash,qa:request.candidate.qa};
|
|
611
263
|
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
const journal=await this.service.history.readRevisionJournal(id);
|
|
615
|
-
const request=find(journal.review??empty(),requestId);
|
|
264
|
+
async promote(id,requestId){
|
|
265
|
+
const journal=await this.journal(id),request=find(journal.reviewV2??empty(),requestId);
|
|
616
266
|
if(request.status==='promoted')return request.revision;
|
|
617
|
-
check(request.status==='candidate','E_REVIEW_STATUS');
|
|
618
|
-
|
|
619
|
-
guard:state=>check(find(state.
|
|
620
|
-
record:(state,revision)=>{
|
|
621
|
-
const active=find(state.review,requestId);
|
|
622
|
-
Object.assign(active,{status:'promoted',revision});
|
|
623
|
-
active.receipt??=createReviewReceipt(requestId,active.expected);
|
|
624
|
-
applyReviewReceiptEvent(active.receipt,{type:'final',disposition:'promoted',candidateId:active.modelRun?.proposalId??active.candidate?.id});
|
|
625
|
-
},
|
|
267
|
+
check(request.status==='candidate'&&request.candidate.qa?.ok===true,'E_REVIEW_STATUS');
|
|
268
|
+
const committed=await this.service.transact(id,this.envelope(request),{
|
|
269
|
+
guard:state=>check(find(state.reviewV2,requestId).status==='candidate','E_REVIEW_STATUS'),
|
|
270
|
+
record:(state,revision)=>{Object.assign(find(state.reviewV2,requestId),{status:'promoted',revision});},
|
|
626
271
|
});
|
|
272
|
+
return {revisionId:committed.revisionId,contentHash:committed.contentHash};
|
|
627
273
|
}
|
|
628
|
-
|
|
629
|
-
async discard(id,requestId) {
|
|
274
|
+
async discard(id,requestId){
|
|
630
275
|
return this.update(id,state=>{
|
|
631
276
|
const request=find(state,requestId);
|
|
632
277
|
check(request.status!=='promoted','E_REVIEW_STATUS');
|
|
633
|
-
request.status='discarded';
|
|
634
|
-
request.receipt??=createReviewReceipt(requestId,request.expected);
|
|
635
|
-
applyReviewReceiptEvent(request.receipt,{type:'final',disposition:'discarded'});
|
|
636
|
-
delete request.agentProgress;
|
|
278
|
+
if(request.status!=='discarded')request.status='discarded';
|
|
637
279
|
return publicRequest(request);
|
|
638
280
|
});
|
|
639
281
|
}
|
|
640
|
-
|
|
641
|
-
async
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
const
|
|
645
|
-
request:Object.hasOwn(input??{},'notes')?['expected','notes','requestId']:['expected','target','fields','moment','note','requestId'],
|
|
646
|
-
store:['expected','url'],
|
|
647
|
-
promote:['requestId'],
|
|
648
|
-
discard:['requestId'],
|
|
649
|
-
resolve:['requestId'],
|
|
650
|
-
answer:['requestId','answer'],
|
|
651
|
-
}[input?.action];
|
|
652
|
-
check(Array.isArray(fields)&&exact(input,['action',...fields,...(input.action==='request'?['frame','intent','replacesRequestId','paid']:[])])&&fields.every(field=>Object.hasOwn(input,field)),'E_REVIEW_ARGUMENT');
|
|
653
|
-
if(input.action==='request')return this.request(id,input);
|
|
654
|
-
if(input.action==='store')return this.store(id,input);
|
|
655
|
-
if(input.action==='answer')return this.answer(id,input.requestId,input.answer);
|
|
282
|
+
async bundle(id){return JSON.parse(await exportProjectFile(this.service,id));}
|
|
283
|
+
async post(id,input){
|
|
284
|
+
const fields={request:['requestId','baseline','notes'],promote:['requestId'],discard:['requestId']}[input?.action];
|
|
285
|
+
check(Array.isArray(fields)&&exact(input,['action',...fields,...(input.action==='request'?['replacesRequestId']:[])])&&fields.every(field=>Object.hasOwn(input,field)),'E_REVIEW_ARGUMENT');
|
|
286
|
+
if(input.action==='request'){const {action,...rest}=input;void action;return this.request(id,rest);}
|
|
656
287
|
return this[input.action](id,input.requestId);
|
|
657
288
|
}
|
|
658
289
|
}
|