Skip to content

Commit

Permalink
fix fallback to text box is no models returned from ollama api
Browse files Browse the repository at this point in the history
  • Loading branch information
rjmacarthy committed Apr 26, 2024
1 parent 370070c commit 5216b47
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 33 deletions.
12 changes: 9 additions & 3 deletions src/extension/providers/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import {
getTextSelection,
getTheme
} from '../utils'
import { WORKSPACE_STORAGE_KEY, EVENT_NAME, TWINNY_COMMAND_NAME } from '../../common/constants'
import {
WORKSPACE_STORAGE_KEY,
EVENT_NAME,
TWINNY_COMMAND_NAME
} from '../../common/constants'
import { ChatService } from '../chat-service'
import {
ClientMessage,
Expand All @@ -32,7 +36,7 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
constructor(
statusBar: vscode.StatusBarItem,
context: vscode.ExtensionContext,
templateDir: string,
templateDir: string
) {
this._statusBar = statusBar
this._context = context
Expand Down Expand Up @@ -162,7 +166,9 @@ export class SidebarProvider implements vscode.WebviewViewProvider {
public fetchOllamaModels = async () => {
try {
const models = await this._ollamaService?.fetchModels()
if (!models) return
if (!models?.length) {
return
}
this.view?.webview.postMessage({
type: EVENT_NAME.twinnyFetchOllamaModels,
value: {
Expand Down
66 changes: 36 additions & 30 deletions src/webview/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,40 @@ function ProviderForm({ onClose, provider }: ProviderFormProps) {

const hasOllamaModels = !!models?.length

const getModelInput = () => {
if (formState.provider === ApiProviders.Ollama && hasOllamaModels) {
return (
<>
<div>
<label htmlFor="modelName">Model name*</label>
</div>
<ModelSelect
models={models}
model={formState.modelName}
setModel={(model: string) => {
setFormState({ ...formState, modelName: model })
}}
/>
</>
)
}

return (
<>
<div>
<label htmlFor="modelName">Model name*</label>
</div>
<VSCodeTextField
required
name="modelName"
onChange={handleChange}
value={formState.modelName}
placeholder='Applicable for some providers like "Ollama"'
></VSCodeTextField>
</>
)
}

return (
<>
<VSCodeDivider />
Expand All @@ -232,7 +266,7 @@ function ProviderForm({ onClose, provider }: ProviderFormProps) {
name="label"
onChange={handleChange}
value={formState.label}
placeholder='Applicable for some providers like "Ollama"'
placeholder='Just for your reference'
></VSCodeTextField>
</div>

Expand Down Expand Up @@ -306,35 +340,7 @@ function ProviderForm({ onClose, provider }: ProviderFormProps) {
</VSCodeDropdown>
</div>

{formState.provider === ApiProviders.Ollama && hasOllamaModels && (
<div>
<div>
<label htmlFor="apiHostname">Model name*</label>
</div>
<ModelSelect
models={models}
model={formState.modelName}
setModel={(model: string) => {
setFormState({ ...formState, modelName: model })
}}
/>
</div>
)}

{formState.provider !== ApiProviders.Ollama && (
<div>
<div>
<label htmlFor="modelName">Model name*</label>
</div>
<VSCodeTextField
required
name="modelName"
onChange={handleChange}
value={formState.modelName}
placeholder='Applicable for some providers like "Ollama"'
></VSCodeTextField>
</div>
)}
{getModelInput()}

<div>
<div>
Expand Down

0 comments on commit 5216b47

Please sign in to comment.