Skip to content

Commit 27368d8

Browse files
author
Евгений Балякин
committed
improvements for python
1 parent 9979191 commit 27368d8

3 files changed

Lines changed: 34 additions & 5 deletions

File tree

src/core/context.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ export async function buildContext(root: string, options: ContextOptions) {
4444
}
4545
try {
4646
const { text: source } = await readTextFileSafe(file.absolutePath, undefined, root);
47-
const skeleton = await skeletonSourceAsync(root, file.relativePath, source, { budget: Math.min(2000, budget) });
47+
const structuralMode = options.mode === 'architecture' || options.mode === 'overview' || options.mode === 'edit_prep' || options.mode === 'composition' || options.mode === 'test_impact';
48+
const skeleton = await skeletonSourceAsync(root, file.relativePath, source, { budget: structuralMode ? undefined : Math.min(2000, budget) });
4849
const symbols = flattenSymbols(skeleton.symbols);
4950
const content = JSON.stringify(skeleton, null, 2);
5051
fileRecords.push({ path: file.relativePath, source, imports: symbols.filter((symbol) => symbol.kind === 'import').map((symbol) => symbol.source ?? symbol.signature), exported: symbols.filter((symbol) => symbol.exported).map((symbol) => ({ name: symbol.qualifiedName, kind: symbol.kind })), symbols, symbolText: symbols.map((symbol) => `${symbol.name} ${symbol.signature}`).join('\n'), tokens: skeleton.tokenEstimate, size: file.size, content, symbolId: symbols.find((symbol) => symbol.kind !== 'import')?.symbolId });
@@ -393,7 +394,7 @@ function uniqueBy<T>(items: T[], key: (item: T) => string): T[] {
393394
}
394395

395396
export function renderContext(data: Awaited<ReturnType<typeof buildContext>>): string {
396-
if (data.mode === 'architecture') return data.items[0]?.content ?? 'Architecture summary: empty';
397+
if (data.mode === 'architecture' || data.mode === 'overview' || data.mode === 'edit_prep' || data.mode === 'composition' || data.mode === 'test_impact') return data.items[0]?.content ?? `${data.mode} summary: empty`;
397398
const relatedTests = data.testRelations.length ? `\n\nRelated tests:\n${data.testRelations.slice(0, 10).map((item) => ` ${item.test} -> ${item.source} (${item.reason})`).join('\n')}` : '';
398399
return `Context pack: ${data.usedTokens} tokens, ${data.items.length} included\n\n${data.items.map((item, index) => `${index + 1}. ${item.path} ${item.type} (${item.reason})`).join('\n')}\n\nNext reads:\n${data.nextReads.map((item) => ` codebone ${item.command} ${item.path}`).join('\n')}${relatedTests}`;
399400
}

src/core/skeleton.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,10 +266,11 @@ function pythonRoute(lines: string[], index: number): { method: string; path: st
266266
}
267267

268268
function pythonTableName(lines: string[], index: number): string | undefined {
269+
if (!/^\s*[A-Za-z_]\w*\s*=\s*(?:\w+\.)?Table\(/.test(lines[index])) return undefined;
269270
const text = lines.slice(index, Math.min(lines.length, index + 5)).map((line) => line.trim()).join(' ');
270-
return text.match(/(?:^|=\s*)(?:\w+\.)?Table\(\s*['"]([^'"]+)['"]/)?.[1]
271-
?? text.match(/(?:^|=\s*)sa\.Table\(\s*['"]([^'"]+)['"]/)?.[1]
272-
?? text.match(/(?:^|=\s*)sqlalchemy\.Table\(\s*['"]([^'"]+)['"]/)?.[1];
271+
return text.match(/(?:^|=\s*)(?:\w+\.)?Table\([^'"]*['"]([^'"]+)['"]/)?.[1]
272+
?? text.match(/(?:^|=\s*)sa\.Table\([^'"]*['"]([^'"]+)['"]/)?.[1]
273+
?? text.match(/(?:^|=\s*)sqlalchemy\.Table\([^'"]*['"]([^'"]+)['"]/)?.[1];
273274
}
274275

275276
function extractGo(lines: string[]): Candidate[] {

tests/skeleton.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,31 @@ describe('codebone core', () => {
148148
const rpcSymbols = flattenSymbols(rpcOnly.symbols);
149149
expect(rpcSymbols.every((symbol) => symbol.kind === 'route' || symbol.name.startsWith('rpc_'))).toBe(true);
150150
});
151+
152+
it('extracts each multiline SQLAlchemy table once', async () => {
153+
const tempRoot = await fs.mkdtemp(path.join(os.tmpdir(), 'codebone-db-tables-'));
154+
await fs.mkdir(path.join(tempRoot, 'app'));
155+
await fs.writeFile(path.join(tempRoot, 'app/db.py'), [
156+
'metadata = MetaData()',
157+
'',
158+
'Nodes = Table(',
159+
" 'nodes',",
160+
' metadata,',
161+
" Column('id', String),",
162+
')',
163+
'',
164+
'Credentials = Table(',
165+
' # deprecated',
166+
' "credentials",',
167+
' metadata,',
168+
" Column('id', String),",
169+
')',
170+
'',
171+
].join('\n'));
172+
173+
const skeleton = await skeletonPath(tempRoot, 'app/db.py', { publicApiOnly: true, noImports: true });
174+
const tables = flattenSymbols(skeleton.symbols).filter((symbol) => symbol.kind === 'table').map((symbol) => `${symbol.name}:${symbol.startLine}`);
175+
176+
expect(tables).toEqual(['nodes:3', 'credentials:9']);
177+
});
151178
});

0 commit comments

Comments
 (0)