Skip to content

Commit f9dfe04

Browse files
committed
added mermaid pr suggestion
1 parent a94f4a6 commit f9dfe04

4 files changed

Lines changed: 75 additions & 0 deletions

File tree

packages/sdk/src/index.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,42 @@ describe('MermaidChart', () => {
171171
).rejects.toThrow(AICreditsLimitExceededError);
172172
});
173173
});
174+
175+
describe('#mermaidPrSuggestion', () => {
176+
beforeEach(async () => {
177+
await client.setAccessToken('test-access-token');
178+
});
179+
180+
it('should return title and description from diagram diff', async () => {
181+
vi.spyOn(client, 'mermaidPrSuggestion').mockResolvedValue({
182+
title: 'Add validation step to flowchart',
183+
description: '## What changed\n- Added node C',
184+
});
185+
186+
const result = await client.mermaidPrSuggestion({
187+
originalDiagram: 'flowchart TD\n A --> B',
188+
editedDiagram: 'flowchart TD\n A --> B\n B --> C[Validate]',
189+
});
190+
191+
expect(result.title).toContain('validation');
192+
expect(result.description).toContain('What changed');
193+
});
194+
195+
it('should throw AICreditsLimitExceededError on 402', async () => {
196+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
197+
vi.spyOn((client as any).axios, 'post').mockRejectedValue({
198+
response: {
199+
status: 402,
200+
data: 'AI credits limit exceeded',
201+
},
202+
});
203+
204+
await expect(
205+
client.mermaidPrSuggestion({
206+
originalDiagram: 'flowchart TD\n A --> B',
207+
editedDiagram: 'flowchart TD\n A --> B\n B --> C',
208+
}),
209+
).rejects.toThrow(AICreditsLimitExceededError);
210+
});
211+
});
174212
});

packages/sdk/src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import type {
1919
MCUser,
2020
RepairDiagramRequest,
2121
RepairDiagramResponse,
22+
MermaidPrSuggestionRequest,
23+
MermaidPrSuggestionResponse,
2224
AICreditsUsage,
2325
} from './types.js';
2426
import { URLS } from './urls.js';
@@ -330,6 +332,24 @@ export class MermaidChart {
330332
}
331333
}
332334

335+
/**
336+
* Suggests a pull request title and body (markdown) from a before/after diagram diff (Mermaid AI).
337+
*
338+
* @param request - `originalDiagram` and `editedDiagram` only
339+
* @throws {@link AICreditsLimitExceededError} if credits limit exceeded (HTTP 402)
340+
*/
341+
public async mermaidPrSuggestion(request: MermaidPrSuggestionRequest): Promise<MermaidPrSuggestionResponse> {
342+
try {
343+
const response = await this.axios.post<MermaidPrSuggestionResponse>(
344+
URLS.rest.openai.prSummary,
345+
request,
346+
);
347+
return response.data;
348+
} catch (error: unknown) {
349+
throwIfAICreditsExceeded(error);
350+
}
351+
}
352+
333353
/**
334354
* Chat with Mermaid AI about a diagram.
335355
*

packages/sdk/src/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,22 @@ export interface RepairDiagramRequest {
9696
userID?: string;
9797
}
9898

99+
/**
100+
* Only the two diagram versions are sent; the server derives user plan and usage from the session.
101+
*/
102+
export interface MermaidPrSuggestionRequest {
103+
originalDiagram: string;
104+
editedDiagram: string;
105+
}
106+
107+
/**
108+
* Public response: suggested PR title and markdown description
109+
*/
110+
export interface MermaidPrSuggestionResponse {
111+
title: string;
112+
description: string;
113+
}
114+
99115
/**
100116
* Request parameters for chatting with the Mermaid AI about a diagram.
101117
*/

packages/sdk/src/urls.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export const URLS = {
4141
},
4242
openai: {
4343
repair: `/rest-api/openai/repair`,
44+
prSummary: `/rest-api/openai/pr-summary`,
4445
chat: `/rest-api/openai/chat`,
4546
},
4647
},

0 commit comments

Comments
 (0)