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.
- package/README.bridge.md +6 -0
- package/README.md +40 -15
- package/deploy/bootstrap-vps.sh +184 -0
- package/deploy/deploy-dashboard.sh +174 -0
- package/deploy/deploy-landing.sh +79 -0
- package/dist/activationMetrics.d.ts +67 -0
- package/dist/activationMetrics.js +255 -0
- package/dist/activationMetrics.js.map +1 -0
- package/dist/approvalHttp.d.ts +24 -2
- package/dist/approvalHttp.js +150 -10
- package/dist/approvalHttp.js.map +1 -1
- package/dist/approvalQueue.d.ts +16 -1
- package/dist/approvalQueue.js +44 -3
- package/dist/approvalQueue.js.map +1 -1
- package/dist/automation.d.ts +20 -0
- package/dist/automation.js +54 -1
- package/dist/automation.js.map +1 -1
- package/dist/bridge.d.ts +2 -0
- package/dist/bridge.js +55 -130
- package/dist/bridge.js.map +1 -1
- package/dist/bridgeToken.js +57 -19
- package/dist/bridgeToken.js.map +1 -1
- package/dist/ccPermissions.js +6 -4
- package/dist/ccPermissions.js.map +1 -1
- package/dist/claudeOrchestrator.d.ts +1 -1
- package/dist/claudeOrchestrator.js +14 -8
- package/dist/claudeOrchestrator.js.map +1 -1
- package/dist/commands/launchd.d.ts +2 -0
- package/dist/commands/launchd.js +94 -0
- package/dist/commands/launchd.js.map +1 -0
- package/dist/commands/recipe.d.ts +258 -0
- package/dist/commands/recipe.js +1130 -0
- package/dist/commands/recipe.js.map +1 -0
- package/dist/commands/recipeInstall.d.ts +72 -0
- package/dist/commands/recipeInstall.js +339 -0
- package/dist/commands/recipeInstall.js.map +1 -0
- package/dist/config.d.ts +14 -1
- package/dist/config.js +99 -8
- package/dist/config.js.map +1 -1
- package/dist/connectors/baseConnector.d.ts +117 -0
- package/dist/connectors/baseConnector.js +213 -0
- package/dist/connectors/baseConnector.js.map +1 -0
- package/dist/connectors/confluence.d.ts +111 -0
- package/dist/connectors/confluence.js +406 -0
- package/dist/connectors/confluence.js.map +1 -0
- package/dist/connectors/datadog.d.ts +116 -0
- package/dist/connectors/datadog.js +385 -0
- package/dist/connectors/datadog.js.map +1 -0
- package/dist/connectors/fixtureLibrary.d.ts +21 -0
- package/dist/connectors/fixtureLibrary.js +70 -0
- package/dist/connectors/fixtureLibrary.js.map +1 -0
- package/dist/connectors/fixtureRecorder.d.ts +1 -0
- package/dist/connectors/fixtureRecorder.js +35 -0
- package/dist/connectors/fixtureRecorder.js.map +1 -0
- package/dist/connectors/github.d.ts +58 -8
- package/dist/connectors/github.js +312 -84
- package/dist/connectors/github.js.map +1 -1
- package/dist/connectors/gmail.d.ts +4 -1
- package/dist/connectors/gmail.js +79 -16
- package/dist/connectors/gmail.js.map +1 -1
- package/dist/connectors/googleCalendar.d.ts +60 -0
- package/dist/connectors/googleCalendar.js +345 -0
- package/dist/connectors/googleCalendar.js.map +1 -0
- package/dist/connectors/hubspot.d.ts +112 -0
- package/dist/connectors/hubspot.js +408 -0
- package/dist/connectors/hubspot.js.map +1 -0
- package/dist/connectors/intercom.d.ts +102 -0
- package/dist/connectors/intercom.js +402 -0
- package/dist/connectors/intercom.js.map +1 -0
- package/dist/connectors/jira.d.ts +98 -0
- package/dist/connectors/jira.js +379 -0
- package/dist/connectors/jira.js.map +1 -0
- package/dist/connectors/linear.d.ts +69 -19
- package/dist/connectors/linear.js +170 -129
- package/dist/connectors/linear.js.map +1 -1
- package/dist/connectors/mcpClient.d.ts +56 -0
- package/dist/connectors/mcpClient.js +189 -0
- package/dist/connectors/mcpClient.js.map +1 -0
- package/dist/connectors/mcpOAuth.d.ts +84 -0
- package/dist/connectors/mcpOAuth.js +389 -0
- package/dist/connectors/mcpOAuth.js.map +1 -0
- package/dist/connectors/mockConnector.d.ts +28 -0
- package/dist/connectors/mockConnector.js +81 -0
- package/dist/connectors/mockConnector.js.map +1 -0
- package/dist/connectors/notion.d.ts +143 -0
- package/dist/connectors/notion.js +424 -0
- package/dist/connectors/notion.js.map +1 -0
- package/dist/connectors/sentry.d.ts +17 -21
- package/dist/connectors/sentry.js +115 -131
- package/dist/connectors/sentry.js.map +1 -1
- package/dist/connectors/slack.d.ts +50 -0
- package/dist/connectors/slack.js +324 -0
- package/dist/connectors/slack.js.map +1 -0
- package/dist/connectors/stripe.d.ts +116 -0
- package/dist/connectors/stripe.js +379 -0
- package/dist/connectors/stripe.js.map +1 -0
- package/dist/connectors/tokenStorage.d.ts +35 -0
- package/dist/connectors/tokenStorage.js +459 -0
- package/dist/connectors/tokenStorage.js.map +1 -0
- package/dist/connectors/zendesk.d.ts +104 -0
- package/dist/connectors/zendesk.js +424 -0
- package/dist/connectors/zendesk.js.map +1 -0
- package/dist/drivers/gemini/index.d.ts +5 -1
- package/dist/drivers/gemini/index.js +39 -5
- package/dist/drivers/gemini/index.js.map +1 -1
- package/dist/drivers/index.d.ts +5 -0
- package/dist/drivers/index.js +1 -1
- package/dist/drivers/index.js.map +1 -1
- package/dist/featureFlags.d.ts +73 -0
- package/dist/featureFlags.js +203 -0
- package/dist/featureFlags.js.map +1 -0
- package/dist/fp/automationInterpreter.js +1 -0
- package/dist/fp/automationInterpreter.js.map +1 -1
- package/dist/fp/automationProgram.d.ts +1 -1
- package/dist/fp/automationProgram.js.map +1 -1
- package/dist/fp/policyParser.js +17 -0
- package/dist/fp/policyParser.js.map +1 -1
- package/dist/index.js +621 -61
- package/dist/index.js.map +1 -1
- package/dist/installGuard.d.ts +25 -0
- package/dist/installGuard.js +48 -0
- package/dist/installGuard.js.map +1 -0
- package/dist/oauth.d.ts +4 -1
- package/dist/oauth.js +50 -14
- package/dist/oauth.js.map +1 -1
- package/dist/patchworkConfig.d.ts +9 -0
- package/dist/patchworkConfig.js.map +1 -1
- package/dist/recipeOrchestration.d.ts +53 -0
- package/dist/recipeOrchestration.js +272 -0
- package/dist/recipeOrchestration.js.map +1 -0
- package/dist/recipes/RecipeOrchestrator.d.ts +40 -0
- package/dist/recipes/RecipeOrchestrator.js +51 -0
- package/dist/recipes/RecipeOrchestrator.js.map +1 -0
- package/dist/recipes/agentExecutor.d.ts +28 -0
- package/dist/recipes/agentExecutor.js +42 -0
- package/dist/recipes/agentExecutor.js.map +1 -0
- package/dist/recipes/chainedRunner.d.ts +140 -0
- package/dist/recipes/chainedRunner.js +539 -0
- package/dist/recipes/chainedRunner.js.map +1 -0
- package/dist/recipes/dependencyGraph.d.ts +39 -0
- package/dist/recipes/dependencyGraph.js +199 -0
- package/dist/recipes/dependencyGraph.js.map +1 -0
- package/dist/recipes/legacyRecipeCompat.d.ts +2 -0
- package/dist/recipes/legacyRecipeCompat.js +112 -0
- package/dist/recipes/legacyRecipeCompat.js.map +1 -0
- package/dist/recipes/manifest.d.ts +47 -0
- package/dist/recipes/manifest.js +141 -0
- package/dist/recipes/manifest.js.map +1 -0
- package/dist/recipes/nestedRecipeStep.d.ts +58 -0
- package/dist/recipes/nestedRecipeStep.js +95 -0
- package/dist/recipes/nestedRecipeStep.js.map +1 -0
- package/dist/recipes/outputRegistry.d.ts +28 -0
- package/dist/recipes/outputRegistry.js +52 -0
- package/dist/recipes/outputRegistry.js.map +1 -0
- package/dist/recipes/scheduler.d.ts +23 -7
- package/dist/recipes/scheduler.js +131 -41
- package/dist/recipes/scheduler.js.map +1 -1
- package/dist/recipes/schema.d.ts +17 -2
- package/dist/recipes/schemaGenerator.d.ts +28 -0
- package/dist/recipes/schemaGenerator.js +565 -0
- package/dist/recipes/schemaGenerator.js.map +1 -0
- package/dist/recipes/templateEngine.d.ts +62 -0
- package/dist/recipes/templateEngine.js +182 -0
- package/dist/recipes/templateEngine.js.map +1 -0
- package/dist/recipes/toolRegistry.d.ts +181 -0
- package/dist/recipes/toolRegistry.js +300 -0
- package/dist/recipes/toolRegistry.js.map +1 -0
- package/dist/recipes/tools/calendar.d.ts +6 -0
- package/dist/recipes/tools/calendar.js +61 -0
- package/dist/recipes/tools/calendar.js.map +1 -0
- package/dist/recipes/tools/confluence.d.ts +6 -0
- package/dist/recipes/tools/confluence.js +254 -0
- package/dist/recipes/tools/confluence.js.map +1 -0
- package/dist/recipes/tools/datadog.d.ts +6 -0
- package/dist/recipes/tools/datadog.js +239 -0
- package/dist/recipes/tools/datadog.js.map +1 -0
- package/dist/recipes/tools/diagnostics.d.ts +6 -0
- package/dist/recipes/tools/diagnostics.js +36 -0
- package/dist/recipes/tools/diagnostics.js.map +1 -0
- package/dist/recipes/tools/file.d.ts +6 -0
- package/dist/recipes/tools/file.js +170 -0
- package/dist/recipes/tools/file.js.map +1 -0
- package/dist/recipes/tools/git.d.ts +6 -0
- package/dist/recipes/tools/git.js +63 -0
- package/dist/recipes/tools/git.js.map +1 -0
- package/dist/recipes/tools/github.d.ts +6 -0
- package/dist/recipes/tools/github.js +91 -0
- package/dist/recipes/tools/github.js.map +1 -0
- package/dist/recipes/tools/gmail.d.ts +6 -0
- package/dist/recipes/tools/gmail.js +210 -0
- package/dist/recipes/tools/gmail.js.map +1 -0
- package/dist/recipes/tools/hubspot.d.ts +6 -0
- package/dist/recipes/tools/hubspot.js +232 -0
- package/dist/recipes/tools/hubspot.js.map +1 -0
- package/dist/recipes/tools/index.d.ts +22 -0
- package/dist/recipes/tools/index.js +25 -0
- package/dist/recipes/tools/index.js.map +1 -0
- package/dist/recipes/tools/intercom.d.ts +6 -0
- package/dist/recipes/tools/intercom.js +226 -0
- package/dist/recipes/tools/intercom.js.map +1 -0
- package/dist/recipes/tools/linear.d.ts +6 -0
- package/dist/recipes/tools/linear.js +83 -0
- package/dist/recipes/tools/linear.js.map +1 -0
- package/dist/recipes/tools/notion.d.ts +6 -0
- package/dist/recipes/tools/notion.js +278 -0
- package/dist/recipes/tools/notion.js.map +1 -0
- package/dist/recipes/tools/slack.d.ts +6 -0
- package/dist/recipes/tools/slack.js +72 -0
- package/dist/recipes/tools/slack.js.map +1 -0
- package/dist/recipes/tools/stripe.d.ts +6 -0
- package/dist/recipes/tools/stripe.js +265 -0
- package/dist/recipes/tools/stripe.js.map +1 -0
- package/dist/recipes/tools/zendesk.d.ts +6 -0
- package/dist/recipes/tools/zendesk.js +245 -0
- package/dist/recipes/tools/zendesk.js.map +1 -0
- package/dist/recipes/validation.d.ts +13 -0
- package/dist/recipes/validation.js +433 -0
- package/dist/recipes/validation.js.map +1 -0
- package/dist/recipes/yamlRunner.d.ts +87 -0
- package/dist/recipes/yamlRunner.js +693 -409
- package/dist/recipes/yamlRunner.js.map +1 -1
- package/dist/recipesHttp.d.ts +34 -6
- package/dist/recipesHttp.js +285 -15
- package/dist/recipesHttp.js.map +1 -1
- package/dist/riskTier.js +1 -0
- package/dist/riskTier.js.map +1 -1
- package/dist/runLog.d.ts +23 -0
- package/dist/runLog.js +56 -1
- package/dist/runLog.js.map +1 -1
- package/dist/schemas/dry-run-plan.v1.json +139 -0
- package/dist/schemas/recipe.v1.json +684 -0
- package/dist/server.d.ts +32 -1
- package/dist/server.js +980 -97
- package/dist/server.js.map +1 -1
- package/dist/streamableHttp.js +2 -0
- package/dist/streamableHttp.js.map +1 -1
- package/dist/tools/addLinearComment.d.ts +55 -0
- package/dist/tools/addLinearComment.js +72 -0
- package/dist/tools/addLinearComment.js.map +1 -0
- package/dist/tools/bridgeDoctor.js +2 -2
- package/dist/tools/bridgeDoctor.js.map +1 -1
- package/dist/tools/createLinearIssue.d.ts +84 -0
- package/dist/tools/createLinearIssue.js +146 -0
- package/dist/tools/createLinearIssue.js.map +1 -0
- package/dist/tools/fetchCalendarEvents.d.ts +94 -0
- package/dist/tools/fetchCalendarEvents.js +97 -0
- package/dist/tools/fetchCalendarEvents.js.map +1 -0
- package/dist/tools/fetchGithubIssue.d.ts +80 -0
- package/dist/tools/fetchGithubIssue.js +84 -0
- package/dist/tools/fetchGithubIssue.js.map +1 -0
- package/dist/tools/fetchGithubPR.d.ts +89 -0
- package/dist/tools/fetchGithubPR.js +96 -0
- package/dist/tools/fetchGithubPR.js.map +1 -0
- package/dist/tools/fetchSlackProfile.d.ts +43 -0
- package/dist/tools/fetchSlackProfile.js +46 -0
- package/dist/tools/fetchSlackProfile.js.map +1 -0
- package/dist/tools/getConnectorStatus.d.ts +58 -0
- package/dist/tools/getConnectorStatus.js +56 -0
- package/dist/tools/getConnectorStatus.js.map +1 -0
- package/dist/tools/github/actions.js +4 -2
- package/dist/tools/github/actions.js.map +1 -1
- package/dist/tools/github/composite.d.ts +339 -0
- package/dist/tools/github/composite.js +343 -0
- package/dist/tools/github/composite.js.map +1 -0
- package/dist/tools/github/index.d.ts +2 -1
- package/dist/tools/github/index.js +2 -1
- package/dist/tools/github/index.js.map +1 -1
- package/dist/tools/github/issues.js +8 -4
- package/dist/tools/github/issues.js.map +1 -1
- package/dist/tools/github/pr.d.ts +122 -0
- package/dist/tools/github/pr.js +195 -5
- package/dist/tools/github/pr.js.map +1 -1
- package/dist/tools/index.js +32 -1
- package/dist/tools/index.js.map +1 -1
- package/dist/tools/searchTools.js +1 -1
- package/dist/tools/searchTools.js.map +1 -1
- package/dist/tools/slackListChannels.d.ts +65 -0
- package/dist/tools/slackListChannels.js +70 -0
- package/dist/tools/slackListChannels.js.map +1 -0
- package/dist/tools/slackPostMessage.d.ts +57 -0
- package/dist/tools/slackPostMessage.js +77 -0
- package/dist/tools/slackPostMessage.js.map +1 -0
- package/dist/tools/testTraceToSource.js +2 -2
- package/dist/tools/testTraceToSource.js.map +1 -1
- package/dist/tools/updateLinearIssue.d.ts +89 -0
- package/dist/tools/updateLinearIssue.js +117 -0
- package/dist/tools/updateLinearIssue.js.map +1 -0
- package/dist/transport.d.ts +7 -1
- package/dist/transport.js +85 -11
- package/dist/transport.js.map +1 -1
- package/package.json +5 -2
- package/scripts/start-all.sh +56 -19
- package/templates/automation-policies/recipe-authoring.json +25 -0
- package/templates/automation-policy.example.json +6 -0
- package/templates/co.patchwork-os.bridge.plist +34 -0
- package/templates/recipes/ctx-loop-test.yaml +75 -0
- package/templates/recipes/lint-on-save.yaml +1 -2
- package/templates/recipes/morning-brief-slack.yaml +57 -0
- package/templates/recipes/morning-brief.yaml +14 -6
- package/templates/recipes/project-health-check.yaml +50 -0
- package/templates/recipes/sentry-to-linear.yaml +77 -0
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slack connector — calls Slack Web API directly with bot token.
|
|
3
|
+
*
|
|
4
|
+
* OAuth 2.0 (not PKCE) — Slack uses its own flow with bot token in response.
|
|
5
|
+
* Tokens stored at ~/.patchwork/tokens/slack.json (mode 0600).
|
|
6
|
+
* Client credentials: PATCHWORK_SLACK_CLIENT_ID / PATCHWORK_SLACK_CLIENT_SECRET
|
|
7
|
+
*
|
|
8
|
+
* HTTP routes (wired in src/server.ts):
|
|
9
|
+
* GET /connections/slack/authorize — redirect to Slack consent
|
|
10
|
+
* GET /connections/slack/callback — exchange code for bot token
|
|
11
|
+
* POST /connections/slack/test — ping Slack API
|
|
12
|
+
* DELETE /connections/slack — delete stored token
|
|
13
|
+
*/
|
|
14
|
+
import crypto from "node:crypto";
|
|
15
|
+
import { existsSync, mkdirSync, readFileSync, unlinkSync, writeFileSync, } from "node:fs";
|
|
16
|
+
import { homedir } from "node:os";
|
|
17
|
+
import path from "node:path";
|
|
18
|
+
import { deleteSecretJsonSync, getSecretJsonSync, storeSecretJsonSync, } from "./tokenStorage.js";
|
|
19
|
+
const SLACK_AUTH_URL = "https://slack.com/oauth/v2/authorize";
|
|
20
|
+
const SLACK_TOKEN_URL = "https://slack.com/api/oauth.v2.access";
|
|
21
|
+
const SCOPES = [
|
|
22
|
+
"chat:write",
|
|
23
|
+
"channels:read",
|
|
24
|
+
"channels:history",
|
|
25
|
+
"users:read",
|
|
26
|
+
].join(",");
|
|
27
|
+
function getPatchworkTokensDir() {
|
|
28
|
+
const patchworkHome = process.env.PATCHWORK_HOME ?? path.join(homedir(), ".patchwork");
|
|
29
|
+
return path.join(patchworkHome, "tokens");
|
|
30
|
+
}
|
|
31
|
+
function getLegacyTokenPath() {
|
|
32
|
+
return path.join(getPatchworkTokensDir(), "slack.json");
|
|
33
|
+
}
|
|
34
|
+
function getStatePath() {
|
|
35
|
+
return path.join(getPatchworkTokensDir(), "slack-state.json");
|
|
36
|
+
}
|
|
37
|
+
// ── Config ───────────────────────────────────────────────────────────────────
|
|
38
|
+
function clientId() {
|
|
39
|
+
return process.env.PATCHWORK_SLACK_CLIENT_ID ?? "";
|
|
40
|
+
}
|
|
41
|
+
function clientSecret() {
|
|
42
|
+
return process.env.PATCHWORK_SLACK_CLIENT_SECRET ?? "";
|
|
43
|
+
}
|
|
44
|
+
function redirectUri() {
|
|
45
|
+
const base = (process.env.PATCHWORK_BRIDGE_URL ??
|
|
46
|
+
`http://localhost:${process.env.PATCHWORK_BRIDGE_PORT ?? "3101"}`).replace(/\/$/, "");
|
|
47
|
+
return `${base}/connections/slack/callback`;
|
|
48
|
+
}
|
|
49
|
+
// ── Token storage ─────────────────────────────────────────────────────────────
|
|
50
|
+
export function loadTokens() {
|
|
51
|
+
const secureTokens = getSecretJsonSync("slack");
|
|
52
|
+
if (secureTokens) {
|
|
53
|
+
return secureTokens;
|
|
54
|
+
}
|
|
55
|
+
const legacyTokenPath = getLegacyTokenPath();
|
|
56
|
+
if (!existsSync(legacyTokenPath))
|
|
57
|
+
return null;
|
|
58
|
+
try {
|
|
59
|
+
const tokens = JSON.parse(readFileSync(legacyTokenPath, "utf-8"));
|
|
60
|
+
saveTokens(tokens);
|
|
61
|
+
return tokens;
|
|
62
|
+
}
|
|
63
|
+
catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function saveTokens(file) {
|
|
68
|
+
storeSecretJsonSync("slack", file);
|
|
69
|
+
const legacyTokenPath = getLegacyTokenPath();
|
|
70
|
+
if (existsSync(legacyTokenPath)) {
|
|
71
|
+
try {
|
|
72
|
+
unlinkSync(legacyTokenPath);
|
|
73
|
+
}
|
|
74
|
+
catch { }
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function deleteTokens() {
|
|
78
|
+
deleteSecretJsonSync("slack");
|
|
79
|
+
const legacyTokenPath = getLegacyTokenPath();
|
|
80
|
+
if (existsSync(legacyTokenPath)) {
|
|
81
|
+
try {
|
|
82
|
+
unlinkSync(legacyTokenPath);
|
|
83
|
+
}
|
|
84
|
+
catch { }
|
|
85
|
+
}
|
|
86
|
+
const statePath = getStatePath();
|
|
87
|
+
if (existsSync(statePath)) {
|
|
88
|
+
try {
|
|
89
|
+
unlinkSync(statePath);
|
|
90
|
+
}
|
|
91
|
+
catch { }
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
export function isConnected() {
|
|
95
|
+
return loadTokens() !== null;
|
|
96
|
+
}
|
|
97
|
+
// ── State (CSRF) ──────────────────────────────────────────────────────────────
|
|
98
|
+
function saveState(state) {
|
|
99
|
+
const statePath = getStatePath();
|
|
100
|
+
mkdirSync(path.dirname(statePath), { recursive: true, mode: 0o700 });
|
|
101
|
+
writeFileSync(statePath, JSON.stringify({ state, ts: Date.now() }), {
|
|
102
|
+
mode: 0o600,
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
function consumeState() {
|
|
106
|
+
const statePath = getStatePath();
|
|
107
|
+
if (!existsSync(statePath))
|
|
108
|
+
return null;
|
|
109
|
+
try {
|
|
110
|
+
const { state, ts } = JSON.parse(readFileSync(statePath, "utf-8"));
|
|
111
|
+
unlinkSync(statePath);
|
|
112
|
+
if (Date.now() - ts > 10 * 60 * 1000)
|
|
113
|
+
return null; // 10-min TTL
|
|
114
|
+
return state;
|
|
115
|
+
}
|
|
116
|
+
catch {
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// ── API helper ────────────────────────────────────────────────────────────────
|
|
121
|
+
async function slackGet(method, params, token, signal) {
|
|
122
|
+
const url = new URL(`https://slack.com/api/${method}`);
|
|
123
|
+
for (const [k, v] of Object.entries(params))
|
|
124
|
+
url.searchParams.set(k, v);
|
|
125
|
+
const res = await fetch(url.toString(), {
|
|
126
|
+
headers: { Authorization: `Bearer ${token}`, Accept: "application/json" },
|
|
127
|
+
signal,
|
|
128
|
+
});
|
|
129
|
+
if (!res.ok)
|
|
130
|
+
throw new Error(`Slack API ${method} HTTP ${res.status}`);
|
|
131
|
+
const json = (await res.json());
|
|
132
|
+
if (!json.ok)
|
|
133
|
+
throw new Error(`Slack API ${method} error: ${json.error ?? "unknown"}`);
|
|
134
|
+
return json;
|
|
135
|
+
}
|
|
136
|
+
async function slackPost(method, body, token, signal) {
|
|
137
|
+
const res = await fetch(`https://slack.com/api/${method}`, {
|
|
138
|
+
method: "POST",
|
|
139
|
+
headers: {
|
|
140
|
+
Authorization: `Bearer ${token}`,
|
|
141
|
+
"Content-Type": "application/json; charset=utf-8",
|
|
142
|
+
Accept: "application/json",
|
|
143
|
+
},
|
|
144
|
+
body: JSON.stringify(body),
|
|
145
|
+
signal,
|
|
146
|
+
});
|
|
147
|
+
if (!res.ok)
|
|
148
|
+
throw new Error(`Slack API ${method} HTTP ${res.status}`);
|
|
149
|
+
const json = (await res.json());
|
|
150
|
+
if (!json.ok)
|
|
151
|
+
throw new Error(`Slack API ${method} error: ${json.error ?? "unknown"}`);
|
|
152
|
+
return json;
|
|
153
|
+
}
|
|
154
|
+
// ── Public API ────────────────────────────────────────────────────────────────
|
|
155
|
+
export async function postMessage(channel, text, threadTs, signal) {
|
|
156
|
+
const tokens = loadTokens();
|
|
157
|
+
if (!tokens)
|
|
158
|
+
throw new Error("Slack not connected. GET /connections/slack/authorize first.");
|
|
159
|
+
const body = { channel, text };
|
|
160
|
+
if (threadTs)
|
|
161
|
+
body.thread_ts = threadTs;
|
|
162
|
+
const res = await slackPost("chat.postMessage", body, tokens.access_token, signal);
|
|
163
|
+
return {
|
|
164
|
+
ts: res.ts ?? "",
|
|
165
|
+
channel: res.channel ?? channel,
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
export async function listChannels(limit = 100, signal) {
|
|
169
|
+
const tokens = loadTokens();
|
|
170
|
+
if (!tokens)
|
|
171
|
+
throw new Error("Slack not connected. GET /connections/slack/authorize first.");
|
|
172
|
+
const res = await slackGet("conversations.list", {
|
|
173
|
+
types: "public_channel",
|
|
174
|
+
exclude_archived: "true",
|
|
175
|
+
limit: String(Math.min(limit, 200)),
|
|
176
|
+
}, tokens.access_token, signal);
|
|
177
|
+
const channels = res.channels ?? [];
|
|
178
|
+
return channels.map((c) => ({
|
|
179
|
+
id: c.id ?? "",
|
|
180
|
+
name: c.name ?? "",
|
|
181
|
+
isMember: Boolean(c.is_member),
|
|
182
|
+
isPrivate: Boolean(c.is_private),
|
|
183
|
+
numMembers: typeof c.num_members === "number" ? c.num_members : undefined,
|
|
184
|
+
}));
|
|
185
|
+
}
|
|
186
|
+
export function getProfile() {
|
|
187
|
+
const tokens = loadTokens();
|
|
188
|
+
if (!tokens)
|
|
189
|
+
return null;
|
|
190
|
+
return {
|
|
191
|
+
teamId: tokens.team_id,
|
|
192
|
+
teamName: tokens.team_name,
|
|
193
|
+
botUserId: tokens.bot_user_id,
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
// ── HTTP handlers ─────────────────────────────────────────────────────────────
|
|
197
|
+
export function handleSlackAuthorize() {
|
|
198
|
+
if (!clientId()) {
|
|
199
|
+
return {
|
|
200
|
+
status: 400,
|
|
201
|
+
contentType: "application/json",
|
|
202
|
+
body: JSON.stringify({
|
|
203
|
+
ok: false,
|
|
204
|
+
error: "PATCHWORK_SLACK_CLIENT_ID not set — create a Slack app first",
|
|
205
|
+
}),
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
const state = crypto.randomBytes(16).toString("hex");
|
|
209
|
+
saveState(state);
|
|
210
|
+
const params = new URLSearchParams({
|
|
211
|
+
client_id: clientId(),
|
|
212
|
+
scope: SCOPES,
|
|
213
|
+
redirect_uri: redirectUri(),
|
|
214
|
+
state,
|
|
215
|
+
});
|
|
216
|
+
return { status: 302, body: "", redirect: `${SLACK_AUTH_URL}?${params}` };
|
|
217
|
+
}
|
|
218
|
+
export async function handleSlackCallback(code, state, error) {
|
|
219
|
+
if (error) {
|
|
220
|
+
return {
|
|
221
|
+
status: 400,
|
|
222
|
+
contentType: "text/html",
|
|
223
|
+
body: `<html><body><h2>Slack connect failed</h2><pre>${error}</pre></body></html>`,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
if (!code || !state) {
|
|
227
|
+
return {
|
|
228
|
+
status: 400,
|
|
229
|
+
contentType: "text/html",
|
|
230
|
+
body: `<html><body><h2>Slack connect failed</h2><pre>missing code or state</pre></body></html>`,
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
const savedState = consumeState();
|
|
234
|
+
if (!savedState || savedState !== state) {
|
|
235
|
+
return {
|
|
236
|
+
status: 400,
|
|
237
|
+
contentType: "text/html",
|
|
238
|
+
body: `<html><body><h2>Slack connect failed</h2><pre>invalid state</pre></body></html>`,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
try {
|
|
242
|
+
const params = new URLSearchParams({
|
|
243
|
+
code,
|
|
244
|
+
client_id: clientId(),
|
|
245
|
+
client_secret: clientSecret(),
|
|
246
|
+
redirect_uri: redirectUri(),
|
|
247
|
+
});
|
|
248
|
+
const res = await fetch(SLACK_TOKEN_URL, {
|
|
249
|
+
method: "POST",
|
|
250
|
+
headers: {
|
|
251
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
252
|
+
Accept: "application/json",
|
|
253
|
+
},
|
|
254
|
+
body: params.toString(),
|
|
255
|
+
});
|
|
256
|
+
if (!res.ok)
|
|
257
|
+
throw new Error(`Token exchange HTTP ${res.status}`);
|
|
258
|
+
const json = (await res.json());
|
|
259
|
+
if (!json.ok)
|
|
260
|
+
throw new Error(`Token exchange error: ${json.error ?? "unknown"}`);
|
|
261
|
+
const botToken = json.access_token ?? "";
|
|
262
|
+
const team = json.team ?? {};
|
|
263
|
+
const botUser = json.bot_user_id ?? "";
|
|
264
|
+
saveTokens({
|
|
265
|
+
access_token: botToken,
|
|
266
|
+
team_id: team.id ?? "",
|
|
267
|
+
team_name: team.name ?? "",
|
|
268
|
+
bot_user_id: botUser,
|
|
269
|
+
connected_at: new Date().toISOString(),
|
|
270
|
+
});
|
|
271
|
+
return {
|
|
272
|
+
status: 200,
|
|
273
|
+
contentType: "text/html",
|
|
274
|
+
body: `<html><body><h2>Slack connected to ${team.name ?? "workspace"}</h2><script>try { window.opener.postMessage('patchwork:slack:connected', '*'); } catch(_) {} window.close();</script></body></html>`,
|
|
275
|
+
};
|
|
276
|
+
}
|
|
277
|
+
catch (err) {
|
|
278
|
+
return {
|
|
279
|
+
status: 400,
|
|
280
|
+
contentType: "text/html",
|
|
281
|
+
body: `<html><body><h2>Slack connect failed</h2><pre>${err instanceof Error ? err.message : String(err)}</pre></body></html>`,
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
export async function handleSlackTest() {
|
|
286
|
+
const tokens = loadTokens();
|
|
287
|
+
if (!tokens) {
|
|
288
|
+
return {
|
|
289
|
+
status: 200,
|
|
290
|
+
contentType: "application/json",
|
|
291
|
+
body: JSON.stringify({ ok: false, message: "Not connected" }),
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
try {
|
|
295
|
+
await slackGet("auth.test", {}, tokens.access_token);
|
|
296
|
+
return {
|
|
297
|
+
status: 200,
|
|
298
|
+
contentType: "application/json",
|
|
299
|
+
body: JSON.stringify({
|
|
300
|
+
ok: true,
|
|
301
|
+
message: `Connected to ${tokens.team_name}`,
|
|
302
|
+
}),
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
catch (err) {
|
|
306
|
+
return {
|
|
307
|
+
status: 200,
|
|
308
|
+
contentType: "application/json",
|
|
309
|
+
body: JSON.stringify({
|
|
310
|
+
ok: false,
|
|
311
|
+
message: err instanceof Error ? err.message : String(err),
|
|
312
|
+
}),
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
export function handleSlackDisconnect() {
|
|
317
|
+
deleteTokens();
|
|
318
|
+
return {
|
|
319
|
+
status: 200,
|
|
320
|
+
contentType: "application/json",
|
|
321
|
+
body: JSON.stringify({ ok: true }),
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
//# sourceMappingURL=slack.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"slack.js","sourceRoot":"","sources":["../../src/connectors/slack.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,MAAM,MAAM,aAAa,CAAC;AACjC,OAAO,EACL,UAAU,EACV,SAAS,EACT,YAAY,EACZ,UAAU,EACV,aAAa,GACd,MAAM,SAAS,CAAC;AACjB,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAClC,OAAO,IAAI,MAAM,WAAW,CAAC;AAC7B,OAAO,EACL,oBAAoB,EACpB,iBAAiB,EACjB,mBAAmB,GACpB,MAAM,mBAAmB,CAAC;AAE3B,MAAM,cAAc,GAAG,sCAAsC,CAAC;AAC9D,MAAM,eAAe,GAAG,uCAAuC,CAAC;AAChE,MAAM,MAAM,GAAG;IACb,YAAY;IACZ,eAAe;IACf,kBAAkB;IAClB,YAAY;CACb,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAEZ,SAAS,qBAAqB;IAC5B,MAAM,aAAa,GACjB,OAAO,CAAC,GAAG,CAAC,cAAc,IAAI,IAAI,CAAC,IAAI,CAAC,OAAO,EAAE,EAAE,YAAY,CAAC,CAAC;IACnE,OAAO,IAAI,CAAC,IAAI,CAAC,aAAa,EAAE,QAAQ,CAAC,CAAC;AAC5C,CAAC;AAED,SAAS,kBAAkB;IACzB,OAAO,IAAI,CAAC,IAAI,CAAC,qBAAqB,EAAE,EAAE,YAAY,CAAC,CAAC;AAC1D,CAAC;AAED,SAAS,YAAY;IACnB,OAAO,IAAI,CAAC,IAAI,CAAC,qBAAqB,EAAE,EAAE,kBAAkB,CAAC,CAAC;AAChE,CAAC;AAiBD,gFAAgF;AAEhF,SAAS,QAAQ;IACf,OA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,CAAC;IACf,OAAO;QACL,MAAM,EAAE,GAAG;QACX,WAAW,EAAE,kBAAkB;QAC/B,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,EAAE,EAAE,IAAI,EAAE,CAAC;KACnC,CAAC;AACJ,CAAC"}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stripe connector — read-only access to Stripe data via the Stripe REST API v1.
|
|
3
|
+
*
|
|
4
|
+
* Auth: Basic auth where username=secret_key, password="".
|
|
5
|
+
* - Env var: STRIPE_SECRET_KEY
|
|
6
|
+
* - Stored: getSecretJsonSync("stripe") → StripeTokens
|
|
7
|
+
*
|
|
8
|
+
* Tools: listCharges, getCharge, listCustomers, getCustomer, listSubscriptions, listInvoices
|
|
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 StripeTokens {
|
|
14
|
+
secretKey: string;
|
|
15
|
+
accountId?: string;
|
|
16
|
+
accountName?: string;
|
|
17
|
+
connected_at: string;
|
|
18
|
+
}
|
|
19
|
+
export interface StripeCharge {
|
|
20
|
+
id: string;
|
|
21
|
+
amount: number;
|
|
22
|
+
currency: string;
|
|
23
|
+
status: string;
|
|
24
|
+
customer: string | null;
|
|
25
|
+
description: string | null;
|
|
26
|
+
created: number;
|
|
27
|
+
paid: boolean;
|
|
28
|
+
refunded: boolean;
|
|
29
|
+
}
|
|
30
|
+
export interface StripeCustomer {
|
|
31
|
+
id: string;
|
|
32
|
+
email: string | null;
|
|
33
|
+
name: string | null;
|
|
34
|
+
created: number;
|
|
35
|
+
currency: string | null;
|
|
36
|
+
balance: number;
|
|
37
|
+
}
|
|
38
|
+
export interface StripeSubscription {
|
|
39
|
+
id: string;
|
|
40
|
+
customer: string;
|
|
41
|
+
status: string;
|
|
42
|
+
current_period_start: number;
|
|
43
|
+
current_period_end: number;
|
|
44
|
+
created: number;
|
|
45
|
+
cancel_at_period_end: boolean;
|
|
46
|
+
}
|
|
47
|
+
export interface StripeInvoice {
|
|
48
|
+
id: string;
|
|
49
|
+
customer: string;
|
|
50
|
+
status: string | null;
|
|
51
|
+
amount_due: number;
|
|
52
|
+
amount_paid: number;
|
|
53
|
+
currency: string;
|
|
54
|
+
created: number;
|
|
55
|
+
due_date: number | null;
|
|
56
|
+
}
|
|
57
|
+
export interface StripeListResult<T> {
|
|
58
|
+
data: T[];
|
|
59
|
+
has_more: boolean;
|
|
60
|
+
}
|
|
61
|
+
export declare class StripeConnector extends BaseConnector {
|
|
62
|
+
readonly providerName = "stripe";
|
|
63
|
+
private tokens;
|
|
64
|
+
protected getOAuthConfig(): null;
|
|
65
|
+
authenticate(): Promise<AuthContext>;
|
|
66
|
+
healthCheck(): Promise<{
|
|
67
|
+
ok: boolean;
|
|
68
|
+
error?: ConnectorError;
|
|
69
|
+
}>;
|
|
70
|
+
normalizeError(error: unknown): ConnectorError;
|
|
71
|
+
getStatus(): ConnectorStatus;
|
|
72
|
+
listCharges(params?: {
|
|
73
|
+
limit?: number;
|
|
74
|
+
customerId?: string;
|
|
75
|
+
status?: string;
|
|
76
|
+
}): Promise<StripeListResult<StripeCharge>>;
|
|
77
|
+
getCharge(chargeId: string): Promise<StripeCharge>;
|
|
78
|
+
listCustomers(params?: {
|
|
79
|
+
limit?: number;
|
|
80
|
+
email?: string;
|
|
81
|
+
}): Promise<StripeListResult<StripeCustomer>>;
|
|
82
|
+
getCustomer(customerId: string): Promise<StripeCustomer>;
|
|
83
|
+
listSubscriptions(params?: {
|
|
84
|
+
limit?: number;
|
|
85
|
+
customerId?: string;
|
|
86
|
+
status?: string;
|
|
87
|
+
}): Promise<StripeListResult<StripeSubscription>>;
|
|
88
|
+
listInvoices(params?: {
|
|
89
|
+
limit?: number;
|
|
90
|
+
customerId?: string;
|
|
91
|
+
status?: string;
|
|
92
|
+
}): Promise<StripeListResult<StripeInvoice>>;
|
|
93
|
+
private buildHeaders;
|
|
94
|
+
}
|
|
95
|
+
export declare function loadTokens(): StripeTokens | null;
|
|
96
|
+
export declare function saveTokens(tokens: StripeTokens): void;
|
|
97
|
+
export declare function clearTokens(): void;
|
|
98
|
+
export declare function getStripeConnector(): StripeConnector;
|
|
99
|
+
export { getStripeConnector as stripe };
|
|
100
|
+
export interface ConnectorHandlerResult {
|
|
101
|
+
status: number;
|
|
102
|
+
body: string;
|
|
103
|
+
contentType?: string;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* POST /connections/stripe/connect { secretKey }
|
|
107
|
+
*/
|
|
108
|
+
export declare function handleStripeConnect(body: string): Promise<ConnectorHandlerResult>;
|
|
109
|
+
/**
|
|
110
|
+
* POST /connections/stripe/test
|
|
111
|
+
*/
|
|
112
|
+
export declare function handleStripeTest(): Promise<ConnectorHandlerResult>;
|
|
113
|
+
/**
|
|
114
|
+
* DELETE /connections/stripe
|
|
115
|
+
*/
|
|
116
|
+
export declare function handleStripeDisconnect(): ConnectorHandlerResult;
|