Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ export interface ExecutorPositions {
executorPositions?: ExecutorPosition[];
}

// Test Manager related interfaces
// Test Manager related interfaces

export interface TestsDiscoveryRequest {
projectPath: string;
Expand Down Expand Up @@ -1939,6 +1939,25 @@ export interface ProjectArtifacts {
artifacts: Artifacts;
}

// CodeMap interfaces
export interface CodeMapRequest {
projectPath: string;
changesOnly?: boolean;
}

export interface CodeMapArtifact {
name: string;
type: string;
lineRange: Range;
properties: Record<string, any>;
children: CodeMapArtifact[];
}

export interface CodeMapResponse {
files?: Record<string, CodeMapArtifact[]>;
error?: string;
}

export interface ProjectInfoRequest {
projectPath: string;
}
Expand Down Expand Up @@ -2049,6 +2068,7 @@ export interface ExtendedLangClientInterface extends BIInterface {
updateStatusBar(): void;
getDidOpenParams(): DidOpenParams;
getProjectArtifacts(params: ProjectArtifactsRequest): Promise<ProjectArtifacts>;
getCodeMap(params: CodeMapRequest): Promise<CodeMapResponse>;
getProjectInfo(params: ProjectInfoRequest): Promise<ProjectInfo>;
openConfigToml(params: OpenConfigTomlRequest): Promise<void>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ import {
ImportsInfoResponse,
ProjectArtifactsRequest,
ProjectArtifacts,
CodeMapRequest,
CodeMapResponse,
Artifacts,
MemoryManagersRequest,
MemoryManagersResponse,
Expand Down Expand Up @@ -473,6 +475,7 @@ enum EXTENDED_APIS {
WSDL_API_CLIENT_GENERATE = 'wsdlService/genClient',
GET_PROJECT_INFO = 'designModelService/projectInfo',
GET_ARTIFACTS = 'designModelService/artifacts',
GET_CODEMAP = 'designModelService/codemap',
PUBLISH_ARTIFACTS = 'designModelService/publishArtifacts',
COPILOT_ALL_LIBRARIES = 'copilotLibraryManager/getLibrariesList',
COPILOT_FILTER_LIBRARIES = 'copilotLibraryManager/getFilteredLibraries',
Expand Down Expand Up @@ -623,6 +626,10 @@ export class ExtendedLangClient extends LanguageClient implements ExtendedLangCl
return this.sendRequest<ProjectArtifacts>(EXTENDED_APIS.GET_ARTIFACTS, params);
}

async getCodeMap(params: CodeMapRequest): Promise<CodeMapResponse> {
return this.sendRequest<CodeMapResponse>(EXTENDED_APIS.GET_CODEMAP, params);
}

async getProjectInfo(params: ProjectInfoRequest): Promise<ProjectInfo> {
return this.sendRequest<ProjectInfo>(EXTENDED_APIS.GET_PROJECT_INFO, params);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

import { AICommandExecutor, AICommandConfig, AIExecutionResult } from '../executors/base/AICommandExecutor';
import { Command, GenerateAgentCodeRequest, ProjectSource, MACHINE_VIEW, refreshReviewMode, ExecutionContext } from '@wso2/ballerina-core';
import { Command, GenerateAgentCodeRequest, ProjectSource, MACHINE_VIEW, refreshReviewMode, ExecutionContext, CodeMapResponse } from '@wso2/ballerina-core';
import { ModelMessage, stepCountIs, streamText, TextStreamPart } from 'ai';
import { getAnthropicClient, getProviderCacheControl, ANTHROPIC_SONNET_4 } from '../utils/ai-client';
import { populateHistoryForAgent, getErrorMessage } from '../utils/ai-utils';
Expand All @@ -28,12 +28,18 @@ import { createToolRegistry } from './tool-registry';
import { getProjectSource, cleanupTempProject } from '../utils/project/temp-project';
import { StreamContext } from './stream-handlers/stream-context';
import { checkCompilationErrors } from './tools/diagnostics-utils';
import { generateCodeMapMarkdown } from '../../bal-md/codemap-markdown';
import { updateAndSaveChat } from '../utils/events';
import { chatStateStorage } from '../../../views/ai-panel/chatStateStorage';
import { RPCLayer } from '../../../RPCLayer';
import { VisualizerWebview } from '../../../views/visualizer/webview';
import * as path from 'path';
import { approvalViewManager } from '../state/ApprovalViewManager';
import { StateMachine } from '../../../stateMachine';
import * as fs from 'fs';

// Cache for incremental code map generation, keyed by project path
const codeMapCache = new Map<string, CodeMapResponse>();

/**
* Determines which packages have been affected by analyzing modified files
Expand Down Expand Up @@ -144,6 +150,47 @@ export class AgentExecutor extends AICommandExecutor<GenerateAgentCodeRequest> {
this.config.executionContext
);

// Generate bal.md from codemap
try {
const langClient = StateMachine.langClient();
const projectPath = this.config.executionContext.projectPath;

let codeMap: CodeMapResponse;
const cached = codeMapCache.get(projectPath);

if (cached?.files) {
// Incremental: only fetch changed files
const delta = await langClient.getCodeMap({ projectPath, changesOnly: true });

if (delta.files && Object.keys(delta.files).length > 0) {
for (const [filePath, artifacts] of Object.entries(delta.files)) {
cached.files[filePath] = artifacts;
}
console.log(`[AgentExecutor] CodeMap incremental update: ${Object.keys(delta.files).length} file(s)`);
} else {
console.log(`[AgentExecutor] CodeMap incremental update: no changes`);
}
codeMap = cached;
} else {
// First call: full generation
codeMap = await langClient.getCodeMap({ projectPath });
codeMapCache.set(projectPath, codeMap);
console.log(`[AgentExecutor] CodeMap full generation cached`);
}

// Debug: save codemap JSON response
const codeMapJsonPath = path.join(projectPath, 'codemap.json');
fs.writeFileSync(codeMapJsonPath, JSON.stringify(codeMap, null, 2), 'utf-8');
console.log(`[AgentExecutor] CodeMap JSON saved to: ${codeMapJsonPath}`);

const balMd = generateCodeMapMarkdown(codeMap);
const balMdPath = path.join(projectPath, 'bal.md');
fs.writeFileSync(balMdPath, balMd, 'utf-8');
console.log(`[AgentExecutor] bal.md saved to: ${balMdPath}`);
} catch (error) {
console.warn('[AgentExecutor] Failed to generate bal.md:', error);
}

// 2. Send didOpen only if creating NEW temp (not reusing for review continuation)
if (!this.config.lifecycle?.existingTempPath) {
// Fresh project - Both schemas - correct
Expand Down
Loading