patchwork-os 0.2.0-alpha.3 → 0.2.0-alpha.31

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/README.bridge.md +6 -0
  2. package/README.md +40 -15
  3. package/deploy/bootstrap-vps.sh +184 -0
  4. package/deploy/deploy-dashboard.sh +174 -0
  5. package/deploy/deploy-landing.sh +79 -0
  6. package/dist/activationMetrics.d.ts +67 -0
  7. package/dist/activationMetrics.js +255 -0
  8. package/dist/activationMetrics.js.map +1 -0
  9. package/dist/approvalHttp.d.ts +24 -2
  10. package/dist/approvalHttp.js +150 -10
  11. package/dist/approvalHttp.js.map +1 -1
  12. package/dist/approvalQueue.d.ts +16 -1
  13. package/dist/approvalQueue.js +44 -3
  14. package/dist/approvalQueue.js.map +1 -1
  15. package/dist/automation.d.ts +20 -0
  16. package/dist/automation.js +54 -1
  17. package/dist/automation.js.map +1 -1
  18. package/dist/bridge.d.ts +2 -0
  19. package/dist/bridge.js +55 -130
  20. package/dist/bridge.js.map +1 -1
  21. package/dist/bridgeToken.js +57 -19
  22. package/dist/bridgeToken.js.map +1 -1
  23. package/dist/ccPermissions.js +6 -4
  24. package/dist/ccPermissions.js.map +1 -1
  25. package/dist/claudeOrchestrator.d.ts +1 -1
  26. package/dist/claudeOrchestrator.js +14 -8
  27. package/dist/claudeOrchestrator.js.map +1 -1
  28. package/dist/commands/launchd.d.ts +2 -0
  29. package/dist/commands/launchd.js +94 -0
  30. package/dist/commands/launchd.js.map +1 -0
  31. package/dist/commands/recipe.d.ts +258 -0
  32. package/dist/commands/recipe.js +1130 -0
  33. package/dist/commands/recipe.js.map +1 -0
  34. package/dist/commands/recipeInstall.d.ts +72 -0
  35. package/dist/commands/recipeInstall.js +339 -0
  36. package/dist/commands/recipeInstall.js.map +1 -0
  37. package/dist/config.d.ts +14 -1
  38. package/dist/config.js +99 -8
  39. package/dist/config.js.map +1 -1
  40. package/dist/connectors/baseConnector.d.ts +117 -0
  41. package/dist/connectors/baseConnector.js +213 -0
  42. package/dist/connectors/baseConnector.js.map +1 -0
  43. package/dist/connectors/confluence.d.ts +111 -0
  44. package/dist/connectors/confluence.js +406 -0
  45. package/dist/connectors/confluence.js.map +1 -0
  46. package/dist/connectors/datadog.d.ts +116 -0
  47. package/dist/connectors/datadog.js +385 -0
  48. package/dist/connectors/datadog.js.map +1 -0
  49. package/dist/connectors/fixtureLibrary.d.ts +21 -0
  50. package/dist/connectors/fixtureLibrary.js +70 -0
  51. package/dist/connectors/fixtureLibrary.js.map +1 -0
  52. package/dist/connectors/fixtureRecorder.d.ts +1 -0
  53. package/dist/connectors/fixtureRecorder.js +35 -0
  54. package/dist/connectors/fixtureRecorder.js.map +1 -0
  55. package/dist/connectors/github.d.ts +58 -8
  56. package/dist/connectors/github.js +312 -84
  57. package/dist/connectors/github.js.map +1 -1
  58. package/dist/connectors/gmail.d.ts +4 -1
  59. package/dist/connectors/gmail.js +79 -16
  60. package/dist/connectors/gmail.js.map +1 -1
  61. package/dist/connectors/googleCalendar.d.ts +60 -0
  62. package/dist/connectors/googleCalendar.js +345 -0
  63. package/dist/connectors/googleCalendar.js.map +1 -0
  64. package/dist/connectors/hubspot.d.ts +112 -0
  65. package/dist/connectors/hubspot.js +408 -0
  66. package/dist/connectors/hubspot.js.map +1 -0
  67. package/dist/connectors/intercom.d.ts +102 -0
  68. package/dist/connectors/intercom.js +402 -0
  69. package/dist/connectors/intercom.js.map +1 -0
  70. package/dist/connectors/jira.d.ts +98 -0
  71. package/dist/connectors/jira.js +379 -0
  72. package/dist/connectors/jira.js.map +1 -0
  73. package/dist/connectors/linear.d.ts +69 -19
  74. package/dist/connectors/linear.js +170 -129
  75. package/dist/connectors/linear.js.map +1 -1
  76. package/dist/connectors/mcpClient.d.ts +56 -0
  77. package/dist/connectors/mcpClient.js +189 -0
  78. package/dist/connectors/mcpClient.js.map +1 -0
  79. package/dist/connectors/mcpOAuth.d.ts +84 -0
  80. package/dist/connectors/mcpOAuth.js +389 -0
  81. package/dist/connectors/mcpOAuth.js.map +1 -0
  82. package/dist/connectors/mockConnector.d.ts +28 -0
  83. package/dist/connectors/mockConnector.js +81 -0
  84. package/dist/connectors/mockConnector.js.map +1 -0
  85. package/dist/connectors/notion.d.ts +143 -0
  86. package/dist/connectors/notion.js +424 -0
  87. package/dist/connectors/notion.js.map +1 -0
  88. package/dist/connectors/sentry.d.ts +17 -21
  89. package/dist/connectors/sentry.js +115 -131
  90. package/dist/connectors/sentry.js.map +1 -1
  91. package/dist/connectors/slack.d.ts +50 -0
  92. package/dist/connectors/slack.js +324 -0
  93. package/dist/connectors/slack.js.map +1 -0
  94. package/dist/connectors/stripe.d.ts +116 -0
  95. package/dist/connectors/stripe.js +379 -0
  96. package/dist/connectors/stripe.js.map +1 -0
  97. package/dist/connectors/tokenStorage.d.ts +35 -0
  98. package/dist/connectors/tokenStorage.js +459 -0
  99. package/dist/connectors/tokenStorage.js.map +1 -0
  100. package/dist/connectors/zendesk.d.ts +104 -0
  101. package/dist/connectors/zendesk.js +424 -0
  102. package/dist/connectors/zendesk.js.map +1 -0
  103. package/dist/drivers/gemini/index.d.ts +5 -1
  104. package/dist/drivers/gemini/index.js +39 -5
  105. package/dist/drivers/gemini/index.js.map +1 -1
  106. package/dist/drivers/index.d.ts +5 -0
  107. package/dist/drivers/index.js +1 -1
  108. package/dist/drivers/index.js.map +1 -1
  109. package/dist/featureFlags.d.ts +73 -0
  110. package/dist/featureFlags.js +203 -0
  111. package/dist/featureFlags.js.map +1 -0
  112. package/dist/fp/automationInterpreter.js +1 -0
  113. package/dist/fp/automationInterpreter.js.map +1 -1
  114. package/dist/fp/automationProgram.d.ts +1 -1
  115. package/dist/fp/automationProgram.js.map +1 -1
  116. package/dist/fp/policyParser.js +17 -0
  117. package/dist/fp/policyParser.js.map +1 -1
  118. package/dist/index.js +621 -61
  119. package/dist/index.js.map +1 -1
  120. package/dist/installGuard.d.ts +25 -0
  121. package/dist/installGuard.js +48 -0
  122. package/dist/installGuard.js.map +1 -0
  123. package/dist/oauth.d.ts +4 -1
  124. package/dist/oauth.js +50 -14
  125. package/dist/oauth.js.map +1 -1
  126. package/dist/patchworkConfig.d.ts +9 -0
  127. package/dist/patchworkConfig.js.map +1 -1
  128. package/dist/recipeOrchestration.d.ts +53 -0
  129. package/dist/recipeOrchestration.js +272 -0
  130. package/dist/recipeOrchestration.js.map +1 -0
  131. package/dist/recipes/RecipeOrchestrator.d.ts +40 -0
  132. package/dist/recipes/RecipeOrchestrator.js +51 -0
  133. package/dist/recipes/RecipeOrchestrator.js.map +1 -0
  134. package/dist/recipes/agentExecutor.d.ts +28 -0
  135. package/dist/recipes/agentExecutor.js +42 -0
  136. package/dist/recipes/agentExecutor.js.map +1 -0
  137. package/dist/recipes/chainedRunner.d.ts +140 -0
  138. package/dist/recipes/chainedRunner.js +539 -0
  139. package/dist/recipes/chainedRunner.js.map +1 -0
  140. package/dist/recipes/dependencyGraph.d.ts +39 -0
  141. package/dist/recipes/dependencyGraph.js +199 -0
  142. package/dist/recipes/dependencyGraph.js.map +1 -0
  143. package/dist/recipes/legacyRecipeCompat.d.ts +2 -0
  144. package/dist/recipes/legacyRecipeCompat.js +112 -0
  145. package/dist/recipes/legacyRecipeCompat.js.map +1 -0
  146. package/dist/recipes/manifest.d.ts +47 -0
  147. package/dist/recipes/manifest.js +141 -0
  148. package/dist/recipes/manifest.js.map +1 -0
  149. package/dist/recipes/nestedRecipeStep.d.ts +58 -0
  150. package/dist/recipes/nestedRecipeStep.js +95 -0
  151. package/dist/recipes/nestedRecipeStep.js.map +1 -0
  152. package/dist/recipes/outputRegistry.d.ts +28 -0
  153. package/dist/recipes/outputRegistry.js +52 -0
  154. package/dist/recipes/outputRegistry.js.map +1 -0
  155. package/dist/recipes/scheduler.d.ts +23 -7
  156. package/dist/recipes/scheduler.js +131 -41
  157. package/dist/recipes/scheduler.js.map +1 -1
  158. package/dist/recipes/schema.d.ts +17 -2
  159. package/dist/recipes/schemaGenerator.d.ts +28 -0
  160. package/dist/recipes/schemaGenerator.js +565 -0
  161. package/dist/recipes/schemaGenerator.js.map +1 -0
  162. package/dist/recipes/templateEngine.d.ts +62 -0
  163. package/dist/recipes/templateEngine.js +182 -0
  164. package/dist/recipes/templateEngine.js.map +1 -0
  165. package/dist/recipes/toolRegistry.d.ts +181 -0
  166. package/dist/recipes/toolRegistry.js +300 -0
  167. package/dist/recipes/toolRegistry.js.map +1 -0
  168. package/dist/recipes/tools/calendar.d.ts +6 -0
  169. package/dist/recipes/tools/calendar.js +61 -0
  170. package/dist/recipes/tools/calendar.js.map +1 -0
  171. package/dist/recipes/tools/confluence.d.ts +6 -0
  172. package/dist/recipes/tools/confluence.js +254 -0
  173. package/dist/recipes/tools/confluence.js.map +1 -0
  174. package/dist/recipes/tools/datadog.d.ts +6 -0
  175. package/dist/recipes/tools/datadog.js +239 -0
  176. package/dist/recipes/tools/datadog.js.map +1 -0
  177. package/dist/recipes/tools/diagnostics.d.ts +6 -0
  178. package/dist/recipes/tools/diagnostics.js +36 -0
  179. package/dist/recipes/tools/diagnostics.js.map +1 -0
  180. package/dist/recipes/tools/file.d.ts +6 -0
  181. package/dist/recipes/tools/file.js +170 -0
  182. package/dist/recipes/tools/file.js.map +1 -0
  183. package/dist/recipes/tools/git.d.ts +6 -0
  184. package/dist/recipes/tools/git.js +63 -0
  185. package/dist/recipes/tools/git.js.map +1 -0
  186. package/dist/recipes/tools/github.d.ts +6 -0
  187. package/dist/recipes/tools/github.js +91 -0
  188. package/dist/recipes/tools/github.js.map +1 -0
  189. package/dist/recipes/tools/gmail.d.ts +6 -0
  190. package/dist/recipes/tools/gmail.js +210 -0
  191. package/dist/recipes/tools/gmail.js.map +1 -0
  192. package/dist/recipes/tools/hubspot.d.ts +6 -0
  193. package/dist/recipes/tools/hubspot.js +232 -0
  194. package/dist/recipes/tools/hubspot.js.map +1 -0
  195. package/dist/recipes/tools/index.d.ts +22 -0
  196. package/dist/recipes/tools/index.js +25 -0
  197. package/dist/recipes/tools/index.js.map +1 -0
  198. package/dist/recipes/tools/intercom.d.ts +6 -0
  199. package/dist/recipes/tools/intercom.js +226 -0
  200. package/dist/recipes/tools/intercom.js.map +1 -0
  201. package/dist/recipes/tools/linear.d.ts +6 -0
  202. package/dist/recipes/tools/linear.js +83 -0
  203. package/dist/recipes/tools/linear.js.map +1 -0
  204. package/dist/recipes/tools/notion.d.ts +6 -0
  205. package/dist/recipes/tools/notion.js +278 -0
  206. package/dist/recipes/tools/notion.js.map +1 -0
  207. package/dist/recipes/tools/slack.d.ts +6 -0
  208. package/dist/recipes/tools/slack.js +72 -0
  209. package/dist/recipes/tools/slack.js.map +1 -0
  210. package/dist/recipes/tools/stripe.d.ts +6 -0
  211. package/dist/recipes/tools/stripe.js +265 -0
  212. package/dist/recipes/tools/stripe.js.map +1 -0
  213. package/dist/recipes/tools/zendesk.d.ts +6 -0
  214. package/dist/recipes/tools/zendesk.js +245 -0
  215. package/dist/recipes/tools/zendesk.js.map +1 -0
  216. package/dist/recipes/validation.d.ts +13 -0
  217. package/dist/recipes/validation.js +433 -0
  218. package/dist/recipes/validation.js.map +1 -0
  219. package/dist/recipes/yamlRunner.d.ts +87 -0
  220. package/dist/recipes/yamlRunner.js +693 -409
  221. package/dist/recipes/yamlRunner.js.map +1 -1
  222. package/dist/recipesHttp.d.ts +34 -6
  223. package/dist/recipesHttp.js +285 -15
  224. package/dist/recipesHttp.js.map +1 -1
  225. package/dist/riskTier.js +1 -0
  226. package/dist/riskTier.js.map +1 -1
  227. package/dist/runLog.d.ts +23 -0
  228. package/dist/runLog.js +56 -1
  229. package/dist/runLog.js.map +1 -1
  230. package/dist/schemas/dry-run-plan.v1.json +139 -0
  231. package/dist/schemas/recipe.v1.json +684 -0
  232. package/dist/server.d.ts +32 -1
  233. package/dist/server.js +980 -97
  234. package/dist/server.js.map +1 -1
  235. package/dist/streamableHttp.js +2 -0
  236. package/dist/streamableHttp.js.map +1 -1
  237. package/dist/tools/addLinearComment.d.ts +55 -0
  238. package/dist/tools/addLinearComment.js +72 -0
  239. package/dist/tools/addLinearComment.js.map +1 -0
  240. package/dist/tools/bridgeDoctor.js +2 -2
  241. package/dist/tools/bridgeDoctor.js.map +1 -1
  242. package/dist/tools/createLinearIssue.d.ts +84 -0
  243. package/dist/tools/createLinearIssue.js +146 -0
  244. package/dist/tools/createLinearIssue.js.map +1 -0
  245. package/dist/tools/fetchCalendarEvents.d.ts +94 -0
  246. package/dist/tools/fetchCalendarEvents.js +97 -0
  247. package/dist/tools/fetchCalendarEvents.js.map +1 -0
  248. package/dist/tools/fetchGithubIssue.d.ts +80 -0
  249. package/dist/tools/fetchGithubIssue.js +84 -0
  250. package/dist/tools/fetchGithubIssue.js.map +1 -0
  251. package/dist/tools/fetchGithubPR.d.ts +89 -0
  252. package/dist/tools/fetchGithubPR.js +96 -0
  253. package/dist/tools/fetchGithubPR.js.map +1 -0
  254. package/dist/tools/fetchSlackProfile.d.ts +43 -0
  255. package/dist/tools/fetchSlackProfile.js +46 -0
  256. package/dist/tools/fetchSlackProfile.js.map +1 -0
  257. package/dist/tools/getConnectorStatus.d.ts +58 -0
  258. package/dist/tools/getConnectorStatus.js +56 -0
  259. package/dist/tools/getConnectorStatus.js.map +1 -0
  260. package/dist/tools/github/actions.js +4 -2
  261. package/dist/tools/github/actions.js.map +1 -1
  262. package/dist/tools/github/composite.d.ts +339 -0
  263. package/dist/tools/github/composite.js +343 -0
  264. package/dist/tools/github/composite.js.map +1 -0
  265. package/dist/tools/github/index.d.ts +2 -1
  266. package/dist/tools/github/index.js +2 -1
  267. package/dist/tools/github/index.js.map +1 -1
  268. package/dist/tools/github/issues.js +8 -4
  269. package/dist/tools/github/issues.js.map +1 -1
  270. package/dist/tools/github/pr.d.ts +122 -0
  271. package/dist/tools/github/pr.js +195 -5
  272. package/dist/tools/github/pr.js.map +1 -1
  273. package/dist/tools/index.js +32 -1
  274. package/dist/tools/index.js.map +1 -1
  275. package/dist/tools/searchTools.js +1 -1
  276. package/dist/tools/searchTools.js.map +1 -1
  277. package/dist/tools/slackListChannels.d.ts +65 -0
  278. package/dist/tools/slackListChannels.js +70 -0
  279. package/dist/tools/slackListChannels.js.map +1 -0
  280. package/dist/tools/slackPostMessage.d.ts +57 -0
  281. package/dist/tools/slackPostMessage.js +77 -0
  282. package/dist/tools/slackPostMessage.js.map +1 -0
  283. package/dist/tools/testTraceToSource.js +2 -2
  284. package/dist/tools/testTraceToSource.js.map +1 -1
  285. package/dist/tools/updateLinearIssue.d.ts +89 -0
  286. package/dist/tools/updateLinearIssue.js +117 -0
  287. package/dist/tools/updateLinearIssue.js.map +1 -0
  288. package/dist/transport.d.ts +7 -1
  289. package/dist/transport.js +85 -11
  290. package/dist/transport.js.map +1 -1
  291. package/package.json +5 -2
  292. package/scripts/start-all.sh +56 -19
  293. package/templates/automation-policies/recipe-authoring.json +25 -0
  294. package/templates/automation-policy.example.json +6 -0
  295. package/templates/co.patchwork-os.bridge.plist +34 -0
  296. package/templates/recipes/ctx-loop-test.yaml +75 -0
  297. package/templates/recipes/lint-on-save.yaml +1 -2
  298. package/templates/recipes/morning-brief-slack.yaml +57 -0
  299. package/templates/recipes/morning-brief.yaml +14 -6
  300. package/templates/recipes/project-health-check.yaml +50 -0
  301. package/templates/recipes/sentry-to-linear.yaml +77 -0
@@ -0,0 +1,143 @@
1
+ /**
2
+ * Notion connector — read/write Notion databases and pages via the Notion API.
3
+ *
4
+ * Auth: API token (internal integration) or OAuth 2.0 (public integration).
5
+ * - Env var: NOTION_TOKEN overrides stored token for CI/headless use.
6
+ * - Stored: getSecretJsonSync("notion") → NotionTokens
7
+ *
8
+ * Tools: queryDatabase, getPage, search, createPage, appendBlock
9
+ *
10
+ * Extends BaseConnector for unified auth, retry, rate-limit, error handling.
11
+ */
12
+ import { type AuthContext, BaseConnector, type ConnectorError, type ConnectorStatus } from "./baseConnector.js";
13
+ export interface NotionTokens {
14
+ accessToken: string;
15
+ workspaceName?: string;
16
+ workspaceId?: string;
17
+ botId?: string;
18
+ connected_at: string;
19
+ }
20
+ export interface NotionUser {
21
+ object: "user";
22
+ id: string;
23
+ name?: string;
24
+ avatar_url?: string;
25
+ type: "person" | "bot";
26
+ }
27
+ export interface NotionRichText {
28
+ type: "text";
29
+ text: {
30
+ content: string;
31
+ link?: {
32
+ url: string;
33
+ } | null;
34
+ };
35
+ plain_text: string;
36
+ href?: string | null;
37
+ }
38
+ export interface NotionPage {
39
+ object: "page";
40
+ id: string;
41
+ created_time: string;
42
+ last_edited_time: string;
43
+ archived: boolean;
44
+ url: string;
45
+ properties: Record<string, unknown>;
46
+ parent: {
47
+ type: string;
48
+ database_id?: string;
49
+ page_id?: string;
50
+ };
51
+ }
52
+ export interface NotionDatabase {
53
+ object: "database";
54
+ id: string;
55
+ title: NotionRichText[];
56
+ description?: NotionRichText[];
57
+ created_time: string;
58
+ last_edited_time: string;
59
+ url: string;
60
+ properties: Record<string, unknown>;
61
+ }
62
+ export interface NotionBlock {
63
+ object: "block";
64
+ id: string;
65
+ type: string;
66
+ created_time: string;
67
+ last_edited_time: string;
68
+ has_children: boolean;
69
+ [key: string]: unknown;
70
+ }
71
+ export interface NotionQueryResult {
72
+ object: "list";
73
+ results: NotionPage[];
74
+ next_cursor: string | null;
75
+ has_more: boolean;
76
+ }
77
+ export interface NotionSearchResult {
78
+ object: "list";
79
+ results: Array<NotionPage | NotionDatabase>;
80
+ next_cursor: string | null;
81
+ has_more: boolean;
82
+ }
83
+ export interface CreatePageParams {
84
+ parentId: string;
85
+ parentType: "database" | "page";
86
+ title: string;
87
+ properties?: Record<string, unknown>;
88
+ content?: string;
89
+ }
90
+ export interface AppendBlockParams {
91
+ pageId: string;
92
+ content: string;
93
+ blockType?: "paragraph" | "bulleted_list_item" | "numbered_list_item" | "heading_1" | "heading_2" | "heading_3" | "quote" | "code";
94
+ }
95
+ export declare function loadTokens(): NotionTokens | null;
96
+ export declare function saveTokens(tokens: NotionTokens): void;
97
+ export declare function clearTokens(): void;
98
+ export declare class NotionConnector extends BaseConnector {
99
+ readonly providerName = "notion";
100
+ protected cachedTokens: NotionTokens | null;
101
+ protected getOAuthConfig(): null;
102
+ authenticate(): Promise<AuthContext>;
103
+ healthCheck(): Promise<{
104
+ ok: boolean;
105
+ error?: ConnectorError;
106
+ }>;
107
+ normalizeError(error: unknown): ConnectorError;
108
+ getStatus(): ConnectorStatus;
109
+ private buildHeaders;
110
+ queryDatabase(databaseId: string, filter?: Record<string, unknown>, sorts?: Array<{
111
+ property: string;
112
+ direction: "ascending" | "descending";
113
+ }>, pageSize?: number): Promise<NotionQueryResult>;
114
+ getPage(pageId: string): Promise<NotionPage>;
115
+ search(query: string, filterType?: "page" | "database", pageSize?: number): Promise<NotionSearchResult>;
116
+ createPage(params: CreatePageParams): Promise<NotionPage>;
117
+ appendBlock(params: AppendBlockParams): Promise<{
118
+ results: NotionBlock[];
119
+ }>;
120
+ }
121
+ export declare function getNotionConnector(): NotionConnector;
122
+ export declare function resetNotionConnector(): void;
123
+ export { loadTokens as isConnected };
124
+ export interface ConnectorHandlerResult {
125
+ status: number;
126
+ body: string;
127
+ contentType?: string;
128
+ }
129
+ /**
130
+ * POST /connections/notion/connect { token: "secret_..." }
131
+ * Stores the integration token and verifies it by calling /users/me.
132
+ */
133
+ export declare function handleNotionConnect(body: string): Promise<ConnectorHandlerResult>;
134
+ /**
135
+ * POST /connections/notion/test
136
+ * Verifies stored token is still valid.
137
+ */
138
+ export declare function handleNotionTest(): Promise<ConnectorHandlerResult>;
139
+ /**
140
+ * DELETE /connections/notion
141
+ * Removes stored token.
142
+ */
143
+ export declare function handleNotionDisconnect(): ConnectorHandlerResult;
@@ -0,0 +1,424 @@
1
+ /**
2
+ * Notion connector — read/write Notion databases and pages via the Notion API.
3
+ *
4
+ * Auth: API token (internal integration) or OAuth 2.0 (public integration).
5
+ * - Env var: NOTION_TOKEN overrides stored token for CI/headless use.
6
+ * - Stored: getSecretJsonSync("notion") → NotionTokens
7
+ *
8
+ * Tools: queryDatabase, getPage, search, createPage, appendBlock
9
+ *
10
+ * Extends BaseConnector for unified auth, retry, rate-limit, error handling.
11
+ */
12
+ import { unlinkSync } from "node:fs";
13
+ import { homedir } from "node:os";
14
+ import path from "node:path";
15
+ import { BaseConnector, } from "./baseConnector.js";
16
+ import { getSecretJsonSync, storeSecretJsonSync } from "./tokenStorage.js";
17
+ const NOTION_API = "https://api.notion.com/v1";
18
+ const NOTION_VERSION = "2022-06-28";
19
+ // ------------------------------------------------------------------ token helpers
20
+ export function loadTokens() {
21
+ const envToken = process.env.NOTION_TOKEN;
22
+ if (envToken) {
23
+ return {
24
+ accessToken: envToken,
25
+ connected_at: new Date().toISOString(),
26
+ };
27
+ }
28
+ return getSecretJsonSync("notion");
29
+ }
30
+ export function saveTokens(tokens) {
31
+ storeSecretJsonSync("notion", tokens);
32
+ }
33
+ export function clearTokens() {
34
+ try {
35
+ const p = path.join(homedir(), ".patchwork", "tokens", "notion.json");
36
+ unlinkSync(p);
37
+ }
38
+ catch {
39
+ /* already gone */
40
+ }
41
+ }
42
+ // ------------------------------------------------------------------ connector
43
+ export class NotionConnector extends BaseConnector {
44
+ providerName = "notion";
45
+ // Cached after authenticate(); re-read in getStatus() to stay fresh
46
+ cachedTokens = null;
47
+ getOAuthConfig() {
48
+ return null;
49
+ }
50
+ async authenticate() {
51
+ const tokens = loadTokens();
52
+ if (!tokens) {
53
+ throw new Error("Notion not connected. Run: patchwork connect notion or set NOTION_TOKEN");
54
+ }
55
+ this.cachedTokens = tokens;
56
+ return { token: tokens.accessToken };
57
+ }
58
+ async healthCheck() {
59
+ try {
60
+ const result = await this.apiCall(async (token) => {
61
+ const res = await fetch(`${NOTION_API}/users/me`, {
62
+ headers: this.buildHeaders(token),
63
+ });
64
+ if (!res.ok)
65
+ throw new Error(`HTTP ${res.status}`);
66
+ return res.json();
67
+ });
68
+ if ("error" in result)
69
+ return { ok: false, error: result.error };
70
+ return { ok: true };
71
+ }
72
+ catch (err) {
73
+ return { ok: false, error: this.normalizeError(err) };
74
+ }
75
+ }
76
+ normalizeError(error) {
77
+ if (error instanceof Response ||
78
+ (error && typeof error === "object" && "status" in error)) {
79
+ const status = error.status;
80
+ if (status === 401)
81
+ return {
82
+ code: "auth_expired",
83
+ message: "Notion token expired or invalid",
84
+ retryable: false,
85
+ suggestedAction: "Reconnect: patchwork connect notion",
86
+ };
87
+ if (status === 403)
88
+ return {
89
+ code: "permission_denied",
90
+ message: "Notion integration lacks permission for this resource",
91
+ retryable: false,
92
+ suggestedAction: "Share the page/database with your integration",
93
+ };
94
+ if (status === 404)
95
+ return {
96
+ code: "not_found",
97
+ message: "Notion page or database not found",
98
+ retryable: false,
99
+ };
100
+ if (status === 429)
101
+ return {
102
+ code: "rate_limited",
103
+ message: "Notion API rate limit exceeded",
104
+ retryable: true,
105
+ suggestedAction: "Wait and retry",
106
+ };
107
+ return {
108
+ code: "provider_error",
109
+ message: `Notion API error: HTTP ${status}`,
110
+ retryable: status >= 500,
111
+ };
112
+ }
113
+ if (error instanceof Error) {
114
+ if (error.message.includes("ENOTFOUND") ||
115
+ error.message.includes("ECONNREFUSED")) {
116
+ return {
117
+ code: "network_error",
118
+ message: `Cannot reach Notion API: ${error.message}`,
119
+ retryable: true,
120
+ };
121
+ }
122
+ }
123
+ return {
124
+ code: "provider_error",
125
+ message: error instanceof Error ? error.message : String(error),
126
+ retryable: false,
127
+ };
128
+ }
129
+ getStatus() {
130
+ const tokens = loadTokens();
131
+ return {
132
+ id: "notion",
133
+ status: tokens ? "connected" : "disconnected",
134
+ lastSync: tokens?.connected_at,
135
+ workspace: tokens?.workspaceName,
136
+ };
137
+ }
138
+ buildHeaders(token) {
139
+ return {
140
+ Authorization: `Bearer ${token}`,
141
+ "Content-Type": "application/json",
142
+ "Notion-Version": NOTION_VERSION,
143
+ };
144
+ }
145
+ // ---------------------------------------------------------------- read ops
146
+ async queryDatabase(databaseId, filter, sorts, pageSize = 20) {
147
+ const result = await this.apiCall(async (token) => {
148
+ const body = {
149
+ page_size: Math.min(pageSize, 100),
150
+ };
151
+ if (filter)
152
+ body.filter = filter;
153
+ if (sorts)
154
+ body.sorts = sorts;
155
+ const res = await fetch(`${NOTION_API}/databases/${normalizeId(databaseId)}/query`, {
156
+ method: "POST",
157
+ headers: this.buildHeaders(token),
158
+ body: JSON.stringify(body),
159
+ });
160
+ if (!res.ok) {
161
+ const err = (await res.json().catch(() => ({})));
162
+ throw Object.assign(new Error(err.message ?? `HTTP ${res.status}`), {
163
+ status: res.status,
164
+ });
165
+ }
166
+ return res.json();
167
+ });
168
+ if ("error" in result)
169
+ throw new Error(result.error.message);
170
+ return result.data;
171
+ }
172
+ async getPage(pageId) {
173
+ const result = await this.apiCall(async (token) => {
174
+ const res = await fetch(`${NOTION_API}/pages/${normalizeId(pageId)}`, {
175
+ headers: this.buildHeaders(token),
176
+ });
177
+ if (!res.ok) {
178
+ const err = (await res.json().catch(() => ({})));
179
+ throw Object.assign(new Error(err.message ?? `HTTP ${res.status}`), {
180
+ status: res.status,
181
+ });
182
+ }
183
+ return res.json();
184
+ });
185
+ if ("error" in result)
186
+ throw new Error(result.error.message);
187
+ return result.data;
188
+ }
189
+ async search(query, filterType, pageSize = 10) {
190
+ const result = await this.apiCall(async (token) => {
191
+ const body = {
192
+ query,
193
+ page_size: Math.min(pageSize, 100),
194
+ };
195
+ if (filterType)
196
+ body.filter = { value: filterType, property: "object" };
197
+ const res = await fetch(`${NOTION_API}/search`, {
198
+ method: "POST",
199
+ headers: this.buildHeaders(token),
200
+ body: JSON.stringify(body),
201
+ });
202
+ if (!res.ok) {
203
+ const err = (await res.json().catch(() => ({})));
204
+ throw Object.assign(new Error(err.message ?? `HTTP ${res.status}`), {
205
+ status: res.status,
206
+ });
207
+ }
208
+ return res.json();
209
+ });
210
+ if ("error" in result)
211
+ throw new Error(result.error.message);
212
+ return result.data;
213
+ }
214
+ // ---------------------------------------------------------------- write ops
215
+ async createPage(params) {
216
+ const result = await this.apiCall(async (token) => {
217
+ const parent = params.parentType === "database"
218
+ ? { database_id: normalizeId(params.parentId) }
219
+ : { page_id: normalizeId(params.parentId) };
220
+ const properties = params.properties ?? {};
221
+ // Set title property — key is "title" for pages, "Name" for database rows
222
+ const titleKey = params.parentType === "database" ? "Name" : "title";
223
+ properties[titleKey] = {
224
+ title: [{ text: { content: params.title } }],
225
+ };
226
+ const body = { parent, properties };
227
+ if (params.content) {
228
+ body.children = [
229
+ {
230
+ object: "block",
231
+ type: "paragraph",
232
+ paragraph: {
233
+ rich_text: [{ type: "text", text: { content: params.content } }],
234
+ },
235
+ },
236
+ ];
237
+ }
238
+ const res = await fetch(`${NOTION_API}/pages`, {
239
+ method: "POST",
240
+ headers: this.buildHeaders(token),
241
+ body: JSON.stringify(body),
242
+ });
243
+ if (!res.ok) {
244
+ const err = (await res.json().catch(() => ({})));
245
+ throw Object.assign(new Error(err.message ?? `HTTP ${res.status}`), {
246
+ status: res.status,
247
+ });
248
+ }
249
+ return res.json();
250
+ });
251
+ if ("error" in result)
252
+ throw new Error(result.error.message);
253
+ return result.data;
254
+ }
255
+ async appendBlock(params) {
256
+ const result = await this.apiCall(async (token) => {
257
+ const type = params.blockType ?? "paragraph";
258
+ const richText = [{ type: "text", text: { content: params.content } }];
259
+ const block = {
260
+ object: "block",
261
+ type,
262
+ [type]: { rich_text: richText },
263
+ };
264
+ const res = await fetch(`${NOTION_API}/blocks/${normalizeId(params.pageId)}/children`, {
265
+ method: "PATCH",
266
+ headers: this.buildHeaders(token),
267
+ body: JSON.stringify({ children: [block] }),
268
+ });
269
+ if (!res.ok) {
270
+ const err = (await res.json().catch(() => ({})));
271
+ throw Object.assign(new Error(err.message ?? `HTTP ${res.status}`), {
272
+ status: res.status,
273
+ });
274
+ }
275
+ return res.json();
276
+ });
277
+ if ("error" in result)
278
+ throw new Error(result.error.message);
279
+ return result.data;
280
+ }
281
+ }
282
+ // ------------------------------------------------------------------ helpers
283
+ /** Normalize Notion IDs — strip hyphens if present, add back in UUID format. */
284
+ function normalizeId(id) {
285
+ const stripped = id.replace(/-/g, "");
286
+ if (stripped.length === 32) {
287
+ return `${stripped.slice(0, 8)}-${stripped.slice(8, 12)}-${stripped.slice(12, 16)}-${stripped.slice(16, 20)}-${stripped.slice(20)}`;
288
+ }
289
+ return id;
290
+ }
291
+ // ------------------------------------------------------------------ singleton
292
+ let _instance = null;
293
+ export function getNotionConnector() {
294
+ if (!_instance)
295
+ _instance = new NotionConnector();
296
+ return _instance;
297
+ }
298
+ export function resetNotionConnector() {
299
+ _instance = null;
300
+ }
301
+ // ------------------------------------------------------------------ convenience re-exports
302
+ export { loadTokens as isConnected };
303
+ /**
304
+ * POST /connections/notion/connect { token: "secret_..." }
305
+ * Stores the integration token and verifies it by calling /users/me.
306
+ */
307
+ export async function handleNotionConnect(body) {
308
+ let token;
309
+ try {
310
+ const parsed = JSON.parse(body);
311
+ if (typeof parsed.token !== "string" ||
312
+ !parsed.token.startsWith("secret_")) {
313
+ return {
314
+ status: 400,
315
+ contentType: "application/json",
316
+ body: JSON.stringify({
317
+ ok: false,
318
+ error: 'Notion integration token must start with "secret_". Find it at https://www.notion.so/my-integrations',
319
+ }),
320
+ };
321
+ }
322
+ token = parsed.token;
323
+ }
324
+ catch {
325
+ return {
326
+ status: 400,
327
+ contentType: "application/json",
328
+ body: JSON.stringify({ ok: false, error: "Invalid JSON body" }),
329
+ };
330
+ }
331
+ try {
332
+ const res = await fetch(`${NOTION_API}/users/me`, {
333
+ headers: {
334
+ Authorization: `Bearer ${token}`,
335
+ "Notion-Version": NOTION_VERSION,
336
+ },
337
+ });
338
+ if (!res.ok) {
339
+ return {
340
+ status: 401,
341
+ contentType: "application/json",
342
+ body: JSON.stringify({
343
+ ok: false,
344
+ error: "Token rejected by Notion API — check the token is valid and the integration is active",
345
+ }),
346
+ };
347
+ }
348
+ const user = (await res.json());
349
+ const tokens = {
350
+ accessToken: token,
351
+ workspaceName: user.bot?.workspace_name,
352
+ workspaceId: user.bot?.owner?.workspace_id,
353
+ connected_at: new Date().toISOString(),
354
+ };
355
+ saveTokens(tokens);
356
+ resetNotionConnector();
357
+ return {
358
+ status: 200,
359
+ contentType: "application/json",
360
+ body: JSON.stringify({
361
+ ok: true,
362
+ workspace: tokens.workspaceName ?? "unknown",
363
+ connectedAt: tokens.connected_at,
364
+ }),
365
+ };
366
+ }
367
+ catch (err) {
368
+ return {
369
+ status: 500,
370
+ contentType: "application/json",
371
+ body: JSON.stringify({
372
+ ok: false,
373
+ error: err instanceof Error ? err.message : String(err),
374
+ }),
375
+ };
376
+ }
377
+ }
378
+ /**
379
+ * POST /connections/notion/test
380
+ * Verifies stored token is still valid.
381
+ */
382
+ export async function handleNotionTest() {
383
+ const tokens = loadTokens();
384
+ if (!tokens) {
385
+ return {
386
+ status: 400,
387
+ contentType: "application/json",
388
+ body: JSON.stringify({ ok: false, error: "Notion not connected" }),
389
+ };
390
+ }
391
+ try {
392
+ const connector = getNotionConnector();
393
+ const check = await connector.healthCheck();
394
+ return {
395
+ status: check.ok ? 200 : 401,
396
+ contentType: "application/json",
397
+ body: JSON.stringify(check.ok ? { ok: true } : { ok: false, error: check.error?.message }),
398
+ };
399
+ }
400
+ catch (err) {
401
+ return {
402
+ status: 500,
403
+ contentType: "application/json",
404
+ body: JSON.stringify({
405
+ ok: false,
406
+ error: err instanceof Error ? err.message : String(err),
407
+ }),
408
+ };
409
+ }
410
+ }
411
+ /**
412
+ * DELETE /connections/notion
413
+ * Removes stored token.
414
+ */
415
+ export function handleNotionDisconnect() {
416
+ clearTokens();
417
+ resetNotionConnector();
418
+ return {
419
+ status: 200,
420
+ contentType: "application/json",
421
+ body: JSON.stringify({ ok: true }),
422
+ };
423
+ }
424
+ //# sourceMappingURL=notion.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"notion.js","sourceRoot":"","sources":["../../src/connectors/notion.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG;AAEH,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AACrC,OAAO,EAAE,OAAO,EAAE,MAAM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I,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;KACnC,CAAC;AACJ,CAAC"}
@@ -1,14 +1,14 @@
1
1
  /**
2
- * Sentry connector.
2
+ * Sentry connector — routes through Sentry's official MCP server.
3
3
  *
4
- * Uses Sentry's REST API with an auth token (no OAuth app required).
5
- * Token stored at ~/.patchwork/tokens/sentry.json (mode 0600).
6
- * Env vars: SENTRY_AUTH_TOKEN, SENTRY_ORG (optional default org slug).
4
+ * Endpoint: https://mcp.sentry.dev/mcp
5
+ * Auth: OAuth 2.1 w/ PKCE; dynamic client registration (RFC 7591).
7
6
  *
8
- * HTTP routes registered in bridge.ts:
9
- * POST /connections/sentry/connect store token + verify
10
- * POST /connections/sentry/test verify stored token works
11
- * DELETE /connections/sentry delete stored token
7
+ * HTTP routes (wired in src/server.ts):
8
+ * GET /connections/sentry/authorize returns { url } for popup
9
+ * GET /connections/sentry/callback — token exchange
10
+ * POST /connections/sentry/test ping MCP server
11
+ * DELETE /connections/sentry — revoke + delete token
12
12
  *
13
13
  * MCP tool: fetchSentryIssue — fetches a Sentry issue/event and returns
14
14
  * the stack trace string, ready to pass into enrichStackTrace.
@@ -24,24 +24,20 @@ export interface ConnectorStatus {
24
24
  lastSync?: string;
25
25
  org?: string;
26
26
  }
27
+ export interface ConnectorHandlerResult {
28
+ status: number;
29
+ body: string;
30
+ contentType?: string;
31
+ redirect?: string;
32
+ }
27
33
  export declare function loadTokens(): SentryTokens | null;
28
34
  export declare function getStatus(): ConnectorStatus;
29
- /**
30
- * Fetch the latest event for a Sentry issue and extract the stack trace text.
31
- * issueIdOrUrl accepts:
32
- * - A numeric issue ID: "12345"
33
- * - A Sentry issue URL: "https://sentry.io/organizations/my-org/issues/12345/"
34
- */
35
35
  export declare function fetchIssueStackTrace(issueIdOrUrl: string, signal?: AbortSignal): Promise<{
36
36
  stackTrace: string;
37
37
  title: string;
38
38
  issueId: string;
39
39
  }>;
40
- export interface ConnectorHandlerResult {
41
- status: number;
42
- body: string;
43
- contentType?: string;
44
- }
45
- export declare function handleSentryConnect(body: unknown): Promise<ConnectorHandlerResult>;
40
+ export declare function handleSentryAuthorize(): Promise<ConnectorHandlerResult>;
41
+ export declare function handleSentryCallback(code: string | null, state: string | null, error: string | null): Promise<ConnectorHandlerResult>;
46
42
  export declare function handleSentryTest(): Promise<ConnectorHandlerResult>;
47
- export declare function handleSentryDisconnect(): ConnectorHandlerResult;
43
+ export declare function handleSentryDisconnect(): Promise<ConnectorHandlerResult>;