Pudu programming language
Menu
Package

@chrismichaelps / pudu-lang-mcp

Model Context Protocol server for Pudu language documentation and compiler tools

0.1.1Apache-2.01

InstallClose

Reply.pudu

Pudu57 lines2.0 KB

GitHub ↗
1/** @Domain.Rpc.Reply.Module — encodes results and errors as lines */2module PuduLangMcp.Domain.Rpc.Reply34import Std.Json as Json5import Std.Math as Math6import PuduLangMcp.Constants.Protocol as Protocol7import PuduLangMcp.Constants.Server as Server8import PuduLangMcp.Domain.Rpc.Message as Message9import PuduLangMcp.Errors.RpcError as RpcError1011/** @Domain.Rpc.Reply.Cache — caching hint for complete results */12export type Cache = NoCache | Cached(Int, Str)1314/// A complete result body: the given fields plus `resultType`, `serverInfo`, and any cache hint.15export fn complete(fields: &Array[(Str, Json.Json)], cache: &Cache) -> Json.Json {16  let identity = Json.object(&[17      ("name", Json.Text(Server.SERVER_NAME)),18      ("title", Json.Text(Server.SERVER_TITLE)),19      ("version", Json.Text(Server.SERVER_VERSION))20    ])21  var all = [("resultType", Json.Text(Protocol.RESULT_COMPLETE))].concat(*fields)22  match cache {23    case Cached(ttl, visibility) => {24      all = all.concat([("ttlMs", Json.Number(Math.max(ttl, 0))), ("cacheScope", Json.Text(visibility))])25    }26    case NoCache => ()27  }28  Json.object(&all.push((Protocol.META_KEY, Json.object(&[(Protocol.META_SERVER_INFO, identity)]))))29}3031/// The reply line carrying a result.32export fn result(id: &Message.RequestId, body: &Json.Json) -> Str {33  Json.encode(&Json.object(&[34        ("jsonrpc", Json.Text(Protocol.JSONRPC_VERSION)),35        ("id", Message.idJson(id)),36        ("result", *body)37      ]))38}3940/// The reply line carrying an error; the id is `null` when it could not be read.41export fn failure(id: &Option[Message.RequestId], problem: &RpcError.ProtocolError) -> Str {42  var error = [43    ("code", Json.Number(RpcError.code(problem))),44    ("message", Json.Text(RpcError.message(problem)))45  ]46  if let Some(extra) = RpcError.data(problem) { error = error.push(("data", extra)) }47  let shownId = match id {48    case Some(found) => Message.idJson(&found)49    case None => Json.Null50  }51  Json.encode(&Json.object(&[52        ("jsonrpc", Json.Text(Protocol.JSONRPC_VERSION)),53        ("id", shownId),54        ("error", Json.object(&error))55      ]))56}57