import { createSessionFsAdapter } from "./sessionFsProvider.js";
import {
  CopilotRequestHandler,
  CopilotWebSocketHandler,
  CopilotWebSocketCloseStatus,
  CopilotWebSocketForwarder
} from "./copilotRequestHandler.js";
const RuntimeConnection = {
  /**
   * Spawn a runtime child process and communicate over its stdin/stdout.
   * This is the default if no {@link CopilotClientOptions.connection} is set.
   */
  forStdio(opts = {}) {
    return { kind: "stdio", path: opts.path, args: opts.args };
  },
  /**
   * Spawn a runtime child process that listens on a TCP socket and connect to it.
   */
  forTcp(opts = {}) {
    return {
      kind: "tcp",
      port: opts.port,
      connectionToken: opts.connectionToken,
      path: opts.path,
      args: opts.args
    };
  },
  /**
   * Connect to an already-running runtime at the given URL. The SDK does not
   * spawn a process in this mode.
   */
  forUri(url, opts = {}) {
    return { kind: "uri", url, connectionToken: opts.connectionToken };
  }
};
function convertMcpCallToolResult(callResult) {
  const textParts = [];
  const binaryResults = [];
  for (const block of callResult.content) {
    switch (block.type) {
      case "text":
        if (typeof block.text === "string") {
          textParts.push(block.text);
        }
        break;
      case "image":
        if (typeof block.data === "string" && block.data && typeof block.mimeType === "string") {
          binaryResults.push({
            data: block.data,
            mimeType: block.mimeType,
            type: "image"
          });
        }
        break;
      case "resource": {
        if (block.resource?.text) {
          textParts.push(block.resource.text);
        }
        if (block.resource?.blob) {
          const mimeType = block.resource.mimeType;
          binaryResults.push({
            data: block.resource.blob,
            mimeType: typeof mimeType === "string" && mimeType ? mimeType : "application/octet-stream",
            type: "resource",
            description: block.resource.uri
          });
        }
        break;
      }
    }
  }
  return {
    textResultForLlm: textParts.join("\n"),
    resultType: callResult.isError ? "failure" : "success",
    ...binaryResults.length > 0 ? { binaryResultsForLlm: binaryResults } : {}
  };
}
function defineTool(name, config) {
  return { name, ...config };
}
const SYSTEM_MESSAGE_SECTIONS = {
  preamble: { description: "Agent identity preamble and mode statement" },
  identity: {
    description: "Section group covering the identity preamble and its sibling sub-sections (tone, tool efficiency, etc.)"
  },
  tone: { description: "Response style, conciseness rules, output formatting preferences" },
  tool_efficiency: { description: "Tool usage patterns, parallel calling, batching guidelines" },
  environment_context: { description: "CWD, OS, git root, directory listing, available tools" },
  code_change_rules: { description: "Coding rules, linting/testing, ecosystem tools, style" },
  guidelines: { description: "Tips, behavioral best practices, behavioral guidelines" },
  safety: { description: "Environment limitations, prohibited actions, security policies" },
  tool_instructions: { description: "Per-tool usage instructions" },
  custom_instructions: { description: "Repository and organization custom instructions" },
  runtime_instructions: {
    description: "Runtime-provided context and instructions (e.g. system notifications, memories, workspace context, mode-specific instructions, content-exclusion policy)"
  },
  last_instructions: {
    description: "End-of-prompt instructions: parallel tool calling, persistence, task completion"
  }
};
const approveAll = () => ({ kind: "approve-once" });
const defaultJoinSessionPermissionHandler = () => ({
  kind: "no-result"
});
export {
  CopilotRequestHandler,
  CopilotWebSocketCloseStatus,
  CopilotWebSocketForwarder,
  CopilotWebSocketHandler,
  RuntimeConnection,
  SYSTEM_MESSAGE_SECTIONS,
  approveAll,
  convertMcpCallToolResult,
  createSessionFsAdapter,
  defaultJoinSessionPermissionHandler,
  defineTool
};
