Skip to content

REST API and Web UI Now Support Dynamic Pattern Variables #1533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

ksylvan
Copy link
Collaborator

@ksylvan ksylvan commented Jun 19, 2025

REST API and Web UI Now Support Dynamic Pattern Variables

Summary

This PR adds support for pattern variables in the REST API, allowing users to pass dynamic variables to patterns when making chat requests. This brings the REST API to feature parity with the CLI's pattern variable functionality.

Related Issues

Closes #1446

Screenshots

Before fix (on main)

The translate pattern needs a lang_code variable. Note that selecting the pattern does not have it show up in the Web UI.

image

image

After fix (on PR branch)

image

image

Files Changed

Modified Files

restapi/chat.go

  • Added Variables field to PromptRequest struct to accept pattern variables from API requests
  • Updated chat handler to pass pattern variables through to the chat options

restapi/patterns.go

  • Refactored pattern handler initialization to use custom routes instead of generic storage handler routes
  • Added new ApplyPattern endpoint (POST /patterns/:name/apply) for applying patterns with variables
  • Modified Get method to return raw pattern content without variable processing
  • Added PatternApplyRequest struct for the new apply endpoint

web/src/lib/components/chat/DropdownGroup.svelte

  • Added a new textarea input for entering pattern variables as JSON
  • Integrated with the pattern variables store to update variables in real-time
  • Added error-tolerant JSON parsing to allow partial typing

web/src/lib/interfaces/chat-interface.ts

  • Added optional variables field to ChatPrompt interface

web/src/lib/services/ChatService.ts

  • Updated createChatPrompt method to include pattern variables from the store
  • Minor formatting improvements to existing code

web/src/lib/store/pattern-store.ts

  • Added new patternVariables writable store to manage pattern variables state

Added Files

restapi/docs/API_VARIABLES_EXAMPLE.md

  • Comprehensive documentation for using pattern variables with the REST API
  • Includes examples for single and multiple variable usage
  • Provides curl commands for testing
  • Explains the implementation details and variable processing

Deleted Files

These appear to be some non-pattern-related raycast scripts.

  • patterns/raycast/capture_thinkers_work
  • patterns/raycast/create_story_explanation
  • patterns/raycast/extract_primary_problem
  • patterns/raycast/extract_wisdom
  • patterns/raycast/yt

Code Changes

Key Changes in restapi/chat.go

type PromptRequest struct {
    // ... existing fields ...
    Variables    map[string]string `json:"variables,omitempty"` // Pattern variables
}

Added pattern variables to the chat processing:

PatternVariables: p.Variables,      // Pass pattern variables

Key Changes in restapi/patterns.go

New endpoint for applying patterns with variables:

// POST /patterns/:name/apply
func (h *PatternsHandler) ApplyPattern(c *gin.Context) {
    // Accepts JSON body with input and variables
    // Merges query parameters with body variables
    // Applies variables to pattern and returns processed result
}

Key Changes in Web UI

Added JSON textarea for pattern variables:

<textarea
    id="pattern-variables"
    bind:value={variablesJsonString}
    on:input={updateVariables}
    placeholder="{`{\"lang_code\": \"fr\", \"role\": \"expert\"}`}"
    // ... styling ...
></textarea>

Reason for Changes

  1. Feature Parity: The CLI already supports pattern variables, but the REST API and web UI were missing this functionality
  2. User Flexibility: Allows users to create more dynamic and reusable patterns by passing variables at runtime
  3. Better UX: Web UI users can now leverage the same powerful pattern system as CLI users

Impact of Changes

  1. Backward Compatibility: All changes are backward compatible - existing API calls will continue to work without modification
  2. New Capabilities: Users can now:
    • Pass variables to patterns via REST API
    • Use the web UI to specify pattern variables
    • Apply patterns with variables programmatically
  3. Documentation: Added comprehensive examples to help users understand and adopt the new feature

Test Plan

  1. Manual Testing:

    • Test existing chat endpoints without variables (regression testing)
    • Test new pattern variables functionality with the translate pattern
    • Test multiple variables with custom patterns
    • Test error handling for invalid JSON in the web UI
  2. API Testing:

    • Use the provided curl examples in the documentation
    • Test with both query parameters and request body variables
    • Verify variable precedence (body overrides query params)
  3. Web UI Testing:

    • Enter valid JSON and verify variables are passed correctly
    • Test partial/invalid JSON entry (should not break the UI)
    • Verify pattern execution with variables produces expected results

Additional Notes

  • The implementation follows the existing pattern variable system used in the CLI
  • Variables use Go's template syntax (e.g., {{variable_name}})
  • The {{input}} variable is handled automatically and should not be included in the variables map
  • The web UI provides a monospace font for better JSON editing experience
  • Error handling is graceful - invalid JSON in the UI won't break functionality

ksylvan added 4 commits June 19, 2025 13:10
## CHANGES

- Add Variables field to PromptRequest struct
- Pass pattern variables through chat handler
- Create API variables documentation example
- Add pattern variables UI in web interface
- Create pattern variables store in Svelte
- Include variables in chat service requests
- Add JSON textarea for variable input
## CHANGES
- Create `PatternApplyRequest` struct for request body parsing
- Implement `ApplyPattern` method for POST /patterns/:name/apply
- Register manual routes for pattern operations in `NewPatternsHandler`
- Refactor `Get` method to return raw pattern content
- Merge query parameters with request body variables in `ApplyPattern`
- Use `StorageHandler` for pattern-related storage operations
…hods

- Refactor `cleanPatternOutput` to use a dedicated return variable.
- Hoist `processResponse` function for improved stream readability.
- Remove unnecessary whitespace and trailing newlines from file.
@ksylvan ksylvan requested review from Copilot and eugeis June 19, 2025 21:24
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Adds support for passing dynamic pattern variables through the REST API and Web UI, matching CLI functionality.

  • Introduces a new /patterns/:name/apply endpoint for rendering patterns with variables.
  • Extends the chat API (/chat) and UI components to accept and propagate a variables map.
  • Updates internal services and stores to manage and transmit pattern variables in requests.

Reviewed Changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated no comments.

Show a summary per file
File Description
web/src/lib/store/pattern-store.ts Adds patternVariables writable store for JSON input.
web/src/lib/services/ChatService.ts Includes variables in chat request options; renames cleaning variable.
web/src/lib/interfaces/chat-interface.ts Adds optional variables field to ChatPrompt interface.
web/src/lib/components/chat/DropdownGroup.svelte Adds textarea for JSON pattern variables with live parsing.
restapi/patterns.go Registers new apply route; custom GET returns raw pattern.
restapi/chat.go Adds Variables to PromptRequest and passes through to handler.
restapi/docs/API_VARIABLES_EXAMPLE.md Documents usage of pattern variables with examples.
Comments suppressed due to low confidence (4)

restapi/patterns.go:60

  • Consider adding automated tests for the new /patterns/:name/apply endpoint to verify variable merging (query vs. body) and error handling for invalid JSON.
// ApplyPattern handles the POST /patterns/:name/apply route

restapi/patterns.go:48

  • [nitpick] The returned pattern Description is always empty. It would improve clarity to populate this field from the stored pattern metadata so API consumers get the actual description.
		Description: "",

web/src/lib/services/ChatService.ts:184

  • The selectedStrategy store is used but not imported in this file, causing a reference error. Please import selectedStrategy from your strategy store (e.g., import { selectedStrategy } from '$lib/store/strategy-store';).
        strategyName: get(selectedStrategy), // Add selected strategy to prompt

restapi/patterns.go:81

  • After processing the pattern with GetApplyVariables, the handler does not send a success response. You should add c.JSON(http.StatusOK, pattern) to return the applied pattern to the client.
	pattern, err := h.patterns.GetApplyVariables(name, variables, request.Input)

Copy link
Collaborator

@eugeis eugeis left a comment

Choose a reason for hiding this comment

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

Looks good for me. Thank you

@ksylvan ksylvan merged commit dc9168a into danielmiessler:main Jun 19, 2025
1 check passed
@ksylvan ksylvan deleted the 0619-enhance-restapi-and-webui-with-variables branch June 23, 2025 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Web interface / restapi does not play well with patterns that use template variables
2 participants