Skip to content

Commit

Permalink
Implement tests for the subscription check
Browse files Browse the repository at this point in the history
  • Loading branch information
blackforestboi committed May 13, 2024
1 parent 49200c6 commit 02e9ce6
Show file tree
Hide file tree
Showing 7 changed files with 430 additions and 87 deletions.
8 changes: 5 additions & 3 deletions src/content-scripts/content_script/global.ts
Expand Up @@ -121,7 +121,7 @@ import {
} from '@worldbrain/memex-common/lib/annotations/utils'
import {
COUNTER_STORAGE_KEY,
DEFAULT_COUNTER_STORAGE_KEY,
DEFAULT_COUNTER_STORAGE_VALUE,
} from '@worldbrain/memex-common/lib/subscriptions/constants'
import type { RemoteSearchInterface } from 'src/search/background/types'
import * as anchoring from '@worldbrain/memex-common/lib/annotations'
Expand Down Expand Up @@ -1495,7 +1495,8 @@ export async function main(
subscriptionBefore[COUNTER_STORAGE_KEY]

const subscriptionsBefore =
subscriptionDataBefore?.pU ?? DEFAULT_COUNTER_STORAGE_KEY.pU
subscriptionDataBefore?.pU ??
DEFAULT_COUNTER_STORAGE_VALUE.pU

await sleepPromise(1000)
await runInBackground<
Expand All @@ -1509,7 +1510,8 @@ export async function main(
subscriptionAfter[COUNTER_STORAGE_KEY]

const subscriptionsAfter =
subscriptionDataAfter?.pU ?? DEFAULT_COUNTER_STORAGE_KEY.pU
subscriptionDataAfter?.pU ??
DEFAULT_COUNTER_STORAGE_VALUE.pU

let keyChanged = false
for (const key in subscriptionsBefore) {
Expand Down
6 changes: 1 addition & 5 deletions src/custom-lists/background/storage.ts
Expand Up @@ -920,11 +920,7 @@ export default class CustomListStorage extends StorageModule {
await trackSpaceEntryCreate(analyticsBG, {
type: isShared ? 'shared' : 'private',
})
} catch (error) {
console.error(
`Error tracking space Entry create event', ${error}`,
)
}
} catch (error) {}
}

if (isPkmSyncEnabled({ storageAPI: this.options.___storageAPI })) {
Expand Down
Expand Up @@ -1214,8 +1214,6 @@ export class AnnotationsSidebar extends React.Component<
})
}

console.log('listAnnotations', !annotationsData?.length)

return (
<FollowedNotesContainer zIndex={parseFloat(listData.unifiedId)}>
{(cacheUtils.deriveListOwnershipStatus(
Expand Down
74 changes: 0 additions & 74 deletions src/sidebar/annotations-sidebar/containers/logic.ts
Expand Up @@ -3349,80 +3349,6 @@ export class SidebarContainerLogic extends UILogic<
}
}

// saveAIPrompt: EventHandler<'saveAIPrompt'> = async ({
// event,
// previousState,
// }) => {
// this.emitMutation({
// showAISuggestionsDropDown: { $set: true },
// })
// let suggestions = this.AIpromptSuggestions

// let newSuggestion = { prompt: event.prompt, focused: null }

// suggestions.unshift(newSuggestion)

// const newSuggestionsToSave = suggestions.map((item) => item.prompt)

// await this.syncSettings.openAI.set(
// 'promptSuggestions',
// newSuggestionsToSave,
// )

// this._updateFocusAISuggestions(-1, suggestions)

// this.AIpromptSuggestions = suggestions
// }

// toggleAISuggestionsDropDown: EventHandler<
// 'toggleAISuggestionsDropDown'
// > = async ({ event, previousState }) => {
// if (previousState.showAISuggestionsDropDown) {
// this._updateFocusAISuggestions(-1, previousState.AIsuggestions)
// this.emitMutation({
// showAISuggestionsDropDown: {
// $set: false,
// },
// })
// return
// }

// const rawSuggestions = await this.syncSettings.openAI.get(
// 'promptSuggestions',
// )

// let suggestions = []

// if (!rawSuggestions) {
// await this.syncSettings.openAI.set(
// 'promptSuggestions',
// AI_PROMPT_DEFAULTS,
// )

// suggestions = AI_PROMPT_DEFAULTS.map((prompt: string) => {
// return { prompt, focused: null }
// })
// } else {
// suggestions = rawSuggestions.map((prompt: string) => ({
// prompt,
// focused: null,
// }))
// }

// this.emitMutation({
// showAISuggestionsDropDown: {
// $set: !previousState.showAISuggestionsDropDown,
// },
// })

// if (!previousState.showAISuggestionsDropDown) {
// this.emitMutation({
// AIsuggestions: { $set: suggestions },
// })
// }
// this.AIpromptSuggestions = suggestions
// }

private _updateFocusAISuggestions = (
focusIndex: number | undefined,
displayEntries?: { prompt: string; focused: boolean }[],
Expand Down
31 changes: 30 additions & 1 deletion src/summarization-llm/background/index.ts
Expand Up @@ -15,7 +15,10 @@ import {
import { SidebarTab } from 'src/sidebar/annotations-sidebar/containers/types'
import browser, { Browser } from 'webextension-polyfill'
import { COUNTER_STORAGE_KEY } from '@worldbrain/memex-common/lib/subscriptions/constants'
import { AIActionAllowed } from '@worldbrain/memex-common/lib/subscriptions/storage'
import {
AIActionAllowed,
updateTabAISessions,
} from '@worldbrain/memex-common/lib/subscriptions/storage'

export interface SummarizationInterface<Role extends 'provider' | 'caller'> {
startPageSummaryStream: RemoteFunction<
Expand Down Expand Up @@ -75,6 +78,30 @@ export default class SummarizeBackground {
makeRemotelyCallable(this.remoteFunctions, { insertExtraArg: true })
}

async saveActiveTabId(hasKey: boolean, AImodel: AImodels) {
const isAlreadySaved = await updateTabAISessions(
this.options.browserAPIs,
)

if (isAlreadySaved) {
return
} else {
await AIActionAllowed(
this.options.browserAPIs,
this.options.analyticsBG,
hasKey,
true,
AImodel,
)
}

this.options.browserAPIs.tabs.onRemoved.addListener(this.onTabClosed)
}

onTabClosed = (tabId, removeInfo) => {
updateTabAISessions(this.options.browserAPIs, tabId)
}

startPageSummaryStream: SummarizationInterface<
'provider'
>['startPageSummaryStream'] = async (
Expand All @@ -90,6 +117,8 @@ export default class SummarizeBackground {
promptData,
},
) => {
await this.saveActiveTabId(apiKey?.length > 0, AImodel)

let isAllowed = null

isAllowed = await AIActionAllowed(
Expand Down

0 comments on commit 02e9ce6

Please sign in to comment.