-
Notifications
You must be signed in to change notification settings - Fork 896
fix(desktop): auto-name workspaces after setup completes #2107
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f505509
fix(desktop): auto-name workspaces after setup completes
Kitenite 3afcf15
Merge origin/main into kitenite/serious-saturn
Kitenite 759324e
refactor(desktop): remove workspace auto-name debug noise
Kitenite 20771f4
Merge remote-tracking branch 'origin' into kitenite/serious-saturn
Kitenite 62f0fa1
refactor(desktop): drop redundant workspace naming paths
Kitenite 73ba31b
Handle workspace auto-name review feedback
Kitenite a14d4cf
Merge remote-tracking branch 'origin' into kitenite/serious-saturn
Kitenite a939b6e
Fix auth
Kitenite fa39f69
Refactor shared title generation
Kitenite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,71 +1,86 @@ | ||
| import { createAnthropic } from "@ai-sdk/anthropic"; | ||
| import { createOpenAI } from "@ai-sdk/openai"; | ||
| import { Agent } from "@mastra/core/agent"; | ||
| import type { Agent } from "@mastra/core/agent"; | ||
| import { | ||
| generateTitleFromMessage, | ||
| getCredentialsFromAnySource as getAnthropicCredentialsFromAnySource, | ||
| getAnthropicProviderOptions, | ||
| getOpenAICredentialsFromAnySource, | ||
| } from "@superset/chat/host"; | ||
| import { workspaces } from "@superset/local-db"; | ||
| import { and, eq, isNull } from "drizzle-orm"; | ||
| import { localDb } from "main/lib/local-db"; | ||
| import { getWorkspaceAutoRenameDecision } from "./workspace-auto-rename"; | ||
|
|
||
| export type WorkspaceAutoRenameResult = | ||
| | { status: "renamed"; name: string } | ||
| | { | ||
| status: "skipped"; | ||
| reason: | ||
| | "empty-prompt" | ||
| | "missing-credentials" | ||
| | "generation-failed" | ||
| | "missing-workspace" | ||
| | "empty-generated-name" | ||
| | "workspace-deleting" | ||
| | "workspace-named" | ||
| | "workspace-name-changed"; | ||
| warning?: string; | ||
| }; | ||
|
|
||
| type AgentModel = ConstructorParameters<typeof Agent>[0]["model"]; | ||
| type AnthropicCredentials = NonNullable< | ||
| ReturnType<typeof getAnthropicCredentialsFromAnySource> | ||
| >; | ||
| type OpenAICredentials = NonNullable< | ||
| ReturnType<typeof getOpenAICredentialsFromAnySource> | ||
| >; | ||
|
|
||
| interface TitleProvider { | ||
| name: "Anthropic" | "OpenAI"; | ||
| agentId: string; | ||
| resolveApiKey: () => string | null; | ||
| createModel: (apiKey: string) => AgentModel; | ||
| resolveCredentials: () => AnthropicCredentials | OpenAICredentials | null; | ||
| createModel: ( | ||
| credentials: AnthropicCredentials | OpenAICredentials, | ||
| ) => AgentModel; | ||
| } | ||
|
|
||
| const TITLE_PROVIDERS: TitleProvider[] = [ | ||
| { | ||
| name: "Anthropic", | ||
| agentId: "workspace-namer-anthropic", | ||
| resolveApiKey: () => getAnthropicCredentialsFromAnySource()?.apiKey ?? null, | ||
| createModel: (apiKey) => | ||
| createAnthropic({ apiKey })("claude-haiku-4-5-20251001"), | ||
| resolveCredentials: () => getAnthropicCredentialsFromAnySource(), | ||
| createModel: (credentials) => | ||
| createAnthropic(getAnthropicProviderOptions(credentials))( | ||
| "claude-haiku-4-5-20251001", | ||
| ), | ||
| }, | ||
| { | ||
| name: "OpenAI", | ||
| agentId: "workspace-namer-openai", | ||
| resolveApiKey: () => getOpenAICredentialsFromAnySource()?.apiKey ?? null, | ||
| createModel: (apiKey) => createOpenAI({ apiKey })("gpt-4o-mini"), | ||
| resolveCredentials: () => getOpenAICredentialsFromAnySource(), | ||
| createModel: (credentials) => | ||
| createOpenAI({ apiKey: credentials.apiKey })("gpt-4o-mini"), | ||
| }, | ||
| ]; | ||
|
|
||
| async function generateTitleWithModel( | ||
| prompt: string, | ||
| agentId: string, | ||
| model: AgentModel, | ||
| ): Promise<string | null> { | ||
| const agent = new Agent({ | ||
| id: agentId, | ||
| name: "Workspace Namer", | ||
| instructions: "You generate concise workspace titles.", | ||
| model, | ||
| }); | ||
|
|
||
| const title = await agent.generateTitleFromUserMessage({ | ||
| message: prompt, | ||
| tracingContext: {}, | ||
| }); | ||
|
|
||
| return title?.trim() || null; | ||
| } | ||
|
|
||
| export async function generateWorkspaceNameFromPrompt( | ||
| prompt: string, | ||
| ): Promise<string | null> { | ||
| for (const provider of TITLE_PROVIDERS) { | ||
| const apiKey = provider.resolveApiKey(); | ||
| if (!apiKey) { | ||
| const credentials = provider.resolveCredentials(); | ||
| if (!credentials) { | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| const title = await generateTitleWithModel( | ||
| prompt, | ||
| provider.agentId, | ||
| provider.createModel(apiKey), | ||
| ); | ||
| const title = await generateTitleFromMessage({ | ||
| message: prompt, | ||
| agentModel: provider.createModel(credentials), | ||
| agentId: provider.agentId, | ||
| agentName: "Workspace Namer", | ||
| instructions: "You generate concise workspace titles.", | ||
| }); | ||
| if (title) { | ||
| return title; | ||
| } | ||
|
|
@@ -79,3 +94,97 @@ export async function generateWorkspaceNameFromPrompt( | |
|
|
||
| return null; | ||
| } | ||
|
|
||
| export async function attemptWorkspaceAutoRenameFromPrompt({ | ||
| workspaceId, | ||
| prompt, | ||
| }: { | ||
| workspaceId: string; | ||
| prompt?: string | null; | ||
| }): Promise<WorkspaceAutoRenameResult> { | ||
| const cleanedPrompt = prompt?.trim(); | ||
| if (!cleanedPrompt) { | ||
| return { status: "skipped", reason: "empty-prompt" }; | ||
| } | ||
|
|
||
| const generatedName = await generateWorkspaceNameFromPrompt(cleanedPrompt); | ||
| if (!generatedName) { | ||
| const hasCredentials = | ||
| getAnthropicCredentialsFromAnySource() !== null || | ||
| getOpenAICredentialsFromAnySource() !== null; | ||
| return { | ||
| status: "skipped", | ||
| reason: hasCredentials ? "generation-failed" : "missing-credentials", | ||
| warning: hasCredentials | ||
| ? "Couldn't auto-name this workspace." | ||
| : "Couldn't auto-name this workspace because chat credentials aren't configured.", | ||
| }; | ||
| } | ||
|
|
||
| const workspace = localDb | ||
| .select({ | ||
| id: workspaces.id, | ||
| branch: workspaces.branch, | ||
| name: workspaces.name, | ||
| isUnnamed: workspaces.isUnnamed, | ||
| deletingAt: workspaces.deletingAt, | ||
| }) | ||
| .from(workspaces) | ||
| .where(eq(workspaces.id, workspaceId)) | ||
| .get(); | ||
|
|
||
| const decision = getWorkspaceAutoRenameDecision({ | ||
| workspace: workspace ?? null, | ||
| generatedName, | ||
| }); | ||
| if (decision.kind === "skip") { | ||
| return { status: "skipped", reason: decision.reason }; | ||
| } | ||
| if (!workspace) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P3: This Prompt for AI agents |
||
| return { status: "skipped", reason: "missing-workspace" }; | ||
| } | ||
|
|
||
| const renameResult = localDb | ||
| .update(workspaces) | ||
| .set({ | ||
| name: decision.name, | ||
| isUnnamed: false, | ||
| updatedAt: Date.now(), | ||
| }) | ||
| .where( | ||
| and( | ||
| eq(workspaces.id, workspace.id), | ||
| eq(workspaces.branch, workspace.branch), | ||
| eq(workspaces.name, workspace.branch), | ||
| eq(workspaces.isUnnamed, true), | ||
| isNull(workspaces.deletingAt), | ||
| ), | ||
| ) | ||
| .run(); | ||
|
Kitenite marked this conversation as resolved.
|
||
| if (renameResult.changes > 0) { | ||
| return { status: "renamed", name: decision.name }; | ||
| } | ||
|
|
||
| const latestWorkspace = localDb | ||
| .select({ | ||
| branch: workspaces.branch, | ||
| name: workspaces.name, | ||
| isUnnamed: workspaces.isUnnamed, | ||
| deletingAt: workspaces.deletingAt, | ||
| }) | ||
| .from(workspaces) | ||
| .where(eq(workspaces.id, workspace.id)) | ||
| .get(); | ||
|
|
||
| const latestDecision = getWorkspaceAutoRenameDecision({ | ||
| workspace: latestWorkspace ?? null, | ||
| generatedName, | ||
| }); | ||
| return { | ||
| status: "skipped", | ||
| reason: | ||
| latestDecision.kind === "skip" | ||
| ? latestDecision.reason | ||
| : "workspace-name-changed", | ||
| }; | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
apps/desktop/src/lib/trpc/routers/workspaces/utils/workspace-auto-rename.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import { describe, expect, test } from "bun:test"; | ||
| import { | ||
| getWorkspaceAutoRenameDecision, | ||
| resolveWorkspaceAutoRename, | ||
| } from "./workspace-auto-rename"; | ||
|
|
||
| describe("resolveWorkspaceAutoRename", () => { | ||
| test("returns generated name for untouched unnamed workspace", () => { | ||
| expect( | ||
| resolveWorkspaceAutoRename({ | ||
| workspace: { | ||
| branch: "feat/test-branch", | ||
| name: "feat/test-branch", | ||
| isUnnamed: true, | ||
| deletingAt: null, | ||
| }, | ||
| generatedName: "Fix auth flow", | ||
| }), | ||
| ).toBe("Fix auth flow"); | ||
| }); | ||
|
|
||
| test("does not overwrite an already named workspace", () => { | ||
| expect( | ||
| resolveWorkspaceAutoRename({ | ||
| workspace: { | ||
| branch: "feat/test-branch", | ||
| name: "Custom name", | ||
| isUnnamed: false, | ||
| deletingAt: null, | ||
| }, | ||
| generatedName: "Fix auth flow", | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| test("does not overwrite placeholder once another name has been applied", () => { | ||
| expect( | ||
| resolveWorkspaceAutoRename({ | ||
| workspace: { | ||
| branch: "feat/test-branch", | ||
| name: "Running setup", | ||
| isUnnamed: true, | ||
| deletingAt: null, | ||
| }, | ||
| generatedName: "Fix auth flow", | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| test("ignores empty generated names", () => { | ||
| expect( | ||
| resolveWorkspaceAutoRename({ | ||
| workspace: { | ||
| branch: "feat/test-branch", | ||
| name: "feat/test-branch", | ||
| isUnnamed: true, | ||
| deletingAt: null, | ||
| }, | ||
| generatedName: " ", | ||
| }), | ||
| ).toBeNull(); | ||
| }); | ||
|
|
||
| test("reports skip reason for already named workspace", () => { | ||
| expect( | ||
| getWorkspaceAutoRenameDecision({ | ||
| workspace: { | ||
| branch: "feat/test-branch", | ||
| name: "Custom name", | ||
| isUnnamed: false, | ||
| deletingAt: null, | ||
| }, | ||
| generatedName: "Fix auth flow", | ||
| }), | ||
| ).toEqual({ | ||
| kind: "skip", | ||
| reason: "workspace-named", | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Short-circuit skip cases before generating a title.
attemptWorkspaceAutoRenameFromPrompt()calls the provider before loading the workspace. If the workspace is already missing, deleting, or no longer eligible for rename, this still burns a provider call and can returnmissing-credentials/generation-failedinstead of the real skip reason. Load the workspace first, return the non-empty-generated-nameskip cases early, then keep the guardedUPDATEas the post-generation race check.🤖 Prompt for AI Agents