-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Expand file tree
/
Copy pathcreate_document.ts
More file actions
199 lines (181 loc) · 5.72 KB
/
create_document.ts
File metadata and controls
199 lines (181 loc) · 5.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import type { KnowledgeCreateDocumentResponse } from '@/tools/knowledge/types'
import type { ToolConfig } from '@/tools/types'
export const knowledgeCreateDocumentTool: ToolConfig<any, KnowledgeCreateDocumentResponse> = {
id: 'knowledge_create_document',
name: 'Knowledge Create Document',
description: 'Create a new document in a knowledge base',
version: '1.0.0',
params: {
knowledgeBaseId: {
type: 'string',
required: true,
description: 'ID of the knowledge base containing the document',
},
name: {
type: 'string',
required: true,
description: 'Name of the document',
},
content: {
type: 'string',
required: true,
description: 'Content of the document',
},
tag1: {
type: 'string',
required: false,
description: 'Tag 1 value for the document',
},
tag2: {
type: 'string',
required: false,
description: 'Tag 2 value for the document',
},
tag3: {
type: 'string',
required: false,
description: 'Tag 3 value for the document',
},
tag4: {
type: 'string',
required: false,
description: 'Tag 4 value for the document',
},
tag5: {
type: 'string',
required: false,
description: 'Tag 5 value for the document',
},
tag6: {
type: 'string',
required: false,
description: 'Tag 6 value for the document',
},
tag7: {
type: 'string',
required: false,
description: 'Tag 7 value for the document',
},
documentTagsData: {
type: 'array',
required: false,
description: 'Structured tag data with names, types, and values',
},
},
request: {
url: (params) => `/api/knowledge/${params.knowledgeBaseId}/documents`,
method: 'POST',
headers: () => ({
'Content-Type': 'application/json',
}),
body: (params) => {
const workflowId = params._context?.workflowId
const textContent = params.content?.trim()
const documentName = params.name?.trim()
if (!documentName || documentName.length === 0) {
throw new Error('Document name is required')
}
if (documentName.length > 255) {
throw new Error('Document name must be 255 characters or less')
}
if (!textContent || textContent.length < 1) {
throw new Error('Document content cannot be empty')
}
if (textContent.length > 1000000) {
throw new Error('Document content exceeds maximum size of 1MB')
}
const contentBytes = new TextEncoder().encode(textContent).length
const utf8Bytes = new TextEncoder().encode(textContent)
const base64Content =
typeof Buffer !== 'undefined'
? Buffer.from(textContent, 'utf8').toString('base64')
: btoa(String.fromCharCode(...utf8Bytes))
const dataUri = `data:text/plain;base64,${base64Content}`
const tagData: Record<string, string> = {}
if (params.documentTags) {
let parsedTags = params.documentTags
// Handle both string (JSON) and array formats
if (typeof params.documentTags === 'string') {
try {
parsedTags = JSON.parse(params.documentTags)
} catch (error) {
parsedTags = []
}
}
if (Array.isArray(parsedTags)) {
tagData.documentTagsData = JSON.stringify(parsedTags)
}
}
const documents = [
{
filename: documentName.endsWith('.txt') ? documentName : `${documentName}.txt`,
fileUrl: dataUri,
fileSize: contentBytes,
mimeType: 'text/plain',
...tagData,
},
]
const requestBody = {
documents: documents,
processingOptions: {
chunkSize: 1024,
minCharactersPerChunk: 1,
chunkOverlap: 200,
recipe: 'default',
lang: 'en',
},
bulk: true,
...(workflowId && { workflowId }),
}
return requestBody
},
},
transformResponse: async (response): Promise<KnowledgeCreateDocumentResponse> => {
const result = await response.json()
const data = result.data || result
const documentsCreated = data.documentsCreated || []
// Handle multiple documents response
const uploadCount = documentsCreated.length
const firstDocument = documentsCreated[0]
return {
success: true,
output: {
message:
uploadCount > 1
? `Successfully created ${uploadCount} documents in knowledge base`
: `Successfully created document in knowledge base`,
data: {
documentId: firstDocument?.documentId || firstDocument?.id || '',
documentName:
uploadCount > 1 ? `${uploadCount} documents` : firstDocument?.filename || 'Unknown',
type: 'document',
enabled: true,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
},
},
}
},
outputs: {
data: {
type: 'object',
description: 'Information about the created document',
properties: {
documentId: { type: 'string', description: 'Document ID' },
documentName: { type: 'string', description: 'Document name' },
type: { type: 'string', description: 'Document type' },
enabled: { type: 'boolean', description: 'Whether the document is enabled' },
createdAt: { type: 'string', description: 'Creation timestamp' },
updatedAt: { type: 'string', description: 'Last update timestamp' },
},
},
message: {
type: 'string',
description: 'Success or error message describing the operation result',
},
documentId: {
type: 'string',
description: 'ID of the created document',
},
},
}