ogi-addon 2.0.0 → 2.1.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/src/main.ts CHANGED
@@ -427,15 +427,13 @@ export default class OGIAddon {
427
427
 
428
428
  /**
429
429
  * Notify the OGI Addon Server that you are performing a background task. This can be used to help users understand what is happening in the background.
430
- * @param id {string}
431
- * @param progress {number}
432
- * @param logs {string[]}
430
+ * @returns {Promise<Task>} A Task instance for managing the background task.
433
431
  */
434
- public async task() {
432
+ public async task(): Promise<Task> {
435
433
  const id = Math.random().toString(36).substring(7);
436
434
  const progress = 0;
437
435
  const logs: string[] = [];
438
- const task = new CustomTask(this.addonWSListener, id, progress, logs);
436
+ const task = new Task(this.addonWSListener, id, progress, logs);
439
437
  this.addonWSListener.send('task-update', {
440
438
  id,
441
439
  progress,
@@ -620,61 +618,55 @@ export default class OGIAddon {
620
618
  }
621
619
  }
622
620
 
623
- export class CustomTask {
624
- public readonly id: string;
625
- public progress: number;
626
- public logs: string[];
627
- public finished: boolean = false;
628
- public ws: OGIAddonWSListener;
629
- public failed: string | undefined = undefined;
630
- constructor(
631
- ws: OGIAddonWSListener,
632
- id: string,
633
- progress: number,
634
- logs: string[]
635
- ) {
636
- this.id = id;
637
- this.progress = progress;
638
- this.logs = logs;
639
- this.ws = ws;
640
- }
641
- public log(log: string) {
642
- this.logs.push(log);
643
- this.update();
644
- }
645
- public finish() {
646
- this.finished = true;
647
- this.update();
648
- }
649
- public fail(message: string) {
650
- this.failed = message;
651
- this.update();
652
- }
653
- public setProgress(progress: number) {
654
- this.progress = progress;
655
- this.update();
656
- }
657
- public update() {
658
- this.ws.send('task-update', {
659
- id: this.id,
660
- progress: this.progress,
661
- logs: this.logs,
662
- finished: this.finished,
663
- failed: this.failed,
664
- });
665
- }
666
- }
667
-
668
621
  /**
669
- * A cleaner API wrapper around EventResponse for task handlers.
622
+ * A unified task API for both server-initiated tasks (via onTask handlers)
623
+ * and addon-initiated background tasks (via addon.task()).
670
624
  * Provides chainable methods for logging, progress updates, and completion.
671
625
  */
672
626
  export class Task {
673
- private event: EventResponse<void>;
627
+ // EventResponse-based mode (for onTask handlers)
628
+ private event: EventResponse<void> | undefined;
629
+
630
+ // WebSocket-based mode (for addon.task())
631
+ private ws: OGIAddonWSListener | undefined;
632
+ private readonly id: string | undefined;
633
+ private progress: number = 0;
634
+ private logs: string[] = [];
635
+ private finished: boolean = false;
636
+ private failed: string | undefined = undefined;
674
637
 
675
- constructor(event: EventResponse<void>) {
676
- this.event = event;
677
- this.event.defer();
638
+ /**
639
+ * Construct a Task from an EventResponse (for onTask handlers).
640
+ * @param event {EventResponse<void>} The event response to wrap.
641
+ */
642
+ constructor(event: EventResponse<void>);
643
+
644
+ /**
645
+ * Construct a Task from WebSocket listener (for addon.task()).
646
+ * @param ws {OGIAddonWSListener} The WebSocket listener.
647
+ * @param id {string} The task ID.
648
+ * @param progress {number} Initial progress (0-100).
649
+ * @param logs {string[]} Initial logs array.
650
+ */
651
+ constructor(ws: OGIAddonWSListener, id: string, progress: number, logs: string[]);
652
+
653
+ constructor(
654
+ eventOrWs: EventResponse<void> | OGIAddonWSListener,
655
+ id?: string,
656
+ progress?: number,
657
+ logs?: string[]
658
+ ) {
659
+ if (eventOrWs instanceof EventResponse) {
660
+ // EventResponse-based mode
661
+ this.event = eventOrWs;
662
+ this.event.defer();
663
+ } else {
664
+ // WebSocket-based mode
665
+ this.ws = eventOrWs;
666
+ this.id = id!;
667
+ this.progress = progress ?? 0;
668
+ this.logs = logs ?? [];
669
+ }
678
670
  }
679
671
 
680
672
  /**
@@ -682,7 +674,12 @@ export class Task {
682
674
  * @param message {string} The message to log.
683
675
  */
684
676
  log(message: string): this {
685
- this.event.log(message);
677
+ if (this.event) {
678
+ this.event.log(message);
679
+ } else {
680
+ this.logs.push(message);
681
+ this.update();
682
+ }
686
683
  return this;
687
684
  }
688
685
 
@@ -691,7 +688,12 @@ export class Task {
691
688
  * @param progress {number} The progress value (0-100).
692
689
  */
693
690
  setProgress(progress: number): this {
694
- this.event.progress = progress;
691
+ if (this.event) {
692
+ this.event.progress = progress;
693
+ } else {
694
+ this.progress = progress;
695
+ this.update();
696
+ }
695
697
  return this;
696
698
  }
697
699
 
@@ -699,7 +701,12 @@ export class Task {
699
701
  * Complete the task successfully.
700
702
  */
701
703
  complete(): void {
702
- this.event.complete();
704
+ if (this.event) {
705
+ this.event.complete();
706
+ } else {
707
+ this.finished = true;
708
+ this.update();
709
+ }
703
710
  }
704
711
 
705
712
  /**
@@ -707,24 +714,50 @@ export class Task {
707
714
  * @param message {string} The error message.
708
715
  */
709
716
  fail(message: string): void {
710
- this.event.fail(message);
717
+ if (this.event) {
718
+ this.event.fail(message);
719
+ } else {
720
+ this.failed = message;
721
+ this.update();
722
+ }
711
723
  }
712
724
 
713
725
  /**
714
726
  * Ask the user for input using a ConfigurationBuilder screen.
727
+ * Only available for EventResponse-based tasks (onTask handlers).
715
728
  * The return type is inferred from the ConfigurationBuilder's accumulated option types.
716
729
  * @param name {string} The name/title of the input prompt.
717
730
  * @param description {string} The description of what input is needed.
718
731
  * @param screen {ConfigurationBuilder<U>} The configuration builder for the input form.
719
732
  * @returns {Promise<U>} The user's input with types matching the configuration options.
733
+ * @throws {Error} If called on a WebSocket-based task.
720
734
  */
721
735
  async askForInput<U extends Record<string, string | number | boolean>>(
722
736
  name: string,
723
737
  description: string,
724
738
  screen: ConfigurationBuilder<U>
725
739
  ): Promise<U> {
740
+ if (!this.event) {
741
+ throw new Error('askForInput() is only available for EventResponse-based tasks (onTask handlers)');
742
+ }
726
743
  return this.event.askForInput(name, description, screen);
727
744
  }
745
+
746
+ /**
747
+ * Update the task state (for WebSocket-based tasks only).
748
+ * Called automatically when using log(), setProgress(), complete(), or fail().
749
+ */
750
+ private update(): void {
751
+ if (this.ws && this.id !== undefined) {
752
+ this.ws.send('task-update', {
753
+ id: this.id,
754
+ progress: this.progress,
755
+ logs: this.logs,
756
+ finished: this.finished,
757
+ failed: this.failed,
758
+ });
759
+ }
760
+ }
728
761
  }
729
762
  /**
730
763
  * A search tool wrapper over Fuse.js for the OGI Addon. This tool is used to search for items in the library.