Skip to content

Commit 49254ab

Browse files
committed
added input validation, tested persistence of KB selector
1 parent 7381b51 commit 49254ab

File tree

3 files changed

+41
-15
lines changed

3 files changed

+41
-15
lines changed

apps/sim/blocks/blocks/knowledge.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export const KnowledgeBlock: BlockConfig = {
66
name: 'Knowledge',
77
description: 'Use vector search',
88
longDescription:
9-
'Perform semantic vector search across one or more knowledge bases or upload new chunks to documents. Uses advanced AI embeddings to understand meaning and context for search operations.',
9+
'Perform semantic vector search across knowledge bases, upload individual chunks to existing documents, or create new documents from text content. Uses advanced AI embeddings to understand meaning and context for search operations.',
1010
bgColor: '#00B0B0',
1111
icon: PackageSearchIcon,
1212
category: 'blocks',
@@ -115,15 +115,15 @@ export const KnowledgeBlock: BlockConfig = {
115115
title: 'Document Name',
116116
type: 'short-input',
117117
layout: 'full',
118-
placeholder: 'Enter the document name to create',
118+
placeholder: 'Enter document name',
119119
condition: { field: 'operation', value: ['create_document'] },
120120
},
121121
{
122122
id: 'content',
123123
title: 'Document Content',
124124
type: 'long-input',
125125
layout: 'full',
126-
placeholder: 'Enter the document content to create',
126+
placeholder: 'Enter the document content',
127127
rows: 6,
128128
condition: { field: 'operation', value: ['create_document'] },
129129
},

apps/sim/providers/utils.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ describe('Model Capabilities', () => {
110110
it.concurrent('should return true for models that support temperature', () => {
111111
const supportedModels = [
112112
'gpt-4o',
113+
'gpt-4.1',
114+
'gpt-4.1-mini',
115+
'gpt-4.1-nano',
113116
'gemini-2.5-flash',
114117
'claude-sonnet-4-0',
115118
'claude-opus-4-0',
@@ -139,10 +142,6 @@ describe('Model Capabilities', () => {
139142
'deepseek-r1',
140143
// Chat models that don't support temperature
141144
'deepseek-chat',
142-
// GPT-4.1 family models that don't support temperature
143-
'gpt-4.1',
144-
'gpt-4.1-nano',
145-
'gpt-4.1-mini',
146145
'azure/gpt-4.1',
147146
'azure/model-router',
148147
]

apps/sim/tools/knowledge/create_document.ts

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,39 @@ export const knowledgeCreateDocumentTool: ToolConfig<any, KnowledgeCreateDocumen
3030
'Content-Type': 'application/json',
3131
}),
3232
body: (params) => {
33-
// Create document from text content
34-
const textContent = params.content
35-
const documentName = params.name
33+
const textContent = params.content?.trim()
34+
const documentName = params.name?.trim()
35+
36+
if (!documentName || documentName.length === 0) {
37+
throw new Error('Document name is required')
38+
}
39+
if (documentName.length > 255) {
40+
throw new Error('Document name must be 255 characters or less')
41+
}
42+
if (/[<>:"/\\|?*]/.test(documentName)) {
43+
throw new Error('Document name contains invalid characters. Avoid: < > : " / \\ | ? *')
44+
}
45+
if (!textContent || textContent.length < 10) {
46+
throw new Error('Document content must be at least 10 characters long')
47+
}
48+
if (textContent.length > 1000000) {
49+
throw new Error('Document content exceeds maximum size of 1MB')
50+
}
3651

37-
// Calculate content metrics
3852
const contentBytes = new TextEncoder().encode(textContent).length
3953

40-
// Properly encode UTF-8 text to base64
4154
const utf8Bytes = new TextEncoder().encode(textContent)
4255
const base64Content =
4356
typeof Buffer !== 'undefined'
4457
? Buffer.from(textContent, 'utf8').toString('base64')
4558
: btoa(String.fromCharCode(...utf8Bytes))
4659

47-
// Create data URI for text content
4860
const dataUri = `data:text/plain;base64,${base64Content}`
4961

5062
const documents = [
5163
{
5264
filename: documentName.endsWith('.txt') ? documentName : `${documentName}.txt`,
53-
fileUrl: dataUri, // or handle as direct text content
65+
fileUrl: dataUri,
5466
fileSize: contentBytes,
5567
mimeType: 'text/plain',
5668
},
@@ -125,7 +137,22 @@ export const knowledgeCreateDocumentTool: ToolConfig<any, KnowledgeCreateDocumen
125137
}
126138
},
127139
transformError: async (error): Promise<KnowledgeCreateDocumentResponse> => {
128-
const errorMessage = `Failed to create document: ${error.message || 'Unknown error'}`
140+
let errorMessage = 'Failed to create document'
141+
142+
if (error.message) {
143+
if (error.message.includes('Document name')) {
144+
errorMessage = `Document name error: ${error.message}`
145+
} else if (error.message.includes('Document content')) {
146+
errorMessage = `Document content error: ${error.message}`
147+
} else if (error.message.includes('invalid characters')) {
148+
errorMessage = `${error.message}. Please use a valid filename.`
149+
} else if (error.message.includes('maximum size')) {
150+
errorMessage = `${error.message}. Consider breaking large content into smaller documents.`
151+
} else {
152+
errorMessage = `Failed to create document: ${error.message}`
153+
}
154+
}
155+
129156
return {
130157
success: false,
131158
output: {

0 commit comments

Comments
 (0)