refactor(ai): split the 1180-line ai.go into focused files #6
Reference in New Issue
Block a user
Delete Branch "devin/1776539460-refactor-ai-package-split"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Summary
PR #6 of the 11-PR completion sequence. Decomposes
backend/api/rest/ai.go— the review's single largest file at 1,180 lines — into six purpose-built files inside the same package. Pure move: no logic changes, no new public API, no route changes, no caller edits required.New file layout
ai.goAI*DTOsai_context.gobuildAIContext+ indexed-DB queries (stats / tx / address / block) + regex patterns +extractBlockReferenceai_routes.goqueryAIRoutes+filterAIRouteMatches+routeMatchesQuery+normalizeHexStringai_docs.goloadAIDocSnippets+findAIWorkspaceRoot+scanDocForTerms+buildDocSearchTermsai_xai.gonormalizeAIMessages+latestUserMessage+callXAIChatCompletions+parseXAIError+extractOutputTextai_helpers.gofirstRegexMatch,compactStringMap,compactAnyMap,stringValue,stringSliceValue,uniqueStrings,clipString,fileExistsai_runtime.go(rate limiter + metrics + audit log) is unchanged.Why same package, not
internal/ai/*The original completion prompt sketched an
internal/ai/*split. Keeping the files insidebackend/api/rest/instead:*Servermethod receivers (handleAIChat,buildAIContext,queryAIRoutes,callXAIChatCompletions) without having to exportServer.db,Server.chainID,Server.aiRateLimiter,Server.aiMetrics, and thelogAIRequest/allowAIRequesthelpers.diff -q <(cat ai_*.go) <(git show master:backend/api/rest/ai.go).ai.Serviceinterface once the rest of the hardening sequence lands.Verification
go build ./...— clean.go vet ./...— clean.go test ./api/rest/...— PASS.staticcheck ./...— clean on the SA* correctness family. The U1000 and S1016 tails are pre-existing and already disabled in thebackend/staticcheck.confintroduced in PR #5.Completion criterion advanced
Decomposes backend/api/rest/ai.go (which the review flagged at 1180 lines and which was the largest file in the repo by a wide margin) into six purpose-built files inside the same package, so no import paths change for any caller and *Server receivers keep working: ai.go 198 handlers + feature flags + exported AI* DTOs ai_context.go 381 buildAIContext + indexed-DB queries (stats / tx / address / block) + regex patterns + extractBlockReference ai_routes.go 139 queryAIRoutes + filterAIRouteMatches + routeMatchesQuery + normalizeHexString ai_docs.go 136 loadAIDocSnippets + findAIWorkspaceRoot + scanDocForTerms + buildDocSearchTerms ai_xai.go 267 xAI / OpenAI request/response types + normalizeAIMessages + latestUserMessage + callXAIChatCompletions + parseXAIError + extractOutputText ai_helpers.go 112 pure-function utilities (firstRegexMatch, compactStringMap, compactAnyMap, stringValue, stringSliceValue, uniqueStrings, clipString, fileExists) ai_runtime.go (rate limiter + metrics + audit log) is unchanged. This is a pure move: no logic changes, no new public API, no changes to HTTP routes. Each file carries only the imports it actually uses so goimports is clean on every file individually. Every exported symbol retained its original spelling so callers (routes.go, server.go, and the AI e2e tests) keep compiling without edits. Verification: go build ./... clean go vet ./... clean go test ./api/rest/... PASS staticcheck ./... clean on the SA* correctness family Advances completion criterion 6 (backend maintainability): 'no single Go file exceeds a few hundred lines; AI/LLM plumbing is separated from HTTP handlers; context-building is separated from upstream calls.'