Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sqs committed Dec 26, 2023
1 parent 3ca366e commit 631cd21
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 4 deletions.
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions provider/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ TODOs:
- make it slurp up gdocs/confluence/markdown in repos
- show OCG annotations (but in a way that doesn't overlay lines in the file, is more passive?)
- show a demo of Cody working with this
- show docs most relevant to the current visible portion or the selection, not the whole file
1 change: 1 addition & 0 deletions provider/docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@mozilla/readability": "^0.5.0",
"@opencodegraph/provider": "workspace:*",
"@xenova/transformers": "^2.12.1",
"better-localstorage": "^1.0.5",
"buffer": "^6.0.3",
"env-paths": "^3.0.0",
"jsdom": "^23.0.1",
Expand Down
36 changes: 36 additions & 0 deletions provider/docs/src/corpus/cache/indexedDB.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/// <reference lib="dom" />

import IndexedDBStorage from 'better-localstorage'
import { type ContentID, type CorpusCache } from './cache'

/**
* Create a {@link CorpusCache} that stores cache data using IndexedDB.
*/
export function createIndexedDBCorpusCache(keyPrefix: string): CorpusCache {
const storage = new IndexedDBStorage(keyPrefix)

function storageKey(contentID: ContentID, key: string): string {
return `${keyPrefix}:${contentID}:${key}`
}

return {
async get(contentID, key) {
const k = storageKey(contentID, key)
const data = await storage.getItem(k)
try {
return data ?? null
} catch (error) {
// TODO(sqs): cast because https://github.com/dreamsavior/Better-localStorage/pull/1
await (storage as any).delete(k)
throw error
}
},
async set(contentID, key, value) {
try {
await storage.setItem(storageKey(contentID, key), value)
} catch (error) {
console.error(`failed to store data for ${contentID}:${key}`, error)
}
},
}
}
2 changes: 1 addition & 1 deletion provider/docs/src/corpus/cache/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { type ContentID, type CorpusCache } from './cache'

/**
* Create a {@link CorpusCache} that stores cache data localStorage (using the Web Storage API).
* Create a {@link CorpusCache} that stores cache data in localStorage (using the Web Storage API).
*/
export function createWebStorageCorpusCache(storage: Storage, keyPrefix: string): CorpusCache {
function storageKey(contentID: ContentID, key: string): string {
Expand Down
3 changes: 1 addition & 2 deletions provider/docs/src/corpus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ export async function indexCorpus(
const indexedDocs: IndexedDoc[] = []

for (const doc of data.docs) {
const USE_NOOP_CACHE = typeof window !== 'undefined' // TODO(sqs): dont cache in browser
const content = await cachedExtractContent(USE_NOOP_CACHE ? noopCache : cache, contentExtractor, doc)
const content = await cachedExtractContent(cache, contentExtractor, doc)

const chunks = chunk(content?.content ?? doc.text, { isMarkdown: doc.text.includes('##') })

Expand Down
7 changes: 6 additions & 1 deletion provider/docs/src/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
type CapabilitiesResult,
} from '@opencodegraph/provider'
import { indexCorpus } from '../corpus'
import { createIndexedDBCorpusCache } from '../corpus/cache/indexedDB'
import { createWebStorageCorpusCache } from '../corpus/cache/localStorage'
import { corpusData } from '../corpus/data'
import { extractContentUsingMozillaReadability } from '../corpus/doc/contentExtractor'
Expand All @@ -25,7 +26,11 @@ export interface Settings {
}

const CORPUS_CACHE =
typeof localStorage !== 'undefined' ? createWebStorageCorpusCache(localStorage, 'ocg-provider-docs') : undefined
typeof indexedDB !== 'undefined'
? createIndexedDBCorpusCache('ocg-provider-docs')
: typeof localStorage !== 'undefined'
? createWebStorageCorpusCache(localStorage, 'ocg-provider-docs')
: undefined

/**
* An [OpenCodeGraph](https://opencodegraph.org) provider that adds contextual documentation to your
Expand Down

0 comments on commit 631cd21

Please sign in to comment.