Modified envvar detection & evaluation criteria to be text inside of {{}} instead of {}

This commit is contained in:
Waleed Latif
2025-02-01 17:37:04 -08:00
parent 4e44594953
commit 667704820d
3 changed files with 119 additions and 13 deletions
+11 -9
View File
@@ -3,32 +3,34 @@
import { ReactNode } from 'react'
/**
* Formats text by highlighting block references (<...>) and environment variables ({...})
* Formats text by highlighting block references (<...>) and environment variables ({{...}})
* Used in code editor, long inputs, and short inputs for consistent syntax highlighting
*/
export function formatDisplayText(text: string | null): ReactNode {
if (!text) return null
export function formatDisplayText(text: string): ReactNode[] {
if (!text) return []
// Split the text by both tag patterns <something.something> and {ENV_VAR}
const parts = text.split(/(<[^>]+>|\{[^}]+\})/g)
// Split the text by both tag patterns <something.something> and {{ENV_VAR}}
const parts = text.split(/(<[^>]+>|\{\{[^}]+\}\})/g)
return parts.map((part, index) => {
// Check if the part matches connection tag pattern
if (part.match(/^<[^>]+>$/)) {
// Handle block references
if (part.startsWith('<') && part.endsWith('>')) {
return (
<span key={index} className="text-blue-500">
{part}
</span>
)
}
// Check if the part matches environment variable pattern
if (part.match(/^\{[^}]+\}$/)) {
// Handle environment variables
if (part.match(/^\{\{[^}]+\}\}$/)) {
return (
<span key={index} className="text-blue-500">
{part}
</span>
)
}
return <span key={index}>{part}</span>
})
}
+104
View File
@@ -898,4 +898,108 @@ describe('Executor', () => {
expect(fetchCalls).toHaveLength(2);
});
});
describe('Environment Variables', () => {
beforeEach(() => {
// Reset fetch mock before each test
global.fetch = jest.fn()
})
it('should resolve environment variables with double curly braces', async () => {
const mockTool = createMockTool(
'test-tool',
'Test Tool',
{ result: 'test processed', data: { status: 200 } }
);
(tools as any)['test-tool'] = mockTool
const workflow: SerializedWorkflow = {
version: '1.0',
blocks: [{
id: 'block-1',
position: { x: 0, y: 0 },
config: {
tool: 'test-tool',
params: { input: 'test {{ENV_VAR}}' }
},
inputs: { input: 'string' },
outputs: {
output: {
response: {
text: 'string',
status: 'number'
} as ValueType
} as BlockOutput
},
enabled: true
}],
connections: []
}
// Mock fetch response
global.fetch = jest.fn().mockImplementation(() =>
Promise.resolve({
ok: true,
json: () => Promise.resolve({
success: true,
output: {
text: 'test processed',
status: 200
}
})
})
)
const envVars = { ENV_VAR: 'value' }
const executor = new Executor(workflow, {}, envVars)
const result = await executor.execute('workflow-1')
expect(result.success).toBe(true)
expect(global.fetch).toHaveBeenCalledWith(
'https://api.test.com/endpoint',
expect.objectContaining({
method: 'POST',
body: JSON.stringify({ input: 'test value' })
})
)
})
it('should throw error for undefined environment variables', async () => {
const mockTool = createMockTool(
'test-tool',
'Test Tool',
{ result: 'test processed', data: { status: 200 } }
);
(tools as any)['test-tool'] = mockTool
const workflow: SerializedWorkflow = {
version: '1.0',
blocks: [{
id: 'block-1',
position: { x: 0, y: 0 },
config: {
tool: 'test-tool',
params: { input: 'test {{UNDEFINED_VAR}}' }
},
inputs: { input: 'string' },
outputs: {
output: {
response: {
text: 'string',
status: 'number'
} as ValueType
} as BlockOutput
},
enabled: true
}],
connections: []
}
const executor = new Executor(workflow)
const result = await executor.execute('workflow-1')
expect(result.success).toBe(false)
expect(result.error).toContain('Environment variable "UNDEFINED_VAR" was not found')
})
})
})
+4 -4
View File
@@ -371,12 +371,12 @@ export class Executor {
}
}
// Handle environment variables with {} syntax
const envMatches = resolvedValue.match(/\{([^}]+)\}/g)
// Handle environment variables with {{}} syntax
const envMatches = resolvedValue.match(/\{\{([^}]+)\}\}/g)
if (envMatches) {
for (const match of envMatches) {
const envKey = match.slice(1, -1) // remove { and }
const envValue = context.environmentVariables?.[envKey]
const envKey = match.slice(2, -2) // remove {{ and }}
const envValue = this.environmentVariables?.[envKey]
if (envValue === undefined) {
throw new Error(`Environment variable "${envKey}" was not found.`)