example.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import { createOpencodeClient, createOpencodeServer } from "@opencode-ai/sdk"
  2. import { pathToFileURL } from "bun"
  3. const server = await createOpencodeServer()
  4. const client = createOpencodeClient({ baseUrl: server.url })
  5. const input = await Array.fromAsync(new Bun.Glob("packages/core/*.ts").scan())
  6. const tasks: Promise<void>[] = []
  7. for await (const file of input) {
  8. console.log("processing", file)
  9. const session = await client.session.create()
  10. tasks.push(
  11. client.session.prompt({
  12. path: { id: session.data.id },
  13. body: {
  14. parts: [
  15. {
  16. type: "file",
  17. mime: "text/plain",
  18. url: pathToFileURL(file).href,
  19. },
  20. {
  21. type: "text",
  22. text: `Write tests for every public function in this file.`,
  23. },
  24. ],
  25. },
  26. }),
  27. )
  28. console.log("done", file)
  29. }
  30. await Promise.all(
  31. input.map(async (file) => {
  32. const session = await client.session.create()
  33. console.log("processing", file)
  34. await client.session.prompt({
  35. path: { id: session.data.id },
  36. body: {
  37. parts: [
  38. {
  39. type: "file",
  40. mime: "text/plain",
  41. url: pathToFileURL(file).href,
  42. },
  43. {
  44. type: "text",
  45. text: `Write tests for every public function in this file.`,
  46. },
  47. ],
  48. },
  49. })
  50. console.log("done", file)
  51. }),
  52. )