Compare commits

..

1 Commits

Author SHA1 Message Date
waleed
09a0f5af05 fix(agent): always fetch latest custom tool from DB when customToolId is present 2026-02-12 11:14:11 -08:00
10 changed files with 318 additions and 701 deletions

View File

@@ -1,198 +0,0 @@
import { GoogleBooksIcon } from '@/components/icons'
import type { BlockConfig } from '@/blocks/types'
export const GoogleBooksBlock: BlockConfig = {
type: 'google_books',
name: 'Google Books',
description: 'Search and retrieve book information',
longDescription:
'Search for books using the Google Books API. Find volumes by title, author, ISBN, or keywords, and retrieve detailed information about specific books including descriptions, ratings, and publication details.',
docsLink: 'https://docs.sim.ai/tools/google_books',
category: 'tools',
bgColor: '#4285F4',
icon: GoogleBooksIcon,
subBlocks: [
{
id: 'operation',
title: 'Operation',
type: 'dropdown',
options: [
{ label: 'Search Volumes', id: 'volume_search' },
{ label: 'Get Volume Details', id: 'volume_details' },
],
value: () => 'volume_search',
},
{
id: 'apiKey',
title: 'API Key',
type: 'short-input',
password: true,
placeholder: 'Enter your Google Books API key',
required: true,
},
{
id: 'query',
title: 'Search Query',
type: 'short-input',
placeholder: 'e.g., intitle:harry potter inauthor:rowling',
condition: { field: 'operation', value: 'volume_search' },
required: { field: 'operation', value: 'volume_search' },
},
{
id: 'filter',
title: 'Filter',
type: 'dropdown',
options: [
{ label: 'None', id: '' },
{ label: 'Partial Preview', id: 'partial' },
{ label: 'Full Preview', id: 'full' },
{ label: 'Free eBooks', id: 'free-ebooks' },
{ label: 'Paid eBooks', id: 'paid-ebooks' },
{ label: 'All eBooks', id: 'ebooks' },
],
condition: { field: 'operation', value: 'volume_search' },
mode: 'advanced',
},
{
id: 'printType',
title: 'Print Type',
type: 'dropdown',
options: [
{ label: 'All', id: 'all' },
{ label: 'Books', id: 'books' },
{ label: 'Magazines', id: 'magazines' },
],
value: () => 'all',
condition: { field: 'operation', value: 'volume_search' },
mode: 'advanced',
},
{
id: 'orderBy',
title: 'Order By',
type: 'dropdown',
options: [
{ label: 'Relevance', id: 'relevance' },
{ label: 'Newest', id: 'newest' },
],
value: () => 'relevance',
condition: { field: 'operation', value: 'volume_search' },
mode: 'advanced',
},
{
id: 'maxResults',
title: 'Max Results',
type: 'short-input',
placeholder: 'Number of results (1-40)',
condition: { field: 'operation', value: 'volume_search' },
mode: 'advanced',
},
{
id: 'startIndex',
title: 'Start Index',
type: 'short-input',
placeholder: 'Starting index for pagination',
condition: { field: 'operation', value: 'volume_search' },
mode: 'advanced',
},
{
id: 'langRestrict',
title: 'Language',
type: 'short-input',
placeholder: 'ISO 639-1 code (e.g., en, es, fr)',
condition: { field: 'operation', value: 'volume_search' },
mode: 'advanced',
},
{
id: 'volumeId',
title: 'Volume ID',
type: 'short-input',
placeholder: 'Google Books volume ID',
condition: { field: 'operation', value: 'volume_details' },
required: { field: 'operation', value: 'volume_details' },
},
{
id: 'projection',
title: 'Projection',
type: 'dropdown',
options: [
{ label: 'Full', id: 'full' },
{ label: 'Lite', id: 'lite' },
],
value: () => 'full',
mode: 'advanced',
},
],
tools: {
access: ['google_books_volume_search', 'google_books_volume_details'],
config: {
tool: (params) => `google_books_${params.operation}`,
params: (params) => {
const { operation, ...rest } = params
let maxResults: number | undefined
if (params.maxResults) {
maxResults = Number.parseInt(params.maxResults, 10)
if (Number.isNaN(maxResults)) {
maxResults = undefined
}
}
let startIndex: number | undefined
if (params.startIndex) {
startIndex = Number.parseInt(params.startIndex, 10)
if (Number.isNaN(startIndex)) {
startIndex = undefined
}
}
return {
...rest,
maxResults,
startIndex,
filter: params.filter || undefined,
printType: params.printType || undefined,
orderBy: params.orderBy || undefined,
projection: params.projection || undefined,
}
},
},
},
inputs: {
operation: { type: 'string', description: 'Operation to perform' },
apiKey: { type: 'string', description: 'Google Books API key' },
query: { type: 'string', description: 'Search query' },
filter: { type: 'string', description: 'Filter by availability' },
printType: { type: 'string', description: 'Print type filter' },
orderBy: { type: 'string', description: 'Sort order' },
maxResults: { type: 'string', description: 'Maximum number of results' },
startIndex: { type: 'string', description: 'Starting index for pagination' },
langRestrict: { type: 'string', description: 'Language restriction' },
volumeId: { type: 'string', description: 'Volume ID for details' },
projection: { type: 'string', description: 'Projection level' },
},
outputs: {
totalItems: { type: 'number', description: 'Total number of matching results' },
volumes: { type: 'json', description: 'List of matching volumes' },
id: { type: 'string', description: 'Volume ID' },
title: { type: 'string', description: 'Book title' },
subtitle: { type: 'string', description: 'Book subtitle' },
authors: { type: 'json', description: 'List of authors' },
publisher: { type: 'string', description: 'Publisher name' },
publishedDate: { type: 'string', description: 'Publication date' },
description: { type: 'string', description: 'Book description' },
pageCount: { type: 'number', description: 'Number of pages' },
categories: { type: 'json', description: 'Book categories' },
averageRating: { type: 'number', description: 'Average rating (1-5)' },
ratingsCount: { type: 'number', description: 'Number of ratings' },
language: { type: 'string', description: 'Language code' },
previewLink: { type: 'string', description: 'Link to preview on Google Books' },
infoLink: { type: 'string', description: 'Link to info page' },
thumbnailUrl: { type: 'string', description: 'Book cover thumbnail URL' },
isbn10: { type: 'string', description: 'ISBN-10 identifier' },
isbn13: { type: 'string', description: 'ISBN-13 identifier' },
},
}

View File

@@ -40,7 +40,6 @@ import { GitLabBlock } from '@/blocks/blocks/gitlab'
import { GmailBlock, GmailV2Block } from '@/blocks/blocks/gmail' import { GmailBlock, GmailV2Block } from '@/blocks/blocks/gmail'
import { GoogleSearchBlock } from '@/blocks/blocks/google' import { GoogleSearchBlock } from '@/blocks/blocks/google'
import { GoogleCalendarBlock, GoogleCalendarV2Block } from '@/blocks/blocks/google_calendar' import { GoogleCalendarBlock, GoogleCalendarV2Block } from '@/blocks/blocks/google_calendar'
import { GoogleBooksBlock } from '@/blocks/blocks/google_books'
import { GoogleDocsBlock } from '@/blocks/blocks/google_docs' import { GoogleDocsBlock } from '@/blocks/blocks/google_docs'
import { GoogleDriveBlock } from '@/blocks/blocks/google_drive' import { GoogleDriveBlock } from '@/blocks/blocks/google_drive'
import { GoogleFormsBlock } from '@/blocks/blocks/google_forms' import { GoogleFormsBlock } from '@/blocks/blocks/google_forms'
@@ -215,7 +214,6 @@ export const registry: Record<string, BlockConfig> = {
gmail_v2: GmailV2Block, gmail_v2: GmailV2Block,
google_calendar: GoogleCalendarBlock, google_calendar: GoogleCalendarBlock,
google_calendar_v2: GoogleCalendarV2Block, google_calendar_v2: GoogleCalendarV2Block,
google_books: GoogleBooksBlock,
google_docs: GoogleDocsBlock, google_docs: GoogleDocsBlock,
google_drive: GoogleDriveBlock, google_drive: GoogleDriveBlock,
google_forms: GoogleFormsBlock, google_forms: GoogleFormsBlock,

View File

@@ -1157,19 +1157,6 @@ export function AirweaveIcon(props: SVGProps<SVGSVGElement>) {
) )
} }
export function GoogleBooksIcon(props: SVGProps<SVGSVGElement>) {
return (
<svg {...props} xmlns='http://www.w3.org/2000/svg' viewBox='0 0 48 48'>
<path
fill='#4285F4'
d='M38 44H14c-2.2 0-4-1.8-4-4V8c0-2.2 1.8-4 4-4h18l10 10v26c0 2.2-1.8 4-4 4z'
/>
<path fill='#A0C3FF' d='M32 4v10h10L32 4z' />
<path fill='#FFFFFF' d='M16 20h16v2H16zm0 5h16v2H16zm0 5h12v2H16z' />
</svg>
)
}
export function GoogleDocsIcon(props: SVGProps<SVGSVGElement>) { export function GoogleDocsIcon(props: SVGProps<SVGSVGElement>) {
return ( return (
<svg <svg

View File

@@ -1901,5 +1901,317 @@ describe('AgentBlockHandler', () => {
expect(discoveryCalls[0].url).toContain('serverId=mcp-legacy-server') expect(discoveryCalls[0].url).toContain('serverId=mcp-legacy-server')
}) })
describe('customToolId resolution - DB as source of truth', () => {
const staleInlineSchema = {
function: {
name: 'buttonTemplate',
description: 'Creates a button template',
parameters: {
type: 'object',
properties: {
sender_id: { type: 'string', description: 'Sender ID' },
header_value: { type: 'string', description: 'Header text' },
body_value: { type: 'string', description: 'Body text' },
button_array: {
type: 'array',
items: { type: 'string' },
description: 'Button labels',
},
},
required: ['sender_id', 'header_value', 'body_value', 'button_array'],
},
},
}
const dbSchema = {
function: {
name: 'buttonTemplate',
description: 'Creates a button template',
parameters: {
type: 'object',
properties: {
sender_id: { type: 'string', description: 'Sender ID' },
header_value: { type: 'string', description: 'Header text' },
body_value: { type: 'string', description: 'Body text' },
button_array: {
type: 'array',
items: { type: 'string' },
description: 'Button labels',
},
channel: { type: 'string', description: 'Channel name' },
},
required: ['sender_id', 'header_value', 'body_value', 'button_array', 'channel'],
},
},
}
const staleInlineCode =
'return JSON.stringify({ type: "button", phone: sender_id, header: header_value, body: body_value, buttons: button_array });'
const dbCode =
'if (channel === "whatsapp") { return JSON.stringify({ type: "button", phone: sender_id, header: header_value, body: body_value, buttons: button_array }); }'
function mockFetchForCustomTool(toolId: string) {
mockFetch.mockImplementation((url: string) => {
if (typeof url === 'string' && url.includes('/api/tools/custom')) {
return Promise.resolve({
ok: true,
headers: { get: () => null },
json: () =>
Promise.resolve({
data: [
{
id: toolId,
title: 'buttonTemplate',
schema: dbSchema,
code: dbCode,
},
],
}),
})
}
return Promise.resolve({
ok: true,
headers: { get: () => null },
json: () => Promise.resolve({}),
})
})
}
function mockFetchFailure() {
mockFetch.mockImplementation((url: string) => {
if (typeof url === 'string' && url.includes('/api/tools/custom')) {
return Promise.resolve({
ok: false,
status: 500,
headers: { get: () => null },
json: () => Promise.resolve({}),
})
}
return Promise.resolve({
ok: true,
headers: { get: () => null },
json: () => Promise.resolve({}),
})
})
}
beforeEach(() => {
Object.defineProperty(global, 'window', {
value: undefined,
writable: true,
configurable: true,
})
})
it('should always fetch latest schema from DB when customToolId is present', async () => {
const toolId = 'custom-tool-123'
mockFetchForCustomTool(toolId)
const inputs = {
model: 'gpt-4o',
userPrompt: 'Send a button template',
apiKey: 'test-api-key',
tools: [
{
type: 'custom-tool',
customToolId: toolId,
title: 'buttonTemplate',
schema: staleInlineSchema,
code: staleInlineCode,
usageControl: 'auto' as const,
},
],
}
mockGetProviderFromModel.mockReturnValue('openai')
await handler.execute(mockContext, mockBlock, inputs)
expect(mockExecuteProviderRequest).toHaveBeenCalled()
const providerCall = mockExecuteProviderRequest.mock.calls[0]
const tools = providerCall[1].tools
expect(tools.length).toBe(1)
// DB schema wins over stale inline — includes channel param
expect(tools[0].parameters.required).toContain('channel')
expect(tools[0].parameters.properties).toHaveProperty('channel')
})
it('should fetch from DB when customToolId has no inline schema', async () => {
const toolId = 'custom-tool-123'
mockFetchForCustomTool(toolId)
const inputs = {
model: 'gpt-4o',
userPrompt: 'Send a button template',
apiKey: 'test-api-key',
tools: [
{
type: 'custom-tool',
customToolId: toolId,
usageControl: 'auto' as const,
},
],
}
mockGetProviderFromModel.mockReturnValue('openai')
await handler.execute(mockContext, mockBlock, inputs)
expect(mockExecuteProviderRequest).toHaveBeenCalled()
const providerCall = mockExecuteProviderRequest.mock.calls[0]
const tools = providerCall[1].tools
expect(tools.length).toBe(1)
expect(tools[0].name).toBe('buttonTemplate')
expect(tools[0].parameters.required).toContain('channel')
})
it('should fall back to inline schema when DB fetch fails and inline exists', async () => {
mockFetchFailure()
const inputs = {
model: 'gpt-4o',
userPrompt: 'Send a button template',
apiKey: 'test-api-key',
tools: [
{
type: 'custom-tool',
customToolId: 'custom-tool-123',
title: 'buttonTemplate',
schema: staleInlineSchema,
code: staleInlineCode,
usageControl: 'auto' as const,
},
],
}
mockGetProviderFromModel.mockReturnValue('openai')
await handler.execute(mockContext, mockBlock, inputs)
expect(mockExecuteProviderRequest).toHaveBeenCalled()
const providerCall = mockExecuteProviderRequest.mock.calls[0]
const tools = providerCall[1].tools
// Falls back to inline schema (4 params, no channel)
expect(tools.length).toBe(1)
expect(tools[0].name).toBe('buttonTemplate')
expect(tools[0].parameters.required).not.toContain('channel')
})
it('should return null when DB fetch fails and no inline schema exists', async () => {
mockFetchFailure()
const inputs = {
model: 'gpt-4o',
userPrompt: 'Send a button template',
apiKey: 'test-api-key',
tools: [
{
type: 'custom-tool',
customToolId: 'custom-tool-123',
usageControl: 'auto' as const,
},
],
}
mockGetProviderFromModel.mockReturnValue('openai')
await handler.execute(mockContext, mockBlock, inputs)
expect(mockExecuteProviderRequest).toHaveBeenCalled()
const providerCall = mockExecuteProviderRequest.mock.calls[0]
const tools = providerCall[1].tools
expect(tools.length).toBe(0)
})
it('should use DB code for executeFunction when customToolId resolves', async () => {
const toolId = 'custom-tool-123'
mockFetchForCustomTool(toolId)
let capturedTools: any[] = []
Promise.all = vi.fn().mockImplementation((promises: Promise<any>[]) => {
const result = originalPromiseAll.call(Promise, promises)
result.then((tools: any[]) => {
if (tools?.length) {
capturedTools = tools.filter((t) => t !== null)
}
})
return result
})
const inputs = {
model: 'gpt-4o',
userPrompt: 'Send a button template',
apiKey: 'test-api-key',
tools: [
{
type: 'custom-tool',
customToolId: toolId,
title: 'buttonTemplate',
schema: staleInlineSchema,
code: staleInlineCode,
usageControl: 'auto' as const,
},
],
}
mockGetProviderFromModel.mockReturnValue('openai')
await handler.execute(mockContext, mockBlock, inputs)
expect(capturedTools.length).toBe(1)
expect(typeof capturedTools[0].executeFunction).toBe('function')
await capturedTools[0].executeFunction({ sender_id: '123', channel: 'whatsapp' })
// Should use DB code, not stale inline code
expect(mockExecuteTool).toHaveBeenCalledWith(
'function_execute',
expect.objectContaining({
code: dbCode,
}),
false,
expect.any(Object)
)
})
it('should not fetch from DB when no customToolId is present', async () => {
const inputs = {
model: 'gpt-4o',
userPrompt: 'Use the tool',
apiKey: 'test-api-key',
tools: [
{
type: 'custom-tool',
title: 'inlineTool',
schema: staleInlineSchema,
code: staleInlineCode,
usageControl: 'auto' as const,
},
],
}
mockGetProviderFromModel.mockReturnValue('openai')
await handler.execute(mockContext, mockBlock, inputs)
const customToolFetches = mockFetch.mock.calls.filter(
(call: any[]) => typeof call[0] === 'string' && call[0].includes('/api/tools/custom')
)
expect(customToolFetches.length).toBe(0)
expect(mockExecuteProviderRequest).toHaveBeenCalled()
const providerCall = mockExecuteProviderRequest.mock.calls[0]
const tools = providerCall[1].tools
expect(tools.length).toBe(1)
expect(tools[0].name).toBe('buttonTemplate')
expect(tools[0].parameters.required).not.toContain('channel')
})
})
}) })
}) })

View File

@@ -272,15 +272,16 @@ export class AgentBlockHandler implements BlockHandler {
let code = tool.code let code = tool.code
let title = tool.title let title = tool.title
if (tool.customToolId && !schema) { if (tool.customToolId) {
const resolved = await this.fetchCustomToolById(ctx, tool.customToolId) const resolved = await this.fetchCustomToolById(ctx, tool.customToolId)
if (!resolved) { if (resolved) {
schema = resolved.schema
code = resolved.code
title = resolved.title
} else if (!schema) {
logger.error(`Custom tool not found: ${tool.customToolId}`) logger.error(`Custom tool not found: ${tool.customToolId}`)
return null return null
} }
schema = resolved.schema
code = resolved.code
title = resolved.title
} }
if (!schema?.function) { if (!schema?.function) {

View File

@@ -1,3 +0,0 @@
export * from './types'
export { googleBooksVolumeDetailsTool } from './volume_details'
export { googleBooksVolumeSearchTool } from './volume_search'

View File

@@ -1,64 +0,0 @@
import type { ToolResponse } from '@/tools/types'
/**
* Volume information structure shared between search and details responses
*/
export interface VolumeInfo {
id: string
title: string
subtitle: string | null
authors: string[]
publisher: string | null
publishedDate: string | null
description: string | null
pageCount: number | null
categories: string[]
averageRating: number | null
ratingsCount: number | null
language: string | null
previewLink: string | null
infoLink: string | null
thumbnailUrl: string | null
isbn10: string | null
isbn13: string | null
}
/**
* Parameters for searching volumes
*/
export interface GoogleBooksVolumeSearchParams {
apiKey: string
query: string
filter?: 'partial' | 'full' | 'free-ebooks' | 'paid-ebooks' | 'ebooks'
printType?: 'all' | 'books' | 'magazines'
orderBy?: 'relevance' | 'newest'
startIndex?: number
maxResults?: number
langRestrict?: string
}
/**
* Response from volume search
*/
export interface GoogleBooksVolumeSearchResponse extends ToolResponse {
output: {
totalItems: number
volumes: VolumeInfo[]
}
}
/**
* Parameters for getting volume details
*/
export interface GoogleBooksVolumeDetailsParams {
apiKey: string
volumeId: string
projection?: 'full' | 'lite'
}
/**
* Response from volume details
*/
export interface GoogleBooksVolumeDetailsResponse extends ToolResponse {
output: VolumeInfo
}

View File

@@ -1,205 +0,0 @@
import type {
GoogleBooksVolumeDetailsParams,
GoogleBooksVolumeDetailsResponse,
} from '@/tools/google_books/types'
import type { ToolConfig } from '@/tools/types'
interface GoogleBooksVolumeResponse {
id: string
volumeInfo: {
title?: string
subtitle?: string
authors?: string[]
publisher?: string
publishedDate?: string
description?: string
pageCount?: number
categories?: string[]
averageRating?: number
ratingsCount?: number
language?: string
previewLink?: string
infoLink?: string
imageLinks?: {
thumbnail?: string
smallThumbnail?: string
}
industryIdentifiers?: Array<{
type: string
identifier: string
}>
}
error?: {
message?: string
}
}
export const googleBooksVolumeDetailsTool: ToolConfig<
GoogleBooksVolumeDetailsParams,
GoogleBooksVolumeDetailsResponse
> = {
id: 'google_books_volume_details',
name: 'Google Books Volume Details',
description: 'Get detailed information about a specific book volume',
version: '1.0.0',
params: {
apiKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'Google Books API key',
},
volumeId: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description: 'The ID of the volume to retrieve',
},
projection: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Projection level (full, lite)',
},
},
request: {
url: (params) => {
const url = new URL(`https://www.googleapis.com/books/v1/volumes/${params.volumeId.trim()}`)
url.searchParams.set('key', params.apiKey.trim())
if (params.projection) {
url.searchParams.set('projection', params.projection)
}
return url.toString()
},
method: 'GET',
headers: () => ({
'Content-Type': 'application/json',
}),
},
transformResponse: async (response: Response) => {
const data: GoogleBooksVolumeResponse = await response.json()
if (data.error) {
throw new Error(`Google Books API error: ${data.error.message || 'Unknown error'}`)
}
if (!data.volumeInfo) {
throw new Error('Volume not found')
}
const info = data.volumeInfo
const identifiers = info.industryIdentifiers ?? []
return {
success: true,
output: {
id: data.id,
title: info.title ?? '',
subtitle: info.subtitle ?? null,
authors: info.authors ?? [],
publisher: info.publisher ?? null,
publishedDate: info.publishedDate ?? null,
description: info.description ?? null,
pageCount: info.pageCount ?? null,
categories: info.categories ?? [],
averageRating: info.averageRating ?? null,
ratingsCount: info.ratingsCount ?? null,
language: info.language ?? null,
previewLink: info.previewLink ?? null,
infoLink: info.infoLink ?? null,
thumbnailUrl: info.imageLinks?.thumbnail ?? info.imageLinks?.smallThumbnail ?? null,
isbn10: identifiers.find((id) => id.type === 'ISBN_10')?.identifier ?? null,
isbn13: identifiers.find((id) => id.type === 'ISBN_13')?.identifier ?? null,
},
}
},
outputs: {
id: {
type: 'string',
description: 'Volume ID',
},
title: {
type: 'string',
description: 'Book title',
},
subtitle: {
type: 'string',
description: 'Book subtitle',
optional: true,
},
authors: {
type: 'array',
description: 'List of authors',
},
publisher: {
type: 'string',
description: 'Publisher name',
optional: true,
},
publishedDate: {
type: 'string',
description: 'Publication date',
optional: true,
},
description: {
type: 'string',
description: 'Book description',
optional: true,
},
pageCount: {
type: 'number',
description: 'Number of pages',
optional: true,
},
categories: {
type: 'array',
description: 'Book categories',
},
averageRating: {
type: 'number',
description: 'Average rating (1-5)',
optional: true,
},
ratingsCount: {
type: 'number',
description: 'Number of ratings',
optional: true,
},
language: {
type: 'string',
description: 'Language code',
optional: true,
},
previewLink: {
type: 'string',
description: 'Link to preview on Google Books',
optional: true,
},
infoLink: {
type: 'string',
description: 'Link to info page',
optional: true,
},
thumbnailUrl: {
type: 'string',
description: 'Book cover thumbnail URL',
optional: true,
},
isbn10: {
type: 'string',
description: 'ISBN-10 identifier',
optional: true,
},
isbn13: {
type: 'string',
description: 'ISBN-13 identifier',
optional: true,
},
},
}

View File

@@ -1,205 +0,0 @@
import type {
GoogleBooksVolumeSearchParams,
GoogleBooksVolumeSearchResponse,
VolumeInfo,
} from '@/tools/google_books/types'
import type { ToolConfig } from '@/tools/types'
interface GoogleBooksVolumeItem {
id: string
volumeInfo: {
title?: string
subtitle?: string
authors?: string[]
publisher?: string
publishedDate?: string
description?: string
pageCount?: number
categories?: string[]
averageRating?: number
ratingsCount?: number
language?: string
previewLink?: string
infoLink?: string
imageLinks?: {
thumbnail?: string
smallThumbnail?: string
}
industryIdentifiers?: Array<{
type: string
identifier: string
}>
}
}
function extractVolumeInfo(item: GoogleBooksVolumeItem): VolumeInfo {
const info = item.volumeInfo
const identifiers = info.industryIdentifiers ?? []
return {
id: item.id,
title: info.title ?? '',
subtitle: info.subtitle ?? null,
authors: info.authors ?? [],
publisher: info.publisher ?? null,
publishedDate: info.publishedDate ?? null,
description: info.description ?? null,
pageCount: info.pageCount ?? null,
categories: info.categories ?? [],
averageRating: info.averageRating ?? null,
ratingsCount: info.ratingsCount ?? null,
language: info.language ?? null,
previewLink: info.previewLink ?? null,
infoLink: info.infoLink ?? null,
thumbnailUrl: info.imageLinks?.thumbnail ?? info.imageLinks?.smallThumbnail ?? null,
isbn10: identifiers.find((id) => id.type === 'ISBN_10')?.identifier ?? null,
isbn13: identifiers.find((id) => id.type === 'ISBN_13')?.identifier ?? null,
}
}
export const googleBooksVolumeSearchTool: ToolConfig<
GoogleBooksVolumeSearchParams,
GoogleBooksVolumeSearchResponse
> = {
id: 'google_books_volume_search',
name: 'Google Books Volume Search',
description: 'Search for books using the Google Books API',
version: '1.0.0',
params: {
apiKey: {
type: 'string',
required: true,
visibility: 'user-only',
description: 'Google Books API key',
},
query: {
type: 'string',
required: true,
visibility: 'user-or-llm',
description:
'Search query. Supports special keywords: intitle:, inauthor:, inpublisher:, subject:, isbn:',
},
filter: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Filter results by availability (partial, full, free-ebooks, paid-ebooks, ebooks)',
},
printType: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Restrict to print type (all, books, magazines)',
},
orderBy: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Sort order (relevance, newest)',
},
startIndex: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'Index of the first result to return (for pagination)',
},
maxResults: {
type: 'number',
required: false,
visibility: 'user-or-llm',
description: 'Maximum number of results to return (1-40)',
},
langRestrict: {
type: 'string',
required: false,
visibility: 'user-or-llm',
description: 'Restrict results to a specific language (ISO 639-1 code)',
},
},
request: {
url: (params) => {
const url = new URL('https://www.googleapis.com/books/v1/volumes')
url.searchParams.set('q', params.query.trim())
url.searchParams.set('key', params.apiKey.trim())
if (params.filter) {
url.searchParams.set('filter', params.filter)
}
if (params.printType) {
url.searchParams.set('printType', params.printType)
}
if (params.orderBy) {
url.searchParams.set('orderBy', params.orderBy)
}
if (params.startIndex !== undefined) {
url.searchParams.set('startIndex', String(params.startIndex))
}
if (params.maxResults !== undefined) {
url.searchParams.set('maxResults', String(params.maxResults))
}
if (params.langRestrict) {
url.searchParams.set('langRestrict', params.langRestrict)
}
return url.toString()
},
method: 'GET',
headers: () => ({
'Content-Type': 'application/json',
}),
},
transformResponse: async (response: Response) => {
const data = await response.json()
if (data.error) {
throw new Error(`Google Books API error: ${data.error.message || 'Unknown error'}`)
}
const items: GoogleBooksVolumeItem[] = data.items ?? []
const volumes = items.map(extractVolumeInfo)
return {
success: true,
output: {
totalItems: data.totalItems ?? 0,
volumes,
},
}
},
outputs: {
totalItems: {
type: 'number',
description: 'Total number of matching results',
},
volumes: {
type: 'array',
description: 'List of matching volumes',
items: {
type: 'object',
properties: {
id: { type: 'string', description: 'Volume ID' },
title: { type: 'string', description: 'Book title' },
subtitle: { type: 'string', description: 'Book subtitle' },
authors: { type: 'array', description: 'List of authors' },
publisher: { type: 'string', description: 'Publisher name' },
publishedDate: { type: 'string', description: 'Publication date' },
description: { type: 'string', description: 'Book description' },
pageCount: { type: 'number', description: 'Number of pages' },
categories: { type: 'array', description: 'Book categories' },
averageRating: { type: 'number', description: 'Average rating (1-5)' },
ratingsCount: { type: 'number', description: 'Number of ratings' },
language: { type: 'string', description: 'Language code' },
previewLink: { type: 'string', description: 'Link to preview on Google Books' },
infoLink: { type: 'string', description: 'Link to info page' },
thumbnailUrl: { type: 'string', description: 'Book cover thumbnail URL' },
isbn10: { type: 'string', description: 'ISBN-10 identifier' },
isbn13: { type: 'string', description: 'ISBN-13 identifier' },
},
},
},
},
}

View File

@@ -549,10 +549,6 @@ import {
googleCalendarUpdateV2Tool, googleCalendarUpdateV2Tool,
} from '@/tools/google_calendar' } from '@/tools/google_calendar'
import { googleDocsCreateTool, googleDocsReadTool, googleDocsWriteTool } from '@/tools/google_docs' import { googleDocsCreateTool, googleDocsReadTool, googleDocsWriteTool } from '@/tools/google_docs'
import {
googleBooksVolumeDetailsTool,
googleBooksVolumeSearchTool,
} from '@/tools/google_books'
import { import {
googleDriveCopyTool, googleDriveCopyTool,
googleDriveCreateFolderTool, googleDriveCreateFolderTool,
@@ -2560,8 +2556,6 @@ export const tools: Record<string, ToolConfig> = {
google_docs_read: googleDocsReadTool, google_docs_read: googleDocsReadTool,
google_docs_write: googleDocsWriteTool, google_docs_write: googleDocsWriteTool,
google_docs_create: googleDocsCreateTool, google_docs_create: googleDocsCreateTool,
google_books_volume_search: googleBooksVolumeSearchTool,
google_books_volume_details: googleBooksVolumeDetailsTool,
google_maps_air_quality: googleMapsAirQualityTool, google_maps_air_quality: googleMapsAirQualityTool,
google_maps_directions: googleMapsDirectionsTool, google_maps_directions: googleMapsDirectionsTool,
google_maps_distance_matrix: googleMapsDistanceMatrixTool, google_maps_distance_matrix: googleMapsDistanceMatrixTool,