feat: add initial implementation of Opencode client and session handling

- Created agent.ts to export necessary components from the plugin source.
- Implemented index.ts to initialize Opencode client and list projects and sessions.
- Added functionality to prompt a session with specific tool instructions.
This commit is contained in:
2026-02-07 03:24:10 +08:00
parent bf88cfab5c
commit 17731be911
5 changed files with 302 additions and 370 deletions

49
src/index.ts Normal file
View File

@@ -0,0 +1,49 @@
import { createOpencode, createOpencodeClient } from '@opencode-ai/sdk'
import util from 'node:util'
const showMore = (obj: any) => {
return util.inspect(obj, { showHidden: false, depth: null, colors: true })
}
// const client = await createOpencode({
// })
const client = await createOpencodeClient({
baseUrl: "http://localhost:4096",
})
// const sessions = await client.session.list()
// console.log('session', sessions)
// const sessions = await client.session.list({
// query: {
// directory: '/home/ubuntu/kevisual'
// }
// })
// console.log('session', sessions)
const project = await client.project.list()
console.log('project', project)
const first = project.data?.[0]
if (first) {
const sessions = await client.session.list()
console.log('session', sessions)
const session = sessions.data?.[0]
if (session) {
const id = session.id;
const res = await client.session.prompt({
path: { id: session.id },
body: {
// noReply: true,
tools: {
greet: false,
},
// parts: [{ type: "text", text: "执行Greet工具" }],
// parts: [{ type: "text", text: "name是 xiong" }],
parts: [
{ type: "text", text: "执行Greet工具" },
{ type: "text", text: "name是 xiong" }],
},
})
console.log('res', showMore(res))
}
}