Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/insomnia/src/entry.preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const main: Window['main'] = {
writeFile: options => ipcRenderer.invoke('writeFile', options),
readFile: options => ipcRenderer.invoke('readFile', options),
readDir: options => ipcRenderer.invoke('readDir', options),
readOrCreateDataDir: options => ipcRenderer.invoke('readOrCreateDataDir', options),
lintSpec: options => ipcRenderer.invoke('lintSpec', options),
on: (channel, listener) => {
ipcRenderer.on(channel, listener);
Expand Down
1 change: 1 addition & 0 deletions packages/insomnia/src/main/ipc/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ export type HandleChannels =
| 'open-channel-to-hidden-browser-window'
| 'openPath'
| 'readCurlResponse'
| 'readOrCreateDataDir'
| 'readDir'
| 'readFile'
| 'restoreBackup'
Expand Down
46 changes: 29 additions & 17 deletions packages/insomnia/src/main/ipc/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import fs from 'node:fs';
import fs, { mkdirSync } from 'node:fs';
import os from 'node:os';
import path from 'node:path';

Expand Down Expand Up @@ -60,6 +60,24 @@ export const openInBrowser = (href: string) => {
}
};

const readDir = async (_: unknown, options: { path: string }) => {
try {
const files = await fs.promises.readdir(options.path);
return files
.map(file => {
const filePath = path.join(options.path, file);
return {
type: fs.statSync(filePath).isDirectory() ? 'directory' : fs.statSync(filePath).isFile() ? 'file' : 'other',
name: file,
path: filePath,
};
})
.filter(file => file.type !== 'other');
} catch (err) {
throw new Error(`Failed to read directory: ${err}`);
}
};

export interface RendererToMainBridgeAPI {
loginStateChange: () => void;
openInBrowser: (url: string) => void;
Expand All @@ -78,6 +96,9 @@ export interface RendererToMainBridgeAPI {
writeFile: (options: { path: string; content: string }) => Promise<string>;
readFile: (options: { path: string; encoding?: string }) => Promise<{ content: string; encoding: string }>;
readDir: (options: { path: string }) => Promise<{ type: 'file' | 'directory'; name: string; path: string }[]>;
readOrCreateDataDir: (options: {
folder: string;
}) => Promise<{ type: 'file' | 'directory'; name: string; path: string }[]>;
cancelCurlRequest: typeof cancelCurlRequest;
curlRequest: typeof curlRequest;
on: (channel: RendererOnChannels, listener: (event: IpcRendererEvent, ...args: any[]) => void) => () => void;
Expand Down Expand Up @@ -251,22 +272,13 @@ export function registerMainHandlers() {
};
});

ipcMainHandle('readDir', async (_, options: { path: string }) => {
try {
const files = await fs.promises.readdir(options.path);
return files
.map(file => {
const filePath = path.join(options.path, file);
return {
type: fs.statSync(filePath).isDirectory() ? 'directory' : fs.statSync(filePath).isFile() ? 'file' : 'other',
name: file,
path: filePath,
};
})
.filter(file => file.type !== 'other');
} catch (err) {
throw new Error(`Failed to read directory: ${err}`);
}
ipcMainHandle('readDir', readDir);

ipcMainHandle('readOrCreateDataDir', async (_, options: { folder: string }) => {
const dataPath = app.getPath('userData');
const folderPath = path.join(dataPath, options.folder);
mkdirSync(folderPath, { recursive: true });
return readDir(_, { path: folderPath });
});

ipcMainHandle('curlRequest', (_, options: Parameters<typeof curlRequest>[0]) => {
Expand Down
23 changes: 15 additions & 8 deletions packages/insomnia/src/ui/components/settings/llms/gguf.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const DEFAULT_MODEL_PARAMETERS = {
repeatPenalty: 1.1,
};

const LLMS_FOLDER_NAME = 'llms';

export const GGUF = ({
saveLLMSettings,
configuredLLMs,
Expand All @@ -40,18 +42,23 @@ export const GGUF = ({
const [showAdvancedOptions, setShowAdvancedOptions] = useState(false);

const userDataPath = resolve(window.app.getPath('userData'));
const llmsFolder = resolve(userDataPath, 'llms');
const llmsFolder = resolve(userDataPath, LLMS_FOLDER_NAME);
const [availableLLMs, setAvailableLLMs] = useState<string[]>([]);
const [selectedModel, setSelectedModel] = useState<string>('');
const refreshModelsDirectory = useCallback(() => {
window.main.readDir({ path: llmsFolder }).then(models => {
const currentlyAvailableLLMs = models
.filter(model => model.type === 'file' && model.name.toLowerCase().endsWith('.gguf'))
.map(model => model.name);
window.main
.readOrCreateDataDir({ folder: LLMS_FOLDER_NAME })
.then(models => {
const currentlyAvailableLLMs = models
.filter(model => model.type === 'file' && model.name.toLowerCase().endsWith('.gguf'))
.map(model => model.name);

setAvailableLLMs(currentlyAvailableLLMs);
});
}, [llmsFolder]);
setAvailableLLMs(currentlyAvailableLLMs);
})
.catch(() => {
setAvailableLLMs([]);
});
}, []);

useEffect(() => {
if (configuredLLMs.length === 1) {
Expand Down
Loading