Skip to content

Commit

Permalink
Move the APIs into shared so we can make better use of code splitting
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas101 committed Dec 9, 2024
1 parent 6ed5857 commit 05bbc8d
Show file tree
Hide file tree
Showing 48 changed files with 128 additions and 128 deletions.
2 changes: 1 addition & 1 deletion src/extension/background/APIHandler/AICoreModelHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNonEmptyString } from '#Shared/API/Untrusted/UntrustedParser'
import { getNonEmptyString } from '#Shared/Typo/TypoParser'
import {
AICoreModelData
} from '#Shared/API/AICoreModel/AICoreModelTypes'
Expand Down
2 changes: 1 addition & 1 deletion src/extension/background/APIHandler/AIEmbeddingHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNonEmptyString } from '#Shared/API/Untrusted/UntrustedParser'
import { getNonEmptyString } from '#Shared/Typo/TypoParser'
import {
AIEmbeddingData,
AIEmbeddingVector
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNonEmptyString } from '#Shared/API/Untrusted/UntrustedParser'
import { getNonEmptyString } from '#Shared/Typo/TypoParser'
import {
AILanguageDetectorData,
AILanguageDetectorDetectResult,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
AILanguageModelPrompt,
AILanguageModelPromptRole
} from '#Shared/API/AILanguageModel/AILanguageModelTypes'
import { getNonEmptyString } from '#Shared/API/Untrusted/UntrustedParser'
import { getNonEmptyString } from '#Shared/Typo/TypoParser'
import { kModelPromptAborted, kModelPromptTypeNotSupported } from '#Shared/Errors'
import APIHelper from './APIHelper'
import AILlmSession from '../AI/AILlmSession'
Expand Down
2 changes: 1 addition & 1 deletion src/extension/background/APIHandler/AIRewriterHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNonEmptyString } from '#Shared/API/Untrusted/UntrustedParser'
import { getNonEmptyString } from '#Shared/Typo/TypoParser'
import {
AIRewriterTone,
AIRewriterFormat,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/background/APIHandler/AISummarizerHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNonEmptyString } from '#Shared/API/Untrusted/UntrustedParser'
import { getNonEmptyString } from '#Shared/Typo/TypoParser'
import {
AISummarizerType,
AISummarizerFormat,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/background/APIHandler/AITranslatorHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNonEmptyString } from '#Shared/API/Untrusted/UntrustedParser'
import { getNonEmptyString } from '#Shared/Typo/TypoParser'
import { AITranslatorData } from '#Shared/API/AITranslator/AITranslatorTypes'
import {
kTranslatorGetCapabilities,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/background/APIHandler/AIWriterHandler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getNonEmptyString } from '#Shared/API/Untrusted/UntrustedParser'
import { getNonEmptyString } from '#Shared/Typo/TypoParser'
import {
AIWriterTone,
AIWriterFormat,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/background/APIHandler/APIHandlerFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { kContentScriptPortName, kExtensionLibPortName } from '#Shared/API/ContentScript'
import { kContentScriptPortName, kExtensionLibPortName } from '#Shared/IPC/ContentScriptIPC'
import APIHandler from './APIHandler'
import NativeIPC from '../NativeIPC'
import { updateOverrideBrowserAIScriptInjection, getOverrideBrowserAI } from '#Shared/Prefs'
Expand Down
14 changes: 7 additions & 7 deletions src/extension/background/APIHandler/APIHelper.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { clamp } from '#Shared/API/Untrusted/UntrustedParser'
import { clamp } from '#Shared/Typo/TypoParser'
import {
getDefaultModel,
getDefaultModelEngine,
Expand Down Expand Up @@ -37,7 +37,7 @@ import { IPCInflightChannel } from '#Shared/IPC/IPCServer'
import PermissionProvider from '../PermissionProvider'
import AILlmSession from '../AI/AILlmSession'
import { AIModelManifest } from '#Shared/AIModelManifest'
import UntrustedParser from '#Shared/API/Untrusted/UntrustedObject'
import TypoParser from '#Shared/Typo/TypoObject'
import AIModelManager from '../AI/AIModelManager'
import config from '#Shared/Config'

Expand Down Expand Up @@ -210,7 +210,7 @@ class APIHelper {
* @returns the core llm prompt options
*/
async #sanitizeModelProps (modelId: AIModelId, gpuEngine: AICapabilityGpuEngine, manifest: AIModelManifest, modelProps: any) {
const props = new UntrustedParser(modelProps)
const props = new TypoParser(modelProps)
return {
model: modelId.toString(),
gpuEngine,
Expand Down Expand Up @@ -239,7 +239,7 @@ class APIHelper {
promptType: AICapabilityPromptType,
postflightFn: (
manifest: AIModelManifest,
payload: UntrustedParser,
payload: TypoParser,
props: AIRootModelProps
) => Promise<any>
) {
Expand All @@ -251,7 +251,7 @@ class APIHelper {
throw new Error(kGpuEngineNotSupported)
}

const payload = new UntrustedParser(rawPayload)
const payload = new TypoParser(rawPayload)
return await this.captureCommonErrorsForResponse(async () => {
// Values with user-defined defaults
const modelId = await this.getModelId(rawPayload?.model, modelType)
Expand Down Expand Up @@ -314,12 +314,12 @@ class APIHelper {
modelType: AIModelType,
postflightFn: (
manifest: AIModelManifest,
payload: UntrustedParser,
payload: TypoParser,
props: AIRootModelProps
) => Promise<any>
) {
const rawPayload = channel.payload
const payload = new UntrustedParser(rawPayload)
const payload = new TypoParser(rawPayload)

// Values with user-defined defaults
const modelId = await this.getModelId(rawPayload?.props?.model, modelType)
Expand Down
2 changes: 1 addition & 1 deletion src/extension/contentscript-isolated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
kContentScriptMessageChannel,
kContentScriptDisconnectChannel,
kContentScriptPortName
} from '#Shared/API/ContentScript'
} from '#Shared/IPC/ContentScriptIPC'

let port: chrome.runtime.Port | undefined

Expand Down
16 changes: 8 additions & 8 deletions src/extension/contentscript-main/AI.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import AISummarizerFactory from './AISummarizer/AISummarizerFactory'
import AIWriterFactory from './AIWriter/AIWriterFactory'
import AIRewriterFactory from './AIRewriter/AIRewriterFactory'
import AILanguageModelFactory from './AILanguageModel/AILanguageModelFactory'
import AICoreModelFactory from './AICoreModel/AICoreModelFactory'
import AIEmbeddingFactory from './AIEmbedding/AIEmbeddingFactory'
import AITranslatorFactory from './AITranslator/AITranslatorFactory'
import AILanguageDetectorFactory from './AILanguageDetector/AILanguageDetectorFactory'
import AISummarizerFactory from '#Shared/API/AISummarizer/AISummarizerFactory'
import AIWriterFactory from '#Shared/API/AIWriter/AIWriterFactory'
import AIRewriterFactory from '#Shared/API/AIRewriter/AIRewriterFactory'
import AILanguageModelFactory from '#Shared/API/AILanguageModel/AILanguageModelFactory'
import AICoreModelFactory from '#Shared/API/AICoreModel/AICoreModelFactory'
import AIEmbeddingFactory from '#Shared/API/AIEmbedding/AIEmbeddingFactory'
import AITranslatorFactory from '#Shared/API/AITranslator/AITranslatorFactory'
import AILanguageDetectorFactory from '#Shared/API/AILanguageDetector/AILanguageDetectorFactory'
import IPC from './IPC'
import {
kAIGetCapabilities,
Expand Down
2 changes: 1 addition & 1 deletion src/extension/contentscript-main/IPC/ContentScriptPort.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
kContentScriptMessageChannel,
kContentScriptDisconnectChannel
} from '#Shared/API/ContentScript'
} from '#Shared/IPC/ContentScriptIPC'
import { PortEventHandler } from './PortEventHandler'

export default class ContentScriptPort {
Expand Down
2 changes: 1 addition & 1 deletion src/extension/contentscript-main/IPC/ExtensionLibPort.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
kExtensionLibPortName
} from '#Shared/API/ContentScript'
} from '#Shared/IPC/ContentScriptIPC'
import config from '#Shared/Config'
import { PortEventHandler } from './PortEventHandler'
import { kExtensionNotFound } from '#Shared/BrowserErrors'
Expand Down
4 changes: 2 additions & 2 deletions src/extension/contentscript-main/Translation.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AI from './AI'
import AILanguageDetector from './AILanguageDetector/AILanguageDetector'
import AITranslator from './AITranslator/AITranslator'
import AILanguageDetector from '#Shared/API/AILanguageDetector/AILanguageDetector'
import AITranslator from '#Shared/API/AITranslator/AITranslator'
import { AICapabilityAvailability } from '#Shared/API/AI'

type DetectorBridge = {
Expand Down
14 changes: 7 additions & 7 deletions src/native/main/APIHandler/LlmSessionAPIHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import config from '#Shared/Config'
import type { Llama, LlamaCompletion, LlamaContext, LlamaModel, LlamaGrammar, LlamaEmbeddingContext } from 'node-llama-cpp'
import AIModelFileSystem from '#R/AI/AIModelFileSystem'
import { kModelPromptAborted } from '#Shared/Errors'
import UntrustedParser from '#Shared/API/Untrusted/UntrustedObject'
import TypoParser from '#Shared/Typo/TypoObject'
import { nanoid } from 'nanoid'
import { clamp } from '#Shared/API/Untrusted/UntrustedParser'
import { clamp } from '#Shared/Typo/TypoParser'
import { AIModelManifest } from '#Shared/AIModelManifest'
import AsyncQueue from '#Shared/AsyncQueue'
import AIModelId from '#Shared/AIModelId'
Expand Down Expand Up @@ -112,7 +112,7 @@ class LlmSessionAPIHandler {
}

#handleGetModelScore = async (channel: IPCInflightChannel) => {
const props = new UntrustedParser(channel.payload)
const props = new TypoParser(channel.payload)
const gpuEngine = props.getEnum('gpuEngine', AICapabilityGpuEngine, undefined)
const flashAttention = props.getBool('flashAttention', false)
const contextSize = props.getNumber('contextSize', 2048)
Expand Down Expand Up @@ -365,7 +365,7 @@ class LlmSessionAPIHandler {
* @returns the model props sanitized with the manifest
*/
#sanitizeModelProps = (modelProps: any, manifest: AIModelManifest) => {
const props = new UntrustedParser(modelProps)
const props = new TypoParser(modelProps)
return {
model: manifest.id,
gpuEngine: props.getEnum('gpuEngine', AICapabilityGpuEngine, undefined),
Expand All @@ -386,7 +386,7 @@ class LlmSessionAPIHandler {

try {
// Extract the options
const payload = new UntrustedParser(channel.payload)
const payload = new TypoParser(channel.payload)
const modelId = new AIModelId(payload.getNonEmptyString('props.model', config.defaultModels[AIModelType.Text]))
const manifest = await AIModelFileSystem.readModelManifest(modelId)

Expand Down Expand Up @@ -456,7 +456,7 @@ class LlmSessionAPIHandler {
return await this.#requestQueue.push(async () => {
try {
// Extract the options
const payload = new UntrustedParser(channel.payload)
const payload = new TypoParser(channel.payload)
const modelId = new AIModelId(payload.getNonEmptyString('props.model', config.defaultModels[AIModelType.Embedding]))
const manifest = await AIModelFileSystem.readModelManifest(modelId)

Expand Down Expand Up @@ -498,7 +498,7 @@ class LlmSessionAPIHandler {
return await this.#requestQueue.push(async () => {
try {
// Extract the options
const payload = new UntrustedParser(channel.payload)
const payload = new TypoParser(channel.payload)
const modelId = new AIModelId(payload.getNonEmptyString('props.model', config.defaultModels[AIModelType.Text]))
const input = payload.getString('input')
const manifest = await AIModelFileSystem.readModelManifest(modelId)
Expand Down
2 changes: 1 addition & 1 deletion src/native/main/APIHandler/ModelDownloadAPIHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { finished } from 'stream/promises'
import { withDir, withFile } from 'tmp-promise'
import config from '#Shared/Config'
import { importLlama } from '#R/Llama'
import { getNonEmptyString } from '#Shared/API/Untrusted/UntrustedParser'
import { getNonEmptyString } from '#Shared/Typo/TypoParser'
import AIModelId, { AIModelIdProvider } from '#Shared/AIModelId'
import AIModelAssetId from '#Shared/AIModelAssetId'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ import {
AICoreModelData,
AICoreModelProps,
AICoreModelCloneOptions
} from '#Shared/API/AICoreModel/AICoreModelTypes'
import { kCoreModelCreate, kCoreModelPrompt, kCoreModelCountTokens } from '#Shared/API/AICoreModel/AICoreModelIPCTypes'
import { kSessionDestroyed } from '#Shared/Errors'
} from './AICoreModelTypes'
import { kCoreModelCreate, kCoreModelPrompt, kCoreModelCountTokens } from './AICoreModelIPCTypes'
import { kSessionDestroyed } from '../../Errors'
import { readablePromptStreamToString } from '../AIHelpers'
import AIRootModel from '../AIRootModel'
import IPCClient from '#Shared/IPC/IPCClient'
import IPCClient from '../../IPC/IPCClient'

class AICoreModel extends AIRootModel {
/* **************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AICoreModelCapabilitiesData } from '#Shared/API/AICoreModel/AICoreModelTypes'
import { AICoreModelCapabilitiesData } from './AICoreModelTypes'
import AIRootModelCapabilities from '../AIRootModelCapabilities'

class AICoreModelCapabilities extends AIRootModelCapabilities {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import {
AICoreModelCreateOptions,
AICoreModelData,
AICoreModelCapabilitiesData
} from '#Shared/API/AICoreModel/AICoreModelTypes'
} from './AICoreModelTypes'
import {
kCoreModelGetCapabilities,
kCoreModelCreate
} from '#Shared/API/AICoreModel/AICoreModelIPCTypes'
import { throwIPCErrorResponse } from '#Shared/IPC/IPCErrorHelper'
} from './AICoreModelIPCTypes'
import { throwIPCErrorResponse } from '../../IPC/IPCErrorHelper'
import AICoreModelCapabilities from './AICoreModelCapabilities'
import AICoreModel from './AICoreModel'
import { kModelCreationAborted } from '#Shared/Errors'
import { kModelCreationAborted } from '../../Errors'
import { createDownloadProgressFn } from '../AIHelpers'
import IPCClient from '#Shared/IPC/IPCClient'
import IPCClient from '../../IPC/IPCClient'

class AICoreModelFactory {
/* **************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import {
AIEmbeddingData,
AIEmbeddingProps,
AIEmbeddingCloneOptions
} from '#Shared/API/AIEmbedding/AIEmbeddingTypes'
import { kEmbeddingCreate, kEmbeddingGet } from '#Shared/API/AIEmbedding/AIEmbeddingIPCTypes'
import { kSessionDestroyed } from '#Shared/Errors'
} from './AIEmbeddingTypes'
import { kEmbeddingCreate, kEmbeddingGet } from './AIEmbeddingIPCTypes'
import { kSessionDestroyed } from '../../Errors'
import AIRootModel from '../AIRootModel'
import { calculateCosineSimilarity, findSimilar } from './AIEmbeddingUtil'
import IPCClient from '#Shared/IPC/IPCClient'
import IPCClient from '../../IPC/IPCClient'

class AIEmbedding extends AIRootModel {
/* **************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AIEmbeddingCapabilitiesData } from '#Shared/API/AIEmbedding/AIEmbeddingTypes'
import { AIEmbeddingCapabilitiesData } from './AIEmbeddingTypes'
import AIRootModelCapabilities from '../AIRootModelCapabilities'

class AIEmbeddingCapabilities extends AIRootModelCapabilities {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ import {
AIEmbeddingCreateOptions,
AIEmbeddingData,
AIEmbeddingCapabilitiesData
} from '#Shared/API/AIEmbedding/AIEmbeddingTypes'
} from './AIEmbeddingTypes'
import {
kEmbeddingGetCapabilities,
kEmbeddingCreate
} from '#Shared/API/AIEmbedding/AIEmbeddingIPCTypes'
import { throwIPCErrorResponse } from '#Shared/IPC/IPCErrorHelper'
} from './AIEmbeddingIPCTypes'
import { throwIPCErrorResponse } from '../../IPC/IPCErrorHelper'
import AIEmbeddingCapabilities from './AIEmbeddingCapabilities'
import AIEmbedding from './AIEmbedding'
import { kModelCreationAborted } from '#Shared/Errors'
import { kModelCreationAborted } from '../../Errors'
import { createDownloadProgressFn } from '../AIHelpers'
import { calculateCosineSimilarity, findSimilar } from './AIEmbeddingUtil'
import IPCClient from '#Shared/IPC/IPCClient'
import IPCClient from '../../IPC/IPCClient'

class AIEmbeddingFactory {
/* **************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
AIEmbeddingVector
} from '#Shared/API/AIEmbedding/AIEmbeddingTypes'
} from './AIEmbeddingTypes'

type AIEmbeddingVectorListItem = Array<{
id: any
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
AILanguageDetectorCloneOptions,
AILanguageDetectorDetectOptions,
AILanguageDetectorDetectResult
} from '#Shared/API/AILanguageDetector/AILanguageDetectorTypes'
import { kLanguageDetectorCreate, kLanguageDetectorDetect } from '#Shared/API/AILanguageDetector/AILanguageDetectorIPCTypes'
import { kSessionDestroyed } from '#Shared/Errors'
} from './AILanguageDetectorTypes'
import { kLanguageDetectorCreate, kLanguageDetectorDetect } from './AILanguageDetectorIPCTypes'
import { kSessionDestroyed } from '../../Errors'
import AIRootModel from '../AIRootModel'
import IPCClient from '#Shared/IPC/IPCClient'
import IPCClient from '../../IPC/IPCClient'

class AILanguageDetector extends AIRootModel {
/* **************************************************************************/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AILanguageDetectorCapabilitiesData } from '#Shared/API/AILanguageDetector/AILanguageDetectorTypes'
import { AILanguageDetectorCapabilitiesData } from './AILanguageDetectorTypes'
import AIRootModelCapabilities from '../AIRootModelCapabilities'

class AILanguageDetectorCapabilities extends AIRootModelCapabilities {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ import {
AILanguageDetectorCreateOptions,
AILanguageDetectorData,
AILanguageDetectorCapabilitiesData
} from '#Shared/API/AILanguageDetector/AILanguageDetectorTypes'
} from './AILanguageDetectorTypes'
import {
kLanguageDetectorGetCapabilities,
kLanguageDetectorCreate
} from '#Shared/API/AILanguageDetector/AILanguageDetectorIPCTypes'
import { throwIPCErrorResponse } from '#Shared/IPC/IPCErrorHelper'
} from './AILanguageDetectorIPCTypes'
import { throwIPCErrorResponse } from '../../IPC/IPCErrorHelper'
import AILanguageDetectorCapabilities from './AILanguageDetectorCapabilities'
import AILanguageDetector from './AILanguageDetector'
import { kModelCreationAborted } from '#Shared/Errors'
import { kModelCreationAborted } from '../../Errors'
import { createDownloadProgressFn } from '../AIHelpers'
import IPCClient from '#Shared/IPC/IPCClient'
import IPCClient from '../../IPC/IPCClient'

class AILanguageDetectorFactory {
/* **************************************************************************/
Expand Down
Loading

0 comments on commit 05bbc8d

Please sign in to comment.