Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/calm-berries-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mermaidchart/sdk': patch
---

Add a suggestPrSummary method to the SDK that generates PR details based on Mermaid diagram changes, including the branch name, title, commit message, and description.
25 changes: 25 additions & 0 deletions packages/sdk/src/index.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,28 @@ describe('repairDiagram', () => {
}
}, 60000); // 60 seconds timeout for AI repair operations
});

describe('suggestPrSummary', () => {
it('should generate PR summary from diagram differences', async () => {
const originalDiagram = `flowchart TD\n A[Start] --> B[Process]\n B --> C[End]`;
const editedDiagram = `flowchart TD\n A[Start] --> B[Process]\n B --> D[Validate]\n D --> C[End]`;

try {
const result = await client.suggestPrSummary({
originalDiagram,
editedDiagram,
});

// Verify response structure
expect(result).toHaveProperty('title');
expect(result).toHaveProperty('description');
expect(result).toHaveProperty('branchName');
expect(result).toHaveProperty('commitMessage');
} catch (error) {
if (error instanceof AICreditsLimitExceededError) {
return; // Credits exceeded is acceptable for E2E test
}
throw error;
}
}, 60000); // 60 seconds timeout for AI operations
});
49 changes: 49 additions & 0 deletions packages/sdk/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { beforeEach, describe, expect, it, vi } from 'vitest';
import { MermaidChart } from './index.js';
import { AICreditsLimitExceededError } from './errors.js';
import type { AuthorizationData } from './types.js';
import { URLS } from './urls.js';

import { OAuth2Client } from '@badgateway/oauth2-client';

Expand Down Expand Up @@ -171,4 +172,52 @@ describe('MermaidChart', () => {
).rejects.toThrow(AICreditsLimitExceededError);
});
});

describe('#suggestPrSummary', () => {
beforeEach(async () => {
await client.setAccessToken('test-access-token');
});

it('should POST to the pr-summary endpoint with the request body and return response.data', async () => {
const jsonResponse = {
title: 'Add validation step to flowchart',
description: '## What changed\n- Added node C',
branchName: 'feature/flowchart-validation',
commitMessage: 'Add validation node C',
};

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const postSpy = vi.spyOn((client as any).axios, 'post').mockResolvedValue({
data: jsonResponse,
});

const requestBody = {
originalDiagram: 'flowchart TD\n A --> B',
editedDiagram: 'flowchart TD\n A --> B\n B --> C[Validate]',
};

const result = await client.suggestPrSummary(requestBody);

expect(postSpy).toHaveBeenCalledWith(URLS.rest.openai.prSummary, requestBody);
expect(result).toEqual(jsonResponse);
});

it('should throw AICreditsLimitExceededError on 402', async () => {
// Mock the underlying axios call so the error mapping in suggestPrSummary is exercised.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
vi.spyOn((client as any).axios, 'post').mockRejectedValue({
response: {
status: 402,
data: 'AI credits limit exceeded',
},
});

await expect(
client.suggestPrSummary({
originalDiagram: 'flowchart TD\n A --> B',
editedDiagram: 'flowchart TD\n A --> B\n B --> C',
}),
).rejects.toThrow(AICreditsLimitExceededError);
});
});
});
21 changes: 21 additions & 0 deletions packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import type {
MCUser,
RepairDiagramRequest,
RepairDiagramResponse,
PrSummaryRequest,
PrSummaryResponse,
AICreditsUsage,
} from './types.js';
import { URLS } from './urls.js';
Expand Down Expand Up @@ -330,6 +332,25 @@ export class MermaidChart {
}
}

/**
* Suggests a pull request title and body (markdown) from a before/after diagram diff (Mermaid AI).
* The response also includes `branchName` and `commitMessage`.
*
* @param request - `originalDiagram` and `editedDiagram` only
* @throws {@link AICreditsLimitExceededError} if credits limit exceeded (HTTP 402)
*/
public async suggestPrSummary(request: PrSummaryRequest): Promise<PrSummaryResponse> {
try {
const response = await this.axios.post<PrSummaryResponse>(
URLS.rest.openai.prSummary,
request,
);
return response.data;
} catch (error: unknown) {
throwIfAICreditsExceeded(error);
}
}

/**
* Chat with Mermaid AI about a diagram.
*
Expand Down
18 changes: 18 additions & 0 deletions packages/sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,24 @@ export interface RepairDiagramRequest {
userID?: string;
}

/**
* Only the two diagram versions are sent; the server derives user plan and usage from the auth context provided by the access token.
*/
export interface PrSummaryRequest {
originalDiagram: string;
editedDiagram: string;
}

/**
* Public response: suggested PR title and markdown description, branch name, and commit message.
*/
export interface PrSummaryResponse {
title: string;
description: string;
branchName: string;
commitMessage: string;
}

/**
* Request parameters for chatting with the Mermaid AI about a diagram.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/sdk/src/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const URLS = {
},
openai: {
repair: `/rest-api/openai/repair`,
prSummary: `/rest-api/openai/pr-summary`,
chat: `/rest-api/openai/chat`,
},
},
Expand Down
Loading