-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpec.json
More file actions
152 lines (152 loc) · 6.85 KB
/
Spec.json
File metadata and controls
152 lines (152 loc) · 6.85 KB
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
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "MinimalMessagingProtocolSpec",
"version": "1.0.0",
"meta": {
"description": "Cross-platform specification for robust offline-first point-to-point messaging.",
"transport": "WebSocket",
"wire_format": "JSON",
"server_reference_note": "The server implementation in Server/server.py is constant. Use and inspect Server/server.py to build the clients efficiently and guarantee complete wire-format compatibility."
},
"data_models": {
"ClientToServer": {
"connect": {
"description": "Sent by client immediately upon establishing connection to authenticate identity.",
"properties": {
"type": { "type": "string", "const": "connect" },
"username": { "type": "string", "pattern": "^[a-zA-Z0-9_-]{1,32}$" }
},
"required": ["type", "username"]
},
"send_msg": {
"description": "Dispatched by client to transmit a text message to a specific peer.",
"properties": {
"type": { "type": "string", "const": "send_msg" },
"msg_id": { "type": "string", "format": "uuid", "description": "Client-generated unique ID for tracking lifecycle and deduplication." },
"to": { "type": "string", "description": "Username of target recipient." },
"body": { "type": "string", "description": "Text payload content." }
},
"required": ["type", "msg_id", "to", "body"]
},
"ack": {
"description": "Sent by client confirming a message has been received. Authorizes server to purge it from its offline buffer.",
"properties": {
"type": { "type": "string", "const": "ack" },
"msg_id": { "type": "string", "format": "uuid" }
},
"required": ["type", "msg_id"]
}
},
"ServerToClient": {
"msg": {
"description": "Inbound message pushed by the server, either in real-time or as a flushed buffer entry.",
"properties": {
"type": { "type": "string", "const": "msg" },
"msg_id": { "type": "string", "format": "uuid" },
"from": { "type": "string" },
"body": { "type": "string" }
},
"required": ["type", "msg_id", "from", "body"]
},
"receipt": {
"description": "Server confirmation acknowledging receipt of a sent message. Signals client to remove the entry from its local outbound queue.",
"properties": {
"type": { "type": "string", "const": "receipt" },
"msg_id": { "type": "string", "format": "uuid" }
},
"required": ["type", "msg_id"]
}
}
},
"local_storage_schemas": {
"outbound_queue": {
"description": "Persistent transactional log for queuing messages while offline.",
"fields": {
"msg_id": { "type": "string", "primary_key": true },
"to": { "type": "string", "description": "Recipient username." },
"body": { "type": "string" },
"status": {
"type": "string",
"enum": ["QUEUED", "PENDING_RECEIPT"],
"description": "Tracks delivery state."
},
"created_at": {
"type": "string",
"format": "iso8601-utc",
"description": "Insertion timestamp (UTC, ISO-8601). Used for FIFO ordering."
}
}
},
"inbound_history": {
"description": "Idempotent-safe log to prevent duplicate rendering from network retries.",
"fields": {
"msg_id": { "type": "string", "primary_key": true },
"from": { "type": "string" },
"body": { "type": "string" },
"timestamp": { "type": "integer" }
}
}
},
"state_machine": {
"states": ["UNINITIALIZED", "DISCONNECTED", "CONNECTING", "CONNECTED"],
"initial_state": "UNINITIALIZED",
"transitions": [
{
"from": "UNINITIALIZED",
"event": "INITIALIZE",
"action": "Set username, initialize local storage engines",
"to": "DISCONNECTED"
},
{
"from": "DISCONNECTED",
"event": "NETWORK_AVAILABLE",
"action": "Initiate WebSocket connection attempt",
"to": "CONNECTING"
},
{
"from": "CONNECTING",
"event": "SOCKET_OPEN",
"action": "Transmit 'connect' payload immediately. Transition to live system state.",
"to": "CONNECTED"
},
{
"from": "CONNECTED",
"event": "SOCKET_CLOSE",
"action": "Revert all PENDING_RECEIPT outbound messages back to QUEUED.",
"to": "DISCONNECTED"
}
],
"reactive_behaviors": {
"ON_CONNECTED": {
"description": "Triggered automatically when shifting into CONNECTED state.",
"sequence": [
"Scan outbound_queue for all entries.",
"For each item: transmit raw 'send_msg' JSON frame to socket."
]
},
"ON_USER_ACTION_SEND": {
"description": "Triggered when a user hits send in the interface.",
"sequence": [
"Generate cryptographically secure v4 UUID as msg_id.",
"Insert into outbound_queue with status = QUEUED.",
"If state == CONNECTED: dispatch 'send_msg' frame immediately. Else: remain in queue."
]
},
"ON_RECEIVE_RECEIPT": {
"description": "Triggered when server responds with type: 'receipt'.",
"sequence": [
"Match receipt.msg_id against entries in outbound_queue.",
"Delete the matched record from outbound_queue completely."
]
},
"ON_RECEIVE_MSG": {
"description": "Triggered when server pushes an inbound type: 'msg'.",
"sequence": [
"Check inbound_history for pre-existing msg_id.",
"If NOT present: write to inbound_history, dispatch to message handler.",
"Always: send immediate raw 'ack' JSON frame back to server containing msg_id."
]
}
}
}
}