Action

AI Prompt Processor

Posted by TheChels.uk, Last update about 17 hours ago

UPDATES

about 17 hours ago

Tidy up description and add example prompts

About

Takes a prompt and data and provides an outcome using local on device model.

## PROMPT

<|prompt>

## DATA

<|data>

## OUTCOME

Note: Create a template with the above format for easy use, in conjunction with the ‘new from template’ action and placeholders.

Prompt Examples

  • Order the follow items alphabetically.
  • Summarises the follow text in a few bullets

Tip

If you find this useful and want to support me, you can tip at ko-fi.com/thechelsuk

Steps

  • script

    /*
     * @title: AI Prompt Processor
     * @author: TheChelsuk
     * @notes: Takes prompt and data from template,
     * processes with AI, and adds result to OUTCOME section
     */
    
    // Get draft content
    let content = draft.content;
    
    if (!content || content.trim() === '') {
        app.displayErrorMessage('Draft is empty');
        context.cancel();
    }
    
    // Extract PROMPT section (everything between ## PROMPT and ## DATA)
    let promptMatch = content.match(/## PROMPT\s*\n([\s\S]*?)(?=\n## DATA|$)/);
    let promptText = promptMatch ? promptMatch[1].trim() : null;
    
    // Extract DATA section (everything between ## DATA and ## OUTCOME)
    let dataMatch = content.match(/## DATA\s*\n([\s\S]*?)(?=\n## OUTCOME|$)/);
    let dataText = dataMatch ? dataMatch[1].trim() : null;
    
    // Clean up the text (remove leading dashes and bullets if present, but keep content)
    if (promptText) {
        promptText = promptText.replace(/^[\s-•*]+/, '').trim();
    }
    if (dataText) {
        dataText = dataText.replace(/^[\s-•*]+/, '').trim();
    }
    
    // Validate we have a prompt
    if (!promptText || promptText === '<|prompt>' || promptText === '') {
        app.displayErrorMessage('No prompt found. Add your prompt in the PROMPT section.');
        context.cancel();
    }
    
    // Validate we have data
    if (!dataText || dataText === '<|data>' || dataText === '') {
        app.displayErrorMessage('No data found. Add your data in the DATA section.');
        context.cancel();
    }
    
    // Combine prompt and data
    let fullPrompt = `${promptText}\n\nData to process:\n${dataText}`;
    
    // Create AI model
    let model = SystemLanguageModel.create();
    
    // Query the model (returns plain text)
    let response = model.respond(fullPrompt);
    
    // Check if we got a response
    if (response) {
        // Find the OUTCOME section and replace it
        let outcomeRegex = /(## OUTCOME\s*\n\s*)[\s\S]*/;
        
        if (content.match(outcomeRegex)) {
            // Replace existing outcome
            draft.content = content.replace(outcomeRegex, `$1- ${response}`);
        } else {
            // Append outcome if section doesn't exist
            draft.content = content + `\n\n## OUTCOME\n\n- ${response}`;
        }
        
        draft.update();
        app.displaySuccessMessage('AI response added to OUTCOME');
    } else {
        app.displayErrorMessage('AI Error: ' + model.lastError);
        context.fail();
    }

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.