feat: add AI log analysis component and integrate into deployment views#4183
feat: add AI log analysis component and integrate into deployment views#4183Siumauricio merged 6 commits intocanaryfrom
Conversation
- Introduced the AnalyzeLogs component for analyzing logs using AI, allowing users to select AI providers and view analysis results. - Integrated AnalyzeLogs into the ShowDeployment and DockerLogsId components, enabling log analysis for both build and runtime contexts. - Updated the AI router to include a new endpoint for log analysis, which processes logs and returns structured insights. - Enhanced the AI provider selection logic to support new providers, including Z.AI and MiniMax. This feature enhances the user experience by providing actionable insights from logs, improving troubleshooting and operational efficiency.
| .mutation(async ({ input }) => { | ||
| try { |
There was a problem hiding this comment.
Missing organization ownership check on
analyzeLogs
The mutation fetches aiSettings by aiId without verifying that it belongs to the calling user's organization. Any authenticated user can pass an arbitrary aiId from a different organization and the handler will use that org's stored API key to run the analysis, burning their AI credits without consent.
Compare getEnabledProviders, which correctly scopes to ctx.session.activeOrganizationId. The same guard is missing here:
.mutation(async ({ input, ctx }) => {
const aiSettings = await getAiSettingById(input.aiId);
if (aiSettings.organizationId !== ctx.session.activeOrganizationId) {
throw new TRPCError({ code: "FORBIDDEN", message: "Access denied" });
}
...
})| .input( | ||
| z.object({ | ||
| aiId: z.string().min(1), | ||
| logs: z.string().min(1), | ||
| context: z.enum(["build", "runtime"]), | ||
| }), |
There was a problem hiding this comment.
No upper bound on
logs payload size
logs: z.string().min(1) places no upper limit. A client could send megabytes of text, inflating AI API costs and potentially hitting provider rate limits. Consider capping it to match the 200-line client-side truncation — something like z.string().min(1).max(50_000).
| .input( | |
| z.object({ | |
| aiId: z.string().min(1), | |
| logs: z.string().min(1), | |
| context: z.enum(["build", "runtime"]), | |
| }), | |
| logs: z.string().min(1).max(50_000), |
| { name: "DeepInfra", apiUrl: "https://api.deepinfra.com/v1/openai" }, | ||
| { name: "Ollama", apiUrl: "http://localhost:11434" }, | ||
| { name: "OpenRouter", apiUrl: "https://openrouter.ai/api/v1" }, | ||
| { name: "Z.AI", apiUrl: "https://api.z.ai/api/paas/v4/" }, |
There was a problem hiding this comment.
Trailing slash on Z.AI base URL may produce double-slash paths
https://api.z.ai/api/paas/v4/ ends with a slash. Depending on how createOpenAICompatible appends paths (e.g. /chat/completions), this can result in …/v4//chat/completions, which some servers reject. Remove the trailing slash for consistency with other providers in this list.
| { name: "Z.AI", apiUrl: "https://api.z.ai/api/paas/v4/" }, | |
| { name: "Z.AI", apiUrl: "https://api.z.ai/api/paas/v4" }, |
- Corrected the API URL for Z.AI by removing the trailing slash. - Modified the AI router mutation to include context and added access control to ensure users can only access their organization's AI settings. These changes improve the accuracy of the API integration and enhance security by enforcing organizational access restrictions.
…trieval - Implemented a new `readLogs` procedure across various routers (application, compose, libsql, mariadb, mongo, mysql, postgres, redis) to enable users to retrieve logs from containers. - Each procedure includes input validation for parameters such as `tail`, `since`, and `search`, ensuring robust access control and authorization checks. - Enhanced the `getContainerLogs` service to support fetching logs from both Docker containers and services, improving the logging capabilities of the application. This feature enhances observability and troubleshooting for users by providing direct access to container logs.
- Updated the `getContainerLogs` function to accept either an application name or container ID, improving flexibility in log retrieval. - Simplified the command execution logic by consolidating the remote and local execution paths. - Added a new parameter to directly use container IDs, streamlining the process for users. These changes enhance the usability of the logging feature, allowing for more efficient access to container logs.
…ompt - Updated the AnalyzeLogs component to display a message and button for configuring AI providers when none are available, improving user guidance. - Added a link to the settings page for easy access to AI provider configuration. - Integrated new icon for the configuration button to enhance UI clarity. These changes improve the user experience by ensuring users are informed about the need to set up AI providers for log analysis.
This feature enhances the user experience by providing actionable insights from logs, improving troubleshooting and operational efficiency.
What is this PR about?
Please describe in a short paragraph what this PR is about.
Checklist
Before submitting this PR, please make sure that:
canarybranch.Issues related (if applicable)
closes #2440 #3831
Screenshots (if applicable)