import { Utils as l } from "vscode-uri";
import { W as d } from "./workspace-object-CspubCq6.js";
import { A as u } from "./shared-types-CIxCt9tj.js";
import { a as k } from "./tool-filter-CS5Fsu0N.js";
import { F as _ } from "./file-lock-D4f90hJ2.js";
class s extends Error {
  constructor(t) {
    super(`${t} is not supported in an empty workspace`), this.name = "UnsupportedWorkspaceOperationError";
  }
}
class n extends Error {
  constructor(t) {
    super(`Execution context is required for ${t} operation`), this.name = "MissingExecutionContextError";
  }
}
class c {
  _workspaceFolders;
  _machineId;
  _fs;
  _terminalManager;
  _backgroundProcessManager;
  _steeringProvider;
  _taskProvider;
  _checkpointProvider;
  _fileOperationsProvider;
  _workspaceResolver;
  _getTools;
  _context;
  /**
   * Creates a new WorkspaceConnectionImpl instance.
   * @param config - Configuration containing all dependencies
   * @param context - Optional execution context
   */
  constructor(t, e) {
    this._workspaceFolders = t.workspaceFolders, this._machineId = t.machineId, this._fs = t.fs, this._terminalManager = t.terminalManager, this._backgroundProcessManager = t.backgroundProcessManager, this._steeringProvider = t.steeringProvider, this._taskProvider = t.taskProvider, this._checkpointProvider = t.checkpointProvider, this._fileOperationsProvider = t.fileOperationsProvider, this._workspaceResolver = t.workspaceResolver, this._getTools = t.getTools, this._context = e;
  }
  /** Whether this is an empty workspace (no folders). */
  get isEmpty() {
    return this._workspaceFolders.length === 0;
  }
  /** Returns the file system implementation. */
  get fs() {
    return this._fs;
  }
  /** Returns the steering document provider. */
  get steeringProvider() {
    return this._steeringProvider;
  }
  /** Returns the unique machine identifier. */
  get machineId() {
    return this._machineId;
  }
  /** Returns the number of workspace folders. */
  get workspaceFolderCount() {
    return this._workspaceFolders.length;
  }
  /** Returns the background process manager. */
  get backgroundProcessManager() {
    return this._backgroundProcessManager;
  }
  /** Returns the terminal manager. */
  get terminalManager() {
    return this._terminalManager;
  }
  /** Returns the current autonomy mode. */
  getAutonomyMode() {
    return this._context?.autonomyMode ?? u.Supervised;
  }
  /** Creates a new connection with additional execution context. */
  withContext(t) {
    return new c(
      {
        workspaceFolders: this._workspaceFolders,
        machineId: this._machineId,
        fs: this._fs,
        terminalManager: this._terminalManager,
        backgroundProcessManager: this._backgroundProcessManager,
        steeringProvider: this._steeringProvider,
        taskProvider: this._taskProvider,
        checkpointProvider: this._checkpointProvider,
        fileOperationsProvider: this._fileOperationsProvider,
        workspaceResolver: this._workspaceResolver,
        getTools: this._getTools
      },
      { ...this._context, ...t }
    );
  }
  /** Creates a new connection with filtered tools. */
  withAllowedTools(t, e) {
    return this.withContext({
      allowedTools: t,
      includeMcpJson: e?.includeMcpJson,
      includePowers: e?.includePowers
    });
  }
  /** Reads a file from the workspace. */
  async readFile(t) {
    if (this.isEmpty)
      throw new s("Read file");
    return await this._fileOperationsProvider.readFileWithMetadata({
      uri: t.file.uri,
      range: t.range
    });
  }
  /** Lists contents of a directory. */
  async listDirectory(t) {
    return this.isEmpty ? { children: [] } : (await this._fs.stat(t.path.uri.fsPath)).type !== "directory" ? { children: [] } : { children: (await this._fs.readDirectory(t.path.uri.fsPath)).map((i) => ({
      type: i.type === "directory" ? 2 : i.type === "symlink" ? 64 : 1,
      path: l.joinPath(t.path.uri, i.name)
    })) };
  }
  /** Writes content to a file. */
  async writeFile(t) {
    if (this.isEmpty)
      throw new s("Write file");
    if (!this._context)
      throw new n("write");
    return this._fileOperationsProvider.writeFileWithStreaming({
      uri: t.file.uri,
      text: t.text,
      range: t.range,
      createFile: t.createFile,
      skipCheckpointing: t.skipCheckpointing,
      executionId: this._context.executionId,
      chatSessionId: this._context.chatSessionId,
      autonomyMode: this._context.autonomyMode
    });
  }
  /** Deletes a file from the workspace. */
  async deleteFile(t) {
    if (this.isEmpty)
      throw new s("Delete file");
    return await _.getInstance().withLock(t.file.uri, async () => {
      await this._fs.delete(t.file.uri.fsPath);
    }), this._fileOperationsProvider.notifyFilesDeleted([t.file.uri.fsPath]), { success: !0 };
  }
  /** Runs a bash command in the workspace. */
  async runBashCommand(t) {
    const e = typeof t.cwd.uri == "string" ? t.cwd.uri : t.cwd.uri.fsPath, r = await this._terminalManager.getOrCreateTerminal({ cwd: e }), o = r.ensureCommandRunsInCwd(t.command, e);
    t.onTerminalReady?.(r.id);
    let i;
    t.onInputStall && r.onInputStall && (i = r.onInputStall(t.onInputStall));
    try {
      const a = await r.runCommand(o, t.timeout);
      return {
        terminalId: a.terminalId,
        output: a.output,
        code: a.exitCode
      };
    } finally {
      i?.dispose();
    }
  }
  /** Creates a checkpoint of a file. */
  async checkpointFile(t) {
    if (this.isEmpty || !this._checkpointProvider)
      throw new s("File checkpointing");
    if (!this._context)
      throw new n("checkpoint");
    return this._checkpointProvider.checkpointFile({
      fileUri: t.file.uri,
      filePath: t.file.reference,
      executionId: this._context.executionId,
      chatSessionId: this._context.chatSessionId
    });
  }
  /** Gets steering documents, optionally filtered by file paths. */
  async getSteeringDocuments(t) {
    return this.isEmpty ? { documents: [] } : { documents: await this._steeringProvider.getDocuments(t?.files) };
  }
  /** Updates the status of a task. */
  async updateTaskStatus(t) {
    if (this.isEmpty || !this._taskProvider)
      throw new s("Task status management");
    return this._taskProvider.updateTaskStatus({
      taskId: t.taskId,
      tasksPath: t.tasksPath.uri,
      status: t.status,
      executionId: t.executionId,
      chatSessionId: t.chatSessionId
    });
  }
  /** Validates if a task status change is allowed. */
  async validateTaskStatusChange(t) {
    if (this.isEmpty || !this._taskProvider)
      throw new s("Task validation");
    return this._taskProvider.validateTaskStatusChange({
      taskId: t.taskId,
      tasksPath: t.tasksPath.uri,
      newStatus: t.newStatus,
      currentStatus: t.currentStatus
    });
  }
  /** Gets metadata about a task. */
  async getTaskMetadata(t) {
    if (this.isEmpty || !this._taskProvider)
      throw new s("Task Retrieval");
    return this._taskProvider.getTaskMetadata({
      taskId: t.taskId,
      tasksPath: t.tasksPath.uri
    });
  }
  /** Updates PBT test status for a task. */
  async updatePBTStatus(t) {
    if (this.isEmpty || !this._taskProvider)
      throw new s("PBT Status Update");
    return this._taskProvider.updatePBTStatus({
      taskId: t.taskId,
      tasksPath: t.tasksPath.uri,
      status: t.status,
      failingExample: t.failingExample,
      executionId: t.executionId,
      chatSessionId: t.chatSessionId
    });
  }
  /** Gets task information by ID. */
  async getTask(t) {
    if (this.isEmpty || !this._taskProvider)
      throw new s("Task Retrieval");
    return this._taskProvider.getTask({
      taskId: t.taskId,
      tasksPath: t.tasksPath.uri
    });
  }
  /** Resolves a relative path to a full URI. */
  async resolveRelativePath(t) {
    return this._workspaceResolver.resolveRelativePath(t);
  }
  /** Converts a URI to a relative path. */
  asRelativePath(t) {
    return this._workspaceResolver.asRelativePath(t);
  }
  /** Lists all workspace folders. */
  listWorkspaceFolders() {
    return { folders: this._workspaceFolders.map((e) => d.fromUri(this, e)) };
  }
  /** Gets tools available for chat. */
  getChatTools() {
    const t = this._getTools(this).chat;
    return this.filterTools(t);
  }
  /** Gets tools available for spec agent. */
  getSpecAgentTools() {
    const t = this._getTools(this).specAgent;
    return this.filterTools(t);
  }
  /** Gets tools available for spec orchestrator. */
  getSpecOrchestratorTools() {
    return this._getTools(this).specOrchestrator;
  }
  /** Gets tools available for subagents. */
  getSubAgentTools() {
    return this._getTools(this).subAgent;
  }
  /** Filters tools based on allowed tools configuration. */
  filterTools(t) {
    const e = this._context?.allowedTools;
    return e ? k(t, e, {
      includeMcpJson: this._context.includeMcpJson ?? !1,
      includePowers: this._context.includePowers ?? !1
    }) : t;
  }
}
export {
  n as M,
  s as U,
  c as W
};
//# sourceMappingURL=workspace-connection-impl-Dee9nf40.js.map
