@plotday/twister 0.52.0 → 0.53.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.
- package/bin/commands/deploy.js +4 -0
- package/bin/commands/deploy.js.map +1 -1
- package/bin/templates/AGENTS.template.md +6 -10
- package/cli/templates/AGENTS.template.md +6 -10
- package/dist/connector.d.ts +164 -10
- package/dist/connector.d.ts.map +1 -1
- package/dist/connector.js +78 -4
- package/dist/connector.js.map +1 -1
- package/dist/docs/assets/hierarchy.js +1 -1
- package/dist/docs/assets/navigation.js +1 -1
- package/dist/docs/assets/search.js +1 -1
- package/dist/docs/classes/index.Connector.html +74 -26
- package/dist/docs/classes/index.FileNotFoundError.html +2 -0
- package/dist/docs/classes/index.Files.html +16 -0
- package/dist/docs/classes/index.Imap.html +1 -1
- package/dist/docs/classes/index.Options.html +1 -1
- package/dist/docs/classes/index.Smtp.html +1 -1
- package/dist/docs/classes/tool.ITool.html +1 -1
- package/dist/docs/classes/tools_ai.AI.html +1 -1
- package/dist/docs/classes/tools_callbacks.Callbacks.html +1 -1
- package/dist/docs/classes/tools_integrations.Integrations.html +17 -23
- package/dist/docs/classes/tools_network.Network.html +1 -1
- package/dist/docs/classes/tools_plot.Plot.html +1 -1
- package/dist/docs/classes/tools_store.Store.html +1 -1
- package/dist/docs/classes/tools_tasks.Tasks.html +1 -1
- package/dist/docs/classes/tools_twists.Twists.html +1 -1
- package/dist/docs/documents/Building_Connectors.html +1 -1
- package/dist/docs/enums/plot.ActionType.html +10 -8
- package/dist/docs/enums/plot.ActorType.html +4 -4
- package/dist/docs/enums/plot.ConferencingProvider.html +6 -6
- package/dist/docs/enums/plot.ThemeColor.html +9 -9
- package/dist/docs/enums/tag.Tag.html +16 -46
- package/dist/docs/enums/tools_integrations.AuthProvider.html +13 -13
- package/dist/docs/hierarchy.html +1 -1
- package/dist/docs/media/AGENTS.md +22 -14
- package/dist/docs/modules/index.html +1 -1
- package/dist/docs/modules/plot.html +1 -1
- package/dist/docs/modules/tools_integrations.html +1 -1
- package/dist/docs/types/index.CreateLinkDraft.html +30 -7
- package/dist/docs/types/index.NoteWriteBackResult.html +3 -3
- package/dist/docs/types/index.ReactionCapabilities.html +17 -0
- package/dist/docs/types/index.ResolvedRecipient.html +13 -0
- package/dist/docs/types/plot.Action.html +9 -2
- package/dist/docs/types/plot.Actor.html +5 -5
- package/dist/docs/types/plot.ActorId.html +1 -1
- package/dist/docs/types/plot.Contact.html +4 -4
- package/dist/docs/types/plot.ContentType.html +1 -1
- package/dist/docs/types/plot.Link.html +16 -16
- package/dist/docs/types/plot.LinkUpdate.html +1 -1
- package/dist/docs/types/plot.NewActor.html +1 -1
- package/dist/docs/types/plot.NewContact.html +1 -1
- package/dist/docs/types/plot.NewLink.html +1 -1
- package/dist/docs/types/plot.NewLinkWithNotes.html +1 -1
- package/dist/docs/types/plot.NewNote.html +4 -2
- package/dist/docs/types/plot.NewPriority.html +1 -1
- package/dist/docs/types/plot.NewReactions.html +4 -0
- package/dist/docs/types/plot.NewTags.html +1 -1
- package/dist/docs/types/plot.NewThread.html +4 -2
- package/dist/docs/types/plot.NewThreadWithNotes.html +1 -1
- package/dist/docs/types/plot.Note.html +1 -1
- package/dist/docs/types/plot.NoteUpdate.html +4 -2
- package/dist/docs/types/plot.PlanOperation.html +1 -1
- package/dist/docs/types/plot.Priority.html +6 -6
- package/dist/docs/types/plot.PriorityUpdate.html +1 -1
- package/dist/docs/types/plot.Reaction.html +13 -0
- package/dist/docs/types/plot.Reactions.html +3 -0
- package/dist/docs/types/plot.Tags.html +1 -1
- package/dist/docs/types/plot.Thread.html +1 -1
- package/dist/docs/types/plot.ThreadAccessLevel.html +1 -1
- package/dist/docs/types/plot.ThreadCommon.html +9 -5
- package/dist/docs/types/plot.ThreadFilter.html +2 -2
- package/dist/docs/types/plot.ThreadMeta.html +1 -1
- package/dist/docs/types/plot.ThreadType.html +1 -1
- package/dist/docs/types/plot.ThreadUpdate.html +1 -1
- package/dist/docs/types/plot.ThreadWithNotes.html +1 -1
- package/dist/docs/types/tools_integrations.ArchiveLinkFilter.html +5 -5
- package/dist/docs/types/tools_integrations.AuthToken.html +4 -4
- package/dist/docs/types/tools_integrations.Authorization.html +4 -4
- package/dist/docs/types/tools_integrations.Channel.html +5 -5
- package/dist/docs/types/tools_integrations.ComposeConfig.html +35 -0
- package/dist/docs/types/tools_integrations.ContactRoleConfig.html +14 -0
- package/dist/docs/types/tools_integrations.LinkTypeConfig.html +50 -14
- package/dist/docs/types/tools_integrations.SyncContext.html +3 -3
- package/dist/llm-docs/connector.d.ts +1 -1
- package/dist/llm-docs/connector.d.ts.map +1 -1
- package/dist/llm-docs/connector.js +1 -1
- package/dist/llm-docs/connector.js.map +1 -1
- package/dist/llm-docs/index.d.ts.map +1 -1
- package/dist/llm-docs/index.js +2 -0
- package/dist/llm-docs/index.js.map +1 -1
- package/dist/llm-docs/plot.d.ts +1 -1
- package/dist/llm-docs/plot.d.ts.map +1 -1
- package/dist/llm-docs/plot.js +1 -1
- package/dist/llm-docs/plot.js.map +1 -1
- package/dist/llm-docs/tag.d.ts +1 -1
- package/dist/llm-docs/tag.d.ts.map +1 -1
- package/dist/llm-docs/tag.js +1 -1
- package/dist/llm-docs/tag.js.map +1 -1
- package/dist/llm-docs/tools/files.d.ts +9 -0
- package/dist/llm-docs/tools/files.d.ts.map +1 -0
- package/dist/llm-docs/tools/files.js +8 -0
- package/dist/llm-docs/tools/files.js.map +1 -0
- package/dist/llm-docs/tools/integrations.d.ts +1 -1
- package/dist/llm-docs/tools/integrations.d.ts.map +1 -1
- package/dist/llm-docs/tools/integrations.js +1 -1
- package/dist/llm-docs/tools/integrations.js.map +1 -1
- package/dist/llm-docs/twist-guide-template.d.ts +1 -1
- package/dist/llm-docs/twist-guide-template.d.ts.map +1 -1
- package/dist/llm-docs/twist-guide-template.js +1 -1
- package/dist/llm-docs/twist-guide-template.js.map +1 -1
- package/dist/plot.d.ts +85 -4
- package/dist/plot.d.ts.map +1 -1
- package/dist/plot.js +2 -0
- package/dist/plot.js.map +1 -1
- package/dist/tag.d.ts +17 -43
- package/dist/tag.d.ts.map +1 -1
- package/dist/tag.js +17 -46
- package/dist/tag.js.map +1 -1
- package/dist/tools/files.d.ts +33 -0
- package/dist/tools/files.d.ts.map +1 -0
- package/dist/tools/files.js +22 -0
- package/dist/tools/files.js.map +1 -0
- package/dist/tools/index.d.ts +1 -0
- package/dist/tools/index.d.ts.map +1 -1
- package/dist/tools/index.js +1 -0
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/integrations.d.ts +124 -28
- package/dist/tools/integrations.d.ts.map +1 -1
- package/dist/tools/integrations.js.map +1 -1
- package/dist/twist-guide.d.ts +1 -1
- package/dist/twist-guide.d.ts.map +1 -1
- package/package.json +6 -1
- package/src/connector.ts +167 -10
- package/src/llm-docs/connector.ts +1 -1
- package/src/llm-docs/index.ts +2 -0
- package/src/llm-docs/plot.ts +1 -1
- package/src/llm-docs/tag.ts +1 -1
- package/src/llm-docs/tools/files.ts +8 -0
- package/src/llm-docs/tools/integrations.ts +1 -1
- package/src/llm-docs/twist-guide-template.ts +1 -1
- package/src/plot.ts +94 -4
- package/src/tag.ts +17 -48
- package/src/tools/files.ts +37 -0
- package/src/tools/index.ts +1 -0
- package/src/tools/integrations.ts +125 -39
|
@@ -5,4 +5,4 @@
|
|
|
5
5
|
* Generated from: cli/templates/AGENTS.template.md
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
export default "# Twist Implementation Guide for LLMs\n\nThis document provides context for AI assistants generating or modifying twists.\n\n## Architecture Overview\n\nPlot Twists are TypeScript classes that extend the `Twist` base class. Twists interact with external services and Plot's core functionality through a tool-based architecture.\n\n### Runtime Environment\n\n**Critical**: All Twists and tool functions are executed in a sandboxed, ephemeral environment with limited resources:\n\n- **Memory is temporary**: Anything stored in memory (e.g. as a variable in the twist/tool object) is lost after the function completes. Use the Store tool instead. Only use memory for temporary caching.\n- **Limited requests per execution**: Each execution has ~1000 requests (HTTP requests, tool calls, database operations)\n- **Limited CPU time**: Each execution has limited CPU time (typically ~60 seconds) and memory (128MB)\n- **Use tasks to get fresh request limits**: `this.runTask()` creates a NEW execution with a fresh ~1000 request limit\n- **Calling callbacks continues same execution**: `this.run()` continues the same execution and shares the request count\n- **Break long loops**: Split large operations into batches that each stay under the ~1000 request limit\n- **Store intermediate state**: Use the Store tool to persist state between batches\n- **Examples**: Syncing large datasets, processing many API calls, or performing batch operations\n\n## Understanding Threads and Notes\n\n**CRITICAL CONCEPT**: A **Thread** represents something done or to be done (a task, event, or conversation), while **Notes** represent the updates and details on that thread.\n\n**Think of a Thread as a thread** on a messaging platform, and **Notes as the messages in that thread**.\n\n### Key Guidelines\n\n1. **Always create Threads with an initial Note** - The title is just a summary; detailed content goes in Notes\n2. **Add Notes to existing Threads for updates** - Don't create a new Thread for each related message\n3. **Use Thread.source and Note.key for automatic upserts (Recommended)** - Set Thread.source to the external item's URL for deduplication, and use Note.key for upsertable note content. No manual ID tracking needed.\n4. **For advanced cases, use generated UUIDs** - Only when you need multiple Plot threads per external item (see SYNC_STRATEGIES.md)\n5. **Most Threads should be `ThreadType.Note`** - Use `Action` only for tasks with `done`, use `Event` only for items with `start`/`end`\n\n### Recommended Decision Tree (Strategy 2: Upsert via Source/Key)\n\n```\nNew event/task/conversation from external system?\n ├─ Has stable URL or ID?\n │ └─ Yes → Set Thread.source to the canonical URL/ID\n │ Create Thread (Plot handles deduplication automatically)\n │ Use Note.key for different note types:\n │ - \"description\" for main content\n │ - \"metadata\" for status/priority/assignee\n │ - \"comment-{id}\" for individual comments\n │\n └─ No stable identifier OR need multiple Plot threads per external item?\n └─ Use Advanced Pattern (Strategy 3: Generate and Store IDs)\n See SYNC_STRATEGIES.md for details\n```\n\n### Advanced Decision Tree (Strategy 3: Generate and Store IDs)\n\nOnly use when source/key upserts aren't sufficient (e.g., creating multiple threads from one external item):\n\n```\nNew event/task/conversation?\n ├─ Yes → Generate UUID with Uuid.Generate()\n │ Create new Thread with that UUID\n │ Store mapping: external_id → thread_uuid\n │\n └─ No (update/reply/comment) → Look up mapping by external_id\n ├─ Found → Add Note to existing Thread using stored UUID\n └─ Not found → Create new Thread with UUID + store mapping\n```\n\n## Twist Structure Pattern\n\n```typescript\nimport {\n type Thread,\n type NewThreadWithNotes,\n type ThreadFilter,\n type Priority,\n type ToolBuilder,\n Twist,\n ThreadType,\n} from \"@plotday/twister\";\nimport { ThreadAccess, Plot } from \"@plotday/twister/tools/plot\";\n// Import your sources or tools as needed\n\nexport default class MyTwist extends Twist<MyTwist> {\n build(build: ToolBuilder) {\n return {\n plot: build(Plot, {\n thread: { access: ThreadAccess.Create },\n }),\n };\n }\n\n async activate(_priority: Pick<Priority, \"id\">) {\n // Auth and resource selection handled in the twist edit modal.\n }\n}\n```\n\n## Tool System\n\n### Accessing Tools\n\nAll tools are declared in the `build` method:\n\n```typescript\nbuild(build: ToolBuilder) {\n return {\n toolName: build(ToolClass),\n };\n}\n```\n\nAll `build()` calls must occur in the `build` method as they are used for dependency analysis.\n\nIMPORTANT: HTTP access is restricted to URLs requested via `build(Network, { urls: [url1, url2, ...] })` in the `build` method. Wildcards are supported. Use `build(Network, { urls: ['*'] })` if full access is needed.\n\n### Built-in Tools (Always Available)\n\nFor complete API documentation of built-in tools including all methods, types, and detailed examples, see the TypeScript definitions in your installed package at `node_modules/@plotday/twister/src/tools/*.ts`. Each tool file contains comprehensive JSDoc documentation.\n\n**Quick reference - Available tools:**\n\n- `@plotday/twister/tools/plot` - Core data layer (create/update activities, priorities, contacts)\n- `@plotday/twister/tools/ai` - LLM integration (text generation, structured output, reasoning)\n - Use ModelPreferences to specify `speed` (fast/balanced/capable) and `cost` (low/medium/high)\n- `@plotday/twister/tools/store` - Persistent key-value storage (also via `this.set()`, `this.get()`)\n- `@plotday/twister/tools/tasks` - Queue batched work (also via `this.run()`)\n- `@plotday/twister/tools/callbacks` - Persistent function references (also via `this.callback()`)\n- `@plotday/twister/tools/integrations` - OAuth2 authentication flows\n- `@plotday/twister/tools/network` - HTTP access permissions and webhook management\n- `@plotday/twister/tools/twists` - Manage other Twists\n\n**Critical**: Never use instance variables for state. They are lost after function execution. Always use Store methods.\n\n## Lifecycle Methods\n\n### activate(priority: Pick<Priority, \"id\">)\n\nCalled when the twist is enabled for a priority. Auth and resource selection are handled automatically via the twist edit modal when using external tools with Integrations.\n\nMost twists have an empty or minimal `activate()`:\n\n```typescript\nasync activate(_priority: Pick<Priority, \"id\">) {\n // Auth and resource selection are handled in the twist edit modal.\n // Only add custom initialization here if needed.\n}\n```\n\n**Store Parent Thread for Later (optional):**\n\n```typescript\nasync activate(_priority: Pick<Priority, \"id\">) {\n const threadId = await this.tools.plot.createThread({\n type: ThreadType.Note,\n title: \"Setup complete\",\n notes: [{\n content: \"Your twist is ready. Threads will appear as they sync.\",\n }],\n });\n await this.set(\"setup_thread_id\", threadId);\n}\n```\n\n### Event Callbacks (via build options)\n\nTwists respond to events through callbacks declared in `build()`:\n\n**React to thread changes (for two-way sync):**\n\n```typescript\nplot: build(Plot, {\n thread: {\n access: ThreadAccess.Create,\n updated: this.onThreadUpdated,\n },\n note: {\n created: this.onNoteCreated,\n },\n}),\n\nasync onThreadUpdated(thread: Thread, changes: { tagsAdded, tagsRemoved }): Promise<void> {\n const tool = this.getToolForThread(thread);\n if (tool?.updateIssue) await tool.updateIssue(thread);\n}\n\nasync onNoteCreated(note: Note, thread: Thread): Promise<NoteWriteBackResult | void> {\n if (note.author.type === ActorType.Twist) return; // Prevent loops\n // Sync note to external service as a comment. Return the external\n // system's id + stored content so the runtime can set note.key AND\n // record a sync baseline that preserves Plot's content on round-trip.\n // See connectors/AGENTS.md → \"Sync baseline preservation\".\n const comment = await externalApi.createComment(thread.meta.externalId, { body: note.content ?? \"\" });\n if (!comment?.id) return;\n return { key: `comment-${comment.id}`, externalContent: comment.body };\n}\n```\n\n**Respond to mentions (AI twist pattern):**\n\n```typescript\nplot: build(Plot, {\n thread: { access: ThreadAccess.Respond },\n note: {\n intents: [{\n description: \"Respond to general questions\",\n examples: [\"What's the weather?\", \"Help me plan my week\"],\n handler: this.respond,\n }],\n },\n}),\n```\n\n**Default mention on replies:**\n\nWhen your twist processes replies (two-way comment sync or conversational AI), set `defaultMention: true` so the user doesn't have to manually re-mention your twist on every note:\n\n- `thread.defaultMention` — Auto-mention on replies to threads your twist created (e.g., synced issues, emails)\n- `note.defaultMention` — Auto-mention on follow-up notes in threads where your twist was @-mentioned (e.g., conversational agents)\n\n```typescript\n// Connector with two-way comment sync\nplot: build(Plot, {\n thread: {\n access: ThreadAccess.Create,\n defaultMention: true, // Users replying to synced threads will mention this twist by default\n updated: this.onThreadUpdated,\n },\n note: {\n created: this.onNoteCreated,\n },\n}),\n\n// Conversational AI twist\nplot: build(Plot, {\n thread: { access: ThreadAccess.Respond },\n note: {\n defaultMention: true, // Follow-up notes auto-mention this twist\n intents: [{ description: \"...\", examples: [...], handler: this.respond }],\n },\n}),\n```\n\nWithout `defaultMention`, the twist chip appears toggled OFF by default — the user can still enable it manually per-note.\n\n## Actions\n\nActions enable user interaction:\n\n```typescript\nimport { type Action, ActionType } from \"@plotday/twister\";\n\n// External URL action\nconst urlAction: Action = {\n title: \"Open website\",\n type: ActionType.external,\n url: \"https://example.com\",\n};\n\n// Callback action (uses Callbacks tool — use linkCallback, not callback)\nconst token = await this.linkCallback(this.onActionClicked, \"context\");\nconst callbackAction: Action = {\n title: \"Click me\",\n type: ActionType.callback,\n callback: token,\n};\n\n// Add to thread note\nawait this.tools.plot.createThread({\n type: ThreadType.Note,\n title: \"Task with actions\",\n notes: [\n {\n content: \"Click the actions below to take action.\",\n actions: [urlAction, callbackAction],\n },\n ],\n});\n\n// Callback handler receives the Action as first argument\nasync onActionClicked(action: Action, context: string): Promise<void> {\n // Handle action click\n}\n```\n\n## Authentication Pattern\n\nAuth is handled automatically via the Integrations tool. Tools declare their OAuth provider in `build()`, and users connect in the twist edit modal. **You do not need to create auth activities manually.**\n\n```typescript\n// In your tool's build() method:\nbuild(build: ToolBuilder) {\n return {\n integrations: build(Integrations, {\n providers: [{\n provider: AuthProvider.Google,\n scopes: [\"https://www.googleapis.com/auth/calendar\"],\n getChannels: this.getChannels, // List available resources after auth\n onChannelEnabled: this.onChannelEnabled, // User enabled a resource\n onChannelDisabled: this.onChannelDisabled, // User disabled a resource\n }],\n }),\n // ...\n };\n}\n\n// Get a token for API calls:\nconst token = await this.tools.integrations.get(AuthProvider.Google, channelId);\nif (!token) throw new Error(\"No auth token available\");\nconst client = new ApiClient({ accessToken: token.token });\n```\n\nFor per-user write-backs (e.g., RSVP, comments attributed to the acting user):\n\n```typescript\nawait this.tools.integrations.actAs(\n AuthProvider.Google,\n actorId, // The user who performed the action\n threadId, // Thread to prompt for auth if needed\n this.performWriteBack,\n ...extraArgs\n);\n```\n\n## Sync Pattern\n\n### Upsert via Source/Key (Strategy 2)\n\nUse source/key for automatic upserts:\n\n```typescript\nasync handleEvent(event: ExternalEvent): Promise<void> {\n const thread: NewThreadWithNotes = {\n source: event.htmlLink, // Canonical URL for automatic deduplication\n type: ThreadType.Event,\n title: event.summary || \"(No title)\",\n notes: [],\n };\n\n if (event.description) {\n thread.notes.push({\n thread: { source: event.htmlLink },\n key: \"description\", // This key enables note-level upserts\n content: event.description,\n });\n }\n\n // Create or update — Plot handles deduplication automatically\n await this.tools.plot.createThread(thread);\n}\n```\n\n### Advanced: Generate and Store IDs (Strategy 3)\n\nOnly use this pattern when you need to create multiple Plot threads from a single external item, or when the external system doesn't provide stable identifiers. See SYNC_STRATEGIES.md for details.\n\n```typescript\nasync handleEventAdvanced(\n incomingThread: NewThreadWithNotes,\n calendarId: string\n): Promise<void> {\n // Extract external event ID from meta (adapt based on your tool's data)\n const externalId = incomingThread.meta?.eventId;\n\n if (!externalId) {\n console.error(\"Event missing external ID\");\n return;\n }\n\n // Check if we've already synced this event\n const mappingKey = `event_mapping:${calendarId}:${externalId}`;\n const existingThreadId = await this.get<Uuid>(mappingKey);\n\n if (existingThreadId) {\n // Event already exists - add update as a Note (add message to thread)\n if (incomingThread.notes?.[0]?.content) {\n await this.tools.plot.createNote({\n thread: { id: existingThreadId },\n content: incomingThread.notes[0].content,\n });\n }\n return;\n }\n\n // New event - generate UUID and store mapping\n const threadId = Uuid.Generate();\n await this.set(mappingKey, threadId);\n\n // Create new Thread with initial Note (new thread with first message)\n await this.tools.plot.createThread({\n ...incomingThread,\n id: threadId,\n });\n}\n```\n\n## Resource Selection\n\nResource selection (calendars, projects, channels) is handled automatically in the twist edit modal via the Integrations tool. Users see a list of available resources returned by your tool's `getChannels()` method and toggle them on/off. You do **not** need to build custom selection UI.\n\n```typescript\n// In your tool:\nasync getChannels(_auth: Authorization, token: AuthToken): Promise<Channel[]> {\n const client = new ApiClient({ accessToken: token.token });\n const calendars = await client.listCalendars();\n return calendars.map(c => ({\n id: c.id,\n title: c.name,\n children: c.subCalendars?.map(sc => ({ id: sc.id, title: sc.name })),\n }));\n}\n```\n\n## Batch Processing Pattern\n\n**Important**: Because Twists run in an ephemeral environment with limited requests per execution (~1000 requests), you must break long operations into batches. Each batch runs independently in a new execution context with its own fresh request limit.\n\n### Key Principles\n\n1. **Stay under request limits**: Each execution has ~1000 requests. Size batches accordingly.\n2. **Use runTask() for fresh limits**: Each call to `this.runTask()` creates a NEW execution with fresh ~1000 requests\n3. **Store state between batches**: Use the Store tool to persist progress\n4. **Calculate safe batch sizes**: Determine requests per item to size batches (e.g., ~10 requests per item = ~100 items per batch)\n5. **Clean up when done**: Delete stored state after completion\n6. **Handle failures**: Store enough state to resume if a batch fails\n\n### Example Implementation\n\n```typescript\nasync startSync(resourceId: string): Promise<void> {\n // Initialize state in Store (persists between executions)\n await this.set(`sync_state_${resourceId}`, {\n nextPageToken: null,\n batchNumber: 1,\n itemsProcessed: 0,\n initialSync: true, // Track whether this is the first sync\n });\n\n // Queue first batch using runTask method\n const callback = await this.callback(this.syncBatch, resourceId);\n // runTask creates NEW execution with fresh ~1000 request limit\n await this.runTask(callback);\n}\n\nasync syncBatch(resourceId: string): Promise<void> {\n // Load state from Store (set by previous execution)\n const state = await this.get(`sync_state_${resourceId}`);\n\n // Process one batch (size to stay under ~1000 request limit)\n const result = await this.fetchBatch(state.nextPageToken);\n\n // Process results using source/key pattern (automatic upserts, no manual tracking)\n // If each item makes ~10 requests, keep batch size ≤ 100 items to stay under limit\n for (const item of result.items) {\n // Each createThread may make ~5-10 requests depending on notes/links\n await this.tools.plot.createThread({\n source: item.url, // Use item's canonical URL for automatic deduplication\n type: ThreadType.Note,\n title: item.title,\n notes: [{\n activity: { source: item.url },\n key: \"description\", // Use key for upsertable notes\n content: item.description,\n }],\n ...(state.initialSync ? { unread: false } : {}), // false for initial, omit for incremental\n ...(state.initialSync ? { archived: false } : {}), // unarchive on initial only\n });\n }\n\n if (result.nextPageToken) {\n // Update state in Store for next batch\n await this.set(`sync_state_${resourceId}`, {\n nextPageToken: result.nextPageToken,\n batchNumber: state.batchNumber + 1,\n itemsProcessed: state.itemsProcessed + result.items.length,\n initialSync: state.initialSync, // Preserve initialSync flag across batches\n });\n\n // Queue next batch - creates NEW execution with fresh request limit\n const nextCallback = await this.callback(this.syncBatch, resourceId);\n await this.runTask(nextCallback);\n } else {\n // Cleanup when complete\n await this.clear(`sync_state_${resourceId}`);\n\n // Optionally notify user of completion\n await this.tools.plot.createThread({\n type: ThreadType.Note,\n title: \"Sync complete\",\n notes: [\n {\n content: `Successfully processed ${state.itemsProcessed + result.items.length} items.`,\n },\n ],\n });\n }\n}\n```\n\n## Thread Sync Best Practices\n\nWhen syncing threads from external systems, follow these patterns for optimal user experience:\n\n### The `initialSync` Flag\n\nAll sync-based tools should distinguish between initial sync (first import) and incremental sync (ongoing updates):\n\n| Field | Initial Sync | Incremental Sync | Reason |\n|-------|--------------|------------------|---------|\n| `unread` | `false` | *omit* | Initial: mark read for all. Incremental: auto-mark read for author only |\n| `archived` | `false` | *omit* | Unarchive on install, preserve user choice on updates |\n\n**Example:**\n```typescript\nconst thread: NewThread = {\n type: ThreadType.Event,\n source: event.url,\n title: event.title,\n ...(initialSync ? { unread: false } : {}), // false for initial, omit for incremental\n ...(initialSync ? { archived: false } : {}), // unarchive on initial only\n};\n```\n\n**Why this matters:**\n- **Initial sync**: Activities are unarchived and marked as read for all users, preventing spam from bulk historical imports\n- **Incremental sync**: Activities are auto-marked read for the author (twist owner), unread for everyone else. Archived state is preserved\n- **Reinstall**: Acts as initial sync, so previously archived activities are unarchived (fresh start)\n\n### Two-Way Sync: Avoiding Race Conditions\n\nWhen implementing two-way sync where items created in Plot are pushed to an external system (e.g. Notes becoming comments), a race condition can occur: the external system may send a webhook for the newly created item before you've updated the Thread/Note with the external key. The webhook handler won't find the item by external key and may create a duplicate.\n\n**Solution:** Embed the Plot `Thread.id` / `Note.id` in the external item's metadata when creating it, and update `Thread.source` / `Note.key` after creation. When processing webhooks, check for the Plot ID in metadata first.\n\n```typescript\nasync pushNoteAsComment(note: Note, externalItemId: string): Promise<void> {\n // Create external item with Plot ID in metadata for webhook correlation\n const externalComment = await externalApi.createComment(externalItemId, {\n body: note.content,\n metadata: { plotNoteId: note.id },\n });\n\n // Update Note with external key AFTER creation\n // A webhook may arrive before this completes — that's OK (see onWebhook below)\n await this.tools.plot.updateNote({\n id: note.id,\n key: `comment-${externalComment.id}`,\n });\n}\n\nasync onWebhook(payload: WebhookPayload): Promise<void> {\n const comment = payload.comment;\n\n // Use Plot ID from metadata if present (handles race condition),\n // otherwise fall back to upserting by activity source and key\n await this.tools.plot.createNote({\n ...(comment.metadata?.plotNoteId\n ? { id: comment.metadata.plotNoteId }\n : { activity: { source: payload.itemUrl } }),\n key: `comment-${comment.id}`,\n content: comment.body,\n });\n}\n```\n\n## Error Handling\n\nAlways handle errors gracefully and communicate them to users:\n\n```typescript\ntry {\n await this.externalOperation();\n} catch (error) {\n console.error(\"Operation failed:\", error);\n\n await this.tools.plot.createThread({\n type: ThreadType.Note,\n title: \"Operation failed\",\n notes: [\n {\n content: `Failed to complete operation: ${error.message}`,\n },\n ],\n });\n}\n```\n\n## Common Pitfalls\n\n- **Don't use instance variables for state** - Anything stored in memory is lost after function execution. Always use the Store tool for data that needs to persist.\n- **Processing self-created threads** - Other users may change a Thread created by the twist, resulting in a callback. Be sure to check the `changes === null` and/or `thread.author.id !== this.id` to avoid re-processing.\n- **Always create Threads with Notes** - See \"Understanding Threads and Notes\" section above for the thread/message pattern and decision tree.\n- **Use correct Thread types** - Most should be `ThreadType.Note`. Only use `Action` for tasks with `done`, and `Event` for items with `start`/`end`.\n- **Use Thread.source and Note.key for automatic upserts (Recommended)** - Set Thread.source to the external item's URL for automatic deduplication. Only use UUID generation and storage for advanced cases (see SYNC_STRATEGIES.md).\n- **Add Notes to existing Threads** - For source/key pattern, reference threads by source. For UUID pattern, look up stored mappings before creating new Threads. Think thread replies, not new threads.\n- Tools are declared in the `build` method and accessed via `this.tools.toolName` in twist methods.\n- **Don't forget request limits** - Each execution has ~1000 requests (HTTP requests, tool calls). Break long loops into batches with `this.runTask()` to get fresh request limits. Calculate requests per item to determine safe batch size (e.g., if each item needs ~10 requests, batch size = ~100 items).\n- **Always use Callbacks tool for persistent references** - Direct function references don't survive worker restarts.\n- **Store auth tokens** - Don't re-request authentication unnecessarily.\n- **Clean up callbacks and stored state** - Delete callbacks and Store entries when no longer needed.\n- **Handle missing auth gracefully** - Check for stored auth before operations.\n- **CRITICAL: Maintain callback backward compatibility** - All callbacks (webhooks, tasks, batch operations) automatically upgrade to new twist versions. You **must** maintain backward compatibility in callback method signatures. Only add optional parameters at the end, never remove or reorder parameters. For breaking changes, implement migration logic in the `upgrade()` lifecycle method to recreate affected callbacks.\n\n## Testing\n\nBefore deploying, verify:\n\n1. Linting passes: `{{packageManager}} lint`\n2. All dependencies are in package.json\n3. Authentication flow works end-to-end\n4. Batch operations handle pagination correctly\n5. Error cases are handled gracefully\n";
|
|
8
|
+
export default "# Twist Implementation Guide for LLMs\n\nThis document provides context for AI assistants generating or modifying twists.\n\n## Architecture Overview\n\nPlot Twists are TypeScript classes that extend the `Twist` base class. Twists interact with external services and Plot's core functionality through a tool-based architecture.\n\n### Runtime Environment\n\n**Critical**: All Twists and tool functions are executed in a sandboxed, ephemeral environment with limited resources:\n\n- **Memory is temporary**: Anything stored in memory (e.g. as a variable in the twist/tool object) is lost after the function completes. Use the Store tool instead. Only use memory for temporary caching.\n- **Limited requests per execution**: Each execution has ~1000 requests (HTTP requests, tool calls, database operations)\n- **Limited CPU time**: Each execution has limited CPU time (typically ~60 seconds) and memory (128MB)\n- **Use tasks to get fresh request limits**: `this.runTask()` creates a NEW execution with a fresh ~1000 request limit\n- **Calling callbacks continues same execution**: `this.run()` continues the same execution and shares the request count\n- **Break long loops**: Split large operations into batches that each stay under the ~1000 request limit\n- **Store intermediate state**: Use the Store tool to persist state between batches\n- **Examples**: Syncing large datasets, processing many API calls, or performing batch operations\n\n## Understanding Threads and Notes\n\n**CRITICAL CONCEPT**: A **Thread** represents something done or to be done (a task, event, or conversation), while **Notes** represent the updates and details on that thread.\n\n**Think of a Thread as a thread** on a messaging platform, and **Notes as the messages in that thread**.\n\n### Key Guidelines\n\n1. **Always create Threads with an initial Note** - The title is just a summary; detailed content goes in Notes\n2. **Add Notes to existing Threads for updates** - Don't create a new Thread for each related message\n3. **Use Thread.source and Note.key for automatic upserts (Recommended)** - Set Thread.source to the external item's URL for deduplication, and use Note.key for upsertable note content. No manual ID tracking needed.\n4. **For advanced cases, use generated UUIDs** - Only when you need multiple Plot threads per external item (see SYNC_STRATEGIES.md)\n5. **Most Threads should be `ThreadType.Note`** - Use `Action` only for tasks with `done`, use `Event` only for items with `start`/`end`\n\n### Recommended Decision Tree (Strategy 2: Upsert via Source/Key)\n\n```\nNew event/task/conversation from external system?\n ├─ Has stable URL or ID?\n │ └─ Yes → Set Thread.source to the canonical URL/ID\n │ Create Thread (Plot handles deduplication automatically)\n │ Use Note.key for different note types:\n │ - \"description\" for main content\n │ - \"metadata\" for status/priority/assignee\n │ - \"comment-{id}\" for individual comments\n │\n └─ No stable identifier OR need multiple Plot threads per external item?\n └─ Use Advanced Pattern (Strategy 3: Generate and Store IDs)\n See SYNC_STRATEGIES.md for details\n```\n\n### Advanced Decision Tree (Strategy 3: Generate and Store IDs)\n\nOnly use when source/key upserts aren't sufficient (e.g., creating multiple threads from one external item):\n\n```\nNew event/task/conversation?\n ├─ Yes → Generate UUID with Uuid.Generate()\n │ Create new Thread with that UUID\n │ Store mapping: external_id → thread_uuid\n │\n └─ No (update/reply/comment) → Look up mapping by external_id\n ├─ Found → Add Note to existing Thread using stored UUID\n └─ Not found → Create new Thread with UUID + store mapping\n```\n\n## Twist Structure Pattern\n\n```typescript\nimport {\n type Thread,\n type NewThreadWithNotes,\n type ThreadFilter,\n type Priority,\n type ToolBuilder,\n Twist,\n ThreadType,\n} from \"@plotday/twister\";\nimport { ThreadAccess, Plot } from \"@plotday/twister/tools/plot\";\n// Import your sources or tools as needed\n\nexport default class MyTwist extends Twist<MyTwist> {\n build(build: ToolBuilder) {\n return {\n plot: build(Plot, {\n thread: { access: ThreadAccess.Create },\n }),\n };\n }\n\n async activate(_priority: Pick<Priority, \"id\">) {\n // Auth and resource selection handled in the twist edit modal.\n }\n}\n```\n\n## Tool System\n\n### Accessing Tools\n\nAll tools are declared in the `build` method:\n\n```typescript\nbuild(build: ToolBuilder) {\n return {\n toolName: build(ToolClass),\n };\n}\n```\n\nAll `build()` calls must occur in the `build` method as they are used for dependency analysis.\n\nIMPORTANT: HTTP access is restricted to URLs requested via `build(Network, { urls: [url1, url2, ...] })` in the `build` method. Wildcards are supported. Use `build(Network, { urls: ['*'] })` if full access is needed.\n\n### Built-in Tools (Always Available)\n\nFor complete API documentation of built-in tools including all methods, types, and detailed examples, see the TypeScript definitions in your installed package at `node_modules/@plotday/twister/src/tools/*.ts`. Each tool file contains comprehensive JSDoc documentation.\n\n**Quick reference - Available tools:**\n\n- `@plotday/twister/tools/plot` - Core data layer (create/update activities, priorities, contacts)\n- `@plotday/twister/tools/ai` - LLM integration (text generation, structured output, reasoning)\n - Use ModelPreferences to specify `speed` (fast/balanced/capable) and `cost` (low/medium/high)\n- `@plotday/twister/tools/store` - Persistent key-value storage (also via `this.set()`, `this.get()`)\n- `@plotday/twister/tools/tasks` - Queue batched work (also via `this.run()`)\n- `@plotday/twister/tools/callbacks` - Persistent function references (also via `this.callback()`)\n- `@plotday/twister/tools/integrations` - OAuth2 authentication flows\n- `@plotday/twister/tools/network` - HTTP access permissions and webhook management\n- `@plotday/twister/tools/twists` - Manage other Twists\n\n**Critical**: Never use instance variables for state. They are lost after function execution. Always use Store methods.\n\n## Lifecycle Methods\n\n### activate(priority: Pick<Priority, \"id\">)\n\nCalled when the twist is enabled for a priority. Auth and resource selection are handled automatically via the twist edit modal when using external tools with Integrations.\n\nMost twists have an empty or minimal `activate()`:\n\n```typescript\nasync activate(_priority: Pick<Priority, \"id\">) {\n // Auth and resource selection are handled in the twist edit modal.\n // Only add custom initialization here if needed.\n}\n```\n\n**Store Parent Thread for Later (optional):**\n\n```typescript\nasync activate(_priority: Pick<Priority, \"id\">) {\n const threadId = await this.tools.plot.createThread({\n type: ThreadType.Note,\n title: \"Setup complete\",\n notes: [{\n content: \"Your twist is ready. Threads will appear as they sync.\",\n }],\n });\n await this.set(\"setup_thread_id\", threadId);\n}\n```\n\n### Event Callbacks (via build options)\n\nTwists respond to events through callbacks declared in `build()`:\n\n**React to thread changes (for two-way sync):**\n\n```typescript\nplot: build(Plot, {\n thread: {\n access: ThreadAccess.Create,\n updated: this.onThreadUpdated,\n },\n note: {\n created: this.onNoteCreated,\n },\n}),\n\nasync onThreadUpdated(thread: Thread, changes: { tagsAdded, tagsRemoved }): Promise<void> {\n const tool = this.getToolForThread(thread);\n if (tool?.updateIssue) await tool.updateIssue(thread);\n}\n\nasync onNoteCreated(note: Note, thread: Thread): Promise<NoteWriteBackResult | void> {\n if (note.author.type === ActorType.Twist) return; // Prevent loops\n // Sync note to external service as a comment. Return the external\n // system's id + stored content so the runtime can set note.key AND\n // record a sync baseline that preserves Plot's content on round-trip.\n // See connectors/AGENTS.md → \"Sync baseline preservation\".\n const comment = await externalApi.createComment(thread.meta.externalId, { body: note.content ?? \"\" });\n if (!comment?.id) return;\n return { key: `comment-${comment.id}`, externalContent: comment.body };\n}\n```\n\n**Respond to mentions (AI twist pattern):**\n\n```typescript\nplot: build(Plot, {\n thread: { access: ThreadAccess.Respond },\n note: {\n intents: [{\n description: \"Respond to general questions\",\n examples: [\"What's the weather?\", \"Help me plan my week\"],\n handler: this.respond,\n }],\n },\n}),\n```\n\n**Default mention on replies:**\n\nWhen your twist processes replies (two-way comment sync or conversational AI), set `defaultMention: true` so the user doesn't have to manually re-mention your twist on every note:\n\n- `thread.defaultMention` — Auto-mention on replies to threads your twist created (e.g., synced issues, emails)\n- `note.defaultMention` — Auto-mention on follow-up notes in threads where your twist was @-mentioned (e.g., conversational agents)\n\n```typescript\n// Connector with two-way comment sync\nplot: build(Plot, {\n thread: {\n access: ThreadAccess.Create,\n defaultMention: true, // Users replying to synced threads will mention this twist by default\n updated: this.onThreadUpdated,\n },\n note: {\n created: this.onNoteCreated,\n },\n}),\n\n// Conversational AI twist\nplot: build(Plot, {\n thread: { access: ThreadAccess.Respond },\n note: {\n defaultMention: true, // Follow-up notes auto-mention this twist\n intents: [{ description: \"...\", examples: [...], handler: this.respond }],\n },\n}),\n```\n\nWithout `defaultMention`, the twist chip appears toggled OFF by default — the user can still enable it manually per-note.\n\n## Actions\n\nActions enable user interaction:\n\n```typescript\nimport { type Action, ActionType } from \"@plotday/twister\";\n\n// External URL action\nconst urlAction: Action = {\n title: \"Open website\",\n type: ActionType.external,\n url: \"https://example.com\",\n};\n\n// Callback action (uses Callbacks tool — use linkCallback, not callback)\nconst token = await this.linkCallback(this.onActionClicked, \"context\");\nconst callbackAction: Action = {\n title: \"Click me\",\n type: ActionType.callback,\n callback: token,\n};\n\n// Add to thread note\nawait this.tools.plot.createThread({\n type: ThreadType.Note,\n title: \"Task with actions\",\n notes: [\n {\n content: \"Click the actions below to take action.\",\n actions: [urlAction, callbackAction],\n },\n ],\n});\n\n// Callback handler receives the Action as first argument\nasync onActionClicked(action: Action, context: string): Promise<void> {\n // Handle action click\n}\n```\n\n## Authentication Pattern\n\nAuth is handled automatically via the Integrations tool. Tools declare their OAuth provider in `build()`, and users connect in the twist edit modal. **You do not need to create auth activities manually.**\n\n```typescript\n// In your tool's build() method:\nbuild(build: ToolBuilder) {\n return {\n integrations: build(Integrations, {\n providers: [{\n provider: AuthProvider.Google,\n scopes: [\"https://www.googleapis.com/auth/calendar\"],\n getChannels: this.getChannels, // List available resources after auth\n onChannelEnabled: this.onChannelEnabled, // User enabled a resource\n onChannelDisabled: this.onChannelDisabled, // User disabled a resource\n }],\n }),\n // ...\n };\n}\n\n// Get a token for API calls:\nconst token = await this.tools.integrations.get(AuthProvider.Google, channelId);\nif (!token) throw new Error(\"No auth token available\");\nconst client = new ApiClient({ accessToken: token.token });\n```\n\nFor per-user write-backs (e.g., RSVP, comments attributed to the acting user):\nthe dispatch runtime routes the change to the acting user's own connector\ninstance, so your callback (`onScheduleContactUpdated`, etc.) already runs\nunder that user's auth. Just call `this.tools.integrations.get(channelId)`\nto fetch the token and the write-back is attributed correctly. If the\nacting user has no connection of this type, the change lives in Plot but\nis not dispatched.\n\n## Sync Pattern\n\n### Upsert via Source/Key (Strategy 2)\n\nUse source/key for automatic upserts:\n\n```typescript\nasync handleEvent(event: ExternalEvent): Promise<void> {\n const thread: NewThreadWithNotes = {\n source: event.htmlLink, // Canonical URL for automatic deduplication\n type: ThreadType.Event,\n title: event.summary || \"(No title)\",\n notes: [],\n };\n\n if (event.description) {\n thread.notes.push({\n thread: { source: event.htmlLink },\n key: \"description\", // This key enables note-level upserts\n content: event.description,\n });\n }\n\n // Create or update — Plot handles deduplication automatically\n await this.tools.plot.createThread(thread);\n}\n```\n\n### Advanced: Generate and Store IDs (Strategy 3)\n\nOnly use this pattern when you need to create multiple Plot threads from a single external item, or when the external system doesn't provide stable identifiers. See SYNC_STRATEGIES.md for details.\n\n```typescript\nasync handleEventAdvanced(\n incomingThread: NewThreadWithNotes,\n calendarId: string\n): Promise<void> {\n // Extract external event ID from meta (adapt based on your tool's data)\n const externalId = incomingThread.meta?.eventId;\n\n if (!externalId) {\n console.error(\"Event missing external ID\");\n return;\n }\n\n // Check if we've already synced this event\n const mappingKey = `event_mapping:${calendarId}:${externalId}`;\n const existingThreadId = await this.get<Uuid>(mappingKey);\n\n if (existingThreadId) {\n // Event already exists - add update as a Note (add message to thread)\n if (incomingThread.notes?.[0]?.content) {\n await this.tools.plot.createNote({\n thread: { id: existingThreadId },\n content: incomingThread.notes[0].content,\n });\n }\n return;\n }\n\n // New event - generate UUID and store mapping\n const threadId = Uuid.Generate();\n await this.set(mappingKey, threadId);\n\n // Create new Thread with initial Note (new thread with first message)\n await this.tools.plot.createThread({\n ...incomingThread,\n id: threadId,\n });\n}\n```\n\n## Resource Selection\n\nResource selection (calendars, projects, channels) is handled automatically in the twist edit modal via the Integrations tool. Users see a list of available resources returned by your tool's `getChannels()` method and toggle them on/off. You do **not** need to build custom selection UI.\n\n```typescript\n// In your tool:\nasync getChannels(_auth: Authorization, token: AuthToken): Promise<Channel[]> {\n const client = new ApiClient({ accessToken: token.token });\n const calendars = await client.listCalendars();\n return calendars.map(c => ({\n id: c.id,\n title: c.name,\n children: c.subCalendars?.map(sc => ({ id: sc.id, title: sc.name })),\n }));\n}\n```\n\n## Batch Processing Pattern\n\n**Important**: Because Twists run in an ephemeral environment with limited requests per execution (~1000 requests), you must break long operations into batches. Each batch runs independently in a new execution context with its own fresh request limit.\n\n### Key Principles\n\n1. **Stay under request limits**: Each execution has ~1000 requests. Size batches accordingly.\n2. **Use runTask() for fresh limits**: Each call to `this.runTask()` creates a NEW execution with fresh ~1000 requests\n3. **Store state between batches**: Use the Store tool to persist progress\n4. **Calculate safe batch sizes**: Determine requests per item to size batches (e.g., ~10 requests per item = ~100 items per batch)\n5. **Clean up when done**: Delete stored state after completion\n6. **Handle failures**: Store enough state to resume if a batch fails\n\n### Example Implementation\n\n```typescript\nasync startSync(resourceId: string): Promise<void> {\n // Initialize state in Store (persists between executions)\n await this.set(`sync_state_${resourceId}`, {\n nextPageToken: null,\n batchNumber: 1,\n itemsProcessed: 0,\n initialSync: true, // Track whether this is the first sync\n });\n\n // Queue first batch using runTask method\n const callback = await this.callback(this.syncBatch, resourceId);\n // runTask creates NEW execution with fresh ~1000 request limit\n await this.runTask(callback);\n}\n\nasync syncBatch(resourceId: string): Promise<void> {\n // Load state from Store (set by previous execution)\n const state = await this.get(`sync_state_${resourceId}`);\n\n // Process one batch (size to stay under ~1000 request limit)\n const result = await this.fetchBatch(state.nextPageToken);\n\n // Process results using source/key pattern (automatic upserts, no manual tracking)\n // If each item makes ~10 requests, keep batch size ≤ 100 items to stay under limit\n for (const item of result.items) {\n // Each createThread may make ~5-10 requests depending on notes/links\n await this.tools.plot.createThread({\n source: item.url, // Use item's canonical URL for automatic deduplication\n type: ThreadType.Note,\n title: item.title,\n notes: [{\n activity: { source: item.url },\n key: \"description\", // Use key for upsertable notes\n content: item.description,\n }],\n ...(state.initialSync ? { unread: false } : {}), // false for initial, omit for incremental\n ...(state.initialSync ? { archived: false } : {}), // unarchive on initial only\n });\n }\n\n if (result.nextPageToken) {\n // Update state in Store for next batch\n await this.set(`sync_state_${resourceId}`, {\n nextPageToken: result.nextPageToken,\n batchNumber: state.batchNumber + 1,\n itemsProcessed: state.itemsProcessed + result.items.length,\n initialSync: state.initialSync, // Preserve initialSync flag across batches\n });\n\n // Queue next batch - creates NEW execution with fresh request limit\n const nextCallback = await this.callback(this.syncBatch, resourceId);\n await this.runTask(nextCallback);\n } else {\n // Cleanup when complete\n await this.clear(`sync_state_${resourceId}`);\n\n // Optionally notify user of completion\n await this.tools.plot.createThread({\n type: ThreadType.Note,\n title: \"Sync complete\",\n notes: [\n {\n content: `Successfully processed ${state.itemsProcessed + result.items.length} items.`,\n },\n ],\n });\n }\n}\n```\n\n## Thread Sync Best Practices\n\nWhen syncing threads from external systems, follow these patterns for optimal user experience:\n\n### The `initialSync` Flag\n\nAll sync-based tools should distinguish between initial sync (first import) and incremental sync (ongoing updates):\n\n| Field | Initial Sync | Incremental Sync | Reason |\n|-------|--------------|------------------|---------|\n| `unread` | `false` | *omit* | Initial: mark read for all. Incremental: auto-mark read for author only |\n| `archived` | `false` | *omit* | Unarchive on install, preserve user choice on updates |\n\n**Example:**\n```typescript\nconst thread: NewThread = {\n type: ThreadType.Event,\n source: event.url,\n title: event.title,\n ...(initialSync ? { unread: false } : {}), // false for initial, omit for incremental\n ...(initialSync ? { archived: false } : {}), // unarchive on initial only\n};\n```\n\n**Why this matters:**\n- **Initial sync**: Activities are unarchived and marked as read for all users, preventing spam from bulk historical imports\n- **Incremental sync**: Activities are auto-marked read for the author (twist owner), unread for everyone else. Archived state is preserved\n- **Reinstall**: Acts as initial sync, so previously archived activities are unarchived (fresh start)\n\n### Two-Way Sync: Avoiding Race Conditions\n\nWhen implementing two-way sync where items created in Plot are pushed to an external system (e.g. Notes becoming comments), a race condition can occur: the external system may send a webhook for the newly created item before you've updated the Thread/Note with the external key. The webhook handler won't find the item by external key and may create a duplicate.\n\n**Solution:** Embed the Plot `Thread.id` / `Note.id` in the external item's metadata when creating it, and update `Thread.source` / `Note.key` after creation. When processing webhooks, check for the Plot ID in metadata first.\n\n```typescript\nasync pushNoteAsComment(note: Note, externalItemId: string): Promise<void> {\n // Create external item with Plot ID in metadata for webhook correlation\n const externalComment = await externalApi.createComment(externalItemId, {\n body: note.content,\n metadata: { plotNoteId: note.id },\n });\n\n // Update Note with external key AFTER creation\n // A webhook may arrive before this completes — that's OK (see onWebhook below)\n await this.tools.plot.updateNote({\n id: note.id,\n key: `comment-${externalComment.id}`,\n });\n}\n\nasync onWebhook(payload: WebhookPayload): Promise<void> {\n const comment = payload.comment;\n\n // Use Plot ID from metadata if present (handles race condition),\n // otherwise fall back to upserting by activity source and key\n await this.tools.plot.createNote({\n ...(comment.metadata?.plotNoteId\n ? { id: comment.metadata.plotNoteId }\n : { activity: { source: payload.itemUrl } }),\n key: `comment-${comment.id}`,\n content: comment.body,\n });\n}\n```\n\n## Error Handling\n\nAlways handle errors gracefully and communicate them to users:\n\n```typescript\ntry {\n await this.externalOperation();\n} catch (error) {\n console.error(\"Operation failed:\", error);\n\n await this.tools.plot.createThread({\n type: ThreadType.Note,\n title: \"Operation failed\",\n notes: [\n {\n content: `Failed to complete operation: ${error.message}`,\n },\n ],\n });\n}\n```\n\n## Common Pitfalls\n\n- **Don't use instance variables for state** - Anything stored in memory is lost after function execution. Always use the Store tool for data that needs to persist.\n- **Processing self-created threads** - Other users may change a Thread created by the twist, resulting in a callback. Be sure to check the `changes === null` and/or `thread.author.id !== this.id` to avoid re-processing.\n- **Always create Threads with Notes** - See \"Understanding Threads and Notes\" section above for the thread/message pattern and decision tree.\n- **Use correct Thread types** - Most should be `ThreadType.Note`. Only use `Action` for tasks with `done`, and `Event` for items with `start`/`end`.\n- **Use Thread.source and Note.key for automatic upserts (Recommended)** - Set Thread.source to the external item's URL for automatic deduplication. Only use UUID generation and storage for advanced cases (see SYNC_STRATEGIES.md).\n- **Add Notes to existing Threads** - For source/key pattern, reference threads by source. For UUID pattern, look up stored mappings before creating new Threads. Think thread replies, not new threads.\n- Tools are declared in the `build` method and accessed via `this.tools.toolName` in twist methods.\n- **Don't forget request limits** - Each execution has ~1000 requests (HTTP requests, tool calls). Break long loops into batches with `this.runTask()` to get fresh request limits. Calculate requests per item to determine safe batch size (e.g., if each item needs ~10 requests, batch size = ~100 items).\n- **Always use Callbacks tool for persistent references** - Direct function references don't survive worker restarts.\n- **Store auth tokens** - Don't re-request authentication unnecessarily.\n- **Clean up callbacks and stored state** - Delete callbacks and Store entries when no longer needed.\n- **Handle missing auth gracefully** - Check for stored auth before operations.\n- **CRITICAL: Maintain callback backward compatibility** - All callbacks (webhooks, tasks, batch operations) automatically upgrade to new twist versions. You **must** maintain backward compatibility in callback method signatures. Only add optional parameters at the end, never remove or reorder parameters. For breaking changes, implement migration logic in the `upgrade()` lifecycle method to recreate affected callbacks.\n\n## Testing\n\nBefore deploying, verify:\n\n1. Linting passes: `{{packageManager}} lint`\n2. All dependencies are in package.json\n3. Authentication flow works end-to-end\n4. Batch operations handle pagination correctly\n5. Error cases are handled gracefully\n";
|
package/src/plot.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import type { NewSchedule, NewScheduleOccurrence, Schedule } from "./schedule";
|
|
2
2
|
import { type Tag } from "./tag";
|
|
3
3
|
import { type Callback } from "./tools/callbacks";
|
|
4
|
-
import { type AuthProvider } from "./tools/integrations";
|
|
5
4
|
import { type JSONValue } from "./utils/types";
|
|
6
5
|
import { Uuid } from "./utils/uuid";
|
|
7
6
|
|
|
@@ -184,6 +183,8 @@ export enum ActionType {
|
|
|
184
183
|
conferencing = "conferencing",
|
|
185
184
|
/** File attachment links stored in R2 */
|
|
186
185
|
file = "file",
|
|
186
|
+
/** Reference to an attachment hosted by a connector's source system */
|
|
187
|
+
fileRef = "fileRef",
|
|
187
188
|
/** Thread reference links for navigating to related threads */
|
|
188
189
|
thread = "thread",
|
|
189
190
|
/** Structured plan of operations for user approval */
|
|
@@ -301,6 +302,22 @@ export type Action =
|
|
|
301
302
|
/** Intrinsic height of the image in pixels (only for image files) */
|
|
302
303
|
imageHeight?: number | null;
|
|
303
304
|
}
|
|
305
|
+
| {
|
|
306
|
+
/** Reference to an attachment hosted by a connector's source system */
|
|
307
|
+
type: ActionType.fileRef;
|
|
308
|
+
/** Opaque identifier interpreted only by the owning connector */
|
|
309
|
+
ref: string;
|
|
310
|
+
/** Display filename */
|
|
311
|
+
fileName: string;
|
|
312
|
+
/** File size in bytes if known */
|
|
313
|
+
fileSize: number | null;
|
|
314
|
+
/** MIME type */
|
|
315
|
+
mimeType: string;
|
|
316
|
+
/** Intrinsic width of the image in pixels (only for image files) */
|
|
317
|
+
imageWidth?: number | null;
|
|
318
|
+
/** Intrinsic height of the image in pixels (only for image files) */
|
|
319
|
+
imageHeight?: number | null;
|
|
320
|
+
}
|
|
304
321
|
| {
|
|
305
322
|
/** Thread reference action for navigating to a related thread */
|
|
306
323
|
type: ActionType.thread;
|
|
@@ -382,6 +399,35 @@ export type Tags = { [K in Tag]?: ActorId[] };
|
|
|
382
399
|
*/
|
|
383
400
|
export type NewTags = { [K in Tag]?: NewActor[] };
|
|
384
401
|
|
|
402
|
+
/**
|
|
403
|
+
* A single emoji reaction key. Either:
|
|
404
|
+
* - A Unicode emoji grapheme cluster (e.g. `"👍"`, `"👨👩👧"`), or
|
|
405
|
+
* - A provider-scoped custom-emoji ref of the form
|
|
406
|
+
* `"<provider>:<workspaceId>/<name>"` (e.g. `"slack:T0123/party_parrot"`).
|
|
407
|
+
*
|
|
408
|
+
* Anything matching a known provider prefix is treated as a custom-emoji
|
|
409
|
+
* reference; everything else is rendered as the Unicode it contains.
|
|
410
|
+
*
|
|
411
|
+
* Reactions are the open-set counterpart to {@link Tag}'s count range
|
|
412
|
+
* (`1000+`). Use reactions for emoji that round-trip with chat platforms;
|
|
413
|
+
* use tags for Plot-managed compute/toggle state (todo, pinned, urgent,
|
|
414
|
+
* ...).
|
|
415
|
+
*/
|
|
416
|
+
export type Reaction = string;
|
|
417
|
+
|
|
418
|
+
/**
|
|
419
|
+
* Emoji reactions on an item, keyed by emoji string, with the list of
|
|
420
|
+
* actors who added each reaction.
|
|
421
|
+
*/
|
|
422
|
+
export type Reactions = Record<Reaction, ActorId[]>;
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* A set of reactions to add to an item, along with the actors adding each
|
|
426
|
+
* reaction. To remove a reaction for a given actor, omit them from the
|
|
427
|
+
* `NewActor[]` list — passing an empty list removes the reaction entirely.
|
|
428
|
+
*/
|
|
429
|
+
export type NewReactions = Record<Reaction, NewActor[]>;
|
|
430
|
+
|
|
385
431
|
/**
|
|
386
432
|
* Thread access level determining visibility.
|
|
387
433
|
* - "public": Visible to all users with priority access
|
|
@@ -408,6 +454,12 @@ export type ThreadCommon = {
|
|
|
408
454
|
archived: boolean;
|
|
409
455
|
/** Tags attached to this thread. Maps tag ID to array of actor IDs who added that tag. */
|
|
410
456
|
tags: Tags;
|
|
457
|
+
/**
|
|
458
|
+
* Emoji reactions on this item. Maps each emoji (Unicode grapheme or
|
|
459
|
+
* `provider:workspace/name` custom-emoji ref) to the list of actor IDs
|
|
460
|
+
* who reacted with it.
|
|
461
|
+
*/
|
|
462
|
+
reactions: Reactions;
|
|
411
463
|
};
|
|
412
464
|
|
|
413
465
|
/**
|
|
@@ -454,7 +506,7 @@ export type NewThreadWithNotes = NewThread & {
|
|
|
454
506
|
* ```
|
|
455
507
|
*/
|
|
456
508
|
export type NewThread = Partial<
|
|
457
|
-
Omit<ThreadFields, "priority" | "tags" | "id" | "accessContacts">
|
|
509
|
+
Omit<ThreadFields, "priority" | "tags" | "reactions" | "id" | "accessContacts">
|
|
458
510
|
> &
|
|
459
511
|
(
|
|
460
512
|
| {
|
|
@@ -474,6 +526,12 @@ export type NewThread = Partial<
|
|
|
474
526
|
*/
|
|
475
527
|
tags?: NewTags;
|
|
476
528
|
|
|
529
|
+
/**
|
|
530
|
+
* Emoji reactions to set on the new thread.
|
|
531
|
+
* Each emoji maps to the list of actors who reacted with it.
|
|
532
|
+
*/
|
|
533
|
+
reactions?: NewReactions;
|
|
534
|
+
|
|
477
535
|
/**
|
|
478
536
|
* The thread's sub-type/category. Sets the thread's icon.
|
|
479
537
|
* If omitted, defaults to "notes" (private) or "discussion" (shared).
|
|
@@ -549,6 +607,12 @@ type ThreadSingleUpdateFields = ThreadBulkUpdateFields & {
|
|
|
549
607
|
*/
|
|
550
608
|
tags?: NewTags;
|
|
551
609
|
|
|
610
|
+
/**
|
|
611
|
+
* Emoji reactions to change on the thread. Pass an empty `NewActor[]` to
|
|
612
|
+
* remove a reaction entirely; omit an emoji to leave it untouched.
|
|
613
|
+
*/
|
|
614
|
+
reactions?: NewReactions;
|
|
615
|
+
|
|
552
616
|
/**
|
|
553
617
|
* Add or remove the twist's tags.
|
|
554
618
|
* Maps tag ID to boolean: true = add tag, false = remove tag.
|
|
@@ -651,7 +715,7 @@ export type Note = ThreadCommon & {
|
|
|
651
715
|
export type NewNote = Partial<
|
|
652
716
|
Omit<
|
|
653
717
|
Note,
|
|
654
|
-
"author" | "thread" | "tags" | "mentions" | "accessContacts" | "id" | "key" | "reNote"
|
|
718
|
+
"author" | "thread" | "tags" | "reactions" | "mentions" | "accessContacts" | "id" | "key" | "reNote"
|
|
655
719
|
>
|
|
656
720
|
> &
|
|
657
721
|
({ id: Uuid } | { key: string } | {}) & {
|
|
@@ -681,6 +745,12 @@ export type NewNote = Partial<
|
|
|
681
745
|
*/
|
|
682
746
|
tags?: NewTags;
|
|
683
747
|
|
|
748
|
+
/**
|
|
749
|
+
* Emoji reactions to set on the note. Pass an empty `NewActor[]` to
|
|
750
|
+
* remove a reaction entirely; omit an emoji to leave it untouched.
|
|
751
|
+
*/
|
|
752
|
+
reactions?: NewReactions;
|
|
753
|
+
|
|
684
754
|
/**
|
|
685
755
|
* Contacts who can see this note, or null/undefined to inherit thread visibility.
|
|
686
756
|
* Accepts resolved ActorId UUIDs or email-based NewContact objects (resolved server-side).
|
|
@@ -748,6 +818,12 @@ export type NoteUpdate = ({ id: Uuid; key?: string } | { key: string }) &
|
|
|
748
818
|
*/
|
|
749
819
|
tags?: NewTags;
|
|
750
820
|
|
|
821
|
+
/**
|
|
822
|
+
* Emoji reactions to change on the note. Pass an empty `NewActor[]` to
|
|
823
|
+
* remove a reaction entirely; omit an emoji to leave it untouched.
|
|
824
|
+
*/
|
|
825
|
+
reactions?: NewReactions;
|
|
826
|
+
|
|
751
827
|
/**
|
|
752
828
|
* Add or remove the twist's tags.
|
|
753
829
|
* Maps tag ID to boolean: true = add tag, false = remove tag.
|
|
@@ -861,8 +937,22 @@ type NewContactBase = {
|
|
|
861
937
|
/**
|
|
862
938
|
* External provider account source. Used for identity resolution
|
|
863
939
|
* when email is unavailable and for privacy compliance reporting.
|
|
940
|
+
*
|
|
941
|
+
* The runtime scopes the resulting `contact_external_account` row to
|
|
942
|
+
* the dispatching twist instance (i.e. one row per connection per
|
|
943
|
+
* contact), so the same Plot contact can have multiple rows when
|
|
944
|
+
* reachable through multiple connections (e.g. two Slack workspaces,
|
|
945
|
+
* Gmail + Google Chat sharing one Google account).
|
|
946
|
+
*/
|
|
947
|
+
source?: { accountId: string };
|
|
948
|
+
/**
|
|
949
|
+
* Optional connector-defined role for this contact on the thread, matching
|
|
950
|
+
* a `LinkTypeConfig.contactRoles[].id` (e.g. "to" / "cc" / "bcc" for
|
|
951
|
+
* email, "required" / "optional" for calendar). Omitted ⇒ default role.
|
|
952
|
+
* Connectors set this on inbound sync; the runtime persists it under
|
|
953
|
+
* `thread.contact_meta[contact_id].role`.
|
|
864
954
|
*/
|
|
865
|
-
|
|
955
|
+
role?: string;
|
|
866
956
|
};
|
|
867
957
|
|
|
868
958
|
/**
|
package/src/tag.ts
CHANGED
|
@@ -1,54 +1,23 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* Compute tags — system state the runtime auto-manages on threads and
|
|
3
|
+
* notes (`todo`, `done`, `twist` activity marker, …).
|
|
4
|
+
*
|
|
5
|
+
* The toggle range (100–999) and count range (1000–1027) have been
|
|
6
|
+
* retired in favour of the open Unicode emoji `Reaction` type — see
|
|
7
|
+
* `@plotday/twister/plot`'s `Reactions` / `NewReactions` and the
|
|
8
|
+
* per-row `note.reactions` / `thread.reactions` fields. Connectors
|
|
9
|
+
* route emoji reactions through `reactions`, not `tags`.
|
|
10
|
+
*
|
|
11
|
+
* `Tag.Twist` is the surviving non-trivial tag: a system marker the
|
|
12
|
+
* runtime adds to a note while a twist is processing it, and clears
|
|
13
|
+
* once the twist returns. It's not user-facing and not a reaction.
|
|
6
14
|
*/
|
|
7
15
|
export enum Tag {
|
|
8
|
-
// Special tags
|
|
9
16
|
Todo = 1,
|
|
10
17
|
Done = 3,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
Decision = 104,
|
|
17
|
-
Waiting = 105,
|
|
18
|
-
Blocked = 106,
|
|
19
|
-
Warning = 107,
|
|
20
|
-
Question = 108,
|
|
21
|
-
Twist = 109,
|
|
22
|
-
Star = 110,
|
|
23
|
-
Idea = 111,
|
|
24
|
-
|
|
25
|
-
// Count tags
|
|
26
|
-
Yes = 1000,
|
|
27
|
-
No = 1001,
|
|
28
|
-
Volunteer = 1002,
|
|
29
|
-
Tada = 1003,
|
|
30
|
-
Fire = 1004,
|
|
31
|
-
Totally = 1005,
|
|
32
|
-
Looking = 1006,
|
|
33
|
-
Love = 1007,
|
|
34
|
-
Rocket = 1008,
|
|
35
|
-
Sparkles = 1009,
|
|
36
|
-
Thanks = 1010,
|
|
37
|
-
Smile = 1011,
|
|
38
|
-
Wave = 1012,
|
|
39
|
-
Praise = 1015,
|
|
40
|
-
Applause = 1016,
|
|
41
|
-
Cool = 1017,
|
|
42
|
-
Sad = 1018,
|
|
43
|
-
Reply = 1019,
|
|
44
|
-
Thinking = 1013,
|
|
45
|
-
Remember = 1014,
|
|
46
|
-
Agreed = 1020,
|
|
47
|
-
Relieved = 1021,
|
|
48
|
-
Send = 1022,
|
|
49
|
-
Noted = 1023,
|
|
50
|
-
Laugh = 1024,
|
|
51
|
-
Surprised = 1025,
|
|
52
|
-
Confused = 1026,
|
|
53
|
-
Dismayed = 1027,
|
|
18
|
+
/** System marker for "a twist is processing this note." Set by the
|
|
19
|
+
* runtime when a twist callback fires, cleared on return. Twists
|
|
20
|
+
* may still write `{ [Tag.Twist]: true | false }` to twistTags to
|
|
21
|
+
* mark/unmark a note explicitly. */
|
|
22
|
+
Twist = 12,
|
|
54
23
|
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { ITool } from "..";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Built-in tool for reading files attached to notes in Plot.
|
|
5
|
+
*
|
|
6
|
+
* Files are uploaded by clients via POST /files which creates an
|
|
7
|
+
* ActionType.file entry on a note. Connectors call read() during outbound
|
|
8
|
+
* (e.g. onNoteCreated) to retrieve those bytes and send them to the source
|
|
9
|
+
* system.
|
|
10
|
+
*
|
|
11
|
+
* For inbound attachments, connectors emit ActionType.fileRef actions and
|
|
12
|
+
* implement Connector.downloadAttachment — no upload tool is needed because
|
|
13
|
+
* inbound bytes never enter Plot's R2 storage.
|
|
14
|
+
*/
|
|
15
|
+
export abstract class Files extends ITool {
|
|
16
|
+
/**
|
|
17
|
+
* Read a file uploaded by a client and attached to a note in a priority
|
|
18
|
+
* where this twist is installed.
|
|
19
|
+
*
|
|
20
|
+
* @param fileId The id from an ActionType.file action.
|
|
21
|
+
* @returns Bytes plus original metadata.
|
|
22
|
+
* @throws FileNotFoundError if the file is missing or out of scope.
|
|
23
|
+
*/
|
|
24
|
+
abstract read(fileId: string): Promise<{
|
|
25
|
+
data: Uint8Array;
|
|
26
|
+
fileName: string;
|
|
27
|
+
mimeType: string;
|
|
28
|
+
fileSize: number;
|
|
29
|
+
}>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export class FileNotFoundError extends Error {
|
|
33
|
+
constructor(fileId: string) {
|
|
34
|
+
super(`File not found or out of scope: ${fileId}`);
|
|
35
|
+
this.name = "FileNotFoundError";
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/tools/index.ts
CHANGED
|
@@ -4,9 +4,7 @@ import {
|
|
|
4
4
|
type NewContact,
|
|
5
5
|
type NewLinkWithNotes,
|
|
6
6
|
ITool,
|
|
7
|
-
Serializable,
|
|
8
7
|
} from "..";
|
|
9
|
-
import { Tag } from "../tag";
|
|
10
8
|
import type { JSONValue } from "../utils/types";
|
|
11
9
|
import type { Uuid } from "../utils/uuid";
|
|
12
10
|
|
|
@@ -34,6 +32,13 @@ export type LinkTypeConfig = {
|
|
|
34
32
|
type: string;
|
|
35
33
|
/** Human-readable label (e.g., "Issue", "Pull Request") */
|
|
36
34
|
label: string;
|
|
35
|
+
/**
|
|
36
|
+
* Connector's word for a note on a linked item of this type — used by the
|
|
37
|
+
* Flutter app to adapt note/composer copy ("Add a comment" on Linear,
|
|
38
|
+
* "Add a message" on Slack, "Add a reply" on Gmail). Defaults to "note"
|
|
39
|
+
* when omitted. Use the singular noun in title case (e.g. "Comment").
|
|
40
|
+
*/
|
|
41
|
+
noteLabel?: string;
|
|
37
42
|
/** URL to an icon for this link type (light mode). Prefer Iconify `logos/*` URLs. */
|
|
38
43
|
logo?: string;
|
|
39
44
|
/** URL to an icon for dark mode. Use when the default logo is invisible on dark backgrounds (e.g., Iconify `simple-icons/*` with `?color=`). */
|
|
@@ -46,8 +51,6 @@ export type LinkTypeConfig = {
|
|
|
46
51
|
status: string;
|
|
47
52
|
/** Human-readable label (e.g., "Open", "Done") */
|
|
48
53
|
label: string;
|
|
49
|
-
/** Tag to propagate to thread when this status is active (e.g., Tag.Done) */
|
|
50
|
-
tag?: Tag;
|
|
51
54
|
/** Whether this status represents completion (done, closed, merged, cancelled, etc.) */
|
|
52
55
|
done?: boolean;
|
|
53
56
|
/**
|
|
@@ -82,18 +85,121 @@ export type LinkTypeConfig = {
|
|
|
82
85
|
* Gmail's "starred", Linear's "todo").
|
|
83
86
|
*/
|
|
84
87
|
todo?: boolean;
|
|
85
|
-
/**
|
|
86
|
-
* Default status applied when Plot asks the connector to create a new
|
|
87
|
-
* item of this type via `Connector.onCreateLink`. Declaring at least one
|
|
88
|
-
* status with `createDefault: true` is how a link type opts in to
|
|
89
|
-
* Plot-initiated creation. At most one status per type should set this.
|
|
90
|
-
*/
|
|
91
|
-
createDefault?: boolean;
|
|
92
88
|
}>;
|
|
93
89
|
/** Whether this link type supports displaying and changing the assignee */
|
|
94
90
|
supportsAssignee?: boolean;
|
|
95
91
|
/** Default thread creation mode for this link type: 'all' | 'actionable' | 'manual' */
|
|
96
92
|
defaultCreateThreads?: string;
|
|
93
|
+
/**
|
|
94
|
+
* Opt-in: declares this link type is composable from Plot via
|
|
95
|
+
* `Connector.onCreateLink`. Omit to make the link type sync-only (no
|
|
96
|
+
* "Create new …" picker entry).
|
|
97
|
+
*
|
|
98
|
+
* Connectors that need multiple compose modes for what users perceive as
|
|
99
|
+
* the same kind of thing (e.g. Slack channel post vs DM) should declare
|
|
100
|
+
* **separate linkTypes**, one per user-facing thread type. That keeps
|
|
101
|
+
* each linkType isomorphic to one filter chip.
|
|
102
|
+
*/
|
|
103
|
+
compose?: ComposeConfig;
|
|
104
|
+
/**
|
|
105
|
+
* Per-connector contact roles. Examples:
|
|
106
|
+
* email → [{id:"to",label:"To",default:true},{id:"cc",label:"CC"},{id:"bcc",label:"BCC",hidden:true}]
|
|
107
|
+
* calendar → [{id:"required",label:"Required",default:true},{id:"optional",label:"Optional"}]
|
|
108
|
+
*
|
|
109
|
+
* Plot uses this list to render a role picker on each contact chip in the
|
|
110
|
+
* composer and to label non-default roles on existing threads. Exactly one
|
|
111
|
+
* role should be marked `default: true`. Connectors that don't distinguish
|
|
112
|
+
* roles (Slack, Linear) omit this field entirely.
|
|
113
|
+
*/
|
|
114
|
+
contactRoles?: ContactRoleConfig[];
|
|
115
|
+
/**
|
|
116
|
+
* Whether contacts on an existing thread can be added, removed, or have
|
|
117
|
+
* their role changed (email-style mid-thread recipient changes). When
|
|
118
|
+
* false, the thread's contact list is fixed after creation. Defaults to
|
|
119
|
+
* false when omitted.
|
|
120
|
+
*/
|
|
121
|
+
supportsContactChanges?: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Declares how sharing on threads of this link type is scoped:
|
|
124
|
+
*
|
|
125
|
+
* - `"thread"` (default): one roster shared across all notes in the
|
|
126
|
+
* thread. Native Plot threads, Slack DMs, calendar events.
|
|
127
|
+
* - `"channel"`: visibility is the external channel's membership;
|
|
128
|
+
* the per-thread `contacts` array is ignored for sharing UI.
|
|
129
|
+
* Slack channels, Linear projects.
|
|
130
|
+
* - `"message"`: each note carries its own recipient set via
|
|
131
|
+
* `note.access_contacts`; the thread roster is the union across
|
|
132
|
+
* all messages. Email.
|
|
133
|
+
*
|
|
134
|
+
* Omit to default to `"thread"`. When set to `"message"`, every
|
|
135
|
+
* note this connector ingests must populate `access_contacts`
|
|
136
|
+
* explicitly (never NULL).
|
|
137
|
+
*/
|
|
138
|
+
sharingModel?: "thread" | "channel" | "message";
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Declares how a link type is composable from Plot via
|
|
143
|
+
* `Connector.onCreateLink`. Attached to {@link LinkTypeConfig.compose}.
|
|
144
|
+
*/
|
|
145
|
+
export type ComposeConfig = {
|
|
146
|
+
/**
|
|
147
|
+
* Selects the destination model for the "Create new …" picker.
|
|
148
|
+
*
|
|
149
|
+
* - `"channels"` (default): one chip per enabled channel (e.g. a Linear
|
|
150
|
+
* team, a Slack channel). Existing behaviour for task-tracker / calendar
|
|
151
|
+
* connectors.
|
|
152
|
+
* - `"contacts"`: one chip per connection (account); the user picks
|
|
153
|
+
* recipients from their contacts. The runtime pre-resolves the chosen
|
|
154
|
+
* Plot contacts to platform account IDs via the per-connection
|
|
155
|
+
* `contact_external_account` rows and delivers them as
|
|
156
|
+
* `CreateLinkDraft.recipients`. Contacts without a row for this specific
|
|
157
|
+
* connection are filtered out of the picker — used by closed-roster
|
|
158
|
+
* messaging platforms (Slack DM, Teams DM, Google Chat DM, LinkedIn DM).
|
|
159
|
+
* - `"addresses"`: one chip per connection; the picker accepts any
|
|
160
|
+
* contact with an addressable identifier (e.g. an email) or a free-form
|
|
161
|
+
* typed address. The runtime fills `recipients` for contacts with a
|
|
162
|
+
* connection-scoped row and falls back to the contact's primary address
|
|
163
|
+
* (e.g. `contact.email`) when no row exists. Free-form addresses arrive
|
|
164
|
+
* via the thread's `inviteEmails`. Used by open address spaces like
|
|
165
|
+
* Gmail.
|
|
166
|
+
*/
|
|
167
|
+
targets?: "channels" | "contacts" | "addresses";
|
|
168
|
+
/**
|
|
169
|
+
* Status to assign newly-created links. Should match an entry in the
|
|
170
|
+
* parent linkType's `statuses[]`, OR a symbolic id that the connector's
|
|
171
|
+
* `onCreateLink` resolves itself (e.g. Linear's `"unstarted"` category is
|
|
172
|
+
* resolved per-team to a state UUID inside the connector — see
|
|
173
|
+
* `connectors/linear/src/linear.ts`).
|
|
174
|
+
*/
|
|
175
|
+
status: string;
|
|
176
|
+
/**
|
|
177
|
+
* Optional override for the picker chip / "Create new …" copy. Defaults
|
|
178
|
+
* to the parent linkType's `label`. Use to disambiguate compose entries
|
|
179
|
+
* when the parent label alone isn't specific enough (e.g. "Direct
|
|
180
|
+
* messages" for a DM-mode compose on a chat connector).
|
|
181
|
+
*/
|
|
182
|
+
label?: string;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Declares one contact role for a connector's link type. See
|
|
187
|
+
* `LinkTypeConfig.contactRoles`.
|
|
188
|
+
*/
|
|
189
|
+
export type ContactRoleConfig = {
|
|
190
|
+
/** Stable machine id, e.g. "to" / "cc" / "bcc" / "required" / "optional". */
|
|
191
|
+
id: string;
|
|
192
|
+
/** Display label shown next to a contact chip, e.g. "To", "CC", "Required". */
|
|
193
|
+
label: string;
|
|
194
|
+
/** Exactly one role per linkType should be marked default. */
|
|
195
|
+
default?: boolean;
|
|
196
|
+
/**
|
|
197
|
+
* Hidden roles are visible only to (a) the contact themselves and
|
|
198
|
+
* (b) the user who added them. The API filters them out of every other
|
|
199
|
+
* viewer's `thread.contacts` and `thread.contactMeta`. Use for BCC-style
|
|
200
|
+
* semantics where other recipients must not see the hidden contact.
|
|
201
|
+
*/
|
|
202
|
+
hidden?: boolean;
|
|
97
203
|
};
|
|
98
204
|
|
|
99
205
|
/**
|
|
@@ -209,31 +315,6 @@ export abstract class Integrations extends ITool {
|
|
|
209
315
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
210
316
|
abstract get(provider: AuthProvider, channelId: string): Promise<AuthToken | null>;
|
|
211
317
|
|
|
212
|
-
/**
|
|
213
|
-
* Execute a callback as a specific actor, requesting auth if needed.
|
|
214
|
-
*
|
|
215
|
-
* If the actor has a valid token, calls the callback immediately with it.
|
|
216
|
-
* If the actor has no token, creates a private auth note in the specified
|
|
217
|
-
* activity prompting them to connect. Once they authorize, this callback fires.
|
|
218
|
-
*
|
|
219
|
-
* @param provider - The OAuth provider
|
|
220
|
-
* @param actorId - The actor to act as
|
|
221
|
-
* @param activityId - The activity to create an auth note in (if needed)
|
|
222
|
-
* @param callback - Function to call with the token
|
|
223
|
-
* @param extraArgs - Additional arguments to pass to the callback
|
|
224
|
-
*/
|
|
225
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
226
|
-
abstract actAs<
|
|
227
|
-
TArgs extends Serializable[],
|
|
228
|
-
TCallback extends (token: AuthToken, ...args: TArgs) => any
|
|
229
|
-
>(
|
|
230
|
-
provider: AuthProvider,
|
|
231
|
-
actorId: ActorId,
|
|
232
|
-
activityId: Uuid,
|
|
233
|
-
callback: TCallback,
|
|
234
|
-
...extraArgs: TArgs
|
|
235
|
-
): Promise<void>;
|
|
236
|
-
|
|
237
318
|
/**
|
|
238
319
|
* Saves a link with notes to the connector's priority.
|
|
239
320
|
*
|
|
@@ -274,10 +355,15 @@ export abstract class Integrations extends ITool {
|
|
|
274
355
|
abstract saveLinks(links: NewLinkWithNotes[]): Promise<(Uuid | null)[]>;
|
|
275
356
|
|
|
276
357
|
/**
|
|
277
|
-
*
|
|
358
|
+
* Upserts contacts into the connector's priority without requiring a Link.
|
|
359
|
+
*
|
|
360
|
+
* Use this for messaging connectors to bulk-sync workspace members so the
|
|
361
|
+
* recipient picker can filter contacts by reachable platform account. Populate
|
|
362
|
+
* `NewContact.source` to persist `contact_external_account` rows (the platform
|
|
363
|
+
* identity used to address the contact). Returns one `Actor` per input, in order.
|
|
278
364
|
*
|
|
279
|
-
* @param contacts -
|
|
280
|
-
* @returns Promise resolving to the saved actors
|
|
365
|
+
* @param contacts - Contacts to upsert, keyed by `source`/`key`
|
|
366
|
+
* @returns Promise resolving to the saved actors, 1:1 with input order
|
|
281
367
|
*/
|
|
282
368
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
283
369
|
abstract saveContacts(contacts: NewContact[]): Promise<Actor[]>;
|