wellknown-server.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. const token = "dummy-wellknown-token"
  2. const port = Number(process.env.PORT ?? 8787)
  3. const config = {
  4. $schema: "https://opencode.ai/config.json",
  5. share: "manual",
  6. model: "example-primary/example-chat",
  7. enabled_providers: ["example-primary", "example-secondary"],
  8. disabled_providers: ["opencode", "anthropic", "openai", "google", "xai", "amazon-bedrock", "azure"],
  9. provider: {
  10. "example-primary": {
  11. name: "Example Primary",
  12. npm: "@ai-sdk/openai-compatible",
  13. whitelist: ["example-chat", "example-code"],
  14. options: {
  15. baseURL: "https://models.example.com/v1",
  16. apiKey: "{env:TOKEN}",
  17. },
  18. models: {
  19. "example-chat": {
  20. name: "Example Chat",
  21. reasoning: true,
  22. tool_call: true,
  23. attachment: true,
  24. modalities: {
  25. input: ["text", "image"],
  26. output: ["text"],
  27. },
  28. limit: {
  29. context: 128000,
  30. output: 16000,
  31. },
  32. },
  33. "example-code": {
  34. name: "Example Code",
  35. reasoning: true,
  36. tool_call: true,
  37. limit: {
  38. context: 200000,
  39. output: 32000,
  40. },
  41. },
  42. },
  43. },
  44. "example-secondary": {
  45. name: "Example Secondary",
  46. npm: "@ai-sdk/openai-compatible",
  47. whitelist: ["example-fast"],
  48. options: {
  49. baseURL: "https://inference.example.org/v1",
  50. apiKey: "{env:TOKEN}",
  51. },
  52. models: {
  53. "example-fast": {
  54. name: "Example Fast",
  55. tool_call: true,
  56. limit: {
  57. context: 64000,
  58. output: 8000,
  59. },
  60. },
  61. },
  62. },
  63. },
  64. mcp: {
  65. "example-tools": {
  66. type: "remote",
  67. url: "https://tools.example.net/mcp",
  68. enabled: false,
  69. },
  70. },
  71. permission: {
  72. bash: "ask",
  73. edit: "ask",
  74. webfetch: "ask",
  75. read: {
  76. "*": "allow",
  77. "*.env": "deny",
  78. "*.env.*": "deny",
  79. "*.env.example": "allow",
  80. },
  81. },
  82. }
  83. const server = Bun.serve({
  84. hostname: "127.0.0.1",
  85. port,
  86. async fetch(request) {
  87. const url = new URL(request.url)
  88. if (url.pathname === "/.well-known/opencode") {
  89. return Response.json({
  90. auth: {
  91. command: ["bun", "-e", `await Bun.sleep(5000); process.stdout.write(${JSON.stringify(token)})`],
  92. env: "TOKEN",
  93. },
  94. remote_config: {
  95. url: `${url.origin}/config/opencode.json`,
  96. headers: { authorization: "Bearer {env:TOKEN}" },
  97. },
  98. })
  99. }
  100. if (url.pathname === "/config/opencode.json") {
  101. if (request.headers.get("authorization") !== `Bearer ${token}`) {
  102. return new Response("Unauthorized", { status: 401 })
  103. }
  104. return Response.json(config)
  105. }
  106. return new Response("Not found", { status: 404 })
  107. },
  108. })
  109. console.log(`Well-known fixture listening at ${server.url.origin}`)
  110. console.log(`Test with: bun run dev auth connect ${server.url.origin}`)