mirror of
https://github.com/paperclipai/paperclip
synced 2026-04-26 01:35:18 +02:00
Compare commits
85 Commits
@paperclip
...
feature/pl
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4cfbeaba9d | ||
|
|
0605c9f229 | ||
|
|
22b8e90ba6 | ||
|
|
7c4b02f02b | ||
|
|
eafb5b8fd9 | ||
|
|
30888759f2 | ||
|
|
193a987513 | ||
|
|
cb5d7e76fb | ||
|
|
bc12f08c66 | ||
|
|
a7a64f11be | ||
|
|
31e6e30fe3 | ||
|
|
ad7bf4288a | ||
|
|
16dfcb56a4 | ||
|
|
924762c073 | ||
|
|
abb70ca5c5 | ||
|
|
1e3a485408 | ||
|
|
07d13e1738 | ||
|
|
c8cd950a03 | ||
|
|
501ab4ffa9 | ||
|
|
6fa1dd2197 | ||
|
|
eb0a74384e | ||
|
|
ab41fdbaee | ||
|
|
45998aa9a0 | ||
|
|
12ccfc2c9a | ||
|
|
80cdbdbd47 | ||
|
|
bcce5b7ec2 | ||
|
|
8eacc9c697 | ||
|
|
db81a06386 | ||
|
|
626a8f1976 | ||
|
|
aa799bba4c | ||
|
|
aaadbdc144 | ||
|
|
a393db78b4 | ||
|
|
c1430e7b06 | ||
|
|
7e288d20fc | ||
|
|
528505a04a | ||
|
|
e2a0347c6d | ||
|
|
cce9941464 | ||
|
|
d51c4b1a4c | ||
|
|
3b0d9a93f4 | ||
|
|
41eb8e51e3 | ||
|
|
cdebf7b538 | ||
|
|
32ab4f8e47 | ||
|
|
6365e03731 | ||
|
|
2b9de934e3 | ||
|
|
4a368f54d5 | ||
|
|
7d1748b3a7 | ||
|
|
2246d5f1eb | ||
|
|
575a2fd83f | ||
|
|
c9259bbec0 | ||
|
|
f3c18db7dd | ||
|
|
43baf709dd | ||
|
|
24d6e3a543 | ||
|
|
0b8223b8b9 | ||
|
|
e2f0241533 | ||
|
|
89e247b410 | ||
|
|
216cb3fb28 | ||
|
|
84fc6d4a87 | ||
|
|
9c7d9ded1e | ||
|
|
dfe40ffcca | ||
|
|
f477f23738 | ||
|
|
29a743cb9e | ||
|
|
4e759da070 | ||
|
|
69b9e45eaf | ||
|
|
5c7d2116e9 | ||
|
|
0f3e9937f6 | ||
|
|
c32d19415b | ||
|
|
0a0d74eb94 | ||
|
|
2c5e48993d | ||
|
|
77af1ae544 | ||
|
|
e24a116943 | ||
|
|
872d2434a9 | ||
|
|
fe764cac75 | ||
|
|
f81d37fbf7 | ||
|
|
83381f9c12 | ||
|
|
f7e1952a55 | ||
|
|
cf77ff927f | ||
|
|
fc8b5e3956 | ||
|
|
ed16d30afc | ||
|
|
402cef66e9 | ||
|
|
13c2ecd1d0 | ||
|
|
a2b7611d8d | ||
|
|
d14e656ec1 | ||
|
|
5201222ce7 | ||
|
|
b888f92718 | ||
|
|
057e3a494c |
39
.github/workflows/pr-policy.yml
vendored
39
.github/workflows/pr-policy.yml
vendored
@@ -13,8 +13,6 @@ jobs:
|
|||||||
policy:
|
policy:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
timeout-minutes: 10
|
timeout-minutes: 10
|
||||||
permissions:
|
|
||||||
pull-requests: read
|
|
||||||
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout repository
|
- name: Checkout repository
|
||||||
@@ -33,38 +31,19 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
node-version: 20
|
node-version: 20
|
||||||
|
|
||||||
- name: Enforce lockfile policy when manifests change
|
- name: Block manual lockfile edits
|
||||||
env:
|
if: github.head_ref != 'chore/refresh-lockfile'
|
||||||
GH_TOKEN: ${{ github.token }}
|
|
||||||
run: |
|
run: |
|
||||||
changed="$(gh api "repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files" --paginate --jq '.[].filename')"
|
changed="$(git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}")"
|
||||||
manifest_pattern='(^|/)package\.json$|^pnpm-workspace\.yaml$|^\.npmrc$|^pnpmfile\.(cjs|js|mjs)$'
|
|
||||||
|
|
||||||
manifest_changed=false
|
|
||||||
lockfile_changed=false
|
|
||||||
|
|
||||||
if printf '%s\n' "$changed" | grep -Eq "$manifest_pattern"; then
|
|
||||||
manifest_changed=true
|
|
||||||
fi
|
|
||||||
|
|
||||||
if printf '%s\n' "$changed" | grep -qx 'pnpm-lock.yaml'; then
|
if printf '%s\n' "$changed" | grep -qx 'pnpm-lock.yaml'; then
|
||||||
lockfile_changed=true
|
echo "Do not commit pnpm-lock.yaml in pull requests. CI owns lockfile updates."
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$lockfile_changed" = true ] && [ "$manifest_changed" != true ]; then
|
|
||||||
echo "pnpm-lock.yaml changed without a dependency manifest change." >&2
|
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
if [ "$manifest_changed" = true ]; then
|
- name: Validate dependency resolution when manifests change
|
||||||
|
run: |
|
||||||
|
changed="$(git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}")"
|
||||||
|
manifest_pattern='(^|/)package\.json$|^pnpm-workspace\.yaml$|^\.npmrc$|^pnpmfile\.(cjs|js|mjs)$'
|
||||||
|
if printf '%s\n' "$changed" | grep -Eq "$manifest_pattern"; then
|
||||||
pnpm install --lockfile-only --ignore-scripts --no-frozen-lockfile
|
pnpm install --lockfile-only --ignore-scripts --no-frozen-lockfile
|
||||||
|
|
||||||
if ! git diff --quiet -- pnpm-lock.yaml; then
|
|
||||||
if [ "${{ github.event.pull_request.head.repo.full_name }}" = "${{ github.repository }}" ]; then
|
|
||||||
echo "pnpm-lock.yaml is stale for this PR. Wait for the Refresh Lockfile workflow to push the bot commit, then rerun checks." >&2
|
|
||||||
else
|
|
||||||
echo "pnpm-lock.yaml is stale for this fork PR. Run pnpm install --lockfile-only --ignore-scripts --no-frozen-lockfile and commit pnpm-lock.yaml." >&2
|
|
||||||
fi
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
|||||||
2
.github/workflows/pr-verify.yml
vendored
2
.github/workflows/pr-verify.yml
vendored
@@ -30,7 +30,7 @@ jobs:
|
|||||||
cache: pnpm
|
cache: pnpm
|
||||||
|
|
||||||
- name: Install dependencies
|
- name: Install dependencies
|
||||||
run: pnpm install --frozen-lockfile
|
run: pnpm install --no-frozen-lockfile
|
||||||
|
|
||||||
- name: Typecheck
|
- name: Typecheck
|
||||||
run: pnpm -r typecheck
|
run: pnpm -r typecheck
|
||||||
|
|||||||
111
.github/workflows/refresh-lockfile-pr.yml
vendored
111
.github/workflows/refresh-lockfile-pr.yml
vendored
@@ -1,111 +0,0 @@
|
|||||||
name: Refresh Lockfile
|
|
||||||
|
|
||||||
on:
|
|
||||||
pull_request:
|
|
||||||
branches:
|
|
||||||
- master
|
|
||||||
types:
|
|
||||||
- opened
|
|
||||||
- synchronize
|
|
||||||
- reopened
|
|
||||||
- ready_for_review
|
|
||||||
|
|
||||||
concurrency:
|
|
||||||
group: refresh-lockfile-pr-${{ github.event.pull_request.number }}
|
|
||||||
cancel-in-progress: true
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
refresh:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
timeout-minutes: 10
|
|
||||||
permissions:
|
|
||||||
contents: write
|
|
||||||
pull-requests: read
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- name: Detect dependency manifest changes
|
|
||||||
id: changes
|
|
||||||
env:
|
|
||||||
GH_TOKEN: ${{ github.token }}
|
|
||||||
run: |
|
|
||||||
changed="$(gh api "repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }}/files" --paginate --jq '.[].filename')"
|
|
||||||
manifest_pattern='(^|/)package\.json$|^pnpm-workspace\.yaml$|^\.npmrc$|^pnpmfile\.(cjs|js|mjs)$'
|
|
||||||
|
|
||||||
if printf '%s\n' "$changed" | grep -Eq "$manifest_pattern"; then
|
|
||||||
echo "manifest_changed=true" >> "$GITHUB_OUTPUT"
|
|
||||||
else
|
|
||||||
echo "manifest_changed=false" >> "$GITHUB_OUTPUT"
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "${{ github.event.pull_request.head.repo.full_name }}" = "${{ github.repository }}" ]; then
|
|
||||||
echo "same_repo=true" >> "$GITHUB_OUTPUT"
|
|
||||||
else
|
|
||||||
echo "same_repo=false" >> "$GITHUB_OUTPUT"
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Checkout pull request head
|
|
||||||
if: steps.changes.outputs.manifest_changed == 'true'
|
|
||||||
uses: actions/checkout@v4
|
|
||||||
with:
|
|
||||||
repository: ${{ github.event.pull_request.head.repo.full_name }}
|
|
||||||
ref: ${{ github.event.pull_request.head.ref }}
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- name: Setup pnpm
|
|
||||||
if: steps.changes.outputs.manifest_changed == 'true'
|
|
||||||
uses: pnpm/action-setup@v4
|
|
||||||
with:
|
|
||||||
version: 9.15.4
|
|
||||||
run_install: false
|
|
||||||
|
|
||||||
- name: Setup Node.js
|
|
||||||
if: steps.changes.outputs.manifest_changed == 'true'
|
|
||||||
uses: actions/setup-node@v4
|
|
||||||
with:
|
|
||||||
node-version: 20
|
|
||||||
cache: pnpm
|
|
||||||
|
|
||||||
- name: Refresh pnpm lockfile
|
|
||||||
if: steps.changes.outputs.manifest_changed == 'true'
|
|
||||||
run: pnpm install --lockfile-only --ignore-scripts --no-frozen-lockfile
|
|
||||||
|
|
||||||
- name: Fail on unexpected file changes
|
|
||||||
if: steps.changes.outputs.manifest_changed == 'true'
|
|
||||||
run: |
|
|
||||||
changed="$(git status --porcelain)"
|
|
||||||
if [ -z "$changed" ]; then
|
|
||||||
echo "Lockfile is already up to date."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
if printf '%s\n' "$changed" | grep -Fvq ' pnpm-lock.yaml'; then
|
|
||||||
echo "Unexpected files changed during lockfile refresh:"
|
|
||||||
echo "$changed"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
- name: Commit refreshed lockfile to same-repo PR branch
|
|
||||||
if: steps.changes.outputs.manifest_changed == 'true' && steps.changes.outputs.same_repo == 'true'
|
|
||||||
run: |
|
|
||||||
if git diff --quiet -- pnpm-lock.yaml; then
|
|
||||||
echo "Lockfile unchanged, nothing to do."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
git config user.name "lockfile-bot"
|
|
||||||
git config user.email "lockfile-bot@users.noreply.github.com"
|
|
||||||
git add pnpm-lock.yaml
|
|
||||||
git commit -m "chore(lockfile): refresh pnpm-lock.yaml"
|
|
||||||
git push origin "HEAD:${{ github.event.pull_request.head.ref }}"
|
|
||||||
|
|
||||||
- name: Fail fork PRs that need a lockfile refresh
|
|
||||||
if: steps.changes.outputs.manifest_changed == 'true' && steps.changes.outputs.same_repo != 'true'
|
|
||||||
run: |
|
|
||||||
if git diff --quiet -- pnpm-lock.yaml; then
|
|
||||||
echo "Lockfile unchanged, nothing to do."
|
|
||||||
exit 0
|
|
||||||
fi
|
|
||||||
|
|
||||||
echo "This fork PR changes dependency manifests and requires a refreshed pnpm-lock.yaml." >&2
|
|
||||||
echo "Run: pnpm install --lockfile-only --ignore-scripts --no-frozen-lockfile" >&2
|
|
||||||
echo "Then commit pnpm-lock.yaml to the PR branch." >&2
|
|
||||||
exit 1
|
|
||||||
81
.github/workflows/refresh-lockfile.yml
vendored
Normal file
81
.github/workflows/refresh-lockfile.yml
vendored
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
name: Refresh Lockfile
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: refresh-lockfile-master
|
||||||
|
cancel-in-progress: false
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
refresh:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
timeout-minutes: 10
|
||||||
|
permissions:
|
||||||
|
contents: write
|
||||||
|
pull-requests: write
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout repository
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
|
- name: Setup pnpm
|
||||||
|
uses: pnpm/action-setup@v4
|
||||||
|
with:
|
||||||
|
version: 9.15.4
|
||||||
|
run_install: false
|
||||||
|
|
||||||
|
- name: Setup Node.js
|
||||||
|
uses: actions/setup-node@v4
|
||||||
|
with:
|
||||||
|
node-version: 20
|
||||||
|
cache: pnpm
|
||||||
|
|
||||||
|
- name: Refresh pnpm lockfile
|
||||||
|
run: pnpm install --lockfile-only --ignore-scripts --no-frozen-lockfile
|
||||||
|
|
||||||
|
- name: Fail on unexpected file changes
|
||||||
|
run: |
|
||||||
|
changed="$(git status --porcelain)"
|
||||||
|
if [ -z "$changed" ]; then
|
||||||
|
echo "Lockfile is already up to date."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
if printf '%s\n' "$changed" | grep -Fvq ' pnpm-lock.yaml'; then
|
||||||
|
echo "Unexpected files changed during lockfile refresh:"
|
||||||
|
echo "$changed"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
- name: Create or update pull request
|
||||||
|
env:
|
||||||
|
GH_TOKEN: ${{ github.token }}
|
||||||
|
run: |
|
||||||
|
if git diff --quiet -- pnpm-lock.yaml; then
|
||||||
|
echo "Lockfile unchanged, nothing to do."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
BRANCH="chore/refresh-lockfile"
|
||||||
|
git config user.name "lockfile-bot"
|
||||||
|
git config user.email "lockfile-bot@users.noreply.github.com"
|
||||||
|
|
||||||
|
git checkout -B "$BRANCH"
|
||||||
|
git add pnpm-lock.yaml
|
||||||
|
git commit -m "chore(lockfile): refresh pnpm-lock.yaml"
|
||||||
|
git push --force origin "$BRANCH"
|
||||||
|
|
||||||
|
# Create PR if one doesn't already exist
|
||||||
|
existing=$(gh pr list --head "$BRANCH" --json number --jq '.[0].number')
|
||||||
|
if [ -z "$existing" ]; then
|
||||||
|
gh pr create \
|
||||||
|
--head "$BRANCH" \
|
||||||
|
--title "chore(lockfile): refresh pnpm-lock.yaml" \
|
||||||
|
--body "Auto-generated lockfile refresh after dependencies changed on master. This PR only updates pnpm-lock.yaml."
|
||||||
|
echo "Created new PR."
|
||||||
|
else
|
||||||
|
echo "PR #$existing already exists, branch updated via force push."
|
||||||
|
fi
|
||||||
@@ -78,6 +78,9 @@ If you change schema/API behavior, update all impacted layers:
|
|||||||
4. Do not replace strategic docs wholesale unless asked.
|
4. Do not replace strategic docs wholesale unless asked.
|
||||||
Prefer additive updates. Keep `doc/SPEC.md` and `doc/SPEC-implementation.md` aligned.
|
Prefer additive updates. Keep `doc/SPEC.md` and `doc/SPEC-implementation.md` aligned.
|
||||||
|
|
||||||
|
5. Keep plan docs dated and centralized.
|
||||||
|
New plan documents belong in `doc/plans/` and should use `YYYY-MM-DD-slug.md` filenames.
|
||||||
|
|
||||||
## 6. Database Change Workflow
|
## 6. Database Change Workflow
|
||||||
|
|
||||||
When changing data model:
|
When changing data model:
|
||||||
|
|||||||
@@ -4,7 +4,9 @@ import path from "node:path";
|
|||||||
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
import { afterEach, beforeEach, describe, expect, it } from "vitest";
|
||||||
import {
|
import {
|
||||||
ensureAgentJwtSecret,
|
ensureAgentJwtSecret,
|
||||||
|
mergePaperclipEnvEntries,
|
||||||
readAgentJwtSecretFromEnv,
|
readAgentJwtSecretFromEnv,
|
||||||
|
readPaperclipEnvEntries,
|
||||||
resolveAgentJwtEnvFile,
|
resolveAgentJwtEnvFile,
|
||||||
} from "../config/env.js";
|
} from "../config/env.js";
|
||||||
import { agentJwtSecretCheck } from "../checks/agent-jwt-secret-check.js";
|
import { agentJwtSecretCheck } from "../checks/agent-jwt-secret-check.js";
|
||||||
@@ -58,4 +60,20 @@ describe("agent jwt env helpers", () => {
|
|||||||
const result = agentJwtSecretCheck(configPath);
|
const result = agentJwtSecretCheck(configPath);
|
||||||
expect(result.status).toBe("pass");
|
expect(result.status).toBe("pass");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("quotes hash-prefixed env values so dotenv round-trips them", () => {
|
||||||
|
const configPath = tempConfigPath();
|
||||||
|
const envPath = resolveAgentJwtEnvFile(configPath);
|
||||||
|
|
||||||
|
mergePaperclipEnvEntries(
|
||||||
|
{
|
||||||
|
PAPERCLIP_WORKTREE_COLOR: "#439edb",
|
||||||
|
},
|
||||||
|
envPath,
|
||||||
|
);
|
||||||
|
|
||||||
|
const contents = fs.readFileSync(envPath, "utf-8");
|
||||||
|
expect(contents).toContain('PAPERCLIP_WORKTREE_COLOR="#439edb"');
|
||||||
|
expect(readPaperclipEnvEntries(envPath).PAPERCLIP_WORKTREE_COLOR).toBe("#439edb");
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
copyGitHooksToWorktreeGitDir,
|
copyGitHooksToWorktreeGitDir,
|
||||||
copySeededSecretsKey,
|
copySeededSecretsKey,
|
||||||
rebindWorkspaceCwd,
|
rebindWorkspaceCwd,
|
||||||
|
resolveSourceConfigPath,
|
||||||
resolveGitWorktreeAddArgs,
|
resolveGitWorktreeAddArgs,
|
||||||
resolveWorktreeMakeTargetPath,
|
resolveWorktreeMakeTargetPath,
|
||||||
worktreeInitCommand,
|
worktreeInitCommand,
|
||||||
@@ -16,6 +17,7 @@ import {
|
|||||||
buildWorktreeConfig,
|
buildWorktreeConfig,
|
||||||
buildWorktreeEnvEntries,
|
buildWorktreeEnvEntries,
|
||||||
formatShellExports,
|
formatShellExports,
|
||||||
|
generateWorktreeColor,
|
||||||
resolveWorktreeSeedPlan,
|
resolveWorktreeSeedPlan,
|
||||||
resolveWorktreeLocalPaths,
|
resolveWorktreeLocalPaths,
|
||||||
rewriteLocalUrlPort,
|
rewriteLocalUrlPort,
|
||||||
@@ -181,13 +183,22 @@ describe("worktree helpers", () => {
|
|||||||
path.resolve("/tmp/paperclip-worktrees", "instances", "feature-worktree-support", "data", "storage"),
|
path.resolve("/tmp/paperclip-worktrees", "instances", "feature-worktree-support", "data", "storage"),
|
||||||
);
|
);
|
||||||
|
|
||||||
const env = buildWorktreeEnvEntries(paths);
|
const env = buildWorktreeEnvEntries(paths, {
|
||||||
|
name: "feature-worktree-support",
|
||||||
|
color: "#3abf7a",
|
||||||
|
});
|
||||||
expect(env.PAPERCLIP_HOME).toBe(path.resolve("/tmp/paperclip-worktrees"));
|
expect(env.PAPERCLIP_HOME).toBe(path.resolve("/tmp/paperclip-worktrees"));
|
||||||
expect(env.PAPERCLIP_INSTANCE_ID).toBe("feature-worktree-support");
|
expect(env.PAPERCLIP_INSTANCE_ID).toBe("feature-worktree-support");
|
||||||
expect(env.PAPERCLIP_IN_WORKTREE).toBe("true");
|
expect(env.PAPERCLIP_IN_WORKTREE).toBe("true");
|
||||||
|
expect(env.PAPERCLIP_WORKTREE_NAME).toBe("feature-worktree-support");
|
||||||
|
expect(env.PAPERCLIP_WORKTREE_COLOR).toBe("#3abf7a");
|
||||||
expect(formatShellExports(env)).toContain("export PAPERCLIP_INSTANCE_ID='feature-worktree-support'");
|
expect(formatShellExports(env)).toContain("export PAPERCLIP_INSTANCE_ID='feature-worktree-support'");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("generates vivid worktree colors as hex", () => {
|
||||||
|
expect(generateWorktreeColor()).toMatch(/^#[0-9a-f]{6}$/);
|
||||||
|
});
|
||||||
|
|
||||||
it("uses minimal seed mode to keep app state but drop heavy runtime history", () => {
|
it("uses minimal seed mode to keep app state but drop heavy runtime history", () => {
|
||||||
const minimal = resolveWorktreeSeedPlan("minimal");
|
const minimal = resolveWorktreeSeedPlan("minimal");
|
||||||
const full = resolveWorktreeSeedPlan("full");
|
const full = resolveWorktreeSeedPlan("full");
|
||||||
@@ -280,7 +291,10 @@ describe("worktree helpers", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const envPath = path.join(repoRoot, ".paperclip", ".env");
|
const envPath = path.join(repoRoot, ".paperclip", ".env");
|
||||||
expect(fs.readFileSync(envPath, "utf8")).toContain("PAPERCLIP_AGENT_JWT_SECRET=worktree-shared-secret");
|
const envContents = fs.readFileSync(envPath, "utf8");
|
||||||
|
expect(envContents).toContain("PAPERCLIP_AGENT_JWT_SECRET=worktree-shared-secret");
|
||||||
|
expect(envContents).toContain("PAPERCLIP_WORKTREE_NAME=repo");
|
||||||
|
expect(envContents).toMatch(/PAPERCLIP_WORKTREE_COLOR=\"#[0-9a-f]{6}\"/);
|
||||||
} finally {
|
} finally {
|
||||||
process.chdir(originalCwd);
|
process.chdir(originalCwd);
|
||||||
if (originalJwtSecret === undefined) {
|
if (originalJwtSecret === undefined) {
|
||||||
@@ -292,6 +306,59 @@ describe("worktree helpers", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("defaults the seed source config to the current repo-local Paperclip config", () => {
|
||||||
|
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-worktree-source-config-"));
|
||||||
|
const repoRoot = path.join(tempRoot, "repo");
|
||||||
|
const localConfigPath = path.join(repoRoot, ".paperclip", "config.json");
|
||||||
|
const originalCwd = process.cwd();
|
||||||
|
const originalPaperclipConfig = process.env.PAPERCLIP_CONFIG;
|
||||||
|
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(path.dirname(localConfigPath), { recursive: true });
|
||||||
|
fs.writeFileSync(localConfigPath, JSON.stringify(buildSourceConfig()), "utf8");
|
||||||
|
delete process.env.PAPERCLIP_CONFIG;
|
||||||
|
process.chdir(repoRoot);
|
||||||
|
|
||||||
|
expect(fs.realpathSync(resolveSourceConfigPath({}))).toBe(fs.realpathSync(localConfigPath));
|
||||||
|
} finally {
|
||||||
|
process.chdir(originalCwd);
|
||||||
|
if (originalPaperclipConfig === undefined) {
|
||||||
|
delete process.env.PAPERCLIP_CONFIG;
|
||||||
|
} else {
|
||||||
|
process.env.PAPERCLIP_CONFIG = originalPaperclipConfig;
|
||||||
|
}
|
||||||
|
fs.rmSync(tempRoot, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it("preserves the source config path across worktree:make cwd changes", () => {
|
||||||
|
const tempRoot = fs.mkdtempSync(path.join(os.tmpdir(), "paperclip-worktree-source-override-"));
|
||||||
|
const sourceConfigPath = path.join(tempRoot, "source", "config.json");
|
||||||
|
const targetRoot = path.join(tempRoot, "target");
|
||||||
|
const originalCwd = process.cwd();
|
||||||
|
const originalPaperclipConfig = process.env.PAPERCLIP_CONFIG;
|
||||||
|
|
||||||
|
try {
|
||||||
|
fs.mkdirSync(path.dirname(sourceConfigPath), { recursive: true });
|
||||||
|
fs.mkdirSync(targetRoot, { recursive: true });
|
||||||
|
fs.writeFileSync(sourceConfigPath, JSON.stringify(buildSourceConfig()), "utf8");
|
||||||
|
delete process.env.PAPERCLIP_CONFIG;
|
||||||
|
process.chdir(targetRoot);
|
||||||
|
|
||||||
|
expect(resolveSourceConfigPath({ sourceConfigPathOverride: sourceConfigPath })).toBe(
|
||||||
|
path.resolve(sourceConfigPath),
|
||||||
|
);
|
||||||
|
} finally {
|
||||||
|
process.chdir(originalCwd);
|
||||||
|
if (originalPaperclipConfig === undefined) {
|
||||||
|
delete process.env.PAPERCLIP_CONFIG;
|
||||||
|
} else {
|
||||||
|
process.env.PAPERCLIP_CONFIG = originalPaperclipConfig;
|
||||||
|
}
|
||||||
|
fs.rmSync(tempRoot, { recursive: true, force: true });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
it("rebinds same-repo workspace paths onto the current worktree root", () => {
|
it("rebinds same-repo workspace paths onto the current worktree root", () => {
|
||||||
expect(
|
expect(
|
||||||
rebindWorkspaceCwd({
|
rebindWorkspaceCwd({
|
||||||
|
|||||||
@@ -1,5 +1,9 @@
|
|||||||
import { Command } from "commander";
|
import { Command } from "commander";
|
||||||
import type { Agent } from "@paperclipai/shared";
|
import type { Agent } from "@paperclipai/shared";
|
||||||
|
import {
|
||||||
|
removeMaintainerOnlySkillSymlinks,
|
||||||
|
resolvePaperclipSkillsDir,
|
||||||
|
} from "@paperclipai/adapter-utils/server-utils";
|
||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
@@ -34,15 +38,12 @@ interface SkillsInstallSummary {
|
|||||||
tool: "codex" | "claude";
|
tool: "codex" | "claude";
|
||||||
target: string;
|
target: string;
|
||||||
linked: string[];
|
linked: string[];
|
||||||
|
removed: string[];
|
||||||
skipped: string[];
|
skipped: string[];
|
||||||
failed: Array<{ name: string; error: string }>;
|
failed: Array<{ name: string; error: string }>;
|
||||||
}
|
}
|
||||||
|
|
||||||
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
||||||
const PAPERCLIP_SKILLS_CANDIDATES = [
|
|
||||||
path.resolve(__moduleDir, "../../../../../skills"), // dev: cli/src/commands/client -> repo root/skills
|
|
||||||
path.resolve(process.cwd(), "skills"),
|
|
||||||
];
|
|
||||||
|
|
||||||
function codexSkillsHome(): string {
|
function codexSkillsHome(): string {
|
||||||
const fromEnv = process.env.CODEX_HOME?.trim();
|
const fromEnv = process.env.CODEX_HOME?.trim();
|
||||||
@@ -56,14 +57,6 @@ function claudeSkillsHome(): string {
|
|||||||
return path.join(base, "skills");
|
return path.join(base, "skills");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolvePaperclipSkillsDir(): Promise<string | null> {
|
|
||||||
for (const candidate of PAPERCLIP_SKILLS_CANDIDATES) {
|
|
||||||
const isDir = await fs.stat(candidate).then((s) => s.isDirectory()).catch(() => false);
|
|
||||||
if (isDir) return candidate;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function installSkillsForTarget(
|
async function installSkillsForTarget(
|
||||||
sourceSkillsDir: string,
|
sourceSkillsDir: string,
|
||||||
targetSkillsDir: string,
|
targetSkillsDir: string,
|
||||||
@@ -73,20 +66,65 @@ async function installSkillsForTarget(
|
|||||||
tool,
|
tool,
|
||||||
target: targetSkillsDir,
|
target: targetSkillsDir,
|
||||||
linked: [],
|
linked: [],
|
||||||
|
removed: [],
|
||||||
skipped: [],
|
skipped: [],
|
||||||
failed: [],
|
failed: [],
|
||||||
};
|
};
|
||||||
|
|
||||||
await fs.mkdir(targetSkillsDir, { recursive: true });
|
await fs.mkdir(targetSkillsDir, { recursive: true });
|
||||||
const entries = await fs.readdir(sourceSkillsDir, { withFileTypes: true });
|
const entries = await fs.readdir(sourceSkillsDir, { withFileTypes: true });
|
||||||
|
summary.removed = await removeMaintainerOnlySkillSymlinks(
|
||||||
|
targetSkillsDir,
|
||||||
|
entries.filter((entry) => entry.isDirectory()).map((entry) => entry.name),
|
||||||
|
);
|
||||||
for (const entry of entries) {
|
for (const entry of entries) {
|
||||||
if (!entry.isDirectory()) continue;
|
if (!entry.isDirectory()) continue;
|
||||||
const source = path.join(sourceSkillsDir, entry.name);
|
const source = path.join(sourceSkillsDir, entry.name);
|
||||||
const target = path.join(targetSkillsDir, entry.name);
|
const target = path.join(targetSkillsDir, entry.name);
|
||||||
const existing = await fs.lstat(target).catch(() => null);
|
const existing = await fs.lstat(target).catch(() => null);
|
||||||
if (existing) {
|
if (existing) {
|
||||||
summary.skipped.push(entry.name);
|
if (existing.isSymbolicLink()) {
|
||||||
continue;
|
let linkedPath: string | null = null;
|
||||||
|
try {
|
||||||
|
linkedPath = await fs.readlink(target);
|
||||||
|
} catch (err) {
|
||||||
|
await fs.unlink(target);
|
||||||
|
try {
|
||||||
|
await fs.symlink(source, target);
|
||||||
|
summary.linked.push(entry.name);
|
||||||
|
continue;
|
||||||
|
} catch (linkErr) {
|
||||||
|
summary.failed.push({
|
||||||
|
name: entry.name,
|
||||||
|
error:
|
||||||
|
err instanceof Error && linkErr instanceof Error
|
||||||
|
? `${err.message}; then ${linkErr.message}`
|
||||||
|
: err instanceof Error
|
||||||
|
? err.message
|
||||||
|
: `Failed to recover broken symlink: ${String(err)}`,
|
||||||
|
});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const resolvedLinkedPath = path.isAbsolute(linkedPath)
|
||||||
|
? linkedPath
|
||||||
|
: path.resolve(path.dirname(target), linkedPath);
|
||||||
|
const linkedTargetExists = await fs
|
||||||
|
.stat(resolvedLinkedPath)
|
||||||
|
.then(() => true)
|
||||||
|
.catch(() => false);
|
||||||
|
|
||||||
|
if (!linkedTargetExists) {
|
||||||
|
await fs.unlink(target);
|
||||||
|
} else {
|
||||||
|
summary.skipped.push(entry.name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
summary.skipped.push(entry.name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -210,7 +248,7 @@ export function registerAgentCommands(program: Command): void {
|
|||||||
|
|
||||||
const installSummaries: SkillsInstallSummary[] = [];
|
const installSummaries: SkillsInstallSummary[] = [];
|
||||||
if (opts.installSkills !== false) {
|
if (opts.installSkills !== false) {
|
||||||
const skillsDir = await resolvePaperclipSkillsDir();
|
const skillsDir = await resolvePaperclipSkillsDir(__moduleDir, [path.resolve(process.cwd(), "skills")]);
|
||||||
if (!skillsDir) {
|
if (!skillsDir) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Could not locate local Paperclip skills directory. Expected ./skills in the repo checkout.",
|
"Could not locate local Paperclip skills directory. Expected ./skills in the repo checkout.",
|
||||||
@@ -258,7 +296,7 @@ export function registerAgentCommands(program: Command): void {
|
|||||||
if (installSummaries.length > 0) {
|
if (installSummaries.length > 0) {
|
||||||
for (const summary of installSummaries) {
|
for (const summary of installSummaries) {
|
||||||
console.log(
|
console.log(
|
||||||
`${summary.tool}: linked=${summary.linked.length} skipped=${summary.skipped.length} failed=${summary.failed.length} target=${summary.target}`,
|
`${summary.tool}: linked=${summary.linked.length} removed=${summary.removed.length} skipped=${summary.skipped.length} failed=${summary.failed.length} target=${summary.target}`,
|
||||||
);
|
);
|
||||||
for (const failed of summary.failed) {
|
for (const failed of summary.failed) {
|
||||||
console.log(` failed ${failed.name}: ${failed.error}`);
|
console.log(` failed ${failed.name}: ${failed.error}`);
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { randomInt } from "node:crypto";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import type { PaperclipConfig } from "../config/schema.js";
|
import type { PaperclipConfig } from "../config/schema.js";
|
||||||
import { expandHomePrefix } from "../config/home.js";
|
import { expandHomePrefix } from "../config/home.js";
|
||||||
@@ -44,6 +45,11 @@ export type WorktreeLocalPaths = {
|
|||||||
storageDir: string;
|
storageDir: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export type WorktreeUiBranding = {
|
||||||
|
name: string;
|
||||||
|
color: string;
|
||||||
|
};
|
||||||
|
|
||||||
export function isWorktreeSeedMode(value: string): value is WorktreeSeedMode {
|
export function isWorktreeSeedMode(value: string): value is WorktreeSeedMode {
|
||||||
return (WORKTREE_SEED_MODES as readonly string[]).includes(value);
|
return (WORKTREE_SEED_MODES as readonly string[]).includes(value);
|
||||||
}
|
}
|
||||||
@@ -87,6 +93,51 @@ export function resolveSuggestedWorktreeName(cwd: string, explicitName?: string)
|
|||||||
return nonEmpty(explicitName) ?? path.basename(path.resolve(cwd));
|
return nonEmpty(explicitName) ?? path.basename(path.resolve(cwd));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function hslComponentToHex(n: number): string {
|
||||||
|
return Math.round(Math.max(0, Math.min(255, n)))
|
||||||
|
.toString(16)
|
||||||
|
.padStart(2, "0");
|
||||||
|
}
|
||||||
|
|
||||||
|
function hslToHex(hue: number, saturation: number, lightness: number): string {
|
||||||
|
const s = Math.max(0, Math.min(100, saturation)) / 100;
|
||||||
|
const l = Math.max(0, Math.min(100, lightness)) / 100;
|
||||||
|
const c = (1 - Math.abs((2 * l) - 1)) * s;
|
||||||
|
const h = ((hue % 360) + 360) % 360;
|
||||||
|
const x = c * (1 - Math.abs(((h / 60) % 2) - 1));
|
||||||
|
const m = l - (c / 2);
|
||||||
|
|
||||||
|
let r = 0;
|
||||||
|
let g = 0;
|
||||||
|
let b = 0;
|
||||||
|
|
||||||
|
if (h < 60) {
|
||||||
|
r = c;
|
||||||
|
g = x;
|
||||||
|
} else if (h < 120) {
|
||||||
|
r = x;
|
||||||
|
g = c;
|
||||||
|
} else if (h < 180) {
|
||||||
|
g = c;
|
||||||
|
b = x;
|
||||||
|
} else if (h < 240) {
|
||||||
|
g = x;
|
||||||
|
b = c;
|
||||||
|
} else if (h < 300) {
|
||||||
|
r = x;
|
||||||
|
b = c;
|
||||||
|
} else {
|
||||||
|
r = c;
|
||||||
|
b = x;
|
||||||
|
}
|
||||||
|
|
||||||
|
return `#${hslComponentToHex((r + m) * 255)}${hslComponentToHex((g + m) * 255)}${hslComponentToHex((b + m) * 255)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function generateWorktreeColor(): string {
|
||||||
|
return hslToHex(randomInt(0, 360), 68, 56);
|
||||||
|
}
|
||||||
|
|
||||||
export function resolveWorktreeLocalPaths(opts: {
|
export function resolveWorktreeLocalPaths(opts: {
|
||||||
cwd: string;
|
cwd: string;
|
||||||
homeDir?: string;
|
homeDir?: string;
|
||||||
@@ -196,13 +247,18 @@ export function buildWorktreeConfig(input: {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export function buildWorktreeEnvEntries(paths: WorktreeLocalPaths): Record<string, string> {
|
export function buildWorktreeEnvEntries(
|
||||||
|
paths: WorktreeLocalPaths,
|
||||||
|
branding?: WorktreeUiBranding,
|
||||||
|
): Record<string, string> {
|
||||||
return {
|
return {
|
||||||
PAPERCLIP_HOME: paths.homeDir,
|
PAPERCLIP_HOME: paths.homeDir,
|
||||||
PAPERCLIP_INSTANCE_ID: paths.instanceId,
|
PAPERCLIP_INSTANCE_ID: paths.instanceId,
|
||||||
PAPERCLIP_CONFIG: paths.configPath,
|
PAPERCLIP_CONFIG: paths.configPath,
|
||||||
PAPERCLIP_CONTEXT: paths.contextPath,
|
PAPERCLIP_CONTEXT: paths.contextPath,
|
||||||
PAPERCLIP_IN_WORKTREE: "true",
|
PAPERCLIP_IN_WORKTREE: "true",
|
||||||
|
...(branding?.name ? { PAPERCLIP_WORKTREE_NAME: branding.name } : {}),
|
||||||
|
...(branding?.color ? { PAPERCLIP_WORKTREE_COLOR: branding.color } : {}),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ import {
|
|||||||
buildWorktreeEnvEntries,
|
buildWorktreeEnvEntries,
|
||||||
DEFAULT_WORKTREE_HOME,
|
DEFAULT_WORKTREE_HOME,
|
||||||
formatShellExports,
|
formatShellExports,
|
||||||
|
generateWorktreeColor,
|
||||||
isWorktreeSeedMode,
|
isWorktreeSeedMode,
|
||||||
resolveSuggestedWorktreeName,
|
resolveSuggestedWorktreeName,
|
||||||
resolveWorktreeSeedPlan,
|
resolveWorktreeSeedPlan,
|
||||||
@@ -55,6 +56,7 @@ type WorktreeInitOptions = {
|
|||||||
fromConfig?: string;
|
fromConfig?: string;
|
||||||
fromDataDir?: string;
|
fromDataDir?: string;
|
||||||
fromInstance?: string;
|
fromInstance?: string;
|
||||||
|
sourceConfigPathOverride?: string;
|
||||||
serverPort?: number;
|
serverPort?: number;
|
||||||
dbPort?: number;
|
dbPort?: number;
|
||||||
seed?: boolean;
|
seed?: boolean;
|
||||||
@@ -83,6 +85,7 @@ type EmbeddedPostgresCtor = new (opts: {
|
|||||||
password: string;
|
password: string;
|
||||||
port: number;
|
port: number;
|
||||||
persistent: boolean;
|
persistent: boolean;
|
||||||
|
initdbFlags?: string[];
|
||||||
onLog?: (message: unknown) => void;
|
onLog?: (message: unknown) => void;
|
||||||
onError?: (message: unknown) => void;
|
onError?: (message: unknown) => void;
|
||||||
}) => EmbeddedPostgresInstance;
|
}) => EmbeddedPostgresInstance;
|
||||||
@@ -127,6 +130,8 @@ function isCurrentSourceConfigPath(sourceConfigPath: string): boolean {
|
|||||||
return path.resolve(currentConfigPath) === path.resolve(sourceConfigPath);
|
return path.resolve(currentConfigPath) === path.resolve(sourceConfigPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const WORKTREE_NAME_PREFIX = "paperclip-";
|
||||||
|
|
||||||
function resolveWorktreeMakeName(name: string): string {
|
function resolveWorktreeMakeName(name: string): string {
|
||||||
const value = nonEmpty(name);
|
const value = nonEmpty(name);
|
||||||
if (!value) {
|
if (!value) {
|
||||||
@@ -137,7 +142,15 @@ function resolveWorktreeMakeName(name: string): string {
|
|||||||
"Worktree name must contain only letters, numbers, dots, underscores, or dashes.",
|
"Worktree name must contain only letters, numbers, dots, underscores, or dashes.",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return value;
|
return value.startsWith(WORKTREE_NAME_PREFIX) ? value : `${WORKTREE_NAME_PREFIX}${value}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveWorktreeHome(explicit?: string): string {
|
||||||
|
return explicit ?? process.env.PAPERCLIP_WORKTREES_DIR ?? DEFAULT_WORKTREE_HOME;
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveWorktreeStartPoint(explicit?: string): string | undefined {
|
||||||
|
return explicit ?? nonEmpty(process.env.PAPERCLIP_WORKTREE_START_POINT) ?? undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function resolveWorktreeMakeTargetPath(name: string): string {
|
export function resolveWorktreeMakeTargetPath(name: string): string {
|
||||||
@@ -414,8 +427,12 @@ async function rebindSeededProjectWorkspaces(input: {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolveSourceConfigPath(opts: WorktreeInitOptions): string {
|
export function resolveSourceConfigPath(opts: WorktreeInitOptions): string {
|
||||||
|
if (opts.sourceConfigPathOverride) return path.resolve(opts.sourceConfigPathOverride);
|
||||||
if (opts.fromConfig) return path.resolve(opts.fromConfig);
|
if (opts.fromConfig) return path.resolve(opts.fromConfig);
|
||||||
|
if (!opts.fromDataDir && !opts.fromInstance) {
|
||||||
|
return resolveConfigPath();
|
||||||
|
}
|
||||||
const sourceHome = path.resolve(expandHomePrefix(opts.fromDataDir ?? "~/.paperclip"));
|
const sourceHome = path.resolve(expandHomePrefix(opts.fromDataDir ?? "~/.paperclip"));
|
||||||
const sourceInstanceId = sanitizeWorktreeInstanceId(opts.fromInstance ?? "default");
|
const sourceInstanceId = sanitizeWorktreeInstanceId(opts.fromInstance ?? "default");
|
||||||
return path.resolve(sourceHome, "instances", sourceInstanceId, "config.json");
|
return path.resolve(sourceHome, "instances", sourceInstanceId, "config.json");
|
||||||
@@ -514,6 +531,7 @@ async function ensureEmbeddedPostgres(dataDir: string, preferredPort: number): P
|
|||||||
password: "paperclip",
|
password: "paperclip",
|
||||||
port,
|
port,
|
||||||
persistent: true,
|
persistent: true,
|
||||||
|
initdbFlags: ["--encoding=UTF8", "--locale=C"],
|
||||||
onLog: () => {},
|
onLog: () => {},
|
||||||
onError: () => {},
|
onError: () => {},
|
||||||
});
|
});
|
||||||
@@ -611,7 +629,7 @@ async function seedWorktreeDatabase(input: {
|
|||||||
|
|
||||||
async function runWorktreeInit(opts: WorktreeInitOptions): Promise<void> {
|
async function runWorktreeInit(opts: WorktreeInitOptions): Promise<void> {
|
||||||
const cwd = process.cwd();
|
const cwd = process.cwd();
|
||||||
const name = resolveSuggestedWorktreeName(
|
const worktreeName = resolveSuggestedWorktreeName(
|
||||||
cwd,
|
cwd,
|
||||||
opts.name ?? detectGitBranchName(cwd) ?? undefined,
|
opts.name ?? detectGitBranchName(cwd) ?? undefined,
|
||||||
);
|
);
|
||||||
@@ -619,12 +637,16 @@ async function runWorktreeInit(opts: WorktreeInitOptions): Promise<void> {
|
|||||||
if (!isWorktreeSeedMode(seedMode)) {
|
if (!isWorktreeSeedMode(seedMode)) {
|
||||||
throw new Error(`Unsupported seed mode "${seedMode}". Expected one of: minimal, full.`);
|
throw new Error(`Unsupported seed mode "${seedMode}". Expected one of: minimal, full.`);
|
||||||
}
|
}
|
||||||
const instanceId = sanitizeWorktreeInstanceId(opts.instance ?? name);
|
const instanceId = sanitizeWorktreeInstanceId(opts.instance ?? worktreeName);
|
||||||
const paths = resolveWorktreeLocalPaths({
|
const paths = resolveWorktreeLocalPaths({
|
||||||
cwd,
|
cwd,
|
||||||
homeDir: opts.home ?? DEFAULT_WORKTREE_HOME,
|
homeDir: resolveWorktreeHome(opts.home),
|
||||||
instanceId,
|
instanceId,
|
||||||
});
|
});
|
||||||
|
const branding = {
|
||||||
|
name: worktreeName,
|
||||||
|
color: generateWorktreeColor(),
|
||||||
|
};
|
||||||
const sourceConfigPath = resolveSourceConfigPath(opts);
|
const sourceConfigPath = resolveSourceConfigPath(opts);
|
||||||
const sourceConfig = existsSync(sourceConfigPath) ? readConfig(sourceConfigPath) : null;
|
const sourceConfig = existsSync(sourceConfigPath) ? readConfig(sourceConfigPath) : null;
|
||||||
|
|
||||||
@@ -657,7 +679,7 @@ async function runWorktreeInit(opts: WorktreeInitOptions): Promise<void> {
|
|||||||
nonEmpty(process.env.PAPERCLIP_AGENT_JWT_SECRET);
|
nonEmpty(process.env.PAPERCLIP_AGENT_JWT_SECRET);
|
||||||
mergePaperclipEnvEntries(
|
mergePaperclipEnvEntries(
|
||||||
{
|
{
|
||||||
...buildWorktreeEnvEntries(paths),
|
...buildWorktreeEnvEntries(paths, branding),
|
||||||
...(existingAgentJwtSecret ? { PAPERCLIP_AGENT_JWT_SECRET: existingAgentJwtSecret } : {}),
|
...(existingAgentJwtSecret ? { PAPERCLIP_AGENT_JWT_SECRET: existingAgentJwtSecret } : {}),
|
||||||
},
|
},
|
||||||
paths.envPath,
|
paths.envPath,
|
||||||
@@ -698,6 +720,7 @@ async function runWorktreeInit(opts: WorktreeInitOptions): Promise<void> {
|
|||||||
p.log.message(pc.dim(`Repo env: ${paths.envPath}`));
|
p.log.message(pc.dim(`Repo env: ${paths.envPath}`));
|
||||||
p.log.message(pc.dim(`Isolated home: ${paths.homeDir}`));
|
p.log.message(pc.dim(`Isolated home: ${paths.homeDir}`));
|
||||||
p.log.message(pc.dim(`Instance: ${paths.instanceId}`));
|
p.log.message(pc.dim(`Instance: ${paths.instanceId}`));
|
||||||
|
p.log.message(pc.dim(`Worktree badge: ${branding.name} (${branding.color})`));
|
||||||
p.log.message(pc.dim(`Server port: ${serverPort} | DB port: ${databasePort}`));
|
p.log.message(pc.dim(`Server port: ${serverPort} | DB port: ${databasePort}`));
|
||||||
if (copiedGitHooks?.copied) {
|
if (copiedGitHooks?.copied) {
|
||||||
p.log.message(
|
p.log.message(
|
||||||
@@ -731,15 +754,17 @@ export async function worktreeMakeCommand(nameArg: string, opts: WorktreeMakeOpt
|
|||||||
p.intro(pc.bgCyan(pc.black(" paperclipai worktree:make ")));
|
p.intro(pc.bgCyan(pc.black(" paperclipai worktree:make ")));
|
||||||
|
|
||||||
const name = resolveWorktreeMakeName(nameArg);
|
const name = resolveWorktreeMakeName(nameArg);
|
||||||
|
const startPoint = resolveWorktreeStartPoint(opts.startPoint);
|
||||||
const sourceCwd = process.cwd();
|
const sourceCwd = process.cwd();
|
||||||
|
const sourceConfigPath = resolveSourceConfigPath(opts);
|
||||||
const targetPath = resolveWorktreeMakeTargetPath(name);
|
const targetPath = resolveWorktreeMakeTargetPath(name);
|
||||||
if (existsSync(targetPath)) {
|
if (existsSync(targetPath)) {
|
||||||
throw new Error(`Target path already exists: ${targetPath}`);
|
throw new Error(`Target path already exists: ${targetPath}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
mkdirSync(path.dirname(targetPath), { recursive: true });
|
mkdirSync(path.dirname(targetPath), { recursive: true });
|
||||||
if (opts.startPoint) {
|
if (startPoint) {
|
||||||
const [remote] = opts.startPoint.split("/", 1);
|
const [remote] = startPoint.split("/", 1);
|
||||||
try {
|
try {
|
||||||
execFileSync("git", ["fetch", remote], {
|
execFileSync("git", ["fetch", remote], {
|
||||||
cwd: sourceCwd,
|
cwd: sourceCwd,
|
||||||
@@ -755,8 +780,8 @@ export async function worktreeMakeCommand(nameArg: string, opts: WorktreeMakeOpt
|
|||||||
const worktreeArgs = resolveGitWorktreeAddArgs({
|
const worktreeArgs = resolveGitWorktreeAddArgs({
|
||||||
branchName: name,
|
branchName: name,
|
||||||
targetPath,
|
targetPath,
|
||||||
branchExists: !opts.startPoint && localBranchExists(sourceCwd, name),
|
branchExists: !startPoint && localBranchExists(sourceCwd, name),
|
||||||
startPoint: opts.startPoint,
|
startPoint,
|
||||||
});
|
});
|
||||||
|
|
||||||
const spinner = p.spinner();
|
const spinner = p.spinner();
|
||||||
@@ -791,6 +816,7 @@ export async function worktreeMakeCommand(nameArg: string, opts: WorktreeMakeOpt
|
|||||||
await runWorktreeInit({
|
await runWorktreeInit({
|
||||||
...opts,
|
...opts,
|
||||||
name,
|
name,
|
||||||
|
sourceConfigPathOverride: sourceConfigPath,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
throw error;
|
throw error;
|
||||||
@@ -799,6 +825,232 @@ export async function worktreeMakeCommand(nameArg: string, opts: WorktreeMakeOpt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type WorktreeCleanupOptions = {
|
||||||
|
instance?: string;
|
||||||
|
home?: string;
|
||||||
|
force?: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
type GitWorktreeListEntry = {
|
||||||
|
worktree: string;
|
||||||
|
branch: string | null;
|
||||||
|
bare: boolean;
|
||||||
|
detached: boolean;
|
||||||
|
};
|
||||||
|
|
||||||
|
function parseGitWorktreeList(cwd: string): GitWorktreeListEntry[] {
|
||||||
|
const raw = execFileSync("git", ["worktree", "list", "--porcelain"], {
|
||||||
|
cwd,
|
||||||
|
encoding: "utf8",
|
||||||
|
stdio: ["ignore", "pipe", "pipe"],
|
||||||
|
});
|
||||||
|
const entries: GitWorktreeListEntry[] = [];
|
||||||
|
let current: Partial<GitWorktreeListEntry> = {};
|
||||||
|
for (const line of raw.split("\n")) {
|
||||||
|
if (line.startsWith("worktree ")) {
|
||||||
|
current = { worktree: line.slice("worktree ".length) };
|
||||||
|
} else if (line.startsWith("branch ")) {
|
||||||
|
current.branch = line.slice("branch ".length);
|
||||||
|
} else if (line === "bare") {
|
||||||
|
current.bare = true;
|
||||||
|
} else if (line === "detached") {
|
||||||
|
current.detached = true;
|
||||||
|
} else if (line === "" && current.worktree) {
|
||||||
|
entries.push({
|
||||||
|
worktree: current.worktree,
|
||||||
|
branch: current.branch ?? null,
|
||||||
|
bare: current.bare ?? false,
|
||||||
|
detached: current.detached ?? false,
|
||||||
|
});
|
||||||
|
current = {};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (current.worktree) {
|
||||||
|
entries.push({
|
||||||
|
worktree: current.worktree,
|
||||||
|
branch: current.branch ?? null,
|
||||||
|
bare: current.bare ?? false,
|
||||||
|
detached: current.detached ?? false,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
function branchHasUniqueCommits(cwd: string, branchName: string): boolean {
|
||||||
|
try {
|
||||||
|
const output = execFileSync(
|
||||||
|
"git",
|
||||||
|
["log", "--oneline", branchName, "--not", "--remotes", "--exclude", `refs/heads/${branchName}`, "--branches"],
|
||||||
|
{ cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] },
|
||||||
|
).trim();
|
||||||
|
return output.length > 0;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function branchExistsOnAnyRemote(cwd: string, branchName: string): boolean {
|
||||||
|
try {
|
||||||
|
const output = execFileSync(
|
||||||
|
"git",
|
||||||
|
["branch", "-r", "--list", `*/${branchName}`],
|
||||||
|
{ cwd, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] },
|
||||||
|
).trim();
|
||||||
|
return output.length > 0;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function worktreePathHasUncommittedChanges(worktreePath: string): boolean {
|
||||||
|
try {
|
||||||
|
const output = execFileSync(
|
||||||
|
"git",
|
||||||
|
["status", "--porcelain"],
|
||||||
|
{ cwd: worktreePath, encoding: "utf8", stdio: ["ignore", "pipe", "pipe"] },
|
||||||
|
).trim();
|
||||||
|
return output.length > 0;
|
||||||
|
} catch {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function worktreeCleanupCommand(nameArg: string, opts: WorktreeCleanupOptions): Promise<void> {
|
||||||
|
printPaperclipCliBanner();
|
||||||
|
p.intro(pc.bgCyan(pc.black(" paperclipai worktree:cleanup ")));
|
||||||
|
|
||||||
|
const name = resolveWorktreeMakeName(nameArg);
|
||||||
|
const sourceCwd = process.cwd();
|
||||||
|
const targetPath = resolveWorktreeMakeTargetPath(name);
|
||||||
|
const instanceId = sanitizeWorktreeInstanceId(opts.instance ?? name);
|
||||||
|
const homeDir = path.resolve(expandHomePrefix(resolveWorktreeHome(opts.home)));
|
||||||
|
const instanceRoot = path.resolve(homeDir, "instances", instanceId);
|
||||||
|
|
||||||
|
// ── 1. Assess current state ──────────────────────────────────────────
|
||||||
|
|
||||||
|
const hasBranch = localBranchExists(sourceCwd, name);
|
||||||
|
const hasTargetDir = existsSync(targetPath);
|
||||||
|
const hasInstanceData = existsSync(instanceRoot);
|
||||||
|
|
||||||
|
const worktrees = parseGitWorktreeList(sourceCwd);
|
||||||
|
const linkedWorktree = worktrees.find(
|
||||||
|
(wt) => wt.branch === `refs/heads/${name}` || path.resolve(wt.worktree) === path.resolve(targetPath),
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!hasBranch && !hasTargetDir && !hasInstanceData && !linkedWorktree) {
|
||||||
|
p.log.info("Nothing to clean up — no branch, worktree directory, or instance data found.");
|
||||||
|
p.outro(pc.green("Already clean."));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── 2. Safety checks ────────────────────────────────────────────────
|
||||||
|
|
||||||
|
const problems: string[] = [];
|
||||||
|
|
||||||
|
if (hasBranch && branchHasUniqueCommits(sourceCwd, name)) {
|
||||||
|
const onRemote = branchExistsOnAnyRemote(sourceCwd, name);
|
||||||
|
if (onRemote) {
|
||||||
|
p.log.info(
|
||||||
|
`Branch "${name}" has unique local commits, but the branch also exists on a remote — safe to delete locally.`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
problems.push(
|
||||||
|
`Branch "${name}" has commits not found on any other branch or remote. ` +
|
||||||
|
`Deleting it will lose work. Push it first, or use --force.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (hasTargetDir && worktreePathHasUncommittedChanges(targetPath)) {
|
||||||
|
problems.push(
|
||||||
|
`Worktree directory ${targetPath} has uncommitted changes. Commit or stash first, or use --force.`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (problems.length > 0 && !opts.force) {
|
||||||
|
for (const problem of problems) {
|
||||||
|
p.log.error(problem);
|
||||||
|
}
|
||||||
|
throw new Error("Safety checks failed. Resolve the issues above or re-run with --force.");
|
||||||
|
}
|
||||||
|
if (problems.length > 0 && opts.force) {
|
||||||
|
for (const problem of problems) {
|
||||||
|
p.log.warning(`Overridden by --force: ${problem}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ── 3. Clean up (idempotent steps) ──────────────────────────────────
|
||||||
|
|
||||||
|
// 3a. Remove the git worktree registration
|
||||||
|
if (linkedWorktree) {
|
||||||
|
const worktreeDirExists = existsSync(linkedWorktree.worktree);
|
||||||
|
const spinner = p.spinner();
|
||||||
|
if (worktreeDirExists) {
|
||||||
|
spinner.start(`Removing git worktree at ${linkedWorktree.worktree}...`);
|
||||||
|
try {
|
||||||
|
const removeArgs = ["worktree", "remove", linkedWorktree.worktree];
|
||||||
|
if (opts.force) removeArgs.push("--force");
|
||||||
|
execFileSync("git", removeArgs, {
|
||||||
|
cwd: sourceCwd,
|
||||||
|
stdio: ["ignore", "pipe", "pipe"],
|
||||||
|
});
|
||||||
|
spinner.stop(`Removed git worktree at ${linkedWorktree.worktree}.`);
|
||||||
|
} catch (error) {
|
||||||
|
spinner.stop(pc.yellow(`Could not remove worktree cleanly, will prune instead.`));
|
||||||
|
p.log.warning(extractExecSyncErrorMessage(error) ?? String(error));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
spinner.start("Pruning stale worktree entry...");
|
||||||
|
execFileSync("git", ["worktree", "prune"], {
|
||||||
|
cwd: sourceCwd,
|
||||||
|
stdio: ["ignore", "pipe", "pipe"],
|
||||||
|
});
|
||||||
|
spinner.stop("Pruned stale worktree entry.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Even without a linked worktree, prune to clean up any orphaned entries
|
||||||
|
execFileSync("git", ["worktree", "prune"], {
|
||||||
|
cwd: sourceCwd,
|
||||||
|
stdio: ["ignore", "pipe", "pipe"],
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3b. Remove the worktree directory if it still exists (e.g. partial creation)
|
||||||
|
if (existsSync(targetPath)) {
|
||||||
|
const spinner = p.spinner();
|
||||||
|
spinner.start(`Removing worktree directory ${targetPath}...`);
|
||||||
|
rmSync(targetPath, { recursive: true, force: true });
|
||||||
|
spinner.stop(`Removed worktree directory ${targetPath}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3c. Delete the local branch (now safe — worktree is gone)
|
||||||
|
if (localBranchExists(sourceCwd, name)) {
|
||||||
|
const spinner = p.spinner();
|
||||||
|
spinner.start(`Deleting local branch "${name}"...`);
|
||||||
|
try {
|
||||||
|
const deleteFlag = opts.force ? "-D" : "-d";
|
||||||
|
execFileSync("git", ["branch", deleteFlag, name], {
|
||||||
|
cwd: sourceCwd,
|
||||||
|
stdio: ["ignore", "pipe", "pipe"],
|
||||||
|
});
|
||||||
|
spinner.stop(`Deleted local branch "${name}".`);
|
||||||
|
} catch (error) {
|
||||||
|
spinner.stop(pc.yellow(`Could not delete branch "${name}".`));
|
||||||
|
p.log.warning(extractExecSyncErrorMessage(error) ?? String(error));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3d. Remove instance data
|
||||||
|
if (existsSync(instanceRoot)) {
|
||||||
|
const spinner = p.spinner();
|
||||||
|
spinner.start(`Removing instance data at ${instanceRoot}...`);
|
||||||
|
rmSync(instanceRoot, { recursive: true, force: true });
|
||||||
|
spinner.stop(`Removed instance data at ${instanceRoot}.`);
|
||||||
|
}
|
||||||
|
|
||||||
|
p.outro(pc.green("Cleanup complete."));
|
||||||
|
}
|
||||||
|
|
||||||
export async function worktreeEnvCommand(opts: WorktreeEnvOptions): Promise<void> {
|
export async function worktreeEnvCommand(opts: WorktreeEnvOptions): Promise<void> {
|
||||||
const configPath = resolveConfigPath(opts.config);
|
const configPath = resolveConfigPath(opts.config);
|
||||||
const envPath = resolvePaperclipEnvFile(configPath);
|
const envPath = resolvePaperclipEnvFile(configPath);
|
||||||
@@ -825,10 +1077,10 @@ export function registerWorktreeCommands(program: Command): void {
|
|||||||
program
|
program
|
||||||
.command("worktree:make")
|
.command("worktree:make")
|
||||||
.description("Create ~/NAME as a git worktree, then initialize an isolated Paperclip instance inside it")
|
.description("Create ~/NAME as a git worktree, then initialize an isolated Paperclip instance inside it")
|
||||||
.argument("<name>", "Worktree directory and branch name (created at ~/NAME)")
|
.argument("<name>", "Worktree name — auto-prefixed with paperclip- if needed (created at ~/paperclip-NAME)")
|
||||||
.option("--start-point <ref>", "Remote ref to base the new branch on (e.g. origin/main)")
|
.option("--start-point <ref>", "Remote ref to base the new branch on (env: PAPERCLIP_WORKTREE_START_POINT)")
|
||||||
.option("--instance <id>", "Explicit isolated instance id")
|
.option("--instance <id>", "Explicit isolated instance id")
|
||||||
.option("--home <path>", `Home root for worktree instances (default: ${DEFAULT_WORKTREE_HOME})`)
|
.option("--home <path>", `Home root for worktree instances (env: PAPERCLIP_WORKTREES_DIR, default: ${DEFAULT_WORKTREE_HOME})`)
|
||||||
.option("--from-config <path>", "Source config.json to seed from")
|
.option("--from-config <path>", "Source config.json to seed from")
|
||||||
.option("--from-data-dir <path>", "Source PAPERCLIP_HOME used when deriving the source config")
|
.option("--from-data-dir <path>", "Source PAPERCLIP_HOME used when deriving the source config")
|
||||||
.option("--from-instance <id>", "Source instance id when deriving the source config", "default")
|
.option("--from-instance <id>", "Source instance id when deriving the source config", "default")
|
||||||
@@ -844,7 +1096,7 @@ export function registerWorktreeCommands(program: Command): void {
|
|||||||
.description("Create repo-local config/env and an isolated instance for this worktree")
|
.description("Create repo-local config/env and an isolated instance for this worktree")
|
||||||
.option("--name <name>", "Display name used to derive the instance id")
|
.option("--name <name>", "Display name used to derive the instance id")
|
||||||
.option("--instance <id>", "Explicit isolated instance id")
|
.option("--instance <id>", "Explicit isolated instance id")
|
||||||
.option("--home <path>", `Home root for worktree instances (default: ${DEFAULT_WORKTREE_HOME})`)
|
.option("--home <path>", `Home root for worktree instances (env: PAPERCLIP_WORKTREES_DIR, default: ${DEFAULT_WORKTREE_HOME})`)
|
||||||
.option("--from-config <path>", "Source config.json to seed from")
|
.option("--from-config <path>", "Source config.json to seed from")
|
||||||
.option("--from-data-dir <path>", "Source PAPERCLIP_HOME used when deriving the source config")
|
.option("--from-data-dir <path>", "Source PAPERCLIP_HOME used when deriving the source config")
|
||||||
.option("--from-instance <id>", "Source instance id when deriving the source config", "default")
|
.option("--from-instance <id>", "Source instance id when deriving the source config", "default")
|
||||||
@@ -861,4 +1113,13 @@ export function registerWorktreeCommands(program: Command): void {
|
|||||||
.option("-c, --config <path>", "Path to config file")
|
.option("-c, --config <path>", "Path to config file")
|
||||||
.option("--json", "Print JSON instead of shell exports")
|
.option("--json", "Print JSON instead of shell exports")
|
||||||
.action(worktreeEnvCommand);
|
.action(worktreeEnvCommand);
|
||||||
|
|
||||||
|
program
|
||||||
|
.command("worktree:cleanup")
|
||||||
|
.description("Safely remove a worktree, its branch, and its isolated instance data")
|
||||||
|
.argument("<name>", "Worktree name — auto-prefixed with paperclip- if needed")
|
||||||
|
.option("--instance <id>", "Explicit instance id (if different from the worktree name)")
|
||||||
|
.option("--home <path>", `Home root for worktree instances (env: PAPERCLIP_WORKTREES_DIR, default: ${DEFAULT_WORKTREE_HOME})`)
|
||||||
|
.option("--force", "Bypass safety checks (uncommitted changes, unique commits)", false)
|
||||||
|
.action(worktreeCleanupCommand);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,11 +22,18 @@ function parseEnvFile(contents: string) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function formatEnvValue(value: string): string {
|
||||||
|
if (/^[A-Za-z0-9_./:@-]+$/.test(value)) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
return JSON.stringify(value);
|
||||||
|
}
|
||||||
|
|
||||||
function renderEnvFile(entries: Record<string, string>) {
|
function renderEnvFile(entries: Record<string, string>) {
|
||||||
const lines = [
|
const lines = [
|
||||||
"# Paperclip environment variables",
|
"# Paperclip environment variables",
|
||||||
"# Generated by Paperclip CLI commands",
|
"# Generated by Paperclip CLI commands",
|
||||||
...Object.entries(entries).map(([key, value]) => `${key}=${value}`),
|
...Object.entries(entries).map(([key, value]) => `${key}=${formatEnvValue(value)}`),
|
||||||
"",
|
"",
|
||||||
];
|
];
|
||||||
return lines.join("\n");
|
return lines.join("\n");
|
||||||
|
|||||||
@@ -19,9 +19,9 @@ Current implementation status:
|
|||||||
|
|
||||||
GitHub Actions owns `pnpm-lock.yaml`.
|
GitHub Actions owns `pnpm-lock.yaml`.
|
||||||
|
|
||||||
- Same-repo pull requests that change dependency manifests are auto-refreshed by GitHub Actions before merge.
|
- Do not commit `pnpm-lock.yaml` in pull requests.
|
||||||
- Fork pull requests that change dependency manifests must include the refreshed `pnpm-lock.yaml`.
|
- Pull request CI validates dependency resolution when manifests change.
|
||||||
- Pull request CI validates lockfile freshness when manifests change and verifies with `--frozen-lockfile`.
|
- Pushes to `master` regenerate `pnpm-lock.yaml` with `pnpm install --lockfile-only --no-frozen-lockfile`, commit it back if needed, and then run verification with `--frozen-lockfile`.
|
||||||
|
|
||||||
## Start Dev
|
## Start Dev
|
||||||
|
|
||||||
@@ -142,7 +142,7 @@ This command:
|
|||||||
- creates an isolated instance under `~/.paperclip-worktrees/instances/<worktree-id>/`
|
- creates an isolated instance under `~/.paperclip-worktrees/instances/<worktree-id>/`
|
||||||
- when run inside a linked git worktree, mirrors the effective git hooks into that worktree's private git dir
|
- when run inside a linked git worktree, mirrors the effective git hooks into that worktree's private git dir
|
||||||
- picks a free app port and embedded PostgreSQL port
|
- picks a free app port and embedded PostgreSQL port
|
||||||
- by default seeds the isolated DB in `minimal` mode from your main instance via a logical SQL snapshot
|
- by default seeds the isolated DB in `minimal` mode from the current effective Paperclip instance/config (repo-local worktree config when present, otherwise the default instance) via a logical SQL snapshot
|
||||||
|
|
||||||
Seed modes:
|
Seed modes:
|
||||||
|
|
||||||
@@ -152,7 +152,13 @@ Seed modes:
|
|||||||
|
|
||||||
After `worktree init`, both the server and the CLI auto-load the repo-local `.paperclip/.env` when run inside that worktree, so normal commands like `pnpm dev`, `paperclipai doctor`, and `paperclipai db:backup` stay scoped to the worktree instance.
|
After `worktree init`, both the server and the CLI auto-load the repo-local `.paperclip/.env` when run inside that worktree, so normal commands like `pnpm dev`, `paperclipai doctor`, and `paperclipai db:backup` stay scoped to the worktree instance.
|
||||||
|
|
||||||
That repo-local env also sets `PAPERCLIP_IN_WORKTREE=true`, which the server can use for worktree-specific UI behavior such as an alternate favicon.
|
That repo-local env also sets:
|
||||||
|
|
||||||
|
- `PAPERCLIP_IN_WORKTREE=true`
|
||||||
|
- `PAPERCLIP_WORKTREE_NAME=<worktree-name>`
|
||||||
|
- `PAPERCLIP_WORKTREE_COLOR=<hex-color>`
|
||||||
|
|
||||||
|
The server/UI use those values for worktree-specific branding such as the top banner and dynamically colored favicon.
|
||||||
|
|
||||||
Print shell exports explicitly when needed:
|
Print shell exports explicitly when needed:
|
||||||
|
|
||||||
|
|||||||
@@ -94,3 +94,53 @@ Canonical mode design and command expectations live in `doc/DEPLOYMENT-MODES.md`
|
|||||||
## Further Detail
|
## Further Detail
|
||||||
|
|
||||||
See [SPEC.md](./SPEC.md) for the full technical specification and [TASKS.md](./TASKS.md) for the task management data model.
|
See [SPEC.md](./SPEC.md) for the full technical specification and [TASKS.md](./TASKS.md) for the task management data model.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Paperclip’s core identity is a **control plane for autonomous AI companies**, centered on **companies, org charts, goals, issues/comments, heartbeats, budgets, approvals, and board governance**. The public docs are also explicit about the current boundaries: **tasks/comments are the built-in communication model**, Paperclip is **not a chatbot**, and it is **not a code review tool**. The roadmap already points toward **easier onboarding, cloud agents, easier agent configuration, plugins, better docs, and ClipMart/ClipHub-style reusable companies/templates**.
|
||||||
|
|
||||||
|
## What Paperclip should do vs. not do
|
||||||
|
|
||||||
|
**Do**
|
||||||
|
|
||||||
|
- Stay **board-level and company-level**. Users should manage goals, orgs, budgets, approvals, and outputs.
|
||||||
|
- Make the first five minutes feel magical: install, answer a few questions, see a CEO do something real.
|
||||||
|
- Keep work anchored to **issues/comments/projects/goals**, even if the surface feels conversational.
|
||||||
|
- Treat **agency / internal team / startup** as the same underlying abstraction with different templates and labels.
|
||||||
|
- Make outputs first-class: files, docs, reports, previews, links, screenshots.
|
||||||
|
- Provide **hooks into engineering workflows**: worktrees, preview servers, PR links, external review tools.
|
||||||
|
- Use **plugins** for edge cases like rich chat, knowledge bases, doc editors, custom tracing.
|
||||||
|
|
||||||
|
**Do not**
|
||||||
|
|
||||||
|
- Do not make the core product a general chat app. The current product definition is explicitly task/comment-centric and “not a chatbot,” and that boundary is valuable.
|
||||||
|
- Do not build a complete Jira/GitHub replacement. The repo/docs already position Paperclip as organization orchestration, not focused on pull-request review.
|
||||||
|
- Do not build enterprise-grade RBAC first. The current V1 spec still treats multi-board governance and fine-grained human permissions as out of scope, so the first multi-user version should be coarse and company-scoped.
|
||||||
|
- Do not lead with raw bash logs and transcripts. Default view should be human-readable intent/progress, with raw detail beneath.
|
||||||
|
- Do not force users to understand provider/API-key plumbing unless absolutely necessary. There are active onboarding/auth issues already; friction here is clearly real.
|
||||||
|
|
||||||
|
## Specific design goals
|
||||||
|
|
||||||
|
1. **Time-to-first-success under 5 minutes**
|
||||||
|
A fresh user should go from install to “my CEO completed a first task” in one sitting.
|
||||||
|
|
||||||
|
2. **Board-level abstraction always wins**
|
||||||
|
The default UI should answer: what is the company doing, who is doing it, why does it matter, what did it cost, and what needs my approval.
|
||||||
|
|
||||||
|
3. **Conversation stays attached to work objects**
|
||||||
|
“Chat with CEO” should still resolve to strategy threads, decisions, tasks, or approvals.
|
||||||
|
|
||||||
|
4. **Progressive disclosure**
|
||||||
|
Top layer: human-readable summary. Middle layer: checklist/steps/artifacts. Bottom layer: raw logs/tool calls/transcript.
|
||||||
|
|
||||||
|
5. **Output-first**
|
||||||
|
Work is not done until the user can see the result: file, document, preview link, screenshot, plan, or PR.
|
||||||
|
|
||||||
|
6. **Local-first, cloud-ready**
|
||||||
|
The mental model should not change between local solo use and shared/private or public/cloud deployment.
|
||||||
|
|
||||||
|
7. **Safe autonomy**
|
||||||
|
Auto mode is allowed; hidden token burn is not.
|
||||||
|
|
||||||
|
8. **Thin core, rich edges**
|
||||||
|
Put optional chat, knowledge, and special surfaces into plugins/extensions rather than bloating the control plane.
|
||||||
|
|||||||
@@ -330,6 +330,34 @@ Operational policy:
|
|||||||
- `asset_id` uuid fk not null
|
- `asset_id` uuid fk not null
|
||||||
- `issue_comment_id` uuid fk null
|
- `issue_comment_id` uuid fk null
|
||||||
|
|
||||||
|
## 7.15 `documents` + `document_revisions` + `issue_documents`
|
||||||
|
|
||||||
|
- `documents` stores editable text-first documents:
|
||||||
|
- `id` uuid pk
|
||||||
|
- `company_id` uuid fk not null
|
||||||
|
- `title` text null
|
||||||
|
- `format` text not null (`markdown`)
|
||||||
|
- `latest_body` text not null
|
||||||
|
- `latest_revision_id` uuid null
|
||||||
|
- `latest_revision_number` int not null
|
||||||
|
- `created_by_agent_id` uuid fk null
|
||||||
|
- `created_by_user_id` uuid/text fk null
|
||||||
|
- `updated_by_agent_id` uuid fk null
|
||||||
|
- `updated_by_user_id` uuid/text fk null
|
||||||
|
- `document_revisions` stores append-only history:
|
||||||
|
- `id` uuid pk
|
||||||
|
- `company_id` uuid fk not null
|
||||||
|
- `document_id` uuid fk not null
|
||||||
|
- `revision_number` int not null
|
||||||
|
- `body` text not null
|
||||||
|
- `change_summary` text null
|
||||||
|
- `issue_documents` links documents to issues with a stable workflow key:
|
||||||
|
- `id` uuid pk
|
||||||
|
- `company_id` uuid fk not null
|
||||||
|
- `issue_id` uuid fk not null
|
||||||
|
- `document_id` uuid fk not null
|
||||||
|
- `key` text not null (`plan`, `design`, `notes`, etc.)
|
||||||
|
|
||||||
## 8. State Machines
|
## 8. State Machines
|
||||||
|
|
||||||
## 8.1 Agent Status
|
## 8.1 Agent Status
|
||||||
@@ -441,6 +469,11 @@ All endpoints are under `/api` and return JSON.
|
|||||||
- `POST /companies/:companyId/issues`
|
- `POST /companies/:companyId/issues`
|
||||||
- `GET /issues/:issueId`
|
- `GET /issues/:issueId`
|
||||||
- `PATCH /issues/:issueId`
|
- `PATCH /issues/:issueId`
|
||||||
|
- `GET /issues/:issueId/documents`
|
||||||
|
- `GET /issues/:issueId/documents/:key`
|
||||||
|
- `PUT /issues/:issueId/documents/:key`
|
||||||
|
- `GET /issues/:issueId/documents/:key/revisions`
|
||||||
|
- `DELETE /issues/:issueId/documents/:key`
|
||||||
- `POST /issues/:issueId/checkout`
|
- `POST /issues/:issueId/checkout`
|
||||||
- `POST /issues/:issueId/release`
|
- `POST /issues/:issueId/release`
|
||||||
- `POST /issues/:issueId/comments`
|
- `POST /issues/:issueId/comments`
|
||||||
|
|||||||
397
doc/plans/2026-03-13-TOKEN-OPTIMIZATION-PLAN.md
Normal file
397
doc/plans/2026-03-13-TOKEN-OPTIMIZATION-PLAN.md
Normal file
@@ -0,0 +1,397 @@
|
|||||||
|
# Token Optimization Plan
|
||||||
|
|
||||||
|
Date: 2026-03-13
|
||||||
|
Related discussion: https://github.com/paperclipai/paperclip/discussions/449
|
||||||
|
|
||||||
|
## Goal
|
||||||
|
|
||||||
|
Reduce token consumption materially without reducing agent capability, control-plane visibility, or task completion quality.
|
||||||
|
|
||||||
|
This plan is based on:
|
||||||
|
|
||||||
|
- the current V1 control-plane design
|
||||||
|
- the current adapter and heartbeat implementation
|
||||||
|
- the linked user discussion
|
||||||
|
- local runtime data from the default Paperclip instance on 2026-03-13
|
||||||
|
|
||||||
|
## Executive Summary
|
||||||
|
|
||||||
|
The discussion is directionally right about two things:
|
||||||
|
|
||||||
|
1. We should preserve session and prompt-cache locality more aggressively.
|
||||||
|
2. We should separate stable startup instructions from per-heartbeat dynamic context.
|
||||||
|
|
||||||
|
But that is not enough on its own.
|
||||||
|
|
||||||
|
After reviewing the code and local run data, the token problem appears to have four distinct causes:
|
||||||
|
|
||||||
|
1. **Measurement inflation on sessioned adapters.** Some token counters, especially for `codex_local`, appear to be recorded as cumulative session totals instead of per-heartbeat deltas.
|
||||||
|
2. **Avoidable session resets.** Task sessions are intentionally reset on timer wakes and manual wakes, which destroys cache locality for common heartbeat paths.
|
||||||
|
3. **Repeated context reacquisition.** The `paperclip` skill tells agents to re-fetch assignments, issue details, ancestors, and full comment threads on every heartbeat. The API does not currently offer efficient delta-oriented alternatives.
|
||||||
|
4. **Large static instruction surfaces.** Agent instruction files and globally injected skills are reintroduced at startup even when most of that content is unchanged and not needed for the current task.
|
||||||
|
|
||||||
|
The correct approach is:
|
||||||
|
|
||||||
|
1. fix telemetry so we can trust the numbers
|
||||||
|
2. preserve reuse where it is safe
|
||||||
|
3. make context retrieval incremental
|
||||||
|
4. add session compaction/rotation so long-lived sessions do not become progressively more expensive
|
||||||
|
|
||||||
|
## Validated Findings
|
||||||
|
|
||||||
|
### 1. Token telemetry is at least partly overstated today
|
||||||
|
|
||||||
|
Observed from the local default instance:
|
||||||
|
|
||||||
|
- `heartbeat_runs`: 11,360 runs between 2026-02-18 and 2026-03-13
|
||||||
|
- summed `usage_json.inputTokens`: `2,272,142,368,952`
|
||||||
|
- summed `usage_json.cachedInputTokens`: `2,217,501,559,420`
|
||||||
|
|
||||||
|
Those totals are not credible as true per-heartbeat usage for the observed prompt sizes.
|
||||||
|
|
||||||
|
Supporting evidence:
|
||||||
|
|
||||||
|
- `adapter.invoke.payload.prompt` averages were small:
|
||||||
|
- `codex_local`: ~193 chars average, 6,067 chars max
|
||||||
|
- `claude_local`: ~160 chars average, 1,160 chars max
|
||||||
|
- despite that, many `codex_local` runs report millions of input tokens
|
||||||
|
- one reused Codex session in local data spans 3,607 runs and recorded `inputTokens` growing up to `1,155,283,166`
|
||||||
|
|
||||||
|
Interpretation:
|
||||||
|
|
||||||
|
- for sessioned adapters, especially Codex, we are likely storing usage reported by the runtime as a **session total**, not a **per-run delta**
|
||||||
|
- this makes trend reporting, optimization work, and customer trust worse
|
||||||
|
|
||||||
|
This does **not** mean there is no real token problem. It means we need a trustworthy baseline before we can judge optimization impact.
|
||||||
|
|
||||||
|
### 2. Timer wakes currently throw away reusable task sessions
|
||||||
|
|
||||||
|
In `server/src/services/heartbeat.ts`, `shouldResetTaskSessionForWake(...)` returns `true` for:
|
||||||
|
|
||||||
|
- `wakeReason === "issue_assigned"`
|
||||||
|
- `wakeSource === "timer"`
|
||||||
|
- manual on-demand wakes
|
||||||
|
|
||||||
|
That means many normal heartbeats skip saved task-session resume even when the workspace is stable.
|
||||||
|
|
||||||
|
Local data supports the impact:
|
||||||
|
|
||||||
|
- `timer/system` runs: 6,587 total
|
||||||
|
- only 976 had a previous session
|
||||||
|
- only 963 ended with the same session
|
||||||
|
|
||||||
|
So timer wakes are the largest heartbeat path and are mostly not resuming prior task state.
|
||||||
|
|
||||||
|
### 3. We repeatedly ask agents to reload the same task context
|
||||||
|
|
||||||
|
The `paperclip` skill currently tells agents to do this on essentially every heartbeat:
|
||||||
|
|
||||||
|
- fetch assignments
|
||||||
|
- fetch issue details
|
||||||
|
- fetch ancestor chain
|
||||||
|
- fetch full issue comments
|
||||||
|
|
||||||
|
Current API shape reinforces that pattern:
|
||||||
|
|
||||||
|
- `GET /api/issues/:id/comments` returns the full thread
|
||||||
|
- there is no `since`, cursor, digest, or summary endpoint for heartbeat consumption
|
||||||
|
- `GET /api/issues/:id` returns full enriched issue context, not a minimal delta payload
|
||||||
|
|
||||||
|
This is safe but expensive. It forces the model to repeatedly consume unchanged information.
|
||||||
|
|
||||||
|
### 4. Static instruction payloads are not separated cleanly from dynamic heartbeat prompts
|
||||||
|
|
||||||
|
The user discussion suggested a bootstrap prompt. That is the right direction.
|
||||||
|
|
||||||
|
Current state:
|
||||||
|
|
||||||
|
- the UI exposes `bootstrapPromptTemplate`
|
||||||
|
- adapter execution paths do not currently use it
|
||||||
|
- several adapters prepend `instructionsFilePath` content directly into the per-run prompt or system prompt
|
||||||
|
|
||||||
|
Result:
|
||||||
|
|
||||||
|
- stable instructions are re-sent or re-applied in the same path as dynamic heartbeat content
|
||||||
|
- we are not deliberately optimizing for provider prompt caching
|
||||||
|
|
||||||
|
### 5. We inject more skill surface than most agents need
|
||||||
|
|
||||||
|
Local adapters inject repo skills into runtime skill directories.
|
||||||
|
|
||||||
|
Important `codex_local` nuance:
|
||||||
|
|
||||||
|
- Codex does not read skills directly from the active worktree.
|
||||||
|
- Paperclip discovers repo skills from the current checkout, then symlinks them into `$CODEX_HOME/skills` or `~/.codex/skills`.
|
||||||
|
- If an existing Paperclip skill symlink already points at another live checkout, the current implementation skips it instead of repointing it.
|
||||||
|
- This can leave Codex using stale skill content from a different worktree even after Paperclip-side skill changes land.
|
||||||
|
- That is both a correctness risk and a token-analysis risk, because runtime behavior may not reflect the instructions in the checkout being tested.
|
||||||
|
|
||||||
|
Current repo skill sizes:
|
||||||
|
|
||||||
|
- `skills/paperclip/SKILL.md`: 17,441 bytes
|
||||||
|
- `.agents/skills/create-agent-adapter/SKILL.md`: 31,832 bytes
|
||||||
|
- `skills/paperclip-create-agent/SKILL.md`: 4,718 bytes
|
||||||
|
- `skills/para-memory-files/SKILL.md`: 3,978 bytes
|
||||||
|
|
||||||
|
That is nearly 58 KB of skill markdown before any company-specific instructions.
|
||||||
|
|
||||||
|
Not all of that is necessarily loaded into model context every run, but it increases startup surface area and should be treated as a token budget concern.
|
||||||
|
|
||||||
|
## Principles
|
||||||
|
|
||||||
|
We should optimize tokens under these rules:
|
||||||
|
|
||||||
|
1. **Do not lose functionality.** Agents must still be able to resume work safely, understand why tasks exist, and act within governance rules.
|
||||||
|
2. **Prefer stable context over repeated context.** Unchanged instructions should not be resent through the most expensive path.
|
||||||
|
3. **Prefer deltas over full reloads.** Heartbeats should consume only what changed since the last useful run.
|
||||||
|
4. **Measure normalized deltas, not raw adapter claims.** Especially for sessioned CLIs.
|
||||||
|
5. **Keep escape hatches.** Board/manual runs may still want a forced fresh session.
|
||||||
|
|
||||||
|
## Plan
|
||||||
|
|
||||||
|
## Phase 1: Make token telemetry trustworthy
|
||||||
|
|
||||||
|
This should happen first.
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
- Store both:
|
||||||
|
- raw adapter-reported usage
|
||||||
|
- Paperclip-normalized per-run usage
|
||||||
|
- For sessioned adapters, compute normalized deltas against prior usage for the same persisted session.
|
||||||
|
- Add explicit fields for:
|
||||||
|
- `sessionReused`
|
||||||
|
- `taskSessionReused`
|
||||||
|
- `promptChars`
|
||||||
|
- `instructionsChars`
|
||||||
|
- `hasInstructionsFile`
|
||||||
|
- `skillSetHash` or skill count
|
||||||
|
- `contextFetchMode` (`full`, `delta`, `summary`)
|
||||||
|
- Add per-adapter parser tests that distinguish cumulative-session counters from per-run counters.
|
||||||
|
|
||||||
|
### Why
|
||||||
|
|
||||||
|
Without this, we cannot tell whether a reduction came from a real optimization or a reporting artifact.
|
||||||
|
|
||||||
|
### Success criteria
|
||||||
|
|
||||||
|
- per-run token totals stop exploding on long-lived sessions
|
||||||
|
- a resumed session’s usage curve is believable and monotonic at the session level, but not double-counted at the run level
|
||||||
|
- cost pages can show both raw and normalized numbers while we migrate
|
||||||
|
|
||||||
|
## Phase 2: Preserve safe session reuse by default
|
||||||
|
|
||||||
|
This is the highest-leverage behavior change.
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
- Stop resetting task sessions on ordinary timer wakes.
|
||||||
|
- Keep resetting on:
|
||||||
|
- explicit manual “fresh run” invocations
|
||||||
|
- assignment changes
|
||||||
|
- workspace mismatch
|
||||||
|
- model mismatch / invalid resume errors
|
||||||
|
- Add an explicit wake flag like `forceFreshSession: true` when the board wants a reset.
|
||||||
|
- Record why a session was reused or reset in run metadata.
|
||||||
|
|
||||||
|
### Why
|
||||||
|
|
||||||
|
Timer wakes are the dominant heartbeat path. Resetting them destroys both session continuity and prompt cache reuse.
|
||||||
|
|
||||||
|
### Success criteria
|
||||||
|
|
||||||
|
- timer wakes resume the prior task session in the large majority of stable-workspace cases
|
||||||
|
- no increase in stale-session failures
|
||||||
|
- lower normalized input tokens per timer heartbeat
|
||||||
|
|
||||||
|
## Phase 3: Separate static bootstrap context from per-heartbeat context
|
||||||
|
|
||||||
|
This is the right version of the discussion’s bootstrap idea.
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
- Implement `bootstrapPromptTemplate` in adapter execution paths.
|
||||||
|
- Use it only when starting a fresh session, not on resumed sessions.
|
||||||
|
- Keep `promptTemplate` intentionally small and stable:
|
||||||
|
- who I am
|
||||||
|
- what triggered this wake
|
||||||
|
- which task/comment/approval to prioritize
|
||||||
|
- Move long-lived setup text out of recurring per-run prompts where possible.
|
||||||
|
- Add UI guidance and warnings when `promptTemplate` contains high-churn or large inline content.
|
||||||
|
|
||||||
|
### Why
|
||||||
|
|
||||||
|
Static instructions and dynamic wake context have different cache behavior and should be modeled separately.
|
||||||
|
|
||||||
|
For `codex_local`, this also requires isolating the Codex skill home per worktree or teaching Paperclip to repoint its own skill symlinks when the source checkout changes. Otherwise prompt and skill improvements in the active worktree may not reach the running agent.
|
||||||
|
|
||||||
|
### Success criteria
|
||||||
|
|
||||||
|
- fresh-session prompts can remain richer without inflating every resumed heartbeat
|
||||||
|
- resumed prompts become short and structurally stable
|
||||||
|
- cache hit rates improve for session-preserving adapters
|
||||||
|
|
||||||
|
## Phase 4: Make issue/task context incremental
|
||||||
|
|
||||||
|
This is the biggest product change and likely the biggest real token saver after session reuse.
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
Add heartbeat-oriented endpoints and skill behavior:
|
||||||
|
|
||||||
|
- `GET /api/agents/me/inbox-lite`
|
||||||
|
- minimal assignment list
|
||||||
|
- issue id, identifier, status, priority, updatedAt, lastExternalCommentAt
|
||||||
|
- `GET /api/issues/:id/heartbeat-context`
|
||||||
|
- compact issue state
|
||||||
|
- parent-chain summary
|
||||||
|
- latest execution summary
|
||||||
|
- change markers
|
||||||
|
- `GET /api/issues/:id/comments?after=<cursor>` or `?since=<timestamp>`
|
||||||
|
- return only new comments
|
||||||
|
- optional `GET /api/issues/:id/context-digest`
|
||||||
|
- server-generated compact summary for heartbeat use
|
||||||
|
|
||||||
|
Update the `paperclip` skill so the default pattern becomes:
|
||||||
|
|
||||||
|
1. fetch compact inbox
|
||||||
|
2. fetch compact task context
|
||||||
|
3. fetch only new comments unless this is the first read, a mention-triggered wake, or a cache miss
|
||||||
|
4. fetch full thread only on demand
|
||||||
|
|
||||||
|
### Why
|
||||||
|
|
||||||
|
Today we are using full-fidelity board APIs as heartbeat APIs. That is convenient but token-inefficient.
|
||||||
|
|
||||||
|
### Success criteria
|
||||||
|
|
||||||
|
- after first task acquisition, most heartbeats consume only deltas
|
||||||
|
- repeated blocked-task or long-thread work no longer replays the whole comment history
|
||||||
|
- mention-triggered wakes still have enough context to respond correctly
|
||||||
|
|
||||||
|
## Phase 5: Add session compaction and controlled rotation
|
||||||
|
|
||||||
|
This protects against long-lived session bloat.
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
- Add rotation thresholds per adapter/session:
|
||||||
|
- turns
|
||||||
|
- normalized input tokens
|
||||||
|
- age
|
||||||
|
- cache hit degradation
|
||||||
|
- Before rotating, produce a structured carry-forward summary:
|
||||||
|
- current objective
|
||||||
|
- work completed
|
||||||
|
- open decisions
|
||||||
|
- blockers
|
||||||
|
- files/artifacts touched
|
||||||
|
- next recommended action
|
||||||
|
- Persist that summary in task session state or runtime state.
|
||||||
|
- Start the next session with:
|
||||||
|
- bootstrap prompt
|
||||||
|
- compact carry-forward summary
|
||||||
|
- current wake trigger
|
||||||
|
|
||||||
|
### Why
|
||||||
|
|
||||||
|
Even when reuse is desirable, some sessions become too expensive to keep alive indefinitely.
|
||||||
|
|
||||||
|
### Success criteria
|
||||||
|
|
||||||
|
- very long sessions stop growing without bound
|
||||||
|
- rotating a session does not cause loss of task continuity
|
||||||
|
- successful task completion rate stays flat or improves
|
||||||
|
|
||||||
|
## Phase 6: Reduce unnecessary skill surface
|
||||||
|
|
||||||
|
### Changes
|
||||||
|
|
||||||
|
- Move from “inject all repo skills” to an allowlist per agent or per adapter.
|
||||||
|
- Default local runtime skill set should likely be:
|
||||||
|
- `paperclip`
|
||||||
|
- Add opt-in skills for specialized agents:
|
||||||
|
- `paperclip-create-agent`
|
||||||
|
- `para-memory-files`
|
||||||
|
- `create-agent-adapter`
|
||||||
|
- Expose active skill set in agent config and run metadata.
|
||||||
|
- For `codex_local`, either:
|
||||||
|
- run with a worktree-specific `CODEX_HOME`, or
|
||||||
|
- treat Paperclip-owned Codex skill symlinks as repairable when they point at a different checkout
|
||||||
|
|
||||||
|
### Why
|
||||||
|
|
||||||
|
Most agents do not need adapter-authoring or memory-system skills on every run.
|
||||||
|
|
||||||
|
### Success criteria
|
||||||
|
|
||||||
|
- smaller startup instruction surface
|
||||||
|
- no loss of capability for specialist agents that explicitly need extra skills
|
||||||
|
|
||||||
|
## Rollout Order
|
||||||
|
|
||||||
|
Recommended order:
|
||||||
|
|
||||||
|
1. telemetry normalization
|
||||||
|
2. timer-wake session reuse
|
||||||
|
3. bootstrap prompt implementation
|
||||||
|
4. heartbeat delta APIs + `paperclip` skill rewrite
|
||||||
|
5. session compaction/rotation
|
||||||
|
6. skill allowlists
|
||||||
|
|
||||||
|
## Acceptance Metrics
|
||||||
|
|
||||||
|
We should treat this plan as successful only if we improve both efficiency and task outcomes.
|
||||||
|
|
||||||
|
Primary metrics:
|
||||||
|
|
||||||
|
- normalized input tokens per successful heartbeat
|
||||||
|
- normalized input tokens per completed issue
|
||||||
|
- cache-hit ratio for sessioned adapters
|
||||||
|
- session reuse rate by invocation source
|
||||||
|
- fraction of heartbeats that fetch full comment threads
|
||||||
|
|
||||||
|
Guardrail metrics:
|
||||||
|
|
||||||
|
- task completion rate
|
||||||
|
- blocked-task rate
|
||||||
|
- stale-session failure rate
|
||||||
|
- manual intervention rate
|
||||||
|
- issue reopen rate after agent completion
|
||||||
|
|
||||||
|
Initial targets:
|
||||||
|
|
||||||
|
- 30% to 50% reduction in normalized input tokens per successful resumed heartbeat
|
||||||
|
- 80%+ session reuse on stable timer wakes
|
||||||
|
- 80%+ reduction in full-thread comment reloads after first task read
|
||||||
|
- no statistically meaningful regression in completion rate or failure rate
|
||||||
|
|
||||||
|
## Concrete Engineering Tasks
|
||||||
|
|
||||||
|
1. Add normalized usage fields and migration support for run analytics.
|
||||||
|
2. Patch sessioned adapter accounting to compute deltas from prior session totals.
|
||||||
|
3. Change `shouldResetTaskSessionForWake(...)` so timer wakes do not reset by default.
|
||||||
|
4. Implement `bootstrapPromptTemplate` end-to-end in adapter execution.
|
||||||
|
5. Add compact heartbeat context and incremental comment APIs.
|
||||||
|
6. Rewrite `skills/paperclip/SKILL.md` around delta-fetch behavior.
|
||||||
|
7. Add session rotation with carry-forward summaries.
|
||||||
|
8. Replace global skill injection with explicit allowlists.
|
||||||
|
9. Fix `codex_local` skill resolution so worktree-local skill changes reliably reach the runtime.
|
||||||
|
|
||||||
|
## Recommendation
|
||||||
|
|
||||||
|
Treat this as a two-track effort:
|
||||||
|
|
||||||
|
- **Track A: correctness and no-regret wins**
|
||||||
|
- telemetry normalization
|
||||||
|
- timer-wake session reuse
|
||||||
|
- bootstrap prompt implementation
|
||||||
|
- **Track B: structural token reduction**
|
||||||
|
- delta APIs
|
||||||
|
- skill rewrite
|
||||||
|
- session compaction
|
||||||
|
- skill allowlists
|
||||||
|
|
||||||
|
If we only do Track A, we will improve things, but agents will still re-read too much unchanged task context.
|
||||||
|
|
||||||
|
If we only do Track B without fixing telemetry first, we will not be able to prove the gains cleanly.
|
||||||
775
doc/plans/2026-03-13-agent-evals-framework.md
Normal file
775
doc/plans/2026-03-13-agent-evals-framework.md
Normal file
@@ -0,0 +1,775 @@
|
|||||||
|
# Agent Evals Framework Plan
|
||||||
|
|
||||||
|
Date: 2026-03-13
|
||||||
|
|
||||||
|
## Context
|
||||||
|
|
||||||
|
We need evals for the thing Paperclip actually ships:
|
||||||
|
|
||||||
|
- agent behavior produced by adapter config
|
||||||
|
- prompt templates and bootstrap prompts
|
||||||
|
- skill sets and skill instructions
|
||||||
|
- model choice
|
||||||
|
- runtime policy choices that affect outcomes and cost
|
||||||
|
|
||||||
|
We do **not** primarily need a fine-tuning pipeline.
|
||||||
|
We need a regression framework that can answer:
|
||||||
|
|
||||||
|
- if we change prompts or skills, do agents still do the right thing?
|
||||||
|
- if we switch models, what got better, worse, or more expensive?
|
||||||
|
- if we optimize tokens, did we preserve task outcomes?
|
||||||
|
- can we grow the suite over time from real Paperclip usage?
|
||||||
|
|
||||||
|
This plan is based on:
|
||||||
|
|
||||||
|
- `doc/GOAL.md`
|
||||||
|
- `doc/PRODUCT.md`
|
||||||
|
- `doc/SPEC-implementation.md`
|
||||||
|
- `docs/agents-runtime.md`
|
||||||
|
- `doc/plans/2026-03-13-TOKEN-OPTIMIZATION-PLAN.md`
|
||||||
|
- Discussion #449: <https://github.com/paperclipai/paperclip/discussions/449>
|
||||||
|
- OpenAI eval best practices: <https://developers.openai.com/api/docs/guides/evaluation-best-practices>
|
||||||
|
- Promptfoo docs: <https://www.promptfoo.dev/docs/configuration/test-cases/> and <https://www.promptfoo.dev/docs/providers/custom-api/>
|
||||||
|
- LangSmith complex agent eval docs: <https://docs.langchain.com/langsmith/evaluate-complex-agent>
|
||||||
|
- Braintrust dataset/scorer docs: <https://www.braintrust.dev/docs/annotate/datasets> and <https://www.braintrust.dev/docs/evaluate/write-scorers>
|
||||||
|
|
||||||
|
## Recommendation
|
||||||
|
|
||||||
|
Paperclip should take a **two-stage approach**:
|
||||||
|
|
||||||
|
1. **Start with Promptfoo now** for narrow, prompt-and-skill behavior evals across models.
|
||||||
|
2. **Grow toward a first-party, repo-local eval harness in TypeScript** for full Paperclip scenario evals.
|
||||||
|
|
||||||
|
So the recommendation is no longer “skip Promptfoo.” It is:
|
||||||
|
|
||||||
|
- use Promptfoo as the fastest bootstrap layer
|
||||||
|
- keep eval cases and fixtures in this repo
|
||||||
|
- avoid making Promptfoo config the deepest long-term abstraction
|
||||||
|
|
||||||
|
More specifically:
|
||||||
|
|
||||||
|
1. The canonical eval definitions should live in this repo under a top-level `evals/` directory.
|
||||||
|
2. `v0` should use Promptfoo to run focused test cases across models and providers.
|
||||||
|
3. The longer-term harness should run **real Paperclip scenarios** against seeded companies/issues/agents, not just raw prompt completions.
|
||||||
|
4. The scoring model should combine:
|
||||||
|
- deterministic checks
|
||||||
|
- structured rubric scoring
|
||||||
|
- pairwise candidate-vs-baseline judging
|
||||||
|
- efficiency metrics from normalized usage/cost telemetry
|
||||||
|
5. The framework should compare **bundles**, not just models.
|
||||||
|
|
||||||
|
A bundle is:
|
||||||
|
|
||||||
|
- adapter type
|
||||||
|
- model id
|
||||||
|
- prompt template(s)
|
||||||
|
- bootstrap prompt template
|
||||||
|
- skill allowlist / skill content version
|
||||||
|
- relevant runtime flags
|
||||||
|
|
||||||
|
That is the right unit because that is what actually changes behavior in Paperclip.
|
||||||
|
|
||||||
|
## Why This Is The Right Shape
|
||||||
|
|
||||||
|
### 1. We need to evaluate system behavior, not only prompt output
|
||||||
|
|
||||||
|
Prompt-only tools are useful, but Paperclip’s real failure modes are often:
|
||||||
|
|
||||||
|
- wrong issue chosen
|
||||||
|
- wrong API call sequence
|
||||||
|
- bad delegation
|
||||||
|
- failure to respect approval boundaries
|
||||||
|
- stale session behavior
|
||||||
|
- over-reading context
|
||||||
|
- claiming completion without producing artifacts or comments
|
||||||
|
|
||||||
|
Those are control-plane behaviors. They require scenario setup, execution, and trace inspection.
|
||||||
|
|
||||||
|
### 2. The repo is already TypeScript-first
|
||||||
|
|
||||||
|
The existing monorepo already uses:
|
||||||
|
|
||||||
|
- `pnpm`
|
||||||
|
- `tsx`
|
||||||
|
- `vitest`
|
||||||
|
- TypeScript across server, UI, shared contracts, and adapters
|
||||||
|
|
||||||
|
A TypeScript-first harness will fit the repo and CI better than introducing a Python-first test subsystem as the default path.
|
||||||
|
|
||||||
|
Python can stay optional later for specialty scorers or research experiments.
|
||||||
|
|
||||||
|
### 3. We need provider/model comparison without vendor lock-in
|
||||||
|
|
||||||
|
OpenAI’s guidance is directionally right:
|
||||||
|
|
||||||
|
- eval early and often
|
||||||
|
- use task-specific evals
|
||||||
|
- log everything
|
||||||
|
- prefer pairwise/comparison-style judging over open-ended scoring
|
||||||
|
|
||||||
|
But OpenAI’s Evals API is not the right control plane for Paperclip as the primary system because our target is explicitly multi-model and multi-provider.
|
||||||
|
|
||||||
|
### 4. Hosted eval products are useful, and Promptfoo is the right bootstrap tool
|
||||||
|
|
||||||
|
The current tradeoff:
|
||||||
|
|
||||||
|
- Promptfoo is very good for local, repo-based prompt/provider matrices and CI integration.
|
||||||
|
- LangSmith is strong on trajectory-style agent evals.
|
||||||
|
- Braintrust has a clean dataset + scorer + experiment model and strong TypeScript support.
|
||||||
|
|
||||||
|
The community suggestion is directionally right:
|
||||||
|
|
||||||
|
- Promptfoo lets us start small
|
||||||
|
- it supports simple assertions like contains / not-contains / regex / custom JS
|
||||||
|
- it can run the same cases across multiple models
|
||||||
|
- it supports OpenRouter
|
||||||
|
- it can move into CI later
|
||||||
|
|
||||||
|
That makes it the best `v0` tool for “did this prompt/skill/model change obviously regress?”
|
||||||
|
|
||||||
|
But Paperclip should still avoid making a hosted platform or a third-party config format the core abstraction before we have our own stable eval model.
|
||||||
|
|
||||||
|
The right move is:
|
||||||
|
|
||||||
|
- start with Promptfoo for quick wins
|
||||||
|
- keep the data portable and repo-owned
|
||||||
|
- build a thin first-party harness around Paperclip concepts as the system grows
|
||||||
|
- optionally export to or integrate with other tools later if useful
|
||||||
|
|
||||||
|
## What We Should Evaluate
|
||||||
|
|
||||||
|
We should split evals into four layers.
|
||||||
|
|
||||||
|
### Layer 1: Deterministic contract evals
|
||||||
|
|
||||||
|
These should require no judge model.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
- agent comments on the assigned issue
|
||||||
|
- no mutation outside the agent’s company
|
||||||
|
- approval-required actions do not bypass approval flow
|
||||||
|
- task transitions are legal
|
||||||
|
- output contains required structured fields
|
||||||
|
- artifact links exist when the task required an artifact
|
||||||
|
- no full-thread refetch on delta-only cases once the API supports it
|
||||||
|
|
||||||
|
These are cheap, reliable, and should be the first line of defense.
|
||||||
|
|
||||||
|
### Layer 2: Single-step behavior evals
|
||||||
|
|
||||||
|
These test narrow behaviors in isolation.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
- chooses the correct issue from inbox
|
||||||
|
- writes a reasonable first status comment
|
||||||
|
- decides to ask for approval instead of acting directly
|
||||||
|
- delegates to the correct report
|
||||||
|
- recognizes blocked state and reports it clearly
|
||||||
|
|
||||||
|
These are the closest thing to prompt evals, but still framed in Paperclip terms.
|
||||||
|
|
||||||
|
### Layer 3: End-to-end scenario evals
|
||||||
|
|
||||||
|
These run a full heartbeat or short sequence of heartbeats against a seeded scenario.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
- new assignment pickup
|
||||||
|
- long-thread continuation
|
||||||
|
- mention-triggered clarification
|
||||||
|
- approval-gated hire request
|
||||||
|
- manager escalation
|
||||||
|
- workspace coding task that must leave a meaningful issue update
|
||||||
|
|
||||||
|
These should evaluate both final state and trace quality.
|
||||||
|
|
||||||
|
### Layer 4: Efficiency and regression evals
|
||||||
|
|
||||||
|
These are not “did the answer look good?” evals. They are “did we preserve quality while improving cost/latency?” evals.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
- normalized input tokens per successful heartbeat
|
||||||
|
- normalized tokens per completed issue
|
||||||
|
- session reuse rate
|
||||||
|
- full-thread reload rate
|
||||||
|
- wall-clock duration
|
||||||
|
- cost per successful scenario
|
||||||
|
|
||||||
|
This layer is especially important for token optimization work.
|
||||||
|
|
||||||
|
## Core Design
|
||||||
|
|
||||||
|
## 1. Canonical object: `EvalCase`
|
||||||
|
|
||||||
|
Each eval case should define:
|
||||||
|
|
||||||
|
- scenario setup
|
||||||
|
- target bundle(s)
|
||||||
|
- execution mode
|
||||||
|
- expected invariants
|
||||||
|
- scoring rubric
|
||||||
|
- tags/metadata
|
||||||
|
|
||||||
|
Suggested shape:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type EvalCase = {
|
||||||
|
id: string;
|
||||||
|
description: string;
|
||||||
|
tags: string[];
|
||||||
|
setup: {
|
||||||
|
fixture: string;
|
||||||
|
agentId: string;
|
||||||
|
trigger: "assignment" | "timer" | "on_demand" | "comment" | "approval";
|
||||||
|
};
|
||||||
|
inputs?: Record<string, unknown>;
|
||||||
|
checks: {
|
||||||
|
hard: HardCheck[];
|
||||||
|
rubric?: RubricCheck[];
|
||||||
|
pairwise?: PairwiseCheck[];
|
||||||
|
};
|
||||||
|
metrics: MetricSpec[];
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
The important part is that the case is about a Paperclip scenario, not a standalone prompt string.
|
||||||
|
|
||||||
|
## 2. Canonical object: `EvalBundle`
|
||||||
|
|
||||||
|
Suggested shape:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type EvalBundle = {
|
||||||
|
id: string;
|
||||||
|
adapter: string;
|
||||||
|
model: string;
|
||||||
|
promptTemplate: string;
|
||||||
|
bootstrapPromptTemplate?: string;
|
||||||
|
skills: string[];
|
||||||
|
flags?: Record<string, string | number | boolean>;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Every comparison run should say which bundle was tested.
|
||||||
|
|
||||||
|
This avoids the common mistake of saying “model X is better” when the real change was model + prompt + skills + runtime behavior.
|
||||||
|
|
||||||
|
## 3. Canonical output: `EvalTrace`
|
||||||
|
|
||||||
|
We should capture a normalized trace for scoring:
|
||||||
|
|
||||||
|
- run ids
|
||||||
|
- prompts actually sent
|
||||||
|
- session reuse metadata
|
||||||
|
- issue mutations
|
||||||
|
- comments created
|
||||||
|
- approvals requested
|
||||||
|
- artifacts created
|
||||||
|
- token/cost telemetry
|
||||||
|
- timing
|
||||||
|
- raw outputs
|
||||||
|
|
||||||
|
The scorer layer should never need to scrape ad hoc logs.
|
||||||
|
|
||||||
|
## Scoring Framework
|
||||||
|
|
||||||
|
## 1. Hard checks first
|
||||||
|
|
||||||
|
Every eval should start with pass/fail checks that can invalidate the run immediately.
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
- touched wrong company
|
||||||
|
- skipped required approval
|
||||||
|
- no issue update produced
|
||||||
|
- returned malformed structured output
|
||||||
|
- marked task done without required artifact
|
||||||
|
|
||||||
|
If a hard check fails, the scenario fails regardless of style or judge score.
|
||||||
|
|
||||||
|
## 2. Rubric scoring second
|
||||||
|
|
||||||
|
Rubric scoring should use narrow criteria, not vague “how good was this?” prompts.
|
||||||
|
|
||||||
|
Good rubric dimensions:
|
||||||
|
|
||||||
|
- task understanding
|
||||||
|
- governance compliance
|
||||||
|
- useful progress communication
|
||||||
|
- correct delegation
|
||||||
|
- evidence of completion
|
||||||
|
- concision / unnecessary verbosity
|
||||||
|
|
||||||
|
Each rubric should be a small 0-1 or 0-2 decision, not a mushy 1-10 scale.
|
||||||
|
|
||||||
|
## 3. Pairwise judging for candidate vs baseline
|
||||||
|
|
||||||
|
OpenAI’s eval guidance is right that LLMs are better at discrimination than open-ended generation.
|
||||||
|
|
||||||
|
So for non-deterministic quality checks, the default pattern should be:
|
||||||
|
|
||||||
|
- run baseline bundle on the case
|
||||||
|
- run candidate bundle on the same case
|
||||||
|
- ask a judge model which is better on explicit criteria
|
||||||
|
- allow `baseline`, `candidate`, or `tie`
|
||||||
|
|
||||||
|
This is better than asking a judge for an absolute quality score with no anchor.
|
||||||
|
|
||||||
|
## 4. Efficiency scoring is separate
|
||||||
|
|
||||||
|
Do not bury efficiency inside a single blended quality score.
|
||||||
|
|
||||||
|
Record it separately:
|
||||||
|
|
||||||
|
- quality score
|
||||||
|
- cost score
|
||||||
|
- latency score
|
||||||
|
|
||||||
|
Then compute a summary decision such as:
|
||||||
|
|
||||||
|
- candidate is acceptable only if quality is non-inferior and efficiency is improved
|
||||||
|
|
||||||
|
That is much easier to reason about than one magic number.
|
||||||
|
|
||||||
|
## Suggested Decision Rule
|
||||||
|
|
||||||
|
For PR gating:
|
||||||
|
|
||||||
|
1. No hard-check regressions.
|
||||||
|
2. No significant regression on required scenario pass rate.
|
||||||
|
3. No significant regression on key rubric dimensions.
|
||||||
|
4. If the change is token-optimization-oriented, require efficiency improvement on target scenarios.
|
||||||
|
|
||||||
|
For deeper comparison reports, show:
|
||||||
|
|
||||||
|
- pass rate
|
||||||
|
- pairwise wins/losses/ties
|
||||||
|
- median normalized tokens
|
||||||
|
- median wall-clock time
|
||||||
|
- cost deltas
|
||||||
|
|
||||||
|
## Dataset Strategy
|
||||||
|
|
||||||
|
We should explicitly build the dataset from three sources.
|
||||||
|
|
||||||
|
### 1. Hand-authored seed cases
|
||||||
|
|
||||||
|
Start here.
|
||||||
|
|
||||||
|
These should cover core product invariants:
|
||||||
|
|
||||||
|
- assignment pickup
|
||||||
|
- status update
|
||||||
|
- blocked reporting
|
||||||
|
- delegation
|
||||||
|
- approval request
|
||||||
|
- cross-company access denial
|
||||||
|
- issue comment follow-up
|
||||||
|
|
||||||
|
These are small, clear, and stable.
|
||||||
|
|
||||||
|
### 2. Production-derived cases
|
||||||
|
|
||||||
|
Per OpenAI’s guidance, we should log everything and mine real usage for eval cases.
|
||||||
|
|
||||||
|
Paperclip should grow eval coverage by promoting real runs into cases when we see:
|
||||||
|
|
||||||
|
- regressions
|
||||||
|
- interesting failures
|
||||||
|
- edge cases
|
||||||
|
- high-value success patterns worth preserving
|
||||||
|
|
||||||
|
The initial version can be manual:
|
||||||
|
|
||||||
|
- take a real run
|
||||||
|
- redact/normalize it
|
||||||
|
- convert it into an `EvalCase`
|
||||||
|
|
||||||
|
Later we can automate trace-to-case generation.
|
||||||
|
|
||||||
|
### 3. Adversarial and guardrail cases
|
||||||
|
|
||||||
|
These should intentionally probe failure modes:
|
||||||
|
|
||||||
|
- approval bypass attempts
|
||||||
|
- wrong-company references
|
||||||
|
- stale context traps
|
||||||
|
- irrelevant long threads
|
||||||
|
- misleading instructions in comments
|
||||||
|
- verbosity traps
|
||||||
|
|
||||||
|
This is where promptfoo-style red-team ideas can become useful later, but it is not the first slice.
|
||||||
|
|
||||||
|
## Repo Layout
|
||||||
|
|
||||||
|
Recommended initial layout:
|
||||||
|
|
||||||
|
```text
|
||||||
|
evals/
|
||||||
|
README.md
|
||||||
|
promptfoo/
|
||||||
|
promptfooconfig.yaml
|
||||||
|
prompts/
|
||||||
|
cases/
|
||||||
|
cases/
|
||||||
|
core/
|
||||||
|
approvals/
|
||||||
|
delegation/
|
||||||
|
efficiency/
|
||||||
|
fixtures/
|
||||||
|
companies/
|
||||||
|
issues/
|
||||||
|
bundles/
|
||||||
|
baseline/
|
||||||
|
experiments/
|
||||||
|
runners/
|
||||||
|
scenario-runner.ts
|
||||||
|
compare-runner.ts
|
||||||
|
scorers/
|
||||||
|
hard/
|
||||||
|
rubric/
|
||||||
|
pairwise/
|
||||||
|
judges/
|
||||||
|
rubric-judge.ts
|
||||||
|
pairwise-judge.ts
|
||||||
|
lib/
|
||||||
|
types.ts
|
||||||
|
traces.ts
|
||||||
|
metrics.ts
|
||||||
|
reports/
|
||||||
|
.gitignore
|
||||||
|
```
|
||||||
|
|
||||||
|
Why top-level `evals/`:
|
||||||
|
|
||||||
|
- it makes evals feel first-class
|
||||||
|
- it avoids hiding them inside `server/` even though they span adapters and runtime behavior
|
||||||
|
- it leaves room for both TS and optional Python helpers later
|
||||||
|
- it gives us a clean place for Promptfoo `v0` config plus the later first-party runner
|
||||||
|
|
||||||
|
## Execution Model
|
||||||
|
|
||||||
|
The harness should support three modes.
|
||||||
|
|
||||||
|
### Mode A: Cheap local smoke
|
||||||
|
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
- run on PRs
|
||||||
|
- keep cost low
|
||||||
|
- catch obvious regressions
|
||||||
|
|
||||||
|
Characteristics:
|
||||||
|
|
||||||
|
- 5 to 20 cases
|
||||||
|
- 1 or 2 bundles
|
||||||
|
- mostly hard checks and narrow rubrics
|
||||||
|
|
||||||
|
### Mode B: Candidate vs baseline compare
|
||||||
|
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
- evaluate a prompt/skill/model change before merge
|
||||||
|
|
||||||
|
Characteristics:
|
||||||
|
|
||||||
|
- paired runs
|
||||||
|
- pairwise judging enabled
|
||||||
|
- quality + efficiency diff report
|
||||||
|
|
||||||
|
### Mode C: Nightly broader matrix
|
||||||
|
|
||||||
|
Purpose:
|
||||||
|
|
||||||
|
- compare multiple models and bundles
|
||||||
|
- grow historical benchmark data
|
||||||
|
|
||||||
|
Characteristics:
|
||||||
|
|
||||||
|
- larger case set
|
||||||
|
- multiple models
|
||||||
|
- more expensive rubric/pairwise judging
|
||||||
|
|
||||||
|
## CI and Developer Workflow
|
||||||
|
|
||||||
|
Suggested commands:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
pnpm evals:smoke
|
||||||
|
pnpm evals:compare --baseline baseline/codex-default --candidate experiments/codex-lean-skillset
|
||||||
|
pnpm evals:nightly
|
||||||
|
```
|
||||||
|
|
||||||
|
PR behavior:
|
||||||
|
|
||||||
|
- run `evals:smoke` on prompt/skill/adapter/runtime changes
|
||||||
|
- optionally trigger `evals:compare` for labeled PRs or manual runs
|
||||||
|
|
||||||
|
Nightly behavior:
|
||||||
|
|
||||||
|
- run larger matrix
|
||||||
|
- save report artifact
|
||||||
|
- surface trend lines on pass rate, pairwise wins, and efficiency
|
||||||
|
|
||||||
|
## Framework Comparison
|
||||||
|
|
||||||
|
## Promptfoo
|
||||||
|
|
||||||
|
Best use for Paperclip:
|
||||||
|
|
||||||
|
- prompt-level micro-evals
|
||||||
|
- provider/model comparison
|
||||||
|
- quick local CI integration
|
||||||
|
- custom JS assertions and custom providers
|
||||||
|
- bootstrap-layer evals for one skill or one agent workflow
|
||||||
|
|
||||||
|
What changed in this recommendation:
|
||||||
|
|
||||||
|
- Promptfoo is now the recommended **starting point**
|
||||||
|
- especially for “one skill, a handful of cases, compare across models”
|
||||||
|
|
||||||
|
Why it still should not be the only long-term system:
|
||||||
|
|
||||||
|
- its primary abstraction is still prompt/provider/test-case oriented
|
||||||
|
- Paperclip needs scenario setup, control-plane state inspection, and multi-step traces as first-class concepts
|
||||||
|
|
||||||
|
Recommendation:
|
||||||
|
|
||||||
|
- use Promptfoo first
|
||||||
|
- store Promptfoo config and cases in-repo under `evals/promptfoo/`
|
||||||
|
- use custom JS/TS assertions and, if needed later, a custom provider that calls Paperclip scenario runners
|
||||||
|
- do not make Promptfoo YAML the only canonical Paperclip eval format once we outgrow prompt-level evals
|
||||||
|
|
||||||
|
## LangSmith
|
||||||
|
|
||||||
|
What it gets right:
|
||||||
|
|
||||||
|
- final response evals
|
||||||
|
- trajectory evals
|
||||||
|
- single-step evals
|
||||||
|
|
||||||
|
Why not the primary system today:
|
||||||
|
|
||||||
|
- stronger fit for teams already centered on LangChain/LangGraph
|
||||||
|
- introduces hosted/external workflow gravity before our own eval model is stable
|
||||||
|
|
||||||
|
Recommendation:
|
||||||
|
|
||||||
|
- copy the trajectory/final/single-step taxonomy
|
||||||
|
- do not adopt the platform as the default requirement
|
||||||
|
|
||||||
|
## Braintrust
|
||||||
|
|
||||||
|
What it gets right:
|
||||||
|
|
||||||
|
- TypeScript support
|
||||||
|
- clean dataset/task/scorer model
|
||||||
|
- production logging to datasets
|
||||||
|
- experiment comparison over time
|
||||||
|
|
||||||
|
Why not the primary system today:
|
||||||
|
|
||||||
|
- still externalizes the canonical dataset and review workflow
|
||||||
|
- we are not yet at the maturity where hosted experiment management should define the shape of the system
|
||||||
|
|
||||||
|
Recommendation:
|
||||||
|
|
||||||
|
- borrow its dataset/scorer/experiment mental model
|
||||||
|
- revisit once we want hosted review and experiment history at scale
|
||||||
|
|
||||||
|
## OpenAI Evals / Evals API
|
||||||
|
|
||||||
|
What it gets right:
|
||||||
|
|
||||||
|
- strong eval principles
|
||||||
|
- emphasis on task-specific evals
|
||||||
|
- continuous evaluation mindset
|
||||||
|
|
||||||
|
Why not the primary system:
|
||||||
|
|
||||||
|
- Paperclip must compare across models/providers
|
||||||
|
- we do not want our primary eval runner coupled to one model vendor
|
||||||
|
|
||||||
|
Recommendation:
|
||||||
|
|
||||||
|
- use the guidance
|
||||||
|
- do not use it as the core Paperclip eval runtime
|
||||||
|
|
||||||
|
## First Implementation Slice
|
||||||
|
|
||||||
|
The first version should be intentionally small.
|
||||||
|
|
||||||
|
## Phase 0: Promptfoo bootstrap
|
||||||
|
|
||||||
|
Build:
|
||||||
|
|
||||||
|
- `evals/promptfoo/promptfooconfig.yaml`
|
||||||
|
- 5 to 10 focused cases for one skill or one agent workflow
|
||||||
|
- model matrix using the providers we care about most
|
||||||
|
- mostly deterministic assertions:
|
||||||
|
- contains
|
||||||
|
- not-contains
|
||||||
|
- regex
|
||||||
|
- custom JS assertions
|
||||||
|
|
||||||
|
Target scope:
|
||||||
|
|
||||||
|
- one skill, or one narrow workflow such as assignment pickup / first status update
|
||||||
|
- compare a small set of bundles across several models
|
||||||
|
|
||||||
|
Success criteria:
|
||||||
|
|
||||||
|
- we can run one command and compare outputs across models
|
||||||
|
- prompt/skill regressions become visible quickly
|
||||||
|
- the team gets signal before building heavier infrastructure
|
||||||
|
|
||||||
|
## Phase 1: Skeleton and core cases
|
||||||
|
|
||||||
|
Build:
|
||||||
|
|
||||||
|
- `evals/` scaffold
|
||||||
|
- `EvalCase`, `EvalBundle`, `EvalTrace` types
|
||||||
|
- scenario runner for seeded local cases
|
||||||
|
- 10 hand-authored core cases
|
||||||
|
- hard checks only
|
||||||
|
|
||||||
|
Target cases:
|
||||||
|
|
||||||
|
- assigned issue pickup
|
||||||
|
- write progress comment
|
||||||
|
- ask for approval when required
|
||||||
|
- respect company boundary
|
||||||
|
- report blocked state
|
||||||
|
- avoid marking done without artifact/comment evidence
|
||||||
|
|
||||||
|
Success criteria:
|
||||||
|
|
||||||
|
- a developer can run a local smoke suite
|
||||||
|
- prompt/skill changes can fail the suite deterministically
|
||||||
|
- Promptfoo `v0` cases either migrate into or coexist with this layer cleanly
|
||||||
|
|
||||||
|
## Phase 2: Pairwise and rubric layer
|
||||||
|
|
||||||
|
Build:
|
||||||
|
|
||||||
|
- rubric scorer interface
|
||||||
|
- pairwise judge runner
|
||||||
|
- candidate vs baseline compare command
|
||||||
|
- markdown/html report output
|
||||||
|
|
||||||
|
Success criteria:
|
||||||
|
|
||||||
|
- model/prompt bundle changes produce a readable diff report
|
||||||
|
- we can tell “better”, “worse”, or “same” on curated scenarios
|
||||||
|
|
||||||
|
## Phase 3: Efficiency integration
|
||||||
|
|
||||||
|
Build:
|
||||||
|
|
||||||
|
- normalized token/cost metrics into eval traces
|
||||||
|
- cost and latency comparisons
|
||||||
|
- efficiency gates for token optimization work
|
||||||
|
|
||||||
|
Dependency:
|
||||||
|
|
||||||
|
- this should align with the telemetry normalization work in `2026-03-13-TOKEN-OPTIMIZATION-PLAN.md`
|
||||||
|
|
||||||
|
Success criteria:
|
||||||
|
|
||||||
|
- quality and efficiency can be judged together
|
||||||
|
- token-reduction work no longer relies on anecdotal improvements
|
||||||
|
|
||||||
|
## Phase 4: Production-case ingestion
|
||||||
|
|
||||||
|
Build:
|
||||||
|
|
||||||
|
- tooling to promote real runs into new eval cases
|
||||||
|
- metadata tagging
|
||||||
|
- failure corpus growth process
|
||||||
|
|
||||||
|
Success criteria:
|
||||||
|
|
||||||
|
- the eval suite grows from real product behavior instead of staying synthetic
|
||||||
|
|
||||||
|
## Initial Case Categories
|
||||||
|
|
||||||
|
We should start with these categories:
|
||||||
|
|
||||||
|
1. `core.assignment_pickup`
|
||||||
|
2. `core.progress_update`
|
||||||
|
3. `core.blocked_reporting`
|
||||||
|
4. `governance.approval_required`
|
||||||
|
5. `governance.company_boundary`
|
||||||
|
6. `delegation.correct_report`
|
||||||
|
7. `threads.long_context_followup`
|
||||||
|
8. `efficiency.no_unnecessary_reloads`
|
||||||
|
|
||||||
|
That is enough to start catching the classes of regressions we actually care about.
|
||||||
|
|
||||||
|
## Important Guardrails
|
||||||
|
|
||||||
|
### 1. Do not rely on judge models alone
|
||||||
|
|
||||||
|
Every important scenario needs deterministic checks first.
|
||||||
|
|
||||||
|
### 2. Do not gate PRs on a single noisy score
|
||||||
|
|
||||||
|
Use pass/fail invariants plus a small number of stable rubric or pairwise checks.
|
||||||
|
|
||||||
|
### 3. Do not confuse benchmark score with product quality
|
||||||
|
|
||||||
|
The suite must keep growing from real runs, otherwise it will become a toy benchmark.
|
||||||
|
|
||||||
|
### 4. Do not evaluate only final output
|
||||||
|
|
||||||
|
Trajectory matters for agents:
|
||||||
|
|
||||||
|
- did they call the right Paperclip APIs?
|
||||||
|
- did they ask for approval?
|
||||||
|
- did they communicate progress?
|
||||||
|
- did they choose the right issue?
|
||||||
|
|
||||||
|
### 5. Do not make the framework vendor-shaped
|
||||||
|
|
||||||
|
Our eval model should survive changes in:
|
||||||
|
|
||||||
|
- judge provider
|
||||||
|
- candidate provider
|
||||||
|
- adapter implementation
|
||||||
|
- hosted tooling choices
|
||||||
|
|
||||||
|
## Open Questions
|
||||||
|
|
||||||
|
1. Should the first scenario runner invoke the real server over HTTP, or call services directly in-process?
|
||||||
|
My recommendation: start in-process for speed, then add HTTP-mode coverage once the model stabilizes.
|
||||||
|
|
||||||
|
2. Should we support Python scorers in v1?
|
||||||
|
My recommendation: no. Keep v1 all-TypeScript.
|
||||||
|
|
||||||
|
3. Should we commit baseline outputs?
|
||||||
|
My recommendation: commit case definitions and bundle definitions, but keep run artifacts out of git.
|
||||||
|
|
||||||
|
4. Should we add hosted experiment tracking immediately?
|
||||||
|
My recommendation: no. Revisit after the local harness proves useful.
|
||||||
|
|
||||||
|
## Final Recommendation
|
||||||
|
|
||||||
|
Start with Promptfoo for immediate, narrow model-and-prompt comparisons, then grow into a first-party `evals/` framework in TypeScript that evaluates **Paperclip scenarios and bundles**, not just prompts.
|
||||||
|
|
||||||
|
Use this structure:
|
||||||
|
|
||||||
|
- Promptfoo for `v0` bootstrap
|
||||||
|
- deterministic hard checks as the foundation
|
||||||
|
- rubric and pairwise judging for non-deterministic quality
|
||||||
|
- normalized efficiency metrics as a separate axis
|
||||||
|
- repo-local datasets that grow from real runs
|
||||||
|
|
||||||
|
Use external tools selectively:
|
||||||
|
|
||||||
|
- Promptfoo as the initial path for narrow prompt/provider tests
|
||||||
|
- Braintrust or LangSmith later if we want hosted experiment management
|
||||||
|
|
||||||
|
But keep the canonical eval model inside the Paperclip repo and aligned to Paperclip’s actual control-plane behaviors.
|
||||||
780
doc/plans/2026-03-13-features.md
Normal file
780
doc/plans/2026-03-13-features.md
Normal file
@@ -0,0 +1,780 @@
|
|||||||
|
# Feature specs
|
||||||
|
|
||||||
|
## 1) Guided onboarding + first-job magic
|
||||||
|
|
||||||
|
The repo already has `onboard`, `doctor`, `run`, deployment modes, and even agent-oriented onboarding text/skills endpoints, but there are also current onboarding/auth validation issues and an open “onboard failed” report. That means this is not just polish; it is product-critical. ([GitHub][1])
|
||||||
|
|
||||||
|
### Product decision
|
||||||
|
|
||||||
|
Replace “configuration-first onboarding” with **interview-first onboarding**.
|
||||||
|
|
||||||
|
### What we want
|
||||||
|
|
||||||
|
- Ask 3–4 questions up front, not 20 settings.
|
||||||
|
- Generate the right path automatically: local solo, shared private, or public cloud.
|
||||||
|
- Detect what agent/runtime environment already exists.
|
||||||
|
- Make it normal to have Claude/OpenClaw/Codex help complete setup.
|
||||||
|
- End onboarding with a **real first task**, not a blank dashboard.
|
||||||
|
|
||||||
|
### What we do not want
|
||||||
|
|
||||||
|
- Provider jargon before value.
|
||||||
|
- “Go find an API key” as the default first instruction.
|
||||||
|
- A successful install that still leaves users unsure what to do next.
|
||||||
|
|
||||||
|
### Proposed UX
|
||||||
|
|
||||||
|
On first run, show an interview:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type OnboardingProfile = {
|
||||||
|
useCase: "startup" | "agency" | "internal_team";
|
||||||
|
companySource: "new" | "existing";
|
||||||
|
deployMode: "local_solo" | "shared_private" | "shared_public";
|
||||||
|
autonomyMode: "hands_on" | "hybrid" | "full_auto";
|
||||||
|
primaryRuntime: "claude_code" | "codex" | "openclaw" | "other";
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Questions:
|
||||||
|
|
||||||
|
1. What are you building?
|
||||||
|
2. Is this a new company, an existing company, or a service/agency team?
|
||||||
|
3. Are you working solo on one machine, sharing privately with a team, or deploying publicly?
|
||||||
|
4. Do you want full auto, hybrid, or tight manual control?
|
||||||
|
|
||||||
|
Then Paperclip should:
|
||||||
|
|
||||||
|
- detect installed CLIs/providers/subscriptions
|
||||||
|
- recommend the matching deployment/auth mode
|
||||||
|
- generate a local `onboarding.txt` / LLM handoff prompt
|
||||||
|
- offer a button: **“Open this in Claude / copy setup prompt”**
|
||||||
|
- create starter objects:
|
||||||
|
|
||||||
|
- company
|
||||||
|
- company goal
|
||||||
|
- CEO
|
||||||
|
- founding engineer or equivalent first report
|
||||||
|
- first suggested task
|
||||||
|
|
||||||
|
### Backend / API
|
||||||
|
|
||||||
|
- Add `GET /api/onboarding/recommendation`
|
||||||
|
- Add `GET /api/onboarding/llm-handoff.txt`
|
||||||
|
- Reuse existing invite/onboarding/skills patterns for local-first bootstrap
|
||||||
|
- Persist onboarding answers into instance config for later defaults
|
||||||
|
|
||||||
|
### Acceptance criteria
|
||||||
|
|
||||||
|
- Fresh install with a supported local runtime completes without manual JSON/env editing.
|
||||||
|
- User sees first live agent action before leaving onboarding.
|
||||||
|
- A blank dashboard is no longer the default post-install state.
|
||||||
|
- If a required dependency is missing, the error is prescriptive and fixable from the UI/CLI.
|
||||||
|
|
||||||
|
### Non-goals
|
||||||
|
|
||||||
|
- Account creation
|
||||||
|
- enterprise SSO
|
||||||
|
- perfect provider auto-detection for every runtime
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2) Board command surface, not generic chat
|
||||||
|
|
||||||
|
There is a real tension here: the transcript says users want “chat with my CEO,” while the public product definition says Paperclip is **not a chatbot** and V1 communication is **tasks + comments only**. At the same time, the repo is already exploring plugin infrastructure and even a chat plugin via plugin SSE streaming. The clean resolution is: **make the core surface conversational, but keep the data model task/thread-centric; reserve full chat as an optional plugin**. ([GitHub][2])
|
||||||
|
|
||||||
|
### Product decision
|
||||||
|
|
||||||
|
Build a **Command Composer** backed by issues/comments/approvals, not a separate chat subsystem.
|
||||||
|
|
||||||
|
### What we want
|
||||||
|
|
||||||
|
- “Talk to the CEO” feeling for the user.
|
||||||
|
- Every conversation ends up attached to a real company object.
|
||||||
|
- Strategy discussion can produce issues, artifacts, and approvals.
|
||||||
|
|
||||||
|
### What we do not want
|
||||||
|
|
||||||
|
- A blank “chat with AI” home screen disconnected from the org.
|
||||||
|
- Yet another agent-chat product.
|
||||||
|
|
||||||
|
### Proposed UX
|
||||||
|
|
||||||
|
Add a global composer with modes:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type ComposerMode = "ask" | "task" | "decision";
|
||||||
|
type ThreadScope = "company" | "project" | "issue" | "agent";
|
||||||
|
```
|
||||||
|
|
||||||
|
Examples:
|
||||||
|
|
||||||
|
- On dashboard: “Ask the CEO for a hiring plan” → creates a `strategy` issue/thread scoped to the company.
|
||||||
|
- On agent page: “Tell the designer to make this cleaner” → appends an instruction comment to an issue or spawns a new delegated task.
|
||||||
|
- On approval page: “Why are you asking to hire?” → appends a board comment to the approval context.
|
||||||
|
|
||||||
|
Add issue kinds:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type IssueKind = "task" | "strategy" | "question" | "decision";
|
||||||
|
```
|
||||||
|
|
||||||
|
### Backend / data model
|
||||||
|
|
||||||
|
Prefer extending existing `issues` rather than creating `chats`:
|
||||||
|
|
||||||
|
- `issues.kind`
|
||||||
|
- `issues.scope`
|
||||||
|
- optional `issues.target_agent_id`
|
||||||
|
- comment metadata: `comment.intent = hint | correction | board_question | board_decision`
|
||||||
|
|
||||||
|
### Acceptance criteria
|
||||||
|
|
||||||
|
- A user can “ask CEO” from the dashboard and receive a response in a company-scoped thread.
|
||||||
|
- From that thread, the user can create/approve tasks with one click.
|
||||||
|
- No separate chat database is required for v1 of this feature.
|
||||||
|
|
||||||
|
### Non-goals
|
||||||
|
|
||||||
|
- consumer chat UX
|
||||||
|
- model marketplace
|
||||||
|
- general-purpose assistant unrelated to company context
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3) Live org visibility + explainability layer
|
||||||
|
|
||||||
|
The core product promise is already visibility and governance, but right now the transcript makes clear that the UI is still too close to raw agent execution. The repo already has org charts, activity, heartbeat runs, costs, and agent detail surfaces; the missing piece is the explanatory layer above them. ([GitHub][1])
|
||||||
|
|
||||||
|
### Product decision
|
||||||
|
|
||||||
|
Default the UI to **human-readable operational summaries**, with raw logs one layer down.
|
||||||
|
|
||||||
|
### What we want
|
||||||
|
|
||||||
|
- At company level: “who is active, what are they doing, what is moving between teams”
|
||||||
|
- At agent level: “what is the plan, what step is complete, what outputs were produced”
|
||||||
|
- At run level: “summary first, transcript second”
|
||||||
|
|
||||||
|
### Proposed UX
|
||||||
|
|
||||||
|
Company page:
|
||||||
|
|
||||||
|
- org chart with live active-state indicators
|
||||||
|
- delegation animation between nodes when work moves
|
||||||
|
- current open priorities
|
||||||
|
- pending approvals
|
||||||
|
- burn / budget warning strip
|
||||||
|
|
||||||
|
Agent page:
|
||||||
|
|
||||||
|
- status card
|
||||||
|
- current issue
|
||||||
|
- plan checklist
|
||||||
|
- latest artifact(s)
|
||||||
|
- summary of last run
|
||||||
|
- expandable raw trace/logs
|
||||||
|
|
||||||
|
Run page:
|
||||||
|
|
||||||
|
- **Summary**
|
||||||
|
- **Steps**
|
||||||
|
- **Raw transcript / tool calls**
|
||||||
|
|
||||||
|
### Backend / API
|
||||||
|
|
||||||
|
Generate a run view model from current run/activity data:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type RunSummary = {
|
||||||
|
runId: string;
|
||||||
|
headline: string;
|
||||||
|
objective: string | null;
|
||||||
|
currentStep: string | null;
|
||||||
|
completedSteps: string[];
|
||||||
|
delegatedTo: { agentId: string; issueId?: string }[];
|
||||||
|
artifactIds: string[];
|
||||||
|
warnings: string[];
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Phase 1 can derive this server-side from existing run logs/comments. Persist only if needed later.
|
||||||
|
|
||||||
|
### Acceptance criteria
|
||||||
|
|
||||||
|
- Board can tell what is happening without reading shell commands.
|
||||||
|
- Raw logs are still accessible, but not the default surface.
|
||||||
|
- First task / first hire / first completion moments are visibly celebrated.
|
||||||
|
|
||||||
|
### Non-goals
|
||||||
|
|
||||||
|
- overdesigned animation system
|
||||||
|
- perfect semantic summarization before core data quality exists
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4) Artifact system: attachments, file browser, previews
|
||||||
|
|
||||||
|
This gap is already showing up in the repo. Storage is present, attachment endpoints exist, but current issues show that attachments are still effectively image-centric and comment attachment rendering is incomplete. At the same time, your transcript wants plans, docs, files, and generated web pages surfaced cleanly. ([GitHub][4])
|
||||||
|
|
||||||
|
### Product decision
|
||||||
|
|
||||||
|
Introduce a first-class **Artifact** model that unifies:
|
||||||
|
|
||||||
|
- uploaded/generated files
|
||||||
|
- workspace files of interest
|
||||||
|
- preview URLs
|
||||||
|
- generated docs/reports
|
||||||
|
|
||||||
|
### What we want
|
||||||
|
|
||||||
|
- Plans, specs, CSVs, markdown, PDFs, logs, JSON, HTML outputs
|
||||||
|
- easy discoverability from the issue/run/company pages
|
||||||
|
- a lightweight file browser for project workspaces
|
||||||
|
- preview links for generated websites/apps
|
||||||
|
|
||||||
|
### What we do not want
|
||||||
|
|
||||||
|
- forcing agents to paste everything inline into comments
|
||||||
|
- HTML stuffed into comment bodies as a workaround
|
||||||
|
- a full web IDE
|
||||||
|
|
||||||
|
### Phase 1: fix the obvious gaps
|
||||||
|
|
||||||
|
- Accept non-image MIME types for issue attachments
|
||||||
|
- Attach files to comments correctly
|
||||||
|
- Show file metadata + download/open on issue page
|
||||||
|
|
||||||
|
### Phase 2: introduce artifacts
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type ArtifactKind = "attachment" | "workspace_file" | "preview" | "report_link";
|
||||||
|
|
||||||
|
interface Artifact {
|
||||||
|
id: string;
|
||||||
|
companyId: string;
|
||||||
|
issueId?: string;
|
||||||
|
runId?: string;
|
||||||
|
agentId?: string;
|
||||||
|
kind: ArtifactKind;
|
||||||
|
title: string;
|
||||||
|
mimeType?: string;
|
||||||
|
filename?: string;
|
||||||
|
sizeBytes?: number;
|
||||||
|
storageKind: "local_disk" | "s3" | "external_url";
|
||||||
|
contentPath?: string;
|
||||||
|
previewUrl?: string;
|
||||||
|
metadata: Record<string, unknown>;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### UX
|
||||||
|
|
||||||
|
Issue page gets a **Deliverables** section:
|
||||||
|
|
||||||
|
- Files
|
||||||
|
- Reports
|
||||||
|
- Preview links
|
||||||
|
- Latest generated artifact highlighted at top
|
||||||
|
|
||||||
|
Project page gets a **Files** tab:
|
||||||
|
|
||||||
|
- folder tree
|
||||||
|
- recent changes
|
||||||
|
- “Open produced files” shortcut
|
||||||
|
|
||||||
|
### Preview handling
|
||||||
|
|
||||||
|
For HTML/static outputs:
|
||||||
|
|
||||||
|
- local deploy → open local preview URL
|
||||||
|
- shared/public deploy → host via configured preview service or static storage
|
||||||
|
- preview URL is registered back onto the issue as an artifact
|
||||||
|
|
||||||
|
### Acceptance criteria
|
||||||
|
|
||||||
|
- Agents can attach `.md`, `.txt`, `.json`, `.csv`, `.pdf`, and `.html`.
|
||||||
|
- Users can open/download them from the issue page.
|
||||||
|
- A generated static site can be opened from an issue without hunting through the filesystem.
|
||||||
|
|
||||||
|
### Non-goals
|
||||||
|
|
||||||
|
- browser IDE
|
||||||
|
- collaborative docs editor
|
||||||
|
- full object-storage admin UI
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5) Shared/cloud deployment + cloud runtimes
|
||||||
|
|
||||||
|
The repo already has a clear deployment story in docs: `local_trusted`, `authenticated/private`, and `authenticated/public`, plus Tailscale guidance. The roadmap explicitly calls out cloud agents like Cursor / e2b. That means the next step is not inventing a deployment model; it is making the shared/cloud path canonical and production-usable. ([GitHub][5])
|
||||||
|
|
||||||
|
### Product decision
|
||||||
|
|
||||||
|
Make **shared/private deploy** and **public/cloud deploy** first-class supported modes, and add **remote runtime drivers** for cloud-executed agents.
|
||||||
|
|
||||||
|
### What we want
|
||||||
|
|
||||||
|
- one instance a team can actually share
|
||||||
|
- local-first path that upgrades to private/public without a mental model change
|
||||||
|
- remote agent execution for non-local runtimes
|
||||||
|
|
||||||
|
### Proposed architecture
|
||||||
|
|
||||||
|
Separate **control plane** from **execution runtime** more explicitly:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type RuntimeDriver = "local_process" | "remote_sandbox" | "webhook";
|
||||||
|
|
||||||
|
interface ExecutionHandle {
|
||||||
|
externalRunId: string;
|
||||||
|
status: "queued" | "running" | "completed" | "failed" | "cancelled";
|
||||||
|
previewUrl?: string;
|
||||||
|
logsUrl?: string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
First remote driver: `remote_sandbox` for e2b-style execution.
|
||||||
|
|
||||||
|
### Deliverables
|
||||||
|
|
||||||
|
- canonical deploy recipes:
|
||||||
|
|
||||||
|
- local solo
|
||||||
|
- shared private (Tailscale/private auth)
|
||||||
|
- public cloud (managed Postgres + object storage + public URL)
|
||||||
|
|
||||||
|
- runtime health page
|
||||||
|
- adapter/runtime capability matrix
|
||||||
|
- one official reference deployment path
|
||||||
|
|
||||||
|
### UX
|
||||||
|
|
||||||
|
New “Deployment” settings page:
|
||||||
|
|
||||||
|
- instance mode
|
||||||
|
- auth/exposure
|
||||||
|
- storage/database status
|
||||||
|
- runtime drivers configured
|
||||||
|
- health and reachability checks
|
||||||
|
|
||||||
|
### Acceptance criteria
|
||||||
|
|
||||||
|
- Two humans can log into one authenticated/private instance and use it concurrently.
|
||||||
|
- A public deployment can run agents via at least one remote runtime.
|
||||||
|
- `doctor` catches missing public/private config and gives concrete fixes.
|
||||||
|
|
||||||
|
### Non-goals
|
||||||
|
|
||||||
|
- fully managed Paperclip SaaS
|
||||||
|
- every possible cloud provider in v1
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6) Multi-human collaboration (minimal, not enterprise RBAC)
|
||||||
|
|
||||||
|
This is the biggest deliberate departure from the current V1 spec. Publicly, V1 still says “single human board operator” and puts role-based human granularity out of scope. But the transcript is right that shared use is necessary if Paperclip is going to be real for teams. The key is to do a **minimal collaboration model**, not a giant permission system. ([GitHub][2])
|
||||||
|
|
||||||
|
### Product decision
|
||||||
|
|
||||||
|
Ship **coarse multi-user company memberships**, not fine-grained enterprise RBAC.
|
||||||
|
|
||||||
|
### Proposed roles
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type CompanyRole = "owner" | "admin" | "operator" | "viewer";
|
||||||
|
```
|
||||||
|
|
||||||
|
- **owner**: instance/company ownership, user invites, config
|
||||||
|
- **admin**: manage org, agents, budgets, approvals
|
||||||
|
- **operator**: create/update issues, interact with agents, view artifacts
|
||||||
|
- **viewer**: read-only
|
||||||
|
|
||||||
|
### Data model
|
||||||
|
|
||||||
|
```ts
|
||||||
|
interface CompanyMembership {
|
||||||
|
userId: string;
|
||||||
|
companyId: string;
|
||||||
|
role: CompanyRole;
|
||||||
|
invitedByUserId: string;
|
||||||
|
createdAt: string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Stretch goal later:
|
||||||
|
|
||||||
|
- optional project/team scoping
|
||||||
|
|
||||||
|
### What we want
|
||||||
|
|
||||||
|
- shared dashboard for real teams
|
||||||
|
- user attribution in activity log
|
||||||
|
- simple invite flow
|
||||||
|
- company-level isolation preserved
|
||||||
|
|
||||||
|
### What we do not want
|
||||||
|
|
||||||
|
- per-field ACLs
|
||||||
|
- SCIM/SSO/enterprise admin consoles
|
||||||
|
- ten permission toggles per page
|
||||||
|
|
||||||
|
### Acceptance criteria
|
||||||
|
|
||||||
|
- Team of 3 can use one shared Paperclip instance.
|
||||||
|
- Every user action is attributed correctly in activity.
|
||||||
|
- Company membership boundaries are enforced.
|
||||||
|
- Viewer cannot mutate; operator/admin can.
|
||||||
|
|
||||||
|
### Non-goals
|
||||||
|
|
||||||
|
- enterprise RBAC
|
||||||
|
- cross-company matrix permissions
|
||||||
|
- multi-board governance logic in first cut
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7) Auto mode + interrupt/resume
|
||||||
|
|
||||||
|
This is a product behavior issue, not a UI nicety. If agents cannot keep working or accept course correction without restarting, the autonomy model feels fake.
|
||||||
|
|
||||||
|
### Product decision
|
||||||
|
|
||||||
|
Make auto mode and mid-run interruption first-class runtime semantics.
|
||||||
|
|
||||||
|
### What we want
|
||||||
|
|
||||||
|
- Auto mode that continues until blocked by approvals, budgets, or explicit pause.
|
||||||
|
- Mid-run “you missed this” correction without losing session continuity.
|
||||||
|
- Clear state when an agent is waiting, blocked, or paused.
|
||||||
|
|
||||||
|
### Proposed state model
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type RunState =
|
||||||
|
| "queued"
|
||||||
|
| "running"
|
||||||
|
| "waiting_approval"
|
||||||
|
| "waiting_input"
|
||||||
|
| "paused"
|
||||||
|
| "completed"
|
||||||
|
| "failed"
|
||||||
|
| "cancelled";
|
||||||
|
```
|
||||||
|
|
||||||
|
Add board interjections as resumable input events:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
interface RunMessage {
|
||||||
|
runId: string;
|
||||||
|
authorUserId: string;
|
||||||
|
mode: "hint" | "correction" | "hard_override";
|
||||||
|
body: string;
|
||||||
|
resumeCurrentSession: boolean;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### UX
|
||||||
|
|
||||||
|
Buttons on active run:
|
||||||
|
|
||||||
|
- Pause
|
||||||
|
- Resume
|
||||||
|
- Interrupt
|
||||||
|
- Abort
|
||||||
|
- Restart from scratch
|
||||||
|
|
||||||
|
Interrupt opens a small composer that explicitly says:
|
||||||
|
|
||||||
|
- continue current session
|
||||||
|
- or restart run
|
||||||
|
|
||||||
|
### Acceptance criteria
|
||||||
|
|
||||||
|
- A board comment can resume an active session instead of spawning a fresh one.
|
||||||
|
- Session ID remains stable for “continue” path.
|
||||||
|
- UI clearly distinguishes blocked vs. waiting vs. paused.
|
||||||
|
|
||||||
|
### Non-goals
|
||||||
|
|
||||||
|
- simultaneous multi-user live editing of the same run transcript
|
||||||
|
- perfect conversational UX before runtime semantics are fixed
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8) Cost safety + heartbeat/runtime hardening
|
||||||
|
|
||||||
|
This is probably the most important immediate workstream. The transcript says token burn is the highest pain, and the repo currently has active issues around budget enforcement evidence, onboarding/auth validation, and circuit-breaker style waste prevention. Public docs already promise hard budgets, and the issue tracker is pointing at the missing operational protections. ([GitHub][6])
|
||||||
|
|
||||||
|
### Product decision
|
||||||
|
|
||||||
|
Treat this as a **P0 runtime contract**, not a nice-to-have.
|
||||||
|
|
||||||
|
### Part A: deterministic wake gating
|
||||||
|
|
||||||
|
Do cheap, explicit work detection before invoking an LLM.
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type WakeReason =
|
||||||
|
| "new_assignment"
|
||||||
|
| "new_comment"
|
||||||
|
| "mention"
|
||||||
|
| "approval_resolved"
|
||||||
|
| "scheduled_scan"
|
||||||
|
| "manual";
|
||||||
|
```
|
||||||
|
|
||||||
|
Rules:
|
||||||
|
|
||||||
|
- if no new actionable input exists, do not call the model
|
||||||
|
- scheduled scan should be a cheap policy check first, not a full reasoning pass
|
||||||
|
|
||||||
|
### Part B: budget contract
|
||||||
|
|
||||||
|
Keep the existing public promise, but make it undeniable:
|
||||||
|
|
||||||
|
- warning at 80%
|
||||||
|
- auto-pause at 100%
|
||||||
|
- visible audit trail
|
||||||
|
- explicit board override to continue
|
||||||
|
|
||||||
|
### Part C: circuit breaker
|
||||||
|
|
||||||
|
Add per-agent runtime guards:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
interface CircuitBreakerConfig {
|
||||||
|
enabled: boolean;
|
||||||
|
maxConsecutiveNoProgress: number;
|
||||||
|
maxConsecutiveFailures: number;
|
||||||
|
tokenVelocityMultiplier: number;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Trip when:
|
||||||
|
|
||||||
|
- no issue/status/comment progress for N runs
|
||||||
|
- N failures in a row
|
||||||
|
- token spike vs rolling average
|
||||||
|
|
||||||
|
### Part D: refactor heartbeat service
|
||||||
|
|
||||||
|
Split current orchestration into modules:
|
||||||
|
|
||||||
|
- wake detector
|
||||||
|
- checkout/lock manager
|
||||||
|
- adapter runner
|
||||||
|
- session manager
|
||||||
|
- cost recorder
|
||||||
|
- breaker evaluator
|
||||||
|
- event streamer
|
||||||
|
|
||||||
|
### Part E: regression suite
|
||||||
|
|
||||||
|
Mandatory automated proofs for:
|
||||||
|
|
||||||
|
- onboarding/auth matrix
|
||||||
|
- 80/100 budget behavior
|
||||||
|
- no cross-company auth leakage
|
||||||
|
- no-spurious-wake idle behavior
|
||||||
|
- active-run resume/interruption
|
||||||
|
- remote runtime smoke
|
||||||
|
|
||||||
|
### Acceptance criteria
|
||||||
|
|
||||||
|
- Idle org with no new work does not generate model calls from heartbeat scans.
|
||||||
|
- 80% shows warning only.
|
||||||
|
- 100% pauses the agent and blocks continued execution until override.
|
||||||
|
- Circuit breaker pause is visible in audit/activity.
|
||||||
|
- Runtime modules have explicit contracts and are testable independently.
|
||||||
|
|
||||||
|
### Non-goals
|
||||||
|
|
||||||
|
- perfect autonomous optimization
|
||||||
|
- eliminating all wasted calls in every adapter/provider
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9) Project workspaces, previews, and PR handoff — without becoming GitHub
|
||||||
|
|
||||||
|
This is the right way to resolve the code-workflow debate. The repo already has worktree-local instances, project `workspaceStrategy.provisionCommand`, and an RFC for adapter-level git worktree isolation. That is the correct architectural direction: **project execution policies and workspace isolation**, not built-in PR review. ([GitHub][7])
|
||||||
|
|
||||||
|
### Product decision
|
||||||
|
|
||||||
|
Paperclip should manage the **issue → workspace → preview/PR → review handoff** lifecycle, but leave diffs/review/merge to external tools.
|
||||||
|
|
||||||
|
### Proposed config
|
||||||
|
|
||||||
|
Prefer repo-local project config:
|
||||||
|
|
||||||
|
```yaml
|
||||||
|
# .paperclip/project.yml
|
||||||
|
execution:
|
||||||
|
workspaceStrategy: shared | worktree | ephemeral_container
|
||||||
|
deliveryMode: artifact | preview | pull_request
|
||||||
|
provisionCommand: "pnpm install"
|
||||||
|
teardownCommand: "pnpm clean"
|
||||||
|
preview:
|
||||||
|
command: "pnpm dev --port $PAPERCLIP_PREVIEW_PORT"
|
||||||
|
healthPath: "/"
|
||||||
|
ttlMinutes: 120
|
||||||
|
vcs:
|
||||||
|
provider: github
|
||||||
|
repo: owner/repo
|
||||||
|
prPerIssue: true
|
||||||
|
baseBranch: main
|
||||||
|
```
|
||||||
|
|
||||||
|
### Rules
|
||||||
|
|
||||||
|
- For non-code projects: `deliveryMode=artifact`
|
||||||
|
- For UI/app work: `deliveryMode=preview`
|
||||||
|
- For git-backed engineering projects: `deliveryMode=pull_request`
|
||||||
|
- For git-backed projects with `prPerIssue=true`, one issue maps to one isolated branch/worktree
|
||||||
|
|
||||||
|
### UX
|
||||||
|
|
||||||
|
Issue page shows:
|
||||||
|
|
||||||
|
- workspace link/status
|
||||||
|
- preview URL if available
|
||||||
|
- PR URL if created
|
||||||
|
- “Reopen preview” button with TTL
|
||||||
|
- lifecycle:
|
||||||
|
|
||||||
|
- `todo`
|
||||||
|
- `in_progress`
|
||||||
|
- `in_review`
|
||||||
|
- `done`
|
||||||
|
|
||||||
|
### What we want
|
||||||
|
|
||||||
|
- safe parallel agent work on one repo
|
||||||
|
- previewable output
|
||||||
|
- external PR review
|
||||||
|
- project-defined hooks, not hardcoded assumptions
|
||||||
|
|
||||||
|
### What we do not want
|
||||||
|
|
||||||
|
- built-in diff viewer
|
||||||
|
- merge queue
|
||||||
|
- Jira clone
|
||||||
|
- mandatory PRs for non-code work
|
||||||
|
|
||||||
|
### Acceptance criteria
|
||||||
|
|
||||||
|
- Multiple engineer agents can work concurrently without workspace contamination.
|
||||||
|
- When a project is in PR mode, the issue contains branch/worktree/preview/PR metadata.
|
||||||
|
- Preview can be reopened on demand until TTL expires.
|
||||||
|
|
||||||
|
### Non-goals
|
||||||
|
|
||||||
|
- replacing GitHub/GitLab
|
||||||
|
- universal preview hosting for every framework on day one
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10) Plugin system as the escape hatch
|
||||||
|
|
||||||
|
The roadmap already includes plugins, GitHub discussions are active around it, and there is an open issue proposing an SSE bridge specifically to enable streaming plugin UIs such as chat, logs, and monitors. This is exactly the right place for optional surfaces. ([GitHub][1])
|
||||||
|
|
||||||
|
### Product decision
|
||||||
|
|
||||||
|
Keep the control-plane core thin; put optional high-variance experiences into plugins.
|
||||||
|
|
||||||
|
### First-party plugin targets
|
||||||
|
|
||||||
|
- Chat
|
||||||
|
- Knowledge base / RAG
|
||||||
|
- Log tail / live build output
|
||||||
|
- Custom tracing or queues
|
||||||
|
- Doc editor / proposal builder
|
||||||
|
|
||||||
|
### Plugin manifest
|
||||||
|
|
||||||
|
```ts
|
||||||
|
interface PluginManifest {
|
||||||
|
id: string;
|
||||||
|
version: string;
|
||||||
|
requestedPermissions: (
|
||||||
|
| "read_company"
|
||||||
|
| "read_issue"
|
||||||
|
| "write_issue_comment"
|
||||||
|
| "create_issue"
|
||||||
|
| "stream_ui"
|
||||||
|
)[];
|
||||||
|
surfaces: ("company_home" | "issue_panel" | "agent_panel" | "sidebar")[];
|
||||||
|
workerEntry: string;
|
||||||
|
uiEntry: string;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Platform requirements
|
||||||
|
|
||||||
|
- host ↔ worker action bridge
|
||||||
|
- SSE/UI streaming
|
||||||
|
- company-scoped auth
|
||||||
|
- permission declaration
|
||||||
|
- surface slots in UI
|
||||||
|
|
||||||
|
### Acceptance criteria
|
||||||
|
|
||||||
|
- A plugin can stream events to UI in real time.
|
||||||
|
- A chat plugin can converse without requiring chat to become the core Paperclip product.
|
||||||
|
- Plugin permissions are company-scoped and auditable.
|
||||||
|
|
||||||
|
### Non-goals
|
||||||
|
|
||||||
|
- plugins mutating core schema directly
|
||||||
|
- arbitrary privileged code execution without explicit permissions
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## Priority order I would use
|
||||||
|
|
||||||
|
Given the repo state and the transcript, I would sequence it like this:
|
||||||
|
|
||||||
|
**P0**
|
||||||
|
|
||||||
|
1. Cost safety + heartbeat hardening
|
||||||
|
2. Guided onboarding + first-job magic
|
||||||
|
3. Shared/cloud deployment foundation
|
||||||
|
4. Artifact phase 1: non-image attachments + deliverables surfacing
|
||||||
|
|
||||||
|
**P1** 5. Board command surface 6. Visibility/explainability layer 7. Auto mode + interrupt/resume 8. Minimal multi-user collaboration
|
||||||
|
|
||||||
|
**P2** 9. Project workspace / preview / PR lifecycle 10. Plugin system + optional chat plugin 11. Template/preset expansion for startup vs agency vs internal-team onboarding
|
||||||
|
|
||||||
|
Why this order: the current repo is already getting pressure on onboarding failures, auth/onboarding validation, budget enforcement, and wasted token burn. If those are shaky, everything else feels impressive but unsafe. ([GitHub][3])
|
||||||
|
|
||||||
|
## Bottom line
|
||||||
|
|
||||||
|
The best synthesis is:
|
||||||
|
|
||||||
|
- **Keep** Paperclip as the board-level control plane.
|
||||||
|
- **Do not** make chat, code review, or workflow-building the core identity.
|
||||||
|
- **Do** make the product feel conversational, visible, output-oriented, and shared.
|
||||||
|
- **Do** make coding workflows an integration surface via workspaces/previews/PR links.
|
||||||
|
- **Use plugins** for richer edges like chat and knowledge.
|
||||||
|
|
||||||
|
That keeps the repo’s current product direction intact while solving almost every pain surfaced in the transcript.
|
||||||
|
|
||||||
|
### Key references
|
||||||
|
|
||||||
|
- README / positioning / roadmap / product boundaries. ([GitHub][1])
|
||||||
|
- Product definition. ([GitHub][8])
|
||||||
|
- V1 implementation spec and explicit non-goals. ([GitHub][2])
|
||||||
|
- Core concepts and architecture. ([GitHub][9])
|
||||||
|
- Deployment modes / Tailscale / local-to-cloud path. ([GitHub][5])
|
||||||
|
- Developing guide: worktree-local instances, provision hooks, onboarding endpoints. ([GitHub][7])
|
||||||
|
- Current issue pressure: onboarding failure, auth/onboarding validation, budget enforcement, circuit breaker, attachment gaps, plugin chat. ([GitHub][3])
|
||||||
|
|
||||||
|
[1]: https://github.com/paperclipai/paperclip "https://github.com/paperclipai/paperclip"
|
||||||
|
[2]: https://github.com/paperclipai/paperclip/blob/master/doc/SPEC-implementation.md "https://github.com/paperclipai/paperclip/blob/master/doc/SPEC-implementation.md"
|
||||||
|
[3]: https://github.com/paperclipai/paperclip/issues/704 "https://github.com/paperclipai/paperclip/issues/704"
|
||||||
|
[4]: https://github.com/paperclipai/paperclip/blob/master/docs/deploy/tailscale-private-access.md "https://github.com/paperclipai/paperclip/blob/master/docs/deploy/tailscale-private-access.md"
|
||||||
|
[5]: https://github.com/paperclipai/paperclip/blob/master/docs/deploy/deployment-modes.md "https://github.com/paperclipai/paperclip/blob/master/docs/deploy/deployment-modes.md"
|
||||||
|
[6]: https://github.com/paperclipai/paperclip/issues/692 "https://github.com/paperclipai/paperclip/issues/692"
|
||||||
|
[7]: https://github.com/paperclipai/paperclip/blob/master/doc/DEVELOPING.md "https://github.com/paperclipai/paperclip/blob/master/doc/DEVELOPING.md"
|
||||||
|
[8]: https://github.com/paperclipai/paperclip/blob/master/doc/PRODUCT.md "https://github.com/paperclipai/paperclip/blob/master/doc/PRODUCT.md"
|
||||||
|
[9]: https://github.com/paperclipai/paperclip/blob/master/docs/start/core-concepts.md "https://github.com/paperclipai/paperclip/blob/master/docs/start/core-concepts.md"
|
||||||
186
doc/plans/2026-03-13-paperclip-skill-tightening-plan.md
Normal file
186
doc/plans/2026-03-13-paperclip-skill-tightening-plan.md
Normal file
@@ -0,0 +1,186 @@
|
|||||||
|
# Paperclip Skill Tightening Plan
|
||||||
|
|
||||||
|
## Status
|
||||||
|
|
||||||
|
Deferred follow-up. Do not include in the current token-optimization PR beyond documenting the plan.
|
||||||
|
|
||||||
|
## Why This Is Deferred
|
||||||
|
|
||||||
|
The `paperclip` skill is part of the critical control-plane safety surface. Tightening it may reduce fresh-session token use, but it also carries prompt-regression risk. We do not yet have evals that would let us safely prove behavior preservation across assignment handling, checkout rules, comment etiquette, approval workflows, and escalation paths.
|
||||||
|
|
||||||
|
The current PR should ship the lower-risk infrastructure wins first:
|
||||||
|
|
||||||
|
- telemetry normalization
|
||||||
|
- safe session reuse
|
||||||
|
- incremental issue/comment context
|
||||||
|
- bootstrap versus heartbeat prompt separation
|
||||||
|
- Codex worktree isolation
|
||||||
|
|
||||||
|
## Current Problem
|
||||||
|
|
||||||
|
Fresh runs still spend substantial input tokens even after the context-path fixes. The remaining large startup cost appears to come from loading the full `paperclip` skill and related instruction surface into context at run start.
|
||||||
|
|
||||||
|
The skill currently mixes three kinds of content in one file:
|
||||||
|
|
||||||
|
- hot-path heartbeat procedure used on nearly every run
|
||||||
|
- critical policy and safety invariants
|
||||||
|
- rare workflow/reference material that most runs do not need
|
||||||
|
|
||||||
|
That structure is safe but expensive.
|
||||||
|
|
||||||
|
## Goals
|
||||||
|
|
||||||
|
- reduce first-run instruction tokens without weakening agent safety
|
||||||
|
- preserve all current Paperclip control-plane capabilities
|
||||||
|
- keep common heartbeat behavior explicit and easy for agents to follow
|
||||||
|
- move rare workflows and reference material out of the hot path
|
||||||
|
- create a structure that can later be evaluated systematically
|
||||||
|
|
||||||
|
## Non-Goals
|
||||||
|
|
||||||
|
- changing Paperclip API semantics
|
||||||
|
- removing required governance rules
|
||||||
|
- deleting rare workflows
|
||||||
|
- changing agent defaults in the current PR
|
||||||
|
|
||||||
|
## Recommended Direction
|
||||||
|
|
||||||
|
### 1. Split Hot Path From Lookup Material
|
||||||
|
|
||||||
|
Restructure the skill into:
|
||||||
|
|
||||||
|
- an always-loaded core section for the common heartbeat loop
|
||||||
|
- on-demand material for infrequent workflows and deep reference
|
||||||
|
|
||||||
|
The core should cover only what is needed on nearly every wake:
|
||||||
|
|
||||||
|
- auth and required headers
|
||||||
|
- inbox-first assignment retrieval
|
||||||
|
- mandatory checkout behavior
|
||||||
|
- `heartbeat-context` first
|
||||||
|
- incremental comment retrieval rules
|
||||||
|
- mention/self-assign exception
|
||||||
|
- blocked-task dedup
|
||||||
|
- status/comment/release expectations before exit
|
||||||
|
|
||||||
|
### 2. Normalize The Skill Around One Canonical Procedure
|
||||||
|
|
||||||
|
The same rules are currently expressed multiple times across:
|
||||||
|
|
||||||
|
- heartbeat steps
|
||||||
|
- critical rules
|
||||||
|
- endpoint reference
|
||||||
|
- workflow examples
|
||||||
|
|
||||||
|
Refactor so each operational fact has one primary home:
|
||||||
|
|
||||||
|
- procedure
|
||||||
|
- invariant list
|
||||||
|
- appendix/reference
|
||||||
|
|
||||||
|
This reduces prompt weight and lowers the chance of internal instruction drift.
|
||||||
|
|
||||||
|
### 3. Compress Prose Into High-Signal Instruction Forms
|
||||||
|
|
||||||
|
Rewrite the hot path using compact operational forms:
|
||||||
|
|
||||||
|
- short ordered checklist
|
||||||
|
- flat invariant list
|
||||||
|
- minimal examples only where ambiguity would be risky
|
||||||
|
|
||||||
|
Reduce:
|
||||||
|
|
||||||
|
- narrative explanation
|
||||||
|
- repeated warnings already covered elsewhere
|
||||||
|
- large example payloads for common operations
|
||||||
|
- long endpoint matrices in the main body
|
||||||
|
|
||||||
|
### 4. Move Rare Workflows Behind Explicit Triggers
|
||||||
|
|
||||||
|
These workflows should remain available but should not dominate fresh-run context:
|
||||||
|
|
||||||
|
- OpenClaw invite flow
|
||||||
|
- project setup flow
|
||||||
|
- planning `<plan/>` writeback flow
|
||||||
|
- instructions-path update flow
|
||||||
|
- detailed link-formatting examples
|
||||||
|
|
||||||
|
Recommended approach:
|
||||||
|
|
||||||
|
- keep a short pointer in the main skill
|
||||||
|
- move detailed procedures into sibling skills or referenced docs that agents read only when needed
|
||||||
|
|
||||||
|
### 5. Separate Policy From Reference
|
||||||
|
|
||||||
|
The skill should distinguish:
|
||||||
|
|
||||||
|
- mandatory operating rules
|
||||||
|
- endpoint lookup/reference
|
||||||
|
- business-process playbooks
|
||||||
|
|
||||||
|
That separation makes it easier to evaluate prompt changes later and lets adapters or orchestration choose what must always be loaded.
|
||||||
|
|
||||||
|
## Proposed Target Structure
|
||||||
|
|
||||||
|
1. Purpose and authentication
|
||||||
|
2. Compact heartbeat procedure
|
||||||
|
3. Hard invariants
|
||||||
|
4. Required comment/update style
|
||||||
|
5. Triggered workflow index
|
||||||
|
6. Appendix/reference
|
||||||
|
|
||||||
|
## Rollout Plan
|
||||||
|
|
||||||
|
### Phase 1. Inventory And Measure
|
||||||
|
|
||||||
|
- annotate the current skill by section and estimate token weight
|
||||||
|
- identify which sections are truly hot-path versus rare
|
||||||
|
- capture representative runs to compare before/after prompt size and behavior
|
||||||
|
|
||||||
|
### Phase 2. Structural Refactor Without Semantic Changes
|
||||||
|
|
||||||
|
- rewrite the main skill into the target structure
|
||||||
|
- preserve all existing rules and capabilities
|
||||||
|
- move rare workflow details into referenced companion material
|
||||||
|
- keep wording changes conservative
|
||||||
|
|
||||||
|
### Phase 3. Validate Against Real Scenarios
|
||||||
|
|
||||||
|
Run scenario checks for:
|
||||||
|
|
||||||
|
- normal assigned heartbeat
|
||||||
|
- comment-triggered wake
|
||||||
|
- blocked-task dedup behavior
|
||||||
|
- approval-resolution wake
|
||||||
|
- delegation/subtask creation
|
||||||
|
- board handoff back to user
|
||||||
|
- plan-request handling
|
||||||
|
|
||||||
|
### Phase 4. Decide Default Loading Strategy
|
||||||
|
|
||||||
|
After validation, decide whether:
|
||||||
|
|
||||||
|
- the entire main skill still loads by default, or
|
||||||
|
- only the compact core loads by default and rare sections are fetched on demand
|
||||||
|
|
||||||
|
Do not change this loading policy without validation.
|
||||||
|
|
||||||
|
## Risks
|
||||||
|
|
||||||
|
- prompt degradation on control-plane safety rules
|
||||||
|
- agents forgetting rare but important workflows
|
||||||
|
- accidental removal of repeated wording that was carrying useful behavior
|
||||||
|
- introducing ambiguous instruction precedence between the core skill and companion materials
|
||||||
|
|
||||||
|
## Preconditions Before Implementation
|
||||||
|
|
||||||
|
- define acceptance scenarios for control-plane correctness
|
||||||
|
- add at least lightweight eval or scripted scenario coverage for key Paperclip flows
|
||||||
|
- confirm how adapter/bootstrap layering should load skill content versus references
|
||||||
|
|
||||||
|
## Success Criteria
|
||||||
|
|
||||||
|
- materially lower first-run input tokens for Paperclip-coordinated agents
|
||||||
|
- no regression in checkout discipline, issue updates, blocked handling, or delegation
|
||||||
|
- no increase in malformed API usage or ownership mistakes
|
||||||
|
- agents still complete rare workflows correctly when explicitly asked
|
||||||
699
doc/plans/2026-03-13-plugin-kitchen-sink-example.md
Normal file
699
doc/plans/2026-03-13-plugin-kitchen-sink-example.md
Normal file
@@ -0,0 +1,699 @@
|
|||||||
|
# Kitchen Sink Plugin Plan
|
||||||
|
|
||||||
|
## Goal
|
||||||
|
|
||||||
|
Add a new first-party example plugin, `Kitchen Sink (Example)`, that demonstrates every currently implemented Paperclip plugin API surface in one place.
|
||||||
|
|
||||||
|
This plugin is meant to be:
|
||||||
|
|
||||||
|
- a living reference implementation for contributors
|
||||||
|
- a manual test harness for the plugin runtime
|
||||||
|
- a discoverable demo of what plugins can actually do today
|
||||||
|
|
||||||
|
It is not meant to be a polished end-user product plugin.
|
||||||
|
|
||||||
|
## Why
|
||||||
|
|
||||||
|
The current plugin system has a real API surface, but it is spread across:
|
||||||
|
|
||||||
|
- SDK docs
|
||||||
|
- SDK types
|
||||||
|
- plugin spec prose
|
||||||
|
- two example plugins that each show only a narrow slice
|
||||||
|
|
||||||
|
That makes it hard to answer basic questions like:
|
||||||
|
|
||||||
|
- what can plugins render?
|
||||||
|
- what can plugin workers actually do?
|
||||||
|
- which surfaces are real versus aspirational?
|
||||||
|
- how should a new plugin be structured in this repo?
|
||||||
|
|
||||||
|
The kitchen-sink plugin should answer those questions by example.
|
||||||
|
|
||||||
|
## Success Criteria
|
||||||
|
|
||||||
|
The plugin is successful if a contributor can install it and, without reading the SDK first, discover and exercise the current plugin runtime surface area from inside Paperclip.
|
||||||
|
|
||||||
|
Concretely:
|
||||||
|
|
||||||
|
- it installs from the bundled examples list
|
||||||
|
- it exposes at least one demo for every implemented worker API surface
|
||||||
|
- it exposes at least one demo for every host-mounted UI surface
|
||||||
|
- it clearly labels local-only / trusted-only demos
|
||||||
|
- it is safe enough for local development by default
|
||||||
|
- it doubles as a regression harness for plugin runtime changes
|
||||||
|
|
||||||
|
## Constraints
|
||||||
|
|
||||||
|
- Keep it instance-installed, not company-installed.
|
||||||
|
- Treat this as a trusted/local example plugin.
|
||||||
|
- Do not rely on cloud-safe runtime assumptions.
|
||||||
|
- Avoid destructive defaults.
|
||||||
|
- Avoid irreversible mutations unless they are clearly labeled and easy to undo.
|
||||||
|
|
||||||
|
## Source Of Truth For This Plan
|
||||||
|
|
||||||
|
This plan is based on the currently implemented SDK/types/runtime, not only the long-horizon spec.
|
||||||
|
|
||||||
|
Primary references:
|
||||||
|
|
||||||
|
- `packages/plugins/sdk/README.md`
|
||||||
|
- `packages/plugins/sdk/src/types.ts`
|
||||||
|
- `packages/plugins/sdk/src/ui/types.ts`
|
||||||
|
- `packages/shared/src/constants.ts`
|
||||||
|
- `packages/shared/src/types/plugin.ts`
|
||||||
|
|
||||||
|
## Current Surface Inventory
|
||||||
|
|
||||||
|
### Worker/runtime APIs to demonstrate
|
||||||
|
|
||||||
|
These are the concrete `ctx` clients currently exposed by the SDK:
|
||||||
|
|
||||||
|
- `ctx.config`
|
||||||
|
- `ctx.events`
|
||||||
|
- `ctx.jobs`
|
||||||
|
- `ctx.launchers`
|
||||||
|
- `ctx.http`
|
||||||
|
- `ctx.secrets`
|
||||||
|
- `ctx.assets`
|
||||||
|
- `ctx.activity`
|
||||||
|
- `ctx.state`
|
||||||
|
- `ctx.entities`
|
||||||
|
- `ctx.projects`
|
||||||
|
- `ctx.companies`
|
||||||
|
- `ctx.issues`
|
||||||
|
- `ctx.agents`
|
||||||
|
- `ctx.goals`
|
||||||
|
- `ctx.data`
|
||||||
|
- `ctx.actions`
|
||||||
|
- `ctx.streams`
|
||||||
|
- `ctx.tools`
|
||||||
|
- `ctx.metrics`
|
||||||
|
- `ctx.logger`
|
||||||
|
|
||||||
|
### UI surfaces to demonstrate
|
||||||
|
|
||||||
|
Surfaces defined in the SDK:
|
||||||
|
|
||||||
|
- `page`
|
||||||
|
- `settingsPage`
|
||||||
|
- `dashboardWidget`
|
||||||
|
- `sidebar`
|
||||||
|
- `sidebarPanel`
|
||||||
|
- `detailTab`
|
||||||
|
- `taskDetailView`
|
||||||
|
- `projectSidebarItem`
|
||||||
|
- `toolbarButton`
|
||||||
|
- `contextMenuItem`
|
||||||
|
- `commentAnnotation`
|
||||||
|
- `commentContextMenuItem`
|
||||||
|
|
||||||
|
### Current host confidence
|
||||||
|
|
||||||
|
Confirmed or strongly indicated as mounted in the current app:
|
||||||
|
|
||||||
|
- `page`
|
||||||
|
- `settingsPage`
|
||||||
|
- `dashboardWidget`
|
||||||
|
- `detailTab`
|
||||||
|
- `projectSidebarItem`
|
||||||
|
- comment surfaces
|
||||||
|
- launcher infrastructure
|
||||||
|
|
||||||
|
Need explicit validation before claiming full demo coverage:
|
||||||
|
|
||||||
|
- `sidebar`
|
||||||
|
- `sidebarPanel`
|
||||||
|
- `taskDetailView`
|
||||||
|
- `toolbarButton` as direct slot, distinct from launcher placement
|
||||||
|
- `contextMenuItem` as direct slot, distinct from comment menu and launcher placement
|
||||||
|
|
||||||
|
The implementation should keep a small validation checklist for these before we call the plugin "complete".
|
||||||
|
|
||||||
|
## Plugin Concept
|
||||||
|
|
||||||
|
The plugin should be named:
|
||||||
|
|
||||||
|
- display name: `Kitchen Sink (Example)`
|
||||||
|
- package: `@paperclipai/plugin-kitchen-sink-example`
|
||||||
|
- plugin id: `paperclip.kitchen-sink-example` or `paperclip-kitchen-sink-example`
|
||||||
|
|
||||||
|
Recommendation: use `paperclip-kitchen-sink-example` to match current in-repo example naming style.
|
||||||
|
|
||||||
|
Category mix:
|
||||||
|
|
||||||
|
- `ui`
|
||||||
|
- `automation`
|
||||||
|
- `workspace`
|
||||||
|
- `connector`
|
||||||
|
|
||||||
|
That is intentionally broad because the point is coverage.
|
||||||
|
|
||||||
|
## UX Shape
|
||||||
|
|
||||||
|
The plugin should have one main full-page demo console plus smaller satellites on other surfaces.
|
||||||
|
|
||||||
|
### 1. Plugin page
|
||||||
|
|
||||||
|
Primary route: the plugin `page` surface should be the central dashboard for all demos.
|
||||||
|
|
||||||
|
Recommended page sections:
|
||||||
|
|
||||||
|
- `Overview`
|
||||||
|
- what this plugin demonstrates
|
||||||
|
- current capabilities granted
|
||||||
|
- current host context
|
||||||
|
- `UI Surfaces`
|
||||||
|
- links explaining where each other surface should appear
|
||||||
|
- `Data + Actions`
|
||||||
|
- buttons and forms for bridge-driven worker demos
|
||||||
|
- `Events + Streams`
|
||||||
|
- emit event
|
||||||
|
- watch event log
|
||||||
|
- stream demo output
|
||||||
|
- `Paperclip Domain APIs`
|
||||||
|
- companies
|
||||||
|
- projects/workspaces
|
||||||
|
- issues
|
||||||
|
- goals
|
||||||
|
- agents
|
||||||
|
- `Local Workspace + Process`
|
||||||
|
- file listing
|
||||||
|
- file read/write scratch area
|
||||||
|
- child process demo
|
||||||
|
- `Jobs + Webhooks + Tools`
|
||||||
|
- job status
|
||||||
|
- webhook URL and recent deliveries
|
||||||
|
- declared tools
|
||||||
|
- `State + Entities + Assets`
|
||||||
|
- scoped state editor
|
||||||
|
- plugin entity inspector
|
||||||
|
- upload/generated asset demo
|
||||||
|
- `Observability`
|
||||||
|
- metrics written
|
||||||
|
- activity log samples
|
||||||
|
- latest worker logs
|
||||||
|
|
||||||
|
### 2. Dashboard widget
|
||||||
|
|
||||||
|
A compact widget on the main dashboard should show:
|
||||||
|
|
||||||
|
- plugin health
|
||||||
|
- count of demos exercised
|
||||||
|
- recent event/stream activity
|
||||||
|
- shortcut to the full plugin page
|
||||||
|
|
||||||
|
### 3. Project sidebar item
|
||||||
|
|
||||||
|
Add a `Kitchen Sink` link under each project that deep-links into a project-scoped plugin tab.
|
||||||
|
|
||||||
|
### 4. Detail tabs
|
||||||
|
|
||||||
|
Use detail tabs to demonstrate entity-context rendering on:
|
||||||
|
|
||||||
|
- `project`
|
||||||
|
- `issue`
|
||||||
|
- `agent`
|
||||||
|
- `goal`
|
||||||
|
|
||||||
|
Each tab should show:
|
||||||
|
|
||||||
|
- the host context it received
|
||||||
|
- the relevant entity fetch via worker bridge
|
||||||
|
- one small action scoped to that entity
|
||||||
|
|
||||||
|
### 5. Comment surfaces
|
||||||
|
|
||||||
|
Use issue comment demos to prove comment-specific extension points:
|
||||||
|
|
||||||
|
- `commentAnnotation`
|
||||||
|
- render parsed metadata below each comment
|
||||||
|
- show comment id, issue id, and a small derived status
|
||||||
|
- `commentContextMenuItem`
|
||||||
|
- add a menu action like `Copy Context To Kitchen Sink`
|
||||||
|
- action writes a plugin entity or state record for later inspection
|
||||||
|
|
||||||
|
### 6. Settings page
|
||||||
|
|
||||||
|
Custom `settingsPage` should be intentionally simple and operational:
|
||||||
|
|
||||||
|
- `About`
|
||||||
|
- `Danger / Trust Model`
|
||||||
|
- demo toggles
|
||||||
|
- local process defaults
|
||||||
|
- workspace scratch-path behavior
|
||||||
|
- secret reference inputs
|
||||||
|
- event/job/webhook sample config
|
||||||
|
|
||||||
|
This plugin should also keep the generic plugin settings `Status` tab useful by writing health, logs, and metrics.
|
||||||
|
|
||||||
|
## Feature Matrix
|
||||||
|
|
||||||
|
Each implemented worker API should have a visible demo.
|
||||||
|
|
||||||
|
### `ctx.config`
|
||||||
|
|
||||||
|
Demo:
|
||||||
|
|
||||||
|
- read live config
|
||||||
|
- show config JSON
|
||||||
|
- react to config changes without restart where possible
|
||||||
|
|
||||||
|
### `ctx.events`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- emit a plugin event
|
||||||
|
- subscribe to plugin events
|
||||||
|
- subscribe to a core Paperclip event such as `issue.created`
|
||||||
|
- show recent received events in a timeline
|
||||||
|
|
||||||
|
### `ctx.jobs`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- one scheduled heartbeat-style demo job
|
||||||
|
- one manual run button from the UI if host supports manual job trigger
|
||||||
|
- show last run result and timestamps
|
||||||
|
|
||||||
|
### `ctx.launchers`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- declare launchers in manifest
|
||||||
|
- optionally register one runtime launcher from the worker
|
||||||
|
- show launcher metadata on the plugin page
|
||||||
|
|
||||||
|
### `ctx.http`
|
||||||
|
|
||||||
|
Demo:
|
||||||
|
|
||||||
|
- make a simple outbound GET request to a safe endpoint
|
||||||
|
- show status code, latency, and JSON result
|
||||||
|
|
||||||
|
Recommendation: default to a Paperclip-local endpoint or a stable public echo endpoint to avoid flaky docs.
|
||||||
|
|
||||||
|
### `ctx.secrets`
|
||||||
|
|
||||||
|
Demo:
|
||||||
|
|
||||||
|
- operator enters a secret reference in config
|
||||||
|
- plugin resolves it on demand
|
||||||
|
- UI only shows masked result length / success status, never raw secret
|
||||||
|
|
||||||
|
### `ctx.assets`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- generate a text asset from the UI
|
||||||
|
- optionally upload a tiny JSON blob or screenshot-like text file
|
||||||
|
- show returned asset URL
|
||||||
|
|
||||||
|
### `ctx.activity`
|
||||||
|
|
||||||
|
Demo:
|
||||||
|
|
||||||
|
- button to write a plugin activity log entry against current company/entity
|
||||||
|
|
||||||
|
### `ctx.state`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- instance-scoped state
|
||||||
|
- company-scoped state
|
||||||
|
- project-scoped state
|
||||||
|
- issue-scoped state
|
||||||
|
- delete/reset controls
|
||||||
|
|
||||||
|
Use a small state inspector/editor on the plugin page.
|
||||||
|
|
||||||
|
### `ctx.entities`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- create plugin-owned sample records
|
||||||
|
- list/filter them
|
||||||
|
- show one realistic use case such as "copied comments" or "demo sync records"
|
||||||
|
|
||||||
|
### `ctx.projects`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- list projects
|
||||||
|
- list project workspaces
|
||||||
|
- resolve primary workspace
|
||||||
|
- resolve workspace for issue
|
||||||
|
|
||||||
|
### `ctx.companies`
|
||||||
|
|
||||||
|
Demo:
|
||||||
|
|
||||||
|
- list companies and show current selected company
|
||||||
|
|
||||||
|
### `ctx.issues`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- list issues in current company
|
||||||
|
- create issue
|
||||||
|
- update issue status/title
|
||||||
|
- list comments
|
||||||
|
- create comment
|
||||||
|
|
||||||
|
### `ctx.agents`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- list agents
|
||||||
|
- invoke one agent with a test prompt
|
||||||
|
- pause/resume where safe
|
||||||
|
|
||||||
|
Agent mutation controls should be behind an explicit warning.
|
||||||
|
|
||||||
|
### `ctx.agents.sessions`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- create agent chat session
|
||||||
|
- send message
|
||||||
|
- stream events back to the UI
|
||||||
|
- close session
|
||||||
|
|
||||||
|
This is a strong candidate for the best "wow" demo on the plugin page.
|
||||||
|
|
||||||
|
### `ctx.goals`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- list goals
|
||||||
|
- create goal
|
||||||
|
- update status/title
|
||||||
|
|
||||||
|
### `ctx.data`
|
||||||
|
|
||||||
|
Use throughout the plugin for all read-side bridge demos.
|
||||||
|
|
||||||
|
### `ctx.actions`
|
||||||
|
|
||||||
|
Use throughout the plugin for all mutation-side bridge demos.
|
||||||
|
|
||||||
|
### `ctx.streams`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- live event log stream
|
||||||
|
- token-style stream from an agent session relay
|
||||||
|
- fake progress stream for a long-running action
|
||||||
|
|
||||||
|
### `ctx.tools`
|
||||||
|
|
||||||
|
Demos:
|
||||||
|
|
||||||
|
- declare 2-3 simple agent tools
|
||||||
|
- tool 1: echo/diagnostics
|
||||||
|
- tool 2: project/workspace summary
|
||||||
|
- tool 3: create issue or write plugin state
|
||||||
|
|
||||||
|
The plugin page should list declared tools and show example input payloads.
|
||||||
|
|
||||||
|
### `ctx.metrics`
|
||||||
|
|
||||||
|
Demo:
|
||||||
|
|
||||||
|
- write a sample metric on each major demo action
|
||||||
|
- surface a small recent metrics table in the plugin page
|
||||||
|
|
||||||
|
### `ctx.logger`
|
||||||
|
|
||||||
|
Demo:
|
||||||
|
|
||||||
|
- every action logs structured entries
|
||||||
|
- plugin settings `Status` page then doubles as the log viewer
|
||||||
|
|
||||||
|
## Local Workspace And Process Demos
|
||||||
|
|
||||||
|
The plugin SDK intentionally leaves file/process operations to the plugin itself once it has workspace metadata.
|
||||||
|
|
||||||
|
The kitchen-sink plugin should demonstrate that explicitly.
|
||||||
|
|
||||||
|
### Workspace demos
|
||||||
|
|
||||||
|
- list files from a selected workspace
|
||||||
|
- read a file
|
||||||
|
- write to a plugin-owned scratch file
|
||||||
|
- optionally search files with `rg` if available
|
||||||
|
|
||||||
|
### Process demos
|
||||||
|
|
||||||
|
- run a short-lived command like `pwd`, `ls`, or `git status`
|
||||||
|
- stream stdout/stderr back to UI
|
||||||
|
- show exit code and timing
|
||||||
|
|
||||||
|
Important safeguards:
|
||||||
|
|
||||||
|
- default commands must be read-only
|
||||||
|
- no shell interpolation from arbitrary free-form input in v1
|
||||||
|
- provide a curated command list or a strongly validated command form
|
||||||
|
- clearly label this area as local-only and trusted-only
|
||||||
|
|
||||||
|
## Proposed Manifest Coverage
|
||||||
|
|
||||||
|
The plugin should aim to declare:
|
||||||
|
|
||||||
|
- `page`
|
||||||
|
- `settingsPage`
|
||||||
|
- `dashboardWidget`
|
||||||
|
- `detailTab` for `project`, `issue`, `agent`, `goal`
|
||||||
|
- `projectSidebarItem`
|
||||||
|
- `commentAnnotation`
|
||||||
|
- `commentContextMenuItem`
|
||||||
|
|
||||||
|
Then, after host validation, add if supported:
|
||||||
|
|
||||||
|
- `sidebar`
|
||||||
|
- `sidebarPanel`
|
||||||
|
- `taskDetailView`
|
||||||
|
- `toolbarButton`
|
||||||
|
- `contextMenuItem`
|
||||||
|
|
||||||
|
It should also declare one or more `ui.launchers` entries to exercise launcher behavior independently of slot rendering.
|
||||||
|
|
||||||
|
## Proposed Package Layout
|
||||||
|
|
||||||
|
New package:
|
||||||
|
|
||||||
|
- `packages/plugins/examples/plugin-kitchen-sink-example/`
|
||||||
|
|
||||||
|
Expected files:
|
||||||
|
|
||||||
|
- `package.json`
|
||||||
|
- `README.md`
|
||||||
|
- `tsconfig.json`
|
||||||
|
- `src/index.ts`
|
||||||
|
- `src/manifest.ts`
|
||||||
|
- `src/worker.ts`
|
||||||
|
- `src/ui/index.tsx`
|
||||||
|
- `src/ui/components/...`
|
||||||
|
- `src/ui/hooks/...`
|
||||||
|
- `src/lib/...`
|
||||||
|
- optional `scripts/build-ui.mjs` if UI bundling needs esbuild
|
||||||
|
|
||||||
|
## Proposed Internal Architecture
|
||||||
|
|
||||||
|
### Worker modules
|
||||||
|
|
||||||
|
Recommended split:
|
||||||
|
|
||||||
|
- `src/worker.ts`
|
||||||
|
- plugin definition and wiring
|
||||||
|
- `src/worker/data.ts`
|
||||||
|
- `ctx.data.register(...)`
|
||||||
|
- `src/worker/actions.ts`
|
||||||
|
- `ctx.actions.register(...)`
|
||||||
|
- `src/worker/events.ts`
|
||||||
|
- event subscriptions and event log buffer
|
||||||
|
- `src/worker/jobs.ts`
|
||||||
|
- scheduled job handlers
|
||||||
|
- `src/worker/tools.ts`
|
||||||
|
- tool declarations and handlers
|
||||||
|
- `src/worker/local-runtime.ts`
|
||||||
|
- file/process demos
|
||||||
|
- `src/worker/demo-store.ts`
|
||||||
|
- helpers for state/entities/assets/metrics
|
||||||
|
|
||||||
|
### UI modules
|
||||||
|
|
||||||
|
Recommended split:
|
||||||
|
|
||||||
|
- `src/ui/index.tsx`
|
||||||
|
- exported slot components
|
||||||
|
- `src/ui/page/KitchenSinkPage.tsx`
|
||||||
|
- `src/ui/settings/KitchenSinkSettingsPage.tsx`
|
||||||
|
- `src/ui/widgets/KitchenSinkDashboardWidget.tsx`
|
||||||
|
- `src/ui/tabs/ProjectKitchenSinkTab.tsx`
|
||||||
|
- `src/ui/tabs/IssueKitchenSinkTab.tsx`
|
||||||
|
- `src/ui/tabs/AgentKitchenSinkTab.tsx`
|
||||||
|
- `src/ui/tabs/GoalKitchenSinkTab.tsx`
|
||||||
|
- `src/ui/comments/KitchenSinkCommentAnnotation.tsx`
|
||||||
|
- `src/ui/comments/KitchenSinkCommentMenuItem.tsx`
|
||||||
|
- `src/ui/shared/...`
|
||||||
|
|
||||||
|
## Configuration Schema
|
||||||
|
|
||||||
|
The plugin should have a substantial but understandable `instanceConfigSchema`.
|
||||||
|
|
||||||
|
Recommended config fields:
|
||||||
|
|
||||||
|
- `enableDangerousDemos`
|
||||||
|
- `enableWorkspaceDemos`
|
||||||
|
- `enableProcessDemos`
|
||||||
|
- `showSidebarEntry`
|
||||||
|
- `showSidebarPanel`
|
||||||
|
- `showProjectSidebarItem`
|
||||||
|
- `showCommentAnnotation`
|
||||||
|
- `showCommentContextMenuItem`
|
||||||
|
- `showToolbarLauncher`
|
||||||
|
- `defaultDemoCompanyId` optional
|
||||||
|
- `secretRefExample`
|
||||||
|
- `httpDemoUrl`
|
||||||
|
- `processAllowedCommands`
|
||||||
|
- `workspaceScratchSubdir`
|
||||||
|
|
||||||
|
Defaults should keep risky behavior off.
|
||||||
|
|
||||||
|
## Safety Defaults
|
||||||
|
|
||||||
|
Default posture:
|
||||||
|
|
||||||
|
- UI and read-only demos on
|
||||||
|
- mutating domain demos on but explicitly labeled
|
||||||
|
- process demos off by default
|
||||||
|
- no arbitrary shell input by default
|
||||||
|
- no raw secret rendering ever
|
||||||
|
|
||||||
|
## Phased Build Plan
|
||||||
|
|
||||||
|
### Phase 1: Core plugin skeleton
|
||||||
|
|
||||||
|
- scaffold package
|
||||||
|
- add manifest, worker, UI entrypoints
|
||||||
|
- add README
|
||||||
|
- make it appear in bundled examples list
|
||||||
|
|
||||||
|
### Phase 2: Core, confirmed UI surfaces
|
||||||
|
|
||||||
|
- plugin page
|
||||||
|
- settings page
|
||||||
|
- dashboard widget
|
||||||
|
- project sidebar item
|
||||||
|
- detail tabs
|
||||||
|
|
||||||
|
### Phase 3: Core worker APIs
|
||||||
|
|
||||||
|
- config
|
||||||
|
- state
|
||||||
|
- entities
|
||||||
|
- companies/projects/issues/goals
|
||||||
|
- data/actions
|
||||||
|
- metrics/logger/activity
|
||||||
|
|
||||||
|
### Phase 4: Real-time and automation APIs
|
||||||
|
|
||||||
|
- streams
|
||||||
|
- events
|
||||||
|
- jobs
|
||||||
|
- webhooks
|
||||||
|
- agent sessions
|
||||||
|
- tools
|
||||||
|
|
||||||
|
### Phase 5: Local trusted runtime demos
|
||||||
|
|
||||||
|
- workspace file demos
|
||||||
|
- child process demos
|
||||||
|
- guarded by config
|
||||||
|
|
||||||
|
### Phase 6: Secondary UI surfaces
|
||||||
|
|
||||||
|
- comment annotation
|
||||||
|
- comment context menu item
|
||||||
|
- launchers
|
||||||
|
|
||||||
|
### Phase 7: Validation-only surfaces
|
||||||
|
|
||||||
|
Validate whether the current host truly mounts:
|
||||||
|
|
||||||
|
- `sidebar`
|
||||||
|
- `sidebarPanel`
|
||||||
|
- `taskDetailView`
|
||||||
|
- direct-slot `toolbarButton`
|
||||||
|
- direct-slot `contextMenuItem`
|
||||||
|
|
||||||
|
If mounted, add demos.
|
||||||
|
If not mounted, document them as SDK-defined but host-pending.
|
||||||
|
|
||||||
|
## Documentation Deliverables
|
||||||
|
|
||||||
|
The plugin should ship with a README that includes:
|
||||||
|
|
||||||
|
- what it demonstrates
|
||||||
|
- which surfaces are local-only
|
||||||
|
- how to install it
|
||||||
|
- where each UI surface should appear
|
||||||
|
- a mapping from demo card to SDK API
|
||||||
|
|
||||||
|
It should also be referenced from plugin docs as the "reference everything plugin".
|
||||||
|
|
||||||
|
## Testing And Verification
|
||||||
|
|
||||||
|
Minimum verification:
|
||||||
|
|
||||||
|
- package typecheck/build
|
||||||
|
- install from bundled example list
|
||||||
|
- page loads
|
||||||
|
- widget appears
|
||||||
|
- project tab appears
|
||||||
|
- comment surfaces render
|
||||||
|
- settings page loads
|
||||||
|
- key actions succeed
|
||||||
|
|
||||||
|
Recommended manual checklist:
|
||||||
|
|
||||||
|
- create issue from plugin
|
||||||
|
- create goal from plugin
|
||||||
|
- emit and receive plugin event
|
||||||
|
- stream action output
|
||||||
|
- open agent session and receive streamed reply
|
||||||
|
- upload an asset
|
||||||
|
- write plugin activity log
|
||||||
|
- run a safe local process demo
|
||||||
|
|
||||||
|
## Open Questions
|
||||||
|
|
||||||
|
1. Should the process demo remain curated-command-only in the first pass?
|
||||||
|
Recommendation: yes.
|
||||||
|
|
||||||
|
2. Should the plugin create throwaway "kitchen sink demo" issues/goals automatically?
|
||||||
|
Recommendation: no. Make creation explicit.
|
||||||
|
|
||||||
|
3. Should we expose unsupported-but-typed surfaces in the UI even if host mounting is not wired?
|
||||||
|
Recommendation: yes, but label them as `SDK-defined / host validation pending`.
|
||||||
|
|
||||||
|
4. Should agent mutation demos include pause/resume by default?
|
||||||
|
Recommendation: probably yes, but behind a warning block.
|
||||||
|
|
||||||
|
5. Should this plugin be treated as a supported regression harness in CI later?
|
||||||
|
Recommendation: yes. Long term, this should be the plugin-runtime smoke test package.
|
||||||
|
|
||||||
|
## Recommended Next Step
|
||||||
|
|
||||||
|
If this plan looks right, the next implementation pass should start by building only:
|
||||||
|
|
||||||
|
- package skeleton
|
||||||
|
- page
|
||||||
|
- settings page
|
||||||
|
- dashboard widget
|
||||||
|
- one project detail tab
|
||||||
|
- one issue detail tab
|
||||||
|
- the basic worker/action/data/state/event scaffolding
|
||||||
|
|
||||||
|
That is enough to lock the architecture before filling in every demo surface.
|
||||||
1126
doc/plans/2026-03-13-workspace-product-model-and-work-product.md
Normal file
1126
doc/plans/2026-03-13-workspace-product-model-and-work-product.md
Normal file
File diff suppressed because it is too large
Load Diff
154
doc/plugins/PLUGIN_AUTHORING_GUIDE.md
Normal file
154
doc/plugins/PLUGIN_AUTHORING_GUIDE.md
Normal file
@@ -0,0 +1,154 @@
|
|||||||
|
# Plugin Authoring Guide
|
||||||
|
|
||||||
|
This guide describes the current, implemented way to create a Paperclip plugin in this repo.
|
||||||
|
|
||||||
|
It is intentionally narrower than [PLUGIN_SPEC.md](./PLUGIN_SPEC.md). The spec includes future ideas; this guide only covers the alpha surface that exists now.
|
||||||
|
|
||||||
|
## Current reality
|
||||||
|
|
||||||
|
- Treat plugin workers and plugin UI as trusted code.
|
||||||
|
- Plugin UI runs as same-origin JavaScript inside the main Paperclip app.
|
||||||
|
- Worker-side host APIs are capability-gated.
|
||||||
|
- Plugin UI is not sandboxed by manifest capabilities.
|
||||||
|
- There is no host-provided shared React component kit for plugins yet.
|
||||||
|
- `ctx.assets` is not supported in the current runtime.
|
||||||
|
|
||||||
|
## Scaffold a plugin
|
||||||
|
|
||||||
|
Use the scaffold package:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm --filter @paperclipai/create-paperclip-plugin build
|
||||||
|
node packages/plugins/create-paperclip-plugin/dist/index.js @yourscope/plugin-name --output ./packages/plugins/examples
|
||||||
|
```
|
||||||
|
|
||||||
|
For a plugin that lives outside the Paperclip repo:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm --filter @paperclipai/create-paperclip-plugin build
|
||||||
|
node packages/plugins/create-paperclip-plugin/dist/index.js @yourscope/plugin-name \
|
||||||
|
--output /absolute/path/to/plugin-repos \
|
||||||
|
--sdk-path /absolute/path/to/paperclip/packages/plugins/sdk
|
||||||
|
```
|
||||||
|
|
||||||
|
That creates a package with:
|
||||||
|
|
||||||
|
- `src/manifest.ts`
|
||||||
|
- `src/worker.ts`
|
||||||
|
- `src/ui/index.tsx`
|
||||||
|
- `tests/plugin.spec.ts`
|
||||||
|
- `esbuild.config.mjs`
|
||||||
|
- `rollup.config.mjs`
|
||||||
|
|
||||||
|
Inside this monorepo, the scaffold uses `workspace:*` for `@paperclipai/plugin-sdk`.
|
||||||
|
|
||||||
|
Outside this monorepo, the scaffold snapshots `@paperclipai/plugin-sdk` from the local Paperclip checkout into a `.paperclip-sdk/` tarball so you can build and test a plugin without publishing anything to npm first.
|
||||||
|
|
||||||
|
## Recommended local workflow
|
||||||
|
|
||||||
|
From the generated plugin folder:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm install
|
||||||
|
pnpm typecheck
|
||||||
|
pnpm test
|
||||||
|
pnpm build
|
||||||
|
```
|
||||||
|
|
||||||
|
For local development, install it into Paperclip from an absolute local path through the plugin manager or API. The server supports local filesystem installs and watches local-path plugins for file changes so worker restarts happen automatically after rebuilds.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
curl -X POST http://127.0.0.1:3100/api/plugins/install \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"packageName":"/absolute/path/to/your-plugin","isLocalPath":true}'
|
||||||
|
```
|
||||||
|
|
||||||
|
## Supported alpha surface
|
||||||
|
|
||||||
|
Worker:
|
||||||
|
|
||||||
|
- config
|
||||||
|
- events
|
||||||
|
- jobs
|
||||||
|
- launchers
|
||||||
|
- http
|
||||||
|
- secrets
|
||||||
|
- activity
|
||||||
|
- state
|
||||||
|
- entities
|
||||||
|
- projects and project workspaces
|
||||||
|
- companies
|
||||||
|
- issues and comments
|
||||||
|
- agents and agent sessions
|
||||||
|
- goals
|
||||||
|
- data/actions
|
||||||
|
- streams
|
||||||
|
- tools
|
||||||
|
- metrics
|
||||||
|
- logger
|
||||||
|
|
||||||
|
UI:
|
||||||
|
|
||||||
|
- `usePluginData`
|
||||||
|
- `usePluginAction`
|
||||||
|
- `usePluginStream`
|
||||||
|
- `usePluginToast`
|
||||||
|
- `useHostContext`
|
||||||
|
- typed slot props from `@paperclipai/plugin-sdk/ui`
|
||||||
|
|
||||||
|
Mount surfaces currently wired in the host include:
|
||||||
|
|
||||||
|
- `page`
|
||||||
|
- `settingsPage`
|
||||||
|
- `dashboardWidget`
|
||||||
|
- `sidebar`
|
||||||
|
- `sidebarPanel`
|
||||||
|
- `detailTab`
|
||||||
|
- `taskDetailView`
|
||||||
|
- `projectSidebarItem`
|
||||||
|
- `toolbarButton`
|
||||||
|
- `contextMenuItem`
|
||||||
|
- `commentAnnotation`
|
||||||
|
- `commentContextMenuItem`
|
||||||
|
|
||||||
|
## Company routes
|
||||||
|
|
||||||
|
Plugins may declare a `page` slot with `routePath` to own a company route like:
|
||||||
|
|
||||||
|
```text
|
||||||
|
/:companyPrefix/<routePath>
|
||||||
|
```
|
||||||
|
|
||||||
|
Rules:
|
||||||
|
|
||||||
|
- `routePath` must be a single lowercase slug
|
||||||
|
- it cannot collide with reserved host routes
|
||||||
|
- it cannot duplicate another installed plugin page route
|
||||||
|
|
||||||
|
## Publishing guidance
|
||||||
|
|
||||||
|
- Use npm packages as the deployment artifact.
|
||||||
|
- Treat repo-local example installs as a development workflow only.
|
||||||
|
- Prefer keeping plugin UI self-contained inside the package.
|
||||||
|
- Do not rely on host design-system components or undocumented app internals.
|
||||||
|
- GitHub repository installs are not a first-class workflow today. For local development, use a checked-out local path. For production, publish to npm or a private npm-compatible registry.
|
||||||
|
|
||||||
|
## Verification before handoff
|
||||||
|
|
||||||
|
At minimum:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm --filter <your-plugin-package> typecheck
|
||||||
|
pnpm --filter <your-plugin-package> test
|
||||||
|
pnpm --filter <your-plugin-package> build
|
||||||
|
```
|
||||||
|
|
||||||
|
If you changed host integration too, also run:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm -r typecheck
|
||||||
|
pnpm test:run
|
||||||
|
pnpm build
|
||||||
|
```
|
||||||
@@ -8,6 +8,29 @@ It expands the brief plugin notes in [doc/SPEC.md](../SPEC.md) and should be rea
|
|||||||
This is not part of the V1 implementation contract in [doc/SPEC-implementation.md](../SPEC-implementation.md).
|
This is not part of the V1 implementation contract in [doc/SPEC-implementation.md](../SPEC-implementation.md).
|
||||||
It is the full target architecture for the plugin system that should follow V1.
|
It is the full target architecture for the plugin system that should follow V1.
|
||||||
|
|
||||||
|
## Current implementation caveats
|
||||||
|
|
||||||
|
The code in this repo now includes an early plugin runtime and admin UI, but it does not yet deliver the full deployment model described in this spec.
|
||||||
|
|
||||||
|
Today, the practical deployment model is:
|
||||||
|
|
||||||
|
- single-tenant
|
||||||
|
- self-hosted
|
||||||
|
- single-node or otherwise filesystem-persistent
|
||||||
|
|
||||||
|
Current limitations to keep in mind:
|
||||||
|
|
||||||
|
- Plugin UI bundles currently run as same-origin JavaScript inside the main Paperclip app. Treat plugin UI as trusted code, not a sandboxed frontend capability boundary.
|
||||||
|
- Manifest capabilities currently gate worker-side host RPC calls. They do not prevent plugin UI code from calling ordinary Paperclip HTTP APIs directly.
|
||||||
|
- Runtime installs assume a writable local filesystem for the plugin package directory and plugin data directory.
|
||||||
|
- Runtime npm installs assume `npm` is available in the running environment and that the host can reach the configured package registry.
|
||||||
|
- Published npm packages are the intended install artifact for deployed plugins.
|
||||||
|
- The repo example plugins under `packages/plugins/examples/` are development conveniences. They work from a source checkout and should not be assumed to exist in a generic published build unless they are explicitly shipped with that build.
|
||||||
|
- Dynamic plugin install is not yet cloud-ready for horizontally scaled or ephemeral deployments. There is no shared artifact store, install coordination, or cross-node distribution layer yet.
|
||||||
|
- The current runtime does not yet ship a real host-provided plugin UI component kit, and it does not support plugin asset uploads/reads. Treat those as future-scope ideas in this spec, not current implementation promises.
|
||||||
|
|
||||||
|
In practice, that means the current implementation is a good fit for local development and self-hosted persistent deployments, but not yet for multi-instance cloud plugin distribution.
|
||||||
|
|
||||||
## 1. Scope
|
## 1. Scope
|
||||||
|
|
||||||
This spec covers:
|
This spec covers:
|
||||||
@@ -212,6 +235,8 @@ Suggested layout:
|
|||||||
|
|
||||||
The package install directory and the plugin data directory are separate.
|
The package install directory and the plugin data directory are separate.
|
||||||
|
|
||||||
|
This on-disk model is the reason the current implementation expects a persistent writable host filesystem. Cloud-safe artifact replication is future work.
|
||||||
|
|
||||||
## 8.2 Operator Commands
|
## 8.2 Operator Commands
|
||||||
|
|
||||||
Paperclip should add CLI commands:
|
Paperclip should add CLI commands:
|
||||||
@@ -237,6 +262,8 @@ The install process is:
|
|||||||
7. Start plugin worker and run health/validation.
|
7. Start plugin worker and run health/validation.
|
||||||
8. Mark plugin `ready` or `error`.
|
8. Mark plugin `ready` or `error`.
|
||||||
|
|
||||||
|
For the current implementation, this install flow should be read as a single-host workflow. A successful install writes packages to the local host, and other app nodes will not automatically receive that plugin unless a future shared distribution mechanism is added.
|
||||||
|
|
||||||
## 9. Load Order And Precedence
|
## 9. Load Order And Precedence
|
||||||
|
|
||||||
Load order must be deterministic.
|
Load order must be deterministic.
|
||||||
|
|||||||
@@ -30,6 +30,8 @@ Codex uses `previous_response_id` for session continuity. The adapter serializes
|
|||||||
|
|
||||||
The adapter symlinks Paperclip skills into the global Codex skills directory (`~/.codex/skills`). Existing user skills are not overwritten.
|
The adapter symlinks Paperclip skills into the global Codex skills directory (`~/.codex/skills`). Existing user skills are not overwritten.
|
||||||
|
|
||||||
|
When Paperclip is running inside a managed worktree instance (`PAPERCLIP_IN_WORKTREE=true`), the adapter instead uses a worktree-isolated `CODEX_HOME` under the Paperclip instance so Codex skills, sessions, logs, and other runtime state do not leak across checkouts. It seeds that isolated home from the user's main Codex home for shared auth/config continuity.
|
||||||
|
|
||||||
For manual local CLI usage outside heartbeat runs (for example running as `codexcoder` directly), use:
|
For manual local CLI usage outside heartbeat runs (for example running as `codexcoder` directly), use:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ summary: Guide to building a custom adapter
|
|||||||
Build a custom adapter to connect Paperclip to any agent runtime.
|
Build a custom adapter to connect Paperclip to any agent runtime.
|
||||||
|
|
||||||
<Tip>
|
<Tip>
|
||||||
If you're using Claude Code, the `create-agent-adapter` skill can guide you through the full adapter creation process interactively. Just ask Claude to create a new adapter and it will walk you through each step.
|
If you're using Claude Code, the `.agents/skills/create-agent-adapter` skill can guide you through the full adapter creation process interactively. Just ask Claude to create a new adapter and it will walk you through each step.
|
||||||
</Tip>
|
</Tip>
|
||||||
|
|
||||||
## Package Structure
|
## Package Structure
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
---
|
---
|
||||||
title: Issues
|
title: Issues
|
||||||
summary: Issue CRUD, checkout/release, comments, and attachments
|
summary: Issue CRUD, checkout/release, comments, documents, and attachments
|
||||||
---
|
---
|
||||||
|
|
||||||
Issues are the unit of work in Paperclip. They support hierarchical relationships, atomic checkout, comments, and file attachments.
|
Issues are the unit of work in Paperclip. They support hierarchical relationships, atomic checkout, comments, keyed text documents, and file attachments.
|
||||||
|
|
||||||
## List Issues
|
## List Issues
|
||||||
|
|
||||||
@@ -29,6 +29,12 @@ GET /api/issues/{issueId}
|
|||||||
|
|
||||||
Returns the issue with `project`, `goal`, and `ancestors` (parent chain with their projects and goals).
|
Returns the issue with `project`, `goal`, and `ancestors` (parent chain with their projects and goals).
|
||||||
|
|
||||||
|
The response also includes:
|
||||||
|
|
||||||
|
- `planDocument`: the full text of the issue document with key `plan`, when present
|
||||||
|
- `documentSummaries`: metadata for all linked issue documents
|
||||||
|
- `legacyPlanDocument`: a read-only fallback when the description still contains an old `<plan>` block
|
||||||
|
|
||||||
## Create Issue
|
## Create Issue
|
||||||
|
|
||||||
```
|
```
|
||||||
@@ -100,6 +106,54 @@ POST /api/issues/{issueId}/comments
|
|||||||
|
|
||||||
@-mentions (`@AgentName`) in comments trigger heartbeats for the mentioned agent.
|
@-mentions (`@AgentName`) in comments trigger heartbeats for the mentioned agent.
|
||||||
|
|
||||||
|
## Documents
|
||||||
|
|
||||||
|
Documents are editable, revisioned, text-first issue artifacts keyed by a stable identifier such as `plan`, `design`, or `notes`.
|
||||||
|
|
||||||
|
### List
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/issues/{issueId}/documents
|
||||||
|
```
|
||||||
|
|
||||||
|
### Get By Key
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/issues/{issueId}/documents/{key}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Create Or Update
|
||||||
|
|
||||||
|
```
|
||||||
|
PUT /api/issues/{issueId}/documents/{key}
|
||||||
|
{
|
||||||
|
"title": "Implementation plan",
|
||||||
|
"format": "markdown",
|
||||||
|
"body": "# Plan\n\n...",
|
||||||
|
"baseRevisionId": "{latestRevisionId}"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Rules:
|
||||||
|
|
||||||
|
- omit `baseRevisionId` when creating a new document
|
||||||
|
- provide the current `baseRevisionId` when updating an existing document
|
||||||
|
- stale `baseRevisionId` returns `409 Conflict`
|
||||||
|
|
||||||
|
### Revision History
|
||||||
|
|
||||||
|
```
|
||||||
|
GET /api/issues/{issueId}/documents/{key}/revisions
|
||||||
|
```
|
||||||
|
|
||||||
|
### Delete
|
||||||
|
|
||||||
|
```
|
||||||
|
DELETE /api/issues/{issueId}/documents/{key}
|
||||||
|
```
|
||||||
|
|
||||||
|
Delete is board-only in the current implementation.
|
||||||
|
|
||||||
## Attachments
|
## Attachments
|
||||||
|
|
||||||
### Upload
|
### Upload
|
||||||
|
|||||||
569
docs/plans/2026-03-13-issue-documents-plan.md
Normal file
569
docs/plans/2026-03-13-issue-documents-plan.md
Normal file
@@ -0,0 +1,569 @@
|
|||||||
|
# Issue Documents Plan
|
||||||
|
|
||||||
|
Status: Draft
|
||||||
|
Owner: Backend + UI + Agent Protocol
|
||||||
|
Date: 2026-03-13
|
||||||
|
Primary issue: `PAP-448`
|
||||||
|
|
||||||
|
## Summary
|
||||||
|
|
||||||
|
Add first-class **documents** to Paperclip as editable, revisioned, company-scoped text artifacts that can be linked to issues.
|
||||||
|
|
||||||
|
The first required convention is a document with key `plan`.
|
||||||
|
|
||||||
|
This solves the immediate workflow problem in `PAP-448`:
|
||||||
|
|
||||||
|
- plans should stop living inside issue descriptions as `<plan>` blocks
|
||||||
|
- agents and board users should be able to create/update issue documents directly
|
||||||
|
- `GET /api/issues/:id` should include the full `plan` document and expose the other available documents
|
||||||
|
- issue detail should render documents under the description
|
||||||
|
|
||||||
|
This should be built as the **text-document slice** of the broader artifact system, not as a replacement for attachments/assets.
|
||||||
|
|
||||||
|
## Recommended Product Shape
|
||||||
|
|
||||||
|
### Documents vs attachments vs artifacts
|
||||||
|
|
||||||
|
- **Documents**: editable text content with stable keys and revision history.
|
||||||
|
- **Attachments**: uploaded/generated opaque files backed by storage (`assets` + `issue_attachments`).
|
||||||
|
- **Artifacts**: later umbrella/read-model that can unify documents, attachments, previews, and workspace files.
|
||||||
|
|
||||||
|
Recommendation:
|
||||||
|
|
||||||
|
- implement **issue documents now**
|
||||||
|
- keep existing attachments as-is
|
||||||
|
- defer full artifact unification until there is a second real consumer beyond issue documents + attachments
|
||||||
|
|
||||||
|
This keeps `PAP-448` focused while still fitting the larger artifact direction.
|
||||||
|
|
||||||
|
## Goals
|
||||||
|
|
||||||
|
1. Give issues first-class keyed documents, starting with `plan`.
|
||||||
|
2. Make documents editable by board users and same-company agents with issue access.
|
||||||
|
3. Preserve change history with append-only revisions.
|
||||||
|
4. Make the `plan` document automatically available in the normal issue fetch used by agents/heartbeats.
|
||||||
|
5. Replace the current `<plan>`-in-description convention in skills/docs.
|
||||||
|
6. Keep the design compatible with a future artifact/deliverables layer.
|
||||||
|
|
||||||
|
## Non-Goals
|
||||||
|
|
||||||
|
- full collaborative doc editing
|
||||||
|
- binary-file version history
|
||||||
|
- browser IDE or workspace editor
|
||||||
|
- full artifact-system implementation in the same change
|
||||||
|
- generalized polymorphic relations for every entity type on day one
|
||||||
|
|
||||||
|
## Product Decisions
|
||||||
|
|
||||||
|
### 1. Keyed issue documents
|
||||||
|
|
||||||
|
Each issue can have multiple documents. Each document relation has a stable key:
|
||||||
|
|
||||||
|
- `plan`
|
||||||
|
- `design`
|
||||||
|
- `notes`
|
||||||
|
- `report`
|
||||||
|
- custom keys later
|
||||||
|
|
||||||
|
Key rules:
|
||||||
|
|
||||||
|
- unique per issue, case-insensitive
|
||||||
|
- normalized to lowercase slug form
|
||||||
|
- machine-oriented and stable
|
||||||
|
- title is separate and user-facing
|
||||||
|
|
||||||
|
The `plan` key is conventional and reserved by Paperclip workflow/docs.
|
||||||
|
|
||||||
|
### 2. Text-first v1
|
||||||
|
|
||||||
|
V1 documents should be text-first, not arbitrary blobs.
|
||||||
|
|
||||||
|
Recommended supported formats:
|
||||||
|
|
||||||
|
- `markdown`
|
||||||
|
- `plain_text`
|
||||||
|
- `json`
|
||||||
|
- `html`
|
||||||
|
|
||||||
|
Recommendation:
|
||||||
|
|
||||||
|
- optimize UI for `markdown`
|
||||||
|
- allow raw editing for the others
|
||||||
|
- keep PDFs/images/CSVs/etc as attachments/artifacts, not editable documents
|
||||||
|
|
||||||
|
### 3. Revision model
|
||||||
|
|
||||||
|
Every document update creates a new immutable revision.
|
||||||
|
|
||||||
|
The current document row stores the latest snapshot for fast reads.
|
||||||
|
|
||||||
|
### 4. Concurrency model
|
||||||
|
|
||||||
|
Do not use silent last-write-wins.
|
||||||
|
|
||||||
|
Updates should include `baseRevisionId`:
|
||||||
|
|
||||||
|
- create: no base revision required
|
||||||
|
- update: `baseRevisionId` must match current latest revision
|
||||||
|
- mismatch: return `409 Conflict`
|
||||||
|
|
||||||
|
This is important because both board users and agents may edit the same document.
|
||||||
|
|
||||||
|
### 5. Issue fetch behavior
|
||||||
|
|
||||||
|
`GET /api/issues/:id` should include:
|
||||||
|
|
||||||
|
- full `planDocument` when a `plan` document exists
|
||||||
|
- `documentSummaries` for all linked documents
|
||||||
|
|
||||||
|
It should not inline every document body by default.
|
||||||
|
|
||||||
|
This keeps issue fetches useful for agents without making every issue payload unbounded.
|
||||||
|
|
||||||
|
### 6. Legacy `<plan>` compatibility
|
||||||
|
|
||||||
|
If an issue has no `plan` document but its description contains a legacy `<plan>` block:
|
||||||
|
|
||||||
|
- expose that as a legacy read-only fallback in API/UI
|
||||||
|
- mark it as legacy/synthetic
|
||||||
|
- prefer a real `plan` document when both exist
|
||||||
|
|
||||||
|
Recommendation:
|
||||||
|
|
||||||
|
- do not auto-rewrite old issue descriptions in the first rollout
|
||||||
|
- provide an explicit import/migrate path later
|
||||||
|
|
||||||
|
## Proposed Data Model
|
||||||
|
|
||||||
|
Recommendation: make documents first-class, but keep issue linkage explicit via a join table.
|
||||||
|
|
||||||
|
This preserves foreign keys today and gives a clean path to future `project_documents` or `company_documents` tables later.
|
||||||
|
|
||||||
|
## Tables
|
||||||
|
|
||||||
|
### `documents`
|
||||||
|
|
||||||
|
Canonical text document record.
|
||||||
|
|
||||||
|
Suggested columns:
|
||||||
|
|
||||||
|
- `id`
|
||||||
|
- `company_id`
|
||||||
|
- `title`
|
||||||
|
- `format`
|
||||||
|
- `latest_body`
|
||||||
|
- `latest_revision_id`
|
||||||
|
- `latest_revision_number`
|
||||||
|
- `created_by_agent_id`
|
||||||
|
- `created_by_user_id`
|
||||||
|
- `updated_by_agent_id`
|
||||||
|
- `updated_by_user_id`
|
||||||
|
- `created_at`
|
||||||
|
- `updated_at`
|
||||||
|
|
||||||
|
### `document_revisions`
|
||||||
|
|
||||||
|
Append-only history.
|
||||||
|
|
||||||
|
Suggested columns:
|
||||||
|
|
||||||
|
- `id`
|
||||||
|
- `company_id`
|
||||||
|
- `document_id`
|
||||||
|
- `revision_number`
|
||||||
|
- `body`
|
||||||
|
- `change_summary`
|
||||||
|
- `created_by_agent_id`
|
||||||
|
- `created_by_user_id`
|
||||||
|
- `created_at`
|
||||||
|
|
||||||
|
Constraints:
|
||||||
|
|
||||||
|
- unique `(document_id, revision_number)`
|
||||||
|
|
||||||
|
### `issue_documents`
|
||||||
|
|
||||||
|
Issue relation + workflow key.
|
||||||
|
|
||||||
|
Suggested columns:
|
||||||
|
|
||||||
|
- `id`
|
||||||
|
- `company_id`
|
||||||
|
- `issue_id`
|
||||||
|
- `document_id`
|
||||||
|
- `key`
|
||||||
|
- `created_at`
|
||||||
|
- `updated_at`
|
||||||
|
|
||||||
|
Constraints:
|
||||||
|
|
||||||
|
- unique `(company_id, issue_id, key)`
|
||||||
|
- unique `(document_id)` to keep one issue relation per document in v1
|
||||||
|
|
||||||
|
## Why not use `assets` for this?
|
||||||
|
|
||||||
|
Because `assets` solves blob storage, not:
|
||||||
|
|
||||||
|
- stable keyed semantics like `plan`
|
||||||
|
- inline text editing
|
||||||
|
- revision history
|
||||||
|
- optimistic concurrency
|
||||||
|
- cheap inclusion in `GET /issues/:id`
|
||||||
|
|
||||||
|
Documents and attachments should remain separate primitives, then meet later in a deliverables/artifact read-model.
|
||||||
|
|
||||||
|
## Shared Types and API Contract
|
||||||
|
|
||||||
|
## New shared types
|
||||||
|
|
||||||
|
Add:
|
||||||
|
|
||||||
|
- `DocumentFormat`
|
||||||
|
- `IssueDocument`
|
||||||
|
- `IssueDocumentSummary`
|
||||||
|
- `DocumentRevision`
|
||||||
|
|
||||||
|
Recommended `IssueDocument` shape:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type DocumentFormat = "markdown" | "plain_text" | "json" | "html";
|
||||||
|
|
||||||
|
interface IssueDocument {
|
||||||
|
id: string;
|
||||||
|
companyId: string;
|
||||||
|
issueId: string;
|
||||||
|
key: string;
|
||||||
|
title: string | null;
|
||||||
|
format: DocumentFormat;
|
||||||
|
body: string;
|
||||||
|
latestRevisionId: string;
|
||||||
|
latestRevisionNumber: number;
|
||||||
|
createdByAgentId: string | null;
|
||||||
|
createdByUserId: string | null;
|
||||||
|
updatedByAgentId: string | null;
|
||||||
|
updatedByUserId: string | null;
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Recommended `IssueDocumentSummary` shape:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
interface IssueDocumentSummary {
|
||||||
|
id: string;
|
||||||
|
key: string;
|
||||||
|
title: string | null;
|
||||||
|
format: DocumentFormat;
|
||||||
|
latestRevisionId: string;
|
||||||
|
latestRevisionNumber: number;
|
||||||
|
updatedAt: Date;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Issue type enrichment
|
||||||
|
|
||||||
|
Extend `Issue` with:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
interface Issue {
|
||||||
|
...
|
||||||
|
planDocument?: IssueDocument | null;
|
||||||
|
documentSummaries?: IssueDocumentSummary[];
|
||||||
|
legacyPlanDocument?: {
|
||||||
|
key: "plan";
|
||||||
|
body: string;
|
||||||
|
source: "issue_description";
|
||||||
|
} | null;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
This directly satisfies the `PAP-448` requirement for heartbeat/API issue fetches.
|
||||||
|
|
||||||
|
## API endpoints
|
||||||
|
|
||||||
|
Recommended endpoints:
|
||||||
|
|
||||||
|
- `GET /api/issues/:issueId/documents`
|
||||||
|
- `GET /api/issues/:issueId/documents/:key`
|
||||||
|
- `PUT /api/issues/:issueId/documents/:key`
|
||||||
|
- `GET /api/issues/:issueId/documents/:key/revisions`
|
||||||
|
- `DELETE /api/issues/:issueId/documents/:key` optionally board-only in v1
|
||||||
|
|
||||||
|
Recommended `PUT` body:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
{
|
||||||
|
title?: string | null;
|
||||||
|
format: "markdown" | "plain_text" | "json" | "html";
|
||||||
|
body: string;
|
||||||
|
changeSummary?: string | null;
|
||||||
|
baseRevisionId?: string | null;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
Behavior:
|
||||||
|
|
||||||
|
- missing document + no `baseRevisionId`: create
|
||||||
|
- existing document + matching `baseRevisionId`: update
|
||||||
|
- existing document + stale `baseRevisionId`: `409`
|
||||||
|
|
||||||
|
## Authorization and invariants
|
||||||
|
|
||||||
|
- all document records are company-scoped
|
||||||
|
- issue relation must belong to same company
|
||||||
|
- board access follows existing issue access rules
|
||||||
|
- agent access follows existing same-company issue access rules
|
||||||
|
- every mutation writes activity log entries
|
||||||
|
|
||||||
|
Recommended delete rule for v1:
|
||||||
|
|
||||||
|
- board can delete documents
|
||||||
|
- agents can create/update, but not delete
|
||||||
|
|
||||||
|
That keeps automated systems from removing canonical docs too easily.
|
||||||
|
|
||||||
|
## UI Plan
|
||||||
|
|
||||||
|
## Issue detail
|
||||||
|
|
||||||
|
Add a new **Documents** section directly under the issue description.
|
||||||
|
|
||||||
|
Recommended behavior:
|
||||||
|
|
||||||
|
- show `plan` first when present
|
||||||
|
- show other documents below it
|
||||||
|
- render a gist-like header:
|
||||||
|
- key
|
||||||
|
- title
|
||||||
|
- last updated metadata
|
||||||
|
- revision number
|
||||||
|
- support inline edit
|
||||||
|
- support create new document by key
|
||||||
|
- support revision history drawer or sheet
|
||||||
|
|
||||||
|
Recommended presentation order:
|
||||||
|
|
||||||
|
1. Description
|
||||||
|
2. Documents
|
||||||
|
3. Attachments
|
||||||
|
4. Comments / activity / sub-issues
|
||||||
|
|
||||||
|
This matches the request that documents live under the description while still leaving attachments available.
|
||||||
|
|
||||||
|
## Editing UX
|
||||||
|
|
||||||
|
Recommendation:
|
||||||
|
|
||||||
|
- use markdown preview + raw edit toggle for markdown docs
|
||||||
|
- use raw textarea editor for non-markdown docs in v1
|
||||||
|
- show explicit save conflicts on `409`
|
||||||
|
- show a clear empty state: "No documents yet"
|
||||||
|
|
||||||
|
## Legacy plan rendering
|
||||||
|
|
||||||
|
If there is no stored `plan` document but legacy `<plan>` exists:
|
||||||
|
|
||||||
|
- show it in the Documents section
|
||||||
|
- mark it `Legacy plan from description`
|
||||||
|
- offer create/import in a later pass
|
||||||
|
|
||||||
|
## Agent Protocol and Skills
|
||||||
|
|
||||||
|
Update the Paperclip agent workflow so planning no longer edits the issue description.
|
||||||
|
|
||||||
|
Required changes:
|
||||||
|
|
||||||
|
- update `skills/paperclip/SKILL.md`
|
||||||
|
- replace the `<plan>` instructions with document creation/update instructions
|
||||||
|
- document the new endpoints in `docs/api/issues.md`
|
||||||
|
- update any internal planning docs that still teach inline `<plan>` blocks
|
||||||
|
|
||||||
|
New rule:
|
||||||
|
|
||||||
|
- when asked to make a plan for an issue, create or update the issue document with key `plan`
|
||||||
|
- leave a comment that the plan document was created/updated
|
||||||
|
- do not mark the issue done
|
||||||
|
|
||||||
|
## Relationship to the Artifact Plan
|
||||||
|
|
||||||
|
This work should explicitly feed the broader artifact/deliverables direction.
|
||||||
|
|
||||||
|
Recommendation:
|
||||||
|
|
||||||
|
- keep documents as their own primitive in this change
|
||||||
|
- add `document` to any future `ArtifactKind`
|
||||||
|
- later build a deliverables read-model that aggregates:
|
||||||
|
- issue documents
|
||||||
|
- issue attachments
|
||||||
|
- preview URLs
|
||||||
|
- workspace-file references
|
||||||
|
|
||||||
|
The artifact proposal currently has no explicit `document` kind. It should.
|
||||||
|
|
||||||
|
Recommended future shape:
|
||||||
|
|
||||||
|
```ts
|
||||||
|
type ArtifactKind =
|
||||||
|
| "document"
|
||||||
|
| "attachment"
|
||||||
|
| "workspace_file"
|
||||||
|
| "preview"
|
||||||
|
| "report_link";
|
||||||
|
```
|
||||||
|
|
||||||
|
## Implementation Phases
|
||||||
|
|
||||||
|
## Phase 1: Shared contract and schema
|
||||||
|
|
||||||
|
Files:
|
||||||
|
|
||||||
|
- `packages/db/src/schema/documents.ts`
|
||||||
|
- `packages/db/src/schema/document_revisions.ts`
|
||||||
|
- `packages/db/src/schema/issue_documents.ts`
|
||||||
|
- `packages/db/src/schema/index.ts`
|
||||||
|
- `packages/db/src/migrations/*`
|
||||||
|
- `packages/shared/src/types/issue.ts`
|
||||||
|
- `packages/shared/src/validators/issue.ts` or new document validator file
|
||||||
|
- `packages/shared/src/index.ts`
|
||||||
|
|
||||||
|
Acceptance:
|
||||||
|
|
||||||
|
- schema enforces one key per issue
|
||||||
|
- revisions are append-only
|
||||||
|
- shared types expose plan/document fields on issue fetch
|
||||||
|
|
||||||
|
## Phase 2: Server services and routes
|
||||||
|
|
||||||
|
Files:
|
||||||
|
|
||||||
|
- `server/src/services/issues.ts` or `server/src/services/documents.ts`
|
||||||
|
- `server/src/routes/issues.ts`
|
||||||
|
- `server/src/services/activity.ts` callsites
|
||||||
|
|
||||||
|
Behavior:
|
||||||
|
|
||||||
|
- list/get/upsert/delete documents
|
||||||
|
- revision listing
|
||||||
|
- `GET /issues/:id` returns `planDocument` + `documentSummaries`
|
||||||
|
- company boundary checks match issue routes
|
||||||
|
|
||||||
|
Acceptance:
|
||||||
|
|
||||||
|
- agents and board can fetch/update same-company issue documents
|
||||||
|
- stale edits return `409`
|
||||||
|
- activity timeline shows document changes
|
||||||
|
|
||||||
|
## Phase 3: UI issue documents surface
|
||||||
|
|
||||||
|
Files:
|
||||||
|
|
||||||
|
- `ui/src/api/issues.ts`
|
||||||
|
- `ui/src/lib/queryKeys.ts`
|
||||||
|
- `ui/src/pages/IssueDetail.tsx`
|
||||||
|
- new reusable document UI component if needed
|
||||||
|
|
||||||
|
Behavior:
|
||||||
|
|
||||||
|
- render plan + documents under description
|
||||||
|
- create/update by key
|
||||||
|
- open revision history
|
||||||
|
- show conflicts/errors clearly
|
||||||
|
|
||||||
|
Acceptance:
|
||||||
|
|
||||||
|
- board can create a `plan` doc from issue detail
|
||||||
|
- updated plan appears immediately
|
||||||
|
- issue detail no longer depends on description-embedded `<plan>`
|
||||||
|
|
||||||
|
## Phase 4: Skills/docs migration
|
||||||
|
|
||||||
|
Files:
|
||||||
|
|
||||||
|
- `skills/paperclip/SKILL.md`
|
||||||
|
- `docs/api/issues.md`
|
||||||
|
- `doc/SPEC-implementation.md`
|
||||||
|
- relevant plan/docs that mention `<plan>`
|
||||||
|
|
||||||
|
Acceptance:
|
||||||
|
|
||||||
|
- planning guidance references issue documents, not inline issue description tags
|
||||||
|
- API docs describe the new document endpoints and issue payload additions
|
||||||
|
|
||||||
|
## Phase 5: Legacy compatibility and follow-up
|
||||||
|
|
||||||
|
Behavior:
|
||||||
|
|
||||||
|
- read legacy `<plan>` blocks as fallback
|
||||||
|
- optionally add explicit import/migration command later
|
||||||
|
|
||||||
|
Follow-up, not required for first merge:
|
||||||
|
|
||||||
|
- deliverables/artifact read-model
|
||||||
|
- project/company documents
|
||||||
|
- comment-linked documents
|
||||||
|
- diff view between revisions
|
||||||
|
|
||||||
|
## Test Plan
|
||||||
|
|
||||||
|
### Server
|
||||||
|
|
||||||
|
- document create/read/update/delete lifecycle
|
||||||
|
- revision numbering
|
||||||
|
- `baseRevisionId` conflict handling
|
||||||
|
- company boundary enforcement
|
||||||
|
- agent vs board authorization
|
||||||
|
- issue fetch includes `planDocument` and document summaries
|
||||||
|
- legacy `<plan>` fallback behavior
|
||||||
|
- activity log mutation coverage
|
||||||
|
|
||||||
|
### UI
|
||||||
|
|
||||||
|
- issue detail shows plan document
|
||||||
|
- create/update flows invalidate queries correctly
|
||||||
|
- conflict and validation errors are surfaced
|
||||||
|
- legacy plan fallback renders correctly
|
||||||
|
|
||||||
|
### Verification
|
||||||
|
|
||||||
|
Run before implementation is declared complete:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
pnpm -r typecheck
|
||||||
|
pnpm test:run
|
||||||
|
pnpm build
|
||||||
|
```
|
||||||
|
|
||||||
|
## Open Questions
|
||||||
|
|
||||||
|
1. Should v1 documents be markdown-only, with `json/html/plain_text` deferred?
|
||||||
|
Recommendation: allow all four in API, optimize UI for markdown only.
|
||||||
|
|
||||||
|
2. Should agents be allowed to create arbitrary keys, or only conventional keys?
|
||||||
|
Recommendation: allow arbitrary keys with normalized validation; reserve `plan` as special behavior only.
|
||||||
|
|
||||||
|
3. Should delete exist in v1?
|
||||||
|
Recommendation: yes, but board-only.
|
||||||
|
|
||||||
|
4. Should legacy `<plan>` blocks ever be auto-migrated?
|
||||||
|
Recommendation: no automatic mutation in the first rollout.
|
||||||
|
|
||||||
|
5. Should documents appear inside a future Deliverables section or remain a top-level Issue section?
|
||||||
|
Recommendation: keep a dedicated Documents section now; later also expose them in Deliverables if an aggregated artifact view is added.
|
||||||
|
|
||||||
|
## Final Recommendation
|
||||||
|
|
||||||
|
Ship **issue documents** as a focused, text-first primitive now.
|
||||||
|
|
||||||
|
Do not try to solve full artifact unification in the same implementation.
|
||||||
|
|
||||||
|
Use:
|
||||||
|
|
||||||
|
- first-class document tables
|
||||||
|
- issue-level keyed linkage
|
||||||
|
- append-only revisions
|
||||||
|
- `planDocument` embedded in normal issue fetches
|
||||||
|
- legacy `<plan>` fallback
|
||||||
|
- skill/docs migration away from description-embedded plans
|
||||||
|
|
||||||
|
This addresses the real planning workflow problem immediately and leaves the artifact system room to grow cleanly afterward.
|
||||||
@@ -32,6 +32,23 @@ export const runningProcesses = new Map<string, RunningProcess>();
|
|||||||
export const MAX_CAPTURE_BYTES = 4 * 1024 * 1024;
|
export const MAX_CAPTURE_BYTES = 4 * 1024 * 1024;
|
||||||
export const MAX_EXCERPT_BYTES = 32 * 1024;
|
export const MAX_EXCERPT_BYTES = 32 * 1024;
|
||||||
const SENSITIVE_ENV_KEY = /(key|token|secret|password|passwd|authorization|cookie)/i;
|
const SENSITIVE_ENV_KEY = /(key|token|secret|password|passwd|authorization|cookie)/i;
|
||||||
|
const PAPERCLIP_SKILL_ROOT_RELATIVE_CANDIDATES = [
|
||||||
|
"../../skills",
|
||||||
|
"../../../../../skills",
|
||||||
|
];
|
||||||
|
|
||||||
|
export interface PaperclipSkillEntry {
|
||||||
|
name: string;
|
||||||
|
source: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function normalizePathSlashes(value: string): string {
|
||||||
|
return value.replaceAll("\\", "/");
|
||||||
|
}
|
||||||
|
|
||||||
|
function isMaintainerOnlySkillTarget(candidate: string): boolean {
|
||||||
|
return normalizePathSlashes(candidate).includes("/.agents/skills/");
|
||||||
|
}
|
||||||
|
|
||||||
export function parseObject(value: unknown): Record<string, unknown> {
|
export function parseObject(value: unknown): Record<string, unknown> {
|
||||||
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
||||||
@@ -95,6 +112,16 @@ export function renderTemplate(template: string, data: Record<string, unknown>)
|
|||||||
return template.replace(/{{\s*([a-zA-Z0-9_.-]+)\s*}}/g, (_, path) => resolvePathValue(data, path));
|
return template.replace(/{{\s*([a-zA-Z0-9_.-]+)\s*}}/g, (_, path) => resolvePathValue(data, path));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function joinPromptSections(
|
||||||
|
sections: Array<string | null | undefined>,
|
||||||
|
separator = "\n\n",
|
||||||
|
) {
|
||||||
|
return sections
|
||||||
|
.map((value) => (typeof value === "string" ? value.trim() : ""))
|
||||||
|
.filter(Boolean)
|
||||||
|
.join(separator);
|
||||||
|
}
|
||||||
|
|
||||||
export function redactEnvForLogs(env: Record<string, string>): Record<string, string> {
|
export function redactEnvForLogs(env: Record<string, string>): Record<string, string> {
|
||||||
const redacted: Record<string, string> = {};
|
const redacted: Record<string, string> = {};
|
||||||
for (const [key, value] of Object.entries(env)) {
|
for (const [key, value] of Object.entries(env)) {
|
||||||
@@ -245,6 +272,136 @@ export async function ensureAbsoluteDirectory(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function resolvePaperclipSkillsDir(
|
||||||
|
moduleDir: string,
|
||||||
|
additionalCandidates: string[] = [],
|
||||||
|
): Promise<string | null> {
|
||||||
|
const candidates = [
|
||||||
|
...PAPERCLIP_SKILL_ROOT_RELATIVE_CANDIDATES.map((relativePath) => path.resolve(moduleDir, relativePath)),
|
||||||
|
...additionalCandidates.map((candidate) => path.resolve(candidate)),
|
||||||
|
];
|
||||||
|
const seenRoots = new Set<string>();
|
||||||
|
|
||||||
|
for (const root of candidates) {
|
||||||
|
if (seenRoots.has(root)) continue;
|
||||||
|
seenRoots.add(root);
|
||||||
|
const isDirectory = await fs.stat(root).then((stats) => stats.isDirectory()).catch(() => false);
|
||||||
|
if (isDirectory) return root;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function listPaperclipSkillEntries(
|
||||||
|
moduleDir: string,
|
||||||
|
additionalCandidates: string[] = [],
|
||||||
|
): Promise<PaperclipSkillEntry[]> {
|
||||||
|
const root = await resolvePaperclipSkillsDir(moduleDir, additionalCandidates);
|
||||||
|
if (!root) return [];
|
||||||
|
|
||||||
|
try {
|
||||||
|
const entries = await fs.readdir(root, { withFileTypes: true });
|
||||||
|
return entries
|
||||||
|
.filter((entry) => entry.isDirectory())
|
||||||
|
.map((entry) => ({
|
||||||
|
name: entry.name,
|
||||||
|
source: path.join(root, entry.name),
|
||||||
|
}));
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function readPaperclipSkillMarkdown(
|
||||||
|
moduleDir: string,
|
||||||
|
skillName: string,
|
||||||
|
): Promise<string | null> {
|
||||||
|
const normalized = skillName.trim().toLowerCase();
|
||||||
|
if (!normalized) return null;
|
||||||
|
|
||||||
|
const entries = await listPaperclipSkillEntries(moduleDir);
|
||||||
|
const match = entries.find((entry) => entry.name === normalized);
|
||||||
|
if (!match) return null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await fs.readFile(path.join(match.source, "SKILL.md"), "utf8");
|
||||||
|
} catch {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function ensurePaperclipSkillSymlink(
|
||||||
|
source: string,
|
||||||
|
target: string,
|
||||||
|
linkSkill: (source: string, target: string) => Promise<void> = (linkSource, linkTarget) =>
|
||||||
|
fs.symlink(linkSource, linkTarget),
|
||||||
|
): Promise<"created" | "repaired" | "skipped"> {
|
||||||
|
const existing = await fs.lstat(target).catch(() => null);
|
||||||
|
if (!existing) {
|
||||||
|
await linkSkill(source, target);
|
||||||
|
return "created";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!existing.isSymbolicLink()) {
|
||||||
|
return "skipped";
|
||||||
|
}
|
||||||
|
|
||||||
|
const linkedPath = await fs.readlink(target).catch(() => null);
|
||||||
|
if (!linkedPath) return "skipped";
|
||||||
|
|
||||||
|
const resolvedLinkedPath = path.resolve(path.dirname(target), linkedPath);
|
||||||
|
if (resolvedLinkedPath === source) {
|
||||||
|
return "skipped";
|
||||||
|
}
|
||||||
|
|
||||||
|
const linkedPathExists = await fs.stat(resolvedLinkedPath).then(() => true).catch(() => false);
|
||||||
|
if (linkedPathExists) {
|
||||||
|
return "skipped";
|
||||||
|
}
|
||||||
|
|
||||||
|
await fs.unlink(target);
|
||||||
|
await linkSkill(source, target);
|
||||||
|
return "repaired";
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function removeMaintainerOnlySkillSymlinks(
|
||||||
|
skillsHome: string,
|
||||||
|
allowedSkillNames: Iterable<string>,
|
||||||
|
): Promise<string[]> {
|
||||||
|
const allowed = new Set(Array.from(allowedSkillNames));
|
||||||
|
try {
|
||||||
|
const entries = await fs.readdir(skillsHome, { withFileTypes: true });
|
||||||
|
const removed: string[] = [];
|
||||||
|
for (const entry of entries) {
|
||||||
|
if (allowed.has(entry.name)) continue;
|
||||||
|
|
||||||
|
const target = path.join(skillsHome, entry.name);
|
||||||
|
const existing = await fs.lstat(target).catch(() => null);
|
||||||
|
if (!existing?.isSymbolicLink()) continue;
|
||||||
|
|
||||||
|
const linkedPath = await fs.readlink(target).catch(() => null);
|
||||||
|
if (!linkedPath) continue;
|
||||||
|
|
||||||
|
const resolvedLinkedPath = path.isAbsolute(linkedPath)
|
||||||
|
? linkedPath
|
||||||
|
: path.resolve(path.dirname(target), linkedPath);
|
||||||
|
if (
|
||||||
|
!isMaintainerOnlySkillTarget(linkedPath) &&
|
||||||
|
!isMaintainerOnlySkillTarget(resolvedLinkedPath)
|
||||||
|
) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
await fs.unlink(target);
|
||||||
|
removed.push(entry.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
return removed;
|
||||||
|
} catch {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function ensureCommandResolvable(command: string, cwd: string, env: NodeJS.ProcessEnv) {
|
export async function ensureCommandResolvable(command: string, cwd: string, env: NodeJS.ProcessEnv) {
|
||||||
const resolved = await resolveCommandPath(command, cwd, env);
|
const resolved = await resolveCommandPath(command, cwd, env);
|
||||||
if (resolved) return;
|
if (resolved) return;
|
||||||
|
|||||||
@@ -99,6 +99,7 @@ export interface AdapterInvocationMeta {
|
|||||||
commandNotes?: string[];
|
commandNotes?: string[];
|
||||||
env?: Record<string, string>;
|
env?: Record<string, string>;
|
||||||
prompt?: string;
|
prompt?: string;
|
||||||
|
promptMetrics?: Record<string, number>;
|
||||||
context?: Record<string, unknown>;
|
context?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import {
|
|||||||
parseObject,
|
parseObject,
|
||||||
parseJson,
|
parseJson,
|
||||||
buildPaperclipEnv,
|
buildPaperclipEnv,
|
||||||
|
joinPromptSections,
|
||||||
redactEnvForLogs,
|
redactEnvForLogs,
|
||||||
ensureAbsoluteDirectory,
|
ensureAbsoluteDirectory,
|
||||||
ensureCommandResolvable,
|
ensureCommandResolvable,
|
||||||
@@ -363,7 +364,8 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
`[paperclip] Claude session "${runtimeSessionId}" was saved for cwd "${runtimeSessionCwd}" and will not be resumed in "${cwd}".\n`,
|
`[paperclip] Claude session "${runtimeSessionId}" was saved for cwd "${runtimeSessionCwd}" and will not be resumed in "${cwd}".\n`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
const prompt = renderTemplate(promptTemplate, {
|
const bootstrapPromptTemplate = asString(config.bootstrapPromptTemplate, "");
|
||||||
|
const templateData = {
|
||||||
agentId: agent.id,
|
agentId: agent.id,
|
||||||
companyId: agent.companyId,
|
companyId: agent.companyId,
|
||||||
runId,
|
runId,
|
||||||
@@ -371,7 +373,24 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
agent,
|
agent,
|
||||||
run: { id: runId, source: "on_demand" },
|
run: { id: runId, source: "on_demand" },
|
||||||
context,
|
context,
|
||||||
});
|
};
|
||||||
|
const renderedPrompt = renderTemplate(promptTemplate, templateData);
|
||||||
|
const renderedBootstrapPrompt =
|
||||||
|
!sessionId && bootstrapPromptTemplate.trim().length > 0
|
||||||
|
? renderTemplate(bootstrapPromptTemplate, templateData).trim()
|
||||||
|
: "";
|
||||||
|
const sessionHandoffNote = asString(context.paperclipSessionHandoffMarkdown, "").trim();
|
||||||
|
const prompt = joinPromptSections([
|
||||||
|
renderedBootstrapPrompt,
|
||||||
|
sessionHandoffNote,
|
||||||
|
renderedPrompt,
|
||||||
|
]);
|
||||||
|
const promptMetrics = {
|
||||||
|
promptChars: prompt.length,
|
||||||
|
bootstrapPromptChars: renderedBootstrapPrompt.length,
|
||||||
|
sessionHandoffChars: sessionHandoffNote.length,
|
||||||
|
heartbeatPromptChars: renderedPrompt.length,
|
||||||
|
};
|
||||||
|
|
||||||
const buildClaudeArgs = (resumeSessionId: string | null) => {
|
const buildClaudeArgs = (resumeSessionId: string | null) => {
|
||||||
const args = ["--print", "-", "--output-format", "stream-json", "--verbose"];
|
const args = ["--print", "-", "--output-format", "stream-json", "--verbose"];
|
||||||
@@ -416,6 +435,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
commandNotes,
|
commandNotes,
|
||||||
env: redactEnvForLogs(env),
|
env: redactEnvForLogs(env),
|
||||||
prompt,
|
prompt,
|
||||||
|
promptMetrics,
|
||||||
context,
|
context,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ export function buildClaudeLocalConfig(v: CreateConfigValues): Record<string, un
|
|||||||
if (v.cwd) ac.cwd = v.cwd;
|
if (v.cwd) ac.cwd = v.cwd;
|
||||||
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
||||||
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
||||||
|
if (v.bootstrapPrompt) ac.bootstrapPromptTemplate = v.bootstrapPrompt;
|
||||||
if (v.model) ac.model = v.model;
|
if (v.model) ac.model = v.model;
|
||||||
if (v.thinkingEffort) ac.effort = v.thinkingEffort;
|
if (v.thinkingEffort) ac.effort = v.thinkingEffort;
|
||||||
if (v.chrome) ac.chrome = true;
|
if (v.chrome) ac.chrome = true;
|
||||||
|
|||||||
101
packages/adapters/codex-local/src/server/codex-home.ts
Normal file
101
packages/adapters/codex-local/src/server/codex-home.ts
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
import fs from "node:fs/promises";
|
||||||
|
import os from "node:os";
|
||||||
|
import path from "node:path";
|
||||||
|
import type { AdapterExecutionContext } from "@paperclipai/adapter-utils";
|
||||||
|
|
||||||
|
const TRUTHY_ENV_RE = /^(1|true|yes|on)$/i;
|
||||||
|
const COPIED_SHARED_FILES = ["config.json", "config.toml", "instructions.md"] as const;
|
||||||
|
const SYMLINKED_SHARED_FILES = ["auth.json"] as const;
|
||||||
|
|
||||||
|
function nonEmpty(value: string | undefined): string | null {
|
||||||
|
return typeof value === "string" && value.trim().length > 0 ? value.trim() : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function pathExists(candidate: string): Promise<boolean> {
|
||||||
|
return fs.access(candidate).then(() => true).catch(() => false);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function resolveCodexHomeDir(env: NodeJS.ProcessEnv = process.env): string {
|
||||||
|
const fromEnv = nonEmpty(env.CODEX_HOME);
|
||||||
|
if (fromEnv) return path.resolve(fromEnv);
|
||||||
|
return path.join(os.homedir(), ".codex");
|
||||||
|
}
|
||||||
|
|
||||||
|
function isWorktreeMode(env: NodeJS.ProcessEnv): boolean {
|
||||||
|
return TRUTHY_ENV_RE.test(env.PAPERCLIP_IN_WORKTREE ?? "");
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveWorktreeCodexHomeDir(env: NodeJS.ProcessEnv): string | null {
|
||||||
|
if (!isWorktreeMode(env)) return null;
|
||||||
|
const paperclipHome = nonEmpty(env.PAPERCLIP_HOME);
|
||||||
|
if (!paperclipHome) return null;
|
||||||
|
const instanceId = nonEmpty(env.PAPERCLIP_INSTANCE_ID);
|
||||||
|
if (instanceId) {
|
||||||
|
return path.resolve(paperclipHome, "instances", instanceId, "codex-home");
|
||||||
|
}
|
||||||
|
return path.resolve(paperclipHome, "codex-home");
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ensureParentDir(target: string): Promise<void> {
|
||||||
|
await fs.mkdir(path.dirname(target), { recursive: true });
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ensureSymlink(target: string, source: string): Promise<void> {
|
||||||
|
const existing = await fs.lstat(target).catch(() => null);
|
||||||
|
if (!existing) {
|
||||||
|
await ensureParentDir(target);
|
||||||
|
await fs.symlink(source, target);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!existing.isSymbolicLink()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const linkedPath = await fs.readlink(target).catch(() => null);
|
||||||
|
if (!linkedPath) return;
|
||||||
|
|
||||||
|
const resolvedLinkedPath = path.resolve(path.dirname(target), linkedPath);
|
||||||
|
if (resolvedLinkedPath === source) return;
|
||||||
|
|
||||||
|
await fs.unlink(target);
|
||||||
|
await fs.symlink(source, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function ensureCopiedFile(target: string, source: string): Promise<void> {
|
||||||
|
const existing = await fs.lstat(target).catch(() => null);
|
||||||
|
if (existing) return;
|
||||||
|
await ensureParentDir(target);
|
||||||
|
await fs.copyFile(source, target);
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function prepareWorktreeCodexHome(
|
||||||
|
env: NodeJS.ProcessEnv,
|
||||||
|
onLog: AdapterExecutionContext["onLog"],
|
||||||
|
): Promise<string | null> {
|
||||||
|
const targetHome = resolveWorktreeCodexHomeDir(env);
|
||||||
|
if (!targetHome) return null;
|
||||||
|
|
||||||
|
const sourceHome = resolveCodexHomeDir(env);
|
||||||
|
if (path.resolve(sourceHome) === path.resolve(targetHome)) return targetHome;
|
||||||
|
|
||||||
|
await fs.mkdir(targetHome, { recursive: true });
|
||||||
|
|
||||||
|
for (const name of SYMLINKED_SHARED_FILES) {
|
||||||
|
const source = path.join(sourceHome, name);
|
||||||
|
if (!(await pathExists(source))) continue;
|
||||||
|
await ensureSymlink(path.join(targetHome, name), source);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const name of COPIED_SHARED_FILES) {
|
||||||
|
const source = path.join(sourceHome, name);
|
||||||
|
if (!(await pathExists(source))) continue;
|
||||||
|
await ensureCopiedFile(path.join(targetHome, name), source);
|
||||||
|
}
|
||||||
|
|
||||||
|
await onLog(
|
||||||
|
"stderr",
|
||||||
|
`[paperclip] Using worktree-isolated Codex home "${targetHome}" (seeded from "${sourceHome}").\n`,
|
||||||
|
);
|
||||||
|
return targetHome;
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import os from "node:os";
|
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
import type { AdapterExecutionContext, AdapterExecutionResult } from "@paperclipai/adapter-utils";
|
import type { AdapterExecutionContext, AdapterExecutionResult } from "@paperclipai/adapter-utils";
|
||||||
@@ -13,17 +12,18 @@ import {
|
|||||||
redactEnvForLogs,
|
redactEnvForLogs,
|
||||||
ensureAbsoluteDirectory,
|
ensureAbsoluteDirectory,
|
||||||
ensureCommandResolvable,
|
ensureCommandResolvable,
|
||||||
|
ensurePaperclipSkillSymlink,
|
||||||
ensurePathInEnv,
|
ensurePathInEnv,
|
||||||
|
listPaperclipSkillEntries,
|
||||||
|
removeMaintainerOnlySkillSymlinks,
|
||||||
renderTemplate,
|
renderTemplate,
|
||||||
|
joinPromptSections,
|
||||||
runChildProcess,
|
runChildProcess,
|
||||||
} from "@paperclipai/adapter-utils/server-utils";
|
} from "@paperclipai/adapter-utils/server-utils";
|
||||||
import { parseCodexJsonl, isCodexUnknownSessionError } from "./parse.js";
|
import { parseCodexJsonl, isCodexUnknownSessionError } from "./parse.js";
|
||||||
|
import { pathExists, prepareWorktreeCodexHome, resolveCodexHomeDir } from "./codex-home.js";
|
||||||
|
|
||||||
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
||||||
const PAPERCLIP_SKILLS_CANDIDATES = [
|
|
||||||
path.resolve(__moduleDir, "../../skills"), // published: <pkg>/dist/server/ -> <pkg>/skills/
|
|
||||||
path.resolve(__moduleDir, "../../../../../skills"), // dev: src/server/ -> repo root/skills/
|
|
||||||
];
|
|
||||||
const CODEX_ROLLOUT_NOISE_RE =
|
const CODEX_ROLLOUT_NOISE_RE =
|
||||||
/^\d{4}-\d{2}-\d{2}T[^\s]+\s+ERROR\s+codex_core::rollout::list:\s+state db missing rollout path for thread\s+[a-z0-9-]+$/i;
|
/^\d{4}-\d{2}-\d{2}T[^\s]+\s+ERROR\s+codex_core::rollout::list:\s+state db missing rollout path for thread\s+[a-z0-9-]+$/i;
|
||||||
|
|
||||||
@@ -61,39 +61,95 @@ function resolveCodexBillingType(env: Record<string, string>): "api" | "subscrip
|
|||||||
return hasNonEmptyEnvValue(env, "OPENAI_API_KEY") ? "api" : "subscription";
|
return hasNonEmptyEnvValue(env, "OPENAI_API_KEY") ? "api" : "subscription";
|
||||||
}
|
}
|
||||||
|
|
||||||
function codexHomeDir(): string {
|
async function isLikelyPaperclipRepoRoot(candidate: string): Promise<boolean> {
|
||||||
const fromEnv = process.env.CODEX_HOME;
|
const [hasWorkspace, hasPackageJson, hasServerDir, hasAdapterUtilsDir] = await Promise.all([
|
||||||
if (typeof fromEnv === "string" && fromEnv.trim().length > 0) return fromEnv.trim();
|
pathExists(path.join(candidate, "pnpm-workspace.yaml")),
|
||||||
return path.join(os.homedir(), ".codex");
|
pathExists(path.join(candidate, "package.json")),
|
||||||
|
pathExists(path.join(candidate, "server")),
|
||||||
|
pathExists(path.join(candidate, "packages", "adapter-utils")),
|
||||||
|
]);
|
||||||
|
|
||||||
|
return hasWorkspace && hasPackageJson && hasServerDir && hasAdapterUtilsDir;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolvePaperclipSkillsDir(): Promise<string | null> {
|
async function isLikelyPaperclipRuntimeSkillSource(candidate: string, skillName: string): Promise<boolean> {
|
||||||
for (const candidate of PAPERCLIP_SKILLS_CANDIDATES) {
|
if (path.basename(candidate) !== skillName) return false;
|
||||||
const isDir = await fs.stat(candidate).then((s) => s.isDirectory()).catch(() => false);
|
const skillsRoot = path.dirname(candidate);
|
||||||
if (isDir) return candidate;
|
if (path.basename(skillsRoot) !== "skills") return false;
|
||||||
|
if (!(await pathExists(path.join(candidate, "SKILL.md")))) return false;
|
||||||
|
|
||||||
|
let cursor = path.dirname(skillsRoot);
|
||||||
|
for (let depth = 0; depth < 6; depth += 1) {
|
||||||
|
if (await isLikelyPaperclipRepoRoot(cursor)) return true;
|
||||||
|
const parent = path.dirname(cursor);
|
||||||
|
if (parent === cursor) break;
|
||||||
|
cursor = parent;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function ensureCodexSkillsInjected(onLog: AdapterExecutionContext["onLog"]) {
|
type EnsureCodexSkillsInjectedOptions = {
|
||||||
const skillsDir = await resolvePaperclipSkillsDir();
|
skillsHome?: string;
|
||||||
if (!skillsDir) return;
|
skillsEntries?: Awaited<ReturnType<typeof listPaperclipSkillEntries>>;
|
||||||
|
linkSkill?: (source: string, target: string) => Promise<void>;
|
||||||
|
};
|
||||||
|
|
||||||
const skillsHome = path.join(codexHomeDir(), "skills");
|
export async function ensureCodexSkillsInjected(
|
||||||
|
onLog: AdapterExecutionContext["onLog"],
|
||||||
|
options: EnsureCodexSkillsInjectedOptions = {},
|
||||||
|
) {
|
||||||
|
const skillsEntries = options.skillsEntries ?? await listPaperclipSkillEntries(__moduleDir);
|
||||||
|
if (skillsEntries.length === 0) return;
|
||||||
|
|
||||||
|
const skillsHome = options.skillsHome ?? path.join(resolveCodexHomeDir(process.env), "skills");
|
||||||
await fs.mkdir(skillsHome, { recursive: true });
|
await fs.mkdir(skillsHome, { recursive: true });
|
||||||
const entries = await fs.readdir(skillsDir, { withFileTypes: true });
|
const removedSkills = await removeMaintainerOnlySkillSymlinks(
|
||||||
for (const entry of entries) {
|
skillsHome,
|
||||||
if (!entry.isDirectory()) continue;
|
skillsEntries.map((entry) => entry.name),
|
||||||
const source = path.join(skillsDir, entry.name);
|
);
|
||||||
|
for (const skillName of removedSkills) {
|
||||||
|
await onLog(
|
||||||
|
"stderr",
|
||||||
|
`[paperclip] Removed maintainer-only Codex skill "${skillName}" from ${skillsHome}\n`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
const linkSkill = options.linkSkill;
|
||||||
|
for (const entry of skillsEntries) {
|
||||||
const target = path.join(skillsHome, entry.name);
|
const target = path.join(skillsHome, entry.name);
|
||||||
const existing = await fs.lstat(target).catch(() => null);
|
|
||||||
if (existing) continue;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fs.symlink(source, target);
|
const existing = await fs.lstat(target).catch(() => null);
|
||||||
|
if (existing?.isSymbolicLink()) {
|
||||||
|
const linkedPath = await fs.readlink(target).catch(() => null);
|
||||||
|
const resolvedLinkedPath = linkedPath
|
||||||
|
? path.resolve(path.dirname(target), linkedPath)
|
||||||
|
: null;
|
||||||
|
if (
|
||||||
|
resolvedLinkedPath &&
|
||||||
|
resolvedLinkedPath !== entry.source &&
|
||||||
|
(await isLikelyPaperclipRuntimeSkillSource(resolvedLinkedPath, entry.name))
|
||||||
|
) {
|
||||||
|
await fs.unlink(target);
|
||||||
|
if (linkSkill) {
|
||||||
|
await linkSkill(entry.source, target);
|
||||||
|
} else {
|
||||||
|
await fs.symlink(entry.source, target);
|
||||||
|
}
|
||||||
|
await onLog(
|
||||||
|
"stderr",
|
||||||
|
`[paperclip] Repaired Codex skill "${entry.name}" into ${skillsHome}\n`,
|
||||||
|
);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await ensurePaperclipSkillSymlink(entry.source, target, linkSkill);
|
||||||
|
if (result === "skipped") continue;
|
||||||
|
|
||||||
await onLog(
|
await onLog(
|
||||||
"stderr",
|
"stderr",
|
||||||
`[paperclip] Injected Codex skill "${entry.name}" into ${skillsHome}\n`,
|
`[paperclip] ${result === "repaired" ? "Repaired" : "Injected"} Codex skill "${entry.name}" into ${skillsHome}\n`,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await onLog(
|
await onLog(
|
||||||
@@ -152,12 +208,25 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
const useConfiguredInsteadOfAgentHome = workspaceSource === "agent_home" && configuredCwd.length > 0;
|
const useConfiguredInsteadOfAgentHome = workspaceSource === "agent_home" && configuredCwd.length > 0;
|
||||||
const effectiveWorkspaceCwd = useConfiguredInsteadOfAgentHome ? "" : workspaceCwd;
|
const effectiveWorkspaceCwd = useConfiguredInsteadOfAgentHome ? "" : workspaceCwd;
|
||||||
const cwd = effectiveWorkspaceCwd || configuredCwd || process.cwd();
|
const cwd = effectiveWorkspaceCwd || configuredCwd || process.cwd();
|
||||||
await ensureAbsoluteDirectory(cwd, { createIfMissing: true });
|
|
||||||
await ensureCodexSkillsInjected(onLog);
|
|
||||||
const envConfig = parseObject(config.env);
|
const envConfig = parseObject(config.env);
|
||||||
|
const configuredCodexHome =
|
||||||
|
typeof envConfig.CODEX_HOME === "string" && envConfig.CODEX_HOME.trim().length > 0
|
||||||
|
? path.resolve(envConfig.CODEX_HOME.trim())
|
||||||
|
: null;
|
||||||
|
await ensureAbsoluteDirectory(cwd, { createIfMissing: true });
|
||||||
|
const preparedWorktreeCodexHome =
|
||||||
|
configuredCodexHome ? null : await prepareWorktreeCodexHome(process.env, onLog);
|
||||||
|
const effectiveCodexHome = configuredCodexHome ?? preparedWorktreeCodexHome;
|
||||||
|
await ensureCodexSkillsInjected(
|
||||||
|
onLog,
|
||||||
|
effectiveCodexHome ? { skillsHome: path.join(effectiveCodexHome, "skills") } : {},
|
||||||
|
);
|
||||||
const hasExplicitApiKey =
|
const hasExplicitApiKey =
|
||||||
typeof envConfig.PAPERCLIP_API_KEY === "string" && envConfig.PAPERCLIP_API_KEY.trim().length > 0;
|
typeof envConfig.PAPERCLIP_API_KEY === "string" && envConfig.PAPERCLIP_API_KEY.trim().length > 0;
|
||||||
const env: Record<string, string> = { ...buildPaperclipEnv(agent) };
|
const env: Record<string, string> = { ...buildPaperclipEnv(agent) };
|
||||||
|
if (effectiveCodexHome) {
|
||||||
|
env.CODEX_HOME = effectiveCodexHome;
|
||||||
|
}
|
||||||
env.PAPERCLIP_RUN_ID = runId;
|
env.PAPERCLIP_RUN_ID = runId;
|
||||||
const wakeTaskId =
|
const wakeTaskId =
|
||||||
(typeof context.taskId === "string" && context.taskId.trim().length > 0 && context.taskId.trim()) ||
|
(typeof context.taskId === "string" && context.taskId.trim().length > 0 && context.taskId.trim()) ||
|
||||||
@@ -270,6 +339,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
const instructionsFilePath = asString(config.instructionsFilePath, "").trim();
|
const instructionsFilePath = asString(config.instructionsFilePath, "").trim();
|
||||||
const instructionsDir = instructionsFilePath ? `${path.dirname(instructionsFilePath)}/` : "";
|
const instructionsDir = instructionsFilePath ? `${path.dirname(instructionsFilePath)}/` : "";
|
||||||
let instructionsPrefix = "";
|
let instructionsPrefix = "";
|
||||||
|
let instructionsChars = 0;
|
||||||
if (instructionsFilePath) {
|
if (instructionsFilePath) {
|
||||||
try {
|
try {
|
||||||
const instructionsContents = await fs.readFile(instructionsFilePath, "utf8");
|
const instructionsContents = await fs.readFile(instructionsFilePath, "utf8");
|
||||||
@@ -277,6 +347,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
`${instructionsContents}\n\n` +
|
`${instructionsContents}\n\n` +
|
||||||
`The above agent instructions were loaded from ${instructionsFilePath}. ` +
|
`The above agent instructions were loaded from ${instructionsFilePath}. ` +
|
||||||
`Resolve any relative file references from ${instructionsDir}.\n\n`;
|
`Resolve any relative file references from ${instructionsDir}.\n\n`;
|
||||||
|
instructionsChars = instructionsPrefix.length;
|
||||||
await onLog(
|
await onLog(
|
||||||
"stderr",
|
"stderr",
|
||||||
`[paperclip] Loaded agent instructions file: ${instructionsFilePath}\n`,
|
`[paperclip] Loaded agent instructions file: ${instructionsFilePath}\n`,
|
||||||
@@ -301,7 +372,8 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
`Configured instructionsFilePath ${instructionsFilePath}, but file could not be read; continuing without injected instructions.`,
|
`Configured instructionsFilePath ${instructionsFilePath}, but file could not be read; continuing without injected instructions.`,
|
||||||
];
|
];
|
||||||
})();
|
})();
|
||||||
const renderedPrompt = renderTemplate(promptTemplate, {
|
const bootstrapPromptTemplate = asString(config.bootstrapPromptTemplate, "");
|
||||||
|
const templateData = {
|
||||||
agentId: agent.id,
|
agentId: agent.id,
|
||||||
companyId: agent.companyId,
|
companyId: agent.companyId,
|
||||||
runId,
|
runId,
|
||||||
@@ -309,8 +381,26 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
agent,
|
agent,
|
||||||
run: { id: runId, source: "on_demand" },
|
run: { id: runId, source: "on_demand" },
|
||||||
context,
|
context,
|
||||||
});
|
};
|
||||||
const prompt = `${instructionsPrefix}${renderedPrompt}`;
|
const renderedPrompt = renderTemplate(promptTemplate, templateData);
|
||||||
|
const renderedBootstrapPrompt =
|
||||||
|
!sessionId && bootstrapPromptTemplate.trim().length > 0
|
||||||
|
? renderTemplate(bootstrapPromptTemplate, templateData).trim()
|
||||||
|
: "";
|
||||||
|
const sessionHandoffNote = asString(context.paperclipSessionHandoffMarkdown, "").trim();
|
||||||
|
const prompt = joinPromptSections([
|
||||||
|
instructionsPrefix,
|
||||||
|
renderedBootstrapPrompt,
|
||||||
|
sessionHandoffNote,
|
||||||
|
renderedPrompt,
|
||||||
|
]);
|
||||||
|
const promptMetrics = {
|
||||||
|
promptChars: prompt.length,
|
||||||
|
instructionsChars,
|
||||||
|
bootstrapPromptChars: renderedBootstrapPrompt.length,
|
||||||
|
sessionHandoffChars: sessionHandoffNote.length,
|
||||||
|
heartbeatPromptChars: renderedPrompt.length,
|
||||||
|
};
|
||||||
|
|
||||||
const buildArgs = (resumeSessionId: string | null) => {
|
const buildArgs = (resumeSessionId: string | null) => {
|
||||||
const args = ["exec", "--json"];
|
const args = ["exec", "--json"];
|
||||||
@@ -338,6 +428,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
}),
|
}),
|
||||||
env: redactEnvForLogs(env),
|
env: redactEnvForLogs(env),
|
||||||
prompt,
|
prompt,
|
||||||
|
promptMetrics,
|
||||||
context,
|
context,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
export { execute } from "./execute.js";
|
export { execute, ensureCodexSkillsInjected } from "./execute.js";
|
||||||
export { testEnvironment } from "./test.js";
|
export { testEnvironment } from "./test.js";
|
||||||
export { parseCodexJsonl, isCodexUnknownSessionError } from "./parse.js";
|
export { parseCodexJsonl, isCodexUnknownSessionError } from "./parse.js";
|
||||||
import type { AdapterSessionCodec } from "@paperclipai/adapter-utils";
|
import type { AdapterSessionCodec } from "@paperclipai/adapter-utils";
|
||||||
|
|||||||
@@ -71,6 +71,7 @@ export function buildCodexLocalConfig(v: CreateConfigValues): Record<string, unk
|
|||||||
if (v.cwd) ac.cwd = v.cwd;
|
if (v.cwd) ac.cwd = v.cwd;
|
||||||
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
||||||
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
||||||
|
if (v.bootstrapPrompt) ac.bootstrapPromptTemplate = v.bootstrapPrompt;
|
||||||
ac.model = v.model || DEFAULT_CODEX_LOCAL_MODEL;
|
ac.model = v.model || DEFAULT_CODEX_LOCAL_MODEL;
|
||||||
if (v.thinkingEffort) ac.modelReasoningEffort = v.thinkingEffort;
|
if (v.thinkingEffort) ac.modelReasoningEffort = v.thinkingEffort;
|
||||||
ac.timeoutSec = 0;
|
ac.timeoutSec = 0;
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import fs from "node:fs/promises";
|
import fs from "node:fs/promises";
|
||||||
import type { Dirent } from "node:fs";
|
|
||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
import { fileURLToPath } from "node:url";
|
import { fileURLToPath } from "node:url";
|
||||||
@@ -13,8 +12,12 @@ import {
|
|||||||
redactEnvForLogs,
|
redactEnvForLogs,
|
||||||
ensureAbsoluteDirectory,
|
ensureAbsoluteDirectory,
|
||||||
ensureCommandResolvable,
|
ensureCommandResolvable,
|
||||||
|
ensurePaperclipSkillSymlink,
|
||||||
ensurePathInEnv,
|
ensurePathInEnv,
|
||||||
|
listPaperclipSkillEntries,
|
||||||
|
removeMaintainerOnlySkillSymlinks,
|
||||||
renderTemplate,
|
renderTemplate,
|
||||||
|
joinPromptSections,
|
||||||
runChildProcess,
|
runChildProcess,
|
||||||
} from "@paperclipai/adapter-utils/server-utils";
|
} from "@paperclipai/adapter-utils/server-utils";
|
||||||
import { DEFAULT_CURSOR_LOCAL_MODEL } from "../index.js";
|
import { DEFAULT_CURSOR_LOCAL_MODEL } from "../index.js";
|
||||||
@@ -23,10 +26,6 @@ import { normalizeCursorStreamLine } from "../shared/stream.js";
|
|||||||
import { hasCursorTrustBypassArg } from "../shared/trust.js";
|
import { hasCursorTrustBypassArg } from "../shared/trust.js";
|
||||||
|
|
||||||
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
||||||
const PAPERCLIP_SKILLS_CANDIDATES = [
|
|
||||||
path.resolve(__moduleDir, "../../skills"),
|
|
||||||
path.resolve(__moduleDir, "../../../../../skills"),
|
|
||||||
];
|
|
||||||
|
|
||||||
function firstNonEmptyLine(text: string): string {
|
function firstNonEmptyLine(text: string): string {
|
||||||
return (
|
return (
|
||||||
@@ -82,16 +81,9 @@ function cursorSkillsHome(): string {
|
|||||||
return path.join(os.homedir(), ".cursor", "skills");
|
return path.join(os.homedir(), ".cursor", "skills");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolvePaperclipSkillsDir(): Promise<string | null> {
|
|
||||||
for (const candidate of PAPERCLIP_SKILLS_CANDIDATES) {
|
|
||||||
const isDir = await fs.stat(candidate).then((s) => s.isDirectory()).catch(() => false);
|
|
||||||
if (isDir) return candidate;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
type EnsureCursorSkillsInjectedOptions = {
|
type EnsureCursorSkillsInjectedOptions = {
|
||||||
skillsDir?: string | null;
|
skillsDir?: string | null;
|
||||||
|
skillsEntries?: Array<{ name: string; source: string }>;
|
||||||
skillsHome?: string;
|
skillsHome?: string;
|
||||||
linkSkill?: (source: string, target: string) => Promise<void>;
|
linkSkill?: (source: string, target: string) => Promise<void>;
|
||||||
};
|
};
|
||||||
@@ -100,8 +92,13 @@ export async function ensureCursorSkillsInjected(
|
|||||||
onLog: AdapterExecutionContext["onLog"],
|
onLog: AdapterExecutionContext["onLog"],
|
||||||
options: EnsureCursorSkillsInjectedOptions = {},
|
options: EnsureCursorSkillsInjectedOptions = {},
|
||||||
) {
|
) {
|
||||||
const skillsDir = options.skillsDir ?? await resolvePaperclipSkillsDir();
|
const skillsEntries = options.skillsEntries
|
||||||
if (!skillsDir) return;
|
?? (options.skillsDir
|
||||||
|
? (await fs.readdir(options.skillsDir, { withFileTypes: true }))
|
||||||
|
.filter((entry) => entry.isDirectory())
|
||||||
|
.map((entry) => ({ name: entry.name, source: path.join(options.skillsDir!, entry.name) }))
|
||||||
|
: await listPaperclipSkillEntries(__moduleDir));
|
||||||
|
if (skillsEntries.length === 0) return;
|
||||||
|
|
||||||
const skillsHome = options.skillsHome ?? cursorSkillsHome();
|
const skillsHome = options.skillsHome ?? cursorSkillsHome();
|
||||||
try {
|
try {
|
||||||
@@ -113,31 +110,26 @@ export async function ensureCursorSkillsInjected(
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const removedSkills = await removeMaintainerOnlySkillSymlinks(
|
||||||
let entries: Dirent[];
|
skillsHome,
|
||||||
try {
|
skillsEntries.map((entry) => entry.name),
|
||||||
entries = await fs.readdir(skillsDir, { withFileTypes: true });
|
);
|
||||||
} catch (err) {
|
for (const skillName of removedSkills) {
|
||||||
await onLog(
|
await onLog(
|
||||||
"stderr",
|
"stderr",
|
||||||
`[paperclip] Failed to read Paperclip skills from ${skillsDir}: ${err instanceof Error ? err.message : String(err)}\n`,
|
`[paperclip] Removed maintainer-only Cursor skill "${skillName}" from ${skillsHome}\n`,
|
||||||
);
|
);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const linkSkill = options.linkSkill ?? ((source: string, target: string) => fs.symlink(source, target));
|
const linkSkill = options.linkSkill ?? ((source: string, target: string) => fs.symlink(source, target));
|
||||||
for (const entry of entries) {
|
for (const entry of skillsEntries) {
|
||||||
if (!entry.isDirectory()) continue;
|
|
||||||
const source = path.join(skillsDir, entry.name);
|
|
||||||
const target = path.join(skillsHome, entry.name);
|
const target = path.join(skillsHome, entry.name);
|
||||||
const existing = await fs.lstat(target).catch(() => null);
|
|
||||||
if (existing) continue;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await linkSkill(source, target);
|
const result = await ensurePaperclipSkillSymlink(entry.source, target, linkSkill);
|
||||||
|
if (result === "skipped") continue;
|
||||||
|
|
||||||
await onLog(
|
await onLog(
|
||||||
"stderr",
|
"stderr",
|
||||||
`[paperclip] Injected Cursor skill "${entry.name}" into ${skillsHome}\n`,
|
`[paperclip] ${result === "repaired" ? "Repaired" : "Injected"} Cursor skill "${entry.name}" into ${skillsHome}\n`,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await onLog(
|
await onLog(
|
||||||
@@ -277,6 +269,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
const instructionsFilePath = asString(config.instructionsFilePath, "").trim();
|
const instructionsFilePath = asString(config.instructionsFilePath, "").trim();
|
||||||
const instructionsDir = instructionsFilePath ? `${path.dirname(instructionsFilePath)}/` : "";
|
const instructionsDir = instructionsFilePath ? `${path.dirname(instructionsFilePath)}/` : "";
|
||||||
let instructionsPrefix = "";
|
let instructionsPrefix = "";
|
||||||
|
let instructionsChars = 0;
|
||||||
if (instructionsFilePath) {
|
if (instructionsFilePath) {
|
||||||
try {
|
try {
|
||||||
const instructionsContents = await fs.readFile(instructionsFilePath, "utf8");
|
const instructionsContents = await fs.readFile(instructionsFilePath, "utf8");
|
||||||
@@ -284,6 +277,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
`${instructionsContents}\n\n` +
|
`${instructionsContents}\n\n` +
|
||||||
`The above agent instructions were loaded from ${instructionsFilePath}. ` +
|
`The above agent instructions were loaded from ${instructionsFilePath}. ` +
|
||||||
`Resolve any relative file references from ${instructionsDir}.\n\n`;
|
`Resolve any relative file references from ${instructionsDir}.\n\n`;
|
||||||
|
instructionsChars = instructionsPrefix.length;
|
||||||
await onLog(
|
await onLog(
|
||||||
"stderr",
|
"stderr",
|
||||||
`[paperclip] Loaded agent instructions file: ${instructionsFilePath}\n`,
|
`[paperclip] Loaded agent instructions file: ${instructionsFilePath}\n`,
|
||||||
@@ -316,7 +310,8 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
return notes;
|
return notes;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
const renderedPrompt = renderTemplate(promptTemplate, {
|
const bootstrapPromptTemplate = asString(config.bootstrapPromptTemplate, "");
|
||||||
|
const templateData = {
|
||||||
agentId: agent.id,
|
agentId: agent.id,
|
||||||
companyId: agent.companyId,
|
companyId: agent.companyId,
|
||||||
runId,
|
runId,
|
||||||
@@ -324,9 +319,29 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
agent,
|
agent,
|
||||||
run: { id: runId, source: "on_demand" },
|
run: { id: runId, source: "on_demand" },
|
||||||
context,
|
context,
|
||||||
});
|
};
|
||||||
|
const renderedPrompt = renderTemplate(promptTemplate, templateData);
|
||||||
|
const renderedBootstrapPrompt =
|
||||||
|
!sessionId && bootstrapPromptTemplate.trim().length > 0
|
||||||
|
? renderTemplate(bootstrapPromptTemplate, templateData).trim()
|
||||||
|
: "";
|
||||||
|
const sessionHandoffNote = asString(context.paperclipSessionHandoffMarkdown, "").trim();
|
||||||
const paperclipEnvNote = renderPaperclipEnvNote(env);
|
const paperclipEnvNote = renderPaperclipEnvNote(env);
|
||||||
const prompt = `${instructionsPrefix}${paperclipEnvNote}${renderedPrompt}`;
|
const prompt = joinPromptSections([
|
||||||
|
instructionsPrefix,
|
||||||
|
renderedBootstrapPrompt,
|
||||||
|
sessionHandoffNote,
|
||||||
|
paperclipEnvNote,
|
||||||
|
renderedPrompt,
|
||||||
|
]);
|
||||||
|
const promptMetrics = {
|
||||||
|
promptChars: prompt.length,
|
||||||
|
instructionsChars,
|
||||||
|
bootstrapPromptChars: renderedBootstrapPrompt.length,
|
||||||
|
sessionHandoffChars: sessionHandoffNote.length,
|
||||||
|
runtimeNoteChars: paperclipEnvNote.length,
|
||||||
|
heartbeatPromptChars: renderedPrompt.length,
|
||||||
|
};
|
||||||
|
|
||||||
const buildArgs = (resumeSessionId: string | null) => {
|
const buildArgs = (resumeSessionId: string | null) => {
|
||||||
const args = ["-p", "--output-format", "stream-json", "--workspace", cwd];
|
const args = ["-p", "--output-format", "stream-json", "--workspace", cwd];
|
||||||
@@ -349,6 +364,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
commandArgs: args,
|
commandArgs: args,
|
||||||
env: redactEnvForLogs(env),
|
env: redactEnvForLogs(env),
|
||||||
prompt,
|
prompt,
|
||||||
|
promptMetrics,
|
||||||
context,
|
context,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ export function buildCursorLocalConfig(v: CreateConfigValues): Record<string, un
|
|||||||
if (v.cwd) ac.cwd = v.cwd;
|
if (v.cwd) ac.cwd = v.cwd;
|
||||||
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
||||||
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
||||||
|
if (v.bootstrapPrompt) ac.bootstrapPromptTemplate = v.bootstrapPrompt;
|
||||||
ac.model = v.model || DEFAULT_CURSOR_LOCAL_MODEL;
|
ac.model = v.model || DEFAULT_CURSOR_LOCAL_MODEL;
|
||||||
const mode = normalizeMode(v.thinkingEffort);
|
const mode = normalizeMode(v.thinkingEffort);
|
||||||
if (mode) ac.mode = mode;
|
if (mode) ac.mode = mode;
|
||||||
|
|||||||
@@ -12,7 +12,11 @@ import {
|
|||||||
buildPaperclipEnv,
|
buildPaperclipEnv,
|
||||||
ensureAbsoluteDirectory,
|
ensureAbsoluteDirectory,
|
||||||
ensureCommandResolvable,
|
ensureCommandResolvable,
|
||||||
|
ensurePaperclipSkillSymlink,
|
||||||
|
joinPromptSections,
|
||||||
ensurePathInEnv,
|
ensurePathInEnv,
|
||||||
|
listPaperclipSkillEntries,
|
||||||
|
removeMaintainerOnlySkillSymlinks,
|
||||||
parseObject,
|
parseObject,
|
||||||
redactEnvForLogs,
|
redactEnvForLogs,
|
||||||
renderTemplate,
|
renderTemplate,
|
||||||
@@ -29,10 +33,6 @@ import {
|
|||||||
import { firstNonEmptyLine } from "./utils.js";
|
import { firstNonEmptyLine } from "./utils.js";
|
||||||
|
|
||||||
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
||||||
const PAPERCLIP_SKILLS_CANDIDATES = [
|
|
||||||
path.resolve(__moduleDir, "../../skills"),
|
|
||||||
path.resolve(__moduleDir, "../../../../../skills"),
|
|
||||||
];
|
|
||||||
|
|
||||||
function hasNonEmptyEnvValue(env: Record<string, string>, key: string): boolean {
|
function hasNonEmptyEnvValue(env: Record<string, string>, key: string): boolean {
|
||||||
const raw = env[key];
|
const raw = env[key];
|
||||||
@@ -73,14 +73,6 @@ function renderApiAccessNote(env: Record<string, string>): string {
|
|||||||
].join("\n");
|
].join("\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolvePaperclipSkillsDir(): Promise<string | null> {
|
|
||||||
for (const candidate of PAPERCLIP_SKILLS_CANDIDATES) {
|
|
||||||
const isDir = await fs.stat(candidate).then((s) => s.isDirectory()).catch(() => false);
|
|
||||||
if (isDir) return candidate;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function geminiSkillsHome(): string {
|
function geminiSkillsHome(): string {
|
||||||
return path.join(os.homedir(), ".gemini", "skills");
|
return path.join(os.homedir(), ".gemini", "skills");
|
||||||
}
|
}
|
||||||
@@ -93,8 +85,8 @@ function geminiSkillsHome(): string {
|
|||||||
async function ensureGeminiSkillsInjected(
|
async function ensureGeminiSkillsInjected(
|
||||||
onLog: AdapterExecutionContext["onLog"],
|
onLog: AdapterExecutionContext["onLog"],
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
const skillsDir = await resolvePaperclipSkillsDir();
|
const skillsEntries = await listPaperclipSkillEntries(__moduleDir);
|
||||||
if (!skillsDir) return;
|
if (skillsEntries.length === 0) return;
|
||||||
|
|
||||||
const skillsHome = geminiSkillsHome();
|
const skillsHome = geminiSkillsHome();
|
||||||
try {
|
try {
|
||||||
@@ -106,28 +98,27 @@ async function ensureGeminiSkillsInjected(
|
|||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
const removedSkills = await removeMaintainerOnlySkillSymlinks(
|
||||||
let entries: Dirent[];
|
skillsHome,
|
||||||
try {
|
skillsEntries.map((entry) => entry.name),
|
||||||
entries = await fs.readdir(skillsDir, { withFileTypes: true });
|
);
|
||||||
} catch (err) {
|
for (const skillName of removedSkills) {
|
||||||
await onLog(
|
await onLog(
|
||||||
"stderr",
|
"stderr",
|
||||||
`[paperclip] Failed to read Paperclip skills from ${skillsDir}: ${err instanceof Error ? err.message : String(err)}\n`,
|
`[paperclip] Removed maintainer-only Gemini skill "${skillName}" from ${skillsHome}\n`,
|
||||||
);
|
);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const entry of entries) {
|
for (const entry of skillsEntries) {
|
||||||
if (!entry.isDirectory()) continue;
|
|
||||||
const source = path.join(skillsDir, entry.name);
|
|
||||||
const target = path.join(skillsHome, entry.name);
|
const target = path.join(skillsHome, entry.name);
|
||||||
const existing = await fs.lstat(target).catch(() => null);
|
|
||||||
if (existing) continue;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fs.symlink(source, target);
|
const result = await ensurePaperclipSkillSymlink(entry.source, target);
|
||||||
await onLog("stderr", `[paperclip] Linked Gemini skill: ${entry.name}\n`);
|
if (result === "skipped") continue;
|
||||||
|
await onLog(
|
||||||
|
"stderr",
|
||||||
|
`[paperclip] ${result === "repaired" ? "Repaired" : "Linked"} Gemini skill: ${entry.name}\n`,
|
||||||
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await onLog(
|
await onLog(
|
||||||
"stderr",
|
"stderr",
|
||||||
@@ -278,7 +269,8 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
return notes;
|
return notes;
|
||||||
})();
|
})();
|
||||||
|
|
||||||
const renderedPrompt = renderTemplate(promptTemplate, {
|
const bootstrapPromptTemplate = asString(config.bootstrapPromptTemplate, "");
|
||||||
|
const templateData = {
|
||||||
agentId: agent.id,
|
agentId: agent.id,
|
||||||
companyId: agent.companyId,
|
companyId: agent.companyId,
|
||||||
runId,
|
runId,
|
||||||
@@ -286,10 +278,31 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
agent,
|
agent,
|
||||||
run: { id: runId, source: "on_demand" },
|
run: { id: runId, source: "on_demand" },
|
||||||
context,
|
context,
|
||||||
});
|
};
|
||||||
|
const renderedPrompt = renderTemplate(promptTemplate, templateData);
|
||||||
|
const renderedBootstrapPrompt =
|
||||||
|
!sessionId && bootstrapPromptTemplate.trim().length > 0
|
||||||
|
? renderTemplate(bootstrapPromptTemplate, templateData).trim()
|
||||||
|
: "";
|
||||||
|
const sessionHandoffNote = asString(context.paperclipSessionHandoffMarkdown, "").trim();
|
||||||
const paperclipEnvNote = renderPaperclipEnvNote(env);
|
const paperclipEnvNote = renderPaperclipEnvNote(env);
|
||||||
const apiAccessNote = renderApiAccessNote(env);
|
const apiAccessNote = renderApiAccessNote(env);
|
||||||
const prompt = `${instructionsPrefix}${paperclipEnvNote}${apiAccessNote}${renderedPrompt}`;
|
const prompt = joinPromptSections([
|
||||||
|
instructionsPrefix,
|
||||||
|
renderedBootstrapPrompt,
|
||||||
|
sessionHandoffNote,
|
||||||
|
paperclipEnvNote,
|
||||||
|
apiAccessNote,
|
||||||
|
renderedPrompt,
|
||||||
|
]);
|
||||||
|
const promptMetrics = {
|
||||||
|
promptChars: prompt.length,
|
||||||
|
instructionsChars: instructionsPrefix.length,
|
||||||
|
bootstrapPromptChars: renderedBootstrapPrompt.length,
|
||||||
|
sessionHandoffChars: sessionHandoffNote.length,
|
||||||
|
runtimeNoteChars: paperclipEnvNote.length + apiAccessNote.length,
|
||||||
|
heartbeatPromptChars: renderedPrompt.length,
|
||||||
|
};
|
||||||
|
|
||||||
const buildArgs = (resumeSessionId: string | null) => {
|
const buildArgs = (resumeSessionId: string | null) => {
|
||||||
const args = ["--output-format", "stream-json"];
|
const args = ["--output-format", "stream-json"];
|
||||||
@@ -319,6 +332,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
)),
|
)),
|
||||||
env: redactEnvForLogs(env),
|
env: redactEnvForLogs(env),
|
||||||
prompt,
|
prompt,
|
||||||
|
promptMetrics,
|
||||||
context,
|
context,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -56,6 +56,7 @@ export function buildGeminiLocalConfig(v: CreateConfigValues): Record<string, un
|
|||||||
if (v.cwd) ac.cwd = v.cwd;
|
if (v.cwd) ac.cwd = v.cwd;
|
||||||
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
||||||
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
||||||
|
if (v.bootstrapPrompt) ac.bootstrapPromptTemplate = v.bootstrapPrompt;
|
||||||
ac.model = v.model || DEFAULT_GEMINI_LOCAL_MODEL;
|
ac.model = v.model || DEFAULT_GEMINI_LOCAL_MODEL;
|
||||||
ac.timeoutSec = 0;
|
ac.timeoutSec = 0;
|
||||||
ac.graceSec = 15;
|
ac.graceSec = 15;
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import {
|
|||||||
asStringArray,
|
asStringArray,
|
||||||
parseObject,
|
parseObject,
|
||||||
buildPaperclipEnv,
|
buildPaperclipEnv,
|
||||||
|
joinPromptSections,
|
||||||
redactEnvForLogs,
|
redactEnvForLogs,
|
||||||
ensureAbsoluteDirectory,
|
ensureAbsoluteDirectory,
|
||||||
ensureCommandResolvable,
|
ensureCommandResolvable,
|
||||||
@@ -233,7 +234,8 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
];
|
];
|
||||||
})();
|
})();
|
||||||
|
|
||||||
const renderedPrompt = renderTemplate(promptTemplate, {
|
const bootstrapPromptTemplate = asString(config.bootstrapPromptTemplate, "");
|
||||||
|
const templateData = {
|
||||||
agentId: agent.id,
|
agentId: agent.id,
|
||||||
companyId: agent.companyId,
|
companyId: agent.companyId,
|
||||||
runId,
|
runId,
|
||||||
@@ -241,8 +243,26 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
agent,
|
agent,
|
||||||
run: { id: runId, source: "on_demand" },
|
run: { id: runId, source: "on_demand" },
|
||||||
context,
|
context,
|
||||||
});
|
};
|
||||||
const prompt = `${instructionsPrefix}${renderedPrompt}`;
|
const renderedPrompt = renderTemplate(promptTemplate, templateData);
|
||||||
|
const renderedBootstrapPrompt =
|
||||||
|
!sessionId && bootstrapPromptTemplate.trim().length > 0
|
||||||
|
? renderTemplate(bootstrapPromptTemplate, templateData).trim()
|
||||||
|
: "";
|
||||||
|
const sessionHandoffNote = asString(context.paperclipSessionHandoffMarkdown, "").trim();
|
||||||
|
const prompt = joinPromptSections([
|
||||||
|
instructionsPrefix,
|
||||||
|
renderedBootstrapPrompt,
|
||||||
|
sessionHandoffNote,
|
||||||
|
renderedPrompt,
|
||||||
|
]);
|
||||||
|
const promptMetrics = {
|
||||||
|
promptChars: prompt.length,
|
||||||
|
instructionsChars: instructionsPrefix.length,
|
||||||
|
bootstrapPromptChars: renderedBootstrapPrompt.length,
|
||||||
|
sessionHandoffChars: sessionHandoffNote.length,
|
||||||
|
heartbeatPromptChars: renderedPrompt.length,
|
||||||
|
};
|
||||||
|
|
||||||
const buildArgs = (resumeSessionId: string | null) => {
|
const buildArgs = (resumeSessionId: string | null) => {
|
||||||
const args = ["run", "--format", "json"];
|
const args = ["run", "--format", "json"];
|
||||||
@@ -264,6 +284,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
commandArgs: [...args, `<stdin prompt ${prompt.length} chars>`],
|
commandArgs: [...args, `<stdin prompt ${prompt.length} chars>`],
|
||||||
env: redactEnvForLogs(env),
|
env: redactEnvForLogs(env),
|
||||||
prompt,
|
prompt,
|
||||||
|
promptMetrics,
|
||||||
context,
|
context,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
} from "@paperclipai/adapter-utils/server-utils";
|
} from "@paperclipai/adapter-utils/server-utils";
|
||||||
|
|
||||||
const MODELS_CACHE_TTL_MS = 60_000;
|
const MODELS_CACHE_TTL_MS = 60_000;
|
||||||
|
const MODELS_DISCOVERY_TIMEOUT_MS = 20_000;
|
||||||
|
|
||||||
function resolveOpenCodeCommand(input: unknown): string {
|
function resolveOpenCodeCommand(input: unknown): string {
|
||||||
const envOverride =
|
const envOverride =
|
||||||
@@ -115,14 +116,14 @@ export async function discoverOpenCodeModels(input: {
|
|||||||
{
|
{
|
||||||
cwd,
|
cwd,
|
||||||
env: runtimeEnv,
|
env: runtimeEnv,
|
||||||
timeoutSec: 20,
|
timeoutSec: MODELS_DISCOVERY_TIMEOUT_MS / 1000,
|
||||||
graceSec: 3,
|
graceSec: 3,
|
||||||
onLog: async () => {},
|
onLog: async () => {},
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
if (result.timedOut) {
|
if (result.timedOut) {
|
||||||
throw new Error("`opencode models` timed out.");
|
throw new Error(`\`opencode models\` timed out after ${MODELS_DISCOVERY_TIMEOUT_MS / 1000}s.`);
|
||||||
}
|
}
|
||||||
if ((result.exitCode ?? 1) !== 0) {
|
if ((result.exitCode ?? 1) !== 0) {
|
||||||
const detail = firstNonEmptyLine(result.stderr) || firstNonEmptyLine(result.stdout);
|
const detail = firstNonEmptyLine(result.stderr) || firstNonEmptyLine(result.stdout);
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ export function buildOpenCodeLocalConfig(v: CreateConfigValues): Record<string,
|
|||||||
if (v.cwd) ac.cwd = v.cwd;
|
if (v.cwd) ac.cwd = v.cwd;
|
||||||
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
||||||
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
||||||
|
if (v.bootstrapPrompt) ac.bootstrapPromptTemplate = v.bootstrapPrompt;
|
||||||
if (v.model) ac.model = v.model;
|
if (v.model) ac.model = v.model;
|
||||||
if (v.thinkingEffort) ac.variant = v.thinkingEffort;
|
if (v.thinkingEffort) ac.variant = v.thinkingEffort;
|
||||||
// OpenCode sessions can run until the CLI exits naturally; keep timeout disabled (0)
|
// OpenCode sessions can run until the CLI exits naturally; keep timeout disabled (0)
|
||||||
|
|||||||
@@ -9,10 +9,14 @@ import {
|
|||||||
asStringArray,
|
asStringArray,
|
||||||
parseObject,
|
parseObject,
|
||||||
buildPaperclipEnv,
|
buildPaperclipEnv,
|
||||||
|
joinPromptSections,
|
||||||
redactEnvForLogs,
|
redactEnvForLogs,
|
||||||
ensureAbsoluteDirectory,
|
ensureAbsoluteDirectory,
|
||||||
ensureCommandResolvable,
|
ensureCommandResolvable,
|
||||||
|
ensurePaperclipSkillSymlink,
|
||||||
ensurePathInEnv,
|
ensurePathInEnv,
|
||||||
|
listPaperclipSkillEntries,
|
||||||
|
removeMaintainerOnlySkillSymlinks,
|
||||||
renderTemplate,
|
renderTemplate,
|
||||||
runChildProcess,
|
runChildProcess,
|
||||||
} from "@paperclipai/adapter-utils/server-utils";
|
} from "@paperclipai/adapter-utils/server-utils";
|
||||||
@@ -20,10 +24,6 @@ import { isPiUnknownSessionError, parsePiJsonl } from "./parse.js";
|
|||||||
import { ensurePiModelConfiguredAndAvailable } from "./models.js";
|
import { ensurePiModelConfiguredAndAvailable } from "./models.js";
|
||||||
|
|
||||||
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
const __moduleDir = path.dirname(fileURLToPath(import.meta.url));
|
||||||
const PAPERCLIP_SKILLS_CANDIDATES = [
|
|
||||||
path.resolve(__moduleDir, "../../skills"),
|
|
||||||
path.resolve(__moduleDir, "../../../../../skills"),
|
|
||||||
];
|
|
||||||
|
|
||||||
const PAPERCLIP_SESSIONS_DIR = path.join(os.homedir(), ".pi", "paperclips");
|
const PAPERCLIP_SESSIONS_DIR = path.join(os.homedir(), ".pi", "paperclips");
|
||||||
|
|
||||||
@@ -50,34 +50,32 @@ function parseModelId(model: string | null): string | null {
|
|||||||
return trimmed.slice(trimmed.indexOf("/") + 1).trim() || null;
|
return trimmed.slice(trimmed.indexOf("/") + 1).trim() || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function resolvePaperclipSkillsDir(): Promise<string | null> {
|
|
||||||
for (const candidate of PAPERCLIP_SKILLS_CANDIDATES) {
|
|
||||||
const isDir = await fs.stat(candidate).then((s) => s.isDirectory()).catch(() => false);
|
|
||||||
if (isDir) return candidate;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function ensurePiSkillsInjected(onLog: AdapterExecutionContext["onLog"]) {
|
async function ensurePiSkillsInjected(onLog: AdapterExecutionContext["onLog"]) {
|
||||||
const skillsDir = await resolvePaperclipSkillsDir();
|
const skillsEntries = await listPaperclipSkillEntries(__moduleDir);
|
||||||
if (!skillsDir) return;
|
if (skillsEntries.length === 0) return;
|
||||||
|
|
||||||
const piSkillsHome = path.join(os.homedir(), ".pi", "agent", "skills");
|
const piSkillsHome = path.join(os.homedir(), ".pi", "agent", "skills");
|
||||||
await fs.mkdir(piSkillsHome, { recursive: true });
|
await fs.mkdir(piSkillsHome, { recursive: true });
|
||||||
|
const removedSkills = await removeMaintainerOnlySkillSymlinks(
|
||||||
|
piSkillsHome,
|
||||||
|
skillsEntries.map((entry) => entry.name),
|
||||||
|
);
|
||||||
|
for (const skillName of removedSkills) {
|
||||||
|
await onLog(
|
||||||
|
"stderr",
|
||||||
|
`[paperclip] Removed maintainer-only Pi skill "${skillName}" from ${piSkillsHome}\n`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const entries = await fs.readdir(skillsDir, { withFileTypes: true });
|
for (const entry of skillsEntries) {
|
||||||
for (const entry of entries) {
|
|
||||||
if (!entry.isDirectory()) continue;
|
|
||||||
const source = path.join(skillsDir, entry.name);
|
|
||||||
const target = path.join(piSkillsHome, entry.name);
|
const target = path.join(piSkillsHome, entry.name);
|
||||||
const existing = await fs.lstat(target).catch(() => null);
|
|
||||||
if (existing) continue;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fs.symlink(source, target);
|
const result = await ensurePaperclipSkillSymlink(entry.source, target);
|
||||||
|
if (result === "skipped") continue;
|
||||||
await onLog(
|
await onLog(
|
||||||
"stderr",
|
"stderr",
|
||||||
`[paperclip] Injected Pi skill "${entry.name}" into ${piSkillsHome}\n`,
|
`[paperclip] ${result === "repaired" ? "Repaired" : "Injected"} Pi skill "${entry.name}" into ${piSkillsHome}\n`,
|
||||||
);
|
);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
await onLog(
|
await onLog(
|
||||||
@@ -273,7 +271,8 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
systemPromptExtension = promptTemplate;
|
systemPromptExtension = promptTemplate;
|
||||||
}
|
}
|
||||||
|
|
||||||
const renderedSystemPromptExtension = renderTemplate(systemPromptExtension, {
|
const bootstrapPromptTemplate = asString(config.bootstrapPromptTemplate, "");
|
||||||
|
const templateData = {
|
||||||
agentId: agent.id,
|
agentId: agent.id,
|
||||||
companyId: agent.companyId,
|
companyId: agent.companyId,
|
||||||
runId,
|
runId,
|
||||||
@@ -281,18 +280,26 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
agent,
|
agent,
|
||||||
run: { id: runId, source: "on_demand" },
|
run: { id: runId, source: "on_demand" },
|
||||||
context,
|
context,
|
||||||
});
|
};
|
||||||
|
const renderedSystemPromptExtension = renderTemplate(systemPromptExtension, templateData);
|
||||||
// User prompt is simple - just the rendered prompt template without instructions
|
const renderedHeartbeatPrompt = renderTemplate(promptTemplate, templateData);
|
||||||
const userPrompt = renderTemplate(promptTemplate, {
|
const renderedBootstrapPrompt =
|
||||||
agentId: agent.id,
|
!canResumeSession && bootstrapPromptTemplate.trim().length > 0
|
||||||
companyId: agent.companyId,
|
? renderTemplate(bootstrapPromptTemplate, templateData).trim()
|
||||||
runId,
|
: "";
|
||||||
company: { id: agent.companyId },
|
const sessionHandoffNote = asString(context.paperclipSessionHandoffMarkdown, "").trim();
|
||||||
agent,
|
const userPrompt = joinPromptSections([
|
||||||
run: { id: runId, source: "on_demand" },
|
renderedBootstrapPrompt,
|
||||||
context,
|
sessionHandoffNote,
|
||||||
});
|
renderedHeartbeatPrompt,
|
||||||
|
]);
|
||||||
|
const promptMetrics = {
|
||||||
|
systemPromptChars: renderedSystemPromptExtension.length,
|
||||||
|
promptChars: userPrompt.length,
|
||||||
|
bootstrapPromptChars: renderedBootstrapPrompt.length,
|
||||||
|
sessionHandoffChars: sessionHandoffNote.length,
|
||||||
|
heartbeatPromptChars: renderedHeartbeatPrompt.length,
|
||||||
|
};
|
||||||
|
|
||||||
const commandNotes = (() => {
|
const commandNotes = (() => {
|
||||||
if (!resolvedInstructionsFilePath) return [] as string[];
|
if (!resolvedInstructionsFilePath) return [] as string[];
|
||||||
@@ -348,6 +355,7 @@ export async function execute(ctx: AdapterExecutionContext): Promise<AdapterExec
|
|||||||
commandArgs: args,
|
commandArgs: args,
|
||||||
env: redactEnvForLogs(env),
|
env: redactEnvForLogs(env),
|
||||||
prompt: userPrompt,
|
prompt: userPrompt,
|
||||||
|
promptMetrics,
|
||||||
context,
|
context,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,6 +48,7 @@ export function buildPiLocalConfig(v: CreateConfigValues): Record<string, unknow
|
|||||||
if (v.cwd) ac.cwd = v.cwd;
|
if (v.cwd) ac.cwd = v.cwd;
|
||||||
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
if (v.instructionsFilePath) ac.instructionsFilePath = v.instructionsFilePath;
|
||||||
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
if (v.promptTemplate) ac.promptTemplate = v.promptTemplate;
|
||||||
|
if (v.bootstrapPrompt) ac.bootstrapPromptTemplate = v.bootstrapPrompt;
|
||||||
if (v.model) ac.model = v.model;
|
if (v.model) ac.model = v.model;
|
||||||
if (v.thinkingEffort) ac.thinking = v.thinkingEffort;
|
if (v.thinkingEffort) ac.thinking = v.thinkingEffort;
|
||||||
|
|
||||||
|
|||||||
@@ -730,7 +730,7 @@ export async function ensurePostgresDatabase(
|
|||||||
`;
|
`;
|
||||||
if (existing.length > 0) return "exists";
|
if (existing.length > 0) return "exists";
|
||||||
|
|
||||||
await sql.unsafe(`create database "${databaseName}"`);
|
await sql.unsafe(`create database "${databaseName}" encoding 'UTF8' lc_collate 'C' lc_ctype 'C' template template0`);
|
||||||
return "created";
|
return "created";
|
||||||
} finally {
|
} finally {
|
||||||
await sql.end();
|
await sql.end();
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ type EmbeddedPostgresCtor = new (opts: {
|
|||||||
password: string;
|
password: string;
|
||||||
port: number;
|
port: number;
|
||||||
persistent: boolean;
|
persistent: boolean;
|
||||||
|
initdbFlags?: string[];
|
||||||
onLog?: (message: unknown) => void;
|
onLog?: (message: unknown) => void;
|
||||||
onError?: (message: unknown) => void;
|
onError?: (message: unknown) => void;
|
||||||
}) => EmbeddedPostgresInstance;
|
}) => EmbeddedPostgresInstance;
|
||||||
@@ -96,6 +97,7 @@ async function ensureEmbeddedPostgresConnection(
|
|||||||
password: "paperclip",
|
password: "paperclip",
|
||||||
port: preferredPort,
|
port: preferredPort,
|
||||||
persistent: true,
|
persistent: true,
|
||||||
|
initdbFlags: ["--encoding=UTF8", "--locale=C"],
|
||||||
onLog: () => {},
|
onLog: () => {},
|
||||||
onError: () => {},
|
onError: () => {},
|
||||||
});
|
});
|
||||||
|
|||||||
54
packages/db/src/migrations/0028_harsh_goliath.sql
Normal file
54
packages/db/src/migrations/0028_harsh_goliath.sql
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
CREATE TABLE "document_revisions" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"company_id" uuid NOT NULL,
|
||||||
|
"document_id" uuid NOT NULL,
|
||||||
|
"revision_number" integer NOT NULL,
|
||||||
|
"body" text NOT NULL,
|
||||||
|
"change_summary" text,
|
||||||
|
"created_by_agent_id" uuid,
|
||||||
|
"created_by_user_id" text,
|
||||||
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "documents" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"company_id" uuid NOT NULL,
|
||||||
|
"title" text,
|
||||||
|
"format" text DEFAULT 'markdown' NOT NULL,
|
||||||
|
"latest_body" text NOT NULL,
|
||||||
|
"latest_revision_id" uuid,
|
||||||
|
"latest_revision_number" integer DEFAULT 1 NOT NULL,
|
||||||
|
"created_by_agent_id" uuid,
|
||||||
|
"created_by_user_id" text,
|
||||||
|
"updated_by_agent_id" uuid,
|
||||||
|
"updated_by_user_id" text,
|
||||||
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "issue_documents" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"company_id" uuid NOT NULL,
|
||||||
|
"issue_id" uuid NOT NULL,
|
||||||
|
"document_id" uuid NOT NULL,
|
||||||
|
"key" text NOT NULL,
|
||||||
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
ALTER TABLE "document_revisions" ADD CONSTRAINT "document_revisions_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "document_revisions" ADD CONSTRAINT "document_revisions_document_id_documents_id_fk" FOREIGN KEY ("document_id") REFERENCES "public"."documents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "document_revisions" ADD CONSTRAINT "document_revisions_created_by_agent_id_agents_id_fk" FOREIGN KEY ("created_by_agent_id") REFERENCES "public"."agents"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "documents" ADD CONSTRAINT "documents_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "documents" ADD CONSTRAINT "documents_created_by_agent_id_agents_id_fk" FOREIGN KEY ("created_by_agent_id") REFERENCES "public"."agents"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "documents" ADD CONSTRAINT "documents_updated_by_agent_id_agents_id_fk" FOREIGN KEY ("updated_by_agent_id") REFERENCES "public"."agents"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "issue_documents" ADD CONSTRAINT "issue_documents_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "issue_documents" ADD CONSTRAINT "issue_documents_issue_id_issues_id_fk" FOREIGN KEY ("issue_id") REFERENCES "public"."issues"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "issue_documents" ADD CONSTRAINT "issue_documents_document_id_documents_id_fk" FOREIGN KEY ("document_id") REFERENCES "public"."documents"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX "document_revisions_document_revision_uq" ON "document_revisions" USING btree ("document_id","revision_number");--> statement-breakpoint
|
||||||
|
CREATE INDEX "document_revisions_company_document_created_idx" ON "document_revisions" USING btree ("company_id","document_id","created_at");--> statement-breakpoint
|
||||||
|
CREATE INDEX "documents_company_updated_idx" ON "documents" USING btree ("company_id","updated_at");--> statement-breakpoint
|
||||||
|
CREATE INDEX "documents_company_created_idx" ON "documents" USING btree ("company_id","created_at");--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX "issue_documents_company_issue_key_uq" ON "issue_documents" USING btree ("company_id","issue_id","key");--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX "issue_documents_document_uq" ON "issue_documents" USING btree ("document_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "issue_documents_company_issue_updated_idx" ON "issue_documents" USING btree ("company_id","issue_id","updated_at");
|
||||||
177
packages/db/src/migrations/0029_plugin_tables.sql
Normal file
177
packages/db/src/migrations/0029_plugin_tables.sql
Normal file
@@ -0,0 +1,177 @@
|
|||||||
|
-- Rollback:
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_logs_level_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_logs_plugin_time_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_company_settings_company_plugin_uq";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_company_settings_plugin_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_company_settings_company_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_webhook_deliveries_key_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_webhook_deliveries_status_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_webhook_deliveries_plugin_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_job_runs_status_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_job_runs_plugin_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_job_runs_job_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_jobs_unique_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_jobs_next_run_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_jobs_plugin_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_entities_external_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_entities_scope_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_entities_type_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_entities_plugin_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_state_plugin_scope_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugin_config_plugin_id_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugins_status_idx";
|
||||||
|
-- DROP INDEX IF EXISTS "plugins_plugin_key_idx";
|
||||||
|
-- DROP TABLE IF EXISTS "plugin_logs";
|
||||||
|
-- DROP TABLE IF EXISTS "plugin_company_settings";
|
||||||
|
-- DROP TABLE IF EXISTS "plugin_webhook_deliveries";
|
||||||
|
-- DROP TABLE IF EXISTS "plugin_job_runs";
|
||||||
|
-- DROP TABLE IF EXISTS "plugin_jobs";
|
||||||
|
-- DROP TABLE IF EXISTS "plugin_entities";
|
||||||
|
-- DROP TABLE IF EXISTS "plugin_state";
|
||||||
|
-- DROP TABLE IF EXISTS "plugin_config";
|
||||||
|
-- DROP TABLE IF EXISTS "plugins";
|
||||||
|
|
||||||
|
CREATE TABLE "plugins" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"plugin_key" text NOT NULL,
|
||||||
|
"package_name" text NOT NULL,
|
||||||
|
"package_path" text,
|
||||||
|
"version" text NOT NULL,
|
||||||
|
"api_version" integer DEFAULT 1 NOT NULL,
|
||||||
|
"categories" jsonb DEFAULT '[]'::jsonb NOT NULL,
|
||||||
|
"manifest_json" jsonb NOT NULL,
|
||||||
|
"status" text DEFAULT 'installed' NOT NULL,
|
||||||
|
"install_order" integer,
|
||||||
|
"last_error" text,
|
||||||
|
"installed_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "plugin_config" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"plugin_id" uuid NOT NULL,
|
||||||
|
"config_json" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
||||||
|
"last_error" text,
|
||||||
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "plugin_state" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"plugin_id" uuid NOT NULL,
|
||||||
|
"scope_kind" text NOT NULL,
|
||||||
|
"scope_id" text,
|
||||||
|
"namespace" text DEFAULT 'default' NOT NULL,
|
||||||
|
"state_key" text NOT NULL,
|
||||||
|
"value_json" jsonb NOT NULL,
|
||||||
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
|
CONSTRAINT "plugin_state_unique_entry_idx" UNIQUE NULLS NOT DISTINCT("plugin_id","scope_kind","scope_id","namespace","state_key")
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "plugin_entities" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"plugin_id" uuid NOT NULL,
|
||||||
|
"entity_type" text NOT NULL,
|
||||||
|
"scope_kind" text NOT NULL,
|
||||||
|
"scope_id" text,
|
||||||
|
"external_id" text,
|
||||||
|
"title" text,
|
||||||
|
"status" text,
|
||||||
|
"data" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
||||||
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "plugin_jobs" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"plugin_id" uuid NOT NULL,
|
||||||
|
"job_key" text NOT NULL,
|
||||||
|
"schedule" text NOT NULL,
|
||||||
|
"status" text DEFAULT 'active' NOT NULL,
|
||||||
|
"last_run_at" timestamp with time zone,
|
||||||
|
"next_run_at" timestamp with time zone,
|
||||||
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "plugin_job_runs" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"job_id" uuid NOT NULL,
|
||||||
|
"plugin_id" uuid NOT NULL,
|
||||||
|
"trigger" text NOT NULL,
|
||||||
|
"status" text DEFAULT 'pending' NOT NULL,
|
||||||
|
"duration_ms" integer,
|
||||||
|
"error" text,
|
||||||
|
"logs" jsonb DEFAULT '[]'::jsonb NOT NULL,
|
||||||
|
"started_at" timestamp with time zone,
|
||||||
|
"finished_at" timestamp with time zone,
|
||||||
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "plugin_webhook_deliveries" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"plugin_id" uuid NOT NULL,
|
||||||
|
"webhook_key" text NOT NULL,
|
||||||
|
"external_id" text,
|
||||||
|
"status" text DEFAULT 'pending' NOT NULL,
|
||||||
|
"duration_ms" integer,
|
||||||
|
"error" text,
|
||||||
|
"payload" jsonb NOT NULL,
|
||||||
|
"headers" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
||||||
|
"started_at" timestamp with time zone,
|
||||||
|
"finished_at" timestamp with time zone,
|
||||||
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "plugin_company_settings" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||||
|
"company_id" uuid NOT NULL,
|
||||||
|
"plugin_id" uuid NOT NULL,
|
||||||
|
"settings_json" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
||||||
|
"last_error" text,
|
||||||
|
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
|
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
||||||
|
"enabled" boolean DEFAULT true NOT NULL
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
CREATE TABLE "plugin_logs" (
|
||||||
|
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||||
|
"plugin_id" uuid NOT NULL,
|
||||||
|
"level" text NOT NULL DEFAULT 'info',
|
||||||
|
"message" text NOT NULL,
|
||||||
|
"meta" jsonb,
|
||||||
|
"created_at" timestamp with time zone NOT NULL DEFAULT now()
|
||||||
|
);
|
||||||
|
--> statement-breakpoint
|
||||||
|
ALTER TABLE "plugin_config" ADD CONSTRAINT "plugin_config_plugin_id_plugins_id_fk" FOREIGN KEY ("plugin_id") REFERENCES "public"."plugins"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "plugin_state" ADD CONSTRAINT "plugin_state_plugin_id_plugins_id_fk" FOREIGN KEY ("plugin_id") REFERENCES "public"."plugins"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "plugin_entities" ADD CONSTRAINT "plugin_entities_plugin_id_plugins_id_fk" FOREIGN KEY ("plugin_id") REFERENCES "public"."plugins"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "plugin_jobs" ADD CONSTRAINT "plugin_jobs_plugin_id_plugins_id_fk" FOREIGN KEY ("plugin_id") REFERENCES "public"."plugins"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "plugin_job_runs" ADD CONSTRAINT "plugin_job_runs_job_id_plugin_jobs_id_fk" FOREIGN KEY ("job_id") REFERENCES "public"."plugin_jobs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "plugin_job_runs" ADD CONSTRAINT "plugin_job_runs_plugin_id_plugins_id_fk" FOREIGN KEY ("plugin_id") REFERENCES "public"."plugins"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "plugin_webhook_deliveries" ADD CONSTRAINT "plugin_webhook_deliveries_plugin_id_plugins_id_fk" FOREIGN KEY ("plugin_id") REFERENCES "public"."plugins"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "plugin_company_settings" ADD CONSTRAINT "plugin_company_settings_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "plugin_company_settings" ADD CONSTRAINT "plugin_company_settings_plugin_id_plugins_id_fk" FOREIGN KEY ("plugin_id") REFERENCES "public"."plugins"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
ALTER TABLE "plugin_logs" ADD CONSTRAINT "plugin_logs_plugin_id_plugins_id_fk" FOREIGN KEY ("plugin_id") REFERENCES "public"."plugins"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX "plugins_plugin_key_idx" ON "plugins" USING btree ("plugin_key");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugins_status_idx" ON "plugins" USING btree ("status");--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX "plugin_config_plugin_id_idx" ON "plugin_config" USING btree ("plugin_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_state_plugin_scope_idx" ON "plugin_state" USING btree ("plugin_id","scope_kind");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_entities_plugin_idx" ON "plugin_entities" USING btree ("plugin_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_entities_type_idx" ON "plugin_entities" USING btree ("entity_type");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_entities_scope_idx" ON "plugin_entities" USING btree ("scope_kind","scope_id");--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX "plugin_entities_external_idx" ON "plugin_entities" USING btree ("plugin_id","entity_type","external_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_jobs_plugin_idx" ON "plugin_jobs" USING btree ("plugin_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_jobs_next_run_idx" ON "plugin_jobs" USING btree ("next_run_at");--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX "plugin_jobs_unique_idx" ON "plugin_jobs" USING btree ("plugin_id","job_key");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_job_runs_job_idx" ON "plugin_job_runs" USING btree ("job_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_job_runs_plugin_idx" ON "plugin_job_runs" USING btree ("plugin_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_job_runs_status_idx" ON "plugin_job_runs" USING btree ("status");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_webhook_deliveries_plugin_idx" ON "plugin_webhook_deliveries" USING btree ("plugin_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_webhook_deliveries_status_idx" ON "plugin_webhook_deliveries" USING btree ("status");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_webhook_deliveries_key_idx" ON "plugin_webhook_deliveries" USING btree ("webhook_key");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_company_settings_company_idx" ON "plugin_company_settings" USING btree ("company_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_company_settings_plugin_idx" ON "plugin_company_settings" USING btree ("plugin_id");--> statement-breakpoint
|
||||||
|
CREATE UNIQUE INDEX "plugin_company_settings_company_plugin_uq" ON "plugin_company_settings" USING btree ("company_id","plugin_id");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_logs_plugin_time_idx" ON "plugin_logs" USING btree ("plugin_id","created_at");--> statement-breakpoint
|
||||||
|
CREATE INDEX "plugin_logs_level_idx" ON "plugin_logs" USING btree ("level");
|
||||||
6710
packages/db/src/migrations/meta/0028_snapshot.json
Normal file
6710
packages/db/src/migrations/meta/0028_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
7899
packages/db/src/migrations/meta/0029_snapshot.json
Normal file
7899
packages/db/src/migrations/meta/0029_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -197,6 +197,20 @@
|
|||||||
"when": 1773150731736,
|
"when": 1773150731736,
|
||||||
"tag": "0027_tranquil_tenebrous",
|
"tag": "0027_tranquil_tenebrous",
|
||||||
"breakpoints": true
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 28,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1773432085646,
|
||||||
|
"tag": "0028_harsh_goliath",
|
||||||
|
"breakpoints": true
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"idx": 29,
|
||||||
|
"version": "7",
|
||||||
|
"when": 1773417600000,
|
||||||
|
"tag": "0029_plugin_tables",
|
||||||
|
"breakpoints": true
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
30
packages/db/src/schema/document_revisions.ts
Normal file
30
packages/db/src/schema/document_revisions.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { pgTable, uuid, text, integer, timestamp, index, uniqueIndex } from "drizzle-orm/pg-core";
|
||||||
|
import { companies } from "./companies.js";
|
||||||
|
import { agents } from "./agents.js";
|
||||||
|
import { documents } from "./documents.js";
|
||||||
|
|
||||||
|
export const documentRevisions = pgTable(
|
||||||
|
"document_revisions",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
companyId: uuid("company_id").notNull().references(() => companies.id),
|
||||||
|
documentId: uuid("document_id").notNull().references(() => documents.id, { onDelete: "cascade" }),
|
||||||
|
revisionNumber: integer("revision_number").notNull(),
|
||||||
|
body: text("body").notNull(),
|
||||||
|
changeSummary: text("change_summary"),
|
||||||
|
createdByAgentId: uuid("created_by_agent_id").references(() => agents.id, { onDelete: "set null" }),
|
||||||
|
createdByUserId: text("created_by_user_id"),
|
||||||
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
documentRevisionUq: uniqueIndex("document_revisions_document_revision_uq").on(
|
||||||
|
table.documentId,
|
||||||
|
table.revisionNumber,
|
||||||
|
),
|
||||||
|
companyDocumentCreatedIdx: index("document_revisions_company_document_created_idx").on(
|
||||||
|
table.companyId,
|
||||||
|
table.documentId,
|
||||||
|
table.createdAt,
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
);
|
||||||
26
packages/db/src/schema/documents.ts
Normal file
26
packages/db/src/schema/documents.ts
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
import { pgTable, uuid, text, integer, timestamp, index } from "drizzle-orm/pg-core";
|
||||||
|
import { companies } from "./companies.js";
|
||||||
|
import { agents } from "./agents.js";
|
||||||
|
|
||||||
|
export const documents = pgTable(
|
||||||
|
"documents",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
companyId: uuid("company_id").notNull().references(() => companies.id),
|
||||||
|
title: text("title"),
|
||||||
|
format: text("format").notNull().default("markdown"),
|
||||||
|
latestBody: text("latest_body").notNull(),
|
||||||
|
latestRevisionId: uuid("latest_revision_id"),
|
||||||
|
latestRevisionNumber: integer("latest_revision_number").notNull().default(1),
|
||||||
|
createdByAgentId: uuid("created_by_agent_id").references(() => agents.id, { onDelete: "set null" }),
|
||||||
|
createdByUserId: text("created_by_user_id"),
|
||||||
|
updatedByAgentId: uuid("updated_by_agent_id").references(() => agents.id, { onDelete: "set null" }),
|
||||||
|
updatedByUserId: text("updated_by_user_id"),
|
||||||
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
companyUpdatedIdx: index("documents_company_updated_idx").on(table.companyId, table.updatedAt),
|
||||||
|
companyCreatedIdx: index("documents_company_created_idx").on(table.companyId, table.createdAt),
|
||||||
|
}),
|
||||||
|
);
|
||||||
@@ -24,6 +24,9 @@ export { issueComments } from "./issue_comments.js";
|
|||||||
export { issueReadStates } from "./issue_read_states.js";
|
export { issueReadStates } from "./issue_read_states.js";
|
||||||
export { assets } from "./assets.js";
|
export { assets } from "./assets.js";
|
||||||
export { issueAttachments } from "./issue_attachments.js";
|
export { issueAttachments } from "./issue_attachments.js";
|
||||||
|
export { documents } from "./documents.js";
|
||||||
|
export { documentRevisions } from "./document_revisions.js";
|
||||||
|
export { issueDocuments } from "./issue_documents.js";
|
||||||
export { heartbeatRuns } from "./heartbeat_runs.js";
|
export { heartbeatRuns } from "./heartbeat_runs.js";
|
||||||
export { heartbeatRunEvents } from "./heartbeat_run_events.js";
|
export { heartbeatRunEvents } from "./heartbeat_run_events.js";
|
||||||
export { costEvents } from "./cost_events.js";
|
export { costEvents } from "./cost_events.js";
|
||||||
@@ -32,3 +35,11 @@ export { approvalComments } from "./approval_comments.js";
|
|||||||
export { activityLog } from "./activity_log.js";
|
export { activityLog } from "./activity_log.js";
|
||||||
export { companySecrets } from "./company_secrets.js";
|
export { companySecrets } from "./company_secrets.js";
|
||||||
export { companySecretVersions } from "./company_secret_versions.js";
|
export { companySecretVersions } from "./company_secret_versions.js";
|
||||||
|
export { plugins } from "./plugins.js";
|
||||||
|
export { pluginConfig } from "./plugin_config.js";
|
||||||
|
export { pluginCompanySettings } from "./plugin_company_settings.js";
|
||||||
|
export { pluginState } from "./plugin_state.js";
|
||||||
|
export { pluginEntities } from "./plugin_entities.js";
|
||||||
|
export { pluginJobs, pluginJobRuns } from "./plugin_jobs.js";
|
||||||
|
export { pluginWebhookDeliveries } from "./plugin_webhooks.js";
|
||||||
|
export { pluginLogs } from "./plugin_logs.js";
|
||||||
|
|||||||
30
packages/db/src/schema/issue_documents.ts
Normal file
30
packages/db/src/schema/issue_documents.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { pgTable, uuid, text, timestamp, index, uniqueIndex } from "drizzle-orm/pg-core";
|
||||||
|
import { companies } from "./companies.js";
|
||||||
|
import { issues } from "./issues.js";
|
||||||
|
import { documents } from "./documents.js";
|
||||||
|
|
||||||
|
export const issueDocuments = pgTable(
|
||||||
|
"issue_documents",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
companyId: uuid("company_id").notNull().references(() => companies.id),
|
||||||
|
issueId: uuid("issue_id").notNull().references(() => issues.id, { onDelete: "cascade" }),
|
||||||
|
documentId: uuid("document_id").notNull().references(() => documents.id, { onDelete: "cascade" }),
|
||||||
|
key: text("key").notNull(),
|
||||||
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
companyIssueKeyUq: uniqueIndex("issue_documents_company_issue_key_uq").on(
|
||||||
|
table.companyId,
|
||||||
|
table.issueId,
|
||||||
|
table.key,
|
||||||
|
),
|
||||||
|
documentUq: uniqueIndex("issue_documents_document_uq").on(table.documentId),
|
||||||
|
companyIssueUpdatedIdx: index("issue_documents_company_issue_updated_idx").on(
|
||||||
|
table.companyId,
|
||||||
|
table.issueId,
|
||||||
|
table.updatedAt,
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
);
|
||||||
41
packages/db/src/schema/plugin_company_settings.ts
Normal file
41
packages/db/src/schema/plugin_company_settings.ts
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
import { pgTable, uuid, text, timestamp, jsonb, index, uniqueIndex, boolean } from "drizzle-orm/pg-core";
|
||||||
|
import { companies } from "./companies.js";
|
||||||
|
import { plugins } from "./plugins.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `plugin_company_settings` table — stores operator-managed plugin settings
|
||||||
|
* scoped to a specific company.
|
||||||
|
*
|
||||||
|
* This is distinct from `plugin_config`, which stores instance-wide plugin
|
||||||
|
* configuration. Each company can have at most one settings row per plugin.
|
||||||
|
*
|
||||||
|
* Rows represent explicit overrides from the default company behavior:
|
||||||
|
* - no row => plugin is enabled for the company by default
|
||||||
|
* - row with `enabled = false` => plugin is disabled for that company
|
||||||
|
* - row with `enabled = true` => plugin remains enabled and stores company settings
|
||||||
|
*/
|
||||||
|
export const pluginCompanySettings = pgTable(
|
||||||
|
"plugin_company_settings",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
companyId: uuid("company_id")
|
||||||
|
.notNull()
|
||||||
|
.references(() => companies.id, { onDelete: "cascade" }),
|
||||||
|
pluginId: uuid("plugin_id")
|
||||||
|
.notNull()
|
||||||
|
.references(() => plugins.id, { onDelete: "cascade" }),
|
||||||
|
enabled: boolean("enabled").notNull().default(true),
|
||||||
|
settingsJson: jsonb("settings_json").$type<Record<string, unknown>>().notNull().default({}),
|
||||||
|
lastError: text("last_error"),
|
||||||
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
companyIdx: index("plugin_company_settings_company_idx").on(table.companyId),
|
||||||
|
pluginIdx: index("plugin_company_settings_plugin_idx").on(table.pluginId),
|
||||||
|
companyPluginUq: uniqueIndex("plugin_company_settings_company_plugin_uq").on(
|
||||||
|
table.companyId,
|
||||||
|
table.pluginId,
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
);
|
||||||
30
packages/db/src/schema/plugin_config.ts
Normal file
30
packages/db/src/schema/plugin_config.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import { pgTable, uuid, text, timestamp, jsonb, uniqueIndex } from "drizzle-orm/pg-core";
|
||||||
|
import { plugins } from "./plugins.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `plugin_config` table — stores operator-provided instance configuration
|
||||||
|
* for each plugin (one row per plugin, enforced by a unique index on
|
||||||
|
* `plugin_id`).
|
||||||
|
*
|
||||||
|
* The `config_json` column holds the values that the operator enters in the
|
||||||
|
* plugin settings UI. These values are validated at runtime against the
|
||||||
|
* plugin's `instanceConfigSchema` from the manifest.
|
||||||
|
*
|
||||||
|
* @see PLUGIN_SPEC.md §21.3
|
||||||
|
*/
|
||||||
|
export const pluginConfig = pgTable(
|
||||||
|
"plugin_config",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
pluginId: uuid("plugin_id")
|
||||||
|
.notNull()
|
||||||
|
.references(() => plugins.id, { onDelete: "cascade" }),
|
||||||
|
configJson: jsonb("config_json").$type<Record<string, unknown>>().notNull().default({}),
|
||||||
|
lastError: text("last_error"),
|
||||||
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
pluginIdIdx: uniqueIndex("plugin_config_plugin_id_idx").on(table.pluginId),
|
||||||
|
}),
|
||||||
|
);
|
||||||
54
packages/db/src/schema/plugin_entities.ts
Normal file
54
packages/db/src/schema/plugin_entities.ts
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
import {
|
||||||
|
pgTable,
|
||||||
|
uuid,
|
||||||
|
text,
|
||||||
|
timestamp,
|
||||||
|
jsonb,
|
||||||
|
index,
|
||||||
|
uniqueIndex,
|
||||||
|
} from "drizzle-orm/pg-core";
|
||||||
|
import { plugins } from "./plugins.js";
|
||||||
|
import type { PluginStateScopeKind } from "@paperclipai/shared";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `plugin_entities` table — persistent high-level mapping between Paperclip
|
||||||
|
* objects and external plugin-defined entities.
|
||||||
|
*
|
||||||
|
* This table is used by plugins (e.g. `linear`, `github`) to store pointers
|
||||||
|
* to their respective external IDs for projects, issues, etc. and to store
|
||||||
|
* their custom data.
|
||||||
|
*
|
||||||
|
* Unlike `plugin_state`, which is for raw K-V persistence, `plugin_entities`
|
||||||
|
* is intended for structured object mappings that the host can understand
|
||||||
|
* and query for cross-plugin UI integration.
|
||||||
|
*
|
||||||
|
* @see PLUGIN_SPEC.md §21.3
|
||||||
|
*/
|
||||||
|
export const pluginEntities = pgTable(
|
||||||
|
"plugin_entities",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
pluginId: uuid("plugin_id")
|
||||||
|
.notNull()
|
||||||
|
.references(() => plugins.id, { onDelete: "cascade" }),
|
||||||
|
entityType: text("entity_type").notNull(),
|
||||||
|
scopeKind: text("scope_kind").$type<PluginStateScopeKind>().notNull(),
|
||||||
|
scopeId: text("scope_id"), // NULL for global scope (text to match plugin_state.scope_id)
|
||||||
|
externalId: text("external_id"), // ID in the external system
|
||||||
|
title: text("title"),
|
||||||
|
status: text("status"),
|
||||||
|
data: jsonb("data").$type<Record<string, unknown>>().notNull().default({}),
|
||||||
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
pluginIdx: index("plugin_entities_plugin_idx").on(table.pluginId),
|
||||||
|
typeIdx: index("plugin_entities_type_idx").on(table.entityType),
|
||||||
|
scopeIdx: index("plugin_entities_scope_idx").on(table.scopeKind, table.scopeId),
|
||||||
|
externalIdx: uniqueIndex("plugin_entities_external_idx").on(
|
||||||
|
table.pluginId,
|
||||||
|
table.entityType,
|
||||||
|
table.externalId,
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
);
|
||||||
102
packages/db/src/schema/plugin_jobs.ts
Normal file
102
packages/db/src/schema/plugin_jobs.ts
Normal file
@@ -0,0 +1,102 @@
|
|||||||
|
import {
|
||||||
|
pgTable,
|
||||||
|
uuid,
|
||||||
|
text,
|
||||||
|
integer,
|
||||||
|
timestamp,
|
||||||
|
jsonb,
|
||||||
|
index,
|
||||||
|
uniqueIndex,
|
||||||
|
} from "drizzle-orm/pg-core";
|
||||||
|
import { plugins } from "./plugins.js";
|
||||||
|
import type { PluginJobStatus, PluginJobRunStatus, PluginJobRunTrigger } from "@paperclipai/shared";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `plugin_jobs` table — registration and runtime configuration for
|
||||||
|
* scheduled jobs declared by plugins in their manifests.
|
||||||
|
*
|
||||||
|
* Each row represents one scheduled job entry for a plugin. The
|
||||||
|
* `job_key` matches the key declared in the manifest's `jobs` array.
|
||||||
|
* The `schedule` column stores the cron expression or interval string
|
||||||
|
* used by the job scheduler to decide when to fire the job.
|
||||||
|
*
|
||||||
|
* Status values:
|
||||||
|
* - `active` — job is enabled and will run on schedule
|
||||||
|
* - `paused` — job is temporarily disabled by the operator
|
||||||
|
* - `error` — job has been disabled due to repeated failures
|
||||||
|
*
|
||||||
|
* @see PLUGIN_SPEC.md §21.3 — `plugin_jobs`
|
||||||
|
*/
|
||||||
|
export const pluginJobs = pgTable(
|
||||||
|
"plugin_jobs",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
/** FK to the owning plugin. Cascades on delete. */
|
||||||
|
pluginId: uuid("plugin_id")
|
||||||
|
.notNull()
|
||||||
|
.references(() => plugins.id, { onDelete: "cascade" }),
|
||||||
|
/** Identifier matching the key in the plugin manifest's `jobs` array. */
|
||||||
|
jobKey: text("job_key").notNull(),
|
||||||
|
/** Cron expression (e.g. `"0 * * * *"`) or interval string. */
|
||||||
|
schedule: text("schedule").notNull(),
|
||||||
|
/** Current scheduling state. */
|
||||||
|
status: text("status").$type<PluginJobStatus>().notNull().default("active"),
|
||||||
|
/** Timestamp of the most recent successful execution. */
|
||||||
|
lastRunAt: timestamp("last_run_at", { withTimezone: true }),
|
||||||
|
/** Pre-computed timestamp of the next scheduled execution. */
|
||||||
|
nextRunAt: timestamp("next_run_at", { withTimezone: true }),
|
||||||
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
pluginIdx: index("plugin_jobs_plugin_idx").on(table.pluginId),
|
||||||
|
nextRunIdx: index("plugin_jobs_next_run_idx").on(table.nextRunAt),
|
||||||
|
uniqueJobIdx: uniqueIndex("plugin_jobs_unique_idx").on(table.pluginId, table.jobKey),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `plugin_job_runs` table — immutable execution history for plugin-owned jobs.
|
||||||
|
*
|
||||||
|
* Each row is created when a job run begins and updated when it completes.
|
||||||
|
* Rows are never modified after `status` reaches a terminal value
|
||||||
|
* (`succeeded` | `failed` | `cancelled`).
|
||||||
|
*
|
||||||
|
* Trigger values:
|
||||||
|
* - `scheduled` — fired automatically by the cron/interval scheduler
|
||||||
|
* - `manual` — triggered by an operator via the admin UI or API
|
||||||
|
*
|
||||||
|
* @see PLUGIN_SPEC.md §21.3 — `plugin_job_runs`
|
||||||
|
*/
|
||||||
|
export const pluginJobRuns = pgTable(
|
||||||
|
"plugin_job_runs",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
/** FK to the parent job definition. Cascades on delete. */
|
||||||
|
jobId: uuid("job_id")
|
||||||
|
.notNull()
|
||||||
|
.references(() => pluginJobs.id, { onDelete: "cascade" }),
|
||||||
|
/** Denormalized FK to the owning plugin for efficient querying. Cascades on delete. */
|
||||||
|
pluginId: uuid("plugin_id")
|
||||||
|
.notNull()
|
||||||
|
.references(() => plugins.id, { onDelete: "cascade" }),
|
||||||
|
/** What caused this run to start (`"scheduled"` or `"manual"`). */
|
||||||
|
trigger: text("trigger").$type<PluginJobRunTrigger>().notNull(),
|
||||||
|
/** Current lifecycle state of this run. */
|
||||||
|
status: text("status").$type<PluginJobRunStatus>().notNull().default("pending"),
|
||||||
|
/** Wall-clock duration in milliseconds. Null until the run finishes. */
|
||||||
|
durationMs: integer("duration_ms"),
|
||||||
|
/** Error message if `status === "failed"`. */
|
||||||
|
error: text("error"),
|
||||||
|
/** Ordered list of log lines emitted during this run. */
|
||||||
|
logs: jsonb("logs").$type<string[]>().notNull().default([]),
|
||||||
|
startedAt: timestamp("started_at", { withTimezone: true }),
|
||||||
|
finishedAt: timestamp("finished_at", { withTimezone: true }),
|
||||||
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
jobIdx: index("plugin_job_runs_job_idx").on(table.jobId),
|
||||||
|
pluginIdx: index("plugin_job_runs_plugin_idx").on(table.pluginId),
|
||||||
|
statusIdx: index("plugin_job_runs_status_idx").on(table.status),
|
||||||
|
}),
|
||||||
|
);
|
||||||
43
packages/db/src/schema/plugin_logs.ts
Normal file
43
packages/db/src/schema/plugin_logs.ts
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
import {
|
||||||
|
pgTable,
|
||||||
|
uuid,
|
||||||
|
text,
|
||||||
|
timestamp,
|
||||||
|
jsonb,
|
||||||
|
index,
|
||||||
|
} from "drizzle-orm/pg-core";
|
||||||
|
import { plugins } from "./plugins.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `plugin_logs` table — structured log storage for plugin workers.
|
||||||
|
*
|
||||||
|
* Each row stores a single log entry emitted by a plugin worker via
|
||||||
|
* `ctx.logger.info(...)` etc. Logs are queryable by plugin, level, and
|
||||||
|
* time range to support the operator logs panel and debugging workflows.
|
||||||
|
*
|
||||||
|
* Rows are inserted by the host when handling `log` notifications from
|
||||||
|
* the worker process. A capped retention policy can be applied via
|
||||||
|
* periodic cleanup (e.g. delete rows older than 7 days).
|
||||||
|
*
|
||||||
|
* @see PLUGIN_SPEC.md §26 — Observability
|
||||||
|
*/
|
||||||
|
export const pluginLogs = pgTable(
|
||||||
|
"plugin_logs",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
pluginId: uuid("plugin_id")
|
||||||
|
.notNull()
|
||||||
|
.references(() => plugins.id, { onDelete: "cascade" }),
|
||||||
|
level: text("level").notNull().default("info"),
|
||||||
|
message: text("message").notNull(),
|
||||||
|
meta: jsonb("meta").$type<Record<string, unknown>>(),
|
||||||
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
pluginTimeIdx: index("plugin_logs_plugin_time_idx").on(
|
||||||
|
table.pluginId,
|
||||||
|
table.createdAt,
|
||||||
|
),
|
||||||
|
levelIdx: index("plugin_logs_level_idx").on(table.level),
|
||||||
|
}),
|
||||||
|
);
|
||||||
90
packages/db/src/schema/plugin_state.ts
Normal file
90
packages/db/src/schema/plugin_state.ts
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
import {
|
||||||
|
pgTable,
|
||||||
|
uuid,
|
||||||
|
text,
|
||||||
|
timestamp,
|
||||||
|
jsonb,
|
||||||
|
index,
|
||||||
|
unique,
|
||||||
|
} from "drizzle-orm/pg-core";
|
||||||
|
import type { PluginStateScopeKind } from "@paperclipai/shared";
|
||||||
|
import { plugins } from "./plugins.js";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `plugin_state` table — scoped key-value storage for plugin workers.
|
||||||
|
*
|
||||||
|
* Each row stores a single JSON value identified by
|
||||||
|
* `(plugin_id, scope_kind, scope_id, namespace, state_key)`. Plugins use
|
||||||
|
* this table through `ctx.state.get()`, `ctx.state.set()`, and
|
||||||
|
* `ctx.state.delete()` in the SDK.
|
||||||
|
*
|
||||||
|
* Scope kinds determine the granularity of isolation:
|
||||||
|
* - `instance` — one value shared across the whole Paperclip instance
|
||||||
|
* - `company` — one value per company
|
||||||
|
* - `project` — one value per project
|
||||||
|
* - `project_workspace` — one value per project workspace
|
||||||
|
* - `agent` — one value per agent
|
||||||
|
* - `issue` — one value per issue
|
||||||
|
* - `goal` — one value per goal
|
||||||
|
* - `run` — one value per agent run
|
||||||
|
*
|
||||||
|
* The `namespace` column defaults to `"default"` and can be used to
|
||||||
|
* logically group keys without polluting the root namespace.
|
||||||
|
*
|
||||||
|
* @see PLUGIN_SPEC.md §21.3 — `plugin_state`
|
||||||
|
*/
|
||||||
|
export const pluginState = pgTable(
|
||||||
|
"plugin_state",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
/** FK to the owning plugin. Cascades on delete. */
|
||||||
|
pluginId: uuid("plugin_id")
|
||||||
|
.notNull()
|
||||||
|
.references(() => plugins.id, { onDelete: "cascade" }),
|
||||||
|
/** Granularity of the scope (e.g. `"instance"`, `"project"`, `"issue"`). */
|
||||||
|
scopeKind: text("scope_kind").$type<PluginStateScopeKind>().notNull(),
|
||||||
|
/**
|
||||||
|
* UUID or text identifier for the scoped object.
|
||||||
|
* Null for `instance` scope (which has no associated entity).
|
||||||
|
*/
|
||||||
|
scopeId: text("scope_id"),
|
||||||
|
/**
|
||||||
|
* Sub-namespace to avoid key collisions within a scope.
|
||||||
|
* Defaults to `"default"` if the plugin does not specify one.
|
||||||
|
*/
|
||||||
|
namespace: text("namespace").notNull().default("default"),
|
||||||
|
/** The key identifying this state entry within the namespace. */
|
||||||
|
stateKey: text("state_key").notNull(),
|
||||||
|
/** JSON-serializable value stored by the plugin. */
|
||||||
|
valueJson: jsonb("value_json").notNull(),
|
||||||
|
/** Timestamp of the most recent write. */
|
||||||
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
/**
|
||||||
|
* Unique constraint enforces that there is at most one value per
|
||||||
|
* (plugin, scope kind, scope id, namespace, key) tuple.
|
||||||
|
*
|
||||||
|
* `nullsNotDistinct()` is required so that `scope_id IS NULL` entries
|
||||||
|
* (used by `instance` scope) are treated as equal by PostgreSQL rather
|
||||||
|
* than as distinct nulls — otherwise the upsert target in `set()` would
|
||||||
|
* fail to match existing rows and create duplicates.
|
||||||
|
*
|
||||||
|
* Requires PostgreSQL 15+.
|
||||||
|
*/
|
||||||
|
uniqueEntry: unique("plugin_state_unique_entry_idx")
|
||||||
|
.on(
|
||||||
|
table.pluginId,
|
||||||
|
table.scopeKind,
|
||||||
|
table.scopeId,
|
||||||
|
table.namespace,
|
||||||
|
table.stateKey,
|
||||||
|
)
|
||||||
|
.nullsNotDistinct(),
|
||||||
|
/** Speed up lookups by plugin + scope kind (most common access pattern). */
|
||||||
|
pluginScopeIdx: index("plugin_state_plugin_scope_idx").on(
|
||||||
|
table.pluginId,
|
||||||
|
table.scopeKind,
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
);
|
||||||
65
packages/db/src/schema/plugin_webhooks.ts
Normal file
65
packages/db/src/schema/plugin_webhooks.ts
Normal file
@@ -0,0 +1,65 @@
|
|||||||
|
import {
|
||||||
|
pgTable,
|
||||||
|
uuid,
|
||||||
|
text,
|
||||||
|
integer,
|
||||||
|
timestamp,
|
||||||
|
jsonb,
|
||||||
|
index,
|
||||||
|
} from "drizzle-orm/pg-core";
|
||||||
|
import { plugins } from "./plugins.js";
|
||||||
|
import type { PluginWebhookDeliveryStatus } from "@paperclipai/shared";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `plugin_webhook_deliveries` table — inbound webhook delivery history for plugins.
|
||||||
|
*
|
||||||
|
* When an external system sends an HTTP POST to a plugin's registered webhook
|
||||||
|
* endpoint (e.g. `/api/plugins/:pluginKey/webhooks/:webhookKey`), the server
|
||||||
|
* creates a row in this table before dispatching the payload to the plugin
|
||||||
|
* worker. This provides an auditable log of every delivery attempt.
|
||||||
|
*
|
||||||
|
* The `webhook_key` matches the key declared in the plugin manifest's
|
||||||
|
* `webhooks` array. `external_id` is an optional identifier supplied by the
|
||||||
|
* remote system (e.g. a GitHub delivery GUID) that can be used to detect
|
||||||
|
* and reject duplicate deliveries.
|
||||||
|
*
|
||||||
|
* Status values:
|
||||||
|
* - `pending` — received but not yet dispatched to the worker
|
||||||
|
* - `processing` — currently being handled by the plugin worker
|
||||||
|
* - `succeeded` — worker processed the payload successfully
|
||||||
|
* - `failed` — worker returned an error or timed out
|
||||||
|
*
|
||||||
|
* @see PLUGIN_SPEC.md §21.3 — `plugin_webhook_deliveries`
|
||||||
|
*/
|
||||||
|
export const pluginWebhookDeliveries = pgTable(
|
||||||
|
"plugin_webhook_deliveries",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
/** FK to the owning plugin. Cascades on delete. */
|
||||||
|
pluginId: uuid("plugin_id")
|
||||||
|
.notNull()
|
||||||
|
.references(() => plugins.id, { onDelete: "cascade" }),
|
||||||
|
/** Identifier matching the key in the plugin manifest's `webhooks` array. */
|
||||||
|
webhookKey: text("webhook_key").notNull(),
|
||||||
|
/** Optional de-duplication ID provided by the external system. */
|
||||||
|
externalId: text("external_id"),
|
||||||
|
/** Current delivery state. */
|
||||||
|
status: text("status").$type<PluginWebhookDeliveryStatus>().notNull().default("pending"),
|
||||||
|
/** Wall-clock processing duration in milliseconds. Null until delivery finishes. */
|
||||||
|
durationMs: integer("duration_ms"),
|
||||||
|
/** Error message if `status === "failed"`. */
|
||||||
|
error: text("error"),
|
||||||
|
/** Raw JSON body of the inbound HTTP request. */
|
||||||
|
payload: jsonb("payload").$type<Record<string, unknown>>().notNull(),
|
||||||
|
/** Relevant HTTP headers from the inbound request (e.g. signature headers). */
|
||||||
|
headers: jsonb("headers").$type<Record<string, string>>().notNull().default({}),
|
||||||
|
startedAt: timestamp("started_at", { withTimezone: true }),
|
||||||
|
finishedAt: timestamp("finished_at", { withTimezone: true }),
|
||||||
|
createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
pluginIdx: index("plugin_webhook_deliveries_plugin_idx").on(table.pluginId),
|
||||||
|
statusIdx: index("plugin_webhook_deliveries_status_idx").on(table.status),
|
||||||
|
keyIdx: index("plugin_webhook_deliveries_key_idx").on(table.webhookKey),
|
||||||
|
}),
|
||||||
|
);
|
||||||
45
packages/db/src/schema/plugins.ts
Normal file
45
packages/db/src/schema/plugins.ts
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import {
|
||||||
|
pgTable,
|
||||||
|
uuid,
|
||||||
|
text,
|
||||||
|
integer,
|
||||||
|
timestamp,
|
||||||
|
jsonb,
|
||||||
|
index,
|
||||||
|
uniqueIndex,
|
||||||
|
} from "drizzle-orm/pg-core";
|
||||||
|
import type { PluginCategory, PluginStatus, PaperclipPluginManifestV1 } from "@paperclipai/shared";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* `plugins` table — stores one row per installed plugin.
|
||||||
|
*
|
||||||
|
* Each plugin is uniquely identified by `plugin_key` (derived from
|
||||||
|
* the manifest `id`). The full manifest is persisted as JSONB in
|
||||||
|
* `manifest_json` so the host can reconstruct capability and UI
|
||||||
|
* slot information without loading the plugin package.
|
||||||
|
*
|
||||||
|
* @see PLUGIN_SPEC.md §21.3
|
||||||
|
*/
|
||||||
|
export const plugins = pgTable(
|
||||||
|
"plugins",
|
||||||
|
{
|
||||||
|
id: uuid("id").primaryKey().defaultRandom(),
|
||||||
|
pluginKey: text("plugin_key").notNull(),
|
||||||
|
packageName: text("package_name").notNull(),
|
||||||
|
version: text("version").notNull(),
|
||||||
|
apiVersion: integer("api_version").notNull().default(1),
|
||||||
|
categories: jsonb("categories").$type<PluginCategory[]>().notNull().default([]),
|
||||||
|
manifestJson: jsonb("manifest_json").$type<PaperclipPluginManifestV1>().notNull(),
|
||||||
|
status: text("status").$type<PluginStatus>().notNull().default("installed"),
|
||||||
|
installOrder: integer("install_order"),
|
||||||
|
/** Resolved package path for local-path installs; used to find worker entrypoint. */
|
||||||
|
packagePath: text("package_path"),
|
||||||
|
lastError: text("last_error"),
|
||||||
|
installedAt: timestamp("installed_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull().defaultNow(),
|
||||||
|
},
|
||||||
|
(table) => ({
|
||||||
|
pluginKeyIdx: uniqueIndex("plugins_plugin_key_idx").on(table.pluginKey),
|
||||||
|
statusIdx: index("plugins_status_idx").on(table.status),
|
||||||
|
}),
|
||||||
|
);
|
||||||
52
packages/plugins/create-paperclip-plugin/README.md
Normal file
52
packages/plugins/create-paperclip-plugin/README.md
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
# @paperclipai/create-paperclip-plugin
|
||||||
|
|
||||||
|
Scaffolding tool for creating new Paperclip plugins.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npx @paperclipai/create-paperclip-plugin my-plugin
|
||||||
|
```
|
||||||
|
|
||||||
|
Or with options:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npx @paperclipai/create-paperclip-plugin @acme/my-plugin \
|
||||||
|
--template connector \
|
||||||
|
--category connector \
|
||||||
|
--display-name "Acme Connector" \
|
||||||
|
--description "Syncs Acme data into Paperclip" \
|
||||||
|
--author "Acme Inc"
|
||||||
|
```
|
||||||
|
|
||||||
|
Supported templates: `default`, `connector`, `workspace`
|
||||||
|
Supported categories: `connector`, `workspace`, `automation`, `ui`
|
||||||
|
|
||||||
|
Generates:
|
||||||
|
- typed manifest + worker entrypoint
|
||||||
|
- example UI widget using the supported `@paperclipai/plugin-sdk/ui` hooks
|
||||||
|
- test file using `@paperclipai/plugin-sdk/testing`
|
||||||
|
- `esbuild` and `rollup` config files using SDK bundler presets
|
||||||
|
- dev server script for hot-reload (`paperclip-plugin-dev-server`)
|
||||||
|
|
||||||
|
The scaffold intentionally uses plain React elements rather than host-provided UI kit components, because the current plugin runtime does not ship a stable shared component library yet.
|
||||||
|
|
||||||
|
Inside this repo, the generated package uses `@paperclipai/plugin-sdk` via `workspace:*`.
|
||||||
|
|
||||||
|
Outside this repo, the scaffold snapshots `@paperclipai/plugin-sdk` from your local Paperclip checkout into a `.paperclip-sdk/` tarball and points the generated package at that local file by default. You can override the SDK source explicitly:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
node packages/plugins/create-paperclip-plugin/dist/index.js @acme/my-plugin \
|
||||||
|
--output /absolute/path/to/plugins \
|
||||||
|
--sdk-path /absolute/path/to/paperclip/packages/plugins/sdk
|
||||||
|
```
|
||||||
|
|
||||||
|
That gives you an outside-repo local development path before the SDK is published to npm.
|
||||||
|
|
||||||
|
## Workflow after scaffolding
|
||||||
|
|
||||||
|
```bash
|
||||||
|
cd my-plugin
|
||||||
|
pnpm install
|
||||||
|
pnpm dev # watch worker + manifest + ui bundles
|
||||||
|
pnpm dev:ui # local UI preview server with hot-reload events
|
||||||
|
pnpm test
|
||||||
|
```
|
||||||
40
packages/plugins/create-paperclip-plugin/package.json
Normal file
40
packages/plugins/create-paperclip-plugin/package.json
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
{
|
||||||
|
"name": "@paperclipai/create-paperclip-plugin",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"type": "module",
|
||||||
|
"bin": {
|
||||||
|
"create-paperclip-plugin": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"exports": {
|
||||||
|
".": "./src/index.ts"
|
||||||
|
},
|
||||||
|
"publishConfig": {
|
||||||
|
"access": "public",
|
||||||
|
"bin": {
|
||||||
|
"create-paperclip-plugin": "./dist/index.js"
|
||||||
|
},
|
||||||
|
"exports": {
|
||||||
|
".": {
|
||||||
|
"types": "./dist/index.d.ts",
|
||||||
|
"import": "./dist/index.js"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"main": "./dist/index.js",
|
||||||
|
"types": "./dist/index.d.ts"
|
||||||
|
},
|
||||||
|
"files": [
|
||||||
|
"dist"
|
||||||
|
],
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"clean": "rm -rf dist",
|
||||||
|
"typecheck": "tsc --noEmit"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@paperclipai/plugin-sdk": "workspace:*"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^24.6.0",
|
||||||
|
"typescript": "^5.7.3"
|
||||||
|
}
|
||||||
|
}
|
||||||
496
packages/plugins/create-paperclip-plugin/src/index.ts
Normal file
496
packages/plugins/create-paperclip-plugin/src/index.ts
Normal file
@@ -0,0 +1,496 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
import { execFileSync } from "node:child_process";
|
||||||
|
import fs from "node:fs";
|
||||||
|
import path from "node:path";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
|
const VALID_TEMPLATES = ["default", "connector", "workspace"] as const;
|
||||||
|
type PluginTemplate = (typeof VALID_TEMPLATES)[number];
|
||||||
|
const VALID_CATEGORIES = new Set(["connector", "workspace", "automation", "ui"] as const);
|
||||||
|
|
||||||
|
export interface ScaffoldPluginOptions {
|
||||||
|
pluginName: string;
|
||||||
|
outputDir: string;
|
||||||
|
template?: PluginTemplate;
|
||||||
|
displayName?: string;
|
||||||
|
description?: string;
|
||||||
|
author?: string;
|
||||||
|
category?: "connector" | "workspace" | "automation" | "ui";
|
||||||
|
sdkPath?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Validate npm-style plugin package names (scoped or unscoped). */
|
||||||
|
export function isValidPluginName(name: string): boolean {
|
||||||
|
const scopedPattern = /^@[a-z0-9_-]+\/[a-z0-9._-]+$/;
|
||||||
|
const unscopedPattern = /^[a-z0-9._-]+$/;
|
||||||
|
return scopedPattern.test(name) || unscopedPattern.test(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Convert `@scope/name` to an output directory basename (`name`). */
|
||||||
|
function packageToDirName(pluginName: string): string {
|
||||||
|
return pluginName.replace(/^@[^/]+\//, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Convert an npm package name into a manifest-safe plugin id. */
|
||||||
|
function packageToManifestId(pluginName: string): string {
|
||||||
|
if (!pluginName.startsWith("@")) {
|
||||||
|
return pluginName;
|
||||||
|
}
|
||||||
|
|
||||||
|
return pluginName.slice(1).replace("/", ".");
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Build a human-readable display name from package name tokens. */
|
||||||
|
function makeDisplayName(pluginName: string): string {
|
||||||
|
const raw = packageToDirName(pluginName).replace(/[._-]+/g, " ").trim();
|
||||||
|
return raw
|
||||||
|
.split(/\s+/)
|
||||||
|
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
|
||||||
|
.join(" ");
|
||||||
|
}
|
||||||
|
|
||||||
|
function writeFile(target: string, content: string) {
|
||||||
|
fs.mkdirSync(path.dirname(target), { recursive: true });
|
||||||
|
fs.writeFileSync(target, content);
|
||||||
|
}
|
||||||
|
|
||||||
|
function quote(value: string): string {
|
||||||
|
return JSON.stringify(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
function toPosixPath(value: string): string {
|
||||||
|
return value.split(path.sep).join("/");
|
||||||
|
}
|
||||||
|
|
||||||
|
function formatFileDependency(absPath: string): string {
|
||||||
|
return `file:${toPosixPath(path.resolve(absPath))}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLocalSdkPackagePath(): string {
|
||||||
|
return path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..", "..", "sdk");
|
||||||
|
}
|
||||||
|
|
||||||
|
function getRepoRootFromSdkPath(sdkPath: string): string {
|
||||||
|
return path.resolve(sdkPath, "..", "..", "..");
|
||||||
|
}
|
||||||
|
|
||||||
|
function getLocalSharedPackagePath(sdkPath: string): string {
|
||||||
|
return path.resolve(getRepoRootFromSdkPath(sdkPath), "packages", "shared");
|
||||||
|
}
|
||||||
|
|
||||||
|
function isInsideDir(targetPath: string, parentPath: string): boolean {
|
||||||
|
const relative = path.relative(parentPath, targetPath);
|
||||||
|
return relative === "" || (!relative.startsWith("..") && !path.isAbsolute(relative));
|
||||||
|
}
|
||||||
|
|
||||||
|
function packLocalPackage(packagePath: string, outputDir: string): string {
|
||||||
|
const packageJsonPath = path.join(packagePath, "package.json");
|
||||||
|
if (!fs.existsSync(packageJsonPath)) {
|
||||||
|
throw new Error(`Package package.json not found at ${packageJsonPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, "utf8")) as {
|
||||||
|
name?: string;
|
||||||
|
version?: string;
|
||||||
|
};
|
||||||
|
const packageName = packageJson.name ?? path.basename(packagePath);
|
||||||
|
const packageVersion = packageJson.version ?? "0.0.0";
|
||||||
|
const tarballFileName = `${packageName.replace(/^@/, "").replace("/", "-")}-${packageVersion}.tgz`;
|
||||||
|
const sdkBundleDir = path.join(outputDir, ".paperclip-sdk");
|
||||||
|
|
||||||
|
fs.mkdirSync(sdkBundleDir, { recursive: true });
|
||||||
|
execFileSync("pnpm", ["build"], { cwd: packagePath, stdio: "pipe" });
|
||||||
|
execFileSync("pnpm", ["pack", "--pack-destination", sdkBundleDir], { cwd: packagePath, stdio: "pipe" });
|
||||||
|
|
||||||
|
const tarballPath = path.join(sdkBundleDir, tarballFileName);
|
||||||
|
if (!fs.existsSync(tarballPath)) {
|
||||||
|
throw new Error(`Packed tarball was not created at ${tarballPath}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tarballPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate a complete Paperclip plugin starter project.
|
||||||
|
*
|
||||||
|
* Output includes manifest/worker/UI entries, SDK harness tests, bundler presets,
|
||||||
|
* and a local dev server script for hot-reload workflow.
|
||||||
|
*/
|
||||||
|
export function scaffoldPluginProject(options: ScaffoldPluginOptions): string {
|
||||||
|
const template = options.template ?? "default";
|
||||||
|
if (!VALID_TEMPLATES.includes(template)) {
|
||||||
|
throw new Error(`Invalid template '${template}'. Expected one of: ${VALID_TEMPLATES.join(", ")}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isValidPluginName(options.pluginName)) {
|
||||||
|
throw new Error("Invalid plugin name. Must be lowercase and may include scope, dots, underscores, or hyphens.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (options.category && !VALID_CATEGORIES.has(options.category)) {
|
||||||
|
throw new Error(`Invalid category '${options.category}'. Expected one of: ${[...VALID_CATEGORIES].join(", ")}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const outputDir = path.resolve(options.outputDir);
|
||||||
|
if (fs.existsSync(outputDir)) {
|
||||||
|
throw new Error(`Directory already exists: ${outputDir}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const displayName = options.displayName ?? makeDisplayName(options.pluginName);
|
||||||
|
const description = options.description ?? "A Paperclip plugin";
|
||||||
|
const author = options.author ?? "Plugin Author";
|
||||||
|
const category = options.category ?? (template === "workspace" ? "workspace" : "connector");
|
||||||
|
const manifestId = packageToManifestId(options.pluginName);
|
||||||
|
const localSdkPath = path.resolve(options.sdkPath ?? getLocalSdkPackagePath());
|
||||||
|
const localSharedPath = getLocalSharedPackagePath(localSdkPath);
|
||||||
|
const repoRoot = getRepoRootFromSdkPath(localSdkPath);
|
||||||
|
const useWorkspaceSdk = isInsideDir(outputDir, repoRoot);
|
||||||
|
|
||||||
|
fs.mkdirSync(outputDir, { recursive: true });
|
||||||
|
|
||||||
|
const packedSharedTarball = useWorkspaceSdk ? null : packLocalPackage(localSharedPath, outputDir);
|
||||||
|
const sdkDependency = useWorkspaceSdk
|
||||||
|
? "workspace:*"
|
||||||
|
: `file:${toPosixPath(path.relative(outputDir, packLocalPackage(localSdkPath, outputDir)))}`;
|
||||||
|
|
||||||
|
const packageJson = {
|
||||||
|
name: options.pluginName,
|
||||||
|
version: "0.1.0",
|
||||||
|
type: "module",
|
||||||
|
private: true,
|
||||||
|
description,
|
||||||
|
scripts: {
|
||||||
|
build: "node ./esbuild.config.mjs",
|
||||||
|
"build:rollup": "rollup -c",
|
||||||
|
dev: "node ./esbuild.config.mjs --watch",
|
||||||
|
"dev:ui": "paperclip-plugin-dev-server --root . --ui-dir dist/ui --port 4177",
|
||||||
|
test: "vitest run --config ./vitest.config.ts",
|
||||||
|
typecheck: "tsc --noEmit"
|
||||||
|
},
|
||||||
|
paperclipPlugin: {
|
||||||
|
manifest: "./dist/manifest.js",
|
||||||
|
worker: "./dist/worker.js",
|
||||||
|
ui: "./dist/ui/"
|
||||||
|
},
|
||||||
|
keywords: ["paperclip", "plugin", category],
|
||||||
|
author,
|
||||||
|
license: "MIT",
|
||||||
|
...(packedSharedTarball
|
||||||
|
? {
|
||||||
|
pnpm: {
|
||||||
|
overrides: {
|
||||||
|
"@paperclipai/shared": `file:${toPosixPath(path.relative(outputDir, packedSharedTarball))}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
devDependencies: {
|
||||||
|
...(packedSharedTarball
|
||||||
|
? {
|
||||||
|
"@paperclipai/shared": `file:${toPosixPath(path.relative(outputDir, packedSharedTarball))}`,
|
||||||
|
}
|
||||||
|
: {}),
|
||||||
|
"@paperclipai/plugin-sdk": sdkDependency,
|
||||||
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
||||||
|
"@rollup/plugin-typescript": "^12.1.2",
|
||||||
|
"@types/node": "^24.6.0",
|
||||||
|
"@types/react": "^19.0.8",
|
||||||
|
esbuild: "^0.27.3",
|
||||||
|
rollup: "^4.38.0",
|
||||||
|
tslib: "^2.8.1",
|
||||||
|
typescript: "^5.7.3",
|
||||||
|
vitest: "^3.0.5"
|
||||||
|
},
|
||||||
|
peerDependencies: {
|
||||||
|
react: ">=18"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
writeFile(path.join(outputDir, "package.json"), `${JSON.stringify(packageJson, null, 2)}\n`);
|
||||||
|
|
||||||
|
const tsconfig = {
|
||||||
|
compilerOptions: {
|
||||||
|
target: "ES2022",
|
||||||
|
module: "NodeNext",
|
||||||
|
moduleResolution: "NodeNext",
|
||||||
|
lib: ["ES2022", "DOM"],
|
||||||
|
jsx: "react-jsx",
|
||||||
|
strict: true,
|
||||||
|
skipLibCheck: true,
|
||||||
|
declaration: true,
|
||||||
|
declarationMap: true,
|
||||||
|
sourceMap: true,
|
||||||
|
outDir: "dist",
|
||||||
|
rootDir: "."
|
||||||
|
},
|
||||||
|
include: ["src", "tests"],
|
||||||
|
exclude: ["dist", "node_modules"]
|
||||||
|
};
|
||||||
|
|
||||||
|
writeFile(path.join(outputDir, "tsconfig.json"), `${JSON.stringify(tsconfig, null, 2)}\n`);
|
||||||
|
|
||||||
|
writeFile(
|
||||||
|
path.join(outputDir, "esbuild.config.mjs"),
|
||||||
|
`import esbuild from "esbuild";
|
||||||
|
import { createPluginBundlerPresets } from "@paperclipai/plugin-sdk/bundlers";
|
||||||
|
|
||||||
|
const presets = createPluginBundlerPresets({ uiEntry: "src/ui/index.tsx" });
|
||||||
|
const watch = process.argv.includes("--watch");
|
||||||
|
|
||||||
|
const workerCtx = await esbuild.context(presets.esbuild.worker);
|
||||||
|
const manifestCtx = await esbuild.context(presets.esbuild.manifest);
|
||||||
|
const uiCtx = await esbuild.context(presets.esbuild.ui);
|
||||||
|
|
||||||
|
if (watch) {
|
||||||
|
await Promise.all([workerCtx.watch(), manifestCtx.watch(), uiCtx.watch()]);
|
||||||
|
console.log("esbuild watch mode enabled for worker, manifest, and ui");
|
||||||
|
} else {
|
||||||
|
await Promise.all([workerCtx.rebuild(), manifestCtx.rebuild(), uiCtx.rebuild()]);
|
||||||
|
await Promise.all([workerCtx.dispose(), manifestCtx.dispose(), uiCtx.dispose()]);
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
|
writeFile(
|
||||||
|
path.join(outputDir, "rollup.config.mjs"),
|
||||||
|
`import { nodeResolve } from "@rollup/plugin-node-resolve";
|
||||||
|
import typescript from "@rollup/plugin-typescript";
|
||||||
|
import { createPluginBundlerPresets } from "@paperclipai/plugin-sdk/bundlers";
|
||||||
|
|
||||||
|
const presets = createPluginBundlerPresets({ uiEntry: "src/ui/index.tsx" });
|
||||||
|
|
||||||
|
function withPlugins(config) {
|
||||||
|
if (!config) return null;
|
||||||
|
return {
|
||||||
|
...config,
|
||||||
|
plugins: [
|
||||||
|
nodeResolve({
|
||||||
|
extensions: [".ts", ".tsx", ".js", ".jsx", ".mjs"],
|
||||||
|
}),
|
||||||
|
typescript({
|
||||||
|
tsconfig: "./tsconfig.json",
|
||||||
|
declaration: false,
|
||||||
|
declarationMap: false,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default [
|
||||||
|
withPlugins(presets.rollup.manifest),
|
||||||
|
withPlugins(presets.rollup.worker),
|
||||||
|
withPlugins(presets.rollup.ui),
|
||||||
|
].filter(Boolean);
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
|
writeFile(
|
||||||
|
path.join(outputDir, "vitest.config.ts"),
|
||||||
|
`import { defineConfig } from "vitest/config";
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
test: {
|
||||||
|
include: ["tests/**/*.spec.ts"],
|
||||||
|
environment: "node",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
|
writeFile(
|
||||||
|
path.join(outputDir, "src", "manifest.ts"),
|
||||||
|
`import type { PaperclipPluginManifestV1 } from "@paperclipai/plugin-sdk";
|
||||||
|
|
||||||
|
const manifest: PaperclipPluginManifestV1 = {
|
||||||
|
id: ${quote(manifestId)},
|
||||||
|
apiVersion: 1,
|
||||||
|
version: "0.1.0",
|
||||||
|
displayName: ${quote(displayName)},
|
||||||
|
description: ${quote(description)},
|
||||||
|
author: ${quote(author)},
|
||||||
|
categories: [${quote(category)}],
|
||||||
|
capabilities: [
|
||||||
|
"events.subscribe",
|
||||||
|
"plugin.state.read",
|
||||||
|
"plugin.state.write"
|
||||||
|
],
|
||||||
|
entrypoints: {
|
||||||
|
worker: "./dist/worker.js",
|
||||||
|
ui: "./dist/ui"
|
||||||
|
},
|
||||||
|
ui: {
|
||||||
|
slots: [
|
||||||
|
{
|
||||||
|
type: "dashboardWidget",
|
||||||
|
id: "health-widget",
|
||||||
|
displayName: ${quote(`${displayName} Health`)},
|
||||||
|
exportName: "DashboardWidget"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default manifest;
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
|
writeFile(
|
||||||
|
path.join(outputDir, "src", "worker.ts"),
|
||||||
|
`import { definePlugin, runWorker } from "@paperclipai/plugin-sdk";
|
||||||
|
|
||||||
|
const plugin = definePlugin({
|
||||||
|
async setup(ctx) {
|
||||||
|
ctx.events.on("issue.created", async (event) => {
|
||||||
|
const issueId = event.entityId ?? "unknown";
|
||||||
|
await ctx.state.set({ scopeKind: "issue", scopeId: issueId, stateKey: "seen" }, true);
|
||||||
|
ctx.logger.info("Observed issue.created", { issueId });
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.data.register("health", async () => {
|
||||||
|
return { status: "ok", checkedAt: new Date().toISOString() };
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.actions.register("ping", async () => {
|
||||||
|
ctx.logger.info("Ping action invoked");
|
||||||
|
return { pong: true, at: new Date().toISOString() };
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
async onHealth() {
|
||||||
|
return { status: "ok", message: "Plugin worker is running" };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default plugin;
|
||||||
|
runWorker(plugin, import.meta.url);
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
|
writeFile(
|
||||||
|
path.join(outputDir, "src", "ui", "index.tsx"),
|
||||||
|
`import { usePluginAction, usePluginData, type PluginWidgetProps } from "@paperclipai/plugin-sdk/ui";
|
||||||
|
|
||||||
|
type HealthData = {
|
||||||
|
status: "ok" | "degraded" | "error";
|
||||||
|
checkedAt: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function DashboardWidget(_props: PluginWidgetProps) {
|
||||||
|
const { data, loading, error } = usePluginData<HealthData>("health");
|
||||||
|
const ping = usePluginAction("ping");
|
||||||
|
|
||||||
|
if (loading) return <div>Loading plugin health...</div>;
|
||||||
|
if (error) return <div>Plugin error: {error.message}</div>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ display: "grid", gap: "0.5rem" }}>
|
||||||
|
<strong>${displayName}</strong>
|
||||||
|
<div>Health: {data?.status ?? "unknown"}</div>
|
||||||
|
<div>Checked: {data?.checkedAt ?? "never"}</div>
|
||||||
|
<button onClick={() => void ping()}>Ping Worker</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
|
writeFile(
|
||||||
|
path.join(outputDir, "tests", "plugin.spec.ts"),
|
||||||
|
`import { describe, expect, it } from "vitest";
|
||||||
|
import { createTestHarness } from "@paperclipai/plugin-sdk/testing";
|
||||||
|
import manifest from "../src/manifest.js";
|
||||||
|
import plugin from "../src/worker.js";
|
||||||
|
|
||||||
|
describe("plugin scaffold", () => {
|
||||||
|
it("registers data + actions and handles events", async () => {
|
||||||
|
const harness = createTestHarness({ manifest, capabilities: [...manifest.capabilities, "events.emit"] });
|
||||||
|
await plugin.definition.setup(harness.ctx);
|
||||||
|
|
||||||
|
await harness.emit("issue.created", { issueId: "iss_1" }, { entityId: "iss_1", entityType: "issue" });
|
||||||
|
expect(harness.getState({ scopeKind: "issue", scopeId: "iss_1", stateKey: "seen" })).toBe(true);
|
||||||
|
|
||||||
|
const data = await harness.getData<{ status: string }>("health");
|
||||||
|
expect(data.status).toBe("ok");
|
||||||
|
|
||||||
|
const action = await harness.performAction<{ pong: boolean }>("ping");
|
||||||
|
expect(action.pong).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
|
writeFile(
|
||||||
|
path.join(outputDir, "README.md"),
|
||||||
|
`# ${displayName}
|
||||||
|
|
||||||
|
${description}
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
\`\`\`bash
|
||||||
|
pnpm install
|
||||||
|
pnpm dev # watch builds
|
||||||
|
pnpm dev:ui # local dev server with hot-reload events
|
||||||
|
pnpm test
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
${sdkDependency.startsWith("file:")
|
||||||
|
? `This scaffold snapshots \`@paperclipai/plugin-sdk\` and \`@paperclipai/shared\` from a local Paperclip checkout at:\n\n\`${toPosixPath(localSdkPath)}\`\n\nThe packed tarballs live in \`.paperclip-sdk/\` for local development. Before publishing this plugin, switch those dependencies to published package versions once they are available on npm.\n\n`
|
||||||
|
: ""}
|
||||||
|
|
||||||
|
## Install Into Paperclip
|
||||||
|
|
||||||
|
\`\`\`bash
|
||||||
|
curl -X POST http://127.0.0.1:3100/api/plugins/install \\
|
||||||
|
-H "Content-Type: application/json" \\
|
||||||
|
-d '{"packageName":"${toPosixPath(outputDir)}","isLocalPath":true}'
|
||||||
|
\`\`\`
|
||||||
|
|
||||||
|
## Build Options
|
||||||
|
|
||||||
|
- \`pnpm build\` uses esbuild presets from \`@paperclipai/plugin-sdk/bundlers\`.
|
||||||
|
- \`pnpm build:rollup\` uses rollup presets from the same SDK.
|
||||||
|
`,
|
||||||
|
);
|
||||||
|
|
||||||
|
writeFile(path.join(outputDir, ".gitignore"), "dist\nnode_modules\n.paperclip-sdk\n");
|
||||||
|
|
||||||
|
return outputDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseArg(name: string): string | undefined {
|
||||||
|
const index = process.argv.indexOf(name);
|
||||||
|
if (index === -1) return undefined;
|
||||||
|
return process.argv[index + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
/** CLI wrapper for `scaffoldPluginProject`. */
|
||||||
|
function runCli() {
|
||||||
|
const pluginName = process.argv[2];
|
||||||
|
if (!pluginName) {
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.error("Usage: create-paperclip-plugin <name> [--template default|connector|workspace] [--output <dir>] [--sdk-path <paperclip-sdk-path>]");
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
const template = (parseArg("--template") ?? "default") as PluginTemplate;
|
||||||
|
const outputRoot = parseArg("--output") ?? process.cwd();
|
||||||
|
const targetDir = path.resolve(outputRoot, packageToDirName(pluginName));
|
||||||
|
|
||||||
|
const out = scaffoldPluginProject({
|
||||||
|
pluginName,
|
||||||
|
outputDir: targetDir,
|
||||||
|
template,
|
||||||
|
displayName: parseArg("--display-name"),
|
||||||
|
description: parseArg("--description"),
|
||||||
|
author: parseArg("--author"),
|
||||||
|
category: parseArg("--category") as ScaffoldPluginOptions["category"] | undefined,
|
||||||
|
sdkPath: parseArg("--sdk-path"),
|
||||||
|
});
|
||||||
|
|
||||||
|
// eslint-disable-next-line no-console
|
||||||
|
console.log(`Created plugin scaffold at ${out}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (import.meta.url === `file://${process.argv[1]}`) {
|
||||||
|
runCli();
|
||||||
|
}
|
||||||
9
packages/plugins/create-paperclip-plugin/tsconfig.json
Normal file
9
packages/plugins/create-paperclip-plugin/tsconfig.json
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "dist",
|
||||||
|
"rootDir": "src",
|
||||||
|
"types": ["node"]
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
||||||
2
packages/plugins/examples/plugin-authoring-smoke-example/.gitignore
vendored
Normal file
2
packages/plugins/examples/plugin-authoring-smoke-example/.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
dist
|
||||||
|
node_modules
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
# Plugin Authoring Smoke Example
|
||||||
|
|
||||||
|
A Paperclip plugin
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm install
|
||||||
|
pnpm dev # watch builds
|
||||||
|
pnpm dev:ui # local dev server with hot-reload events
|
||||||
|
pnpm test
|
||||||
|
```
|
||||||
|
|
||||||
|
## Install Into Paperclip
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm paperclipai plugin install ./
|
||||||
|
```
|
||||||
|
|
||||||
|
## Build Options
|
||||||
|
|
||||||
|
- `pnpm build` uses esbuild presets from `@paperclipai/plugin-sdk/bundlers`.
|
||||||
|
- `pnpm build:rollup` uses rollup presets from the same SDK.
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import esbuild from "esbuild";
|
||||||
|
import { createPluginBundlerPresets } from "@paperclipai/plugin-sdk/bundlers";
|
||||||
|
|
||||||
|
const presets = createPluginBundlerPresets({ uiEntry: "src/ui/index.tsx" });
|
||||||
|
const watch = process.argv.includes("--watch");
|
||||||
|
|
||||||
|
const workerCtx = await esbuild.context(presets.esbuild.worker);
|
||||||
|
const manifestCtx = await esbuild.context(presets.esbuild.manifest);
|
||||||
|
const uiCtx = await esbuild.context(presets.esbuild.ui);
|
||||||
|
|
||||||
|
if (watch) {
|
||||||
|
await Promise.all([workerCtx.watch(), manifestCtx.watch(), uiCtx.watch()]);
|
||||||
|
console.log("esbuild watch mode enabled for worker, manifest, and ui");
|
||||||
|
} else {
|
||||||
|
await Promise.all([workerCtx.rebuild(), manifestCtx.rebuild(), uiCtx.rebuild()]);
|
||||||
|
await Promise.all([workerCtx.dispose(), manifestCtx.dispose(), uiCtx.dispose()]);
|
||||||
|
}
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
{
|
||||||
|
"name": "@paperclipai/plugin-authoring-smoke-example",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"type": "module",
|
||||||
|
"private": true,
|
||||||
|
"description": "A Paperclip plugin",
|
||||||
|
"scripts": {
|
||||||
|
"prebuild": "node ../../../../scripts/ensure-plugin-build-deps.mjs",
|
||||||
|
"build": "node ./esbuild.config.mjs",
|
||||||
|
"build:rollup": "rollup -c",
|
||||||
|
"dev": "node ./esbuild.config.mjs --watch",
|
||||||
|
"dev:ui": "paperclip-plugin-dev-server --root . --ui-dir dist/ui --port 4177",
|
||||||
|
"test": "vitest run --config ./vitest.config.ts",
|
||||||
|
"typecheck": "pnpm --filter @paperclipai/plugin-sdk build && tsc --noEmit"
|
||||||
|
},
|
||||||
|
"paperclipPlugin": {
|
||||||
|
"manifest": "./dist/manifest.js",
|
||||||
|
"worker": "./dist/worker.js",
|
||||||
|
"ui": "./dist/ui/"
|
||||||
|
},
|
||||||
|
"keywords": [
|
||||||
|
"paperclip",
|
||||||
|
"plugin",
|
||||||
|
"connector"
|
||||||
|
],
|
||||||
|
"author": "Plugin Author",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"@paperclipai/plugin-sdk": "workspace:*"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@rollup/plugin-node-resolve": "^16.0.1",
|
||||||
|
"@rollup/plugin-typescript": "^12.1.2",
|
||||||
|
"@types/node": "^24.6.0",
|
||||||
|
"@types/react": "^19.0.8",
|
||||||
|
"esbuild": "^0.27.3",
|
||||||
|
"rollup": "^4.38.0",
|
||||||
|
"tslib": "^2.8.1",
|
||||||
|
"typescript": "^5.7.3",
|
||||||
|
"vitest": "^3.0.5"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": ">=18"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { nodeResolve } from "@rollup/plugin-node-resolve";
|
||||||
|
import typescript from "@rollup/plugin-typescript";
|
||||||
|
import { createPluginBundlerPresets } from "@paperclipai/plugin-sdk/bundlers";
|
||||||
|
|
||||||
|
const presets = createPluginBundlerPresets({ uiEntry: "src/ui/index.tsx" });
|
||||||
|
|
||||||
|
function withPlugins(config) {
|
||||||
|
if (!config) return null;
|
||||||
|
return {
|
||||||
|
...config,
|
||||||
|
plugins: [
|
||||||
|
nodeResolve({
|
||||||
|
extensions: [".ts", ".tsx", ".js", ".jsx", ".mjs"],
|
||||||
|
}),
|
||||||
|
typescript({
|
||||||
|
tsconfig: "./tsconfig.json",
|
||||||
|
declaration: false,
|
||||||
|
declarationMap: false,
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default [
|
||||||
|
withPlugins(presets.rollup.manifest),
|
||||||
|
withPlugins(presets.rollup.worker),
|
||||||
|
withPlugins(presets.rollup.ui),
|
||||||
|
].filter(Boolean);
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import type { PaperclipPluginManifestV1 } from "@paperclipai/plugin-sdk";
|
||||||
|
|
||||||
|
const manifest: PaperclipPluginManifestV1 = {
|
||||||
|
id: "paperclipai.plugin-authoring-smoke-example",
|
||||||
|
apiVersion: 1,
|
||||||
|
version: "0.1.0",
|
||||||
|
displayName: "Plugin Authoring Smoke Example",
|
||||||
|
description: "A Paperclip plugin",
|
||||||
|
author: "Plugin Author",
|
||||||
|
categories: ["connector"],
|
||||||
|
capabilities: [
|
||||||
|
"events.subscribe",
|
||||||
|
"plugin.state.read",
|
||||||
|
"plugin.state.write"
|
||||||
|
],
|
||||||
|
entrypoints: {
|
||||||
|
worker: "./dist/worker.js",
|
||||||
|
ui: "./dist/ui"
|
||||||
|
},
|
||||||
|
ui: {
|
||||||
|
slots: [
|
||||||
|
{
|
||||||
|
type: "dashboardWidget",
|
||||||
|
id: "health-widget",
|
||||||
|
displayName: "Plugin Authoring Smoke Example Health",
|
||||||
|
exportName: "DashboardWidget"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export default manifest;
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
import { usePluginAction, usePluginData, type PluginWidgetProps } from "@paperclipai/plugin-sdk/ui";
|
||||||
|
|
||||||
|
type HealthData = {
|
||||||
|
status: "ok" | "degraded" | "error";
|
||||||
|
checkedAt: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function DashboardWidget(_props: PluginWidgetProps) {
|
||||||
|
const { data, loading, error } = usePluginData<HealthData>("health");
|
||||||
|
const ping = usePluginAction("ping");
|
||||||
|
|
||||||
|
if (loading) return <div>Loading plugin health...</div>;
|
||||||
|
if (error) return <div>Plugin error: {error.message}</div>;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div style={{ display: "grid", gap: "0.5rem" }}>
|
||||||
|
<strong>Plugin Authoring Smoke Example</strong>
|
||||||
|
<div>Health: {data?.status ?? "unknown"}</div>
|
||||||
|
<div>Checked: {data?.checkedAt ?? "never"}</div>
|
||||||
|
<button onClick={() => void ping()}>Ping Worker</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
import { definePlugin, runWorker } from "@paperclipai/plugin-sdk";
|
||||||
|
|
||||||
|
const plugin = definePlugin({
|
||||||
|
async setup(ctx) {
|
||||||
|
ctx.events.on("issue.created", async (event) => {
|
||||||
|
const issueId = event.entityId ?? "unknown";
|
||||||
|
await ctx.state.set({ scopeKind: "issue", scopeId: issueId, stateKey: "seen" }, true);
|
||||||
|
ctx.logger.info("Observed issue.created", { issueId });
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.data.register("health", async () => {
|
||||||
|
return { status: "ok", checkedAt: new Date().toISOString() };
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.actions.register("ping", async () => {
|
||||||
|
ctx.logger.info("Ping action invoked");
|
||||||
|
return { pong: true, at: new Date().toISOString() };
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
async onHealth() {
|
||||||
|
return { status: "ok", message: "Plugin worker is running" };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default plugin;
|
||||||
|
runWorker(plugin, import.meta.url);
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { createTestHarness } from "@paperclipai/plugin-sdk/testing";
|
||||||
|
import manifest from "../src/manifest.js";
|
||||||
|
import plugin from "../src/worker.js";
|
||||||
|
|
||||||
|
describe("plugin scaffold", () => {
|
||||||
|
it("registers data + actions and handles events", async () => {
|
||||||
|
const harness = createTestHarness({ manifest, capabilities: [...manifest.capabilities, "events.emit"] });
|
||||||
|
await plugin.definition.setup(harness.ctx);
|
||||||
|
|
||||||
|
await harness.emit("issue.created", { issueId: "iss_1" }, { entityId: "iss_1", entityType: "issue" });
|
||||||
|
expect(harness.getState({ scopeKind: "issue", scopeId: "iss_1", stateKey: "seen" })).toBe(true);
|
||||||
|
|
||||||
|
const data = await harness.getData<{ status: string }>("health");
|
||||||
|
expect(data.status).toBe("ok");
|
||||||
|
|
||||||
|
const action = await harness.performAction<{ pong: boolean }>("ping");
|
||||||
|
expect(action.pong).toBe(true);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "NodeNext",
|
||||||
|
"moduleResolution": "NodeNext",
|
||||||
|
"lib": [
|
||||||
|
"ES2022",
|
||||||
|
"DOM"
|
||||||
|
],
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
"strict": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"declaration": true,
|
||||||
|
"declarationMap": true,
|
||||||
|
"sourceMap": true,
|
||||||
|
"outDir": "dist",
|
||||||
|
"rootDir": "."
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"src",
|
||||||
|
"tests"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"dist",
|
||||||
|
"node_modules"
|
||||||
|
]
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
import { defineConfig } from "vitest/config";
|
||||||
|
|
||||||
|
export default defineConfig({
|
||||||
|
test: {
|
||||||
|
include: ["tests/**/*.spec.ts"],
|
||||||
|
environment: "node",
|
||||||
|
},
|
||||||
|
});
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
# File Browser Example Plugin
|
||||||
|
|
||||||
|
Example Paperclip plugin that demonstrates:
|
||||||
|
|
||||||
|
- **projectSidebarItem** — An optional "Files" link under each project in the sidebar that opens the project detail with this plugin’s tab selected. This is controlled by plugin settings and defaults to off.
|
||||||
|
- **detailTab** (entityType project) — A project detail tab with a workspace-path selector, a desktop two-column layout (file tree left, editor right), and a mobile one-panel flow with a back button from editor to file tree, including save support.
|
||||||
|
|
||||||
|
This is a repo-local example plugin for development. It should not be assumed to ship in a generic production build unless it is explicitly included.
|
||||||
|
|
||||||
|
## Slots
|
||||||
|
|
||||||
|
| Slot | Type | Description |
|
||||||
|
|---------------------|---------------------|--------------------------------------------------|
|
||||||
|
| Files (sidebar) | `projectSidebarItem`| Optional link under each project → project detail + tab. |
|
||||||
|
| Files (tab) | `detailTab` | Responsive tree/editor layout with save support.|
|
||||||
|
|
||||||
|
## Settings
|
||||||
|
|
||||||
|
- `Show Files in Sidebar` — toggles the project sidebar link on or off. Defaults to off.
|
||||||
|
- `Comment File Links` — controls whether comment annotations and the comment context-menu action are shown.
|
||||||
|
|
||||||
|
## Capabilities
|
||||||
|
|
||||||
|
- `ui.sidebar.register` — project sidebar item
|
||||||
|
- `ui.detailTab.register` — project detail tab
|
||||||
|
- `projects.read` — resolve project
|
||||||
|
- `project.workspaces.read` — list workspaces and read paths for file access
|
||||||
|
|
||||||
|
## Worker
|
||||||
|
|
||||||
|
- **getData `workspaces`** — `ctx.projects.listWorkspaces(projectId, companyId)` (ordered, primary first).
|
||||||
|
- **getData `fileList`** — `{ projectId, workspaceId, directoryPath? }` → list directory entries for the workspace root or a subdirectory (Node `fs`).
|
||||||
|
- **getData `fileContent`** — `{ projectId, workspaceId, filePath }` → read file content using workspace-relative paths (Node `fs`).
|
||||||
|
- **performAction `writeFile`** — `{ projectId, workspaceId, filePath, content }` → write the current editor buffer back to disk.
|
||||||
|
|
||||||
|
## Local Install (Dev)
|
||||||
|
|
||||||
|
From the repo root, build the plugin and install it by local path:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm --filter @paperclipai/plugin-file-browser-example build
|
||||||
|
pnpm paperclipai plugin install ./packages/plugins/examples/plugin-file-browser-example
|
||||||
|
```
|
||||||
|
|
||||||
|
To uninstall:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm paperclipai plugin uninstall paperclip-file-browser-example --force
|
||||||
|
```
|
||||||
|
|
||||||
|
**Local development notes:**
|
||||||
|
|
||||||
|
- **Build first.** The host resolves the worker from the manifest `entrypoints.worker` (e.g. `./dist/worker.js`). Run `pnpm build` in the plugin directory before installing so the worker file exists.
|
||||||
|
- **Dev-only install path.** This local-path install flow assumes this monorepo checkout is present on disk. For deployed installs, publish an npm package instead of depending on `packages/plugins/examples/...` existing on the host.
|
||||||
|
- **Reinstall after pulling.** If you installed a plugin by local path before the server stored `package_path`, the plugin may show status **error** (worker not found). Uninstall and install again so the server persists the path and can activate the plugin.
|
||||||
|
- Optional: use `paperclip-plugin-dev-server` for UI hot-reload with `devUiUrl` in plugin config.
|
||||||
|
|
||||||
|
## Structure
|
||||||
|
|
||||||
|
- `src/manifest.ts` — manifest with `projectSidebarItem` and `detailTab` (entityTypes `["project"]`).
|
||||||
|
- `src/worker.ts` — data handlers for workspaces, file list, file content.
|
||||||
|
- `src/ui/index.tsx` — `FilesLink` (sidebar) and `FilesTab` (workspace path selector + two-panel file tree/editor).
|
||||||
@@ -0,0 +1,42 @@
|
|||||||
|
{
|
||||||
|
"name": "@paperclipai/plugin-file-browser-example",
|
||||||
|
"version": "0.1.0",
|
||||||
|
"description": "Example plugin: project sidebar Files link + project detail tab with workspace selector and file browser",
|
||||||
|
"type": "module",
|
||||||
|
"private": true,
|
||||||
|
"exports": {
|
||||||
|
".": "./src/index.ts"
|
||||||
|
},
|
||||||
|
"paperclipPlugin": {
|
||||||
|
"manifest": "./dist/manifest.js",
|
||||||
|
"worker": "./dist/worker.js",
|
||||||
|
"ui": "./dist/ui/"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"prebuild": "node ../../../../scripts/ensure-plugin-build-deps.mjs",
|
||||||
|
"build": "tsc && node ./scripts/build-ui.mjs",
|
||||||
|
"clean": "rm -rf dist",
|
||||||
|
"typecheck": "pnpm --filter @paperclipai/plugin-sdk build && tsc --noEmit"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@codemirror/lang-javascript": "^6.2.2",
|
||||||
|
"@codemirror/language": "^6.11.0",
|
||||||
|
"@codemirror/state": "^6.4.0",
|
||||||
|
"@codemirror/view": "^6.28.0",
|
||||||
|
"@lezer/highlight": "^1.2.1",
|
||||||
|
"@paperclipai/plugin-sdk": "workspace:*",
|
||||||
|
"codemirror": "^6.0.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^24.6.0",
|
||||||
|
"@types/react": "^19.0.8",
|
||||||
|
"@types/react-dom": "^19.0.3",
|
||||||
|
"esbuild": "^0.27.3",
|
||||||
|
"react": "^19.0.0",
|
||||||
|
"react-dom": "^19.0.0",
|
||||||
|
"typescript": "^5.7.3"
|
||||||
|
},
|
||||||
|
"peerDependencies": {
|
||||||
|
"react": ">=18"
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
import esbuild from "esbuild";
|
||||||
|
import path from "node:path";
|
||||||
|
import { fileURLToPath } from "node:url";
|
||||||
|
|
||||||
|
const __filename = fileURLToPath(import.meta.url);
|
||||||
|
const __dirname = path.dirname(__filename);
|
||||||
|
const packageRoot = path.resolve(__dirname, "..");
|
||||||
|
|
||||||
|
await esbuild.build({
|
||||||
|
entryPoints: [path.join(packageRoot, "src/ui/index.tsx")],
|
||||||
|
outfile: path.join(packageRoot, "dist/ui/index.js"),
|
||||||
|
bundle: true,
|
||||||
|
format: "esm",
|
||||||
|
platform: "browser",
|
||||||
|
target: ["es2022"],
|
||||||
|
sourcemap: true,
|
||||||
|
external: [
|
||||||
|
"react",
|
||||||
|
"react-dom",
|
||||||
|
"react/jsx-runtime",
|
||||||
|
"@paperclipai/plugin-sdk/ui",
|
||||||
|
],
|
||||||
|
logLevel: "info",
|
||||||
|
});
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
export { default as manifest } from "./manifest.js";
|
||||||
|
export { default as worker } from "./worker.js";
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
import type { PaperclipPluginManifestV1 } from "@paperclipai/plugin-sdk";
|
||||||
|
|
||||||
|
const PLUGIN_ID = "paperclip-file-browser-example";
|
||||||
|
const FILES_SIDEBAR_SLOT_ID = "files-link";
|
||||||
|
const FILES_TAB_SLOT_ID = "files-tab";
|
||||||
|
const COMMENT_FILE_LINKS_SLOT_ID = "comment-file-links";
|
||||||
|
const COMMENT_OPEN_FILES_SLOT_ID = "comment-open-files";
|
||||||
|
|
||||||
|
const manifest: PaperclipPluginManifestV1 = {
|
||||||
|
id: PLUGIN_ID,
|
||||||
|
apiVersion: 1,
|
||||||
|
version: "0.2.0",
|
||||||
|
displayName: "File Browser (Example)",
|
||||||
|
description: "Example plugin that adds a Files link under each project in the sidebar, a file browser + editor tab on the project detail page, and per-comment file link annotations with a context menu action to open referenced files.",
|
||||||
|
author: "Paperclip",
|
||||||
|
categories: ["workspace", "ui"],
|
||||||
|
capabilities: [
|
||||||
|
"ui.sidebar.register",
|
||||||
|
"ui.detailTab.register",
|
||||||
|
"ui.commentAnnotation.register",
|
||||||
|
"ui.action.register",
|
||||||
|
"projects.read",
|
||||||
|
"project.workspaces.read",
|
||||||
|
"issue.comments.read",
|
||||||
|
"plugin.state.read",
|
||||||
|
],
|
||||||
|
instanceConfigSchema: {
|
||||||
|
type: "object",
|
||||||
|
properties: {
|
||||||
|
showFilesInSidebar: {
|
||||||
|
type: "boolean",
|
||||||
|
title: "Show Files in Sidebar",
|
||||||
|
default: false,
|
||||||
|
description: "Adds the Files link under each project in the sidebar.",
|
||||||
|
},
|
||||||
|
commentAnnotationMode: {
|
||||||
|
type: "string",
|
||||||
|
title: "Comment File Links",
|
||||||
|
enum: ["annotation", "contextMenu", "both", "none"],
|
||||||
|
default: "both",
|
||||||
|
description: "Controls which comment extensions are active: 'annotation' shows file links below each comment, 'contextMenu' adds an \"Open in Files\" action to the comment menu, 'both' enables both, 'none' disables comment features.",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
entrypoints: {
|
||||||
|
worker: "./dist/worker.js",
|
||||||
|
ui: "./dist/ui",
|
||||||
|
},
|
||||||
|
ui: {
|
||||||
|
slots: [
|
||||||
|
{
|
||||||
|
type: "projectSidebarItem",
|
||||||
|
id: FILES_SIDEBAR_SLOT_ID,
|
||||||
|
displayName: "Files",
|
||||||
|
exportName: "FilesLink",
|
||||||
|
entityTypes: ["project"],
|
||||||
|
order: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "detailTab",
|
||||||
|
id: FILES_TAB_SLOT_ID,
|
||||||
|
displayName: "Files",
|
||||||
|
exportName: "FilesTab",
|
||||||
|
entityTypes: ["project"],
|
||||||
|
order: 10,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "commentAnnotation",
|
||||||
|
id: COMMENT_FILE_LINKS_SLOT_ID,
|
||||||
|
displayName: "File Links",
|
||||||
|
exportName: "CommentFileLinks",
|
||||||
|
entityTypes: ["comment"],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "commentContextMenuItem",
|
||||||
|
id: COMMENT_OPEN_FILES_SLOT_ID,
|
||||||
|
displayName: "Open in Files",
|
||||||
|
exportName: "CommentOpenFiles",
|
||||||
|
entityTypes: ["comment"],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
export default manifest;
|
||||||
@@ -0,0 +1,815 @@
|
|||||||
|
import type {
|
||||||
|
PluginProjectSidebarItemProps,
|
||||||
|
PluginDetailTabProps,
|
||||||
|
PluginCommentAnnotationProps,
|
||||||
|
PluginCommentContextMenuItemProps,
|
||||||
|
} from "@paperclipai/plugin-sdk/ui";
|
||||||
|
import { usePluginAction, usePluginData } from "@paperclipai/plugin-sdk/ui";
|
||||||
|
import { useMemo, useState, useEffect, useRef, type MouseEvent, type RefObject } from "react";
|
||||||
|
import { EditorView } from "@codemirror/view";
|
||||||
|
import { basicSetup } from "codemirror";
|
||||||
|
import { javascript } from "@codemirror/lang-javascript";
|
||||||
|
import { HighlightStyle, syntaxHighlighting } from "@codemirror/language";
|
||||||
|
import { tags } from "@lezer/highlight";
|
||||||
|
|
||||||
|
const PLUGIN_KEY = "paperclip-file-browser-example";
|
||||||
|
const FILES_TAB_SLOT_ID = "files-tab";
|
||||||
|
|
||||||
|
const editorBaseTheme = {
|
||||||
|
"&": {
|
||||||
|
height: "100%",
|
||||||
|
},
|
||||||
|
".cm-scroller": {
|
||||||
|
overflow: "auto",
|
||||||
|
fontFamily:
|
||||||
|
"ui-monospace, SFMono-Regular, SF Mono, Menlo, Monaco, Consolas, Liberation Mono, monospace",
|
||||||
|
fontSize: "13px",
|
||||||
|
lineHeight: "1.6",
|
||||||
|
},
|
||||||
|
".cm-content": {
|
||||||
|
padding: "12px 14px 18px",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
const editorDarkTheme = EditorView.theme({
|
||||||
|
...editorBaseTheme,
|
||||||
|
"&": {
|
||||||
|
...editorBaseTheme["&"],
|
||||||
|
backgroundColor: "oklch(0.23 0.02 255)",
|
||||||
|
color: "oklch(0.93 0.01 255)",
|
||||||
|
},
|
||||||
|
".cm-gutters": {
|
||||||
|
backgroundColor: "oklch(0.25 0.015 255)",
|
||||||
|
color: "oklch(0.74 0.015 255)",
|
||||||
|
borderRight: "1px solid oklch(0.34 0.01 255)",
|
||||||
|
},
|
||||||
|
".cm-activeLine, .cm-activeLineGutter": {
|
||||||
|
backgroundColor: "oklch(0.30 0.012 255 / 0.55)",
|
||||||
|
},
|
||||||
|
".cm-selectionBackground, .cm-content ::selection": {
|
||||||
|
backgroundColor: "oklch(0.42 0.02 255 / 0.45)",
|
||||||
|
},
|
||||||
|
"&.cm-focused .cm-selectionBackground": {
|
||||||
|
backgroundColor: "oklch(0.47 0.025 255 / 0.5)",
|
||||||
|
},
|
||||||
|
".cm-cursor, .cm-dropCursor": {
|
||||||
|
borderLeftColor: "oklch(0.93 0.01 255)",
|
||||||
|
},
|
||||||
|
".cm-matchingBracket": {
|
||||||
|
backgroundColor: "oklch(0.37 0.015 255 / 0.5)",
|
||||||
|
color: "oklch(0.95 0.01 255)",
|
||||||
|
outline: "none",
|
||||||
|
},
|
||||||
|
".cm-nonmatchingBracket": {
|
||||||
|
color: "oklch(0.70 0.08 24)",
|
||||||
|
},
|
||||||
|
}, { dark: true });
|
||||||
|
|
||||||
|
const editorLightTheme = EditorView.theme({
|
||||||
|
...editorBaseTheme,
|
||||||
|
"&": {
|
||||||
|
...editorBaseTheme["&"],
|
||||||
|
backgroundColor: "color-mix(in oklab, var(--card) 92%, var(--background))",
|
||||||
|
color: "var(--foreground)",
|
||||||
|
},
|
||||||
|
".cm-content": {
|
||||||
|
...editorBaseTheme[".cm-content"],
|
||||||
|
caretColor: "var(--foreground)",
|
||||||
|
},
|
||||||
|
".cm-gutters": {
|
||||||
|
backgroundColor: "color-mix(in oklab, var(--card) 96%, var(--background))",
|
||||||
|
color: "var(--muted-foreground)",
|
||||||
|
borderRight: "1px solid var(--border)",
|
||||||
|
},
|
||||||
|
".cm-activeLine, .cm-activeLineGutter": {
|
||||||
|
backgroundColor: "color-mix(in oklab, var(--accent) 52%, transparent)",
|
||||||
|
},
|
||||||
|
".cm-selectionBackground, .cm-content ::selection": {
|
||||||
|
backgroundColor: "color-mix(in oklab, var(--accent) 72%, transparent)",
|
||||||
|
},
|
||||||
|
"&.cm-focused .cm-selectionBackground": {
|
||||||
|
backgroundColor: "color-mix(in oklab, var(--accent) 84%, transparent)",
|
||||||
|
},
|
||||||
|
".cm-cursor, .cm-dropCursor": {
|
||||||
|
borderLeftColor: "color-mix(in oklab, var(--foreground) 88%, transparent)",
|
||||||
|
},
|
||||||
|
".cm-matchingBracket": {
|
||||||
|
backgroundColor: "color-mix(in oklab, var(--accent) 45%, transparent)",
|
||||||
|
color: "var(--foreground)",
|
||||||
|
outline: "none",
|
||||||
|
},
|
||||||
|
".cm-nonmatchingBracket": {
|
||||||
|
color: "var(--destructive)",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const editorDarkHighlightStyle = HighlightStyle.define([
|
||||||
|
{ tag: tags.keyword, color: "oklch(0.78 0.025 265)" },
|
||||||
|
{ tag: [tags.name, tags.variableName], color: "oklch(0.88 0.01 255)" },
|
||||||
|
{ tag: [tags.string, tags.special(tags.string)], color: "oklch(0.80 0.02 170)" },
|
||||||
|
{ tag: [tags.number, tags.bool, tags.null], color: "oklch(0.79 0.02 95)" },
|
||||||
|
{ tag: [tags.comment, tags.lineComment, tags.blockComment], color: "oklch(0.64 0.01 255)" },
|
||||||
|
{ tag: [tags.function(tags.variableName), tags.labelName], color: "oklch(0.84 0.018 220)" },
|
||||||
|
{ tag: [tags.typeName, tags.className], color: "oklch(0.82 0.02 245)" },
|
||||||
|
{ tag: [tags.operator, tags.punctuation], color: "oklch(0.77 0.01 255)" },
|
||||||
|
{ tag: [tags.invalid, tags.deleted], color: "oklch(0.70 0.08 24)" },
|
||||||
|
]);
|
||||||
|
|
||||||
|
const editorLightHighlightStyle = HighlightStyle.define([
|
||||||
|
{ tag: tags.keyword, color: "oklch(0.45 0.07 270)" },
|
||||||
|
{ tag: [tags.name, tags.variableName], color: "oklch(0.28 0.01 255)" },
|
||||||
|
{ tag: [tags.string, tags.special(tags.string)], color: "oklch(0.45 0.06 165)" },
|
||||||
|
{ tag: [tags.number, tags.bool, tags.null], color: "oklch(0.48 0.08 90)" },
|
||||||
|
{ tag: [tags.comment, tags.lineComment, tags.blockComment], color: "oklch(0.53 0.01 255)" },
|
||||||
|
{ tag: [tags.function(tags.variableName), tags.labelName], color: "oklch(0.42 0.07 220)" },
|
||||||
|
{ tag: [tags.typeName, tags.className], color: "oklch(0.40 0.06 245)" },
|
||||||
|
{ tag: [tags.operator, tags.punctuation], color: "oklch(0.36 0.01 255)" },
|
||||||
|
{ tag: [tags.invalid, tags.deleted], color: "oklch(0.55 0.16 24)" },
|
||||||
|
]);
|
||||||
|
|
||||||
|
type Workspace = { id: string; projectId: string; name: string; path: string; isPrimary: boolean };
|
||||||
|
type FileEntry = { name: string; path: string; isDirectory: boolean };
|
||||||
|
type FileTreeNodeProps = {
|
||||||
|
entry: FileEntry;
|
||||||
|
companyId: string | null;
|
||||||
|
projectId: string;
|
||||||
|
workspaceId: string;
|
||||||
|
selectedPath: string | null;
|
||||||
|
onSelect: (path: string) => void;
|
||||||
|
depth?: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const PathLikePattern = /[\\/]/;
|
||||||
|
const WindowsDrivePathPattern = /^[A-Za-z]:[\\/]/;
|
||||||
|
const UuidPattern = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
||||||
|
|
||||||
|
function isLikelyPath(pathValue: string): boolean {
|
||||||
|
const trimmed = pathValue.trim();
|
||||||
|
return PathLikePattern.test(trimmed) || WindowsDrivePathPattern.test(trimmed);
|
||||||
|
}
|
||||||
|
|
||||||
|
function workspaceLabel(workspace: Workspace): string {
|
||||||
|
const pathLabel = workspace.path.trim();
|
||||||
|
const nameLabel = workspace.name.trim();
|
||||||
|
const hasPathLabel = isLikelyPath(pathLabel) && !UuidPattern.test(pathLabel);
|
||||||
|
const hasNameLabel = nameLabel.length > 0 && !UuidPattern.test(nameLabel);
|
||||||
|
const baseLabel = hasPathLabel ? pathLabel : hasNameLabel ? nameLabel : "";
|
||||||
|
if (!baseLabel) {
|
||||||
|
return workspace.isPrimary ? "(no workspace path) (primary)" : "(no workspace path)";
|
||||||
|
}
|
||||||
|
|
||||||
|
return workspace.isPrimary ? `${baseLabel} (primary)` : baseLabel;
|
||||||
|
}
|
||||||
|
|
||||||
|
function useIsMobile(breakpointPx = 768): boolean {
|
||||||
|
const [isMobile, setIsMobile] = useState(() =>
|
||||||
|
typeof window !== "undefined" ? window.innerWidth < breakpointPx : false,
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof window === "undefined") return;
|
||||||
|
const mediaQuery = window.matchMedia(`(max-width: ${breakpointPx - 1}px)`);
|
||||||
|
const update = () => setIsMobile(mediaQuery.matches);
|
||||||
|
update();
|
||||||
|
mediaQuery.addEventListener("change", update);
|
||||||
|
return () => mediaQuery.removeEventListener("change", update);
|
||||||
|
}, [breakpointPx]);
|
||||||
|
|
||||||
|
return isMobile;
|
||||||
|
}
|
||||||
|
|
||||||
|
function useIsDarkMode(): boolean {
|
||||||
|
const [isDarkMode, setIsDarkMode] = useState(() =>
|
||||||
|
typeof document !== "undefined" && document.documentElement.classList.contains("dark"),
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof document === "undefined") return;
|
||||||
|
const root = document.documentElement;
|
||||||
|
const update = () => setIsDarkMode(root.classList.contains("dark"));
|
||||||
|
update();
|
||||||
|
|
||||||
|
const observer = new MutationObserver(update);
|
||||||
|
observer.observe(root, { attributes: true, attributeFilter: ["class"] });
|
||||||
|
return () => observer.disconnect();
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return isDarkMode;
|
||||||
|
}
|
||||||
|
|
||||||
|
function useAvailableHeight(
|
||||||
|
ref: RefObject<HTMLElement | null>,
|
||||||
|
options?: { bottomPadding?: number; minHeight?: number },
|
||||||
|
): number | null {
|
||||||
|
const bottomPadding = options?.bottomPadding ?? 24;
|
||||||
|
const minHeight = options?.minHeight ?? 384;
|
||||||
|
const [height, setHeight] = useState<number | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof window === "undefined") return;
|
||||||
|
|
||||||
|
const update = () => {
|
||||||
|
const element = ref.current;
|
||||||
|
if (!element) return;
|
||||||
|
const rect = element.getBoundingClientRect();
|
||||||
|
const nextHeight = Math.max(minHeight, Math.floor(window.innerHeight - rect.top - bottomPadding));
|
||||||
|
setHeight(nextHeight);
|
||||||
|
};
|
||||||
|
|
||||||
|
update();
|
||||||
|
window.addEventListener("resize", update);
|
||||||
|
window.addEventListener("orientationchange", update);
|
||||||
|
|
||||||
|
const observer = typeof ResizeObserver !== "undefined"
|
||||||
|
? new ResizeObserver(() => update())
|
||||||
|
: null;
|
||||||
|
if (observer && ref.current) observer.observe(ref.current);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("resize", update);
|
||||||
|
window.removeEventListener("orientationchange", update);
|
||||||
|
observer?.disconnect();
|
||||||
|
};
|
||||||
|
}, [bottomPadding, minHeight, ref]);
|
||||||
|
|
||||||
|
return height;
|
||||||
|
}
|
||||||
|
|
||||||
|
function FileTreeNode({
|
||||||
|
entry,
|
||||||
|
companyId,
|
||||||
|
projectId,
|
||||||
|
workspaceId,
|
||||||
|
selectedPath,
|
||||||
|
onSelect,
|
||||||
|
depth = 0,
|
||||||
|
}: FileTreeNodeProps) {
|
||||||
|
const [isExpanded, setIsExpanded] = useState(false);
|
||||||
|
const isSelected = selectedPath === entry.path;
|
||||||
|
|
||||||
|
if (entry.isDirectory) {
|
||||||
|
return (
|
||||||
|
<li>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="flex w-full items-center gap-2 rounded-none px-2 py-1.5 text-left text-sm text-foreground hover:bg-accent/60"
|
||||||
|
style={{ paddingLeft: `${depth * 14 + 8}px` }}
|
||||||
|
onClick={() => setIsExpanded((value) => !value)}
|
||||||
|
aria-expanded={isExpanded}
|
||||||
|
>
|
||||||
|
<span className="w-3 text-xs text-muted-foreground">{isExpanded ? "▾" : "▸"}</span>
|
||||||
|
<span className="truncate font-medium">{entry.name}</span>
|
||||||
|
</button>
|
||||||
|
{isExpanded ? (
|
||||||
|
<ExpandedDirectoryChildren
|
||||||
|
directoryPath={entry.path}
|
||||||
|
companyId={companyId}
|
||||||
|
projectId={projectId}
|
||||||
|
workspaceId={workspaceId}
|
||||||
|
selectedPath={selectedPath}
|
||||||
|
onSelect={onSelect}
|
||||||
|
depth={depth}
|
||||||
|
/>
|
||||||
|
) : null}
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<li>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className={`block w-full rounded-none px-2 py-1.5 text-left text-sm transition-colors ${
|
||||||
|
isSelected ? "bg-accent text-foreground" : "text-muted-foreground hover:bg-accent/50 hover:text-foreground"
|
||||||
|
}`}
|
||||||
|
style={{ paddingLeft: `${depth * 14 + 23}px` }}
|
||||||
|
onClick={() => onSelect(entry.path)}
|
||||||
|
>
|
||||||
|
<span className="truncate">{entry.name}</span>
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function ExpandedDirectoryChildren({
|
||||||
|
directoryPath,
|
||||||
|
companyId,
|
||||||
|
projectId,
|
||||||
|
workspaceId,
|
||||||
|
selectedPath,
|
||||||
|
onSelect,
|
||||||
|
depth,
|
||||||
|
}: {
|
||||||
|
directoryPath: string;
|
||||||
|
companyId: string | null;
|
||||||
|
projectId: string;
|
||||||
|
workspaceId: string;
|
||||||
|
selectedPath: string | null;
|
||||||
|
onSelect: (path: string) => void;
|
||||||
|
depth: number;
|
||||||
|
}) {
|
||||||
|
const { data: childData } = usePluginData<{ entries: FileEntry[] }>("fileList", {
|
||||||
|
companyId,
|
||||||
|
projectId,
|
||||||
|
workspaceId,
|
||||||
|
directoryPath,
|
||||||
|
});
|
||||||
|
const children = childData?.entries ?? [];
|
||||||
|
|
||||||
|
if (children.length === 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<ul className="space-y-0.5">
|
||||||
|
{children.map((child) => (
|
||||||
|
<FileTreeNode
|
||||||
|
key={child.path}
|
||||||
|
entry={child}
|
||||||
|
companyId={companyId}
|
||||||
|
projectId={projectId}
|
||||||
|
workspaceId={workspaceId}
|
||||||
|
selectedPath={selectedPath}
|
||||||
|
onSelect={onSelect}
|
||||||
|
depth={depth + 1}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project sidebar item: link "Files" that opens the project detail with the Files plugin tab.
|
||||||
|
*/
|
||||||
|
export function FilesLink({ context }: PluginProjectSidebarItemProps) {
|
||||||
|
const { data: config, loading: configLoading } = usePluginData<PluginConfig>("plugin-config", {});
|
||||||
|
const showFilesInSidebar = config?.showFilesInSidebar ?? false;
|
||||||
|
|
||||||
|
if (configLoading || !showFilesInSidebar) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const projectId = context.entityId;
|
||||||
|
const projectRef = (context as PluginProjectSidebarItemProps["context"] & { projectRef?: string | null })
|
||||||
|
.projectRef
|
||||||
|
?? projectId;
|
||||||
|
const prefix = context.companyPrefix ? `/${context.companyPrefix}` : "";
|
||||||
|
const tabValue = `plugin:${PLUGIN_KEY}:${FILES_TAB_SLOT_ID}`;
|
||||||
|
const href = `${prefix}/projects/${projectRef}?tab=${encodeURIComponent(tabValue)}`;
|
||||||
|
const isActive = typeof window !== "undefined" && (() => {
|
||||||
|
const pathname = window.location.pathname.replace(/\/+$/, "");
|
||||||
|
const segments = pathname.split("/").filter(Boolean);
|
||||||
|
const projectsIndex = segments.indexOf("projects");
|
||||||
|
const activeProjectRef = projectsIndex >= 0 ? segments[projectsIndex + 1] ?? null : null;
|
||||||
|
const activeTab = new URLSearchParams(window.location.search).get("tab");
|
||||||
|
if (activeTab !== tabValue) return false;
|
||||||
|
if (!activeProjectRef) return false;
|
||||||
|
return activeProjectRef === projectId || activeProjectRef === projectRef;
|
||||||
|
})();
|
||||||
|
|
||||||
|
const handleClick = (event: MouseEvent<HTMLAnchorElement>) => {
|
||||||
|
if (
|
||||||
|
event.defaultPrevented
|
||||||
|
|| event.button !== 0
|
||||||
|
|| event.metaKey
|
||||||
|
|| event.ctrlKey
|
||||||
|
|| event.altKey
|
||||||
|
|| event.shiftKey
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
window.history.pushState({}, "", href);
|
||||||
|
window.dispatchEvent(new PopStateEvent("popstate"));
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<a
|
||||||
|
href={href}
|
||||||
|
onClick={handleClick}
|
||||||
|
aria-current={isActive ? "page" : undefined}
|
||||||
|
className={`block px-3 py-1 text-[12px] truncate transition-colors ${
|
||||||
|
isActive
|
||||||
|
? "bg-accent text-foreground font-medium"
|
||||||
|
: "text-muted-foreground hover:text-foreground hover:bg-accent/50"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
Files
|
||||||
|
</a>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Project detail tab: workspace selector, file tree, and CodeMirror editor.
|
||||||
|
*/
|
||||||
|
export function FilesTab({ context }: PluginDetailTabProps) {
|
||||||
|
const companyId = context.companyId;
|
||||||
|
const projectId = context.entityId;
|
||||||
|
const isMobile = useIsMobile();
|
||||||
|
const isDarkMode = useIsDarkMode();
|
||||||
|
const panesRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
const availableHeight = useAvailableHeight(panesRef, {
|
||||||
|
bottomPadding: isMobile ? 16 : 24,
|
||||||
|
minHeight: isMobile ? 320 : 420,
|
||||||
|
});
|
||||||
|
const { data: workspacesData } = usePluginData<Workspace[]>("workspaces", {
|
||||||
|
projectId,
|
||||||
|
companyId,
|
||||||
|
});
|
||||||
|
const workspaces = workspacesData ?? [];
|
||||||
|
const workspaceSelectKey = workspaces.map((w) => `${w.id}:${workspaceLabel(w)}`).join("|");
|
||||||
|
const [workspaceId, setWorkspaceId] = useState<string | null>(null);
|
||||||
|
const resolvedWorkspaceId = workspaceId ?? workspaces[0]?.id ?? null;
|
||||||
|
const selectedWorkspace = useMemo(
|
||||||
|
() => workspaces.find((w) => w.id === resolvedWorkspaceId) ?? null,
|
||||||
|
[workspaces, resolvedWorkspaceId],
|
||||||
|
);
|
||||||
|
|
||||||
|
const fileListParams = useMemo(
|
||||||
|
() => (selectedWorkspace ? { projectId, companyId, workspaceId: selectedWorkspace.id } : {}),
|
||||||
|
[companyId, projectId, selectedWorkspace],
|
||||||
|
);
|
||||||
|
const { data: fileListData, loading: fileListLoading } = usePluginData<{ entries: FileEntry[] }>(
|
||||||
|
"fileList",
|
||||||
|
fileListParams,
|
||||||
|
);
|
||||||
|
const entries = fileListData?.entries ?? [];
|
||||||
|
|
||||||
|
// Track the `?file=` query parameter across navigations (popstate).
|
||||||
|
const [urlFilePath, setUrlFilePath] = useState<string | null>(() => {
|
||||||
|
if (typeof window === "undefined") return null;
|
||||||
|
return new URLSearchParams(window.location.search).get("file") || null;
|
||||||
|
});
|
||||||
|
const lastConsumedFileRef = useRef<string | null>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (typeof window === "undefined") return;
|
||||||
|
const onNav = () => {
|
||||||
|
const next = new URLSearchParams(window.location.search).get("file") || null;
|
||||||
|
setUrlFilePath(next);
|
||||||
|
};
|
||||||
|
window.addEventListener("popstate", onNav);
|
||||||
|
return () => window.removeEventListener("popstate", onNav);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
const [selectedPath, setSelectedPath] = useState<string | null>(null);
|
||||||
|
useEffect(() => {
|
||||||
|
setSelectedPath(null);
|
||||||
|
setMobileView("browser");
|
||||||
|
lastConsumedFileRef.current = null;
|
||||||
|
}, [selectedWorkspace?.id]);
|
||||||
|
|
||||||
|
// When a file path appears (or changes) in the URL and workspace is ready, select it.
|
||||||
|
useEffect(() => {
|
||||||
|
if (!urlFilePath || !selectedWorkspace) return;
|
||||||
|
if (lastConsumedFileRef.current === urlFilePath) return;
|
||||||
|
lastConsumedFileRef.current = urlFilePath;
|
||||||
|
setSelectedPath(urlFilePath);
|
||||||
|
setMobileView("editor");
|
||||||
|
}, [urlFilePath, selectedWorkspace]);
|
||||||
|
|
||||||
|
const fileContentParams = useMemo(
|
||||||
|
() =>
|
||||||
|
selectedPath && selectedWorkspace
|
||||||
|
? { projectId, companyId, workspaceId: selectedWorkspace.id, filePath: selectedPath }
|
||||||
|
: null,
|
||||||
|
[companyId, projectId, selectedWorkspace, selectedPath],
|
||||||
|
);
|
||||||
|
const fileContentResult = usePluginData<{ content: string | null; error?: string }>(
|
||||||
|
"fileContent",
|
||||||
|
fileContentParams ?? {},
|
||||||
|
);
|
||||||
|
const { data: fileContentData, refresh: refreshFileContent } = fileContentResult;
|
||||||
|
const writeFile = usePluginAction("writeFile");
|
||||||
|
const editorRef = useRef<HTMLDivElement | null>(null);
|
||||||
|
const viewRef = useRef<EditorView | null>(null);
|
||||||
|
const loadedContentRef = useRef("");
|
||||||
|
const [isDirty, setIsDirty] = useState(false);
|
||||||
|
const [isSaving, setIsSaving] = useState(false);
|
||||||
|
const [saveMessage, setSaveMessage] = useState<string | null>(null);
|
||||||
|
const [saveError, setSaveError] = useState<string | null>(null);
|
||||||
|
const [mobileView, setMobileView] = useState<"browser" | "editor">("browser");
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!editorRef.current) return;
|
||||||
|
const content = fileContentData?.content ?? "";
|
||||||
|
loadedContentRef.current = content;
|
||||||
|
setIsDirty(false);
|
||||||
|
setSaveMessage(null);
|
||||||
|
setSaveError(null);
|
||||||
|
if (viewRef.current) {
|
||||||
|
viewRef.current.destroy();
|
||||||
|
viewRef.current = null;
|
||||||
|
}
|
||||||
|
const view = new EditorView({
|
||||||
|
doc: content,
|
||||||
|
extensions: [
|
||||||
|
basicSetup,
|
||||||
|
javascript(),
|
||||||
|
isDarkMode ? editorDarkTheme : editorLightTheme,
|
||||||
|
syntaxHighlighting(isDarkMode ? editorDarkHighlightStyle : editorLightHighlightStyle),
|
||||||
|
EditorView.updateListener.of((update) => {
|
||||||
|
if (!update.docChanged) return;
|
||||||
|
const nextValue = update.state.doc.toString();
|
||||||
|
setIsDirty(nextValue !== loadedContentRef.current);
|
||||||
|
setSaveMessage(null);
|
||||||
|
setSaveError(null);
|
||||||
|
}),
|
||||||
|
],
|
||||||
|
parent: editorRef.current,
|
||||||
|
});
|
||||||
|
viewRef.current = view;
|
||||||
|
return () => {
|
||||||
|
view.destroy();
|
||||||
|
viewRef.current = null;
|
||||||
|
};
|
||||||
|
}, [fileContentData?.content, selectedPath, isDarkMode]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleKeydown = (event: KeyboardEvent) => {
|
||||||
|
if (!(event.metaKey || event.ctrlKey) || event.key.toLowerCase() !== "s") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!selectedWorkspace || !selectedPath || !isDirty || isSaving) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.preventDefault();
|
||||||
|
void handleSave();
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("keydown", handleKeydown);
|
||||||
|
return () => window.removeEventListener("keydown", handleKeydown);
|
||||||
|
}, [selectedWorkspace, selectedPath, isDirty, isSaving]);
|
||||||
|
|
||||||
|
async function handleSave() {
|
||||||
|
if (!selectedWorkspace || !selectedPath || !viewRef.current) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const content = viewRef.current.state.doc.toString();
|
||||||
|
setIsSaving(true);
|
||||||
|
setSaveError(null);
|
||||||
|
setSaveMessage(null);
|
||||||
|
try {
|
||||||
|
await writeFile({
|
||||||
|
projectId,
|
||||||
|
companyId,
|
||||||
|
workspaceId: selectedWorkspace.id,
|
||||||
|
filePath: selectedPath,
|
||||||
|
content,
|
||||||
|
});
|
||||||
|
loadedContentRef.current = content;
|
||||||
|
setIsDirty(false);
|
||||||
|
setSaveMessage("Saved");
|
||||||
|
refreshFileContent();
|
||||||
|
} catch (error) {
|
||||||
|
setSaveError(error instanceof Error ? error.message : String(error));
|
||||||
|
} finally {
|
||||||
|
setIsSaving(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="space-y-4">
|
||||||
|
<div className="rounded-lg border border-border bg-card p-4">
|
||||||
|
<label className="text-sm font-medium text-muted-foreground">Workspace</label>
|
||||||
|
<select
|
||||||
|
key={workspaceSelectKey}
|
||||||
|
className="mt-2 block w-full rounded-md border border-input bg-background px-3 py-2 text-sm"
|
||||||
|
value={resolvedWorkspaceId ?? ""}
|
||||||
|
onChange={(e) => setWorkspaceId(e.target.value || null)}
|
||||||
|
>
|
||||||
|
{workspaces.map((w) => {
|
||||||
|
const label = workspaceLabel(w);
|
||||||
|
return (
|
||||||
|
<option key={`${w.id}:${label}`} value={w.id} label={label} title={label}>
|
||||||
|
{label}
|
||||||
|
</option>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div
|
||||||
|
ref={panesRef}
|
||||||
|
className="min-h-0"
|
||||||
|
style={{
|
||||||
|
display: isMobile ? "block" : "grid",
|
||||||
|
gap: "1rem",
|
||||||
|
gridTemplateColumns: isMobile ? undefined : "320px minmax(0, 1fr)",
|
||||||
|
height: availableHeight ? `${availableHeight}px` : undefined,
|
||||||
|
minHeight: isMobile ? "20rem" : "26rem",
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<div
|
||||||
|
className="flex min-h-0 flex-col overflow-hidden rounded-lg border border-border bg-card"
|
||||||
|
style={{ display: isMobile && mobileView === "editor" ? "none" : "flex" }}
|
||||||
|
>
|
||||||
|
<div className="border-b border-border px-3 py-2 text-xs font-medium uppercase tracking-[0.12em] text-muted-foreground">
|
||||||
|
File Tree
|
||||||
|
</div>
|
||||||
|
<div className="min-h-0 flex-1 overflow-auto p-2">
|
||||||
|
{selectedWorkspace ? (
|
||||||
|
fileListLoading ? (
|
||||||
|
<p className="px-2 py-3 text-sm text-muted-foreground">Loading files...</p>
|
||||||
|
) : entries.length > 0 ? (
|
||||||
|
<ul className="space-y-0.5">
|
||||||
|
{entries.map((entry) => (
|
||||||
|
<FileTreeNode
|
||||||
|
key={entry.path}
|
||||||
|
entry={entry}
|
||||||
|
companyId={companyId}
|
||||||
|
projectId={projectId}
|
||||||
|
workspaceId={selectedWorkspace.id}
|
||||||
|
selectedPath={selectedPath}
|
||||||
|
onSelect={(path) => {
|
||||||
|
setSelectedPath(path);
|
||||||
|
setMobileView("editor");
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
) : (
|
||||||
|
<p className="px-2 py-3 text-sm text-muted-foreground">No files found in this workspace.</p>
|
||||||
|
)
|
||||||
|
) : (
|
||||||
|
<p className="px-2 py-3 text-sm text-muted-foreground">Select a workspace to browse files.</p>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div
|
||||||
|
className="flex min-h-0 flex-col overflow-hidden rounded-lg border border-border bg-card"
|
||||||
|
style={{ display: isMobile && mobileView === "browser" ? "none" : "flex" }}
|
||||||
|
>
|
||||||
|
<div className="sticky top-0 z-10 flex items-center justify-between gap-3 border-b border-border bg-card px-4 py-2">
|
||||||
|
<div className="min-w-0">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="mb-2 inline-flex rounded-md border border-input bg-background px-2 py-1 text-xs font-medium text-muted-foreground"
|
||||||
|
style={{ display: isMobile ? "inline-flex" : "none" }}
|
||||||
|
onClick={() => setMobileView("browser")}
|
||||||
|
>
|
||||||
|
Back to files
|
||||||
|
</button>
|
||||||
|
<div className="text-xs font-medium uppercase tracking-[0.12em] text-muted-foreground">Editor</div>
|
||||||
|
<div className="truncate text-sm text-foreground">{selectedPath ?? "No file selected"}</div>
|
||||||
|
</div>
|
||||||
|
<div className="flex items-center gap-3">
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="rounded-md border border-input bg-background px-3 py-1.5 text-sm font-medium text-foreground transition-colors hover:bg-accent disabled:cursor-not-allowed disabled:opacity-50"
|
||||||
|
disabled={!selectedWorkspace || !selectedPath || !isDirty || isSaving}
|
||||||
|
onClick={() => void handleSave()}
|
||||||
|
>
|
||||||
|
{isSaving ? "Saving..." : "Save"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
{isDirty || saveMessage || saveError ? (
|
||||||
|
<div className="border-b border-border px-4 py-2 text-xs">
|
||||||
|
{saveError ? (
|
||||||
|
<span className="text-destructive">{saveError}</span>
|
||||||
|
) : saveMessage ? (
|
||||||
|
<span className="text-emerald-600">{saveMessage}</span>
|
||||||
|
) : (
|
||||||
|
<span className="text-muted-foreground">Unsaved changes</span>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
{selectedPath && fileContentData?.error && fileContentData.error !== "Missing file context" ? (
|
||||||
|
<div className="border-b border-border px-4 py-2 text-xs text-destructive">{fileContentData.error}</div>
|
||||||
|
) : null}
|
||||||
|
<div ref={editorRef} className="min-h-0 flex-1 overflow-auto overscroll-contain" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Comment Annotation: renders detected file links below each comment
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
type PluginConfig = {
|
||||||
|
showFilesInSidebar?: boolean;
|
||||||
|
commentAnnotationMode: "annotation" | "contextMenu" | "both" | "none";
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Per-comment annotation showing file-path-like links extracted from the
|
||||||
|
* comment body. Each link navigates to the project Files tab with the
|
||||||
|
* matching path pre-selected.
|
||||||
|
*
|
||||||
|
* Respects the `commentAnnotationMode` instance config — hidden when mode
|
||||||
|
* is `"contextMenu"` or `"none"`.
|
||||||
|
*/
|
||||||
|
function buildFileBrowserHref(prefix: string, projectId: string | null, filePath: string): string {
|
||||||
|
if (!projectId) return "#";
|
||||||
|
const tabValue = `plugin:${PLUGIN_KEY}:${FILES_TAB_SLOT_ID}`;
|
||||||
|
return `${prefix}/projects/${projectId}?tab=${encodeURIComponent(tabValue)}&file=${encodeURIComponent(filePath)}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
function navigateToFileBrowser(href: string, event: MouseEvent<HTMLAnchorElement>) {
|
||||||
|
if (
|
||||||
|
event.defaultPrevented
|
||||||
|
|| event.button !== 0
|
||||||
|
|| event.metaKey
|
||||||
|
|| event.ctrlKey
|
||||||
|
|| event.altKey
|
||||||
|
|| event.shiftKey
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
event.preventDefault();
|
||||||
|
window.history.pushState({}, "", href);
|
||||||
|
window.dispatchEvent(new PopStateEvent("popstate"));
|
||||||
|
}
|
||||||
|
|
||||||
|
export function CommentFileLinks({ context }: PluginCommentAnnotationProps) {
|
||||||
|
const { data: config } = usePluginData<PluginConfig>("plugin-config", {});
|
||||||
|
const mode = config?.commentAnnotationMode ?? "both";
|
||||||
|
|
||||||
|
const { data } = usePluginData<{ links: string[] }>("comment-file-links", {
|
||||||
|
commentId: context.entityId,
|
||||||
|
issueId: context.parentEntityId,
|
||||||
|
companyId: context.companyId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (mode === "contextMenu" || mode === "none") return null;
|
||||||
|
if (!data?.links?.length) return null;
|
||||||
|
|
||||||
|
const prefix = context.companyPrefix ? `/${context.companyPrefix}` : "";
|
||||||
|
const projectId = context.projectId;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex flex-wrap items-center gap-1.5">
|
||||||
|
<span className="text-[10px] font-medium uppercase tracking-wider text-muted-foreground">Files:</span>
|
||||||
|
{data.links.map((link) => {
|
||||||
|
const href = buildFileBrowserHref(prefix, projectId, link);
|
||||||
|
return (
|
||||||
|
<a
|
||||||
|
key={link}
|
||||||
|
href={href}
|
||||||
|
onClick={(e) => navigateToFileBrowser(href, e)}
|
||||||
|
className="inline-flex items-center rounded-md border border-border bg-accent/30 px-1.5 py-0.5 text-xs font-mono text-primary hover:bg-accent/60 hover:underline transition-colors"
|
||||||
|
title={`Open ${link} in file browser`}
|
||||||
|
>
|
||||||
|
{link}
|
||||||
|
</a>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Comment Context Menu Item: "Open in Files" action per comment
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Per-comment context menu item that appears in the comment "more" (⋮) menu.
|
||||||
|
* Extracts file paths from the comment body and, if any are found, renders
|
||||||
|
* a button to open the first file in the project Files tab.
|
||||||
|
*
|
||||||
|
* Respects the `commentAnnotationMode` instance config — hidden when mode
|
||||||
|
* is `"annotation"` or `"none"`.
|
||||||
|
*/
|
||||||
|
export function CommentOpenFiles({ context }: PluginCommentContextMenuItemProps) {
|
||||||
|
const { data: config } = usePluginData<PluginConfig>("plugin-config", {});
|
||||||
|
const mode = config?.commentAnnotationMode ?? "both";
|
||||||
|
|
||||||
|
const { data } = usePluginData<{ links: string[] }>("comment-file-links", {
|
||||||
|
commentId: context.entityId,
|
||||||
|
issueId: context.parentEntityId,
|
||||||
|
companyId: context.companyId,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (mode === "annotation" || mode === "none") return null;
|
||||||
|
if (!data?.links?.length) return null;
|
||||||
|
|
||||||
|
const prefix = context.companyPrefix ? `/${context.companyPrefix}` : "";
|
||||||
|
const projectId = context.projectId;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<div className="px-2 py-1 text-[10px] font-medium uppercase tracking-wider text-muted-foreground">
|
||||||
|
Files
|
||||||
|
</div>
|
||||||
|
{data.links.map((link) => {
|
||||||
|
const href = buildFileBrowserHref(prefix, projectId, link);
|
||||||
|
const fileName = link.split("/").pop() ?? link;
|
||||||
|
return (
|
||||||
|
<a
|
||||||
|
key={link}
|
||||||
|
href={href}
|
||||||
|
onClick={(e) => navigateToFileBrowser(href, e)}
|
||||||
|
className="flex w-full items-center gap-2 rounded px-2 py-1 text-xs text-foreground hover:bg-accent transition-colors"
|
||||||
|
title={`Open ${link} in file browser`}
|
||||||
|
>
|
||||||
|
<span className="truncate font-mono">{fileName}</span>
|
||||||
|
</a>
|
||||||
|
);
|
||||||
|
})}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,226 @@
|
|||||||
|
import { definePlugin, runWorker } from "@paperclipai/plugin-sdk";
|
||||||
|
import * as fs from "node:fs";
|
||||||
|
import * as path from "node:path";
|
||||||
|
|
||||||
|
const PLUGIN_NAME = "file-browser-example";
|
||||||
|
const UUID_PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
||||||
|
const PATH_LIKE_PATTERN = /[\\/]/;
|
||||||
|
const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
|
||||||
|
|
||||||
|
function looksLikePath(value: string): boolean {
|
||||||
|
const normalized = value.trim();
|
||||||
|
return (PATH_LIKE_PATTERN.test(normalized) || WINDOWS_DRIVE_PATH_PATTERN.test(normalized))
|
||||||
|
&& !UUID_PATTERN.test(normalized);
|
||||||
|
}
|
||||||
|
|
||||||
|
function sanitizeWorkspacePath(pathValue: string): string {
|
||||||
|
return looksLikePath(pathValue) ? pathValue.trim() : "";
|
||||||
|
}
|
||||||
|
|
||||||
|
function resolveWorkspace(workspacePath: string, requestedPath?: string): string | null {
|
||||||
|
const root = path.resolve(workspacePath);
|
||||||
|
const resolved = requestedPath ? path.resolve(root, requestedPath) : root;
|
||||||
|
const relative = path.relative(root, resolved);
|
||||||
|
if (relative.startsWith("..") || path.isAbsolute(relative)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return resolved;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Regex that matches file-path-like tokens in comment text.
|
||||||
|
* Captures tokens that either start with `.` `/` `~` or contain a `/`
|
||||||
|
* (directory separator), plus bare words that could be filenames with
|
||||||
|
* extensions (e.g. `README.md`). The file-extension check in
|
||||||
|
* `extractFilePaths` filters out non-file matches.
|
||||||
|
*/
|
||||||
|
const FILE_PATH_REGEX = /(?:^|[\s(`"'])([^\s,;)}`"'>\]]*\/[^\s,;)}`"'>\]]+|[.\/~][^\s,;)}`"'>\]]+|[a-zA-Z0-9_-]+\.[a-zA-Z0-9]{1,10}(?:\/[^\s,;)}`"'>\]]+)?)/g;
|
||||||
|
|
||||||
|
/** Common file extensions to recognise path-like tokens as actual file references. */
|
||||||
|
const FILE_EXTENSION_REGEX = /\.[a-zA-Z0-9]{1,10}$/;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tokens that look like paths but are almost certainly URL route segments
|
||||||
|
* (e.g. `/projects/abc`, `/settings`, `/dashboard`).
|
||||||
|
*/
|
||||||
|
const URL_ROUTE_PATTERN = /^\/(?:projects|issues|agents|settings|dashboard|plugins|api|auth|admin)\b/i;
|
||||||
|
|
||||||
|
function extractFilePaths(body: string): string[] {
|
||||||
|
const paths = new Set<string>();
|
||||||
|
for (const match of body.matchAll(FILE_PATH_REGEX)) {
|
||||||
|
const raw = match[1];
|
||||||
|
// Strip trailing punctuation that isn't part of a path
|
||||||
|
const cleaned = raw.replace(/[.:,;!?)]+$/, "");
|
||||||
|
if (cleaned.length <= 1) continue;
|
||||||
|
// Must have a file extension (e.g. .ts, .json, .md)
|
||||||
|
if (!FILE_EXTENSION_REGEX.test(cleaned)) continue;
|
||||||
|
// Skip things that look like URL routes
|
||||||
|
if (URL_ROUTE_PATTERN.test(cleaned)) continue;
|
||||||
|
paths.add(cleaned);
|
||||||
|
}
|
||||||
|
return [...paths];
|
||||||
|
}
|
||||||
|
|
||||||
|
const plugin = definePlugin({
|
||||||
|
async setup(ctx) {
|
||||||
|
ctx.logger.info(`${PLUGIN_NAME} plugin setup`);
|
||||||
|
|
||||||
|
// Expose the current plugin config so UI components can read operator
|
||||||
|
// settings from the canonical instance config store.
|
||||||
|
ctx.data.register("plugin-config", async () => {
|
||||||
|
const config = await ctx.config.get();
|
||||||
|
return {
|
||||||
|
showFilesInSidebar: config?.showFilesInSidebar === true,
|
||||||
|
commentAnnotationMode: config?.commentAnnotationMode ?? "both",
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Fetch a comment by ID and extract file-path-like tokens from its body.
|
||||||
|
ctx.data.register("comment-file-links", async (params: Record<string, unknown>) => {
|
||||||
|
const commentId = typeof params.commentId === "string" ? params.commentId : "";
|
||||||
|
const issueId = typeof params.issueId === "string" ? params.issueId : "";
|
||||||
|
const companyId = typeof params.companyId === "string" ? params.companyId : "";
|
||||||
|
if (!commentId || !issueId || !companyId) return { links: [] };
|
||||||
|
try {
|
||||||
|
const comments = await ctx.issues.listComments(issueId, companyId);
|
||||||
|
const comment = comments.find((c) => c.id === commentId);
|
||||||
|
if (!comment?.body) return { links: [] };
|
||||||
|
return { links: extractFilePaths(comment.body) };
|
||||||
|
} catch (err) {
|
||||||
|
ctx.logger.warn("Failed to fetch comment for file link extraction", { commentId, error: String(err) });
|
||||||
|
return { links: [] };
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.data.register("workspaces", async (params: Record<string, unknown>) => {
|
||||||
|
const projectId = params.projectId as string;
|
||||||
|
const companyId = typeof params.companyId === "string" ? params.companyId : "";
|
||||||
|
if (!projectId || !companyId) return [];
|
||||||
|
const workspaces = await ctx.projects.listWorkspaces(projectId, companyId);
|
||||||
|
return workspaces.map((w) => ({
|
||||||
|
id: w.id,
|
||||||
|
projectId: w.projectId,
|
||||||
|
name: w.name,
|
||||||
|
path: sanitizeWorkspacePath(w.path),
|
||||||
|
isPrimary: w.isPrimary,
|
||||||
|
}));
|
||||||
|
});
|
||||||
|
|
||||||
|
ctx.data.register(
|
||||||
|
"fileList",
|
||||||
|
async (params: Record<string, unknown>) => {
|
||||||
|
const projectId = params.projectId as string;
|
||||||
|
const companyId = typeof params.companyId === "string" ? params.companyId : "";
|
||||||
|
const workspaceId = params.workspaceId as string;
|
||||||
|
const directoryPath = typeof params.directoryPath === "string" ? params.directoryPath : "";
|
||||||
|
if (!projectId || !companyId || !workspaceId) return { entries: [] };
|
||||||
|
const workspaces = await ctx.projects.listWorkspaces(projectId, companyId);
|
||||||
|
const workspace = workspaces.find((w) => w.id === workspaceId);
|
||||||
|
if (!workspace) return { entries: [] };
|
||||||
|
const workspacePath = sanitizeWorkspacePath(workspace.path);
|
||||||
|
if (!workspacePath) return { entries: [] };
|
||||||
|
const dirPath = resolveWorkspace(workspacePath, directoryPath);
|
||||||
|
if (!dirPath) {
|
||||||
|
return { entries: [] };
|
||||||
|
}
|
||||||
|
if (!fs.existsSync(dirPath) || !fs.statSync(dirPath).isDirectory()) {
|
||||||
|
return { entries: [] };
|
||||||
|
}
|
||||||
|
const names = fs.readdirSync(dirPath).sort((a, b) => a.localeCompare(b));
|
||||||
|
const entries = names.map((name) => {
|
||||||
|
const full = path.join(dirPath, name);
|
||||||
|
const stat = fs.lstatSync(full);
|
||||||
|
const relativePath = path.relative(workspacePath, full);
|
||||||
|
return {
|
||||||
|
name,
|
||||||
|
path: relativePath,
|
||||||
|
isDirectory: stat.isDirectory(),
|
||||||
|
};
|
||||||
|
}).sort((a, b) => {
|
||||||
|
if (a.isDirectory !== b.isDirectory) return a.isDirectory ? -1 : 1;
|
||||||
|
return a.name.localeCompare(b.name);
|
||||||
|
});
|
||||||
|
return { entries };
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.data.register(
|
||||||
|
"fileContent",
|
||||||
|
async (params: Record<string, unknown>) => {
|
||||||
|
const projectId = params.projectId as string;
|
||||||
|
const companyId = typeof params.companyId === "string" ? params.companyId : "";
|
||||||
|
const workspaceId = params.workspaceId as string;
|
||||||
|
const filePath = params.filePath as string;
|
||||||
|
if (!projectId || !companyId || !workspaceId || !filePath) {
|
||||||
|
return { content: null, error: "Missing file context" };
|
||||||
|
}
|
||||||
|
const workspaces = await ctx.projects.listWorkspaces(projectId, companyId);
|
||||||
|
const workspace = workspaces.find((w) => w.id === workspaceId);
|
||||||
|
if (!workspace) return { content: null, error: "Workspace not found" };
|
||||||
|
const workspacePath = sanitizeWorkspacePath(workspace.path);
|
||||||
|
if (!workspacePath) return { content: null, error: "Workspace has no path" };
|
||||||
|
const fullPath = resolveWorkspace(workspacePath, filePath);
|
||||||
|
if (!fullPath) {
|
||||||
|
return { content: null, error: "Path outside workspace" };
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const content = fs.readFileSync(fullPath, "utf-8");
|
||||||
|
return { content };
|
||||||
|
} catch (err) {
|
||||||
|
const message = err instanceof Error ? err.message : String(err);
|
||||||
|
return { content: null, error: message };
|
||||||
|
}
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
ctx.actions.register(
|
||||||
|
"writeFile",
|
||||||
|
async (params: Record<string, unknown>) => {
|
||||||
|
const projectId = params.projectId as string;
|
||||||
|
const companyId = typeof params.companyId === "string" ? params.companyId : "";
|
||||||
|
const workspaceId = params.workspaceId as string;
|
||||||
|
const filePath = typeof params.filePath === "string" ? params.filePath.trim() : "";
|
||||||
|
if (!filePath) {
|
||||||
|
throw new Error("filePath must be a non-empty string");
|
||||||
|
}
|
||||||
|
const content = typeof params.content === "string" ? params.content : null;
|
||||||
|
if (!projectId || !companyId || !workspaceId) {
|
||||||
|
throw new Error("Missing workspace context");
|
||||||
|
}
|
||||||
|
const workspaces = await ctx.projects.listWorkspaces(projectId, companyId);
|
||||||
|
const workspace = workspaces.find((w) => w.id === workspaceId);
|
||||||
|
if (!workspace) {
|
||||||
|
throw new Error("Workspace not found");
|
||||||
|
}
|
||||||
|
const workspacePath = sanitizeWorkspacePath(workspace.path);
|
||||||
|
if (!workspacePath) {
|
||||||
|
throw new Error("Workspace has no path");
|
||||||
|
}
|
||||||
|
if (content === null) {
|
||||||
|
throw new Error("Missing file content");
|
||||||
|
}
|
||||||
|
const fullPath = resolveWorkspace(workspacePath, filePath);
|
||||||
|
if (!fullPath) {
|
||||||
|
throw new Error("Path outside workspace");
|
||||||
|
}
|
||||||
|
const stat = fs.statSync(fullPath);
|
||||||
|
if (!stat.isFile()) {
|
||||||
|
throw new Error("Selected path is not a file");
|
||||||
|
}
|
||||||
|
fs.writeFileSync(fullPath, content, "utf-8");
|
||||||
|
return {
|
||||||
|
ok: true,
|
||||||
|
path: filePath,
|
||||||
|
bytes: Buffer.byteLength(content, "utf-8"),
|
||||||
|
};
|
||||||
|
},
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
async onHealth() {
|
||||||
|
return { status: "ok", message: `${PLUGIN_NAME} ready` };
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export default plugin;
|
||||||
|
runWorker(plugin, import.meta.url);
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../../../tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "dist",
|
||||||
|
"rootDir": "src",
|
||||||
|
"lib": ["ES2023", "DOM"],
|
||||||
|
"jsx": "react-jsx"
|
||||||
|
},
|
||||||
|
"include": ["src"]
|
||||||
|
}
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
# @paperclipai/plugin-hello-world-example
|
||||||
|
|
||||||
|
First-party reference plugin showing the smallest possible UI extension.
|
||||||
|
|
||||||
|
## What It Demonstrates
|
||||||
|
|
||||||
|
- a manifest with a `dashboardWidget` UI slot
|
||||||
|
- `entrypoints.ui` wiring for plugin UI bundles
|
||||||
|
- a minimal React widget rendered in the Paperclip dashboard
|
||||||
|
- reading host context (`companyId`) from `PluginWidgetProps`
|
||||||
|
- worker lifecycle hooks (`setup`, `onHealth`) for basic runtime observability
|
||||||
|
|
||||||
|
## API Surface
|
||||||
|
|
||||||
|
- This example does not add custom HTTP endpoints.
|
||||||
|
- The widget is discovered/rendered through host-managed plugin APIs (for example `GET /api/plugins/ui-contributions`).
|
||||||
|
|
||||||
|
## Notes
|
||||||
|
|
||||||
|
This is intentionally simple and is designed as the quickest "hello world" starting point for UI plugin authors.
|
||||||
|
It is a repo-local example plugin for development, not a plugin that should be assumed to ship in generic production builds.
|
||||||
|
|
||||||
|
## Local Install (Dev)
|
||||||
|
|
||||||
|
From the repo root, build the plugin and install it by local path:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pnpm --filter @paperclipai/plugin-hello-world-example build
|
||||||
|
pnpm paperclipai plugin install ./packages/plugins/examples/plugin-hello-world-example
|
||||||
|
```
|
||||||
|
|
||||||
|
**Local development notes:**
|
||||||
|
|
||||||
|
- **Build first.** The host resolves the worker from the manifest `entrypoints.worker` (e.g. `./dist/worker.js`). Run `pnpm build` in the plugin directory before installing so the worker file exists.
|
||||||
|
- **Dev-only install path.** This local-path install flow assumes a source checkout with this example package present on disk. For deployed installs, publish an npm package instead of relying on the monorepo example path.
|
||||||
|
- **Reinstall after pulling.** If you installed a plugin by local path before the server stored `package_path`, the plugin may show status **error** (worker not found). Uninstall and install again so the server persists the path and can activate the plugin:
|
||||||
|
`pnpm paperclipai plugin uninstall paperclip.hello-world-example --force` then
|
||||||
|
`pnpm paperclipai plugin install ./packages/plugins/examples/plugin-hello-world-example`.
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user