Creating Plot Twists
    Preparing search index...

    Class Tool<TSelf>Abstract

    Base class for regular tools.

    Regular tools run in isolation and can only access other tools declared in their build method. They are ideal for external API integrations and reusable functionality that doesn't require Plot's internal infrastructure.

    class GoogleCalendarTool extends Tool<GoogleCalendarTool> {
    constructor(id: string, options: { clientId: string }) {
    super(id, options);
    }

    build(tools: ToolBuilder) {
    return {
    auth: tools.build(Integrations),
    network: tools.build(Network),
    };
    }

    async getCalendars() {
    const token = await this.tools.auth.get(...);
    // Implementation
    }
    }

    Type Parameters

    • TSelf

    Implements

    Index

    Accessors

    Constructors

    Methods

    • Declares tool dependencies for this tool. Return an object mapping tool names to build() promises. Default implementation returns empty object (no custom tools).

      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 {
      network: build(Network, { urls: ["https://api.example.com/*"] }),
      };
      }
    • Creates a persistent callback to a method on this tool.

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

      Type Parameters

      Parameters

      • fn: Fn

        The method to callback

      • ...extraArgs: TArgs

        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 tool.

      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: any[]

        Optional arguments to pass to the callback

      Returns Promise<any>

      Promise resolving to the callback result

    • Retrieves a value from persistent storage by key.

      Values are automatically deserialized using SuperJSON, which properly restores Date objects, Maps, Sets, and other complex types.

      Type Parameters

      • T extends Serializable

        The expected type of the stored value (must be Serializable)

      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.

      The value will be serialized using SuperJSON and stored persistently. SuperJSON automatically handles Date objects, Maps, Sets, undefined values, and other complex types that standard JSON doesn't support.

      Important: Functions and Symbols cannot be stored. For function references: Use callbacks instead of storing functions directly.

      Type Parameters

      • T extends Serializable

        The type of value being stored (must be Serializable)

      Parameters

      • key: string

        The storage key to use

      • value: T

        The value to store (must be SuperJSON-serializable)

      Returns Promise<void>

      Promise that resolves when the value is stored

      // ✅ Date objects are preserved
      await this.set("sync_state", {
      lastSync: new Date(),
      minDate: new Date(2024, 0, 1)
      });

      // ✅ undefined is now supported
      await this.set("data", { name: "test", optional: undefined });

      // ✅ Arrays with undefined are supported
      await this.set("items", [1, undefined, 3]);
      await this.set("items", [1, null, 3]); // Also works

      // ✅ Maps and Sets are supported
      await this.set("mapping", new Map([["key", "value"]]));
      await this.set("tags", new Set(["tag1", "tag2"]));

      // ❌ 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);
    • Lists all storage keys matching a prefix.

      Parameters

      • prefix: string

        The prefix to match keys against

      Returns Promise<string[]>

      Promise resolving to an array of matching key strings

    • 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 tool's storage.

      Returns Promise<void>

      Promise that resolves when all keys are removed

    • Queues a callback to execute in a separate worker context with a fresh request limit.

      Creates a NEW execution with its own request limit of ~1000 requests (HTTP requests, tool calls, database operations). This is the primary way to stay under request limits when processing large datasets or making many API calls.

      Use this to break long loops into chunks that each stay under the ~1000 request limit. Each task runs in an isolated execution environment with ~1000 requests and ~60 seconds CPU time.

      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)

      // Break large loop into batches
      const callback = await this.callback("processBatch", { page: 1 });
      await this.runTask(callback); // New execution with fresh request limit
    • 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 tool.

      Returns Promise<void>

      Promise that resolves when all cancellations are processed

    • Called before the twist's activate method, starting from the deepest tool dependencies.

      This method is called in a depth-first manner, with the deepest dependencies being called first, bubbling up to the top-level tools before the twist's activate method is called.

      Parameters

      • priority: Priority

        The priority context containing the priority ID

      • Optionalcontext: { actor: Actor }

        Optional context containing the actor who triggered activation

      Returns Promise<void>

      Promise that resolves when pre-activation is complete

    • Called after the twist's activate method, starting from the top-level tools.

      This method is called in reverse order, with top-level tools being called first, then cascading down to the deepest dependencies.

      Parameters

      • priority: Priority

        The priority context containing the priority ID

      • Optionalcontext: { actor: Actor }

        Optional context containing the actor who triggered activation

      Returns Promise<void>

      Promise that resolves when post-activation is complete

    • Called before the twist's upgrade method, starting from the deepest tool dependencies.

      This method is called in a depth-first manner, with the deepest dependencies being called first, bubbling up to the top-level tools before the twist's upgrade method is called.

      Returns Promise<void>

      Promise that resolves when pre-upgrade is complete

    • Called after the twist's upgrade method, starting from the top-level tools.

      This method is called in reverse order, with top-level tools being called first, then cascading down to the deepest dependencies.

      Returns Promise<void>

      Promise that resolves when post-upgrade is complete

    • Called before the twist's deactivate method, starting from the deepest tool dependencies.

      This method is called in a depth-first manner, with the deepest dependencies being called first, bubbling up to the top-level tools before the twist's deactivate method is called.

      Returns Promise<void>

      Promise that resolves when pre-deactivation is complete

    • Called after the twist's deactivate method, starting from the top-level tools.

      This method is called in reverse order, with top-level tools being called first, then cascading down to the deepest dependencies.

      Returns Promise<void>

      Promise that resolves when post-deactivation is complete

    Properties

    id: string