AgentSkillsCN

add-transport

为 ShareGo 实现全新传输层的指南(例如 WebRTC、蓝牙)。当您需要添加新的传输实现、优化传输接口,或在用户询问传输的可移植性时,请使用本指南。

SKILL.md
--- frontmatter
name: add-transport
description: Guide for implementing a new transport layer for ShareGo (e.g. WebRTC, Bluetooth). Use when adding a new transport implementation, working on the transport interface, or when the user asks about transport portability.

Adding a new transport

ShareGo uses the ILocalTransport interface to abstract the network layer. This allows swapping WebSocket for WebRTC (or anything else) without changing crypto, protocol, or session logic.

Steps

  1. Create a new file in core/src/transport/ (e.g. webrtc-transport.ts)

  2. Implement ILocalTransport from core/src/transport/types.ts:

typescript
import type { ILocalTransport, TransportState, TransportStateCallback, MessageCallback } from "./types.js";

export class WebRTCTransport implements ILocalTransport {
  async listen(port: number): Promise<void> { /* ... */ }
  async connect(addr: string): Promise<void> { /* ... */ }
  send(data: Uint8Array): void { /* ... */ }
  onMessage(cb: MessageCallback): void { /* ... */ }
  offMessage(cb: MessageCallback): void { /* ... */ }
  onStateChange(cb: TransportStateCallback): void { /* ... */ }
  offStateChange(cb: TransportStateCallback): void { /* ... */ }
  close(): void { /* ... */ }
  getState(): TransportState { /* ... */ }
  getLocalAddress(): string | null { /* ... */ }
}
  1. Key requirements:

    • listen() = receiver role (wait for incoming connection)
    • connect() = sender role (connect to receiver)
    • Only one peer per transport (2-user session limit)
    • Reject additional connections after the first peer
    • Fire onStateChange callbacks on every state transition
    • send() must throw if no peer is connected
    • close() must release all resources
  2. Export from core/src/transport/index.ts

  3. Test by wiring it into a Session and running the handshake

  4. Platform adapters — if the transport requires platform-specific code (e.g. native sockets), create adapters in each app shell that implement the same interface:

    • Desktop: apps/desktop-tauri/src/adapters/
    • Mobile: apps/mobile-rn/src/adapters/
  5. Update docs — add the new transport to docs/ARCHITECTURE.md (tech stack section) and mention any security implications in docs/THREAT_MODEL.md

Checklist

  • Implements all ILocalTransport methods (including offMessage and offStateChange)
  • Rejects second peer connection
  • Fires state callbacks: idle -> listening/connected -> disconnected -> closed
  • send() throws when no peer connected
  • close() releases all resources
  • offMessage() and offStateChange() properly unregister callbacks
  • Exported from transport barrel (core/src/transport/index.ts)
  • Works on all target platforms for this transport type
  • Platform adapters created for desktop and mobile (if needed)
  • Unit tests covering connection, messaging, disconnection, and rejection
  • docs/ARCHITECTURE.md updated
  • docs/THREAT_MODEL.md updated if security surface changes