@@ -206,27 +206,63 @@ <h3 class="text-sm font-black text-black mb-2 flex items-center gap-1">
206206 createNewContextFromScratch ( ) ;
207207}
208208
209- // Create a new context and apply a template to it
210- function createNewContextAndApplyTemplate ( templateName ) {
209+
210+ // Create a new empty context from scratch
211+ function createNewContextFromScratch ( ) {
212+ if ( ! window . workspaceManager ) {
213+ console . error ( 'WorkspaceManager not available' ) ;
214+ return ;
215+ }
216+
217+ // Generate unique context ID
218+ const timestamp = Date . now ( ) ;
219+ const contextId = `scratch-${ timestamp } ` ;
220+ const contextName = `From Scratch - ${ new Date ( ) . toLocaleString ( ) } ` ;
221+
222+ // Create new context
223+ if ( workspaceManager . createContext ( contextId , contextName ) ) {
224+ // Switch to new context (it will be empty by default)
225+ workspaceManager . switchContext ( contextId ) ;
226+
227+ // Show workspace
228+ showWorkspace ( ) ;
229+ } else {
230+ console . error ( 'Failed to create new context' ) ;
231+ }
232+ }
233+
234+ // Action functions that use backend data
235+ function addAgent ( name ) {
236+ createNewContextAndInstallAgent ( name ) ;
237+ }
238+
239+ function addRule ( name ) {
240+ createNewContextAndInsertRule ( name ) ;
241+ }
242+
243+ function addMCP ( name ) {
244+ createNewContextAndInstallMCP ( name ) ;
245+ }
246+
247+ // New context creation functions that use the same API as workspace
248+ function createNewContextAndInstallAgent ( agentName ) {
211249 if ( ! window . workspaceManager ) {
212250 console . error ( 'WorkspaceManager not available' ) ;
213251 return ;
214252 }
215253
216254 // Generate unique context ID
217255 const timestamp = Date . now ( ) ;
218- const contextId = `context -${ timestamp } ` ;
219- const contextName = `${ templateName } - ${ new Date ( ) . toLocaleString ( ) } ` ;
256+ const contextId = `agent -${ timestamp } ` ;
257+ const contextName = `${ agentName } - ${ new Date ( ) . toLocaleString ( ) } ` ;
220258
221259 // Create new context
222260 if ( workspaceManager . createContext ( contextId , contextName ) ) {
223261 // Switch to new context
224262 workspaceManager . switchContext ( contextId ) ;
225263
226- // Apply template to new context
227- if ( window . includeTemplate ) {
228- window . includeTemplate ( templateName ) ;
229- }
264+ // Install agent using the same function as workspace sidebar
265+ installAgentInContext ( agentName ) ;
230266
231267 // Show workspace
232268 showWorkspace ( ) ;
@@ -235,41 +271,134 @@ <h3 class="text-sm font-black text-black mb-2 flex items-center gap-1">
235271 }
236272}
237273
238- // Create a new empty context from scratch
239- function createNewContextFromScratch ( ) {
274+ function createNewContextAndInstallMCP ( mcpName ) {
240275 if ( ! window . workspaceManager ) {
241276 console . error ( 'WorkspaceManager not available' ) ;
242277 return ;
243278 }
244279
245280 // Generate unique context ID
246281 const timestamp = Date . now ( ) ;
247- const contextId = `scratch -${ timestamp } ` ;
248- const contextName = `From Scratch - ${ new Date ( ) . toLocaleString ( ) } ` ;
282+ const contextId = `mcp -${ timestamp } ` ;
283+ const contextName = `${ mcpName } - ${ new Date ( ) . toLocaleString ( ) } ` ;
249284
250285 // Create new context
251286 if ( workspaceManager . createContext ( contextId , contextName ) ) {
252- // Switch to new context (it will be empty by default)
287+ // Switch to new context
253288 workspaceManager . switchContext ( contextId ) ;
254289
290+ // Install MCP using the same function as workspace sidebar
291+ installMCPInContext ( mcpName ) ;
292+
255293 // Show workspace
256294 showWorkspace ( ) ;
257295 } else {
258296 console . error ( 'Failed to create new context' ) ;
259297 }
260298}
261299
262- // Action functions that use backend data
263- function addAgent ( name ) {
264- createNewContextAndApplyTemplate ( name ) ;
300+ function createNewContextAndInsertRule ( ruleName ) {
301+ if ( ! window . workspaceManager ) {
302+ console . error ( 'WorkspaceManager not available' ) ;
303+ return ;
304+ }
305+
306+ // Generate unique context ID
307+ const timestamp = Date . now ( ) ;
308+ const contextId = `rule-${ timestamp } ` ;
309+ const contextName = `${ ruleName } - ${ new Date ( ) . toLocaleString ( ) } ` ;
310+
311+ // Create new context
312+ if ( workspaceManager . createContext ( contextId , contextName ) ) {
313+ // Switch to new context
314+ workspaceManager . switchContext ( contextId ) ;
315+
316+ // Insert rule text directly into editor
317+ insertRuleInContext ( ruleName ) ;
318+
319+ // Show workspace
320+ showWorkspace ( ) ;
321+ } else {
322+ console . error ( 'Failed to create new context' ) ;
323+ }
265324}
266325
267- function addRule ( name ) {
268- createNewContextAndApplyTemplate ( name ) ;
326+ // Helper functions that use the same API as workspace sidebar
327+ async function installAgentInContext ( agentName ) {
328+ try {
329+ const response = await fetch ( `/api/actions/agent-content/${ encodeURIComponent ( agentName ) } ` ) ;
330+
331+ if ( response . ok ) {
332+ const result = await response . json ( ) ;
333+
334+ // Add to virtual workspace
335+ if ( window . workspaceManager && window . workspaceManager . currentState ) {
336+ window . workspaceManager . currentState . addFile ( result . path , result . content ) ;
337+ window . workspaceManager . saveState ( window . workspaceManager . currentContextId ) ;
338+ window . workspaceManager . render ( ) ;
339+ }
340+ } else {
341+ const error = await response . json ( ) ;
342+ console . error ( `Error: ${ error . detail } ` ) ;
343+ }
344+ } catch ( error ) {
345+ console . error ( `Error installing agent: ${ error . message } ` ) ;
346+ }
269347}
270348
271- function addMCP ( name ) {
272- createNewContextAndApplyTemplate ( name + ' MCP' ) ;
349+ async function installMCPInContext ( mcpName ) {
350+ try {
351+ // Get current .mcp.json content from workspace
352+ let currentConfig = { } ;
353+ if ( window . workspaceManager && window . workspaceManager . getState ( ) ) {
354+ const mcpFile = window . workspaceManager . getState ( ) . files [ '.mcp.json' ] ;
355+ if ( mcpFile ) {
356+ try {
357+ currentConfig = JSON . parse ( mcpFile ) ;
358+ } catch ( e ) {
359+ currentConfig = { } ;
360+ }
361+ }
362+ }
363+
364+ const response = await fetch ( `/api/actions/mcp-config/${ encodeURIComponent ( mcpName ) } ` , {
365+ method : 'POST' ,
366+ headers : {
367+ 'Content-Type' : 'application/json'
368+ } ,
369+ body : JSON . stringify ( currentConfig )
370+ } ) ;
371+
372+ if ( response . ok ) {
373+ const result = await response . json ( ) ;
374+
375+ // Add to virtual workspace
376+ if ( window . workspaceManager && window . workspaceManager . currentState ) {
377+ window . workspaceManager . currentState . addFile ( result . path , result . content ) ;
378+ window . workspaceManager . saveState ( window . workspaceManager . currentContextId ) ;
379+ window . workspaceManager . render ( ) ;
380+ }
381+ } else {
382+ const error = await response . json ( ) ;
383+ console . error ( `Error: ${ error . detail } ` ) ;
384+ }
385+ } catch ( error ) {
386+ console . error ( `Error installing MCP: ${ error . message } ` ) ;
387+ }
388+ }
389+
390+ function insertRuleInContext ( ruleName ) {
391+ // For rules, we just insert the text directly into the editor
392+ // This matches the workspace sidebar behavior for rules
393+ setTimeout ( ( ) => {
394+ if ( window . insertTextAtCursor ) {
395+ window . insertTextAtCursor ( ruleName ) ;
396+ } else if ( window . workspaceMonacoEditor ) {
397+ // Fallback: set editor value directly
398+ window . workspaceMonacoEditor . setValue ( ruleName ) ;
399+ window . workspaceMonacoEditor . focus ( ) ;
400+ }
401+ } , 100 ) ;
273402}
274403
275404// Show workspace with fade transition
0 commit comments