| 1 | import type { WireEvent, WireMCPInteraction } from "./types"; |
| 2 | |
| 3 | let sequence = 0; |
| 4 | let activeID = ""; |
| 5 | |
| 6 | /** Browser-only fixture for exercising the structured MCP elicitation flow. */ |
| 7 | function createMockMCPInteraction(id: string): WireMCPInteraction { |
| 8 | return { |
| 9 | id, |
| 10 | server: "github", |
| 11 | mode: "form", |
| 12 | message: "MCP server requests additional information", |
| 13 | requestedSchema: { |
| 14 | type: "object", |
| 15 | required: ["code", "environment", "permissions"], |
| 16 | properties: { |
| 17 | code: { type: "string", title: "Device code", default: "123-456" }, |
| 18 | environment: { |
| 19 | type: "string", |
| 20 | title: "Environment", |
| 21 | oneOf: [ |
| 22 | { const: "production", title: "Production" }, |
| 23 | { const: "staging", title: "Staging" }, |
| 24 | { const: "development", title: "Development" }, |
| 25 | ], |
| 26 | default: "staging", |
| 27 | }, |
| 28 | permissions: { |
| 29 | type: "array", |
| 30 | title: "Permissions", |
| 31 | description: "Choose what this connection may access.", |
| 32 | items: { |
| 33 | anyOf: [ |
| 34 | { const: "repo:read", title: "Read repositories" }, |
| 35 | { const: "issues:write", title: "Update issues" }, |
| 36 | { const: "actions:read", title: "Read Actions" }, |
| 37 | ], |
| 38 | }, |
| 39 | minItems: 1, |
| 40 | default: ["repo:read"], |
| 41 | }, |
| 42 | remember: { type: "boolean", title: "Remember this choice", default: false }, |
| 43 | }, |
| 44 | }, |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | export async function showMockMCPInteraction( |
| 49 | wait: (milliseconds: number) => Promise<void>, |
| 50 | isCancelled: () => boolean, |
| 51 | emit: (event: WireEvent) => void, |
| 52 | ): Promise<void> { |
| 53 | const id = `mock-mcp-interaction-${++sequence}`; |
| 54 | activeID = id; |
| 55 | await wait(250); |
| 56 | if (isCancelled() || activeID !== id) return; |
| 57 | emit({ kind: "mcp_interaction", mcpInteraction: createMockMCPInteraction(id) }); |
| 58 | } |
| 59 | |
| 60 | export function consumeMockMCPInteraction(id: string): boolean { |
| 61 | if (activeID !== id) return false; |
| 62 | activeID = ""; |
| 63 | return true; |
| 64 | } |
| 65 |