From 9ff9612bbb65433cc12a7ab0cd04b66a846a5fdc Mon Sep 17 00:00:00 2001 From: kuzakhmetovartur Date: Tue, 30 Jun 2026 16:42:32 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20solution=20for=20'=D0=9F=D0=BE=D0=B2?= =?UTF-8?q?=D1=82=D0=BE=D1=80=D0=BD=D1=8B=D0=B9=20=D1=8D=D0=BA=D0=B7=D0=B0?= =?UTF-8?q?=D0=BC=D0=B5=D0=BD:=20=D0=93=D1=80=D0=B0=D1=84=20=D1=81=20?= =?UTF-8?q?=D1=80=D0=B5=D1=84=D0=BB=D0=B5=D0=BA=D1=81=D0=B8=D0=B5=D0=B9=20?= =?UTF-8?q?=D0=B8=20=D0=B4=D0=BE=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BA=D0=BE?= =?UTF-8?q?=D0=B9'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 24 ++++++++++++++++-------- package.json | 22 ++++++---------------- src/index.ts | 10 ++++++++++ src/langgraph.ts | 41 +++++++++++++++++++++++++++++++++++++++++ tsconfig.json | 10 ++++++++++ 5 files changed, 83 insertions(+), 24 deletions(-) create mode 100644 src/index.ts create mode 100644 src/langgraph.ts create mode 100644 tsconfig.json diff --git a/README.md b/README.md index 0c58bf9..89de68f 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,25 @@ -# LangGraph Project +# LangGraph Reflection & Rewriting Demo -This project demonstrates a minimal setup for using LangGraph with LangChain OpenAI integration. +This project demonstrates a simple LangGraph state machine that includes: +- **Reflection node** – logs the current state. +- **Rewriting node** – transforms the input string to uppercase. +- **End node** – logs the final state. ## Setup ```bash -pip install -r requirements.txt +npm install +npm run build +npm start ``` -## Running +The output will show the state at each node and the final transformed result. -```bash -python main.py -``` +## Project Structure -The script will import the necessary modules and print a confirmation message. \ No newline at end of file +- `src/langgraph.ts` – Defines the state type, node functions, and constructs the graph. +- `src/index.ts` – Entry point that runs the graph with an example input. +- `package.json` – Project metadata and dependencies. +- `tsconfig.json` – TypeScript compiler configuration. + +No JavaScript files are present; the entire project is written in TypeScript. \ No newline at end of file diff --git a/package.json b/package.json index 5f193f1..fc2e65f 100644 --- a/package.json +++ b/package.json @@ -1,25 +1,15 @@ { - "name": "self-correcting-agent", + "name": "langgraph-reflection-rewrite", "version": "1.0.0", - "description": "A simple Node.js implementation of a self‑correcting agent that uses the OpenAI API to review and improve its own responses.", - "main": "index.js", + "main": "dist/index.js", "scripts": { - "start": "node index.js", - "test": "jest" + "build": "tsc", + "start": "node dist/index.js" }, - "keywords": [ - "openai", - "self-correcting", - "agent", - "nodejs" - ], - "author": "Your Name", - "license": "MIT", "dependencies": { - "dotenv": "^16.4.5", - "openai": "^4.20.0" + "langgraph": "^0.1.0" }, "devDependencies": { - "jest": "^29.7.0" + "typescript": "^5.0.0" } } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts new file mode 100644 index 0000000..1ca94db --- /dev/null +++ b/src/index.ts @@ -0,0 +1,10 @@ +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); +}); \ No newline at end of file diff --git a/src/langgraph.ts b/src/langgraph.ts new file mode 100644 index 0000000..627cb22 --- /dev/null +++ b/src/langgraph.ts @@ -0,0 +1,41 @@ +import { StateGraph } from 'langgraph'; + +export type State = { + input: string; + output?: string; +}; + +const startFn = (state: State) => { + // The start node simply passes the initial state through. + return state; +}; + +const reflection = (state: State) => { + console.log('Reflection node:', state); + return state; +}; + +const rewriting = (state: State) => { + const newState = { ...state, output: state.input.toUpperCase() }; + console.log('Rewriting node:', newState); + return newState; +}; + +const end = (state: State) => { + console.log('End node:', state); + return state; +}; + +export const graph = new StateGraph(); + +graph.addNode('start', startFn); +graph.addNode('reflection', reflection); +graph.addNode('rewriting', rewriting); +graph.addNode('end', end); + +graph.setEntryPoint('start'); +graph.addEdge('start', 'reflection'); +graph.addEdge('reflection', 'rewriting'); +graph.addEdge('rewriting', 'end'); + +export const app = graph.compile(); \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..d52442e --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,10 @@ +{ + "compilerOptions": { + "target": "ES2020", + "module": "CommonJS", + "outDir": "dist", + "strict": true, + "esModuleInterop": true + }, + "include": ["src"] +} \ No newline at end of file