11#!/usr/bin/env node
22
3+ import * as fs from 'fs'
4+ import * as os from 'os'
5+
36import { type CostMode } from '@codebuff/common/constants'
47import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
58import { Command , Option } from 'commander'
69import { red } from 'picocolors'
10+ import { CodebuffClient } from '@codebuff/sdk'
711
812import { displayLoadedAgents , loadLocalAgents } from './agents/load-agents'
913import { CLI } from './cli'
@@ -22,7 +26,7 @@ import {
2226} from './project-files'
2327import { rageDetectors } from './rage-detectors'
2428import { logAndHandleStartup } from './startup-process-handler'
25- import { recreateShell } from './terminal/run-command'
29+ import { recreateShell , runTerminalCommand } from './terminal/run-command'
2630import { initAnalytics , trackEvent } from './utils/analytics'
2731import { logger } from './utils/logger'
2832
@@ -40,50 +44,54 @@ async function codebuff({
4044 cwd,
4145 trace,
4246} : CliOptions ) {
43- enableSquashNewlines ( )
44- const workingDir = getWorkingDirectory ( )
45- const projectRoot = getProjectRoot ( )
46- await recreateShell ( workingDir )
47-
48- // Kill all processes we failed to kill before
49- const processCleanupPromise = logAndHandleStartup ( )
50-
51- initAnalytics ( )
52- rageDetectors . startupTimeDetector . start ( )
53-
54- const initFileContextPromise = initProjectFileContextWithWorker ( projectRoot )
55-
56- // Only load local agents if no specific agent is requested
57- const loadLocalAgentsPromise = new Promise < void > ( ( resolve ) => {
58- loadLocalAgents ( { verbose : true } ) . then ( ( ) => {
59- const codebuffConfig = loadCodebuffConfig ( )
60- displayLoadedAgents ( codebuffConfig )
61- } )
62- resolve ( )
47+ const apiKey = JSON . parse (
48+ fs
49+ . readFileSync ( os . homedir ( ) + '/.config/manicode-dev/credentials.json' )
50+ . toString ( ) ,
51+ ) . default . authToken
52+
53+ const client = new CodebuffClient ( {
54+ apiKey,
55+ cwd : cwd ?? process . cwd ( ) ,
56+ onError : ( error ) => {
57+ console . error ( red ( error . message ) )
58+ } ,
59+ overrideTools : {
60+ run_terminal_command : async ( args ) => {
61+ const { command, mode, timeout, cwd } = args
62+ const result = await runTerminalCommand (
63+ 'id' ,
64+ command ,
65+ mode ,
66+ mode ,
67+ timeout ,
68+ cwd ,
69+ )
70+ return {
71+ toolResultMessage : result . result ,
72+ }
73+ } ,
74+ } ,
6375 } )
6476
65- const readyPromise = Promise . all ( [
66- initFileContextPromise ,
67- processCleanupPromise ,
68- loadLocalAgentsPromise ,
69- ] )
70-
71- // Initialize the CLI singleton
72- CLI . initialize ( readyPromise , {
73- git,
74- costMode,
75- model,
76- agent,
77- params,
78- print,
79- trace,
77+ const run = await client . run ( {
78+ agent : agent ?? 'base' ,
79+ prompt : initialInput ?? 'hi' ,
80+ handleEvent : ( event ) => {
81+ console . log ( 'event:' , event )
82+ } ,
8083 } )
8184
82- const cli = CLI . getInstance ( )
83-
84- await cli . printInitialPrompt ( { initialInput, runInitFlow } )
85+ await client . run ( {
86+ agent : agent ?? 'base' ,
87+ prompt : 'thank you' ,
88+ previousRun : run ,
89+ handleEvent : ( event ) => {
90+ console . log ( 'event:' , event )
91+ } ,
92+ } )
8593
86- rageDetectors . startupTimeDetector . end ( )
94+ process . exit ( 0 )
8795}
8896
8997if ( require . main === module ) {
0 commit comments