Skip to content

Commit

Permalink
Merge pull request #313 from elsoul/cli
Browse files Browse the repository at this point in the history
Update - skeet ai
  • Loading branch information
POPPIN-FUMI authored Mar 12, 2024
2 parents a653940 + efad937 commit 18ce938
Show file tree
Hide file tree
Showing 51 changed files with 586 additions and 1,071 deletions.
45 changes: 45 additions & 0 deletions .changeset/thin-cheetahs-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
---
'@skeet-framework/utils': patch
'@skeet-framework/cli': patch
---

Update - skeet ai

```bash
$ skeet ai
╔═════════╤════════╗
│ Option │ Value │
╟─────────┼────────╢
│ AI Type │ Gemini │
╚═════════╧════════╝

🤖 Skeet AI Mode
Type `mode` to change AI mode 🤖


Gemini is selected 🤖 (type 'q' to quit)


? What can I do for you?

You: mode
Skeet:
? 🤖 Select Mode (Use arrow keys)
❯ prisma
typedoc
firestore
function
method
```

or directly call skeet ai mode with the option

```bash
$ skeet ai --mode
? 🤖 Select Mode (Use arrow keys)
❯ prisma
typedoc
firestore
function
method
```
2 changes: 1 addition & 1 deletion .github/workflows/website-skeet-utils.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
branches:
- main
paths:
- 'packages/utils/docs/**'
- 'packages/utils/**'
- '.github/workflows/website-skeet-utils.yml'

jobs:
Expand Down
9 changes: 0 additions & 9 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@
"build": "tsx build.ts && tsx add-line.ts && pnpm gen:cli",
"dev": "tsx build.ts && tsx add-line.ts && node ./dist/index.js",
"start": "node dist/index.js",
"discord:labo": "npx ts-node -r tsconfig-paths/register --transpile-only src/lib/runDiscordChangeLog.ts labo",
"discord:epics": "npx ts-node -r tsconfig-paths/register --transpile-only src/lib/runDiscordChangeLog.ts epics",
"gen:cli": "tsx src/lib/genCLIhelp.ts"
},
"dependencies": {
Expand All @@ -66,21 +64,14 @@
"@types/cli-spinner": "0.2.3",
"@types/i18n": "0.13.10",
"@types/inquirer": "9.0.7",
"@types/jest": "29.5.12",
"@types/node": "20.11.25",
"@types/prompt": "1.1.8",
"@types/semver": "7.5.8",
"@typescript-eslint/eslint-plugin": "7.1.1",
"@typescript-eslint/parser": "7.1.1",
"babel-loader": "9.1.3",
"copyfiles": "2.4.1",
"esbuild": "0.20.1",
"esbuild-plugin-alias-path": "2.0.2",
"eslint": "8.57.0",
"nodemon": "3.1.0",
"prettier": "3.2.5",
"ts-loader": "9.5.1",
"tsconfig-paths": "4.2.0",
"tsx": "4.7.1",
"typescript": "5.4.2",
"vite": "5.1.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/skeet-cloud.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
}
],
"ai": {
"lang": "ja",
"lang": "en",
"ais": [
{
"name": "VertexAI",
Expand Down
79 changes: 16 additions & 63 deletions packages/cli/src/cli/ai/ai.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,11 @@
import chalk from 'chalk'
import { skeetAiPrompt } from './skeetPrompt'
// import { prismaMode } from './mode/prismaMode'
// import { skeetMode } from './mode/skeetMode'
// import { typedocMode } from './mode/typedocMode'
// import { translateMode } from './mode/translateMode'
// import { firestoreMode } from './mode/firestoreMode'
// import { functionMode } from './mode/functionMode'
// import { SkeetAiMode, SkeetRole } from '@/types/skeetTypes'
// import { methodMode } from './mode/methodMode'
import inquirer from 'inquirer'
import { AiLog } from './aiLog'
import { SkeetAIOptions } from '.'
import { Readable } from 'stream'
import {
ConfigGeminiType,
ConfigOpenAIType,
OpenAIModel,
chat,
defaultGeminiConfig,
geminiChat,
generatePrompt,
openAIChat,
readGeminiStream,
readOpenAIStream,
} from '@skeet-framework/ai'

const GEMINI = 'Gemini'
const OPENAI = 'OpenAI'
type AIType = 'Gemini' | 'OpenAI'
import { AIType, chat } from '@skeet-framework/ai'
import { modeSelect } from './modeSelect'
import { skeetMode } from './mode/skeetMode'

export async function promptUser(
options: SkeetAIOptions,
Expand All @@ -36,9 +14,6 @@ export async function promptUser(
const log = logger.text() as SkeetLog
const aiOptions = {
ai: (options.ai as AIType) || ('Gemini' as AIType),
maxTokens: options.maxTokens || '1000',
model: options.model || defaultGeminiConfig.model,
temperature: options.temperature || '0.1',
}

console.log('\n')
Expand All @@ -59,45 +34,23 @@ export async function promptUser(
process.exit(0)
}
if (userInput.input.toLowerCase() === '') {
promptUser(aiOptions, logger)
await promptUser(aiOptions, logger)
return
}
console.log(chalk.blue('Skeet:'))

// if (userInput.input.toLowerCase().match(/^\$ prisma$/)) {
// await prismaMode(aiOptions, logger)
// return
// }
// if (userInput.input.toLowerCase().match(/^\$ skeet/)) {
// await skeetMode(userInput.input, skeetAi, logger)
// return
// }
// if (userInput.input.toLowerCase().match(/^\$ typedoc/)) {
// await typedocMode(skeetAi, logger)
// return
// }
// if (userInput.input.toLowerCase().match(/^\$ translate/)) {
// await translateMode(skeetAi, logger)
// promptUser(aiOptions, logger)
// return
// }
// if (userInput.input.toLowerCase().match(/^\$ firestore/)) {
// await firestoreMode(skeetAi, logger)
// return
// }
// if (userInput.input.toLowerCase().match(/^\$ function/)) {
// await functionMode(skeetAi, logger)
// return
// }
// if (userInput.input.toLowerCase().match(/^\$ method/)) {
// await methodMode(skeetAi, logger)
// return
// }
// if (userInput.input.toLowerCase().match(/^\$ help/)) {
// logger.help()
// promptUser(aiOptions, logger)
// return
// }
if (
userInput.input.toLowerCase().match(/^\$ mode$/) ||
userInput.input.toLowerCase() === 'mode'
) {
await modeSelect(aiOptions, logger)
return
}

if (userInput.input.toLowerCase().match(/^\$ skeet/)) {
await skeetMode(userInput.input, aiOptions, logger)
return
}

const skeetPrompt = skeetAiPrompt('en')
await chat(skeetPrompt.context, skeetPrompt.examples, userInput.input)
Expand Down
19 changes: 2 additions & 17 deletions packages/cli/src/cli/ai/aiLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,7 @@ export class AiLog {

help = () => {
console.log(
chalk.white(
`\n🤖 ${this.text().common.skeetAiModeText} 🤖\n\n` +
'$ prisma\n' +
'$ typedoc\n' +
'$ translate\n' +
'$ firestore\n' +
'$ function\n' +
'$ method\n' +
'$ help\n' +
'$ q\n',
),
chalk.white(`\n🤖 ${this.text().common.skeetAiModeText} 🤖\n\n`),
)
}

Expand All @@ -60,12 +50,7 @@ export class AiLog {
}, // テーブルの罫線スタイルを指定
})

table.push(
[this.text().common.aiType, aiOptions.ai],
[this.text().common.model, aiOptions.model],
[this.text().common.maxToken, aiOptions.maxTokens],
[this.text().common.temperature, aiOptions.temperature],
)
table.push([this.text().common.aiType, aiOptions.ai])

console.log(table.toString())
}
Expand Down
24 changes: 24 additions & 0 deletions packages/cli/src/cli/ai/createOrReadModeFrequency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { existsAsync } from '@skeet-framework/utils'
import { mkdir, readFile, writeFile } from 'fs/promises'
import { ModeFrequency, defaultModeFrequency } from './modeSelect'

export const createOrReadModeFrequency = async () => {
const filePath = `./tmp/modeFrequency.json`
if (await existsAsync(filePath)) {
const modeFrequency: ModeFrequency = JSON.parse(
await readFile(filePath, 'utf8'),
)
const sortedKeys = Object.entries(modeFrequency)
.sort((a, b) => b[1] - a[1])
.map((entry) => entry[0])
return sortedKeys
} else {
const tmp = './tmp'
if (!(await existsAsync(tmp))) {
await mkdir(tmp)
}
const body = JSON.stringify(defaultModeFrequency, null, 2)
await writeFile(filePath, body, 'utf8')
return Object.keys(defaultModeFrequency)
}
}
19 changes: 19 additions & 0 deletions packages/cli/src/cli/ai/incrementModeFrequency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { readFile, writeFile } from 'fs/promises'
import { ModeFrequency } from './modeSelect'

export const incrementModeFrequency = async (
modeKey: keyof ModeFrequency,
): Promise<void> => {
const filePath = `./tmp/modeFrequency.json`

const modeFrequency: ModeFrequency = JSON.parse(
await readFile(filePath, 'utf8'),
)

if (modeFrequency[modeKey] !== undefined) {
modeFrequency[modeKey] += 1
} else {
modeFrequency[modeKey] = 1
}
await writeFile(filePath, JSON.stringify(modeFrequency, null, 2), 'utf8')
}
28 changes: 8 additions & 20 deletions packages/cli/src/cli/ai/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import { importConfig } from '@/lib/files/importConfig'
import { AiLog } from './aiLog'
import { validEnv } from './validEnv'
import { validateAiConfig } from './validateAiConfig'
import { modeSelect } from './modeSelect'

export type SkeetAIOptions = {
ai: AIType
maxTokens: string
model: string
temperature: string
}

export const aiCommands = () => {
Expand All @@ -20,31 +18,21 @@ export const aiCommands = () => {
.description('AI Playground')
.option('-g, --gemini', 'Gemini')
.option('-o, --openai', 'OpenAI')
.option('-m, --model <string>', 'Model')
.option('-token, --token <number>', 'Max Tokens')
.option('-temp, --temperature <number>', 'Temperature')
.option('--mode', 'Mode')
.action(async (options) => {
await validateAiConfig()
const { ai } = await importConfig()
const lang = ai.lang as 'en' | 'ja'
const logger = new AiLog(lang)
const aiType = options.openai ? 'OpenAI' : 'Gemini'
validEnv(aiType as AIType, logger)
const model = options.openai
? options.model || 'gpt-4-turbo-preview'
: options.model || 'gemini-1.0-pro'
const maxTokens = options.token || '1000'
const temperature = options.temperature || '0'
if (Number(temperature) > 1 || Number(temperature) < 0) {
console.log(chalk.yellow(logger.text().warning.temperature))
process.exit(1)
}

const aiOptions: SkeetAIOptions = {
ai: aiType as AIType,
maxTokens,
model,
temperature,
}

if (options.mode) {
await modeSelect(aiOptions, logger)
return
}

logger.aiOptionTable(aiOptions)
Expand All @@ -54,6 +42,6 @@ export const aiCommands = () => {
`${chalk.blue(aiType)} ${logger.text().common.isSelected}`,
)}`,
)
promptUser(aiOptions, logger)
await promptUser(aiOptions, logger)
})
}
3 changes: 2 additions & 1 deletion packages/cli/src/cli/ai/locales/en/skeetAi.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"model": "Model",
"maxToken": "Max Token",
"temperature": "Temperature",
"skeetAiModeText": "Skeet AI Mode\n `$ <mode>` to change AI mode",
"skeetAiModeText": "Skeet AI Mode\nType `mode` to change AI mode",
"isSelected": "is selected 🤖 (type 'q' to quit)",
"shutdown": "is shutting down",
"howAboutThis": "How about this?\n\n",
Expand Down Expand Up @@ -69,6 +69,7 @@
"firestore": "- Triggered by Firestore event",
"pubsub": "- Triggered by PubSub event",
"schedule": "- Triggered by Schedule event",
"onCall": "- Triggered by onCall event",
"example1": "I want to create a blog app.",
"example2": "I want to create a new model called Client with the following fields: id, name, email, password, createdAt, updatedAt.",
"ExitingMode": "Exiting Function Mode"
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/cli/ai/locales/ja/skeetAi.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"model": "モデル",
"maxToken": "最大トークン",
"temperature": "感情の大きさ",
"skeetAiModeText": "Skeet AIモード\n `$ <mode>` でAIモードを変更",
"skeetAiModeText": "Skeet AIモード\n `mode` と入力してAIモードを変更",
"isSelected": "が選択されました 🤖 ('q'を入力して終了)",
"shutdown": "シャットダウン中",
"howAboutThis": "これはいかがでしょうか?\n\n",
Expand Down Expand Up @@ -70,6 +70,7 @@
"firestore": "- Firestoreイベントによってトリガーされます",
"pubsub": "- PubSubイベントによってトリガーされます",
"schedule": "- スケジュールイベントによってトリガーされます",
"onCall": "- クライアントからの呼び出しによってトリガーされます",
"example1": "ブログアプリを作りたい。",
"example2": "次のフィールドを持つ新しいモデル、Clientを作りたい:id、名前、メール、パスワード、作成日、更新日。",
"ExitingMode": "Functionモードを終了"
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/cli/ai/locales/localeTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ interface ExtendedMode extends BaseMode {
firestore?: string
storage?: string
schedule?: string
onCall?: string
auth?: string
modeDesc2?: string
}
Expand Down
Loading

0 comments on commit 18ce938

Please sign in to comment.