Skip to content

Commit c1c3876

Browse files
committed
sdk example
1 parent e9bc924 commit c1c3876

File tree

3 files changed

+55
-41
lines changed

3 files changed

+55
-41
lines changed

bun.lock

Lines changed: 6 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

npm-app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
"dependencies": {
3737
"@codebuff/code-map": "workspace:*",
3838
"@codebuff/common": "workspace:*",
39+
"@codebuff/sdk": "^0.1.3",
3940
"@types/diff": "5.2.1",
4041
"@types/micromatch": "^4.0.9",
4142
"@vscode/ripgrep": "1.15.9",

npm-app/src/index.ts

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#!/usr/bin/env node
22

3+
import * as fs from 'fs'
4+
import * as os from 'os'
5+
36
import { type CostMode } from '@codebuff/common/constants'
47
import { AnalyticsEvent } from '@codebuff/common/constants/analytics-events'
58
import { Command, Option } from 'commander'
69
import { red } from 'picocolors'
10+
import { CodebuffClient } from '@codebuff/sdk'
711

812
import { displayLoadedAgents, loadLocalAgents } from './agents/load-agents'
913
import { CLI } from './cli'
@@ -22,7 +26,7 @@ import {
2226
} from './project-files'
2327
import { rageDetectors } from './rage-detectors'
2428
import { logAndHandleStartup } from './startup-process-handler'
25-
import { recreateShell } from './terminal/run-command'
29+
import { recreateShell, runTerminalCommand } from './terminal/run-command'
2630
import { initAnalytics, trackEvent } from './utils/analytics'
2731
import { 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

8997
if (require.main === module) {

0 commit comments

Comments
 (0)