cloud-gateway.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**
  2. * CloudGatewayClient — daemon-side abstraction over the existing managed
  3. * permission classify endpoint.
  4. *
  5. * Wire contract is pixel-aligned to the production endpoint already in use:
  6. *
  7. * POST {region-routed-host}/mavis/api/v1/permission/check
  8. * Authorization: Bearer <managed-auth-token>
  9. *
  10. * Request:
  11. * {
  12. * "tool_name": string,
  13. * "input": string, // serialized tool input
  14. * "platform": string,
  15. * "home_dir": string,
  16. * "workspace_root": string,
  17. * "mode": "auto",
  18. * "conversation_context": string, // pre-rendered by daemon
  19. * "agent_id": string?,
  20. * "session_id": string?
  21. * }
  22. *
  23. * Response:
  24. * {
  25. * "base_resp": { "status_code": number, "status_msg": string },
  26. * "verdict": "allow" | "confirm" | "block",
  27. * "reason": string
  28. * }
  29. *
  30. * The cloud side is intentionally untouched. Prompt assembly stays in the
  31. * local runtime (`packages/local-runtime/prompts/llm-gate-classifier.md`),
  32. * conversation context is rendered runtime-side (see `conversation-renderer.ts`),
  33. * and the cloud endpoint remains a thin wrapper around gemini-flash with the
  34. * same prompt baked in.
  35. *
  36. * Future-compatibility hooks:
  37. * - `daemonPromptVersion` is sent so the cloud side can log/route by
  38. * prompt revision when it adopts a registry. Unknown today, ignored
  39. * server-side.
  40. * - `suggestedRule` is reserved in the verdict shape so a future cloud
  41. * response can carry a persistence recommendation without a wire bump.
  42. * Today: cloud never returns it, the daemon never persists from a gateway
  43. * verdict.
  44. */
  45. import type { ProposedRule } from './ask-policy.js';
  46. /**
  47. * What the daemon hands to the gateway. All fields except the new
  48. * `daemonPromptVersion` map 1:1 to the existing payload.
  49. */
  50. export interface CloudClassifyRequest {
  51. toolName: string;
  52. /** Serialized tool input (string), matching the `input` field. */
  53. input: string;
  54. platform: string;
  55. homeDir: string;
  56. workspaceRoot: string;
  57. /** Reserved for future modes; today always 'auto'. */
  58. mode: 'auto';
  59. /**
  60. * Daemon-rendered conversation context. Contains "## Recent User
  61. * Instructions" + "## Recent Conversation" sections built by the
  62. * conversation renderer. Cloud forwards it into the prompt as-is.
  63. */
  64. conversationContext: string;
  65. agentId?: string;
  66. sessionId?: string;
  67. /**
  68. * Daemon's prompt revision identifier (hash of llm-gate-classifier.md).
  69. * Cloud may log this but does not consume it today. Always send.
  70. */
  71. daemonPromptVersion?: string;
  72. }
  73. /**
  74. * What the gateway hands back. Verdict is mapped from the cloud
  75. * `verdict: 'allow' | 'confirm' | 'block'` plus a synthesized `timeout`
  76. * variant the HTTP client emits on network/timeout failure (fail-safe to
  77. * ask-user).
  78. */
  79. export interface CloudClassifyVerdict {
  80. kind: 'allow' | 'confirm' | 'block' | 'timeout';
  81. /** Localized rationale from the cloud, or a daemon-local fallback on timeout. */
  82. reasonLocalized: string;
  83. /**
  84. * Forward-compatible: when the cloud endpoint adds a `suggested_rule`
  85. * field, the HTTP client surfaces it here. Today always undefined — the
  86. * cloud never returns it and the daemon never persists from a gateway
  87. * verdict.
  88. */
  89. suggestedRule?: ProposedRule;
  90. /** Telemetry — server-side request id, response time. Optional. */
  91. serverRequestId?: string;
  92. durationMs?: number;
  93. /**
  94. * Telemetry — model identifier reported by the gateway (e.g.
  95. * `cloud:gemini-flash`) or `cloud:unavailable` when the client falls back
  96. * to timeout. The cloud-classify wire always returned this; the field is
  97. * optional so daemons that log it can keep doing so without a wire
  98. * renegotiation.
  99. */
  100. model?: string;
  101. }
  102. /**
  103. * The injection seam. local-runtime provides `HttpCloudGatewayClient`; tests
  104. * provide `InMemoryCloudGatewayClient`; offline / BYO-key daemons can inject a
  105. * no-op that always returns `{ kind: 'timeout', ... }` to force the ask-user
  106. * path.
  107. */
  108. export interface CloudGatewayClient {
  109. classify(req: CloudClassifyRequest): Promise<CloudClassifyVerdict>;
  110. }