Skip to content

feat(prompts): bulk import prompts and add several at once - #1013

Merged
gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/prompts-bulk-import
Oct 14, 2026
Merged

gkhngyk merged 7 commits into
ansvisor:mainfrom
cihan-gyk:feature/prompts-bulk-import

Conversation

@cihan-gyk

Copy link
Copy Markdown
Collaborator

Stacked on #1009 (tracked prompt limit counter), whose LocationQuota and limit actions this reuses. Please merge #1009 first. The new work is the last two commits.

Summary

PROMPT-005. Moving to Ansvisor from another tool meant adding prompts one at a time, and Add Prompt closed after each one. The dialog now has two tabs.

Single: queue several, then save

  • Add to list (or Enter) queues the typed prompt in a "Ready to save" list below the form, without saving it. The topic and engines stay selected and the field clears for the next prompt.
  • Each queued prompt shows its topic and has a × to remove it. A prompt that's already tracked or already in the list is refused when queued.
  • Save & add more saves the list and keeps the dialog open with an empty list.
  • Save N & close saves and closes. With an empty list and just a typed prompt, it reads Save & close, so adding one prompt is still one click.

Bulk import: a one-column file

  • The file is a .xlsx or .csv with one column of prompts, one per row. A first row titled Prompt is read as the header and is optional; without it, the first column is prompts from row 1. Other columns are ignored, with a note. An info box in the dialog explains this, and a one-column template can be downloaded.
  • Product decided that topic and location are not columns. Imported prompts get the dialog's platforms and the brand's location; the user sets topic and location afterwards with Edit in All Prompts. The All Prompts table is unchanged.
  • The file is read in the browser. CSV handles a comma or semicolon delimiter (detected), quotes, CRLF and a BOM; Turkish characters survive. XLSX uses read-excel-file (new dependency, MIT, read-only, ~small, loaded on demand). SheetJS was avoided because its npm package is unmaintained with known advisories.
  • The preview shows Row · Prompt · Status:
    • Valid
    • Duplicate: case and whitespace insensitive, against tracked prompts and earlier rows
    • Error: empty, or > 500 chars
    • Over plan limit
  • When rows don't fit, a warning appears with Upgrade plan (Starter) or Contact us. Download not-imported prompts (.csv) and Download errors (.csv) are available. The button reads "Import N prompts", and the toast reads "N imported · M not imported due to plan limit".
  • At most 1,000 rows are read per file; larger files are asked to be split.

The limit and concurrency

  • Both tabs save through a new server action, importPrompts. It re-checks the rows (text, duplicates, valid locations, plan-allowed engines, the brand's shopping pref), creates any new topics without touching existing ones (the existing createTopics deletes the brand's topics first, so it isn't used), and calls:
  • import_prompts (migration 00122), a SECURITY INVOKER plpgsql function. It takes a per-organization pg_advisory_xact_lock, counts the org's tracked locations (org_prompt_location_usage), and inserts rows in order until the next one would exceed maxPrompts, in the same tracked-location unit as feat(prompts): show tracked prompt usage against the plan limit #1009. Two imports running at once still stop at the limit. I verified this locally with two concurrent calls against a one-slot limit: one inserted, one didn't.
  • The saved prompts are submitted for their first run in one analyze-new request (new analyzeNewPrompts; analyzeNewPrompt now delegates to it).

Not in this PR

  • Tags: prompts have no tags field.
  • Topic/location columns: dropped by product decision.
  • Background processing for > 1,000 rows: larger files are asked to be split.
  • "View imported" filtered link: the table has no such filter.

Notes for reviewers

Related issue

None. Product task PROMPT-005 ("Bulk import prompts from Excel / CSV"), adjusted with product (Cihan) to a one-column file plus a multi-add Single tab.

Type of change

  • feat — New feature
  • fix — Bug fix
  • chore — Maintenance / dependencies
  • docs — Documentation only
  • refactor — Code change that neither fixes a bug nor adds a feature
  • test — Adding or updating tests

How to test

  1. Apply 00122. Open Prompts → Add prompt → Single. Type a prompt, choose a topic, then Add to list. Add two more, remove one with ×, and try re-adding an existing prompt (it's refused).
  2. Save & add more: the dialog stays open with an empty list. Type one more, then Save & close. All the prompts appear in All Prompts with the chosen topic.
  3. Bulk import: drop a one-column .xlsx or .csv (with or without a Prompt header, including a repeated row and an already-tracked prompt). Duplicates are marked, and Import adds the rest with no topic and the brand's location.
  4. With NEXT_PUBLIC_IS_CLOUD=true and a low maxPrompts, import a longer file: only the first rows that fit are imported, the warning and the not-imported download appear, and usage stops exactly at the limit.

All of the above was tested locally (light and dark mode); the test rows were removed afterwards.

Checklist

  • Branch follows the naming convention (feature/, fix/, chore/, docs/) — see CONTRIBUTING.md
  • Commits follow Conventional Commits
  • yarn lint passes (run from web/)
  • yarn typecheck passes (run from web/)
  • yarn format:check passes (run from web/)
  • Changes are focused — one concern per PR

yarn test (485, including 13 new in lib/prompt-import.test.ts) and yarn build (CI placeholder env) pass. Server checks are unchanged and pass. bash supabase/build-schema.sh leaves schema.sql unchanged.

🤖 Generated with Claude Code

cihan-gyk and others added 3 commits October 13, 2026 13:25
The Tracked Prompts page showed only "All Prompts (N)", with no sign of the
plan limit or how many slots were left, so users found out at save time.
The header now shows "used / limit prompt locations · <plan> plan" with a
progress bar and the slots left (amber from 80%, red when full or over).
When the limit is reached, Add prompt explains it with Upgrade plan
(Starter) or Contact us (Growth / Enterprise) instead of the form.

The numbers come from getLocationQuota, the same org-wide prompt-location
count the plan-limit guard enforces, so the counter can't promise a slot a
save would reject. getPromptsPageData returns it in the page's single
round trip.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Moving to Ansvisor from another tool meant adding prompts one at a time,
and the Add Prompt dialog closed after each one.

Add Prompt now has two tabs:
- Single: "Add to list" queues the typed prompt below the form without
  saving it, keeps the topic and engines, and clears the field for the
  next one. "Save & add more" saves the list and stays open; "Save N &
  close" saves and closes. Duplicates are refused when queued.
- Bulk import: a one-column .xlsx or .csv of prompts, one per row, with
  an optional "Prompt" header, read in the browser (read-excel-file).
  The preview marks each row valid, duplicate (case and whitespace
  insensitive, against tracked prompts and earlier rows), error, or over
  the plan limit. Not-imported and error rows can be downloaded. Imported
  prompts get the dialog's platforms and the brand's location; topic and
  location are set afterwards with Edit.

Both save through importPrompts, which re-checks the rows and writes them
with import_prompts (00122): a per-organization advisory lock, the
tracked-location count, and inserts in order until the plan cap, so two
imports at once still stop at the limit. The saved prompts go for their
first run in one analyze-new request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…panel's

Keeps this branch merging cleanly with the Positions panel range change
(feature/prompts-positions-panel-range), which edits the import next to them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk PROMPT-005 (bulk import + adding several prompts at once) is ready for review. It's stacked on #1009, adds migration 00122 and a small new dependency (read-excel-file). After #1011/#1012, schema.sql needs a re-run of build-schema.sh.

@gkhngyk

gkhngyk commented Oct 13, 2026

Copy link
Copy Markdown
Contributor

#1009 was squash-merged, so this branch now conflicts with main. Please merge main in:

I'll review once it's mergeable.

# Conflicts:
#	supabase/schema.sql
#	web/src/app/[locale]/(dashboard)/dashboard/prompts/page.tsx
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk merged main in (76e8fe5), and the PR is mergeable again. prompts/page.tsx now differs from main only by this PR's changes. schema.sql was regenerated with build-schema.sh and includes 00120–00122. Web format, lint, typecheck, tests (503) and build pass. Ready for review.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall, thanks. One thing to fix before merge:

Large imports never get their first analysis. The import sends up to 1,000 prompt ids to analyze-new in one request, and the route validates them with a single .in('id', ids). Against production, a PostgREST in filter worked with 300 ids and failed from 400 up. The error is ignored, so the response is newCount: 0 and the toast says the prompts wait for the next run. This only happens on unlimited plans, since Growth caps at 200, but the import allows 1,000. Please chunk the in filters in analyze-new by 100, as prompt-positions does.

Note: [email protected] was published six days ago. It's from the same long-time maintainer, but consider pinning the exact version in package.json.

cihan-gyk and others added 3 commits October 14, 2026 12:28
# Conflicts:
#	supabase/schema.sql
#	web/src/app/[locale]/(dashboard)/dashboard/prompts/page.tsx
A bulk import sends up to 1,000 prompt ids to /analyze-new, and one
PostgREST `in` filter over that many fails. The error was ignored, so the
route answered newCount: 0 and the imported prompts never got their first
analysis. Validate and look up results through selectInChunks (100 ids per
request), and fail loudly when a lookup errors.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@cihan-gyk

Copy link
Copy Markdown
Collaborator Author

@gkhngyk thanks, both addressed:

  • Large imports now get their first analysis (a512c92). analyze-new checks the prompt ids and looks up existing results through selectInChunks (100 ids per request, like prompt-positions). A failed lookup now returns an error instead of newCount: 0. New tests in tracking-analyze-new.test.js cover two cases: a 1,000-id request stays at ≤ 100 ids per in filter and submits all 1,000 prompts, and a failed lookup returns 500. Both tests fail on the old code.
  • read-excel-file is pinned to exact 9.3.12 (7bf212a). The lockfile change is that one entry.

I also merged main in again; the conflict was only the imports in prompts/page.tsx, and I kept both sides. schema.sql was regenerated. Server lint and tests (1110) pass. Web lint, typecheck, tests (516) and build pass.

Separate from this PR: the "already analyzed" lookup in analyze-new reads one row per result. That means a chunk whose prompts have more than 1,000 results between them can be cut off at max_rows, and some analyzed prompts would look new. This was already the case before this PR, and I'll follow it up separately.

@gkhngyk gkhngyk left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, both addressed. Migration 00122 is applied to production.

@gkhngyk
gkhngyk merged commit 32a4d7b into ansvisor:main Oct 14, 2026
5 checks passed
cihan-gyk added a commit to cihan-gyk/ansvisor that referenced this pull request Oct 14, 2026
Resolves the conflicts with the bulk import (ansvisor#1013): prompt.ts keeps both
setPromptActive and importPrompts, and the Prompts page imports both. The
import RPC (00122) counts locations through org_prompt_location_usage, which
00128 limits to active prompts, so imports follow the same count.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-migration Adds or edits a migration — apply it to the hosted database

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants