// Force strict mode and setup for ESM
"use strict";
import {
  CHANNEL_DELIVERY_ERROR_CODES
} from "./chunk-BK2UNYWD.js";
import {
  init_esbuild_shims
} from "./chunk-5O2XNYP6.js";
import {
  __name
} from "./chunk-J2S4EL5Y.js";

// packages/cli/src/serve/channel-worker-env.ts
init_esbuild_shims();
var CHANNEL_DAEMON_WORKER_SENTINEL = "QWEN_CHANNEL_DAEMON_WORKER";
var CHANNEL_WORKER_HEARTBEAT_INTERVAL_MS = 15e3;
var QWEN_DAEMON_TOKEN_ENV = "QWEN_DAEMON_TOKEN";
var QWEN_DAEMON_URL_ENV = "QWEN_DAEMON_URL";
var QWEN_DAEMON_WORKSPACE_ENV = "QWEN_DAEMON_WORKSPACE";
var QWEN_SERVER_TOKEN_ENV = "QWEN_SERVER_TOKEN";

// packages/cli/src/serve/channel-webhook-ipc.ts
init_esbuild_shims();
import { randomUUID } from "node:crypto";
var CHANNEL_WEBHOOK_ENQUEUE_ERROR_CODES = /* @__PURE__ */ new Set([
  "channel_worker_unavailable",
  "channel_webhook_enqueue_timeout",
  "channel_webhook_queue_full",
  "channel_webhook_target_unavailable",
  "channel_webhook_invalid_task",
  "channel_webhook_enqueue_failed"
]);
var ChannelWebhookEnqueueError = class extends Error {
  constructor(code, message) {
    super(message);
    this.code = code;
    this.name = "ChannelWebhookEnqueueError";
  }
  static {
    __name(this, "ChannelWebhookEnqueueError");
  }
};
function isChannelWebhookEnqueueErrorCode(value) {
  return typeof value === "string" && CHANNEL_WEBHOOK_ENQUEUE_ERROR_CODES.has(value);
}
__name(isChannelWebhookEnqueueErrorCode, "isChannelWebhookEnqueueErrorCode");
function isChannelWebhookEnqueueError(value) {
  return value instanceof ChannelWebhookEnqueueError || typeof value === "object" && value !== null && isChannelWebhookEnqueueErrorCode(value.code) && typeof value.message === "string";
}
__name(isChannelWebhookEnqueueError, "isChannelWebhookEnqueueError");
var CHANNEL_WEBHOOK_TASK_IPC_TIMEOUT_MS = 3e4;
function createChannelWebhookTaskMessage(task) {
  return {
    type: "webhook_task",
    id: randomUUID(),
    expiresAt: Date.now() + CHANNEL_WEBHOOK_TASK_IPC_TIMEOUT_MS,
    task
  };
}
__name(createChannelWebhookTaskMessage, "createChannelWebhookTaskMessage");
function isChannelWebhookTaskMessage(value) {
  return typeof value === "object" && value !== null && value.type === "webhook_task" && typeof value.id === "string" && typeof value.expiresAt === "number" && typeof value.task === "object" && value.task !== null;
}
__name(isChannelWebhookTaskMessage, "isChannelWebhookTaskMessage");
function isChannelWebhookTaskResultMessage(value) {
  return typeof value === "object" && value !== null && value.type === "webhook_task_result" && typeof value.id === "string" && typeof value.ok === "boolean";
}
__name(isChannelWebhookTaskResultMessage, "isChannelWebhookTaskResultMessage");

// packages/cli/src/runtime/channel-delivery-ipc.ts
init_esbuild_shims();
import { randomUUID as randomUUID2 } from "node:crypto";
var ChannelDeliveryError = class extends Error {
  constructor(code, message) {
    super(message);
    this.code = code;
    this.name = "ChannelDeliveryError";
  }
  static {
    __name(this, "ChannelDeliveryError");
  }
};
function isChannelDeliveryErrorCode(value) {
  return typeof value === "string" && CHANNEL_DELIVERY_ERROR_CODES.has(value);
}
__name(isChannelDeliveryErrorCode, "isChannelDeliveryErrorCode");
function isChannelDeliveryError(value) {
  return value instanceof ChannelDeliveryError || typeof value === "object" && value !== null && isChannelDeliveryErrorCode(value.code) && typeof value.message === "string";
}
__name(isChannelDeliveryError, "isChannelDeliveryError");
var CHANNEL_DELIVERY_IPC_TIMEOUT_MS = 3e4;
var MAX_CHANNEL_DELIVERIES_IN_FLIGHT = 16;
var MAX_CHANNEL_DELIVERY_TEXT_LENGTH = 1e5;
function createChannelDeliveryMessage(request) {
  return {
    type: "channel_delivery",
    id: randomUUID2(),
    expiresAt: Date.now() + CHANNEL_DELIVERY_IPC_TIMEOUT_MS,
    request
  };
}
__name(createChannelDeliveryMessage, "createChannelDeliveryMessage");
function isNonEmptyString(value) {
  return typeof value === "string" && value.trim().length > 0;
}
__name(isNonEmptyString, "isNonEmptyString");
function isChannelDeliveryTarget(value) {
  if (typeof value !== "object" || value === null) return false;
  const target = value;
  return (target["type"] === "user" || target["type"] === "chat") && isNonEmptyString(target["id"]) && Object.keys(target).every((key) => key === "type" || key === "id");
}
__name(isChannelDeliveryTarget, "isChannelDeliveryTarget");
function isChannelDeliveryRequest(value) {
  if (typeof value !== "object" || value === null) return false;
  const request = value;
  return isNonEmptyString(request["deliveryId"]) && isNonEmptyString(request["channelName"]) && isChannelDeliveryTarget(request["target"]) && isNonEmptyString(request["text"]) && request["text"].length <= MAX_CHANNEL_DELIVERY_TEXT_LENGTH;
}
__name(isChannelDeliveryRequest, "isChannelDeliveryRequest");
function isChannelDeliveryMessage(value) {
  if (typeof value !== "object" || value === null) return false;
  const message = value;
  return message["type"] === "channel_delivery" && isNonEmptyString(message["id"]) && typeof message["expiresAt"] === "number" && Number.isFinite(message["expiresAt"]) && isChannelDeliveryRequest(message["request"]);
}
__name(isChannelDeliveryMessage, "isChannelDeliveryMessage");
function isChannelDeliveryResultMessage(value) {
  if (typeof value !== "object" || value === null) return false;
  const message = value;
  if (message["type"] !== "channel_delivery_result" || !isNonEmptyString(message["id"]) || typeof message["ok"] !== "boolean") {
    return false;
  }
  if (message["ok"]) return true;
  return isChannelDeliveryErrorCode(message["code"]) && typeof message["error"] === "string";
}
__name(isChannelDeliveryResultMessage, "isChannelDeliveryResultMessage");

// packages/cli/src/serve/channel-worker-startup-ipc.ts
init_esbuild_shims();
var MAX_CHANNEL_STARTUP_FAILURES = 64;
var MAX_CHANNEL_STARTUP_FAILURE_CHANNEL_LENGTH = 128;
var MAX_CHANNEL_STARTUP_FAILURE_CODE_LENGTH = 64;
var MAX_CHANNEL_STARTUP_FAILURE_MESSAGE_LENGTH = 512;
function isBoundedNonEmptyString(value, maxLength) {
  if (typeof value !== "string" || value.length === 0) return false;
  let length = 0;
  for (const _codePoint of value) {
    length += 1;
    if (length > maxLength) return false;
  }
  return true;
}
__name(isBoundedNonEmptyString, "isBoundedNonEmptyString");
function isChannelStartupFailure(value) {
  if (!value || typeof value !== "object" || Array.isArray(value)) {
    return false;
  }
  try {
    const failure = value;
    return isBoundedNonEmptyString(
      failure["channel"],
      MAX_CHANNEL_STARTUP_FAILURE_CHANNEL_LENGTH
    ) && failure["phase"] === "connect" && (failure["code"] === void 0 || isBoundedNonEmptyString(
      failure["code"],
      MAX_CHANNEL_STARTUP_FAILURE_CODE_LENGTH
    )) && isBoundedNonEmptyString(
      failure["message"],
      MAX_CHANNEL_STARTUP_FAILURE_MESSAGE_LENGTH
    );
  } catch {
    return false;
  }
}
__name(isChannelStartupFailure, "isChannelStartupFailure");
function isChannelStartupReportMessage(value) {
  if (!value || typeof value !== "object" || Array.isArray(value)) {
    return false;
  }
  try {
    const message = value;
    if (message["type"] === "channel_startup_failures_truncated") {
      return true;
    }
    return message["type"] === "channel_startup_failure" && isChannelStartupFailure(message["failure"]);
  } catch {
    return false;
  }
}
__name(isChannelStartupReportMessage, "isChannelStartupReportMessage");
function isChannelStartupReportAckMessage(value) {
  try {
    return typeof value === "object" && value !== null && !Array.isArray(value) && value.type === "channel_startup_report_ack";
  } catch {
    return false;
  }
}
__name(isChannelStartupReportAckMessage, "isChannelStartupReportAckMessage");
function isChannelStartupReportType(value) {
  if (!value || typeof value !== "object" || Array.isArray(value)) {
    return false;
  }
  try {
    const type = value.type;
    return type === "channel_startup_failure" || type === "channel_startup_failures_truncated";
  } catch {
    return false;
  }
}
__name(isChannelStartupReportType, "isChannelStartupReportType");

export {
  CHANNEL_DAEMON_WORKER_SENTINEL,
  CHANNEL_WORKER_HEARTBEAT_INTERVAL_MS,
  QWEN_DAEMON_TOKEN_ENV,
  QWEN_DAEMON_URL_ENV,
  QWEN_DAEMON_WORKSPACE_ENV,
  QWEN_SERVER_TOKEN_ENV,
  ChannelWebhookEnqueueError,
  isChannelWebhookEnqueueErrorCode,
  isChannelWebhookEnqueueError,
  CHANNEL_WEBHOOK_TASK_IPC_TIMEOUT_MS,
  createChannelWebhookTaskMessage,
  isChannelWebhookTaskMessage,
  isChannelWebhookTaskResultMessage,
  ChannelDeliveryError,
  isChannelDeliveryError,
  CHANNEL_DELIVERY_IPC_TIMEOUT_MS,
  MAX_CHANNEL_DELIVERIES_IN_FLIGHT,
  MAX_CHANNEL_DELIVERY_TEXT_LENGTH,
  createChannelDeliveryMessage,
  isChannelDeliveryMessage,
  isChannelDeliveryResultMessage,
  MAX_CHANNEL_STARTUP_FAILURES,
  MAX_CHANNEL_STARTUP_FAILURE_CHANNEL_LENGTH,
  MAX_CHANNEL_STARTUP_FAILURE_CODE_LENGTH,
  MAX_CHANNEL_STARTUP_FAILURE_MESSAGE_LENGTH,
  isChannelStartupReportMessage,
  isChannelStartupReportAckMessage,
  isChannelStartupReportType
};
/**
 * @license
 * Copyright 2026 Qwen Team
 * SPDX-License-Identifier: Apache-2.0
 */
