From 96de48a521c1be5247a3c7dc47a900c5626472c7 Mon Sep 17 00:00:00 2001 From: Volodymyr Machula Date: Tue, 29 Oct 2024 00:51:33 +0100 Subject: [PATCH] Clean up code, rename action --- ...kNotionPage.ts => getNotionPageContent.ts} | 59 +++++++++---------- src/index.ts | 7 +-- 2 files changed, 30 insertions(+), 36 deletions(-) rename src/actions/{askNotionPage.ts => getNotionPageContent.ts} (80%) diff --git a/src/actions/askNotionPage.ts b/src/actions/getNotionPageContent.ts similarity index 80% rename from src/actions/askNotionPage.ts rename to src/actions/getNotionPageContent.ts index 302bcc6..cf7706d 100644 --- a/src/actions/askNotionPage.ts +++ b/src/actions/getNotionPageContent.ts @@ -1,8 +1,8 @@ import { ActionDefinition, ActionContext, OutputObject } from 'connery'; -import { Client, iteratePaginatedAPI, isFullBlock } from '@notionhq/client'; // Import Client and types from Notion +import { Client, iteratePaginatedAPI, isFullBlock } from '@notionhq/client'; const actionDefinition: ActionDefinition = { - key: 'askNotionPage', + key: 'getNotionPageContent', name: 'Get Notion Page Content', description: 'This action retrieves the content of a Notion page using its URL and the Notion API. It can optionally include instructions before the page content. The action required the Notion page URL and Notion API key connected to this URL. It fetches all content elements including text, media, and toggles, and returns the page content as a single string. It does not extract content form inline DBs.', @@ -41,8 +41,8 @@ const actionDefinition: ActionDefinition = { }, outputParameters: [ { - key: 'notionContent', - name: 'Notion Content', + key: 'notionPageContent', + name: 'Notion Page Content', type: 'string', validation: { required: true, @@ -54,40 +54,35 @@ const actionDefinition: ActionDefinition = { export default actionDefinition; export async function handler({ input }: ActionContext): Promise { - try { - // Extract the page ID from the provided Notion URL - const notionPageId = extractPageIdFromUrl(input.notionPageUrl); + // Extract the page ID from the provided Notion URL + const notionPageId = extractPageIdFromUrl(input.notionPageUrl); - // Initialize the Notion client - const notion = new Client({ auth: input.notionApiKey }); + // Initialize the Notion client + const notion = new Client({ auth: input.notionApiKey }); - // Retrieve all blocks of the Notion page - const blocks = await retrieveBlockChildren(notion, notionPageId); + // Retrieve all blocks of the Notion page + const blocks = await retrieveBlockChildren(notion, notionPageId); - // Process the blocks to get the content as a single string - const pageContent = blocks.map(getTextFromBlock).join('\n'); + // Process the blocks to get the content as a single string + const pageContent = blocks.map(getTextFromBlock).join('\n'); - // Check if the content length is less than 5 characters - if (pageContent.length < 5) { - throw new Error( - `The extracted content is too short: ${pageContent.length} characters. It must be at least 5 characters long.`, - ); - } - - // Prepare the output based on whether instructions are provided - let output: string; - if (input.instructions) { - output = `Follow these instructions: ${input.instructions}\nContent: ${pageContent}`; - } else { - output = pageContent; - } + // Check if the content length is less than 5 characters + if (pageContent.length < 5) { + throw new Error( + `The extracted content is too short: ${pageContent.length} characters. It must be at least 5 characters long.`, + ); + } - // Return the formatted output - return { notionContent: output }; - } catch (error: any) { - console.error('An error occurred:', (error as Error).message); - throw new Error(`Error occurred: ${(error as Error).message}`); + // Prepare the output based on whether instructions are provided + let output: string; + if (input.instructions) { + output = `Follow these instructions: ${input.instructions}\nContent: ${pageContent}`; + } else { + output = pageContent; } + + // Return the formatted output + return { notionPageContent: output }; } // Helper function to retrieve all blocks from a Notion page using pagination. Recursively fetches child blocks if they exist. diff --git a/src/index.ts b/src/index.ts index f85dc85..608c78b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,11 +1,10 @@ import { PluginDefinition, setupPluginServer } from 'connery'; -import askNotionPage from './actions/askNotionPage.js'; +import getNotionPageContent from './actions/getNotionPageContent.js'; const pluginDefinition: PluginDefinition = { name: 'Notion', - description: - 'This plugin enables interaction with Notion-based knowledge repositories, allowing users to query and retrieve answers from both public and private Notion pages. The plugin integrates with OpenAI to provide high-certainty answers based on the content available in Notion and suggests follow-ups in case of missing content.', - actions: [askNotionPage], + description: 'Notion plugin for Connery', + actions: [getNotionPageContent], }; const handler = await setupPluginServer(pluginDefinition);