Skip to content

Commit ec60868

Browse files
committed
feat(api): add new string template nodes and a conversion node for enhanced text processing
1 parent 4c95c6a commit ec60868

4 files changed

Lines changed: 103 additions & 13 deletions

File tree

apps/api/src/nodes/node-registry.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ import { InputTextNode } from "./text/input-text-node";
6363
import { Llama318BInstructFastNode } from "./text/llama-3-1-8b-instruct-fast-node";
6464
import { Llama3370BInstructFastNode } from "./text/llama-3-3-70b-instruct-fp8-fast-node";
6565
import { M2m10012bNode } from "./text/m2m100-1-2b-node";
66+
import { MultiVariableStringTemplateNode } from "./text/multi-variable-string-template-node";
6667
import { RadioGroupNode } from "./text/radio-group-node";
67-
import { SimpleStringTemplateNode } from "./text/simple-string-template-node";
68-
import { StringTemplateNode } from "./text/string-template-node";
68+
import { SingleVariableStringTemplateNode } from "./text/single-variable-string-template-node";
6969
import { TextAreaNode } from "./text/text-area-node";
70+
import { ToStringNode } from "./text/to-string-node";
7071
import { TwilioSmsNode } from "./text/twilio-sms-node";
7172
import { ExecutableNode } from "./types";
7273

@@ -125,6 +126,7 @@ export class NodeRegistry {
125126
this.registerImplementation(RadioGroupNode);
126127
this.registerImplementation(TextAreaNode);
127128
this.registerImplementation(InputTextNode);
129+
this.registerImplementation(ToStringNode);
128130
this.registerImplementation(NumberInputNode);
129131
this.registerImplementation(SliderNode);
130132
this.registerImplementation(Llama318BInstructFastNode);
@@ -149,8 +151,8 @@ export class NodeRegistry {
149151
this.registerImplementation(JsonNumberExtractorNode);
150152
this.registerImplementation(JsonObjectArrayExtractorNode);
151153
this.registerImplementation(JsonTemplateNode);
152-
this.registerImplementation(StringTemplateNode);
153-
this.registerImplementation(SimpleStringTemplateNode);
154+
this.registerImplementation(MultiVariableStringTemplateNode);
155+
this.registerImplementation(SingleVariableStringTemplateNode);
154156
this.registerImplementation(MonacoEditorNode);
155157
this.registerImplementation(LLaVA157BHFNode);
156158
this.registerImplementation(CanvasDoodleNode);

apps/api/src/nodes/text/string-template-node.ts renamed to apps/api/src/nodes/text/multi-variable-string-template-node.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { NodeExecution, NodeType } from "@dafthunk/types";
33
import { ExecutableNode } from "../types";
44
import { NodeContext } from "../types";
55

6-
export class StringTemplateNode extends ExecutableNode {
6+
export class MultiVariableStringTemplateNode extends ExecutableNode {
77
public static readonly nodeType: NodeType = {
8-
id: "string-template",
9-
name: "String Template",
10-
type: "string-template",
8+
id: "multi-variable-string-template",
9+
name: "Multi-Variable String Template",
10+
type: "multi-variable-string-template",
1111
description:
12-
"Create a string using a template with variable injection using ${variableName} syntax",
12+
"Create a string using a template with multiple variable injection using ${variableName} syntax",
1313
category: "Text",
1414
icon: "quote",
1515
inputs: [

apps/api/src/nodes/text/simple-string-template-node.ts renamed to apps/api/src/nodes/text/single-variable-string-template-node.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { NodeExecution, NodeType } from "@dafthunk/types";
33
import { ExecutableNode } from "../types";
44
import { NodeContext } from "../types";
55

6-
export class SimpleStringTemplateNode extends ExecutableNode {
6+
export class SingleVariableStringTemplateNode extends ExecutableNode {
77
public static readonly nodeType: NodeType = {
8-
id: "simple-string-template",
9-
name: "Simple String Template",
10-
type: "simple-string-template",
8+
id: "single-variable-string-template",
9+
name: "Single Variable String Template",
10+
type: "single-variable-string-template",
1111
description:
1212
"Create a string using a template with a single variable injection using ${variable} syntax",
1313
category: "Text",
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import { NodeExecution, NodeType } from "@dafthunk/types";
2+
3+
import { ExecutableNode } from "../types";
4+
import { NodeContext } from "../types";
5+
6+
/**
7+
* ToString node implementation
8+
* This node converts various input types to their string representation.
9+
*/
10+
export class ToStringNode extends ExecutableNode {
11+
public static readonly nodeType: NodeType = {
12+
id: "to-string",
13+
name: "To String",
14+
type: "to-string",
15+
description: "Converts any input value to its string representation",
16+
category: "Text",
17+
icon: "text",
18+
inputs: [
19+
{
20+
name: "value",
21+
type: "string",
22+
description: "Value to convert to string (accepts any type)",
23+
required: true,
24+
},
25+
],
26+
outputs: [
27+
{
28+
name: "result",
29+
type: "string",
30+
description: "The string representation of the input value",
31+
},
32+
],
33+
};
34+
35+
async execute(context: NodeContext): Promise<NodeExecution> {
36+
try {
37+
const value = context.inputs.value;
38+
let result: string;
39+
40+
// Handle null and undefined
41+
if (value === null) {
42+
result = "null";
43+
} else if (value === undefined) {
44+
result = "undefined";
45+
}
46+
// Handle string (pass through)
47+
else if (typeof value === "string") {
48+
result = value;
49+
}
50+
// Handle number
51+
else if (typeof value === "number") {
52+
result = value.toString();
53+
}
54+
// Handle boolean
55+
else if (typeof value === "boolean") {
56+
result = value.toString();
57+
}
58+
// Handle arrays
59+
else if (Array.isArray(value)) {
60+
try {
61+
result = JSON.stringify(value);
62+
} catch {
63+
result = value.toString();
64+
}
65+
}
66+
// Handle objects (including JSON)
67+
else if (typeof value === "object") {
68+
try {
69+
result = JSON.stringify(value);
70+
} catch {
71+
result = value.toString();
72+
}
73+
}
74+
// Handle everything else
75+
else {
76+
result = String(value);
77+
}
78+
79+
return this.createSuccessResult({
80+
result,
81+
});
82+
} catch (error) {
83+
return this.createErrorResult(
84+
error instanceof Error ? error.message : "Unknown error"
85+
);
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)