feat: solution for 'Повторный экзамен: Граф с рефлексией и доработкой'

This commit is contained in:
2026-07-01 16:54:54 +03:00
parent e9a6f09c70
commit ab3d08e839
6 changed files with 388 additions and 270 deletions
+71 -32
View File
@@ -1,41 +1,80 @@
const Graph = require('./graph');
const { reflect } = require('./nodes/reflect');
const { rewrite } = require('./nodes/rewrite');
/**
* Entry point of the application.
* Builds a simple graph with reflect and rewrite nodes and runs it on sample input.
*/
async function main() {
// Ensure the OpenAI API key is set
if (!process.env.OPENAI_API_KEY) {
console.error('Error: OPENAI_API_KEY environment variable is not set.');
process.exit(1);
class Graph {
constructor() {
this.nodes = new Map(); // nodeId -> nodeData
this.edges = new Map(); // nodeId -> Set of neighbor nodeIds
this.edgeData = new Map(); // key `${from}->${to}` -> data
}
// Create graph and add nodes
const graph = new Graph();
graph.addNode('reflect', reflect);
graph.addNode('rewrite', rewrite);
addNode(id, data = {}) {
if (this.nodes.has(id)) {
throw new Error(`Node with id ${id} already exists`);
}
this.nodes.set(id, data);
this.edges.set(id, new Set());
}
// Sample input message
const inputMessage = 'I am feeling overwhelmed with my workload and unsure how to prioritize tasks.';
addEdge(from, to, data = {}) {
if (!this.nodes.has(from) || !this.nodes.has(to)) {
throw new Error(`Both nodes must exist to add an edge`);
}
this.edges.get(from).add(to);
const key = `${from}->${to}`;
this.edgeData.set(key, data);
}
console.log('--- Input Message ---');
console.log(inputMessage);
console.log('---------------------\n');
getNeighbors(id) {
if (!this.nodes.has(id)) {
throw new Error(`Node with id ${id} does not exist`);
}
return Array.from(this.edges.get(id));
}
try {
// Execute the graph: first reflect, then rewrite
const finalOutput = await graph.run(['reflect', 'rewrite'], inputMessage);
getNode(id) {
return this.nodes.get(id);
}
console.log('--- Final Output ---');
console.log(finalOutput);
console.log('---------------------');
} catch (err) {
console.error('An error occurred during graph execution:');
console.error(err.message);
getAllNodes() {
return Array.from(this.nodes.keys());
}
getAllEdges() {
const edges = [];
for (const [from, neighbors] of this.edges.entries()) {
for (const to of neighbors) {
const key = `${from}->${to}`;
edges.push({ from, to, data: this.edgeData.get(key) });
}
}
return edges;
}
getEdgeData(from, to) {
const key = `${from}->${to}`;
return this.edgeData.get(key);
}
// Reflection methods
getProperties() {
return Object.getOwnPropertyNames(this);
}
getMethods() {
const proto = Object.getPrototypeOf(this);
return Object.getOwnPropertyNames(proto).filter(
(name) => typeof this[name] === 'function' && name !== 'constructor'
);
}
// Introspection utilities
getNodeProperties(id) {
const node = this.nodes.get(id);
return node ? Object.keys(node) : null;
}
getEdgeProperties(from, to) {
const data = this.getEdgeData(from, to);
return data ? Object.keys(data) : null;
}
}
main();
module.exports = Graph;