Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Add slack-drafts skill: create Slack drafts as the user via their Slack OAuth (#279)

Joshua France005b9b0b7b00
diff --git a/skills-seed/slack-drafts/SKILL.md b/skills-seed/slack-drafts/SKILL.md
new file mode 100644
index 000000000..72111c0a0
--- /dev/null
+++ b/skills-seed/slack-drafts/SKILL.md
@@ -0,0 +1,46 @@
+---
+name: slack-drafts
+description: Create Slack message drafts in the user's own composer, as the user, through their per-user Slack OAuth. Drafting only — the user reviews and sends from Slack.
+requiredCapabilities:
+ - egress:slack.com
+---
+
+# Slack drafts
+
+Use this skill when the user wants a Slack message prepared for them to review and
+send themselves: "draft a reply to X", "prep a message for #general", "write that up
+as a Slack draft". The draft lands in their own Slack composer, attributed to them,
+targeted at the conversation you choose — nothing is posted.
+
+This is an OAuth connector. The resolved user's Slack user token lives on your
+computer as `$VAULT_TOKEN_SLACK_COM`. Do not ask the user for a token, log it, or
+use another principal's. If the variable is empty or Slack returns 401/403, the user
+either has not connected Slack or connected before this permission existed — tell
+them to (re)connect it through the product OAuth flow.
+
+Use the bundled helper for every draft — it owns destination resolution, rich-text
+block construction, and the required draft envelope:
+
+```bash
+python3 skills/slack-drafts/scripts/slack_drafts.py create --to '#general' --text 'hello'
+python3 skills/slack-drafts/scripts/slack_drafts.py create --to '@jane' --text-file body.txt
+python3 skills/slack-drafts/scripts/slack_drafts.py create --to C0123ABCDEF --thread-ts 1712345678.000100 --text '...'
+python3 skills/slack-drafts/scripts/slack_drafts.py resolve --to '#general'
+```
+
+- `--to` takes a channel/IM ID (`C…`/`G…`/`D…`), a user ID (`U…`/`W…`), `#channel-name`,
+ or `@name` (matched against username, display name, real name, or email; ambiguous
+ matches are refused — fall back to a user ID).
+- Text is plain; URLs become clickable links. Write it exactly as the user would send
+ it — it goes out under their name, in their voice, once they hit send.
+- `--thread-ts` targets the draft at a thread reply instead of the channel composer.
+- Drafts are create-only through this API: you cannot list, edit, or delete a draft
+ once made. To revise, tell the user to discard the old draft in Slack and create a
+ fresh one. Never work around this by posting messages — drafting means the user
+ sends.
+- Opening a brand-new DM needs the `im:write` scope; tokens connected before that
+ scope existed can only draft into conversations that already exist. On a
+ `missing_scope` error, tell the user to reconnect Slack.
+- Slack keeps at most one draft per composer: a second draft into the same channel or
+ thread fails with `attached_draft_exists`. Tell the user to send or discard the
+ existing draft, then retry.
diff --git a/skills-seed/slack-drafts/scripts/slack_drafts.py b/skills-seed/slack-drafts/scripts/slack_drafts.py
new file mode 100755
index 000000000..982bc8b4e
--- /dev/null
+++ b/skills-seed/slack-drafts/scripts/slack_drafts.py
@@ -0,0 +1,187 @@
+#!/usr/bin/env python3
+"""Slack draft helper: creates a draft in the user's Slack composer, as the user.
+
+Usage (token from $VAULT_TOKEN_SLACK_COM):
+ slack_drafts.py create --to '#channel-name' --text 'hello'
+ slack_drafts.py create --to '@display-name' --text-file body.txt
+ slack_drafts.py create --to C0123ABCDEF --thread-ts 1712345678.000100 --text 'reply'
+ slack_drafts.py resolve --to '#channel-name'
+
+Only drafts.create accepts an OAuth user token; drafts cannot be listed, edited,
+or deleted through this API — the person manages the draft in their Slack client.
+"""
+
+import argparse
+import json
+import os
+import re
+import subprocess
+import sys
+import urllib.parse
+import uuid
+
+API = "https://slack.com/api"
+
+
+def call(method: str, body: dict | None = None, query: dict | None = None):
+ tok = os.environ.get("VAULT_TOKEN_SLACK_COM", "")
+ if not tok:
+ sys.exit("no Slack token: ask the user to connect Slack")
+ url = f"{API}/{method}" + (f"?{urllib.parse.urlencode(query, doseq=True)}" if query else "")
+ cmd = ["curl", "-sS", "--fail-with-body", "--max-time", "60",
+ "-H", f"Authorization: Bearer {tok}", url]
+ if body is not None:
+ cmd += ["-H", "Content-Type: application/json; charset=utf-8", "--data-binary", "@-"]
+ proc = subprocess.run(cmd, input=json.dumps(body) if body is not None else None,
+ capture_output=True, text=True)
+ if proc.returncode != 0:
+ sys.exit(f"slack api unreachable on {method}: {proc.stderr.strip()[:300]}")
+ try:
+ payload = json.loads(proc.stdout)
+ except ValueError:
+ sys.exit(f"slack api returned non-JSON on {method}: {proc.stdout[:300]}")
+ if not payload.get("ok"):
+ err = payload.get("error")
+ hints = {
+ "missing_scope": "the user connected Slack before this permission existed — ask them to reconnect Slack",
+ "attached_draft_exists": "that conversation's composer already holds a draft — ask the user to send or discard it first",
+ }
+ detail = "; ".join(payload.get("response_metadata", {}).get("messages", []))
+ parts = [p for p in (detail, hints.get(str(err))) if p]
+ sys.exit(f"slack api error on {method}: {err}{' — ' + '; '.join(parts) if parts else ''}")
+ return payload
+
+
+def paginate(method: str, query: dict, key: str):
+ cursor = None
+ while True:
+ q = dict(query)
+ if cursor:
+ q["cursor"] = cursor
+ payload = call(method, query=q)
+ yield from payload.get(key, [])
+ cursor = payload.get("response_metadata", {}).get("next_cursor") or None
+ if not cursor:
+ return
+
+
+URL_RE = re.compile(r"https?://[^\s<>|]+")
+
+
+def text_to_blocks(text: str) -> list:
+ elements = []
+ last = 0
+ for m in URL_RE.finditer(text):
+ if m.start() > last:
+ elements.append({"type": "text", "text": text[last:m.start()]})
+ elements.append({"type": "link", "url": m.group(0)})
+ last = m.end()
+ if last < len(text):
+ elements.append({"type": "text", "text": text[last:]})
+ if not elements:
+ sys.exit("draft text is empty")
+ return [{"type": "rich_text", "elements": [{"type": "rich_text_section", "elements": elements}]}]
+
+
+def find_channel(name: str) -> str:
+ want = name.lstrip("#").lower()
+ for ch in paginate(
+ "conversations.list",
+ {"types": "public_channel,private_channel,mpim", "exclude_archived": "true", "limit": 200},
+ "channels",
+ ):
+ if str(ch.get("name", "")).lower() == want:
+ return ch["id"]
+ sys.exit(f"no channel named #{want} visible to this user")
+
+
+def find_user(name: str) -> str:
+ want = name.lstrip("@").lower()
+ matches = []
+ for u in paginate("users.list", {"limit": 200}, "members"):
+ if u.get("deleted"):
+ continue
+ profile = u.get("profile", {})
+ names = {
+ str(u.get("name", "")).lower(),
+ str(profile.get("display_name", "")).lower(),
+ str(profile.get("real_name", "")).lower(),
+ str(profile.get("email", "")).lower(),
+ }
+ if want in names:
+ matches.append(u["id"])
+ if len(matches) == 1:
+ return matches[0]
+ if not matches:
+ sys.exit(f"no Slack user matching '{name}'")
+ sys.exit(f"'{name}' is ambiguous ({len(matches)} users) — use a user ID")
+
+
+def open_im(user_id: str) -> str:
+ payload = call("conversations.open", body={"users": user_id})
+ return payload["channel"]["id"]
+
+
+def resolve_destination(to: str) -> str:
+ if re.fullmatch(r"[CGD][A-Z0-9]{8,}", to):
+ return to
+ if re.fullmatch(r"[UW][A-Z0-9]{8,}", to):
+ return open_im(to)
+ if to.startswith("#"):
+ return find_channel(to)
+ if to.startswith("@") or "@" in to:
+ return open_im(find_user(to))
+ return find_channel(to)
+
+
+def read_text(args) -> str:
+ if args.text is not None:
+ return args.text
+ if args.text_file == "-":
+ return sys.stdin.read().rstrip("\n")
+ with open(args.text_file, encoding="utf-8") as f:
+ return f.read().rstrip("\n")
+
+
+def cmd_create(args):
+ channel = resolve_destination(args.to)
+ destination = {"channel_id": channel}
+ if args.thread_ts:
+ destination["thread_ts"] = args.thread_ts
+ payload = call(
+ "drafts.create",
+ body={
+ "blocks": text_to_blocks(read_text(args)),
+ "destinations": [destination],
+ "file_ids": [],
+ "is_from_composer": False,
+ "client_msg_id": str(uuid.uuid4()),
+ },
+ )
+ draft = payload.get("draft", {})
+ print(json.dumps({"ok": True, "draftId": draft.get("id"), "channel": channel}))
+
+
+def cmd_resolve(args):
+ print(json.dumps({"channel": resolve_destination(args.to)}))
+
+
+def main():
+ parser = argparse.ArgumentParser(description=__doc__)
+ sub = parser.add_subparsers(dest="cmd", required=True)
+ create = sub.add_parser("create")
+ create.add_argument("--to", required=True)
+ group = create.add_mutually_exclusive_group(required=True)
+ group.add_argument("--text")
+ group.add_argument("--text-file")
+ create.add_argument("--thread-ts")
+ create.set_defaults(fn=cmd_create)
+ resolve = sub.add_parser("resolve")
+ resolve.add_argument("--to", required=True)
+ resolve.set_defaults(fn=cmd_resolve)
+ args = parser.parse_args()
+ args.fn(args)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/src/connectors/oauth.ts b/src/connectors/oauth.ts
index db7939590..a747e8470 100644
--- a/src/connectors/oauth.ts
+++ b/src/connectors/oauth.ts
@@ -273,8 +273,10 @@ export const PROVIDERS: Record<string, OAuthProviderConfig> = {
"groups:read",
"groups:history",
"im:read",
+ "im:write",
"im:history",
"mpim:read",
+ "mpim:write",
"mpim:history",
"chat:write",
"canvases:read",
diff --git a/src/core/orchestrator/turn-helpers.ts b/src/core/orchestrator/turn-helpers.ts
index 452848b68..cb16051ad 100644
--- a/src/core/orchestrator/turn-helpers.ts
+++ b/src/core/orchestrator/turn-helpers.ts
@@ -107,6 +107,7 @@ const CONNECTOR_SKILL_PROVIDERS: Readonly<Record<string, string>> = {
"google-workspace": "google",
linear: "linear",
"morning-digest": "x",
+ "slack-drafts": "slack",
x: "x",
};
diff --git a/test/connector-skill-filter.test.ts b/test/connector-skill-filter.test.ts
index d1b2ca11d..9a26b4a8d 100644
--- a/test/connector-skill-filter.test.ts
+++ b/test/connector-skill-filter.test.ts
@@ -24,7 +24,14 @@ function skill(name: string): SkillResolution {
}
test("provider skills are visible only for connectors configured by the admin", () => {
- const all = [skill("memory"), skill("google-workspace"), skill("dropbox"), skill("linear"), skill("x")];
+ const all = [
+ skill("memory"),
+ skill("google-workspace"),
+ skill("dropbox"),
+ skill("linear"),
+ skill("x"),
+ skill("slack-drafts"),
+ ];
assert.deepEqual(
filterConnectorSkills(all, []).map((entry) => entry.skill?.manifest.name),
["memory"],
@@ -33,4 +40,8 @@ test("provider skills are visible only for connectors configured by the admin",
filterConnectorSkills(all, ["google", "linear"]).map((entry) => entry.skill?.manifest.name),
["memory", "google-workspace", "linear"],
);
+ assert.deepEqual(
+ filterConnectorSkills(all, ["slack"]).map((entry) => entry.skill?.manifest.name),
+ ["memory", "slack-drafts"],
+ );
});