Skip to content

Commit d8ed027

Browse files
committed
refactor: replace component creation instruction with generic task step functionality
1 parent 9459348 commit d8ed027

File tree

5 files changed

+86
-162
lines changed

5 files changed

+86
-162
lines changed

src/get-component-creation-instruction.ts

Lines changed: 0 additions & 157 deletions
This file was deleted.

src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { z } from 'zod';
88
import { getCodingGuidelines } from './coding-guideline.js';
99
import { getFigmaData } from './figma/get-data.js';
1010
import { getFigmaImage } from './figma/get-image.js';
11-
import { getComponentCreationInstruction } from './get-component-creation-instruction.js';
11+
import { getTaskStep } from './utils/get-task-step.js';
1212

1313
const packageJsonPath = path.resolve(import.meta.dirname, '../package.json');
1414
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'));
@@ -43,13 +43,18 @@ server.tool(
4343
);
4444

4545
server.tool(
46-
'get_component_creation_instruction',
47-
'Get component creation instruction',
46+
'get_task_step',
47+
'Get task step',
4848
{
49+
cwd: z.string().describe('Current working directory (Absolute path)'),
50+
filePath: z.string().describe('Task file path (Relative to cwd)'),
4951
step: z.optional(z.number().min(1).int()).describe('Step number (default: 1)'),
5052
},
51-
({ step }) => {
52-
const content = getComponentCreationInstruction(step);
53+
async ({ cwd, filePath, step }) => {
54+
const content = await getTaskStep(
55+
path.isAbsolute(filePath) ? filePath : path.join(cwd, filePath),
56+
step,
57+
);
5358
return {
5459
content: [
5560
{

src/utils/get-task-step.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { promises as fs } from 'node:fs';
2+
3+
import { splitTaskSteps } from './split-task-steps.js';
4+
5+
const cache = new Map<string, string[]>();
6+
7+
/**
8+
*
9+
* @param filePath
10+
* @param step
11+
*/
12+
export async function getTaskStep(filePath: string, step = 1) {
13+
if (cache.has(filePath)) {
14+
const steps = cache.get(filePath);
15+
if (!steps) {
16+
throw new Error('No steps found');
17+
}
18+
return getStep(steps, step);
19+
}
20+
const task = await fs.readFile(filePath, 'utf8');
21+
const steps = splitTaskSteps(task);
22+
cache.set(filePath, steps);
23+
return getStep(steps, step);
24+
}
25+
26+
/**
27+
*
28+
* @param steps
29+
* @param step
30+
*/
31+
function getStep(steps: string[], step: number) {
32+
if (steps.length < step) {
33+
return '全てのステップが完了しました。';
34+
}
35+
return steps.at(step - 1) ?? 'ステップが見つかりません。次のステップに進んでください。';
36+
}

src/utils/split-task-steps.spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { describe, it, expect } from 'vitest';
2+
3+
import { splitTaskSteps } from './split-task-steps.js';
4+
5+
describe('splitTaskSteps', () => {
6+
it('should split the task into steps', () => {
7+
const task = `
8+
# 1
9+
Step 1
10+
11+
---
12+
13+
# 2
14+
Step 2
15+
16+
---
17+
18+
# 3
19+
Step 3
20+
21+
`;
22+
const steps = splitTaskSteps(task);
23+
expect(steps).toEqual(['Step 1\n\n---', 'Step 2\n\n---', 'Step 3']);
24+
});
25+
});

src/utils/split-task-steps.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
*
3+
* @param task
4+
*/
5+
export function splitTaskSteps(task: string) {
6+
const steps = task.trim().split(/(?:^|\n\r?)(?=#\s)/);
7+
return steps.map((step) => {
8+
const lines = step
9+
.trim()
10+
.split('\n')
11+
.map((line) => line.trim())
12+
.filter((line) => !/^#\s+\d+/.test(line));
13+
return lines.join('\n');
14+
});
15+
}

0 commit comments

Comments
 (0)