Files
simstudioai--sim/apps/sim/tools/notion/search.ts
T
wehub-resource-sync d25d482dc2
Publish CLI Package / publish-npm (push) Waiting to run
Publish Python SDK / publish-pypi (push) Waiting to run
Publish TypeScript SDK / publish-npm (push) Waiting to run
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

197 lines
5.4 KiB
TypeScript

import type { NotionResponse, NotionSearchParams } from '@/tools/notion/types'
import { PAGINATION_OUTPUT_PROPERTIES, SEARCH_RESULTS_OUTPUT } from '@/tools/notion/types'
import { extractTitleFromItem } from '@/tools/notion/utils'
import type { ToolConfig } from '@/tools/types'
export const notionSearchTool: ToolConfig<NotionSearchParams, NotionResponse> = {
id: 'notion_search',
name: 'Search Notion Workspace',
description: 'Search across all pages and databases in Notion workspace',
version: '1.0.0',
oauth: {
required: true,
provider: 'notion',
},
params: {
accessToken: {
type: 'string',
required: true,
visibility: 'hidden',
description: 'Notion OAuth access token',
},
query: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Search terms to find pages and databases (leave empty to get all pages)',
},
filterType: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Filter by object type: "page", "database", or leave empty for all',
},
pageSize: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'Number of results to return (default: 100, max: 100)',
},
startCursor: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Pagination cursor returned by a previous request',
},
},
request: {
url: () => 'https://api.notion.com/v1/search',
method: 'POST',
headers: (params: NotionSearchParams) => {
if (!params.accessToken) {
throw new Error('Access token is required')
}
return {
Authorization: `Bearer ${params.accessToken}`,
'Notion-Version': '2022-06-28',
'Content-Type': 'application/json',
}
},
body: (params: NotionSearchParams) => {
const body: any = {}
// Add query if provided
if (params.query?.trim()) {
body.query = params.query.trim()
}
// Add filter if provided (skip 'all' as it means no filter)
if (
params.filterType &&
params.filterType !== 'all' &&
['page', 'database'].includes(params.filterType)
) {
body.filter = {
value: params.filterType,
property: 'object',
}
}
// Add page size if provided
if (params.pageSize) {
body.page_size = Math.min(Number(params.pageSize), 100)
}
// Add pagination cursor if provided
if (params.startCursor) {
body.start_cursor = params.startCursor.trim()
}
return body
},
},
transformResponse: async (response: Response) => {
const data = await response.json()
const results = data.results || []
// Format the results into readable content
const content = results
.map((item: any, index: number) => {
const objectType = item.object === 'page' ? 'Page' : 'Database'
const title = extractTitleFromItem(item)
const url = item.url || ''
const lastEdited = item.last_edited_time
? new Date(item.last_edited_time).toLocaleDateString()
: ''
return [
`${index + 1}. ${objectType}: ${title}`,
` URL: ${url}`,
lastEdited ? ` Last edited: ${lastEdited}` : '',
]
.filter(Boolean)
.join('\n')
})
.join('\n\n')
return {
success: true,
output: {
content: content || 'No results found',
metadata: {
totalResults: results.length,
hasMore: data.has_more || false,
nextCursor: data.next_cursor || null,
results: results,
},
},
}
},
outputs: {
content: {
type: 'string',
description: 'Formatted list of search results including pages and databases',
},
metadata: {
type: 'object',
description:
'Search metadata including total results count, pagination info, and raw results array',
properties: {
totalResults: { type: 'number', description: 'Number of results returned' },
hasMore: PAGINATION_OUTPUT_PROPERTIES.has_more,
nextCursor: PAGINATION_OUTPUT_PROPERTIES.next_cursor,
results: SEARCH_RESULTS_OUTPUT,
},
},
},
}
// V2 Tool with API-aligned outputs
interface NotionSearchV2Response {
success: boolean
output: {
results: any[]
has_more: boolean
next_cursor: string | null
total_results: number
}
}
export const notionSearchV2Tool: ToolConfig<NotionSearchParams, NotionSearchV2Response> = {
id: 'notion_search_v2',
name: 'Search Notion Workspace',
description: 'Search across all pages and databases in Notion workspace',
version: '2.0.0',
oauth: notionSearchTool.oauth,
params: notionSearchTool.params,
request: notionSearchTool.request,
transformResponse: async (response: Response) => {
const data = await response.json()
const results = data.results || []
return {
success: true,
output: {
results,
has_more: data.has_more || false,
next_cursor: data.next_cursor || null,
total_results: results.length,
},
}
},
outputs: {
results: SEARCH_RESULTS_OUTPUT,
has_more: PAGINATION_OUTPUT_PROPERTIES.has_more,
next_cursor: PAGINATION_OUTPUT_PROPERTIES.next_cursor,
total_results: { type: 'number', description: 'Number of results returned' },
},
}