@semiont/jobs 0.4.20 → 0.4.22

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 CHANGED
@@ -6,13 +6,11 @@
6
6
  [![npm downloads](https://img.shields.io/npm/dm/@semiont/jobs.svg)](https://www.npmjs.com/package/@semiont/jobs)
7
7
  [![License](https://img.shields.io/npm/l/@semiont/jobs.svg)](https://github.com/The-AI-Alliance/semiont/blob/main/LICENSE)
8
8
 
9
- Filesystem-based job queue, worker infrastructure, and annotation workers for [Semiont](https://github.com/The-AI-Alliance/semiont).
9
+ Job queue, worker infrastructure, and annotation workers for [Semiont](https://github.com/The-AI-Alliance/semiont).
10
10
 
11
11
  ## Architecture Context
12
12
 
13
- In production, the job queue and workers are created by `@semiont/make-meaning`'s `startMakeMeaning()` function. Workers emit commands on the **EventBus** the **Stower** actor (in @semiont/make-meaning) handles all persistence to the Knowledge Base.
14
-
15
- Workers are **not** actors. They use a polling loop, not RxJS subscriptions. But they emit the same EventBus commands as any other caller in the system.
13
+ Workers run in a separate separate process and connect to the Knowledge System (KS) over HTTP/SSE using `WorkerVM` from `@semiont/api-client`. Workers receive job assignments via SSE push, claim jobs atomically, and emit domain events back to the KS via HTTP. The KS ingests these events onto its EventBus for SSE delivery to the frontend.
16
14
 
17
15
  ## Installation
18
16
 
package/dist/index.d.ts CHANGED
@@ -1,8 +1,8 @@
1
1
  import { Readable } from 'stream';
2
- import { ResourceId, JobId, UserId, EntityType, AnnotationId, components, GatheredContext, Logger, EventBus } from '@semiont/core';
2
+ import * as _semiont_core from '@semiont/core';
3
+ import { ResourceId, JobId, UserId, EntityType, AnnotationId, Annotation, GatheredContext, Logger, EventBus, components } from '@semiont/core';
3
4
  import { SemiontProject } from '@semiont/core/node';
4
5
  import { InferenceClient } from '@semiont/inference';
5
- import { WorkingTreeStore } from '@semiont/content';
6
6
 
7
7
  /**
8
8
  * Job Queue Type Definitions - Discriminated Union Design
@@ -17,7 +17,6 @@ import { WorkingTreeStore } from '@semiont/content';
17
17
  * - State machine is explicit and type-safe
18
18
  */
19
19
 
20
- type Annotation = components['schemas']['Annotation'];
21
20
  /**
22
21
  * Content fetcher - turns a ResourceId into a readable stream.
23
22
  * Workers use this to access resource content on demand.
@@ -275,6 +274,23 @@ interface JobQueryFilters {
275
274
  offset?: number;
276
275
  }
277
276
 
277
+ interface JobQueue {
278
+ initialize(): Promise<void>;
279
+ destroy(): void;
280
+ createJob(job: AnyJob): Promise<void>;
281
+ getJob(jobId: JobId): Promise<AnyJob | null>;
282
+ updateJob(job: AnyJob, oldStatus?: JobStatus): Promise<void>;
283
+ pollNextPendingJob(predicate?: (job: AnyJob) => boolean): Promise<AnyJob | null>;
284
+ cancelJob(jobId: JobId): Promise<boolean>;
285
+ getStats(): Promise<{
286
+ pending: number;
287
+ running: number;
288
+ complete: number;
289
+ failed: number;
290
+ cancelled: number;
291
+ }>;
292
+ }
293
+
278
294
  /**
279
295
  * Job Queue Manager
280
296
  *
@@ -282,7 +298,7 @@ interface JobQueryFilters {
282
298
  * Jobs are stored in directories by status for easy polling.
283
299
  */
284
300
 
285
- declare class JobQueue {
301
+ declare class FsJobQueue implements JobQueue {
286
302
  private eventBus?;
287
303
  private jobsDir;
288
304
  private logger;
@@ -416,231 +432,32 @@ declare abstract class JobWorker {
416
432
  protected abstract executeJob(job: AnyJob): Promise<any>;
417
433
  }
418
434
 
435
+ type Agent = components['schemas']['Agent'];
419
436
  /**
420
- * Reference Detection Worker
421
- *
422
- * Processes detection jobs: runs AI inference to find entities in resources
423
- * and creates annotations for each detected entity via the EventBus.
424
- *
425
- * This worker is INDEPENDENT of HTTP clients - it just processes jobs and
426
- * emits events on the EventBus for all writes.
427
- */
428
-
429
- type Agent$5 = components['schemas']['Agent'];
430
- interface DetectedAnnotation {
431
- annotation: {
432
- selector: {
433
- start: number;
434
- end: number;
435
- exact: string;
436
- prefix?: string;
437
- suffix?: string;
438
- };
439
- entityTypes: string[];
440
- };
441
- }
442
- declare class ReferenceAnnotationWorker extends JobWorker {
443
- private inferenceClient;
444
- private generator;
445
- private eventBus;
446
- private contentFetcher;
447
- constructor(jobQueue: JobQueue, inferenceClient: InferenceClient, generator: Agent$5, eventBus: EventBus, contentFetcher: ContentFetcher, logger: Logger);
448
- protected getWorkerName(): string;
449
- protected canProcessJob(job: AnyJob): boolean;
450
- protected executeJob(job: AnyJob): Promise<DetectionResult>;
451
- /**
452
- * Detect entity references in content using AI
453
- *
454
- * Public for testing charset handling - see entity-detection-charset.test.ts
455
- */
456
- detectReferences(content: string, entityTypes: string[], includeDescriptiveReferences?: boolean): Promise<DetectedAnnotation[]>;
457
- private processDetectionJob;
458
- /**
459
- * Emit completion event with result data
460
- * Override base class to emit on EventBus
461
- */
462
- protected emitCompletionEvent(job: RunningJob<DetectionParams, DetectionProgress>, result: DetectionResult): Promise<void>;
463
- protected handleJobFailure(job: AnyJob, error: any): Promise<void>;
464
- /**
465
- * Update job progress and emit ephemeral events via EventBus
466
- * Overrides base class to emit job lifecycle events and mark:progress events
467
- */
468
- protected updateJobProgress(job: AnyJob): Promise<void>;
469
- }
470
-
471
- /**
472
- * Generation Worker
473
- *
474
- * Processes generation jobs: runs AI inference to generate new resources
475
- * and emits events on the EventBus for all writes.
476
- *
477
- * This worker is INDEPENDENT of HTTP clients - it just processes jobs and
478
- * emits events on the EventBus for all writes.
479
- */
480
-
481
- type Agent$4 = components['schemas']['Agent'];
482
- declare class GenerationWorker extends JobWorker {
483
- private inferenceClient;
484
- private generator;
485
- private eventBus;
486
- private contentStore;
487
- constructor(jobQueue: JobQueue, inferenceClient: InferenceClient, generator: Agent$4, eventBus: EventBus, contentStore: WorkingTreeStore, logger: Logger);
488
- protected getWorkerName(): string;
489
- protected canProcessJob(job: AnyJob): boolean;
490
- protected executeJob(job: AnyJob): Promise<GenerationResult>;
491
- private processGenerationJob;
492
- /**
493
- * Emit completion event with result data
494
- * Override base class to emit on EventBus
495
- */
496
- protected emitCompletionEvent(job: RunningJob<GenerationParams, YieldProgress>, result: GenerationResult): Promise<void>;
497
- protected handleJobFailure(job: AnyJob, error: any): Promise<void>;
498
- /**
499
- * Update job progress and emit ephemeral events via EventBus
500
- * Overrides base class to emit job lifecycle events and yield:progress events
501
- */
502
- protected updateJobProgress(job: AnyJob): Promise<void>;
503
- }
504
-
505
- /**
506
- * Highlight Detection Worker
507
- *
508
- * Processes highlight-detection jobs: runs AI inference to find passages
509
- * that should be highlighted and creates highlight annotations.
510
- */
511
-
512
- type Agent$3 = components['schemas']['Agent'];
513
- declare class HighlightAnnotationWorker extends JobWorker {
514
- private inferenceClient;
515
- private generator;
516
- private eventBus;
517
- private contentFetcher;
518
- private isFirstProgress;
519
- constructor(jobQueue: JobQueue, inferenceClient: InferenceClient, generator: Agent$3, eventBus: EventBus, contentFetcher: ContentFetcher, logger: Logger);
520
- protected getWorkerName(): string;
521
- protected canProcessJob(job: AnyJob): boolean;
522
- protected executeJob(job: AnyJob): Promise<HighlightDetectionResult>;
523
- /**
524
- * Emit completion event with result data
525
- * Override base class to emit on EventBus
526
- */
527
- protected emitCompletionEvent(job: RunningJob<HighlightDetectionParams, HighlightDetectionProgress>, result: HighlightDetectionResult): Promise<void>;
528
- /**
529
- * Override updateJobProgress to emit events via EventBus
530
- */
531
- protected updateJobProgress(job: AnyJob): Promise<void>;
532
- protected handleJobFailure(job: AnyJob, error: any): Promise<void>;
533
- private processHighlightDetectionJob;
534
- private createHighlightAnnotation;
535
- }
536
-
537
- /**
538
- * Assessment Detection Worker
539
- *
540
- * Processes assessment-detection jobs: runs AI inference to assess/evaluate
541
- * passages in the text and creates assessment annotations.
542
- */
543
-
544
- type Agent$2 = components['schemas']['Agent'];
545
- declare class AssessmentAnnotationWorker extends JobWorker {
546
- private inferenceClient;
547
- private generator;
548
- private eventBus;
549
- private contentFetcher;
550
- private isFirstProgress;
551
- constructor(jobQueue: JobQueue, inferenceClient: InferenceClient, generator: Agent$2, eventBus: EventBus, contentFetcher: ContentFetcher, logger: Logger);
552
- protected getWorkerName(): string;
553
- protected canProcessJob(job: AnyJob): boolean;
554
- protected executeJob(job: AnyJob): Promise<AssessmentDetectionResult>;
555
- /**
556
- * Emit completion event with result data
557
- * Override base class to emit on EventBus
558
- */
559
- protected emitCompletionEvent(job: RunningJob<AssessmentDetectionParams, AssessmentDetectionProgress>, result: AssessmentDetectionResult): Promise<void>;
560
- /**
561
- * Override updateJobProgress to emit events via EventBus
562
- */
563
- protected updateJobProgress(job: AnyJob): Promise<void>;
564
- protected handleJobFailure(job: AnyJob, error: any): Promise<void>;
565
- private processAssessmentDetectionJob;
566
- private createAssessmentAnnotation;
567
- }
568
-
569
- /**
570
- * Comment Detection Worker
571
- *
572
- * Processes comment-detection jobs: runs AI inference to identify passages
573
- * that would benefit from explanatory comments and creates comment annotations.
574
- */
575
-
576
- type Agent$1 = components['schemas']['Agent'];
577
- declare class CommentAnnotationWorker extends JobWorker {
578
- private inferenceClient;
579
- private generator;
580
- private eventBus;
581
- private contentFetcher;
582
- private isFirstProgress;
583
- constructor(jobQueue: JobQueue, inferenceClient: InferenceClient, generator: Agent$1, eventBus: EventBus, contentFetcher: ContentFetcher, logger: Logger);
584
- protected getWorkerName(): string;
585
- protected canProcessJob(job: AnyJob): boolean;
586
- protected executeJob(job: AnyJob): Promise<CommentDetectionResult>;
587
- /**
588
- * Emit completion event with result data
589
- * Override base class to emit on EventBus
590
- */
591
- protected emitCompletionEvent(job: RunningJob<CommentDetectionParams, CommentDetectionProgress>, result: CommentDetectionResult): Promise<void>;
592
- /**
593
- * Override updateJobProgress to emit events via EventBus
594
- */
595
- protected updateJobProgress(job: AnyJob): Promise<void>;
596
- protected handleJobFailure(job: AnyJob, error: any): Promise<void>;
597
- private processCommentDetectionJob;
598
- private createCommentAnnotation;
599
- }
600
-
601
- /**
602
- * Tag Detection Worker
603
- *
604
- * Processes tag-detection jobs: runs AI inference to identify passages
605
- * serving specific structural roles (IRAC, IMRAD, Toulmin, etc.) and
606
- * creates tag annotations with dual-body structure.
437
+ * Progress callback. The three positional args satisfy the minimum
438
+ * `JobProgress` required fields (`percentage`, `message`, `stage`).
439
+ * The fourth optional arg carries job-type-specific fields
440
+ * (`currentEntityType`, `completedEntityTypes`, `requestParams`, etc.)
441
+ * that the progress UI renders.
607
442
  */
608
-
609
- type Agent = components['schemas']['Agent'];
610
- declare class TagAnnotationWorker extends JobWorker {
611
- private inferenceClient;
612
- private generator;
613
- private eventBus;
614
- private contentFetcher;
615
- private isFirstProgress;
616
- constructor(jobQueue: JobQueue, inferenceClient: InferenceClient, generator: Agent, eventBus: EventBus, contentFetcher: ContentFetcher, logger: Logger);
617
- protected getWorkerName(): string;
618
- protected canProcessJob(job: AnyJob): boolean;
619
- protected executeJob(job: AnyJob): Promise<TagDetectionResult>;
620
- /**
621
- * Emit completion event with result data
622
- * Override base class to emit on EventBus
623
- */
624
- protected emitCompletionEvent(job: RunningJob<TagDetectionParams, TagDetectionProgress>, result: TagDetectionResult): Promise<void>;
625
- /**
626
- * Override updateJobProgress to emit events via EventBus
627
- */
628
- protected updateJobProgress(job: AnyJob): Promise<void>;
629
- protected handleJobFailure(job: AnyJob, error: any): Promise<void>;
630
- private processTagDetectionJob;
631
- private createTagAnnotation;
443
+ type OnProgress = (percentage: number, message: string, stage: string, extra?: Partial<JobProgress>) => void;
444
+ type JobProgress = components['schemas']['JobProgress'];
445
+ interface ProcessorResult<R> {
446
+ annotations: Record<string, unknown>[];
447
+ result: R;
632
448
  }
449
+ declare function processHighlightJob(content: string, inferenceClient: InferenceClient, params: HighlightDetectionParams, userId: string, generator: Agent, onProgress: OnProgress): Promise<ProcessorResult<HighlightDetectionResult>>;
450
+ declare function processCommentJob(content: string, inferenceClient: InferenceClient, params: CommentDetectionParams, userId: string, generator: Agent, onProgress: OnProgress): Promise<ProcessorResult<CommentDetectionResult>>;
451
+ declare function processAssessmentJob(content: string, inferenceClient: InferenceClient, params: AssessmentDetectionParams, userId: string, generator: Agent, onProgress: OnProgress): Promise<ProcessorResult<AssessmentDetectionResult>>;
452
+ declare function processReferenceJob(content: string, inferenceClient: InferenceClient, params: DetectionParams, userId: string, generator: Agent, onProgress: OnProgress, logger?: _semiont_core.Logger): Promise<ProcessorResult<DetectionResult>>;
453
+ declare function processTagJob(content: string, inferenceClient: InferenceClient, params: TagDetectionParams, userId: string, generator: Agent, onProgress: OnProgress): Promise<ProcessorResult<TagDetectionResult>>;
454
+ declare function processGenerationJob(inferenceClient: InferenceClient, params: GenerationParams, onProgress: OnProgress): Promise<{
455
+ content: string;
456
+ title: string;
457
+ format: string;
458
+ result: GenerationResult;
459
+ }>;
633
460
 
634
- /**
635
- * Response parsers for annotation detection motivations
636
- *
637
- * Provides static methods to parse and validate AI responses for each motivation type.
638
- * Includes offset validation and correction logic.
639
- * Extracted from worker implementations to centralize parsing logic.
640
- *
641
- * NOTE: These are static utility methods without logger access.
642
- * Console statements kept for debugging - consider adding logger parameter in future.
643
- */
644
461
  /**
645
462
  * Represents a detected comment with validated position
646
463
  */
@@ -735,4 +552,4 @@ declare function generateResourceFromTopic(topic: string, entityTypes: string[],
735
552
  content: string;
736
553
  }>;
737
554
 
738
- export { AnnotationDetection, type AnyJob, AssessmentAnnotationWorker, type AssessmentDetectionJob, type AssessmentDetectionParams, type AssessmentDetectionProgress, type AssessmentDetectionResult, type CancelledJob, CommentAnnotationWorker, type CommentDetectionJob, type CommentDetectionParams, type CommentDetectionProgress, type CommentDetectionResult, type CompleteJob, type ContentFetcher, type DetectionJob, type DetectionParams, type DetectionProgress, type DetectionResult, type FailedJob, type GenerationJob, type GenerationParams, type GenerationResult, GenerationWorker, HighlightAnnotationWorker, type HighlightDetectionJob, type HighlightDetectionParams, type HighlightDetectionProgress, type HighlightDetectionResult, type JobMetadata, type JobQueryFilters, JobQueue, type JobStatus, type JobType, JobWorker, type PendingJob, ReferenceAnnotationWorker, type RunningJob, TagAnnotationWorker, type TagDetectionJob, type TagDetectionParams, type TagDetectionProgress, type TagDetectionResult, type YieldProgress, generateResourceFromTopic, isCancelledJob, isCompleteJob, isFailedJob, isPendingJob, isRunningJob };
555
+ export { AnnotationDetection, type AnyJob, type AssessmentDetectionJob, type AssessmentDetectionParams, type AssessmentDetectionProgress, type AssessmentDetectionResult, type CancelledJob, type CommentDetectionJob, type CommentDetectionParams, type CommentDetectionProgress, type CommentDetectionResult, type CompleteJob, type ContentFetcher, type DetectionJob, type DetectionParams, type DetectionProgress, type DetectionResult, type FailedJob, FsJobQueue, type GenerationJob, type GenerationParams, type GenerationResult, type HighlightDetectionJob, type HighlightDetectionParams, type HighlightDetectionProgress, type HighlightDetectionResult, type JobMetadata, type JobQueryFilters, type JobQueue, type JobStatus, type JobType, JobWorker, type OnProgress, type PendingJob, type ProcessorResult, type RunningJob, type TagDetectionJob, type TagDetectionParams, type TagDetectionProgress, type TagDetectionResult, type YieldProgress, generateResourceFromTopic, isCancelledJob, isCompleteJob, isFailedJob, isPendingJob, isRunningJob, processAssessmentJob, processCommentJob, processGenerationJob, processHighlightJob, processReferenceJob, processTagJob };