@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
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { type Actor, type ActorId, type NewContact, type NewLinkWithNotes, ITool
|
|
2
|
-
import { Tag } from "../tag";
|
|
1
|
+
import { type Actor, type ActorId, type NewContact, type NewLinkWithNotes, ITool } from "..";
|
|
3
2
|
import type { JSONValue } from "../utils/types";
|
|
4
3
|
import type { Uuid } from "../utils/uuid";
|
|
5
4
|
/**
|
|
@@ -25,6 +24,13 @@ export type LinkTypeConfig = {
|
|
|
25
24
|
type: string;
|
|
26
25
|
/** Human-readable label (e.g., "Issue", "Pull Request") */
|
|
27
26
|
label: string;
|
|
27
|
+
/**
|
|
28
|
+
* Connector's word for a note on a linked item of this type — used by the
|
|
29
|
+
* Flutter app to adapt note/composer copy ("Add a comment" on Linear,
|
|
30
|
+
* "Add a message" on Slack, "Add a reply" on Gmail). Defaults to "note"
|
|
31
|
+
* when omitted. Use the singular noun in title case (e.g. "Comment").
|
|
32
|
+
*/
|
|
33
|
+
noteLabel?: string;
|
|
28
34
|
/** URL to an icon for this link type (light mode). Prefer Iconify `logos/*` URLs. */
|
|
29
35
|
logo?: string;
|
|
30
36
|
/** URL to an icon for dark mode. Use when the default logo is invisible on dark backgrounds (e.g., Iconify `simple-icons/*` with `?color=`). */
|
|
@@ -37,8 +43,6 @@ export type LinkTypeConfig = {
|
|
|
37
43
|
status: string;
|
|
38
44
|
/** Human-readable label (e.g., "Open", "Done") */
|
|
39
45
|
label: string;
|
|
40
|
-
/** Tag to propagate to thread when this status is active (e.g., Tag.Done) */
|
|
41
|
-
tag?: Tag;
|
|
42
46
|
/** Whether this status represents completion (done, closed, merged, cancelled, etc.) */
|
|
43
47
|
done?: boolean;
|
|
44
48
|
/**
|
|
@@ -73,18 +77,119 @@ export type LinkTypeConfig = {
|
|
|
73
77
|
* Gmail's "starred", Linear's "todo").
|
|
74
78
|
*/
|
|
75
79
|
todo?: boolean;
|
|
76
|
-
/**
|
|
77
|
-
* Default status applied when Plot asks the connector to create a new
|
|
78
|
-
* item of this type via `Connector.onCreateLink`. Declaring at least one
|
|
79
|
-
* status with `createDefault: true` is how a link type opts in to
|
|
80
|
-
* Plot-initiated creation. At most one status per type should set this.
|
|
81
|
-
*/
|
|
82
|
-
createDefault?: boolean;
|
|
83
80
|
}>;
|
|
84
81
|
/** Whether this link type supports displaying and changing the assignee */
|
|
85
82
|
supportsAssignee?: boolean;
|
|
86
83
|
/** Default thread creation mode for this link type: 'all' | 'actionable' | 'manual' */
|
|
87
84
|
defaultCreateThreads?: string;
|
|
85
|
+
/**
|
|
86
|
+
* Opt-in: declares this link type is composable from Plot via
|
|
87
|
+
* `Connector.onCreateLink`. Omit to make the link type sync-only (no
|
|
88
|
+
* "Create new …" picker entry).
|
|
89
|
+
*
|
|
90
|
+
* Connectors that need multiple compose modes for what users perceive as
|
|
91
|
+
* the same kind of thing (e.g. Slack channel post vs DM) should declare
|
|
92
|
+
* **separate linkTypes**, one per user-facing thread type. That keeps
|
|
93
|
+
* each linkType isomorphic to one filter chip.
|
|
94
|
+
*/
|
|
95
|
+
compose?: ComposeConfig;
|
|
96
|
+
/**
|
|
97
|
+
* Per-connector contact roles. Examples:
|
|
98
|
+
* email → [{id:"to",label:"To",default:true},{id:"cc",label:"CC"},{id:"bcc",label:"BCC",hidden:true}]
|
|
99
|
+
* calendar → [{id:"required",label:"Required",default:true},{id:"optional",label:"Optional"}]
|
|
100
|
+
*
|
|
101
|
+
* Plot uses this list to render a role picker on each contact chip in the
|
|
102
|
+
* composer and to label non-default roles on existing threads. Exactly one
|
|
103
|
+
* role should be marked `default: true`. Connectors that don't distinguish
|
|
104
|
+
* roles (Slack, Linear) omit this field entirely.
|
|
105
|
+
*/
|
|
106
|
+
contactRoles?: ContactRoleConfig[];
|
|
107
|
+
/**
|
|
108
|
+
* Whether contacts on an existing thread can be added, removed, or have
|
|
109
|
+
* their role changed (email-style mid-thread recipient changes). When
|
|
110
|
+
* false, the thread's contact list is fixed after creation. Defaults to
|
|
111
|
+
* false when omitted.
|
|
112
|
+
*/
|
|
113
|
+
supportsContactChanges?: boolean;
|
|
114
|
+
/**
|
|
115
|
+
* Declares how sharing on threads of this link type is scoped:
|
|
116
|
+
*
|
|
117
|
+
* - `"thread"` (default): one roster shared across all notes in the
|
|
118
|
+
* thread. Native Plot threads, Slack DMs, calendar events.
|
|
119
|
+
* - `"channel"`: visibility is the external channel's membership;
|
|
120
|
+
* the per-thread `contacts` array is ignored for sharing UI.
|
|
121
|
+
* Slack channels, Linear projects.
|
|
122
|
+
* - `"message"`: each note carries its own recipient set via
|
|
123
|
+
* `note.access_contacts`; the thread roster is the union across
|
|
124
|
+
* all messages. Email.
|
|
125
|
+
*
|
|
126
|
+
* Omit to default to `"thread"`. When set to `"message"`, every
|
|
127
|
+
* note this connector ingests must populate `access_contacts`
|
|
128
|
+
* explicitly (never NULL).
|
|
129
|
+
*/
|
|
130
|
+
sharingModel?: "thread" | "channel" | "message";
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
133
|
+
* Declares how a link type is composable from Plot via
|
|
134
|
+
* `Connector.onCreateLink`. Attached to {@link LinkTypeConfig.compose}.
|
|
135
|
+
*/
|
|
136
|
+
export type ComposeConfig = {
|
|
137
|
+
/**
|
|
138
|
+
* Selects the destination model for the "Create new …" picker.
|
|
139
|
+
*
|
|
140
|
+
* - `"channels"` (default): one chip per enabled channel (e.g. a Linear
|
|
141
|
+
* team, a Slack channel). Existing behaviour for task-tracker / calendar
|
|
142
|
+
* connectors.
|
|
143
|
+
* - `"contacts"`: one chip per connection (account); the user picks
|
|
144
|
+
* recipients from their contacts. The runtime pre-resolves the chosen
|
|
145
|
+
* Plot contacts to platform account IDs via the per-connection
|
|
146
|
+
* `contact_external_account` rows and delivers them as
|
|
147
|
+
* `CreateLinkDraft.recipients`. Contacts without a row for this specific
|
|
148
|
+
* connection are filtered out of the picker — used by closed-roster
|
|
149
|
+
* messaging platforms (Slack DM, Teams DM, Google Chat DM, LinkedIn DM).
|
|
150
|
+
* - `"addresses"`: one chip per connection; the picker accepts any
|
|
151
|
+
* contact with an addressable identifier (e.g. an email) or a free-form
|
|
152
|
+
* typed address. The runtime fills `recipients` for contacts with a
|
|
153
|
+
* connection-scoped row and falls back to the contact's primary address
|
|
154
|
+
* (e.g. `contact.email`) when no row exists. Free-form addresses arrive
|
|
155
|
+
* via the thread's `inviteEmails`. Used by open address spaces like
|
|
156
|
+
* Gmail.
|
|
157
|
+
*/
|
|
158
|
+
targets?: "channels" | "contacts" | "addresses";
|
|
159
|
+
/**
|
|
160
|
+
* Status to assign newly-created links. Should match an entry in the
|
|
161
|
+
* parent linkType's `statuses[]`, OR a symbolic id that the connector's
|
|
162
|
+
* `onCreateLink` resolves itself (e.g. Linear's `"unstarted"` category is
|
|
163
|
+
* resolved per-team to a state UUID inside the connector — see
|
|
164
|
+
* `connectors/linear/src/linear.ts`).
|
|
165
|
+
*/
|
|
166
|
+
status: string;
|
|
167
|
+
/**
|
|
168
|
+
* Optional override for the picker chip / "Create new …" copy. Defaults
|
|
169
|
+
* to the parent linkType's `label`. Use to disambiguate compose entries
|
|
170
|
+
* when the parent label alone isn't specific enough (e.g. "Direct
|
|
171
|
+
* messages" for a DM-mode compose on a chat connector).
|
|
172
|
+
*/
|
|
173
|
+
label?: string;
|
|
174
|
+
};
|
|
175
|
+
/**
|
|
176
|
+
* Declares one contact role for a connector's link type. See
|
|
177
|
+
* `LinkTypeConfig.contactRoles`.
|
|
178
|
+
*/
|
|
179
|
+
export type ContactRoleConfig = {
|
|
180
|
+
/** Stable machine id, e.g. "to" / "cc" / "bcc" / "required" / "optional". */
|
|
181
|
+
id: string;
|
|
182
|
+
/** Display label shown next to a contact chip, e.g. "To", "CC", "Required". */
|
|
183
|
+
label: string;
|
|
184
|
+
/** Exactly one role per linkType should be marked default. */
|
|
185
|
+
default?: boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Hidden roles are visible only to (a) the contact themselves and
|
|
188
|
+
* (b) the user who added them. The API filters them out of every other
|
|
189
|
+
* viewer's `thread.contacts` and `thread.contactMeta`. Use for BCC-style
|
|
190
|
+
* semantics where other recipients must not see the hidden contact.
|
|
191
|
+
*/
|
|
192
|
+
hidden?: boolean;
|
|
88
193
|
};
|
|
89
194
|
/**
|
|
90
195
|
* Context passed to onChannelEnabled with plan-based sync hints.
|
|
@@ -192,20 +297,6 @@ export declare abstract class Integrations extends ITool {
|
|
|
192
297
|
* @deprecated Use get(channelId) instead. The provider is implicit from the connector.
|
|
193
298
|
*/
|
|
194
299
|
abstract get(provider: AuthProvider, channelId: string): Promise<AuthToken | null>;
|
|
195
|
-
/**
|
|
196
|
-
* Execute a callback as a specific actor, requesting auth if needed.
|
|
197
|
-
*
|
|
198
|
-
* If the actor has a valid token, calls the callback immediately with it.
|
|
199
|
-
* If the actor has no token, creates a private auth note in the specified
|
|
200
|
-
* activity prompting them to connect. Once they authorize, this callback fires.
|
|
201
|
-
*
|
|
202
|
-
* @param provider - The OAuth provider
|
|
203
|
-
* @param actorId - The actor to act as
|
|
204
|
-
* @param activityId - The activity to create an auth note in (if needed)
|
|
205
|
-
* @param callback - Function to call with the token
|
|
206
|
-
* @param extraArgs - Additional arguments to pass to the callback
|
|
207
|
-
*/
|
|
208
|
-
abstract actAs<TArgs extends Serializable[], TCallback extends (token: AuthToken, ...args: TArgs) => any>(provider: AuthProvider, actorId: ActorId, activityId: Uuid, callback: TCallback, ...extraArgs: TArgs): Promise<void>;
|
|
209
300
|
/**
|
|
210
301
|
* Saves a link with notes to the connector's priority.
|
|
211
302
|
*
|
|
@@ -242,10 +333,15 @@ export declare abstract class Integrations extends ITool {
|
|
|
242
333
|
*/
|
|
243
334
|
abstract saveLinks(links: NewLinkWithNotes[]): Promise<(Uuid | null)[]>;
|
|
244
335
|
/**
|
|
245
|
-
*
|
|
336
|
+
* Upserts contacts into the connector's priority without requiring a Link.
|
|
337
|
+
*
|
|
338
|
+
* Use this for messaging connectors to bulk-sync workspace members so the
|
|
339
|
+
* recipient picker can filter contacts by reachable platform account. Populate
|
|
340
|
+
* `NewContact.source` to persist `contact_external_account` rows (the platform
|
|
341
|
+
* identity used to address the contact). Returns one `Actor` per input, in order.
|
|
246
342
|
*
|
|
247
|
-
* @param contacts -
|
|
248
|
-
* @returns Promise resolving to the saved actors
|
|
343
|
+
* @param contacts - Contacts to upsert, keyed by `source`/`key`
|
|
344
|
+
* @returns Promise resolving to the saved actors, 1:1 with input order
|
|
249
345
|
*/
|
|
250
346
|
abstract saveContacts(contacts: NewContact[]): Promise<Actor[]>;
|
|
251
347
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integrations.d.ts","sourceRoot":"","sources":["../../src/tools/integrations.ts"],"names":[],"mappings":"AA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EAAE,MAAM,EAAE,CAAC;IACjB;;;;;;;OAOG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACnC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"integrations.js","sourceRoot":"","sources":["../../src/tools/integrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,
|
|
1
|
+
{"version":3,"file":"integrations.js","sourceRoot":"","sources":["../../src/tools/integrations.ts"],"names":[],"mappings":"AAAA,OAAO,EAKL,KAAK,GACN,MAAM,IAAI,CAAC;AA+OZ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuCG;AACH,MAAM,OAAgB,YAAa,SAAQ,KAAK;IAC9C;;;;;OAKG;IACH,MAAM,CAAC,WAAW,CAAC,GAAG,WAAuB;QAC3C,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,GAAG,CAAC,WAAW,CAAC,IAAI,EAAE,CAAC,CAAC,CAAC;IACjD,CAAC;CAuJF;AAiBD;;;;;GAKG;AACH,MAAM,CAAN,IAAY,YAyBX;AAzBD,WAAY,YAAY;IACtB,0DAA0D;IAC1D,iCAAiB,CAAA;IACjB,0DAA0D;IAC1D,uCAAuB,CAAA;IACvB,kDAAkD;IAClD,iCAAiB,CAAA;IACjB,gDAAgD;IAChD,+BAAe,CAAA;IACf,uDAAuD;IACvD,uCAAuB,CAAA;IACvB,kDAAkD;IAClD,iCAAiB,CAAA;IACjB,gCAAgC;IAChC,iCAAiB,CAAA;IACjB,sEAAsE;IACtE,iCAAiB,CAAA;IACjB,gDAAgD;IAChD,+BAAe,CAAA;IACf,6CAA6C;IAC7C,mCAAmB,CAAA;IACnB,yDAAyD;IACzD,mCAAmB,CAAA;IACnB,iDAAiD;IACjD,qCAAqB,CAAA;AACvB,CAAC,EAzBW,YAAY,KAAZ,YAAY,QAyBvB"}
|
package/dist/twist-guide.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare const TWIST_GUIDE = "# 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 \u251C\u2500 Has stable URL or ID?\n \u2502 \u2514\u2500 Yes \u2192 Set Thread.source to the canonical URL/ID\n \u2502 Create Thread (Plot handles deduplication automatically)\n \u2502 Use Note.key for different note types:\n \u2502 - \"description\" for main content\n \u2502 - \"metadata\" for status/priority/assignee\n \u2502 - \"comment-{id}\" for individual comments\n \u2502\n \u2514\u2500 No stable identifier OR need multiple Plot threads per external item?\n \u2514\u2500 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 \u251C\u2500 Yes \u2192 Generate UUID with Uuid.Generate()\n \u2502 Create new Thread with that UUID\n \u2502 Store mapping: external_id \u2192 thread_uuid\n \u2502\n \u2514\u2500 No (update/reply/comment) \u2192 Look up mapping by external_id\n \u251C\u2500 Found \u2192 Add Note to existing Thread using stored UUID\n \u2514\u2500 Not found \u2192 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 \u2192 \"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` \u2014 Auto-mention on replies to threads your twist created (e.g., synced issues, emails)\n- `note.defaultMention` \u2014 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 \u2014 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 \u2014 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 \u2014 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 \u2264 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 \u2014 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";
|
|
1
|
+
export declare const TWIST_GUIDE = "# 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 \u251C\u2500 Has stable URL or ID?\n \u2502 \u2514\u2500 Yes \u2192 Set Thread.source to the canonical URL/ID\n \u2502 Create Thread (Plot handles deduplication automatically)\n \u2502 Use Note.key for different note types:\n \u2502 - \"description\" for main content\n \u2502 - \"metadata\" for status/priority/assignee\n \u2502 - \"comment-{id}\" for individual comments\n \u2502\n \u2514\u2500 No stable identifier OR need multiple Plot threads per external item?\n \u2514\u2500 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 \u251C\u2500 Yes \u2192 Generate UUID with Uuid.Generate()\n \u2502 Create new Thread with that UUID\n \u2502 Store mapping: external_id \u2192 thread_uuid\n \u2502\n \u2514\u2500 No (update/reply/comment) \u2192 Look up mapping by external_id\n \u251C\u2500 Found \u2192 Add Note to existing Thread using stored UUID\n \u2514\u2500 Not found \u2192 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 \u2192 \"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` \u2014 Auto-mention on replies to threads your twist created (e.g., synced issues, emails)\n- `note.defaultMention` \u2014 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 \u2014 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 \u2014 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 \u2014 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 \u2264 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 \u2014 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";
|
|
2
2
|
//# sourceMappingURL=twist-guide.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"twist-guide.d.ts","sourceRoot":"","sources":["../src/twist-guide.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"twist-guide.d.ts","sourceRoot":"","sources":["../src/twist-guide.ts"],"names":[],"mappings":"AAQA,eAAO,MAAM,WAAW,y6wBAAsB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plotday/twister",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.53.0",
|
|
4
4
|
"description": "Plot Twist Creator - Build intelligent extensions that integrate and automate",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.js",
|
|
@@ -69,6 +69,11 @@
|
|
|
69
69
|
"types": "./dist/tools/callbacks.d.ts",
|
|
70
70
|
"default": "./dist/tools/callbacks.js"
|
|
71
71
|
},
|
|
72
|
+
"./tools/files": {
|
|
73
|
+
"@plotday/connector": "./src/tools/files.ts",
|
|
74
|
+
"types": "./dist/tools/files.d.ts",
|
|
75
|
+
"default": "./dist/tools/files.js"
|
|
76
|
+
},
|
|
72
77
|
"./tools/network": {
|
|
73
78
|
"@plotday/connector": "./src/tools/network.ts",
|
|
74
79
|
"types": "./dist/tools/network.d.ts",
|
package/src/connector.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { type Actor, type ActorId, type Link, type NewLinkWithNotes, type Note, type Thread } from "./plot";
|
|
1
|
+
import { type Actor, type ActorId, type Contact, type Link, type NewLinkWithNotes, type Note, type Thread, type Uuid } from "./plot";
|
|
2
2
|
import type { ScheduleContactStatus } from "./schedule";
|
|
3
3
|
import {
|
|
4
4
|
type AuthProvider,
|
|
@@ -11,13 +11,28 @@ import {
|
|
|
11
11
|
import { Twist } from "./twist";
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
|
-
*
|
|
15
|
-
* item via a connector's `onCreateLink` hook.
|
|
14
|
+
* Declares how a connector's platform handles emoji reactions.
|
|
16
15
|
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
*
|
|
16
|
+
* Drives Plot UI behavior (e.g. the picker filters the available
|
|
17
|
+
* reactions on notes whose primary connector declares `fixed`) and
|
|
18
|
+
* outbound dispatch (Plot won't try to push an emoji the platform
|
|
19
|
+
* can't accept).
|
|
20
|
+
*
|
|
21
|
+
* Variants:
|
|
22
|
+
* - `open-unicode`: Platform accepts any Unicode emoji. `customEmoji`
|
|
23
|
+
* indicates whether the platform additionally supports workspace
|
|
24
|
+
* custom emoji (Slack, Google Chat).
|
|
25
|
+
* - `unicode-subset`: Platform accepts Unicode but only a finite set.
|
|
26
|
+
* `subset` lists the allowed emoji (omit for "currently full Unicode
|
|
27
|
+
* per docs, future-proofed for shrinkage").
|
|
28
|
+
* - `fixed`: Platform only accepts a fixed set (e.g. LinkedIn
|
|
29
|
+
* Messaging's 7-reaction set). `allowed` lists every supported emoji.
|
|
20
30
|
*/
|
|
31
|
+
export type ReactionCapabilities =
|
|
32
|
+
| { mode: "open-unicode"; customEmoji?: "workspace" | "none" }
|
|
33
|
+
| { mode: "unicode-subset"; subset?: readonly string[] }
|
|
34
|
+
| { mode: "fixed"; allowed: readonly string[] };
|
|
35
|
+
|
|
21
36
|
/**
|
|
22
37
|
* Result returned from {@link Connector.onNoteCreated} and
|
|
23
38
|
* {@link Connector.onNoteUpdated} to report what the external system now
|
|
@@ -69,6 +84,32 @@ export type NoteWriteBackResult = {
|
|
|
69
84
|
externalContent?: string;
|
|
70
85
|
};
|
|
71
86
|
|
|
87
|
+
/**
|
|
88
|
+
* A Plot contact pre-resolved to its platform account ID, ready for use
|
|
89
|
+
* in a messaging dispatch.
|
|
90
|
+
*
|
|
91
|
+
* Populated by the runtime for link types with `compose.targets: "contacts"` before
|
|
92
|
+
* `onCreateLink` is called. The connector should use `externalAccountId`
|
|
93
|
+
* directly to address the recipient on the platform (e.g. Slack user ID,
|
|
94
|
+
* LinkedIn URN, Gmail address) without performing its own contact lookup.
|
|
95
|
+
*/
|
|
96
|
+
export type ResolvedRecipient = {
|
|
97
|
+
/** Plot contact UUID */
|
|
98
|
+
id: Uuid;
|
|
99
|
+
/** Display name, or null if not set */
|
|
100
|
+
name: string | null;
|
|
101
|
+
/** Platform-specific account identifier pre-resolved at dispatch time (e.g. Slack `U…`, LinkedIn URN, Gmail email address) */
|
|
102
|
+
externalAccountId: string;
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Fields captured in Plot when a user initiates creation of a new external
|
|
107
|
+
* item via a connector's `onCreateLink` hook.
|
|
108
|
+
*
|
|
109
|
+
* Thread-agnostic on purpose — connectors do not receive the Plot thread.
|
|
110
|
+
* The platform attaches the returned `NewLinkWithNotes` to the originating
|
|
111
|
+
* thread once `onCreateLink` resolves.
|
|
112
|
+
*/
|
|
72
113
|
export type CreateLinkDraft = {
|
|
73
114
|
/** The channel (account + resource) the new item belongs to. */
|
|
74
115
|
channelId: string;
|
|
@@ -85,8 +126,32 @@ export type CreateLinkDraft = {
|
|
|
85
126
|
* creating user. Use these as recipients (email, chat DM members, etc.)
|
|
86
127
|
* when the external item is a message or invite. An empty list means
|
|
87
128
|
* the user did not add anyone to the thread.
|
|
129
|
+
*
|
|
130
|
+
* For link types with `compose.targets: "contacts"`, prefer `recipients` over
|
|
131
|
+
* re-resolving contacts yourself: the runtime pre-resolves each contact
|
|
132
|
+
* to its platform account ID (`externalAccountId`) and populates
|
|
133
|
+
* `recipients` before `onCreateLink` is called.
|
|
88
134
|
*/
|
|
89
135
|
contacts: Actor[];
|
|
136
|
+
/**
|
|
137
|
+
* Pre-resolved recipients for link types whose `compose.targets` is
|
|
138
|
+
* `"contacts"` or `"addresses"`.
|
|
139
|
+
*
|
|
140
|
+
* Only populated for those link types; otherwise undefined. Each entry contains the Plot
|
|
141
|
+
* contact UUID and the platform-specific account ID
|
|
142
|
+
* (`externalAccountId`) the connector should use to address the
|
|
143
|
+
* recipient without performing its own lookup. For `"addresses"` link
|
|
144
|
+
* types, contacts without a connection-scoped row fall back to
|
|
145
|
+
* `contact.email`.
|
|
146
|
+
*/
|
|
147
|
+
recipients?: ResolvedRecipient[];
|
|
148
|
+
/**
|
|
149
|
+
* Free-form addresses the user typed into the picker (no Plot contact
|
|
150
|
+
* row). Only populated for link types with `compose.targets: "addresses"`; otherwise
|
|
151
|
+
* undefined. Connectors should append these alongside `recipients`
|
|
152
|
+
* when constructing the recipient list (e.g. `To:` header for Gmail).
|
|
153
|
+
*/
|
|
154
|
+
inviteEmails?: string[];
|
|
90
155
|
};
|
|
91
156
|
|
|
92
157
|
/**
|
|
@@ -189,6 +254,17 @@ export abstract class Connector<TSelf> extends Twist<TSelf> {
|
|
|
189
254
|
*/
|
|
190
255
|
readonly linkTypes?: LinkTypeConfig[];
|
|
191
256
|
|
|
257
|
+
/**
|
|
258
|
+
* Declares how this connector's platform handles emoji reactions.
|
|
259
|
+
* Used to filter the reaction picker for notes whose primary connector
|
|
260
|
+
* is this one, and to guard outbound dispatch from sending emoji the
|
|
261
|
+
* platform can't accept.
|
|
262
|
+
*
|
|
263
|
+
* Leave undefined for connectors whose platform has no concept of
|
|
264
|
+
* reactions (calendar, file storage, issue trackers without reactions).
|
|
265
|
+
*/
|
|
266
|
+
readonly reactionCapabilities?: ReactionCapabilities;
|
|
267
|
+
|
|
192
268
|
/**
|
|
193
269
|
* When true, this connector is mentioned by default on replies to threads it created.
|
|
194
270
|
* When false (default), this connector cannot be mentioned at all.
|
|
@@ -322,10 +398,11 @@ export abstract class Connector<TSelf> extends Twist<TSelf> {
|
|
|
322
398
|
* Called when a user creates a thread in Plot that should create a new
|
|
323
399
|
* item in this connector's external system.
|
|
324
400
|
*
|
|
325
|
-
* A connector opts in to Plot-initiated creation by declaring a
|
|
326
|
-
*
|
|
327
|
-
* user picks "Create new <type>" from the
|
|
328
|
-
* is synced, the runtime calls this method
|
|
401
|
+
* A connector opts in to Plot-initiated creation by declaring a
|
|
402
|
+
* `compose` block on the relevant `LinkTypeConfig` (see
|
|
403
|
+
* {@link ComposeConfig}). When a user picks "Create new <type>" from the
|
|
404
|
+
* Add link modal and the thread is synced, the runtime calls this method
|
|
405
|
+
* with the draft fields.
|
|
329
406
|
*
|
|
330
407
|
* Implementations should create the item in the external service and
|
|
331
408
|
* return a `NewLinkWithNotes` describing the created item. The platform
|
|
@@ -364,6 +441,30 @@ export abstract class Connector<TSelf> extends Twist<TSelf> {
|
|
|
364
441
|
return Promise.resolve();
|
|
365
442
|
}
|
|
366
443
|
|
|
444
|
+
/**
|
|
445
|
+
* Resolve a `fileRef` action's bytes for download. Called when a user opens
|
|
446
|
+
* an attachment in Plot. Return either a redirect URL (preferred for sources
|
|
447
|
+
* that issue signed URLs, like Linear S3 or Slack permalink_public) or a
|
|
448
|
+
* streamed body (required when bytes are only reachable through an
|
|
449
|
+
* authenticated API call, like Gmail attachments.get).
|
|
450
|
+
*
|
|
451
|
+
* @param ref Opaque value the connector previously emitted on a fileRef action.
|
|
452
|
+
* @returns Either `{ redirectUrl }` or `{ body, mimeType, fileName? }`.
|
|
453
|
+
* @throws If the source is unavailable, the connection is broken, or `ref` is invalid.
|
|
454
|
+
*
|
|
455
|
+
* If not overridden, fileRef actions on this connector's notes will return 410 Gone.
|
|
456
|
+
*/
|
|
457
|
+
async downloadAttachment(
|
|
458
|
+
ref: string,
|
|
459
|
+
): Promise<
|
|
460
|
+
| { redirectUrl: string }
|
|
461
|
+
| { body: ReadableStream | Uint8Array; mimeType: string; fileName?: string }
|
|
462
|
+
> {
|
|
463
|
+
throw new Error(
|
|
464
|
+
`downloadAttachment not implemented for ${this.constructor.name} (ref=${ref})`,
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
367
468
|
/**
|
|
368
469
|
* Called when a note on a thread owned by this connector is updated.
|
|
369
470
|
* Override to write back changes to the external service
|
|
@@ -398,6 +499,35 @@ export abstract class Connector<TSelf> extends Twist<TSelf> {
|
|
|
398
499
|
return Promise.resolve();
|
|
399
500
|
}
|
|
400
501
|
|
|
502
|
+
/**
|
|
503
|
+
* Called when a user adds, removes, or changes the role of contacts on a
|
|
504
|
+
* thread owned by this connector. Override on connectors whose source
|
|
505
|
+
* supports mid-thread recipient changes (Gmail, IMAP, etc.). Connectors
|
|
506
|
+
* that can't change recipients per-message (Slack, Linear) leave this as
|
|
507
|
+
* the default no-op and should also declare
|
|
508
|
+
* `LinkTypeConfig.supportsContactChanges: false`.
|
|
509
|
+
*
|
|
510
|
+
* The dispatch fires after Plot has persisted the change. Connectors are
|
|
511
|
+
* expected to reflect it on the next outbound note (e.g. building To/Cc/Bcc
|
|
512
|
+
* headers from the current `thread.contacts` × `thread.contactMeta`) — this
|
|
513
|
+
* callback is not the right place to send a standalone notification.
|
|
514
|
+
*
|
|
515
|
+
* @param thread - The thread whose contacts changed
|
|
516
|
+
* @param changes - The added/removed contacts and any role transitions on existing contacts
|
|
517
|
+
*/
|
|
518
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
519
|
+
onContactsChanged(
|
|
520
|
+
thread: Thread,
|
|
521
|
+
changes: {
|
|
522
|
+
added: Array<{ contact: Contact; role: string }>;
|
|
523
|
+
removed: Array<{ contact: Contact; role: string }>;
|
|
524
|
+
changed: Array<{ contact: Contact; from: string; to: string }>;
|
|
525
|
+
},
|
|
526
|
+
): Promise<void> {
|
|
527
|
+
return Promise.resolve();
|
|
528
|
+
}
|
|
529
|
+
/* eslint-enable @typescript-eslint/no-unused-vars */
|
|
530
|
+
|
|
401
531
|
/**
|
|
402
532
|
* Called when a user marks or unmarks a thread as todo.
|
|
403
533
|
* Override to sync todo status to the external service
|
|
@@ -429,6 +559,33 @@ export abstract class Connector<TSelf> extends Twist<TSelf> {
|
|
|
429
559
|
return Promise.resolve();
|
|
430
560
|
}
|
|
431
561
|
|
|
562
|
+
/**
|
|
563
|
+
* Called when a user adds or removes a single emoji reaction on a note
|
|
564
|
+
* (one event per `(note, actor, emoji)` state transition).
|
|
565
|
+
*
|
|
566
|
+
* Dispatch is routed to the reacting user's own connector instance via
|
|
567
|
+
* `twist_instance_for_actor` on `note_reaction.actor_id`, so this method
|
|
568
|
+
* already runs under the reactor's auth. Fetch the API client with the
|
|
569
|
+
* connector's normal token-fetch path (`this.tools.integrations.get(...)`)
|
|
570
|
+
* and the external write — e.g. Slack `reactions.add` — will be attributed
|
|
571
|
+
* to the correct user. No `actAs` step required.
|
|
572
|
+
*
|
|
573
|
+
* If the reacting user has no connection of this type, no dispatch fires
|
|
574
|
+
* for that reaction (it stays in Plot only).
|
|
575
|
+
*
|
|
576
|
+
* Override to sync per-actor reactions back to the external system.
|
|
577
|
+
*
|
|
578
|
+
* @param note - The note that was reacted on (partial; `id`, `key`, `content` populated)
|
|
579
|
+
* @param thread - The thread the note belongs to (partial; `id`, `title`, `archived`, `meta` populated)
|
|
580
|
+
* @param actor - The contact who added/removed the reaction
|
|
581
|
+
* @param emoji - The emoji (Unicode grapheme or `provider:workspace/name` custom-emoji ref)
|
|
582
|
+
* @param added - `true` if the reaction is now present, `false` if it was removed
|
|
583
|
+
*/
|
|
584
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
585
|
+
onNoteReactionChanged(note: Note, thread: Thread, actor: Actor, emoji: string, added: boolean): Promise<void> {
|
|
586
|
+
return Promise.resolve();
|
|
587
|
+
}
|
|
588
|
+
|
|
432
589
|
// ---- Activation ----
|
|
433
590
|
|
|
434
591
|
/**
|