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

Add built-in CLI #36

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Check out the [linked project](https://github.com/zAlweNy26/ts-cat/projects?quer
- [x] Supports streaming both in WebSocket and HTTP
- [x] Tokens usage visible through model interactions
- [x] Cache support for LLM and Embedder responses
- [ ] Built-in CLI
- [x] Built-in CLI
- [ ] External plugins registry support
- [ ] Add multi-modality support
- [ ] Add multi-agent support
Expand Down
1 change: 1 addition & 0 deletions bun.lock
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@smithy/util-utf8": "3.0.0",
"cheerio": "1.0.0",
"chokidar": "^4.0.3",
"citty": "0.1.6",
"consola": "^3.4.0",
"console-table-printer": "^2.12.1",
"croner": "9.0.0",
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"ci": "bun run typecheck && bun run lint && bun run test",
"dev": "WATCH=true bun --hot run ./src/index.ts",
"start": "bun run ./src/index.ts",
"cli": "bun run ./src/cli.ts",
"prepare": "bun run .husky/install.mjs",
"predocs": "typedoc",
"docs:dev": "bun run predocs && vitepress dev docs",
Expand Down Expand Up @@ -79,6 +80,7 @@
"@smithy/util-utf8": "3.0.0",
"cheerio": "1.0.0",
"chokidar": "^4.0.3",
"citty": "0.1.6",
"consola": "^3.4.0",
"console-table-printer": "^2.12.1",
"croner": "9.0.0",
Expand Down
91 changes: 91 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#!/usr/bin/env bun

import type { App } from './main'
import { treaty } from '@elysiajs/eden'
import { type ArgsDef, defineCommand, runMain, showUsage } from 'citty'
import pkg from '~/package.json'
import { log } from './logger.ts'
import { catPaths } from './utils.ts'

const globalArgs = {
token: {
type: 'string',
description: 'Authorization header token',
required: false,
},
user: {
type: 'string',
description: 'User identifier header',
required: false,
},
} as const satisfies ArgsDef

const { hostname, port } = catPaths.realDomain

const cli = treaty<App>(`${hostname}:${port}`)

const main = defineCommand({
meta: {
name: 'ccat',
description: 'A CLI for the Cheshire Cat API',
version: pkg.version,
},
subCommands: {
status: defineCommand({
meta: {
name: 'status',
description: 'Check the status of the API',
},
args: globalArgs,
async run({ args }) {
const { token, user } = args
const { data, error } = await cli.index.get({ headers: { token, user } })
log.dir(data ?? error.value)
},
}),
chat: defineCommand({
meta: {
name: 'chat',
description: 'Chat with the Cheshire Cat',
},
args: {
...globalArgs,
text: {
type: 'string',
description: 'The text to send to the Cheshire Cat',
valueHint: 'Hello world',
required: true,
},
save: {
type: 'boolean',
description: 'Whether to save the message in the memory',
default: true,
},
why: {
type: 'boolean',
description: 'Whether to include the reasoning in the response',
default: true,
},
},
async run({ args }) {
const { token, user, text, save, why } = args
const { data, error } = await cli.chat.post({
text: String(text),
}, {
headers: { token, user },
query: {
save: !!save,
why: !!why,
},
})
log.dir(data ?? error.value)
},
}),
},
async run({ rawArgs }) {
if (rawArgs.length === 0) await showUsage(main)
process.exit(0)
},
})

runMain(main)
15 changes: 9 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@ import { swagger } from '@elysiajs/swagger'
import { embedderRoutes, generalRoutes, llmRoutes, memoryRoutes, pluginsRoutes, rabbitHoleRoutes, settingsRoutes } from '@routes'
import { Elysia } from 'elysia'
import { checkPort } from 'get-port-please'
import isDocker from 'is-docker'
import pkg from '~/package.json'
import { serverContext, swaggerTags } from './context.ts'
import { httpLogger, log } from './logger.ts'
import { logWelcome, parsedEnv } from './utils.ts'
import { catPaths, logWelcome, parsedEnv } from './utils.ts'

const app = new Elysia()
.use(httpLogger)
Expand Down Expand Up @@ -74,11 +73,9 @@ const app = new Elysia()
.use(rabbitHoleRoutes)
.use(pluginsRoutes)

const inDocker = isDocker()
const { hostname, port } = catPaths.realDomain

try {
const port = inDocker ? 80 : parsedEnv.port
const hostname = inDocker ? '0.0.0.0' : parsedEnv.host
await checkPort(port, hostname)
app.listen({ hostname, port })
await logWelcome()
Expand All @@ -89,4 +86,10 @@ catch (error) {
process.exit(1)
}

export default app
export type App = typeof generalRoutes &
typeof settingsRoutes &
typeof llmRoutes &
typeof embedderRoutes &
typeof memoryRoutes &
typeof rabbitHoleRoutes &
typeof pluginsRoutes
18 changes: 18 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { BaseMessageChunk } from '@langchain/core/messages'
import { stat } from 'node:fs/promises'
import { join } from 'node:path'
import { safeDestr } from 'destr'
import isDocker from 'is-docker'
import { type CriteriaLike, loadEvaluator } from 'langchain/evaluation'
import _DefaultsDeep from 'lodash/defaultsDeep.js'
import { z } from 'zod'
Expand Down Expand Up @@ -80,10 +81,27 @@ function getBaseUrl() {
return new URL(address)
}

/**
* Retrieves the real domain of the application.
*/
function getRealDomain() {
const inDocker = isDocker()
const port = inDocker ? 80 : parsedEnv.port
const hostname = inDocker ? '0.0.0.0' : parsedEnv.host
return {
hostname,
port,
}
}

/**
* It contains various paths and URLs used in the application.
*/
export const catPaths = {
/**
* The real domain of the application.
*/
realDomain: getRealDomain(),
/**
* The base path of the application.
*/
Expand Down
8 changes: 5 additions & 3 deletions test/api/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import app from '@/main.ts'
import type { App } from '@/main'
import { treaty } from '@elysiajs/eden'
import { parsedEnv } from '@utils'
import { catPaths, parsedEnv } from '@utils'
import { describe, expect, it } from 'bun:test'
import pkg from '~/package.json'

const api = treaty(app)
const { hostname, port } = catPaths.realDomain

const api = treaty<App>(`${hostname}:${port}`)

describe('api status', () => {
it('return correct response', async () => {
Expand Down
Loading