feat: fully dynamic model picker in inline prompt
This commit is contained in:
@@ -5,9 +5,10 @@
|
||||
*
|
||||
* Registered in index.ts via IToolbarWidgetRegistry.addFactory('Cell', ...).
|
||||
*
|
||||
* The frontend does NO semantic processing: it gathers the cell's source /
|
||||
* outputs / error, attaches the user's freeform instruction, and POSTs to
|
||||
* the server. The server (and the LLM it forwards to) decides what to do.
|
||||
* v3-final: the frontend does NO semantic processing. It gathers the cell's
|
||||
* source / outputs / error, attaches the user's freeform instruction, and
|
||||
* POSTs to the server. The chosen provider/model comes from the inline
|
||||
* picker (no settings-based default).
|
||||
*/
|
||||
import type { CodeCell } from '@jupyterlab/cells';
|
||||
import { Notification } from '@jupyterlab/apputils';
|
||||
@@ -19,23 +20,27 @@ import { callOpenCodeEdit } from '../api/opencode_client';
|
||||
import { extractCellContext } from '../context/cell_context';
|
||||
import type {
|
||||
CellContext,
|
||||
OpenCodeProvidersResponse,
|
||||
OpenCodeRequest,
|
||||
OpenCodeResponse,
|
||||
OpenCodeSettings
|
||||
OpenCodeResponse
|
||||
} from '../types';
|
||||
|
||||
import { OpenCodeInlinePrompt } from './opencode_inline_prompt';
|
||||
|
||||
// Module-level runtime injection (set by index.ts after settings load).
|
||||
let _settings: OpenCodeSettings | null = null;
|
||||
// Module-level runtime injection (set by index.ts after activation).
|
||||
let _serverSettings: ServerConnection.ISettings | null = null;
|
||||
let _providers: OpenCodeProvidersResponse | null = null;
|
||||
|
||||
export function setOpenCodeRuntime(args: {
|
||||
settings: OpenCodeSettings;
|
||||
serverSettings: ServerConnection.ISettings;
|
||||
}): void {
|
||||
_settings = args.settings;
|
||||
_serverSettings = args.serverSettings;
|
||||
export function setOpenCodeServerSettings(
|
||||
serverSettings: ServerConnection.ISettings
|
||||
): void {
|
||||
_serverSettings = serverSettings;
|
||||
}
|
||||
|
||||
export function setOpenCodeProviders(
|
||||
p: OpenCodeProvidersResponse | null
|
||||
): void {
|
||||
_providers = p;
|
||||
}
|
||||
|
||||
type Status = 'idle' | 'loading';
|
||||
@@ -108,8 +113,9 @@ export class OpenCodeCellActions extends Widget {
|
||||
}
|
||||
const prompt = new OpenCodeInlinePrompt(this._cell, {
|
||||
disabled: !this._context || this._status === 'loading',
|
||||
onSubmit: (text: string) => {
|
||||
void this._onSubmit(text);
|
||||
providers: _providers,
|
||||
onSubmit: (text: string, providerId?: string, modelId?: string) => {
|
||||
void this._onSubmit(text, providerId, modelId);
|
||||
},
|
||||
onCancel: () => {
|
||||
this._hidePrompt();
|
||||
@@ -126,20 +132,24 @@ export class OpenCodeCellActions extends Widget {
|
||||
}
|
||||
}
|
||||
|
||||
private async _onSubmit(text: string): Promise<void> {
|
||||
private async _onSubmit(
|
||||
text: string,
|
||||
providerId?: string,
|
||||
modelId?: string
|
||||
): Promise<void> {
|
||||
if (!this._context) {
|
||||
return;
|
||||
}
|
||||
if (!_settings || !_serverSettings) {
|
||||
Notification.error('OpenCode 运行时未初始化,请检查 settings');
|
||||
if (!_serverSettings) {
|
||||
Notification.error('OpenCode 运行时未初始化');
|
||||
return;
|
||||
}
|
||||
|
||||
const request: OpenCodeRequest = {
|
||||
prompt: text,
|
||||
context: this._context,
|
||||
providerId: _settings.opencodeProvider || undefined,
|
||||
modelId: _settings.opencodeModel || undefined
|
||||
providerId,
|
||||
modelId
|
||||
};
|
||||
|
||||
this._status = 'loading';
|
||||
|
||||
@@ -1,22 +1,30 @@
|
||||
/**
|
||||
* Inline prompt widget attached to a cell's DOM when the AI button is clicked.
|
||||
* Renders a textarea + send/cancel buttons. The owning cell is passed so
|
||||
* we can resolve the cell context at submit time. submit/cancel are
|
||||
* callbacks owned by OpenCodeCellActions.
|
||||
* Renders a model selector, a textarea, and send/cancel buttons. The owning
|
||||
* cell is passed so we can resolve the cell context at submit time. submit
|
||||
* and cancel are callbacks owned by OpenCodeCellActions.
|
||||
*
|
||||
* v3-final: the model selector is fully dynamic — it lists every
|
||||
* (provider, model) pair from the providers cache and defaults to the first
|
||||
* option. No settings-based default.
|
||||
*/
|
||||
import { CodeCell } from '@jupyterlab/cells';
|
||||
import { Widget } from '@lumino/widgets';
|
||||
|
||||
import type { OpenCodeProvidersResponse } from '../types';
|
||||
|
||||
export interface IOpenCodeInlinePromptOptions {
|
||||
onSubmit: (text: string) => void;
|
||||
onSubmit: (text: string, providerId?: string, modelId?: string) => void;
|
||||
onCancel: () => void;
|
||||
disabled: boolean;
|
||||
providers: OpenCodeProvidersResponse | null;
|
||||
}
|
||||
|
||||
export class OpenCodeInlinePrompt extends Widget {
|
||||
private _textarea: HTMLTextAreaElement;
|
||||
private _sendBtn: HTMLButtonElement;
|
||||
private _cancelBtn: HTMLButtonElement;
|
||||
private _select: HTMLSelectElement | null = null;
|
||||
|
||||
constructor(
|
||||
_cell: CodeCell,
|
||||
@@ -24,6 +32,21 @@ export class OpenCodeInlinePrompt extends Widget {
|
||||
) {
|
||||
super();
|
||||
this.addClass('opencode-inline-prompt');
|
||||
|
||||
const flat = flattenProviders(options.providers);
|
||||
if (flat.length > 0) {
|
||||
this._select = document.createElement('select');
|
||||
this._select.className = 'opencode-model-select';
|
||||
for (const opt of flat) {
|
||||
const o = document.createElement('option');
|
||||
o.value = `${opt.providerId}|${opt.modelId}`;
|
||||
o.textContent = `${opt.providerId} / ${opt.modelId}`;
|
||||
this._select.appendChild(o);
|
||||
}
|
||||
// No settings-based default — pick the first available option.
|
||||
this._select.value = this._select.options[0].value;
|
||||
}
|
||||
|
||||
this._textarea = document.createElement('textarea');
|
||||
this._textarea.placeholder = '向 AI 描述你想要的修改…';
|
||||
this._textarea.rows = 3;
|
||||
@@ -33,9 +56,11 @@ export class OpenCodeInlinePrompt extends Widget {
|
||||
this._sendBtn.disabled = options.disabled;
|
||||
this._sendBtn.addEventListener('click', () => {
|
||||
const text = this._textarea.value;
|
||||
if (text.trim()) {
|
||||
options.onSubmit(text);
|
||||
if (!text.trim()) {
|
||||
return;
|
||||
}
|
||||
const [providerId, modelId] = this._readSelection();
|
||||
options.onSubmit(text, providerId, modelId);
|
||||
});
|
||||
this._cancelBtn = document.createElement('button');
|
||||
this._cancelBtn.className = 'opencode-btn-cancel';
|
||||
@@ -49,11 +74,49 @@ export class OpenCodeInlinePrompt extends Widget {
|
||||
actions.appendChild(this._sendBtn);
|
||||
actions.appendChild(this._cancelBtn);
|
||||
|
||||
if (this._select) {
|
||||
const label = document.createElement('label');
|
||||
label.className = 'opencode-model-label';
|
||||
const span = document.createElement('span');
|
||||
span.textContent = '模型:';
|
||||
label.appendChild(span);
|
||||
label.appendChild(this._select);
|
||||
this.node.appendChild(label);
|
||||
}
|
||||
this.node.appendChild(this._textarea);
|
||||
this.node.appendChild(actions);
|
||||
}
|
||||
|
||||
setDisabled(disabled: boolean): void {
|
||||
this._sendBtn.disabled = disabled;
|
||||
if (this._select) {
|
||||
this._select.disabled = disabled;
|
||||
}
|
||||
}
|
||||
|
||||
private _readSelection(): [string | undefined, string | undefined] {
|
||||
if (!this._select) {
|
||||
return [undefined, undefined];
|
||||
}
|
||||
const parts = this._select.value.split('|', 2);
|
||||
if (parts.length !== 2) {
|
||||
return [undefined, undefined];
|
||||
}
|
||||
return [parts[0], parts[1]];
|
||||
}
|
||||
}
|
||||
|
||||
function flattenProviders(
|
||||
providers: OpenCodeProvidersResponse | null
|
||||
): { providerId: string; modelId: string }[] {
|
||||
if (!providers || !providers.providers) {
|
||||
return [];
|
||||
}
|
||||
const out: { providerId: string; modelId: string }[] = [];
|
||||
for (const p of providers.providers) {
|
||||
for (const m of p.models) {
|
||||
out.push({ providerId: p.id, modelId: m.id });
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user