Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: move to byorg framework #60

Merged
merged 10 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ node_modules/
build/
dist/
website/
rsbuild.config.ts
vitest.config.ts
bin.js
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
uses: ./.github/actions/setup

- name: Run unit tests
run: yarn test --maxWorkers=2 --coverage
run: yarn test --coverage

build-library:
runs-on: ubuntu-latest
Expand Down
3 changes: 3 additions & 0 deletions bin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

import './dist/bin.cjs';
35 changes: 35 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# EXAMPLE USAGE:
#
# Refer for explanation to following link:
# https://github.com/evilmartians/lefthook/blob/master/docs/configuration.md
#
# pre-push:
# commands:
# packages-audit:
# tags: frontend security
# run: yarn audit
# gems-audit:
# tags: backend security
# run: bundle audit
#
# pre-commit:
# parallel: true
# commands:
# eslint:
# glob: "*.{js,ts,jsx,tsx}"
# run: yarn eslint {staged_files}
# rubocop:
# tags: backend style
# glob: "*.rb"
# exclude: '(^|/)(application|routes)\.rb$'
# run: bundle exec rubocop --force-exclusion {all_files}
# govet:
# tags: backend style
# files: git ls-files -m
# glob: "*.go"
# run: go vet {files}
# scripts:
# "hello.js":
# runner: node
# "any.go":
# runner: go run
28 changes: 15 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
"main": "build/index.js",
"source": "src/index.js",
"bin": {
"ai": "build/bin.js"
"ai": "./bin.js"
},
"files": [
"build",
"bin.js",
"dist",
"!**/__tests__",
"!**/__mocks__"
],
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch",
"test": "jest",
"build": "rsbuild build",
"test": "vitest run",
"typecheck": "tsc --noEmit",
"lint": "eslint \"**/*.{js,ts,tsx}\"",
"clean": "del-cli build",
"release": "release-it",
"ai": "chmod +x ./build/bin.js && ./build/bin.js"
"ai": "./bin.js"
},
"repository": {
"type": "git",
Expand All @@ -41,7 +41,11 @@
"registry": "https://registry.npmjs.org/"
},
"dependencies": {
"@ai-sdk/anthropic": "^0.0.54",
"@ai-sdk/mistral": "^0.0.46",
"@ai-sdk/openai": "^0.0.71",
"@anthropic-ai/sdk": "^0.26.1",
"@callstack/byorg-core": "0.1.2",
"@inkjs/ui": "^1.0.0",
"@mistralai/mistralai": "^1.0.2",
"chalk": "^5.3.0",
Expand All @@ -50,7 +54,6 @@
"ink": "^4.4.1",
"ink-link": "^3.0.0",
"ink-text-input": "^5.0.1",
"openai": "^4.56.0",
"prompts": "^2.4.2",
"react": "^18.2.0",
"redent": "^4.0.0",
Expand All @@ -63,32 +66,31 @@
"devDependencies": {
"@callstack/eslint-config": "^14.1.1",
"@release-it/conventional-changelog": "^5.1.1",
"@rsbuild/core": "^1.0.3",
"@types/jest": "^29.5.12",
"@types/mock-fs": "^4.13.4",
"@types/prompts": "^2.4.9",
"@types/react": "^18.2.63",
"@types/update-notifier": "^6.0.8",
"@vitest/coverage-v8": "^2.1.4",
"del-cli": "^5.1.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^9.1.0",
"eslint-import-resolver-typescript": "^3.6.1",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"jest": "^29.7.0",
"memfs": "^4.14.0",
"mock-fs": "^5.2.0",
"prettier": "^3.2.5",
"release-it": "^15.11.0",
"typescript": "^5.4.5"
"typescript": "^5.4.5",
"vitest": "^2.1.4"
},
"packageManager": "[email protected]",
"engines": {
"node": ">= 18.0.0"
},
"jest": {
"modulePathIgnorePatterns": [
"<rootDir>/build/"
]
},
"prettier": {
"quoteProps": "consistent",
"singleQuote": true,
Expand Down
34 changes: 34 additions & 0 deletions rsbuild.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { defineConfig } from '@rsbuild/core';

export default defineConfig({
dev: { progressBar: false },
source: {
entry: {
bin: './src/bin.ts',
},
tsconfigPath: './tsconfig.build.json',
},
output: {
target: 'node',
minify: false,
filename: {
js: '[name].cjs',
},
externals: ['react-devtools-core'],
},
tools: {
rspack: {
resolve: {
extensionAlias: {
'.js': ['.js', '.ts', '.tsx'],
},
},
ignoreWarnings: [
/require function is used in a way in which dependencies cannot be statically extracted/,
/the request of a dependency is an expression/,
/Can't resolve 'bufferutil'/,
/Can't resolve 'utf-8-validate'/,
],
},
},
});
142 changes: 61 additions & 81 deletions src/__tests__/file-utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,61 @@
/* eslint-disable jest/no-commented-out-tests */
it.todo('Move tests to AVA');

// import type { SessionContext } from '../commands/prompt/types.js';
// import { DEFAULT_SYSTEM_PROMPT } from '../default-config.js';
// import { getDefaultFilename, getUniqueFilename } from '../file-utils.js';
// import openAi from '../engine/providers/openAi.js';

// const mockFs = require('mock-fs');

// const mockContext: SessionContext = {
// config: {
// model: 'gpt-4',
// systemPrompt: DEFAULT_SYSTEM_PROMPT,
// apiKey: '***',
// },
// provider: openAi,
// messages: [],
// totalUsage: { inputTokens: 0, outputTokens: 0, requests: 0 },
// };

// // Allows for mocking the time on CLI
// Object.defineProperty(global, 'performance', {
// writable: true,
// });

// beforeAll(() => {
// jest.useFakeTimers();
// jest.setSystemTime(new Date(2020, 3, 1, 12, 0));
// });

// beforeEach(() => {
// mockFs({
// '/path/to/': {
// 'file.txt': 'Content',
// 'incr.txt': 'Content',
// 'incr-1.txt': 'Content',
// },
// });
// });

// // Restores the filesystem functions
// afterEach(() => {
// mockFs.restore();
// });

// afterAll(() => {
// jest.useRealTimers();
// });

// describe('getDefaultFilename', () => {
// it('should return the default file name', () => {
// // Test case 1
// const context = mockContext;
// context.messages.push({ role: 'user', content: 'Hi How Are You Doing Today?' });

// const defaultFilename = getDefaultFilename(context);

// expect(defaultFilename).toBe('2020-04-01 12-00 Hi How Are You Doing');
// });
// });

// describe('getUniqueFilename', () => {
// it('should return a unique file name', () => {
// const filePath = '/path/to/file.txt';
// const uniqueFilename = getUniqueFilename(filePath);
// expect(uniqueFilename).toMatch(`/path/to/file-1.txt`);
// });

// it('should properly increment the name', () => {
// const filePath = '/path/to/incr.txt';
// const uniqueFilename = getUniqueFilename(filePath);
// expect(uniqueFilename).toMatch(`/path/to/incr-2.txt`);
// });

// it('should not modify already unique name', () => {
// const filePath = '/path/to/another/file.txt';
// const uniqueFilename = getUniqueFilename(filePath);
// expect(uniqueFilename).toMatch(`/path/to/another/file.txt`);
// });
// });
import { Message } from '@callstack/byorg-core';
import { beforeAll, beforeEach, describe, expect, test, vi } from 'vitest';
import { fs, vol } from 'memfs';
import { getDefaultFilename, getUniqueFilename } from '../file-utils.js';

vi.mock('node:fs', () => fs);

beforeAll(() => {
vi.useFakeTimers();
vi.setSystemTime(new Date(2020, 3, 1, 12, 0));
return () => {
vi.useRealTimers();
};
});

beforeEach(() => {
vol.fromJSON(
{
'file.txt': 'Content',
'incr.txt': 'Content',
'incr-1.txt': 'Content',
},
'/path/to/',
);

return () => {
// // Restores the filesystem functions
vol.reset();
};
});

describe('getDefaultFilename', () => {
test('should return the default file name', () => {
// Test case 1
const messages: Message[] = [{ role: 'user', content: 'Hi How Are You Doing Today?' }];

const defaultFilename = getDefaultFilename(messages);

expect(defaultFilename).toBe('2020-04-01 12-00 Hi How Are You Doing');
});
});

describe('getUniqueFilename', () => {
test('should return a unique file name', () => {
const filePath = '/path/to/file.txt';
const uniqueFilename = getUniqueFilename(filePath);
expect(uniqueFilename).toMatch(`/path/to/file-1.txt`);
});

test('should properly increment the name', () => {
const filePath = '/path/to/incr.txt';
const uniqueFilename = getUniqueFilename(filePath);
expect(uniqueFilename).toMatch(`/path/to/incr-2.txt`);
});

test('should not modify already unique name', () => {
const filePath = '/path/to/another/file.txt';
const uniqueFilename = getUniqueFilename(filePath);
expect(uniqueFilename).toMatch(`/path/to/another/file.txt`);
});
});
1 change: 0 additions & 1 deletion src/__tests__/index.test.ts

This file was deleted.

12 changes: 8 additions & 4 deletions src/commands/chat/providers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import openAi from '../../engine/providers/open-ai.js';
import anthropic from '../../engine/providers/anthropic.js';
import perplexity from '../../engine/providers/perplexity.js';
import mistral from '../../engine/providers/mistral.js';
import { getProvider, type Provider, type ProviderName } from '../../engine/providers/provider.js';
import {
getProvider,
type ProviderInfo,
type ProviderName,
} from '../../engine/providers/provider-info.js';
import type { ConfigFile } from '../../config-file.js';

export const providerOptionMapping: Record<string, Provider> = {
export const providerOptionMapping: Record<string, ProviderInfo> = {
openai: openAi,
anthropic,
anth: anthropic,
Expand All @@ -16,7 +20,7 @@ export const providerOptionMapping: Record<string, Provider> = {

export const providerOptions = Object.keys(providerOptionMapping);

export function resolveProviderFromOption(providerOption: string): Provider {
export function resolveProviderInfoFromOption(providerOption: string): ProviderInfo {
const provider = providerOptionMapping[providerOption];
if (!provider) {
throw new Error(`Provider not found: ${providerOption}.`);
Expand All @@ -25,7 +29,7 @@ export function resolveProviderFromOption(providerOption: string): Provider {
return provider;
}

export function getDefaultProvider(config: ConfigFile): Provider {
export function getDefaultProviderInfo(config: ConfigFile): ProviderInfo {
const providerNames = Object.keys(config.providers) as ProviderName[];
const providerName = providerNames ? providerNames[0] : undefined;

Expand Down
Loading