mirror of https://github.com/buster-so/buster.git
chats and shortcuts
This commit is contained in:
parent
1b3150f466
commit
fd52da943a
|
@ -9,10 +9,7 @@ import {
|
|||
import { tasks } from '@trigger.dev/sdk';
|
||||
import { handleAssetChat, handleAssetChatWithPrompt } from './services/chat-helpers';
|
||||
import { initializeChat } from './services/chat-service';
|
||||
import {
|
||||
enhanceMessageWithShortcut,
|
||||
enhanceMessageWithShortcutId,
|
||||
} from './services/shortcut-service';
|
||||
import { enhanceMessageWithShortcut } from './services/shortcut-service';
|
||||
|
||||
/**
|
||||
* Handler function for creating a new chat.
|
||||
|
@ -63,18 +60,8 @@ export async function createChatHandler(
|
|||
// Process shortcuts if present
|
||||
let enhancedPrompt = request.prompt;
|
||||
if (request.prompt) {
|
||||
// Check for shortcut ID first (explicit shortcut reference)
|
||||
if (request.shortcut_id) {
|
||||
enhancedPrompt = await enhanceMessageWithShortcutId(
|
||||
request.prompt,
|
||||
request.shortcut_id,
|
||||
user.id,
|
||||
organizationId
|
||||
);
|
||||
} else {
|
||||
// Check for shortcut pattern in message (e.g., /weekly-report)
|
||||
enhancedPrompt = await enhanceMessageWithShortcut(request.prompt, user.id, organizationId);
|
||||
}
|
||||
// Check for shortcut pattern in message (e.g., /weekly-report)
|
||||
enhancedPrompt = await enhanceMessageWithShortcut(request.prompt, user.id, organizationId);
|
||||
}
|
||||
|
||||
// Update request with enhanced prompt
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import { findShortcutByName, getShortcutById } from '@buster/database';
|
||||
import { findShortcutByName } from '@buster/database';
|
||||
import { ChatError, ChatErrorCode } from '@buster/server-shared/chats';
|
||||
|
||||
/**
|
||||
|
@ -18,7 +18,7 @@ export function parseShortcutFromMessage(message: string): {
|
|||
}
|
||||
|
||||
return {
|
||||
shortcutName: match[1],
|
||||
shortcutName: match[1] ?? null,
|
||||
additionalContext: match[2] || '',
|
||||
};
|
||||
}
|
||||
|
@ -57,38 +57,3 @@ export async function enhanceMessageWithShortcut(
|
|||
: shortcut.instructions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Enhance a message with a specific shortcut ID
|
||||
* @param message The original message (optional)
|
||||
* @param shortcutId The shortcut ID to apply
|
||||
* @param userId The user's ID
|
||||
* @param organizationId The organization's ID
|
||||
* @returns Enhanced message with shortcut instructions
|
||||
*/
|
||||
export async function enhanceMessageWithShortcutId(
|
||||
message: string | undefined,
|
||||
shortcutId: string,
|
||||
userId: string,
|
||||
organizationId: string
|
||||
): Promise<string> {
|
||||
const shortcut = await getShortcutById({ id: shortcutId });
|
||||
|
||||
if (!shortcut) {
|
||||
throw new ChatError(ChatErrorCode.INVALID_REQUEST, 'Shortcut not found', 404);
|
||||
}
|
||||
|
||||
// Check permissions: user must be in same org and either creator or shortcut is workspace-shared
|
||||
if (
|
||||
shortcut.organizationId !== organizationId ||
|
||||
(!shortcut.sharedWithWorkspace && shortcut.createdBy !== userId)
|
||||
) {
|
||||
throw new ChatError(
|
||||
ChatErrorCode.INVALID_REQUEST,
|
||||
'You do not have permission to use this shortcut',
|
||||
403
|
||||
);
|
||||
}
|
||||
|
||||
// Concatenate shortcut instructions with any additional message
|
||||
return message ? `${shortcut.instructions} ${message}` : shortcut.instructions;
|
||||
}
|
||||
|
|
|
@ -50,6 +50,12 @@ export async function createShortcutHandler(
|
|||
sharedWithWorkspace: data.sharedWithWorkspace,
|
||||
});
|
||||
|
||||
if (!shortcut) {
|
||||
throw new HTTPException(500, {
|
||||
message: 'Failed to create shortcut',
|
||||
});
|
||||
}
|
||||
|
||||
return shortcut;
|
||||
} catch (error) {
|
||||
console.error('Error in createShortcutHandler:', {
|
||||
|
|
|
@ -51,7 +51,6 @@ export const ChatCreateHandlerRequestSchema = z.object({
|
|||
message_id: z.string().optional(),
|
||||
asset_id: z.string().optional(),
|
||||
asset_type: ChatAssetTypeSchema.optional(),
|
||||
shortcut_id: z.string().uuid().optional(),
|
||||
});
|
||||
|
||||
// Cancel chat params schema
|
||||
|
|
Loading…
Reference in New Issue