{"version":3,"sources":["../../../../tools/warp_grep/providers/code_storage_http.ts"],"sourcesContent":["/**\n * HTTP-based RemoteCommands for code search against the Morph landing API.\n *\n * Translates grep/read/listDir into POST requests to the code-search endpoints,\n * so the WarpGrep agent can search GitHub repositories that have been indexed\n * by the Morph code storage service.\n */\n\nimport type { RemoteCommands } from '../types.js';\n\n/**\n * The WarpGrep model addresses files as if the repo were checked out at\n * /home/ubuntu/repo (its training root), while the code-search endpoints\n * expect repo-relative paths. Strip the training root and any leading\n * slashes at this boundary — the same normalization the dashboard\n * playground applies via warpgrep-mock-paths.ts.\n */\nconst MODEL_REPO_ROOT = /^\\/?home\\/ubuntu\\/repo(?:\\/|$)/;\n\nexport function toRepoRelative(path: string): string {\n  if (!path) return path;\n  const stripped = path.replace(MODEL_REPO_ROOT, '').replace(/^\\.?\\/+/, '');\n  return stripped || '.';\n}\n\nexport interface CodeStorageHttpConfig {\n  /** Base URL for the code search API (e.g. \"https://api.morphllm.com\") */\n  baseUrl: string;\n  /** Repository ID returned by the get-or-create endpoint */\n  repoId: string;\n  /** Git branch to search */\n  branch: string;\n  /** Morph API key, forwarded as a Bearer token (api.morphllm.com requires it) */\n  apiKey?: string;\n}\n\nasync function post(url: string, body: Record<string, unknown>, op: string, apiKey?: string): Promise<string> {\n  const res = await fetch(url, {\n    method: 'POST',\n    headers: {\n      'Content-Type': 'application/json',\n      ...(apiKey ? { Authorization: `Bearer ${apiKey}` } : {}),\n    },\n    body: JSON.stringify(body),\n  });\n  if (!res.ok) {\n    const text = await res.text().catch(() => res.statusText);\n    // Return error as content instead of throwing so the agent can handle it gracefully\n    try {\n      const parsed = JSON.parse(text);\n      if (parsed.error) return `Error: ${parsed.error}`;\n    } catch { /* not JSON, fall through */ }\n    return `Error: ${op} failed (${res.status}): ${text}`;\n  }\n  const data = await res.json();\n  if (data.error) throw new Error(data.error);\n  return data.stdout;\n}\n\nexport function createCodeStorageHttpCommands(config: CodeStorageHttpConfig): RemoteCommands {\n  const { baseUrl, repoId, branch, apiKey } = config;\n  const encodedRepoId = encodeURIComponent(repoId);\n\n  return {\n    grep: (pattern: string, path: string, glob?: string) =>\n      post(`${baseUrl}/api/code-search/${encodedRepoId}/grep`, { pattern, path: toRepoRelative(path), glob, branch }, 'grep', apiKey),\n\n    read: (path: string, start: number, end: number) =>\n      post(`${baseUrl}/api/code-search/${encodedRepoId}/read`, { path: toRepoRelative(path), start, end, branch }, 'read', apiKey),\n\n    listDir: (path: string, maxDepth: number) =>\n      post(`${baseUrl}/api/code-search/${encodedRepoId}/list`, { path: toRepoRelative(path), maxDepth, branch }, 'list', apiKey),\n  };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAiBA,IAAM,kBAAkB;AAEjB,SAAS,eAAe,MAAsB;AACnD,MAAI,CAAC,KAAM,QAAO;AAClB,QAAM,WAAW,KAAK,QAAQ,iBAAiB,EAAE,EAAE,QAAQ,WAAW,EAAE;AACxE,SAAO,YAAY;AACrB;AAaA,eAAe,KAAK,KAAa,MAA+B,IAAY,QAAkC;AAC5G,QAAM,MAAM,MAAM,MAAM,KAAK;AAAA,IAC3B,QAAQ;AAAA,IACR,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,GAAI,SAAS,EAAE,eAAe,UAAU,MAAM,GAAG,IAAI,CAAC;AAAA,IACxD;AAAA,IACA,MAAM,KAAK,UAAU,IAAI;AAAA,EAC3B,CAAC;AACD,MAAI,CAAC,IAAI,IAAI;AACX,UAAM,OAAO,MAAM,IAAI,KAAK,EAAE,MAAM,MAAM,IAAI,UAAU;AAExD,QAAI;AACF,YAAM,SAAS,KAAK,MAAM,IAAI;AAC9B,UAAI,OAAO,MAAO,QAAO,UAAU,OAAO,KAAK;AAAA,IACjD,QAAQ;AAAA,IAA+B;AACvC,WAAO,UAAU,EAAE,YAAY,IAAI,MAAM,MAAM,IAAI;AAAA,EACrD;AACA,QAAM,OAAO,MAAM,IAAI,KAAK;AAC5B,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