import { JupyterFrontEnd, JupyterFrontEndPlugin } from '@jupyterlab/application'; import { INotebookTracker } from '@jupyterlab/notebook'; import { ISettingRegistry } from '@jupyterlab/settingregistry'; import { installOpenCodeEverywhere } from './components/opencode_installer'; import { setOpenCodeRuntime } from './components/opencode_cell_footer'; import { requestAPI } from './request'; import { readOpenCodeSettings } from './types'; import { callOpenCodeProviders } from './api/opencode_client'; /** * Initialization data for the opencode_bridge extension. */ const plugin: JupyterFrontEndPlugin = { id: 'opencode_bridge:plugin', description: 'A JupyterLab extension bridging the UI to OpenCode Serve.', autoStart: true, optional: [ISettingRegistry], requires: [INotebookTracker], activate: ( app: JupyterFrontEnd, tracker: INotebookTracker, settingRegistry: ISettingRegistry | null ) => { console.log('JupyterLab extension opencode_bridge is activated!'); // Install the per-cell footer factory on every notebook. installOpenCodeEverywhere(tracker); // Load settings + push into toolbar module. if (settingRegistry) { void settingRegistry .load(plugin.id) .then(settings => { const bridge = readOpenCodeSettings(settings.composite); setOpenCodeRuntime({ settings: bridge, serverSettings: app.serviceManager.serverSettings }); console.log('opencode_bridge settings loaded:', bridge); void callOpenCodeProviders(app.serviceManager.serverSettings) .then(data => { const lines: string[] = ['[opencode_bridge] Available OpenCode providers:']; for (const p of data.providers) { const models = p.models.map(m => m.id).join(', '); lines.push(` - ${p.id}: ${models || '(no models)'}`); } // eslint-disable-next-line no-console console.log(lines.join('\n')); }) .catch(reason => { // eslint-disable-next-line no-console console.warn( '[opencode_bridge] Could not fetch providers (is the opencode-bridge server extension enabled and opencode serve running?):', reason ); }); }) .catch(reason => { console.error('Failed to load settings for opencode_bridge.', reason); }); } requestAPI('hello', app.serviceManager.serverSettings) .then(data => { console.log('hello endpoint:', data); }) .catch(reason => { console.error( `The opencode_bridge server extension appears to be missing.\n${reason}` ); }); } }; export default plugin;