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

This commit is contained in:
2026-07-01 14:40:09 +03:00
parent f14d41830d
commit 153b04b33c
10 changed files with 312 additions and 95 deletions
+82
View File
@@ -0,0 +1,82 @@
import { BaseNode } from './nodes/baseNode';
import { ReflectionNode } from './nodes/reflectionNode';
import { RewriteNode, RewriteFunction } from './nodes/rewriteNode';
export type Edge = {
from: string;
out: string;
to: string;
in: string;
};
export class Graph {
private nodes: Map<string, BaseNode>;
private edges: Edge[];
private nodeCounter: number;
constructor() {
this.nodes = new Map();
this.edges = [];
this.nodeCounter = 0;
}
private generateId(): string {
return `node_${this.nodeCounter++}`;
}
/**
* Creates a node of the specified type.
* @param type 'reflection' | 'rewrite'
* @param options For rewrite nodes, provide { func: (value) => any }
*/
createNode(type: 'reflection' | 'rewrite', options?: any): BaseNode {
const id = this.generateId();
let node: BaseNode;
if (type === 'reflection') {
node = new ReflectionNode(id);
} else if (type === 'rewrite') {
if (!options || typeof options.func !== 'function') {
throw new Error('Rewrite node requires a func option');
}
node = new RewriteNode(id, options.func);
} else {
throw new Error(`Unknown node type: ${type}`);
}
this.nodes.set(id, node);
return node;
}
addNode(node: BaseNode): void {
if (this.nodes.has(node.id)) {
throw new Error(`Node with id ${node.id} already exists`);
}
this.nodes.set(node.id, node);
}
addEdge(from: string, out: string, to: string, inKey: string): void {
if (!this.nodes.has(from) || !this.nodes.has(to)) {
throw new Error('Both nodes must exist to add an edge');
}
this.edges.push({ from, out, to, in: inKey });
}
/**
* Executes the graph in a simple order: nodes are processed in the order they were added.
* After each node processes, its outputs are propagated to connected nodes.
*/
run(): void {
for (const node of this.nodes.values()) {
node.process();
for (const edge of this.edges.filter(e => e.from === node.id)) {
const target = this.nodes.get(edge.to);
if (!target) continue;
const value = node.outputs.get(edge.out);
target.inputs.set(edge.in, value);
}
}
}
getNode(id: string): BaseNode | undefined {
return this.nodes.get(id);
}
}
+4 -10
View File
@@ -1,10 +1,4 @@
import { app } from './langgraph';
async function main() {
const result = await app.invoke({ input: 'Hello world' });
console.log('Final result:', result);
}
main().catch((err) => {
console.error('Error during execution:', err);
});
export { Graph } from './graph';
export { BaseNode } from './nodes/baseNode';
export { ReflectionNode } from './nodes/reflectionNode';
export { RewriteNode, RewriteFunction } from './nodes/rewriteNode';
+15
View File
@@ -0,0 +1,15 @@
export abstract class BaseNode {
id: string;
type: string;
inputs: Map<string, any>;
outputs: Map<string, any>;
constructor(id: string, type: string) {
this.id = id;
this.type = type;
this.inputs = new Map();
this.outputs = new Map();
}
abstract process(): void;
}
+14
View File
@@ -0,0 +1,14 @@
import { BaseNode } from './baseNode';
export class ReflectionNode extends BaseNode {
constructor(id: string) {
super(id, 'reflection');
}
process(): void {
// Copy all inputs to outputs with the same keys
this.inputs.forEach((value, key) => {
this.outputs.set(key, value);
});
}
}
+19
View File
@@ -0,0 +1,19 @@
import { BaseNode } from './baseNode';
export type RewriteFunction = (value: any) => any;
export class RewriteNode extends BaseNode {
private func: RewriteFunction;
constructor(id: string, func: RewriteFunction) {
super(id, 'rewrite');
this.func = func;
}
process(): void {
this.inputs.forEach((value, key) => {
const newValue = this.func(value);
this.outputs.set(key, newValue);
});
}
}