···11+## Context
22+33+Lively Forms already stores anonymous responses and gives creators an authenticated response review surface, but that data currently stays inside the product. Exporting responses is a cross-cutting addition because it touches creator UI, ownership checks, response shaping, and file generation for multiple output formats.
44+55+The current codebase already has server-side form ownership checks and response retrieval in `lib/forms.ts` plus creator-facing response pages under `app/(creator)/forms/[id]/responses/`. The new export flow should fit that model: exports are generated on the server for owned forms only and downloaded from the creator surface.
66+77+## Goals / Non-Goals
88+99+**Goals:**
1010+- Let creators export responses for forms they own.
1111+- Support two practical spreadsheet-oriented formats in the first release: CSV and XLSX.
1212+- Produce stable, readable exports that include submission metadata and one column per answerable block.
1313+- Keep export authorization aligned with existing response review ownership rules.
1414+- Avoid database schema changes.
1515+1616+**Non-Goals:**
1717+- Building a generic reporting system or analytics dashboard.
1818+- Supporting every possible format in v1 (PDF, JSON, XML, etc.).
1919+- Exporting per-question aggregates or charts.
2020+- Persisting generated export files for later retrieval.
2121+2222+## Decisions
2323+2424+### 1. Add export actions to the creator responses surface
2525+The responses list page is the most natural place to export because it already represents the form-level response dataset. This keeps export discoverable and avoids duplicating controls elsewhere.
2626+2727+**Alternatives considered:**
2828+- Add export actions in the form builder only: rejected because exports belong to response data, not structure editing.
2929+- Add export on each single-response page: rejected because the goal is dataset export, not one-off answer download.
3030+3131+### 2. Generate exports on the server per request
3232+CSV/XLSX generation should happen in a server route or equivalent server-only handler after ownership verification. This avoids exposing raw response shaping logic to the client and keeps sensitive data authorization centralized.
3333+3434+**Alternatives considered:**
3535+- Client-side export from already rendered response data: rejected because the full dataset may not be loaded and authorization should remain server-enforced.
3636+- Background job + stored files: rejected as unnecessary complexity for an on-demand export feature.
3737+3838+### 3. Scope v1 formats to CSV and XLSX
3939+CSV gives a universal, low-dependency format for imports and scripts. XLSX covers common spreadsheet workflows and preserves a cleaner multi-column experience for non-technical creators.
4040+4141+**Alternatives considered:**
4242+- CSV only: simpler, but does not satisfy spreadsheet-first users who expect native Excel-compatible downloads.
4343+- Add JSON in v1: useful for integrations, but weaker product fit than spreadsheet exports for the current audience.
4444+- Add PDF in v1: poor fit for tabular response datasets.
4545+4646+### 4. Shape exports as one row per submission with derived columns
4747+Each exported row should represent one submission. Columns should begin with submission metadata (submission number, submitted timestamp, response ID) followed by one column per answerable block in saved form order. Column labels should use the block prompt when present, with a deterministic fallback for untitled questions.
4848+4949+This mirrors how creators think about submissions and makes the data immediately usable in spreadsheets.
5050+5151+**Alternatives considered:**
5252+- One sheet/tab per question: rejected because it complicates analysis and breaks the simple spreadsheet mental model.
5353+- Raw JSON blobs per submission: rejected because it reduces usability in CSV/XLSX.
5454+5555+### 5. Serialize complex answers into readable cells
5656+Short and long text answers export as plain text. Single choice exports as the selected option. Multiple choice exports as a delimited string in a single cell for both CSV and XLSX so both formats share the same conceptual schema.
5757+5858+**Alternatives considered:**
5959+- Expand multiple choice into one boolean column per option: powerful, but couples exports to mutable option sets and increases column sprawl in v1.
6060+- Store arrays in XLSX but flatten only CSV: rejected because differing schemas by format would be harder to explain and test.
6161+6262+### 6. Reuse existing response/domain mapping helpers where possible
6363+The implementation should centralize export row construction in shared server-side helpers so CSV and XLSX use the same data shaping logic. Format writers should consume the same normalized export dataset.
6464+6565+**Alternatives considered:**
6666+- Separate CSV/XLSX shaping paths: rejected because it invites drift and duplication.
6767+6868+## Risks / Trade-offs
6969+7070+- **Wide forms can produce wide exports** → Use saved block order and straightforward headings; accept width as a natural consequence of flexible forms.
7171+- **Untitled or duplicate question prompts can create confusing columns** → Provide deterministic fallback labels and preserve order so columns remain understandable.
7272+- **CSV formatting can be lossy for multiline text or separators** → Use a proper CSV serializer/escaping strategy rather than hand-built strings.
7373+- **New XLSX dependency increases bundle and maintenance surface** → Keep workbook generation server-only and use a minimal, well-supported library.
7474+- **Large response sets may increase request time** → Start with on-demand synchronous exports and monitor; optimize later if volume warrants it.
7575+7676+## Migration Plan
7777+7878+- No database migration is required.
7979+- Ship the server export handler and creator UI together so the control is only visible when the backend exists.
8080+- If rollback is needed, remove or disable the export action while leaving response review unaffected.
8181+8282+## Open Questions
8383+8484+- Whether the exported timestamp should use UTC ISO strings or a more human-readable format. Initial recommendation: use a stable machine-friendly timestamp value.
8585+- Exact filename convention. Initial recommendation: include form slug and format, e.g. `<slug>-responses.csv` and `<slug>-responses.xlsx`.
···11+## Why
22+33+Creators can review responses inside Lively Forms today, but they cannot take that data into spreadsheets, reporting workflows, or external tools. Adding export support now unlocks a practical next step for real form usage and addresses a common expectation for form products.
44+55+## What Changes
66+77+- Add creator-facing export actions for form responses from the response review area.
88+- Support exporting responses in CSV and XLSX formats for owned forms.
99+- Export one row per submission with stable columns derived from the form structure.
1010+- Include submission metadata such as submission number and submitted timestamp in the export output.
1111+- Serialize multi-select answers into a readable cell value for spreadsheet use.
1212+- Prevent exporting responses for forms the current creator does not own.
1313+1414+## Capabilities
1515+1616+### New Capabilities
1717+- `response-export`: Allow creators to export owned form responses in spreadsheet-friendly formats.
1818+1919+### Modified Capabilities
2020+- `response-review`: Extend response review so creators can trigger exports for owned forms from the responses surface.
2121+2222+## Impact
2323+2424+- Affected areas: response review pages, form/response data shaping, export route or server action, and download UI.
2525+- Likely code: `app/(creator)/forms/[id]/responses/`, `lib/forms.ts`, shared response/domain types, and API/server export logic.
2626+- Likely dependencies: XLSX generation library or equivalent workbook writer.
2727+- Security: ownership checks must apply to every export request.
···11+## ADDED Requirements
22+33+### Requirement: Creator can export owned form responses in CSV and XLSX formats
44+The system SHALL allow an authenticated creator to export responses for a form they own in CSV and XLSX formats.
55+66+#### Scenario: Creator exports owned form responses as CSV
77+- **WHEN** an authenticated creator requests a CSV export for a form they own
88+- **THEN** the system downloads a CSV file containing that form's responses
99+1010+#### Scenario: Creator exports owned form responses as XLSX
1111+- **WHEN** an authenticated creator requests an XLSX export for a form they own
1212+- **THEN** the system downloads an XLSX file containing that form's responses
1313+1414+### Requirement: Export requests are restricted by form ownership
1515+The system SHALL allow response exports only for forms owned by the authenticated creator and SHALL deny export access for forms owned by others.
1616+1717+#### Scenario: Creator exports another creator's form
1818+- **WHEN** an authenticated creator requests an export for a form they do not own
1919+- **THEN** the system denies access and does not return response data
2020+2121+### Requirement: Export output uses one row per submission with stable columns
2222+The system SHALL export one row per submission and SHALL include submission metadata plus one column for each answerable block in the form's saved order.
2323+2424+#### Scenario: Form contains multiple question types
2525+- **WHEN** a creator exports responses for a form with multiple answerable block types
2626+- **THEN** the export contains submission metadata columns followed by one column per answerable block in saved order
2727+2828+#### Scenario: Form contains text-only blocks
2929+- **WHEN** a creator exports responses for a form that includes non-answerable text blocks
3030+- **THEN** the export excludes those text blocks from answer columns
3131+3232+### Requirement: Exported answers are spreadsheet-readable
3333+The system SHALL serialize answers into readable cell values suitable for spreadsheet use across supported formats.
3434+3535+#### Scenario: Submission contains a multiple choice answer
3636+- **WHEN** a creator exports responses that include a multiple choice answer
3737+- **THEN** the selected options are written as a readable delimited value in the exported cell
3838+3939+#### Scenario: Submission contains long text content
4040+- **WHEN** a creator exports responses that include long text answers
4141+- **THEN** the exported file preserves the answer content as text in the corresponding cell
4242+4343+### Requirement: Exports remain available when a form has no submissions
4444+The system SHALL allow a creator to export an owned form even when it has no submissions and SHALL generate a file with headers only.
4545+4646+#### Scenario: Creator exports an owned form with no responses
4747+- **WHEN** an authenticated creator exports a form they own that has no submissions
4848+- **THEN** the system downloads a valid export file containing column headers and no submission rows
···11+## ADDED Requirements
22+33+### Requirement: Creator can start response exports from the responses view
44+The system SHALL present response export actions in the responses view for a form owned by the authenticated creator.
55+66+#### Scenario: Creator opens responses for an owned form
77+- **WHEN** an authenticated creator opens the responses view for a form they own
88+- **THEN** the system displays available export actions for the supported response export formats
···11+## 1. Export data shaping
22+33+- [x] 1.1 Add shared server-side helpers that load an owned form with its responses for export.
44+- [x] 1.2 Build a normalized export row shape with submission metadata and one column per answerable block in saved order.
55+- [x] 1.3 Implement answer serialization rules for short text, long text, single choice, and multiple choice responses.
66+- [x] 1.4 Add deterministic fallback column labels for untitled or duplicate questions.
77+88+## 2. File generation
99+1010+- [x] 2.1 Add or configure the XLSX dependency needed for workbook generation.
1111+- [x] 2.2 Implement CSV file generation using the normalized export dataset and proper escaping.
1212+- [x] 2.3 Implement XLSX file generation using the same normalized export dataset.
1313+- [x] 2.4 Add filename generation for exported files based on the form slug and selected format.
1414+1515+## 3. Creator export flow
1616+1717+- [x] 3.1 Add a server export endpoint or server action that validates form ownership before generating a download.
1818+- [x] 3.2 Wire CSV and XLSX export actions into the creator responses view.
1919+- [x] 3.3 Handle empty-response exports so creators can still download header-only files.
2020+- [x] 3.4 Show loading and error states that fit the existing creator UI patterns.
2121+2222+## 4. Verification
2323+2424+- [x] 4.1 Verify owned-form exports succeed in both CSV and XLSX formats.
2525+- [x] 4.2 Verify export requests for non-owned forms are denied.
2626+- [x] 4.3 Verify exported column order and values match the saved form order and response content.
2727+- [x] 4.4 Run the production build and fix any type or route issues introduced by the change.
+48
openspec/specs/response-export/spec.md
···11+## ADDED Requirements
22+33+### Requirement: Creator can export owned form responses in CSV and XLSX formats
44+The system SHALL allow an authenticated creator to export responses for a form they own in CSV and XLSX formats.
55+66+#### Scenario: Creator exports owned form responses as CSV
77+- **WHEN** an authenticated creator requests a CSV export for a form they own
88+- **THEN** the system downloads a CSV file containing that form's responses
99+1010+#### Scenario: Creator exports owned form responses as XLSX
1111+- **WHEN** an authenticated creator requests an XLSX export for a form they own
1212+- **THEN** the system downloads an XLSX file containing that form's responses
1313+1414+### Requirement: Export requests are restricted by form ownership
1515+The system SHALL allow response exports only for forms owned by the authenticated creator and SHALL deny export access for forms owned by others.
1616+1717+#### Scenario: Creator exports another creator's form
1818+- **WHEN** an authenticated creator requests an export for a form they do not own
1919+- **THEN** the system denies access and does not return response data
2020+2121+### Requirement: Export output uses one row per submission with stable columns
2222+The system SHALL export one row per submission and SHALL include submission metadata plus one column for each answerable block in the form's saved order.
2323+2424+#### Scenario: Form contains multiple question types
2525+- **WHEN** a creator exports responses for a form with multiple answerable block types
2626+- **THEN** the export contains submission metadata columns followed by one column per answerable block in saved order
2727+2828+#### Scenario: Form contains text-only blocks
2929+- **WHEN** a creator exports responses for a form that includes non-answerable text blocks
3030+- **THEN** the export excludes those text blocks from answer columns
3131+3232+### Requirement: Exported answers are spreadsheet-readable
3333+The system SHALL serialize answers into readable cell values suitable for spreadsheet use across supported formats.
3434+3535+#### Scenario: Submission contains a multiple choice answer
3636+- **WHEN** a creator exports responses that include a multiple choice answer
3737+- **THEN** the selected options are written as a readable delimited value in the exported cell
3838+3939+#### Scenario: Submission contains long text content
4040+- **WHEN** a creator exports responses that include long text answers
4141+- **THEN** the exported file preserves the answer content as text in the corresponding cell
4242+4343+### Requirement: Exports remain available when a form has no submissions
4444+The system SHALL allow a creator to export an owned form even when it has no submissions and SHALL generate a file with headers only.
4545+4646+#### Scenario: Creator exports an owned form with no responses
4747+- **WHEN** an authenticated creator exports a form they own that has no submissions
4848+- **THEN** the system downloads a valid export file containing column headers and no submission rows
+7
openspec/specs/response-review/spec.md
···2121#### Scenario: Creator inspects response with text blocks in the form
2222- **WHEN** an authenticated creator views a response for a form that includes text blocks
2323- **THEN** the system shows only answerable block responses while preserving the form order context for the submission
2424+2525+### Requirement: Creator can start response exports from the responses view
2626+The system SHALL present response export actions in the responses view for a form owned by the authenticated creator.
2727+2828+#### Scenario: Creator opens responses for an owned form
2929+- **WHEN** an authenticated creator opens the responses view for a form they own
3030+- **THEN** the system displays available export actions for the supported response export formats