Skip to content
Open
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
22 changes: 18 additions & 4 deletions src/browser.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as utils from './utils.js'
import { AbortableAsyncIterator, parseJSON, post } from './utils.js'
import 'whatwg-fetch'

import type {
ChatRequest,
Expand Down Expand Up @@ -39,9 +38,24 @@ export class Ollama {
this.config.host = utils.formatHost(config?.host ?? 'http://127.0.0.1:11434')
}

this.fetch = fetch
if (config?.fetch != null) {
this.fetch = config.fetch
this.fetch = config?.fetch || this.getFetch();
}

private getFetch(): Fetch {
if (typeof window !== 'undefined' && window.fetch) {
return window.fetch.bind(window);
}

if (typeof global !== 'undefined' && global.fetch) {
return global.fetch;
}

try {
// Use dynamic import to allow for environments where whatwg-fetch is not available
return require('whatwg-fetch');
} catch (error) {
console.error('Failed to import whatwg-fetch:', error);
throw new Error('Fetch is not available. Please provide a fetch implementation in the config.');
}
}

Expand Down