diff --git a/.agents/skills/gh-stack/SKILL.md b/.agents/skills/gh-stack/SKILL.md new file mode 100644 index 000000000..61efdd35a --- /dev/null +++ b/.agents/skills/gh-stack/SKILL.md @@ -0,0 +1,891 @@ +--- +description: | + Manage stacked branches and pull requests with the gh-stack GitHub CLI extension. Use when the user wants to create, push, rebase, sync, navigate, or view stacks of dependent PRs. Triggers on tasks involving stacked diffs, dependent pull requests, branch chains, or incremental code review workflows. +metadata: + author: github + github-path: skills/gh-stack + github-ref: refs/tags/v0.1.0 + github-repo: https://github.com/github/gh-stack + github-tree-sha: c95c8b5b4dd850f3fef007b304428f5684f2fb87 + version: 0.0.9 +name: gh-stack +--- +# gh-stack + +`gh stack` is a [GitHub CLI](https://cli.github.com/) extension for managing **stacked branches and pull requests**. A stack is an ordered list of branches where each branch builds on the one below it, rooted on a trunk branch (typically the repo's default branch). Each branch maps to one PR whose base is the branch below it, so reviewers see only the diff for that layer. + +```text +main (trunk) + └── auth-layer → PR #1 (base: main) - bottom (closest to trunk) + └── api-endpoints → PR #2 (base: auth-layer) + └── frontend → PR #3 (base: api-endpoints) - top (furthest from trunk) +``` + +The **bottom** of the stack is the branch closest to the trunk, and the **top** is the branch furthest from the trunk. Each branch inherits from the one below it. Navigation commands (`up`, `down`, `top`, `bottom`) follow this model: `up` moves away from trunk, `down` moves toward it. + +## When to use this skill + +Use this skill when the user wants to: + +- Break a large change into a chain of small, reviewable PRs +- Create, rebase, push, or sync a stack of dependent branches +- Navigate between layers of a branch stack +- View the status of stacked PRs +- Tear down and rebuild a stack to remove, reorder, or rename branches + +## Prerequisites + +The GitHub CLI (`gh`) v2.0+ must be installed and authenticated. Install the extension with: + +```bash +gh extension install github/gh-stack +``` + +Before using `gh stack`, configure git to prevent interactive prompts: + +```bash +git config rerere.enabled true # remember conflict resolutions (skips prompt on init) +git config remote.pushDefault origin # if multiple remotes exist (skips remote picker) +``` + +## Agent rules + +**All `gh stack` commands must be run non-interactively.** Every command invocation must include the flags and positional arguments needed to avoid prompts, TUIs, and interactive menus. If a command would prompt for input, it will hang indefinitely. + +1. **Always supply branch names as positional arguments** to `init`, `add`, and `checkout`. Running these commands without arguments triggers interactive prompts. Branch names are used exactly as given — a name is never prefixed or transformed, so `gh stack add refactor/foo` creates a branch named `refactor/foo`. +2. **Always use `--auto` with `gh stack submit`** to skip the full-screen editor. Without `--auto`, `submit` opens the editor for each PR's title, body, and draft/ready status. +3. **Always use `--json` with `gh stack view`.** Without `--json`, the command launches an interactive TUI that cannot be operated by agents. There is no other appropriate flag — always pass `--json`. +4. **Handle multiple remotes.** If more than one remote is configured, pre-configure `git config remote.pushDefault origin`, or pass `--remote ` to the commands that accept it: `push`, `submit`, `sync`, `rebase`, and `link`. `checkout`, `modify`, and `trunk` resolve a remote but have **no `--remote` flag** — they rely on `remote.pushDefault`. With multiple remotes and no configured default, these commands exit with an error in non-interactive mode. +5. **Avoid branches shared across multiple stacks.** If a branch belongs to multiple stacks, commands exit with code 6. Check out a non-shared branch first. +6. **Plan your stack layers by dependency order before writing code.** Foundational changes (models, APIs, shared utilities) go in lower branches; dependent changes (UI, consumers) go in higher branches. Think through the dependency chain before running `gh stack init`. +7. **Use standard `git add` and `git commit` for staging and committing.** This gives you full control over which changes go into each branch. The `-Am` shortcut is available but should not be the default approach—stacked PRs are most effective when each branch contains a deliberate, logical set of changes. +8. **Navigate down the stack when you need to change a lower layer.** If you're working on a frontend branch and realize you need API changes, don't hack around it at the current layer. Navigate to the appropriate branch (`gh stack down`, `gh stack checkout`, or `gh stack bottom`), make and commit the changes there, run `gh stack rebase --upstack`, then navigate back up to continue. +9. **Use `gh stack link` for external tool workflows.** When branches are managed by an external tool (jj, Sapling, etc.), use `gh stack link branch-a branch-b`. `link` does not rely on local tracking state and is intended for API-driven PR and stack management. Provide at least two branches/PRs to create or update a stack, or a stack number followed by the new branches/PRs to append them to the top of an existing stack (e.g. `gh stack link 7 branch-c`). +10. **Use `gh stack merge --yes` to merge stacked PRs.** `gh pr merge` does not work with stacked PRs. In a non-interactive terminal `gh stack merge` runs without prompting and merges the entire stack (bottom to top) atomically; pass `--yes` to be explicit. Scope the merge by passing a pull request number (`gh stack merge 42 --yes` merges everything up to and including PR #42) or a stack number (`gh stack merge 7 --yes`, which needs no local checkout). Choose the method with `--squash`, `--rebase`, `--merge`, or `--merge-method `; without one, the last-used method is used. The merge is all-or-nothing — if any PR can't be merged, none are, and the failure reason is reported. Only basic pull request state is checked before merging (open and not a draft); bypassing merge requirements is not supported for stacks. If the base branch uses a merge queue, the stack is added to the queue instead of merging directly: the queue chooses the merge method (any method you pass is ignored with a warning), and the pull requests are added to the queue together but merge as the queue processes them, so they may land in separate groups rather than all at once. + +**Never do any of the following — each triggers an interactive prompt or TUI that will hang:** +- ❌ `gh stack view` or `gh stack view --short` — always use `gh stack view --json` +- ❌ `gh stack submit` without `--auto` — always use `gh stack submit --auto` +- ❌ `gh stack init` without branch arguments — always provide branch names +- ❌ `gh stack add` without a branch name — always provide a branch name +- ❌ `gh stack checkout` without an argument — always provide a PR number or branch name +- ❌ `gh stack checkout ` when a different local stack already exists on those branches — this triggers an unbypassable conflict resolution prompt; use `gh stack unstack --local` first to remove the local tracking state (this keeps the stack on GitHub intact), then retry the checkout + +## Thinking about stack structure + +Each branch in a stack should represent a **discrete, logical unit of work** that can be reviewed independently. The changes within a branch should be cohesive—they belong together and make sense as a single PR. + +### Dependency chain + +Stacked branches form a dependency chain: each branch builds on the one below it. This means **foundational changes must go in lower (earlier) branches**, and code that depends on them goes in higher (later) branches. + +**Plan your layers before writing code.** For example, a full-stack feature might be structured like this (use branch names relevant to your actual task, not these generic ones): + +```text +main (trunk) + └── data-models ← shared types, database schema + └── api-endpoints ← API routes that use the models + └── frontend-ui ← UI components that call the APIs + └── integration ← tests that exercise the full stack +``` + +This is illustrative — choose branch names and layer boundaries that reflect the specific work you're doing. The key principle is: if code in one layer depends on code in another, the dependency must be in the same branch or a lower one. + +### Branch naming + +Choose a clear, descriptive branch name for each layer that reflects the concern it contains (e.g., `auth`, `api-routes`, `frontend`). Branch names are used exactly as you provide them to `init` and `add` — nothing is prepended or transformed. Slashes are allowed and are treated as part of the name (e.g., `gh stack add refactor/foo` creates a branch named `refactor/foo`). + +### Staging changes deliberately + +The main reason to use `git add` and `git commit` directly is to control **which changes go into which branch**. When you have multiple files in your working tree, you can stage a subset for the current branch, commit them, then create a new branch and stage the rest there: + +```bash +# You're on data-models with several new files in your working tree. +# Stage only the model files for this branch: +git add internal/models/user.go internal/models/session.go +git commit -m "Add user and session models" + +git add db/migrations/001_create_users.sql +git commit -m "Add user table migration" + +# Now create a new branch for the API layer and stage the API files there: +gh stack add api-routes # created & switched to the api-routes branch +git add internal/api/routes.go internal/api/handlers.go +git commit -m "Add user API routes" +``` + +This keeps each branch focused on one concern. Multiple commits per branch are fine — the key is that all commits in a branch relate to the same logical concern, and changes that belong to a different concern go in a different branch. + +### When to create a new branch + +Create a new branch (`gh stack add`) when you're starting a **different concern** that depends on what you've built so far. Signs it's time for a new branch: + +- You're switching from backend to frontend work +- You're moving from core logic to tests or documentation +- The next set of changes has a different reviewer audience +- The current branch's PR is already large enough to review + +### One stack, one story + +Think of a stack from the reviewer's perspective: the stack of PRs should **tell a cohesive story** about a feature or project. A reviewer should be able to read the PRs in sequence and understand the progression of changes, with each PR being a small, logical piece of the whole. + +**When to use a single stack:** All the branches are part of the same feature, project, or closely related effort. Even if the work spans multiple concerns (models, API, frontend), they're all building toward the same goal. + +**When to create a separate stack:** The work is unrelated to your current stack — a different feature, a bug fix in an unrelated area, or an independent refactor. Don't mix unrelated work into a single stack just because you happen to be working on both. Start a new stack with `gh stack init` or switch to an existing stack with `gh stack checkout` for each distinct effort. + +Small, incidental fixes (e.g., fixing a typo you noticed) can go in the current stack if they're trivial. But if a change grows into its own project, it deserves its own stack. + +## Quick reference + +| Task | Command | +|------|---------| +| Create a stack | `gh stack init auth` | +| Create a stack of multiple branches | `gh stack init auth api frontend` | +| Adopt existing branches | `gh stack init existing-branch-a existing-branch-b` | +| Set custom trunk | `gh stack init --base develop branch-a` | +| Add a branch to stack | `gh stack add api-routes` | +| Add branch + stage all + commit | `gh stack add -Am "message" api-routes` | +| Push branches to remote | `gh stack push` | +| Push to specific remote | `gh stack push --remote origin` | +| Push branches + create draft PRs | `gh stack submit --auto` | +| Create PRs as ready for review | `gh stack submit --auto --open` | +| Sync (fetch, rebase, push) | `gh stack sync` | +| Sync with specific remote | `gh stack sync --remote origin` | +| Sync and prune merged branches | `gh stack sync --prune` | +| Rebase entire stack | `gh stack rebase` | +| Rebase upstack only | `gh stack rebase --upstack` | +| Rebase without trunk | `gh stack rebase --no-trunk` | +| Continue after conflict | `gh stack rebase --continue` | +| Abort rebase | `gh stack rebase --abort` | +| View stack details (JSON) | `gh stack view --json` | +| Switch branches up/down in stack | `gh stack up [n]` / `gh stack down [n]` | +| Switch to top/bottom branch | `gh stack top` / `gh stack bottom` | +| Check out by stack number | `gh stack checkout 7` | +| Check out by PR | `gh stack checkout 42` | +| Check out by branch (local only) | `gh stack checkout feature-auth` | +| Tear down the current stack to restructure it | `gh stack unstack` | +| Tear down a specific stack by number | `gh stack unstack 7` | +| Merge the whole current stack | `gh stack merge --yes` | +| Merge a stack by number | `gh stack merge 7 --yes` | +| Merge up to a specific PR | `gh stack merge 42 --yes` | +| Merge with a specific method | `gh stack merge --yes --squash` | + +--- + +## Workflows + +### End-to-end: create a stack from scratch + +```bash +# 1. Initialize a stack with the first branch +gh stack init auth +# → creates auth and checks it out + +# 2. Write code for the first layer (auth) +cat > auth.go << 'EOF' +package auth + +func Middleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + // verify token + next.ServeHTTP(w, r) + }) +} +EOF + +# 3. Stage and commit using standard git commands +git add auth.go +git commit -m "Add auth middleware" + +# You can make multiple commits on the same branch +cat > auth_test.go << 'EOF' +package auth + +func TestMiddleware(t *testing.T) { + // test auth middleware +} +EOF +git add auth_test.go +git commit -m "Add auth middleware tests" + +# 4. When you're ready for a new concern, add the next branch +gh stack add api-routes +# → creates api-routes + +# 5. Write code for the API layer +cat > api.go << 'EOF' +package api + +func RegisterRoutes(mux *http.ServeMux) { + mux.HandleFunc("/users", handleUsers) +} +EOF +git add api.go +git commit -m "Add API routes" + +# 6. Add a third layer for frontend +gh stack add frontend +# → creates frontend + +cat > frontend.go << 'EOF' +package frontend + +func RenderDashboard(w http.ResponseWriter) { + // calls the API endpoints from the layer below +} +EOF +git add frontend.go +git commit -m "Add frontend dashboard" + +# ── Stack complete: auth → api-routes → frontend ── + +# 7. Push everything and create PRs (drafts by default) +gh stack submit --auto + +# 8. Verify the stack +gh stack view --json +``` + +> **Shortcut:** If you prefer a faster flow, `gh stack add -Am "message" branch-name` combines staging, committing, and branch creation into one command. This is useful for single-commit layers but bypasses deliberate staging. + +### Making mid-stack changes + +This is a critical workflow for agents. When you're working on a higher layer and realize you need to change something in a lower layer (e.g., you're building frontend components but need to add an API endpoint), **navigate down to the correct branch, make the change there, and rebase**. + +```bash +# You're on frontend but need to add an API endpoint + +# 1. Navigate to the API branch +gh stack down +# or: gh stack checkout api-routes + +# 2. Make the change where it belongs +cat > users_api.go << 'EOF' +package api + +func handleGetUser(w http.ResponseWriter, r *http.Request) { + // new endpoint the frontend needs +} +EOF +git add users_api.go +git commit -m "Add get-user endpoint" + +# 3. Rebase everything above to pick up the change +gh stack rebase --upstack + +# 4. Navigate back to where you were working +gh stack top +# or: gh stack checkout frontend + +# 5. Continue working — the API changes are now available +``` + +**Why this matters:** If you make API changes on the frontend branch, those changes will end up in the wrong PR. The API PR won't include them, and the frontend PR will have unrelated API diffs mixed in. Always put changes in the branch where they logically belong. + +### Modify a mid-stack branch and sync + +When you need to revisit a branch after the initial creation (e.g., responding to review feedback): + +```bash +# 1. Navigate to the branch that needs changes +gh stack bottom +# or: gh stack checkout auth +# or: gh stack checkout 42 (by PR number) + +# 2. Make changes and commit +cat > auth.go << 'EOF' +package auth +// updated implementation +EOF +git add auth.go +git commit -m "Fix auth token validation" + +# 3. Rebase everything above this branch +gh stack rebase --upstack + +# 4. Push the updated stack +gh stack push +``` + +### Routine sync after merges + +```bash +# Single command: fetch, rebase, push, sync PR and stack state +gh stack sync + +# Sync and automatically clean up local branches for merged PRs +gh stack sync --prune +``` + +> **Note for agents:** In non-interactive environments, the prune prompt is not shown. Use `--prune` explicitly to delete local branches for merged PRs. +> **Note for agents:** `sync` also mirrors the stack on GitHub locally. If PRs were added to the stack on GitHub, their branches are pulled down and appended to the local stack automatically. If the local and remote stacks have **diverged** (you changed the local stack while the remote stack changed differently), sync can only prompt to resolve it in an interactive terminal — in non-interactive environments it aborts the sync (nothing is pushed or updated) and exits successfully with `ℹ Sync aborted`. Automation must inspect stderr for that message and stop instead of treating the exit code as success. For a flow that continues, validate the current stack with `gh stack view --json`; resolve the divergence by unstacking and recreating the stack. Before running `gh stack init` again, confirm that the GitHub stack has disappeared. If any PR is queued or has auto-merge enabled, `unstack` may leave the GitHub stack and local tracking in place; if all PRs are affected it can return HTTP 422. Remove the PRs from the merge queue or disable auto-merge, rerun `gh stack unstack`, and only then initialize the stack. + +### Squash-merge recovery + +When a PR is squash-merged on GitHub, the original branch's commits no longer exist in the trunk history. `gh stack` detects this automatically and uses `git rebase --onto` to correctly replay remaining commits. + +```bash +# After PR #1 (auth) is squash-merged on GitHub: +gh stack sync +# → fetches latest, detects the merge, fast-forwards trunk +# → rebases api-routes onto updated trunk (skips merged branch) +# → rebases frontend onto api-routes +# → pushes updated branches +# → reports: "Merged: #1" + +# Verify the result +gh stack view --json +# → auth shows "isMerged": true, "state": "MERGED" +# → api-routes and frontend show updated heads +``` + +If `sync` hits a conflict during this process, it restores all branches to their pre-rebase state and exits with code 3. See [Handle rebase conflicts](#handle-rebase-conflicts-agent-workflow) for the resolution workflow. + +### Handle rebase conflicts (agent workflow) + +```bash +# 1. Start the rebase +gh stack rebase + +# 2. If exit code 3 (conflict): +# - Parse stderr for conflicted file paths +# - Read those files to find <<<<<<< / ======= / >>>>>>> markers +# - Edit files to resolve conflicts +# - Stage resolved files: +git add path/to/resolved-file.go + +# 3. Continue the rebase +gh stack rebase --continue + +# 4. If another conflict occurs, repeat steps 2-3 + +# 5. If unable to resolve, abort to restore everything +gh stack rebase --abort +``` + +### Parsing `--json` output + +```bash +# Get stack state as JSON +output=$(gh stack view --json) + +# Check if any branch needs a rebase, and rebase if so +needs_rebase=$(echo "$output" | jq '[.branches[] | select(.needsRebase == true)] | length') +if [ "$needs_rebase" -gt 0 ]; then + echo "Branches need rebase, rebasing stack..." + gh stack rebase +fi + +# Get all open PR URLs +echo "$output" | jq -r '.branches[] | select(.pr.state == "OPEN") | .pr.url' + +# Find merged branches +echo "$output" | jq -r '.branches[] | select(.isMerged == true) | .name' + +# Get the current branch +echo "$output" | jq -r '.currentBranch' + +# Check if the stack is fully merged (all branches merged) +echo "$output" | jq '[.branches[] | .isMerged] | all' +``` + +### Restructure a stack (remove a branch, reorder, or rename) + +Use `unstack` to tear down the stack, make structural changes, then re-init: + +```bash +# 1. Remove the local tracking and the GitHub stack grouping (PRs are NOT deleted) +gh stack unstack + +# 2. Make structural changes — e.g. delete a branch, reorder, rename +git branch -m old-branch-1 new-branch-1 + +# 3. Re-create the stack with the new structure +gh stack init --base main new-branch-1 new-branch-2 new-branch-3 +``` + +--- + +## Commands + +### Initialize a stack — `gh stack init` + +Creates a new stack. **Always provide at least one branch name as a positional argument** — running without branch arguments triggers interactive prompts that agents cannot use. + +```text +gh stack init [flags] +``` + +```bash +# Create a stack with a new branch +gh stack init auth +# → creates auth and checks it out + +# Create a stack with new branches +gh stack init branch-a branch-b branch-c + +# Use a different trunk branch +gh stack init --base develop branch-a branch-b + +# Adopt existing branches into a stack (handled automatically if the branches exist) +gh stack init branch-a branch-b branch-c +``` + +| Flag | Description | +|------|-------------| +| `-b, --base ` | Trunk branch (defaults to the repo's default branch) | + +**Behavior:** + +- Branch names are created exactly as given (slashes are allowed and kept as-is) +- Creates any branches that don't already exist (branching from the trunk branch) +- Existing branches are adopted automatically; missing branches are created from the trunk +- Checks out the last branch in the list +- Enables `git rerere` so conflict resolutions are remembered across rebases. On first run in a repo, this may trigger a confirmation prompt — pre-configure with `git config rerere.enabled true` to avoid it + +--- + +### Add a branch — `gh stack add` + +Add a new branch on top of the current stack. Must be run while on the topmost branch (or the trunk if the stack has no branches yet). **Always provide a branch name** — running without one triggers an interactive prompt. + +```text +gh stack add [flags] +``` + +**Recommended workflow — create the branch, then use standard git:** + +```bash +# Create a new branch and switch to it +gh stack add api-routes + +# Write code, stage deliberately, and commit +git add internal/api/routes.go internal/api/handlers.go +git commit -m "Add user API routes" + +# Make more commits on the same branch as needed +git add internal/api/middleware.go +git commit -m "Add rate limiting middleware" +``` + +**Shortcut — stage, commit, and branch in one command:** + +```bash +# Create a new branch, stage all changes, and commit +gh stack add -Am "Add API routes" api-routes + +# Create a new branch, stage tracked files only, and commit +gh stack add -um "Fix auth bug" auth-fix +``` + +| Flag | Description | +|------|-------------| +| `-m, --message ` | Create a commit with this message | +| `-A, --all` | Stage all changes including untracked files (requires `-m`) | +| `-u, --update` | Stage tracked files only (requires `-m`) | + +**Behavior notes:** + +- `-A` and `-u` are mutually exclusive. +- When the current branch has no commits (e.g., right after `init`), `add -Am` commits directly on the current branch instead of creating a new one. +- **Branch names are used verbatim.** `gh stack add refactor/foo` creates a branch named `refactor/foo` — names are never prefixed or transformed. When `-m` is given without a branch name, the name is auto-generated from the commit message in date+slug format (e.g., `03-24-add_api_routes`). +- If called from a branch that is not the topmost in the stack, exits with code 5: `"can only add branches on top of the stack"`. Use `gh stack top` to switch first. +- **Uncommitted changes:** When using `gh stack add branch-name` without `-Am`, any uncommitted changes (staged or unstaged) in your working tree carry over to the new branch. This is standard git behavior — the working tree is not touched. Commit or stash changes on the current branch before running `add` if you want a clean starting point on the new branch. + +--- + +### Push branches to remote — `gh stack push` + +Push active stack branches to the remote. + +```text +gh stack push [flags] +``` + +```bash +# Push all branches +gh stack push + +# Push to specific remote +gh stack push --remote upstream +``` + +| Flag | Description | +|------|-------------| +| `--remote ` | Remote to push to (use if multiple remotes exist) | + +**Behavior:** + +- Pushes all active (non-merged, non-queued) branches in one non-atomic multi-ref push with explicit per-branch `--force-with-lease` checks +- Some branches may update if another is rejected; fix the rejected branch and rerun the command +- Does **not** create or update pull requests — use `gh stack submit` for that + +**Output (stderr):** + +- `Pushed N branches` summary + +--- + +### Submit branches and create PRs — `gh stack submit` + +Push all stack branches and create PRs on GitHub. **Always pass `--auto`** — without it, `submit` prompts for a PR title for each new branch. + +```bash +# Submit and auto-title new PRs (required for non-interactive use) +gh stack submit --auto + +# Submit and create PRs as ready for review (not drafts) +gh stack submit --auto --open +``` + +| Flag | Description | +|------|-------------| +| `--auto` | Auto-generate PR titles without prompting (**required** for non-interactive use) | +| `--open` | Mark new and existing PRs as ready for review | +| `--remote ` | Remote to push to (use if multiple remotes exist) | + +**Behavior:** + +- Pushes each active (non-merged, non-queued) branch sequentially with explicit per-branch `--force-with-lease` checks; the overall submit is not atomic +- If a later branch push is rejected, earlier branch pushes and PR updates remain; fix the rejection and rerun the same command +- Creates a new PR for each branch that doesn't have one (base set to the first non-merged ancestor branch) +- After creating PRs, links them together as a **Stack** on GitHub (requires the repository to have stacks enabled) +- If every PR in the stack has already been merged, the stack is complete and can't be extended. `submit` automatically forks your unmerged branches into a **new** stack rooted at the trunk and creates it on GitHub, leaving the merged stack untouched. +- If stacks are not available (exit code 9), the repository does not have stacked PRs enabled. In interactive mode, `submit` offers to create regular (unstacked) PRs instead. In non-interactive mode, it exits with code 9. +- Syncs PR metadata for branches that already have PRs + +**PR title auto-generation (`--auto`):** + +- Single commit on branch → uses the commit subject as the PR title, commit body as PR body +- Multiple commits on branch → humanizes the branch name (hyphens/underscores → spaces) as the title + +**Output (stderr):** + +- `Created PR #N for ` for each newly created PR +- `PR #N for is up to date` for existing PRs +- `Pushed and synced N branches` summary + +--- + +### Link branches as a stack (no local tracking) — `gh stack link` + +Link PRs into a stack on GitHub without creating any local tracking state. This is the recommended approach if you are managing stacked branches with other tools (jj, Sapling, git-town) and want to simply create GitHub Stacked PRs via an API. + +```text +gh stack link [flags] [...] +``` + +```bash +# Link branches into a stack (pushes, creates PRs, creates stack) +gh stack link branch-a branch-b branch-c + +# Use a different base branch and mark PRs as ready for review +gh stack link --base develop --open branch-a branch-b branch-c + +# Link existing PRs by number +gh stack link 10 20 30 + +# Add branches to an existing stack of PRs +gh stack link 42 43 feature-auth feature-ui + +# Append to the top of an existing stack by its stack number +# (7 is a stack number; only the new PRs/branches are listed) +gh stack link 7 48 feature-auth +``` + +When the first argument is a stack number, the remaining arguments are appended to the top of that stack, so you don't have to re-list its current PRs. Arguments already in the stack are skipped; arguments in a different stack are rejected. A numeric first argument is treated as a stack only when it matches an existing stack — otherwise it is a PR or branch. + +| Flag | Description | +|------|---------| +| `--base ` | Base branch for the bottom of the stack (defaults to the repository's default branch) | +| `--open` | Mark new and existing PRs as ready for review | +| `--remote ` | Remote to push to (use if multiple remotes exist) | + +**Behavior:** + +- Arguments are provided in stack order (bottom to top) +- Each argument can be a branch name or a PR number. Numeric arguments are tried as PR numbers first; if no PR with that number exists, the argument is treated as a branch name +- Branch arguments are pushed to the remote automatically (non-force, atomic) +- For branches without open PRs, new PRs are created with auto-generated titles and the correct base branch chaining (first branch uses `--base`, subsequent branches use the previous branch) +- Existing PRs whose base branch doesn't match the expected chain are corrected automatically +- If the PRs are not yet in a stack, a new stack is created. If some PRs are already in a stack, the stack is updated (additive only — existing PRs are never removed) +- Does **not** create or modify any local state + +**Output (stderr):** + +- `Pushing N branches to ...` +- `Found PR #N for branch ` for branches with existing PRs +- `Created PR #N for (base: )` for newly created PRs +- `Updated base branch for PR #N to ` when base branches are corrected +- `Created stack with N PRs` or `Updated stack to N PRs` + +--- + +### Sync the stack — `gh stack sync` + +Fetch, rebase, push, and sync PR state in a single command. This is the recommended command for routine synchronization. + +```text +gh stack sync [flags] +``` + +| Flag | Description | +|------|-------------| +| `--remote ` | Remote to fetch from and push to (use if multiple remotes exist) | +| `--prune` | Delete local branches for merged PRs | + +**What it does (in order):** + +1. **Fetch** latest changes from the remote +2. **Reconcile the remote stack** — mirror the GitHub stack locally. If PRs were added to the stack on GitHub, pull their branches down and append them to the local stack. If the local and remote stacks have diverged, aborts the sync in a non-interactive terminal. In an interactive terminal, offers prompts to resolve any divergence (replace local stack with remote version, delete stack on GitHub so it can be recreated, or cancel). +3. **Fast-forward trunk** to match remote (skips if already up to date, warns if diverged) +4. **Cascade rebase** all stack branches onto their updated parents (only if trunk moved). Handles merged PRs automatically. If a conflict is detected, **all branches are restored** to their pre-rebase state and the command exits with code 3 — see [Handle rebase conflicts](#handle-rebase-conflicts-agent-workflow) for the resolution workflow +5. **Push** all active branches atomically +6. **Sync PR state** from GitHub and report the status of each PR +7. **Sync the stack object** — link the open PRs into a stack on GitHub. If the PRs are not yet in a stack, a new stack is created; if some PRs are already in a stack, it is updated (additive only). This only happens when two or more PRs exist. Sync **never opens PRs** — use `gh stack submit` for that +8. **Prune** — in interactive terminals, prompts to delete local branches for merged PRs. Use `--prune` to skip the prompt. In non-interactive environments, pruning only happens when `--prune` is passed explicitly + +**Output (stderr):** + +- `✓ Fetched latest changes from origin` +- `Pulling N new branches from the remote stack ...` then `✓ Pulled N new branches into the stack from the remote` (when the remote stack is ahead) +- `⚠ Your local stack has diverged from the stack on GitHub` (with `Local:` / `Remote:` chains) when the stacks have diverged +- `ℹ Sync aborted — no changes were made` when a sync is cancelled +- `✓ Trunk main fast-forwarded to ` or `✓ Trunk main is already up to date` +- `✓ Rebased onto ` per branch (if base moved) +- `✓ Pushed N branches` +- `✓ PR #N () — Open` per branch +- `Merged: #N, #M` for merged branches +- `✓ Stack created on GitHub with N PRs` / `✓ Stack updated on GitHub with N PRs` / `✓ Linked to the existing stack on GitHub` (when two or more PRs exist) +- `✓ Pruned (merged)` per pruned branch (when pruning) +- `✓ Stack synced` when the stack object on GitHub was created/updated to match local, or `✓ Branches synced` when only the branches were synced (fewer than two PRs or stacked PRs unavailable) + +--- + +### Rebase the stack — `gh stack rebase` + +Pull from remote and cascade-rebase stack branches. Use this when `sync` reports a conflict or when you need finer control (e.g., rebase only part of the stack). + +```text +gh stack rebase [flags] [branch] +``` + +```bash +# Rebase the entire stack +gh stack rebase + +# Rebase only branches from trunk to current branch +gh stack rebase --downstack + +# Rebase only branches from current branch to top +gh stack rebase --upstack + +# Rebase stack branches without pulling from or rebasing with trunk +gh stack rebase --no-trunk + +# After resolving a conflict: stage files with `git add`, then: +gh stack rebase --continue + +# Abort and restore all branches to pre-rebase state +gh stack rebase --abort +``` + +| Flag | Description | +|------|-------------| +| `--downstack` | Only rebase branches from trunk to the current branch | +| `--upstack` | Only rebase branches from the current branch to the top | +| `--no-trunk` | Skip trunk — only rebase stack branches onto each other (no fetch, no trunk rebase) | +| `--continue` | Continue after resolving conflicts | +| `--abort` | Abort and restore all branches | +| `--remote ` | Remote to fetch from (use if multiple remotes exist) | + +| Argument | Description | +|----------|-------------| +| `[branch]` | Target branch (defaults to the current branch) | + +**Conflict handling:** See [Handle rebase conflicts](#handle-rebase-conflicts-agent-workflow) in the Workflows section for the full resolution workflow. + +**Merged PR detection:** If a branch's PR was merged on GitHub, the rebase automatically handles this using `--onto` mode and correctly replays commits on top of the merge target. + +**Rerere (conflict memory):** `git rerere` is enabled by `init` so previously resolved conflicts are auto-resolved in future rebases. + +**No-trunk mode:** Use `--no-trunk` to skip fetching from the remote and rebasing with the trunk branch. Only inter-branch rebases are performed (branch 2 onto branch 1, branch 3 onto branch 2, etc.). Useful when you only need to align stack branches with each other without pulling upstream changes. + +--- + +### View the stack — `gh stack view` + +Display the current stack's branches, PR status, and recent commits. **Always pass `--json`** — without it, this command launches an interactive TUI that agents cannot operate. + +```bash +# Always use --json +gh stack view --json +``` + +| Flag | Description | +|------|-------------| +| `--json` | Output stack data as JSON to stdout (**required** for non-interactive use) | + +**`--json` output format:** + +```json +{ + "trunk": "main", + "currentBranch": "api-routes", + "branches": [ + { + "name": "auth", + "head": "abc1234...", + "base": "def5678...", + "isCurrent": false, + "isMerged": true, + "isQueued": false, + "needsRebase": false, + "pr": { + "number": 42, + "url": "https://github.com/owner/repo/pull/42", + "state": "MERGED" + } + }, + { + "name": "api-routes", + "head": "789abcd...", + "base": "abc1234...", + "isCurrent": true, + "isMerged": false, + "isQueued": false, + "needsRebase": false, + "pr": { + "number": 43, + "url": "https://github.com/owner/repo/pull/43", + "state": "OPEN" + } + } + ] +} +``` + +Fields per branch: +- `name` — branch name +- `head` — current HEAD SHA +- `base` — parent branch's HEAD SHA at last sync +- `isCurrent` — whether this is the checked-out branch +- `isMerged` — whether the PR has been merged +- `isQueued` — whether the PR is queued for merge (in a merge queue) +- `needsRebase` — whether the base branch is not an ancestor (non-linear history) +- `pr` — PR metadata (omitted if no PR exists). `state` is `"OPEN"`, `"MERGED"`, or `"QUEUED"`. + +--- + +### Navigate the stack + +Move between branches without remembering branch names. These commands are fully non-interactive. + +```bash +gh stack up # Move up one branch (further from trunk) +gh stack up 3 # Move up three branches +gh stack down # Move down one branch (closer to trunk) +gh stack down 2 # Move down two branches +gh stack top # Jump to the top of the stack (furthest from trunk) +gh stack bottom # Jump to the bottom (first non-merged branch above trunk) +gh stack trunk # Jump to the trunk branch (e.g. main) +``` + +Navigation clamps to stack bounds. Merged branches are skipped when navigating from active branches. + +--- + +### Check out a stack — `gh stack checkout` + +Check out a stack by stack number, pull request number, PR URL, or branch name. **Always provide an argument** — running `gh stack checkout` without arguments triggers an interactive selection menu. + +```text +gh stack checkout +``` + +```bash +# By stack number (the identifier shown in the GitHub stack UI) +gh stack checkout 7 + +# By PR number (pulls from GitHub) +gh stack checkout 42 + +# By PR URL +gh stack checkout https://github.com/owner/repo/pull/42 + +# By branch name (local only) +gh stack checkout feature-auth +``` + +A bare number is resolved as a **stack number first** (the identifier shown in the GitHub stack UI); if no stack has that number it is tried as a PR number, then a branch name. When a stack or PR number (or PR URL) is provided, the command fetches the stack on GitHub, pulls the branches, and sets up the stack locally. If the stack already exists locally and matches, it switches to the branch. + +> **⚠️ Agent warning:** If the local and remote stacks have different branch compositions, this command triggers an interactive conflict-resolution prompt that cannot be bypassed with a flag. To avoid this: run `gh stack unstack --local` first to remove the conflicting local tracking state (this keeps the stack on GitHub intact), then retry `gh stack checkout `. + +When a branch name is provided, the command resolves it against locally tracked stacks only. This is always safe for non-interactive use. + +--- + +### Remove a stack — `gh stack unstack` + +Tear down a stack so you can restructure it — remove a branch, reorder branches, rename branches, or make other large changes. After unstacking, use `gh stack init` to re-create the stack with the desired structure. + +Unstacking only removes the stack grouping (on GitHub and/or locally); it never deletes the underlying pull requests or branches. + +With no argument, the command targets the active stack — the one containing the currently checked out branch — unstacking it on GitHub and removing local tracking. + +Provide a stack number to unstack a specific stack on GitHub. This works from anywhere in the repository, whether the stack is checked out locally or not — the number is unstacked directly through the GitHub API (like `gh stack link`, no local tracking required). If the stack is also tracked locally, its local tracking is removed as well. + +```text +gh stack unstack [] [flags] +``` + +```bash +# Tear down the current stack — removes local tracking and the GitHub grouping (PRs are NOT deleted), then rebuild +gh stack unstack +gh stack init --base main branch-2 branch-1 branch-3 # reordered + +# Unstack a specific stack by its number, from anywhere in the repo +gh stack unstack 7 + +# Only remove local tracking (keep the stack on GitHub) +gh stack unstack --local +``` + +| Flag | Description | +|------|-------------| +| `--local` | Only remove the stack locally (keep it on GitHub); never contacts GitHub | + +> **Note for agents:** `gh stack unstack ` is a remote-first API wrapper — it unstacks on GitHub by number from anywhere in the repo, tracked locally or not, and is safe for non-interactive use. `--local` never contacts GitHub; combining `--local` with a number that isn't tracked locally is an error. An unknown stack number returns a "not found on GitHub" error (exit code 2). +> **Stopping condition:** If any PR in the stack is queued or has auto-merge enabled, `gh stack unstack` may leave the GitHub stack and local tracking in place. If all PRs are affected, the API can return HTTP 422. Do not run `gh stack init` until the GitHub stack has disappeared; remove the PRs from the merge queue or disable auto-merge, then rerun `gh stack unstack`. + +--- + +## Output conventions + +- **Status messages** go to **stderr** with emoji prefixes: `✓` (success), `✗` (error), `⚠` (warning), `ℹ` (info). +- **Data output** (e.g., `view --json`) goes to **stdout**. +- When piping output, use `2>/dev/null` to suppress status messages if only data output is needed. + +## Exit codes and error recovery + +| Code | Meaning | Agent action | +|------|---------|-------------| +| 0 | Command succeeded; `gh stack sync` may still print `Sync aborted` and make no changes | Inspect stderr for `Sync aborted` before continuing; if present, stop and inspect the stack | +| 1 | Generic error | Read stderr for details; may indicate commit/push failure | +| 2 | Command-specific: not in a stack, or the specified stack was not found | For an unknown `gh stack unstack `, report the missing stack and do not run `gh stack init`; otherwise initialize with explicit branch arguments only when local stack membership is required | +| 3 | Rebase conflict | Parse stderr for conflicted file paths, resolve conflicts, run `gh stack rebase --continue` | +| 4 | GitHub API failure | Check `gh auth status`, retry the command | +| 5 | Invalid arguments | Fix the command invocation (check flags and arguments) | +| 6 | Disambiguation required | A branch belongs to multiple stacks. Run `gh stack checkout ` to switch to a non-shared branch first | +| 7 | Rebase already in progress | Run `gh stack rebase --continue` (after resolving conflicts) or `gh stack rebase --abort` to start over | +| 8 | Stack is locked | Another `gh stack` process is writing the stack file. Wait and retry — the lock times out after 5 seconds | +| 9 | Stacked PRs unavailable | The repository does not have stacked PRs enabled. Tell the user that stacks must be enabled on the repository first | +| 10 | Modify recovery required | A `gh stack modify` session was interrupted. This skill does not use `modify`, so agents should not produce this; if the repo is left in this state, run `gh stack modify --abort` to restore the pre-modify state | + +## Known limitations + +1. **Stacks are strictly linear.** Branching stacks (multiple children on a single parent) are not supported. Each branch has exactly one parent and at most one child. If you need parallel workstreams, use separate stacks. +2. **Stack disambiguation cannot be bypassed.** If the current branch is the trunk of multiple stacks, commands error with code 6. Check out a non-shared branch first. +3. **Multiple remotes require `--remote` or config.** If more than one remote is configured, set `remote.pushDefault` in git config, or pass `--remote ` to the commands that accept it (`push`, `submit`, `sync`, `rebase`, `link`). `checkout`, `modify`, and `trunk` have no `--remote` flag and rely on `remote.pushDefault`. +4. **Remote stack checkout requires a stack or PR number.** `checkout` with a branch name only works with locally tracked stacks. Use a stack number or PR number (e.g. `gh stack checkout 7` or `gh stack checkout 123`) to pull a stack from GitHub. +5. **PR title and body are auto-generated.** There is no flag to set a custom PR title or body during `submit`. The title and body are generated from commit messages plus a footer. Use `gh pr edit` to modify PR title and body after creation. diff --git a/AGENTS.md b/AGENTS.md index ef2afaa2b..ef49495d3 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -67,6 +67,7 @@ | `cli` | imager-cli コマンド開発 | CLIコマンド追加・変更時 | | `git-worktree` | git worktree を用いた並列開発ワークフロー | 複数PRの同時進行時 | | `git-pr` | ブランチ作成→コミット→push→PR作成の標準ワークフロー | 単一PRの作成時 | +| `gh-stack` | GitHub CLIのgh stackによる積み上げブランチ・PRの作成、同期、rebase、状態確認 | stacked PR・依存PRの管理時 | | `issue-driven` | GitHub Issue駆動開発ワークフロー(証跡・進捗管理) | issueをベースに開発作業を始めるとき | | `tanstack-ssr` | TanStack Start SSR、Selective SSR、hydration、loaderの診断と回帰防止 | F5・直接アクセス・Hydration Mismatch・SSR/CSR境界変更時 | | `tanstack-db` | TanStack DBクライアントデータレイヤー(永続化、useLiveQuery、includes) | クライアント側データレイヤー変更時 | diff --git a/REPORT.md b/REPORT.md index 11b13e51c..d14dbaabb 100644 --- a/REPORT.md +++ b/REPORT.md @@ -6,19 +6,19 @@ | 項目 | 現状の原因 | 必要な実装 | V2での扱い | | --- | --- | --- | --- | -| Jobs一覧・履歴・詳細 | jobs contractはイベント購読のみで、一覧取得APIがない | Job DTO、list/detail query | 専用画面の骨格と未対応状態を表示 | -| JobのRetry / Cancel | 対応するuse caseとmutationがない | retry/cancel command、状態遷移、権限制御 | 操作をdisabled | -| Sourceの件数・同期中状態 | `SafeMediaSource`にmedia countとsync lifecycleがない | source summary DTO、集計、sync status event | sidebarに「件数未取得」 | -| Managerの利用件数 | Project / IP / Character一覧にmedia count等がない | 集計queryまたはsummary DTO | 取得可能な実データだけ表示 | +| Jobs一覧・履歴・詳細 | jobs contractはイベント購読のみで、一覧取得APIがない | Job DTO、list/detail query | 既存jobsテーブルからSafe DTOで一覧・詳細を表示。SSEで更新 | +| JobのRetry / Cancel | 対応するuse caseとmutationがない | retry/cancel command、状態遷移、権限制御 | 失敗JobのRetryを実装。Cancelは自然な状態遷移・権限制御が必要なため未対応 | +| Sourceの件数・同期中状態 | `SafeMediaSource`にmedia countとsync lifecycleがない | source summary DTO、集計、sync status event | 既存mediaの集計件数とプロセス内の同期状態を表示。同期イベントで更新 | +| Managerの利用件数 | Project / IP / Character一覧にmedia count等がない | 集計queryまたはsummary DTO | 既存の中間テーブルからProject / IP / Characterの利用件数を表示 | | Export / RestoreのJobs追跡 | 現行はHTTP download/uploadを直接実行する | job type、progress、artifact、失敗履歴 | 転送自体は実行し、Jobs連携は表示しない | -| AI接続状態・latency | health check contractがない | typed health endpointとtimeout/error定義 | Settingsで「未確認」、確認操作をdisabled | -| リロード後の前後メディア移動 | 詳細routeだけでは元のfilter/sort/cursorを復元できない | navigation context永続化、またはneighbor query | 前後ボタンをdisabled | +| AI接続状態・latency | health check contractがない | typed health endpointとtimeout/error定義 | Settingsの確認操作から実際のhealth checkを実行し、状態とlatencyを表示 | +| リロード後の前後メディア移動 | 詳細routeだけでは元のfilter/sort/cursorを復元できない | navigation context永続化、またはneighbor query | sessionStorageに最大500件の表示コンテキストを保存し、前後移動を提供。コンテキストなしではdisabled | ## Frontend follow-up | 項目 | 理由 | V2での扱い | | --- | --- | --- | -| Collectionのlist表示 | 4:3 gridを先行し、listの情報設計とvirtual row実装が未着手 | toggleをdisabled | -| Tauri route adapter | TauriはWebと別のhash routerを持つ | 今回はWebの`/v2/*`のみ。共有screenを使う薄いroute追加が必要 | +| Collectionのlist表示 | 4:3 gridを先行し、listの情報設計とvirtual row実装が未着手 | Name / Type / Dimensions / Size / Modifiedを表示するlistへ切り替え可能 | +| Tauri route adapter | TauriはWebと別のhash routerを持つ | 既存Tauri画面へつなぐ`/v2/*`薄いroute adapterを追加。JobsはWeb側のみ | バックエンド追加時は、無効状態を消すだけでなく、loading / error / offline / retryとリアルタイム更新まで同じ画面内で接続します。 diff --git a/apps/server/public/openapi.json b/apps/server/public/openapi.json index ec1a9a928..e08222735 100644 --- a/apps/server/public/openapi.json +++ b/apps/server/public/openapi.json @@ -1175,6 +1175,45 @@ "data" ] }, + { + "type": "object", + "properties": { + "event": { + "const": "source-sync-status" + }, + "data": { + "type": "object", + "properties": { + "mediaSourceId": { + "type": "string", + "format": "uuid" + }, + "status": { + "enum": [ + "idle", + "syncing", + "error" + ], + "type": "string" + }, + "message": { + "type": "string" + }, + "timestamp": { + "type": "string" + } + }, + "required": [ + "mediaSourceId", + "status" + ] + } + }, + "required": [ + "event", + "data" + ] + }, { "type": "object", "properties": { @@ -5253,6 +5292,77 @@ } } }, + "/ai/health": { + "post": { + "operationId": "ai.health", + "summary": "health", + "tags": [ + "AI" + ], + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "status": { + "enum": [ + "available", + "unavailable" + ], + "type": "string" + }, + "mode": { + "enum": [ + "local", + "remote" + ], + "type": "string" + }, + "latencyMs": { + "anyOf": [ + { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + }, + { + "type": "null" + } + ] + }, + "message": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "checkedAt": { + "type": "string", + "format": "date-time", + "x-native-type": "date" + } + }, + "required": [ + "status", + "mode", + "latencyMs", + "message", + "checkedAt" + ] + } + } + } + } + } + } + }, "/ai/tag": { "post": { "operationId": "ai.tag", @@ -6670,6 +6780,493 @@ } } }, + "/jobs/list": { + "post": { + "operationId": "jobs.list", + "summary": "list", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "status": { + "enum": [ + "pending", + "in_progress", + "completed", + "failed" + ], + "type": "string" + }, + "limit": { + "type": "integer", + "minimum": 1, + "maximum": 200, + "default": 50 + }, + "offset": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991, + "default": 0 + } + } + } + } + } + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "items": { + "type": "array", + "items": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "type": { + "type": "string" + }, + "mediaSourceId": { + "anyOf": [ + { + "type": "string", + "format": "uuid" + }, + { + "type": "null" + } + ] + }, + "status": { + "enum": [ + "pending", + "in_progress", + "completed", + "failed" + ], + "type": "string" + }, + "createdAt": { + "type": "string", + "format": "date-time", + "x-native-type": "date" + }, + "updatedAt": { + "type": "string", + "format": "date-time", + "x-native-type": "date" + }, + "parentId": { + "anyOf": [ + { + "type": "string", + "format": "uuid" + }, + { + "type": "null" + } + ] + }, + "error": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "targetMediaId": { + "anyOf": [ + { + "type": "string", + "format": "uuid" + }, + { + "type": "null" + } + ] + }, + "progress": { + "anyOf": [ + { + "type": "object", + "properties": { + "processed": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + }, + "failed": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + }, + "total": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + } + }, + "required": [ + "processed", + "failed", + "total" + ] + }, + { + "type": "null" + } + ] + } + }, + "required": [ + "id", + "type", + "mediaSourceId", + "status", + "createdAt", + "updatedAt", + "parentId", + "error", + "targetMediaId", + "progress" + ] + } + }, + "total": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + } + }, + "required": [ + "items", + "total" + ] + } + } + } + } + } + } + }, + "/jobs/get": { + "post": { + "operationId": "jobs.get", + "summary": "get", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + } + }, + "required": [ + "id" + ] + } + } + } + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "type": { + "type": "string" + }, + "mediaSourceId": { + "anyOf": [ + { + "type": "string", + "format": "uuid" + }, + { + "type": "null" + } + ] + }, + "status": { + "enum": [ + "pending", + "in_progress", + "completed", + "failed" + ], + "type": "string" + }, + "createdAt": { + "type": "string", + "format": "date-time", + "x-native-type": "date" + }, + "updatedAt": { + "type": "string", + "format": "date-time", + "x-native-type": "date" + }, + "parentId": { + "anyOf": [ + { + "type": "string", + "format": "uuid" + }, + { + "type": "null" + } + ] + }, + "error": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "targetMediaId": { + "anyOf": [ + { + "type": "string", + "format": "uuid" + }, + { + "type": "null" + } + ] + }, + "progress": { + "anyOf": [ + { + "type": "object", + "properties": { + "processed": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + }, + "failed": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + }, + "total": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + } + }, + "required": [ + "processed", + "failed", + "total" + ] + }, + { + "type": "null" + } + ] + } + }, + "required": [ + "id", + "type", + "mediaSourceId", + "status", + "createdAt", + "updatedAt", + "parentId", + "error", + "targetMediaId", + "progress" + ] + } + } + } + } + } + } + }, + "/jobs/retry": { + "post": { + "operationId": "jobs.retry", + "summary": "retry", + "requestBody": { + "required": true, + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + } + }, + "required": [ + "id" + ] + } + } + } + }, + "responses": { + "200": { + "description": "OK", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "id": { + "type": "string", + "format": "uuid" + }, + "type": { + "type": "string" + }, + "mediaSourceId": { + "anyOf": [ + { + "type": "string", + "format": "uuid" + }, + { + "type": "null" + } + ] + }, + "status": { + "enum": [ + "pending", + "in_progress", + "completed", + "failed" + ], + "type": "string" + }, + "createdAt": { + "type": "string", + "format": "date-time", + "x-native-type": "date" + }, + "updatedAt": { + "type": "string", + "format": "date-time", + "x-native-type": "date" + }, + "parentId": { + "anyOf": [ + { + "type": "string", + "format": "uuid" + }, + { + "type": "null" + } + ] + }, + "error": { + "anyOf": [ + { + "type": "string" + }, + { + "type": "null" + } + ] + }, + "targetMediaId": { + "anyOf": [ + { + "type": "string", + "format": "uuid" + }, + { + "type": "null" + } + ] + }, + "progress": { + "anyOf": [ + { + "type": "object", + "properties": { + "processed": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + }, + "failed": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + }, + "total": { + "type": "integer", + "minimum": 0, + "maximum": 9007199254740991 + } + }, + "required": [ + "processed", + "failed", + "total" + ] + }, + { + "type": "null" + } + ] + } + }, + "required": [ + "id", + "type", + "mediaSourceId", + "status", + "createdAt", + "updatedAt", + "parentId", + "error", + "targetMediaId", + "progress" + ] + } + } + } + } + } + } + }, "/jobs/events": { "post": { "operationId": "jobs.events", @@ -6719,6 +7316,29 @@ "data" ] }, + { + "type": "object", + "properties": { + "event": { + "const": "job-retried" + }, + "data": { + "type": "object", + "properties": { + "jobId": { + "type": "string" + }, + "message": { + "type": "string" + } + } + } + }, + "required": [ + "event", + "data" + ] + }, { "type": "object", "properties": { diff --git a/apps/server/src/application/services/directory-sync-service.ts b/apps/server/src/application/services/directory-sync-service.ts index 5a6d5535b..498c92231 100644 --- a/apps/server/src/application/services/directory-sync-service.ts +++ b/apps/server/src/application/services/directory-sync-service.ts @@ -1,5 +1,6 @@ import fs from "node:fs/promises"; import path from "node:path"; +import type { MediaSourceSyncState } from "@solid-imager/core/domain/sources/schemas"; import { services } from "~/application/registry"; import { ccipVectorService } from "~/application/services/ccip-vector-service"; import { MediaProcessingService } from "~/application/services/media-processing-service"; @@ -17,6 +18,44 @@ type SyncResult = { deleted: number; }; +type SourceSyncStatus = { + message?: string; + status: MediaSourceSyncState; + updatedAt: Date; +}; + +const syncStateGlobal = globalThis as typeof globalThis & { + __SOLID_IMAGER_SOURCE_SYNC_STATES__?: Map; +}; +const sourceSyncStates = + syncStateGlobal.__SOLID_IMAGER_SOURCE_SYNC_STATES__ ?? + new Map(); +syncStateGlobal.__SOLID_IMAGER_SOURCE_SYNC_STATES__ = sourceSyncStates; +const activeSyncs = new Map>(); +const PublicDirectorySyncFailureMessage = "Directory sync failed"; + +function publishSyncStatus( + mediaSourceId: string, + status: MediaSourceSyncState, + message?: string, +): void { + const updatedAt = new Date(); + const state = { status, message, updatedAt } satisfies SourceSyncStatus; + sourceSyncStates.set(mediaSourceId, state); + RealtimeEventBus.publishSource(mediaSourceId, "source-sync-status", { + mediaSourceId, + status, + message, + timestamp: updatedAt.toISOString(), + }); +} + +export function getSourceSyncState( + mediaSourceId: string, +): MediaSourceSyncState { + return sourceSyncStates.get(mediaSourceId)?.status ?? "idle"; +} + async function scanFiles(basePath: string): Promise { const files: string[] = []; const pendingDirectories = [""]; @@ -123,97 +162,135 @@ export const DirectorySyncService = { * Performs a comprehensive sync for a specific local media source. */ async syncMediaSource(mediaSourceId: string): Promise { - const result: SyncResult = { - sourceId: mediaSourceId, - added: 0, - deleted: 0, - }; - try { - const source = await sourceRepo.findById(mediaSourceId); - if (source?.type !== "local") { + const activeSync = activeSyncs.get(mediaSourceId); + if (activeSync) { + return activeSync; + } + + const syncPromise = (async (): Promise => { + const result: SyncResult = { + sourceId: mediaSourceId, + added: 0, + deleted: 0, + }; + publishSyncStatus(mediaSourceId, "syncing"); + try { + const source = await sourceRepo.findById(mediaSourceId); + if (source?.type !== "local") { + logger.info( + { mediaSourceId }, + "Skipping sync for non-local or missing source", + ); + publishSyncStatus( + mediaSourceId, + "idle", + source + ? "Source type does not support directory sync" + : "Source not found", + ); + return result; + } + + const basePath = (source.connectionInfo as { path: string }).path; + + try { + await fs.access(basePath); + } catch { + logger.error( + { mediaSourceId, basePath }, + "Base path does not exist or is not accessible during sync", + ); + publishSyncStatus( + mediaSourceId, + "error", + "Source path is not accessible", + ); + return result; + } + logger.info( - { mediaSourceId }, - "Skipping sync for non-local or missing source", + { mediaSourceId, basePath }, + "Starting directory sync for source", ); - return result; - } - const basePath = (source.connectionInfo as { path: string }).path; + // 1. Get existing paths from DB + const existingRecords = + await MediaRepository.findAllPathsBySourceId(mediaSourceId); + const dbPathMap = new Map(); // relativePath -> id + for (const record of existingRecords) { + // Ensure path uses POSIX separators for uniform comparison + const normalizedPath = record.filePath.split(path.sep).join("/"); + dbPathMap.set(normalizedPath, record.id); + } - try { - await fs.access(basePath); - } catch { - logger.error( - { mediaSourceId, basePath }, - "Base path does not exist or is not accessible during sync", + // 2. Scan the actual file system with runtime-portable Node APIs. + const fsPaths = await scanFiles(basePath); + + const mediaExtensions = services.getConfigService().getConfig() + .media.supportedExtensions; + const allowedExts = new Set( + Object.values(mediaExtensions) + .flat() + .map((ext) => ext.toLowerCase()), ); - return result; - } - logger.info( - { mediaSourceId, basePath }, - "Starting directory sync for source", - ); - - // 1. Get existing paths from DB - const existingRecords = - await MediaRepository.findAllPathsBySourceId(mediaSourceId); - const dbPathMap = new Map(); // relativePath -> id - for (const record of existingRecords) { - // Ensure path uses POSIX separators for uniform comparison - const normalizedPath = record.filePath.split(path.sep).join("/"); - dbPathMap.set(normalizedPath, record.id); - } + const actualMediaPaths = fsPaths.filter((p) => { + const ext = path.extname(p).toLowerCase(); + return allowedExts.has(ext); + }); + const allFilesPathSet = new Set(fsPaths); - // 2. Scan the actual file system with runtime-portable Node APIs. - const fsPaths = await scanFiles(basePath); - - const mediaExtensions = services.getConfigService().getConfig() - .media.supportedExtensions; - const allowedExts = new Set( - Object.values(mediaExtensions) - .flat() - .map((ext) => ext.toLowerCase()), - ); - - const actualMediaPaths = fsPaths.filter((p) => { - const ext = path.extname(p).toLowerCase(); - return allowedExts.has(ext); - }); - const allFilesPathSet = new Set(fsPaths); - - // 3. Calculate diffs - const filesToAdd: string[] = []; - for (const p of actualMediaPaths) { - if (!dbPathMap.has(p)) { - filesToAdd.push(p.split("/").join(path.sep)); + // 3. Calculate diffs + const filesToAdd: string[] = []; + for (const p of actualMediaPaths) { + if (!dbPathMap.has(p)) { + filesToAdd.push(p.split("/").join(path.sep)); + } } - } - const filesToDelete: { id: string; relativePath: string }[] = []; - for (const [p, id] of dbPathMap.entries()) { - if (!allFilesPathSet.has(p)) { - filesToDelete.push({ id, relativePath: p.split("/").join(path.sep) }); + const filesToDelete: { id: string; relativePath: string }[] = []; + for (const [p, id] of dbPathMap.entries()) { + if (!allFilesPathSet.has(p)) { + filesToDelete.push({ + id, + relativePath: p.split("/").join(path.sep), + }); + } } + + // 4. Batch process additions + await processAdditions(mediaSourceId, filesToAdd, result); + + // 5. Batch process deletions + await processDeletions(mediaSourceId, filesToDelete, result); + + logger.info( + { mediaSourceId, syncResult: result }, + "Directory sync completed successfully", + ); + publishSyncStatus(mediaSourceId, "idle"); + return result; + } catch (error) { + logger.error( + { err: error, mediaSourceId }, + "Error during directory sync", + ); + publishSyncStatus( + mediaSourceId, + "error", + PublicDirectorySyncFailureMessage, + ); + return result; } + })(); - // 4. Batch process additions - await processAdditions(mediaSourceId, filesToAdd, result); - - // 5. Batch process deletions - await processDeletions(mediaSourceId, filesToDelete, result); - - logger.info( - { mediaSourceId, syncResult: result }, - "Directory sync completed successfully", - ); - return result; - } catch (error) { - logger.error( - { err: error, mediaSourceId }, - "Error during directory sync", - ); - return result; + activeSyncs.set(mediaSourceId, syncPromise); + try { + return await syncPromise; + } finally { + if (activeSyncs.get(mediaSourceId) === syncPromise) { + activeSyncs.delete(mediaSourceId); + } } }, diff --git a/apps/server/src/components/media/legacy-media-grid-item.tsx b/apps/server/src/components/media/legacy-media-grid-item.tsx index 16f295555..7d0c357a9 100644 --- a/apps/server/src/components/media/legacy-media-grid-item.tsx +++ b/apps/server/src/components/media/legacy-media-grid-item.tsx @@ -18,6 +18,7 @@ export type ServerMediaGridItemProps = { isBulkSelectMode?: boolean; isSelected?: boolean; onToggleSelect?: () => void; + onPrepareMediaDetail?: () => void; onPreviewSelect?: () => void; }; diff --git a/apps/server/src/components/media/v2-media-grid-item.tsx b/apps/server/src/components/media/v2-media-grid-item.tsx index 93ba45241..478211bb7 100644 --- a/apps/server/src/components/media/v2-media-grid-item.tsx +++ b/apps/server/src/components/media/v2-media-grid-item.tsx @@ -37,6 +37,7 @@ export function V2MediaGridItem(props: V2ServerMediaGridItemProps) { !event.shiftKey && !event.altKey ) { + props.onPrepareMediaDetail?.(); sessionStorage.setItem("v2:media-return", location().href); } }} diff --git a/apps/server/src/components/v2/v2-source-list.tsx b/apps/server/src/components/v2/v2-source-list.tsx index 7619d978c..a8294c1e6 100644 --- a/apps/server/src/components/v2/v2-source-list.tsx +++ b/apps/server/src/components/v2/v2-source-list.tsx @@ -35,10 +35,17 @@ function sourceTypeLabel(source: SafeMediaSource): string { return source.type === "local" ? "Local" : source.type.toUpperCase(); } +function sourceStatusLabel(source: SafeMediaSource): string { + if (source.syncStatus === "syncing") return "同期中"; + if (source.syncStatus === "error") return "同期エラー"; + return sourceTypeLabel(source); +} + function V2SourceActions(props: { onDelete: () => void; onEdit: () => void; onSync: () => void; + isSyncing: boolean; sourceName: string; }) { return ( @@ -52,12 +59,13 @@ function V2SourceActions(props: { diff --git a/packages/ui/src/screens/v2-config-state-screen.tsx b/packages/ui/src/screens/v2-config-state-screen.tsx index fcfffb250..bf48a718f 100644 --- a/packages/ui/src/screens/v2-config-state-screen.tsx +++ b/packages/ui/src/screens/v2-config-state-screen.tsx @@ -1,3 +1,4 @@ +import type { AiHealthResponse } from "@solid-imager/core/domain/tagging/schemas"; import { Match, Show, Switch } from "solid-js"; import { ErrorState, OfflineState, QueryStatus } from "../async-state"; import { ConfigSkeleton, LoadingRegion } from "../skeleton"; @@ -6,7 +7,11 @@ import { V2ManagementHeader } from "../v2/management-layout"; import type { ConfigStateScreenProps } from "./config-state-screen.types"; import { V2ConfigScreen } from "./v2-config-screen"; -export function V2ConfigStateScreen(props: ConfigStateScreenProps) { +export type V2ConfigStateScreenProps = ConfigStateScreenProps & { + checkAiHealth: () => Promise; +}; + +export function V2ConfigStateScreen(props: V2ConfigStateScreenProps) { const hasData = () => props.data !== undefined; return ( @@ -35,6 +40,7 @@ export function V2ConfigStateScreen(props: ConfigStateScreenProps) { {(data) => ( ; + jobs: Accessor; + onRefresh: () => void | Promise; + onRetry: (jobId: string) => void | Promise; + state: Accessor>; +}; + +function jobTypeLabel(type: string): string { + return type + .replaceAll("_", " ") + .replace(/\b\w/g, (character) => character.toUpperCase()); +} + +function statusLabel(status: JobDto["status"]): string { + return { + completed: "Completed", + failed: "Failed", + in_progress: "In progress", + pending: "Pending", + }[status]; +} + +function statusClass(status: JobDto["status"]): string { + return { + completed: + "border-emerald-200 bg-emerald-50 text-emerald-700 dark:border-emerald-800 dark:bg-emerald-950/40 dark:text-emerald-300", + failed: + "border-red-200 bg-red-50 text-red-700 dark:border-red-800 dark:bg-red-950/40 dark:text-red-300", + in_progress: + "border-blue-200 bg-blue-50 text-blue-700 dark:border-blue-800 dark:bg-blue-950/40 dark:text-blue-300", + pending: + "border-amber-200 bg-amber-50 text-amber-700 dark:border-amber-800 dark:bg-amber-950/40 dark:text-amber-300", + }[status]; +} + +function JobStatusBadge(props: { status: JobDto["status"] }) { + return ( + + {statusLabel(props.status)} + + ); +} + +function JobProgress(props: { progress: JobDto["progress"] }) { + const percent = () => { + if (!props.progress || props.progress.total === 0) return 0; + return Math.min( + 100, + Math.round((props.progress.processed / props.progress.total) * 100), + ); + }; + + return ( + —} + when={props.progress} + > + {(progress) => ( +
+
+ {percent()}% + + {progress().processed.toLocaleString()}/ + {progress().total.toLocaleString()} + +
+
+
+
+
+ )} + + ); +} + +function JobsTable(props: { + jobs: JobDto[]; + onSelect: (job: JobDto) => void; + selectedJobId: string | null; +}) { return ( - - - - -

- {props.filterLabel} jobsはまだ表示できません -

-

- 現在のバックエンドはジョブイベントのみを配信しており、一覧・詳細・履歴を取得するAPIは未実装です。 - 実データを取得できるようになるまで、この画面では状態を推測して表示しません。 -

- - Backend support required - -
-
+
+
+ + + + + + + + + + + + + {(job) => ( + + + + + + + )} + + +
+ ジョブの一覧。{props.jobs.length}件。 +
+ Type + + Status + + Progress + + Updated +
+ + + + + + + {formatDate(job.updatedAt, { includeTime: true })} +
+
+
); } -function JobsInspectorPlaceholder() { +function JobsInspector(props: { + class?: string; + job: JobDto | undefined; + onRetry: (jobId: string) => void | Promise; +}) { + const [isRetrying, setIsRetrying] = createSignal(false); + + const retry = async () => { + const job = props.job; + if (job?.status !== "failed" || isRetrying()) return; + setIsRetrying(true); + try { + await props.onRetry(job.id); + } catch { + // The route reports mutation failures; keep the selected job visible. + } finally { + setIsRetrying(false); + } + }; + return ( ); } -export function V2JobsScreen() { - const [activeFilter, setActiveFilter] = createSignal("all"); +export function V2JobsScreen(props: V2JobsScreenProps) { + const [activeFilter, setActiveFilter] = createSignal("all"); + const [selectedJobId, setSelectedJobId] = createSignal(null); + + const selectedJob = createMemo(() => + props.jobs().find((job) => job.id === selectedJobId()), + ); + const filteredJobs = createMemo(() => { + const jobs = props.jobs(); + switch (activeFilter()) { + case "active": + return jobs.filter( + (job) => job.status === "pending" || job.status === "in_progress", + ); + case "completed": + return jobs.filter((job) => job.status === "completed"); + case "failed": + return jobs.filter((job) => job.status === "failed"); + default: + return jobs; + } + }); + const refresh = async () => { + await props.onRefresh(); + }; return (
- + {(data) => ( + + )} + +
} @@ -153,7 +405,10 @@ export function V2JobsScreen() {
- + setActiveFilter(value as JobFilter)} + value={activeFilter()} + >
{(filter) => ( - + + + +
+ + + + + + + + + + + } + when={filteredJobs().length > 0} + > + setSelectedJobId(job.id)} + selectedJobId={selectedJobId()} + /> + + {(total) => ( +

+ Showing{" "} + {filteredJobs().length.toLocaleString()} of{" "} + {total().toLocaleString()} jobs +

+ )} +
+ + {(job) => ( + + )} + +
+
+ )} @@ -191,7 +505,7 @@ export function V2JobsScreen() {
- +
); diff --git a/packages/ui/src/screens/v2-manager/entity-panel.tsx b/packages/ui/src/screens/v2-manager/entity-panel.tsx index 59eb587cb..c58b43482 100644 --- a/packages/ui/src/screens/v2-manager/entity-panel.tsx +++ b/packages/ui/src/screens/v2-manager/entity-panel.tsx @@ -88,6 +88,12 @@ function EntityInspector(props: {
+
+
Media
+
+ {props.item.mediaCount?.toLocaleString() ?? "—"} +
+
@@ -157,7 +172,7 @@ export function V2SourceMediaScreen(props: SourceMediaScreenProps) { {(media) => ( diff --git a/packages/ui/src/source-media-grid.tsx b/packages/ui/src/source-media-grid.tsx index 3f18de7bc..8fb80cdc5 100644 --- a/packages/ui/src/source-media-grid.tsx +++ b/packages/ui/src/source-media-grid.tsx @@ -48,6 +48,8 @@ const INITIAL_PRIORITY_ROWS = 2; const INITIAL_HIGH_PRIORITY_MEDIA = 2; const INITIAL_SKELETON_ROWS = 3; +export type SourceMediaViewMode = "grid" | "list"; + type ScrollDirection = "backward" | "forward" | null; function extractDirectionalRows( @@ -102,6 +104,7 @@ type SourceMediaGridProps = { isBulkSelectMode?: boolean; isSelected?: boolean; onPreviewSelect?: () => void; + onPrepareMediaDetail?: () => void; isPreviewSelected?: boolean; }, ) => JSX.Element; @@ -119,6 +122,12 @@ type SourceMediaGridProps = { totalCount?: number; /** Screen-specific copy for the initial error state. */ errorTitle?: string; + /** Optional collection presentation mode. */ + viewMode?: Accessor; + /** Opens the media detail route from list rows. */ + onOpenMediaDetail?: (media: Media) => void; + /** Saves the current collection context before a direct card navigation. */ + onPrepareMediaDetail?: (media: Media) => void; }; export function SourceMediaGrid(props: SourceMediaGridProps) { @@ -128,6 +137,7 @@ export function SourceMediaGrid(props: SourceMediaGridProps) { const enableVirtualization = () => props.enableVirtualization ?? false; const disableContextMenu = () => props.disableContextMenu ?? false; const totalCount = () => props.totalCount ?? props.mediaResults().length; + const viewMode = () => props.viewMode?.() ?? "grid"; const errorMessage = () => { const error = props.state().error; return error instanceof Error ? error.message : "API接続に失敗しました"; @@ -472,6 +482,8 @@ export function SourceMediaGrid(props: SourceMediaGridProps) { } : undefined, onContextMenu: onContextMenuHandler(media.id), + onPrepareMediaDetail: () => + props.onPrepareMediaDetail?.(media), priority: props.scrollMode === "element" ? index() < initialPriorityMediaCount() @@ -525,6 +537,8 @@ export function SourceMediaGrid(props: SourceMediaGridProps) { ) : undefined, onContextMenu: onContextMenuHandler(media.id), + onPrepareMediaDetail: () => + props.onPrepareMediaDetail?.(media), // Window virtualization keeps its established native lazy-loading // behavior. Element mode supplies a separate direction-aware policy. priority: @@ -553,6 +567,101 @@ export function SourceMediaGrid(props: SourceMediaGridProps) {
); + const formatFileSize = (bytes: number | null): string => { + if (bytes === null) return "—"; + if (bytes < 1024) return `${bytes} B`; + if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; + return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; + }; + const listContent = ( +
+ + + + + + + + + + + + + + + + + {(media) => ( + + + + + + + + + + + )} + + +
+ メディア一覧。{totalCount().toLocaleString()}件。 +
+ 選択 + + Name + + Type + + Dimensions + + Size + + Modified +
+ props.onToggleSelect?.(media.id)} + type="checkbox" + /> + + + + {media.mediaType} + + {media.width} × {media.height} + + {formatFileSize(media.fileSize)} + + {media.modifiedAt.toLocaleDateString("ja-JP")} +
+
+ ); return (
@@ -595,121 +704,128 @@ export function SourceMediaGrid(props: SourceMediaGridProps) {
- {/* Grid with optional context menu */} - - - - {gridContent} - - - - No media selected - - } - when={contextMenuMediaId()} - > - { - const id = contextMenuMediaId(); - if (id) props.onToggleSelect?.(id); - }} - > - {(() => { - const id = contextMenuMediaId(); - return id && - props.isBulkSelectMode?.() && - props.isSelected?.(id) - ? "選択解除" - : "選択"; - })()} - - - - - 0 - } - > - { - props.onBulkAction?.(); - }} - > - 一括操作を実行 ({props.selectedCount?.()}件選択中) - - { - props.onClearSelection?.(); - }} + {/* Grid/list with optional context menu for the grid presentation */} + + + + {gridContent} + + + + No media selected + + } + when={contextMenuMediaId()} > - 選択をクリア - - - - - - { - const id = contextMenuMediaId(); - const sourceId = props.mediaSourceId(); - if (id && sourceId) { - window.open( - `${props.detailBasePath ?? "/sources"}/${sourceId}/${id}`, - "_blank", - ); + { + const id = contextMenuMediaId(); + if (id) props.onToggleSelect?.(id); + }} + > + {(() => { + const id = contextMenuMediaId(); + return id && + props.isBulkSelectMode?.() && + props.isSelected?.(id) + ? "選択解除" + : "選択"; + })()} + + + + + 0 } - }} - > - 新しいタブで開く - - - - { - const id = contextMenuMediaId(); - if (id) props.onDelete?.(id); - }} - > - 削除 - - - - - { - const id = contextMenuMediaId(); - if (id) props.onCopyMove?.(id, "copy"); - }} - > - 他のソースへコピー - - { - const id = contextMenuMediaId(); - if (id) props.onCopyMove?.(id, "move"); - }} - > - 他のソースへ移動 - - - - - { - const id = contextMenuMediaId(); - if (id) props.onSyncSingleMedia?.(id); - }} - > - メタデータを同期 (再処理) - - - - + > + { + props.onBulkAction?.(); + }} + > + 一括操作を実行 ({props.selectedCount?.()}件選択中) + + { + props.onClearSelection?.(); + }} + > + 選択をクリア + + + + + + { + const id = contextMenuMediaId(); + const sourceId = props.mediaSourceId(); + if (id && sourceId) { + window.open( + `${props.detailBasePath ?? "/sources"}/${sourceId}/${id}`, + "_blank", + ); + } + }} + > + 新しいタブで開く + + + + { + const id = contextMenuMediaId(); + if (id) props.onDelete?.(id); + }} + > + 削除 + + + + + { + const id = contextMenuMediaId(); + if (id) props.onCopyMove?.(id, "copy"); + }} + > + 他のソースへコピー + + { + const id = contextMenuMediaId(); + if (id) props.onCopyMove?.(id, "move"); + }} + > + 他のソースへ移動 + + + + + { + const id = contextMenuMediaId(); + if (id) props.onSyncSingleMedia?.(id); + }} + > + メタデータを同期 (再処理) + + + + + + } + when={viewMode() === "list"} + > + {listContent} {/* Empty state */} diff --git a/packages/ui/src/source-media-page.tsx b/packages/ui/src/source-media-page.tsx index 03a268313..ad403f021 100644 --- a/packages/ui/src/source-media-page.tsx +++ b/packages/ui/src/source-media-page.tsx @@ -54,6 +54,7 @@ export type SourceMediaPageProps = { renderItem: SourceMediaScreenProps["renderItem"]; renderMediaPreview?: SourceMediaScreenProps["renderMediaPreview"]; onOpenMediaDetail?: SourceMediaScreenProps["onOpenMediaDetail"]; + onPrepareMediaDetail?: SourceMediaScreenProps["onPrepareMediaDetail"]; showOpenInNewTab?: boolean; onToggleSelect?: (mediaId: string) => void; isBulkSelectMode?: () => boolean; @@ -155,6 +156,7 @@ export function SourceMediaPage(props: SourceMediaPageProps): JSX.Element { renderActions={renderActions} renderItem={props.renderItem} onOpenMediaDetail={props.onOpenMediaDetail} + onPrepareMediaDetail={props.onPrepareMediaDetail} renderMediaPreview={props.renderMediaPreview} moveCopyDialogComponent={props.moveCopyDialogComponent} uploadModalComponent={props.uploadModalComponent} diff --git a/packages/ui/src/v2/search-toolbar.tsx b/packages/ui/src/v2/search-toolbar.tsx index e2db26770..b93650e1e 100644 --- a/packages/ui/src/v2/search-toolbar.tsx +++ b/packages/ui/src/v2/search-toolbar.tsx @@ -19,6 +19,7 @@ import { Popover, PopoverContent, PopoverTrigger } from "../popover"; import type { PresetManagerClient } from "../search-control-panel"; import { SearchControlPanel } from "../search-control-panel"; import { SortControls } from "../sort-controls"; +import type { SourceMediaViewMode } from "../source-media-grid"; import { clearPresetFilters, searchState, @@ -56,6 +57,8 @@ export type V2SearchToolbarProps = { selectedSource?: string; sourceName: string; sources?: SafeMediaSource[]; + viewMode?: SourceMediaViewMode; + onViewModeChange?: (mode: SourceMediaViewMode) => void; }; function parseValues(value: string): string[] { @@ -482,18 +485,27 @@ export function V2SearchToolbar(props: V2SearchToolbarProps) {