-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
1,886 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import modelManager from '../../src/lib/server/modelManager' | ||
import ora from 'ora' | ||
import chalk from 'chalk' | ||
import cliWidth from 'cli-width' | ||
import { parseArgs, type ParseArgsConfig } from 'node:util' | ||
import { parseFromUrl } from '@latitude-data/custom_types' | ||
|
||
type CommandArgs = { | ||
prompt: string | ||
params: Record<string, unknown> | ||
debug: boolean | ||
} | ||
|
||
const OPTIONS = { | ||
debug: { | ||
type: 'boolean', | ||
short: 'd', | ||
}, | ||
param: { | ||
type: 'string', | ||
short: 'p', | ||
multiple: true, | ||
}, | ||
} | ||
function getArgs(): CommandArgs { | ||
const args = process.argv.slice(2) | ||
const { values, positionals } = parseArgs({ | ||
args, | ||
allowPositionals: true, | ||
options: OPTIONS as ParseArgsConfig['options'], | ||
}) | ||
const { debug, param } = values as { debug?: boolean; param?: string[] } | ||
const paramsUrl = param | ||
?.reduce((acc: string[], param: string) => { | ||
const [key, ...rest] = param.split('=') | ||
const value = rest.join('=') | ||
return [...acc, `${key}=${value}`] | ||
}, []) | ||
.join('&') | ||
const params = paramsUrl ? parseFromUrl(paramsUrl) : {} | ||
|
||
const [prompt] = positionals as string[] | ||
if (!prompt) throw new Error('Prompt is required') | ||
|
||
return { | ||
debug: debug ?? false, | ||
prompt, | ||
params, | ||
} | ||
} | ||
|
||
async function runPrompt({ prompt, params, debug }: CommandArgs) { | ||
const spinner = ora().start() | ||
|
||
const onDebug = (message: string) => { | ||
spinner.text = message | ||
} | ||
|
||
let lastToken = '' | ||
let currentLine = 0 | ||
const onToken = async (token: string) => { | ||
if (spinner.isSpinning) spinner.stop() | ||
|
||
/* Fancy printing method ahead. I could just print the token and call it a day | ||
but I'm going to try to make it look nicer. :) | ||
*/ | ||
|
||
// Re-print the last token with its regular color | ||
process.stdout.moveCursor(-lastToken.length, 0) | ||
process.stdout.write(chalk.reset(lastToken)) | ||
|
||
// If there's going to be a new line (either by line break or by overflowing), | ||
// print the part of the token before the new line with its regular color | ||
if (token.includes('\n')) { | ||
const parts = token.split('\n') | ||
parts.slice(0, -1).forEach((part) => { | ||
process.stdout.write(chalk.reset(part)) | ||
process.stdout.write('\n') | ||
}) | ||
token = parts[parts.length - 1]! | ||
currentLine = 0 | ||
} | ||
|
||
const maxWidth = cliWidth() | ||
if (currentLine + token.length > maxWidth) { | ||
const leftChars = maxWidth - currentLine | ||
process.stdout.write(token.slice(0, leftChars)) | ||
token = token.slice(leftChars) | ||
currentLine = 0 | ||
} | ||
|
||
// Print the rest of the token with a blue color | ||
process.stdout.write(chalk.blue(token)) | ||
lastToken = token | ||
currentLine += token.length | ||
} | ||
|
||
try { | ||
const model = await modelManager.loadFromPrompt(prompt) | ||
|
||
if (debug) { | ||
const compiledPrompt = await model.compilePrompt({ | ||
promptPath: prompt, | ||
params, | ||
onDebug, | ||
}) | ||
spinner.stop() | ||
console.log(compiledPrompt.prompt) | ||
return | ||
} | ||
|
||
await model.runPrompt({ promptPath: prompt, params, onDebug, onToken }) | ||
await onToken('') | ||
if (spinner.isSpinning) spinner.stop() | ||
console.log() | ||
} catch (e) { | ||
const error = e as Error | ||
if (spinner.isSpinning) { | ||
spinner.fail(error.message) | ||
} else { | ||
console.error(error.message) | ||
} | ||
} | ||
} | ||
|
||
const args = getArgs() | ||
|
||
runPrompt(args) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export const BASE_STATIC_PATH = 'static/.latitude' | ||
export const APP_CONFIG_PATH = `${BASE_STATIC_PATH}/latitude.json` | ||
export const QUERIES_DIR = `${BASE_STATIC_PATH}/queries` | ||
export const PROMPTS_DIR = `${BASE_STATIC_PATH}/prompts` | ||
export const STORAGE_DIR = `${BASE_STATIC_PATH}/storage` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { PROMPTS_DIR } from '../constants' | ||
import { ModelManager } from '@latitude-data/llm-manager' | ||
import sourceManager from './sourceManager' | ||
|
||
const modelManager = new ModelManager(PROMPTS_DIR, sourceManager) | ||
export default modelManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import config from '$src/config' | ||
import setRootDir from '$src/lib/decorators/setRootDir' | ||
import setup from '$src/lib/decorators/setup' | ||
import spawn from '$src/lib/spawn' | ||
import syncQueries from '$src/lib/sync/syncQueries' | ||
import tracked from '$src/lib/decorators/tracked' | ||
import syncLatitudeJson from '$src/lib/sync/syncLatitudeJson' | ||
import syncPrompts from '$src/lib/sync/syncPrompts' | ||
|
||
async function prompt( | ||
promptPath: string, | ||
opts?: { | ||
param: string[] | string | undefined | ||
debug: boolean | ||
}, | ||
) { | ||
const debug = opts?.debug || false | ||
const params = | ||
typeof opts?.param === 'string' ? [opts.param] : opts?.param ?? [] | ||
|
||
await syncQueries({ watch: false }) | ||
await syncPrompts({ watch: false }) | ||
await syncLatitudeJson({ watch: false }) | ||
|
||
const args = [ | ||
'run', | ||
'prompt', | ||
promptPath, | ||
'--', | ||
debug ? '--debug' : '', | ||
...params.map((param) => `--param=${param}`), | ||
].filter(Boolean) | ||
|
||
return spawn( | ||
'npm', | ||
args, | ||
{ | ||
detached: false, | ||
cwd: config.appDir, | ||
stdio: 'inherit', | ||
}, | ||
{ | ||
onError: (error: Error) => { | ||
console.error('Error running query:', error) | ||
process.exit(1) | ||
}, | ||
onClose: (code: number) => { | ||
process.exit(code) | ||
}, | ||
}, | ||
) | ||
} | ||
|
||
export default tracked('runCommand', setRootDir(setup(prompt))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
packages/cli/core/src/lib/isSourceFile.ts → packages/cli/core/src/lib/isConfigFile.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export default function isSourceFile(srcPath: string) { | ||
export default function isConfigFile(srcPath: string) { | ||
return srcPath.endsWith('.yml') || srcPath.endsWith('.yaml') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.