|
| 1 | +import { Tool } from '@langchain/core/tools' |
| 2 | +import { ICommonObject, INode, INodeData, INodeParams } from '../../../src/Interface' |
| 3 | +import { getBaseClasses, getCredentialData, getCredentialParam } from '../../../src/utils' |
| 4 | + |
| 5 | +const defaultDesc = |
| 6 | + 'A context-engineered web search tool powered by Seltz. Useful for when you need to answer questions using fast, up-to-date web knowledge with sources for real-time AI reasoning. Input should be a search query. Output is a JSON array of documents with url and content fields.' |
| 7 | + |
| 8 | +class SeltzSearch_Tools implements INode { |
| 9 | + label: string |
| 10 | + name: string |
| 11 | + version: number |
| 12 | + description: string |
| 13 | + type: string |
| 14 | + icon: string |
| 15 | + category: string |
| 16 | + baseClasses: string[] |
| 17 | + credential: INodeParams |
| 18 | + inputs: INodeParams[] |
| 19 | + |
| 20 | + constructor() { |
| 21 | + this.label = 'Seltz Search' |
| 22 | + this.name = 'seltzSearch' |
| 23 | + this.version = 1.0 |
| 24 | + this.type = 'SeltzSearch' |
| 25 | + this.icon = 'seltz.svg' |
| 26 | + this.category = 'Tools' |
| 27 | + this.description = 'Wrapper around Seltz Web Knowledge API - context-engineered web content with sources for real-time AI reasoning' |
| 28 | + this.inputs = [ |
| 29 | + { |
| 30 | + label: 'Tool Description', |
| 31 | + name: 'toolDescription', |
| 32 | + type: 'string', |
| 33 | + rows: 4, |
| 34 | + default: defaultDesc, |
| 35 | + optional: true, |
| 36 | + additionalParams: true |
| 37 | + }, |
| 38 | + { |
| 39 | + label: 'Max Documents', |
| 40 | + name: 'maxDocuments', |
| 41 | + type: 'number', |
| 42 | + description: 'Maximum number of documents to return', |
| 43 | + optional: true, |
| 44 | + step: 1, |
| 45 | + additionalParams: true |
| 46 | + }, |
| 47 | + { |
| 48 | + label: 'Context', |
| 49 | + name: 'context', |
| 50 | + type: 'string', |
| 51 | + description: 'Additional context to refine the search', |
| 52 | + rows: 2, |
| 53 | + optional: true, |
| 54 | + additionalParams: true |
| 55 | + }, |
| 56 | + { |
| 57 | + label: 'Profile', |
| 58 | + name: 'profile', |
| 59 | + type: 'string', |
| 60 | + description: 'Search profile to use', |
| 61 | + optional: true, |
| 62 | + additionalParams: true |
| 63 | + } |
| 64 | + ] |
| 65 | + this.credential = { |
| 66 | + label: 'Connect Credential', |
| 67 | + name: 'credential', |
| 68 | + type: 'credential', |
| 69 | + credentialNames: ['seltzApi'] |
| 70 | + } |
| 71 | + this.baseClasses = [this.type, ...getBaseClasses(SeltzSearchTool)] |
| 72 | + } |
| 73 | + |
| 74 | + async init(nodeData: INodeData, _: string, options: ICommonObject): Promise<any> { |
| 75 | + const credentialData = await getCredentialData(nodeData.credential ?? '', options) |
| 76 | + const seltzApiKey = getCredentialParam('seltzApiKey', credentialData, nodeData) |
| 77 | + |
| 78 | + const toolDescription = nodeData.inputs?.toolDescription as string |
| 79 | + const maxDocuments = nodeData.inputs?.maxDocuments as string |
| 80 | + const context = nodeData.inputs?.context as string |
| 81 | + const profile = nodeData.inputs?.profile as string |
| 82 | + |
| 83 | + const tool = new SeltzSearchTool({ |
| 84 | + apiKey: seltzApiKey, |
| 85 | + maxDocuments: maxDocuments ? parseInt(maxDocuments, 10) : undefined, |
| 86 | + context: context || undefined, |
| 87 | + profile: profile || undefined |
| 88 | + }) |
| 89 | + |
| 90 | + if (toolDescription) tool.description = toolDescription |
| 91 | + |
| 92 | + return tool |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +class SeltzSearchTool extends Tool { |
| 97 | + static lc_name() { |
| 98 | + return 'SeltzSearchTool' |
| 99 | + } |
| 100 | + |
| 101 | + name = 'seltz-search' |
| 102 | + description = defaultDesc |
| 103 | + |
| 104 | + private apiKey: string |
| 105 | + private maxDocuments?: number |
| 106 | + private context?: string |
| 107 | + private profile?: string |
| 108 | + |
| 109 | + constructor({ apiKey, maxDocuments, context, profile }: { apiKey: string; maxDocuments?: number; context?: string; profile?: string }) { |
| 110 | + super() |
| 111 | + this.apiKey = apiKey |
| 112 | + this.maxDocuments = maxDocuments |
| 113 | + this.context = context |
| 114 | + this.profile = profile |
| 115 | + } |
| 116 | + |
| 117 | + async _call(input: string): Promise<string> { |
| 118 | + const { Seltz } = await import('seltz') |
| 119 | + const client = new Seltz({ apiKey: this.apiKey }) |
| 120 | + |
| 121 | + const searchOptions: Record<string, any> = {} |
| 122 | + if (this.maxDocuments) { |
| 123 | + searchOptions.includes = { maxDocuments: this.maxDocuments } |
| 124 | + } |
| 125 | + if (this.context) { |
| 126 | + searchOptions.context = this.context |
| 127 | + } |
| 128 | + if (this.profile) { |
| 129 | + searchOptions.profile = this.profile |
| 130 | + } |
| 131 | + |
| 132 | + const response = await client.search(input, Object.keys(searchOptions).length > 0 ? searchOptions : undefined) |
| 133 | + |
| 134 | + if (!response.documents || response.documents.length === 0) { |
| 135 | + return 'No results found.' |
| 136 | + } |
| 137 | + |
| 138 | + const results = response.documents.map((doc: any) => ({ |
| 139 | + url: doc.url || '', |
| 140 | + content: doc.content || '' |
| 141 | + })) |
| 142 | + |
| 143 | + return JSON.stringify(results) |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +module.exports = { nodeClass: SeltzSearch_Tools, SeltzSearchTool } |
0 commit comments