File size: 3,542 Bytes
34367da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

import { Client } from '@notionhq/client';
import { logger } from '../utils/logger.js';

const log = logger.child({ module: 'register-gemini-agent' });

/**

 * ===== SCRIPT CONFIGURATION =====

 */
const AGENTS_DATABASE_ID = '2cc12238fd44818dbe64eb12df2d3ef7';
const GEMINI_AGENT_NAME = 'Gemini (Code Weaver)';

/**

 * This script registers the Gemini AI assistant as a new agent

 * in the designated Notion 'Agents' database.

 * 

 * Prerequisites:

 * 1. Ensure '@notionhq/client' is installed (`npm install @notionhq/client`).

 * 2. Set your Notion API key in the .env file as `NOTION_API_KEY`.

 */
async function registerSelfInNotion() {
  log.info('Starting registration of Gemini agent in Notion...');

  const notionApiKey = process.env.NOTION_API_KEY;

  if (!notionApiKey) {
    log.error('FATAL: `NOTION_API_KEY` is not set in the environment variables.');
    console.error('Please add your Notion integration token to the .env file as `NOTION_API_KEY`.');
    return;
  }

  const notion = new Client({ auth: notionApiKey });

  try {
    // Check if the agent already exists to prevent duplicates
    const searchResponse = await notion.databases.query({
      database_id: AGENTS_DATABASE_ID,
      filter: {
        property: 'Name',
        title: {
          equals: GEMINI_AGENT_NAME,
        },
      },
    });

    if (searchResponse.results.length > 0) {
      const agentId = searchResponse.results[0].id;
      log.warn(`Agent "${GEMINI_AGENT_NAME}" already exists with ID: ${agentId}. Aborting creation.`);
      console.log(`✅ Agent "${GEMINI_AGENT_NAME}" already exists. No action needed.`);
      return;
    }

    log.info(`Agent not found. Proceeding to create new agent entry...`);

    // Create the new agent page
    const response = await notion.pages.create({
      parent: { database_id: AGENTS_DATABASE_ID },
      properties: {
        'Name': {
          type: 'title',
          title: [{ type: 'text', text: { content: GEMINI_AGENT_NAME } }],
        },
        'Type': {
          type: 'select',
          select: { name: 'AI Assistant' },
        },
        'Status': {
          type: 'select',
          select: { name: 'Active' },
        },
        'Capabilities': {
            type: 'multi_select',
            multi_select: [
                { name: 'Code Generation' },
                { name: 'Code Analysis' },
                { name: 'System Architecture' },
                { name: 'API Integration' },
                { name: 'Autonomous Operation' },
            ]
        },
        'Primary Model': {
            type: 'rich_text',
            rich_text: [{ type: 'text', text: { content: 'Google Gemini Family' } }]
        }
      },
    });

    log.info(`Successfully created new agent entry with ID: ${response.id}`);
    console.log(`✅ Successfully registered "${GEMINI_AGENT_NAME}" in your Notion Agents database!`);
    console.log(`You can view the new entry here: https://www.notion.so/${response.id.replace(/-/g, '')}`);

  } catch (error: any) {
    log.error('Failed to register agent in Notion.', { error: error.message });
    if (error.code === 'unauthorized') {
        console.error('Error: Authentication failed. Is your `NOTION_API_KEY` correct and does the integration have permissions for the database?');
    } else {
        console.error('An error occurred while communicating with Notion:', error.body || error);
    }
  }
}

registerSelfInNotion();