@plotday/twister 0.47.0 → 0.48.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 (72) hide show
  1. package/bin/commands/generate.js +5 -5
  2. package/bin/commands/generate.js.map +1 -1
  3. package/bin/utils/bundle.js +14 -0
  4. package/bin/utils/bundle.js.map +1 -1
  5. package/dist/docs/assets/hierarchy.js +1 -1
  6. package/dist/docs/assets/search.js +1 -1
  7. package/dist/docs/classes/index.Connector.html +1 -1
  8. package/dist/docs/classes/index.Imap.html +1 -1
  9. package/dist/docs/classes/index.Options.html +1 -1
  10. package/dist/docs/classes/index.Smtp.html +1 -1
  11. package/dist/docs/classes/tools_network.Network.html +1 -1
  12. package/dist/docs/classes/tools_plot.Plot.html +1 -1
  13. package/dist/docs/classes/tools_store.Store.html +1 -1
  14. package/dist/docs/classes/tools_tasks.Tasks.html +1 -1
  15. package/dist/docs/documents/CLI_Reference.html +6 -4
  16. package/dist/docs/enums/tools_integrations.AuthProvider.html +3 -1
  17. package/dist/docs/hierarchy.html +1 -1
  18. package/dist/docs/types/tools_integrations.AuthToken.html +4 -4
  19. package/dist/docs/types/tools_integrations.Authorization.html +4 -4
  20. package/dist/llm-docs/tools/integrations.d.ts +1 -1
  21. package/dist/llm-docs/tools/integrations.d.ts.map +1 -1
  22. package/dist/llm-docs/tools/integrations.js +1 -1
  23. package/dist/llm-docs/tools/integrations.js.map +1 -1
  24. package/dist/tools/integrations.d.ts +3 -1
  25. package/dist/tools/integrations.d.ts.map +1 -1
  26. package/dist/tools/integrations.js +2 -0
  27. package/dist/tools/integrations.js.map +1 -1
  28. package/package.json +2 -1
  29. package/src/connector.ts +366 -0
  30. package/src/creator-docs.ts +29 -0
  31. package/src/index.ts +10 -0
  32. package/src/llm-docs/connector.ts +8 -0
  33. package/src/llm-docs/index.ts +48 -0
  34. package/src/llm-docs/options.ts +8 -0
  35. package/src/llm-docs/plot.ts +8 -0
  36. package/src/llm-docs/schedule.ts +8 -0
  37. package/src/llm-docs/tag.ts +8 -0
  38. package/src/llm-docs/tool.ts +8 -0
  39. package/src/llm-docs/tools/ai.ts +8 -0
  40. package/src/llm-docs/tools/callbacks.ts +8 -0
  41. package/src/llm-docs/tools/imap.ts +8 -0
  42. package/src/llm-docs/tools/integrations.ts +8 -0
  43. package/src/llm-docs/tools/network.ts +8 -0
  44. package/src/llm-docs/tools/plot.ts +8 -0
  45. package/src/llm-docs/tools/smtp.ts +8 -0
  46. package/src/llm-docs/tools/store.ts +8 -0
  47. package/src/llm-docs/tools/tasks.ts +8 -0
  48. package/src/llm-docs/tools/twists.ts +8 -0
  49. package/src/llm-docs/twist-guide-template.ts +8 -0
  50. package/src/llm-docs/twist.ts +8 -0
  51. package/src/options.ts +115 -0
  52. package/src/plot.ts +1068 -0
  53. package/src/schedule.ts +203 -0
  54. package/src/tag.ts +44 -0
  55. package/src/tool.ts +377 -0
  56. package/src/tools/ai.ts +845 -0
  57. package/src/tools/callbacks.ts +134 -0
  58. package/src/tools/imap.ts +266 -0
  59. package/src/tools/index.ts +10 -0
  60. package/src/tools/integrations.ts +328 -0
  61. package/src/tools/network.ts +240 -0
  62. package/src/tools/plot.ts +692 -0
  63. package/src/tools/smtp.ts +166 -0
  64. package/src/tools/store.ts +149 -0
  65. package/src/tools/tasks.ts +137 -0
  66. package/src/tools/twists.ts +228 -0
  67. package/src/twist-guide.ts +9 -0
  68. package/src/twist.ts +435 -0
  69. package/src/utils/hash.ts +8 -0
  70. package/src/utils/serializable.ts +54 -0
  71. package/src/utils/types.ts +130 -0
  72. package/src/utils/uuid.ts +9 -0
package/src/twist.ts ADDED
@@ -0,0 +1,435 @@
1
+ import { type Action, type Actor, type ActorId, type Link, type Note, type Thread, Uuid } from "./plot";
2
+ import type { Tag } from "./tag";
3
+ import { type ITool } from "./tool";
4
+ import type { Callback } from "./tools/callbacks";
5
+ import type { Serializable } from "./utils/serializable";
6
+ import type { InferTools, ToolBuilder, ToolShed } from "./utils/types";
7
+
8
+ /**
9
+ * Base class for all twists.
10
+ *
11
+ * A twist is installed at the workspace level and is owned by a single user
12
+ * (see `this.userId`). It has no inherent priority scope: threads, notes, and
13
+ * links it creates are filed against the owner's priorities, with automatic
14
+ * priority matching when no explicit target is provided.
15
+ *
16
+ * Override `build()` to declare tool dependencies and lifecycle methods to
17
+ * handle events.
18
+ *
19
+ * @example
20
+ * ```typescript
21
+ * class FlatteringTwist extends Twist<FlatteringTwist> {
22
+ * build(build: ToolBuilder) {
23
+ * return {
24
+ * plot: build(Plot),
25
+ * };
26
+ * }
27
+ *
28
+ * async activate() {
29
+ * await this.tools.plot.createThread({
30
+ * title: "Hello, good looking!",
31
+ * });
32
+ * }
33
+ * }
34
+ * ```
35
+ */
36
+ export abstract class Twist<TSelf> {
37
+ /**
38
+ * When `true`, users may install multiple instances of this twist within
39
+ * the same scope (personal workspace or team). Each instance must have a
40
+ * distinct name.
41
+ *
42
+ * Defaults to `false` (single instance per scope).
43
+ *
44
+ * @example
45
+ * ```typescript
46
+ * class WorkflowTwist extends Twist<WorkflowTwist> {
47
+ * static readonly multipleInstances = true;
48
+ * // ...
49
+ * }
50
+ * ```
51
+ */
52
+ static readonly multipleInstances?: boolean;
53
+
54
+ /**
55
+ * The user ID (`twist_instance.owner_id`) that installed this twist.
56
+ * Populated by the runtime before any lifecycle method runs.
57
+ */
58
+ protected userId!: Uuid;
59
+
60
+ constructor(protected id: Uuid, private toolShed: ToolShed) {}
61
+
62
+ /**
63
+ * Gets the initialized tools for this twist.
64
+ * @throws Error if called before initialization is complete
65
+ */
66
+ protected get tools(): InferTools<TSelf> {
67
+ return this.toolShed.getTools<InferTools<TSelf>>();
68
+ }
69
+
70
+ /**
71
+ * Declares tool dependencies for this twist.
72
+ * Return an object mapping tool names to build() promises.
73
+ *
74
+ * @param build - The build function to use for declaring dependencies
75
+ * @returns Object mapping tool names to tool promises
76
+ *
77
+ * @example
78
+ * ```typescript
79
+ * build(build: ToolBuilder) {
80
+ * return {
81
+ * plot: build(Plot),
82
+ * calendar: build(GoogleCalendar, { apiKey: "..." }),
83
+ * };
84
+ * }
85
+ * ```
86
+ */
87
+ abstract build(build: ToolBuilder): Record<string, Promise<ITool>>;
88
+
89
+ /**
90
+ * Creates a persistent callback to a method on this twist.
91
+ *
92
+ * ExtraArgs are strongly typed to match the method's signature. They must be serializable.
93
+ *
94
+ * @param fn - The method to callback
95
+ * @param extraArgs - Additional arguments to pass (type-checked, must be serializable)
96
+ * @returns Promise resolving to a persistent callback token
97
+ *
98
+ * @example
99
+ * ```typescript
100
+ * const callback = await this.callback(this.onWebhook, "calendar", 123);
101
+ * ```
102
+ */
103
+ protected callback<
104
+ TArgs extends Serializable[],
105
+ Fn extends (...args: TArgs) => any
106
+ >(fn: Fn, ...extraArgs: TArgs): Promise<Callback>;
107
+ // Overload when caller provides the first argument
108
+ protected callback<
109
+ TArgs extends Serializable[],
110
+ Fn extends (arg1: any, ...extraArgs: TArgs) => any
111
+ >(fn: Fn, ...extraArgs: TArgs): Promise<Callback>;
112
+ protected async callback<
113
+ TArgs extends Serializable[],
114
+ Fn extends (...args: any[]) => any
115
+ >(fn: Fn, ...extraArgs: TArgs): Promise<Callback> {
116
+ return this.tools.callbacks.create(fn, ...extraArgs);
117
+ }
118
+
119
+ /**
120
+ * Like callback(), but for an Action, which receives the action as the first argument.
121
+ *
122
+ * @param fn - The method to callback
123
+ * @param extraArgs - Additional arguments to pass after the action
124
+ * @returns Promise resolving to a persistent callback token
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * const callback = await this.actionCallback(this.doSomething, 123);
129
+ * const action: Action = {
130
+ * type: ActionType.callback,
131
+ * title: "Do Something",
132
+ * callback,
133
+ * };
134
+ * ```
135
+ */
136
+ protected async actionCallback<
137
+ TArgs extends Serializable[],
138
+ Fn extends (action: Action, ...extraArgs: TArgs) => any
139
+ >(fn: Fn, ...extraArgs: TArgs): Promise<Callback> {
140
+ return this.tools.callbacks.create(fn, ...extraArgs);
141
+ }
142
+
143
+ /**
144
+ * Deletes a specific callback by its token.
145
+ *
146
+ * @param token - The callback token to delete
147
+ * @returns Promise that resolves when the callback is deleted
148
+ */
149
+ protected async deleteCallback(token: Callback): Promise<void> {
150
+ return this.tools.callbacks.delete(token);
151
+ }
152
+
153
+ /**
154
+ * Deletes all callbacks for this twist.
155
+ *
156
+ * @returns Promise that resolves when all callbacks are deleted
157
+ */
158
+ protected async deleteAllCallbacks(): Promise<void> {
159
+ return this.tools.callbacks.deleteAll();
160
+ }
161
+
162
+ /**
163
+ * Executes a callback by its token inline in the current execution.
164
+ *
165
+ * **Use `this.runTask()` instead for batch continuations and long-running work.**
166
+ * `this.run()` executes inline, sharing the current request count (~1000 limit)
167
+ * and blocking the HTTP response. This causes timeouts when used in lifecycle
168
+ * methods like `onChannelEnabled` or `syncBatch` continuations.
169
+ *
170
+ * `this.run()` is appropriate when you need the callback's **return value** —
171
+ * e.g., running a parent callback token that returns data. For fire-and-forget
172
+ * work, always prefer `this.runTask()`.
173
+ *
174
+ * @param token - The callback token to execute
175
+ * @param args - Optional arguments to pass to the callback
176
+ * @returns Promise resolving to the callback result
177
+ */
178
+ protected async run(token: Callback, ...args: []): Promise<any> {
179
+ return this.tools.callbacks.run(token, ...args);
180
+ }
181
+
182
+ /**
183
+ * Retrieves a value from persistent storage by key.
184
+ *
185
+ * Values are automatically deserialized using SuperJSON, which
186
+ * properly restores Date objects, Maps, Sets, and other complex types.
187
+ *
188
+ * @template T - The expected type of the stored value (must be Serializable)
189
+ * @param key - The storage key to retrieve
190
+ * @returns Promise resolving to the stored value or null
191
+ */
192
+ protected async get<T extends import("./index").Serializable>(
193
+ key: string
194
+ ): Promise<T | null> {
195
+ return this.tools.store.get(key);
196
+ }
197
+
198
+ /**
199
+ * Stores a value in persistent storage.
200
+ *
201
+ * The value will be serialized using SuperJSON and stored persistently.
202
+ * SuperJSON automatically handles Date objects, Maps, Sets, undefined values,
203
+ * and other complex types that standard JSON doesn't support.
204
+ *
205
+ * **Important**: Functions and Symbols cannot be stored.
206
+ * **For function references**: Use callbacks instead of storing functions directly.
207
+ *
208
+ * @example
209
+ * ```typescript
210
+ * // ✅ Date objects are preserved
211
+ * await this.set("sync_state", {
212
+ * lastSync: new Date(),
213
+ * minDate: new Date(2024, 0, 1)
214
+ * });
215
+ *
216
+ * // ✅ undefined is now supported
217
+ * await this.set("data", { name: "test", optional: undefined });
218
+ *
219
+ * // ❌ WRONG: Cannot store functions directly
220
+ * await this.set("handler", this.myHandler);
221
+ *
222
+ * // ✅ CORRECT: Create a callback token first
223
+ * const token = await this.callback(this.myHandler, "arg1", "arg2");
224
+ * await this.set("handler_token", token);
225
+ *
226
+ * // Later, execute the callback
227
+ * const token = await this.get<string>("handler_token");
228
+ * await this.run(token, args);
229
+ * ```
230
+ *
231
+ * @template T - The type of value being stored (must be Serializable)
232
+ * @param key - The storage key to use
233
+ * @param value - The value to store (must be SuperJSON-serializable)
234
+ * @returns Promise that resolves when the value is stored
235
+ */
236
+ protected async set<T extends import("./index").Serializable>(
237
+ key: string,
238
+ value: T
239
+ ): Promise<void> {
240
+ return this.tools.store.set(key, value);
241
+ }
242
+
243
+ /**
244
+ * Removes a specific key from persistent storage.
245
+ *
246
+ * @param key - The storage key to remove
247
+ * @returns Promise that resolves when the key is removed
248
+ */
249
+ protected async clear(key: string): Promise<void> {
250
+ return this.tools.store.clear(key);
251
+ }
252
+
253
+ /**
254
+ * Removes all keys from this twist's storage.
255
+ *
256
+ * @returns Promise that resolves when all keys are removed
257
+ */
258
+ protected async clearAll(): Promise<void> {
259
+ return this.tools.store.clearAll();
260
+ }
261
+
262
+ /**
263
+ * Queues a callback to execute in a separate worker context.
264
+ *
265
+ * @param callback - The callback token created with `this.callback()`
266
+ * @param options - Optional configuration for the execution
267
+ * @param options.runAt - If provided, schedules execution at this time; otherwise runs immediately
268
+ * @returns Promise resolving to a cancellation token (only for scheduled executions)
269
+ */
270
+ protected async runTask(
271
+ callback: Callback,
272
+ options?: { runAt?: Date }
273
+ ): Promise<string | void> {
274
+ return this.tools.tasks.runTask(callback, options);
275
+ }
276
+
277
+ /**
278
+ * Cancels a previously scheduled execution.
279
+ *
280
+ * @param token - The cancellation token returned by runTask() with runAt option
281
+ * @returns Promise that resolves when the cancellation is processed
282
+ */
283
+ protected async cancelTask(token: string): Promise<void> {
284
+ return this.tools.tasks.cancelTask(token);
285
+ }
286
+
287
+ /**
288
+ * Cancels all scheduled executions for this twist.
289
+ *
290
+ * @returns Promise that resolves when all cancellations are processed
291
+ */
292
+ protected async cancelAllTasks(): Promise<void> {
293
+ return this.tools.tasks.cancelAllTasks();
294
+ }
295
+
296
+ /**
297
+ * Called when the twist is installed by a user.
298
+ *
299
+ * This method should contain initialization logic such as seeding
300
+ * initial threads, configuring webhooks, or establishing external
301
+ * connections. When it runs, `this.userId` is already populated with
302
+ * the installing user's ID.
303
+ *
304
+ * @param context - Optional context containing the actor who triggered activation
305
+ * @returns Promise that resolves when activation is complete
306
+ */
307
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
308
+ activate(context?: { actor: Actor }): Promise<void> {
309
+ return Promise.resolve();
310
+ }
311
+
312
+ /**
313
+ * Called when a new version of the twist is deployed.
314
+ *
315
+ * This method should contain migration logic for updating old data structures
316
+ * or setting up new resources that weren't needed by the previous version.
317
+ * It is called once per active twist_instance with the new version.
318
+ *
319
+ * @returns Promise that resolves when upgrade is complete
320
+ */
321
+ upgrade(): Promise<void> {
322
+ return Promise.resolve();
323
+ }
324
+
325
+ /**
326
+ * Called when the twist's options configuration changes.
327
+ *
328
+ * Override to react to option changes, e.g. archiving items when a sync
329
+ * type is toggled off, or starting sync when a type is toggled on.
330
+ *
331
+ * @param oldOptions - The previously resolved options
332
+ * @param newOptions - The newly resolved options
333
+ * @returns Promise that resolves when the change is handled
334
+ */
335
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
336
+ onOptionsChanged(
337
+ oldOptions: Record<string, any>,
338
+ newOptions: Record<string, any>
339
+ ): Promise<void> {
340
+ return Promise.resolve();
341
+ }
342
+
343
+ /**
344
+ * Called when the twist is uninstalled.
345
+ *
346
+ * This method should contain cleanup logic such as removing webhooks,
347
+ * cleaning up external resources, or performing final data operations.
348
+ *
349
+ * @returns Promise that resolves when deactivation is complete
350
+ */
351
+ deactivate(): Promise<void> {
352
+ return Promise.resolve();
353
+ }
354
+
355
+ /**
356
+ * Called when a thread created by this twist is updated.
357
+ * Override to implement two-way sync with an external system.
358
+ *
359
+ * @param thread - The updated thread
360
+ * @param changes - Tag additions and removals on the thread
361
+ */
362
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
363
+ onThreadUpdated(
364
+ thread: Thread,
365
+ changes: {
366
+ tagsAdded: Record<Tag, ActorId[]>;
367
+ tagsRemoved: Record<Tag, ActorId[]>;
368
+ }
369
+ ): Promise<void> {
370
+ return Promise.resolve();
371
+ }
372
+
373
+ /**
374
+ * Called when a note is created on a thread created by this twist.
375
+ * Override to implement two-way sync (e.g. syncing notes as comments).
376
+ *
377
+ * Notes created by the twist itself are filtered out to prevent loops.
378
+ *
379
+ * Returning a string sets the note's `key` for future upsert matching,
380
+ * linking the Plot note to its external counterpart so that subsequent
381
+ * syncs (reactions, edits) update the existing note instead of creating duplicates.
382
+ *
383
+ * @param note - The newly created note
384
+ * @returns Optional note key for external deduplication
385
+ */
386
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
387
+ onNoteCreated(note: Note, ...args: any[]): Promise<string | void> {
388
+ return Promise.resolve();
389
+ }
390
+
391
+ /**
392
+ * Called when a link is created in a connected source channel.
393
+ * Requires `link: true` in Plot options.
394
+ *
395
+ * @param link - The newly created link
396
+ * @param notes - Notes on the link's thread
397
+ */
398
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
399
+ onLinkCreated(link: Link, notes: Note[]): Promise<void> {
400
+ return Promise.resolve();
401
+ }
402
+
403
+ /**
404
+ * Called when a link in a connected source channel is updated.
405
+ * Requires `link: true` in Plot options.
406
+ *
407
+ * @param link - The updated link
408
+ * @param notes - Notes on the link's thread (optional)
409
+ */
410
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
411
+ onLinkUpdated(link: Link, notes?: Note[]): Promise<void> {
412
+ return Promise.resolve();
413
+ }
414
+
415
+ /**
416
+ * Called when a note is created on a thread with a link from a connected channel.
417
+ * Requires `link: true` in Plot options.
418
+ *
419
+ * @param note - The newly created note
420
+ * @param link - The link associated with the thread
421
+ */
422
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
423
+ onLinkNoteCreated(note: Note, link: Link): Promise<void> {
424
+ return Promise.resolve();
425
+ }
426
+
427
+ /**
428
+ * Waits for tool initialization to complete.
429
+ * Called automatically by the entrypoint before lifecycle methods.
430
+ * @internal
431
+ */
432
+ async waitForReady(): Promise<void> {
433
+ await this.toolShed.waitForReady();
434
+ }
435
+ }
@@ -0,0 +1,8 @@
1
+ export function quickHash(str: string) {
2
+ let hash = 0x811c9dc5;
3
+ for (let i = 0; i < str.length; ++i) {
4
+ hash ^= str.charCodeAt(i);
5
+ hash = (hash * 0x01000193) >>> 0;
6
+ }
7
+ return hash.toString(16).padStart(8, "0");
8
+ }
@@ -0,0 +1,54 @@
1
+ /**
2
+ * Types supported by SuperJSON serialization.
3
+ *
4
+ * SuperJSON extends standard JSON serialization to support additional JavaScript types
5
+ * while maintaining type safety and preventing common serialization errors.
6
+ *
7
+ * Supported types:
8
+ * - Primitives: string, number, boolean, null, undefined
9
+ * - Complex types: Date, RegExp, Map, Set, Error, URL, BigInt
10
+ * - Collections: Arrays and objects (recursively)
11
+ *
12
+ * NOT supported (will throw validation errors):
13
+ * - Functions
14
+ * - Symbols
15
+ * - Circular references
16
+ * - Custom class instances (unless explicitly registered)
17
+ */
18
+ export type Serializable =
19
+ | string
20
+ | number
21
+ | boolean
22
+ | null
23
+ | undefined
24
+ | Date
25
+ | RegExp
26
+ | Error
27
+ | URL
28
+ | bigint
29
+ | SerializableArray
30
+ | SerializableObject
31
+ | SerializableMap
32
+ | SerializableSet;
33
+
34
+ /**
35
+ * Array of serializable values
36
+ */
37
+ export interface SerializableArray extends Array<Serializable> {}
38
+
39
+ /**
40
+ * Object with string keys and serializable values
41
+ */
42
+ export interface SerializableObject {
43
+ [key: string]: Serializable;
44
+ }
45
+
46
+ /**
47
+ * Map with serializable keys and values
48
+ */
49
+ export interface SerializableMap extends Map<Serializable, Serializable> {}
50
+
51
+ /**
52
+ * Set with serializable values
53
+ */
54
+ export interface SerializableSet extends Set<Serializable> {}
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Internal type utilities for SDK implementation.
3
+ *
4
+ * This file contains advanced TypeScript type utilities used internally
5
+ * by the SDK to provide type-safe APIs. Most developers don't need to
6
+ * reference these types directly - they work behind the scenes to power
7
+ * the Twist and Tool APIs.
8
+ *
9
+ * @internal
10
+ */
11
+ import type { Options, OptionsSchema, ResolvedOptions } from "../options";
12
+ import type { Callbacks } from "../tools/callbacks";
13
+ import type { Store } from "../tools/store";
14
+ import type { Tasks } from "../tools/tasks";
15
+
16
+ export type { Serializable } from "./serializable";
17
+
18
+ // ============================================================================
19
+ // Type utilities for twist.ts
20
+ // ============================================================================
21
+
22
+ /**
23
+ * Unwraps Promise types to their resolved values.
24
+ * Converts { foo: Promise<string> } to { foo: string }
25
+ */
26
+ export type PromiseValues<T> = {
27
+ [K in keyof T]: T[K] extends Promise<infer U> ? U : T[K];
28
+ };
29
+
30
+ /**
31
+ * Extracts the return type from an instance build method.
32
+ */
33
+ export type ExtractBuildReturn<T> = T extends {
34
+ build: (...args: any[]) => infer R;
35
+ }
36
+ ? R
37
+ : {};
38
+
39
+ /**
40
+ * Built-in tools available to all twists and tools.
41
+ */
42
+ export type BuiltInTools = {
43
+ callbacks: Callbacks;
44
+ store: Store;
45
+ tasks: Tasks;
46
+ };
47
+
48
+ /**
49
+ * Infers the complete set of tools available to an twist or tool,
50
+ * combining tools declared in build with built-in tools.
51
+ */
52
+ export type InferTools<T> = PromiseValues<ExtractBuildReturn<T>> & BuiltInTools;
53
+
54
+ /**
55
+ * Infers the options type from a constructor's second parameter.
56
+ */
57
+ export type InferOptions<T> = T extends {
58
+ Options: infer O;
59
+ }
60
+ ? O
61
+ : unknown;
62
+
63
+ /**
64
+ * Function type for building tool dependencies.
65
+ * Used in build methods to request tool instances.
66
+ */
67
+ export type ToolBuilder = {
68
+ <T extends OptionsSchema>(
69
+ ToolClass: typeof Options,
70
+ schema: T
71
+ ): Promise<ResolvedOptions<T>>;
72
+ <TC extends abstract new (...args: any) => any>(
73
+ ToolClass: TC,
74
+ options?: InferOptions<TC>
75
+ ): Promise<InstanceType<TC>>;
76
+ };
77
+
78
+ /**
79
+ * Interface for managing tool initialization and lifecycle.
80
+ * Implemented by the twist runtime to provide tools to twists and tools.
81
+ */
82
+ export interface ToolShed {
83
+ /**
84
+ * Build function for requesting tool dependencies
85
+ */
86
+ build: ToolBuilder;
87
+
88
+ /**
89
+ * Whether tools are ready (all promises resolved)
90
+ */
91
+ readonly ready: boolean;
92
+
93
+ /**
94
+ * Wait for all tool promises to resolve
95
+ */
96
+ waitForReady(): Promise<void>;
97
+
98
+ /**
99
+ * Get resolved tools (throws if not ready)
100
+ */
101
+ getTools<T>(): T;
102
+ }
103
+
104
+ // ============================================================================
105
+ // Type utilities for callbacks.ts
106
+ // ============================================================================
107
+
108
+ /**
109
+ * Represents a valid JSON value.
110
+ *
111
+ * This type ensures type safety for data that needs to be serialized to JSON,
112
+ * such as metadata fields, webhook payloads, and stored data.
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * const meta: JSONValue = {
117
+ * name: "Example",
118
+ * count: 42,
119
+ * tags: ["foo", "bar"],
120
+ * nested: { value: true }
121
+ * };
122
+ * ```
123
+ */
124
+ export type JSONValue =
125
+ | string
126
+ | number
127
+ | boolean
128
+ | null
129
+ | { [key: string]: JSONValue }
130
+ | JSONValue[];
@@ -0,0 +1,9 @@
1
+ import { v7 as uuidv7 } from "uuid";
2
+
3
+ export type Uuid = string & { readonly __brand: "UuidV7" };
4
+
5
+ export namespace Uuid {
6
+ export function Generate(): Uuid {
7
+ return uuidv7() as Uuid;
8
+ }
9
+ }