Skip to content
Draft
Show file tree
Hide file tree
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
82 changes: 72 additions & 10 deletions pnpm-lock.yaml

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

25 changes: 25 additions & 0 deletions provider/excel-vba/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Excel VBA context provider for OpenCtx

This is a context provider for [OpenCtx](https://openctx.org) that lets you @-mention Excel files (`.xlsm`) and use their VBA scripts as context in code AI tools.

**Status:** experimental

## Usage

Add the following to your settings in any OpenCtx client:

```json
"openctx.providers": {
// ...other providers...
"https://openctx.org/npm/@openctx/provider-excel-vba": {
// TODO(sqs)
}
},
```


## Development

- [Source code](https://sourcegraph.com/github.com/sourcegraph/openctx/-/tree/provider/excel-vba)
- [Docs](https://openctx.org/docs/providers/excel-vba)
- License: Apache 2.0
18 changes: 18 additions & 0 deletions provider/excel-vba/extract_vba_code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import olefile
import sys

def extract_vba_code(file_path):
vba_code = ""
try:
with olefile.OleFileIO(file_path) as ole:
for stream_name in ole.listdir():
if stream_name[0] == 'VBA': # VBA code streams
vba_code += ole.openstream(stream_name).read().decode('utf-8', errors='ignore')
except Exception as e:
print(f"Error: {e}")
return vba_code

# Example usage
file_path = sys.argv[1]
code = extract_vba_code(file_path)
print(code)
83 changes: 83 additions & 0 deletions provider/excel-vba/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { execSync } from 'child_process'
import type {
ItemsParams,
ItemsResult,
MentionsParams,
MentionsResult,
MetaResult,
Provider,
} from '@openctx/provider'
import { readFile, readdir, writeFile } from 'fs/promises'
import JSZip from 'jszip'

/** Settings for the Google Docs OpenCtx provider. */
export type Settings = {
path: string
}

/**
* An [OpenCtx](https://openctx.org) provider that brings Google Docs context to code AI and
* editors.
*/
const excelVBA: Provider<Settings> = {
meta(): MetaResult {
return { name: 'Excel VBA', mentions: {} }
},

async mentions(params: MentionsParams, settings: Settings): Promise<MentionsResult> {
const files = (await readdir(settings.path, { recursive: true })).filter(
name => !name.startsWith('~$') && name.endsWith('.xlsm')
)
return (files ?? []).map((file: any) => ({
title: file,
uri: `file://${settings.path}/${file}`,
}))
},

async items(params: ItemsParams, settings: Settings): Promise<ItemsResult> {
if (!params.mention) {
return []
}

const modules = await extractVBAModules(params.mention.uri.replace(/^file:\/\//, ''))

return [
{
title: params.mention.title,
url: params.mention.uri,
ai: {
content: modules.join('\n\n'),
},
},
]
},
}

async function extractVBAModules(filePath: string): Promise<string[]> {
// Read the .xlsm file as a zip archive
const data = await readFile(filePath)
const zip = await JSZip.loadAsync(data)

// Get the entries in the zip file
const vbaModules: string[] = []
zip.forEach(async (relPath, zipEntry) => {
if (relPath.endsWith('/vbaProject.bin')) {
// Read the vbaProject.bin file
const vbaProjectBin = await zipEntry.async('nodebuffer')

const tmpFile = '/tmp/TMP1.ole'
await writeFile(tmpFile, vbaProjectBin)

// exec extract_vba_code.py
const stdout = execSync(
'python /Users/sqs/src/github.com/sourcegraph/openctx/provider/excel-vba/extract_vba_code.py ' +
tmpFile
)
vbaModules.push(stdout.toString())
}
})

return vbaModules
}

export default excelVBA
29 changes: 29 additions & 0 deletions provider/excel-vba/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "@openctx/provider-excel-vba",
"version": "0.0.10",
"description": "Excel VBA context for code AI and editors (OpenCtx provider)",
"license": "Apache-2.0",
"repository": {
"type": "git",
"url": "https://github.com/sourcegraph/openctx",
"directory": "provider/excel-vba"
},
"type": "module",
"main": "dist/bundle.js",
"types": "dist/index.d.ts",
"files": [
"dist/bundle.js",
"dist/index.d.ts"
],
"sideEffects": false,
"scripts": {
"bundle": "tsc --build && esbuild --log-level=error --platform=node --bundle --format=esm --outfile=dist/bundle.js index.ts",
"prepublishOnly": "tsc --build --clean && npm run --silent bundle",
"test": "vitest"
},
"dependencies": {
"@openctx/provider": "workspace:*",
"jszip": "^3.10.1",
"xlsx": "^0.18.5"
}
}
11 changes: 11 additions & 0 deletions provider/excel-vba/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../.config/tsconfig.base.json",
"compilerOptions": {
"rootDir": ".",
"outDir": "dist",
"lib": ["ESNext"],
},
"include": ["*.ts"],
"exclude": ["dist", "vitest.config.ts"],
"references": [{ "path": "../../lib/provider" }],
}
3 changes: 3 additions & 0 deletions provider/excel-vba/vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { defineConfig } from 'vitest/config'

export default defineConfig({})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
{ "path": "client/vscode/test/integration" },
{ "path": "client/web-playground" },
{ "path": "provider/devdocs" },
{ "path": "provider/excel-vba" },
{ "path": "provider/google-docs" },
{ "path": "provider/hello-world" },
{ "path": "provider/links" },
Expand Down
Loading