All files / dingtalk-openclaw-connector/src/sdk types.ts

0% Statements 0/1
100% Branches 1/1
100% Functions 1/1
0% Lines 0/1

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
/**
 * DingTalk Connector SDK Types
 * 
 * 完全独立的类型定义,不依赖任何外部 SDK。
 * 这是钉钉连接器插件的核心类型系统。
 */
 
// ============================================================================
// 基础类型
// ============================================================================
 
/**
 * SecretInput 支持多种输入方式
 */
export type SecretInput = 
  | string // 直接字符串
  | SecretInputRef; // 引用
 
/**
 * SecretInput 引用类型
 */
export interface SecretInputRef {
  source: "env" | "file" | "exec";
  provider: string;
  id: string;
}
 
/**
 * 工具权限策略
 */
export interface ToolPolicy {
  allow?: string[];
  deny?: string[];
}
 
/**
 * DM 策略
 */
export type DmPolicy = "open" | "pairing" | "allowlist";
 
/**
 * 群组策略
 */
export type GroupPolicy = "open" | "allowlist" | "disabled";
 
/**
 * 会话作用域
 */
export type GroupSessionScope = "group" | "group_sender";
 
/**
 * 发送模式
 */
export type DeliveryMode = "direct" | "queued";
 
// ============================================================================
// 配置类型
// ============================================================================
 
/**
 * 钉钉工具配置
 */
export interface DingtalkToolsConfig {
  docs?: boolean;
  media?: boolean;
}
 
/**
 * 钉钉群组配置
 */
export interface DingtalkGroupConfig {
  requireMention?: boolean;
  tools?: ToolPolicy;
  enabled?: boolean;
  allowFrom?: (string | number)[];
  systemPrompt?: string;
  groupSessionScope?: GroupSessionScope;
}
 
/**
 * 钉钉账号配置
 */
export interface DingtalkAccountConfig {
  enabled?: boolean;
  name?: string;
  clientId?: string | number;
  clientSecret?: SecretInput;
  enableMediaUpload?: boolean;
  systemPrompt?: string;
  dmPolicy?: DmPolicy;
  allowFrom?: (string | number)[];
  groupPolicy?: GroupPolicy;
  groupAllowFrom?: (string | number)[];
  requireMention?: boolean;
  groupSessionScope?: GroupSessionScope;
  separateSessionByConversation?: boolean;
  historyLimit?: number;
  dmHistoryLimit?: number;
  textChunkLimit?: number;
  mediaMaxMb?: number;
  typingIndicator?: boolean;
  tools?: DingtalkToolsConfig;
}
 
/**
 * 钉钉通道配置
 */
export interface DingtalkConfig {
  enabled?: boolean;
  defaultAccount?: string;
  clientId?: string | number;
  clientSecret?: SecretInput;
  enableMediaUpload?: boolean;
  systemPrompt?: string;
  dmPolicy?: DmPolicy;
  allowFrom?: (string | number)[];
  groupPolicy?: GroupPolicy;
  groupAllowFrom?: (string | number)[];
  requireMention?: boolean;
  groupSessionScope?: GroupSessionScope;
  separateSessionByConversation?: boolean;
  historyLimit?: number;
  dmHistoryLimit?: number;
  textChunkLimit?: number;
  mediaMaxMb?: number;
  typingIndicator?: boolean;
  resolveSenderNames?: boolean;
  tools?: DingtalkToolsConfig;
  groups?: Record<string, DingtalkGroupConfig>;
  accounts?: Record<string, DingtalkAccountConfig>;
}
 
/**
 * 通道配置映射
 */
export interface ChannelsConfig {
  [key: string]: unknown;
  "dingtalk-connector"?: DingtalkConfig;
}
 
/**
 * 全局配置
 */
export interface ClawdbotConfig {
  channels?: ChannelsConfig;
  [key: string]: unknown;
}
 
// ============================================================================
// 通道插件类型
// ============================================================================
 
/**
 * 通道元数据
 */
export interface ChannelMeta {
  id: string;
  label: string;
  selectionLabel: string;
  docsPath: string;
  docsLabel: string;
  blurb: string;
  aliases: string[];
  order: number;
}
 
/**
 * 通道能力
 */
export interface ChannelCapabilities {
  chatTypes: ("direct" | "group")[];
  polls: boolean;
  threads: boolean;
  media: boolean;
  reactions: boolean;
  edit: boolean;
  reply: boolean;
  nativeCommands?: boolean;
}
 
/**
 * 配对功能
 */
export interface ChannelPairing {
  idLabel: string;
  normalizeAllowEntry: (entry: string) => string;
  notifyApproval: (params: { cfg: ClawdbotConfig; id: string }) => Promise<void>;
}
 
/**
 * Agent 提示
 */
export interface ChannelAgentPrompt {
  messageToolHints: () => string[];
}
 
/**
 * 群组功能
 */
export interface ChannelGroups {
  resolveToolPolicy: (params: { account: unknown; groupId: string }) => ToolPolicy | undefined;
}
 
/**
 * 提及功能
 */
export interface ChannelMentions {
  stripPatterns: () => RegExp[];
}
 
/**
 * 重载配置
 */
export interface ChannelReload {
  configPrefixes: string[];
}
 
/**
 * 配置 Schema
 */
export interface ChannelConfigSchema {
  schema: unknown;
}
 
/**
 * 配置管理
 */
export interface ChannelConfig<TAccount> {
  listAccountIds: (cfg: ClawdbotConfig) => string[];
  resolveAccount: (cfg: ClawdbotConfig, accountId: string) => TAccount;
  defaultAccountId: (cfg: ClawdbotConfig) => string;
  setAccountEnabled: (params: { cfg: ClawdbotConfig; accountId: string; enabled: boolean }) => ClawdbotConfig;
  deleteAccount: (params: { cfg: ClawdbotConfig; accountId: string }) => ClawdbotConfig;
  isConfigured: (account: TAccount) => boolean;
  describeAccount: (account: TAccount) => unknown;
  resolveAllowFrom: (params: { cfg: ClawdbotConfig; accountId: string }) => string[];
  formatAllowFrom: (params: { allowFrom: (string | number)[] }) => string[];
}
 
/**
 * 安全功能
 */
export interface ChannelSecurity<TAccount> {
  collectWarnings: (params: { cfg: ClawdbotConfig; accountId: string }) => string[];
}
 
/**
 * 设置功能
 */
export interface ChannelSetup {
  resolveAccountId: () => string;
  applyAccountConfig: (params: { cfg: ClawdbotConfig; accountId: string }) => ClawdbotConfig;
}
 
/**
 * 入引导适配器
 */
export interface ChannelOnboardingAdapter {
  channel: string;
  getStatus: (params: { cfg: ClawdbotConfig }) => Promise<{
    channel: string;
    configured: boolean;
    statusLines: string[];
    selectionHint: string;
    quickstartScore: number;
  }>;
  configure: (params: { cfg: ClawdbotConfig; prompter: unknown }) => Promise<{
    cfg: ClawdbotConfig;
    accountId: string;
  }>;
  dmPolicy?: {
    label: string;
    channel: string;
    policyKey: string;
    allowFromKey: string;
    getCurrent: (cfg: ClawdbotConfig) => string;
    setPolicy: (cfg: ClawdbotConfig, policy: string) => ClawdbotConfig;
    promptAllowFrom: (params: { cfg: ClawdbotConfig; prompter: unknown }) => Promise<ClawdbotConfig>;
  };
  disable: (cfg: ClawdbotConfig) => ClawdbotConfig;
}
 
/**
 * 消息功能
 */
export interface ChannelMessaging {
  normalizeTarget: (raw: string) => string | undefined;
  targetResolver: {
    looksLikeId: (raw: string) => boolean;
    hint: string;
  };
}
 
/**
 * 目录功能
 */
export interface ChannelDirectory {
  self: () => Promise<unknown>;
  listPeers: (params: { cfg: ClawdbotConfig; query?: string; limit?: number; accountId?: string }) => Promise<unknown[]>;
  listGroups: (params: { cfg: ClawdbotConfig; query?: string; limit?: number; accountId?: string }) => Promise<unknown[]>;
  listPeersLive: (params: { cfg: ClawdbotConfig; query?: string; limit?: number; accountId?: string }) => Promise<unknown[]>;
  listGroupsLive: (params: { cfg: ClawdbotConfig; query?: string; limit?: number; accountId?: string }) => Promise<unknown[]>;
}
 
/**
 * 发送结果
 */
export interface SendResult {
  channel: string;
  messageId: string;
  conversationId: string;
}
 
/**
 * 出站消息功能
 */
export interface ChannelOutbound {
  deliveryMode: DeliveryMode;
  chunker: (text: string, limit: number) => string[];
  chunkerMode: "markdown" | "text";
  textChunkLimit: number;
  sendText: (params: {
    cfg: ClawdbotConfig;
    to: string;
    text: string;
    accountId?: string;
    replyToId?: string;
    threadId?: string;
  }) => Promise<SendResult>;
  sendMedia: (params: {
    cfg: ClawdbotConfig;
    to: string;
    text?: string;
    mediaUrl?: string;
    accountId?: string;
    mediaLocalRoots?: string[];
    replyToId?: string;
    threadId?: string;
  }) => Promise<SendResult>;
}
 
/**
 * 探测结果
 */
export interface BaseProbeResult<T> {
  ok: boolean;
  error?: string;
  data?: T;
}
 
/**
 * 运行时状态
 */
export interface ChannelRuntimeState {
  running: boolean;
  lastStartAt: string | null;
  lastStopAt: string | null;
  lastError: string | null;
  port: number | null;
  [key: string]: unknown;
}
 
/**
 * 状态功能
 */
export interface ChannelStatus<TAccount> {
  defaultRuntime: ChannelRuntimeState;
  buildChannelSummary: (params: { snapshot: unknown }) => unknown;
  probeAccount: (params: { account: TAccount }) => Promise<BaseProbeResult<unknown>>;
  buildAccountSnapshot: (params: { account: TAccount; runtime?: ChannelRuntimeState; probe?: BaseProbeResult<unknown> }) => unknown;
}
 
/**
 * 网关启动上下文
 */
export interface GatewayStartContext {
  cfg: ClawdbotConfig;
  accountId: string;
  runtime: ChannelRuntimeState;
  abortSignal: AbortSignal;
  setStatus: (params: { accountId: string; port: number | null }) => void;
  log?: {
    info: (message: string) => void;
    error: (message: string) => void;
    warn: (message: string) => void;
  };
}
 
/**
 * 网关功能
 */
export interface ChannelGateway {
  startAccount: (ctx: GatewayStartContext) => Promise<void>;
}
 
/**
 * 通道插件接口
 */
export interface ChannelPlugin<TAccount> {
  id: string;
  meta: ChannelMeta;
  pairing?: ChannelPairing;
  capabilities?: ChannelCapabilities;
  agentPrompt?: ChannelAgentPrompt;
  groups?: ChannelGroups;
  mentions?: ChannelMentions;
  reload?: ChannelReload;
  configSchema?: ChannelConfigSchema;
  config: ChannelConfig<TAccount>;
  security?: ChannelSecurity<TAccount>;
  setup?: ChannelSetup;
  onboarding?: ChannelOnboardingAdapter;
  messaging?: ChannelMessaging;
  directory?: ChannelDirectory;
  outbound?: ChannelOutbound;
  status?: ChannelStatus<TAccount>;
  gateway?: ChannelGateway;
  streaming?: unknown;
  actions?: unknown;
  resolver?: unknown;
  threading?: unknown;
}
 
/**
 * 插件 API
 */
export interface PluginApi {
  registerChannel: (opts: { plugin: ChannelPlugin<unknown> }) => void;
  runtime: {
    env: Record<string, string | undefined>;
    [key: string]: unknown;
  };
  [key: string]: unknown;
}
 
// ============================================================================
// 常量
// ============================================================================
 
/**
 * 默认账号 ID
 */
export const DEFAULT_ACCOUNT_ID = "default" as const;
 
// ============================================================================
// 运行时类型
// ============================================================================
 
/**
 * 运行时环境
 */
export interface RuntimeEnv {
  env: Record<string, string | undefined>;
  [key: string]: unknown;
}
 
/**
 * 历史记录条目
 */
export interface HistoryEntry {
  role: "user" | "assistant" | "system";
  content: string;
  timestamp?: string;
  [key: string]: unknown;
}
 
/**
 * 插件运行时
 */
export interface PluginRuntime {
  env: RuntimeEnv;
  [key: string]: unknown;
}
 
// ============================================================================
// 向导类型
// ============================================================================
 
/**
 * 向导提示器
 */
export interface WizardPrompter {
  note: (message: string, title?: string) => Promise<void>;
  text: (params: {
    message: string;
    placeholder?: string;
    initialValue?: string;
    validate?: (value: unknown) => string | undefined;
  }) => Promise<string>;
  select: <T>(params: {
    message: string;
    options: Array<{ value: T; label: string }>;
    initialValue?: T;
  }) => Promise<T>;
  confirm: (params: {
    message: string;
    initialValue?: boolean;
  }) => Promise<boolean>;
  [key: string]: unknown;
}
 
/**
 * DM 策略入引导
 */
export interface ChannelOnboardingDmPolicy {
  label: string;
  channel: string;
  policyKey: string;
  allowFromKey: string;
  getCurrent: (cfg: ClawdbotConfig) => string;
  setPolicy: (cfg: ClawdbotConfig, policy: string) => ClawdbotConfig;
  promptAllowFrom: (params: { cfg: ClawdbotConfig; prompter: WizardPrompter }) => Promise<ClawdbotConfig>;
}