// src/lib/api-client/sessions.ts
import {
  createSession as apiCreateSession,
  listSessions as apiListSessions,
  listMessages as apiListMessages,
  createMessage as apiCreateMessage,
  abortSession as apiAbortSession,
  deleteSession as apiDeleteSession,
  updateSession as apiUpdateSession,
  getSessionQueue as apiGetSessionQueue,
  removeFromQueue as apiRemoveFromQueue,
  retryMessage as apiRetryMessage
} from "@ottocode/api";

// src/lib/api-client/utils.ts
import { client } from "@ottocode/api";

// src/lib/config.ts
function computeApiBaseUrl() {
  const envUrl = import.meta.env?.VITE_API_BASE_URL;
  if (envUrl) {
    return envUrl;
  }
  if (typeof window !== "undefined") {
    const win = window;
    if (win.OTTO_SERVER_URL) {
      return win.OTTO_SERVER_URL;
    }
    if (win.__OTTO_API_URL__) {
      return win.__OTTO_API_URL__;
    }
  }
  return "http://localhost:9100";
}
function getRuntimeApiBaseUrl() {
  return computeApiBaseUrl();
}
var API_BASE_URL = computeApiBaseUrl();
var config = {
  apiBaseUrl: API_BASE_URL
};

// src/lib/api-client/utils.ts
function getClientAdapter() {
  if (typeof window !== "undefined" && typeof window.fetch === "function") {
    return "fetch";
  }
  return;
}
function extractErrorMessage(error) {
  if (!error)
    return "Unknown error";
  if (typeof error === "string")
    return error;
  if (error && typeof error === "object") {
    const errObj = error;
    if (errObj.error && typeof errObj.error === "object") {
      const innerError = errObj.error;
      if (typeof innerError.message === "string")
        return innerError.message;
    }
    if (typeof errObj.error === "string")
      return errObj.error;
    if (typeof errObj.message === "string")
      return errObj.message;
    try {
      return JSON.stringify(error);
    } catch {
      return "Error occurred (unable to parse)";
    }
  }
  return "Unknown error";
}
function configureApiClient() {
  const win = window;
  const baseURL = win.OTTO_SERVER_URL || API_BASE_URL;
  client.setConfig({
    baseURL,
    adapter: getClientAdapter()
  });
}
configureApiClient();
function getBaseUrl() {
  const win = window;
  if (win.OTTO_SERVER_URL)
    return win.OTTO_SERVER_URL;
  return API_BASE_URL;
}
function convertSession(apiSession) {
  return {
    ...apiSession,
    title: apiSession.title ?? null,
    createdAt: typeof apiSession.createdAt === "string" ? new Date(apiSession.createdAt).getTime() : apiSession.createdAt,
    lastActiveAt: typeof apiSession.lastActiveAt === "string" ? new Date(apiSession.lastActiveAt).getTime() : apiSession.lastActiveAt
  };
}
function convertMessage(apiMessage) {
  return {
    ...apiMessage,
    createdAt: typeof apiMessage.createdAt === "string" ? new Date(apiMessage.createdAt).getTime() : apiMessage.createdAt,
    completedAt: apiMessage.completedAt ? typeof apiMessage.completedAt === "string" ? new Date(apiMessage.completedAt).getTime() : apiMessage.completedAt : null
  };
}

// src/lib/api-client/sessions.ts
var sessionsMixin = {
  async getSessions() {
    const page = await this.getSessionsPage({ limit: 200 });
    return page.items;
  },
  async getSessionsPage(params = {}) {
    const { limit = 50, offset = 0 } = params;
    const response = await apiListSessions({
      query: { limit, offset }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    const data = response.data;
    return {
      items: (data?.items ?? []).map((s) => convertSession(s)),
      hasMore: data?.hasMore ?? false,
      nextOffset: data?.nextOffset ?? null
    };
  },
  async createSession(data) {
    const response = await apiCreateSession({
      body: data
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    if (!response.data)
      throw new Error("No data returned from create session");
    return convertSession(response.data);
  },
  async updateSession(sessionId, data) {
    const response = await apiUpdateSession({
      path: { sessionId },
      body: data
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return convertSession(response.data);
  },
  async deleteSession(sessionId) {
    const response = await apiDeleteSession({ path: { sessionId } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async abortSession(sessionId) {
    const response = await apiAbortSession({ path: { sessionId } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async abortMessage(sessionId, _messageId) {
    const response = await apiAbortSession({
      path: { sessionId }
    });
    if (response.error)
      throw new Error("Failed to abort message");
    return response.data;
  },
  async getQueueState(sessionId) {
    const response = await apiGetSessionQueue({ path: { sessionId } });
    if (response.error)
      throw new Error("Failed to get queue state");
    return response.data;
  },
  async removeFromQueue(sessionId, messageId) {
    const response = await apiRemoveFromQueue({
      path: { sessionId, messageId }
    });
    if (response.error)
      throw new Error("Failed to remove from queue");
    return response.data;
  },
  async getMessages(sessionId) {
    const response = await apiListMessages({ path: { id: sessionId } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return (response.data || []).map(convertMessage);
  },
  async sendMessage(sessionId, data) {
    const response = await apiCreateMessage({
      path: { id: sessionId },
      body: data
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  getStreamUrl(sessionId) {
    return `${getBaseUrl()}/v1/sessions/${sessionId}/stream`;
  },
  async retryMessage(sessionId, messageId) {
    const response = await apiRetryMessage({
      path: { sessionId, messageId }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  }
};

// src/lib/api-client/git.ts
import {
  getGitStatus as apiGetGitStatus,
  getGitDiff as apiGetGitDiff,
  getGitBranch as apiGetGitBranch,
  stageFiles as apiStageFiles,
  unstageFiles as apiUnstageFiles,
  restoreFiles as apiRestoreFiles,
  deleteFiles as apiDeleteFiles,
  commitChanges as apiCommitChanges,
  generateCommitMessage as apiGenerateCommitMessage,
  pushCommits as apiPushCommits,
  pullChanges as apiPullChanges,
  initGitRepo as apiInitGitRepo,
  getGitRemotes as apiGetGitRemotes,
  addGitRemote as apiAddGitRemote,
  removeGitRemote as apiRemoveGitRemote
} from "@ottocode/api";
var gitMixin = {
  async initGitRepo() {
    const response = await apiInitGitRepo();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async getGitStatus() {
    const response = await apiGetGitStatus();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async getGitDiff(file, staged = false) {
    const response = await apiGetGitDiff({
      query: { file, staged: staged ? "true" : "false" }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async getGitDiffFullFile(file, staged = false) {
    const response = await apiGetGitDiff({
      query: {
        file,
        staged: staged ? "true" : "false",
        fullFile: "true"
      }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async generateCommitMessage(sessionId) {
    const response = await apiGenerateCommitMessage({
      body: sessionId ? { sessionId } : {}
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async stageFiles(files) {
    const response = await apiStageFiles({
      body: { files }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async unstageFiles(files) {
    const response = await apiUnstageFiles({
      body: { files }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async restoreFiles(files) {
    const response = await apiRestoreFiles({
      body: { files }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async deleteFiles(files) {
    const response = await apiDeleteFiles({
      body: { files }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async commitChanges(message) {
    const response = await apiCommitChanges({
      body: { message }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async getGitBranch() {
    const response = await apiGetGitBranch();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async pushCommits() {
    const response = await apiPushCommits({
      body: {}
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async pullChanges() {
    const response = await apiPullChanges({
      body: {}
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async getRemotes() {
    const response = await apiGetGitRemotes();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data?.remotes;
  },
  async addRemote(name, url) {
    const response = await apiAddGitRemote({
      body: { name, url }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  },
  async removeRemote(name) {
    const response = await apiRemoveGitRemote({
      body: { name }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data?.data;
  }
};

// src/lib/api-client/config.ts
import {
  getConfig as apiGetConfig,
  getProviderModels as apiGetProviderModels,
  getAllModels as apiGetAllModels,
  updateDefaults as apiUpdateDefaults
} from "@ottocode/api";
var configMixin = {
  async getConfig() {
    const response = await apiGetConfig();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getModels(providerId) {
    const response = await apiGetProviderModels({
      path: { provider: providerId }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getAllModels() {
    const response = await apiGetAllModels();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async updateDefaults(data) {
    const response = await apiUpdateDefaults({
      body: data
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  }
};

// src/lib/api-client/files.ts
import {
  listFiles as apiListFiles,
  getFileTree as apiGetFileTree,
  readFile as apiReadFile,
  getSessionFiles as apiGetSessionFiles
} from "@ottocode/api";
var filesMixin = {
  async listFiles() {
    const response = await apiListFiles();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getFileTree(dirPath = ".") {
    const response = await apiGetFileTree({
      query: { path: dirPath }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async readFileContent(filePath) {
    const response = await apiReadFile({
      query: { path: filePath }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getSessionFiles(sessionId) {
    const response = await apiGetSessionFiles({ path: { sessionId } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  }
};

// src/lib/api-client/branches.ts
import {
  createBranch as apiCreateBranch,
  listBranches as apiListBranches,
  getParentSession as apiGetParentSession,
  getShareStatus as apiGetShareStatus,
  shareSession as apiShareSession,
  syncShare as apiSyncShare
} from "@ottocode/api";
var branchesMixin = {
  async createBranch(sessionId, data) {
    const response = await apiCreateBranch({
      path: { sessionId },
      body: data
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async listBranches(sessionId) {
    const response = await apiListBranches({ path: { sessionId } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getParentSession(sessionId) {
    const response = await apiGetParentSession({ path: { sessionId } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getShareStatus(sessionId) {
    const response = await apiGetShareStatus({ path: { sessionId } });
    if (response.error)
      return { shared: false };
    return response.data;
  },
  async shareSession(sessionId) {
    const response = await apiShareSession({ path: { sessionId } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async syncSession(sessionId) {
    const response = await apiSyncShare({ path: { sessionId } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  }
};

// src/lib/api-client/approval.ts
import {
  resolveApproval as apiResolveApproval,
  getPendingApprovals as apiGetPendingApprovals
} from "@ottocode/api";
var approvalMixin = {
  async approveToolCall(sessionId, callId, approved) {
    const response = await apiResolveApproval({
      path: { id: sessionId },
      body: { callId, approved, sessionId }
    });
    if (response.error)
      throw new Error("Failed to send tool approval");
    return response.data;
  },
  async getPendingApprovals(sessionId) {
    const response = await apiGetPendingApprovals({
      path: { id: sessionId }
    });
    if (response.error)
      return { ok: false, pending: [] };
    return response.data;
  }
};

// src/lib/api-client/setu.ts
import {
  getSetuBalance as apiGetSetuBalance,
  getSetuWallet as apiGetSetuWallet,
  getSetuUsdcBalance as apiGetSetuUsdcBalance,
  createPolarCheckout as apiCreatePolarCheckout,
  getPolarTopupEstimate as apiGetPolarTopupEstimate,
  getPolarTopupStatus as apiGetPolarTopupStatus,
  getRazorpayTopupEstimate as apiGetRazorpayTopupEstimate,
  createRazorpayOrder as apiCreateRazorpayOrder,
  verifyRazorpayPayment as apiVerifyRazorpayPayment,
  selectTopupMethod as apiSelectTopupMethod,
  cancelTopup as apiCancelTopup,
  getPendingTopup as apiGetPendingTopup
} from "@ottocode/api";
var setuMixin = {
  async getSetuBalance() {
    try {
      const response = await apiGetSetuBalance();
      if (response.error)
        return null;
      return response.data;
    } catch {
      return null;
    }
  },
  async getSetuWallet() {
    try {
      const response = await apiGetSetuWallet();
      if (response.error)
        return { configured: false };
      return response.data;
    } catch {
      return { configured: false };
    }
  },
  async getSetuUsdcBalance(network = "mainnet") {
    try {
      const response = await apiGetSetuUsdcBalance({
        query: { network }
      });
      if (response.error)
        return null;
      return response.data;
    } catch {
      return null;
    }
  },
  async getPolarTopupEstimate(amount) {
    try {
      const response = await apiGetPolarTopupEstimate({
        query: { amount }
      });
      if (response.error)
        return null;
      return response.data;
    } catch {
      return null;
    }
  },
  async createPolarCheckout(amount, successUrl) {
    const response = await apiCreatePolarCheckout({
      body: { amount, successUrl }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async selectTopupMethod(sessionId, method) {
    const response = await apiSelectTopupMethod({
      body: { sessionId, method }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async cancelTopup(sessionId, reason) {
    const response = await apiCancelTopup({
      body: { sessionId, reason }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getPendingTopup(sessionId) {
    try {
      const response = await apiGetPendingTopup({
        query: { sessionId }
      });
      if (response.error)
        return { hasPending: false };
      return response.data;
    } catch {
      return { hasPending: false };
    }
  },
  async getPolarTopupStatus(checkoutId) {
    try {
      const response = await apiGetPolarTopupStatus({
        query: { checkoutId }
      });
      if (response.error)
        return null;
      return response.data;
    } catch {
      return null;
    }
  },
  async getRazorpayTopupEstimate(amount) {
    try {
      const response = await apiGetRazorpayTopupEstimate({
        query: { amount }
      });
      if (response.error)
        return null;
      return response.data;
    } catch {
      return null;
    }
  },
  async createRazorpayOrder(amount) {
    const response = await apiCreateRazorpayOrder({
      body: { amount }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async verifyRazorpayPayment(params) {
    const response = await apiVerifyRazorpayPayment({
      body: params
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  }
};

// src/lib/api-client/auth.ts
import {
  getAuthStatus as apiGetAuthStatus,
  setupSetuWallet as apiSetupSetuWallet,
  importSetuWallet as apiImportSetuWallet,
  exportSetuWallet as apiExportSetuWallet,
  addProviderApiKey as apiAddProviderApiKey,
  removeProvider as apiRemoveProvider,
  completeOnboarding as apiCompleteOnboarding,
  getOAuthUrl as apiGetOAuthUrl,
  exchangeOAuthCode as apiExchangeOAuthCode,
  startCopilotDeviceFlow as apiStartCopilotDeviceFlow,
  pollCopilotDeviceFlow as apiPollCopilotDeviceFlow,
  getCopilotAuthMethods as apiGetCopilotAuthMethods,
  saveCopilotToken as apiSaveCopilotToken,
  importCopilotTokenFromGh as apiImportCopilotTokenFromGh,
  getCopilotDiagnostics as apiGetCopilotDiagnostics,
  getProviderUsage as apiGetProviderUsage
} from "@ottocode/api";
var authMixin = {
  async getAuthStatus() {
    const response = await apiGetAuthStatus();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async setupSetuWallet() {
    const response = await apiSetupSetuWallet();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async importSetuWallet(privateKey) {
    const response = await apiImportSetuWallet({
      body: { privateKey }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async exportSetuWallet() {
    const response = await apiExportSetuWallet();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async addProvider(provider, apiKey) {
    const response = await apiAddProviderApiKey({
      path: { provider },
      body: { apiKey }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async removeProvider(provider) {
    const response = await apiRemoveProvider({ path: { provider } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async completeOnboarding() {
    const response = await apiCompleteOnboarding();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  getOAuthStartUrl(provider, mode) {
    const baseUrl = `${getBaseUrl()}/v1/auth/${provider}/oauth/start`;
    if (mode)
      return `${baseUrl}?mode=${mode}`;
    return baseUrl;
  },
  async getOAuthUrl(provider, mode) {
    const response = await apiGetOAuthUrl({
      path: { provider },
      body: { mode }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async exchangeOAuthCode(provider, code, sessionId) {
    const response = await apiExchangeOAuthCode({
      path: { provider },
      body: { code, sessionId }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async startCopilotDeviceFlow() {
    const response = await apiStartCopilotDeviceFlow();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async pollCopilotDeviceFlow(sessionId) {
    const response = await apiPollCopilotDeviceFlow({
      body: { sessionId }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getCopilotAuthMethods() {
    const response = await apiGetCopilotAuthMethods();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async saveCopilotToken(token) {
    const response = await apiSaveCopilotToken({ body: { token } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async importCopilotTokenFromGh() {
    const response = await apiImportCopilotTokenFromGh();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getCopilotDiagnostics() {
    const response = await apiGetCopilotDiagnostics();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getProviderUsage(provider) {
    const response = await apiGetProviderUsage({
      path: { provider }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  }
};

// src/lib/api-client/skills.ts
import {
  listSkills as apiListSkills,
  getSkill as apiGetSkill,
  listSkillFiles as apiListSkillFiles,
  getSkillFile as apiGetSkillFile
} from "@ottocode/api";
var skillsMixin = {
  async listSkills() {
    const response = await apiListSkills();
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getSkill(name) {
    const response = await apiGetSkill({ path: { name } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getSkillFiles(name) {
    const response = await apiListSkillFiles({ path: { name } });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  },
  async getSkillFileContent(name, filePath) {
    const response = await apiGetSkillFile({
      path: { name, filePath }
    });
    if (response.error)
      throw new Error(extractErrorMessage(response.error));
    return response.data;
  }
};

// src/lib/api-client/index.ts
class ApiClient {
  getSessions = sessionsMixin.getSessions;
  getSessionsPage = sessionsMixin.getSessionsPage;
  createSession = sessionsMixin.createSession;
  updateSession = sessionsMixin.updateSession;
  deleteSession = sessionsMixin.deleteSession;
  abortSession = sessionsMixin.abortSession;
  abortMessage = sessionsMixin.abortMessage;
  getQueueState = sessionsMixin.getQueueState;
  removeFromQueue = sessionsMixin.removeFromQueue;
  getMessages = sessionsMixin.getMessages;
  sendMessage = sessionsMixin.sendMessage;
  getStreamUrl = sessionsMixin.getStreamUrl;
  retryMessage = sessionsMixin.retryMessage;
  initGitRepo = gitMixin.initGitRepo;
  getGitStatus = gitMixin.getGitStatus;
  getGitDiff = gitMixin.getGitDiff;
  getGitDiffFullFile = gitMixin.getGitDiffFullFile;
  generateCommitMessage = gitMixin.generateCommitMessage;
  stageFiles = gitMixin.stageFiles;
  unstageFiles = gitMixin.unstageFiles;
  restoreFiles = gitMixin.restoreFiles;
  deleteFiles = gitMixin.deleteFiles;
  commitChanges = gitMixin.commitChanges;
  getGitBranch = gitMixin.getGitBranch;
  pushCommits = gitMixin.pushCommits;
  pullChanges = gitMixin.pullChanges;
  getRemotes = gitMixin.getRemotes;
  addRemote = gitMixin.addRemote;
  removeRemote = gitMixin.removeRemote;
  getConfig = configMixin.getConfig;
  getModels = configMixin.getModels;
  getAllModels = configMixin.getAllModels;
  updateDefaults = configMixin.updateDefaults;
  listFiles = filesMixin.listFiles;
  getFileTree = filesMixin.getFileTree;
  readFileContent = filesMixin.readFileContent;
  getSessionFiles = filesMixin.getSessionFiles;
  createBranch = branchesMixin.createBranch;
  listBranches = branchesMixin.listBranches;
  getParentSession = branchesMixin.getParentSession;
  getShareStatus = branchesMixin.getShareStatus;
  shareSession = branchesMixin.shareSession;
  syncSession = branchesMixin.syncSession;
  approveToolCall = approvalMixin.approveToolCall;
  getPendingApprovals = approvalMixin.getPendingApprovals;
  getSetuBalance = setuMixin.getSetuBalance;
  getSetuWallet = setuMixin.getSetuWallet;
  getSetuUsdcBalance = setuMixin.getSetuUsdcBalance;
  getPolarTopupEstimate = setuMixin.getPolarTopupEstimate;
  createPolarCheckout = setuMixin.createPolarCheckout;
  selectTopupMethod = setuMixin.selectTopupMethod;
  cancelTopup = setuMixin.cancelTopup;
  getPendingTopup = setuMixin.getPendingTopup;
  getPolarTopupStatus = setuMixin.getPolarTopupStatus;
  getRazorpayTopupEstimate = setuMixin.getRazorpayTopupEstimate;
  createRazorpayOrder = setuMixin.createRazorpayOrder;
  verifyRazorpayPayment = setuMixin.verifyRazorpayPayment;
  getAuthStatus = authMixin.getAuthStatus;
  setupSetuWallet = authMixin.setupSetuWallet;
  importSetuWallet = authMixin.importSetuWallet;
  exportSetuWallet = authMixin.exportSetuWallet;
  addProvider = authMixin.addProvider;
  removeProvider = authMixin.removeProvider;
  completeOnboarding = authMixin.completeOnboarding;
  getOAuthStartUrl = authMixin.getOAuthStartUrl;
  getOAuthUrl = authMixin.getOAuthUrl;
  exchangeOAuthCode = authMixin.exchangeOAuthCode;
  startCopilotDeviceFlow = authMixin.startCopilotDeviceFlow;
  pollCopilotDeviceFlow = authMixin.pollCopilotDeviceFlow;
  getCopilotAuthMethods = authMixin.getCopilotAuthMethods;
  saveCopilotToken = authMixin.saveCopilotToken;
  importCopilotTokenFromGh = authMixin.importCopilotTokenFromGh;
  getCopilotDiagnostics = authMixin.getCopilotDiagnostics;
  getProviderUsage = authMixin.getProviderUsage;
  listSkills = skillsMixin.listSkills;
  getSkill = skillsMixin.getSkill;
  getSkillFiles = skillsMixin.getSkillFiles;
  getSkillFileContent = skillsMixin.getSkillFileContent;
}
var apiClient = new ApiClient;
// src/lib/sse-client.ts
class SSEClient {
  abortController = null;
  handlers = new Map;
  running = false;
  async connect(url) {
    if (this.abortController) {
      this.abortController.abort();
    }
    this.abortController = new AbortController;
    this.running = true;
    const isTunnel = !url.includes("localhost") && !url.includes("127.0.0.1");
    try {
      const response = await fetch(url, {
        method: isTunnel ? "POST" : "GET",
        headers: { Accept: "text/event-stream" },
        signal: this.abortController.signal
      });
      if (!response.ok) {
        console.error("[SSE] Connection failed:", response.status);
        return;
      }
      const reader = response.body?.getReader();
      if (!reader) {
        console.error("[SSE] No response body");
        return;
      }
      const decoder = new TextDecoder;
      let buffer = "";
      while (this.running) {
        const { done, value } = await reader.read();
        if (done)
          break;
        buffer += decoder.decode(value, { stream: true });
        let idx = buffer.indexOf(`

`);
        while (idx !== -1) {
          const raw = buffer.slice(0, idx);
          buffer = buffer.slice(idx + 2);
          const lines = raw.split(`
`);
          let eventType = "message";
          let data = "";
          for (const line of lines) {
            if (line.startsWith("event: ")) {
              eventType = line.slice(7).trim();
            } else if (line.startsWith("data: ")) {
              data += (data ? `
` : "") + line.slice(6);
            } else if (line.startsWith(":")) {}
          }
          if (data) {
            try {
              const payload = JSON.parse(data);
              this.emit({ type: eventType, payload });
            } catch (error) {
              console.error(`[SSE] Failed to parse ${eventType}:`, error);
            }
          }
          idx = buffer.indexOf(`

`);
        }
      }
    } catch (error) {
      if (error instanceof Error && error.name === "AbortError") {} else if (error instanceof TypeError && error.message === "Load failed") {} else {
        console.error("[SSE] Connection error:", error);
      }
    }
  }
  disconnect() {
    this.running = false;
    if (this.abortController) {
      this.abortController.abort();
      this.abortController = null;
    }
  }
  on(eventType, handler) {
    if (!this.handlers.has(eventType)) {
      this.handlers.set(eventType, new Set);
    }
    this.handlers.get(eventType)?.add(handler);
    return () => {
      this.off(eventType, handler);
    };
  }
  off(eventType, handler) {
    const handlers = this.handlers.get(eventType);
    if (handlers) {
      handlers.delete(handler);
      if (handlers.size === 0) {
        this.handlers.delete(eventType);
      }
    }
  }
  emit(event) {
    const handlers = this.handlers.get(event.type);
    if (handlers) {
      for (const handler of handlers) {
        handler(event);
      }
    }
    const allHandlers = this.handlers.get("*");
    if (allHandlers) {
      for (const handler of allHandlers) {
        handler(event);
      }
    }
  }
}
export {
  getRuntimeApiBaseUrl,
  configureApiClient,
  config,
  apiClient,
  SSEClient,
  API_BASE_URL
};

//# debugId=FC7AB829460C133264756E2164756E21
