Creating Plot Twists
    Preparing search index...

    Class Twist<TSelf>Abstract

    Base class for all twists.

    Twists are activated in a Plot priority and have access to that priority and all its descendants.

    Override build() to declare tool dependencies and lifecycle methods to handle events.

    class FlatteringTwist extends Twist<FlatteringTwist> {
    build(build: ToolBuilder) {
    return {
    plot: build(Plot),
    };
    }

    async activate(priority: Pick<Priority, "id">) {
    // Initialize twist for the given priority
    await this.tools.plot.createActivity({
    type: ActivityType.Note,
    note: "Hello, good looking!",
    });
    }
    }

    Type Parameters

    • TSelf
    Index

    Accessors

    Constructors

    Methods

    • Declares tool dependencies for this twist. Return an object mapping tool names to build() promises.

      Parameters

      • build: ToolBuilder

        The build function to use for declaring dependencies

      Returns Record<string, Promise<ITool>>

      Object mapping tool names to tool promises

      build(build: ToolBuilder) {
      return {
      plot: build(Plot),
      calendar: build(GoogleCalendar, { apiKey: "..." }),
      };
      }
    • Creates a persistent callback to a method on this twist.

      ExtraArgs are strongly typed to match the method's signature after the first argument.

      Parameters

      • fn: Function

        The method to callback

      • ...extraArgs: any[]

        Additional arguments to pass (type-checked, must be serializable)

      Returns Promise<Callback>

      Promise resolving to a persistent callback token

      const callback = await this.callback(this.onWebhook, "calendar", 123);
      
    • Deletes a specific callback by its token.

      Parameters

      • token: Callback

        The callback token to delete

      Returns Promise<void>

      Promise that resolves when the callback is deleted

    • Deletes all callbacks for this twist.

      Returns Promise<void>

      Promise that resolves when all callbacks are deleted

    • Executes a callback by its token.

      Parameters

      • token: Callback

        The callback token to execute

      • ...args: []

        Optional arguments to pass to the callback

      Returns Promise<any>

      Promise resolving to the callback result

    • Retrieves a value from persistent storage by key.

      Type Parameters

      • T

        The expected type of the stored value

      Parameters

      • key: string

        The storage key to retrieve

      Returns Promise<T | null>

      Promise resolving to the stored value or null

    • Stores a value in persistent storage.

      Important: Values must be JSON-serializable. Functions, Symbols, and undefined values cannot be stored directly.

      For function references: Use callbacks instead of storing functions directly.

      Type Parameters

      • T

        The type of value being stored

      Parameters

      • key: string

        The storage key to use

      • value: T

        The value to store (must be JSON-serializable)

      Returns Promise<void>

      Promise that resolves when the value is stored

      // ❌ WRONG: Cannot store functions directly
      await this.set("handler", this.myHandler);

      // ✅ CORRECT: Create a callback token first
      const token = await this.callback(this.myHandler, "arg1", "arg2");
      await this.set("handler_token", token);

      // Later, execute the callback
      const token = await this.get<string>("handler_token");
      await this.run(token, args);
    • Removes a specific key from persistent storage.

      Parameters

      • key: string

        The storage key to remove

      Returns Promise<void>

      Promise that resolves when the key is removed

    • Removes all keys from this twist's storage.

      Returns Promise<void>

      Promise that resolves when all keys are removed

    • Queues a callback to execute in a separate worker context.

      Parameters

      • callback: Callback

        The callback token created with this.callback()

      • Optionaloptions: { runAt?: Date }

        Optional configuration for the execution

        • OptionalrunAt?: Date

          If provided, schedules execution at this time; otherwise runs immediately

      Returns Promise<string | void>

      Promise resolving to a cancellation token (only for scheduled executions)

    • Cancels a previously scheduled execution.

      Parameters

      • token: string

        The cancellation token returned by runTask() with runAt option

      Returns Promise<void>

      Promise that resolves when the cancellation is processed

    • Cancels all scheduled executions for this twist.

      Returns Promise<void>

      Promise that resolves when all cancellations are processed

    • Called when the twist is activated for a specific priority.

      This method should contain initialization logic such as setting up initial activities, configuring webhooks, or establishing external connections.

      Parameters

      • priority: Pick<Priority, "id">

        The priority context containing the priority ID

      Returns Promise<void>

      Promise that resolves when activation is complete

    • Called when a new version of the twist is deployed to an existing priority.

      This method should contain migration logic for updating old data structures or setting up new resources that weren't needed by the previous version. It is called with the new version for each active priorityTwist.

      Returns Promise<void>

      Promise that resolves when upgrade is complete

    • Called when the twist is removed from a priority.

      This method should contain cleanup logic such as removing webhooks, cleaning up external resources, or performing final data operations.

      Returns Promise<void>

      Promise that resolves when deactivation is complete

    Properties

    id: string