@plotday/twister 0.59.0 → 0.61.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.
Files changed (105) hide show
  1. package/dist/connector.d.ts +52 -3
  2. package/dist/connector.d.ts.map +1 -1
  3. package/dist/connector.js +29 -0
  4. package/dist/connector.js.map +1 -1
  5. package/dist/docs/assets/hierarchy.js +1 -1
  6. package/dist/docs/assets/navigation.js +1 -1
  7. package/dist/docs/assets/search.js +1 -1
  8. package/dist/docs/classes/index.Connector.html +73 -35
  9. package/dist/docs/classes/index.FileNotFoundError.html +1 -1
  10. package/dist/docs/classes/index.Files.html +1 -1
  11. package/dist/docs/classes/index.Imap.html +1 -1
  12. package/dist/docs/classes/index.Options.html +1 -1
  13. package/dist/docs/classes/index.Smtp.html +1 -1
  14. package/dist/docs/classes/tool.ITool.html +1 -1
  15. package/dist/docs/classes/tool.Tool.html +23 -7
  16. package/dist/docs/classes/tools_ai.AI.html +1 -1
  17. package/dist/docs/classes/tools_callbacks.Callbacks.html +1 -1
  18. package/dist/docs/classes/tools_integrations.Integrations.html +1 -1
  19. package/dist/docs/classes/tools_network.Network.html +1 -1
  20. package/dist/docs/classes/tools_plot.Plot.html +1 -1
  21. package/dist/docs/classes/tools_store.Store.html +1 -1
  22. package/dist/docs/classes/tools_tasks.Tasks.html +32 -2
  23. package/dist/docs/classes/tools_twists.Twists.html +1 -1
  24. package/dist/docs/classes/twist.Twist.html +27 -11
  25. package/dist/docs/documents/Built-in_Tools.html +15 -1
  26. package/dist/docs/documents/Runtime_Environment.html +10 -2
  27. package/dist/docs/enums/plot.ActorType.html +4 -4
  28. package/dist/docs/hierarchy.html +1 -1
  29. package/dist/docs/media/AGENTS.md +44 -2
  30. package/dist/docs/modules/index.html +1 -1
  31. package/dist/docs/modules/plot.html +1 -1
  32. package/dist/docs/types/index.CreateLinkDraft.html +9 -9
  33. package/dist/docs/types/index.NoteWriteBackResult.html +21 -2
  34. package/dist/docs/types/index.OptionalScopeGroup.html +6 -6
  35. package/dist/docs/types/index.ResolvedRecipient.html +5 -5
  36. package/dist/docs/types/index.ScopeConfig.html +2 -4
  37. package/dist/docs/types/plot.Actor.html +5 -5
  38. package/dist/docs/types/plot.AutoThreadConfig.html +9 -0
  39. package/dist/docs/types/plot.AutoThreadMode.html +14 -0
  40. package/dist/docs/types/plot.Contact.html +4 -4
  41. package/dist/docs/types/plot.ContentType.html +1 -1
  42. package/dist/docs/types/plot.DeliveryError.html +17 -0
  43. package/dist/docs/types/plot.Link.html +17 -17
  44. package/dist/docs/types/plot.LinkUpdate.html +1 -1
  45. package/dist/docs/types/plot.NewActor.html +1 -1
  46. package/dist/docs/types/plot.NewContact.html +1 -1
  47. package/dist/docs/types/plot.NewLink.html +12 -2
  48. package/dist/docs/types/plot.NewLinkWithNotes.html +11 -3
  49. package/dist/docs/types/plot.NewNote.html +1 -1
  50. package/dist/docs/types/plot.Note.html +5 -5
  51. package/dist/docs/types/plot.NoteUpdate.html +1 -1
  52. package/dist/docs/types/plot.PlanOperation.html +1 -1
  53. package/dist/facets.d.ts +17 -1
  54. package/dist/facets.d.ts.map +1 -1
  55. package/dist/llm-docs/connector.d.ts +1 -1
  56. package/dist/llm-docs/connector.d.ts.map +1 -1
  57. package/dist/llm-docs/connector.js +1 -1
  58. package/dist/llm-docs/connector.js.map +1 -1
  59. package/dist/llm-docs/facets.d.ts +1 -1
  60. package/dist/llm-docs/facets.d.ts.map +1 -1
  61. package/dist/llm-docs/facets.js +1 -1
  62. package/dist/llm-docs/facets.js.map +1 -1
  63. package/dist/llm-docs/plot.d.ts +1 -1
  64. package/dist/llm-docs/plot.d.ts.map +1 -1
  65. package/dist/llm-docs/plot.js +1 -1
  66. package/dist/llm-docs/plot.js.map +1 -1
  67. package/dist/llm-docs/tool.d.ts +1 -1
  68. package/dist/llm-docs/tool.d.ts.map +1 -1
  69. package/dist/llm-docs/tool.js +1 -1
  70. package/dist/llm-docs/tool.js.map +1 -1
  71. package/dist/llm-docs/tools/tasks.d.ts +1 -1
  72. package/dist/llm-docs/tools/tasks.d.ts.map +1 -1
  73. package/dist/llm-docs/tools/tasks.js +1 -1
  74. package/dist/llm-docs/tools/tasks.js.map +1 -1
  75. package/dist/llm-docs/twist.d.ts +1 -1
  76. package/dist/llm-docs/twist.d.ts.map +1 -1
  77. package/dist/llm-docs/twist.js +1 -1
  78. package/dist/llm-docs/twist.js.map +1 -1
  79. package/dist/plot.d.ts +84 -1
  80. package/dist/plot.d.ts.map +1 -1
  81. package/dist/plot.js.map +1 -1
  82. package/dist/tool.d.ts +25 -0
  83. package/dist/tool.d.ts.map +1 -1
  84. package/dist/tool.js +27 -0
  85. package/dist/tool.js.map +1 -1
  86. package/dist/tools/tasks.d.ts +46 -0
  87. package/dist/tools/tasks.d.ts.map +1 -1
  88. package/dist/tools/tasks.js.map +1 -1
  89. package/dist/twist.d.ts +25 -0
  90. package/dist/twist.d.ts.map +1 -1
  91. package/dist/twist.js +27 -0
  92. package/dist/twist.js.map +1 -1
  93. package/package.json +1 -1
  94. package/src/connector.ts +55 -3
  95. package/src/facets.ts +21 -1
  96. package/src/llm-docs/connector.ts +1 -1
  97. package/src/llm-docs/facets.ts +1 -1
  98. package/src/llm-docs/plot.ts +1 -1
  99. package/src/llm-docs/tool.ts +1 -1
  100. package/src/llm-docs/tools/tasks.ts +1 -1
  101. package/src/llm-docs/twist.ts +1 -1
  102. package/src/plot.ts +87 -1
  103. package/src/tool.ts +33 -0
  104. package/src/tools/tasks.ts +52 -0
  105. package/src/twist.ts +33 -0
@@ -5,4 +5,4 @@
5
5
  * Generated from: prebuild.ts
6
6
  */
7
7
 
8
- export default "import { ITool } from \"..\";\nimport type { Callback } from \"./callbacks\";\n\n/**\n * Run background tasks and scheduled jobs.\n *\n * The Tasks tool enables twists and tools to queue callbacks for execution in separate\n * worker contexts. **This is critical for staying under request limits**: each execution\n * has a limit of ~1000 requests (HTTP requests, tool calls, database operations), and\n * running a task creates a NEW execution with a fresh request limit.\n *\n * **Key distinction:**\n * - **Calling a callback** (via `this.run()`) continues the same execution and shares the request count\n * - **Running a task** (via `this.runTask()`) creates a NEW execution with fresh ~1000 request limit\n *\n * **When to use tasks:**\n * - Processing large datasets that would exceed 1000 requests\n * - Breaking loops into chunks where each chunk stays under the request limit\n * - Scheduling operations for future execution\n *\n * **Note:** Tasks tool methods are also available directly on Twist and Tool classes\n * via `this.runTask()`, `this.cancelTask()`, and `this.cancelAllTasks()`.\n * This is the recommended approach for most use cases.\n *\n * **Best Practices:**\n * - Size batches to stay under ~1000 requests per execution\n * - Calculate requests per item to determine safe batch size\n * - Create callbacks first using `this.callback()`\n * - Store intermediate state using the Store tool\n *\n * @example\n * ```typescript\n * class SyncTool extends Tool<SyncTool> {\n * async startBatchSync(totalItems: number) {\n * // Store initial state using built-in set method\n * await this.set(\"sync_progress\", { processed: 0, total: totalItems });\n *\n * // Create callback and queue first batch\n * const callback = await this.callback(this.processBatch, 1);\n * // runTask creates NEW execution with fresh ~1000 request limit\n * await this.runTask(callback);\n * }\n *\n * async processBatch(batchNumber: number) {\n * // Process one batch of items (sized to stay under request limit)\n * const progress = await this.get(\"sync_progress\");\n *\n * // If each item makes ~10 requests, process ~100 items per batch\n * // 100 items × 10 requests = 1000 requests (at limit)\n * const batchSize = 100;\n * const items = await this.fetchItems(progress.processed, batchSize);\n *\n * for (const item of items) {\n * await this.processItem(item); // Makes ~10 requests per item\n * }\n *\n * await this.set(\"sync_progress\", {\n * processed: progress.processed + batchSize,\n * total: progress.total\n * });\n *\n * if (progress.processed < progress.total) {\n * // Queue next batch - creates NEW execution with fresh request limit\n * const callback = await this.callback(this.processBatch, batchNumber + 1);\n * await this.runTask(callback);\n * }\n * }\n *\n * async scheduleCleanup() {\n * const tomorrow = new Date();\n * tomorrow.setDate(tomorrow.getDate() + 1);\n *\n * const callback = await this.callback(this.cleanupOldData);\n * // Schedule for future execution\n * return await this.runTask(callback, { runAt: tomorrow });\n * }\n * }\n * ```\n */\nexport abstract class Tasks extends ITool {\n /**\n * Queues a callback to execute in a separate worker context with a fresh request limit.\n *\n * **Creates a NEW execution** with its own request limit of ~1000 requests (HTTP requests,\n * tool calls, database operations). This is the primary way to stay under request limits\n * when processing large datasets or making many API calls.\n *\n * The callback will be invoked either immediately or at a scheduled time\n * in an isolated execution environment. Each execution has ~1000 requests and ~60 seconds\n * CPU time. Use this for breaking loops into chunks that stay under the request limit.\n *\n * **Key distinction:**\n * - `this.run(callback)` - Continues same execution, shares request count\n * - `this.runTask(callback)` - NEW execution, fresh request limit\n *\n * @param callback - Callback created with `this.callback()`\n * @param options - Optional configuration for the execution\n * @param options.runAt - If provided, schedules execution at this time; otherwise runs immediately\n * @returns Promise resolving to a cancellation token (only for scheduled executions)\n *\n * @example\n * ```typescript\n * // Break large loop into batches to stay under request limit\n * const callback = await this.callback(this.syncBatch, 1);\n * await this.runTask(callback); // Fresh execution with ~1000 requests\n * ```\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n abstract runTask(\n callback: Callback,\n options?: { runAt?: Date }\n ): Promise<string | void>;\n\n /**\n * Cancels a previously scheduled execution.\n *\n * Prevents a scheduled function from executing. No error is thrown\n * if the token is invalid or the execution has already completed.\n *\n * @param token - The cancellation token returned by runTask() with runAt option\n * @returns Promise that resolves when the cancellation is processed\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n abstract cancelTask(token: string): Promise<void>;\n\n /**\n * Cancels all scheduled executions for this tool/twist.\n *\n * Cancels all pending scheduled executions created by this tool or twist\n * instance. Immediate executions cannot be cancelled.\n *\n * @returns Promise that resolves when all cancellations are processed\n */\n abstract cancelAllTasks(): Promise<void>;\n}\n";
8
+ export default "import { ITool } from \"..\";\nimport type { Callback } from \"./callbacks\";\n\n/**\n * Run background tasks and scheduled jobs.\n *\n * The Tasks tool enables twists and tools to queue callbacks for execution in separate\n * worker contexts. **This is critical for staying under request limits**: each execution\n * has a limit of ~1000 requests (HTTP requests, tool calls, database operations), and\n * running a task creates a NEW execution with a fresh request limit.\n *\n * **Key distinction:**\n * - **Calling a callback** (via `this.run()`) continues the same execution and shares the request count\n * - **Running a task** (via `this.runTask()`) creates a NEW execution with fresh ~1000 request limit\n *\n * **When to use tasks:**\n * - Processing large datasets that would exceed 1000 requests\n * - Breaking loops into chunks where each chunk stays under the request limit\n * - Scheduling operations for future execution\n *\n * **Note:** Tasks tool methods are also available directly on Twist and Tool classes\n * via `this.runTask()`, `this.cancelTask()`, and `this.cancelAllTasks()`.\n * This is the recommended approach for most use cases.\n *\n * **Best Practices:**\n * - Size batches to stay under ~1000 requests per execution\n * - Calculate requests per item to determine safe batch size\n * - Create callbacks first using `this.callback()`\n * - Store intermediate state using the Store tool\n *\n * @example\n * ```typescript\n * class SyncTool extends Tool<SyncTool> {\n * async startBatchSync(totalItems: number) {\n * // Store initial state using built-in set method\n * await this.set(\"sync_progress\", { processed: 0, total: totalItems });\n *\n * // Create callback and queue first batch\n * const callback = await this.callback(this.processBatch, 1);\n * // runTask creates NEW execution with fresh ~1000 request limit\n * await this.runTask(callback);\n * }\n *\n * async processBatch(batchNumber: number) {\n * // Process one batch of items (sized to stay under request limit)\n * const progress = await this.get(\"sync_progress\");\n *\n * // If each item makes ~10 requests, process ~100 items per batch\n * // 100 items × 10 requests = 1000 requests (at limit)\n * const batchSize = 100;\n * const items = await this.fetchItems(progress.processed, batchSize);\n *\n * for (const item of items) {\n * await this.processItem(item); // Makes ~10 requests per item\n * }\n *\n * await this.set(\"sync_progress\", {\n * processed: progress.processed + batchSize,\n * total: progress.total\n * });\n *\n * if (progress.processed < progress.total) {\n * // Queue next batch - creates NEW execution with fresh request limit\n * const callback = await this.callback(this.processBatch, batchNumber + 1);\n * await this.runTask(callback);\n * }\n * }\n *\n * async scheduleCleanup() {\n * const tomorrow = new Date();\n * tomorrow.setDate(tomorrow.getDate() + 1);\n *\n * const callback = await this.callback(this.cleanupOldData);\n * // Schedule for future execution\n * return await this.runTask(callback, { runAt: tomorrow });\n * }\n * }\n * ```\n */\nexport abstract class Tasks extends ITool {\n /**\n * Queues a callback to execute in a separate worker context with a fresh request limit.\n *\n * **Creates a NEW execution** with its own request limit of ~1000 requests (HTTP requests,\n * tool calls, database operations). This is the primary way to stay under request limits\n * when processing large datasets or making many API calls.\n *\n * The callback will be invoked either immediately or at a scheduled time\n * in an isolated execution environment. Each execution has ~1000 requests and ~60 seconds\n * CPU time. Use this for breaking loops into chunks that stay under the request limit.\n *\n * **Key distinction:**\n * - `this.run(callback)` - Continues same execution, shares request count\n * - `this.runTask(callback)` - NEW execution, fresh request limit\n *\n * @param callback - Callback created with `this.callback()`\n * @param options - Optional configuration for the execution\n * @param options.runAt - If provided, schedules execution at this time; otherwise runs immediately\n * @returns Promise resolving to a cancellation token (only for scheduled executions)\n *\n * @example\n * ```typescript\n * // Break large loop into batches to stay under request limit\n * const callback = await this.callback(this.syncBatch, 1);\n * await this.runTask(callback); // Fresh execution with ~1000 requests\n * ```\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n abstract runTask(\n callback: Callback,\n options?: { runAt?: Date }\n ): Promise<string | void>;\n\n /**\n * Cancels a previously scheduled execution.\n *\n * Prevents a scheduled function from executing. No error is thrown\n * if the token is invalid or the execution has already completed.\n *\n * @param token - The cancellation token returned by runTask() with runAt option\n * @returns Promise that resolves when the cancellation is processed\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n abstract cancelTask(token: string): Promise<void>;\n\n /**\n * Cancels all scheduled executions for this tool/twist.\n *\n * Cancels all pending scheduled executions created by this tool or twist\n * instance. Immediate executions cannot be cancelled.\n *\n * @returns Promise that resolves when all cancellations are processed\n */\n abstract cancelAllTasks(): Promise<void>;\n\n /**\n * Schedules a **singleton** task identified by `key`: scheduling under a key\n * that already has a pending task atomically cancels the existing one and\n * replaces it. At most one scheduled task per `key` is ever live.\n *\n * Use this for any recurring/self-renewing job — webhook/watch renewals,\n * periodic polling, deferred cleanup — instead of hand-managing tokens with\n * `runTask()` + `cancelTask()`. The manual pattern (store the token, cancel\n * it before re-scheduling) is easy to get wrong: a renewal callback that\n * re-schedules itself, combined with any *extra* scheduling call (a\n * re-dispatched `onChannelEnabled`, a re-init), leaks parallel self-\n * perpetuating chains that accumulate forever and can trip the runtime's\n * execution quota. Keying makes that leak impossible by construction.\n *\n * Replacement is atomic on the server, so concurrent executions racing to\n * schedule the same key converge on a single task rather than leaking.\n *\n * @param key - Stable identifier for this logical task. Scope it to what it\n * renews, e.g. `` `watch-renewal:${folderId}` ``.\n * @param callback - Callback created with `this.callback()`\n * @param options.runAt - When to run. Required: keying only applies to\n * scheduled tasks (immediate tasks go straight to the queue).\n * @returns Promise resolving to the cancellation token for the scheduled task\n *\n * @example\n * ```typescript\n * const cb = await this.callback(this.renewWatch, folderId);\n * await this.scheduleTask(`watch-renewal:${folderId}`, cb, { runAt });\n * // ...later, on disable:\n * await this.cancelScheduledTask(`watch-renewal:${folderId}`);\n * ```\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n abstract scheduleTask(\n key: string,\n callback: Callback,\n options: { runAt: Date }\n ): Promise<string | void>;\n\n /**\n * Cancels the singleton task previously scheduled under `key` (if any).\n *\n * No error is thrown if no task exists for the key or it has already run.\n * Pair this with {@link scheduleTask} in teardown paths (e.g.\n * `onChannelDisabled`, `stopSync`).\n *\n * @param key - The same key passed to {@link scheduleTask}\n * @returns Promise that resolves when the cancellation is processed\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n abstract cancelScheduledTask(key: string): Promise<void>;\n}\n";
@@ -5,4 +5,4 @@
5
5
  * Generated from: prebuild.ts
6
6
  */
7
7
 
8
- export default "import type { NoteWriteBackResult } from \"./connector\";\nimport { type Action, type Actor, type ActorId, type Link, type Note, type Thread, Uuid } from \"./plot\";\nimport type { Tag } from \"./tag\";\nimport { type ITool } from \"./tool\";\nimport type { Callback } from \"./tools/callbacks\";\nimport type { Serializable } from \"./utils/serializable\";\nimport type { InferTools, ToolBuilder, ToolShed } from \"./utils/types\";\n\n/**\n * Base class for all twists.\n *\n * A twist is installed at the workspace level and is owned by a single user\n * (see `this.userId`). It has no inherent focus scope: threads, notes, and\n * links it creates are filed against the owner's focuses, with automatic\n * focus matching when no explicit target is provided.\n *\n * Override `build()` to declare tool dependencies and lifecycle methods to\n * handle events.\n *\n * @example\n * ```typescript\n * class FlatteringTwist extends Twist<FlatteringTwist> {\n * build(build: ToolBuilder) {\n * return {\n * plot: build(Plot),\n * };\n * }\n *\n * async activate() {\n * await this.tools.plot.createThread({\n * title: \"Hello, good looking!\",\n * });\n * }\n * }\n * ```\n */\nexport abstract class Twist<TSelf> {\n /**\n * When `true`, users may install multiple instances of this twist within\n * the same scope (personal workspace or team). Each instance must have a\n * distinct name.\n *\n * Defaults to `false` (single instance per scope).\n *\n * @example\n * ```typescript\n * class WorkflowTwist extends Twist<WorkflowTwist> {\n * static readonly multipleInstances = true;\n * // ...\n * }\n * ```\n */\n static readonly multipleInstances?: boolean;\n\n /**\n * The user ID (`twist_instance.owner_id`) that installed this twist.\n * Populated by the runtime before any lifecycle method runs.\n */\n protected userId!: Uuid;\n\n constructor(protected id: Uuid, private toolShed: ToolShed) {}\n\n /**\n * Gets the initialized tools for this twist.\n * @throws Error if called before initialization is complete\n */\n protected get tools(): InferTools<TSelf> {\n return this.toolShed.getTools<InferTools<TSelf>>();\n }\n\n /**\n * Declares tool dependencies for this twist.\n * Return an object mapping tool names to build() promises.\n *\n * @param build - The build function to use for declaring dependencies\n * @returns Object mapping tool names to tool promises\n *\n * @example\n * ```typescript\n * build(build: ToolBuilder) {\n * return {\n * plot: build(Plot),\n * calendar: build(GoogleCalendar, { apiKey: \"...\" }),\n * };\n * }\n * ```\n */\n abstract build(build: ToolBuilder): Record<string, Promise<ITool>>;\n\n /**\n * Creates a persistent callback to a method on this twist.\n *\n * ExtraArgs are strongly typed to match the method's signature. They must be serializable.\n *\n * @param fn - The method to callback\n * @param extraArgs - Additional arguments to pass (type-checked, must be serializable)\n * @returns Promise resolving to a persistent callback token\n *\n * @example\n * ```typescript\n * const callback = await this.callback(this.onWebhook, \"calendar\", 123);\n * ```\n */\n protected callback<\n TArgs extends Serializable[],\n Fn extends (...args: TArgs) => any\n >(fn: Fn, ...extraArgs: TArgs): Promise<Callback>;\n // Overload when caller provides the first argument\n protected callback<\n TArgs extends Serializable[],\n Fn extends (arg1: any, ...extraArgs: TArgs) => any\n >(fn: Fn, ...extraArgs: TArgs): Promise<Callback>;\n protected async callback<\n TArgs extends Serializable[],\n Fn extends (...args: any[]) => any\n >(fn: Fn, ...extraArgs: TArgs): Promise<Callback> {\n return this.tools.callbacks.create(fn, ...extraArgs);\n }\n\n /**\n * Like callback(), but for an Action, which receives the action as the first argument.\n *\n * @param fn - The method to callback\n * @param extraArgs - Additional arguments to pass after the action\n * @returns Promise resolving to a persistent callback token\n *\n * @example\n * ```typescript\n * const callback = await this.actionCallback(this.doSomething, 123);\n * const action: Action = {\n * type: ActionType.callback,\n * title: \"Do Something\",\n * callback,\n * };\n * ```\n */\n protected async actionCallback<\n TArgs extends Serializable[],\n Fn extends (action: Action, ...extraArgs: TArgs) => any\n >(fn: Fn, ...extraArgs: TArgs): Promise<Callback> {\n return this.tools.callbacks.create(fn, ...extraArgs);\n }\n\n /**\n * Deletes a specific callback by its token.\n *\n * @param token - The callback token to delete\n * @returns Promise that resolves when the callback is deleted\n */\n protected async deleteCallback(token: Callback): Promise<void> {\n return this.tools.callbacks.delete(token);\n }\n\n /**\n * Deletes all callbacks for this twist.\n *\n * @returns Promise that resolves when all callbacks are deleted\n */\n protected async deleteAllCallbacks(): Promise<void> {\n return this.tools.callbacks.deleteAll();\n }\n\n /**\n * Executes a callback by its token inline in the current execution.\n *\n * **Use `this.runTask()` instead for batch continuations and long-running work.**\n * `this.run()` executes inline, sharing the current request count (~1000 limit)\n * and blocking the HTTP response. This causes timeouts when used in lifecycle\n * methods like `onChannelEnabled` or `syncBatch` continuations.\n *\n * `this.run()` is appropriate when you need the callback's **return value** —\n * e.g., running a parent callback token that returns data. For fire-and-forget\n * work, always prefer `this.runTask()`.\n *\n * @param token - The callback token to execute\n * @param args - Optional arguments to pass to the callback\n * @returns Promise resolving to the callback result\n */\n protected async run(token: Callback, ...args: []): Promise<any> {\n return this.tools.callbacks.run(token, ...args);\n }\n\n /**\n * Retrieves a value from persistent storage by key.\n *\n * Values are automatically deserialized using SuperJSON, which\n * properly restores Date objects, Maps, Sets, and other complex types.\n *\n * @template T - The expected type of the stored value (must be Serializable)\n * @param key - The storage key to retrieve\n * @returns Promise resolving to the stored value or null\n */\n protected async get<T extends import(\"./index\").Serializable>(\n key: string\n ): Promise<T | null> {\n return this.tools.store.get(key);\n }\n\n /**\n * Stores a value in persistent storage.\n *\n * The value will be serialized using SuperJSON and stored persistently.\n * SuperJSON automatically handles Date objects, Maps, Sets, undefined values,\n * and other complex types that standard JSON doesn't support.\n *\n * **Important**: Functions and Symbols cannot be stored.\n * **For function references**: Use callbacks instead of storing functions directly.\n *\n * @example\n * ```typescript\n * // ✅ Date objects are preserved\n * await this.set(\"sync_state\", {\n * lastSync: new Date(),\n * minDate: new Date(2024, 0, 1)\n * });\n *\n * // ✅ undefined is now supported\n * await this.set(\"data\", { name: \"test\", optional: undefined });\n *\n * // ❌ WRONG: Cannot store functions directly\n * await this.set(\"handler\", this.myHandler);\n *\n * // ✅ CORRECT: Create a callback token first\n * const token = await this.callback(this.myHandler, \"arg1\", \"arg2\");\n * await this.set(\"handler_token\", token);\n *\n * // Later, execute the callback\n * const token = await this.get<Callback>(\"handler_token\");\n * await this.run(token);\n * ```\n *\n * @template T - The type of value being stored (must be Serializable)\n * @param key - The storage key to use\n * @param value - The value to store (must be SuperJSON-serializable)\n * @returns Promise that resolves when the value is stored\n */\n protected async set<T extends import(\"./index\").Serializable>(\n key: string,\n value: T\n ): Promise<void> {\n return this.tools.store.set(key, value);\n }\n\n /**\n * Removes a specific key from persistent storage.\n *\n * @param key - The storage key to remove\n * @returns Promise that resolves when the key is removed\n */\n protected async clear(key: string): Promise<void> {\n return this.tools.store.clear(key);\n }\n\n /**\n * Removes all keys from this twist's storage.\n *\n * @returns Promise that resolves when all keys are removed\n */\n protected async clearAll(): Promise<void> {\n return this.tools.store.clearAll();\n }\n\n /**\n * Queues a callback to execute in a separate worker context.\n *\n * @param callback - The callback token created with `this.callback()`\n * @param options - Optional configuration for the execution\n * @param options.runAt - If provided, schedules execution at this time; otherwise runs immediately\n * @returns Promise resolving to a cancellation token (only for scheduled executions)\n */\n protected async runTask(\n callback: Callback,\n options?: { runAt?: Date }\n ): Promise<string | void> {\n return this.tools.tasks.runTask(callback, options);\n }\n\n /**\n * Cancels a previously scheduled execution.\n *\n * @param token - The cancellation token returned by runTask() with runAt option\n * @returns Promise that resolves when the cancellation is processed\n */\n protected async cancelTask(token: string): Promise<void> {\n return this.tools.tasks.cancelTask(token);\n }\n\n /**\n * Cancels all scheduled executions for this twist.\n *\n * @returns Promise that resolves when all cancellations are processed\n */\n protected async cancelAllTasks(): Promise<void> {\n return this.tools.tasks.cancelAllTasks();\n }\n\n /**\n * Called when the twist is installed by a user.\n *\n * This method should contain initialization logic such as seeding\n * initial threads, configuring webhooks, or establishing external\n * connections. When it runs, `this.userId` is already populated with\n * the installing user's ID.\n *\n * @param context - Optional context containing the actor who triggered activation\n * @returns Promise that resolves when activation is complete\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n activate(context?: { actor: Actor }): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a new version of the twist is deployed.\n *\n * This method should contain migration logic for updating old data structures\n * or setting up new resources that weren't needed by the previous version.\n * It is called once per active twist_instance with the new version.\n *\n * @returns Promise that resolves when upgrade is complete\n */\n upgrade(): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when the twist's options configuration changes.\n *\n * Override to react to option changes, e.g. archiving items when a sync\n * type is toggled off, or starting sync when a type is toggled on.\n *\n * @param oldOptions - The previously resolved options\n * @param newOptions - The newly resolved options\n * @returns Promise that resolves when the change is handled\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onOptionsChanged(\n oldOptions: Record<string, any>,\n newOptions: Record<string, any>\n ): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when the twist is uninstalled.\n *\n * This method should contain cleanup logic such as removing webhooks,\n * cleaning up external resources, or performing final data operations.\n *\n * @returns Promise that resolves when deactivation is complete\n */\n deactivate(): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a thread created by this twist is updated.\n * Override to implement two-way sync with an external system.\n *\n * @param thread - The updated thread\n * @param changes - Tag additions and removals on the thread\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onThreadUpdated(\n thread: Thread,\n changes: {\n tagsAdded: Record<Tag, ActorId[]>;\n tagsRemoved: Record<Tag, ActorId[]>;\n }\n ): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a note is created on a thread created by this twist.\n * Override to implement two-way sync (e.g. syncing notes as comments).\n *\n * Notes created by the twist itself are filtered out to prevent loops.\n *\n * Returning a string sets the note's `key` for future upsert matching,\n * linking the Plot note to its external counterpart so that subsequent\n * syncs (reactions, edits) update the existing note instead of creating duplicates.\n *\n * @param note - The newly created note\n * @returns Optional note key for external deduplication\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onNoteCreated(note: Note, ...args: any[]): Promise<string | NoteWriteBackResult | void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a link is created in a connected source channel.\n * Requires `link: true` in Plot options.\n *\n * @param link - The newly created link\n * @param notes - Notes on the link's thread\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onLinkCreated(link: Link, notes: Note[]): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a link in a connected source channel is updated.\n * Requires `link: true` in Plot options.\n *\n * @param link - The updated link\n * @param notes - Notes on the link's thread (optional)\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onLinkUpdated(link: Link, notes?: Note[]): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a note is created on a thread with a link from a connected channel.\n * Requires `link: true` in Plot options.\n *\n * @param note - The newly created note\n * @param link - The link associated with the thread\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onLinkNoteCreated(note: Note, link: Link): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Waits for tool initialization to complete.\n * Called automatically by the entrypoint before lifecycle methods.\n * @internal\n */\n async waitForReady(): Promise<void> {\n await this.toolShed.waitForReady();\n }\n}\n";
8
+ export default "import type { NoteWriteBackResult } from \"./connector\";\nimport { type Action, type Actor, type ActorId, type Link, type Note, type Thread, Uuid } from \"./plot\";\nimport type { Tag } from \"./tag\";\nimport { type ITool } from \"./tool\";\nimport type { Callback } from \"./tools/callbacks\";\nimport type { Serializable } from \"./utils/serializable\";\nimport type { InferTools, ToolBuilder, ToolShed } from \"./utils/types\";\n\n/**\n * Base class for all twists.\n *\n * A twist is installed at the workspace level and is owned by a single user\n * (see `this.userId`). It has no inherent focus scope: threads, notes, and\n * links it creates are filed against the owner's focuses, with automatic\n * focus matching when no explicit target is provided.\n *\n * Override `build()` to declare tool dependencies and lifecycle methods to\n * handle events.\n *\n * @example\n * ```typescript\n * class FlatteringTwist extends Twist<FlatteringTwist> {\n * build(build: ToolBuilder) {\n * return {\n * plot: build(Plot),\n * };\n * }\n *\n * async activate() {\n * await this.tools.plot.createThread({\n * title: \"Hello, good looking!\",\n * });\n * }\n * }\n * ```\n */\nexport abstract class Twist<TSelf> {\n /**\n * When `true`, users may install multiple instances of this twist within\n * the same scope (personal workspace or team). Each instance must have a\n * distinct name.\n *\n * Defaults to `false` (single instance per scope).\n *\n * @example\n * ```typescript\n * class WorkflowTwist extends Twist<WorkflowTwist> {\n * static readonly multipleInstances = true;\n * // ...\n * }\n * ```\n */\n static readonly multipleInstances?: boolean;\n\n /**\n * The user ID (`twist_instance.owner_id`) that installed this twist.\n * Populated by the runtime before any lifecycle method runs.\n */\n protected userId!: Uuid;\n\n constructor(protected id: Uuid, private toolShed: ToolShed) {}\n\n /**\n * Gets the initialized tools for this twist.\n * @throws Error if called before initialization is complete\n */\n protected get tools(): InferTools<TSelf> {\n return this.toolShed.getTools<InferTools<TSelf>>();\n }\n\n /**\n * Declares tool dependencies for this twist.\n * Return an object mapping tool names to build() promises.\n *\n * @param build - The build function to use for declaring dependencies\n * @returns Object mapping tool names to tool promises\n *\n * @example\n * ```typescript\n * build(build: ToolBuilder) {\n * return {\n * plot: build(Plot),\n * calendar: build(GoogleCalendar, { apiKey: \"...\" }),\n * };\n * }\n * ```\n */\n abstract build(build: ToolBuilder): Record<string, Promise<ITool>>;\n\n /**\n * Creates a persistent callback to a method on this twist.\n *\n * ExtraArgs are strongly typed to match the method's signature. They must be serializable.\n *\n * @param fn - The method to callback\n * @param extraArgs - Additional arguments to pass (type-checked, must be serializable)\n * @returns Promise resolving to a persistent callback token\n *\n * @example\n * ```typescript\n * const callback = await this.callback(this.onWebhook, \"calendar\", 123);\n * ```\n */\n protected callback<\n TArgs extends Serializable[],\n Fn extends (...args: TArgs) => any\n >(fn: Fn, ...extraArgs: TArgs): Promise<Callback>;\n // Overload when caller provides the first argument\n protected callback<\n TArgs extends Serializable[],\n Fn extends (arg1: any, ...extraArgs: TArgs) => any\n >(fn: Fn, ...extraArgs: TArgs): Promise<Callback>;\n protected async callback<\n TArgs extends Serializable[],\n Fn extends (...args: any[]) => any\n >(fn: Fn, ...extraArgs: TArgs): Promise<Callback> {\n return this.tools.callbacks.create(fn, ...extraArgs);\n }\n\n /**\n * Like callback(), but for an Action, which receives the action as the first argument.\n *\n * @param fn - The method to callback\n * @param extraArgs - Additional arguments to pass after the action\n * @returns Promise resolving to a persistent callback token\n *\n * @example\n * ```typescript\n * const callback = await this.actionCallback(this.doSomething, 123);\n * const action: Action = {\n * type: ActionType.callback,\n * title: \"Do Something\",\n * callback,\n * };\n * ```\n */\n protected async actionCallback<\n TArgs extends Serializable[],\n Fn extends (action: Action, ...extraArgs: TArgs) => any\n >(fn: Fn, ...extraArgs: TArgs): Promise<Callback> {\n return this.tools.callbacks.create(fn, ...extraArgs);\n }\n\n /**\n * Deletes a specific callback by its token.\n *\n * @param token - The callback token to delete\n * @returns Promise that resolves when the callback is deleted\n */\n protected async deleteCallback(token: Callback): Promise<void> {\n return this.tools.callbacks.delete(token);\n }\n\n /**\n * Deletes all callbacks for this twist.\n *\n * @returns Promise that resolves when all callbacks are deleted\n */\n protected async deleteAllCallbacks(): Promise<void> {\n return this.tools.callbacks.deleteAll();\n }\n\n /**\n * Executes a callback by its token inline in the current execution.\n *\n * **Use `this.runTask()` instead for batch continuations and long-running work.**\n * `this.run()` executes inline, sharing the current request count (~1000 limit)\n * and blocking the HTTP response. This causes timeouts when used in lifecycle\n * methods like `onChannelEnabled` or `syncBatch` continuations.\n *\n * `this.run()` is appropriate when you need the callback's **return value** —\n * e.g., running a parent callback token that returns data. For fire-and-forget\n * work, always prefer `this.runTask()`.\n *\n * @param token - The callback token to execute\n * @param args - Optional arguments to pass to the callback\n * @returns Promise resolving to the callback result\n */\n protected async run(token: Callback, ...args: []): Promise<any> {\n return this.tools.callbacks.run(token, ...args);\n }\n\n /**\n * Retrieves a value from persistent storage by key.\n *\n * Values are automatically deserialized using SuperJSON, which\n * properly restores Date objects, Maps, Sets, and other complex types.\n *\n * @template T - The expected type of the stored value (must be Serializable)\n * @param key - The storage key to retrieve\n * @returns Promise resolving to the stored value or null\n */\n protected async get<T extends import(\"./index\").Serializable>(\n key: string\n ): Promise<T | null> {\n return this.tools.store.get(key);\n }\n\n /**\n * Stores a value in persistent storage.\n *\n * The value will be serialized using SuperJSON and stored persistently.\n * SuperJSON automatically handles Date objects, Maps, Sets, undefined values,\n * and other complex types that standard JSON doesn't support.\n *\n * **Important**: Functions and Symbols cannot be stored.\n * **For function references**: Use callbacks instead of storing functions directly.\n *\n * @example\n * ```typescript\n * // ✅ Date objects are preserved\n * await this.set(\"sync_state\", {\n * lastSync: new Date(),\n * minDate: new Date(2024, 0, 1)\n * });\n *\n * // ✅ undefined is now supported\n * await this.set(\"data\", { name: \"test\", optional: undefined });\n *\n * // ❌ WRONG: Cannot store functions directly\n * await this.set(\"handler\", this.myHandler);\n *\n * // ✅ CORRECT: Create a callback token first\n * const token = await this.callback(this.myHandler, \"arg1\", \"arg2\");\n * await this.set(\"handler_token\", token);\n *\n * // Later, execute the callback\n * const token = await this.get<Callback>(\"handler_token\");\n * await this.run(token);\n * ```\n *\n * @template T - The type of value being stored (must be Serializable)\n * @param key - The storage key to use\n * @param value - The value to store (must be SuperJSON-serializable)\n * @returns Promise that resolves when the value is stored\n */\n protected async set<T extends import(\"./index\").Serializable>(\n key: string,\n value: T\n ): Promise<void> {\n return this.tools.store.set(key, value);\n }\n\n /**\n * Removes a specific key from persistent storage.\n *\n * @param key - The storage key to remove\n * @returns Promise that resolves when the key is removed\n */\n protected async clear(key: string): Promise<void> {\n return this.tools.store.clear(key);\n }\n\n /**\n * Removes all keys from this twist's storage.\n *\n * @returns Promise that resolves when all keys are removed\n */\n protected async clearAll(): Promise<void> {\n return this.tools.store.clearAll();\n }\n\n /**\n * Queues a callback to execute in a separate worker context.\n *\n * @param callback - The callback token created with `this.callback()`\n * @param options - Optional configuration for the execution\n * @param options.runAt - If provided, schedules execution at this time; otherwise runs immediately\n * @returns Promise resolving to a cancellation token (only for scheduled executions)\n */\n protected async runTask(\n callback: Callback,\n options?: { runAt?: Date }\n ): Promise<string | void> {\n return this.tools.tasks.runTask(callback, options);\n }\n\n /**\n * Cancels a previously scheduled execution.\n *\n * @param token - The cancellation token returned by runTask() with runAt option\n * @returns Promise that resolves when the cancellation is processed\n */\n protected async cancelTask(token: string): Promise<void> {\n return this.tools.tasks.cancelTask(token);\n }\n\n /**\n * Cancels all scheduled executions for this twist.\n *\n * @returns Promise that resolves when all cancellations are processed\n */\n protected async cancelAllTasks(): Promise<void> {\n return this.tools.tasks.cancelAllTasks();\n }\n\n /**\n * Schedules a **singleton** task keyed by `key`: re-scheduling under the same\n * key atomically replaces any pending task, so at most one is ever live.\n *\n * Prefer this over `runTask({ runAt })` for recurring/self-renewing jobs\n * (watch renewals, polling, deferred cleanup) — it removes the error-prone\n * \"store token, cancel before re-scheduling\" bookkeeping that otherwise leaks\n * parallel task chains. See {@link Tasks.scheduleTask}.\n *\n * @param key - Stable identifier scoped to what the task renews\n * @param callback - The callback token created with `this.callback()`\n * @param options.runAt - When to run (required)\n * @returns Promise resolving to the scheduled task's cancellation token\n */\n protected async scheduleTask(\n key: string,\n callback: Callback,\n options: { runAt: Date }\n ): Promise<string | void> {\n return this.tools.tasks.scheduleTask(key, callback, options);\n }\n\n /**\n * Cancels the singleton task previously scheduled under `key` (if any).\n * No-op if none exists or it already ran. See {@link Tasks.cancelScheduledTask}.\n *\n * @param key - The same key passed to {@link scheduleTask}\n * @returns Promise that resolves when the cancellation is processed\n */\n protected async cancelScheduledTask(key: string): Promise<void> {\n return this.tools.tasks.cancelScheduledTask(key);\n }\n\n /**\n * Called when the twist is installed by a user.\n *\n * This method should contain initialization logic such as seeding\n * initial threads, configuring webhooks, or establishing external\n * connections. When it runs, `this.userId` is already populated with\n * the installing user's ID.\n *\n * @param context - Optional context containing the actor who triggered activation\n * @returns Promise that resolves when activation is complete\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n activate(context?: { actor: Actor }): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a new version of the twist is deployed.\n *\n * This method should contain migration logic for updating old data structures\n * or setting up new resources that weren't needed by the previous version.\n * It is called once per active twist_instance with the new version.\n *\n * @returns Promise that resolves when upgrade is complete\n */\n upgrade(): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when the twist's options configuration changes.\n *\n * Override to react to option changes, e.g. archiving items when a sync\n * type is toggled off, or starting sync when a type is toggled on.\n *\n * @param oldOptions - The previously resolved options\n * @param newOptions - The newly resolved options\n * @returns Promise that resolves when the change is handled\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onOptionsChanged(\n oldOptions: Record<string, any>,\n newOptions: Record<string, any>\n ): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when the twist is uninstalled.\n *\n * This method should contain cleanup logic such as removing webhooks,\n * cleaning up external resources, or performing final data operations.\n *\n * @returns Promise that resolves when deactivation is complete\n */\n deactivate(): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a thread created by this twist is updated.\n * Override to implement two-way sync with an external system.\n *\n * @param thread - The updated thread\n * @param changes - Tag additions and removals on the thread\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onThreadUpdated(\n thread: Thread,\n changes: {\n tagsAdded: Record<Tag, ActorId[]>;\n tagsRemoved: Record<Tag, ActorId[]>;\n }\n ): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a note is created on a thread created by this twist.\n * Override to implement two-way sync (e.g. syncing notes as comments).\n *\n * Notes created by the twist itself are filtered out to prevent loops.\n *\n * Returning a string sets the note's `key` for future upsert matching,\n * linking the Plot note to its external counterpart so that subsequent\n * syncs (reactions, edits) update the existing note instead of creating duplicates.\n *\n * @param note - The newly created note\n * @returns Optional note key for external deduplication\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onNoteCreated(note: Note, ...args: any[]): Promise<string | NoteWriteBackResult | void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a link is created in a connected source channel.\n * Requires `link: true` in Plot options.\n *\n * @param link - The newly created link\n * @param notes - Notes on the link's thread\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onLinkCreated(link: Link, notes: Note[]): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a link in a connected source channel is updated.\n * Requires `link: true` in Plot options.\n *\n * @param link - The updated link\n * @param notes - Notes on the link's thread (optional)\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onLinkUpdated(link: Link, notes?: Note[]): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Called when a note is created on a thread with a link from a connected channel.\n * Requires `link: true` in Plot options.\n *\n * @param note - The newly created note\n * @param link - The link associated with the thread\n */\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n onLinkNoteCreated(note: Note, link: Link): Promise<void> {\n return Promise.resolve();\n }\n\n /**\n * Waits for tool initialization to complete.\n * Called automatically by the entrypoint before lifecycle methods.\n * @internal\n */\n async waitForReady(): Promise<void> {\n await this.toolShed.waitForReady();\n }\n}\n";
package/src/plot.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { ThreadFacets } from "./facets";
1
+ import type { Cta, ThreadFacets } from "./facets";
2
2
  import type { NewSchedule, NewScheduleOccurrence, Schedule } from "./schedule";
3
3
  import { type Tag } from "./tag";
4
4
  import { type Callback } from "./tools/callbacks";
@@ -655,6 +655,32 @@ export type ThreadUpdate =
655
655
  * Notes contain the detailed content (note text, actions) associated with a thread.
656
656
  * They are always ordered by creation time within their parent thread.
657
657
  */
658
+ /**
659
+ * Reports that an outbound send / write-back for a note could not be
660
+ * delivered to the external system, so Plot can surface it to the user.
661
+ *
662
+ * Returned (not thrown) from a write-back hook — see
663
+ * `NoteWriteBackResult.deliveryError` and `NewLinkWithNotes.originatingNote` —
664
+ * after the connector has exhausted its own retries (or immediately for a
665
+ * permanent failure such as a rejected recipient). The runtime records it on
666
+ * the note (the app then shows a "Failed to send" affordance with Retry /
667
+ * Discard) and marks the thread unread so the user notices.
668
+ */
669
+ export type DeliveryError = {
670
+ /**
671
+ * Short, stable machine code for the failure category, e.g. `"rejected"`,
672
+ * `"too_large"`, `"rate_limited"`, `"invalid_recipient"`, or the generic
673
+ * `"send_failed"`. Used for logging/diagnostics, not shown verbatim to users.
674
+ */
675
+ code: string;
676
+ /**
677
+ * Human-readable, user-safe explanation to show beside "Failed to send"
678
+ * (e.g. "Recipient address rejected"). Null/omitted when the connector has
679
+ * no safe message to surface — the app then shows just "Failed to send".
680
+ */
681
+ message?: string | null;
682
+ };
683
+
658
684
  export type Note = ThreadCommon & {
659
685
  /** The author of this note */
660
686
  author: Actor;
@@ -696,6 +722,12 @@ export type Note = ThreadCommon & {
696
722
  accessContacts: ActorId[] | null;
697
723
  /** Focus twist IDs (twists/connectors) mentioned for dispatch routing. Does not include user contacts. */
698
724
  mentions: ActorId[];
725
+ /**
726
+ * A time-sensitive call-to-action extracted from this note's message
727
+ * (OTP code or confirm link). Null when none detected. Set by the runtime
728
+ * from the connector's extraction; clients read it to show an ephemeral prompt.
729
+ */
730
+ cta: Cta | null;
699
731
  };
700
732
 
701
733
  /**
@@ -1135,8 +1167,51 @@ export type NewLink = Partial<
1135
1167
  * an event vs a subscribed copy) so clients display it as the primary.
1136
1168
  */
1137
1169
  priority?: number;
1170
+ /**
1171
+ * Opt this message into the platform's sequential auto-threading. When a
1172
+ * connection has auto-threading enabled, the runtime decides — once,
1173
+ * globally, at ingest — whether this message starts a new thread or folds
1174
+ * (as a note) into the thread of the conversation it continues. Set it on
1175
+ * every message of a conversational surface (a chat channel, a DM); the
1176
+ * runtime keys the sequential chain on {@link AutoThreadConfig.key}.
1177
+ *
1178
+ * Marking a link is a no-op unless the connection opted in, so connectors
1179
+ * can set it unconditionally on eligible links. Leave undefined/null for
1180
+ * non-conversational items (issues, events, files). See
1181
+ * {@link Connector.autoThreading}.
1182
+ */
1183
+ autoThread?: AutoThreadConfig | null;
1138
1184
  };
1139
1185
 
1186
+ /**
1187
+ * How the runtime groups a connector's messages into threads when
1188
+ * auto-threading is enabled for the connection.
1189
+ *
1190
+ * - `"sequential"`: a chat **channel**. Each message either continues the
1191
+ * previous message's conversation (folds in as a note) or starts a new
1192
+ * thread, decided by a high-confidence continuation check. Use for
1193
+ * multi-participant channels where consecutive top-level messages often
1194
+ * form one conversation.
1195
+ * - `"fold"`: a **DM** / one-to-one surface. Every message folds into a
1196
+ * single running thread for that {@link AutoThreadConfig.key} (no per-message
1197
+ * judgment). Use for direct messages, which read naturally as one continuous
1198
+ * conversation.
1199
+ */
1200
+ export type AutoThreadMode = "sequential" | "fold";
1201
+
1202
+ /** Per-link auto-threading directive. See {@link NewLink.autoThread}. */
1203
+ export type AutoThreadConfig = {
1204
+ /**
1205
+ * The conversation grouping the sequential chain runs within — typically the
1206
+ * connector's channel id ({@link Link.channelId}). DMs and each channel form
1207
+ * independent chains. Messages are only ever folded into another message
1208
+ * that shares this key.
1209
+ */
1210
+ key: string;
1211
+ /** Channel (`"sequential"`) vs DM (`"fold"`) grouping. */
1212
+ mode: AutoThreadMode;
1213
+ };
1214
+
1140
1215
  /**
1141
1216
  * A new link with notes to save via integrations.saveLink().
1142
1217
  * Creates a thread+link pair, with notes attached to the thread.
@@ -1174,6 +1249,17 @@ export type NewLinkWithNotes = NewLink & {
1174
1249
  * `content` on re-ingest (same contract as `NoteWriteBackResult.externalContent`).
1175
1250
  */
1176
1251
  externalContent?: string;
1252
+ /**
1253
+ * Reports that sending the composed message FAILED (the `onCreateLink`
1254
+ * send could not be delivered). The runtime records it on the opening
1255
+ * note — surfacing a "Failed to send" affordance (Retry / Discard) — and
1256
+ * marks the thread unread. Same contract as
1257
+ * `NoteWriteBackResult.deliveryError`: object records, `null` clears,
1258
+ * omitted leaves untouched. Return the link anyway (so the user's composed
1259
+ * content is preserved in Plot) with this set, rather than returning
1260
+ * `null`, when a compose send fails.
1261
+ */
1262
+ deliveryError?: DeliveryError | null;
1177
1263
  };
1178
1264
  };
1179
1265
 
package/src/tool.ts CHANGED
@@ -287,6 +287,39 @@ export abstract class Tool<TSelf> implements ITool {
287
287
  return this.tools.tasks.cancelAllTasks();
288
288
  }
289
289
 
290
+ /**
291
+ * Schedules a **singleton** task keyed by `key`: re-scheduling under the same
292
+ * key atomically replaces any pending task, so at most one is ever live.
293
+ *
294
+ * Prefer this over `runTask({ runAt })` for recurring/self-renewing jobs
295
+ * (watch renewals, polling, deferred cleanup) — it removes the error-prone
296
+ * "store token, cancel before re-scheduling" bookkeeping that otherwise leaks
297
+ * parallel task chains. See {@link Tasks.scheduleTask}.
298
+ *
299
+ * @param key - Stable identifier scoped to what the task renews
300
+ * @param callback - The callback token created with `this.callback()`
301
+ * @param options.runAt - When to run (required)
302
+ * @returns Promise resolving to the scheduled task's cancellation token
303
+ */
304
+ protected async scheduleTask(
305
+ key: string,
306
+ callback: Callback,
307
+ options: { runAt: Date }
308
+ ): Promise<string | void> {
309
+ return this.tools.tasks.scheduleTask(key, callback, options);
310
+ }
311
+
312
+ /**
313
+ * Cancels the singleton task previously scheduled under `key` (if any).
314
+ * No-op if none exists or it already ran. See {@link Tasks.cancelScheduledTask}.
315
+ *
316
+ * @param key - The same key passed to {@link scheduleTask}
317
+ * @returns Promise that resolves when the cancellation is processed
318
+ */
319
+ protected async cancelScheduledTask(key: string): Promise<void> {
320
+ return this.tools.tasks.cancelScheduledTask(key);
321
+ }
322
+
290
323
  /**
291
324
  * Called before the twist's activate method, starting from the deepest tool dependencies.
292
325
  *
@@ -132,4 +132,56 @@ export abstract class Tasks extends ITool {
132
132
  * @returns Promise that resolves when all cancellations are processed
133
133
  */
134
134
  abstract cancelAllTasks(): Promise<void>;
135
+
136
+ /**
137
+ * Schedules a **singleton** task identified by `key`: scheduling under a key
138
+ * that already has a pending task atomically cancels the existing one and
139
+ * replaces it. At most one scheduled task per `key` is ever live.
140
+ *
141
+ * Use this for any recurring/self-renewing job — webhook/watch renewals,
142
+ * periodic polling, deferred cleanup — instead of hand-managing tokens with
143
+ * `runTask()` + `cancelTask()`. The manual pattern (store the token, cancel
144
+ * it before re-scheduling) is easy to get wrong: a renewal callback that
145
+ * re-schedules itself, combined with any *extra* scheduling call (a
146
+ * re-dispatched `onChannelEnabled`, a re-init), leaks parallel self-
147
+ * perpetuating chains that accumulate forever and can trip the runtime's
148
+ * execution quota. Keying makes that leak impossible by construction.
149
+ *
150
+ * Replacement is atomic on the server, so concurrent executions racing to
151
+ * schedule the same key converge on a single task rather than leaking.
152
+ *
153
+ * @param key - Stable identifier for this logical task. Scope it to what it
154
+ * renews, e.g. `` `watch-renewal:${folderId}` ``.
155
+ * @param callback - Callback created with `this.callback()`
156
+ * @param options.runAt - When to run. Required: keying only applies to
157
+ * scheduled tasks (immediate tasks go straight to the queue).
158
+ * @returns Promise resolving to the cancellation token for the scheduled task
159
+ *
160
+ * @example
161
+ * ```typescript
162
+ * const cb = await this.callback(this.renewWatch, folderId);
163
+ * await this.scheduleTask(`watch-renewal:${folderId}`, cb, { runAt });
164
+ * // ...later, on disable:
165
+ * await this.cancelScheduledTask(`watch-renewal:${folderId}`);
166
+ * ```
167
+ */
168
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
169
+ abstract scheduleTask(
170
+ key: string,
171
+ callback: Callback,
172
+ options: { runAt: Date }
173
+ ): Promise<string | void>;
174
+
175
+ /**
176
+ * Cancels the singleton task previously scheduled under `key` (if any).
177
+ *
178
+ * No error is thrown if no task exists for the key or it has already run.
179
+ * Pair this with {@link scheduleTask} in teardown paths (e.g.
180
+ * `onChannelDisabled`, `stopSync`).
181
+ *
182
+ * @param key - The same key passed to {@link scheduleTask}
183
+ * @returns Promise that resolves when the cancellation is processed
184
+ */
185
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
186
+ abstract cancelScheduledTask(key: string): Promise<void>;
135
187
  }
package/src/twist.ts CHANGED
@@ -294,6 +294,39 @@ export abstract class Twist<TSelf> {
294
294
  return this.tools.tasks.cancelAllTasks();
295
295
  }
296
296
 
297
+ /**
298
+ * Schedules a **singleton** task keyed by `key`: re-scheduling under the same
299
+ * key atomically replaces any pending task, so at most one is ever live.
300
+ *
301
+ * Prefer this over `runTask({ runAt })` for recurring/self-renewing jobs
302
+ * (watch renewals, polling, deferred cleanup) — it removes the error-prone
303
+ * "store token, cancel before re-scheduling" bookkeeping that otherwise leaks
304
+ * parallel task chains. See {@link Tasks.scheduleTask}.
305
+ *
306
+ * @param key - Stable identifier scoped to what the task renews
307
+ * @param callback - The callback token created with `this.callback()`
308
+ * @param options.runAt - When to run (required)
309
+ * @returns Promise resolving to the scheduled task's cancellation token
310
+ */
311
+ protected async scheduleTask(
312
+ key: string,
313
+ callback: Callback,
314
+ options: { runAt: Date }
315
+ ): Promise<string | void> {
316
+ return this.tools.tasks.scheduleTask(key, callback, options);
317
+ }
318
+
319
+ /**
320
+ * Cancels the singleton task previously scheduled under `key` (if any).
321
+ * No-op if none exists or it already ran. See {@link Tasks.cancelScheduledTask}.
322
+ *
323
+ * @param key - The same key passed to {@link scheduleTask}
324
+ * @returns Promise that resolves when the cancellation is processed
325
+ */
326
+ protected async cancelScheduledTask(key: string): Promise<void> {
327
+ return this.tools.tasks.cancelScheduledTask(key);
328
+ }
329
+
297
330
  /**
298
331
  * Called when the twist is installed by a user.
299
332
  *