Skip to content
This repository was archived by the owner on Aug 22, 2025. It is now read-only.

Commit 494ae8d

Browse files
author
Michael Liebmann
committed
Remove public action, remove shared.ts, working with toggles
1 parent 824adaf commit 494ae8d

File tree

4 files changed

+49
-177
lines changed

4 files changed

+49
-177
lines changed

src/actions/AskMyKnowledgeFromPublicNotionPage.ts

Lines changed: 0 additions & 135 deletions
This file was deleted.

src/actions/AskMyKnowledgeFromPrivateNotionPage.ts renamed to src/actions/AskPrivateNotionPage.ts

Lines changed: 47 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import { ActionDefinition, ActionContext, OutputObject } from 'connery';
2-
import { Client, iteratePaginatedAPI } from '@notionhq/client';
2+
import { Client, iteratePaginatedAPI, isFullBlock } from '@notionhq/client'; // Import Client and types from Notion
33
import OpenAI from 'openai';
4-
import { askModel } from '../shared/shared.js';
54

65
const actionDefinition: ActionDefinition = {
7-
key: 'askMyPrivateNotionPage',
8-
name: 'Ask my Knowledge from Private Notion Page',
6+
key: 'askPrivateNotionPage',
7+
name: 'Ask Private Notion Page',
98
description:
109
'This action enables users to ask questions and receive answers from a knowledge base hosted on a private Notion page. The action accesses the Notion page via its URL using the Notion API and an API key. Users’ questions are processed by OpenAI, which generates answers based on the content retrieved from the page. The action supports all content elements, including toggles.',
1110
type: 'read',
@@ -28,15 +27,6 @@ const actionDefinition: ActionDefinition = {
2827
required: true,
2928
},
3029
},
31-
{
32-
key: 'question',
33-
name: 'User Question',
34-
description: 'The question asked by the user about the particular knowledge base.',
35-
type: 'string',
36-
validation: {
37-
required: true,
38-
},
39-
},
4030
{
4131
key: 'openaiApiKey',
4232
name: 'OpenAI API Key',
@@ -55,6 +45,15 @@ const actionDefinition: ActionDefinition = {
5545
required: true,
5646
},
5747
},
48+
{
49+
key: 'question',
50+
name: 'User Question',
51+
description: 'The question asked by the user about the particular knowledge base.',
52+
type: 'string',
53+
validation: {
54+
required: true,
55+
},
56+
},
5857
],
5958
operation: {
6059
handler: handler,
@@ -90,32 +89,40 @@ export async function handler({ input }: ActionContext): Promise<OutputObject> {
9089
const pageContent = blocks.map(getTextFromBlock).join('\n');
9190

9291
// Log the extracted Notion content length
93-
console.log('Extracted Notion content length:', pageContent.length, 'characters');
92+
console.log("Extracted Notion content length:", pageContent.length, "characters");
93+
console.log("Extracted Notion content:", pageContent);
94+
95+
// Check if the content length is less than 5 characters
96+
if (pageContent.length < 5) {
97+
throw new Error(`The extracted content is too short: ${pageContent.length} characters. It must be at least 5 characters long.`);
98+
}
9499

95100
// Initialize OpenAI with the provided API key
96101
const openai = new OpenAI({ apiKey: openaiApiKey });
97102

98-
// Create the prompt with instructions for the model
99-
const prompt = `
100-
You are an FAQ expert. When asked a question or given a request related to a specific topic, you provide an accurate and concise answer based strictly on the content provided.
101-
You respond in the same language as the user’s input and adjust your answer to fit the context of the request, whether it’s a direct question or an indirect inquiry.
102-
You never guess or paraphrase — only answer if the explicit content for that request is available.
103-
If there are any disclaimers or indications in the content that it should not be shared with clients or is a work in progress, include that information only if it is explicitly mentioned.
104-
Here is the content you should use to generate your answer:
105-
106-
${pageContent}
107-
108-
Based on this content, please respond to the following request or question with high confidence:
109-
110-
${question}”.
111-
If you are not confident that the content fully addresses the request, respond with:
112-
‘My content source does not provide enough context to answer your request. If you want to report this knowledge gap to the admin, just trigger another action with “Report knowledge gap:” and add your original request.’
103+
// Create the system message with instructions for the model
104+
const systemMessage = `You are an FAQ expert. When asked a question or given a request related to a specific topic, you provide an accurate and concise answer based strictly on the content provided.
105+
You respond in the same language as the user’s input and adjust your answer to fit the context of the request, whether it’s a direct question or an indirect inquiry.
106+
You never guess or paraphrase — only answer if the explicit content for that request is available.
107+
If there are any disclaimers or indications in the content that it should not be shared with clients or is a work in progress, include that information only if it is explicitly mentioned.
108+
Here is the content you should use to generate your answer:
109+
${pageContent}
113110
`;
114-
111+
112+
// Set the user's question separately
113+
const userQuestion = `Based on this content, please respond to the following request or question with high confidence:
114+
${question}”.
115+
If you are not confident that the content fully addresses the request, respond with:
116+
‘My content source does not provide enough context to answer your request. If you want to report this knowledge gap to the admin, just trigger another action with “Report knowledge gap:” and add your original request.’
117+
`;
118+
115119
// Request completion from OpenAI using the specified model
116120
const response = await openai.chat.completions.create({
117121
model: openaiModel,
118-
messages: [{ role: 'user', content: prompt }],
122+
messages: [
123+
{ role: 'system', content: systemMessage },
124+
{ role: 'user', content: userQuestion },
125+
],
119126
});
120127

121128
// Log and handle the response
@@ -131,8 +138,6 @@ export async function handler({ input }: ActionContext): Promise<OutputObject> {
131138
throw new Error("Model's answer is too short.");
132139
}
133140

134-
console.log('Model output length:', messageContent.length, 'characters');
135-
136141
const answer = messageContent.trim();
137142

138143
// Return the model's answer directly
@@ -145,12 +150,18 @@ export async function handler({ input }: ActionContext): Promise<OutputObject> {
145150

146151
/**
147152
* Helper function to retrieve all blocks from a Notion page using pagination.
148-
* This ensures that all content from the page is fetched, even if it spans multiple pages of results.
153+
* Recursively fetches child blocks if they exist.
149154
*/
150155
async function retrieveBlockChildren(notion: Client, id: string) {
151-
const blocks = [];
156+
const blocks: Array<any> = [];
152157
for await (const block of iteratePaginatedAPI(notion.blocks.children.list, { block_id: id })) {
153158
blocks.push(block);
159+
160+
// Recursively fetch and process child blocks if the block has children
161+
if (isFullBlock(block) && block.has_children) {
162+
const childBlocks = await retrieveBlockChildren(notion, block.id);
163+
blocks.push(...childBlocks); // Add child blocks to the main block array
164+
}
154165
}
155166
return blocks;
156167
}
@@ -265,4 +276,4 @@ function extractPageIdFromUrl(url: string): string {
265276
throw new Error('Invalid Notion page URL');
266277
}
267278
return match[0].replace(/-/g, ''); // Return the page ID without dashes
268-
}
279+
}

src/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { PluginDefinition, setupPluginServer } from 'connery';
2-
import askMyKnowledgePublicNotion from './actions/AskMyKnowledgeFromPublicNotionPage.js';
3-
import askMyKnowledgePrivateNotion from './actions/AskMyKnowledgeFromPrivateNotionPage.js';
2+
import askPrivateNotionPage from './actions/AskPrivateNotionPage.js';
43

54
const pluginDefinition: PluginDefinition = {
65
name: 'Notion',
76
description:
87
'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.',
9-
actions: [askMyKnowledgePublicNotion, askMyKnowledgePrivateNotion],
8+
actions: [askPrivateNotionPage],
109
};
1110

1211
const handler = await setupPluginServer(pluginDefinition);

src/shared/shared.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)