Skip to content

Instantly share code, notes, and snippets.

@hitamu
Created December 25, 2024 03:57

Revisions

  1. hitamu created this gist Dec 25, 2024.
    44 changes: 44 additions & 0 deletions run.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    function run(input) {
    const key = 'your-key-here';
    return summarizeText(input[0], key)
    }

    function summarizeText(text, apiKey) {
    const app = Application.currentApplication();
    app.includeStandardAdditions = true;

    const endpoint = 'https://api.openai.com/v1/chat/completions';

    const requestData = {
    model: 'gpt-4o-mini',
    messages: [
    {
    role: 'system',
    content: 'You are a helpful assistant that provides concise summaries.'
    },
    {
    role: 'user',
    content: `Please summarize the following text: ${text}`
    }
    ],
    temperature: 0.7,
    max_tokens: 500
    };

    try {
    // Properly escape the JSON data for shell command
    const escapedData = JSON.stringify(requestData).replace(/'/g, "'\\''");

    const curlCommand = `curl -s "${endpoint}" \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer ${apiKey}" \
    -d '${escapedData}'`;

    const response = app.doShellScript(curlCommand);
    const jsonResponse = JSON.parse(response);
    return jsonResponse.choices[0].message.content;

    } catch (error) {
    return `Error: ${error}`;
    }
    }