civitai
A repository of models, textual inversions, and more
Safety scan
A static text scan, not a guarantee. Select the badge to see what was scanned and every finding.
Check it deeper on your machine
It has changed since its scan on 11 Nov 2026.
As a Claude Code plugin, install it and then ask Claude Code:
/plugin marketplace add keithmackay/mackayi/plugin install scanskillsafety@mackayiDo a deep scan of https://github.com/civitai/civitai with scanskillsafety
Or from a terminal, clone it and run it:
git clone https://github.com/keithmackay/scanskillsafety-skill ~/.claude/skills/scanskillsafetynode ~/.claude/skills/scanskillsafety/cli.cjs --deep 'https://github.com/civitai/civitai'
It checks the exact commit: every file, its packages against OSV (api.osv.dev), how it compares with this page's scan (findsafeskills.com receives the repository's address), and, if you agree, Claude reading every file with your own Claude subscription. It never runs the code.
Badge for your README
Markdown
[](/listings/civitai--civitai)HTML
<a href="/listings/civitai--civitai"><img src="/api/listings/civitai--civitai/badge.svg" alt="findsafeskills safety rating" /></a>Install
Install instructions from the README
As written by the repo's author. findsafeskills has not verified these steps.
<details open> <summary>To get a local copy up and running, follow these steps.</summary>
Prerequisites
- [Docker](https://www.docker.com/), with Compose v2 (`docker compose`, not the retired hyphenated `docker-compose`). The database, Redis, MinIO, Meilisearch, ClickHouse and the mail catcher all run as containers. - **Node.js `24.22.1`.** Not "20 or later" — `package.json` declares `engines.node: ">=24.0.0 <25"`. The exact version lives in [`.nvmrc`](.nvmrc); CI installs that file's version and the production image is built on the same one, so `nvm use` (or any tool that reads `.nvmrc`) is the right way to get it. Note that nothing stops you: `pnpm install` only prints `WARN Unsupported engine` and carries on, so the wrong major surfaces later as odd test failures rather than as a refusal at install time. - **pnpm.** This repo is pnpm-only, and this one *is* enforced — `npm install` exits 1 via the `preinstall` `only-allow pnpm` hook. `corepack enable` will pick up the `packageManager` field for you. - Make (optional).
Installation
Standard setup
git clone https://github.com/civitai/civitai.git cd civitai nvm use # reads .nvmrc -> 24.22.1 corepack enable git submodule update --init event-engine-common cp .env-example .env.development docker compose -f docker-compose.base.yml up -d pnpm install pnpm dev
Optional: Nix flake
> **Optional, and not the supported default.** The standard setup above is what the > project expects and what CI builds; nothing in the repo requires Nix, and you can > ignore this section entirely. It exists because NixOS cannot use Prisma's published > engines (there is no `linux-nixos` build), so a flake is the practical way to work on > this repo there. If you are not on NixOS and not already a flakes user, skip it.
The flake owns the toolchain, so you do not install Node or pnpm yourself:
git clone https://github.com/civitai/civitai.git cd civitai nix run .#dev
That single command checks Docker is usable, checks out the `event-engine-common` submodule, creates `.env.development` from `.env-example` if you do not already have one, starts the container stack, waits for Postgres, runs `pnpm install`, and then starts the dev server on [http://localhost:3000](http://localhost:3000). Every step is idempotent — it is safe to re-run in a checkout that already works, and it will not overwrite your `.env.development` or touch your data.
Useful variants:
nix run .#dev -- --no-start # bootstrap only, leave the services running nix run .#dev -- --full # also start the signals/buzz containers (see below) nix run .#doctor # check the flake's pins against the repo nix flake check # the same checks, plus their own self-test
For an interactive shell with the same toolchain, use `nix develop`, or copy [`.envrc.example`](.envrc.example) to `.envrc` and run `direnv allow` to get it automatically on `cd`.
With devcontainers
> ⚠️ **Known out of step:** `.devcontainer/public/docker-compose.yml` pins > `mcr.microsoft.com/devcontainers/typescript-node:1-22`, i.e. Node 22, which is > outside this repo's `engines.node` range. `pnpm install` will warn rather than > stop, so the container comes up and then misbehaves in ways that look like your > branch. There is no `1-24` tag (the template major moved on); `3-24` is the > closest equivalent. Not changed here because it could not be exercised.
> ⚠️ Important Warning for Windows Users: Either clone this repo onto a WSL volume, or use the "clone repository in named container volume" command. Otherwise, you will see performance issues.
- Open the directory up in your IDE of choice - VS Code should prompt you to "Open in container" - If not, you may need to manually run `Dev Containers: Open Folder in Container`
What was scanned
The files the safety scan read, as written by the repository's author and shown as plain text. Lines with a finding are marked.
README.md · README · 260 lines
View this file on GitHub (read at the repository's current version: the scan did not record which commit it read)
This file has changed since it was scanned, so this is not exactly the text that was scanned, and no line is marked. The findings are listed with the safety scan above.
- [![Contributors][contributors-shield]][contributors-url]
- [![Forks][forks-shield]][forks-url]
- [![Stargazers][stars-shield]][stars-url]
- [![Issues][issues-shield]][issues-url]
- [![Apache License 2.0][license-shield]][license-url]
- [![Discord][discord-shield]][discord-url]
- <br />
- <div align="center">
- <a href="https://civitai.com/">
- <img src="media/logo.png" alt="Civitai Logo" width="120" height="auto">
- </a>
- </div>
- ## Table of Contents
- - [Table of Contents](#table-of-contents)
- - [About the Project](#about-the-project)
- - [Tech Stack](#tech-stack)
- - [Getting Started](#getting-started)
- - [Prerequisites](#prerequisites)
- - [Installation](#installation)
- - [Altering your user](#altering-your-user)
- - [Known limitations](#known-limitations)
- - [Contributing](#contributing)
- - [Data Migrations](#data-migrations)
- - [Sponsors](#sponsors)
- - [License](#license)
- ## About the Project
- Our goal with this project is to create a platform where people can share their stable diffusion models (textual inversions, hypernetworks, aesthetic
- gradients, VAEs, and any other crazy stuff people do to customize their AI generations), collaborate with others to improve them, and learn from each
- other's work. The platform allows users to create an account, upload their models, and browse models that have been shared by others. Users can also
- leave comments and feedback on each other's models to facilitate collaboration and knowledge sharing.
- ### Tech Stack
- We've built this project using a combination of modern web technologies, including Next.js for the frontend, TRPC for the API, and Prisma + Postgres
- for the database. By leveraging these tools, we've been able to create a scalable and maintainable platform that is both user-friendly and powerful.
- - **DB:** Prisma + Postgres
- - **API:** tRPC
- - **Front-end + Back-end:** NextJS
- - **UI Kit:** [Mantine](https://mantine.dev/)
- - **Storage:** Cloudflare
- ## Getting Started
- <details open>
- <summary>To get a local copy up and running, follow these steps.</summary>
- ### Prerequisites
- - [Docker](https://www.docker.com/), with Compose v2 (`docker compose`, not the
- retired hyphenated `docker-compose`). The database, Redis, MinIO, Meilisearch,
- ClickHouse and the mail catcher all run as containers.
- - **Node.js `24.22.3`.** Not "20 or later" — `package.json` declares
- `engines.node: ">=24.0.0 <25"`. The exact version lives in [`.nvmrc`](.nvmrc);
- CI installs that file's version and the production image is built on the same
- one, so `nvm use` (or any tool that reads `.nvmrc`) is the right way to get it.
- Note that nothing stops you: `pnpm install` only prints
- `WARN Unsupported engine` and carries on, so the wrong major surfaces later as
- odd test failures rather than as a refusal at install time.
- - **pnpm.** This repo is pnpm-only, and this one *is* enforced — `npm install`
- exits 1 via the `preinstall` `only-allow pnpm` hook. `corepack enable` will
- pick up the `packageManager` field for you.
- - Make (optional).
- ### Installation
- #### Standard setup
- ```sh
- git clone https://github.com/civitai/civitai.git
- cd civitai
- nvm use # reads .nvmrc -> 24.22.3
- corepack enable
- git submodule update --init event-engine-common
- cp .env-example .env.development
- docker compose -f docker-compose.base.yml up -d
- pnpm install
- pnpm dev
- ```
- #### Optional: Nix flake
- > **Optional, and not the supported default.** The standard setup above is what the
- > project expects and what CI builds; nothing in the repo requires Nix, and you can
- > ignore this section entirely. It exists because NixOS cannot use Prisma's published
- > engines (there is no `linux-nixos` build), so a flake is the practical way to work on
- > this repo there. If you are not on NixOS and not already a flakes user, skip it.
- The flake owns the toolchain, so you do not install Node or pnpm yourself:
- ```sh
- git clone https://github.com/civitai/civitai.git
- cd civitai
- nix run .#dev
- ```
- That single command checks Docker is usable, checks out the
- `event-engine-common` submodule, creates `.env.development` from `.env-example`
- if you do not already have one, starts the container stack, waits for Postgres,
- runs `pnpm install`, and then starts the dev server on
- [http://localhost:3000](http://localhost:3000). Every step is idempotent — it is
- safe to re-run in a checkout that already works, and it will not overwrite your
- `.env.development` or touch your data.
- Useful variants:
- ```sh
- nix run .#dev -- --no-start # bootstrap only, leave the services running
- nix run .#dev -- --full # also start the signals/buzz containers (see below)
- nix run .#doctor # check the flake's pins against the repo
- nix flake check # the same checks, plus their own self-test
- ```
- For an interactive shell with the same toolchain, use `nix develop`, or copy
- [`.envrc.example`](.envrc.example) to `.envrc` and run `direnv allow` to get it
- automatically on `cd`.
- #### With devcontainers
- > ⚠️ **Known out of step:** `.devcontainer/public/docker-compose.yml` pins
- > `mcr.microsoft.com/devcontainers/typescript-node:1-22`, i.e. Node 22, which is
- > outside this repo's `engines.node` range. `pnpm install` will warn rather than
- > stop, so the container comes up and then misbehaves in ways that look like your
- > branch. There is no `1-24` tag (the template major moved on); `3-24` is the
- > closest equivalent. Not changed here because it could not be exercised.
- > ⚠️ Important Warning for Windows Users: Either clone this repo onto a WSL volume, or use the "clone repository in named container volume"
- command. Otherwise, you will see performance issues.
- - Open the directory up in your IDE of choice
- - VS Code should prompt you to "Open in container"
- - If not, you may need to manually run `Dev Containers: Open Folder in Container`
- - For other IDEs, you may need to open the `.devcontainer/devcontainer.json` file, and click "Create devcontainer and mount sources"
- - _Note: this may take some time to run initially_
- - Run `make run`
- #### The signals and buzz services
- `docker-compose.base.yml` holds everything a contributor needs (and is also what
- `nix run .#dev` starts). The extra services in `docker-compose.yml`
- (signals, buzz) come from private `ghcr.io` images, so they only work for
- internal members:
- - create a GitHub personal access token with `read:packages`
- - set it as `CR_PAT`
- - `echo $CR_PAT | docker login ghcr.io -u USERNAME --password-stdin`
- - then `docker compose up -d` (or, with the flake, `nix run .#dev -- --full`)
- ### After the first start
- 1. Edit `.env.development`. Most defaults work out of the box; these do not:
- - **S3 upload credentials.** Open the MinIO console at
- [http://localhost:9001](http://localhost:9001) (username and password both
- `minioadmin`) — note it is port **9001**, port 9000 is the S3 API itself —
- go to "Access Keys", click "Create Access Key", and copy the key and secret
- into `S3_UPLOAD_KEY` / `S3_UPLOAD_SECRET` and `S3_IMAGE_UPLOAD_KEY` /
- `S3_IMAGE_UPLOAD_SECRET`.
- - `WEBHOOK_TOKEN` — any random string; it authenticates requests to the
- webhook endpoint.
- 2. On an empty database, populate it. These are slow and destructive, which is
- why no bootstrap runs them for you:
- ```sh
- make run-migrations
- make reseed
- ```
- 3. Visit [http://localhost:3000](http://localhost:3000).
- Please report any issues with these commands to us on [discord][discord-url].
- _* Note that account creation will run emails through Mailpit, which can be accessed at [http://localhost:1080](http://localhost:1080)._
- ### Altering your user
- - First, create an account for yourself as you normally would through the UI.
- - You may wish to set yourself up as a moderator. To do so:
- - Use a database editor (like [DataGrip](https://www.jetbrains.com/datagrip/)) or connect directly to the
- DB (`PGPASSWORD=postgres psql -h localhost -p 15432 -U postgres civitai`)
- - Find your user (by email or username), and change `isModerator` to `true`
- ### Known limitations
- Services that require external input will currently not work locally. These include:
- - Orchestration (Generation, Training)
- - Signals (Chat, Notifications, other real-time updates)
- - Buzz
- </details>
- ## Contributing
- Any contributions you make are **greatly appreciated**.
- If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
- Don't forget to give the project a star! Thanks again!
- 1. Fork the repository to your own GitHub account.
- 2. Create a new branch for your changes.
- 3. Make your changes to the code.
- 4. Commit your changes and push the branch to your forked repository.
- 5. Open a pull request on our repository.
- If you would like to be more involved, consider joining the **Community Development Team**! For more information on the team as well as how to join,
- see [Calling All Developers: Join Civitai's Community Development Team](https://civitai.com/articles/7782).
- ### Data Migrations
- Over the course of development, you may need to change the structure of the database. To do this:
- 1. Make your changes to the `packages/civitai-db-schema/prisma/schema.full.prisma` file.
- **Not `schema.prisma`** — that one is gitignored and regenerated from
- `schema.full.prisma` by `scripts/generate-slim-schema.js` on every
- `pnpm run db:generate`, so edits to it are silently overwritten.
- 2. Run `pnpm run db:migrate:empty "brief description here"`. This creates
- `packages/civitai-db-schema/prisma/migrations/YYYYMMDDHHmmss_brief_description_here/migration.sql`
- for you, in the one directory Prisma reads.
- To create it by hand instead, use that same path — **not** the `prisma/migrations`
- directory at the repo root, which predates the monorepo layout and is no longer read.
- 3. Put your sql changes in the generated `migration.sql`
- - These are usually simple sql commands like `ALTER TABLE ...`
- 4. Run `make run-migrations` and `make gen-prisma`
- 5. If you are adding/changing a column or table, please try to keep the `gen_seed.ts` file up to date with these changes.
- ## Sponsors
- Support this project by becoming a sponsor. Your logo will show up here with a link to your website.
- ## License
- Apache License 2.0 - Please have a look at the [LICENSE](/LICENSE) for more details.
- [contributors-shield]: https://img.shields.io/github/contributors/civitai/civitai.svg?style=for-the-badge
- [contributors-url]: https://github.com/civitai/civitai/graphs/contributors
- [forks-shield]: https://img.shields.io/github/forks/civitai/civitai.svg?style=for-the-badge
- [forks-url]: https://github.com/civitai/civitai/network/members
- [stars-shield]: https://img.shields.io/github/stars/civitai/civitai.svg?style=for-the-badge
- [stars-url]: https://github.com/civitai/civitai/stargazers
- [issues-shield]: https://img.shields.io/github/issues/civitai/civitai.svg?style=for-the-badge
- [issues-url]: https://github.com/civitai/civitai/issues
- [license-shield]: https://img.shields.io/github/license/civitai/civitai.svg?style=for-the-badge
- [license-url]: https://github.com/civitai/civitai/blob/master/LICENSE
- [discord-shield]: https://img.shields.io/discord/1037799583784370196?style=for-the-badge
- [discord-url]: https://discord.gg/UwX5wKwm6c
.claude/skills/clickup/listen.mjs · Bundled script · 430 lines · 13 findings marked
- #!/usr/bin/env node
- /**
- * ClickUp Event Listener (webhook.site + local receiver)
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 4 on GitHub
- *
- * Receives ClickUp webhook events via webhook.site forwarding.
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 6 on GitHub
- * Designed to be run as a background task so the agent gets notified
- * when a watched event arrives (task status change, comment, etc.).
- *
- * KEY FEATURE: On startup, checks webhook.site API for events that
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 10 on GitHub
- * arrived since last seen timestamp. Prevents gaps between restarts.
- *
- * Modes:
- * 1. "wait" (default) — Check for missed events, then listen for ONE
- * new webhook, print it, and exit. Agent gets background task notification.
- *
- * 2. "server" — Stay running and log all events.
- *
- * Usage:
- * node listen.mjs [--since <ISO timestamp>] [--port 3458] [--timeout 600] [--mode wait|server]
- *
- * Configuration (accounts.json > webhookSiteToken):
- * Or environment: CLICKUP_WEBHOOK_SITE_TOKEN
- *
- * Exit codes:
- * 0 — webhook received (wait mode)
- * 1 — timeout or error
- */
- import { spawn } from 'child_process';
- import http from 'http';
- import https from 'https';
- import crypto from 'crypto';
- import fs from 'fs';
- import path from 'path';
- import { fileURLToPath } from 'url';
- const __dirname = path.dirname(fileURLToPath(import.meta.url));
- const LOG_FILE = path.join(__dirname, 'webhooks.jsonl');
- const LATEST_FILE = path.join(__dirname, 'webhook-latest.json');
- const WEBHOOK_URL_FILE = path.join(__dirname, 'webhook-url.txt');
- const LAST_SEEN_FILE = path.join(__dirname, 'last-seen.txt');
- const WATCHERS_FILE = path.join(__dirname, 'watchers.json');
- // ── Load config ───────────────────────────────────────────────────────────
- // Try accounts.json first, then env
- function loadConfig() {
- const accountsPath = path.join(__dirname, 'accounts.json');
- if (fs.existsSync(accountsPath)) {
- try {
- const data = JSON.parse(fs.readFileSync(accountsPath, 'utf-8'));
- return {
- webhookSiteToken: data.webhookSiteToken || process.env.CLICKUP_WEBHOOK_SITE_TOKEN,
- webhookSiteApiKey: data.webhookSiteApiKey || process.env.WEBHOOK_SITE_API_KEY,
- };
- } catch {}
- }
- return {
- webhookSiteToken: process.env.CLICKUP_WEBHOOK_SITE_TOKEN,
- webhookSiteApiKey: process.env.WEBHOOK_SITE_API_KEY,
- };
- }
- const config = loadConfig();
- const WH_TOKEN = config.webhookSiteToken;
- const WH_API_KEY = config.webhookSiteApiKey;
- // ── Parse CLI args ────────────────────────────────────────────────────────
- const args = process.argv.slice(2);
- function getFlag(name) {
- const idx = args.indexOf(name);
- if (idx === -1 || idx + 1 >= args.length) return undefined;
- return args[idx + 1];
- }
- const PORT = parseInt(getFlag('--port') || '3458', 10);
- const TIMEOUT = parseInt(getFlag('--timeout') || '600', 10) * 1000;
- const MODE = getFlag('--mode') || 'wait';
- // Filters — only deliver events matching these criteria
- const FILTER_STATUS = getFlag('--filter-status'); // e.g. "qa review" — only deliver status changes TO this status
- const FILTER_EVENT = getFlag('--filter-event'); // e.g. "taskCommentPosted" — only deliver this event type
- const FILTER_TASK = getFlag('--filter-task'); // e.g. "868abc123" — only deliver events for this task
- let SINCE = getFlag('--since');
- if (!SINCE && fs.existsSync(LAST_SEEN_FILE)) {
- SINCE = fs.readFileSync(LAST_SEEN_FILE, 'utf8').trim();
- }
- if (!WH_TOKEN) {
- console.error('Error: No webhook.site token configured.');
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 94 on GitHub
- console.error('Add "webhookSiteToken" to accounts.json or set CLICKUP_WEBHOOK_SITE_TOKEN env var.');
- console.error('Visit https://webhook.site to get a token (the UUID in the URL).');
- Note: Quoted as data in code: Links to a known data-relay/testing domain (webhook.site) without sending anything to it
- Dismissed after review: Talks to a raw IP address or throwaway host, where malware is often staged
- Line 96 on GitHub
- process.exit(1);
- }
- const WEBHOOK_URL = `https://webhook.site/${WH_TOKEN}`;
- Note: Quoted as data in code: Links to a known data-relay/testing domain (webhook.site) without sending anything to it
- Note: Talks to a raw IP address or throwaway host, where malware is often staged
- Line 100 on GitHub
- // Write the webhook URL so other commands can read it
- fs.writeFileSync(WEBHOOK_URL_FILE, WEBHOOK_URL);
- // ── Load watcher secrets for signature verification ───────────────────────
- function loadWatcherSecrets() {
- if (!fs.existsSync(WATCHERS_FILE)) return {};
- try {
- const watchers = JSON.parse(fs.readFileSync(WATCHERS_FILE, 'utf-8'));
- const secrets = {};
- for (const w of watchers) {
- if (w.webhookId && w.secret) secrets[w.webhookId] = w.secret;
- }
- return secrets;
- } catch {
- return {};
- }
- }
- const WATCHER_SECRETS = loadWatcherSecrets();
- // ── Event Filtering ───────────────────────────────────────────────────────
- function matchesFilters(event) {
- // Filter by event type
- if (FILTER_EVENT) {
- const allowed = FILTER_EVENT.split(',').map(s => s.trim().toLowerCase());
- const eventType = (event.event || '').toLowerCase();
- if (!allowed.includes(eventType)) return false;
- }
- // Filter by task ID
- if (FILTER_TASK) {
- const allowed = FILTER_TASK.split(',').map(s => s.trim());
- if (!allowed.includes(event.task_id)) return false;
- }
- // Filter by target status (only deliver taskStatusUpdated where after.status matches)
- if (FILTER_STATUS) {
- const allowed = FILTER_STATUS.split(',').map(s => s.trim().toLowerCase());
- const items = event.history_items || [];
- // For status updates, check the "after" status
- const hasMatch = items.some(item => {
- const afterStatus = (item.after?.status || '').toLowerCase();
- return allowed.includes(afterStatus);
- });
- // For non-status events, FILTER_STATUS doesn't apply — let them through
- if (event.event === 'taskStatusUpdated' && !hasMatch) return false;
- }
- return true;
- }
- // ── Helpers ───────────────────────────────────────────────────────────────
- function verifySignature(rawBody, signature, webhookId) {
- if (!signature) return null;
- // Try the specific webhook's secret first
- const secret = WATCHER_SECRETS[webhookId];
- if (!secret) return null;
- const hash = crypto
- .createHmac('sha256', secret)
- .update(rawBody)
- .digest('hex');
- return hash === signature;
- }
- function saveLastSeen(timestamp) {
- fs.writeFileSync(LAST_SEEN_FILE, timestamp);
- }
- function processEvent(event, source = 'live', verified = 'skipped') {
- const entry = {
- received_at: new Date().toISOString(),
- source,
- verified,
- ...event,
- };
- fs.appendFileSync(LOG_FILE, JSON.stringify(entry) + '\n');
- fs.writeFileSync(LATEST_FILE, JSON.stringify(entry, null, 2));
- if (entry._wh_created_at) {
- saveLastSeen(entry._wh_created_at);
- } else {
- saveLastSeen(entry.received_at);
- }
- return entry;
- }
- function formatEventSummary(event) {
- const type = event.event || 'unknown';
- const taskId = event.task_id || '?';
- const webhookId = event.webhook_id || '?';
- const items = event.history_items || [];
- let detail = '';
- if (type === 'taskStatusUpdated' && items.length > 0) {
- const item = items[0];
- detail = ` | ${item.before?.status || '?'} → ${item.after?.status || '?'}`;
- } else if (type === 'taskCommentPosted' && items.length > 0) {
- const user = items[0]?.user?.username || '?';
- detail = ` | by ${user}`;
- } else if (items.length > 0) {
- const user = items[0]?.user?.username;
- if (user) detail = ` | by ${user}`;
- }
- return `${type} | task:${taskId} | wh:${webhookId}${detail}`;
- }
- // ── Catch-up: query webhook.site for missed events ────────────────────────
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 215 on GitHub
- function fetchMissedEvents(since) {
- return new Promise((resolve, reject) => {
- const sinceUtc = since.includes('Z') || since.includes('+') ? since : since.replace(' ', 'T') + 'Z';
- const d = new Date(sinceUtc);
- if (d.getMilliseconds() > 0) {
- d.setSeconds(d.getSeconds() + 1);
- d.setMilliseconds(0);
- }
- d.setSeconds(d.getSeconds() + 1);
- const dateFrom = d.toISOString().replace('T', ' ').replace(/\.\d+Z$/, '');
- const url = new URL(`https://webhook.site/token/${WH_TOKEN}/requests`);
- Note: Quoted as data in code: Links to a known data-relay/testing domain (webhook.site) without sending anything to it
- Note: Talks to a raw IP address or throwaway host, where malware is often staged
- Line 228 on GitHub
- url.searchParams.set('date_from', dateFrom);
- url.searchParams.set('sorting', 'oldest');
- url.searchParams.set('per_page', '10');
- const headers = {};
- if (WH_API_KEY) headers['Api-Key'] = WH_API_KEY;
- https.get(url.toString(), { headers }, (res) => {
- let data = '';
- res.on('data', (chunk) => (data += chunk));
- res.on('end', () => {
- try {
- const json = JSON.parse(data);
- resolve(json);
- } catch {
- resolve({ data: [] });
- }
- });
- }).on('error', (err) => {
- process.stderr.write(`[catch-up] Failed to query webhook.site: ${err.message}\n`);
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 248 on GitHub
- resolve({ data: [] });
- });
- });
- }
- function parseWebhookSiteRequest(req) {
- try {
- const event = JSON.parse(req.content);
- event._wh_created_at = req.created_at;
- return event;
- } catch {
- return null;
- }
- }
- // ── Local HTTP server ─────────────────────────────────────────────────────
- let serverResolve;
- const server = http.createServer((req, res) => {
- if (req.method === 'GET') {
- res.writeHead(200, { 'Content-Type': 'application/json' });
- res.end(JSON.stringify({ status: 'listening', webhook_url: WEBHOOK_URL }));
- return;
- }
- if (req.method === 'POST') {
- let rawBody = '';
- req.on('data', (chunk) => (rawBody += chunk));
- req.on('end', () => {
- try {
- const event = JSON.parse(rawBody);
- const signature = req.headers['x-signature'];
- const webhookId = event.webhook_id;
- const verified = verifySignature(rawBody, signature, webhookId);
- const entry = {
- received_at: new Date().toISOString(),
- source: 'live',
- verified: verified === null ? 'skipped' : verified,
- ...event,
- };
- // Always ack the webhook to prevent ClickUp from marking it unhealthy
- res.writeHead(200, { 'Content-Type': 'application/json' });
- res.end(JSON.stringify({ ok: true }));
- // Always log and update cursor
- fs.appendFileSync(LOG_FILE, JSON.stringify(entry) + '\n');
- saveLastSeen(entry.received_at);
- // Check filters — only deliver to agent if event matches
- if (!matchesFilters(event)) {
- process.stderr.write(`[filtered] ${formatEventSummary(event)}\n`);
- return;
- }
- fs.writeFileSync(LATEST_FILE, JSON.stringify(entry, null, 2));
- if (MODE === 'server') {
- process.stderr.write(`[${entry.received_at}] ${formatEventSummary(event)}\n`);
- } else {
- console.log(JSON.stringify(entry, null, 2));
- if (serverResolve) serverResolve();
- }
- } catch (err) {
- res.writeHead(400, { 'Content-Type': 'application/json' });
- res.end(JSON.stringify({ error: 'Invalid JSON' }));
- }
- });
- return;
- }
- res.writeHead(404);
- res.end('Not found');
- });
- // ── whcli forward process ─────────────────────────────────────────────────
- function startForwarder() {
- const whArgs = [
- 'forward',
- `--token=${WH_TOKEN}`,
- `--target=http://localhost:${PORT}`,
- '--listen-timeout=5',
- ];
- if (WH_API_KEY) whArgs.push(`--api-key=${WH_API_KEY}`);
- const proc = spawn('whcli', whArgs, {
- stdio: ['pipe', 'pipe', 'pipe'],
- shell: true,
- });
- proc.stdout.on('data', (d) => process.stderr.write(`[whcli] ${d}`));
- proc.stderr.on('data', (d) => process.stderr.write(`[whcli:err] ${d}`));
- proc.on('error', (err) => process.stderr.write(`[whcli] Error: ${err.message}\n`));
- return proc;
- }
- // ── Main ──────────────────────────────────────────────────────────────────
- async function main() {
- process.stderr.write(`ClickUp Event Listener\n`);
- process.stderr.write(`Webhook URL: ${WEBHOOK_URL}\n`);
- process.stderr.write(`Mode: ${MODE} | Port: ${PORT} | Timeout: ${TIMEOUT / 1000}s\n`);
- process.stderr.write(`Since: ${SINCE || 'none (first run)'}\n`);
- process.stderr.write(`Tracked webhooks: ${Object.keys(WATCHER_SECRETS).length}\n`);
- if (FILTER_STATUS) process.stderr.write(`Filter status: ${FILTER_STATUS}\n`);
- if (FILTER_EVENT) process.stderr.write(`Filter event: ${FILTER_EVENT}\n`);
- if (FILTER_TASK) process.stderr.write(`Filter task: ${FILTER_TASK}\n`);
- process.stderr.write('\n');
- // ── Step 1: Check for missed events ─────────────────────────────────────
- if (SINCE && MODE === 'wait') {
- process.stderr.write(`Checking webhook.site for events since ${SINCE}...\n`);
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 364 on GitHub
- const result = await fetchMissedEvents(SINCE);
- const requests = result.data || [];
- if (requests.length > 0) {
- // Find the first missed event that passes our filters
- for (const req of requests) {
- const event = parseWebhookSiteRequest(req);
- if (!event) continue;
- // Always update cursor even for filtered events
- const entry = processEvent(event, 'catch-up');
- if (matchesFilters(event)) {
- process.stderr.write(`[catch-up] Found matching event (of ${requests.length} missed). Delivering.\n`);
- console.log(JSON.stringify(entry, null, 2));
- process.exit(0);
- } else {
- process.stderr.write(`[catch-up:filtered] ${event.event || 'unknown'} — skipped\n`);
- }
- }
- process.stderr.write(`[catch-up] ${requests.length} missed event(s), none matched filters. Starting live listener.\n`);
- } else {
- process.stderr.write(`[catch-up] No missed events. Starting live listener.\n`);
- }
- }
- // ── Step 2: Start live listener ─────────────────────────────────────────
- await new Promise((resolve) => server.listen(PORT, resolve));
- process.stderr.write(`Listening on port ${PORT}\n\n`);
- const forwarder = startForwarder();
- if (MODE === 'wait') {
- const gotWebhook = new Promise((resolve) => { serverResolve = resolve; });
- const timedOut = new Promise((_, reject) =>
- setTimeout(() => reject(new Error('timeout')), TIMEOUT)
- );
- try {
- await Promise.race([gotWebhook, timedOut]);
- forwarder.kill();
- server.close(() => process.exit(0));
- } catch {
- console.log(JSON.stringify({
- error: 'timeout',
- message: `No ClickUp event received within ${TIMEOUT / 1000}s`,
- webhook_url: WEBHOOK_URL,
- }));
- forwarder.kill();
- server.close(() => process.exit(1));
- }
- } else {
- process.stderr.write('Running in server mode. Press Ctrl+C to stop.\n\n');
- function cleanup() {
- process.stderr.write('\nShutting down...\n');
- forwarder.kill();
- server.close(() => process.exit(0));
- }
- process.on('SIGINT', cleanup);
- process.on('SIGTERM', cleanup);
- }
- }
- main();
.claude/skills/clickup/query.mjs · Bundled script · 2,389 lines · 9 findings marked
- #!/usr/bin/env node
- /**
- * ClickUp - Task and Document interaction skill
- *
- * Task Commands:
- * get <url|id> Get task details
- * comments <url|id> List task comments (--threads to expand)
- * thread <comment_id> View threaded replies on a comment
- * reply <comment_id> "msg" Reply to a comment thread
- * comment <url|id> "msg" Post a comment
- * status <url|id> "status" Update task status
- * tasks <list_id> List tasks in a list
- * me Show current user info
- * create [list_id] "title" Create a new task (list_id optional if default set)
- * my-tasks List all tasks assigned to me
- * search [query] Search tasks — requires a scope: --list, --folder, --me,
- * --assignee, --status, or --all. Text query optional.
- * find-list "name" Find lists/folders by name (fast structural walk, no task fetch)
- * assign <task> <user> Assign task to user
- * due <task> "date" Set due date
- * start <task> "date" Set start date
- * schedule <task> "start" "due" Set both start and due dates
- * rename <task> "new name" Rename a task
- * priority <task> <level> Set priority (urgent/high/normal/low)
- * subtask <task> "title" Create a subtask
- * parent <task> <parent_task> Make an existing task a subtask of another task
- * milestone <task> <on|off> Turn a task into a milestone, or back (reversible)
- * move <task> <list_id> Move task to different list
- * link <task> <url> ["desc"] Add external link reference
- * checklist <task> "item" Add checklist item
- * update-comment <comment_id> "text" Update a comment's text
- * resolve-comment <comment_id> Resolve/close a comment
- * delete-comment <comment_id> Delete a comment
- * watch <task> <user> Add a watcher to task
- * unwatch <task> <user> Remove a watcher from task
- * tag <task> "tag_name" Add a tag to task
- * remove-tag <task> "tag_name" Remove a tag from task
- * description <task> "text" Update task description (markdown supported)
- * depends <task> <other_task> Set task as waiting on another task
- * blocks <task> <other_task> Set task as blocking another task
- * task-link <task> <other> Create bidirectional link between tasks
- * archive <task> Archive a task
- * unarchive <task> Restore an archived task
- * claim <task> Link your session to a task (sets Session ID custom field)
- *
- * List Commands:
- * list <list_id> Get list details
- * lists <folder_id> List all lists in a folder
- * space-lists <space_id> List folderless lists in a space
- * create-list <space> "name" Create a new list in a space
- * update-list <list_id> Update list (--name, --content)
- * delete-list <list_id> Delete a list
- *
- * Document Commands:
- * docs ["query"] Search/list docs in workspace
- * doc <doc_id> Get doc details and page listing
- * create-doc "title" Create a new doc (--space to place in space)
- * page <doc_id> <page_id> Get page content
- * create-page <doc_id> "title" Create a new page in a doc
- * edit-page <doc_id> <page_id> Edit a page's content
- *
- * Attachment Commands:
- * attach <url|id> --file <path> Upload file attachment(s) to a task
- * fetch-image <url> Download attachment to local temp file (--output path)
- *
- * Options:
- * --json Output raw JSON
- * --subtasks Include subtasks (for get command)
- * --me Filter to tasks assigned to me (for tasks command)
- * --content Inline content (for short text)
- * --file Read content from a file path (preferred for long content)
- * --cleanup Delete --file after successful execution
- * --parent Parent page ID for create-page (creates subpage)
- */
- import { readFileSync, existsSync, unlinkSync, writeFileSync, mkdirSync, readdirSync, statSync } from 'fs';
- import { resolve, join } from 'path';
- import { tmpdir, homedir } from 'os';
- // API imports
- import { initClient, getActiveCredentials, getActiveAccountId } from './api/client.mjs';
- import { updateAccountField, listAccounts as listAccountsFn, setDefaultAccount, addAccount as addAccountFn, removeAccount as removeAccountFn } from './lib/accounts.mjs';
- import { getCurrentUser, getUserId, getTeamId, findUser } from './api/user.mjs';
- import {
- getTask,
- getTasksInList,
- getAvailableStatuses,
- updateTaskStatus,
- updateTask,
- createTask,
- createSubtask,
- setParent,
- getAncestorIds,
- findCustomItemByName,
- setCustomItemId,
- searchTasks,
- getMyTasks,
- assignTask,
- setDueDate,
- setStartDate,
- setDates,
- setPriority,
- moveTask,
- parseDateInput,
- addWatcher,
- addTag,
- addDependency,
- removeDependency,
- addTaskLink,
- removeTaskLink,
- archiveTask,
- unarchiveTask,
- removeWatcher,
- removeTag,
- setCustomField,
- findCustomFieldByName,
- } from './api/tasks.mjs';
- import { getComments, postComment, updateComment, deleteComment, getThreadedComments, replyToComment } from './api/comments.mjs';
- import { addChecklistItemToTask, getChecklists, editChecklistItem, deleteChecklistItem } from './api/checklists.mjs';
- import { addExternalLink } from './api/links.mjs';
- import { getList, createList, deleteList, updateList, getLists, getFolderlessLists, getListMembers, findListByName } from './api/lists.mjs';
- import {
- searchDocs,
- getDoc,
- createDoc,
- getDocPageListing,
- getPage,
- createPage,
- editPage,
- } from './api/docs.mjs';
- import { uploadAttachment, uploadAttachments } from './api/attachments.mjs';
- import {
- createWebhook,
- listWebhooks,
- deleteWebhook,
- addWatcherEntry,
- removeWatcherEntry,
- getWatcherEntries,
- clearAllWatcherEntries,
- DEFAULT_TASK_EVENTS,
- DEFAULT_LIST_EVENTS,
- DEFAULT_SPACE_EVENTS,
- } from './api/webhooks.mjs';
- // Lib imports
- import { executeBatchCreate } from './lib/batch-create.mjs';
- import { splitIds, isBulk, bulkExecute, formatBulkResults } from './lib/bulk.mjs';
- import { parseTaskId, parseListId, parseDocId, parsePageId, parseSpaceId } from './lib/parse.mjs';
- import {
- formatTask,
- formatTaskList,
- formatComments,
- formatThread,
- formatDoc,
- formatDocList,
- formatPage,
- formatPageList,
- formatList,
- } from './lib/format.mjs';
- // Parse arguments
- const args = process.argv.slice(2);
- let command = null;
- let targetInput = null;
- let arg2 = null;
- let arg3 = null;
- let jsonOutput = false;
- let includeSubtasks = false;
- let filterMe = false;
- let assigneeArg = null;
- let dueArg = null;
- let descriptionArg = null;
- let contentArg = null;
- let nameArg = null;
- let parentArg = null;
- let spaceArg = null;
- let fileArg = null;
- let cleanupFlag = false;
- let archiveFlag = false;
- let pageArg = null;
- let accountArg = null;
- let threadsFlag = false;
- let attachArgs = [];
- let eventsArg = null;
- let listScopeArg = null;
- let folderScopeArg = null;
- let statusScopeArg = null;
- let allScopeFlag = false;
- for (let i = 0; i < args.length; i++) {
- const arg = args[i];
- if (arg === '--json') {
- jsonOutput = true;
- } else if (arg === '--subtasks') {
- includeSubtasks = true;
- } else if (arg === '--me') {
- filterMe = true;
- } else if (arg === '--assignee' || arg === '-a') {
- assigneeArg = args[++i];
- } else if (arg === '--due' || arg === '-d') {
- dueArg = args[++i];
- } else if (arg === '--description' || arg === '--desc') {
- descriptionArg = args[++i];
- } else if (arg === '--content' || arg === '-c') {
- contentArg = args[++i];
- } else if (arg === '--file' || arg === '-f') {
- fileArg = args[++i];
- } else if (arg === '--cleanup') {
- cleanupFlag = true;
- } else if (arg === '--archive') {
- archiveFlag = true;
- } else if (arg === '--name' || arg === '-n') {
- nameArg = args[++i];
- } else if (arg === '--parent' || arg === '-p') {
- parentArg = args[++i];
- } else if (arg === '--space' || arg === '-s') {
- spaceArg = args[++i];
- } else if (arg === '--page') {
- pageArg = args[++i];
- } else if (arg === '--threads' || arg === '-t') {
- threadsFlag = true;
- } else if (arg === '--attach') {
- attachArgs.push(args[++i]);
- } else if (arg === '--events' || arg === '-e') {
- eventsArg = args[++i];
- } else if (arg === '--account') {
- accountArg = args[++i];
- } else if (arg === '--list') {
- listScopeArg = args[++i];
- } else if (arg === '--folder') {
- folderScopeArg = args[++i];
- } else if (arg === '--status') {
- statusScopeArg = args[++i];
- } else if (arg === '--all') {
- allScopeFlag = true;
- } else if (!command) {
- command = arg;
- } else if (!targetInput) {
- targetInput = arg;
- } else if (!arg2) {
- arg2 = arg;
- } else if (!arg3) {
- arg3 = arg;
- }
- }
- // Unescape literal \n and \t in text args (CLI passes them as-is)
- function unescapeText(str) {
- return str ? str.replace(/\\n/g, '\n').replace(/\\t/g, '\t') : str;
- }
- function detectClaudeSessionId() {
- if (process.env.CLAUDE_SESSION_ID) return process.env.CLAUDE_SESSION_ID;
- // Fallback: the active Claude Code session is the one currently writing to its
- // jsonl transcript. Find the most-recently-modified jsonl across every project
- // dir under ~/.claude/projects/. We can't key off process.cwd() because the
- // caller may have cd'd into a skill dir before invoking node.
- try {
- const root = join(homedir(), '.claude', 'projects');
- if (!existsSync(root)) return null;
- let freshest = null;
- for (const projectName of readdirSync(root)) {
- const projectDir = join(root, projectName);
- try {
- if (!statSync(projectDir).isDirectory()) continue;
- } catch {
- continue;
- }
- for (const f of readdirSync(projectDir)) {
- if (!f.endsWith('.jsonl')) continue;
- const mtime = statSync(join(projectDir, f)).mtimeMs;
- if (!freshest || mtime > freshest.mtime) {
- freshest = { id: f.replace(/\.jsonl$/, ''), mtime };
- }
- }
- }
- // Only trust the pick if it was written within the last 60s — otherwise
- // it's stale and could belong to any old session.
- if (!freshest || Date.now() - freshest.mtime > 60_000) return null;
- return freshest.id;
- } catch {
- return null;
- }
- }
- contentArg = unescapeText(contentArg);
- descriptionArg = unescapeText(descriptionArg);
- arg2 = unescapeText(arg2);
- // --file overrides --content: read file contents as the content/text argument
- if (fileArg) {
- const filePath = resolve(fileArg);
- if (!existsSync(filePath)) {
- console.error(`Error: File not found: ${filePath}`);
- process.exit(1);
- }
- const fileContent = readFileSync(filePath, 'utf-8');
- contentArg = fileContent;
- // Also set arg2 so commands that use positional text (comment, description) pick it up
- if (!arg2) arg2 = fileContent;
- }
- // Backward compatibility (commentText = arg2, which --file may have populated)
- const commentText = arg2;
- // Show usage
- function showUsage() {
- console.error(`Usage: node query.mjs <command> [options]
- Task Commands:
- get <url|id> Get task details
- comments <url|id> List task comments (--threads to expand)
- thread <comment_id> View threaded replies on a comment
- reply <comment_id> "msg" Reply to a comment thread
- comment <url|id> "msg" Post a comment
- status <url|id> "status" Update task status
- tasks <list_id> List tasks in a list
- me Show current user info
- create [list_id] "title" Create a new task (list_id optional if default set)
- my-tasks List all tasks assigned to me
- search [query] Search tasks — requires a scope flag (--list/--folder/--me/
- --assignee/--status) or --all. Text query optional.
- find-list "name" Find a list or folder by name (fast, structural only)
- assign <task> <user> Assign task to user
- due <task> "date" Set due date
- start <task> "date" Set start date
- schedule <task> "start" "due" Set both start and due dates
- rename <task> "new name" Rename a task
- priority <task> <level> Set priority (urgent/high/normal/low)
- subtask <task> "title" Create a subtask
- parent <task> <parent_task> Make an existing task a subtask of another task
- milestone <task> <on|off> Turn a task into a milestone, or back (reversible)
- move <task> <list_id> Move task to different list
- link <task> <url> ["desc"] Add external link reference
- checklist <task> "item" Add checklist item
- update-comment <comment_id> "text" Update a comment's text
- resolve-comment <comment_id> Resolve/close a comment
- delete-comment <comment_id> Delete a comment
- watch <task> <user> Add a watcher to task
- unwatch <task> <user> Remove a watcher from task
- tag <task> "tag_name" Add a tag to task
- remove-tag <task> "tag_name" Remove a tag from task
- description <task> "text" Update task description (markdown supported)
- depends <task> <other_task> Set task as waiting on another task
- blocks <task> <other_task> Set task as blocking another task
- task-link <task> <other> Create bidirectional link between tasks
- archive <task> Archive a task (remove from active views)
- unarchive <task> Restore an archived task
- claim <task> Link your session to this task (for resumability)
- List Commands:
- list <list_id> Get list details
- lists <folder_id> List all lists in a folder
- space-lists <space_id> List folderless lists in a space
- create-list <space> "name" Create a new list in a space (--content for description)
- update-list <list_id> Update list (--name "new name", --content "description")
- delete-list <list_id> Delete a list
- Document Commands:
- docs ["query"] Search/list docs in workspace
- doc <doc_id> Get doc details and page listing
- create-doc "title" Create a new doc (--content, --space)
- page <doc_id> <page_id> Get page content
- create-page <doc_id> "title" Add a new page to a doc (--content, --parent)
- edit-page <doc_id> <page_id> Edit a page's content (--content and/or --name)
- Attachment Commands:
- attach <url|id> Upload file(s) to a task (--attach path, repeatable)
- fetch-image <url> Download attachment to local temp file (--output path)
- Project Setup:
- batch-create --file plan.json Create multiple tasks from JSON (with deps, subtasks, assignments)
- batch-create --file plan.json --dry-run Preview without creating
- Watcher Commands (webhook-based event monitoring):
- watch-task <id>[,id2,...] Watch task(s) for events (--events to customize)
- watch-list <list_id> Watch a list for events
- watch-space <space_id> Watch a space for events
- watchers List active webhook watchers
- webhooks List all webhooks in workspace (from API)
- unwatch-webhook <wh_id> Delete a webhook watcher
- unwatch-webhook --all Delete all webhook watchers
- Listener (run as background task):
- node listen.mjs [--timeout 600] [--port 3458] [--mode wait|server]
- Account Commands:
- accounts List configured accounts
- switch-account <name> Change the default account
- add-account <name> Add a new account (--token required)
- remove-account <name> Remove an account
- Options:
- --account <name> Use a specific account for this command
- --json Output raw JSON
- --subtasks Include subtasks (for get command)
- --me Filter to tasks assigned to me (for tasks command)
- --content Inline content (markdown). For short text only.
- --file Read content from a file path (preferred for long content)
- --cleanup Delete the --file after successful execution
- --name New name for edit-page
- --parent Parent page ID for create-page (creates as subpage)
- --attach File path to upload as attachment (repeatable, for attach/comment)
- --space Space ID for create-doc (places doc in that space)
- --events Comma-separated event types for watch commands (e.g. taskStatusUpdated,taskCommentPosted)
- Bulk Operations (comma-separated IDs):
- node query.mjs get id1,id2,id3 Fetch multiple tasks at once
- node query.mjs status id1,id2 "complete" Update status on multiple tasks
- node query.mjs due id1,id2 "friday" Set due date on multiple tasks
- Examples:
- node query.mjs get 86a1b2c3d --subtasks
- node query.mjs comment 86a1b2c3d "Starting work on this"
- node query.mjs status 86a1b2c3d "in progress"
- node query.mjs tasks 901111220963 --me
- node query.mjs create 901111220963 "New feature: dark mode"
- node query.mjs create "Quick task" (uses CLICKUP_DEFAULT_LIST_ID)
- node query.mjs my-tasks
- node query.mjs search "dark mode" --list 901111220963
- node query.mjs search --me --status "in progress"
- node query.mjs search "bug" --all # full-workspace scan (slow)
- node query.mjs find-list "Red Launch" # locate a list/folder by name
- node query.mjs assign 86a1b2c3d jane
- node query.mjs due 86a1b2c3d "tomorrow"
- node query.mjs priority 86a1b2c3d high
- node query.mjs subtask 86a1b2c3d "Write unit tests"
- node query.mjs move 86a1b2c3d 901111220964
- node query.mjs link 86a1b2c3d "https://github.com/..." "PR #123"
- node query.mjs checklist 86a1b2c3d "Review code"
- node query.mjs delete-comment 90110200841741
- node query.mjs watch 86a1b2c3d john
- node query.mjs tag 86a1b2c3d "DevOps"
- node query.mjs description 86a1b2c3d "## Summary\\nThis is **bold** text"
- List Examples:
- node query.mjs list 901111220963 # Get list details
- node query.mjs create-list 20128955 "New List" # Create list in space
- node query.mjs create-list "https://app.clickup.com/.../v/s/20128955" "New List"
- node query.mjs delete-list 901111220963 # Delete a list
- Document Examples:
- node query.mjs docs # List all docs
- node query.mjs docs "API" # Search docs
- node query.mjs doc abc123 # Get doc details
- node query.mjs create-doc "Project Notes" # Create empty doc
- node query.mjs create-doc "Guide" --content "# Guide\\nContent here" # Short inline content
- node query.mjs page abc123 page456 # Get page content
- node query.mjs create-page abc123 "New Section" # Add additional page
- node query.mjs edit-page abc123 page456 --content "Updated content" --name "Renamed"
- File-based Content (recommended for anything longer than a sentence):
- node query.mjs create-doc "Spec" --file ./spec.md --space 90114072520
- node query.mjs edit-page abc123 p456 --file ./updated-content.md
- node query.mjs comment 86a1b2c3d --file ./review-notes.md
- node query.mjs description 86a1b2c3d --file ./task-description.md
- node query.mjs create-doc "Temp" --file ./draft.md --cleanup # Deletes draft.md after
- `);
- process.exit(1);
- }
- async function main() {
- // Account management commands (run before initClient since they manage the config)
- if (command === 'accounts') {
- const accounts = listAccountsFn();
- if (accounts.length === 0) {
- console.log('No accounts configured. Create accounts.json or .env in the skill directory.');
- } else {
- for (const a of accounts) {
- const marker = a.isDefault ? ' (default)' : '';
- const token = a.hasApiToken ? 'token' : 'no-token';
- const jwt = a.hasJwt ? ', jwt' : '';
- console.log(` ${a.id}${marker} [${token}${jwt}]${a.email ? ` - ${a.email}` : ''}`);
- }
- }
- return;
- }
- if (command === 'switch-account') {
- if (!targetInput) {
- console.error('Error: Account name required');
- console.error('Usage: node query.mjs switch-account <name>');
- process.exit(1);
- }
- setDefaultAccount(targetInput);
- console.log(`Default account set to: ${targetInput}`);
- return;
- }
- if (command === 'add-account') {
- if (!targetInput) {
- console.error('Error: Account name required');
- console.error('Usage: node query.mjs add-account <name> --token pk_...');
- process.exit(1);
- }
- // Look for --token in remaining args
- let token = null;
- for (let i = 0; i < args.length; i++) {
- if (args[i] === '--token' && args[i + 1]) {
- token = args[i + 1];
- break;
- }
- }
- if (!token) {
- console.error('Error: --token is required');
- console.error('Usage: node query.mjs add-account <name> --token pk_...');
- process.exit(1);
- }
- addAccountFn(targetInput, { apiToken: token });
- console.log(`Account "${targetInput}" added.`);
- return;
- }
- if (command === 'remove-account') {
- if (!targetInput) {
- console.error('Error: Account name required');
- console.error('Usage: node query.mjs remove-account <name>');
- process.exit(1);
- }
- removeAccountFn(targetInput);
- console.log(`Account "${targetInput}" removed.`);
- return;
- }
- // Initialize client with selected account
- initClient(accountArg);
- if (!command) {
- showUsage();
- }
- // Handle commands that don't require target input
- if (command === 'me') {
- try {
- const user = await getCurrentUser();
- if (jsonOutput) {
- console.log(JSON.stringify(user, null, 2));
- } else {
- console.log(`User: ${user.username}`);
- console.log(`Email: ${user.email}`);
- console.log(`ID: ${user.id}`);
- console.log(`Timezone: ${user.timezone || 'Not set'}`);
- }
- // Cache user ID if not already cached
- const creds = getActiveCredentials();
- if (!creds.userId) {
- updateAccountField(getActiveAccountId(), 'userId', user.id.toString());
- console.error('\nCached user ID to accounts.json');
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- return;
- }
- if (command === 'my-tasks') {
- try {
- const teamId = await getTeamId();
- const userId = await getUserId();
- const tasks = await getMyTasks(teamId, userId);
- if (jsonOutput) {
- console.log(JSON.stringify(tasks, null, 2));
- } else {
- console.log(formatTaskList(tasks));
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- return;
- }
- if (command === 'search') {
- const hasScope = !!(listScopeArg || folderScopeArg || statusScopeArg || filterMe || assigneeArg);
- if (!hasScope && !allScopeFlag) {
- console.error('Error: `search` requires a scope — text search across the entire workspace is expensive.');
- console.error('');
- console.error('Pick one of:');
- console.error(' --list <id> scope to a single list (fastest)');
- console.error(' --folder <id> scope to a folder');
- console.error(' --me tasks assigned to you');
- console.error(' --assignee <u> tasks assigned to a specific user');
- console.error(' --status <name> tasks with a given status (repeatable via comma)');
- console.error(' --all unscoped full-workspace scan (slow, use sparingly)');
- console.error('');
- console.error('Looking for a list or folder by name? Use `find-list "name"` instead.');
- process.exit(1);
- }
- try {
- const teamId = await getTeamId();
- const opts = {};
- if (listScopeArg) opts.list_ids = [listScopeArg];
- if (folderScopeArg) opts.project_ids = [folderScopeArg];
- if (statusScopeArg) opts.statuses = statusScopeArg.split(',').map(s => s.trim()).filter(Boolean);
- if (filterMe || assigneeArg) {
- const who = assigneeArg ? await findUser(teamId, assigneeArg) : null;
- opts.assigneeId = filterMe ? await getUserId() : (who?.id || who);
- }
- if (allScopeFlag) opts.all = true;
- const tasks = await searchTasks(teamId, targetInput || '', opts);
- if (jsonOutput) {
- console.log(JSON.stringify(tasks, null, 2));
- } else if (tasks.length === 0) {
- const q = targetInput ? ` matching "${targetInput}"` : '';
- console.log(`No tasks found${q}`);
- } else {
- console.log(formatTaskList(tasks));
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- return;
- }
- if (command === 'find-list') {
- if (!targetInput) {
- console.error('Error: find-list requires a name query');
- console.error('Usage: node query.mjs find-list "partial name"');
- process.exit(1);
- }
- try {
- const teamId = await getTeamId();
- const { lists, folders } = await findListByName(teamId, targetInput);
- if (jsonOutput) {
- console.log(JSON.stringify({ lists, folders }, null, 2));
- } else if (!lists.length && !folders.length) {
- console.log(`No lists or folders matching "${targetInput}"`);
- } else {
- if (lists.length) {
- console.log(`Lists (${lists.length}):`);
- for (const l of lists) {
- const loc = l.folder ? `${l.space.name} / ${l.folder.name}` : `${l.space.name} (folderless)`;
- console.log(` ${l.name}`);
- console.log(` id: ${l.id} — ${loc}`);
- }
- }
- if (folders.length) {
- if (lists.length) console.log('');
- console.log(`Folders (${folders.length}):`);
- for (const f of folders) {
- console.log(` ${f.name}`);
- console.log(` id: ${f.id} — ${f.space.name} — ${f.listCount} list(s)`);
- for (const l of f.lists) {
- console.log(` • ${l.name} (list id: ${l.id})`);
- }
- }
- }
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- return;
- }
- // Document commands that may not require a target
- if (command === 'docs') {
- try {
- const workspaceId = await getTeamId();
- const options = targetInput ? { query: targetInput } : {};
- const result = await searchDocs(workspaceId, options);
- const docs = result.docs || [];
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- if (docs.length === 0) {
- console.log(targetInput ? `No docs found matching "${targetInput}"` : 'No docs found.');
- } else {
- console.log(formatDocList(docs));
- }
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- return;
- }
- if (command === 'create-doc') {
- if (!targetInput) {
- console.error('Error: Doc title required');
- console.error('Usage: node query.mjs create-doc "Doc Title" [--space <space_id>] [--content "content"]');
- process.exit(1);
- }
- try {
- const workspaceId = await getTeamId();
- const options = {};
- if (contentArg) {
- options.content = contentArg;
- }
- if (spaceArg) {
- const spaceId = parseSpaceId(spaceArg);
- options.parent = { id: spaceId, type: 4 };
- }
- const doc = await createDoc(workspaceId, targetInput, options);
- if (jsonOutput) {
- console.log(JSON.stringify(doc, null, 2));
- } else {
- console.log(`Doc created: ${doc.name}`);
- console.log(`ID: ${doc.id}`);
- if (spaceArg) {
- console.log(`Space: ${spaceArg}`);
- }
- if (doc.firstPageId) {
- console.log(`First page populated with content`);
- }
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- return;
- }
- if (command === 'lists') {
- if (!targetInput) {
- console.error('Error: Folder ID required');
- console.error('Usage: node query.mjs lists <folder_id>');
- process.exit(1);
- }
- try {
- const folderId = targetInput;
- const lists = await getLists(folderId);
- if (jsonOutput) {
- console.log(JSON.stringify(lists, null, 2));
- } else {
- if (lists.length === 0) {
- console.log('No lists found in this folder.');
- } else {
- for (const l of lists) {
- console.log(` ${l.name} (ID: ${l.id})`);
- }
- console.log(`\nTotal: ${lists.length} list(s)`);
- }
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- return;
- }
- if (command === 'space-lists') {
- if (!targetInput) {
- console.error('Error: Space ID required');
- console.error('Usage: node query.mjs space-lists <space_id>');
- process.exit(1);
- }
- try {
- const spaceId = parseSpaceId(targetInput);
- const lists = await getFolderlessLists(spaceId);
- if (jsonOutput) {
- console.log(JSON.stringify(lists, null, 2));
- } else {
- if (lists.length === 0) {
- console.log('No folderless lists found in this space.');
- } else {
- for (const l of lists) {
- console.log(` ${l.name} (ID: ${l.id})`);
- }
- console.log(`\nTotal: ${lists.length} list(s)`);
- }
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- return;
- }
- // Batch create from JSON file
- if (command === 'batch-create') {
- if (!fileArg && !contentArg) {
- console.error('Error: --file or --content required with a JSON plan');
- console.error('Usage: node query.mjs batch-create --file plan.json');
- console.error(' node query.mjs batch-create --content \'{"listId":"...","tasks":[...]}\'');
- process.exit(1);
- }
- try {
- const planText = contentArg || readFileSync(resolve(fileArg), 'utf-8');
- const plan = JSON.parse(planText);
- const dryRun = args.includes('--dry-run');
- const verbose = !jsonOutput; // verbose by default unless --json
- const result = await executeBatchCreate(plan, { verbose, dryRun });
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- console.log(`\n${result.message}`);
- if (result.errors.length > 0) {
- console.log('\nErrors:');
- for (const e of result.errors) {
- console.log(` ${e.ref || e.name || '?'}: ${e.error}`);
- }
- }
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- return;
- }
- // Watcher commands that don't require a target
- if (command === 'watchers') {
- const entries = getWatcherEntries();
- if (entries.length === 0) {
- console.log('No active watchers. Use watch-task, watch-list, or watch-space to create one.');
- } else if (jsonOutput) {
- console.log(JSON.stringify(entries, null, 2));
- } else {
- console.log(`Active watchers (${entries.length}):\n`);
- for (const e of entries) {
- console.log(` ${e.webhookId} | ${e.scope}:${e.scopeId} | ${e.events.length} events | ${e.createdAt}`);
- }
- }
- return;
- }
- if (command === 'webhooks') {
- try {
- const webhooks = await listWebhooks();
- if (webhooks.length === 0) {
- console.log('No webhooks registered in workspace.');
- } else if (jsonOutput) {
- console.log(JSON.stringify(webhooks, null, 2));
- } else {
- console.log(`Webhooks in workspace (${webhooks.length}):\n`);
- for (const wh of webhooks) {
- const health = wh.health?.status || '?';
- const fails = wh.health?.fail_count || 0;
- const scope = wh.task_id ? `task:${wh.task_id}` :
- wh.list_id ? `list:${wh.list_id}` :
- wh.folder_id ? `folder:${wh.folder_id}` :
- wh.space_id ? `space:${wh.space_id}` : 'workspace';
- console.log(` ${wh.id} | ${scope} | ${wh.events?.length || 0} events | ${health} (${fails} fails)`);
- console.log(` → ${wh.endpoint}`);
- }
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- return;
- }
- // All other commands require a target
- if (!targetInput) {
- showUsage();
- }
- try {
- switch (command) {
- case 'get': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (ids.length === 1) {
- const task = await getTask(ids[0], includeSubtasks);
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- console.log(formatTask(task));
- }
- } else {
- const results = await bulkExecute(ids, id => getTask(id, includeSubtasks));
- if (jsonOutput) {
- console.log(JSON.stringify(results.succeeded.map(s => s.result), null, 2));
- } else {
- for (const s of results.succeeded) {
- console.log(formatTask(s.result));
- console.log('');
- }
- if (results.failed.length > 0) {
- console.log(formatBulkResults({ ...results, succeeded: [] }));
- }
- }
- }
- break;
- }
- case 'comments': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- const comments = await getComments(taskId);
- // If --threads flag, auto-expand all threads with replies
- let threadMap = {};
- if (threadsFlag) {
- const threaded = comments.filter(c => parseInt(c.reply_count, 10) > 0);
- if (threaded.length > 0) {
- const threadResults = await Promise.all(
- threaded.map(c => getThreadedComments(c.id).then(replies => [c.id, replies]))
- );
- for (const [id, replies] of threadResults) {
- threadMap[id] = replies;
- }
- }
- }
- if (jsonOutput) {
- if (threadsFlag) {
- // Embed replies into each comment for JSON output
- const enriched = comments.map(c => ({
- ...c,
- replies: threadMap[c.id] || [],
- }));
- console.log(JSON.stringify(enriched, null, 2));
- } else {
- console.log(JSON.stringify(comments, null, 2));
- }
- } else {
- console.log(formatComments(comments, threadMap));
- }
- break;
- }
- case 'thread': {
- const commentId = targetInput;
- if (!commentId) {
- console.error('Error: Comment ID required');
- console.error('Usage: node query.mjs thread <comment_id>');
- process.exit(1);
- }
- const replies = await getThreadedComments(commentId);
- if (jsonOutput) {
- console.log(JSON.stringify(replies, null, 2));
- } else {
- console.log(formatThread(null, replies));
- }
- break;
- }
- case 'reply': {
- const commentId = targetInput;
- if (!commentId) {
- console.error('Error: Comment ID required');
- console.error('Usage: node query.mjs reply <comment_id> "reply text"');
- process.exit(1);
- }
- if (!commentText) {
- console.error('Error: Reply text required');
- console.error('Usage: node query.mjs reply <comment_id> "reply text"');
- process.exit(1);
- }
- const result = await replyToComment(commentId, commentText);
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- console.log(`Reply posted successfully (ID: ${result.id || 'ok'})`);
- }
- break;
- }
- case 'comment': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!commentText) {
- console.error('Error: Comment text required');
- console.error('Usage: node query.mjs comment <url|id[,id,...]> "Your comment" [--attach ./file.png]');
- process.exit(1);
- }
- // Resolve --attach file paths for comment attachments
- const commentAttachFiles = attachArgs.map(f => resolve(f));
- if (commentAttachFiles.length > 0) {
- for (const fp of commentAttachFiles) {
- if (!existsSync(fp)) {
- console.error(`Error: Attachment file not found: ${fp}`);
- process.exit(1);
- }
- }
- }
- if (ids.length === 1) {
- const result = await postComment(ids[0], commentText);
- if (jsonOutput) {
- const output = { comment: result };
- if (commentAttachFiles.length > 0) {
- output.attachments = await uploadAttachments(ids[0], commentAttachFiles);
- }
- console.log(JSON.stringify(output, null, 2));
- } else {
- console.log(`Comment posted successfully (ID: ${result.id})`);
- if (commentAttachFiles.length > 0) {
- const attResults = await uploadAttachments(ids[0], commentAttachFiles);
- for (const r of attResults) {
- const att = r.attachment || r;
- console.log(` Attached: ${att.title || att.name || 'file'}`);
- }
- }
- }
- } else {
- const results = await bulkExecute(ids, async (id) => {
- const comment = await postComment(id, commentText);
- let attachments;
- if (commentAttachFiles.length > 0) {
- attachments = await uploadAttachments(id, commentAttachFiles);
- }
- return { comment, attachments };
- });
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- console.log(formatBulkResults(results, { action: 'Comment posted on' }));
- if (commentAttachFiles.length > 0) {
- console.log(` (${commentAttachFiles.length} file(s) attached to each task)`);
- }
- }
- }
- break;
- }
- case 'status': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!commentText) {
- // No status provided - show available statuses (single task only)
- const statuses = await getAvailableStatuses(ids[0]);
- console.log('Available statuses:');
- for (const s of statuses) {
- console.log(` - "${s.status}" (${s.type})`);
- }
- break;
- }
- if (ids.length === 1) {
- const { task, matchedStatus } = await updateTaskStatus(ids[0], commentText);
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- console.log(`Status updated to "${matchedStatus.status}"`);
- }
- } else {
- const results = await bulkExecute(ids, id => updateTaskStatus(id, commentText));
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- console.log(formatBulkResults(results, {
- action: 'Updated',
- formatItem: s => `${s.id}: status → "${s.result.matchedStatus.status}"`,
- }));
- }
- }
- break;
- }
- case 'tasks': {
- const listId = parseListId(targetInput);
- if (!listId) {
- console.error('Error: Could not parse list ID from input');
- process.exit(1);
- }
- let assigneeId = null;
- if (filterMe) {
- assigneeId = await getUserId();
- }
- const tasks = await getTasksInList(listId, assigneeId);
- if (jsonOutput) {
- console.log(JSON.stringify(tasks, null, 2));
- } else {
- console.log(formatTaskList(tasks));
- }
- break;
- }
- case 'create': {
- // Support: create <list_id> "title" [options] OR create "title" [options] (uses default list)
- // Options: --assignee/-a <user>, --due/-d <date>, --description/--desc <text>
- let listId = parseListId(targetInput);
- let title = arg2;
- // If targetInput doesn't parse as a list ID, treat it as the title
- if (!listId) {
- title = targetInput;
- listId = getActiveCredentials().defaultListId;
- if (!listId) {
- console.error('Error: No list ID provided and no defaultListId set for this account');
- console.error('Usage: node query.mjs create <list_id> "Task title" [options]');
- console.error(' Or: Set defaultListId in accounts.json to use: node query.mjs create "Task title"');
- console.error('Options: --assignee/-a <user>, --due/-d <date>, --description/--desc <text>');
- process.exit(1);
- }
- }
- if (!title) {
- console.error('Error: Task title required');
- console.error('Usage: node query.mjs create <list_id> "Task title" [options]');
- console.error('Options: --assignee/-a <user>, --due/-d <date>, --description/--desc <text>');
- process.exit(1);
- }
- // Build options from flags
- const options = {};
- // --file/--content populate contentArg; accept them here too so a long
- // description doesn't have to go through a second `description` call.
- const createDescription = descriptionArg || contentArg;
- if (createDescription) {
- // Use markdown_description for proper markdown rendering in ClickUp
- // Note: Task descriptions use markdown_description (ClickUp parses),
- // while comments use JSON array format (we parse via markdownToClickUp)
- options.markdown_description = createDescription;
- }
- if (dueArg) {
- const dueDate = parseDateInput(dueArg);
- options.due_date = dueDate.getTime();
- }
- if (assigneeArg) {
- let assigneeId;
- if (assigneeArg.toLowerCase() === 'me') {
- // Special case: "me" means the current authenticated user
- assigneeId = await getUserId();
- } else {
- const teamId = await getTeamId();
- const user = await findUser(teamId, assigneeArg);
- if (!user) {
- console.error(`Error: User "${assigneeArg}" not found in team`);
- process.exit(1);
- }
- assigneeId = user.id;
- }
- options.assignees = [assigneeId];
- }
- const task = await createTask(listId, title, options);
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- console.log(`Task created: ${task.name}`);
- console.log(`ID: ${task.id}`);
- if (task.assignees?.length) {
- console.log(`Assignees: ${task.assignees.map(a => a.username).join(', ')}`);
- }
- if (task.due_date) {
- console.log(`Due: ${new Date(parseInt(task.due_date, 10)).toLocaleDateString()}`);
- }
- console.log(`URL: ${task.url}`);
- }
- break;
- }
- case 'assign': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: User required');
- console.error('Usage: node query.mjs assign <task[,task,...]> <user>');
- process.exit(1);
- }
- const teamId = await getTeamId();
- const user = await findUser(teamId, arg2);
- if (!user) {
- console.error(`Error: User "${arg2}" not found in team`);
- process.exit(1);
- }
- if (ids.length === 1) {
- const task = await assignTask(ids[0], [user.id]);
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- console.log(`Task assigned to ${user.username || user.email}`);
- }
- } else {
- const results = await bulkExecute(ids, id => assignTask(id, [user.id]));
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- console.log(formatBulkResults(results, { action: `Assigned to ${user.username || user.email}:` }));
- }
- }
- break;
- }
- case 'due': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Due date required');
- console.error('Usage: node query.mjs due <task[,task,...]> "date"');
- console.error('Examples: "tomorrow", "next friday", "2024-01-15", "+3d"');
- process.exit(1);
- }
- if (ids.length === 1) {
- const task = await setDueDate(ids[0], arg2);
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- const dueDate = task.due_date ? new Date(parseInt(task.due_date, 10)).toLocaleDateString() : 'cleared';
- console.log(`Due date set to: ${dueDate}`);
- }
- } else {
- const results = await bulkExecute(ids, id => setDueDate(id, arg2));
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- console.log(formatBulkResults(results, { action: 'Due date set on' }));
- }
- }
- break;
- }
- case 'start': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Start date required');
- console.error('Usage: node query.mjs start <task[,task,...]> "date"');
- console.error('Examples: "today", "tomorrow", "2024-01-15", "+3d"');
- process.exit(1);
- }
- if (ids.length === 1) {
- const task = await setStartDate(ids[0], arg2);
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- const startDate = task.start_date ? new Date(parseInt(task.start_date, 10)).toLocaleDateString() : 'cleared';
- console.log(`Start date set to: ${startDate}`);
- }
- } else {
- const results = await bulkExecute(ids, id => setStartDate(id, arg2));
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- console.log(formatBulkResults(results, { action: 'Start date set on' }));
- }
- }
- break;
- }
- case 'schedule': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2 || !arg3) {
- console.error('Error: Both start and due dates required');
- console.error('Usage: node query.mjs schedule <task> "start_date" "due_date"');
- console.error('Examples: node query.mjs schedule abc123 "today" "friday"');
- process.exit(1);
- }
- const task = await setDates(taskId, { start: arg2, due: arg3 });
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- const startDate = task.start_date ? new Date(parseInt(task.start_date, 10)).toLocaleDateString() : 'not set';
- const dueDate = task.due_date ? new Date(parseInt(task.due_date, 10)).toLocaleDateString() : 'not set';
- console.log(`Scheduled: ${startDate} → ${dueDate}`);
- }
- break;
- }
- case 'rename': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: New name required');
- console.error('Usage: node query.mjs rename <task> "new name"');
- process.exit(1);
- }
- const task = await updateTask(taskId, { name: arg2 });
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- console.log(`Task renamed to: ${task.name}`);
- }
- break;
- }
- case 'priority': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Priority level required');
- console.error('Usage: node query.mjs priority <task[,task,...]> <level>');
- console.error('Levels: urgent, high, normal, low, none');
- process.exit(1);
- }
- if (ids.length === 1) {
- const { task, priority } = await setPriority(ids[0], arg2);
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- console.log(`Priority set to: ${priority}`);
- }
- } else {
- const results = await bulkExecute(ids, id => setPriority(id, arg2));
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- console.log(formatBulkResults(results, {
- action: 'Priority set on',
- formatItem: s => `${s.id}: priority → ${s.result.priority}`,
- }));
- }
- }
- break;
- }
- case 'subtask': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Subtask title required');
- console.error('Usage: node query.mjs subtask <task> "Subtask title"');
- process.exit(1);
- }
- const subtask = await createSubtask(taskId, arg2);
- if (jsonOutput) {
- console.log(JSON.stringify(subtask, null, 2));
- } else {
- console.log(`Subtask created: ${subtask.name}`);
- console.log(`ID: ${subtask.id}`);
- console.log(`URL: ${subtask.url}`);
- }
- break;
- }
- case 'parent': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Parent task ID required');
- console.error('Usage: node query.mjs parent <task> <parent_task>');
- console.error(
- 'ClickUp has no detach: a subtask cannot be turned back into a top-level task.'
- );
- process.exit(1);
- }
- const newParentId = parseTaskId(arg2);
- if (!newParentId) {
- console.error('Error: Could not parse parent task ID');
- process.exit(1);
- }
- if (newParentId === taskId) {
- console.error(`Error: Task ${taskId} cannot be its own parent`);
- process.exit(1);
- }
- const before = await getTask(taskId);
- const parentAncestors = await getAncestorIds(newParentId);
- if (parentAncestors.includes(taskId)) {
- console.error(
- `Error: ${newParentId} is already a descendant of ${taskId} — this would create a cycle`
- );
- console.error(`Chain: ${newParentId} -> ${parentAncestors.join(' -> ')}`);
- process.exit(1);
- }
- if (before.parent === newParentId) {
- console.log(`No change: ${taskId} is already a subtask of ${newParentId}`);
- break;
- }
- await setParent(taskId, newParentId);
- // Report the server's state, not the request we sent — and fail before
- // printing anything success-shaped, because ClickUp can accept an
- // UpdateTask and ignore the `parent` in it.
- const after = await getTask(taskId);
- if (after.parent !== newParentId) {
- console.error(
- `Error: re-read shows parent ${after.parent ?? 'none'}, expected ${newParentId}`
- );
- process.exit(1);
- }
- if (jsonOutput) {
- console.log(JSON.stringify(after, null, 2));
- } else {
- console.log(`Parent now: ${after.parent} (was ${before.parent ?? 'none'})`);
- if (after.list?.id !== before.list?.id) {
- console.log(
- `List moved: ${before.list?.name ?? before.list?.id} -> ${
- after.list?.name ?? after.list?.id
- }`
- );
- }
- console.log(
- `Status: ${after.status?.status ?? 'unknown'}, priority: ${
- after.priority?.priority ?? 'none'
- }`
- );
- if (after.url) console.log(`URL: ${after.url}`);
- }
- break;
- }
- case 'milestone': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Mode required');
- console.error('Usage: node query.mjs milestone <task> <on|off>');
- process.exit(1);
- }
- const mode = arg2.toLowerCase();
- if (!['on', 'off'].includes(mode)) {
- console.error(`Error: Unknown mode "${arg2}"`);
- console.error('Usage: node query.mjs milestone <task> <on|off>');
- process.exit(1);
- }
- const workspaceId = await getTeamId();
- const milestoneType = await findCustomItemByName(workspaceId, 'milestone');
- if (!milestoneType) {
- console.error('Error: This workspace has no "milestone" task type');
- process.exit(1);
- }
- // Clearing the type is WRITTEN as null but READS BACK as 0 (measured
- // against a real task), so the wanted value for `off` is 0, not null.
- const wanted = mode === 'on' ? milestoneType.id : 0;
- const before = await getTask(taskId);
- const beforeId = before.custom_item_id;
- // A missing field is not evidence of anything, so it must not satisfy
- // the no-op — fall through and let the re-read decide.
- if (typeof beforeId === 'number' && beforeId === wanted) {
- console.log(
- `No change: ${taskId} is already ${mode === 'on' ? 'a milestone' : 'a regular task'}`
- );
- break;
- }
- await setCustomItemId(taskId, mode === 'on' ? milestoneType.id : null);
- const after = await getTask(taskId);
- const afterId = after.custom_item_id;
- if (afterId !== wanted) {
- console.error(
- `Error: re-read shows custom_item_id ${afterId ?? 'absent'}, expected ${wanted}`
- );
- process.exit(1);
- }
- if (jsonOutput) {
- console.log(JSON.stringify(after, null, 2));
- } else {
- console.log(
- `Type now: ${
- afterId === milestoneType.id ? 'milestone' : 'task'
- } (custom_item_id ${afterId}, was ${beforeId ?? 'absent'})`
- );
- if (after.url) console.log(`URL: ${after.url}`);
- }
- break;
- }
- case 'move': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Target list ID required');
- console.error('Usage: node query.mjs move <task> <list_id>');
- process.exit(1);
- }
- const listId = parseListId(arg2);
- const workspaceId = await getTeamId();
- const task = await moveTask(taskId, listId, workspaceId);
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- console.log(`Task moved successfully`);
- if (task.url) console.log(`URL: ${task.url}`);
- }
- break;
- }
- case 'link': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: URL required');
- console.error('Usage: node query.mjs link <task> <url> ["description"]');
- process.exit(1);
- }
- const result = await addExternalLink(taskId, arg2, arg3);
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- console.log(`Link added as comment (ID: ${result.id})`);
- }
- break;
- }
- case 'checklist': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Checklist item required');
- console.error('Usage: node query.mjs checklist <task> "Item text"');
- process.exit(1);
- }
- const { checklist, item } = await addChecklistItemToTask(taskId, arg2);
- if (jsonOutput) {
- console.log(JSON.stringify({ checklist, item }, null, 2));
- } else {
- console.log(`Added to checklist "${checklist.name}"`);
- }
- break;
- }
- case 'update-comment': {
- const commentId = targetInput;
- if (!commentId) {
- console.error('Error: Comment ID required');
- console.error('Usage: node query.mjs update-comment <comment_id> "new text"');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Comment text required');
- console.error('Usage: node query.mjs update-comment <comment_id> "new text"');
- process.exit(1);
- }
- const result = await updateComment(commentId, { comment_text: arg2 });
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- console.log(`Comment ${commentId} updated`);
- }
- break;
- }
- case 'resolve-comment': {
- const commentId = targetInput;
- if (!commentId) {
- console.error('Error: Comment ID required');
- console.error('Usage: node query.mjs resolve-comment <comment_id>');
- process.exit(1);
- }
- const result = await updateComment(commentId, { resolved: true });
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- console.log(`Comment ${commentId} resolved`);
- }
- break;
- }
- case 'delete-comment': {
- const commentId = targetInput;
- if (!commentId) {
- console.error('Error: Comment ID required');
- console.error('Usage: node query.mjs delete-comment <comment_id>');
- process.exit(1);
- }
- await deleteComment(commentId);
- if (jsonOutput) {
- console.log(JSON.stringify({ deleted: true, commentId }, null, 2));
- } else {
- console.log(`Comment ${commentId} deleted`);
- }
- break;
- }
- case 'watch': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: User required');
- console.error('Usage: node query.mjs watch <task> <user>');
- process.exit(1);
- }
- const teamId = await getTeamId();
- const user = await findUser(teamId, arg2);
- if (!user) {
- console.error(`Error: User "${arg2}" not found in team`);
- process.exit(1);
- }
- const result = await addWatcher(taskId, user.id);
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- console.log(`Added ${user.username || user.email} as watcher`);
- }
- break;
- }
- case 'unwatch': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: User required');
- console.error('Usage: node query.mjs unwatch <task> <user>');
- process.exit(1);
- }
- const teamId = await getTeamId();
- const user = await findUser(teamId, arg2);
- if (!user) {
- console.error(`Error: User "${arg2}" not found in team`);
- process.exit(1);
- }
- const result = await removeWatcher(taskId, user.id);
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- console.log(`Removed ${user.username || user.email} as watcher`);
- }
- break;
- }
- case 'tag': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Tag name required');
- console.error('Usage: node query.mjs tag <task[,task,...]> "tag_name"');
- process.exit(1);
- }
- if (ids.length === 1) {
- await addTag(ids[0], arg2);
- if (jsonOutput) {
- console.log(JSON.stringify({ tagged: true, taskId: ids[0], tag: arg2 }, null, 2));
- } else {
- console.log(`Tag "${arg2}" added to task`);
- }
- } else {
- const results = await bulkExecute(ids, id => addTag(id, arg2));
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- console.log(formatBulkResults(results, { action: `Tagged "${arg2}" on` }));
- }
- }
- break;
- }
- case 'remove-tag': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Tag name required');
- console.error('Usage: node query.mjs remove-tag <task[,task,...]> "tag_name"');
- process.exit(1);
- }
- if (ids.length === 1) {
- await removeTag(ids[0], arg2);
- if (jsonOutput) {
- console.log(JSON.stringify({ removed: true, taskId: ids[0], tag: arg2 }, null, 2));
- } else {
- console.log(`Tag "${arg2}" removed from task`);
- }
- } else {
- const results = await bulkExecute(ids, id => removeTag(id, arg2));
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- console.log(formatBulkResults(results, { action: `Removed tag "${arg2}" from` }));
- }
- }
- break;
- }
- case 'description': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Description text required');
- console.error('Usage: node query.mjs description <task> "description text"');
- console.error('Markdown formatting is supported.');
- process.exit(1);
- }
- // Use markdown_description for proper markdown rendering in ClickUp
- // Note: Unlike comments (which use JSON array format via markdownToClickUp),
- // task descriptions use ClickUp's native markdown_description field
- const task = await updateTask(taskId, { markdown_description: arg2 });
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- console.log('Task description updated');
- console.log(`URL: ${task.url}`);
- }
- break;
- }
- case 'depends': {
- // Set task as waiting on another task
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Other task ID required');
- console.error('Usage: node query.mjs depends <task> <waits_on_task>');
- console.error('This sets the first task as waiting on/blocked by the second task.');
- process.exit(1);
- }
- const dependsOnId = parseTaskId(arg2);
- if (!dependsOnId) {
- console.error('Error: Could not parse dependency task ID');
- process.exit(1);
- }
- const result = await addDependency(taskId, { depends_on: dependsOnId });
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- console.log(`Task ${taskId} now waits on task ${dependsOnId}`);
- }
- break;
- }
- case 'blocks': {
- // Set task as blocking another task
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Other task ID required');
- console.error('Usage: node query.mjs blocks <task> <blocked_task>');
- console.error('This sets the first task as blocking the second task.');
- process.exit(1);
- }
- const blocksId = parseTaskId(arg2);
- if (!blocksId) {
- console.error('Error: Could not parse blocked task ID');
- process.exit(1);
- }
- const result = await addDependency(taskId, { dependency_of: blocksId });
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- console.log(`Task ${taskId} now blocks task ${blocksId}`);
- }
- break;
- }
- case 'task-link': {
- // Create bidirectional link between tasks
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Other task ID required');
- console.error('Usage: node query.mjs task-link <task> <other_task>');
- console.error('This creates a bidirectional link between tasks.');
- process.exit(1);
- }
- const linksToId = parseTaskId(arg2);
- if (!linksToId) {
- console.error('Error: Could not parse target task ID');
- process.exit(1);
- }
- const result = await addTaskLink(taskId, linksToId);
- if (jsonOutput) {
- console.log(JSON.stringify(result, null, 2));
- } else {
- console.log(`Tasks ${taskId} and ${linksToId} are now linked`);
- }
- break;
- }
- case 'archive': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (ids.length === 1) {
- const task = await archiveTask(ids[0]);
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- console.log(`Task archived: ${task.name || ids[0]}`);
- if (task.url) console.log(`URL: ${task.url}`);
- }
- } else {
- const results = await bulkExecute(ids, id => archiveTask(id));
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- console.log(formatBulkResults(results, { action: 'Archived' }));
- }
- }
- break;
- }
- case 'unarchive': {
- const ids = splitIds(targetInput).map(parseTaskId).filter(Boolean);
- if (ids.length === 0) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- if (ids.length === 1) {
- const task = await unarchiveTask(ids[0]);
- if (jsonOutput) {
- console.log(JSON.stringify(task, null, 2));
- } else {
- console.log(`Task restored: ${task.name || ids[0]}`);
- if (task.url) console.log(`URL: ${task.url}`);
- }
- } else {
- const results = await bulkExecute(ids, id => unarchiveTask(id));
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- console.log(formatBulkResults(results, { action: 'Restored' }));
- }
- }
- break;
- }
- case 'claim': {
- const sessionId = detectClaudeSessionId();
- if (!sessionId) {
- console.error('Error: Could not determine Claude Code session ID.');
- console.error('Tried: $CLAUDE_SESSION_ID, then most-recently-modified');
- console.error(' ~/.claude/projects/*/*.jsonl (within 60s).');
- console.error('Set CLAUDE_SESSION_ID explicitly if running outside a live session.');
- process.exit(1);
- }
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Could not parse task ID from input');
- process.exit(1);
- }
- const field = await findCustomFieldByName(taskId, 'session');
- if (!field) {
- console.error('Error: No "Session ID" custom field found on this task.');
- console.error('Add a text custom field named "Session ID" to the task\'s list in ClickUp.');
- process.exit(1);
- }
- await setCustomField(taskId, field.id, sessionId);
- if (jsonOutput) {
- console.log(JSON.stringify({ taskId, fieldId: field.id, fieldName: field.name, sessionId }, null, 2));
- } else {
- console.log(`Session linked to task ${taskId}`);
- console.log(`Field: ${field.name}`);
- console.log(`Session: ${sessionId}`);
- }
- break;
- }
- // List commands
- case 'list': {
- const listId = parseListId(targetInput);
- if (!listId) {
- console.error('Error: Could not parse list ID from input');
- process.exit(1);
- }
- const list = await getList(listId);
- if (jsonOutput) {
- console.log(JSON.stringify(list, null, 2));
- } else {
- console.log(formatList(list));
- }
- break;
- }
- case 'create-list': {
- const spaceId = parseSpaceId(targetInput);
- if (!spaceId) {
- console.error('Error: Could not parse space ID from input');
- console.error('Usage: node query.mjs create-list <space_id|space_url> "List Name"');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: List name required');
- console.error('Usage: node query.mjs create-list <space_id|space_url> "List Name"');
- process.exit(1);
- }
- const options = {};
- if (contentArg) {
- options.content = contentArg;
- }
- const list = await createList(spaceId, arg2, options);
- if (jsonOutput) {
- console.log(JSON.stringify(list, null, 2));
- } else {
- console.log(`List created: ${list.name}`);
- console.log(`ID: ${list.id}`);
- }
- break;
- }
- case 'update-list': {
- const listId = parseListId(targetInput);
- if (!listId) {
- console.error('Error: Could not parse list ID from input');
- console.error('Usage: node query.mjs update-list <list_id> --name "new name" --content "description"');
- process.exit(1);
- }
- if (!nameArg && !contentArg) {
- console.error('Error: At least --name or --content is required');
- console.error('Usage: node query.mjs update-list <list_id> --name "new name" --content "description"');
- process.exit(1);
- }
- const updates = {};
- if (nameArg) {
- updates.name = nameArg;
- }
- if (contentArg) {
- updates.content = contentArg;
- }
- const list = await updateList(listId, updates);
- if (jsonOutput) {
- console.log(JSON.stringify(list, null, 2));
- } else {
- console.log(`List updated: ${list.name || listId}`);
- if (list.id) console.log(`ID: ${list.id}`);
- }
- break;
- }
- case 'delete-list': {
- const listId = parseListId(targetInput);
- if (!listId) {
- console.error('Error: Could not parse list ID from input');
- console.error('Usage: node query.mjs delete-list <list_id>');
- process.exit(1);
- }
- await deleteList(listId);
- if (jsonOutput) {
- console.log(JSON.stringify({ deleted: true, listId }, null, 2));
- } else {
- console.log(`List ${listId} deleted`);
- }
- break;
- }
- // Document commands
- case 'doc': {
- const docId = parseDocId(targetInput);
- if (!docId) {
- console.error('Error: Could not parse doc ID from input');
- process.exit(1);
- }
- const workspaceId = await getTeamId();
- const doc = await getDoc(workspaceId, docId);
- const pages = await getDocPageListing(workspaceId, docId);
- if (jsonOutput) {
- console.log(JSON.stringify({ doc, pages }, null, 2));
- } else {
- console.log(formatDoc(doc));
- console.log('');
- console.log('Pages:');
- console.log(formatPageList(pages));
- }
- break;
- }
- case 'page': {
- const docId = parseDocId(targetInput);
- if (!docId) {
- console.error('Error: Could not parse doc ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Page ID required');
- console.error('Usage: node query.mjs page <doc_id> <page_id>');
- process.exit(1);
- }
- const pageId = parsePageId(arg2);
- const workspaceId = await getTeamId();
- const page = await getPage(workspaceId, docId, pageId);
- if (jsonOutput) {
- console.log(JSON.stringify(page, null, 2));
- } else {
- console.log(formatPage(page));
- }
- break;
- }
- case 'create-page': {
- const docId = parseDocId(targetInput);
- if (!docId) {
- console.error('Error: Could not parse doc ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Page title required');
- console.error('Usage: node query.mjs create-page <doc_id> "Page Title" [--content "content"]');
- process.exit(1);
- }
- const workspaceId = await getTeamId();
- const options = {};
- if (contentArg) {
- options.content = contentArg;
- }
- if (parentArg) {
- options.parentPageId = parentArg;
- }
- const page = await createPage(workspaceId, docId, arg2, options);
- if (jsonOutput) {
- console.log(JSON.stringify(page, null, 2));
- } else {
- console.log(`Page created: ${page.name}`);
- console.log(`ID: ${page.id}`);
- if (parentArg) {
- console.log(`Parent: ${parentArg}`);
- }
- }
- break;
- }
- case 'edit-page': {
- const docId = parseDocId(targetInput);
- if (!docId) {
- console.error('Error: Could not parse doc ID from input');
- process.exit(1);
- }
- if (!arg2) {
- console.error('Error: Page ID required');
- console.error('Usage: node query.mjs edit-page <doc_id> <page_id> [--content "content"] [--name "name"]');
- process.exit(1);
- }
- if (!contentArg && !nameArg && !archiveFlag) {
- console.error('Error: At least --content, --name, or --archive is required');
- console.error('Usage: node query.mjs edit-page <doc_id> <page_id> [--content "content"] [--name "name"] [--archive]');
- process.exit(1);
- }
- const pageId = parsePageId(arg2);
- const workspaceId = await getTeamId();
- const updates = {};
- if (contentArg) {
- updates.content = contentArg;
- }
- if (nameArg) {
- updates.name = nameArg;
- }
- if (archiveFlag) {
- updates.archived = true;
- }
- const page = await editPage(workspaceId, docId, pageId, updates);
- if (jsonOutput) {
- console.log(JSON.stringify(page, null, 2));
- } else {
- if (archiveFlag) {
- console.log('Page archived successfully');
- } else {
- console.log('Page updated successfully');
- }
- if (page.id) {
- console.log(`ID: ${page.id}`);
- }
- }
- break;
- }
- case 'attach': {
- const taskId = parseTaskId(targetInput);
- if (!taskId) {
- console.error('Error: Task ID or URL required');
- console.error('Usage: node query.mjs attach <url|id> --attach ./file1.png --attach ./file2.pdf');
- console.error('');
- console.error('You can also use --file for a single file:');
- console.error(' node query.mjs attach <url|id> --file ./screenshot.png');
- process.exit(1);
- }
- // Collect files from --attach args and --file arg
- const filesToUpload = [...attachArgs.map(f => resolve(f))];
- if (fileArg && !filesToUpload.includes(resolve(fileArg))) {
- filesToUpload.push(resolve(fileArg));
- }
- if (filesToUpload.length === 0) {
- console.error('Error: At least one file required');
- console.error('Usage: node query.mjs attach <url|id> --attach ./file1.png [--attach ./file2.pdf]');
- process.exit(1);
- }
- // Validate all files exist before uploading
- for (const fp of filesToUpload) {
- if (!existsSync(fp)) {
- console.error(`Error: File not found: ${fp}`);
- process.exit(1);
- }
- }
- const attachResults = await uploadAttachments(taskId, filesToUpload);
- if (jsonOutput) {
- console.log(JSON.stringify(attachResults, null, 2));
- } else {
- for (const result of attachResults) {
- const att = result.attachment || result;
- const title = att.title || att.name || 'file';
- const url = att.url || att.url_w_query || '';
- console.log(`Attached: ${title}${url ? ` (${url})` : ''}`);
- }
- console.log(`\n${filesToUpload.length} file(s) attached to task ${taskId}`);
- }
- break;
- }
- case 'fetch-image':
- case 'fetch-attachment': {
- const url = targetInput;
- if (!url) {
- console.error('Error: Attachment URL required');
- console.error('Usage: node query.mjs fetch-image <url> [--output path]');
- console.error('');
- console.error('Downloads a ClickUp attachment to a local temp file.');
- console.error('The URL is shown in comment output as [Attachment: name](url)');
- process.exit(1);
- }
- // Determine output path
- const outputFlag = args.indexOf('--output');
- let outputPath;
- if (outputFlag !== -1 && args[outputFlag + 1]) {
- outputPath = resolve(args[outputFlag + 1]);
- } else {
- // Extract filename from URL or use a default
- const urlPath = new URL(url).pathname;
- const filename = urlPath.split('/').pop() || 'attachment';
- const clickupTmp = join(tmpdir(), 'clickup-attachments');
- mkdirSync(clickupTmp, { recursive: true });
- outputPath = join(clickupTmp, filename);
- }
- // Fetch the attachment
- const creds = getActiveCredentials();
- const resp = await fetch(url, {
- headers: { Authorization: creds.apiToken },
- });
- if (!resp.ok) {
- // Retry without auth (CDN URLs may not need it)
- const resp2 = await fetch(url);
- if (!resp2.ok) {
- console.error(`Error: Failed to fetch attachment (${resp2.status})`);
- process.exit(1);
- }
- const buffer = Buffer.from(await resp2.arrayBuffer());
- writeFileSync(outputPath, buffer);
- } else {
- const buffer = Buffer.from(await resp.arrayBuffer());
- writeFileSync(outputPath, buffer);
- }
- if (jsonOutput) {
- console.log(JSON.stringify({ path: outputPath, url }, null, 2));
- } else {
- console.log(`Downloaded: ${outputPath}`);
- }
- break;
- }
- // ── Webhook Watcher Commands ──────────────────────────────────────────
- case 'watch-task': {
- // watch-task <task_id>[,task_id2,...] [--events evt1,evt2]
- if (!targetInput) {
- console.error('Error: Task ID(s) required');
- console.error('Usage: node query.mjs watch-task <task_id>[,id2,...] [--events evt1,evt2]');
- process.exit(1);
- }
- const webhookUrlPath = join(resolve(import.meta.dirname), 'webhook-url.txt');
- let webhookUrl;
- if (existsSync(webhookUrlPath)) {
- webhookUrl = readFileSync(webhookUrlPath, 'utf-8').trim();
- } else {
- // Read from accounts.json
- const accountsPath = join(resolve(import.meta.dirname), 'accounts.json');
- const accts = JSON.parse(readFileSync(accountsPath, 'utf-8'));
- if (!accts.webhookSiteToken) {
- console.error('Error: No webhook.site token configured.');
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 2,214 on GitHub
- console.error('Add "webhookSiteToken" to accounts.json.');
- process.exit(1);
- }
- webhookUrl = `https://webhook.site/${accts.webhookSiteToken}`;
- Note: Quoted as data in code: Links to a known data-relay/testing domain (webhook.site) without sending anything to it
- Note: Talks to a raw IP address or throwaway host, where malware is often staged
- Line 2,218 on GitHub
- }
- const events = eventsArg ? eventsArg.split(',') : DEFAULT_TASK_EVENTS;
- const taskIds = targetInput.split(',').map(id => parseTaskId(id));
- const results = [];
- for (const taskId of taskIds) {
- const wh = await createWebhook(webhookUrl, events, { taskId });
- const entry = {
- webhookId: wh.id,
- secret: wh.secret,
- scope: 'task',
- scopeId: taskId,
- events,
- endpoint: webhookUrl,
- createdAt: new Date().toISOString(),
- };
- addWatcherEntry(entry);
- results.push(entry);
- }
- if (jsonOutput) {
- console.log(JSON.stringify(results, null, 2));
- } else {
- for (const r of results) {
- console.log(`Watching task ${r.scopeId} (webhook: ${r.webhookId})`);
- console.log(` Events: ${r.events.join(', ')}`);
- }
- console.log(`\nStart listener: node query.mjs listen`);
- }
- break;
- }
- case 'watch-list': {
- if (!targetInput) {
- console.error('Error: List ID required');
- console.error('Usage: node query.mjs watch-list <list_id> [--events evt1,evt2]');
- process.exit(1);
- }
- const webhookUrlPath = join(resolve(import.meta.dirname), 'webhook-url.txt');
- let webhookUrl;
- if (existsSync(webhookUrlPath)) {
- webhookUrl = readFileSync(webhookUrlPath, 'utf-8').trim();
- } else {
- const accountsPath = join(resolve(import.meta.dirname), 'accounts.json');
- const accts = JSON.parse(readFileSync(accountsPath, 'utf-8'));
- if (!accts.webhookSiteToken) {
- console.error('Error: No webhook.site token configured.');
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 2,265 on GitHub
- process.exit(1);
- }
- webhookUrl = `https://webhook.site/${accts.webhookSiteToken}`;
- Note: Quoted as data in code: Links to a known data-relay/testing domain (webhook.site) without sending anything to it
- Note: Talks to a raw IP address or throwaway host, where malware is often staged
- Line 2,268 on GitHub
- }
- const listId = parseListId(targetInput);
- const events = eventsArg ? eventsArg.split(',') : DEFAULT_LIST_EVENTS;
- const wh = await createWebhook(webhookUrl, events, { listId });
- const entry = {
- webhookId: wh.id,
- secret: wh.secret,
- scope: 'list',
- scopeId: listId,
- events,
- endpoint: webhookUrl,
- createdAt: new Date().toISOString(),
- };
- addWatcherEntry(entry);
- if (jsonOutput) {
- console.log(JSON.stringify(entry, null, 2));
- } else {
- console.log(`Watching list ${listId} (webhook: ${wh.id})`);
- console.log(` Events: ${events.join(', ')}`);
- console.log(`\nStart listener: node query.mjs listen`);
- }
- break;
- }
- case 'watch-space': {
- if (!targetInput) {
- console.error('Error: Space ID required');
- console.error('Usage: node query.mjs watch-space <space_id> [--events evt1,evt2]');
- process.exit(1);
- }
- const webhookUrlPath = join(resolve(import.meta.dirname), 'webhook-url.txt');
- let webhookUrl;
- if (existsSync(webhookUrlPath)) {
- webhookUrl = readFileSync(webhookUrlPath, 'utf-8').trim();
- } else {
- const accountsPath = join(resolve(import.meta.dirname), 'accounts.json');
- const accts = JSON.parse(readFileSync(accountsPath, 'utf-8'));
- if (!accts.webhookSiteToken) {
- console.error('Error: No webhook.site token configured.');
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 2,308 on GitHub
- process.exit(1);
- }
- webhookUrl = `https://webhook.site/${accts.webhookSiteToken}`;
- Note: Quoted as data in code: Links to a known data-relay/testing domain (webhook.site) without sending anything to it
- Note: Talks to a raw IP address or throwaway host, where malware is often staged
- Line 2,311 on GitHub
- }
- const spaceId = parseSpaceId(targetInput);
- const events = eventsArg ? eventsArg.split(',') : DEFAULT_SPACE_EVENTS;
- const wh = await createWebhook(webhookUrl, events, { spaceId });
- const entry = {
- webhookId: wh.id,
- secret: wh.secret,
- scope: 'space',
- scopeId: spaceId,
- events,
- endpoint: webhookUrl,
- createdAt: new Date().toISOString(),
- };
- addWatcherEntry(entry);
- if (jsonOutput) {
- console.log(JSON.stringify(entry, null, 2));
- } else {
- console.log(`Watching space ${spaceId} (webhook: ${wh.id})`);
- console.log(` Events: ${events.join(', ')}`);
- console.log(`\nStart listener: node query.mjs listen`);
- }
- break;
- }
- case 'unwatch-webhook': {
- if (!targetInput) {
- console.error('Error: Webhook ID required (or --all to remove all)');
- console.error('Usage: node query.mjs unwatch-webhook <webhook_id>');
- console.error(' node query.mjs unwatch-webhook --all');
- process.exit(1);
- }
- if (targetInput === '--all') {
- const entries = getWatcherEntries();
- if (entries.length === 0) {
- console.log('No watchers to remove.');
- break;
- }
- let removed = 0;
- for (const e of entries) {
- try {
- await deleteWebhook(e.webhookId);
- removed++;
- } catch (err) {
- console.error(` Failed to delete ${e.webhookId}: ${err.message}`);
- }
- }
- clearAllWatcherEntries();
- console.log(`Removed ${removed}/${entries.length} webhook(s).`);
- } else {
- await deleteWebhook(targetInput);
- removeWatcherEntry(targetInput);
- console.log(`Webhook ${targetInput} deleted.`);
- }
- break;
- }
- default:
- console.error(`Unknown command: ${command}`);
- showUsage();
- }
- } catch (err) {
- console.error('Error:', err.message);
- process.exit(1);
- }
- }
- main().then(() => {
- // --cleanup: delete the source file after successful execution
- if (cleanupFlag && fileArg) {
- const filePath = resolve(fileArg);
- try {
- unlinkSync(filePath);
- } catch {
- // Silently ignore cleanup failures
- }
- }
- });
.claude/skills/clickup/watch.mjs · Bundled script · 358 lines · 1 finding marked
- #!/usr/bin/env node
- /**
- * watch.mjs — Polling-based watcher for ClickUp tasks (and lists)
- *
- * Snapshots a task's baseline (status, comment count / latest comment id,
- * assignees, date_updated) then polls the ClickUp API on an interval. When a
- * watched change is detected vs the baseline it prints a concise JSON summary
- * of what changed and EXITS 0 — so a parent agent running this with
- * run_in_background=true gets woken by the task-notification. On timeout it
- * also exits 0 with a "timeout" summary.
- *
- * Unlike watch-task/listen.mjs (webhook.site + whcli relay), this watcher is
- Note: Quoted as data in code: Mentions a known data-relay/testing domain (webhook.site) without linking or sending anything to it
- Line 13 on GitHub
- * fully self-contained: it needs nothing beyond a working ClickUp API token,
- * so it runs anywhere the rest of the skill runs.
- *
- * Usage:
- * node watch.mjs <taskId|taskUrl>
- * node watch.mjs --list <listId|listUrl>
- *
- * Flags:
- * --timeout <sec> Max seconds to watch before giving up (default 3600)
- * --interval <sec> Poll cadence in seconds (default 45; min 10)
- * --on <types> Comma list of change types that wake the watcher:
- * status,comments,assignee (default: all three)
- * --until-status <statuses> Only wake when the task reaches one of these
- * statuses (comma list). Implies --on status.
- * --account <name> Use a specific named account from accounts.json
- * --json (default output is already JSON)
- * --state-file <path> Optional: write latest change/snapshot JSON here
- *
- * Exit codes:
- * 0 — a watched change fired, OR the watch timed out (both print JSON)
- * 1 — fatal setup/config error (bad task id, no auth, etc.)
- */
- import { writeFileSync } from 'fs';
- import { resolve, dirname } from 'path';
- import { fileURLToPath } from 'url';
- import { initClient } from './api/client.mjs';
- import { getTask, getTasksInList } from './api/tasks.mjs';
- import { getComments } from './api/comments.mjs';
- import { parseTaskId, parseListId } from './lib/parse.mjs';
- const __dirname = dirname(fileURLToPath(import.meta.url));
- // ---------------------------------------------------------------------------
- // Args
- // ---------------------------------------------------------------------------
- const argv = process.argv.slice(2);
- function flag(name) {
- const i = argv.indexOf(`--${name}`);
- if (i === -1) return undefined;
- return argv[i + 1];
- }
- function has(name) {
- return argv.includes(`--${name}`);
- }
- function fatal(msg) {
- console.error(`ERROR: ${msg}`);
- process.exit(1);
- }
- // First positional (not a flag, not a flag value) is the task id/url.
- const positional = [];
- for (let i = 0; i < argv.length; i++) {
- const a = argv[i];
- if (a.startsWith('--')) {
- // Boolean flags take no value; value flags consume the next token.
- if (!['json'].includes(a.slice(2))) i++;
- continue;
- }
- positional.push(a);
- }
- const listInput = flag('list');
- const taskInput = positional[0];
- if (!listInput && !taskInput) {
- fatal(
- 'Provide a task id/url, or --list <listId>.\n' +
- 'Usage: node watch.mjs <taskId> [--timeout 3600] [--interval 45] ' +
- '[--on status,comments,assignee] [--until-status "in progress,complete"]'
- );
- }
- const timeoutSec = Math.max(parseInt(flag('timeout') || '3600', 10), 5);
- const intervalSec = Math.max(parseInt(flag('interval') || '45', 10), 10);
- const account = flag('account');
- const stateFile = flag('state-file');
- const untilStatusRaw = flag('until-status');
- const untilStatuses = untilStatusRaw
- ? untilStatusRaw.split(',').map((s) => s.trim().toLowerCase()).filter(Boolean)
- : null;
- let onTypes;
- const onRaw = flag('on');
- if (onRaw) {
- onTypes = new Set(onRaw.split(',').map((s) => s.trim().toLowerCase()).filter(Boolean));
- } else {
- onTypes = new Set(['status', 'comments', 'assignee']);
- }
- // --until-status is a status-based wake condition; make sure status is watched.
- if (untilStatuses) onTypes.add('status');
- // ---------------------------------------------------------------------------
- // Init
- // ---------------------------------------------------------------------------
- try {
- initClient(account);
- } catch (err) {
- fatal(err.message);
- }
- const mode = listInput ? 'list' : 'task';
- let targetId;
- if (mode === 'list') {
- targetId = parseListId(listInput) || listInput;
- } else {
- targetId = parseTaskId(taskInput);
- }
- if (!targetId) fatal(`Could not parse ${mode} id from input.`);
- const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
- function writeState(obj) {
- if (!stateFile) return;
- try {
- const p = resolve(stateFile);
- writeFileSync(p, JSON.stringify(obj, null, 2) + '\n');
- } catch {
- /* best-effort */
- }
- }
- // ---------------------------------------------------------------------------
- // Snapshotting
- // ---------------------------------------------------------------------------
- // Reduce a raw task to the fields we compare across polls.
- function snapshotTask(task, comments) {
- const assignees = (task.assignees || [])
- .map((a) => a.id)
- .sort((x, y) => String(x).localeCompare(String(y)));
- const latest = comments && comments.length ? comments[comments.length - 1] : null;
- return {
- id: task.id,
- name: task.name,
- status: task.status?.status ?? null,
- statusType: task.status?.type ?? null,
- dateUpdated: task.date_updated ?? null,
- assignees,
- assigneeNames: (task.assignees || []).map((a) => a.username || a.email || String(a.id)),
- commentCount: comments ? comments.length : null,
- latestCommentId: latest ? latest.id : null,
- };
- }
- async function snapshotTaskById(id) {
- const task = await getTask(id);
- let comments = null;
- if (onTypes.has('comments')) {
- try {
- comments = await getComments(id);
- } catch {
- comments = null; // don't let a comment fetch failure abort snapshotting
- }
- }
- return snapshotTask(task, comments);
- }
- // Compare two task snapshots and return array of change descriptors.
- function diffTask(prev, cur) {
- const changes = [];
- if (onTypes.has('status') && prev.status !== cur.status) {
- changes.push({ type: 'status', from: prev.status, to: cur.status });
- }
- if (onTypes.has('assignee')) {
- const a = new Set(prev.assignees.map(String));
- const b = new Set(cur.assignees.map(String));
- const added = [...b].filter((x) => !a.has(x));
- const removed = [...a].filter((x) => !b.has(x));
- if (added.length || removed.length) {
- changes.push({
- type: 'assignee',
- added,
- removed,
- current: cur.assigneeNames,
- });
- }
- }
- if (onTypes.has('comments')) {
- if (
- prev.latestCommentId !== cur.latestCommentId ||
- (prev.commentCount != null && cur.commentCount != null && cur.commentCount > prev.commentCount)
- ) {
- changes.push({
- type: 'comments',
- newCount: cur.commentCount,
- prevCount: prev.commentCount,
- latestCommentId: cur.latestCommentId,
- });
- }
- }
- return changes;
- }
- // A change only satisfies --until-status when the new status matches.
- function passesUntilStatus(changes, cur) {
- if (!untilStatuses) return true;
- const s = (cur.status || '').toLowerCase();
- return untilStatuses.includes(s);
- }
- // ---------------------------------------------------------------------------
- // List-mode snapshot (map of taskId -> per-task snapshot)
- // ---------------------------------------------------------------------------
- async function snapshotList(id) {
- const tasks = await getTasksInList(id);
- const map = {};
- for (const t of tasks) {
- // In list mode we skip per-task comment fetches to stay gentle on the API;
- // status + assignee + date_updated are compared. New/removed tasks tracked too.
- map[t.id] = snapshotTask(t, null);
- }
- return map;
- }
- function diffList(prev, cur) {
- const changes = [];
- const prevIds = new Set(Object.keys(prev));
- const curIds = new Set(Object.keys(cur));
- for (const id of curIds) {
- if (!prevIds.has(id)) {
- changes.push({ type: 'taskCreated', taskId: id, name: cur[id].name, status: cur[id].status });
- continue;
- }
- const sub = diffTask(prev[id], cur[id]);
- for (const c of sub) {
- changes.push({ ...c, taskId: id, name: cur[id].name });
- }
- if (onTypes.has('status') && prev[id].dateUpdated !== cur[id].dateUpdated && prev[id].status === cur[id].status) {
- // date_updated moved without a status/assignee change we track; note as generic update
- // (kept lightweight — only surfaced if nothing else fired for this task)
- }
- }
- for (const id of prevIds) {
- if (!curIds.has(id)) {
- changes.push({ type: 'taskDeleted', taskId: id, name: prev[id].name });
- }
- }
- return changes;
- }
- // ---------------------------------------------------------------------------
- // Main watch loop
- // ---------------------------------------------------------------------------
- async function main() {
- const startedAt = Date.now();
- const deadline = startedAt + timeoutSec * 1000;
- let baseline;
- try {
- baseline = mode === 'list' ? await snapshotList(targetId) : await snapshotTaskById(targetId);
- } catch (err) {
- fatal(`Failed to snapshot ${mode} ${targetId}: ${err.message}`);
- }
- const baseInfo = {
- mode,
- targetId,
- interval: intervalSec,
- timeout: timeoutSec,
- on: [...onTypes],
- untilStatus: untilStatuses,
- baseline: mode === 'task' ? baseline : { taskCount: Object.keys(baseline).length },
- startedAt: new Date(startedAt).toISOString(),
- };
- writeState({ event: 'baseline', ...baseInfo });
- let prev = baseline;
- let consecutiveErrors = 0;
- while (Date.now() < deadline) {
- // Sleep first — the baseline was just taken, so poll after one interval.
- const remaining = deadline - Date.now();
- await sleep(Math.min(intervalSec * 1000, Math.max(remaining, 0)));
- if (Date.now() >= deadline) break;
- let cur;
- try {
- cur = mode === 'list' ? await snapshotList(targetId) : await snapshotTaskById(targetId);
- consecutiveErrors = 0;
- } catch (err) {
- // Graceful backoff on API/network/rate-limit errors; never crash the loop.
- consecutiveErrors++;
- const backoff = Math.min(60, intervalSec * Math.pow(2, consecutiveErrors));
- console.error(`poll error (${consecutiveErrors}): ${err.message} — backing off ${backoff}s`);
- await sleep(backoff * 1000);
- continue;
- }
- const changes = mode === 'list' ? diffList(prev, cur) : diffTask(prev, cur);
- if (changes.length) {
- const curSnap = mode === 'task' ? cur : { taskCount: Object.keys(cur).length };
- if (mode === 'task' && !passesUntilStatus(changes, cur)) {
- // A change happened but the until-status gate isn't satisfied yet;
- // advance the baseline so we don't re-report the same change, keep waiting.
- prev = cur;
- continue;
- }
- const result = {
- event: 'change',
- mode,
- targetId,
- name: mode === 'task' ? cur.name : undefined,
- changes,
- snapshot: curSnap,
- elapsedSec: Math.round((Date.now() - startedAt) / 1000),
- url: mode === 'task' ? `https://app.clickup.com/t/${targetId}` : undefined,
- };
- writeState(result);
- console.log(JSON.stringify(result));
- process.exit(0);
- }
- prev = cur;
- }
- const result = {
- event: 'timeout',
- mode,
- targetId,
- timeout: timeoutSec,
- elapsedSec: Math.round((Date.now() - startedAt) / 1000),
- message: 'No watched change detected within timeout.',
- };
- writeState(result);
- console.log(JSON.stringify(result));
- process.exit(0);
- }
- main().catch((err) => {
- fatal(`Unexpected error: ${err.message}`);
- });
Ratings and comments
Sign in to rate this skill.
Sign inFree to use. If it’s helped, pass it on.