Skip to content

Commit

Permalink
add build and lint workflow in CI, fix lint errors
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBoyle committed Jun 20, 2024
1 parent b888395 commit edbec5c
Show file tree
Hide file tree
Showing 22 changed files with 1,531 additions and 85 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: CI
on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
lint:
name: lint
runs-on: ubuntu-latest
if: github.repository == 'cortexclick/cortex-sdk'

steps:
- uses: actions/checkout@v4

- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Install dependencies
run: npm install

- name: Check types
run: npm run lint
build:
name: build
runs-on: ubuntu-latest
if: github.repository == 'cortexclick/cortex-sdk'

steps:
- uses: actions/checkout@v4

- name: Set up Node
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Build
run: npm run build
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
*.js
*.js
node_modules

!eslint.config.js
4 changes: 4 additions & 0 deletions api-client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
type Method = "POST" | "GET" | "PUT" | "DELETE";

// body can be any object
/* eslint-disable @typescript-eslint/no-explicit-any */


export class CortexApiClient {
constructor(private org: string, private apiUrl: string, private accessToken: string) { }

Expand Down
2 changes: 1 addition & 1 deletion catalog.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { expect, test, beforeEach, afterEach } from 'vitest'
import { expect, test, afterEach } from 'vitest'

Check failure on line 1 in catalog.test.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module 'vitest' or its corresponding type declarations.
import { CortexClient } from "./index";
import { Catalog, CatalogConfig } from "./catalog";

Expand Down
13 changes: 7 additions & 6 deletions catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export class Catalog {
throw new Error(`Failed to list catalog: ${res.statusText}`);
}
const body = await res.json();
for(let catalog of body.catalogs) {
for(const catalog of body.catalogs) {
result.push({
name: catalog.name,
description: catalog.description,
Expand Down Expand Up @@ -111,7 +111,7 @@ export class Catalog {
let hasText = false;
let hasFile = false;
let hasJson = false;
for(let doc of batch) {
for(const doc of batch) {
switch(doc.contentType) {
case "markdown":
case "text":
Expand All @@ -124,6 +124,7 @@ export class Catalog {
hasFile = true;
break;
default:
// eslint-disable-next-line @typescript-eslint/no-explicit-any
throw new Error(`unsupported content type: ${(doc as any).contentType}`);
}
}
Expand Down Expand Up @@ -172,7 +173,7 @@ export class Catalog {
}

async listDocuments(paginationOpts?: DocumentPaginationOpts): Promise<DocumentListResult> {
let { page, pageSize } = paginationOpts || { page: 1, pageSize: 50};
const { page, pageSize } = paginationOpts || { page: 1, pageSize: 50};
const nextPageOpts: DocumentPaginationOpts = { page: page+1, pageSize };
const nextPage = () => {
return this.listDocuments(nextPageOpts);
Expand All @@ -192,6 +193,7 @@ export class Catalog {
};
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
jsonIndexer(documents: any[], opts?: JSONIndexerOpts) {
return new JSONIndexer(this, documents, opts);
}
Expand All @@ -218,9 +220,8 @@ export class Catalog {
const mapBatch = async (batch: DocumentBatch) => {
const blobs: Promise<Blob>[] = [];
const documents: DocumentInput[] = [];
let isFileUpload = false;

for(let doc of batch) {
for(const doc of batch) {
switch(doc.contentType) {
case "markdown":
case "text":
Expand All @@ -235,14 +236,14 @@ const mapBatch = async (batch: DocumentBatch) => {
});
break;
case "file":
isFileUpload = true;
blobs.push(fs.openAsBlob(doc.filePath));
documents.push({
...doc,
content: undefined,
});
break;
default:
// eslint-disable-next-line @typescript-eslint/no-explicit-any
throw new Error(`unsupported content type: ${(doc as any).contentType}`);
}
}
Expand Down
12 changes: 6 additions & 6 deletions chat.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'

Check failure on line 1 in chat.test.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module 'vitest' or its corresponding type declarations.
import { CortexClient, TextDocument } from "./index";
import { Catalog, CatalogConfig } from "./catalog";
import { CatalogConfig } from "./catalog";
import { Readable } from "stream";

Check failure on line 4 in chat.test.ts

View workflow job for this annotation

GitHub Actions / build

Cannot find module 'stream' or its corresponding type declarations.

const client = new CortexClient({
Expand All @@ -27,7 +27,7 @@ test('e2e catalog, cortex, and sync chat', { timeout: 60000 }, async () => {
};

// create
let catalog = await client.configureCatalog(catalogName, config);
const catalog = await client.configureCatalog(catalogName, config);

const documents: TextDocument[] = [
{
Expand Down Expand Up @@ -64,10 +64,10 @@ test('e2e catalog, cortex, and sync chat', { timeout: 60000 }, async () => {

// get chat
const getChatRes = await client.getChat(chat.id);
expect(chat.messages.length).toBe(2);
expect(chat.title).toBe(chatInput)
expect(getChatRes.messages.length).toBe(2);
expect(getChatRes.title).toBe(chatInput)

const response = await chat.respond({ message: "what about customer verticals" });
await chat.respond({ message: "what about customer verticals" });
expect(chat.messages.length).toBe(4);


Expand Down Expand Up @@ -95,7 +95,7 @@ test('streaming chat', { timeout: 60000 }, async () => {
};

// create
let catalog = await client.configureCatalog(catalogName, config);
const catalog = await client.configureCatalog(catalogName, config);

const documents: TextDocument[] = [
{
Expand Down
6 changes: 0 additions & 6 deletions chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ export type Message = {
message: string;
};

export type ListChatsResult = {};

export class Chat {
private constructor(private apiClient: CortexApiClient, readonly id: string, readonly title: string, readonly messages: Message[]) {
}
Expand Down Expand Up @@ -116,10 +114,6 @@ export class Chat {
return new Chat(client, id, body.title, body.messages);
}

static async list(): Promise<ListChatsResult[]> {
return [];
}

async respond(opts: RespondChatOptsSync): Promise<string>;
async respond(opts: RespondChatOptsStreaming): Promise<StreamingChatResult>;
async respond(opts: RespondChatOptsSync | RespondChatOptsStreaming): Promise<string | StreamingChatResult> {
Expand Down
4 changes: 2 additions & 2 deletions content.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ test('e2e catalog, cortex, and sync content generation workflow', {timeout: 1200
};

// create
let catalog = await client.configureCatalog(catalogName, config);
const catalog = await client.configureCatalog(catalogName, config);

const documents: TextDocument[] = [
{
Expand Down Expand Up @@ -124,7 +124,7 @@ test('test streaming content', {timeout: 120000}, async () => {
};

// create
let catalog = await client.configureCatalog(catalogName, config);
const catalog = await client.configureCatalog(catalogName, config);

const documents: TextDocument[] = [
{
Expand Down
3 changes: 0 additions & 3 deletions content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,6 @@ export class Content {
static async create(opts: CreateContentOptsSync): Promise<Content>;
static async create(opts: CreateContentOptsStreaming): Promise<StreamingContentResult>;
static async create(opts: CreateContentOptsSync | CreateContentOptsStreaming): Promise<Content | StreamingContentResult> {
const { client, cortex, title, prompt, stream} = opts;

// note: this if statement is annoying but is necessary to appropriately narrow the return type
if(isCreateContentOptsSync(opts)) {
return this.createContentSync(opts);
Expand Down Expand Up @@ -187,7 +185,6 @@ export class Content {
const reader = res.body!.getReader();
const decoder = new TextDecoder('utf-8');

const id: string = res.headers.get("id") || "";
const version: number = parseInt(res.headers.get("version") || "0");
const commands: ContentCommand[] = JSON.parse(res.headers.get("commands") || "[]");
this._version = version;
Expand Down
2 changes: 1 addition & 1 deletion cortex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ test('can get and set OrgConfig', async () => {
rules: ["never say anything disparaging about AI or LLMs", "do not offer discounts"],
}

const orgConfig = await client.configureOrg(orgConfigOpts);
await client.configureOrg(orgConfigOpts);

const getOrgConfig = await client.getOrgConfig();
expect(getOrgConfig.companyName).toBe(orgConfigOpts.companyName)
Expand Down
14 changes: 7 additions & 7 deletions document.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from 'vitest'
import { CortexClient } from "./index";
import { Catalog, CatalogConfig } from "./catalog";
import { CatalogConfig } from "./catalog";
import { FileDocument, JSONDocument, TextDocument } from './document';

const client = new CortexClient({
Expand All @@ -19,7 +19,7 @@ test('Test upsertDocuments inline text batch', async () => {
instructions: ["a", "b"],
};

let catalog = await client.configureCatalog(catalogName, config);
const catalog = await client.configureCatalog(catalogName, config);

const docs: TextDocument[] = [
{
Expand Down Expand Up @@ -55,7 +55,7 @@ test('Test upsertDocuments inline JSON batch', async () => {
instructions: ["a", "b"],
};

let catalog = await client.configureCatalog(catalogName, config);
const catalog = await client.configureCatalog(catalogName, config);

const docs: JSONDocument[] = [
{
Expand Down Expand Up @@ -97,7 +97,7 @@ test('Test upsertDocuments with files and catalog.truncate', { timeout: 20000 },
instructions: ["a", "b"],
};

let catalog = await client.configureCatalog(catalogName, config);
const catalog = await client.configureCatalog(catalogName, config);

const docs: FileDocument[] = [
{
Expand Down Expand Up @@ -138,7 +138,7 @@ test('Test update documents', {timeout: 10000}, async () => {
instructions: ["a", "b"],
};

let catalog = await client.configureCatalog(catalogName, config);
const catalog = await client.configureCatalog(catalogName, config);

const docs: TextDocument[] = [
{
Expand Down Expand Up @@ -185,7 +185,7 @@ test('Test get and delete documents', {timeout: 10000}, async () => {
instructions: ["a", "b"],
};

let catalog = await client.configureCatalog(catalogName, config);
const catalog = await client.configureCatalog(catalogName, config);

const docs: TextDocument[] = [
{
Expand Down Expand Up @@ -234,7 +234,7 @@ test('Test catalog.listDocuments', { timeout: 10000 } ,async () => {
instructions: ["a", "b"],
};

let catalog = await client.configureCatalog(catalogName, config);
const catalog = await client.configureCatalog(catalogName, config);

const docs: JSONDocument[] = [
];
Expand Down
14 changes: 14 additions & 0 deletions eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import globals from "globals";
import pluginJs from "@eslint/js";
import tseslint from "typescript-eslint";


export default [
{files: ["**/*.{ts}"]},
{languageOptions: { globals: globals.node }},
...tseslint.configs.recommended,
{ rules: {
"@typescript-eslint/no-unused-vars": 0
}},
{ignores: ["**/*.js"]}
];
7 changes: 2 additions & 5 deletions indexers/directory-indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import * as fs from 'node:fs';
import { Catalog } from "../catalog.js";
import { FileDocument } from '../document.js';

const apiUrl = process.env.HOST === "prod" ? "https://api.cortexclick.com" : "http://localhost:3001"

export type DirectoryIndexerOpts = {
rootDir: string;
urlBase?: string;
Expand All @@ -15,7 +13,6 @@ export type DirectoryIndexerOpts = {
}

export class DirectoryIndexer {
private outstandingRequests: Promise<any>[] = [];
private parallelism = 25;
private rootDir: string;
private urlBase?: string;
Expand Down Expand Up @@ -55,9 +52,9 @@ export class DirectoryIndexer {
}

private async processDirectory(docPathList: string[], sitePathList: string[]) {
let fileList = fs.readdirSync(docPathList.join("/"));
const fileList = fs.readdirSync(docPathList.join("/"));

for (let f of fileList) {
for (const f of fileList) {
const path = [...docPathList, f].join("/")
const stat = fs.lstatSync(path)
if (stat.isDirectory() && this.includeDirectory(f)) {
Expand Down
4 changes: 2 additions & 2 deletions indexers/indexers.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { expect, test, beforeEach, afterEach } from 'vitest'
import { CortexClient } from "../index";
import { Catalog, CatalogConfig } from "../catalog";
import { FileDocument, JSONDocument, TextDocument } from '../document';
import { JSONDocument } from '../document';

const client = new CortexClient({
accessToken: process.env.CORTEX_ACCESS_TOKEN || "",
Expand All @@ -17,7 +17,7 @@ beforeEach(async () => {
instructions: ["a", "b"],
};

let catalogName = `catalog-${Math.floor(Math.random() * 10000)}`;
const catalogName = `catalog-${Math.floor(Math.random() * 10000)}`;
catalog = await client.configureCatalog(catalogName, config);
});

Expand Down
Loading

0 comments on commit edbec5c

Please sign in to comment.