Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions src/core/brain/memAgent/__test__/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,27 +255,32 @@ describe('Loader', () => {
expect(result.mcpServers.testServer.args).toEqual([1, 2, 3]);
});

it('should handle case-insensitive environment variable names', async () => {
process.env.test_var = 'lowercase';
process.env.TEST_VAR = 'uppercase';

const mockConfig = {
systemPrompt: '$test_var',
llm: {
provider: 'openai',
model: 'gpt-4',
apiKey: '$TEST_VAR',
},
};
// Windows treats environment variable names as case-insensitive,
// so setting both test_var and TEST_VAR overwrites the same variable.
// This test only applies to platforms with case-sensitive env vars.
it.skipIf(process.platform === 'win32')(
'should handle case-insensitive environment variable names',
async () => {
process.env.test_var = 'lowercase';
process.env.TEST_VAR = 'uppercase';

const mockConfig = {
systemPrompt: '$test_var',
llm: {
provider: 'openai',
model: 'gpt-4',
apiKey: '$TEST_VAR',
},
};

mockFs.readFile.mockResolvedValue(JSON.stringify(mockConfig));
mockParseYaml.mockReturnValue(mockConfig);
mockFs.readFile.mockResolvedValue(JSON.stringify(mockConfig));
mockParseYaml.mockReturnValue(mockConfig);

const result = (await loadAgentConfig('/path/to/config.yml')) as any;
console.log(result);
expect(result.systemPrompt).toBe('lowercase');
expect(result.llm.apiKey).toBe('uppercase');
});
const result = (await loadAgentConfig('/path/to/config.yml')) as any;
expect(result.systemPrompt).toBe('lowercase');
expect(result.llm.apiKey).toBe('uppercase');
}
);
});

describe('Config File Loading', () => {
Expand Down
126 changes: 62 additions & 64 deletions src/core/knowledge_graph/backend/neo4j.ts
Original file line number Diff line number Diff line change
Expand Up @@ -931,70 +931,68 @@ export class Neo4jBackend implements KnowledgeGraph {
return converted;
}

private buildFilterConstraints(
filters: NodeFilters | EdgeFilters,
prefix: string,
params: any
): string {
const constraints: string[] = [];

for (const [rawKey, filter] of Object.entries(filters)) {
const propertyKey = this.escapePropertyKey(rawKey);
const paramKey = `${prefix}_${Object.keys(params).length}`;

if (typeof filter === 'object' && filter !== null && !Array.isArray(filter)) {
// Range filters
if ('gte' in filter) {
constraints.push(`${prefix}.${propertyKey} >= $${paramKey}_gte`);
params[`${paramKey}_gte`] = filter.gte;
}
if ('gt' in filter) {
constraints.push(`${prefix}.${propertyKey} > $${paramKey}_gt`);
params[`${paramKey}_gt`] = filter.gt;
}
if ('lte' in filter) {
constraints.push(`${prefix}.${propertyKey} <= $${paramKey}_lte`);
params[`${paramKey}_lte`] = filter.lte;
}
if ('lt' in filter) {
constraints.push(`${prefix}.${propertyKey} < $${paramKey}_lt`);
params[`${paramKey}_lt`] = filter.lt;
}

// Array filters
if ('any' in filter && Array.isArray(filter.any)) {
constraints.push(`${prefix}.${propertyKey} IN $${paramKey}`);
params[paramKey] = filter.any;
}
if ('all' in filter && Array.isArray(filter.all)) {
// For 'all' filter, check if the property (assumed to be array) contains all values
constraints.push(
`all(x IN $${paramKey} WHERE x IN ${prefix}.${propertyKey})`
);
params[paramKey] = filter.all;
}
} else {
// Direct equality
constraints.push(`${prefix}.${propertyKey} = $${paramKey}`);
params[paramKey] = filter;
}
}

return constraints.join(' AND ');
}

private escapePropertyKey(key: string): string {
if (key.trim().length === 0) {
throw new Error('Filter property names must not be empty.');
}

if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) {
return key;
}

const escapedKey = key.replace(/`/g, '``');
return `\`${escapedKey}\``;
}
private buildFilterConstraints(
filters: NodeFilters | EdgeFilters,
prefix: string,
params: any
): string {
const constraints: string[] = [];

for (const [rawKey, filter] of Object.entries(filters)) {
const propertyKey = this.escapePropertyKey(rawKey);
const paramKey = `${prefix}_${Object.keys(params).length}`;

if (typeof filter === 'object' && filter !== null && !Array.isArray(filter)) {
// Range filters
if ('gte' in filter) {
constraints.push(`${prefix}.${propertyKey} >= $${paramKey}_gte`);
params[`${paramKey}_gte`] = filter.gte;
}
if ('gt' in filter) {
constraints.push(`${prefix}.${propertyKey} > $${paramKey}_gt`);
params[`${paramKey}_gt`] = filter.gt;
}
if ('lte' in filter) {
constraints.push(`${prefix}.${propertyKey} <= $${paramKey}_lte`);
params[`${paramKey}_lte`] = filter.lte;
}
if ('lt' in filter) {
constraints.push(`${prefix}.${propertyKey} < $${paramKey}_lt`);
params[`${paramKey}_lt`] = filter.lt;
}

// Array filters
if ('any' in filter && Array.isArray(filter.any)) {
constraints.push(`${prefix}.${propertyKey} IN $${paramKey}`);
params[paramKey] = filter.any;
}
if ('all' in filter && Array.isArray(filter.all)) {
// For 'all' filter, check if the property (assumed to be array) contains all values
constraints.push(`all(x IN $${paramKey} WHERE x IN ${prefix}.${propertyKey})`);
params[paramKey] = filter.all;
}
} else {
// Direct equality
constraints.push(`${prefix}.${propertyKey} = $${paramKey}`);
params[paramKey] = filter;
}
}

return constraints.join(' AND ');
}

private escapePropertyKey(key: string): string {
if (key.trim().length === 0) {
throw new Error('Filter property names must not be empty.');
}

if (/^[A-Za-z_][A-Za-z0-9_]*$/.test(key)) {
return key;
}

const escapedKey = key.replace(/`/g, '``');
return `\`${escapedKey}\``;
}

private async createIndexes(): Promise<void> {
const session = this.getSession();
Expand Down
Loading