How briansunter/logseq-plugin-gpt3-openai works
Main Components
• Plugin UI Layer – This is represented in the “src/ui” directory. It includes several UI components (e.g., CommandButton, CommandOption, CommandToolbar, and various result display components) that allow users to interact with and trigger commands like “gpt”, “gpt-page”, “gpt-block”, “whisper” and “dalle”.
• Plugin Core / Logic Layer – Located in “src/lib”, this layer includes modules such as logseq.ts (integration with Logseq APIs), openai.ts (communication with the OpenAI API), prompts.ts (handling built-in prompt templates), rawCommands.ts (command processing), and settings.ts (configuration and user settings).
• Prompt Template/Configuration Layer – The “src/prompts” directory holds prompt templates that are used to generate the appropriate text responses. These are engaged when users create custom or built-in prompt templates.
• Build & Deployment Infrastructure – Files like “package.json”, “vite.config.ts”, “pnpm-lock.yaml”, and configuration files (Tailwind, PostCSS, etc.) indicate the build pipeline and deployment setup using Vite and semantic-release for deployment.
Relationships and Interactions
• User Actions and UI:
– The user interacts with the Logseq interface and invokes commands (via keystroke shortcuts or menu selections).
– This invokes one or more UI components (in “src/ui”) such as the gpt popup, command buttons, or command results display.
• UI and Plugin Logic:
– The UI layer calls functions from the core logic (in “src/lib”). For example, when a command is invoked, the corresponding logic in rawCommands.ts or the API handling in openai.ts is triggered.
– Settings in settings.ts influence how data is sent to the OpenAI API, such as specifying the model (gpt-3.5-turbo, text-davinci-003, or gpt-4) and custom parameters like a chat prompt policy.
• Communication with External Services (OpenAI):
– The openai.ts module manages API requests to OpenAI (and possibly OpenAI’s DALL-E and Whisper endpoints) and formats responses to send back to the UI.
– Data flow is bi-directional: the UI sends the prompt to the core logic, which then uses openai.ts to retrieve responses, and the responses are then rendered on the UI.
• Template and Prompt Management:
– The “src/prompts” directory provides the built-in templates, and users can define their custom templates with a prompt-template:: property in Logseq, which is processed via the prompts.ts module.
Architectural Patterns and Design Principles
• Separation of Concerns: The system cleanly separates the UI from business logic (API interaction, prompt management, and command processing).
• Modular Design: Each directory (ui, lib, prompts) encapsulates specific aspects of functionality, easing testing and maintenance.
• Event-Driven Interactions: User events (such as invoking a GPT command) drive the interaction chain, from the UI component to the API call.
• Integration with External APIs: Standardized communication via modules ensures that the plugin’s external dependencies (OpenAI and Logseq) are abstracted and cleanly managed.