Skip to content

Commit 7caee5d

Browse files
committed
refactor(import): deduplicate actions.ts by reusing import-utils utilities
actions.ts reimplemented 5 utilities that already exist in import-utils.ts. Replace local definitions with imports and use updateDeployedState() instead of inline state manipulation. Removed: sanitize(), toStackName(), fixPyprojectForSetuptools(), COPY_EXCLUDE_DIRS, copyDirRecursive() — all duplicates of import-utils.ts.
1 parent 83153af commit 7caee5d

2 files changed

Lines changed: 25 additions & 97 deletions

File tree

src/cli/commands/import/actions.ts

Lines changed: 24 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { silentIoHost } from '../../cdk/toolkit-lib';
1313
import { ExecLogger } from '../../logging';
1414
import { bootstrapEnvironment, buildCdkProject, checkBootstrapNeeded, synthesizeCdk } from '../../operations/deploy';
1515
import { setupPythonProject } from '../../operations/python/setup';
16+
import { copyDirRecursive, fixPyprojectForSetuptools, toStackName, updateDeployedState } from './import-utils';
1617
import { executePhase1, getDeployedTemplate } from './phase1-update';
1718
import { executePhase2, publishCdkAssets } from './phase2-import';
1819
import type { CfnTemplate } from './template-utils';
@@ -29,14 +30,6 @@ export interface ImportOptions {
2930
onProgress?: (message: string) => void;
3031
}
3132

32-
function sanitize(name: string): string {
33-
return name.replace(/_/g, '-');
34-
}
35-
36-
function toStackName(projectName: string, targetName: string): string {
37-
return `AgentCore-${sanitize(projectName)}-${sanitize(targetName)}`;
38-
}
39-
4033
/**
4134
* Convert parsed starter toolkit agents to CLI AgentEnvSpec format.
4235
*/
@@ -707,44 +700,29 @@ export async function handleImport(options: ImportOptions): Promise<ImportResult
707700
logger.startStep('Update deployed state');
708701
logger.log('Updating deployed state...');
709702
onProgress?.('Updating deployed state...');
710-
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any */
711-
const existingState: any = await configIO.readDeployedState().catch(() => ({ targets: {} }));
712-
const targetState = existingState.targets[targetName] ?? { resources: {} };
713-
targetState.resources ??= {};
714-
targetState.resources.stackName = stackName;
715-
716-
if (agentsToImport.length > 0) {
717-
targetState.resources.runtimes ??= {};
718-
for (const agent of agentsToImport) {
719-
if (agent.physicalAgentId) {
720-
targetState.resources.runtimes[agent.name] = {
721-
runtimeId: agent.physicalAgentId,
722-
runtimeArn:
723-
agent.physicalAgentArn ??
724-
`arn:aws:bedrock-agentcore:${target.region}:${target.account}:runtime/${agent.physicalAgentId}`,
725-
roleArn: 'imported', // Placeholder — updated after agentcore deploy
726-
};
727-
}
728-
}
729-
}
730-
731-
if (memoriesToImport.length > 0) {
732-
targetState.resources.memories ??= {};
733-
for (const memory of memoriesToImport) {
734-
if (memory.physicalMemoryId) {
735-
targetState.resources.memories[memory.name] = {
736-
memoryId: memory.physicalMemoryId,
737-
memoryArn:
738-
memory.physicalMemoryArn ??
739-
`arn:aws:bedrock-agentcore:${target.region}:${target.account}:memory/${memory.physicalMemoryId}`,
740-
};
741-
}
742-
}
743-
}
744-
745-
existingState.targets[targetName] = targetState;
746-
await configIO.writeDeployedState(existingState);
747-
/* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any */
703+
const importedResources = [
704+
...agentsToImport
705+
.filter(a => a.physicalAgentId)
706+
.map(a => ({
707+
type: 'runtime' as const,
708+
name: a.name,
709+
id: a.physicalAgentId!,
710+
arn:
711+
a.physicalAgentArn ??
712+
`arn:aws:bedrock-agentcore:${target.region}:${target.account}:runtime/${a.physicalAgentId}`,
713+
})),
714+
...memoriesToImport
715+
.filter(m => m.physicalMemoryId)
716+
.map(m => ({
717+
type: 'memory' as const,
718+
name: m.name,
719+
id: m.physicalMemoryId!,
720+
arn:
721+
m.physicalMemoryArn ??
722+
`arn:aws:bedrock-agentcore:${target.region}:${target.account}:memory/${m.physicalMemoryId}`,
723+
})),
724+
];
725+
await updateDeployedState(configIO, targetName, stackName, importedResources);
748726
logger.endStep('success');
749727

750728
logger.finalize(true);
@@ -764,53 +742,3 @@ export async function handleImport(options: ImportOptions): Promise<ImportResult
764742
return { success: false, error: message, logPath: logger.getRelativeLogPath() };
765743
}
766744
}
767-
768-
/**
769-
* Fix pyproject.toml for setuptools auto-discovery issues.
770-
* Starter toolkit projects may have multiple top-level directories (model/, mcp_client/)
771-
* which causes setuptools to refuse building. Adding `py-modules = []` tells setuptools
772-
* not to auto-discover packages.
773-
*/
774-
function fixPyprojectForSetuptools(pyprojectPath: string): void {
775-
if (!fs.existsSync(pyprojectPath)) return;
776-
777-
const content = fs.readFileSync(pyprojectPath, 'utf-8');
778-
779-
// Already has [tool.setuptools] section — don't touch it
780-
if (content.includes('[tool.setuptools]')) return;
781-
782-
// Append the fix
783-
fs.writeFileSync(pyprojectPath, content.trimEnd() + '\n\n[tool.setuptools]\npy-modules = []\n');
784-
}
785-
786-
const COPY_EXCLUDE_DIRS = new Set([
787-
'.venv',
788-
'.git',
789-
'__pycache__',
790-
'node_modules',
791-
'.pytest_cache',
792-
'.bedrock_agentcore',
793-
'.mypy_cache',
794-
'.ruff_cache',
795-
]);
796-
797-
/**
798-
* Recursively copy directory contents, skipping excluded directories and symlinks.
799-
*/
800-
function copyDirRecursive(src: string, dest: string): void {
801-
const entries = fs.readdirSync(src, { withFileTypes: true });
802-
for (const entry of entries) {
803-
if (entry.isSymbolicLink()) continue;
804-
const srcPath = path.join(src, entry.name);
805-
const destPath = path.join(dest, entry.name);
806-
if (entry.isDirectory()) {
807-
if (COPY_EXCLUDE_DIRS.has(entry.name)) continue;
808-
if (!fs.existsSync(destPath)) {
809-
fs.mkdirSync(destPath, { recursive: true });
810-
}
811-
copyDirRecursive(srcPath, destPath);
812-
} else {
813-
fs.copyFileSync(srcPath, destPath);
814-
}
815-
}
816-
}

src/cli/commands/import/import-utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ export function copyDirRecursive(src: string, dest: string): void {
276276
/**
277277
* Fix pyproject.toml for setuptools auto-discovery issues.
278278
*/
279-
function fixPyprojectForSetuptools(pyprojectPath: string): void {
279+
export function fixPyprojectForSetuptools(pyprojectPath: string): void {
280280
if (!fs.existsSync(pyprojectPath)) return;
281281

282282
const content = fs.readFileSync(pyprojectPath, 'utf-8');

0 commit comments

Comments
 (0)