@semiont/make-meaning 0.2.30-build.61 → 0.2.30-build.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -717,11 +717,14 @@ var CommentDetectionWorker = class extends JobWorker {
717
717
  return "CommentDetectionWorker";
718
718
  }
719
719
  canProcessJob(job) {
720
- return job.type === "comment-detection";
720
+ return job.metadata.type === "comment-detection";
721
721
  }
722
722
  async executeJob(job) {
723
- if (job.type !== "comment-detection") {
724
- throw new Error(`Invalid job type: ${job.type}`);
723
+ if (job.metadata.type !== "comment-detection") {
724
+ throw new Error(`Invalid job type: ${job.metadata.type}`);
725
+ }
726
+ if (job.status !== "running") {
727
+ throw new Error(`Job must be in running state to execute, got: ${job.status}`);
725
728
  }
726
729
  this.isFirstProgress = true;
727
730
  await this.processCommentDetectionJob(job);
@@ -731,23 +734,25 @@ var CommentDetectionWorker = class extends JobWorker {
731
734
  */
732
735
  async updateJobProgress(job) {
733
736
  await super.updateJobProgress(job);
734
- if (job.type !== "comment-detection") return;
737
+ if (job.metadata.type !== "comment-detection") return;
738
+ if (job.status !== "running") {
739
+ return;
740
+ }
735
741
  const cdJob = job;
736
- if (!cdJob.progress) return;
737
742
  const baseEvent = {
738
- resourceId: cdJob.resourceId,
739
- userId: cdJob.userId,
743
+ resourceId: cdJob.params.resourceId,
744
+ userId: cdJob.metadata.userId,
740
745
  version: 1
741
746
  };
742
- const isComplete = cdJob.progress.percentage === 100 && cdJob.result;
747
+ const isComplete = cdJob.progress.percentage === 100;
743
748
  if (this.isFirstProgress) {
744
749
  this.isFirstProgress = false;
745
750
  await this.eventStore.appendEvent({
746
751
  type: "job.started",
747
752
  ...baseEvent,
748
753
  payload: {
749
- jobId: cdJob.id,
750
- jobType: cdJob.type
754
+ jobId: cdJob.metadata.id,
755
+ jobType: cdJob.metadata.type
751
756
  }
752
757
  });
753
758
  } else if (isComplete) {
@@ -755,9 +760,9 @@ var CommentDetectionWorker = class extends JobWorker {
755
760
  type: "job.completed",
756
761
  ...baseEvent,
757
762
  payload: {
758
- jobId: cdJob.id,
759
- jobType: cdJob.type,
760
- result: cdJob.result
763
+ jobId: cdJob.metadata.id,
764
+ jobType: cdJob.metadata.type
765
+ // Note: result would come from job.result, but that's handled by base class
761
766
  }
762
767
  });
763
768
  } else {
@@ -765,8 +770,8 @@ var CommentDetectionWorker = class extends JobWorker {
765
770
  type: "job.progress",
766
771
  ...baseEvent,
767
772
  payload: {
768
- jobId: cdJob.id,
769
- jobType: cdJob.type,
773
+ jobId: cdJob.metadata.id,
774
+ jobType: cdJob.metadata.type,
770
775
  progress: cdJob.progress
771
776
  }
772
777
  });
@@ -774,72 +779,80 @@ var CommentDetectionWorker = class extends JobWorker {
774
779
  }
775
780
  async handleJobFailure(job, error) {
776
781
  await super.handleJobFailure(job, error);
777
- if (job.status === "failed" && job.type === "comment-detection") {
782
+ if (job.status === "failed" && job.metadata.type === "comment-detection") {
778
783
  const cdJob = job;
779
784
  await this.eventStore.appendEvent({
780
785
  type: "job.failed",
781
- resourceId: cdJob.resourceId,
782
- userId: cdJob.userId,
786
+ resourceId: cdJob.params.resourceId,
787
+ userId: cdJob.metadata.userId,
783
788
  version: 1,
784
789
  payload: {
785
- jobId: cdJob.id,
786
- jobType: cdJob.type,
790
+ jobId: cdJob.metadata.id,
791
+ jobType: cdJob.metadata.type,
787
792
  error: "Comment detection failed. Please try again later."
788
793
  }
789
794
  });
790
795
  }
791
796
  }
792
797
  async processCommentDetectionJob(job) {
793
- console.log(`[CommentDetectionWorker] Processing comment detection for resource ${job.resourceId} (job: ${job.id})`);
794
- const resource = await ResourceContext.getResourceMetadata(job.resourceId, this.config);
798
+ console.log(`[CommentDetectionWorker] Processing comment detection for resource ${job.params.resourceId} (job: ${job.metadata.id})`);
799
+ const resource = await ResourceContext.getResourceMetadata(job.params.resourceId, this.config);
795
800
  if (!resource) {
796
- throw new Error(`Resource ${job.resourceId} not found`);
801
+ throw new Error(`Resource ${job.params.resourceId} not found`);
797
802
  }
798
- job.progress = {
799
- stage: "analyzing",
800
- percentage: 10,
801
- message: "Loading resource..."
803
+ let updatedJob = {
804
+ ...job,
805
+ progress: {
806
+ stage: "analyzing",
807
+ percentage: 10,
808
+ message: "Loading resource..."
809
+ }
802
810
  };
803
- await this.updateJobProgress(job);
804
- job.progress = {
805
- stage: "analyzing",
806
- percentage: 30,
807
- message: "Analyzing text and generating comments..."
811
+ await this.updateJobProgress(updatedJob);
812
+ updatedJob = {
813
+ ...updatedJob,
814
+ progress: {
815
+ stage: "analyzing",
816
+ percentage: 30,
817
+ message: "Analyzing text and generating comments..."
818
+ }
808
819
  };
809
- await this.updateJobProgress(job);
820
+ await this.updateJobProgress(updatedJob);
810
821
  const comments = await AnnotationDetection.detectComments(
811
- job.resourceId,
822
+ job.params.resourceId,
812
823
  this.config,
813
- job.instructions,
814
- job.tone,
815
- job.density
824
+ job.params.instructions,
825
+ job.params.tone,
826
+ job.params.density
816
827
  );
817
828
  console.log(`[CommentDetectionWorker] Found ${comments.length} comments to create`);
818
- job.progress = {
819
- stage: "creating",
820
- percentage: 60,
821
- message: `Creating ${comments.length} annotations...`
829
+ updatedJob = {
830
+ ...updatedJob,
831
+ progress: {
832
+ stage: "creating",
833
+ percentage: 60,
834
+ message: `Creating ${comments.length} annotations...`
835
+ }
822
836
  };
823
- await this.updateJobProgress(job);
837
+ await this.updateJobProgress(updatedJob);
824
838
  let created = 0;
825
839
  for (const comment of comments) {
826
840
  try {
827
- await this.createCommentAnnotation(job.resourceId, job.userId, comment);
841
+ await this.createCommentAnnotation(job.params.resourceId, job.metadata.userId, comment);
828
842
  created++;
829
843
  } catch (error) {
830
844
  console.error(`[CommentDetectionWorker] Failed to create comment:`, error);
831
845
  }
832
846
  }
833
- job.result = {
834
- commentsFound: comments.length,
835
- commentsCreated: created
836
- };
837
- job.progress = {
838
- stage: "creating",
839
- percentage: 100,
840
- message: `Complete! Created ${created} comments`
847
+ updatedJob = {
848
+ ...updatedJob,
849
+ progress: {
850
+ stage: "creating",
851
+ percentage: 100,
852
+ message: `Complete! Created ${created} comments`
853
+ }
841
854
  };
842
- await this.updateJobProgress(job);
855
+ await this.updateJobProgress(updatedJob);
843
856
  console.log(`[CommentDetectionWorker] \u2705 Created ${created}/${comments.length} comments`);
844
857
  }
845
858
  async createCommentAnnotation(resourceId2, userId_, comment) {
@@ -910,11 +923,14 @@ var HighlightDetectionWorker = class extends JobWorker2 {
910
923
  return "HighlightDetectionWorker";
911
924
  }
912
925
  canProcessJob(job) {
913
- return job.type === "highlight-detection";
926
+ return job.metadata.type === "highlight-detection";
914
927
  }
915
928
  async executeJob(job) {
916
- if (job.type !== "highlight-detection") {
917
- throw new Error(`Invalid job type: ${job.type}`);
929
+ if (job.metadata.type !== "highlight-detection") {
930
+ throw new Error(`Invalid job type: ${job.metadata.type}`);
931
+ }
932
+ if (job.status !== "running") {
933
+ throw new Error(`Job must be in running state to execute, got: ${job.status}`);
918
934
  }
919
935
  this.isFirstProgress = true;
920
936
  await this.processHighlightDetectionJob(job);
@@ -924,23 +940,25 @@ var HighlightDetectionWorker = class extends JobWorker2 {
924
940
  */
925
941
  async updateJobProgress(job) {
926
942
  await super.updateJobProgress(job);
927
- if (job.type !== "highlight-detection") return;
943
+ if (job.metadata.type !== "highlight-detection") return;
944
+ if (job.status !== "running") {
945
+ return;
946
+ }
928
947
  const hlJob = job;
929
- if (!hlJob.progress) return;
930
948
  const baseEvent = {
931
- resourceId: hlJob.resourceId,
932
- userId: hlJob.userId,
949
+ resourceId: hlJob.params.resourceId,
950
+ userId: hlJob.metadata.userId,
933
951
  version: 1
934
952
  };
935
- const isComplete = hlJob.progress.percentage === 100 && hlJob.result;
953
+ const isComplete = hlJob.progress.percentage === 100;
936
954
  if (this.isFirstProgress) {
937
955
  this.isFirstProgress = false;
938
956
  await this.eventStore.appendEvent({
939
957
  type: "job.started",
940
958
  ...baseEvent,
941
959
  payload: {
942
- jobId: hlJob.id,
943
- jobType: hlJob.type
960
+ jobId: hlJob.metadata.id,
961
+ jobType: hlJob.metadata.type
944
962
  }
945
963
  });
946
964
  } else if (isComplete) {
@@ -948,9 +966,9 @@ var HighlightDetectionWorker = class extends JobWorker2 {
948
966
  type: "job.completed",
949
967
  ...baseEvent,
950
968
  payload: {
951
- jobId: hlJob.id,
952
- jobType: hlJob.type,
953
- result: hlJob.result
969
+ jobId: hlJob.metadata.id,
970
+ jobType: hlJob.metadata.type
971
+ // Note: result would come from job.result, but that's handled by base class
954
972
  }
955
973
  });
956
974
  } else {
@@ -958,8 +976,8 @@ var HighlightDetectionWorker = class extends JobWorker2 {
958
976
  type: "job.progress",
959
977
  ...baseEvent,
960
978
  payload: {
961
- jobId: hlJob.id,
962
- jobType: hlJob.type,
979
+ jobId: hlJob.metadata.id,
980
+ jobType: hlJob.metadata.type,
963
981
  progress: hlJob.progress
964
982
  }
965
983
  });
@@ -967,71 +985,79 @@ var HighlightDetectionWorker = class extends JobWorker2 {
967
985
  }
968
986
  async handleJobFailure(job, error) {
969
987
  await super.handleJobFailure(job, error);
970
- if (job.status === "failed" && job.type === "highlight-detection") {
988
+ if (job.status === "failed" && job.metadata.type === "highlight-detection") {
971
989
  const hlJob = job;
972
990
  await this.eventStore.appendEvent({
973
991
  type: "job.failed",
974
- resourceId: hlJob.resourceId,
975
- userId: hlJob.userId,
992
+ resourceId: hlJob.params.resourceId,
993
+ userId: hlJob.metadata.userId,
976
994
  version: 1,
977
995
  payload: {
978
- jobId: hlJob.id,
979
- jobType: hlJob.type,
996
+ jobId: hlJob.metadata.id,
997
+ jobType: hlJob.metadata.type,
980
998
  error: "Highlight detection failed. Please try again later."
981
999
  }
982
1000
  });
983
1001
  }
984
1002
  }
985
1003
  async processHighlightDetectionJob(job) {
986
- console.log(`[HighlightDetectionWorker] Processing highlight detection for resource ${job.resourceId} (job: ${job.id})`);
987
- const resource = await ResourceContext.getResourceMetadata(job.resourceId, this.config);
1004
+ console.log(`[HighlightDetectionWorker] Processing highlight detection for resource ${job.params.resourceId} (job: ${job.metadata.id})`);
1005
+ const resource = await ResourceContext.getResourceMetadata(job.params.resourceId, this.config);
988
1006
  if (!resource) {
989
- throw new Error(`Resource ${job.resourceId} not found`);
1007
+ throw new Error(`Resource ${job.params.resourceId} not found`);
990
1008
  }
991
- job.progress = {
992
- stage: "analyzing",
993
- percentage: 10,
994
- message: "Loading resource..."
1009
+ let updatedJob = {
1010
+ ...job,
1011
+ progress: {
1012
+ stage: "analyzing",
1013
+ percentage: 10,
1014
+ message: "Loading resource..."
1015
+ }
995
1016
  };
996
- await this.updateJobProgress(job);
997
- job.progress = {
998
- stage: "analyzing",
999
- percentage: 30,
1000
- message: "Analyzing text..."
1017
+ await this.updateJobProgress(updatedJob);
1018
+ updatedJob = {
1019
+ ...updatedJob,
1020
+ progress: {
1021
+ stage: "analyzing",
1022
+ percentage: 30,
1023
+ message: "Analyzing text..."
1024
+ }
1001
1025
  };
1002
- await this.updateJobProgress(job);
1026
+ await this.updateJobProgress(updatedJob);
1003
1027
  const highlights = await AnnotationDetection.detectHighlights(
1004
- job.resourceId,
1028
+ job.params.resourceId,
1005
1029
  this.config,
1006
- job.instructions,
1007
- job.density
1030
+ job.params.instructions,
1031
+ job.params.density
1008
1032
  );
1009
1033
  console.log(`[HighlightDetectionWorker] Found ${highlights.length} highlights to create`);
1010
- job.progress = {
1011
- stage: "creating",
1012
- percentage: 60,
1013
- message: `Creating ${highlights.length} annotations...`
1034
+ updatedJob = {
1035
+ ...updatedJob,
1036
+ progress: {
1037
+ stage: "creating",
1038
+ percentage: 60,
1039
+ message: `Creating ${highlights.length} annotations...`
1040
+ }
1014
1041
  };
1015
- await this.updateJobProgress(job);
1042
+ await this.updateJobProgress(updatedJob);
1016
1043
  let created = 0;
1017
1044
  for (const highlight of highlights) {
1018
1045
  try {
1019
- await this.createHighlightAnnotation(job.resourceId, job.userId, highlight);
1046
+ await this.createHighlightAnnotation(job.params.resourceId, job.metadata.userId, highlight);
1020
1047
  created++;
1021
1048
  } catch (error) {
1022
1049
  console.error(`[HighlightDetectionWorker] Failed to create highlight:`, error);
1023
1050
  }
1024
1051
  }
1025
- job.result = {
1026
- highlightsFound: highlights.length,
1027
- highlightsCreated: created
1028
- };
1029
- job.progress = {
1030
- stage: "creating",
1031
- percentage: 100,
1032
- message: `Complete! Created ${created} highlights`
1052
+ updatedJob = {
1053
+ ...updatedJob,
1054
+ progress: {
1055
+ stage: "creating",
1056
+ percentage: 100,
1057
+ message: `Complete! Created ${created} highlights`
1058
+ }
1033
1059
  };
1034
- await this.updateJobProgress(job);
1060
+ await this.updateJobProgress(updatedJob);
1035
1061
  console.log(`[HighlightDetectionWorker] \u2705 Created ${created}/${highlights.length} highlights`);
1036
1062
  }
1037
1063
  async createHighlightAnnotation(resourceId2, creatorUserId, highlight) {
@@ -1092,11 +1118,14 @@ var AssessmentDetectionWorker = class extends JobWorker3 {
1092
1118
  return "AssessmentDetectionWorker";
1093
1119
  }
1094
1120
  canProcessJob(job) {
1095
- return job.type === "assessment-detection";
1121
+ return job.metadata.type === "assessment-detection";
1096
1122
  }
1097
1123
  async executeJob(job) {
1098
- if (job.type !== "assessment-detection") {
1099
- throw new Error(`Invalid job type: ${job.type}`);
1124
+ if (job.metadata.type !== "assessment-detection") {
1125
+ throw new Error(`Invalid job type: ${job.metadata.type}`);
1126
+ }
1127
+ if (job.status !== "running") {
1128
+ throw new Error(`Job must be in running state to execute, got: ${job.status}`);
1100
1129
  }
1101
1130
  this.isFirstProgress = true;
1102
1131
  await this.processAssessmentDetectionJob(job);
@@ -1106,23 +1135,25 @@ var AssessmentDetectionWorker = class extends JobWorker3 {
1106
1135
  */
1107
1136
  async updateJobProgress(job) {
1108
1137
  await super.updateJobProgress(job);
1109
- if (job.type !== "assessment-detection") return;
1138
+ if (job.metadata.type !== "assessment-detection") return;
1139
+ if (job.status !== "running") {
1140
+ return;
1141
+ }
1110
1142
  const assJob = job;
1111
- if (!assJob.progress) return;
1112
1143
  const baseEvent = {
1113
- resourceId: assJob.resourceId,
1114
- userId: assJob.userId,
1144
+ resourceId: assJob.params.resourceId,
1145
+ userId: assJob.metadata.userId,
1115
1146
  version: 1
1116
1147
  };
1117
- const isComplete = assJob.progress.percentage === 100 && assJob.result;
1148
+ const isComplete = assJob.progress.percentage === 100;
1118
1149
  if (this.isFirstProgress) {
1119
1150
  this.isFirstProgress = false;
1120
1151
  await this.eventStore.appendEvent({
1121
1152
  type: "job.started",
1122
1153
  ...baseEvent,
1123
1154
  payload: {
1124
- jobId: assJob.id,
1125
- jobType: assJob.type
1155
+ jobId: assJob.metadata.id,
1156
+ jobType: assJob.metadata.type
1126
1157
  }
1127
1158
  });
1128
1159
  } else if (isComplete) {
@@ -1130,9 +1161,9 @@ var AssessmentDetectionWorker = class extends JobWorker3 {
1130
1161
  type: "job.completed",
1131
1162
  ...baseEvent,
1132
1163
  payload: {
1133
- jobId: assJob.id,
1134
- jobType: assJob.type,
1135
- result: assJob.result
1164
+ jobId: assJob.metadata.id,
1165
+ jobType: assJob.metadata.type
1166
+ // Note: result would come from job.result, but that's handled by base class
1136
1167
  }
1137
1168
  });
1138
1169
  } else {
@@ -1140,8 +1171,8 @@ var AssessmentDetectionWorker = class extends JobWorker3 {
1140
1171
  type: "job.progress",
1141
1172
  ...baseEvent,
1142
1173
  payload: {
1143
- jobId: assJob.id,
1144
- jobType: assJob.type,
1174
+ jobId: assJob.metadata.id,
1175
+ jobType: assJob.metadata.type,
1145
1176
  progress: assJob.progress
1146
1177
  }
1147
1178
  });
@@ -1149,72 +1180,80 @@ var AssessmentDetectionWorker = class extends JobWorker3 {
1149
1180
  }
1150
1181
  async handleJobFailure(job, error) {
1151
1182
  await super.handleJobFailure(job, error);
1152
- if (job.status === "failed" && job.type === "assessment-detection") {
1183
+ if (job.status === "failed" && job.metadata.type === "assessment-detection") {
1153
1184
  const aJob = job;
1154
1185
  await this.eventStore.appendEvent({
1155
1186
  type: "job.failed",
1156
- resourceId: aJob.resourceId,
1157
- userId: aJob.userId,
1187
+ resourceId: aJob.params.resourceId,
1188
+ userId: aJob.metadata.userId,
1158
1189
  version: 1,
1159
1190
  payload: {
1160
- jobId: aJob.id,
1161
- jobType: aJob.type,
1191
+ jobId: aJob.metadata.id,
1192
+ jobType: aJob.metadata.type,
1162
1193
  error: "Assessment detection failed. Please try again later."
1163
1194
  }
1164
1195
  });
1165
1196
  }
1166
1197
  }
1167
1198
  async processAssessmentDetectionJob(job) {
1168
- console.log(`[AssessmentDetectionWorker] Processing assessment detection for resource ${job.resourceId} (job: ${job.id})`);
1169
- const resource = await ResourceContext.getResourceMetadata(job.resourceId, this.config);
1199
+ console.log(`[AssessmentDetectionWorker] Processing assessment detection for resource ${job.params.resourceId} (job: ${job.metadata.id})`);
1200
+ const resource = await ResourceContext.getResourceMetadata(job.params.resourceId, this.config);
1170
1201
  if (!resource) {
1171
- throw new Error(`Resource ${job.resourceId} not found`);
1202
+ throw new Error(`Resource ${job.params.resourceId} not found`);
1172
1203
  }
1173
- job.progress = {
1174
- stage: "analyzing",
1175
- percentage: 10,
1176
- message: "Loading resource..."
1204
+ let updatedJob = {
1205
+ ...job,
1206
+ progress: {
1207
+ stage: "analyzing",
1208
+ percentage: 10,
1209
+ message: "Loading resource..."
1210
+ }
1177
1211
  };
1178
- await this.updateJobProgress(job);
1179
- job.progress = {
1180
- stage: "analyzing",
1181
- percentage: 30,
1182
- message: "Analyzing text..."
1212
+ await this.updateJobProgress(updatedJob);
1213
+ updatedJob = {
1214
+ ...updatedJob,
1215
+ progress: {
1216
+ stage: "analyzing",
1217
+ percentage: 30,
1218
+ message: "Analyzing text..."
1219
+ }
1183
1220
  };
1184
- await this.updateJobProgress(job);
1221
+ await this.updateJobProgress(updatedJob);
1185
1222
  const assessments = await AnnotationDetection.detectAssessments(
1186
- job.resourceId,
1223
+ job.params.resourceId,
1187
1224
  this.config,
1188
- job.instructions,
1189
- job.tone,
1190
- job.density
1225
+ job.params.instructions,
1226
+ job.params.tone,
1227
+ job.params.density
1191
1228
  );
1192
1229
  console.log(`[AssessmentDetectionWorker] Found ${assessments.length} assessments to create`);
1193
- job.progress = {
1194
- stage: "creating",
1195
- percentage: 60,
1196
- message: `Creating ${assessments.length} annotations...`
1230
+ updatedJob = {
1231
+ ...updatedJob,
1232
+ progress: {
1233
+ stage: "creating",
1234
+ percentage: 60,
1235
+ message: `Creating ${assessments.length} annotations...`
1236
+ }
1197
1237
  };
1198
- await this.updateJobProgress(job);
1238
+ await this.updateJobProgress(updatedJob);
1199
1239
  let created = 0;
1200
1240
  for (const assessment of assessments) {
1201
1241
  try {
1202
- await this.createAssessmentAnnotation(job.resourceId, job.userId, assessment);
1242
+ await this.createAssessmentAnnotation(job.params.resourceId, job.metadata.userId, assessment);
1203
1243
  created++;
1204
1244
  } catch (error) {
1205
1245
  console.error(`[AssessmentDetectionWorker] Failed to create assessment:`, error);
1206
1246
  }
1207
1247
  }
1208
- job.result = {
1209
- assessmentsFound: assessments.length,
1210
- assessmentsCreated: created
1211
- };
1212
- job.progress = {
1213
- stage: "creating",
1214
- percentage: 100,
1215
- message: `Complete! Created ${created} assessments`
1248
+ updatedJob = {
1249
+ ...updatedJob,
1250
+ progress: {
1251
+ stage: "creating",
1252
+ percentage: 100,
1253
+ message: `Complete! Created ${created} assessments`
1254
+ }
1216
1255
  };
1217
- await this.updateJobProgress(job);
1256
+ await this.updateJobProgress(updatedJob);
1218
1257
  console.log(`[AssessmentDetectionWorker] \u2705 Created ${created}/${assessments.length} assessments`);
1219
1258
  }
1220
1259
  async createAssessmentAnnotation(resourceId2, creatorUserId, assessment) {
@@ -1279,11 +1318,14 @@ var TagDetectionWorker = class extends JobWorker4 {
1279
1318
  return "TagDetectionWorker";
1280
1319
  }
1281
1320
  canProcessJob(job) {
1282
- return job.type === "tag-detection";
1321
+ return job.metadata.type === "tag-detection";
1283
1322
  }
1284
1323
  async executeJob(job) {
1285
- if (job.type !== "tag-detection") {
1286
- throw new Error(`Invalid job type: ${job.type}`);
1324
+ if (job.metadata.type !== "tag-detection") {
1325
+ throw new Error(`Invalid job type: ${job.metadata.type}`);
1326
+ }
1327
+ if (job.status !== "running") {
1328
+ throw new Error(`Job must be in running state to execute, got: ${job.status}`);
1287
1329
  }
1288
1330
  this.isFirstProgress = true;
1289
1331
  await this.processTagDetectionJob(job);
@@ -1293,23 +1335,25 @@ var TagDetectionWorker = class extends JobWorker4 {
1293
1335
  */
1294
1336
  async updateJobProgress(job) {
1295
1337
  await super.updateJobProgress(job);
1296
- if (job.type !== "tag-detection") return;
1338
+ if (job.metadata.type !== "tag-detection") return;
1339
+ if (job.status !== "running") {
1340
+ return;
1341
+ }
1297
1342
  const tdJob = job;
1298
- if (!tdJob.progress) return;
1299
1343
  const baseEvent = {
1300
- resourceId: tdJob.resourceId,
1301
- userId: tdJob.userId,
1344
+ resourceId: tdJob.params.resourceId,
1345
+ userId: tdJob.metadata.userId,
1302
1346
  version: 1
1303
1347
  };
1304
- const isComplete = tdJob.progress.percentage === 100 && tdJob.result;
1348
+ const isComplete = tdJob.progress.percentage === 100;
1305
1349
  if (this.isFirstProgress) {
1306
1350
  this.isFirstProgress = false;
1307
1351
  await this.eventStore.appendEvent({
1308
1352
  type: "job.started",
1309
1353
  ...baseEvent,
1310
1354
  payload: {
1311
- jobId: tdJob.id,
1312
- jobType: tdJob.type
1355
+ jobId: tdJob.metadata.id,
1356
+ jobType: tdJob.metadata.type
1313
1357
  }
1314
1358
  });
1315
1359
  } else if (isComplete) {
@@ -1317,9 +1361,9 @@ var TagDetectionWorker = class extends JobWorker4 {
1317
1361
  type: "job.completed",
1318
1362
  ...baseEvent,
1319
1363
  payload: {
1320
- jobId: tdJob.id,
1321
- jobType: tdJob.type,
1322
- result: tdJob.result
1364
+ jobId: tdJob.metadata.id,
1365
+ jobType: tdJob.metadata.type
1366
+ // Note: result would come from job.result, but that's handled by base class
1323
1367
  }
1324
1368
  });
1325
1369
  } else {
@@ -1327,8 +1371,8 @@ var TagDetectionWorker = class extends JobWorker4 {
1327
1371
  type: "job.progress",
1328
1372
  ...baseEvent,
1329
1373
  payload: {
1330
- jobId: tdJob.id,
1331
- jobType: tdJob.type,
1374
+ jobId: tdJob.metadata.id,
1375
+ jobType: tdJob.metadata.type,
1332
1376
  progress: tdJob.progress
1333
1377
  }
1334
1378
  });
@@ -1336,98 +1380,105 @@ var TagDetectionWorker = class extends JobWorker4 {
1336
1380
  }
1337
1381
  async handleJobFailure(job, error) {
1338
1382
  await super.handleJobFailure(job, error);
1339
- if (job.status === "failed" && job.type === "tag-detection") {
1383
+ if (job.status === "failed" && job.metadata.type === "tag-detection") {
1340
1384
  const tdJob = job;
1341
1385
  await this.eventStore.appendEvent({
1342
1386
  type: "job.failed",
1343
- resourceId: tdJob.resourceId,
1344
- userId: tdJob.userId,
1387
+ resourceId: tdJob.params.resourceId,
1388
+ userId: tdJob.metadata.userId,
1345
1389
  version: 1,
1346
1390
  payload: {
1347
- jobId: tdJob.id,
1348
- jobType: tdJob.type,
1391
+ jobId: tdJob.metadata.id,
1392
+ jobType: tdJob.metadata.type,
1349
1393
  error: "Tag detection failed. Please try again later."
1350
1394
  }
1351
1395
  });
1352
1396
  }
1353
1397
  }
1354
1398
  async processTagDetectionJob(job) {
1355
- console.log(`[TagDetectionWorker] Processing tag detection for resource ${job.resourceId} (job: ${job.id})`);
1356
- const schema = getTagSchema2(job.schemaId);
1399
+ console.log(`[TagDetectionWorker] Processing tag detection for resource ${job.params.resourceId} (job: ${job.metadata.id})`);
1400
+ const schema = getTagSchema2(job.params.schemaId);
1357
1401
  if (!schema) {
1358
- throw new Error(`Invalid tag schema: ${job.schemaId}`);
1402
+ throw new Error(`Invalid tag schema: ${job.params.schemaId}`);
1359
1403
  }
1360
- for (const category of job.categories) {
1404
+ for (const category of job.params.categories) {
1361
1405
  if (!schema.tags.some((t) => t.name === category)) {
1362
- throw new Error(`Invalid category "${category}" for schema ${job.schemaId}`);
1406
+ throw new Error(`Invalid category "${category}" for schema ${job.params.schemaId}`);
1363
1407
  }
1364
1408
  }
1365
- const resource = await ResourceContext.getResourceMetadata(job.resourceId, this.config);
1409
+ const resource = await ResourceContext.getResourceMetadata(job.params.resourceId, this.config);
1366
1410
  if (!resource) {
1367
- throw new Error(`Resource ${job.resourceId} not found`);
1368
- }
1369
- job.progress = {
1370
- stage: "analyzing",
1371
- percentage: 10,
1372
- processedCategories: 0,
1373
- totalCategories: job.categories.length,
1374
- message: "Loading resource..."
1411
+ throw new Error(`Resource ${job.params.resourceId} not found`);
1412
+ }
1413
+ let updatedJob = {
1414
+ ...job,
1415
+ progress: {
1416
+ stage: "analyzing",
1417
+ percentage: 10,
1418
+ processedCategories: 0,
1419
+ totalCategories: job.params.categories.length,
1420
+ message: "Loading resource..."
1421
+ }
1375
1422
  };
1376
- await this.updateJobProgress(job);
1423
+ await this.updateJobProgress(updatedJob);
1377
1424
  const allTags = [];
1378
1425
  const byCategory = {};
1379
- for (let i = 0; i < job.categories.length; i++) {
1380
- const category = job.categories[i];
1381
- job.progress = {
1382
- stage: "analyzing",
1383
- percentage: 10 + Math.floor(i / job.categories.length * 50),
1384
- currentCategory: category,
1385
- processedCategories: i + 1,
1386
- totalCategories: job.categories.length,
1387
- message: `Analyzing ${category}...`
1426
+ for (let i = 0; i < job.params.categories.length; i++) {
1427
+ const category = job.params.categories[i];
1428
+ updatedJob = {
1429
+ ...updatedJob,
1430
+ progress: {
1431
+ stage: "analyzing",
1432
+ percentage: 10 + Math.floor(i / job.params.categories.length * 50),
1433
+ currentCategory: category,
1434
+ processedCategories: i + 1,
1435
+ totalCategories: job.params.categories.length,
1436
+ message: `Analyzing ${category}...`
1437
+ }
1388
1438
  };
1389
- await this.updateJobProgress(job);
1439
+ await this.updateJobProgress(updatedJob);
1390
1440
  const tags = await AnnotationDetection.detectTags(
1391
- job.resourceId,
1441
+ job.params.resourceId,
1392
1442
  this.config,
1393
- job.schemaId,
1443
+ job.params.schemaId,
1394
1444
  category
1395
1445
  );
1396
1446
  console.log(`[TagDetectionWorker] Found ${tags.length} tags for category "${category}"`);
1397
1447
  allTags.push(...tags);
1398
1448
  byCategory[category] = tags.length;
1399
1449
  }
1400
- job.progress = {
1401
- stage: "creating",
1402
- percentage: 60,
1403
- processedCategories: job.categories.length,
1404
- totalCategories: job.categories.length,
1405
- message: `Creating ${allTags.length} tag annotations...`
1450
+ updatedJob = {
1451
+ ...updatedJob,
1452
+ progress: {
1453
+ stage: "creating",
1454
+ percentage: 60,
1455
+ processedCategories: job.params.categories.length,
1456
+ totalCategories: job.params.categories.length,
1457
+ message: `Creating ${allTags.length} tag annotations...`
1458
+ }
1406
1459
  };
1407
- await this.updateJobProgress(job);
1460
+ await this.updateJobProgress(updatedJob);
1408
1461
  let created = 0;
1409
1462
  for (const tag of allTags) {
1410
1463
  try {
1411
- await this.createTagAnnotation(job.resourceId, job.userId, job.schemaId, tag);
1464
+ await this.createTagAnnotation(job.params.resourceId, job.metadata.userId, job.params.schemaId, tag);
1412
1465
  created++;
1413
1466
  } catch (error) {
1414
1467
  console.error(`[TagDetectionWorker] Failed to create tag:`, error);
1415
1468
  }
1416
1469
  }
1417
- job.result = {
1418
- tagsFound: allTags.length,
1419
- tagsCreated: created,
1420
- byCategory
1421
- };
1422
- job.progress = {
1423
- stage: "creating",
1424
- percentage: 100,
1425
- processedCategories: job.categories.length,
1426
- totalCategories: job.categories.length,
1427
- message: `Complete! Created ${created} tags`
1470
+ updatedJob = {
1471
+ ...updatedJob,
1472
+ progress: {
1473
+ stage: "creating",
1474
+ percentage: 100,
1475
+ processedCategories: job.params.categories.length,
1476
+ totalCategories: job.params.categories.length,
1477
+ message: `Complete! Created ${created} tags`
1478
+ }
1428
1479
  };
1429
- await this.updateJobProgress(job);
1430
- console.log(`[TagDetectionWorker] \u2705 Created ${created}/${allTags.length} tags across ${job.categories.length} categories`);
1480
+ await this.updateJobProgress(updatedJob);
1481
+ console.log(`[TagDetectionWorker] \u2705 Created ${created}/${allTags.length} tags across ${job.params.categories.length} categories`);
1431
1482
  }
1432
1483
  async createTagAnnotation(resourceId2, userId_, schemaId, tag) {
1433
1484
  const backendUrl = this.config.services.backend?.publicURL;
@@ -1508,11 +1559,14 @@ var ReferenceDetectionWorker = class extends JobWorker5 {
1508
1559
  return "ReferenceDetectionWorker";
1509
1560
  }
1510
1561
  canProcessJob(job) {
1511
- return job.type === "detection";
1562
+ return job.metadata.type === "detection";
1512
1563
  }
1513
1564
  async executeJob(job) {
1514
- if (job.type !== "detection") {
1515
- throw new Error(`Invalid job type: ${job.type}`);
1565
+ if (job.metadata.type !== "detection") {
1566
+ throw new Error(`Invalid job type: ${job.metadata.type}`);
1567
+ }
1568
+ if (job.status !== "running") {
1569
+ throw new Error(`Job must be in running state to execute, got: ${job.status}`);
1516
1570
  }
1517
1571
  await this.processDetectionJob(job);
1518
1572
  }
@@ -1566,27 +1620,30 @@ var ReferenceDetectionWorker = class extends JobWorker5 {
1566
1620
  return detectedAnnotations;
1567
1621
  }
1568
1622
  async processDetectionJob(job) {
1569
- console.log(`[ReferenceDetectionWorker] Processing detection for resource ${job.resourceId} (job: ${job.id})`);
1570
- console.log(`[ReferenceDetectionWorker] \u{1F50D} Entity types: ${job.entityTypes.join(", ")}`);
1571
- const resource = await ResourceContext.getResourceMetadata(job.resourceId, this.config);
1623
+ console.log(`[ReferenceDetectionWorker] Processing detection for resource ${job.params.resourceId} (job: ${job.metadata.id})`);
1624
+ console.log(`[ReferenceDetectionWorker] \u{1F50D} Entity types: ${job.params.entityTypes.join(", ")}`);
1625
+ const resource = await ResourceContext.getResourceMetadata(job.params.resourceId, this.config);
1572
1626
  if (!resource) {
1573
- throw new Error(`Resource ${job.resourceId} not found`);
1627
+ throw new Error(`Resource ${job.params.resourceId} not found`);
1574
1628
  }
1575
1629
  let totalFound = 0;
1576
1630
  let totalEmitted = 0;
1577
1631
  let totalErrors = 0;
1578
- job.progress = {
1579
- totalEntityTypes: job.entityTypes.length,
1580
- processedEntityTypes: 0,
1581
- entitiesFound: 0,
1582
- entitiesEmitted: 0
1632
+ let updatedJob = {
1633
+ ...job,
1634
+ progress: {
1635
+ totalEntityTypes: job.params.entityTypes.length,
1636
+ processedEntityTypes: 0,
1637
+ entitiesFound: 0,
1638
+ entitiesEmitted: 0
1639
+ }
1583
1640
  };
1584
- await this.updateJobProgress(job);
1585
- for (let i = 0; i < job.entityTypes.length; i++) {
1586
- const entityType = job.entityTypes[i];
1641
+ await this.updateJobProgress(updatedJob);
1642
+ for (let i = 0; i < job.params.entityTypes.length; i++) {
1643
+ const entityType = job.params.entityTypes[i];
1587
1644
  if (!entityType) continue;
1588
- console.log(`[ReferenceDetectionWorker] \u{1F916} [${i + 1}/${job.entityTypes.length}] Detecting ${entityType}...`);
1589
- const detectedAnnotations = await this.detectReferences(resource, [entityType], job.includeDescriptiveReferences);
1645
+ console.log(`[ReferenceDetectionWorker] \u{1F916} [${i + 1}/${job.params.entityTypes.length}] Detecting ${entityType}...`);
1646
+ const detectedAnnotations = await this.detectReferences(resource, [entityType], job.params.includeDescriptiveReferences);
1590
1647
  totalFound += detectedAnnotations.length;
1591
1648
  console.log(`[ReferenceDetectionWorker] \u2705 Found ${detectedAnnotations.length} ${entityType} entities`);
1592
1649
  for (let idx = 0; idx < detectedAnnotations.length; idx++) {
@@ -1604,16 +1661,13 @@ var ReferenceDetectionWorker = class extends JobWorker5 {
1604
1661
  referenceId = generateAnnotationId5(backendUrl);
1605
1662
  } catch (error) {
1606
1663
  console.error(`[ReferenceDetectionWorker] Failed to generate annotation ID:`, error);
1607
- job.status = "failed";
1608
- job.error = "Configuration error: Backend publicURL not set";
1609
- await this.updateJobProgress(job);
1610
- return;
1664
+ throw new Error("Configuration error: Backend publicURL not set");
1611
1665
  }
1612
1666
  try {
1613
1667
  await this.eventStore.appendEvent({
1614
1668
  type: "annotation.added",
1615
- resourceId: job.resourceId,
1616
- userId: job.userId,
1669
+ resourceId: job.params.resourceId,
1670
+ userId: job.metadata.userId,
1617
1671
  version: 1,
1618
1672
  payload: {
1619
1673
  annotation: {
@@ -1622,7 +1676,7 @@ var ReferenceDetectionWorker = class extends JobWorker5 {
1622
1676
  id: referenceId,
1623
1677
  motivation: "linking",
1624
1678
  target: {
1625
- source: resourceIdToURI6(job.resourceId, this.config.services.backend.publicURL),
1679
+ source: resourceIdToURI6(job.params.resourceId, this.config.services.backend.publicURL),
1626
1680
  // Convert to full URI
1627
1681
  selector: [
1628
1682
  {
@@ -1657,34 +1711,32 @@ var ReferenceDetectionWorker = class extends JobWorker5 {
1657
1711
  }
1658
1712
  }
1659
1713
  console.log(`[ReferenceDetectionWorker] \u2705 Completed ${entityType}: ${detectedAnnotations.length} found, ${detectedAnnotations.length - (totalErrors - (totalFound - totalEmitted))} emitted`);
1660
- job.progress = {
1661
- totalEntityTypes: job.entityTypes.length,
1662
- processedEntityTypes: i + 1,
1663
- currentEntityType: entityType,
1664
- entitiesFound: totalFound,
1665
- entitiesEmitted: totalEmitted
1714
+ updatedJob = {
1715
+ ...updatedJob,
1716
+ progress: {
1717
+ totalEntityTypes: job.params.entityTypes.length,
1718
+ processedEntityTypes: i + 1,
1719
+ currentEntityType: entityType,
1720
+ entitiesFound: totalFound,
1721
+ entitiesEmitted: totalEmitted
1722
+ }
1666
1723
  };
1667
- await this.updateJobProgress(job);
1724
+ await this.updateJobProgress(updatedJob);
1668
1725
  }
1669
- job.result = {
1670
- totalFound,
1671
- totalEmitted,
1672
- errors: totalErrors
1673
- };
1674
1726
  console.log(`[ReferenceDetectionWorker] \u2705 Detection complete: ${totalFound} entities found, ${totalEmitted} events emitted, ${totalErrors} errors`);
1675
1727
  }
1676
1728
  async handleJobFailure(job, error) {
1677
1729
  await super.handleJobFailure(job, error);
1678
- if (job.status === "failed" && job.type === "detection") {
1730
+ if (job.status === "failed" && job.metadata.type === "detection") {
1679
1731
  const detJob = job;
1680
1732
  await this.eventStore.appendEvent({
1681
1733
  type: "job.failed",
1682
- resourceId: detJob.resourceId,
1683
- userId: detJob.userId,
1734
+ resourceId: detJob.params.resourceId,
1735
+ userId: detJob.metadata.userId,
1684
1736
  version: 1,
1685
1737
  payload: {
1686
- jobId: detJob.id,
1687
- jobType: detJob.type,
1738
+ jobId: detJob.metadata.id,
1739
+ jobType: detJob.metadata.type,
1688
1740
  error: "Entity detection failed. Please try again later."
1689
1741
  }
1690
1742
  });
@@ -1696,18 +1748,18 @@ var ReferenceDetectionWorker = class extends JobWorker5 {
1696
1748
  */
1697
1749
  async updateJobProgress(job) {
1698
1750
  await super.updateJobProgress(job);
1699
- if (job.type !== "detection") {
1751
+ if (job.metadata.type !== "detection") {
1752
+ return;
1753
+ }
1754
+ if (job.status !== "running") {
1700
1755
  return;
1701
1756
  }
1702
1757
  const detJob = job;
1703
1758
  const baseEvent = {
1704
- resourceId: detJob.resourceId,
1705
- userId: detJob.userId,
1759
+ resourceId: detJob.params.resourceId,
1760
+ userId: detJob.metadata.userId,
1706
1761
  version: 1
1707
1762
  };
1708
- if (!detJob.progress) {
1709
- return;
1710
- }
1711
1763
  const isFirstUpdate = detJob.progress.processedEntityTypes === 0;
1712
1764
  const isFinalUpdate = detJob.progress.processedEntityTypes === detJob.progress.totalEntityTypes && detJob.progress.totalEntityTypes > 0;
1713
1765
  if (isFirstUpdate) {
@@ -1715,9 +1767,9 @@ var ReferenceDetectionWorker = class extends JobWorker5 {
1715
1767
  type: "job.started",
1716
1768
  ...baseEvent,
1717
1769
  payload: {
1718
- jobId: detJob.id,
1719
- jobType: detJob.type,
1720
- totalSteps: detJob.entityTypes.length
1770
+ jobId: detJob.metadata.id,
1771
+ jobType: detJob.metadata.type,
1772
+ totalSteps: detJob.params.entityTypes.length
1721
1773
  }
1722
1774
  });
1723
1775
  } else if (isFinalUpdate) {
@@ -1725,8 +1777,8 @@ var ReferenceDetectionWorker = class extends JobWorker5 {
1725
1777
  type: "job.completed",
1726
1778
  ...baseEvent,
1727
1779
  payload: {
1728
- jobId: detJob.id,
1729
- jobType: detJob.type,
1780
+ jobId: detJob.metadata.id,
1781
+ jobType: detJob.metadata.type,
1730
1782
  foundCount: detJob.progress.entitiesFound
1731
1783
  }
1732
1784
  });
@@ -1736,8 +1788,8 @@ var ReferenceDetectionWorker = class extends JobWorker5 {
1736
1788
  type: "job.progress",
1737
1789
  ...baseEvent,
1738
1790
  payload: {
1739
- jobId: detJob.id,
1740
- jobType: detJob.type,
1791
+ jobId: detJob.metadata.id,
1792
+ jobType: detJob.metadata.type,
1741
1793
  percentage,
1742
1794
  currentStep: detJob.progress.currentEntityType,
1743
1795
  processedSteps: detJob.progress.processedEntityTypes,
@@ -1776,88 +1828,103 @@ var GenerationWorker = class extends JobWorker6 {
1776
1828
  return "GenerationWorker";
1777
1829
  }
1778
1830
  canProcessJob(job) {
1779
- return job.type === "generation";
1831
+ return job.metadata.type === "generation";
1780
1832
  }
1781
1833
  async executeJob(job) {
1782
- if (job.type !== "generation") {
1783
- throw new Error(`Invalid job type: ${job.type}`);
1834
+ if (job.metadata.type !== "generation") {
1835
+ throw new Error(`Invalid job type: ${job.metadata.type}`);
1836
+ }
1837
+ if (job.status !== "running") {
1838
+ throw new Error(`Job must be in running state to execute, got: ${job.status}`);
1784
1839
  }
1785
1840
  await this.processGenerationJob(job);
1786
1841
  }
1787
1842
  async processGenerationJob(job) {
1788
- console.log(`[GenerationWorker] Processing generation for reference ${job.referenceId} (job: ${job.id})`);
1843
+ console.log(`[GenerationWorker] Processing generation for reference ${job.params.referenceId} (job: ${job.metadata.id})`);
1789
1844
  const basePath = this.config.services.filesystem.path;
1790
1845
  const projectRoot = this.config._metadata?.projectRoot;
1791
1846
  const repStore = new FilesystemRepresentationStore5({ basePath }, projectRoot);
1792
- job.progress = {
1793
- stage: "fetching",
1794
- percentage: 20,
1795
- message: "Fetching source resource..."
1847
+ let updatedJob = {
1848
+ ...job,
1849
+ progress: {
1850
+ stage: "fetching",
1851
+ percentage: 20,
1852
+ message: "Fetching source resource..."
1853
+ }
1796
1854
  };
1797
- console.log(`[GenerationWorker] \u{1F4E5} ${job.progress.message}`);
1798
- await this.updateJobProgress(job);
1855
+ console.log(`[GenerationWorker] \u{1F4E5} ${updatedJob.progress.message}`);
1856
+ await this.updateJobProgress(updatedJob);
1799
1857
  const { FilesystemViewStorage: FilesystemViewStorage3 } = await import("@semiont/event-sourcing");
1800
1858
  const viewStorage = new FilesystemViewStorage3(basePath, projectRoot);
1801
- const view = await viewStorage.get(job.sourceResourceId);
1859
+ const view = await viewStorage.get(job.params.sourceResourceId);
1802
1860
  if (!view) {
1803
- throw new Error(`Resource ${job.sourceResourceId} not found`);
1861
+ throw new Error(`Resource ${job.params.sourceResourceId} not found`);
1804
1862
  }
1805
1863
  const projection = view.annotations;
1806
- const expectedAnnotationUri = `${this.config.services.backend.publicURL}/annotations/${job.referenceId}`;
1864
+ const expectedAnnotationUri = `${this.config.services.backend.publicURL}/annotations/${job.params.referenceId}`;
1807
1865
  const annotation = projection.annotations.find(
1808
1866
  (a) => a.id === expectedAnnotationUri && a.motivation === "linking"
1809
1867
  );
1810
1868
  if (!annotation) {
1811
- throw new Error(`Annotation ${job.referenceId} not found in resource ${job.sourceResourceId}`);
1869
+ throw new Error(`Annotation ${job.params.referenceId} not found in resource ${job.params.sourceResourceId}`);
1812
1870
  }
1813
- const sourceResource = await ResourceContext.getResourceMetadata(job.sourceResourceId, this.config);
1871
+ const sourceResource = await ResourceContext.getResourceMetadata(job.params.sourceResourceId, this.config);
1814
1872
  if (!sourceResource) {
1815
- throw new Error(`Source resource ${job.sourceResourceId} not found`);
1873
+ throw new Error(`Source resource ${job.params.sourceResourceId} not found`);
1816
1874
  }
1817
1875
  const targetSelector = getTargetSelector2(annotation.target);
1818
- const resourceName = job.title || (targetSelector ? getExactText(targetSelector) : "") || "New Resource";
1876
+ const resourceName = job.params.title || (targetSelector ? getExactText(targetSelector) : "") || "New Resource";
1819
1877
  console.log(`[GenerationWorker] Generating resource: "${resourceName}"`);
1820
- if (!job.context) {
1878
+ if (!job.params.context) {
1821
1879
  throw new Error("Generation context is required but was not provided in job");
1822
1880
  }
1823
- console.log(`[GenerationWorker] Using pre-fetched context: ${job.context.sourceContext?.before?.length || 0} chars before, ${job.context.sourceContext?.selected?.length || 0} chars selected, ${job.context.sourceContext?.after?.length || 0} chars after`);
1824
- job.progress = {
1825
- stage: "generating",
1826
- percentage: 40,
1827
- message: "Creating content with AI..."
1881
+ console.log(`[GenerationWorker] Using pre-fetched context: ${job.params.context.sourceContext?.before?.length || 0} chars before, ${job.params.context.sourceContext?.selected?.length || 0} chars selected, ${job.params.context.sourceContext?.after?.length || 0} chars after`);
1882
+ updatedJob = {
1883
+ ...updatedJob,
1884
+ progress: {
1885
+ stage: "generating",
1886
+ percentage: 40,
1887
+ message: "Creating content with AI..."
1888
+ }
1828
1889
  };
1829
- console.log(`[GenerationWorker] \u{1F916} ${job.progress.message}`);
1830
- await this.updateJobProgress(job);
1831
- const prompt = job.prompt || `Create a comprehensive resource about "${resourceName}"`;
1890
+ console.log(`[GenerationWorker] \u{1F916} ${updatedJob.progress.message}`);
1891
+ await this.updateJobProgress(updatedJob);
1892
+ const prompt = job.params.prompt || `Create a comprehensive resource about "${resourceName}"`;
1832
1893
  const annotationEntityTypes = getEntityTypes2({ body: annotation.body });
1833
1894
  const generatedContent = await generateResourceFromTopic(
1834
1895
  resourceName,
1835
- job.entityTypes || annotationEntityTypes,
1896
+ job.params.entityTypes || annotationEntityTypes,
1836
1897
  this.config,
1837
1898
  prompt,
1838
- job.language,
1839
- job.context,
1899
+ job.params.language,
1900
+ job.params.context,
1840
1901
  // NEW - context from job (passed from modal)
1841
- job.temperature,
1902
+ job.params.temperature,
1842
1903
  // NEW - from job
1843
- job.maxTokens
1904
+ job.params.maxTokens
1844
1905
  // NEW - from job
1845
1906
  );
1846
1907
  console.log(`[GenerationWorker] \u2705 Generated ${generatedContent.content.length} bytes of content`);
1847
- job.progress = {
1848
- stage: "generating",
1849
- percentage: 70,
1850
- message: "Content ready, creating resource..."
1908
+ updatedJob = {
1909
+ ...updatedJob,
1910
+ progress: {
1911
+ stage: "generating",
1912
+ percentage: 70,
1913
+ message: "Content ready, creating resource..."
1914
+ }
1851
1915
  };
1852
- await this.updateJobProgress(job);
1916
+ await this.updateJobProgress(updatedJob);
1853
1917
  const rId = resourceId(generateUuid());
1854
- job.progress = {
1855
- stage: "creating",
1856
- percentage: 85,
1857
- message: "Saving resource..."
1918
+ updatedJob = {
1919
+ ...updatedJob,
1920
+ progress: {
1921
+ stage: "creating",
1922
+ percentage: 85,
1923
+ message: "Saving resource..."
1924
+ }
1858
1925
  };
1859
- console.log(`[GenerationWorker] \u{1F4BE} ${job.progress.message}`);
1860
- await this.updateJobProgress(job);
1926
+ console.log(`[GenerationWorker] \u{1F4BE} ${updatedJob.progress.message}`);
1927
+ await this.updateJobProgress(updatedJob);
1861
1928
  const storedRep = await repStore.store(Buffer.from(generatedContent.content), {
1862
1929
  mediaType: "text/markdown",
1863
1930
  rel: "original"
@@ -1866,29 +1933,34 @@ var GenerationWorker = class extends JobWorker6 {
1866
1933
  await this.eventStore.appendEvent({
1867
1934
  type: "resource.created",
1868
1935
  resourceId: rId,
1869
- userId: job.userId,
1936
+ userId: job.metadata.userId,
1870
1937
  version: 1,
1871
1938
  payload: {
1872
1939
  name: resourceName,
1873
1940
  format: "text/markdown",
1874
1941
  contentChecksum: storedRep.checksum,
1875
1942
  creationMethod: CREATION_METHODS.GENERATED,
1876
- entityTypes: job.entityTypes || annotationEntityTypes,
1877
- language: job.language,
1943
+ entityTypes: job.params.entityTypes || annotationEntityTypes,
1944
+ language: job.params.language,
1878
1945
  isDraft: true,
1879
- generatedFrom: job.referenceId,
1946
+ generatedFrom: job.params.referenceId,
1880
1947
  generationPrompt: void 0
1881
1948
  // Could be added if we track the prompt
1882
1949
  }
1883
1950
  });
1884
1951
  console.log(`[GenerationWorker] Emitted resource.created event for ${rId}`);
1885
- job.progress = {
1886
- stage: "linking",
1887
- percentage: 95,
1888
- message: "Linking reference..."
1952
+ updatedJob = {
1953
+ ...updatedJob,
1954
+ progress: {
1955
+ stage: "linking",
1956
+ percentage: 95,
1957
+ message: "Linking reference...",
1958
+ resultResourceId: rId
1959
+ // Store for job.completed event
1960
+ }
1889
1961
  };
1890
- console.log(`[GenerationWorker] \u{1F517} ${job.progress.message}`);
1891
- await this.updateJobProgress(job);
1962
+ console.log(`[GenerationWorker] \u{1F517} ${updatedJob.progress.message}`);
1963
+ await this.updateJobProgress(updatedJob);
1892
1964
  const newResourceUri = resourceUri(`${this.config.services.backend.publicURL}/resources/${rId}`);
1893
1965
  const operations = [{
1894
1966
  op: "add",
@@ -1898,28 +1970,29 @@ var GenerationWorker = class extends JobWorker6 {
1898
1970
  purpose: "linking"
1899
1971
  }
1900
1972
  }];
1901
- const annotationIdSegment = job.referenceId.split("/").pop();
1973
+ const annotationIdSegment = job.params.referenceId.split("/").pop();
1902
1974
  await this.eventStore.appendEvent({
1903
1975
  type: "annotation.body.updated",
1904
- resourceId: job.sourceResourceId,
1905
- userId: job.userId,
1976
+ resourceId: job.params.sourceResourceId,
1977
+ userId: job.metadata.userId,
1906
1978
  version: 1,
1907
1979
  payload: {
1908
1980
  annotationId: annotationId(annotationIdSegment),
1909
1981
  operations
1910
1982
  }
1911
1983
  });
1912
- console.log(`[GenerationWorker] \u2705 Emitted annotation.body.updated event linking ${job.referenceId} \u2192 ${rId}`);
1913
- job.result = {
1914
- resourceId: rId,
1915
- resourceName
1916
- };
1917
- job.progress = {
1918
- stage: "linking",
1919
- percentage: 100,
1920
- message: "Complete!"
1984
+ console.log(`[GenerationWorker] \u2705 Emitted annotation.body.updated event linking ${job.params.referenceId} \u2192 ${rId}`);
1985
+ updatedJob = {
1986
+ ...updatedJob,
1987
+ progress: {
1988
+ stage: "linking",
1989
+ percentage: 100,
1990
+ message: "Complete!",
1991
+ resultResourceId: rId
1992
+ // Store for job.completed event
1993
+ }
1921
1994
  };
1922
- await this.updateJobProgress(job);
1995
+ await this.updateJobProgress(updatedJob);
1923
1996
  console.log(`[GenerationWorker] \u2705 Generation complete: created resource ${rId}`);
1924
1997
  }
1925
1998
  /**
@@ -1928,44 +2001,47 @@ var GenerationWorker = class extends JobWorker6 {
1928
2001
  */
1929
2002
  async updateJobProgress(job) {
1930
2003
  await super.updateJobProgress(job);
1931
- if (job.type !== "generation") {
2004
+ if (job.metadata.type !== "generation") {
2005
+ return;
2006
+ }
2007
+ if (job.status !== "running") {
1932
2008
  return;
1933
2009
  }
1934
2010
  const genJob = job;
1935
2011
  const baseEvent = {
1936
- resourceId: genJob.sourceResourceId,
1937
- userId: genJob.userId,
2012
+ resourceId: genJob.params.sourceResourceId,
2013
+ userId: genJob.metadata.userId,
1938
2014
  version: 1
1939
2015
  };
1940
- if (genJob.progress?.stage === "fetching" && genJob.progress?.percentage === 20) {
2016
+ if (genJob.progress.stage === "fetching" && genJob.progress.percentage === 20) {
1941
2017
  await this.eventStore.appendEvent({
1942
2018
  type: "job.started",
1943
2019
  ...baseEvent,
1944
2020
  payload: {
1945
- jobId: genJob.id,
1946
- jobType: genJob.type,
2021
+ jobId: genJob.metadata.id,
2022
+ jobType: genJob.metadata.type,
1947
2023
  totalSteps: 5
1948
2024
  // fetching, generating, creating, linking, complete
1949
2025
  }
1950
2026
  });
1951
- } else if (genJob.progress?.stage === "linking" && genJob.progress?.percentage === 100) {
2027
+ } else if (genJob.progress.stage === "linking" && genJob.progress.percentage === 100) {
1952
2028
  await this.eventStore.appendEvent({
1953
2029
  type: "job.completed",
1954
2030
  ...baseEvent,
1955
2031
  payload: {
1956
- jobId: genJob.id,
1957
- jobType: genJob.type,
1958
- resultResourceId: genJob.result?.resourceId,
1959
- annotationUri: annotationUri(`${this.config.services.backend.publicURL}/annotations/${genJob.referenceId}`)
2032
+ jobId: genJob.metadata.id,
2033
+ jobType: genJob.metadata.type,
2034
+ resultResourceId: genJob.progress.resultResourceId,
2035
+ annotationUri: annotationUri(`${this.config.services.backend.publicURL}/annotations/${genJob.params.referenceId}`)
1960
2036
  }
1961
2037
  });
1962
- } else if (genJob.progress) {
2038
+ } else {
1963
2039
  await this.eventStore.appendEvent({
1964
2040
  type: "job.progress",
1965
2041
  ...baseEvent,
1966
2042
  payload: {
1967
- jobId: genJob.id,
1968
- jobType: genJob.type,
2043
+ jobId: genJob.metadata.id,
2044
+ jobType: genJob.metadata.type,
1969
2045
  currentStep: genJob.progress.stage,
1970
2046
  percentage: genJob.progress.percentage,
1971
2047
  message: genJob.progress.message