Skip to content

Commit

Permalink
Add Host validation for LM Studio
Browse files Browse the repository at this point in the history
  • Loading branch information
mrdjohnson committed Jul 25, 2024
1 parent d04be13 commit 37afe33
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/components/HostInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const HostInput = observer(({ connection, isEnabled }: HostInputProps) => {

const {
control,
formState: { dirtyFields },
formState: { dirtyFields, errors },
} = useFormContext<SnapshotIn<IConnectionDataModel>>()

const isDirty = dirtyFields.host
Expand All @@ -43,6 +43,7 @@ const HostInput = observer(({ connection, isEnabled }: HostInputProps) => {
disabled={!isEnabled}
placeholder={host}
type='url'
errorMessage={errors.host?.message}
description={
<span className="flex flex-col gap-2 align-baseline text-sm md:flex-row">
<span className='flex align-baseline'>
Expand Down Expand Up @@ -78,6 +79,9 @@ const HostInput = observer(({ connection, isEnabled }: HostInputProps) => {
control={control}
name="host"
defaultValue={host}
rules={{
validate: connection.validateHost,
}}
/>
)
})
Expand Down
8 changes: 8 additions & 0 deletions src/features/connections/servers/LmsServerConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ class LmsServerConnection extends ServerConnection<ILmsModel> {
],
})

validateHost(host?: string) {
if(!host) return true

if(!host.startsWith('ws')) return 'Host needs to start with ws:// or wss://'

return true
}

async _fetchLmModels(host: string): Promise<LmsLanguageModel[]> {
const client = new LMStudioClient({ baseUrl: host })

Expand Down
4 changes: 4 additions & 0 deletions src/features/connections/servers/ServerConnection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ abstract class ServerConnection<
return host.trim()
}

validateHost(): string | boolean {
return true
}

protected abstract _fetchLmModels(host: string): Promise<Array<LanguageModelType<BaseModelType>>>

async fetchLmModels() {
Expand Down

0 comments on commit 37afe33

Please sign in to comment.