@nodetool-ai/text-nodes 0.8.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -0
- package/dist/.nodetool-build-stamp +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/nodes/lib-svg.d.ts +66 -0
- package/dist/nodes/lib-svg.d.ts.map +1 -0
- package/dist/nodes/lib-svg.js +212 -0
- package/dist/nodes/lib-svg.js.map +1 -0
- package/dist/nodes/text-extra.d.ts +303 -0
- package/dist/nodes/text-extra.d.ts.map +1 -0
- package/dist/nodes/text-extra.js +891 -0
- package/dist/nodes/text-extra.js.map +1 -0
- package/package.json +88 -0
|
@@ -0,0 +1,891 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { BaseNode, prop } from "@nodetool-ai/node-sdk";
|
|
8
|
+
import { assetRefToPromptToken } from "@nodetool-ai/runtime";
|
|
9
|
+
import { tagAsServer, renderTemplate, referencedVariables, base64ToBytes } from "@nodetool-ai/nodes-utils";
|
|
10
|
+
import { loadNodeFsPromises, loadNodePath, folderPathOf, writeSavedFile, VISIBLE_WHEN_NOT_SAVING_TO_WORKSPACE, SAVE_TO_WORKSPACE_DESCRIPTION, SAVE_TO_WORKSPACE_TITLE } from "@nodetool-ai/nodes-utils";
|
|
11
|
+
import { isNonEmptyString, isString } from "@nodetool-ai/node-sdk";
|
|
12
|
+
const NODE_ONLY = ["node"];
|
|
13
|
+
/**
|
|
14
|
+
* `ProcessingContext` declares the provider bridge, but a node is also handed
|
|
15
|
+
* partial stubs (tests, hosts with no provider access), so the method can be
|
|
16
|
+
* absent at runtime.
|
|
17
|
+
*/
|
|
18
|
+
function hasProviderPrediction(run) {
|
|
19
|
+
return typeof run === "function";
|
|
20
|
+
}
|
|
21
|
+
/** The ASR capability's answer — every provider returns an `ASRResult`. */
|
|
22
|
+
function isAsrResult(value) {
|
|
23
|
+
return (value !== null &&
|
|
24
|
+
typeof value === "object" &&
|
|
25
|
+
"text" in value &&
|
|
26
|
+
typeof value.text === "string");
|
|
27
|
+
}
|
|
28
|
+
/** The embedding capability's answer — one vector per embedded chunk. */
|
|
29
|
+
function isEmbeddingVectors(value) {
|
|
30
|
+
return Array.isArray(value) && value.every((row) => Array.isArray(row));
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Turn any asset-ref values in a template's variable bag into their
|
|
34
|
+
* `asset://<id>.<ext>` token so a wired image / audio / video / document
|
|
35
|
+
* expands like an inline `@`-mention instead of rendering as `"[object
|
|
36
|
+
* Object]"`. Non-asset values take the string form the renderer would have
|
|
37
|
+
* given them anyway; `null`/`undefined` stay absent so they render as "".
|
|
38
|
+
*
|
|
39
|
+
* The bag arrives as the dynamic-input map the node already holds: a wire value
|
|
40
|
+
* has no type narrower than `unknown`, and a `Record` would promise callers a
|
|
41
|
+
* value contract that does not exist.
|
|
42
|
+
*/
|
|
43
|
+
function tokenizeAssetVars(vars) {
|
|
44
|
+
const out = {};
|
|
45
|
+
for (const [key, value] of vars) {
|
|
46
|
+
out[key] =
|
|
47
|
+
assetRefToPromptToken(value) ?? (value == null ? undefined : String(value));
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
// Expands strftime-style date tokens (%Y %m %d %H %M %S) so the documented
|
|
52
|
+
// filename variables produce unique names instead of a literal "%Y-...".
|
|
53
|
+
function formatFilename(name) {
|
|
54
|
+
const now = new Date();
|
|
55
|
+
const pad = (n) => String(n).padStart(2, "0");
|
|
56
|
+
return name
|
|
57
|
+
.replace(/%Y/g, String(now.getFullYear()))
|
|
58
|
+
.replace(/%m/g, pad(now.getMonth() + 1))
|
|
59
|
+
.replace(/%d/g, pad(now.getDate()))
|
|
60
|
+
.replace(/%H/g, pad(now.getHours()))
|
|
61
|
+
.replace(/%M/g, pad(now.getMinutes()))
|
|
62
|
+
.replace(/%S/g, pad(now.getSeconds()));
|
|
63
|
+
}
|
|
64
|
+
function folderPath(value) {
|
|
65
|
+
if (isString(value))
|
|
66
|
+
return value;
|
|
67
|
+
if (value == null)
|
|
68
|
+
return "";
|
|
69
|
+
if (isString(value.path))
|
|
70
|
+
return value.path;
|
|
71
|
+
if (isString(value.uri)) {
|
|
72
|
+
return value.uri.startsWith("file://")
|
|
73
|
+
? value.uri.slice("file://".length)
|
|
74
|
+
: value.uri;
|
|
75
|
+
}
|
|
76
|
+
return "";
|
|
77
|
+
}
|
|
78
|
+
function modelConfig(model) {
|
|
79
|
+
const provider = model?.provider;
|
|
80
|
+
const id = model?.id;
|
|
81
|
+
return {
|
|
82
|
+
providerId: isString(provider) ? provider : "",
|
|
83
|
+
modelId: isString(id) ? id : ""
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/** Embedding params; `dimensions` travels only when the model declares one. */
|
|
87
|
+
function embeddingParams(text, dimensions) {
|
|
88
|
+
const params = { text };
|
|
89
|
+
if (dimensions > 0) {
|
|
90
|
+
params.dimensions = dimensions;
|
|
91
|
+
}
|
|
92
|
+
return params;
|
|
93
|
+
}
|
|
94
|
+
/** Split `text` into `chunk_size`-character pieces; never yields an empty list. */
|
|
95
|
+
function chunkText(text, chunkSize) {
|
|
96
|
+
const size = chunkSize > 0 ? Math.trunc(chunkSize) : text.length;
|
|
97
|
+
const chunks = [];
|
|
98
|
+
for (let i = 0; i < text.length; i += size) {
|
|
99
|
+
chunks.push(text.slice(i, i + size));
|
|
100
|
+
}
|
|
101
|
+
return chunks.length > 0 ? chunks : [text];
|
|
102
|
+
}
|
|
103
|
+
/** Element-wise mean of one vector per chunk; a single chunk passes through. */
|
|
104
|
+
function averageVectors(vectors) {
|
|
105
|
+
const first = vectors[0] ?? [];
|
|
106
|
+
if (vectors.length < 2) {
|
|
107
|
+
return first;
|
|
108
|
+
}
|
|
109
|
+
const mean = new Array(first.length).fill(0);
|
|
110
|
+
for (const vector of vectors) {
|
|
111
|
+
for (let i = 0; i < mean.length; i++) {
|
|
112
|
+
mean[i] += (vector[i] ?? 0) / vectors.length;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return mean;
|
|
116
|
+
}
|
|
117
|
+
/** `*` wildcard match, anchored to the whole name. */
|
|
118
|
+
function wildcardToRegExp(pattern) {
|
|
119
|
+
const escaped = pattern.replace(/[.+^${}()|[\]\\]/g, "\\$&");
|
|
120
|
+
return new RegExp(`^${escaped.replaceAll("*", ".*")}$`);
|
|
121
|
+
}
|
|
122
|
+
export class AutomaticSpeechRecognitionNode extends BaseNode {
|
|
123
|
+
static nodeType = "nodetool.text.AutomaticSpeechRecognition";
|
|
124
|
+
static body = "content_card";
|
|
125
|
+
static title = "Automatic Speech Recognition";
|
|
126
|
+
static description = "Transcribe audio to text using automatic speech recognition models.\n audio, speech, recognition, transcription, ASR, whisper";
|
|
127
|
+
static metadataOutputTypes = {
|
|
128
|
+
text: "str"
|
|
129
|
+
};
|
|
130
|
+
static inputFields = ["audio"];
|
|
131
|
+
async process(context) {
|
|
132
|
+
const { providerId, modelId } = modelConfig(this.model);
|
|
133
|
+
const audio = this.audio ?? {};
|
|
134
|
+
let bytes = new Uint8Array();
|
|
135
|
+
if (isString(audio.data)) {
|
|
136
|
+
bytes = base64ToBytes(audio.data);
|
|
137
|
+
}
|
|
138
|
+
else if (audio.data instanceof Uint8Array) {
|
|
139
|
+
bytes = new Uint8Array(audio.data);
|
|
140
|
+
}
|
|
141
|
+
else if (isNonEmptyString(audio.uri)) {
|
|
142
|
+
if (context?.storage) {
|
|
143
|
+
const stored = await context.storage.retrieve(audio.uri);
|
|
144
|
+
if (stored !== null)
|
|
145
|
+
bytes = new Uint8Array(stored);
|
|
146
|
+
}
|
|
147
|
+
if (bytes.length === 0 && audio.uri.startsWith("file://")) {
|
|
148
|
+
const fs = await loadNodeFsPromises();
|
|
149
|
+
bytes = new Uint8Array(await fs.readFile(audio.uri.slice("file://".length)));
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
if (context &&
|
|
153
|
+
hasProviderPrediction(context.runProviderPrediction) &&
|
|
154
|
+
providerId &&
|
|
155
|
+
modelId &&
|
|
156
|
+
bytes.length > 0) {
|
|
157
|
+
const result = await context.runProviderPrediction({
|
|
158
|
+
provider: providerId,
|
|
159
|
+
capability: "automatic_speech_recognition",
|
|
160
|
+
model: modelId,
|
|
161
|
+
params: {
|
|
162
|
+
audio: bytes,
|
|
163
|
+
language: this.language || undefined,
|
|
164
|
+
prompt: this.prompt || undefined,
|
|
165
|
+
temperature: this.temperature || undefined
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
if (!isAsrResult(result)) {
|
|
169
|
+
throw new Error("AutomaticSpeechRecognition expected a transcript from the provider.");
|
|
170
|
+
}
|
|
171
|
+
return { text: result.text, output: result.text };
|
|
172
|
+
}
|
|
173
|
+
throw new Error("AutomaticSpeechRecognition requires a provider-backed model and audio input.");
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
__decorate([
|
|
177
|
+
prop({
|
|
178
|
+
type: "asr_model",
|
|
179
|
+
default: {
|
|
180
|
+
type: "asr_model",
|
|
181
|
+
provider: "fal_ai",
|
|
182
|
+
id: "openai/whisper-large-v3",
|
|
183
|
+
name: "",
|
|
184
|
+
path: null
|
|
185
|
+
},
|
|
186
|
+
title: "Model"
|
|
187
|
+
})
|
|
188
|
+
], AutomaticSpeechRecognitionNode.prototype, "model", void 0);
|
|
189
|
+
__decorate([
|
|
190
|
+
prop({
|
|
191
|
+
type: "audio",
|
|
192
|
+
default: {
|
|
193
|
+
type: "audio",
|
|
194
|
+
uri: "",
|
|
195
|
+
asset_id: null,
|
|
196
|
+
data: null,
|
|
197
|
+
metadata: null
|
|
198
|
+
},
|
|
199
|
+
title: "Audio",
|
|
200
|
+
description: "The audio to transcribe",
|
|
201
|
+
required: true
|
|
202
|
+
})
|
|
203
|
+
], AutomaticSpeechRecognitionNode.prototype, "audio", void 0);
|
|
204
|
+
__decorate([
|
|
205
|
+
prop({
|
|
206
|
+
type: "str",
|
|
207
|
+
default: "",
|
|
208
|
+
title: "Language",
|
|
209
|
+
description: "Language of the audio (ISO 639-1 code, empty for auto-detect)"
|
|
210
|
+
})
|
|
211
|
+
], AutomaticSpeechRecognitionNode.prototype, "language", void 0);
|
|
212
|
+
__decorate([
|
|
213
|
+
prop({
|
|
214
|
+
type: "str",
|
|
215
|
+
default: "",
|
|
216
|
+
title: "Prompt",
|
|
217
|
+
description: "Optional prompt to guide the transcription"
|
|
218
|
+
})
|
|
219
|
+
], AutomaticSpeechRecognitionNode.prototype, "prompt", void 0);
|
|
220
|
+
__decorate([
|
|
221
|
+
prop({
|
|
222
|
+
type: "float",
|
|
223
|
+
default: 0,
|
|
224
|
+
title: "Temperature",
|
|
225
|
+
description: "Sampling temperature for the transcription model",
|
|
226
|
+
min: 0,
|
|
227
|
+
max: 1
|
|
228
|
+
})
|
|
229
|
+
], AutomaticSpeechRecognitionNode.prototype, "temperature", void 0);
|
|
230
|
+
export class EmbeddingTextNode extends BaseNode {
|
|
231
|
+
static nodeType = "nodetool.text.Embedding";
|
|
232
|
+
static title = "Embedding";
|
|
233
|
+
static description = "Generate vector representations of text using any supported embedding provider (OpenAI, Gemini, Mistral).\n embeddings, similarity, search, clustering, classification, vectors, semantic\n\n Use cases:\n - Semantic search and recommendation\n - Text clustering and classification\n - Anomaly detection\n - Measuring text similarity and diversity";
|
|
234
|
+
static metadataOutputTypes = {
|
|
235
|
+
output: "list"
|
|
236
|
+
};
|
|
237
|
+
static inlineFields = [];
|
|
238
|
+
static inputFields = ["input"];
|
|
239
|
+
async process(context) {
|
|
240
|
+
const text = this.input;
|
|
241
|
+
if (!text) {
|
|
242
|
+
throw new Error("input text must not be empty");
|
|
243
|
+
}
|
|
244
|
+
const model = this.model;
|
|
245
|
+
const { providerId, modelId } = modelConfig(model);
|
|
246
|
+
if (!context || !hasProviderPrediction(context.runProviderPrediction)) {
|
|
247
|
+
throw new Error("Embedding requires a processing context with provider access");
|
|
248
|
+
}
|
|
249
|
+
if (!providerId || !modelId) {
|
|
250
|
+
throw new Error("Embedding requires an embedding model with provider and id");
|
|
251
|
+
}
|
|
252
|
+
const dimensions = Number(model?.dimensions ?? 0);
|
|
253
|
+
const vectors = await context.runProviderPrediction({
|
|
254
|
+
provider: providerId,
|
|
255
|
+
capability: "generate_embedding",
|
|
256
|
+
model: modelId,
|
|
257
|
+
params: embeddingParams(chunkText(text, this.chunk_size), dimensions)
|
|
258
|
+
});
|
|
259
|
+
if (!isEmbeddingVectors(vectors)) {
|
|
260
|
+
throw new Error("Embedding expected vectors from the provider.");
|
|
261
|
+
}
|
|
262
|
+
return { output: averageVectors(vectors) };
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
__decorate([
|
|
266
|
+
prop({
|
|
267
|
+
type: "embedding_model",
|
|
268
|
+
default: {
|
|
269
|
+
type: "embedding_model",
|
|
270
|
+
provider: "openai",
|
|
271
|
+
id: "text-embedding-3-small",
|
|
272
|
+
name: "Text Embedding 3 Small",
|
|
273
|
+
dimensions: 0
|
|
274
|
+
},
|
|
275
|
+
title: "Model",
|
|
276
|
+
description: "The embedding model to use"
|
|
277
|
+
})
|
|
278
|
+
], EmbeddingTextNode.prototype, "model", void 0);
|
|
279
|
+
__decorate([
|
|
280
|
+
prop({
|
|
281
|
+
type: "str",
|
|
282
|
+
default: "",
|
|
283
|
+
title: "Input",
|
|
284
|
+
description: "The text to embed"
|
|
285
|
+
})
|
|
286
|
+
], EmbeddingTextNode.prototype, "input", void 0);
|
|
287
|
+
__decorate([
|
|
288
|
+
prop({
|
|
289
|
+
type: "int",
|
|
290
|
+
default: 4096,
|
|
291
|
+
title: "Chunk Size",
|
|
292
|
+
description: "Size of text chunks for embedding (used when input exceeds model limits)",
|
|
293
|
+
min: 1,
|
|
294
|
+
max: 8192
|
|
295
|
+
})
|
|
296
|
+
], EmbeddingTextNode.prototype, "chunk_size", void 0);
|
|
297
|
+
export class SaveTextFileNode extends BaseNode {
|
|
298
|
+
static nodeType = "nodetool.text.SaveTextFile";
|
|
299
|
+
static platforms = NODE_ONLY;
|
|
300
|
+
static title = "Save Text File";
|
|
301
|
+
static description = "Saves input text to a file in the assets folder.\n text, save, file";
|
|
302
|
+
static metadataOutputTypes = {
|
|
303
|
+
output: "text"
|
|
304
|
+
};
|
|
305
|
+
static inlineFields = ["save_to_workspace"];
|
|
306
|
+
static inputFields = ["text"];
|
|
307
|
+
async process(context) {
|
|
308
|
+
const text = this.text;
|
|
309
|
+
const saveToWorkspace = this.save_to_workspace;
|
|
310
|
+
const folder = folderPathOf(this.folder);
|
|
311
|
+
if (!folder && !(saveToWorkspace && context?.workspace)) {
|
|
312
|
+
throw new Error("No destination: set a folder, or turn on \"Save to workspace\" and assign a workspace to this workflow.");
|
|
313
|
+
}
|
|
314
|
+
const fsPath = await writeSavedFile({
|
|
315
|
+
folder: this.folder,
|
|
316
|
+
filename: formatFilename(this.name),
|
|
317
|
+
saveToWorkspace,
|
|
318
|
+
workspace: context?.workspace,
|
|
319
|
+
workspaceDir: context?.workspaceDir,
|
|
320
|
+
bytes: text
|
|
321
|
+
});
|
|
322
|
+
// The output `uri` is a portable, URI-style path (forward slashes) so
|
|
323
|
+
// downstream nodes and the web UI never have to special-case Windows.
|
|
324
|
+
const uri = fsPath.replace(/\\/g, "/");
|
|
325
|
+
return { output: { uri, data: text } };
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
__decorate([
|
|
329
|
+
prop({ type: "str", default: "", title: "Text" })
|
|
330
|
+
], SaveTextFileNode.prototype, "text", void 0);
|
|
331
|
+
__decorate([
|
|
332
|
+
prop({
|
|
333
|
+
type: "bool",
|
|
334
|
+
default: true,
|
|
335
|
+
title: SAVE_TO_WORKSPACE_TITLE,
|
|
336
|
+
description: SAVE_TO_WORKSPACE_DESCRIPTION
|
|
337
|
+
})
|
|
338
|
+
], SaveTextFileNode.prototype, "save_to_workspace", void 0);
|
|
339
|
+
__decorate([
|
|
340
|
+
prop({
|
|
341
|
+
type: "str",
|
|
342
|
+
default: "",
|
|
343
|
+
title: "Folder",
|
|
344
|
+
description: "Path to the output folder.",
|
|
345
|
+
json_schema_extra: VISIBLE_WHEN_NOT_SAVING_TO_WORKSPACE
|
|
346
|
+
})
|
|
347
|
+
], SaveTextFileNode.prototype, "folder", void 0);
|
|
348
|
+
__decorate([
|
|
349
|
+
prop({
|
|
350
|
+
type: "str",
|
|
351
|
+
default: "%Y-%m-%d-%H-%M-%S.txt",
|
|
352
|
+
title: "Name",
|
|
353
|
+
description: "\n Name of the output file.\n You can use time and date variables to create unique names:\n %Y - Year\n %m - Month\n %d - Day\n %H - Hour\n %M - Minute\n %S - Second\n "
|
|
354
|
+
})
|
|
355
|
+
], SaveTextFileNode.prototype, "name", void 0);
|
|
356
|
+
export class SaveTextNode extends BaseNode {
|
|
357
|
+
static nodeType = "nodetool.text.SaveText";
|
|
358
|
+
static platforms = NODE_ONLY;
|
|
359
|
+
static title = "Save Text";
|
|
360
|
+
static description = "Saves input text to a file in the assets folder.\n text, save, file\n\n Use cases:\n - Persisting processed text results\n - Creating text files for downstream nodes or external use\n - Archiving text data within the workflow";
|
|
361
|
+
static metadataOutputTypes = {
|
|
362
|
+
output: "text"
|
|
363
|
+
};
|
|
364
|
+
static inputFields = ["text"];
|
|
365
|
+
async process() {
|
|
366
|
+
const text = this.text;
|
|
367
|
+
const name = formatFilename(this.name);
|
|
368
|
+
const folder = folderPath(this.folder);
|
|
369
|
+
const fs = await loadNodeFsPromises();
|
|
370
|
+
const path = await loadNodePath();
|
|
371
|
+
const fsPath = folder ? path.join(folder, name) : name;
|
|
372
|
+
if (folder) {
|
|
373
|
+
await fs.mkdir(folder, { recursive: true });
|
|
374
|
+
}
|
|
375
|
+
await fs.writeFile(fsPath, text, "utf-8");
|
|
376
|
+
const uri = fsPath.replace(/\\/g, "/");
|
|
377
|
+
return { output: { uri, data: text } };
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
__decorate([
|
|
381
|
+
prop({ type: "str", default: "", title: "Text" })
|
|
382
|
+
], SaveTextNode.prototype, "text", void 0);
|
|
383
|
+
__decorate([
|
|
384
|
+
prop({
|
|
385
|
+
type: "folder",
|
|
386
|
+
default: {
|
|
387
|
+
type: "folder",
|
|
388
|
+
uri: "",
|
|
389
|
+
asset_id: null,
|
|
390
|
+
data: null,
|
|
391
|
+
metadata: null
|
|
392
|
+
},
|
|
393
|
+
title: "Folder",
|
|
394
|
+
description: "Name of the output folder."
|
|
395
|
+
})
|
|
396
|
+
], SaveTextNode.prototype, "folder", void 0);
|
|
397
|
+
__decorate([
|
|
398
|
+
prop({
|
|
399
|
+
type: "str",
|
|
400
|
+
default: "%Y-%m-%d-%H-%M-%S.txt",
|
|
401
|
+
title: "Name",
|
|
402
|
+
description: "\n Name of the output file.\n You can use time and date variables to create unique names:\n %Y - Year\n %m - Month\n %d - Day\n %H - Hour\n %M - Minute\n %S - Second\n "
|
|
403
|
+
})
|
|
404
|
+
], SaveTextNode.prototype, "name", void 0);
|
|
405
|
+
export class LoadTextFolderNode extends BaseNode {
|
|
406
|
+
static nodeType = "nodetool.text.LoadTextFolder";
|
|
407
|
+
static platforms = NODE_ONLY;
|
|
408
|
+
static title = "Load Text Folder";
|
|
409
|
+
static description = "Load all text files from a folder, optionally including subfolders.\n text, load, folder, files";
|
|
410
|
+
static metadataOutputTypes = {
|
|
411
|
+
text: "str",
|
|
412
|
+
path: "str",
|
|
413
|
+
texts: "list",
|
|
414
|
+
paths: "list"
|
|
415
|
+
};
|
|
416
|
+
async process() {
|
|
417
|
+
const allTexts = [];
|
|
418
|
+
const allPaths = [];
|
|
419
|
+
for await (const item of this._walkFiles()) {
|
|
420
|
+
allTexts.push(item.text);
|
|
421
|
+
allPaths.push(item.path);
|
|
422
|
+
}
|
|
423
|
+
return {
|
|
424
|
+
text: allTexts[0] ?? "",
|
|
425
|
+
path: allPaths[0] ?? "",
|
|
426
|
+
texts: allTexts,
|
|
427
|
+
paths: allPaths
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
async *_walkFiles() {
|
|
431
|
+
const folder = this.folder;
|
|
432
|
+
const includeSubdirs = this.include_subdirectories;
|
|
433
|
+
const extensions = this.extensions.map((ext) => String(ext).toLowerCase());
|
|
434
|
+
const matches = this.pattern ? wildcardToRegExp(this.pattern) : null;
|
|
435
|
+
if (!folder) {
|
|
436
|
+
throw new Error("folder cannot be empty");
|
|
437
|
+
}
|
|
438
|
+
const fs = await loadNodeFsPromises();
|
|
439
|
+
const path = await loadNodePath();
|
|
440
|
+
const walk = async function* (dir) {
|
|
441
|
+
const entries = await fs.readdir(dir, { withFileTypes: true });
|
|
442
|
+
for (const entry of entries) {
|
|
443
|
+
const full = path.join(dir, entry.name);
|
|
444
|
+
if (entry.isDirectory()) {
|
|
445
|
+
if (includeSubdirs) {
|
|
446
|
+
yield* walk(full);
|
|
447
|
+
}
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
yield full;
|
|
451
|
+
}
|
|
452
|
+
};
|
|
453
|
+
for await (const filePath of walk(folder)) {
|
|
454
|
+
if (!extensions.includes(path.extname(filePath).toLowerCase())) {
|
|
455
|
+
continue;
|
|
456
|
+
}
|
|
457
|
+
if (matches && !matches.test(path.basename(filePath))) {
|
|
458
|
+
continue;
|
|
459
|
+
}
|
|
460
|
+
const text = await fs.readFile(filePath, "utf-8");
|
|
461
|
+
yield { path: filePath, text };
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
async *genProcess() {
|
|
465
|
+
const allTexts = [];
|
|
466
|
+
const allPaths = [];
|
|
467
|
+
for await (const item of this._walkFiles()) {
|
|
468
|
+
allTexts.push(item.text);
|
|
469
|
+
allPaths.push(item.path);
|
|
470
|
+
yield { path: item.path, text: item.text };
|
|
471
|
+
}
|
|
472
|
+
yield { texts: allTexts, paths: allPaths };
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
__decorate([
|
|
476
|
+
prop({
|
|
477
|
+
type: "str",
|
|
478
|
+
default: "",
|
|
479
|
+
title: "Folder",
|
|
480
|
+
description: "Folder to scan for text files"
|
|
481
|
+
})
|
|
482
|
+
], LoadTextFolderNode.prototype, "folder", void 0);
|
|
483
|
+
__decorate([
|
|
484
|
+
prop({
|
|
485
|
+
type: "bool",
|
|
486
|
+
default: false,
|
|
487
|
+
title: "Include Subdirectories",
|
|
488
|
+
description: "Include text files in subfolders"
|
|
489
|
+
})
|
|
490
|
+
], LoadTextFolderNode.prototype, "include_subdirectories", void 0);
|
|
491
|
+
__decorate([
|
|
492
|
+
prop({
|
|
493
|
+
type: "list[str]",
|
|
494
|
+
default: [".txt", ".csv", ".json", ".xml", ".md", ".html", ".pdf"],
|
|
495
|
+
title: "Extensions",
|
|
496
|
+
description: "Text file extensions to include"
|
|
497
|
+
})
|
|
498
|
+
], LoadTextFolderNode.prototype, "extensions", void 0);
|
|
499
|
+
__decorate([
|
|
500
|
+
prop({
|
|
501
|
+
type: "str",
|
|
502
|
+
default: "",
|
|
503
|
+
title: "Pattern",
|
|
504
|
+
description: "Pattern to match text files"
|
|
505
|
+
})
|
|
506
|
+
], LoadTextFolderNode.prototype, "pattern", void 0);
|
|
507
|
+
export class LoadTextAssetsNode extends BaseNode {
|
|
508
|
+
static nodeType = "nodetool.text.LoadTextAssets";
|
|
509
|
+
static title = "Load Text Assets";
|
|
510
|
+
static description = "Load text files from an asset folder.\n load, text, file, import";
|
|
511
|
+
static metadataOutputTypes = {
|
|
512
|
+
text: "text",
|
|
513
|
+
name: "str",
|
|
514
|
+
texts: "list",
|
|
515
|
+
names: "list"
|
|
516
|
+
};
|
|
517
|
+
async process() {
|
|
518
|
+
const allTexts = [];
|
|
519
|
+
const allNames = [];
|
|
520
|
+
const folder = folderPath(this.folder);
|
|
521
|
+
if (!folder) {
|
|
522
|
+
throw new Error("folder cannot be empty");
|
|
523
|
+
}
|
|
524
|
+
const walker = new LoadTextFolderNode();
|
|
525
|
+
walker.assign({ folder });
|
|
526
|
+
for await (const item of walker._walkFiles()) {
|
|
527
|
+
allTexts.push(item.text);
|
|
528
|
+
allNames.push(item.path);
|
|
529
|
+
}
|
|
530
|
+
return {
|
|
531
|
+
text: allTexts[0] ?? "",
|
|
532
|
+
name: allNames[0] ?? "",
|
|
533
|
+
texts: allTexts,
|
|
534
|
+
names: allNames
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
async *genProcess() {
|
|
538
|
+
const folder = folderPath(this.folder);
|
|
539
|
+
if (!folder) {
|
|
540
|
+
throw new Error("folder cannot be empty");
|
|
541
|
+
}
|
|
542
|
+
const allTexts = [];
|
|
543
|
+
const allNames = [];
|
|
544
|
+
const walker = new LoadTextFolderNode();
|
|
545
|
+
walker.assign({ folder });
|
|
546
|
+
for await (const item of walker._walkFiles()) {
|
|
547
|
+
allTexts.push(item.text);
|
|
548
|
+
allNames.push(item.path);
|
|
549
|
+
yield { text: item.text, name: item.path };
|
|
550
|
+
}
|
|
551
|
+
yield { texts: allTexts, names: allNames };
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
__decorate([
|
|
555
|
+
prop({
|
|
556
|
+
type: "folder",
|
|
557
|
+
default: {
|
|
558
|
+
type: "folder",
|
|
559
|
+
uri: "",
|
|
560
|
+
asset_id: null,
|
|
561
|
+
data: null,
|
|
562
|
+
metadata: null
|
|
563
|
+
},
|
|
564
|
+
title: "Folder",
|
|
565
|
+
description: "The asset folder to load the text files from."
|
|
566
|
+
})
|
|
567
|
+
], LoadTextAssetsNode.prototype, "folder", void 0);
|
|
568
|
+
/** The filters FilterStringNode implements; its enum prop offers exactly these. */
|
|
569
|
+
const FILTER_STRING_TYPES = [
|
|
570
|
+
"contains",
|
|
571
|
+
"starts_with",
|
|
572
|
+
"ends_with",
|
|
573
|
+
"length_greater",
|
|
574
|
+
"length_less",
|
|
575
|
+
"exact_length"
|
|
576
|
+
];
|
|
577
|
+
function isFilterStringType(value) {
|
|
578
|
+
return FILTER_STRING_TYPES.some((filter) => filter === value);
|
|
579
|
+
}
|
|
580
|
+
export class FilterStringNode extends BaseNode {
|
|
581
|
+
static nodeType = "nodetool.text.FilterString";
|
|
582
|
+
static retrySafe = true;
|
|
583
|
+
static title = "Filter String";
|
|
584
|
+
static description = "Filters a stream of strings based on various criteria.\n filter, strings, text, stream";
|
|
585
|
+
static metadataOutputTypes = {
|
|
586
|
+
output: "str"
|
|
587
|
+
};
|
|
588
|
+
static outputCorrelation = {
|
|
589
|
+
output: { kind: "forward", source: "value" }
|
|
590
|
+
};
|
|
591
|
+
/** null when the prop names no filter this node implements — nothing matches. */
|
|
592
|
+
_filterType = "contains";
|
|
593
|
+
_criteria = "";
|
|
594
|
+
readFilterProps() {
|
|
595
|
+
this._filterType = isFilterStringType(this.filter_type)
|
|
596
|
+
? this.filter_type
|
|
597
|
+
: null;
|
|
598
|
+
this._criteria = this.criteria;
|
|
599
|
+
}
|
|
600
|
+
async initialize() {
|
|
601
|
+
this.readFilterProps();
|
|
602
|
+
}
|
|
603
|
+
async process() {
|
|
604
|
+
this.readFilterProps();
|
|
605
|
+
const value = this.value;
|
|
606
|
+
if (!isString(value)) {
|
|
607
|
+
return {};
|
|
608
|
+
}
|
|
609
|
+
const criteria = this._criteria;
|
|
610
|
+
const len = value.length;
|
|
611
|
+
const n = Number(criteria);
|
|
612
|
+
let matched;
|
|
613
|
+
switch (this._filterType) {
|
|
614
|
+
case "contains":
|
|
615
|
+
matched = value.includes(criteria);
|
|
616
|
+
break;
|
|
617
|
+
case "starts_with":
|
|
618
|
+
matched = value.startsWith(criteria);
|
|
619
|
+
break;
|
|
620
|
+
case "ends_with":
|
|
621
|
+
matched = value.endsWith(criteria);
|
|
622
|
+
break;
|
|
623
|
+
case "length_greater":
|
|
624
|
+
matched = Number.isFinite(n) && len > n;
|
|
625
|
+
break;
|
|
626
|
+
case "length_less":
|
|
627
|
+
matched = Number.isFinite(n) && len < n;
|
|
628
|
+
break;
|
|
629
|
+
case "exact_length":
|
|
630
|
+
matched = Number.isFinite(n) && len === n;
|
|
631
|
+
break;
|
|
632
|
+
default:
|
|
633
|
+
matched = false;
|
|
634
|
+
}
|
|
635
|
+
if (!matched) {
|
|
636
|
+
return {};
|
|
637
|
+
}
|
|
638
|
+
return { output: value };
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
__decorate([
|
|
642
|
+
prop({
|
|
643
|
+
type: "str",
|
|
644
|
+
default: "",
|
|
645
|
+
title: "Value",
|
|
646
|
+
description: "Input string stream"
|
|
647
|
+
})
|
|
648
|
+
], FilterStringNode.prototype, "value", void 0);
|
|
649
|
+
__decorate([
|
|
650
|
+
prop({
|
|
651
|
+
type: "enum",
|
|
652
|
+
default: "contains",
|
|
653
|
+
title: "Filter Type",
|
|
654
|
+
description: "The type of filter to apply",
|
|
655
|
+
values: [
|
|
656
|
+
"contains",
|
|
657
|
+
"starts_with",
|
|
658
|
+
"ends_with",
|
|
659
|
+
"length_greater",
|
|
660
|
+
"length_less",
|
|
661
|
+
"exact_length"
|
|
662
|
+
]
|
|
663
|
+
})
|
|
664
|
+
], FilterStringNode.prototype, "filter_type", void 0);
|
|
665
|
+
__decorate([
|
|
666
|
+
prop({
|
|
667
|
+
type: "str",
|
|
668
|
+
default: "",
|
|
669
|
+
title: "Criteria",
|
|
670
|
+
description: "The filtering criteria (text to match or length as string)"
|
|
671
|
+
})
|
|
672
|
+
], FilterStringNode.prototype, "criteria", void 0);
|
|
673
|
+
export class FilterRegexStringNode extends BaseNode {
|
|
674
|
+
static nodeType = "nodetool.text.FilterRegexString";
|
|
675
|
+
static retrySafe = true;
|
|
676
|
+
static title = "Filter Regex String";
|
|
677
|
+
static description = "Filters a stream of strings using regular expressions.\n filter, regex, pattern, text, stream";
|
|
678
|
+
static metadataOutputTypes = {
|
|
679
|
+
output: "str"
|
|
680
|
+
};
|
|
681
|
+
static outputCorrelation = {
|
|
682
|
+
output: { kind: "forward", source: "value" }
|
|
683
|
+
};
|
|
684
|
+
_pattern = "";
|
|
685
|
+
_fullMatch = false;
|
|
686
|
+
async initialize() {
|
|
687
|
+
this._pattern = this.pattern;
|
|
688
|
+
this._fullMatch = this.full_match;
|
|
689
|
+
}
|
|
690
|
+
async process() {
|
|
691
|
+
this._pattern = this.pattern;
|
|
692
|
+
this._fullMatch = this.full_match;
|
|
693
|
+
const value = this.value;
|
|
694
|
+
if (!isString(value)) {
|
|
695
|
+
return {};
|
|
696
|
+
}
|
|
697
|
+
let regex;
|
|
698
|
+
try {
|
|
699
|
+
regex = new RegExp(this._pattern);
|
|
700
|
+
}
|
|
701
|
+
catch {
|
|
702
|
+
return {};
|
|
703
|
+
}
|
|
704
|
+
const matched = this._fullMatch
|
|
705
|
+
? (value.match(regex)?.[0] ?? "") === value
|
|
706
|
+
: regex.test(value);
|
|
707
|
+
if (!matched) {
|
|
708
|
+
return {};
|
|
709
|
+
}
|
|
710
|
+
return { output: value };
|
|
711
|
+
}
|
|
712
|
+
}
|
|
713
|
+
__decorate([
|
|
714
|
+
prop({
|
|
715
|
+
type: "str",
|
|
716
|
+
default: "",
|
|
717
|
+
title: "Value",
|
|
718
|
+
description: "Input string stream"
|
|
719
|
+
})
|
|
720
|
+
], FilterRegexStringNode.prototype, "value", void 0);
|
|
721
|
+
__decorate([
|
|
722
|
+
prop({
|
|
723
|
+
type: "str",
|
|
724
|
+
default: "",
|
|
725
|
+
title: "Pattern",
|
|
726
|
+
description: "The regular expression pattern to match against."
|
|
727
|
+
})
|
|
728
|
+
], FilterRegexStringNode.prototype, "pattern", void 0);
|
|
729
|
+
__decorate([
|
|
730
|
+
prop({
|
|
731
|
+
type: "bool",
|
|
732
|
+
default: false,
|
|
733
|
+
title: "Full Match",
|
|
734
|
+
description: "Whether to match the entire string or find pattern anywhere in string"
|
|
735
|
+
})
|
|
736
|
+
], FilterRegexStringNode.prototype, "full_match", void 0);
|
|
737
|
+
export class ConcatTextNode extends BaseNode {
|
|
738
|
+
static nodeType = "nodetool.text.Concat";
|
|
739
|
+
static retrySafe = true;
|
|
740
|
+
static cacheTtl = "forever";
|
|
741
|
+
static body = "content_card";
|
|
742
|
+
static title = "Concat";
|
|
743
|
+
static description = "Concatenates text inputs into a single output. Add inputs dynamically with the “add text input” button, or wire a list of strings into a single input.\n +, text, combine, add, concatenate, merge, join, append";
|
|
744
|
+
static metadataOutputTypes = {
|
|
745
|
+
output: "str"
|
|
746
|
+
};
|
|
747
|
+
static inlineFields = [];
|
|
748
|
+
static inputFields = [];
|
|
749
|
+
static supportsDynamicInputs = true;
|
|
750
|
+
async process() {
|
|
751
|
+
// A dynamic input may carry a single value or a list<str> from an upstream
|
|
752
|
+
// loop/list node — flatten so list elements concatenate in order instead of
|
|
753
|
+
// stringifying the whole array as "a,b,c".
|
|
754
|
+
return {
|
|
755
|
+
output: Array.from(this.dynamicProps.values())
|
|
756
|
+
.flatMap((value) => (Array.isArray(value) ? value : [value]))
|
|
757
|
+
.map((value) => String(value ?? ""))
|
|
758
|
+
.join("")
|
|
759
|
+
};
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
export class CollectTextNode extends BaseNode {
|
|
763
|
+
static nodeType = "nodetool.text.Collect";
|
|
764
|
+
static title = "Collect Text";
|
|
765
|
+
static description = "Collects streaming text inputs into a single concatenated string.\n text, collect, stream, aggregate";
|
|
766
|
+
static metadataOutputTypes = {
|
|
767
|
+
output: "str"
|
|
768
|
+
};
|
|
769
|
+
_items = [];
|
|
770
|
+
async initialize() {
|
|
771
|
+
this._items = [];
|
|
772
|
+
}
|
|
773
|
+
async process() {
|
|
774
|
+
this._items.push(this.input_item);
|
|
775
|
+
return { output: this._items.join(this.separator) };
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
__decorate([
|
|
779
|
+
prop({
|
|
780
|
+
type: "str",
|
|
781
|
+
default: "",
|
|
782
|
+
title: "Input Item",
|
|
783
|
+
description: "Text to collect."
|
|
784
|
+
})
|
|
785
|
+
], CollectTextNode.prototype, "input_item", void 0);
|
|
786
|
+
__decorate([
|
|
787
|
+
prop({
|
|
788
|
+
type: "str",
|
|
789
|
+
default: "",
|
|
790
|
+
title: "Separator",
|
|
791
|
+
description: "Separator between collected items."
|
|
792
|
+
})
|
|
793
|
+
], CollectTextNode.prototype, "separator", void 0);
|
|
794
|
+
export class PromptNode extends BaseNode {
|
|
795
|
+
static nodeType = "nodetool.text.Prompt";
|
|
796
|
+
static cacheTtl = "forever";
|
|
797
|
+
static title = "Prompt";
|
|
798
|
+
static description = "Compose a prompt string with named variables. Add variables via the Add Variable button; reference them in the prompt as {{ variable }} (or {variable}). Supports filters: {{ var|upper }}, {{ var|lower }}, {{ var|capitalize }}, {{ var|title }}, {{ var|trim }}, {{ var|truncate(n) }}, {{ var|default(val) }}.\n prompt, text, template, variable, llm, agent";
|
|
799
|
+
static metadataOutputTypes = {
|
|
800
|
+
output: "str"
|
|
801
|
+
};
|
|
802
|
+
static supportsDynamicInputs = true;
|
|
803
|
+
// Every variable is also forwarded on an output handle of the same name, so
|
|
804
|
+
// an image used as `{{ var }}` in the text can additionally be wired into a
|
|
805
|
+
// downstream node that wants the real asset (e.g. reference images).
|
|
806
|
+
static supportsDynamicOutputs = true;
|
|
807
|
+
static inputFields = ["prompt"];
|
|
808
|
+
async process(context) {
|
|
809
|
+
const template = this.prompt;
|
|
810
|
+
const props = new Map(this.dynamicProps);
|
|
811
|
+
// Resolve `{{ name }}` references against variable channels: wait for the
|
|
812
|
+
// first value published by a Set Variable node anywhere in the graph. Only
|
|
813
|
+
// names with a registered writer are awaited (others are left intact), and
|
|
814
|
+
// the node's own dynamic inputs take precedence on conflict.
|
|
815
|
+
if (context) {
|
|
816
|
+
const pending = referencedVariables(template)
|
|
817
|
+
.filter((name) => !props.has(name) && context.hasChannelWriters(name))
|
|
818
|
+
.map(async (name) => {
|
|
819
|
+
const value = await context.getChannel(name).first();
|
|
820
|
+
if (value !== undefined) {
|
|
821
|
+
props.set(name, value);
|
|
822
|
+
}
|
|
823
|
+
});
|
|
824
|
+
await Promise.all(pending);
|
|
825
|
+
}
|
|
826
|
+
// Pass every variable through on a handle of its own name, carrying the
|
|
827
|
+
// raw value (an image ref stays an image ref — only the rendered text gets
|
|
828
|
+
// asset tokens). Keys without an outgoing edge are dropped by the runner.
|
|
829
|
+
// The rendered text is assigned last so a variable named "output" can't
|
|
830
|
+
// shadow it.
|
|
831
|
+
return {
|
|
832
|
+
...Object.fromEntries(props),
|
|
833
|
+
output: renderTemplate(template, tokenizeAssetVars(props))
|
|
834
|
+
};
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
__decorate([
|
|
838
|
+
prop({
|
|
839
|
+
type: "str",
|
|
840
|
+
default: "",
|
|
841
|
+
title: "Prompt",
|
|
842
|
+
description: "Prompt text. Reference variables with {{ name }} or {name}."
|
|
843
|
+
})
|
|
844
|
+
], PromptNode.prototype, "prompt", void 0);
|
|
845
|
+
export class TemplateTextNode extends BaseNode {
|
|
846
|
+
static nodeType = "nodetool.text.Template";
|
|
847
|
+
static retrySafe = true;
|
|
848
|
+
static cacheTtl = "forever";
|
|
849
|
+
static title = "Template";
|
|
850
|
+
static description = "Uses template syntax to format strings with variables. Supports {{ variable }} and {variable} patterns.\n text, template, formatting, format, combine, concatenate, variable, replace\n\n Use cases:\n - Generating personalized messages\n - Creating parameterized queries\n - Formatting text with variable inputs";
|
|
851
|
+
static metadataOutputTypes = {
|
|
852
|
+
output: "str"
|
|
853
|
+
};
|
|
854
|
+
static supportsDynamicInputs = true;
|
|
855
|
+
async process() {
|
|
856
|
+
let result = this.string;
|
|
857
|
+
const props = tokenizeAssetVars(this.dynamicProps);
|
|
858
|
+
for (const [key, value] of Object.entries(props)) {
|
|
859
|
+
const strValue = String(value ?? "");
|
|
860
|
+
const escapedKey = key.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
861
|
+
const jinja = new RegExp(`\\{\\{\\s*${escapedKey}\\s*\\}\\}`, "g");
|
|
862
|
+
result = result.replace(jinja, () => strValue);
|
|
863
|
+
const single = new RegExp(`(?<!\\{)\\{${escapedKey}\\}(?!\\})`, "g");
|
|
864
|
+
result = result.replace(single, () => strValue);
|
|
865
|
+
}
|
|
866
|
+
return { output: result };
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
__decorate([
|
|
870
|
+
prop({
|
|
871
|
+
type: "str",
|
|
872
|
+
default: "",
|
|
873
|
+
title: "String",
|
|
874
|
+
description: "Template string with {{ variable }} or {variable} placeholders."
|
|
875
|
+
})
|
|
876
|
+
], TemplateTextNode.prototype, "string", void 0);
|
|
877
|
+
export const TEXT_EXTRA_NODES = tagAsServer([
|
|
878
|
+
AutomaticSpeechRecognitionNode,
|
|
879
|
+
EmbeddingTextNode,
|
|
880
|
+
SaveTextFileNode,
|
|
881
|
+
SaveTextNode,
|
|
882
|
+
LoadTextFolderNode,
|
|
883
|
+
LoadTextAssetsNode,
|
|
884
|
+
FilterStringNode,
|
|
885
|
+
FilterRegexStringNode,
|
|
886
|
+
ConcatTextNode,
|
|
887
|
+
CollectTextNode,
|
|
888
|
+
PromptNode,
|
|
889
|
+
TemplateTextNode
|
|
890
|
+
]);
|
|
891
|
+
//# sourceMappingURL=text-extra.js.map
|