Skip to content

Commit e98340d

Browse files
committed
Improve code formatting and improve readability in various nodes
1 parent ee28b9d commit e98340d

19 files changed

Lines changed: 199 additions & 148 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,7 @@ export class CloudflareNodeRegistry extends BaseNodeRegistry {
554554
this.registerImplementation(Gpt5MiniNode);
555555
this.registerImplementation(Gpt5NanoNode);
556556
}
557-
557+
558558
// Anthropic Claude nodes
559559
if (hasAnthropic) {
560560
this.registerImplementation(ClaudeOpus41Node);

apps/api/src/nodes/math/calculator-node.test.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,24 @@ import { Node } from "@dafthunk/types";
44
import { NodeContext } from "../types";
55

66
describe("CalculatorNode", () => {
7-
const createNode = (): Node => ({
8-
id: "test-calculator",
9-
type: "calculator",
10-
position: { x: 0, y: 0 },
11-
} as unknown as Node);
12-
13-
const createContext = (inputs: Record<string, any>) => ({
14-
nodeId: "test-calculator",
15-
inputs,
16-
workflowId: "test-workflow",
17-
executionId: "test-execution",
18-
organizationId: "test-org",
19-
env: {},
20-
nodeRegistry: null,
21-
toolRegistry: null,
22-
} as unknown as NodeContext);
7+
const createNode = (): Node =>
8+
({
9+
id: "test-calculator",
10+
type: "calculator",
11+
position: { x: 0, y: 0 },
12+
}) as unknown as Node;
13+
14+
const createContext = (inputs: Record<string, any>) =>
15+
({
16+
nodeId: "test-calculator",
17+
inputs,
18+
workflowId: "test-workflow",
19+
executionId: "test-execution",
20+
organizationId: "test-org",
21+
env: {},
22+
nodeRegistry: null,
23+
toolRegistry: null,
24+
}) as unknown as NodeContext;
2325

2426
describe("nodeType", () => {
2527
it("should have correct node type definition", () => {
@@ -216,7 +218,9 @@ describe("CalculatorNode", () => {
216218

217219
it("should handle additional math functions", async () => {
218220
const node = new CalculatorNode(createNode());
219-
const context = createContext({ expression: "sign(-5) + trunc(3.7) + hypot(3, 4)" });
221+
const context = createContext({
222+
expression: "sign(-5) + trunc(3.7) + hypot(3, 4)",
223+
});
220224

221225
const result = await node.execute(context);
222226

apps/api/src/nodes/math/calculator-node.ts

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ export class CalculatorNode extends ExecutableNode {
4040
async execute(context: NodeContext): Promise<NodeExecution> {
4141
const { expression } = context.inputs;
4242

43-
if (!expression || typeof expression !== "string" || expression.trim() === "") {
43+
if (
44+
!expression ||
45+
typeof expression !== "string" ||
46+
expression.trim() === ""
47+
) {
4448
return this.createErrorResult("Missing or empty expression.");
4549
}
4650

@@ -49,19 +53,22 @@ export class CalculatorNode extends ExecutableNode {
4953
try {
5054
// Validate expression for safety - only allow mathematical operations
5155
const allowedPattern = /^[0-9+\-*/()., \t\n\r\s^%&|<>~]+$/;
52-
const mathFunctions = /(sin|cos|tan|asin|acos|atan|sinh|cosh|tanh|asinh|acosh|atanh|sqrt|cbrt|pow|exp|log|log10|abs|floor|ceil|round|min|max|random|PI|E|sign|trunc|hypot|atan2)/g;
53-
56+
const mathFunctions =
57+
/(sin|cos|tan|asin|acos|atan|sinh|cosh|tanh|asinh|acosh|atanh|sqrt|cbrt|pow|exp|log|log10|abs|floor|ceil|round|min|max|random|PI|E|sign|trunc|hypot|atan2)/g;
58+
5459
// Check if expression contains only allowed characters and math functions
55-
const cleanExpression = expression.replace(mathFunctions, '');
60+
const cleanExpression = expression.replace(mathFunctions, "");
5661
if (cleanExpression && !allowedPattern.test(cleanExpression)) {
57-
return this.createErrorResult("Expression contains invalid characters. Only numbers, operators (+, -, *, /, ^, %, &, |, <, >, ~), parentheses, and math functions are allowed.");
62+
return this.createErrorResult(
63+
"Expression contains invalid characters. Only numbers, operators (+, -, *, /, ^, %, &, |, <, >, ~), parentheses, and math functions are allowed."
64+
);
5865
}
5966

6067
const QuickJSModule = await getQuickJSWASMModule();
6168
vm = QuickJSModule.newContext();
6269

6370
// Replace ^ with ** for JavaScript exponentiation
64-
const jsExpression = expression.replace(/\^/g, '**');
71+
const jsExpression = expression.replace(/\^/g, "**");
6572

6673
// Create a bootstrap script that sets up the Math environment
6774
const bootstrapScript = `
@@ -159,8 +166,10 @@ export class CalculatorNode extends ExecutableNode {
159166
evalResult.value.dispose();
160167

161168
// Validate the result
162-
if (typeof result !== 'number' || isNaN(result) || !isFinite(result)) {
163-
return this.createErrorResult("Expression evaluation resulted in an invalid number (NaN or Infinity).");
169+
if (typeof result !== "number" || isNaN(result) || !isFinite(result)) {
170+
return this.createErrorResult(
171+
"Expression evaluation resulted in an invalid number (NaN or Infinity)."
172+
);
164173
}
165174

166175
return this.createSuccessResult({ result });
@@ -174,5 +183,4 @@ export class CalculatorNode extends ExecutableNode {
174183
}
175184
}
176185
}
177-
178-
}
186+
}

apps/api/src/nodes/text/claude-3-opus-node.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Anthropic, { APIError } from '@anthropic-ai/sdk';
1+
import Anthropic, { APIError } from "@anthropic-ai/sdk";
22
import { NodeExecution, NodeType } from "@dafthunk/types";
33

44
import { ExecutableNode } from "../types";
@@ -56,20 +56,20 @@ export class Claude3OpusNode extends ExecutableNode {
5656

5757
const client = new Anthropic({
5858
apiKey: context.env.ANTHROPIC_API_KEY,
59-
timeout: 60000
59+
timeout: 60000,
6060
});
6161

6262
const response = await client.messages.create({
6363
model: "claude-3-opus-latest",
6464
max_tokens: 1024,
6565
messages: [{ role: "user", content: input }],
66-
...(instructions && { system: instructions })
66+
...(instructions && { system: instructions }),
6767
});
6868

6969
const responseText = response.content
70-
.filter(block => block.type === 'text')
71-
.map(block => block.text)
72-
.join('');
70+
.filter((block) => block.type === "text")
71+
.map((block) => block.text)
72+
.join("");
7373

7474
return this.createSuccessResult({
7575
response: responseText,
@@ -84,4 +84,4 @@ export class Claude3OpusNode extends ExecutableNode {
8484
);
8585
}
8686
}
87-
}
87+
}

apps/api/src/nodes/text/claude-35-haiku-node.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Anthropic, { APIError } from '@anthropic-ai/sdk';
1+
import Anthropic, { APIError } from "@anthropic-ai/sdk";
22
import { NodeExecution, NodeType } from "@dafthunk/types";
33

44
import { ExecutableNode } from "../types";
@@ -56,20 +56,20 @@ export class Claude35HaikuNode extends ExecutableNode {
5656

5757
const client = new Anthropic({
5858
apiKey: context.env.ANTHROPIC_API_KEY,
59-
timeout: 60000
59+
timeout: 60000,
6060
});
6161

6262
const response = await client.messages.create({
6363
model: "claude-3-5-haiku-latest",
6464
max_tokens: 1024,
6565
messages: [{ role: "user", content: input }],
66-
...(instructions && { system: instructions })
66+
...(instructions && { system: instructions }),
6767
});
6868

6969
const responseText = response.content
70-
.filter(block => block.type === 'text')
71-
.map(block => block.text)
72-
.join('');
70+
.filter((block) => block.type === "text")
71+
.map((block) => block.text)
72+
.join("");
7373

7474
return this.createSuccessResult({
7575
response: responseText,
@@ -84,4 +84,4 @@ export class Claude35HaikuNode extends ExecutableNode {
8484
);
8585
}
8686
}
87-
}
87+
}

apps/api/src/nodes/text/claude-35-sonnet-node.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Anthropic, { APIError } from '@anthropic-ai/sdk';
1+
import Anthropic, { APIError } from "@anthropic-ai/sdk";
22
import { NodeExecution, NodeType } from "@dafthunk/types";
33

44
import { ExecutableNode } from "../types";
@@ -56,20 +56,20 @@ export class Claude35SonnetNode extends ExecutableNode {
5656

5757
const client = new Anthropic({
5858
apiKey: context.env.ANTHROPIC_API_KEY,
59-
timeout: 60000
59+
timeout: 60000,
6060
});
6161

6262
const response = await client.messages.create({
6363
model: "claude-3-5-sonnet-latest",
6464
max_tokens: 1024,
6565
messages: [{ role: "user", content: input }],
66-
...(instructions && { system: instructions })
66+
...(instructions && { system: instructions }),
6767
});
6868

6969
const responseText = response.content
70-
.filter(block => block.type === 'text')
71-
.map(block => block.text)
72-
.join('');
70+
.filter((block) => block.type === "text")
71+
.map((block) => block.text)
72+
.join("");
7373

7474
return this.createSuccessResult({
7575
response: responseText,
@@ -84,4 +84,4 @@ export class Claude35SonnetNode extends ExecutableNode {
8484
);
8585
}
8686
}
87-
}
87+
}

apps/api/src/nodes/text/claude-37-sonnet-node.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Anthropic, { APIError } from '@anthropic-ai/sdk';
1+
import Anthropic, { APIError } from "@anthropic-ai/sdk";
22
import { NodeExecution, NodeType } from "@dafthunk/types";
33

44
import { ExecutableNode } from "../types";
@@ -56,20 +56,20 @@ export class Claude37SonnetNode extends ExecutableNode {
5656

5757
const client = new Anthropic({
5858
apiKey: context.env.ANTHROPIC_API_KEY,
59-
timeout: 60000
59+
timeout: 60000,
6060
});
6161

6262
const response = await client.messages.create({
6363
model: "claude-3-7-sonnet-latest",
6464
max_tokens: 1024,
6565
messages: [{ role: "user", content: input }],
66-
...(instructions && { system: instructions })
66+
...(instructions && { system: instructions }),
6767
});
6868

6969
const responseText = response.content
70-
.filter(block => block.type === 'text')
71-
.map(block => block.text)
72-
.join('');
70+
.filter((block) => block.type === "text")
71+
.map((block) => block.text)
72+
.join("");
7373

7474
return this.createSuccessResult({
7575
response: responseText,

apps/api/src/nodes/text/claude-opus-4-node.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Anthropic, { APIError } from '@anthropic-ai/sdk';
1+
import Anthropic, { APIError } from "@anthropic-ai/sdk";
22
import { NodeExecution, NodeType } from "@dafthunk/types";
33

44
import { ExecutableNode } from "../types";
@@ -13,7 +13,8 @@ export class ClaudeOpus4Node extends ExecutableNode {
1313
id: "claude-opus-4",
1414
name: "Claude Opus 4",
1515
type: "claude-opus-4",
16-
description: "Most powerful Claude 4 model for complex reasoning and advanced analysis",
16+
description:
17+
"Most powerful Claude 4 model for complex reasoning and advanced analysis",
1718
tags: ["Text", "AI"],
1819
icon: "sparkles",
1920
computeCost: 60,
@@ -56,20 +57,20 @@ export class ClaudeOpus4Node extends ExecutableNode {
5657

5758
const client = new Anthropic({
5859
apiKey: context.env.ANTHROPIC_API_KEY,
59-
timeout: 60000
60+
timeout: 60000,
6061
});
6162

6263
const response = await client.messages.create({
6364
model: "claude-opus-4-0",
6465
max_tokens: 1024,
6566
messages: [{ role: "user", content: input }],
66-
...(instructions && { system: instructions })
67+
...(instructions && { system: instructions }),
6768
});
6869

6970
const responseText = response.content
70-
.filter(block => block.type === 'text')
71-
.map(block => block.text)
72-
.join('');
71+
.filter((block) => block.type === "text")
72+
.map((block) => block.text)
73+
.join("");
7374

7475
return this.createSuccessResult({
7576
response: responseText,
@@ -84,4 +85,4 @@ export class ClaudeOpus4Node extends ExecutableNode {
8485
);
8586
}
8687
}
87-
}
88+
}

apps/api/src/nodes/text/claude-opus-41-node.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Anthropic, { APIError } from '@anthropic-ai/sdk';
1+
import Anthropic, { APIError } from "@anthropic-ai/sdk";
22
import { NodeExecution, NodeType } from "@dafthunk/types";
33

44
import { ExecutableNode } from "../types";
@@ -56,20 +56,20 @@ export class ClaudeOpus41Node extends ExecutableNode {
5656

5757
const client = new Anthropic({
5858
apiKey: context.env.ANTHROPIC_API_KEY,
59-
timeout: 60000
59+
timeout: 60000,
6060
});
6161

6262
const response = await client.messages.create({
6363
model: "claude-opus-4-1",
6464
max_tokens: 1024,
6565
messages: [{ role: "user", content: input }],
66-
...(instructions && { system: instructions })
66+
...(instructions && { system: instructions }),
6767
});
6868

6969
const responseText = response.content
70-
.filter(block => block.type === 'text')
71-
.map(block => block.text)
72-
.join('');
70+
.filter((block) => block.type === "text")
71+
.map((block) => block.text)
72+
.join("");
7373

7474
return this.createSuccessResult({
7575
response: responseText,

0 commit comments

Comments
 (0)