@plotday/twister 0.60.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 (97) hide show
  1. package/dist/connector.d.ts +44 -1
  2. package/dist/connector.d.ts.map +1 -1
  3. package/dist/connector.js +21 -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 +68 -36
  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 -2
  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 +2 -5
  51. package/dist/docs/types/plot.NoteUpdate.html +1 -1
  52. package/dist/docs/types/plot.PlanOperation.html +1 -1
  53. package/dist/llm-docs/connector.d.ts +1 -1
  54. package/dist/llm-docs/connector.d.ts.map +1 -1
  55. package/dist/llm-docs/connector.js +1 -1
  56. package/dist/llm-docs/connector.js.map +1 -1
  57. package/dist/llm-docs/plot.d.ts +1 -1
  58. package/dist/llm-docs/plot.d.ts.map +1 -1
  59. package/dist/llm-docs/plot.js +1 -1
  60. package/dist/llm-docs/plot.js.map +1 -1
  61. package/dist/llm-docs/tool.d.ts +1 -1
  62. package/dist/llm-docs/tool.d.ts.map +1 -1
  63. package/dist/llm-docs/tool.js +1 -1
  64. package/dist/llm-docs/tool.js.map +1 -1
  65. package/dist/llm-docs/tools/tasks.d.ts +1 -1
  66. package/dist/llm-docs/tools/tasks.d.ts.map +1 -1
  67. package/dist/llm-docs/tools/tasks.js +1 -1
  68. package/dist/llm-docs/tools/tasks.js.map +1 -1
  69. package/dist/llm-docs/twist.d.ts +1 -1
  70. package/dist/llm-docs/twist.d.ts.map +1 -1
  71. package/dist/llm-docs/twist.js +1 -1
  72. package/dist/llm-docs/twist.js.map +1 -1
  73. package/dist/plot.d.ts +77 -0
  74. package/dist/plot.d.ts.map +1 -1
  75. package/dist/plot.js.map +1 -1
  76. package/dist/tool.d.ts +25 -0
  77. package/dist/tool.d.ts.map +1 -1
  78. package/dist/tool.js +27 -0
  79. package/dist/tool.js.map +1 -1
  80. package/dist/tools/tasks.d.ts +46 -0
  81. package/dist/tools/tasks.d.ts.map +1 -1
  82. package/dist/tools/tasks.js.map +1 -1
  83. package/dist/twist.d.ts +25 -0
  84. package/dist/twist.d.ts.map +1 -1
  85. package/dist/twist.js +27 -0
  86. package/dist/twist.js.map +1 -1
  87. package/package.json +1 -1
  88. package/src/connector.ts +46 -1
  89. package/src/llm-docs/connector.ts +1 -1
  90. package/src/llm-docs/plot.ts +1 -1
  91. package/src/llm-docs/tool.ts +1 -1
  92. package/src/llm-docs/tools/tasks.ts +1 -1
  93. package/src/llm-docs/twist.ts +1 -1
  94. package/src/plot.ts +80 -0
  95. package/src/tool.ts +33 -0
  96. package/src/tools/tasks.ts +52 -0
  97. package/src/twist.ts +33 -0
package/src/plot.ts CHANGED
@@ -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;
@@ -1141,8 +1167,51 @@ export type NewLink = Partial<
1141
1167
  * an event vs a subscribed copy) so clients display it as the primary.
1142
1168
  */
1143
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;
1144
1184
  };
1145
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
+
1146
1215
  /**
1147
1216
  * A new link with notes to save via integrations.saveLink().
1148
1217
  * Creates a thread+link pair, with notes attached to the thread.
@@ -1180,6 +1249,17 @@ export type NewLinkWithNotes = NewLink & {
1180
1249
  * `content` on re-ingest (same contract as `NoteWriteBackResult.externalContent`).
1181
1250
  */
1182
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;
1183
1263
  };
1184
1264
  };
1185
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
  *