Files
WeHub Mirror 6bf8bebf51
CI / Test and Build (push) Failing after 1s
CI / Migrate Dev DB (push) Has been skipped
CI / Migrate DB (push) Has been skipped
CodeQL / Analyze actions (push) Has been cancelled
CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI / Detect Version (push) Has been cancelled
CI / Detect Desktop Changes (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/cron.Dockerfile, ubuntu-latest, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build AMD64 (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build AMD64 (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/cron.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/cron) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/db.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/pii.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-4vcpu-ubuntu-2404-arm, ./docker/realtime.Dockerfile, ubuntu-24.04-arm, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (blacksmith-8vcpu-ubuntu-2404-arm, ./docker/app.Dockerfile, linux-arm64-8-core, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
Helm Chart / Lint, test, and validate chart (push) Has been cancelled
Helm Chart / Chart version bumped (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
CI / Build Dev ECR (blacksmith-8vcpu-ubuntu-2404, ./docker/app.Dockerfile, ECR_APP, linux-x64-8-core) (push) Has been cancelled
CI / Promote Images (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/cron) (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 / Build Dev ECR (blacksmith-2vcpu-ubuntu-2404, ./docker/db.Dockerfile, ECR_MIGRATIONS, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/pii.Dockerfile, ECR_PII, ubuntu-latest) (push) Has been cancelled
CI / Build Dev ECR (blacksmith-4vcpu-ubuntu-2404, ./docker/realtime.Dockerfile, ECR_REALTIME, ubuntu-latest) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Check Desktop Signing Secrets (push) Has been cancelled
CI / Desktop Release (push) Has been cancelled
CI / Create Desktop Prerelease (push) Has been cancelled
CI / Desktop Prerelease Build (push) Has been cancelled
CI / Publish Desktop Prerelease (push) Has been cancelled
CI / Prune Desktop Prereleases (push) Has been cancelled
Helm Chart / Install on kind and run helm test (push) Has been cancelled
WeHub snapshot of cb28d14c6f2c081de7a0d8729a8c816c9adef67a
2026-08-10 11:17:50 +08:00

206 lines
5.7 KiB
TypeScript

import { toError } from '@sim/utils/errors'
import type { NotionQueryDatabaseParams, NotionResponse } from '@/tools/notion/types'
import { DATABASE_QUERY_RESULTS_OUTPUT, PAGINATION_OUTPUT_PROPERTIES } from '@/tools/notion/types'
import { extractTitle, formatPropertyValue } from '@/tools/notion/utils'
import type { ToolConfig } from '@/tools/types'
export const notionQueryDatabaseTool: ToolConfig<NotionQueryDatabaseParams, NotionResponse> = {
id: 'notion_query_database',
name: 'Query Notion Database',
description: 'Query and filter Notion database entries with advanced filtering',
version: '1.0.0',
oauth: {
required: true,
provider: 'notion',
},
params: {
accessToken: {
type: 'string',
required: true,
visibility: 'hidden',
description: 'Notion OAuth access token',
},
databaseId: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'The UUID of the Notion database to query',
},
filter: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Filter conditions as JSON (optional)',
},
sorts: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Sort criteria as JSON array (optional)',
},
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: (params: NotionQueryDatabaseParams) => {
return `https://api.notion.com/v1/databases/${params.databaseId.trim()}/query`
},
method: 'POST',
headers: (params: NotionQueryDatabaseParams) => {
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: NotionQueryDatabaseParams) => {
const body: any = {}
// Add filter if provided
if (params.filter) {
try {
body.filter = JSON.parse(params.filter)
} catch (error) {
throw new Error(`Invalid filter JSON: ${toError(error).message}`)
}
}
// Add sorts if provided
if (params.sorts) {
try {
body.sorts = JSON.parse(params.sorts)
} catch (error) {
throw new Error(`Invalid sorts JSON: ${toError(error).message}`)
}
}
// 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((page: any, index: number) => {
const properties = page.properties || {}
const title = extractTitle(properties)
const propertyValues = Object.entries(properties)
.map(([key, value]: [string, any]) => {
const formattedValue = formatPropertyValue(value)
return ` ${key}: ${formattedValue}`
})
.join('\n')
return `Entry ${index + 1}${title ? ` - ${title}` : ''}:\n${propertyValues}`
})
.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 database entries with their properties',
},
metadata: {
type: 'object',
description:
'Query 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: DATABASE_QUERY_RESULTS_OUTPUT,
},
},
},
}
// V2 Tool with API-aligned outputs
interface NotionQueryDatabaseV2Response {
success: boolean
output: {
results: any[]
has_more: boolean
next_cursor: string | null
total_results: number
}
}
export const notionQueryDatabaseV2Tool: ToolConfig<
NotionQueryDatabaseParams,
NotionQueryDatabaseV2Response
> = {
id: 'notion_query_database_v2',
name: 'Query Notion Database',
description: 'Query and filter Notion database entries with advanced filtering',
version: '2.0.0',
oauth: notionQueryDatabaseTool.oauth,
params: notionQueryDatabaseTool.params,
request: notionQueryDatabaseTool.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: DATABASE_QUERY_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' },
},
}