Skip to content

Add structured text output for AI model context #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from

Conversation

mathijso
Copy link

@mathijso mathijso commented Mar 11, 2025

Add structured text output for AI model context

Description

This PR adds automatic structured text output for AI models when using a .txt file extension:

When a filename with a .txt extension is provided, the package automatically generates a structured Markdown representation of the ER diagram that is specifically formatted to be used as context for AI models.

Why

When working with AI models like ChatGPT or Claude, providing structured context about database relationships can significantly improve the quality of generated code. This feature allows developers to easily generate documentation about their database structure that can be fed directly to AI assistants.

Changes

  • Added automatic detection of .txt file extension to generate structured output
  • Added generateStructuredTextRepresentation method to GraphBuilder class
  • Updated README with documentation for the new functionality
  • Added tests for the new functionality

Example Output

The structured output format looks like this:

# Entity Relationship Diagram

## Entities

### User (`App\Models\User`)

#### Attributes:
- `id` (integer)
- `name` (string)
- `email` (string)
...

## Relationships

### User Relationships
- **HasMany** `posts` to Post (Local Key: `id`, Foreign Key: `user_id`)
...

@mechelon mechelon changed the base branch from master to dev May 9, 2025 07:01
Copy link
Member

@mechelon mechelon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I really like this addition! However, the tests fail locally for me:

~/Dev/packages/laravel-er-diagram-generator mathijso/master*
✨ ./vendor/bin/phpunit --filter="it_generates_text_output_file_with_text_output_option"
PHPUnit 11.5.12 by Sebastian Bergmann and contributors.

Runtime:       PHP 8.3.20
Configuration: /Users/dianascharf/Dev/packages/laravel-er-diagram-generator/phpunit.xml.dist

F                                                                   1 / 1 (100%)

Time: 00:00.062, Memory: 32.00 MB

There was 1 failure:

1) BeyondCode\ErdGenerator\Tests\GenerationTest::it_generates_text_output_file_with_text_output_option
Failed asserting that 'Found 4 models.\n
Inspecting model relations.\n
\n
 1/4 [▓▓▓▓▓▓▓░░░░░░░░░░░░░░░░░░░░░]  25%\n
 4/4 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%\n
\n
Wrote structured ER diagram to /Users/dianascharf/Dev/packages/laravel-er-diagram-generator/tests/output_test.txt\n
' [UTF-8](length: 354) contains "Wrote text diagram to /Users/dianascharf/Dev/packages/laravel-er-diagram-generator/tests/output_test.txt" [ASCII](length: 104).

/Users/dianascharf/Dev/packages/laravel-er-diagram-generator/tests/GenerationTest.php:80

I think it's because the return is too early / in the wrong order here:
https://github.com/beyondcode/laravel-er-diagram-generator/pull/120/files#diff-4d21df8d1e8f24acfaa7d0c1e109646539b394fd055ace4a9915a618d59cdd68R81

Could you please look into that? Then I'm happy to test again and merge this.

@mathijso
Copy link
Author

Thanks for the feedback and for pointing out the issue!

I've updated the command so that it now checks for the --text-output option before the .txt extension, ensuring the correct message ("Wrote text diagram to ...") is used when the option is set. I ran the test (it_generates_text_output_file_with_text_output_option) locally and it now passes as expected.

Let me know if you have any other feedback or if there's anything else I can improve!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants