Skip to content
Merged
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
1 change: 1 addition & 0 deletions .env.test
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OPENAI_API_KEY=sk-mockapikey1234
39 changes: 39 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Build

on:
push:
branches: [main]
pull_request:
branches: [main]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Bun
uses: oven-sh/setup-bun@v2

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.bun/install/cache
key: bun-${{ hashFiles('**/bun.lockb') }}

- name: Install dependencies
run: bun install --frozen-lockfile

- name: Run linter
run: bun run lint

- name: Run tests
run: bun test

- name: Build project
run: bun run build
38 changes: 38 additions & 0 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Release

on:
release:
types: [published]

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Bun
uses: oven-sh/setup-bun@v2

- name: Install dependencies
run: bun install --frozen-lockfile

- name: Build
run: bun run build

- name: Publish package
env:
NPM_CONFIG_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
# Extract version from tag (remove 'v' prefix if present)
VERSION=${GITHUB_REF_NAME#v}

# Update package.json version
bun x json -I -f package.json -e "this.version='$VERSION'"

# Publish with appropriate tag
if [[ "${{ github.event.release.prerelease }}" == "true" ]]; then
bun publish --access public --tag canary
else
bun publish --access public
fi
Comment thread
thiagozf marked this conversation as resolved.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ out
# Nuxt.js build / generate output

.nuxt
dist
dist/

# Gatsby files

Expand Down
65 changes: 33 additions & 32 deletions biome.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
{
"$schema": "https://biomejs.dev/schemas/2.1.1/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"ignoreUnknown": false
},
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
},
"assist": {
"enabled": true,
"actions": {
"source": {
"organizeImports": "on"
}
}
}
"$schema": "https://biomejs.dev/schemas/2.1.1/schema.json",
"vcs": {
"enabled": false,
"clientKind": "git",
"useIgnoreFile": false
},
"files": {
"includes": ["**", "!**/*.test.ts", "!tests", "!dist", "!node_modules"],
"ignoreUnknown": false
},
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"javascript": {
"formatter": {
"quoteStyle": "single"
}
},
"assist": {
"enabled": true,
"actions": {
"source": {
"organizeImports": "on"
}
}
}
}
10 changes: 3 additions & 7 deletions migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,8 @@
"name": "snippets_library_libraries_name_fk",
"tableFrom": "snippets",
"tableTo": "libraries",
"columnsFrom": [
"library"
],
"columnsTo": [
"name"
],
"columnsFrom": ["library"],
"columnsTo": ["name"],
"onDelete": "cascade",
"onUpdate": "no action"
}
Expand All @@ -179,4 +175,4 @@
"internal": {
"indexes": {}
}
}
}
10 changes: 3 additions & 7 deletions migrations/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,9 @@
"snippets_library_libraries_name_fk": {
"name": "snippets_library_libraries_name_fk",
"tableFrom": "snippets",
"columnsFrom": [
"library"
],
"columnsFrom": ["library"],
"tableTo": "libraries",
"columnsTo": [
"name"
],
"columnsTo": ["name"],
"onUpdate": "no action",
"onDelete": "cascade"
}
Expand All @@ -179,4 +175,4 @@
"internal": {
"indexes": {}
}
}
}
2 changes: 1 addition & 1 deletion migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
"breakpoints": true
}
]
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "libcontext",
"version": "0.0.1",
"version": "0.0.0",
"main": "./dist/index.js",
"type": "module",
"bin": {
Expand All @@ -15,6 +15,7 @@
"scripts": {
"dev": "bun src/index.ts",
"inspect": "fastmcp inspect src/mcp/index.ts",
"lint": "bun biome check",
"build": "bun build.ts",
"db:push": "drizzle-kit push",
"db:generate": "drizzle-kit generate",
Expand Down
2 changes: 1 addition & 1 deletion src/commands/list.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { list as handler } from '@libcontext/handlers/list';
import type { Command } from '@libcontext/types';

export const list: Command<{}> = {
export const list: Command = {
command: 'list',
description: 'List libraries added to the server',
handler: async () => {
Expand Down
2 changes: 1 addition & 1 deletion src/db/migrations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import path from 'node:path';
import { rootPath } from '@libcontext/utils/package';
import { migrate as execute } from 'drizzle-orm/libsql/migrator';
import path from 'path';
import { db } from './db';
import { library } from './schema';

Expand Down
5 changes: 3 additions & 2 deletions src/db/settings.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { mkdirSync } from 'node:fs';
import { name } from '@libcontext/constants';
import { paths } from '@libcontext/utils/paths';
import { mkdirSync } from 'fs';

if (process.env.NODE_ENV !== 'test') {
try {
mkdirSync(paths.data, { recursive: true });
} catch (e) {
} catch (error) {
console.error(
`Permission error to create data folder at "${paths.data}". Please, create this folder manually and make sure this app has write permission.`,
error,
);
process.exit(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/handlers/get.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { type GetOptions, get } from './get';

describe('get', () => {
const lib = stubs.libraries['org1/repo1'];
const embed = mock(async (text: string) =>
const embed = mock(async () =>
Array.from({ length: 1536 }, (_, i) => i * 0.001),
);
Comment thread
thiagozf marked this conversation as resolved.
let mocks: MockResult[] = [];
Expand Down
35 changes: 20 additions & 15 deletions src/handlers/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@ export interface GetOptions {
k: number;
}

const similarity = async ({ name, topic, k }: GetOptions) => {
const vector = await embed(topic!);
const all = async ({ name, k }: GetOptions) => {
return await db
.select()
.from(snippet)
.where(eq(snippet.library, name))
.limit(k);
};

const similarity = async ({
name,
topic,
k,
}: GetOptions & { topic: string }) => {
const vector = await embed(topic);
return db
.select({
title: snippet.title,
Expand All @@ -24,21 +36,14 @@ const similarity = async ({ name, topic, k }: GetOptions) => {
.where(eq(snippet.library, name));
};

const all = async ({ name, k }: GetOptions) => {
return await db
.select()
.from(snippet)
.where(eq(snippet.library, name))
.limit(k);
};

export const get = async (options: GetOptions) => {
export const get = async ({ name, topic, k }: GetOptions) => {
try {
const query = options.topic ? similarity : all;
const results = await query(options);
const results = topic
? await similarity({ name, topic, k })
: await all({ name, topic, k });
Comment thread
thiagozf marked this conversation as resolved.

if (results.length === 0) {
return `No snippets found for library "${options.name}"${options.topic ? ` with topic: "${options.topic}"` : ''}`;
return `No snippets found for library "${name}"${topic ? ` with topic: "${topic}"` : ''}`;
}

const snippets = results.map(
Expand All @@ -51,7 +56,7 @@ ${snippet.code}
\`\`\``,
);
return snippets.join('\n----------------------------------------\n');
} catch (error) {
} catch (_error) {
return 'Failed to retrieve snippets';
}
};
2 changes: 1 addition & 1 deletion src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Argv } from 'yargs';

export interface Command<T = {}> {
export interface Command<T = never> {
command: string;
description: string;
builder?: (yargs: Argv) => Argv<T>;
Expand Down
6 changes: 3 additions & 3 deletions src/utils/package.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import fs, { readFileSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import fs, { readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
Expand Down
6 changes: 3 additions & 3 deletions src/utils/paths.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os from 'node:os';
import path from 'node:path';
import process from 'node:process';
import { name } from '@libcontext/constants';
import os from 'os';
import path from 'path';
import process from 'process';

const homedir = os.homedir();
const tmpdir = os.tmpdir();
Expand Down
6 changes: 3 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"noUnusedParameters": false,
"noPropertyAccessFromIndexSignature": false,
"paths": {
"@libcontext/*": ["./src/*"],
},
},
"@libcontext/*": ["./src/*"]
}
}
}