feat: cài đặt tính năng AI cho để prompt

This commit is contained in:
2026-07-21 19:48:35 +07:00
parent 9de9ae965f
commit bbc42c630e
6 changed files with 853 additions and 12 deletions
+55 -1
View File
@@ -129,6 +129,32 @@ const AIGateway = (function() {
];
}
function buildAIPromptContext(dawState) {
const tracks = (dawState.tracks || []).map(t => ({
id: t.id,
name: t.name,
type: t.buffer ? 'audio' : 'empty',
hasBuffer: !!t.buffer,
clipsCount: t.clips ? t.clips.length : (t.buffer ? 1 : 0),
muted: t.muted,
solo: t.solo,
volumeDb: t.volumeDb ?? 0,
pan: t.pan ?? 0
}));
return {
tempo: dawState.bpm || 120,
timeSignature: '4/4',
selectedTrackId: dawState.selectedTrackId || null,
playheadPosition: dawState.currentTime || 0,
selection: (dawState.selLeft !== null && dawState.selRight !== null && dawState.selRight > dawState.selLeft) ? {
start: parseFloat(dawState.selLeft.toFixed(3)),
end: parseFloat(dawState.selRight.toFixed(3)),
length: parseFloat((dawState.selRight - dawState.selLeft).toFixed(3))
} : null,
tracks
};
}
async function executePrompt({ prompt, provider, model, apiKey, baseUrl, dawContext, tools }) {
const messages = buildUserMessage(prompt, dawContext);
const toolList = tools || DEFAULT_TOOLS;
@@ -155,12 +181,40 @@ const AIGateway = (function() {
};
}
async function createMidiItem(args) {
return fetch('/api/audio_editor', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'add_midi', ...args })
}).then(r => r.json());
}
async function modifyMidiNotes(args) {
return fetch('/api/audio_editor', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'modify_midi_notes', ...args })
}).then(r => r.json());
}
async function processAIDSP(args) {
return fetch('/api/ai_dsp_engine', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ action: 'process_ai_dsp', ...args })
}).then(r => r.json());
}
return {
DEFAULT_TOOLS,
callLLM,
extractFunctionCalls,
buildUserMessage,
executePrompt
buildAIPromptContext,
executePrompt,
createMidiItem,
modifyMidiNotes,
processAIDSP
};
})();
+27 -3
View File
@@ -39,7 +39,30 @@ const DAWCommandDispatcher = (function() {
if (!registry[name]) {
return { success: false, error: `Unknown command: ${name}` };
}
return registry[name](args);
const result = registry[name](args);
pushHistory({ name, args, result, timestamp: Date.now() });
return result;
}
function getHistory() { return history; }
function getHistoryIndex() { return historyIndex; }
function registerDAWCommands(api) {
register('CREATE_TRACK', (args) => api.createTrack(args));
register('DELETE_TRACK', (args) => api.deleteTrack(args));
register('ADD_CLIP', (args) => api.addClip(args));
register('REMOVE_CLIP', (args) => api.removeClip(args));
register('SET_TRACK_VOLUME', (args) => api.setTrackVolume(args));
register('SET_TRACK_PAN', (args) => api.setTrackPan(args));
register('TOGGLE_MUTE', (args) => api.toggleMute(args));
register('TOGGLE_SOLO', (args) => api.toggleSolo(args));
register('PROCESS_AUDIO_DSP', (args) => api.processAudioDsp(args));
register('SET_BPM', (args) => api.setBpm(args));
register('SET_PLAYHEAD', (args) => api.setPlayhead(args));
register('ADD_MARKER', (args) => api.addMarker(args));
register('CREATE_MIDI_ITEM', (args) => AIGateway.createMidiItem(args));
register('MODIFY_MIDI_NOTES', (args) => AIGateway.modifyMidiNotes(args));
register('PROCESS_AI_DSP', (args) => AIGateway.processAIDSP(args));
}
return {
@@ -50,8 +73,9 @@ const DAWCommandDispatcher = (function() {
canUndo,
canRedo,
pushHistory,
get history() { return history; },
get historyIndex() { return historyIndex; }
getHistory,
getHistoryIndex,
registerDAWCommands
};
})();