Skip to content

Add high-level SFS2X client/server API with typed models#18

Merged
Zewsic merged 1 commit into
masterfrom
claude/loving-mayer-repmjh
Jun 15, 2026
Merged

Add high-level SFS2X client/server API with typed models#18
Zewsic merged 1 commit into
masterfrom
claude/loving-mayer-repmjh

Conversation

@Zewsic

@Zewsic Zewsic commented Jun 15, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR introduces a complete high-level application layer for ZewSFS, transforming it from a low-level protocol implementation into a developer-friendly framework with decorator-driven handlers, typed request/response models, and async client/server lifecycle management.

Key Changes

  • New sfs2x.app module — High-level API built on top of the existing transport and protocol layers

    • SFSClient — Decorator-driven async client with @client.on_login(), @client.on_extension(command="..."), lifecycle hooks
    • SFSServer — Decorator-driven async server with per-session handlers and ServerSession state management
    • SFSModel — Typed dataclass-like models with SFS field annotations (UtfString, Int, etc.) that auto-serialize/deserialize to SFSObject
    • SystemRequest/SystemResponse and ExtensionRequest/ExtensionResponse — Base classes for typed request/response pairs
    • Built-in system models — HandshakeRequest, LoginRequest, LoginResponse, LoginErrorResponse, LogoutRequest, PingPongRequest and their responses
  • Decorator handlers — Register async functions with @client.on_login(), @server.on_extension(command="..."), etc.

    • Automatic parameter binding from Message, SFSObject, ServerSession, or typed SFSModel subclasses
    • Union dispatch — handlers can declare req: LoginResponse | LoginErrorResponse and the first matching schema wins
    • raise to reply — handlers can raise ResponseModel(...) or return ResponseModel(...) to send replies
  • Handler registry and dispatcherHandlerRegistry stores decorated handlers; Dispatcher routes incoming messages to handlers by introspecting signatures and binding parameters

  • Session objectsClientSession and ServerSession provide state dicts, connection metadata, and send()/kick() methods

  • Comprehensive test suitetest_app_models.py, test_app_dispatcher.py, test_app_integration.py covering model serialization, handler dispatch, and end-to-end client/server roundtrips

  • Documentation — New docs/lowlevel.md and docs/protocol.md reference guides for the protocol and transport layers

  • Updated README — Simplified introduction with high-level examples, feature list focused on the new API, and installation instructions

  • Package exports — Top-level sfs2x/__init__.py re-exports the high-level API for convenience

Implementation Details

  • SFSModel metaclass — Compiles field annotations at class definition time into FieldSpec tuples, generates __init__, __repr__, __eq__, and serialization methods
  • Typed field bindingfield(sfs_key, default=..., default_factory=...) marks attributes and binds them to SFSObject keys; supports nested models and optional fields
  • Handler introspectioninspect_handler() walks function signatures and classifies parameters (MESSAGE, PAYLOAD, SESSION, MODEL, UNION_MODEL) for runtime binding
  • Exception-based replies — Response models inherit from Exception so raise works; Reply(model) wrapper for non-Response models
  • Lifecycle hookson_connect, on_disconnect, on_error callbacks fire at appropriate times in the dispatcher loop

All changes are backward compatible — the low-level sfs2x.core, sfs2x.protocol, and sfs2x.transport APIs remain unchanged.

https://claude.ai/code/session_01WDQmcTcMNhA9uDjpztRFao

Introduces a decorator-driven client/server layer on top of the existing
core/protocol/transport stack:

- SFSModel + field(): annotate attributes with SFS field classes
  (UtfString, Int, Long, nested SFSModel, SFSObject/SFSArray) and the
  metaclass generates __init__, to_sfs_object() and from_sfs_object()
- SystemRequest/Response and ExtensionRequest/Response bases with
  class kwargs (action=SysAction.X, command="...") and a _RaisableMixin
  so handlers can `raise PongResponse(n=42)` to reply
- HandlerRegistry + dispatcher that introspect handler signatures and
  inject Message, SFSObject payload, ClientSession/ServerSession,
  parsed models, or Union[A, B] (first matching schema wins)
- SFSClient/SFSServer facades with on_login / on_handshake /
  on_extension(command="...") / on_message / on_connect / on_disconnect
  decorators and async context-manager lifecycle
- Built-in models for HANDSHAKE / LOGIN (+ error) / LOGOUT / PING_PONG
- README rewritten around the high-level API; low-level reference moved
  to docs/lowlevel.md and docs/protocol.md
- 37 new tests (models, dispatcher, integration over loopback TCP)

https://claude.ai/code/session_01WDQmcTcMNhA9uDjpztRFao
@Zewsic Zewsic merged commit 73dba9e into master Jun 15, 2026
1 check failed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants