'use strict'; // Project memory for each session folder, so a new conversation does not // re-read the whole project (the expensive part of starting over): // // .agentweb/MEMORY.md what Claude learned about the project (Claude keeps it) // .agentweb/HANDOFF.md where the last conversation stopped (Claude keeps it) // .agentweb/MAP.md every file with its size and its functions/classes // with line numbers; built here, without any LLM // .agentweb/notes.json short per-file notes Claude left (share via the // agentweb MCP tool note_file); shown in the map while // the file is unchanged // .agentweb/changes.json the last few changes of each file: recorded after // every turn that changed files (the request text), or // the one-line summary given with log_change // // Every map line about a file starts with its path ("uses", "used by", recent // changes), so a Grep for a file name or a word returns all that is known // about it and the map never has to be read whole. // // MEMORY and HANDOFF go into the system prompt when a conversation starts; // the map is searched (Grep) instead of exploring the tree. const fs = require('fs'); const fsp = require('fs/promises'); const path = require('path'); const crypto = require('crypto'); let redactText = (t) => t; try { ({ redactText } = require('./redact')); } catch { // redact yoksa metin oldugu gibi } const DIR = '.agentweb'; const MAX_FILES = 6000; const MAX_DEPTH = 10; const MAX_SYMBOL_FILE_BYTES = 1024 * 1024; const MAX_LINECOUNT_BYTES = 4 * 1024 * 1024; const MAX_SYMBOLS_PER_FILE = 150; const MAX_MAP_BYTES = 2 * 1024 * 1024; const MAX_MEMORY_CHARS = 8000; const MAX_HANDOFF_CHARS = 4000; const MAX_NOTE_CHARS = 400; const MAX_NOTES = 2000; const MAX_CHANGES_PER_FILE = 3; const MAX_CHANGE_FILES = 3000; const MAX_CHANGE_CHARS = 200; const MAX_CHANGED_PER_TURN = 40; const MAX_DEPS_SHOWN = 20; const MAP_CACHE_VERSION = 4; const STAT_CONCURRENCY = 16; const SKIP_DIRS = new Set([ '.git', '.hg', '.svn', 'node_modules', '.venv', 'venv', 'env', '__pycache__', '.mypy_cache', '.pytest_cache', '.ruff_cache', '.tox', 'dist', 'build', 'out', '.next', '.nuxt', '.svelte-kit', '.cache', '.parcel-cache', 'target', '.gradle', '.idea', '.vs', '.vscode', 'obj', 'coverage', '.terraform', 'Pods', 'DerivedData', '.agentweb', 'bower_components', 'jspm_packages', '.turbo', '.yarn', '.pnpm-store', 'site-packages', ]); // Hidden folders are skipped except these. const KEEP_HIDDEN = new Set(['.github', '.claude', '.gitlab', '.circleci', '.devcontainer']); const BINARY_EXT = new Set([ 'png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'ico', 'icns', 'tif', 'tiff', 'psd', 'ase', 'aseprite', 'svgz', 'mp3', 'wav', 'ogg', 'flac', 'm4a', 'aac', 'opus', 'mp4', 'mkv', 'mov', 'webm', 'avi', 'zip', 'gz', 'tgz', 'bz2', 'xz', '7z', 'rar', 'tar', 'jar', 'war', 'apk', 'aab', 'ipa', 'dmg', 'iso', 'pdf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'odt', 'woff', 'woff2', 'ttf', 'otf', 'eot', 'exe', 'dll', 'so', 'dylib', 'a', 'o', 'obj', 'class', 'pyc', 'pyo', 'wasm', 'bin', 'dat', 'db', 'sqlite', 'onnx', 'pt', 'pth', 'safetensors', 'ckpt', 'gguf', 'npz', 'npy', 'pkl', 'xnb', 'rpa', 'rpyc', 'lock', ]); // Symbol finders per language: [regex, group index of the name, kind]. const JS = [ [/^\s*(?:export\s+)?(?:default\s+)?(?:async\s+)?function\s*\*?\s*([A-Za-z_$][\w$]*)/, 1], [/^\s*(?:export\s+)?(?:default\s+)?(?:abstract\s+)?class\s+([A-Za-z_$][\w$]*)/, 1], [/^\s*(?:export\s+)?(?:const|let|var)\s+([A-Za-z_$][\w$]*)\s*=\s*(?:async\s+)?(?:function\b|\([^)]*\)\s*=>|[A-Za-z_$][\w$]*\s*=>)/, 1], [/^\s*(?:export\s+)?(?:declare\s+)?(?:interface|type|enum)\s+([A-Za-z_$][\w$]*)/, 1], [/^\s{2,4}(?:static\s+|async\s+|get\s+|set\s+|public\s+|private\s+|protected\s+|readonly\s+)*([A-Za-z_$][\w$]*)\s*\([^)]*\)\s*(?::\s*[^{]+)?\{\s*$/, 1], ]; const LANGS = { js: JS, mjs: JS, cjs: JS, jsx: JS, ts: JS, tsx: JS, mts: JS, cts: JS, vue: JS, svelte: JS, py: [ [/^\s*(?:async\s+)?def\s+([A-Za-z_]\w*)/, 1], [/^\s*class\s+([A-Za-z_]\w*)/, 1], ], cs: [ [/^\s*(?:\[[^\]]*\]\s*)*(?:(?:public|private|protected|internal|static|sealed|abstract|partial|readonly|unsafe)\s+)*(?:class|struct|interface|enum|record)\s+([A-Za-z_]\w*)/, 1], [/^\s*(?:(?:public|private|protected|internal|static|override|virtual|abstract|async|sealed|extern|unsafe|new)\s+)+[\w<>[\],.?\s]+?\s+([A-Za-z_]\w*)\s*(?:<[^>]*>)?\s*\([^;]*$/, 1], ], java: [ [/^\s*(?:(?:public|private|protected|static|final|abstract)\s+)*(?:class|interface|enum|record)\s+([A-Za-z_]\w*)/, 1], [/^\s*(?:(?:public|private|protected|static|final|synchronized|abstract)\s+)+[\w<>[\],.?\s]+?\s+([A-Za-z_]\w*)\s*\([^;]*$/, 1], ], kt: [ [/^\s*(?:(?:public|private|internal|protected|open|data|sealed|abstract|inner|enum)\s+)*(?:class|interface|object)\s+([A-Za-z_]\w*)/, 1], [/^\s*(?:(?:public|private|internal|protected|override|suspend|inline|open)\s+)*fun\s+(?:<[^>]*>\s*)?(?:[\w.]+\.)?([A-Za-z_]\w*)/, 1], ], go: [ [/^func\s+(?:\([^)]*\)\s*)?([A-Za-z_]\w*)/, 1], [/^type\s+([A-Za-z_]\w*)/, 1], ], rs: [ [/^\s*(?:pub(?:\([^)]*\))?\s+)?(?:async\s+)?(?:unsafe\s+)?fn\s+([A-Za-z_]\w*)/, 1], [/^\s*(?:pub(?:\([^)]*\))?\s+)?(?:struct|enum|trait|mod|type)\s+([A-Za-z_]\w*)/, 1], [/^\s*impl(?:<[^>]*>)?\s+(?:[\w:<>]+\s+for\s+)?([A-Za-z_][\w:]*)/, 1], ], rb: [ [/^\s*def\s+(?:self\.)?([A-Za-z_]\w*[?!=]?)/, 1], [/^\s*(?:class|module)\s+([A-Z][\w:]*)/, 1], ], php: [ [/^\s*(?:(?:public|private|protected|static|abstract|final)\s+)*function\s+&?\s*([A-Za-z_]\w*)/, 1], [/^\s*(?:abstract\s+|final\s+)?(?:class|interface|trait|enum)\s+([A-Za-z_]\w*)/, 1], ], lua: [[/^\s*(?:local\s+)?function\s+([\w.:]+)/, 1]], gd: [ [/^\s*(?:static\s+)?func\s+([A-Za-z_]\w*)/, 1], [/^\s*(?:class_name|class)\s+([A-Za-z_]\w*)/, 1], ], rpy: [ [/^\s*(label|screen|transform|image)\s+([\w.]+)/, 2], [/^\s*(?:define|default)\s+([\w.]+)\s*=/, 1], ], sh: [[/^\s*(?:function\s+)?([A-Za-z_][\w-]*)\s*\(\)\s*\{?/, 1]], bash: null, swift: [ [/^\s*(?:(?:public|private|internal|fileprivate|open|static|final|override|@\w+)\s+)*func\s+([A-Za-z_]\w*)/, 1], [/^\s*(?:(?:public|private|internal|fileprivate|open|final)\s+)*(?:class|struct|enum|protocol|extension|actor)\s+([A-Za-z_][\w.]*)/, 1], ], dart: [ [/^\s*(?:abstract\s+)?(?:class|mixin|enum|extension)\s+([A-Za-z_]\w*)/, 1], [/^\s*(?:static\s+|Future<[^>]*>\s+|[\w<>?]+\s+)([a-z_]\w*)\s*\([^;]*\)\s*(?:async\s*)?\{\s*$/, 1], ], c: [ [/^(?:static\s+|inline\s+|extern\s+)*[A-Za-z_][\w\s*&:<>,]*?[\s*&]([A-Za-z_][\w:~]*)\s*\([^;]*$/, 1], [/^\s*(?:typedef\s+)?(?:struct|enum|union|class|namespace)\s+([A-Za-z_]\w*)\s*[{:]?\s*$/, 1], ], sql: [[/^\s*create\s+(?:or\s+replace\s+)?(?:table|view|function|procedure|index)\s+(?:if\s+not\s+exists\s+)?([\w."]+)/i, 1]], md: [[/^(#{1,3})\s+(.+?)\s*#*\s*$/, 2]], }; LANGS.bash = LANGS.sh; LANGS.zsh = LANGS.sh; for (const ext of ['h', 'hpp', 'hh', 'cc', 'cpp', 'cxx', 'm', 'mm']) LANGS[ext] = LANGS.c; LANGS.markdown = LANGS.md; LANGS.mdx = LANGS.md; LANGS.kts = LANGS.kt; LANGS.scala = LANGS.kt; const NOT_NAMES = new Set(['if', 'for', 'while', 'switch', 'catch', 'return', 'function', 'else', 'do', 'try', 'with', 'new', 'typeof', 'await', 'constructor', 'super', 'sizeof', 'elif', 'foreach', 'using', 'lock', 'fixed', 'when']); function agentwebDir(cwd) { return path.join(cwd, DIR); } function ensureDir(cwd) { const dir = agentwebDir(cwd); try { fs.mkdirSync(dir, { recursive: true }); const ignore = path.join(dir, '.gitignore'); if (!fs.existsSync(ignore)) { fs.writeFileSync(ignore, '# Agent Web proje hafizasi (yerel). Paylasmak istersen bu dosyayi sil.\n*\n'); } } catch { // klasor yazilamiyorsa hafiza olmadan devam edilir } return dir; } function readText(file, max) { try { const text = fs.readFileSync(file, 'utf8'); return max && text.length > max ? `${text.slice(0, max)}\n… (devamı dosyada)` : text; } catch { return ''; } } function extOf(name) { const m = /\.([A-Za-z0-9]+)$/.exec(name); return m ? m[1].toLowerCase() : ''; } function findSymbols(text, ext) { const rules = LANGS[ext]; if (!rules) return []; const out = []; const lines = text.split('\n'); for (let i = 0; i < lines.length && out.length < MAX_SYMBOLS_PER_FILE; i += 1) { const line = lines[i]; if (line.length > 400) continue; for (const [re, group] of rules) { const m = re.exec(line); if (!m) continue; let name = (m[group] || '').trim(); if (!name || NOT_NAMES.has(name)) continue; if (ext === 'md' || ext === 'markdown' || ext === 'mdx') name = `# ${name.slice(0, 60)}`; out.push([i + 1, name]); break; } } return out.slice(0, MAX_SYMBOLS_PER_FILE); } // Local files a source file refers to (relative imports,