Skip to content

Commit

Permalink
Remove the browser-ai option from user-prefs as this is effectively t…
Browse files Browse the repository at this point in the history
…he default mode
  • Loading branch information
Thomas101 committed Dec 9, 2024
1 parent c7b8c89 commit 4273e40
Show file tree
Hide file tree
Showing 9 changed files with 8 additions and 155 deletions.
11 changes: 0 additions & 11 deletions src/extension/background/APIHandler/APIHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import AICoreModelHandler from './AICoreModelHandler'
import AIEmbeddingHandler from './AIEmbeddingHandler'
import AILanguageDetectorHandler from './AILanguageDetectorHandler'
import AITranslatorHandler from './AITranslatorHandler'
import { kPrefGetUseBrowserAI } from '#Shared/API/PrefIPCMessageTypes'
import { getUseBrowserAI } from '#Shared/Prefs'
import { kAIGetCapabilities, kAIGetNativeHelperDownloadUrl } from '#Shared/API/AIIPCTypes'
import System from '../System'
import { AICapabilities, AIHelperInstalledState } from '#Shared/API/AI'
Expand Down Expand Up @@ -48,19 +46,10 @@ class APIHandler {
this.#translatorHandler = new AITranslatorHandler(this.#server)

this.#server
.addRequestHandler(kPrefGetUseBrowserAI, this.#handleGetUseBrowserAI)
.addRequestHandler(kAIGetCapabilities, this.#handleGetCapabilities)
.addRequestHandler(kAIGetNativeHelperDownloadUrl, this.#handleGetNativeHelperDownloadUrl)
}

/* **************************************************************************/
// MARK: Handlers: Prefs
/* **************************************************************************/

#handleGetUseBrowserAI = async () => {
return await getUseBrowserAI()
}

/* **************************************************************************/
// MARK: Handlers: Capabilities
/* **************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
kLanguageModelGetCapabilities,
kLanguageModelCreate
} from '#Shared/API/AILanguageModel/AILanguageModelIPCTypes'
import Prefs from '../Prefs'
import IPC from '../IPC'
import { throwIPCErrorResponse } from '#Shared/IPC/IPCErrorHelper'
import AILanguageModelCapabilities from './AILanguageModelCapabilities'
Expand All @@ -31,26 +30,11 @@ class AILanguageModelFactory {
this.#browserAI = browserAI
}

/* **************************************************************************/
// MARK: Utils
/* **************************************************************************/

/**
* @returns true if we should be using the browsers built-in ai for this factory
*/
async #shouldUseBrowserAI () {
return typeof (this.#browserAI?.languageModel) === 'object' && await Prefs.getUseBrowserAI()
}

/* **************************************************************************/
// MARK: Capabilities
/* **************************************************************************/

capabilities = async (options: AILanguageModelCapabilitiesOptions = {}): Promise<AILanguageModelCapabilities> => {
if (await this.#shouldUseBrowserAI()) {
return this.#browserAI.languageModel.capabilities(options)
}

const data = throwIPCErrorResponse(
await IPC.request(kLanguageModelGetCapabilities, options)
) as AILanguageModelCapabilitiesData
Expand All @@ -63,10 +47,6 @@ class AILanguageModelFactory {
/* **************************************************************************/

create = async (options: AILanguageModelCreateOptions = {}) => {
if (await this.#shouldUseBrowserAI()) {
return this.#browserAI.languageModel.create(options)
}

const monitorTarget = new EventTarget()
const {
monitor,
Expand Down
20 changes: 0 additions & 20 deletions src/extension/contentscript-main/AIRewriter/AIRewriterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
AIRewriterData
} from '#Shared/API/AIRewriter/AIRewriterTypes'
import AIRewriterCapabilities from './AIRewriterCapabilities'
import Prefs from '../Prefs'
import IPC from '../IPC'
import { throwIPCErrorResponse } from '#Shared/IPC/IPCErrorHelper'
import {
Expand All @@ -31,26 +30,11 @@ class AIRewriterFactory {
this.#browserAI = browserAI
}

/* **************************************************************************/
// MARK: Utils
/* **************************************************************************/

/**
* @returns true if we should be using the browsers built-in ai for this factory
*/
async #shouldUseBrowserAI () {
return typeof (this.#browserAI?.rewriter) === 'object' && await Prefs.getUseBrowserAI()
}

/* **************************************************************************/
// MARK: Capabilities
/* **************************************************************************/

capabilities = async (options: AIRewriterCapabilitiesOptions): Promise<AIRewriterCapabilities> => {
if (await this.#shouldUseBrowserAI()) {
return this.#browserAI.rewriter.capabilities(options)
}

const data = throwIPCErrorResponse(
await IPC.request(kRewriterGetCapabilities, options)
) as AIRewriterCapabilitiesData
Expand All @@ -63,10 +47,6 @@ class AIRewriterFactory {
/* **************************************************************************/

create = async (options: AIRewriterCreateOptions = {}) => {
if (await this.#shouldUseBrowserAI()) {
return this.#browserAI.rewriter.create(options)
}

const monitorTarget = new EventTarget()
const {
monitor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
AISummarizerData
} from '#Shared/API/AISummarizer/AISummarizerTypes'
import AISummarizerCapabilities from './AISummarizerCapabilities'
import Prefs from '../Prefs'
import IPC from '../IPC'
import { throwIPCErrorResponse } from '#Shared/IPC/IPCErrorHelper'
import {
Expand All @@ -31,26 +30,11 @@ class AISummarizerFactory {
this.#browserAI = browserAI
}

/* **************************************************************************/
// MARK: Utils
/* **************************************************************************/

/**
* @returns true if we should be using the browsers built-in ai for this factory
*/
async #shouldUseBrowserAI () {
return typeof (this.#browserAI?.summarizer) === 'object' && await Prefs.getUseBrowserAI()
}

/* **************************************************************************/
// MARK: Capabilities
/* **************************************************************************/

capabilities = async (options: AISummarizerCapabilitiesOptions): Promise<AISummarizerCapabilities> => {
if (await this.#shouldUseBrowserAI()) {
return this.#browserAI.summarizer.capabilities(options)
}

const data = throwIPCErrorResponse(
await IPC.request(kSummarizerGetCapabilities, options)
) as AISummarizerCapabilitiesData
Expand All @@ -63,10 +47,6 @@ class AISummarizerFactory {
/* **************************************************************************/

create = async (options: AISummarizerCreateOptions = {}) => {
if (await this.#shouldUseBrowserAI()) {
return this.#browserAI.summarizer.create(options)
}

const monitorTarget = new EventTarget()
const {
monitor,
Expand Down
20 changes: 0 additions & 20 deletions src/extension/contentscript-main/AIWriter/AIWriterFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
AIWriterData
} from '#Shared/API/AIWriter/AIWriterTypes'
import AIWriterCapabilities from './AIWriterCapabilities'
import Prefs from '../Prefs'
import IPC from '../IPC'
import { throwIPCErrorResponse } from '#Shared/IPC/IPCErrorHelper'
import {
Expand All @@ -31,26 +30,11 @@ class AIWriterFactory {
this.#browserAI = browserAI
}

/* **************************************************************************/
// MARK: Utils
/* **************************************************************************/

/**
* @returns true if we should be using the browsers built-in ai for this factory
*/
async #shouldUseBrowserAI () {
return typeof (this.#browserAI?.writer) === 'object' && await Prefs.getUseBrowserAI()
}

/* **************************************************************************/
// MARK: Capabilities
/* **************************************************************************/

capabilities = async (options: AIWriterCapabilitiesOptions): Promise<AIWriterCapabilities> => {
if (await this.#shouldUseBrowserAI()) {
return this.#browserAI.writer.capabilities(options)
}

const data = throwIPCErrorResponse(
await IPC.request(kWriterGetCapabilities, options)
) as AIWriterCapabilitiesData
Expand All @@ -63,10 +47,6 @@ class AIWriterFactory {
/* **************************************************************************/

create = async (options: AIWriterCreateOptions = {}) => {
if (await this.#shouldUseBrowserAI()) {
return this.#browserAI.writer.create(options)
}

const monitorTarget = new EventTarget()
const {
monitor,
Expand Down
24 changes: 0 additions & 24 deletions src/extension/contentscript-main/Prefs.ts

This file was deleted.

43 changes: 8 additions & 35 deletions src/extension/ui-options/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ import IPCBackgroundMessager from '#Shared/IPC/IPCBackgroundMessager'
import {
getDefaultModel,
setDefaultModel,
getUseBrowserAI,
setUseBrowserAI,
setDefaultModelEngine,
getDefaultModelEngine,
setModelUpdatePeriod,
Expand All @@ -43,37 +41,19 @@ import { AIModelType } from '#Shared/API/AI'
// MARK: Settings
/* **************************************************************************/

const kBrowserAiModelId = '__browser__'

function renderDefaultModelOptions (
$el: HTMLSelectElement,
selectedModelId: string,
modelList: Array<{ id: string, name: string }>,
hasBrowserAI: boolean,
useBrowserAI: boolean
modelList: Array<{ id: string, name: string }>
) {
UI.empty($el)

if (hasBrowserAI) {
modelList = [{ id: kBrowserAiModelId, name: 'Browser AI' }, ...modelList]
} else {
useBrowserAI = false
}

for (const model of modelList) {
const $option = document.createElement('option')
$option.value = model.id
$option.textContent = model.name
if (model.id === kBrowserAiModelId) {
if (useBrowserAI) {
$option.selected = true
}
} else {
if (model.id === selectedModelId) {
if ((useBrowserAI && modelList.some(({ id }) => id === kBrowserAiModelId)) === false) {
$option.selected = true
}
}
if (model.id === selectedModelId) {
$option.selected = true
}
$el.appendChild($option)
}
Expand Down Expand Up @@ -111,27 +91,20 @@ async function renderSettings () {
const $defaultTextModelOpt = document.getElementById('opt-model-text-default') as HTMLSelectElement
$defaultTextModelOpt.addEventListener('change', async () => {
const modelId = $defaultTextModelOpt.value

if (modelId === kBrowserAiModelId) {
await setUseBrowserAI(true)
} else {
await setUseBrowserAI(false)
await setDefaultModel(AIModelType.Text, modelId)
}
await setDefaultModel(AIModelType.Text, modelId)
})
;(async () => {
const useBrowserAI = await getUseBrowserAI()
const defaultModelId = await getDefaultModel(AIModelType.Text)
const defaultModelList = [
...new Set([defaultModelId, Object.values(config.defaultModels)])
].map((modelId) => ({ id: modelId, name: modelId }))

renderDefaultModelOptions($defaultTextModelOpt, defaultModelId, defaultModelList, Boolean(window.ai?.languageModel), useBrowserAI)
renderDefaultModelOptions($defaultTextModelOpt, defaultModelId, defaultModelList)

const res = await fetch('https://aibrow.ai/api/model/list.json')
if (res.ok) {
const { models } = await res.json()
renderDefaultModelOptions($defaultTextModelOpt, defaultModelId, models, Boolean(window.ai?.languageModel), useBrowserAI)
renderDefaultModelOptions($defaultTextModelOpt, defaultModelId, models)
}
})()

Expand All @@ -146,12 +119,12 @@ async function renderSettings () {
const defaultModelList = [
...new Set([defaultModelId, Object.values(config.defaultModels)])
].map((modelId) => ({ id: modelId, name: modelId }))
renderDefaultModelOptions($defaultEmbeddingModelOpt, defaultModelId, defaultModelList, false, false)
renderDefaultModelOptions($defaultEmbeddingModelOpt, defaultModelId, defaultModelList)

const res = await fetch('https://aibrow.ai/api/model/list.json')
if (res.ok) {
const { models } = await res.json()
renderDefaultModelOptions($defaultEmbeddingModelOpt, defaultModelId, models, false, false)
renderDefaultModelOptions($defaultEmbeddingModelOpt, defaultModelId, models)
}
})()

Expand Down
1 change: 0 additions & 1 deletion src/shared/API/PrefIPCMessageTypes.ts

This file was deleted.

4 changes: 0 additions & 4 deletions src/shared/Prefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ const kDefaultModelsPrefixKey = `${kPrefKeyPrefix}:defaultModels:`
export async function getDefaultModel (modelType: AIModelType) { return getPref(`${kDefaultModelsPrefixKey}${modelType}`, config.defaultModels[modelType]) }
export async function setDefaultModel (modelType: AIModelType, modelId: string) { return setPref(`${kDefaultModelsPrefixKey}${modelType}`, modelId) }

const kDefaultUseBrowserAIKey = `${kPrefKeyPrefix}:useBrowserAI`
export async function getUseBrowserAI () { return getPref(kDefaultUseBrowserAIKey, false) }
export async function setUseBrowserAI (use: boolean) { return setPref(kDefaultUseBrowserAIKey, use) }

const kOverrideBrowserAIKey = `${kPrefKeyPrefix}:overrideBrowserAI`
const kContentscriptMainOverrideId = 'contentscript-main-override'
export async function getOverrideBrowserAI () { return getPref(kOverrideBrowserAIKey, false) }
Expand Down

0 comments on commit 4273e40

Please sign in to comment.