catalog.ts 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { spyOn } from "bun:test"
  2. import type { LocationRef, ModelListOutput, OpenCodeClient, ProviderListOutput } from "@opencode-ai/client/promise"
  3. export function catalogProvider(id: string, name: string): ProviderListOutput["data"][number] {
  4. return {
  5. id,
  6. name,
  7. package: "",
  8. }
  9. }
  10. export function catalogModel(input: {
  11. id: string
  12. modelID?: string
  13. providerID: string
  14. name?: string
  15. context?: number
  16. variants?: string[]
  17. }): ModelListOutput["data"][number] {
  18. return {
  19. id: input.id,
  20. modelID: input.modelID ?? input.id,
  21. providerID: input.providerID,
  22. name: input.name ?? input.id,
  23. capabilities: {
  24. tools: true,
  25. input: ["text"],
  26. output: ["text"],
  27. },
  28. variants: (input.variants ?? []).map((id) => ({ id })),
  29. time: { released: 1 },
  30. cost: [{ input: 0, output: 0, cache: { read: 0, write: 0 } }],
  31. status: "active",
  32. enabled: true,
  33. limit: { context: input.context ?? 128_000, output: 8_192 },
  34. }
  35. }
  36. export function stubCatalogLists(
  37. sdk: OpenCodeClient,
  38. input: {
  39. location?: LocationRef
  40. providers?: ProviderListOutput["data"]
  41. models?: ModelListOutput["data"]
  42. } = {},
  43. ) {
  44. const location = {
  45. directory: input.location?.directory ?? "/tmp",
  46. workspaceID: input.location?.workspaceID,
  47. project: { id: "proj_1", directory: input.location?.directory ?? "/tmp" },
  48. }
  49. const empty = { location, data: [] }
  50. return {
  51. provider: spyOn(sdk.provider, "list").mockResolvedValue({ location, data: input.providers ?? [] } as never),
  52. model: spyOn(sdk.model, "list").mockResolvedValue({ location, data: input.models ?? [] } as never),
  53. agent: spyOn(sdk.agent, "list").mockResolvedValue(empty as never),
  54. reference: spyOn(sdk.reference, "list").mockResolvedValue(empty as never),
  55. command: spyOn(sdk.command, "list").mockResolvedValue(empty as never),
  56. skill: spyOn(sdk.skill, "list").mockResolvedValue(empty as never),
  57. }
  58. }