From e63c403623f53529b08e46d41451f3e138433938 Mon Sep 17 00:00:00 2001 From: sh <37271604+shumvgolove@users.noreply.github.com> Date: Tue, 12 May 2026 11:32:01 +0000 Subject: [PATCH] simplex-chat-python: add python library (#6954) * docs: simplex-chat-python design and implementation plan * bots: Python wire types codegen * simplex-chat-python: package scaffold * simplex-chat-python: native libsimplex loader * simplex-chat-python: async FFI wrappers * simplex-chat-python: ChatApi with 49 api methods * simplex-chat-python: Bot class with decorators and dispatch * simplex-chat-python: install CLI, example bot, README * simplex-chat-python: audit fixes * bots: regenerate API docs and types Catches up the markdown, TypeScript and Python codegen outputs with two upstream schema changes: - APIConnectPlan.connectionLink became optional (from sh/python-lib audit fixes); cmdString and EBNF syntax now reflect optional parameter. - APIAddGroupRelays command and CRGroupRelaysAdded/CRGroupRelaysAddFailed responses added in #6917 (relay management). The TS and markdown outputs were regenerated when #6917 landed but the Python types module only got the new entries with this regeneration. * core: refresh SQLite query plans after relay_inactive_at migration The M20260507_relay_inactive_at migration (#6917 / #6952) shifted the query plans that 'Save query plans' verifies. Regenerated via the test that owns those snapshots; no behavioral change. * bots: keep APIConnectPlan connectionLink as required parameter The prior audit-fixes commit changed the syntax expression to `Optional ...` because the Haskell field is `connectionLink :: Maybe AConnectionLink`. That misrepresents the API contract: the `Maybe` is purely an internal signal for link-parsing failure (the handler returns `CEInvalidConnReq` on `Nothing`), not API-level optionality. Callers MUST always pass a connection link. Revert the syntax expression to `Param "connectionLink"` and add a comment so the intent is preserved next time someone audits. Regenerates COMMANDS.md, commands.ts and _commands.py to match. --- bots/api/COMMANDS.md | 14 +- bots/api/TYPES.md | 2 +- bots/src/API/Docs/Commands.hs | 1 + bots/src/API/Docs/Generate.hs | 2 +- bots/src/API/Docs/Generate/Python.hs | 322 ++ bots/src/API/Docs/Syntax.hs | 10 +- packages/simplex-chat-python/.gitignore | 22 + packages/simplex-chat-python/LICENSE | 661 ++++ packages/simplex-chat-python/README.md | 70 + .../examples/squaring_bot.py | 52 + packages/simplex-chat-python/pyproject.toml | 58 + .../src/simplex_chat/__init__.py | 59 + .../src/simplex_chat/__main__.py | 35 + .../src/simplex_chat/_native.py | 257 ++ .../src/simplex_chat/_version.py | 9 + .../src/simplex_chat/api.py | 704 ++++ .../src/simplex_chat/bot.py | 707 ++++ .../src/simplex_chat/core.py | 200 + .../src/simplex_chat/filters.py | 45 + .../src/simplex_chat/py.typed | 0 .../src/simplex_chat/types/__init__.py | 16 + .../src/simplex_chat/types/_commands.py | 705 ++++ .../src/simplex_chat/types/_events.py | 379 ++ .../src/simplex_chat/types/_responses.py | 360 ++ .../src/simplex_chat/types/_types.py | 3506 +++++++++++++++++ .../src/simplex_chat/util.py | 128 + .../tests/test_bot_registration.py | 357 ++ .../simplex-chat-python/tests/test_codegen.py | 41 + .../simplex-chat-python/tests/test_filters.py | 83 + .../tests/test_native_cache.py | 92 + .../tests/test_native_url.py | 55 + .../simplex-chat-python/tests/test_util.py | 175 + .../2026-05-07-simplex-chat-python-design.md | 575 +++ ...5-07-simplex-chat-python-implementation.md | 2348 +++++++++++ simplex-chat.cabal | 1 + .../SQLite/Migrations/agent_query_plans.txt | 41 +- .../SQLite/Migrations/chat_query_plans.txt | 2 +- tests/APIDocs.hs | 6 + 38 files changed, 12051 insertions(+), 49 deletions(-) create mode 100644 bots/src/API/Docs/Generate/Python.hs create mode 100644 packages/simplex-chat-python/.gitignore create mode 100644 packages/simplex-chat-python/LICENSE create mode 100644 packages/simplex-chat-python/README.md create mode 100644 packages/simplex-chat-python/examples/squaring_bot.py create mode 100644 packages/simplex-chat-python/pyproject.toml create mode 100644 packages/simplex-chat-python/src/simplex_chat/__init__.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/__main__.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/_native.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/_version.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/api.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/bot.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/core.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/filters.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/py.typed create mode 100644 packages/simplex-chat-python/src/simplex_chat/types/__init__.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/types/_commands.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/types/_events.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/types/_responses.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/types/_types.py create mode 100644 packages/simplex-chat-python/src/simplex_chat/util.py create mode 100644 packages/simplex-chat-python/tests/test_bot_registration.py create mode 100644 packages/simplex-chat-python/tests/test_codegen.py create mode 100644 packages/simplex-chat-python/tests/test_filters.py create mode 100644 packages/simplex-chat-python/tests/test_native_cache.py create mode 100644 packages/simplex-chat-python/tests/test_native_url.py create mode 100644 packages/simplex-chat-python/tests/test_util.py create mode 100644 plans/2026-05-07-simplex-chat-python-design.md create mode 100644 plans/2026-05-07-simplex-chat-python-implementation.md diff --git a/bots/api/COMMANDS.md b/bots/api/COMMANDS.md index ed50cdbb9a..5761f303bd 100644 --- a/bots/api/COMMANDS.md +++ b/bots/api/COMMANDS.md @@ -295,7 +295,7 @@ Send messages. ``` ```python -'/_send ' + str(sendRef) + (' live=on' if liveMessage else '') + ((' ttl=' + str(ttl)) if ttl is not None else '') + ' json ' + json.dumps(composedMessages) # Python +'/_send ' + ChatRef_cmd_string(sendRef) + (' live=on' if liveMessage else '') + ((' ttl=' + str(ttl)) if ttl is not None else '') + ' json ' + json.dumps(composedMessages) # Python ``` **Responses**: @@ -335,7 +335,7 @@ Update message. ``` ```python -'/_update item ' + str(chatRef) + ' ' + str(chatItemId) + (' live=on' if liveMessage else '') + ' json ' + json.dumps(updatedMessage) # Python +'/_update item ' + ChatRef_cmd_string(chatRef) + ' ' + str(chatItemId) + (' live=on' if liveMessage else '') + ' json ' + json.dumps(updatedMessage) # Python ``` **Responses**: @@ -382,7 +382,7 @@ Delete message. ``` ```python -'/_delete item ' + str(chatRef) + ' ' + ','.join(map(str, chatItemIds)) + ' ' + str(deleteMode) # Python +'/_delete item ' + ChatRef_cmd_string(chatRef) + ' ' + ','.join(map(str, chatItemIds)) + ' ' + str(deleteMode) # Python ``` **Responses**: @@ -464,7 +464,7 @@ Add/remove message reaction. ``` ```python -'/_reaction ' + str(chatRef) + ' ' + str(chatItemId) + ' ' + ('on' if add else 'off') + ' ' + json.dumps(reaction) # Python +'/_reaction ' + ChatRef_cmd_string(chatRef) + ' ' + str(chatItemId) + ' ' + ('on' if add else 'off') + ' ' + json.dumps(reaction) # Python ``` **Responses**: @@ -1386,7 +1386,7 @@ Connect via prepared SimpleX link. The link can be 1-time invitation link, conta ``` ```python -'/_connect ' + str(userId) + ((' ' + str(preparedLink_)) if preparedLink_ is not None else '') # Python +'/_connect ' + str(userId) + ((' ' + CreatedConnLink_cmd_string(preparedLink_)) if preparedLink_ is not None else '') # Python ``` **Responses**: @@ -1644,7 +1644,7 @@ Get chat previews. Supports time-based pagination — use this instead of APILis ``` ```python -'/_get chats ' + str(userId) + (' pcc=on' if pendingConnections else '') + ' ' + str(pagination) + ' ' + json.dumps(query) # Python +'/_get chats ' + str(userId) + (' pcc=on' if pendingConnections else '') + ' ' + PaginationByTime_cmd_string(pagination) + ' ' + json.dumps(query) # Python ``` **Responses**: @@ -1682,7 +1682,7 @@ Delete chat. ``` ```python -'/_delete ' + str(chatRef) + ' ' + str(chatDeleteMode) # Python +'/_delete ' + ChatRef_cmd_string(chatRef) + ' ' + ChatDeleteMode_cmd_string(chatDeleteMode) # Python ``` **Responses**: diff --git a/bots/api/TYPES.md b/bots/api/TYPES.md index 6145c795c9..2cd61778d5 100644 --- a/bots/api/TYPES.md +++ b/bots/api/TYPES.md @@ -1376,7 +1376,7 @@ ChatType.cmdString(chatType) + chatId + (chatScope ? GroupChatScope.cmdString(ch ``` ```python -str(chatType) + str(chatId) + ((str(chatScope)) if chatScope is not None else '') # Python +ChatType_cmd_string(chatType) + str(chatId) + ((GroupChatScope_cmd_string(chatScope)) if chatScope is not None else '') # Python ``` diff --git a/bots/src/API/Docs/Commands.hs b/bots/src/API/Docs/Commands.hs index be03b13e5c..b3eaf96837 100644 --- a/bots/src/API/Docs/Commands.hs +++ b/bots/src/API/Docs/Commands.hs @@ -134,6 +134,7 @@ chatCommandsDocsData = ( "Connection commands", "These commands may be used to create connections. Most bots do not need to use them - bot users will connect via bot address with auto-accept enabled.", [ ("APIAddContact", [], "Create 1-time invitation link.", ["CRInvitation", "CRChatCmdError"], [], Just UNInteractive, "/_connect " <> Param "userId" <> OnOffParam "incognito" "incognito" (Just False)), + -- `Maybe` in `connectionLink :: Maybe AConnectionLink` is used to signal link parsing error to the runtime (the handler returns CEInvalidConnReq on Nothing); it is NOT API-level optionality. The parameter is required from callers. ("APIConnectPlan", [], "Determine SimpleX link type and if the bot is already connected via this link.", ["CRConnectionPlan", "CRChatCmdError"], [], Just UNInteractive, "/_connect plan " <> Param "userId" <> " " <> Param "connectionLink"), ("APIConnect", [], "Connect via prepared SimpleX link. The link can be 1-time invitation link, contact address or group link.", ["CRSentConfirmation", "CRContactAlreadyExists", "CRSentInvitation", "CRChatCmdError"], [], Just UNInteractive, "/_connect " <> Param "userId" <> Optional "" (" " <> Param "$0") "preparedLink_"), ("Connect", [], "Connect via SimpleX link as string in the active user profile.", ["CRSentConfirmation", "CRContactAlreadyExists", "CRSentInvitation", "CRChatCmdError"], [], Just UNInteractive, "/connect" <> Optional "" (" " <> Param "$0") "connLink_"), diff --git a/bots/src/API/Docs/Generate.hs b/bots/src/API/Docs/Generate.hs index 99886bf222..8bc4cbe6ee 100644 --- a/bots/src/API/Docs/Generate.hs +++ b/bots/src/API/Docs/Generate.hs @@ -74,7 +74,7 @@ syntaxText r syntax = "\n**Syntax**:\n" <> "\n```\n" <> docSyntaxText r syntax <> "\n```\n" <> (if isConst syntax then "" else "\n```javascript\n" <> jsSyntaxText False "" r syntax <> " // JavaScript\n```\n") - <> (if isConst syntax then "" else "\n```python\n" <> pySyntaxText r syntax <> " # Python\n```\n") + <> (if isConst syntax then "" else "\n```python\n" <> pySyntaxText "" r syntax <> " # Python\n```\n") camelToSpace :: String -> String camelToSpace [] = [] diff --git a/bots/src/API/Docs/Generate/Python.hs b/bots/src/API/Docs/Generate/Python.hs new file mode 100644 index 0000000000..a144aa4376 --- /dev/null +++ b/bots/src/API/Docs/Generate/Python.hs @@ -0,0 +1,322 @@ +{-# LANGUAGE DuplicateRecordFields #-} +{-# LANGUAGE LambdaCase #-} +{-# LANGUAGE NamedFieldPuns #-} +{-# LANGUAGE OverloadedStrings #-} + +module API.Docs.Generate.Python where + +import API.Docs.Commands +import API.Docs.Events +import API.Docs.Generate +import API.Docs.Responses +import API.Docs.Syntax +import API.Docs.Syntax.Types +import API.Docs.Types +import API.TypeInfo +import Data.Char (isAlphaNum, toUpper) +import qualified Data.List.NonEmpty as L +import Data.Text (Text) +import qualified Data.Text as T + +commandsCodeFile :: FilePath +commandsCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_commands.py" + +responsesCodeFile :: FilePath +responsesCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_responses.py" + +eventsCodeFile :: FilePath +eventsCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_events.py" + +typesCodeFile :: FilePath +typesCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_types.py" + +-- | Replace dashes with underscores so Python identifiers stay valid. +pyIdent :: String -> Text +pyIdent = T.replace "-" "_" . T.pack + +-- | Python class name for a union member tag. +pyConstrName :: String -> Text +pyConstrName = pyIdent . fstToUpper + +commandsCodeText :: Text +commandsCodeText = + ("# API Commands\n# " <> autoGenerated <> "\n") + <> "from __future__ import annotations\n" + <> "import json\n" + <> "from typing import NotRequired, TypedDict\n" + <> "from . import _types as T\n" + <> "from . import _responses as CR\n" + <> foldMap commandCatCode chatCommandsDocs + where + commandCatCode CCCategory {categoryName, categoryDescr, commands} = + (T.pack $ "\n# " <> categoryName <> "\n# " <> categoryDescr <> "\n") + <> foldMap commandCode commands + where + commandCode CCDoc {commandType = ATUnionMember tag params, commandDescr, syntax, responses, network} = + ("\n# " <> commandDescr <> "\n") + <> ("# Network usage: " <> networkUsage network <> ".\n") + <> classDef + <> (if syntax == "" then "" else cmdStringFunc) + <> respAliasLine + where + constrName = T.pack $ fstToUpper tag + classDef = + ("class " <> constrName <> "(TypedDict):\n") + <> bodyOrPass (fieldsCodePy " " "T." params) + <> "\n" + cmdStringFunc = + ("\ndef " <> constrName <> "_cmd_string(self: " <> constrName <> ") -> str:\n") + <> " return " <> pySelfSyntaxText "T." (fstToUpper tag, params) syntax <> "\n" + respAliasLine = + "\n" <> constrName <> "_Response = " <> respUnion <> "\n" + respUnion = unionAliasRhs "" (responseRef . responseType) responses + responseRef (ATUnionMember rtag _) = "CR." <> pyConstrName rtag + +responsesCodeText :: Text +responsesCodeText = + ("# API Responses\n# " <> autoGenerated <> "\n") + <> pythonImports + <> unionTypeCodePy moduleMember "T." "ChatResponse" chatRespConstrs + where + chatRespConstrs = L.fromList $ map responseType chatResponsesDocs + +eventsCodeText :: Text +eventsCodeText = + ("# API Events\n# " <> autoGenerated <> "\n") + <> pythonImports + <> unionTypeCodePy moduleMember "T." "ChatEvent" chatEventConstrs + where + chatEventConstrs = L.fromList $ concatMap catEvents chatEventsDocs + catEvents CECategory {mainEvents, otherEvents} = map eventType $ mainEvents ++ otherEvents + +typesCodeText :: Text +typesCodeText = + ("# API Types\n# " <> autoGenerated <> "\n") + <> "from __future__ import annotations\n" + <> "from typing import Literal, NotRequired, TypedDict\n" + <> foldMap typeCode chatTypesDocs + where + typeCode ctd@CTDoc {typeDef = APITypeDef {typeName' = name, typeDef}, typeDescr} = + (if T.null typeDescr then "" else "\n# " <> typeDescr <> "\n") + <> typeDefCode + <> typeCmdStringCode ctd + where + name' = T.pack name + enumValue m = case name of + "ConnectionMode" -> map toUpper m + "FileProtocol" -> map toUpper m + _ -> m + typeDefCode = case typeDef of + ATDRecord fields -> + ("\nclass " <> name' <> "(TypedDict):\n") + <> bodyOrPass (fieldsCodePy " " "" fields) + ATDEnum cs -> + "\n" <> name' <> " = Literal[" + <> T.intercalate ", " (map (\m -> "\"" <> T.pack (enumValue m) <> "\"") $ L.toList cs) + <> "]\n" + ATDUnion cs -> unionTypeCodePy typeMember "" name cs + +-- | For types with non-empty `typeSyntax`, emit a top-level +-- `_cmd_string(self: ) -> str` helper that mirrors the +-- Choice/Param expression. Records access fields via `self['']`; +-- enums and unions dispatch on `self` (a literal string) or `self['type']` +-- respectively. Required so generated `_commands.py` produces valid CLI +-- syntax for ChatRef/ChatType/ChatDeleteMode/GroupChatScope/PaginationByTime +-- params instead of stringifying the wire dict. +typeCmdStringCode :: CTDoc -> Text +typeCmdStringCode CTDoc {typeDef = td@APITypeDef {typeName' = name, typeDef}, typeSyntax} + | typeSyntax == "" = "" + | otherwise = + "\n\ndef " <> T.pack name <> "_cmd_string(self: " <> T.pack name <> ") -> str:\n" + <> " return " <> body <> ignore <> "\n" + where + body = pyTypeSyntaxText "" (name, fields) typeSyntax + -- Unions and enums use self/self['type'] to dispatch. Pyright cannot + -- narrow TypedDict access by string-literal key, so suppress per-branch + -- complaints with one ignore on the return. + ignore = case typeDef of + ATDUnion _ -> " # type: ignore[typeddict-item]" + _ -> "" + -- typeFields mirrors TS funcCode: include `self` so Choice "self" + -- resolves; for unions add `type` and flatten member fields. + self = APIRecordField "self" (ATDef td) + fields = case typeDef of + ATDRecord fs -> fs + ATDUnion ms -> + self : APIRecordField "type" tagType : concatMap (\(ATUnionMember _ fs) -> fs) (L.toList ms) + where + tagType = ATDef $ APITypeDef (name <> ".type") $ ATDEnum tags + tags = L.map (\(ATUnionMember tag _) -> tag) ms + ATDEnum _ -> [self] + +-- | Like `pySelfSyntaxText` but excludes `self` from the param-rewrite list +-- so `self == 'tag'` (enum dispatch) and `self['type']` (union dispatch) +-- survive verbatim. Used only for type-level cmd_string functions inside +-- @_types.py@, where peer type cmd_string calls don't need a namespace. +pyTypeSyntaxText :: String -> TypeAndFields -> Expr -> Text +pyTypeSyntaxText typeNamespace r expr = + rewriteParams accessors (pySyntaxText typeNamespace r expr) + where + accessors = filter ((/= "self") . fst) (paramAccessors r) + +-- | Member class name within the multi-type @_types.py@ module: prefix the +-- tag with the union type name so members from different unions don't +-- collide. +typeMember :: String -> String -> Text +typeMember typeName tag = T.pack typeName <> "_" <> pyIdent tag + +-- | Member class name within a single-union module (responses/events): just +-- the PascalCase tag, so commands can reference them as @CR.@. +moduleMember :: String -> String -> Text +moduleMember _ tag = pyConstrName tag + +-- | Common imports for the responses/events modules. +pythonImports :: Text +pythonImports = + "from __future__ import annotations\n" + <> "from typing import Literal, NotRequired, TypedDict\n" + <> "from . import _types as T\n" + +-- | Render a tagged-union type: one TypedDict per member, plus union alias +-- and `_Tag` Literal alias. The member class names are produced by +-- @memberName@ given the union type name and the member tag. +unionTypeCodePy :: + (String -> String -> Text) -> + Text -> + String -> + L.NonEmpty ATUnionMember -> + Text +unionTypeCodePy memberName typesNamespace name cs = + foldMap memberClass (L.toList cs) + <> "\n" <> name' <> " = " <> unionAliasRhs name' constrTypeRef (L.toList cs) + <> "\n" <> name' <> "_Tag = Literal[" <> tagLiterals <> "]\n" + where + name' = T.pack name + constrTypeRef (ATUnionMember tag _) = memberName name tag + tagLiterals = T.intercalate ", " $ map (\(ATUnionMember tag _) -> "\"" <> T.pack tag <> "\"") $ L.toList cs + memberClass (ATUnionMember tag fields) = + ("\nclass " <> memberName name tag <> "(TypedDict):\n") + <> (" type: Literal[\"" <> T.pack tag <> "\"]\n") + <> fieldsCodePy " " typesNamespace fields + +-- | Render the right-hand side of a union alias: either inline (one line) or +-- multi-line wrapped in parentheses with `|` separators between alternatives. +unionAliasRhs :: Text -> (a -> Text) -> [a] -> Text +unionAliasRhs lhs constr cs + | T.length (lhs <> " = " <> oneLine) <= 100 = oneLine <> "\n" + | otherwise = "(\n" <> T.intercalate "\n" (map (" " <>) lines') <> "\n)\n" + where + oneLine = T.intercalate " | " cs' + lines' = case cs' of + [] -> [] + (h : t) -> h : map ("| " <>) t + cs' = map constr cs + +-- | Emit a body of `pass` if there are no fields, otherwise the rendered +-- fields as-is. +bodyOrPass :: Text -> Text +bodyOrPass body + | T.null body = " pass\n" + | otherwise = body + +-- | Render record fields for a TypedDict body. Each field becomes +-- `: [ # ]`. Optional fields wrap the type in +-- `NotRequired[...]`. +fieldsCodePy :: Text -> Text -> [APIRecordField] -> Text +fieldsCodePy indent namespace = foldMap render + where + render (APIRecordField name t) = + indent <> T.pack name <> ": " <> wrapOptional t (typeText t) <> typeComment t <> "\n" + wrapOptional t inner = case t of + ATOptional _ -> "NotRequired[" <> inner <> "]" + _ -> inner + typeText = \case + ATPrim (PT t) -> primName t + ATDef (APITypeDef t _) -> quoted (namespace <> T.pack t) + ATRef t -> quoted (namespace <> T.pack t) + ATOptional t -> typeText t + ATArray {elemType} -> "list[" <> typeText elemType <> "]" + ATMap (PT k) v -> "dict[" <> primName k <> ", " <> typeText v <> "]" + primName = \case + TBool -> "bool" + TString -> "str" + TInt -> "int" + TInt64 -> "int" + TWord32 -> "int" + TDouble -> "float" + TJSONObject -> "dict[str, object]" + TUTCTime -> "str" + t -> T.pack t + quoted s = "\"" <> s <> "\"" + typeComment t = let c = typeComment' t in if T.null c then "" else " # " <> c + typeComment' = \case + ATPrim (PT t) -> typeComment_ t + ATOptional inner -> typeComment' inner + ATArray {elemType, nonEmpty} + | nonEmpty -> if T.null c then "non-empty" else c <> ", non-empty" + | otherwise -> c + where + c = typeComment' elemType + ATMap (PT k) v -> + let kc = typeComment_ k + vc = typeComment' v + tc t c = if T.null c then t else c + in if T.null kc && T.null vc then "" else tc (primName k) kc <> " : " <> tc (typeText v) vc + _ -> "" + typeComment_ = \case + TInt -> "int" + TInt64 -> "int64" + TWord32 -> "word32" + TDouble -> "double" + TUTCTime -> "ISO-8601 timestamp" + _ -> "" + +-- | Wrap `pySyntaxText` so each parameter access uses `self['']`. The +-- output of `pySyntaxText` references params as bare Python identifiers +-- (e.g. `str(userId)`); we rewrite those identifiers — but only outside +-- string literals — into TypedDict subscript accesses. The +-- @typeNamespace@ is prepended to any `_cmd_string(...)` calls +-- emitted for params whose type has its own syntax (e.g. @"T."@ from +-- @_commands.py@, or @""@ from within @_types.py@). +-- +-- Unlike the JS variant, we do NOT collapse adjacent string literals via +-- `T.replace "' + '" ""`: that pattern incorrectly matches `' ' + ','` +-- (the space-then-comma sequence between a literal and `','.join(...)`), +-- producing `' ,'.join(...)` which uses ` ,` as the join separator and +-- swallows the leading space. The `intercalate " + "` output is correct +-- without further string fixups. +pySelfSyntaxText :: String -> TypeAndFields -> Expr -> Text +pySelfSyntaxText typeNamespace r expr = + rewriteParams (paramAccessors r) (pySyntaxText typeNamespace r expr) + +-- | Map field name to the Python access expression: `self['']` for +-- required fields, `self.get('')` for optional ones (since +-- TypedDict's `NotRequired` allows the key to be absent and `[...]` would +-- raise `KeyError`). Used by the rewriter so the same name is substituted +-- consistently in Optional `is not None` checks and in the value position. +paramAccessors :: TypeAndFields -> [(String, String)] +paramAccessors (_, fields) = map mk fields + where + mk (APIRecordField n t) = (n, accessor n t) + accessor n = \case + ATOptional _ -> "self.get('" ++ n ++ "')" + _ -> "self['" ++ n ++ "']" + +-- | Replace bare identifiers (matching a key in @accessors@) with the +-- corresponding accessor expression, skipping characters inside +-- single-quoted string literals and respecting identifier word boundaries. +rewriteParams :: [(String, String)] -> Text -> Text +rewriteParams accessors = T.pack . go False . T.unpack + where + go _ [] = [] + -- Toggle in/out of single-quoted string on every unescaped quote. + go inStr ('\'' : rest) = '\'' : go (not inStr) rest + go True (c : rest) = c : go True rest + go False s@(c : rest) + | isIdentStart c = case takeIdent s of + (ident, after) -> case lookup ident accessors of + Just expr -> expr ++ go False after + Nothing -> ident ++ go False after + | otherwise = c : go False rest + isIdentStart c = isAlphaNum c || c == '_' + takeIdent = span (\c -> isAlphaNum c || c == '_') diff --git a/bots/src/API/Docs/Syntax.hs b/bots/src/API/Docs/Syntax.hs index f96ec03b02..64c848f310 100644 --- a/bots/src/API/Docs/Syntax.hs +++ b/bots/src/API/Docs/Syntax.hs @@ -157,8 +157,8 @@ escapeChar c s | c `elem` s = concatMap (\c' -> if c' == c then ['\\', c] else [c]) s | otherwise = s -pySyntaxText :: TypeAndFields -> Expr -> Text -pySyntaxText r = T.pack . go Nothing True +pySyntaxText :: String -> TypeAndFields -> Expr -> Text +pySyntaxText typeNamespace r = T.pack . go Nothing True where go param top = \case Concat exs -> intercalate " + " $ map (go param False) $ L.toList exs @@ -167,7 +167,13 @@ pySyntaxText r = T.pack . go Nothing True withParamType r param p $ \case ATPrim (PT TString) -> paramName param p ATOptional (ATPrim (PT TString)) -> paramName param p + ATDef td -> toStringSyntax td + ATOptional (ATDef td) -> toStringSyntax td _ -> "str(" <> paramName param p <> ")" + where + toStringSyntax (APITypeDef typeName _) + | typeHasSyntax typeName = typeNamespace <> typeName <> "_cmd_string(" <> paramName param p <> ")" + | otherwise = "str(" <> paramName param p <> ")" Optional exN exJ p -> open <> "(" <> go (Just p) False exJ <> ") if " <> n <> " is not None else " <> nothing <> close where n = paramName param p diff --git a/packages/simplex-chat-python/.gitignore b/packages/simplex-chat-python/.gitignore new file mode 100644 index 0000000000..5d5acffbb9 --- /dev/null +++ b/packages/simplex-chat-python/.gitignore @@ -0,0 +1,22 @@ +# Python build / cache artifacts — never commit these +__pycache__/ +*.py[cod] +*$py.class +*.egg-info/ +build/ +dist/ +.pytest_cache/ +.ruff_cache/ +.mypy_cache/ +.pyright_cache/ + +# Virtual environments +.venv/ +.venv-*/ +venv/ + +# Lazy-downloaded native libs (handled at runtime by _native._resolve_libs_dir) +libs/ + +# Local override for SIMPLEX_LIBS_DIR work, etc. +.env diff --git a/packages/simplex-chat-python/LICENSE b/packages/simplex-chat-python/LICENSE new file mode 100644 index 0000000000..0ad25db4bd --- /dev/null +++ b/packages/simplex-chat-python/LICENSE @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU Affero General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero General Public License from time to time. Such new versions +will be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU Affero General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU Affero General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU AGPL, see +. diff --git a/packages/simplex-chat-python/README.md b/packages/simplex-chat-python/README.md new file mode 100644 index 0000000000..b86886d591 --- /dev/null +++ b/packages/simplex-chat-python/README.md @@ -0,0 +1,70 @@ +# SimpleX Chat Python library + +Python 3.11+ client for [SimpleX Chat](https://simplex.chat) bots. Equivalent to the [Node.js library](https://www.npmjs.com/package/simplex-chat). + +## Install + +```bash +pip install simplex-chat +``` + +The native `libsimplex` is downloaded lazily on first use. To pre-fetch: + +```bash +python -m simplex_chat install # sqlite (default) +python -m simplex_chat install --backend postgres # linux-x86_64 only +``` + +## Quick start + +```python +import re +from simplex_chat import Bot, BotProfile, Message, SqliteDb, TextMessage + +bot = Bot( + profile=BotProfile(display_name="Squaring bot"), + db=SqliteDb(file_prefix="./squaring_bot"), + welcome="Send me a number, I'll square it.", +) + +@bot.on_message(content_type="text", text=re.compile(r"^-?\d+(\.\d+)?$")) +async def square(msg: TextMessage) -> None: + n = float(msg.text or "0") + await msg.reply(f"{n} * {n} = {n * n}") + +@bot.on_message(content_type="text") +async def fallback(msg: Message) -> None: + await msg.reply("Send me a number, like 7 or 3.14.") + +if __name__ == "__main__": + bot.run() +``` + +`bot.run()` blocks. The connection address is logged on startup — paste it into a SimpleX client to talk to the bot. `Ctrl+C` to stop. + +Three decorators: `@bot.on_message(...)`, `@bot.on_command(name)`, `@bot.on_event(tag)`. Message handlers are first-match-wins in registration order, so register specific filters first and catch-alls last. + +See [`examples/squaring_bot.py`](./examples/squaring_bot.py) for the full example. + +## Development + +```bash +uv venv && source .venv/bin/activate +uv pip install -e '.[dev]' +ruff check && pyright && pytest tests/ +``` + +Wire types under `src/simplex_chat/types/_*.py` are generated. Regenerate with `cabal test simplex-chat-test --test-options='--match Python'`. + +## Release + +Manual for now. Bump `_version.py:__version__`, build a wheel, upload to PyPI: + +```bash +uv build --wheel +uv publish --token "$PYPI_TOKEN" +``` + +## License + +[AGPL-3.0](./LICENSE) diff --git a/packages/simplex-chat-python/examples/squaring_bot.py b/packages/simplex-chat-python/examples/squaring_bot.py new file mode 100644 index 0000000000..296b51347e --- /dev/null +++ b/packages/simplex-chat-python/examples/squaring_bot.py @@ -0,0 +1,52 @@ +"""Squaring bot — replies to every number with its square. + +Run with the simplex-chat package installed: + + python examples/squaring_bot.py + +Sends `n * n = ...` for any text message that parses as a number; falls +back to a hint for non-number messages; responds to `/help` with usage. +""" + +from __future__ import annotations + +import re + +from simplex_chat import ( + Bot, + BotCommand, + BotProfile, + Message, + ParsedCommand, + SqliteDb, + TextMessage, +) + +bot = Bot( + profile=BotProfile(display_name="Squaring bot"), + db=SqliteDb(file_prefix="./squaring_bot"), + welcome="Send me a number, I'll square it.", + commands=[BotCommand(keyword="help", label="Show help")], +) + +NUMBER_RE = re.compile(r"^-?\d+(\.\d+)?$") + + +@bot.on_message(content_type="text", text=NUMBER_RE) +async def square(msg: TextMessage) -> None: + n = float(msg.text or "0") + await msg.reply(f"{n} * {n} = {n * n}") + + +@bot.on_message(content_type="text") +async def fallback(msg: Message) -> None: + await msg.reply("Send me a number, like 7 or 3.14.") + + +@bot.on_command("help") +async def help_cmd(msg: Message, _cmd: ParsedCommand) -> None: + await msg.reply("Send a number, I'll square it.") + + +if __name__ == "__main__": + bot.run() diff --git a/packages/simplex-chat-python/pyproject.toml b/packages/simplex-chat-python/pyproject.toml new file mode 100644 index 0000000000..76f22cdabe --- /dev/null +++ b/packages/simplex-chat-python/pyproject.toml @@ -0,0 +1,58 @@ +[build-system] +requires = ["hatchling>=1.24"] +build-backend = "hatchling.build" + +[project] +name = "simplex-chat" +description = "SimpleX Chat Python library for chat bots" +readme = "README.md" +license = "AGPL-3.0-only" +authors = [{name = "SimpleX Chat"}] +requires-python = ">=3.11" +keywords = ["simplex", "messenger", "chat", "privacy", "security", "bots"] +classifiers = [ + "Development Status :: 4 - Beta", + "License :: OSI Approved :: GNU Affero General Public License v3", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Topic :: Communications :: Chat", +] +dynamic = ["version"] + +[project.urls] +Homepage = "https://github.com/simplex-chat/simplex-chat/tree/stable/packages/simplex-chat-python" +Issues = "https://github.com/simplex-chat/simplex-chat/issues" + +[project.optional-dependencies] +test = ["pytest>=8", "pytest-asyncio>=0.23"] +dev = ["pytest>=8", "pytest-asyncio>=0.23", "pyright>=1.1.380", "ruff>=0.6"] + +[tool.hatch.version] +path = "src/simplex_chat/_version.py" + +[tool.hatch.build.targets.wheel] +packages = ["src/simplex_chat"] + +[tool.pytest.ini_options] +asyncio_mode = "auto" + +[tool.ruff] +line-length = 100 +target-version = "py311" + +[tool.ruff.format] +# `src/simplex_chat/types/*.py` are generated by the Haskell codegen +# (bots/src/API/Docs/Generate/Python.hs). Re-formatting them locally +# would diverge from the generator's output and break `cabal test +# simplex-chat-test --match Python`. Lint still applies — only format +# is suppressed. +exclude = ["src/simplex_chat/types/_*.py"] + +[tool.pyright] +# Same rationale: the generated cmd_string helpers use `self.get('x')` +# call pairs that pyright cannot narrow across (`is not None` followed +# by re-access). Hand-written code is still strictly checked. +include = ["src/simplex_chat"] +exclude = ["src/simplex_chat/types/_*.py", "**/__pycache__", "**/.venv*"] diff --git a/packages/simplex-chat-python/src/simplex_chat/__init__.py b/packages/simplex-chat-python/src/simplex_chat/__init__.py new file mode 100644 index 0000000000..dfafef123a --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/__init__.py @@ -0,0 +1,59 @@ +"""SimpleX Chat — Python client library for chat bots.""" + +from ._version import __version__ +from .api import ChatApi, ChatCommandError, ConnReqType, Db, PostgresDb, SqliteDb +from .bot import ( + Bot, + BotCommand, + BotProfile, + ChatMessage, + CommandHandler, + EventHandler, + FileMessage, + ImageMessage, + LinkMessage, + Message, + MessageHandler, + Middleware, + ParsedCommand, + ReportMessage, + TextMessage, + UnknownMessage, + VideoMessage, + VoiceMessage, +) +from .core import ChatAPIError, ChatInitError, CryptoArgs, MigrationConfirmation +from . import util as util # re-export the util namespace + +__all__ = [ + "__version__", + "Bot", + "BotCommand", + "BotProfile", + "ChatAPIError", + "ChatApi", + "ChatCommandError", + "ChatInitError", + "ChatMessage", + "CommandHandler", + "ConnReqType", + "CryptoArgs", + "Db", + "EventHandler", + "FileMessage", + "ImageMessage", + "LinkMessage", + "Message", + "MessageHandler", + "Middleware", + "MigrationConfirmation", + "ParsedCommand", + "PostgresDb", + "ReportMessage", + "SqliteDb", + "TextMessage", + "UnknownMessage", + "VideoMessage", + "VoiceMessage", + "util", +] diff --git a/packages/simplex-chat-python/src/simplex_chat/__main__.py b/packages/simplex-chat-python/src/simplex_chat/__main__.py new file mode 100644 index 0000000000..2fa4f3cd37 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/__main__.py @@ -0,0 +1,35 @@ +"""CLI: ``python -m simplex_chat install [--backend=sqlite|postgres]``.""" + +from __future__ import annotations + +import argparse +import sys + +from . import _native + + +def main(argv: list[str] | None = None) -> int: + p = argparse.ArgumentParser(prog="simplex_chat") + sub = p.add_subparsers(dest="command", required=True) + install = sub.add_parser("install", help="Pre-fetch libsimplex into the user cache") + install.add_argument( + "--backend", + choices=["sqlite", "postgres"], + default="sqlite", + help="which libsimplex variant to download (default: sqlite)", + ) + args = p.parse_args(argv) + # `args.command` is always set: `add_subparsers(required=True)` makes + # argparse exit before reaching this point if no subcommand is given. + assert args.command == "install" + try: + path = _native._resolve_libs_dir(args.backend) + print(f"libsimplex installed at: {path}") + return 0 + except Exception as e: + print(f"install failed: {e}", file=sys.stderr) + return 1 + + +if __name__ == "__main__": + sys.exit(main()) diff --git a/packages/simplex-chat-python/src/simplex_chat/_native.py b/packages/simplex-chat-python/src/simplex_chat/_native.py new file mode 100644 index 0000000000..313c606883 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/_native.py @@ -0,0 +1,257 @@ +"""Native libsimplex loader: platform detection, lazy download, ctypes setup. + +Internal — users interact with `Bot` / `ChatApi`, never with this module. +""" + +from __future__ import annotations + +import ctypes +import errno +import os +import platform +import sys +import tempfile +import threading +import urllib.request +import zipfile +from ctypes import POINTER, c_char_p, c_int, c_uint8, c_void_p +from pathlib import Path +from typing import Literal + +from ._version import LIBS_VERSION + +Backend = Literal["sqlite", "postgres"] + +_GITHUB_REPO = "simplex-chat/simplex-chat-libs" + +_PLATFORM_MAP = { + "linux": ("linux", {"x86_64": "x86_64", "aarch64": "aarch64"}), + "darwin": ("macos", {"x86_64": "x86_64", "arm64": "aarch64"}), + "win32": ("windows", {"AMD64": "x86_64", "x86_64": "x86_64"}), +} + +_LIBNAME = {"linux": "libsimplex.so", "darwin": "libsimplex.dylib", "win32": "libsimplex.dll"} + +SUPPORTED = ( + "linux-x86_64", + "linux-aarch64", + "macos-x86_64", + "macos-aarch64", + "windows-x86_64", +) + + +def _platform_tag() -> str: + info = _PLATFORM_MAP.get(sys.platform) + if not info: + raise RuntimeError(f"Unsupported platform: {sys.platform}") + sysname, archs = info + arch = archs.get(platform.machine()) + if not arch: + raise RuntimeError(f"Unsupported architecture: {sys.platform}/{platform.machine()}") + tag = f"{sysname}-{arch}" + if tag not in SUPPORTED: + raise RuntimeError(f"Unsupported combination: {tag}; supported: {SUPPORTED}") + return tag + + +def _libname() -> str: + return _LIBNAME[sys.platform] + + +def _libs_url(backend: Backend) -> str: + suffix = "-postgres" if backend == "postgres" else "" + return ( + f"https://github.com/{_GITHUB_REPO}/releases/download/" + f"v{LIBS_VERSION}/simplex-chat-libs-{_platform_tag()}{suffix}.zip" + ) + + +def _cache_root() -> Path: + if sys.platform == "darwin": + return Path.home() / "Library" / "Caches" / "simplex-chat" + if sys.platform == "win32": + return Path(os.environ["LOCALAPPDATA"]) / "simplex-chat" + base = os.environ.get("XDG_CACHE_HOME") or str(Path.home() / ".cache") + return Path(base) / "simplex-chat" + + +def _resolve_libs_dir(backend: Backend) -> Path: + if override := os.environ.get("SIMPLEX_LIBS_DIR"): + return Path(override) + if backend == "postgres" and _platform_tag() != "linux-x86_64": + raise RuntimeError( + "postgres backend is only supported on linux-x86_64; " + f"current platform is {_platform_tag()}" + ) + target = _cache_root() / f"v{LIBS_VERSION}" / backend + if not (target / _libname()).exists(): + _download(target, backend) + return target + + +_DOWNLOAD_CHUNK = 1 << 16 # 64 KiB + + +def _stream_to_file(url: str, dest: Path, *, timeout: float = 60.0) -> None: + """Stream `url` → `dest`, printing a carriage-return progress bar. + + `timeout` is per-request; we don't touch `socket.setdefaulttimeout` + so other socket users in the same process aren't affected. + """ + with urllib.request.urlopen(url, timeout=timeout) as resp: # noqa: S310 - https://github.com/... + total = int(resp.headers.get("Content-Length") or 0) + received = 0 + with dest.open("wb") as out: + while chunk := resp.read(_DOWNLOAD_CHUNK): + out.write(chunk) + received += len(chunk) + if total > 0: + pct = min(100, received * 100 // total) + msg = f"\r download: {received >> 20} / {total >> 20} MiB ({pct}%)" + else: + msg = f"\r download: {received >> 20} MiB" + print(msg, end="", file=sys.stderr, flush=True) + print("", file=sys.stderr, flush=True) # newline after final progress line + + +def _download(target: Path, backend: Backend) -> None: + """Download libs zip → atomic rename into `target`. Concurrent processes safe. + + Atomicity strategy: each process extracts to its own sibling tempdir on the same + filesystem, then `os.rename` the `libs/` subdir to `target`. POSIX `os.rename` + onto a NON-EXISTENT path is atomic; if the target exists (another process won + the race), `os.rename` fails on most platforms — we then verify the winner has + what we need and proceed. NEVER rmtree the target: that creates a TOCTOU + window where another process is reading/loading the file we're deleting. + """ + target.parent.mkdir(parents=True, exist_ok=True) + url = _libs_url(backend) + print( + f"Downloading libsimplex ({_platform_tag()}, {backend}) v{LIBS_VERSION} from {url} ...", + file=sys.stderr, + flush=True, + ) + with tempfile.TemporaryDirectory(dir=target.parent) as tmp: + zip_path = Path(tmp) / "libs.zip" + _stream_to_file(url, zip_path, timeout=60.0) + with zipfile.ZipFile(zip_path) as zf: + zf.extractall(tmp) + # zip layout: /libs/libsimplex.* + libHS*.* + extracted_libs = Path(tmp) / "libs" + if not extracted_libs.is_dir(): + raise RuntimeError(f"libs/ missing from {_libs_url(backend)}") + try: + os.rename(extracted_libs, target) + except OSError as e: + # EEXIST / ENOTEMPTY mean another process won the race — fall through + # and check that the winner left a usable libsimplex behind. Anything + # else (ENOSPC, EACCES, EROFS, Windows codes mapped to None) is a real + # failure and must propagate. Same VERSION cached → same content → + # safe to proceed once we've confirmed the file is there. + if e.errno not in (errno.EEXIST, errno.ENOTEMPTY): + raise + if not (target / _libname()).exists(): + raise RuntimeError( + f"another process partially populated {target} but libsimplex " + f"is missing; remove the directory manually and retry" + ) from e + + +_lock = threading.Lock() +_lib: ctypes.CDLL | None = None +_libc: ctypes.CDLL | None = None +_backend: Backend | None = None + + +def _load_libc() -> ctypes.CDLL: + if sys.platform == "win32": + return ctypes.CDLL("msvcrt") + return ctypes.CDLL(None) # libc on POSIX is the process's own symbol table + + +def _setup_signatures(lib: ctypes.CDLL) -> None: + """Declare argtypes/restype for the 8 chat_* functions exported by libsimplex. + + All result strings come back as raw c_void_p so the caller can free them + after copying — matches HandleCResult in cpp/simplex.cc:157-165. + """ + lib.chat_migrate_init.argtypes = [c_char_p, c_char_p, c_char_p, POINTER(c_void_p)] + lib.chat_migrate_init.restype = c_void_p + lib.chat_close_store.argtypes = [c_void_p] + lib.chat_close_store.restype = c_void_p + lib.chat_send_cmd.argtypes = [c_void_p, c_char_p] + lib.chat_send_cmd.restype = c_void_p + lib.chat_recv_msg_wait.argtypes = [c_void_p, c_int] + lib.chat_recv_msg_wait.restype = c_void_p + # chat_write_file's payload is treated read-only by libsimplex; passing + # `bytes` via c_char_p avoids the from_buffer_copy doubling. ctypes pins + # the bytes buffer for the duration of the call. + lib.chat_write_file.argtypes = [c_void_p, c_char_p, c_char_p, c_int] + lib.chat_write_file.restype = c_void_p + lib.chat_read_file.argtypes = [c_char_p, c_char_p, c_char_p] + lib.chat_read_file.restype = POINTER(c_uint8) + lib.chat_encrypt_file.argtypes = [c_void_p, c_char_p, c_char_p] + lib.chat_encrypt_file.restype = c_void_p + lib.chat_decrypt_file.argtypes = [c_char_p, c_char_p, c_char_p, c_char_p] + lib.chat_decrypt_file.restype = c_void_p + + +def _hs_init(lib: ctypes.CDLL) -> None: + """Initialize the Haskell runtime exactly once. Mirrors cpp/simplex.cc:13-32.""" + if sys.platform == "win32": + argv_strs = [b"simplex", b"+RTS", b"-A64m", b"-H64m", b"--install-signal-handlers=no"] + else: + argv_strs = [ + b"simplex", + b"+RTS", + b"-A64m", + b"-H64m", + b"-xn", + b"--install-signal-handlers=no", + ] + argc = c_int(len(argv_strs)) + arr = (c_char_p * (len(argv_strs) + 1))(*argv_strs, None) + arr_ptr = ctypes.byref(ctypes.cast(arr, POINTER(c_char_p))) + lib.hs_init_with_rtsopts.argtypes = [POINTER(c_int), POINTER(POINTER(c_char_p))] + lib.hs_init_with_rtsopts.restype = None + lib.hs_init_with_rtsopts(ctypes.byref(argc), arr_ptr) + + +def lib_for(backend: Backend) -> ctypes.CDLL: + """Resolve, load, and initialize libsimplex for the given backend. + + Idempotent for the same backend; raises if called with a different backend. + Concurrent calls serialize on the module-level lock. + """ + global _lib, _libc, _backend + with _lock: + if _lib is not None: + if _backend != backend: + raise RuntimeError( + f"libsimplex already loaded with backend={_backend!r}; " + f"cannot switch to {backend!r} in the same process" + ) + return _lib + libs_dir = _resolve_libs_dir(backend) + lib = ctypes.CDLL(str(libs_dir / _libname())) + _setup_signatures(lib) + _hs_init(lib) + _libc = _load_libc() + _lib = lib + _backend = backend + return lib + + +def libc() -> ctypes.CDLL: + """libc — needed by `core` to free Haskell-allocated result strings.""" + if _libc is None: + raise RuntimeError("lib_for() must be called before libc()") + return _libc + + +def lib() -> ctypes.CDLL: + """Loaded libsimplex handle. Raises if `lib_for()` has not been called.""" + if _lib is None: + raise RuntimeError("lib_for() must be called before lib()") + return _lib diff --git a/packages/simplex-chat-python/src/simplex_chat/_version.py b/packages/simplex-chat-python/src/simplex_chat/_version.py new file mode 100644 index 0000000000..0468b65dd9 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/_version.py @@ -0,0 +1,9 @@ +"""Single source of truth for both the Python package version and the +simplex-chat-libs release tag we depend on. + +Bump both together for normal releases. For wrapper-only fixes use a PEP 440 +post-release: __version__ = "6.5.1.post1", LIBS_VERSION unchanged. +""" + +__version__ = "6.5.1" # PEP 440 — read by hatchling for wheel metadata +LIBS_VERSION = "6.5.1" # simplex-chat-libs release tag (no 'v' prefix) diff --git a/packages/simplex-chat-python/src/simplex_chat/api.py b/packages/simplex-chat-python/src/simplex_chat/api.py new file mode 100644 index 0000000000..8f116c903f --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/api.py @@ -0,0 +1,704 @@ +"""Low-level escape-hatch API. Most users go through `Bot` instead.""" + +from __future__ import annotations + +import json +from dataclasses import dataclass +from typing import Any, Literal + +from . import _native, core, util +from .core import MigrationConfirmation +from .types import CC, CEvt, CR, T + +# Mirrors Node `ConnReqType` enum (api.ts:15-18) — the two possible outcomes +# of `api_connect` / `api_connect_active_user` depending on the link kind. +ConnReqType = Literal["invitation", "contact"] + + +@dataclass(slots=True) +class SqliteDb: + file_prefix: str + encryption_key: str | None = None + + +@dataclass(slots=True) +class PostgresDb: + connection_string: str + schema_prefix: str | None = None + + +Db = SqliteDb | PostgresDb + + +def _db_to_migrate_args(db: Db) -> tuple[str, str, _native.Backend]: + """Returns (path-or-prefix, key-or-conn, backend).""" + if isinstance(db, SqliteDb): + return (db.file_prefix, db.encryption_key or "", "sqlite") + if isinstance(db, PostgresDb): + return (db.schema_prefix or "", db.connection_string, "postgres") + raise TypeError(f"Unknown db: {db!r}") + + +class ChatCommandError(Exception): + def __init__(self, message: str, response: CR.ChatResponse): + super().__init__(message) + self.response = response + + +class ChatApi: + def __init__(self, ctrl: int): + self._ctrl: int | None = ctrl + self._started = False + + @classmethod + async def init( + cls, + db: Db, + confirm: MigrationConfirmation = MigrationConfirmation.YES_UP, + ) -> "ChatApi": + path_or_prefix, key_or_conn, backend = _db_to_migrate_args(db) + # Trigger lazy lib load with the right backend BEFORE chat_migrate_init. + _native.lib_for(backend) + ctrl = await core.chat_migrate_init(path_or_prefix, key_or_conn, confirm) + return cls(ctrl) + + @property + def ctrl(self) -> int: + """Opaque controller pointer. Raises if `close()` has been called.""" + if self._ctrl is None: + raise RuntimeError("ChatApi controller not initialized (close() called?)") + return self._ctrl + + @property + def initialized(self) -> bool: + """True until `close()` is called. Mirrors Node `ChatApi.initialized`.""" + return self._ctrl is not None + + @property + def started(self) -> bool: + """True between `start_chat()` and the next `stop_chat()` / `close()`.""" + return self._started + + async def start_chat(self) -> None: + r = await self.send_chat_cmd( + CC.StartChat_cmd_string({"mainApp": True, "enableSndFiles": True}) + ) + if r.get("type") not in ("chatStarted", "chatRunning"): + raise ChatCommandError("error starting chat", r) + self._started = True + + async def stop_chat(self) -> None: + r = await self.send_chat_cmd("/_stop") + if r.get("type") != "chatStopped": + raise ChatCommandError("error stopping chat", r) + self._started = False + + async def close(self) -> None: + await core.chat_close_store(self.ctrl) + self._ctrl = None + self._started = False + + async def send_chat_cmd(self, cmd: str) -> CR.ChatResponse: + return await core.chat_send_cmd(self.ctrl, cmd) + + async def recv_chat_event(self, wait_us: int = 500_000) -> CEvt.ChatEvent | None: + return await core.chat_recv_msg_wait(self.ctrl, wait_us) + + # ------------------------------------------------------------------ # + # Address commands + # ------------------------------------------------------------------ # + + async def api_create_user_address(self, user_id: int) -> T.CreatedConnLink: + r = await self.send_chat_cmd(CC.APICreateMyAddress_cmd_string({"userId": user_id})) + if r["type"] == "userContactLinkCreated": + return r["connLinkContact"] + raise ChatCommandError("error creating user address", r) + + async def api_delete_user_address(self, user_id: int) -> None: + r = await self.send_chat_cmd(CC.APIDeleteMyAddress_cmd_string({"userId": user_id})) + if r["type"] != "userContactLinkDeleted": + raise ChatCommandError("error deleting user address", r) + + async def api_get_user_address(self, user_id: int) -> T.UserContactLink | None: + try: + r = await self.send_chat_cmd(CC.APIShowMyAddress_cmd_string({"userId": user_id})) + if r["type"] == "userContactLink": + return r["contactLink"] + raise ChatCommandError("error loading user address", r) + except core.ChatAPIError as e: + ce = e.chat_error + if ( + ce is not None + and ce.get("type") == "errorStore" + and ce.get("storeError", {}).get("type") == "userContactLinkNotFound" + ): + return None + raise + + async def api_set_profile_address( + self, user_id: int, enable: bool + ) -> T.UserProfileUpdateSummary: + r = await self.send_chat_cmd( + CC.APISetProfileAddress_cmd_string({"userId": user_id, "enable": enable}) + ) + if r["type"] == "userProfileUpdated": + return r["updateSummary"] + raise ChatCommandError("error setting profile address", r) + + async def api_set_address_settings(self, user_id: int, settings: T.AddressSettings) -> None: + r = await self.send_chat_cmd( + CC.APISetAddressSettings_cmd_string({"userId": user_id, "settings": settings}) + ) + if r["type"] != "userContactLinkUpdated": + raise ChatCommandError("error changing user contact address settings", r) + + # ------------------------------------------------------------------ # + # Message commands + # ------------------------------------------------------------------ # + + async def api_send_messages( + self, + chat: list | T.ChatRef | T.ChatInfo, + messages: list[T.ComposedMessage], + live_message: bool = False, + ) -> list[T.AChatItem]: + if isinstance(chat, list): + send_ref: T.ChatRef = {"chatType": chat[0], "chatId": chat[1]} + elif "chatType" in chat and "chatId" in chat: + send_ref = chat + else: + ref = util.chat_info_ref(chat) + if ref is None: + raise ValueError("api_send_messages: can't send messages to this chat") + send_ref = ref + r = await self.send_chat_cmd( + CC.APISendMessages_cmd_string( + { + "sendRef": send_ref, + "composedMessages": messages, + "liveMessage": live_message, + } + ) + ) + if r["type"] == "newChatItems": + return r["chatItems"] + raise ChatCommandError("unexpected response", r) + + async def api_send_text_message( + self, + chat: list | T.ChatRef | T.ChatInfo, + text: str, + in_reply_to: int | None = None, + ) -> list[T.AChatItem]: + msg: T.ComposedMessage = {"msgContent": {"type": "text", "text": text}, "mentions": {}} + if in_reply_to is not None: + msg["quotedItemId"] = in_reply_to + return await self.api_send_messages(chat, [msg]) + + async def api_send_text_reply(self, chat_item: T.AChatItem, text: str) -> list[T.AChatItem]: + return await self.api_send_text_message( + chat_item["chatInfo"], text, chat_item["chatItem"]["meta"]["itemId"] + ) + + async def api_update_chat_item( + self, + chat_type: T.ChatType, + chat_id: int, + chat_item_id: int, + msg_content: T.MsgContent, + live_message: bool = False, + ) -> T.ChatItem: + r = await self.send_chat_cmd( + CC.APIUpdateChatItem_cmd_string( + { + "chatRef": {"chatType": chat_type, "chatId": chat_id}, + "chatItemId": chat_item_id, + "liveMessage": live_message, + "updatedMessage": {"msgContent": msg_content, "mentions": {}}, + } + ) + ) + if r["type"] == "chatItemUpdated": + return r["chatItem"]["chatItem"] + raise ChatCommandError("error updating chat item", r) + + async def api_delete_chat_items( + self, + chat_type: T.ChatType, + chat_id: int, + chat_item_ids: list[int], + delete_mode: T.CIDeleteMode, + ) -> list[T.ChatItemDeletion]: + r = await self.send_chat_cmd( + CC.APIDeleteChatItem_cmd_string( + { + "chatRef": {"chatType": chat_type, "chatId": chat_id}, + "chatItemIds": chat_item_ids, + "deleteMode": delete_mode, + } + ) + ) + if r["type"] == "chatItemsDeleted": + return r["chatItemDeletions"] + raise ChatCommandError("error deleting chat item", r) + + async def api_delete_member_chat_item( + self, group_id: int, chat_item_ids: list[int] + ) -> list[T.ChatItemDeletion]: + r = await self.send_chat_cmd( + CC.APIDeleteMemberChatItem_cmd_string( + {"groupId": group_id, "chatItemIds": chat_item_ids} + ) + ) + if r["type"] == "chatItemsDeleted": + return r["chatItemDeletions"] + raise ChatCommandError("error deleting member chat item", r) + + async def api_chat_item_reaction( + self, + chat_type: T.ChatType, + chat_id: int, + chat_item_id: int, + add: bool, + reaction: T.MsgReaction, + ) -> T.ACIReaction: + r = await self.send_chat_cmd( + CC.APIChatItemReaction_cmd_string( + { + "chatRef": {"chatType": chat_type, "chatId": chat_id}, + "chatItemId": chat_item_id, + "add": add, + "reaction": reaction, + } + ) + ) + if r["type"] == "chatItemReaction": + return r["reaction"] + raise ChatCommandError("error setting item reaction", r) + + # ------------------------------------------------------------------ # + # File commands + # ------------------------------------------------------------------ # + + async def api_receive_file(self, file_id: int) -> T.AChatItem: + r = await self.send_chat_cmd( + CC.ReceiveFile_cmd_string({"fileId": file_id, "userApprovedRelays": True}) + ) + if r["type"] == "rcvFileAccepted": + return r["chatItem"] + raise ChatCommandError("error receiving file", r) + + async def api_cancel_file(self, file_id: int) -> None: + r = await self.send_chat_cmd(CC.CancelFile_cmd_string({"fileId": file_id})) + if r["type"] not in ("sndFileCancelled", "rcvFileCancelled"): + raise ChatCommandError("error canceling file", r) + + # ------------------------------------------------------------------ # + # Group commands + # ------------------------------------------------------------------ # + + async def api_add_member( + self, group_id: int, contact_id: int, member_role: T.GroupMemberRole + ) -> T.GroupMember: + r = await self.send_chat_cmd( + CC.APIAddMember_cmd_string( + {"groupId": group_id, "contactId": contact_id, "memberRole": member_role} + ) + ) + if r["type"] == "sentGroupInvitation": + return r["member"] + raise ChatCommandError("error adding member", r) + + async def api_join_group(self, group_id: int) -> T.GroupInfo: + r = await self.send_chat_cmd(CC.APIJoinGroup_cmd_string({"groupId": group_id})) + if r["type"] == "userAcceptedGroupSent": + return r["groupInfo"] + raise ChatCommandError("error joining group", r) + + async def api_accept_member( + self, group_id: int, group_member_id: int, member_role: T.GroupMemberRole + ) -> T.GroupMember: + r = await self.send_chat_cmd( + CC.APIAcceptMember_cmd_string( + {"groupId": group_id, "groupMemberId": group_member_id, "memberRole": member_role} + ) + ) + if r["type"] == "memberAccepted": + return r["member"] + raise ChatCommandError("error accepting member", r) + + async def api_set_members_role( + self, group_id: int, group_member_ids: list[int], member_role: T.GroupMemberRole + ) -> None: + r = await self.send_chat_cmd( + CC.APIMembersRole_cmd_string( + {"groupId": group_id, "groupMemberIds": group_member_ids, "memberRole": member_role} + ) + ) + if r["type"] != "membersRoleUser": + raise ChatCommandError("error setting members role", r) + + async def api_block_members_for_all( + self, group_id: int, group_member_ids: list[int], blocked: bool + ) -> None: + r = await self.send_chat_cmd( + CC.APIBlockMembersForAll_cmd_string( + {"groupId": group_id, "groupMemberIds": group_member_ids, "blocked": blocked} + ) + ) + if r["type"] != "membersBlockedForAllUser": + raise ChatCommandError("error blocking members", r) + + async def api_remove_members( + self, group_id: int, member_ids: list[int], with_messages: bool = False + ) -> list[T.GroupMember]: + r = await self.send_chat_cmd( + CC.APIRemoveMembers_cmd_string( + {"groupId": group_id, "groupMemberIds": member_ids, "withMessages": with_messages} + ) + ) + if r["type"] == "userDeletedMembers": + return r["members"] + raise ChatCommandError("error removing member", r) + + async def api_leave_group(self, group_id: int) -> T.GroupInfo: + r = await self.send_chat_cmd(CC.APILeaveGroup_cmd_string({"groupId": group_id})) + if r["type"] == "leftMemberUser": + return r["groupInfo"] + raise ChatCommandError("error leaving group", r) + + async def api_list_members(self, group_id: int) -> list[T.GroupMember]: + r = await self.send_chat_cmd(CC.APIListMembers_cmd_string({"groupId": group_id})) + if r["type"] == "groupMembers": + return r["group"]["members"] + raise ChatCommandError("error getting group members", r) + + async def api_new_group(self, user_id: int, group_profile: T.GroupProfile) -> T.GroupInfo: + r = await self.send_chat_cmd( + CC.APINewGroup_cmd_string( + {"userId": user_id, "groupProfile": group_profile, "incognito": False} + ) + ) + if r["type"] == "groupCreated": + return r["groupInfo"] + raise ChatCommandError("error creating group", r) + + async def api_update_group_profile( + self, group_id: int, group_profile: T.GroupProfile + ) -> T.GroupInfo: + r = await self.send_chat_cmd( + CC.APIUpdateGroupProfile_cmd_string( + {"groupId": group_id, "groupProfile": group_profile} + ) + ) + if r["type"] == "groupUpdated": + return r["toGroup"] + raise ChatCommandError("error updating group", r) + + # ------------------------------------------------------------------ # + # Group link commands + # ------------------------------------------------------------------ # + + async def api_create_group_link(self, group_id: int, member_role: T.GroupMemberRole) -> str: + r = await self.send_chat_cmd( + CC.APICreateGroupLink_cmd_string({"groupId": group_id, "memberRole": member_role}) + ) + if r["type"] == "groupLinkCreated": + link = r["groupLink"]["connLinkContact"] + return link.get("connShortLink") or link["connFullLink"] + raise ChatCommandError("error creating group link", r) + + async def api_set_group_link_member_role( + self, group_id: int, member_role: T.GroupMemberRole + ) -> None: + r = await self.send_chat_cmd( + CC.APIGroupLinkMemberRole_cmd_string({"groupId": group_id, "memberRole": member_role}) + ) + if r["type"] != "groupLink": + raise ChatCommandError("error setting group link member role", r) + + async def api_delete_group_link(self, group_id: int) -> None: + r = await self.send_chat_cmd(CC.APIDeleteGroupLink_cmd_string({"groupId": group_id})) + if r["type"] != "groupLinkDeleted": + raise ChatCommandError("error deleting group link", r) + + async def api_get_group_link(self, group_id: int) -> T.GroupLink: + r = await self.send_chat_cmd(CC.APIGetGroupLink_cmd_string({"groupId": group_id})) + if r["type"] == "groupLink": + return r["groupLink"] + raise ChatCommandError("error getting group link", r) + + async def api_get_group_link_str(self, group_id: int) -> str: + link = (await self.api_get_group_link(group_id))["connLinkContact"] + return link.get("connShortLink") or link["connFullLink"] + + # ------------------------------------------------------------------ # + # Connection commands + # ------------------------------------------------------------------ # + + async def api_create_link(self, user_id: int) -> str: + r = await self.send_chat_cmd( + CC.APIAddContact_cmd_string({"userId": user_id, "incognito": False}) + ) + if r["type"] == "invitation": + link = r["connLinkInvitation"] + return link.get("connShortLink") or link["connFullLink"] + raise ChatCommandError("error creating link", r) + + async def api_connect_plan( + self, user_id: int, connection_link: str + ) -> tuple[T.ConnectionPlan, T.CreatedConnLink]: + r = await self.send_chat_cmd( + CC.APIConnectPlan_cmd_string( + {"userId": user_id, "connectionLink": connection_link, "resolveKnown": False} + ) + ) + if r["type"] == "connectionPlan": + return (r["connectionPlan"], r["connLink"]) + raise ChatCommandError("error getting connect plan", r) + + async def api_connect( + self, + user_id: int, + incognito: bool, + prepared_link: T.CreatedConnLink | None = None, + ) -> ConnReqType: + args: CC.APIConnect = {"userId": user_id, "incognito": incognito} + if prepared_link is not None: + args["preparedLink_"] = prepared_link + r = await self.send_chat_cmd(CC.APIConnect_cmd_string(args)) + return self._handle_connect_result(r) + + async def api_connect_active_user(self, conn_link: str) -> ConnReqType: + r = await self.send_chat_cmd( + CC.Connect_cmd_string({"incognito": False, "connLink_": conn_link}) + ) + return self._handle_connect_result(r) + + def _handle_connect_result(self, r: CR.ChatResponse) -> ConnReqType: + if r["type"] == "sentConfirmation": + return "invitation" + if r["type"] == "sentInvitation": + return "contact" + if r["type"] == "contactAlreadyExists": + raise ChatCommandError("contact already exists", r) + raise ChatCommandError("connection error", r) + + async def api_accept_contact_request(self, contact_req_id: int) -> T.Contact: + r = await self.send_chat_cmd( + CC.APIAcceptContact_cmd_string({"contactReqId": contact_req_id}) + ) + if r["type"] == "acceptingContactRequest": + return r["contact"] + raise ChatCommandError("error accepting contact request", r) + + async def api_reject_contact_request(self, contact_req_id: int) -> None: + r = await self.send_chat_cmd( + CC.APIRejectContact_cmd_string({"contactReqId": contact_req_id}) + ) + if r["type"] != "contactRequestRejected": + raise ChatCommandError("error rejecting contact request", r) + + # ------------------------------------------------------------------ # + # Chat commands + # ------------------------------------------------------------------ # + + async def api_list_contacts(self, user_id: int) -> list[T.Contact]: + r = await self.send_chat_cmd(CC.APIListContacts_cmd_string({"userId": user_id})) + if r["type"] == "contactsList": + return r["contacts"] + raise ChatCommandError("error listing contacts", r) + + async def api_list_groups( + self, + user_id: int, + contact_id: int | None = None, + search: str | None = None, + ) -> list[T.GroupInfo]: + args: CC.APIListGroups = {"userId": user_id} + if contact_id is not None: + args["contactId_"] = contact_id + if search is not None: + args["search"] = search + r = await self.send_chat_cmd(CC.APIListGroups_cmd_string(args)) + if r["type"] == "groupsList": + return r["groups"] + raise ChatCommandError("error listing groups", r) + + async def api_get_chats( + self, + user_id: int, + pagination: T.PaginationByTime, + query: T.ChatListQuery | None = None, + pending_connections: bool = False, + ) -> list[T.AChat]: + if query is None: + query = {"type": "filters", "favorite": False, "unread": False} + r = await self.send_chat_cmd( + CC.APIGetChats_cmd_string( + { + "userId": user_id, + "pendingConnections": pending_connections, + "pagination": pagination, + "query": query, + } + ) + ) + if r["type"] == "apiChats": + return r["chats"] + raise ChatCommandError("error getting chats", r) + + async def api_delete_chat( + self, + chat_type: T.ChatType, + chat_id: int, + delete_mode: T.ChatDeleteMode | None = None, + ) -> None: + if delete_mode is None: + delete_mode = {"type": "full", "notify": True} + r = await self.send_chat_cmd( + CC.APIDeleteChat_cmd_string( + { + "chatRef": {"chatType": chat_type, "chatId": chat_id}, + "chatDeleteMode": delete_mode, + } + ) + ) + if chat_type == "direct" and r["type"] == "contactDeleted": + return + if chat_type == "group" and r["type"] == "groupDeletedUser": + return + raise ChatCommandError("error deleting chat", r) + + async def api_set_group_custom_data( + self, group_id: int, custom_data: dict[str, object] | None = None + ) -> None: + args: CC.APISetGroupCustomData = {"groupId": group_id} + if custom_data is not None: + args["customData"] = custom_data + r = await self.send_chat_cmd(CC.APISetGroupCustomData_cmd_string(args)) + if r["type"] != "cmdOk": + raise ChatCommandError("error setting group custom data", r) + + async def api_set_contact_custom_data( + self, contact_id: int, custom_data: dict[str, object] | None = None + ) -> None: + args: CC.APISetContactCustomData = {"contactId": contact_id} + if custom_data is not None: + args["customData"] = custom_data + r = await self.send_chat_cmd(CC.APISetContactCustomData_cmd_string(args)) + if r["type"] != "cmdOk": + raise ChatCommandError("error setting contact custom data", r) + + async def api_set_auto_accept_member_contacts(self, user_id: int, on_off: bool) -> None: + r = await self.send_chat_cmd( + CC.APISetUserAutoAcceptMemberContacts_cmd_string({"userId": user_id, "onOff": on_off}) + ) + if r["type"] != "cmdOk": + raise ChatCommandError("error setting auto-accept member contacts", r) + + async def api_get_chat(self, chat_type: T.ChatType, chat_id: int, count: int) -> dict[str, Any]: + ref = T.ChatType_cmd_string(chat_type) + str(chat_id) + r = await self.send_chat_cmd(f"/_get chat {ref} count={count}") + if r["type"] == "apiChat": + return r["chat"] + raise ChatCommandError("error getting chat", r) + + # ------------------------------------------------------------------ # + # User profile commands + # ------------------------------------------------------------------ # + + async def api_get_active_user(self) -> T.User | None: + try: + r = await self.send_chat_cmd(CC.ShowActiveUser_cmd_string({})) + if r["type"] == "activeUser": + return r["user"] + raise ChatCommandError("unexpected response", r) + except core.ChatAPIError as e: + ce = e.chat_error + if ( + ce is not None + and ce.get("type") == "error" + and ce.get("errorType", {}).get("type") == "noActiveUser" + ): + return None + raise + + async def api_create_active_user(self, profile: T.Profile | None = None) -> T.User: + new_user: T.NewUser = {"pastTimestamp": False, "userChatRelay": False} + if profile is not None: + new_user["profile"] = profile + r = await self.send_chat_cmd(CC.CreateActiveUser_cmd_string({"newUser": new_user})) + if r["type"] == "activeUser": + return r["user"] + raise ChatCommandError("unexpected response", r) + + async def api_list_users(self) -> list[T.UserInfo]: + r = await self.send_chat_cmd(CC.ListUsers_cmd_string({})) + if r["type"] == "usersList": + return r["users"] + raise ChatCommandError("error listing users", r) + + async def api_set_active_user(self, user_id: int, view_pwd: str | None = None) -> T.User: + args: CC.APISetActiveUser = {"userId": user_id} + if view_pwd is not None: + args["viewPwd"] = view_pwd + r = await self.send_chat_cmd(CC.APISetActiveUser_cmd_string(args)) + if r["type"] == "activeUser": + return r["user"] + raise ChatCommandError("error setting active user", r) + + async def api_delete_user( + self, user_id: int, del_smp_queues: bool, view_pwd: str | None = None + ) -> None: + args: CC.APIDeleteUser = {"userId": user_id, "delSMPQueues": del_smp_queues} + if view_pwd is not None: + args["viewPwd"] = view_pwd + r = await self.send_chat_cmd(CC.APIDeleteUser_cmd_string(args)) + if r["type"] != "cmdOk": + raise ChatCommandError("error deleting user", r) + + async def api_update_profile( + self, user_id: int, profile: T.Profile + ) -> T.UserProfileUpdateSummary | None: + r = await self.send_chat_cmd( + CC.APIUpdateProfile_cmd_string({"userId": user_id, "profile": profile}) + ) + if r["type"] == "userProfileNoChange": + return None + if r["type"] == "userProfileUpdated": + return r["updateSummary"] + raise ChatCommandError("error updating profile", r) + + async def api_set_contact_prefs(self, contact_id: int, preferences: T.Preferences) -> None: + r = await self.send_chat_cmd( + CC.APISetContactPrefs_cmd_string({"contactId": contact_id, "preferences": preferences}) + ) + if r["type"] != "contactPrefsUpdated": + raise ChatCommandError("error setting contact prefs", r) + + # ------------------------------------------------------------------ # + # Member contact commands + # ------------------------------------------------------------------ # + + async def api_create_member_contact(self, group_id: int, group_member_id: int) -> T.Contact: + r = await self.send_chat_cmd(f"/_create member contact #{group_id} {group_member_id}") + if r["type"] == "newMemberContact": + return r["contact"] + raise ChatCommandError("error creating member contact", r) + + async def api_send_member_contact_invitation( + self, + contact_id: int, + message: T.MsgContent | str | None = None, + ) -> T.Contact: + cmd = f"/_invite member contact @{contact_id}" + if message is not None: + if isinstance(message, str): + cmd += f" text {message}" + else: + cmd += f" json {json.dumps(message)}" + r = await self.send_chat_cmd(cmd) + if r["type"] == "newMemberContactSentInv": + return r["contact"] + raise ChatCommandError("error sending member contact invitation", r) diff --git a/packages/simplex-chat-python/src/simplex_chat/bot.py b/packages/simplex-chat-python/src/simplex_chat/bot.py new file mode 100644 index 0000000000..39709deee6 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/bot.py @@ -0,0 +1,707 @@ +"""User-facing `Bot` API: decorators, filters, Message wrapper, lifecycle.""" + +from __future__ import annotations + +import asyncio +import logging +import os +import signal as _signal +from collections.abc import Awaitable, Callable +from dataclasses import dataclass +from typing import Any, Generic, Literal, TypeVar, overload + +from . import util +from .api import ChatApi, Db +from .core import MigrationConfirmation +from .filters import compile_message_filter +from .types import CEvt, T + +log = logging.getLogger("simplex_chat") + +C = TypeVar("C", bound="T.MsgContent") + + +@dataclass(slots=True) +class BotProfile: + display_name: str + full_name: str = "" + short_descr: str | None = None + image: str | None = None + + +@dataclass(slots=True) +class BotCommand: + keyword: str + label: str + + +@dataclass(slots=True, frozen=True) +class ParsedCommand: + keyword: str + args: str + + +@dataclass(slots=True, frozen=True) +class Message(Generic[C]): + chat_item: T.AChatItem + content: C + bot: "Bot" + + @property + def chat_info(self) -> T.ChatInfo: + return self.chat_item["chatInfo"] + + @property + def text(self) -> str | None: + c = self.content + if isinstance(c, dict): + return c.get("text") # type: ignore[return-value] + return None + + async def reply(self, text: str) -> "Message[T.MsgContent]": + items = await self.bot.api.api_send_text_reply(self.chat_item, text) + ci = items[0] + content = ci["chatItem"]["content"] + # content is CIContent — snd variant has msgContent; cast for type safety. + msg_content: T.MsgContent = content["msgContent"] # type: ignore[index] + return Message(chat_item=ci, content=msg_content, bot=self.bot) + + async def reply_content(self, content: T.MsgContent) -> "Message[T.MsgContent]": + items = await self.bot.api.api_send_messages( + self.chat_info, [{"msgContent": content, "mentions": {}}] + ) + ci = items[0] + ci_content = ci["chatItem"]["content"] + msg_content: T.MsgContent = ci_content["msgContent"] # type: ignore[index] + return Message(chat_item=ci, content=msg_content, bot=self.bot) + + +# Concrete narrowed aliases — one per MsgContent_ variant in _types.py. +TextMessage = Message[T.MsgContent_text] +LinkMessage = Message[T.MsgContent_link] +ImageMessage = Message[T.MsgContent_image] +VideoMessage = Message[T.MsgContent_video] +VoiceMessage = Message[T.MsgContent_voice] +FileMessage = Message[T.MsgContent_file] +ReportMessage = Message[T.MsgContent_report] +ChatMessage = Message[T.MsgContent_chat] +UnknownMessage = Message[T.MsgContent_unknown] + +MessageHandler = Callable[[Message[Any]], Awaitable[None]] +CommandHandler = Callable[[Message[Any], ParsedCommand], Awaitable[None]] +EventHandler = Callable[[CEvt.ChatEvent], Awaitable[None]] + + +class Middleware: + """Override `__call__` to wrap message handlers with cross-cutting logic. + + `handler` is the next stage in the chain — call it with `(message, data)` + to continue, or skip the call to short-circuit. `data` is a per-dispatch + dict that middleware can use to pass values down the chain. + """ + + async def __call__( + self, + handler: Callable[[Message[Any], dict[str, object]], Awaitable[None]], + message: Message[Any], + data: dict[str, object], + ) -> None: + await handler(message, data) + + +class Bot: + def __init__( + self, + *, + profile: BotProfile, + db: Db, + welcome: str | T.MsgContent | None = None, + commands: list[BotCommand] | None = None, + confirm_migrations: MigrationConfirmation = MigrationConfirmation.YES_UP, + create_address: bool = True, + update_address: bool = True, + update_profile: bool = True, + auto_accept: bool = True, + business_address: bool = False, + allow_files: bool = False, + use_bot_profile: bool = True, + log_contacts: bool = True, + log_network: bool = False, + ) -> None: + self._profile = profile + self._db = db + self._welcome = welcome + self._commands = commands or [] + self._confirm_migrations = confirm_migrations + self._opts = { + "create_address": create_address, + "update_address": update_address, + "update_profile": update_profile, + "auto_accept": auto_accept, + "business_address": business_address, + "allow_files": allow_files, + "use_bot_profile": use_bot_profile, + "log_contacts": log_contacts, + "log_network": log_network, + } + self._api: ChatApi | None = None + self._serving = False + self._stop_event = asyncio.Event() + self._message_handlers: list[tuple[Callable[[Message[Any]], bool], MessageHandler]] = [] + self._command_handlers: list[ + tuple[tuple[str, ...], Callable[[Message[Any]], bool], CommandHandler] + ] = [] + self._event_handlers: dict[str, list[EventHandler]] = {} + self._middleware: list[Middleware] = [] + # Track default-handler registration so __aenter__ on a re-used bot + # doesn't accumulate duplicate log/error handlers. + self._defaults_registered = False + + @property + def api(self) -> ChatApi: + if self._api is None: + raise RuntimeError("Bot not initialized — call bot.run() or use `async with bot:`") + return self._api + + # ------------------------------------------------------------------ # + # Decorators + # ------------------------------------------------------------------ # + + @overload + def on_message( + self, *, content_type: Literal["text"], **rest: Any + ) -> Callable[ + [Callable[[TextMessage], Awaitable[None]]], + Callable[[TextMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["link"], **rest: Any + ) -> Callable[ + [Callable[[LinkMessage], Awaitable[None]]], + Callable[[LinkMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["image"], **rest: Any + ) -> Callable[ + [Callable[[ImageMessage], Awaitable[None]]], + Callable[[ImageMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["video"], **rest: Any + ) -> Callable[ + [Callable[[VideoMessage], Awaitable[None]]], + Callable[[VideoMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["voice"], **rest: Any + ) -> Callable[ + [Callable[[VoiceMessage], Awaitable[None]]], + Callable[[VoiceMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["file"], **rest: Any + ) -> Callable[ + [Callable[[FileMessage], Awaitable[None]]], + Callable[[FileMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["report"], **rest: Any + ) -> Callable[ + [Callable[[ReportMessage], Awaitable[None]]], + Callable[[ReportMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["chat"], **rest: Any + ) -> Callable[ + [Callable[[ChatMessage], Awaitable[None]]], + Callable[[ChatMessage], Awaitable[None]], + ]: ... + + @overload + def on_message( + self, *, content_type: Literal["unknown"], **rest: Any + ) -> Callable[ + [Callable[[UnknownMessage], Awaitable[None]]], + Callable[[UnknownMessage], Awaitable[None]], + ]: ... + + @overload + def on_message(self, **rest: Any) -> Callable[[MessageHandler], MessageHandler]: ... + + def on_message(self, **filter_kw: Any) -> Callable[[MessageHandler], MessageHandler]: + predicate = compile_message_filter(filter_kw) + + def deco(fn: MessageHandler) -> MessageHandler: + self._message_handlers.append((predicate, fn)) + return fn + + return deco + + def on_command( + self, name: str | tuple[str, ...], **filter_kw: Any + ) -> Callable[[CommandHandler], CommandHandler]: + names = (name,) if isinstance(name, str) else tuple(name) + predicate = compile_message_filter(filter_kw) + + def deco(fn: CommandHandler) -> CommandHandler: + self._command_handlers.append((names, predicate, fn)) + return fn + + return deco + + def on_event(self, event: CEvt.ChatEvent_Tag, /) -> Callable[[EventHandler], EventHandler]: + def deco(fn: EventHandler) -> EventHandler: + self._event_handlers.setdefault(event, []).append(fn) + return fn + + return deco + + def use(self, middleware: Middleware) -> None: + self._middleware.append(middleware) + + # ------------------------------------------------------------------ # + # Lifecycle + # ------------------------------------------------------------------ # + + async def __aenter__(self) -> "Bot": + # Order matters: libsimplex `/_start` requires an active user, so + # ensure (or create) the user first, THEN start the chat, THEN + # do address + profile sync. Mirrors Node bot.ts:48-64. + self._api = await ChatApi.init(self._db, self._confirm_migrations) + user = await self._ensure_active_user() + await self._api.start_chat() + await self._sync_address_and_profile(user) + self._register_log_handlers() + return self + + async def __aexit__(self, *exc_info: object) -> None: + self.stop() + if self._api is not None: + try: + await self._api.stop_chat() + finally: + await self._api.close() + self._api = None + + def run(self) -> None: + """Blocking entry: runs serve_forever() with SIGINT/SIGTERM handlers installed. + + Configures `logging.basicConfig(level=INFO)` if the root logger has no + handlers yet, so the bot's startup messages and the announced address + are visible without callers having to set up logging. Embedders that + manage logging themselves are unaffected (basicConfig is a no-op when + handlers already exist). + """ + if not logging.getLogger().handlers: + logging.basicConfig( + level=logging.INFO, + format="%(asctime)s %(levelname)s %(name)s %(message)s", + ) + + async def _main() -> None: + async with self: + loop = asyncio.get_running_loop() + # First Ctrl+C → graceful stop (~500ms, bounded by the + # receive-loop poll interval). Second Ctrl+C → force-exit + # immediately (in case stop_chat / close hang on a wedged + # FFI call). Standard CLI UX (jupyter, ipython, …). + sigint_count = 0 + + def on_interrupt() -> None: + nonlocal sigint_count + sigint_count += 1 + if sigint_count == 1: + log.info("stopping bot... (press Ctrl+C again to force exit)") + self.stop() + else: + os._exit(130) # 128 + SIGINT + + if hasattr(_signal, "SIGINT"): + try: + loop.add_signal_handler(_signal.SIGINT, on_interrupt) + loop.add_signal_handler(_signal.SIGTERM, self.stop) + except NotImplementedError: # Windows + _signal.signal(_signal.SIGINT, lambda *_: on_interrupt()) + await self.serve_forever() + + asyncio.run(_main()) + + async def serve_forever(self) -> None: + if self._serving: + raise RuntimeError("already serving") + self._serving = True + self._stop_event.clear() + try: + await self._receive_loop() + finally: + self._serving = False + + def stop(self) -> None: + self._stop_event.set() + + async def _receive_loop(self) -> None: + # Catch broad Exception so a single malformed event or transient + # native error doesn't crash the whole bot. CancelledError must + # always re-raise so `bot.stop()` and asyncio cancellation work. + # `wait_us=500_000` (500ms) bounds the worst-case Ctrl+C latency: + # the C call blocks the worker thread until timeout, and the loop + # only checks `_stop_event` between polls. + while not self._stop_event.is_set(): + try: + event = await self.api.recv_chat_event(wait_us=500_000) + except asyncio.CancelledError: + raise + except Exception: + log.exception("recv_chat_event failed") + # Bound the spin rate when the FFI is wedged on a persistent + # error (vs the timeout path, which already paces itself). + await asyncio.sleep(0.5) + continue + if event is None: + continue + try: + await self._dispatch_event(event) + except asyncio.CancelledError: + raise + except Exception: + log.exception("dispatch_event failed for tag=%s", event.get("type")) + + # ------------------------------------------------------------------ # + # Dispatch + # ------------------------------------------------------------------ # + + async def _dispatch_event(self, event: CEvt.ChatEvent) -> None: + tag = event["type"] + for h in self._event_handlers.get(tag, []): + try: + await h(event) + except Exception: + log.exception("on_event handler failed") + if tag == "newChatItems": + evt: CEvt.NewChatItems = event # type: ignore[assignment] + for ci in evt["chatItems"]: + content = ci["chatItem"]["content"] + if content["type"] != "rcvMsgContent": + continue + msg_content = content["msgContent"] # type: ignore[index] + msg: Message[T.MsgContent] = Message(chat_item=ci, content=msg_content, bot=self) + await self._dispatch_message(msg) + + async def _dispatch_message(self, msg: Message[Any]) -> None: + # First-match-wins. The squaring bot's `@on_message(text=NUMBER_RE)` + # and catch-all `@on_message(content_type="text")` both match a number + # like "1"; we want only the first to fire. Registration order is the + # priority order — register the most-specific filters first. + # + # Slash-commands are tried first against command handlers; if no + # command handler matches, fall through to message handlers (so + # `@on_message` can still catch unknown slash-commands). + cmd = self._parse_command(msg) + if cmd is not None: + for names, predicate, handler in self._command_handlers: + if cmd.keyword in names and predicate(msg): + await self._invoke_command_with_middleware(handler, msg, cmd) + return + for predicate, handler in self._message_handlers: + if predicate(msg): + await self._invoke_with_middleware(handler, msg) + return + + async def _invoke_with_middleware(self, handler: MessageHandler, message: Message[Any]) -> None: + # Fast path: most bots register no middleware. Skip the closure-chain + # construction and the empty-data dict on every dispatch. + if not self._middleware: + try: + await handler(message) + except Exception: + log.exception("message handler failed") + return + + async def call(m: Message[Any], _data: dict[str, object]) -> None: + await handler(m) + + chain: Callable[[Message[Any], dict[str, object]], Awaitable[None]] = call + for mw in reversed(self._middleware): + inner = chain + + async def _wrapped( + m: Message[Any], + d: dict[str, object], + mw: Middleware = mw, + inner: Callable[[Message[Any], dict[str, object]], Awaitable[None]] = inner, + ) -> None: + await mw(inner, m, d) + + chain = _wrapped + + try: + await chain(message, {}) + except Exception: + log.exception("message handler failed") + + async def _invoke_command_with_middleware( + self, handler: CommandHandler, message: Message[Any], cmd: ParsedCommand + ) -> None: + if not self._middleware: + try: + await handler(message, cmd) + except Exception: + log.exception("command handler failed") + return + + async def call(m: Message[Any], _data: dict[str, object]) -> None: + await handler(m, cmd) + + chain: Callable[[Message[Any], dict[str, object]], Awaitable[None]] = call + for mw in reversed(self._middleware): + inner = chain + + async def _wrapped( + m: Message[Any], + d: dict[str, object], + mw: Middleware = mw, + inner: Callable[[Message[Any], dict[str, object]], Awaitable[None]] = inner, + ) -> None: + await mw(inner, m, d) + + chain = _wrapped + + try: + await chain(message, {}) + except Exception: + log.exception("command handler failed") + + @staticmethod + def _parse_command(msg: Message[Any]) -> ParsedCommand | None: + parsed = util.ci_bot_command(msg.chat_item["chatItem"]) + if parsed is None: + return None + keyword, args = parsed + return ParsedCommand(keyword=keyword, args=args) + + # ------------------------------------------------------------------ # + # Profile + address sync + # ------------------------------------------------------------------ # + + async def _ensure_active_user(self) -> T.User: + """Get or create the active user. Must run before `start_chat`. + + Mirrors Node `createBotUser` (bot.ts:158-166). The chat controller + won't accept `/_start` without a user, so this phase has to land + before lifecycle proceeds. + """ + api = self.api + user = await api.api_get_active_user() + if user is None: + log.info("No active user in database, creating...") + user = await api.api_create_active_user(self._bot_profile_to_wire()) + log.info("Bot user: %s", user["profile"]["displayName"]) + return user + + async def _sync_address_and_profile(self, user: T.User) -> None: + """Address + profile sync. Runs after `start_chat` (mirrors bot.ts:57-63).""" + api = self.api + user_id = user["userId"] + + # 2. Address (numbered to match bot.ts comments — phase 1 was user creation). + address = await api.api_get_user_address(user_id) + if address is None: + if self._opts["create_address"]: + log.info("Bot has no address, creating...") + await api.api_create_user_address(user_id) + address = await api.api_get_user_address(user_id) + if address is None: + raise RuntimeError("Failed reading newly created user address") + else: + log.warning("Bot has no address") + + # Always announce the address — matches Node bot.ts:60. + link: str | None = None + if address is not None: + link = util.contact_address_str(address["connLinkContact"]) + log.info("Bot address: %s", link) + + # 3. Address settings (auto-accept + welcome message). Mirrors bot.ts:185-194. + # autoAccept present → accept; absent → no auto-accept (mirrors Node bot.ts). + if address is not None and self._opts["update_address"]: + desired: T.AddressSettings = {"businessAddress": self._opts["business_address"]} + if self._opts["auto_accept"]: + desired["autoAccept"] = {"acceptIncognito": False} + if self._welcome is not None: + desired["autoReply"] = ( + {"type": "text", "text": self._welcome} + if isinstance(self._welcome, str) + else self._welcome + ) + if address["addressSettings"] != desired: + log.info("Bot address settings changed, updating...") + await api.api_set_address_settings(user_id, desired) + + # 4. Profile update. Mirrors Node `updateBotUserProfile` (bot.ts:199-214). + # Field-by-field comparison: user["profile"] is LocalProfile (has extra + # fields profileId, localAlias, preferences, peerType) so a full-dict + # equality would always differ. + new_profile = self._bot_profile_to_wire() + if link is not None and self._opts["use_bot_profile"]: + # Mirrors bot.ts:62 — embed the connection link in the bot's profile + # so contacts that resolve the bot via stored profile data see the + # current address. + new_profile["contactLink"] = link + cur = user["profile"] + changed = ( + cur["displayName"] != new_profile["displayName"] + or cur.get("fullName", "") != new_profile.get("fullName", "") + or cur.get("shortDescr") != new_profile.get("shortDescr") + or cur.get("image") != new_profile.get("image") + or cur.get("preferences") != new_profile.get("preferences") + or cur.get("peerType") != new_profile.get("peerType") + or cur.get("contactLink") != new_profile.get("contactLink") + ) + if changed and self._opts["update_profile"]: + log.info("Bot profile changed, updating...") + await api.api_update_profile(user_id, new_profile) + + def _bot_profile_to_wire(self) -> T.Profile: + """Construct wire-format Profile, applying bot conventions when use_bot_profile=True. + + Mirrors Node mkBotProfile (bot.ts:88-102): bots get peerType="bot", + calls/voice prefs disabled, files gated on `allow_files`, and any + registered `commands` embedded in the profile preferences. + """ + p: T.Profile = { + "displayName": self._profile.display_name, + "fullName": self._profile.full_name, + } + if self._profile.short_descr is not None: + p["shortDescr"] = self._profile.short_descr + if self._profile.image is not None: + p["image"] = self._profile.image + if self._opts["use_bot_profile"]: + prefs: T.Preferences = { + "calls": {"allow": "no"}, + "voice": {"allow": "no"}, + "files": {"allow": "yes" if self._opts["allow_files"] else "no"}, + } + if self._commands: + prefs["commands"] = [ + {"type": "command", "keyword": c.keyword, "label": c.label} + for c in self._commands + ] + p["preferences"] = prefs + p["peerType"] = "bot" + elif self._commands: + raise ValueError( + "use_bot_profile=False but commands were passed; commands are " + "only sent when use_bot_profile=True (they're embedded in the " + "user profile preferences)." + ) + return p + + # ------------------------------------------------------------------ # + # Log subscriptions (mirror Node subscribeLogEvents bot.ts:142-156) + # ------------------------------------------------------------------ # + + def _register_log_handlers(self) -> None: + # Idempotent: a Bot reused across multiple `__aenter__` cycles must + # not stack duplicate log handlers. Always-on error handlers run + # regardless of log_contacts/log_network so messageError/chatError/ + # chatErrors don't disappear into the void. + if self._defaults_registered: + return + self._defaults_registered = True + self._event_handlers.setdefault("messageError", []).append(self._log_message_error) + self._event_handlers.setdefault("chatError", []).append(self._log_chat_error) + self._event_handlers.setdefault("chatErrors", []).append(self._log_chat_errors) + if self._opts["log_contacts"]: + self._event_handlers.setdefault("contactConnected", []).append( + self._log_contact_connected + ) + self._event_handlers.setdefault("contactDeletedByContact", []).append( + self._log_contact_deleted + ) + if self._opts["log_network"]: + self._event_handlers.setdefault("hostConnected", []).append(self._log_host_connected) + self._event_handlers.setdefault("hostDisconnected", []).append( + self._log_host_disconnected + ) + self._event_handlers.setdefault("subscriptionStatus", []).append( + self._log_subscription_status + ) + + @staticmethod + async def _log_contact_connected(evt: CEvt.ChatEvent) -> None: + log.info("%s connected", evt["contact"]["profile"]["displayName"]) # type: ignore[index] + + @staticmethod + async def _log_contact_deleted(evt: CEvt.ChatEvent) -> None: + log.info( + "%s deleted connection with bot", + evt["contact"]["profile"]["displayName"], # type: ignore[index] + ) + + @staticmethod + async def _log_host_connected(evt: CEvt.ChatEvent) -> None: + log.info("connected server %s", evt["transportHost"]) # type: ignore[index] + + @staticmethod + async def _log_host_disconnected(evt: CEvt.ChatEvent) -> None: + log.info("disconnected server %s", evt["transportHost"]) # type: ignore[index] + + @staticmethod + async def _log_subscription_status(evt: CEvt.ChatEvent) -> None: + log.info( + "%d subscription(s) %s", + len(evt["connections"]), # type: ignore[index] + evt["subscriptionStatus"]["type"], # type: ignore[index] + ) + + @staticmethod + async def _log_message_error(evt: CEvt.ChatEvent) -> None: + log.warning("messageError: %s", evt.get("severity", "?")) # type: ignore[union-attr] + + @staticmethod + async def _log_chat_error(evt: CEvt.ChatEvent) -> None: + err = evt.get("chatError") # type: ignore[union-attr] + log.error("chatError: %s", err.get("type") if isinstance(err, dict) else err) + + @staticmethod + async def _log_chat_errors(evt: CEvt.ChatEvent) -> None: + errs = evt.get("chatErrors") or [] # type: ignore[union-attr] + log.error("chatErrors: %d errors", len(errs)) + + +# Suppress unused-import warnings for re-exported names used only at type-check time. +__all__ = [ + "Bot", + "BotCommand", + "BotProfile", + "ChatMessage", + "FileMessage", + "ImageMessage", + "LinkMessage", + "Message", + "MessageHandler", + "CommandHandler", + "EventHandler", + "Middleware", + "ParsedCommand", + "ReportMessage", + "TextMessage", + "UnknownMessage", + "VideoMessage", + "VoiceMessage", +] diff --git a/packages/simplex-chat-python/src/simplex_chat/core.py b/packages/simplex-chat-python/src/simplex_chat/core.py new file mode 100644 index 0000000000..075db34b52 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/core.py @@ -0,0 +1,200 @@ +"""Internal typed async wrapper around libsimplex's 8 C ABI functions. + +Users interact with `Bot` / `ChatApi`. This module is exposed as +`simplex_chat.core` for tests and the api.ChatApi class only. +""" + +from __future__ import annotations + +import asyncio +import ctypes +import json +from enum import StrEnum +from typing import Any, TypedDict + +from . import _native +from .types import T, CR, CEvt + + +class ChatAPIError(Exception): + """Raised when chat_send_cmd / chat_recv_msg_wait returns a chat error.""" + + def __init__(self, message: str, chat_error: T.ChatError | None = None): + super().__init__(message) + self.chat_error = chat_error + + +class ChatInitError(Exception): + """Raised when chat_migrate_init returns a DBMigrationResult error.""" + + def __init__(self, message: str, db_migration_error: dict[str, Any]): + super().__init__(message) + self.db_migration_error = db_migration_error + + +class MigrationConfirmation(StrEnum): + YES_UP = "yesUp" + YES_UP_DOWN = "yesUpDown" + CONSOLE = "console" + ERROR = "error" + + +class CryptoArgs(TypedDict): # wire-format JSON; camelCase fields + fileKey: str + fileNonce: str + + +def _read_and_free(ptr: int | None) -> str: + """Copy a Haskell-allocated null-terminated UTF-8 string and free its buffer. + + Mirrors HandleCResult in packages/simplex-chat-nodejs/cpp/simplex.cc:157-165. + """ + if not ptr: + raise RuntimeError("null pointer returned from libsimplex") + try: + return ctypes.string_at(ptr).decode("utf-8") + finally: + _native.libc().free(ctypes.c_void_p(ptr)) + + +async def chat_send_cmd(ctrl: int, cmd: str) -> CR.ChatResponse: + def _call() -> str: + ptr = _native.lib().chat_send_cmd(ctrl, cmd.encode("utf-8")) + return _read_and_free(ptr) + + raw = await asyncio.to_thread(_call) + parsed = json.loads(raw) + if "result" in parsed and isinstance(parsed["result"], dict): + return parsed["result"] # type: ignore[return-value] + err = parsed.get("error") + if isinstance(err, dict): + raise ChatAPIError(f"chat command error: {err.get('type')}", err) # type: ignore[arg-type] + raise ChatAPIError(f"invalid chat command result: {raw[:200]}") + + +async def chat_recv_msg_wait(ctrl: int, wait_us: int = 500_000) -> CEvt.ChatEvent | None: + def _call() -> str: + # On timeout, the C side returns a non-NULL pointer to a single NUL byte + # (see Mobile.hs `fromMaybe ""`), so `_read_and_free` returns "" — no + # NULL-pointer guard is needed here. + ptr = _native.lib().chat_recv_msg_wait(ctrl, wait_us) + return _read_and_free(ptr) + + raw = await asyncio.to_thread(_call) + if not raw: + return None + parsed = json.loads(raw) + if "result" in parsed and isinstance(parsed["result"], dict): + return parsed["result"] # type: ignore[return-value] + err = parsed.get("error") + if isinstance(err, dict): + raise ChatAPIError(f"chat event error: {err.get('type')}", err) # type: ignore[arg-type] + raise ChatAPIError(f"invalid chat event: {raw[:200]}") + + +async def chat_migrate_init(db_path: str, db_key: str, confirm: MigrationConfirmation) -> int: + """Initialize chat controller. Returns opaque ctrl pointer as Python int.""" + + def _call() -> tuple[int, str]: + ctrl = ctypes.c_void_p() + ptr = _native.lib().chat_migrate_init( + db_path.encode("utf-8"), + db_key.encode("utf-8"), + confirm.encode("utf-8"), + ctypes.byref(ctrl), + ) + return (ctrl.value or 0, _read_and_free(ptr)) + + ctrl_val, raw = await asyncio.to_thread(_call) + parsed = json.loads(raw) + if parsed.get("type") == "ok": + if not ctrl_val: + # ABI invariant: type=="ok" → out-param written. Defensive guard so a + # broken libsimplex doesn't hand us a NULL controller that would only + # crash on first use much later. + raise RuntimeError("chat_migrate_init returned ok but did not set ctrl pointer") + return ctrl_val + raise ChatInitError( + "Database or migration error (see db_migration_error)", + parsed, + ) + + +async def chat_close_store(ctrl: int) -> None: + def _call() -> str: + ptr = _native.lib().chat_close_store(ctrl) + return _read_and_free(ptr) + + res = await asyncio.to_thread(_call) + if res: + raise RuntimeError(res) + + +async def chat_write_file(ctrl: int, path: str, data: bytes) -> CryptoArgs: + def _call() -> str: + ptr = _native.lib().chat_write_file(ctrl, path.encode("utf-8"), data, len(data)) + return _read_and_free(ptr) + + raw = await asyncio.to_thread(_call) + return _crypto_args_result(raw) + + +async def chat_read_file(path: str, args: CryptoArgs) -> bytes: + def _call() -> bytes: + ptr = _native.lib().chat_read_file( + path.encode("utf-8"), + args["fileKey"].encode("utf-8"), + args["fileNonce"].encode("utf-8"), + ) + if not ptr: + raise RuntimeError("chat_read_file returned null") + addr = ctypes.cast(ptr, ctypes.c_void_p).value + assert addr is not None # `if not ptr` above already filtered NULL + try: + status = ctypes.cast(addr, ctypes.POINTER(ctypes.c_uint8))[0] + if status == 1: + msg = ctypes.string_at(addr + 1).decode("utf-8") + raise RuntimeError(msg) + if status != 0: + raise RuntimeError(f"unexpected status {status} from chat_read_file") + # `addr + 1` is unaligned for a uint32 read. On the supported platforms + # (linux-x86_64, linux-aarch64, macos-aarch64, windows-x86_64) this is + # silently handled; matches the Node.js binding (cpp/simplex.cc:344). + length = ctypes.cast(addr + 1, ctypes.POINTER(ctypes.c_uint32))[0] + return ctypes.string_at(addr + 5, length) + finally: + _native.libc().free(ctypes.c_void_p(addr)) + + return await asyncio.to_thread(_call) + + +async def chat_encrypt_file(ctrl: int, src: str, dst: str) -> CryptoArgs: + def _call() -> str: + ptr = _native.lib().chat_encrypt_file(ctrl, src.encode("utf-8"), dst.encode("utf-8")) + return _read_and_free(ptr) + + return _crypto_args_result(await asyncio.to_thread(_call)) + + +async def chat_decrypt_file(src: str, args: CryptoArgs, dst: str) -> None: + def _call() -> str: + ptr = _native.lib().chat_decrypt_file( + src.encode("utf-8"), + args["fileKey"].encode("utf-8"), + args["fileNonce"].encode("utf-8"), + dst.encode("utf-8"), + ) + return _read_and_free(ptr) + + res = await asyncio.to_thread(_call) + if res: + raise RuntimeError(res) + + +def _crypto_args_result(raw: str) -> CryptoArgs: + parsed = json.loads(raw) + if parsed.get("type") == "result": + return parsed["cryptoArgs"] + if parsed.get("type") == "error": + raise RuntimeError(parsed.get("writeError", "unknown write error")) + raise RuntimeError(f"unexpected result: {raw[:200]}") diff --git a/packages/simplex-chat-python/src/simplex_chat/filters.py b/packages/simplex-chat-python/src/simplex_chat/filters.py new file mode 100644 index 0000000000..cdce5b7bb6 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/filters.py @@ -0,0 +1,45 @@ +"""Compile kwarg-based message filters into a single predicate.""" + +from __future__ import annotations + +import re +from typing import Any, Callable + + +def compile_message_filter(kw: dict[str, Any]) -> Callable[[Any], bool]: + """Compile filter kwargs into a single predicate function. + + Multiple kwargs combine with AND; tuples within a kwarg combine with OR. + `when` is the last predicate evaluated. + """ + predicates: list[Callable[[Any], bool]] = [] + + if (ct := kw.get("content_type")) is not None: + ct_set = (ct,) if isinstance(ct, str) else tuple(ct) + predicates.append(lambda m: m.content.get("type") in ct_set) + + if (t := kw.get("text")) is not None: + if isinstance(t, re.Pattern): + predicates.append(lambda m: bool(t.search(m.content.get("text", "") or ""))) + else: + predicates.append(lambda m: m.content.get("text") == t) + + if (cht := kw.get("chat_type")) is not None: + cht_set = (cht,) if isinstance(cht, str) else tuple(cht) + predicates.append(lambda m: m.chat_item["chatInfo"]["type"] in cht_set) + + if (gid := kw.get("group_id")) is not None: + gid_set: tuple[int, ...] = (gid,) if isinstance(gid, int) else tuple(gid) + + def gid_match(m: Any) -> bool: + ci = m.chat_item["chatInfo"] + return ci["type"] == "group" and ci["groupInfo"]["groupId"] in gid_set + + predicates.append(gid_match) + + if (when := kw.get("when")) is not None: + predicates.append(when) + + if not predicates: + return lambda _m: True + return lambda m: all(p(m) for p in predicates) diff --git a/packages/simplex-chat-python/src/simplex_chat/py.typed b/packages/simplex-chat-python/src/simplex_chat/py.typed new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/simplex-chat-python/src/simplex_chat/types/__init__.py b/packages/simplex-chat-python/src/simplex_chat/types/__init__.py new file mode 100644 index 0000000000..4d21965dd4 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/types/__init__.py @@ -0,0 +1,16 @@ +"""SimpleX Chat wire types — auto-generated from Haskell. + +Re-exports the four generated modules as namespaces: + +- ``T`` — :mod:`._types` (records, enums, discriminated unions) +- ``CC`` — :mod:`._commands` (command TypedDicts + ``_cmd_string`` helpers) +- ``CR`` — :mod:`._responses` (``ChatResponse`` and member TypedDicts) +- ``CEvt`` — :mod:`._events` (``ChatEvent`` and member TypedDicts) +""" + +from . import _commands as CC +from . import _events as CEvt +from . import _responses as CR +from . import _types as T + +__all__ = ["T", "CC", "CR", "CEvt"] diff --git a/packages/simplex-chat-python/src/simplex_chat/types/_commands.py b/packages/simplex-chat-python/src/simplex_chat/types/_commands.py new file mode 100644 index 0000000000..9806388835 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/types/_commands.py @@ -0,0 +1,705 @@ +# API Commands +# This file is generated automatically. +from __future__ import annotations +import json +from typing import NotRequired, TypedDict +from . import _types as T +from . import _responses as CR + +# Address commands +# Bots can use these commands to automatically check and create address when initialized + +# Create bot address. +# Network usage: interactive. +class APICreateMyAddress(TypedDict): + userId: int # int64 + + +def APICreateMyAddress_cmd_string(self: APICreateMyAddress) -> str: + return '/_address ' + str(self['userId']) + +APICreateMyAddress_Response = CR.UserContactLinkCreated | CR.ChatCmdError + + +# Delete bot address. +# Network usage: background. +class APIDeleteMyAddress(TypedDict): + userId: int # int64 + + +def APIDeleteMyAddress_cmd_string(self: APIDeleteMyAddress) -> str: + return '/_delete_address ' + str(self['userId']) + +APIDeleteMyAddress_Response = CR.UserContactLinkDeleted | CR.ChatCmdError + + +# Get bot address and settings. +# Network usage: no. +class APIShowMyAddress(TypedDict): + userId: int # int64 + + +def APIShowMyAddress_cmd_string(self: APIShowMyAddress) -> str: + return '/_show_address ' + str(self['userId']) + +APIShowMyAddress_Response = CR.UserContactLink | CR.ChatCmdError + + +# Add address to bot profile. +# Network usage: interactive. +class APISetProfileAddress(TypedDict): + userId: int # int64 + enable: bool + + +def APISetProfileAddress_cmd_string(self: APISetProfileAddress) -> str: + return '/_profile_address ' + str(self['userId']) + ' ' + ('on' if self['enable'] else 'off') + +APISetProfileAddress_Response = CR.UserProfileUpdated | CR.ChatCmdError + + +# Set bot address settings. +# Network usage: interactive. +class APISetAddressSettings(TypedDict): + userId: int # int64 + settings: "T.AddressSettings" + + +def APISetAddressSettings_cmd_string(self: APISetAddressSettings) -> str: + return '/_address_settings ' + str(self['userId']) + ' ' + json.dumps(self['settings']) + +APISetAddressSettings_Response = CR.UserContactLinkUpdated | CR.ChatCmdError + + +# Message commands +# Commands to send, update, delete, moderate messages and set message reactions + +# Send messages. +# Network usage: background. +class APISendMessages(TypedDict): + sendRef: "T.ChatRef" + liveMessage: bool + ttl: NotRequired[int] # int + composedMessages: list["T.ComposedMessage"] # non-empty + + +def APISendMessages_cmd_string(self: APISendMessages) -> str: + return '/_send ' + T.ChatRef_cmd_string(self['sendRef']) + (' live=on' if self['liveMessage'] else '') + ((' ttl=' + str(self.get('ttl'))) if self.get('ttl') is not None else '') + ' json ' + json.dumps(self['composedMessages']) + +APISendMessages_Response = CR.NewChatItems | CR.ChatCmdError + + +# Update message. +# Network usage: background. +class APIUpdateChatItem(TypedDict): + chatRef: "T.ChatRef" + chatItemId: int # int64 + liveMessage: bool + updatedMessage: "T.UpdatedMessage" + + +def APIUpdateChatItem_cmd_string(self: APIUpdateChatItem) -> str: + return '/_update item ' + T.ChatRef_cmd_string(self['chatRef']) + ' ' + str(self['chatItemId']) + (' live=on' if self['liveMessage'] else '') + ' json ' + json.dumps(self['updatedMessage']) + +APIUpdateChatItem_Response = CR.ChatItemUpdated | CR.ChatItemNotChanged | CR.ChatCmdError + + +# Delete message. +# Network usage: background. +class APIDeleteChatItem(TypedDict): + chatRef: "T.ChatRef" + chatItemIds: list[int] # int64, non-empty + deleteMode: "T.CIDeleteMode" + + +def APIDeleteChatItem_cmd_string(self: APIDeleteChatItem) -> str: + return '/_delete item ' + T.ChatRef_cmd_string(self['chatRef']) + ' ' + ','.join(map(str, self['chatItemIds'])) + ' ' + str(self['deleteMode']) + +APIDeleteChatItem_Response = CR.ChatItemsDeleted | CR.ChatCmdError + + +# Moderate message. Requires Moderator role (and higher than message author's). +# Network usage: background. +class APIDeleteMemberChatItem(TypedDict): + groupId: int # int64 + chatItemIds: list[int] # int64, non-empty + + +def APIDeleteMemberChatItem_cmd_string(self: APIDeleteMemberChatItem) -> str: + return '/_delete member item #' + str(self['groupId']) + ' ' + ','.join(map(str, self['chatItemIds'])) + +APIDeleteMemberChatItem_Response = CR.ChatItemsDeleted | CR.ChatCmdError + + +# Add/remove message reaction. +# Network usage: background. +class APIChatItemReaction(TypedDict): + chatRef: "T.ChatRef" + chatItemId: int # int64 + add: bool + reaction: "T.MsgReaction" + + +def APIChatItemReaction_cmd_string(self: APIChatItemReaction) -> str: + return '/_reaction ' + T.ChatRef_cmd_string(self['chatRef']) + ' ' + str(self['chatItemId']) + ' ' + ('on' if self['add'] else 'off') + ' ' + json.dumps(self['reaction']) + +APIChatItemReaction_Response = CR.ChatItemReaction | CR.ChatCmdError + + +# File commands +# Commands to receive and to cancel files. Files are sent as part of the message, there are no separate commands to send files. + +# Receive file. +# Network usage: no. +class ReceiveFile(TypedDict): + fileId: int # int64 + userApprovedRelays: bool + storeEncrypted: NotRequired[bool] + fileInline: NotRequired[bool] + filePath: NotRequired[str] + + +def ReceiveFile_cmd_string(self: ReceiveFile) -> str: + return '/freceive ' + str(self['fileId']) + (' approved_relays=on' if self['userApprovedRelays'] else '') + ((' encrypt=' + ('on' if self.get('storeEncrypted') else 'off')) if self.get('storeEncrypted') is not None else '') + ((' inline=' + ('on' if self.get('fileInline') else 'off')) if self.get('fileInline') is not None else '') + ((' ' + self.get('filePath')) if self.get('filePath') is not None else '') + +ReceiveFile_Response = CR.RcvFileAccepted | CR.RcvFileAcceptedSndCancelled | CR.ChatCmdError + + +# Cancel file. +# Network usage: background. +class CancelFile(TypedDict): + fileId: int # int64 + + +def CancelFile_cmd_string(self: CancelFile) -> str: + return '/fcancel ' + str(self['fileId']) + +CancelFile_Response = CR.SndFileCancelled | CR.RcvFileCancelled | CR.ChatCmdError + + +# Group commands +# Commands to manage and moderate groups. These commands can be used with business chats as well - they are groups. E.g., a common scenario would be to add human agents to business chat with the customer who connected via business address. + +# Add contact to group. Requires bot to have Admin role. +# Network usage: interactive. +class APIAddMember(TypedDict): + groupId: int # int64 + contactId: int # int64 + memberRole: "T.GroupMemberRole" + + +def APIAddMember_cmd_string(self: APIAddMember) -> str: + return '/_add #' + str(self['groupId']) + ' ' + str(self['contactId']) + ' ' + str(self['memberRole']) + +APIAddMember_Response = CR.SentGroupInvitation | CR.ChatCmdError + + +# Join group. +# Network usage: interactive. +class APIJoinGroup(TypedDict): + groupId: int # int64 + + +def APIJoinGroup_cmd_string(self: APIJoinGroup) -> str: + return '/_join #' + str(self['groupId']) + +APIJoinGroup_Response = CR.UserAcceptedGroupSent | CR.ChatCmdError + + +# Accept group member. Requires Admin role. +# Network usage: background. +class APIAcceptMember(TypedDict): + groupId: int # int64 + groupMemberId: int # int64 + memberRole: "T.GroupMemberRole" + + +def APIAcceptMember_cmd_string(self: APIAcceptMember) -> str: + return '/_accept member #' + str(self['groupId']) + ' ' + str(self['groupMemberId']) + ' ' + str(self['memberRole']) + +APIAcceptMember_Response = CR.MemberAccepted | CR.ChatCmdError + + +# Set members role. Requires Admin role. +# Network usage: background. +class APIMembersRole(TypedDict): + groupId: int # int64 + groupMemberIds: list[int] # int64, non-empty + memberRole: "T.GroupMemberRole" + + +def APIMembersRole_cmd_string(self: APIMembersRole) -> str: + return '/_member role #' + str(self['groupId']) + ' ' + ','.join(map(str, self['groupMemberIds'])) + ' ' + str(self['memberRole']) + +APIMembersRole_Response = CR.MembersRoleUser | CR.ChatCmdError + + +# Block members. Requires Moderator role. +# Network usage: background. +class APIBlockMembersForAll(TypedDict): + groupId: int # int64 + groupMemberIds: list[int] # int64, non-empty + blocked: bool + + +def APIBlockMembersForAll_cmd_string(self: APIBlockMembersForAll) -> str: + return '/_block #' + str(self['groupId']) + ' ' + ','.join(map(str, self['groupMemberIds'])) + ' blocked=' + ('on' if self['blocked'] else 'off') + +APIBlockMembersForAll_Response = CR.MembersBlockedForAllUser | CR.ChatCmdError + + +# Remove members. Requires Admin role. +# Network usage: background. +class APIRemoveMembers(TypedDict): + groupId: int # int64 + groupMemberIds: list[int] # int64, non-empty + withMessages: bool + + +def APIRemoveMembers_cmd_string(self: APIRemoveMembers) -> str: + return '/_remove #' + str(self['groupId']) + ' ' + ','.join(map(str, self['groupMemberIds'])) + (' messages=on' if self['withMessages'] else '') + +APIRemoveMembers_Response = CR.UserDeletedMembers | CR.ChatCmdError + + +# Leave group. +# Network usage: background. +class APILeaveGroup(TypedDict): + groupId: int # int64 + + +def APILeaveGroup_cmd_string(self: APILeaveGroup) -> str: + return '/_leave #' + str(self['groupId']) + +APILeaveGroup_Response = CR.LeftMemberUser | CR.ChatCmdError + + +# Get group members. +# Network usage: no. +class APIListMembers(TypedDict): + groupId: int # int64 + + +def APIListMembers_cmd_string(self: APIListMembers) -> str: + return '/_members #' + str(self['groupId']) + +APIListMembers_Response = CR.GroupMembers | CR.ChatCmdError + + +# Create group. +# Network usage: no. +class APINewGroup(TypedDict): + userId: int # int64 + incognito: bool + groupProfile: "T.GroupProfile" + + +def APINewGroup_cmd_string(self: APINewGroup) -> str: + return '/_group ' + str(self['userId']) + (' incognito=on' if self['incognito'] else '') + ' ' + json.dumps(self['groupProfile']) + +APINewGroup_Response = CR.GroupCreated | CR.ChatCmdError + + +# Create public group. +# Network usage: interactive. +class APINewPublicGroup(TypedDict): + userId: int # int64 + incognito: bool + relayIds: list[int] # int64, non-empty + groupProfile: "T.GroupProfile" + + +def APINewPublicGroup_cmd_string(self: APINewPublicGroup) -> str: + return '/_public group ' + str(self['userId']) + (' incognito=on' if self['incognito'] else '') + ' ' + ','.join(map(str, self['relayIds'])) + ' ' + json.dumps(self['groupProfile']) + +APINewPublicGroup_Response = CR.PublicGroupCreated | CR.PublicGroupCreationFailed | CR.ChatCmdError + + +# Get group relays. +# Network usage: no. +class APIGetGroupRelays(TypedDict): + groupId: int # int64 + + +def APIGetGroupRelays_cmd_string(self: APIGetGroupRelays) -> str: + return '/_get relays #' + str(self['groupId']) + +APIGetGroupRelays_Response = CR.GroupRelays | CR.ChatCmdError + + +# Add relays to group. +# Network usage: interactive. +class APIAddGroupRelays(TypedDict): + groupId: int # int64 + relayIds: list[int] # int64, non-empty + + +def APIAddGroupRelays_cmd_string(self: APIAddGroupRelays) -> str: + return '/_add relays #' + str(self['groupId']) + ' ' + ','.join(map(str, self['relayIds'])) + +APIAddGroupRelays_Response = CR.GroupRelaysAdded | CR.GroupRelaysAddFailed | CR.ChatCmdError + + +# Update group profile. +# Network usage: background. +class APIUpdateGroupProfile(TypedDict): + groupId: int # int64 + groupProfile: "T.GroupProfile" + + +def APIUpdateGroupProfile_cmd_string(self: APIUpdateGroupProfile) -> str: + return '/_group_profile #' + str(self['groupId']) + ' ' + json.dumps(self['groupProfile']) + +APIUpdateGroupProfile_Response = CR.GroupUpdated | CR.ChatCmdError + + +# Group link commands +# These commands can be used by bots that manage multiple public groups + +# Create group link. +# Network usage: interactive. +class APICreateGroupLink(TypedDict): + groupId: int # int64 + memberRole: "T.GroupMemberRole" + + +def APICreateGroupLink_cmd_string(self: APICreateGroupLink) -> str: + return '/_create link #' + str(self['groupId']) + ' ' + str(self['memberRole']) + +APICreateGroupLink_Response = CR.GroupLinkCreated | CR.ChatCmdError + + +# Set member role for group link. +# Network usage: no. +class APIGroupLinkMemberRole(TypedDict): + groupId: int # int64 + memberRole: "T.GroupMemberRole" + + +def APIGroupLinkMemberRole_cmd_string(self: APIGroupLinkMemberRole) -> str: + return '/_set link role #' + str(self['groupId']) + ' ' + str(self['memberRole']) + +APIGroupLinkMemberRole_Response = CR.GroupLink | CR.ChatCmdError + + +# Delete group link. +# Network usage: background. +class APIDeleteGroupLink(TypedDict): + groupId: int # int64 + + +def APIDeleteGroupLink_cmd_string(self: APIDeleteGroupLink) -> str: + return '/_delete link #' + str(self['groupId']) + +APIDeleteGroupLink_Response = CR.GroupLinkDeleted | CR.ChatCmdError + + +# Get group link. +# Network usage: no. +class APIGetGroupLink(TypedDict): + groupId: int # int64 + + +def APIGetGroupLink_cmd_string(self: APIGetGroupLink) -> str: + return '/_get link #' + str(self['groupId']) + +APIGetGroupLink_Response = CR.GroupLink | CR.ChatCmdError + + +# Connection commands +# These commands may be used to create connections. Most bots do not need to use them - bot users will connect via bot address with auto-accept enabled. + +# Create 1-time invitation link. +# Network usage: interactive. +class APIAddContact(TypedDict): + userId: int # int64 + incognito: bool + + +def APIAddContact_cmd_string(self: APIAddContact) -> str: + return '/_connect ' + str(self['userId']) + (' incognito=on' if self['incognito'] else '') + +APIAddContact_Response = CR.Invitation | CR.ChatCmdError + + +# Determine SimpleX link type and if the bot is already connected via this link. +# Network usage: interactive. +class APIConnectPlan(TypedDict): + userId: int # int64 + connectionLink: NotRequired[str] + resolveKnown: bool + linkOwnerSig: NotRequired["T.LinkOwnerSig"] + + +def APIConnectPlan_cmd_string(self: APIConnectPlan) -> str: + return '/_connect plan ' + str(self['userId']) + ' ' + self.get('connectionLink') + +APIConnectPlan_Response = CR.ConnectionPlan | CR.ChatCmdError + + +# Connect via prepared SimpleX link. The link can be 1-time invitation link, contact address or group link. +# Network usage: interactive. +class APIConnect(TypedDict): + userId: int # int64 + incognito: bool + preparedLink_: NotRequired["T.CreatedConnLink"] + + +def APIConnect_cmd_string(self: APIConnect) -> str: + return '/_connect ' + str(self['userId']) + ((' ' + T.CreatedConnLink_cmd_string(self.get('preparedLink_'))) if self.get('preparedLink_') is not None else '') + +APIConnect_Response = CR.SentConfirmation | CR.ContactAlreadyExists | CR.SentInvitation | CR.ChatCmdError + + +# Connect via SimpleX link as string in the active user profile. +# Network usage: interactive. +class Connect(TypedDict): + incognito: bool + connLink_: NotRequired[str] + + +def Connect_cmd_string(self: Connect) -> str: + return '/connect' + ((' ' + self.get('connLink_')) if self.get('connLink_') is not None else '') + +Connect_Response = CR.SentConfirmation | CR.ContactAlreadyExists | CR.SentInvitation | CR.ChatCmdError + + +# Accept contact request. +# Network usage: interactive. +class APIAcceptContact(TypedDict): + contactReqId: int # int64 + + +def APIAcceptContact_cmd_string(self: APIAcceptContact) -> str: + return '/_accept ' + str(self['contactReqId']) + +APIAcceptContact_Response = CR.AcceptingContactRequest | CR.ChatCmdError + + +# Reject contact request. The user who sent the request is **not notified**. +# Network usage: no. +class APIRejectContact(TypedDict): + contactReqId: int # int64 + + +def APIRejectContact_cmd_string(self: APIRejectContact) -> str: + return '/_reject ' + str(self['contactReqId']) + +APIRejectContact_Response = CR.ContactRequestRejected | CR.ChatCmdError + + +# Chat commands +# Commands to list and delete conversations. + +# Get contacts. +# Network usage: no. +class APIListContacts(TypedDict): + userId: int # int64 + + +def APIListContacts_cmd_string(self: APIListContacts) -> str: + return '/_contacts ' + str(self['userId']) + +APIListContacts_Response = CR.ContactsList | CR.ChatCmdError + + +# Get groups. +# Network usage: no. +class APIListGroups(TypedDict): + userId: int # int64 + contactId_: NotRequired[int] # int64 + search: NotRequired[str] + + +def APIListGroups_cmd_string(self: APIListGroups) -> str: + return '/_groups ' + str(self['userId']) + ((' @' + str(self.get('contactId_'))) if self.get('contactId_') is not None else '') + ((' ' + self.get('search')) if self.get('search') is not None else '') + +APIListGroups_Response = CR.GroupsList | CR.ChatCmdError + + +# Get chat previews. Supports time-based pagination — use this instead of APIListContacts / APIListGroups when scanning at scale (those load every record into memory and fail on large databases). +# Network usage: no. +class APIGetChats(TypedDict): + userId: int # int64 + pendingConnections: bool + pagination: "T.PaginationByTime" + query: "T.ChatListQuery" + + +def APIGetChats_cmd_string(self: APIGetChats) -> str: + return '/_get chats ' + str(self['userId']) + (' pcc=on' if self['pendingConnections'] else '') + ' ' + T.PaginationByTime_cmd_string(self['pagination']) + ' ' + json.dumps(self['query']) + +APIGetChats_Response = CR.ApiChats | CR.ChatCmdError + + +# Delete chat. +# Network usage: background. +class APIDeleteChat(TypedDict): + chatRef: "T.ChatRef" + chatDeleteMode: "T.ChatDeleteMode" + + +def APIDeleteChat_cmd_string(self: APIDeleteChat) -> str: + return '/_delete ' + T.ChatRef_cmd_string(self['chatRef']) + ' ' + T.ChatDeleteMode_cmd_string(self['chatDeleteMode']) + +APIDeleteChat_Response = CR.ContactDeleted | CR.ContactConnectionDeleted | CR.GroupDeletedUser | CR.ChatCmdError + + +# Set group custom data. +# Network usage: no. +class APISetGroupCustomData(TypedDict): + groupId: int # int64 + customData: NotRequired[dict[str, object]] + + +def APISetGroupCustomData_cmd_string(self: APISetGroupCustomData) -> str: + return '/_set custom #' + str(self['groupId']) + ((' ' + json.dumps(self.get('customData'))) if self.get('customData') is not None else '') + +APISetGroupCustomData_Response = CR.CmdOk | CR.ChatCmdError + + +# Set contact custom data. +# Network usage: no. +class APISetContactCustomData(TypedDict): + contactId: int # int64 + customData: NotRequired[dict[str, object]] + + +def APISetContactCustomData_cmd_string(self: APISetContactCustomData) -> str: + return '/_set custom @' + str(self['contactId']) + ((' ' + json.dumps(self.get('customData'))) if self.get('customData') is not None else '') + +APISetContactCustomData_Response = CR.CmdOk | CR.ChatCmdError + + +# Set auto-accept member contacts. +# Network usage: no. +class APISetUserAutoAcceptMemberContacts(TypedDict): + userId: int # int64 + onOff: bool + + +def APISetUserAutoAcceptMemberContacts_cmd_string(self: APISetUserAutoAcceptMemberContacts) -> str: + return '/_set accept member contacts ' + str(self['userId']) + ' ' + ('on' if self['onOff'] else 'off') + +APISetUserAutoAcceptMemberContacts_Response = CR.CmdOk | CR.ChatCmdError + + +# User profile commands +# Most bots don't need to use these commands, as bot profile can be configured manually via CLI or desktop client. These commands can be used by bots that need to manage multiple user profiles (e.g., the profiles of support agents). + +# Get active user profile. +# Network usage: no. +class ShowActiveUser(TypedDict): + pass + + +def ShowActiveUser_cmd_string(self: ShowActiveUser) -> str: + return '/user' + +ShowActiveUser_Response = CR.ActiveUser | CR.ChatCmdError + + +# Create new user profile. +# Network usage: no. +class CreateActiveUser(TypedDict): + newUser: "T.NewUser" + + +def CreateActiveUser_cmd_string(self: CreateActiveUser) -> str: + return '/_create user ' + json.dumps(self['newUser']) + +CreateActiveUser_Response = CR.ActiveUser | CR.ChatCmdError + + +# Get all user profiles. +# Network usage: no. +class ListUsers(TypedDict): + pass + + +def ListUsers_cmd_string(self: ListUsers) -> str: + return '/users' + +ListUsers_Response = CR.UsersList | CR.ChatCmdError + + +# Set active user profile. +# Network usage: no. +class APISetActiveUser(TypedDict): + userId: int # int64 + viewPwd: NotRequired[str] + + +def APISetActiveUser_cmd_string(self: APISetActiveUser) -> str: + return '/_user ' + str(self['userId']) + ((' ' + json.dumps(self.get('viewPwd'))) if self.get('viewPwd') is not None else '') + +APISetActiveUser_Response = CR.ActiveUser | CR.ChatCmdError + + +# Delete user profile. +# Network usage: background. +class APIDeleteUser(TypedDict): + userId: int # int64 + delSMPQueues: bool + viewPwd: NotRequired[str] + + +def APIDeleteUser_cmd_string(self: APIDeleteUser) -> str: + return '/_delete user ' + str(self['userId']) + ' del_smp=' + ('on' if self['delSMPQueues'] else 'off') + ((' ' + json.dumps(self.get('viewPwd'))) if self.get('viewPwd') is not None else '') + +APIDeleteUser_Response = CR.CmdOk | CR.ChatCmdError + + +# Update user profile. +# Network usage: background. +class APIUpdateProfile(TypedDict): + userId: int # int64 + profile: "T.Profile" + + +def APIUpdateProfile_cmd_string(self: APIUpdateProfile) -> str: + return '/_profile ' + str(self['userId']) + ' ' + json.dumps(self['profile']) + +APIUpdateProfile_Response = CR.UserProfileUpdated | CR.UserProfileNoChange | CR.ChatCmdError + + +# Configure chat preference overrides for the contact. +# Network usage: background. +class APISetContactPrefs(TypedDict): + contactId: int # int64 + preferences: "T.Preferences" + + +def APISetContactPrefs_cmd_string(self: APISetContactPrefs) -> str: + return '/_set prefs @' + str(self['contactId']) + ' ' + json.dumps(self['preferences']) + +APISetContactPrefs_Response = CR.ContactPrefsUpdated | CR.ChatCmdError + + +# Chat management +# These commands should not be used with CLI-based bots + +# Start chat controller. +# Network usage: no. +class StartChat(TypedDict): + mainApp: bool + enableSndFiles: bool + + +def StartChat_cmd_string(self: StartChat) -> str: + return '/_start' + +StartChat_Response = CR.ChatStarted | CR.ChatRunning + + +# Stop chat controller. +# Network usage: no. +class APIStopChat(TypedDict): + pass + + +def APIStopChat_cmd_string(self: APIStopChat) -> str: + return '/_stop' + +APIStopChat_Response = CR.ChatStopped + diff --git a/packages/simplex-chat-python/src/simplex_chat/types/_events.py b/packages/simplex-chat-python/src/simplex_chat/types/_events.py new file mode 100644 index 0000000000..77484fbf3f --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/types/_events.py @@ -0,0 +1,379 @@ +# API Events +# This file is generated automatically. +from __future__ import annotations +from typing import Literal, NotRequired, TypedDict +from . import _types as T + +class ContactConnected(TypedDict): + type: Literal["contactConnected"] + user: "T.User" + contact: "T.Contact" + userCustomProfile: NotRequired["T.Profile"] + +class ContactUpdated(TypedDict): + type: Literal["contactUpdated"] + user: "T.User" + fromContact: "T.Contact" + toContact: "T.Contact" + +class ContactDeletedByContact(TypedDict): + type: Literal["contactDeletedByContact"] + user: "T.User" + contact: "T.Contact" + +class ReceivedContactRequest(TypedDict): + type: Literal["receivedContactRequest"] + user: "T.User" + contactRequest: "T.UserContactRequest" + chat_: NotRequired["T.AChat"] + +class NewMemberContactReceivedInv(TypedDict): + type: Literal["newMemberContactReceivedInv"] + user: "T.User" + contact: "T.Contact" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + +class ContactSndReady(TypedDict): + type: Literal["contactSndReady"] + user: "T.User" + contact: "T.Contact" + +class NewChatItems(TypedDict): + type: Literal["newChatItems"] + user: "T.User" + chatItems: list["T.AChatItem"] + +class ChatItemReaction(TypedDict): + type: Literal["chatItemReaction"] + user: "T.User" + added: bool + reaction: "T.ACIReaction" + +class ChatItemsDeleted(TypedDict): + type: Literal["chatItemsDeleted"] + user: "T.User" + chatItemDeletions: list["T.ChatItemDeletion"] + byUser: bool + timed: bool + +class ChatItemUpdated(TypedDict): + type: Literal["chatItemUpdated"] + user: "T.User" + chatItem: "T.AChatItem" + +class GroupChatItemsDeleted(TypedDict): + type: Literal["groupChatItemsDeleted"] + user: "T.User" + groupInfo: "T.GroupInfo" + chatItemIDs: list[int] # int64 + byUser: bool + member_: NotRequired["T.GroupMember"] + +class ChatItemsStatusesUpdated(TypedDict): + type: Literal["chatItemsStatusesUpdated"] + user: "T.User" + chatItems: list["T.AChatItem"] + +class ReceivedGroupInvitation(TypedDict): + type: Literal["receivedGroupInvitation"] + user: "T.User" + groupInfo: "T.GroupInfo" + contact: "T.Contact" + fromMemberRole: "T.GroupMemberRole" + memberRole: "T.GroupMemberRole" + +class UserJoinedGroup(TypedDict): + type: Literal["userJoinedGroup"] + user: "T.User" + groupInfo: "T.GroupInfo" + hostMember: "T.GroupMember" + +class GroupUpdated(TypedDict): + type: Literal["groupUpdated"] + user: "T.User" + fromGroup: "T.GroupInfo" + toGroup: "T.GroupInfo" + member_: NotRequired["T.GroupMember"] + msgSigned: NotRequired["T.MsgSigStatus"] + +class JoinedGroupMember(TypedDict): + type: Literal["joinedGroupMember"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + +class MemberRole(TypedDict): + type: Literal["memberRole"] + user: "T.User" + groupInfo: "T.GroupInfo" + byMember: "T.GroupMember" + member: "T.GroupMember" + fromRole: "T.GroupMemberRole" + toRole: "T.GroupMemberRole" + msgSigned: NotRequired["T.MsgSigStatus"] + +class DeletedMember(TypedDict): + type: Literal["deletedMember"] + user: "T.User" + groupInfo: "T.GroupInfo" + byMember: "T.GroupMember" + deletedMember: "T.GroupMember" + withMessages: bool + msgSigned: NotRequired["T.MsgSigStatus"] + +class LeftMember(TypedDict): + type: Literal["leftMember"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + msgSigned: NotRequired["T.MsgSigStatus"] + +class DeletedMemberUser(TypedDict): + type: Literal["deletedMemberUser"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + withMessages: bool + msgSigned: NotRequired["T.MsgSigStatus"] + +class GroupDeleted(TypedDict): + type: Literal["groupDeleted"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + msgSigned: NotRequired["T.MsgSigStatus"] + +class ConnectedToGroupMember(TypedDict): + type: Literal["connectedToGroupMember"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + memberContact: NotRequired["T.Contact"] + +class MemberAcceptedByOther(TypedDict): + type: Literal["memberAcceptedByOther"] + user: "T.User" + groupInfo: "T.GroupInfo" + acceptingMember: "T.GroupMember" + member: "T.GroupMember" + +class MemberBlockedForAll(TypedDict): + type: Literal["memberBlockedForAll"] + user: "T.User" + groupInfo: "T.GroupInfo" + byMember: "T.GroupMember" + member: "T.GroupMember" + blocked: bool + msgSigned: NotRequired["T.MsgSigStatus"] + +class GroupMemberUpdated(TypedDict): + type: Literal["groupMemberUpdated"] + user: "T.User" + groupInfo: "T.GroupInfo" + fromMember: "T.GroupMember" + toMember: "T.GroupMember" + +class GroupLinkDataUpdated(TypedDict): + type: Literal["groupLinkDataUpdated"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupLink: "T.GroupLink" + groupRelays: list["T.GroupRelay"] + relaysChanged: bool + +class GroupRelayUpdated(TypedDict): + type: Literal["groupRelayUpdated"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + groupRelay: "T.GroupRelay" + +class RcvFileDescrReady(TypedDict): + type: Literal["rcvFileDescrReady"] + user: "T.User" + chatItem: "T.AChatItem" + rcvFileTransfer: "T.RcvFileTransfer" + rcvFileDescr: "T.RcvFileDescr" + +class RcvFileComplete(TypedDict): + type: Literal["rcvFileComplete"] + user: "T.User" + chatItem: "T.AChatItem" + +class SndFileCompleteXFTP(TypedDict): + type: Literal["sndFileCompleteXFTP"] + user: "T.User" + chatItem: "T.AChatItem" + fileTransferMeta: "T.FileTransferMeta" + +class RcvFileStart(TypedDict): + type: Literal["rcvFileStart"] + user: "T.User" + chatItem: "T.AChatItem" + +class RcvFileSndCancelled(TypedDict): + type: Literal["rcvFileSndCancelled"] + user: "T.User" + chatItem: "T.AChatItem" + rcvFileTransfer: "T.RcvFileTransfer" + +class RcvFileAccepted(TypedDict): + type: Literal["rcvFileAccepted"] + user: "T.User" + chatItem: "T.AChatItem" + +class RcvFileError(TypedDict): + type: Literal["rcvFileError"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + agentError: "T.AgentErrorType" + rcvFileTransfer: "T.RcvFileTransfer" + +class RcvFileWarning(TypedDict): + type: Literal["rcvFileWarning"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + agentError: "T.AgentErrorType" + rcvFileTransfer: "T.RcvFileTransfer" + +class SndFileError(TypedDict): + type: Literal["sndFileError"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + fileTransferMeta: "T.FileTransferMeta" + errorMessage: str + +class SndFileWarning(TypedDict): + type: Literal["sndFileWarning"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + fileTransferMeta: "T.FileTransferMeta" + errorMessage: str + +class AcceptingContactRequest(TypedDict): + type: Literal["acceptingContactRequest"] + user: "T.User" + contact: "T.Contact" + +class AcceptingBusinessRequest(TypedDict): + type: Literal["acceptingBusinessRequest"] + user: "T.User" + groupInfo: "T.GroupInfo" + +class ContactConnecting(TypedDict): + type: Literal["contactConnecting"] + user: "T.User" + contact: "T.Contact" + +class BusinessLinkConnecting(TypedDict): + type: Literal["businessLinkConnecting"] + user: "T.User" + groupInfo: "T.GroupInfo" + hostMember: "T.GroupMember" + fromContact: "T.Contact" + +class JoinedGroupMemberConnecting(TypedDict): + type: Literal["joinedGroupMemberConnecting"] + user: "T.User" + groupInfo: "T.GroupInfo" + hostMember: "T.GroupMember" + member: "T.GroupMember" + +class SentGroupInvitation(TypedDict): + type: Literal["sentGroupInvitation"] + user: "T.User" + groupInfo: "T.GroupInfo" + contact: "T.Contact" + member: "T.GroupMember" + +class GroupLinkConnecting(TypedDict): + type: Literal["groupLinkConnecting"] + user: "T.User" + groupInfo: "T.GroupInfo" + hostMember: "T.GroupMember" + +class HostConnected(TypedDict): + type: Literal["hostConnected"] + protocol: str + transportHost: str + +class HostDisconnected(TypedDict): + type: Literal["hostDisconnected"] + protocol: str + transportHost: str + +class SubscriptionStatus(TypedDict): + type: Literal["subscriptionStatus"] + server: str + subscriptionStatus: "T.SubscriptionStatus" + connections: list[str] + +class MessageError(TypedDict): + type: Literal["messageError"] + user: "T.User" + severity: str + errorMessage: str + +class ChatError(TypedDict): + type: Literal["chatError"] + chatError: "T.ChatError" + +class ChatErrors(TypedDict): + type: Literal["chatErrors"] + chatErrors: list["T.ChatError"] + +ChatEvent = ( + ContactConnected + | ContactUpdated + | ContactDeletedByContact + | ReceivedContactRequest + | NewMemberContactReceivedInv + | ContactSndReady + | NewChatItems + | ChatItemReaction + | ChatItemsDeleted + | ChatItemUpdated + | GroupChatItemsDeleted + | ChatItemsStatusesUpdated + | ReceivedGroupInvitation + | UserJoinedGroup + | GroupUpdated + | JoinedGroupMember + | MemberRole + | DeletedMember + | LeftMember + | DeletedMemberUser + | GroupDeleted + | ConnectedToGroupMember + | MemberAcceptedByOther + | MemberBlockedForAll + | GroupMemberUpdated + | GroupLinkDataUpdated + | GroupRelayUpdated + | RcvFileDescrReady + | RcvFileComplete + | SndFileCompleteXFTP + | RcvFileStart + | RcvFileSndCancelled + | RcvFileAccepted + | RcvFileError + | RcvFileWarning + | SndFileError + | SndFileWarning + | AcceptingContactRequest + | AcceptingBusinessRequest + | ContactConnecting + | BusinessLinkConnecting + | JoinedGroupMemberConnecting + | SentGroupInvitation + | GroupLinkConnecting + | HostConnected + | HostDisconnected + | SubscriptionStatus + | MessageError + | ChatError + | ChatErrors +) + +ChatEvent_Tag = Literal["contactConnected", "contactUpdated", "contactDeletedByContact", "receivedContactRequest", "newMemberContactReceivedInv", "contactSndReady", "newChatItems", "chatItemReaction", "chatItemsDeleted", "chatItemUpdated", "groupChatItemsDeleted", "chatItemsStatusesUpdated", "receivedGroupInvitation", "userJoinedGroup", "groupUpdated", "joinedGroupMember", "memberRole", "deletedMember", "leftMember", "deletedMemberUser", "groupDeleted", "connectedToGroupMember", "memberAcceptedByOther", "memberBlockedForAll", "groupMemberUpdated", "groupLinkDataUpdated", "groupRelayUpdated", "rcvFileDescrReady", "rcvFileComplete", "sndFileCompleteXFTP", "rcvFileStart", "rcvFileSndCancelled", "rcvFileAccepted", "rcvFileError", "rcvFileWarning", "sndFileError", "sndFileWarning", "acceptingContactRequest", "acceptingBusinessRequest", "contactConnecting", "businessLinkConnecting", "joinedGroupMemberConnecting", "sentGroupInvitation", "groupLinkConnecting", "hostConnected", "hostDisconnected", "subscriptionStatus", "messageError", "chatError", "chatErrors"] diff --git a/packages/simplex-chat-python/src/simplex_chat/types/_responses.py b/packages/simplex-chat-python/src/simplex_chat/types/_responses.py new file mode 100644 index 0000000000..84d0f1c79f --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/types/_responses.py @@ -0,0 +1,360 @@ +# API Responses +# This file is generated automatically. +from __future__ import annotations +from typing import Literal, NotRequired, TypedDict +from . import _types as T + +class AcceptingContactRequest(TypedDict): + type: Literal["acceptingContactRequest"] + user: "T.User" + contact: "T.Contact" + +class ActiveUser(TypedDict): + type: Literal["activeUser"] + user: "T.User" + +class ChatItemNotChanged(TypedDict): + type: Literal["chatItemNotChanged"] + user: "T.User" + chatItem: "T.AChatItem" + +class ChatItemReaction(TypedDict): + type: Literal["chatItemReaction"] + user: "T.User" + added: bool + reaction: "T.ACIReaction" + +class ChatItemUpdated(TypedDict): + type: Literal["chatItemUpdated"] + user: "T.User" + chatItem: "T.AChatItem" + +class ChatItemsDeleted(TypedDict): + type: Literal["chatItemsDeleted"] + user: "T.User" + chatItemDeletions: list["T.ChatItemDeletion"] + byUser: bool + timed: bool + +class ChatRunning(TypedDict): + type: Literal["chatRunning"] + +class ChatStarted(TypedDict): + type: Literal["chatStarted"] + +class ChatStopped(TypedDict): + type: Literal["chatStopped"] + +class CmdOk(TypedDict): + type: Literal["cmdOk"] + user_: NotRequired["T.User"] + +class ChatCmdError(TypedDict): + type: Literal["chatCmdError"] + chatError: "T.ChatError" + +class ConnectionPlan(TypedDict): + type: Literal["connectionPlan"] + user: "T.User" + connLink: "T.CreatedConnLink" + connectionPlan: "T.ConnectionPlan" + +class ContactAlreadyExists(TypedDict): + type: Literal["contactAlreadyExists"] + user: "T.User" + contact: "T.Contact" + +class ContactConnectionDeleted(TypedDict): + type: Literal["contactConnectionDeleted"] + user: "T.User" + connection: "T.PendingContactConnection" + +class ContactDeleted(TypedDict): + type: Literal["contactDeleted"] + user: "T.User" + contact: "T.Contact" + +class ContactPrefsUpdated(TypedDict): + type: Literal["contactPrefsUpdated"] + user: "T.User" + fromContact: "T.Contact" + toContact: "T.Contact" + +class ContactRequestRejected(TypedDict): + type: Literal["contactRequestRejected"] + user: "T.User" + contactRequest: "T.UserContactRequest" + contact_: NotRequired["T.Contact"] + +class ContactsList(TypedDict): + type: Literal["contactsList"] + user: "T.User" + contacts: list["T.Contact"] + +class GroupDeletedUser(TypedDict): + type: Literal["groupDeletedUser"] + user: "T.User" + groupInfo: "T.GroupInfo" + msgSigned: bool + +class GroupLink(TypedDict): + type: Literal["groupLink"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupLink: "T.GroupLink" + +class GroupLinkCreated(TypedDict): + type: Literal["groupLinkCreated"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupLink: "T.GroupLink" + +class GroupLinkDeleted(TypedDict): + type: Literal["groupLinkDeleted"] + user: "T.User" + groupInfo: "T.GroupInfo" + +class GroupCreated(TypedDict): + type: Literal["groupCreated"] + user: "T.User" + groupInfo: "T.GroupInfo" + +class PublicGroupCreated(TypedDict): + type: Literal["publicGroupCreated"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupLink: "T.GroupLink" + groupRelays: list["T.GroupRelay"] + +class PublicGroupCreationFailed(TypedDict): + type: Literal["publicGroupCreationFailed"] + user: "T.User" + addRelayResults: list["T.AddRelayResult"] + +class GroupRelays(TypedDict): + type: Literal["groupRelays"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupRelays: list["T.GroupRelay"] + +class GroupRelaysAdded(TypedDict): + type: Literal["groupRelaysAdded"] + user: "T.User" + groupInfo: "T.GroupInfo" + groupLink: "T.GroupLink" + groupRelays: list["T.GroupRelay"] + +class GroupRelaysAddFailed(TypedDict): + type: Literal["groupRelaysAddFailed"] + user: "T.User" + addRelayResults: list["T.AddRelayResult"] + +class GroupMembers(TypedDict): + type: Literal["groupMembers"] + user: "T.User" + group: "T.Group" + +class GroupUpdated(TypedDict): + type: Literal["groupUpdated"] + user: "T.User" + fromGroup: "T.GroupInfo" + toGroup: "T.GroupInfo" + member_: NotRequired["T.GroupMember"] + msgSigned: bool + +class GroupsList(TypedDict): + type: Literal["groupsList"] + user: "T.User" + groups: list["T.GroupInfo"] + +class Invitation(TypedDict): + type: Literal["invitation"] + user: "T.User" + connLinkInvitation: "T.CreatedConnLink" + connection: "T.PendingContactConnection" + +class LeftMemberUser(TypedDict): + type: Literal["leftMemberUser"] + user: "T.User" + groupInfo: "T.GroupInfo" + +class MemberAccepted(TypedDict): + type: Literal["memberAccepted"] + user: "T.User" + groupInfo: "T.GroupInfo" + member: "T.GroupMember" + +class MembersBlockedForAllUser(TypedDict): + type: Literal["membersBlockedForAllUser"] + user: "T.User" + groupInfo: "T.GroupInfo" + members: list["T.GroupMember"] + blocked: bool + msgSigned: bool + +class MembersRoleUser(TypedDict): + type: Literal["membersRoleUser"] + user: "T.User" + groupInfo: "T.GroupInfo" + members: list["T.GroupMember"] + toRole: "T.GroupMemberRole" + msgSigned: bool + +class NewChatItems(TypedDict): + type: Literal["newChatItems"] + user: "T.User" + chatItems: list["T.AChatItem"] + +class RcvFileAccepted(TypedDict): + type: Literal["rcvFileAccepted"] + user: "T.User" + chatItem: "T.AChatItem" + +class RcvFileAcceptedSndCancelled(TypedDict): + type: Literal["rcvFileAcceptedSndCancelled"] + user: "T.User" + rcvFileTransfer: "T.RcvFileTransfer" + +class RcvFileCancelled(TypedDict): + type: Literal["rcvFileCancelled"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + rcvFileTransfer: "T.RcvFileTransfer" + +class SentConfirmation(TypedDict): + type: Literal["sentConfirmation"] + user: "T.User" + connection: "T.PendingContactConnection" + customUserProfile: NotRequired["T.Profile"] + +class SentGroupInvitation(TypedDict): + type: Literal["sentGroupInvitation"] + user: "T.User" + groupInfo: "T.GroupInfo" + contact: "T.Contact" + member: "T.GroupMember" + +class SentInvitation(TypedDict): + type: Literal["sentInvitation"] + user: "T.User" + connection: "T.PendingContactConnection" + customUserProfile: NotRequired["T.Profile"] + +class SndFileCancelled(TypedDict): + type: Literal["sndFileCancelled"] + user: "T.User" + chatItem_: NotRequired["T.AChatItem"] + fileTransferMeta: "T.FileTransferMeta" + sndFileTransfers: list["T.SndFileTransfer"] + +class UserAcceptedGroupSent(TypedDict): + type: Literal["userAcceptedGroupSent"] + user: "T.User" + groupInfo: "T.GroupInfo" + hostContact: NotRequired["T.Contact"] + +class UserContactLink(TypedDict): + type: Literal["userContactLink"] + user: "T.User" + contactLink: "T.UserContactLink" + +class UserContactLinkCreated(TypedDict): + type: Literal["userContactLinkCreated"] + user: "T.User" + connLinkContact: "T.CreatedConnLink" + +class UserContactLinkDeleted(TypedDict): + type: Literal["userContactLinkDeleted"] + user: "T.User" + +class UserContactLinkUpdated(TypedDict): + type: Literal["userContactLinkUpdated"] + user: "T.User" + contactLink: "T.UserContactLink" + +class UserDeletedMembers(TypedDict): + type: Literal["userDeletedMembers"] + user: "T.User" + groupInfo: "T.GroupInfo" + members: list["T.GroupMember"] + withMessages: bool + msgSigned: bool + +class UserProfileUpdated(TypedDict): + type: Literal["userProfileUpdated"] + user: "T.User" + fromProfile: "T.Profile" + toProfile: "T.Profile" + updateSummary: "T.UserProfileUpdateSummary" + +class UserProfileNoChange(TypedDict): + type: Literal["userProfileNoChange"] + user: "T.User" + +class UsersList(TypedDict): + type: Literal["usersList"] + users: list["T.UserInfo"] + +class ApiChats(TypedDict): + type: Literal["apiChats"] + user: "T.User" + chats: list["T.AChat"] + +ChatResponse = ( + AcceptingContactRequest + | ActiveUser + | ChatItemNotChanged + | ChatItemReaction + | ChatItemUpdated + | ChatItemsDeleted + | ChatRunning + | ChatStarted + | ChatStopped + | CmdOk + | ChatCmdError + | ConnectionPlan + | ContactAlreadyExists + | ContactConnectionDeleted + | ContactDeleted + | ContactPrefsUpdated + | ContactRequestRejected + | ContactsList + | GroupDeletedUser + | GroupLink + | GroupLinkCreated + | GroupLinkDeleted + | GroupCreated + | PublicGroupCreated + | PublicGroupCreationFailed + | GroupRelays + | GroupRelaysAdded + | GroupRelaysAddFailed + | GroupMembers + | GroupUpdated + | GroupsList + | Invitation + | LeftMemberUser + | MemberAccepted + | MembersBlockedForAllUser + | MembersRoleUser + | NewChatItems + | RcvFileAccepted + | RcvFileAcceptedSndCancelled + | RcvFileCancelled + | SentConfirmation + | SentGroupInvitation + | SentInvitation + | SndFileCancelled + | UserAcceptedGroupSent + | UserContactLink + | UserContactLinkCreated + | UserContactLinkDeleted + | UserContactLinkUpdated + | UserDeletedMembers + | UserProfileUpdated + | UserProfileNoChange + | UsersList + | ApiChats +) + +ChatResponse_Tag = Literal["acceptingContactRequest", "activeUser", "chatItemNotChanged", "chatItemReaction", "chatItemUpdated", "chatItemsDeleted", "chatRunning", "chatStarted", "chatStopped", "cmdOk", "chatCmdError", "connectionPlan", "contactAlreadyExists", "contactConnectionDeleted", "contactDeleted", "contactPrefsUpdated", "contactRequestRejected", "contactsList", "groupDeletedUser", "groupLink", "groupLinkCreated", "groupLinkDeleted", "groupCreated", "publicGroupCreated", "publicGroupCreationFailed", "groupRelays", "groupRelaysAdded", "groupRelaysAddFailed", "groupMembers", "groupUpdated", "groupsList", "invitation", "leftMemberUser", "memberAccepted", "membersBlockedForAllUser", "membersRoleUser", "newChatItems", "rcvFileAccepted", "rcvFileAcceptedSndCancelled", "rcvFileCancelled", "sentConfirmation", "sentGroupInvitation", "sentInvitation", "sndFileCancelled", "userAcceptedGroupSent", "userContactLink", "userContactLinkCreated", "userContactLinkDeleted", "userContactLinkUpdated", "userDeletedMembers", "userProfileUpdated", "userProfileNoChange", "usersList", "apiChats"] diff --git a/packages/simplex-chat-python/src/simplex_chat/types/_types.py b/packages/simplex-chat-python/src/simplex_chat/types/_types.py new file mode 100644 index 0000000000..b5c448948a --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/types/_types.py @@ -0,0 +1,3506 @@ +# API Types +# This file is generated automatically. +from __future__ import annotations +from typing import Literal, NotRequired, TypedDict + +class ACIReaction(TypedDict): + chatInfo: "ChatInfo" + chatReaction: "CIReaction" + +class AChat(TypedDict): + chatInfo: "ChatInfo" + chatItems: list["ChatItem"] + chatStats: "ChatStats" + +class AChatItem(TypedDict): + chatInfo: "ChatInfo" + chatItem: "ChatItem" + +class AddRelayResult(TypedDict): + relay: "UserChatRelay" + relayError: NotRequired["ChatError"] + +class AddressSettings(TypedDict): + businessAddress: bool + autoAccept: NotRequired["AutoAccept"] + autoReply: NotRequired["MsgContent"] + +class AgentCryptoError_DECRYPT_AES(TypedDict): + type: Literal["DECRYPT_AES"] + +class AgentCryptoError_DECRYPT_CB(TypedDict): + type: Literal["DECRYPT_CB"] + +class AgentCryptoError_RATCHET_HEADER(TypedDict): + type: Literal["RATCHET_HEADER"] + +class AgentCryptoError_RATCHET_SYNC(TypedDict): + type: Literal["RATCHET_SYNC"] + +AgentCryptoError = ( + AgentCryptoError_DECRYPT_AES + | AgentCryptoError_DECRYPT_CB + | AgentCryptoError_RATCHET_HEADER + | AgentCryptoError_RATCHET_SYNC +) + +AgentCryptoError_Tag = Literal["DECRYPT_AES", "DECRYPT_CB", "RATCHET_HEADER", "RATCHET_SYNC"] + +class AgentErrorType_CMD(TypedDict): + type: Literal["CMD"] + cmdErr: "CommandErrorType" + errContext: str + +class AgentErrorType_CONN(TypedDict): + type: Literal["CONN"] + connErr: "ConnectionErrorType" + errContext: str + +class AgentErrorType_NO_USER(TypedDict): + type: Literal["NO_USER"] + +class AgentErrorType_SMP(TypedDict): + type: Literal["SMP"] + serverAddress: str + smpErr: "ErrorType" + +class AgentErrorType_NTF(TypedDict): + type: Literal["NTF"] + serverAddress: str + ntfErr: "ErrorType" + +class AgentErrorType_XFTP(TypedDict): + type: Literal["XFTP"] + serverAddress: str + xftpErr: "XFTPErrorType" + +class AgentErrorType_FILE(TypedDict): + type: Literal["FILE"] + fileErr: "FileErrorType" + +class AgentErrorType_PROXY(TypedDict): + type: Literal["PROXY"] + proxyServer: str + relayServer: str + proxyErr: "ProxyClientError" + +class AgentErrorType_RCP(TypedDict): + type: Literal["RCP"] + rcpErr: "RCErrorType" + +class AgentErrorType_BROKER(TypedDict): + type: Literal["BROKER"] + brokerAddress: str + brokerErr: "BrokerErrorType" + +class AgentErrorType_AGENT(TypedDict): + type: Literal["AGENT"] + agentErr: "SMPAgentError" + +class AgentErrorType_NOTICE(TypedDict): + type: Literal["NOTICE"] + server: str + preset: bool + expiresAt: NotRequired[str] # ISO-8601 timestamp + +class AgentErrorType_INTERNAL(TypedDict): + type: Literal["INTERNAL"] + internalErr: str + +class AgentErrorType_CRITICAL(TypedDict): + type: Literal["CRITICAL"] + offerRestart: bool + criticalErr: str + +class AgentErrorType_INACTIVE(TypedDict): + type: Literal["INACTIVE"] + +AgentErrorType = ( + AgentErrorType_CMD + | AgentErrorType_CONN + | AgentErrorType_NO_USER + | AgentErrorType_SMP + | AgentErrorType_NTF + | AgentErrorType_XFTP + | AgentErrorType_FILE + | AgentErrorType_PROXY + | AgentErrorType_RCP + | AgentErrorType_BROKER + | AgentErrorType_AGENT + | AgentErrorType_NOTICE + | AgentErrorType_INTERNAL + | AgentErrorType_CRITICAL + | AgentErrorType_INACTIVE +) + +AgentErrorType_Tag = Literal["CMD", "CONN", "NO_USER", "SMP", "NTF", "XFTP", "FILE", "PROXY", "RCP", "BROKER", "AGENT", "NOTICE", "INTERNAL", "CRITICAL", "INACTIVE"] + +class AutoAccept(TypedDict): + acceptIncognito: bool + +class BlockingInfo(TypedDict): + reason: "BlockingReason" + notice: NotRequired["ClientNotice"] + +BlockingReason = Literal["spam", "content"] + +class BrokerErrorType_RESPONSE(TypedDict): + type: Literal["RESPONSE"] + respErr: str + +class BrokerErrorType_UNEXPECTED(TypedDict): + type: Literal["UNEXPECTED"] + respErr: str + +class BrokerErrorType_NETWORK(TypedDict): + type: Literal["NETWORK"] + networkError: "NetworkError" + +class BrokerErrorType_HOST(TypedDict): + type: Literal["HOST"] + +class BrokerErrorType_NO_SERVICE(TypedDict): + type: Literal["NO_SERVICE"] + +class BrokerErrorType_TRANSPORT(TypedDict): + type: Literal["TRANSPORT"] + transportErr: "TransportError" + +class BrokerErrorType_TIMEOUT(TypedDict): + type: Literal["TIMEOUT"] + +BrokerErrorType = ( + BrokerErrorType_RESPONSE + | BrokerErrorType_UNEXPECTED + | BrokerErrorType_NETWORK + | BrokerErrorType_HOST + | BrokerErrorType_NO_SERVICE + | BrokerErrorType_TRANSPORT + | BrokerErrorType_TIMEOUT +) + +BrokerErrorType_Tag = Literal["RESPONSE", "UNEXPECTED", "NETWORK", "HOST", "NO_SERVICE", "TRANSPORT", "TIMEOUT"] + +class BusinessChatInfo(TypedDict): + chatType: "BusinessChatType" + businessId: str + customerId: str + +BusinessChatType = Literal["business", "customer"] + +CICallStatus = Literal["pending", "missed", "rejected", "accepted", "negotiated", "progress", "ended", "error"] + +class CIContent_sndMsgContent(TypedDict): + type: Literal["sndMsgContent"] + msgContent: "MsgContent" + +class CIContent_rcvMsgContent(TypedDict): + type: Literal["rcvMsgContent"] + msgContent: "MsgContent" + +class CIContent_sndDeleted(TypedDict): + type: Literal["sndDeleted"] + deleteMode: "CIDeleteMode" + +class CIContent_rcvDeleted(TypedDict): + type: Literal["rcvDeleted"] + deleteMode: "CIDeleteMode" + +class CIContent_sndCall(TypedDict): + type: Literal["sndCall"] + status: "CICallStatus" + duration: int # int + +class CIContent_rcvCall(TypedDict): + type: Literal["rcvCall"] + status: "CICallStatus" + duration: int # int + +class CIContent_rcvIntegrityError(TypedDict): + type: Literal["rcvIntegrityError"] + msgError: "MsgErrorType" + +class CIContent_rcvDecryptionError(TypedDict): + type: Literal["rcvDecryptionError"] + msgDecryptError: "MsgDecryptError" + msgCount: int # word32 + +class CIContent_rcvMsgError(TypedDict): + type: Literal["rcvMsgError"] + rcvMsgError: "RcvMsgError" + +class CIContent_rcvGroupInvitation(TypedDict): + type: Literal["rcvGroupInvitation"] + groupInvitation: "CIGroupInvitation" + memberRole: "GroupMemberRole" + +class CIContent_sndGroupInvitation(TypedDict): + type: Literal["sndGroupInvitation"] + groupInvitation: "CIGroupInvitation" + memberRole: "GroupMemberRole" + +class CIContent_rcvDirectEvent(TypedDict): + type: Literal["rcvDirectEvent"] + rcvDirectEvent: "RcvDirectEvent" + +class CIContent_rcvGroupEvent(TypedDict): + type: Literal["rcvGroupEvent"] + rcvGroupEvent: "RcvGroupEvent" + +class CIContent_sndGroupEvent(TypedDict): + type: Literal["sndGroupEvent"] + sndGroupEvent: "SndGroupEvent" + +class CIContent_rcvConnEvent(TypedDict): + type: Literal["rcvConnEvent"] + rcvConnEvent: "RcvConnEvent" + +class CIContent_sndConnEvent(TypedDict): + type: Literal["sndConnEvent"] + sndConnEvent: "SndConnEvent" + +class CIContent_rcvChatFeature(TypedDict): + type: Literal["rcvChatFeature"] + feature: "ChatFeature" + enabled: "PrefEnabled" + param: NotRequired[int] # int + +class CIContent_sndChatFeature(TypedDict): + type: Literal["sndChatFeature"] + feature: "ChatFeature" + enabled: "PrefEnabled" + param: NotRequired[int] # int + +class CIContent_rcvChatPreference(TypedDict): + type: Literal["rcvChatPreference"] + feature: "ChatFeature" + allowed: "FeatureAllowed" + param: NotRequired[int] # int + +class CIContent_sndChatPreference(TypedDict): + type: Literal["sndChatPreference"] + feature: "ChatFeature" + allowed: "FeatureAllowed" + param: NotRequired[int] # int + +class CIContent_rcvGroupFeature(TypedDict): + type: Literal["rcvGroupFeature"] + groupFeature: "GroupFeature" + preference: "GroupPreference" + param: NotRequired[int] # int + memberRole_: NotRequired["GroupMemberRole"] + +class CIContent_sndGroupFeature(TypedDict): + type: Literal["sndGroupFeature"] + groupFeature: "GroupFeature" + preference: "GroupPreference" + param: NotRequired[int] # int + memberRole_: NotRequired["GroupMemberRole"] + +class CIContent_rcvChatFeatureRejected(TypedDict): + type: Literal["rcvChatFeatureRejected"] + feature: "ChatFeature" + +class CIContent_rcvGroupFeatureRejected(TypedDict): + type: Literal["rcvGroupFeatureRejected"] + groupFeature: "GroupFeature" + +class CIContent_sndModerated(TypedDict): + type: Literal["sndModerated"] + +class CIContent_rcvModerated(TypedDict): + type: Literal["rcvModerated"] + +class CIContent_rcvBlocked(TypedDict): + type: Literal["rcvBlocked"] + +class CIContent_sndDirectE2EEInfo(TypedDict): + type: Literal["sndDirectE2EEInfo"] + e2eeInfo: "E2EInfo" + +class CIContent_rcvDirectE2EEInfo(TypedDict): + type: Literal["rcvDirectE2EEInfo"] + e2eeInfo: "E2EInfo" + +class CIContent_sndGroupE2EEInfo(TypedDict): + type: Literal["sndGroupE2EEInfo"] + e2eeInfo: "E2EInfo" + +class CIContent_rcvGroupE2EEInfo(TypedDict): + type: Literal["rcvGroupE2EEInfo"] + e2eeInfo: "E2EInfo" + +class CIContent_chatBanner(TypedDict): + type: Literal["chatBanner"] + +CIContent = ( + CIContent_sndMsgContent + | CIContent_rcvMsgContent + | CIContent_sndDeleted + | CIContent_rcvDeleted + | CIContent_sndCall + | CIContent_rcvCall + | CIContent_rcvIntegrityError + | CIContent_rcvDecryptionError + | CIContent_rcvMsgError + | CIContent_rcvGroupInvitation + | CIContent_sndGroupInvitation + | CIContent_rcvDirectEvent + | CIContent_rcvGroupEvent + | CIContent_sndGroupEvent + | CIContent_rcvConnEvent + | CIContent_sndConnEvent + | CIContent_rcvChatFeature + | CIContent_sndChatFeature + | CIContent_rcvChatPreference + | CIContent_sndChatPreference + | CIContent_rcvGroupFeature + | CIContent_sndGroupFeature + | CIContent_rcvChatFeatureRejected + | CIContent_rcvGroupFeatureRejected + | CIContent_sndModerated + | CIContent_rcvModerated + | CIContent_rcvBlocked + | CIContent_sndDirectE2EEInfo + | CIContent_rcvDirectE2EEInfo + | CIContent_sndGroupE2EEInfo + | CIContent_rcvGroupE2EEInfo + | CIContent_chatBanner +) + +CIContent_Tag = Literal["sndMsgContent", "rcvMsgContent", "sndDeleted", "rcvDeleted", "sndCall", "rcvCall", "rcvIntegrityError", "rcvDecryptionError", "rcvMsgError", "rcvGroupInvitation", "sndGroupInvitation", "rcvDirectEvent", "rcvGroupEvent", "sndGroupEvent", "rcvConnEvent", "sndConnEvent", "rcvChatFeature", "sndChatFeature", "rcvChatPreference", "sndChatPreference", "rcvGroupFeature", "sndGroupFeature", "rcvChatFeatureRejected", "rcvGroupFeatureRejected", "sndModerated", "rcvModerated", "rcvBlocked", "sndDirectE2EEInfo", "rcvDirectE2EEInfo", "sndGroupE2EEInfo", "rcvGroupE2EEInfo", "chatBanner"] + +CIDeleteMode = Literal["broadcast", "internal", "internalMark"] + +class CIDeleted_deleted(TypedDict): + type: Literal["deleted"] + deletedTs: NotRequired[str] # ISO-8601 timestamp + chatType: "ChatType" + +class CIDeleted_blocked(TypedDict): + type: Literal["blocked"] + deletedTs: NotRequired[str] # ISO-8601 timestamp + +class CIDeleted_blockedByAdmin(TypedDict): + type: Literal["blockedByAdmin"] + deletedTs: NotRequired[str] # ISO-8601 timestamp + +class CIDeleted_moderated(TypedDict): + type: Literal["moderated"] + deletedTs: NotRequired[str] # ISO-8601 timestamp + byGroupMember: "GroupMember" + +CIDeleted = CIDeleted_deleted | CIDeleted_blocked | CIDeleted_blockedByAdmin | CIDeleted_moderated + +CIDeleted_Tag = Literal["deleted", "blocked", "blockedByAdmin", "moderated"] + +class CIDirection_directSnd(TypedDict): + type: Literal["directSnd"] + +class CIDirection_directRcv(TypedDict): + type: Literal["directRcv"] + +class CIDirection_groupSnd(TypedDict): + type: Literal["groupSnd"] + +class CIDirection_groupRcv(TypedDict): + type: Literal["groupRcv"] + groupMember: "GroupMember" + +class CIDirection_channelRcv(TypedDict): + type: Literal["channelRcv"] + +class CIDirection_localSnd(TypedDict): + type: Literal["localSnd"] + +class CIDirection_localRcv(TypedDict): + type: Literal["localRcv"] + +CIDirection = ( + CIDirection_directSnd + | CIDirection_directRcv + | CIDirection_groupSnd + | CIDirection_groupRcv + | CIDirection_channelRcv + | CIDirection_localSnd + | CIDirection_localRcv +) + +CIDirection_Tag = Literal["directSnd", "directRcv", "groupSnd", "groupRcv", "channelRcv", "localSnd", "localRcv"] + +class CIFile(TypedDict): + fileId: int # int64 + fileName: str + fileSize: int # int64 + fileSource: NotRequired["CryptoFile"] + fileStatus: "CIFileStatus" + fileProtocol: "FileProtocol" + +class CIFileStatus_sndStored(TypedDict): + type: Literal["sndStored"] + +class CIFileStatus_sndTransfer(TypedDict): + type: Literal["sndTransfer"] + sndProgress: int # int64 + sndTotal: int # int64 + +class CIFileStatus_sndCancelled(TypedDict): + type: Literal["sndCancelled"] + +class CIFileStatus_sndComplete(TypedDict): + type: Literal["sndComplete"] + +class CIFileStatus_sndError(TypedDict): + type: Literal["sndError"] + sndFileError: "FileError" + +class CIFileStatus_sndWarning(TypedDict): + type: Literal["sndWarning"] + sndFileError: "FileError" + +class CIFileStatus_rcvInvitation(TypedDict): + type: Literal["rcvInvitation"] + +class CIFileStatus_rcvAccepted(TypedDict): + type: Literal["rcvAccepted"] + +class CIFileStatus_rcvTransfer(TypedDict): + type: Literal["rcvTransfer"] + rcvProgress: int # int64 + rcvTotal: int # int64 + +class CIFileStatus_rcvAborted(TypedDict): + type: Literal["rcvAborted"] + +class CIFileStatus_rcvComplete(TypedDict): + type: Literal["rcvComplete"] + +class CIFileStatus_rcvCancelled(TypedDict): + type: Literal["rcvCancelled"] + +class CIFileStatus_rcvError(TypedDict): + type: Literal["rcvError"] + rcvFileError: "FileError" + +class CIFileStatus_rcvWarning(TypedDict): + type: Literal["rcvWarning"] + rcvFileError: "FileError" + +class CIFileStatus_invalid(TypedDict): + type: Literal["invalid"] + text: str + +CIFileStatus = ( + CIFileStatus_sndStored + | CIFileStatus_sndTransfer + | CIFileStatus_sndCancelled + | CIFileStatus_sndComplete + | CIFileStatus_sndError + | CIFileStatus_sndWarning + | CIFileStatus_rcvInvitation + | CIFileStatus_rcvAccepted + | CIFileStatus_rcvTransfer + | CIFileStatus_rcvAborted + | CIFileStatus_rcvComplete + | CIFileStatus_rcvCancelled + | CIFileStatus_rcvError + | CIFileStatus_rcvWarning + | CIFileStatus_invalid +) + +CIFileStatus_Tag = Literal["sndStored", "sndTransfer", "sndCancelled", "sndComplete", "sndError", "sndWarning", "rcvInvitation", "rcvAccepted", "rcvTransfer", "rcvAborted", "rcvComplete", "rcvCancelled", "rcvError", "rcvWarning", "invalid"] + +class CIForwardedFrom_unknown(TypedDict): + type: Literal["unknown"] + +class CIForwardedFrom_contact(TypedDict): + type: Literal["contact"] + chatName: str + msgDir: "MsgDirection" + contactId: NotRequired[int] # int64 + chatItemId: NotRequired[int] # int64 + +class CIForwardedFrom_group(TypedDict): + type: Literal["group"] + chatName: str + msgDir: "MsgDirection" + groupId: NotRequired[int] # int64 + chatItemId: NotRequired[int] # int64 + +CIForwardedFrom = CIForwardedFrom_unknown | CIForwardedFrom_contact | CIForwardedFrom_group + +CIForwardedFrom_Tag = Literal["unknown", "contact", "group"] + +class CIGroupInvitation(TypedDict): + groupId: int # int64 + groupMemberId: int # int64 + localDisplayName: str + groupProfile: "GroupProfile" + status: "CIGroupInvitationStatus" + +CIGroupInvitationStatus = Literal["pending", "accepted", "rejected", "expired"] + +class CIMention(TypedDict): + memberId: str + memberRef: NotRequired["CIMentionMember"] + +class CIMentionMember(TypedDict): + groupMemberId: int # int64 + displayName: str + localAlias: NotRequired[str] + memberRole: "GroupMemberRole" + +class CIMeta(TypedDict): + itemId: int # int64 + itemTs: str # ISO-8601 timestamp + itemText: str + itemStatus: "CIStatus" + sentViaProxy: NotRequired[bool] + itemSharedMsgId: NotRequired[str] + itemForwarded: NotRequired["CIForwardedFrom"] + itemDeleted: NotRequired["CIDeleted"] + itemEdited: bool + itemTimed: NotRequired["CITimed"] + itemLive: NotRequired[bool] + userMention: bool + hasLink: bool + deletable: bool + editable: bool + forwardedByMember: NotRequired[int] # int64 + showGroupAsSender: bool + msgSigned: NotRequired["MsgSigStatus"] + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + +class CIQuote(TypedDict): + chatDir: NotRequired["CIDirection"] + itemId: NotRequired[int] # int64 + sharedMsgId: NotRequired[str] + sentAt: str # ISO-8601 timestamp + content: "MsgContent" + formattedText: NotRequired[list["FormattedText"]] + +class CIReaction(TypedDict): + chatDir: "CIDirection" + chatItem: "ChatItem" + sentAt: str # ISO-8601 timestamp + reaction: "MsgReaction" + +class CIReactionCount(TypedDict): + reaction: "MsgReaction" + userReacted: bool + totalReacted: int # int + +class CIStatus_sndNew(TypedDict): + type: Literal["sndNew"] + +class CIStatus_sndSent(TypedDict): + type: Literal["sndSent"] + sndProgress: "SndCIStatusProgress" + +class CIStatus_sndRcvd(TypedDict): + type: Literal["sndRcvd"] + msgRcptStatus: "MsgReceiptStatus" + sndProgress: "SndCIStatusProgress" + +class CIStatus_sndErrorAuth(TypedDict): + type: Literal["sndErrorAuth"] + +class CIStatus_sndError(TypedDict): + type: Literal["sndError"] + agentError: "SndError" + +class CIStatus_sndWarning(TypedDict): + type: Literal["sndWarning"] + agentError: "SndError" + +class CIStatus_rcvNew(TypedDict): + type: Literal["rcvNew"] + +class CIStatus_rcvRead(TypedDict): + type: Literal["rcvRead"] + +class CIStatus_invalid(TypedDict): + type: Literal["invalid"] + text: str + +CIStatus = ( + CIStatus_sndNew + | CIStatus_sndSent + | CIStatus_sndRcvd + | CIStatus_sndErrorAuth + | CIStatus_sndError + | CIStatus_sndWarning + | CIStatus_rcvNew + | CIStatus_rcvRead + | CIStatus_invalid +) + +CIStatus_Tag = Literal["sndNew", "sndSent", "sndRcvd", "sndErrorAuth", "sndError", "sndWarning", "rcvNew", "rcvRead", "invalid"] + +class CITimed(TypedDict): + ttl: int # int + deleteAt: NotRequired[str] # ISO-8601 timestamp + +class ChatBotCommand_command(TypedDict): + type: Literal["command"] + keyword: str + label: str + params: NotRequired[str] + +class ChatBotCommand_menu(TypedDict): + type: Literal["menu"] + label: str + commands: list["ChatBotCommand"] + +ChatBotCommand = ChatBotCommand_command | ChatBotCommand_menu + +ChatBotCommand_Tag = Literal["command", "menu"] + +class ChatDeleteMode_full(TypedDict): + type: Literal["full"] + notify: bool + +class ChatDeleteMode_entity(TypedDict): + type: Literal["entity"] + notify: bool + +class ChatDeleteMode_messages(TypedDict): + type: Literal["messages"] + +ChatDeleteMode = ChatDeleteMode_full | ChatDeleteMode_entity | ChatDeleteMode_messages + +ChatDeleteMode_Tag = Literal["full", "entity", "messages"] + + +def ChatDeleteMode_cmd_string(self: ChatDeleteMode) -> str: + return str(self['type']) + ('' if str(self['type']) == 'messages' else (' notify=off' if not self['notify'] else '')) # type: ignore[typeddict-item] + +class ChatError_error(TypedDict): + type: Literal["error"] + errorType: "ChatErrorType" + +class ChatError_errorAgent(TypedDict): + type: Literal["errorAgent"] + agentError: "AgentErrorType" + agentConnId: str + connectionEntity_: NotRequired["ConnectionEntity"] + +class ChatError_errorStore(TypedDict): + type: Literal["errorStore"] + storeError: "StoreError" + +ChatError = ChatError_error | ChatError_errorAgent | ChatError_errorStore + +ChatError_Tag = Literal["error", "errorAgent", "errorStore"] + +class ChatErrorType_noActiveUser(TypedDict): + type: Literal["noActiveUser"] + +class ChatErrorType_noConnectionUser(TypedDict): + type: Literal["noConnectionUser"] + agentConnId: str + +class ChatErrorType_noSndFileUser(TypedDict): + type: Literal["noSndFileUser"] + agentSndFileId: str + +class ChatErrorType_noRcvFileUser(TypedDict): + type: Literal["noRcvFileUser"] + agentRcvFileId: str + +class ChatErrorType_userUnknown(TypedDict): + type: Literal["userUnknown"] + +class ChatErrorType_activeUserExists(TypedDict): + type: Literal["activeUserExists"] + +class ChatErrorType_userExists(TypedDict): + type: Literal["userExists"] + contactName: str + +class ChatErrorType_chatRelayExists(TypedDict): + type: Literal["chatRelayExists"] + +class ChatErrorType_differentActiveUser(TypedDict): + type: Literal["differentActiveUser"] + commandUserId: int # int64 + activeUserId: int # int64 + +class ChatErrorType_cantDeleteActiveUser(TypedDict): + type: Literal["cantDeleteActiveUser"] + userId: int # int64 + +class ChatErrorType_cantDeleteLastUser(TypedDict): + type: Literal["cantDeleteLastUser"] + userId: int # int64 + +class ChatErrorType_cantHideLastUser(TypedDict): + type: Literal["cantHideLastUser"] + userId: int # int64 + +class ChatErrorType_hiddenUserAlwaysMuted(TypedDict): + type: Literal["hiddenUserAlwaysMuted"] + userId: int # int64 + +class ChatErrorType_emptyUserPassword(TypedDict): + type: Literal["emptyUserPassword"] + userId: int # int64 + +class ChatErrorType_userAlreadyHidden(TypedDict): + type: Literal["userAlreadyHidden"] + userId: int # int64 + +class ChatErrorType_userNotHidden(TypedDict): + type: Literal["userNotHidden"] + userId: int # int64 + +class ChatErrorType_invalidDisplayName(TypedDict): + type: Literal["invalidDisplayName"] + displayName: str + validName: str + +class ChatErrorType_chatNotStarted(TypedDict): + type: Literal["chatNotStarted"] + +class ChatErrorType_chatNotStopped(TypedDict): + type: Literal["chatNotStopped"] + +class ChatErrorType_chatStoreChanged(TypedDict): + type: Literal["chatStoreChanged"] + +class ChatErrorType_invalidConnReq(TypedDict): + type: Literal["invalidConnReq"] + +class ChatErrorType_unsupportedConnReq(TypedDict): + type: Literal["unsupportedConnReq"] + +class ChatErrorType_connReqMessageProhibited(TypedDict): + type: Literal["connReqMessageProhibited"] + +class ChatErrorType_contactNotReady(TypedDict): + type: Literal["contactNotReady"] + contact: "Contact" + +class ChatErrorType_contactNotActive(TypedDict): + type: Literal["contactNotActive"] + contact: "Contact" + +class ChatErrorType_contactDisabled(TypedDict): + type: Literal["contactDisabled"] + contact: "Contact" + +class ChatErrorType_connectionDisabled(TypedDict): + type: Literal["connectionDisabled"] + connection: "Connection" + +class ChatErrorType_groupUserRole(TypedDict): + type: Literal["groupUserRole"] + groupInfo: "GroupInfo" + requiredRole: "GroupMemberRole" + +class ChatErrorType_groupMemberInitialRole(TypedDict): + type: Literal["groupMemberInitialRole"] + groupInfo: "GroupInfo" + initialRole: "GroupMemberRole" + +class ChatErrorType_contactIncognitoCantInvite(TypedDict): + type: Literal["contactIncognitoCantInvite"] + +class ChatErrorType_groupIncognitoCantInvite(TypedDict): + type: Literal["groupIncognitoCantInvite"] + +class ChatErrorType_groupContactRole(TypedDict): + type: Literal["groupContactRole"] + contactName: str + +class ChatErrorType_groupDuplicateMember(TypedDict): + type: Literal["groupDuplicateMember"] + contactName: str + +class ChatErrorType_groupDuplicateMemberId(TypedDict): + type: Literal["groupDuplicateMemberId"] + +class ChatErrorType_groupNotJoined(TypedDict): + type: Literal["groupNotJoined"] + groupInfo: "GroupInfo" + +class ChatErrorType_groupMemberNotActive(TypedDict): + type: Literal["groupMemberNotActive"] + +class ChatErrorType_cantBlockMemberForSelf(TypedDict): + type: Literal["cantBlockMemberForSelf"] + groupInfo: "GroupInfo" + member: "GroupMember" + setShowMessages: bool + +class ChatErrorType_groupMemberUserRemoved(TypedDict): + type: Literal["groupMemberUserRemoved"] + +class ChatErrorType_groupMemberNotFound(TypedDict): + type: Literal["groupMemberNotFound"] + +class ChatErrorType_groupCantResendInvitation(TypedDict): + type: Literal["groupCantResendInvitation"] + groupInfo: "GroupInfo" + contactName: str + +class ChatErrorType_groupInternal(TypedDict): + type: Literal["groupInternal"] + message: str + +class ChatErrorType_fileNotFound(TypedDict): + type: Literal["fileNotFound"] + message: str + +class ChatErrorType_fileSize(TypedDict): + type: Literal["fileSize"] + filePath: str + +class ChatErrorType_fileAlreadyReceiving(TypedDict): + type: Literal["fileAlreadyReceiving"] + message: str + +class ChatErrorType_fileCancelled(TypedDict): + type: Literal["fileCancelled"] + message: str + +class ChatErrorType_fileCancel(TypedDict): + type: Literal["fileCancel"] + fileId: int # int64 + message: str + +class ChatErrorType_fileAlreadyExists(TypedDict): + type: Literal["fileAlreadyExists"] + filePath: str + +class ChatErrorType_fileWrite(TypedDict): + type: Literal["fileWrite"] + filePath: str + message: str + +class ChatErrorType_fileSend(TypedDict): + type: Literal["fileSend"] + fileId: int # int64 + agentError: "AgentErrorType" + +class ChatErrorType_fileRcvChunk(TypedDict): + type: Literal["fileRcvChunk"] + message: str + +class ChatErrorType_fileInternal(TypedDict): + type: Literal["fileInternal"] + message: str + +class ChatErrorType_fileImageType(TypedDict): + type: Literal["fileImageType"] + filePath: str + +class ChatErrorType_fileImageSize(TypedDict): + type: Literal["fileImageSize"] + filePath: str + +class ChatErrorType_fileNotReceived(TypedDict): + type: Literal["fileNotReceived"] + fileId: int # int64 + +class ChatErrorType_fileNotApproved(TypedDict): + type: Literal["fileNotApproved"] + fileId: int # int64 + unknownServers: list[str] + +class ChatErrorType_fallbackToSMPProhibited(TypedDict): + type: Literal["fallbackToSMPProhibited"] + fileId: int # int64 + +class ChatErrorType_inlineFileProhibited(TypedDict): + type: Literal["inlineFileProhibited"] + fileId: int # int64 + +class ChatErrorType_invalidForward(TypedDict): + type: Literal["invalidForward"] + +class ChatErrorType_invalidChatItemUpdate(TypedDict): + type: Literal["invalidChatItemUpdate"] + +class ChatErrorType_invalidChatItemDelete(TypedDict): + type: Literal["invalidChatItemDelete"] + +class ChatErrorType_hasCurrentCall(TypedDict): + type: Literal["hasCurrentCall"] + +class ChatErrorType_noCurrentCall(TypedDict): + type: Literal["noCurrentCall"] + +class ChatErrorType_callContact(TypedDict): + type: Literal["callContact"] + contactId: int # int64 + +class ChatErrorType_directMessagesProhibited(TypedDict): + type: Literal["directMessagesProhibited"] + direction: "MsgDirection" + contact: "Contact" + +class ChatErrorType_agentVersion(TypedDict): + type: Literal["agentVersion"] + +class ChatErrorType_agentNoSubResult(TypedDict): + type: Literal["agentNoSubResult"] + agentConnId: str + +class ChatErrorType_commandError(TypedDict): + type: Literal["commandError"] + message: str + +class ChatErrorType_agentCommandError(TypedDict): + type: Literal["agentCommandError"] + message: str + +class ChatErrorType_invalidFileDescription(TypedDict): + type: Literal["invalidFileDescription"] + message: str + +class ChatErrorType_connectionIncognitoChangeProhibited(TypedDict): + type: Literal["connectionIncognitoChangeProhibited"] + +class ChatErrorType_connectionUserChangeProhibited(TypedDict): + type: Literal["connectionUserChangeProhibited"] + +class ChatErrorType_peerChatVRangeIncompatible(TypedDict): + type: Literal["peerChatVRangeIncompatible"] + +class ChatErrorType_relayTestError(TypedDict): + type: Literal["relayTestError"] + message: str + +class ChatErrorType_internalError(TypedDict): + type: Literal["internalError"] + message: str + +class ChatErrorType_exception(TypedDict): + type: Literal["exception"] + message: str + +ChatErrorType = ( + ChatErrorType_noActiveUser + | ChatErrorType_noConnectionUser + | ChatErrorType_noSndFileUser + | ChatErrorType_noRcvFileUser + | ChatErrorType_userUnknown + | ChatErrorType_activeUserExists + | ChatErrorType_userExists + | ChatErrorType_chatRelayExists + | ChatErrorType_differentActiveUser + | ChatErrorType_cantDeleteActiveUser + | ChatErrorType_cantDeleteLastUser + | ChatErrorType_cantHideLastUser + | ChatErrorType_hiddenUserAlwaysMuted + | ChatErrorType_emptyUserPassword + | ChatErrorType_userAlreadyHidden + | ChatErrorType_userNotHidden + | ChatErrorType_invalidDisplayName + | ChatErrorType_chatNotStarted + | ChatErrorType_chatNotStopped + | ChatErrorType_chatStoreChanged + | ChatErrorType_invalidConnReq + | ChatErrorType_unsupportedConnReq + | ChatErrorType_connReqMessageProhibited + | ChatErrorType_contactNotReady + | ChatErrorType_contactNotActive + | ChatErrorType_contactDisabled + | ChatErrorType_connectionDisabled + | ChatErrorType_groupUserRole + | ChatErrorType_groupMemberInitialRole + | ChatErrorType_contactIncognitoCantInvite + | ChatErrorType_groupIncognitoCantInvite + | ChatErrorType_groupContactRole + | ChatErrorType_groupDuplicateMember + | ChatErrorType_groupDuplicateMemberId + | ChatErrorType_groupNotJoined + | ChatErrorType_groupMemberNotActive + | ChatErrorType_cantBlockMemberForSelf + | ChatErrorType_groupMemberUserRemoved + | ChatErrorType_groupMemberNotFound + | ChatErrorType_groupCantResendInvitation + | ChatErrorType_groupInternal + | ChatErrorType_fileNotFound + | ChatErrorType_fileSize + | ChatErrorType_fileAlreadyReceiving + | ChatErrorType_fileCancelled + | ChatErrorType_fileCancel + | ChatErrorType_fileAlreadyExists + | ChatErrorType_fileWrite + | ChatErrorType_fileSend + | ChatErrorType_fileRcvChunk + | ChatErrorType_fileInternal + | ChatErrorType_fileImageType + | ChatErrorType_fileImageSize + | ChatErrorType_fileNotReceived + | ChatErrorType_fileNotApproved + | ChatErrorType_fallbackToSMPProhibited + | ChatErrorType_inlineFileProhibited + | ChatErrorType_invalidForward + | ChatErrorType_invalidChatItemUpdate + | ChatErrorType_invalidChatItemDelete + | ChatErrorType_hasCurrentCall + | ChatErrorType_noCurrentCall + | ChatErrorType_callContact + | ChatErrorType_directMessagesProhibited + | ChatErrorType_agentVersion + | ChatErrorType_agentNoSubResult + | ChatErrorType_commandError + | ChatErrorType_agentCommandError + | ChatErrorType_invalidFileDescription + | ChatErrorType_connectionIncognitoChangeProhibited + | ChatErrorType_connectionUserChangeProhibited + | ChatErrorType_peerChatVRangeIncompatible + | ChatErrorType_relayTestError + | ChatErrorType_internalError + | ChatErrorType_exception +) + +ChatErrorType_Tag = Literal["noActiveUser", "noConnectionUser", "noSndFileUser", "noRcvFileUser", "userUnknown", "activeUserExists", "userExists", "chatRelayExists", "differentActiveUser", "cantDeleteActiveUser", "cantDeleteLastUser", "cantHideLastUser", "hiddenUserAlwaysMuted", "emptyUserPassword", "userAlreadyHidden", "userNotHidden", "invalidDisplayName", "chatNotStarted", "chatNotStopped", "chatStoreChanged", "invalidConnReq", "unsupportedConnReq", "connReqMessageProhibited", "contactNotReady", "contactNotActive", "contactDisabled", "connectionDisabled", "groupUserRole", "groupMemberInitialRole", "contactIncognitoCantInvite", "groupIncognitoCantInvite", "groupContactRole", "groupDuplicateMember", "groupDuplicateMemberId", "groupNotJoined", "groupMemberNotActive", "cantBlockMemberForSelf", "groupMemberUserRemoved", "groupMemberNotFound", "groupCantResendInvitation", "groupInternal", "fileNotFound", "fileSize", "fileAlreadyReceiving", "fileCancelled", "fileCancel", "fileAlreadyExists", "fileWrite", "fileSend", "fileRcvChunk", "fileInternal", "fileImageType", "fileImageSize", "fileNotReceived", "fileNotApproved", "fallbackToSMPProhibited", "inlineFileProhibited", "invalidForward", "invalidChatItemUpdate", "invalidChatItemDelete", "hasCurrentCall", "noCurrentCall", "callContact", "directMessagesProhibited", "agentVersion", "agentNoSubResult", "commandError", "agentCommandError", "invalidFileDescription", "connectionIncognitoChangeProhibited", "connectionUserChangeProhibited", "peerChatVRangeIncompatible", "relayTestError", "internalError", "exception"] + +ChatFeature = Literal["timedMessages", "fullDelete", "reactions", "voice", "files", "calls", "sessions"] + +class ChatInfo_direct(TypedDict): + type: Literal["direct"] + contact: "Contact" + +class ChatInfo_group(TypedDict): + type: Literal["group"] + groupInfo: "GroupInfo" + groupChatScope: NotRequired["GroupChatScopeInfo"] + +class ChatInfo_local(TypedDict): + type: Literal["local"] + noteFolder: "NoteFolder" + +class ChatInfo_contactRequest(TypedDict): + type: Literal["contactRequest"] + contactRequest: "UserContactRequest" + +class ChatInfo_contactConnection(TypedDict): + type: Literal["contactConnection"] + contactConnection: "PendingContactConnection" + +ChatInfo = ( + ChatInfo_direct + | ChatInfo_group + | ChatInfo_local + | ChatInfo_contactRequest + | ChatInfo_contactConnection +) + +ChatInfo_Tag = Literal["direct", "group", "local", "contactRequest", "contactConnection"] + +class ChatItem(TypedDict): + chatDir: "CIDirection" + meta: "CIMeta" + content: "CIContent" + mentions: dict[str, "CIMention"] + formattedText: NotRequired[list["FormattedText"]] + quotedItem: NotRequired["CIQuote"] + reactions: list["CIReactionCount"] + file: NotRequired["CIFile"] + +# Message deletion result. + +class ChatItemDeletion(TypedDict): + deletedChatItem: "AChatItem" + toChatItem: NotRequired["AChatItem"] + +class ChatListQuery_filters(TypedDict): + type: Literal["filters"] + favorite: bool + unread: bool + +class ChatListQuery_search(TypedDict): + type: Literal["search"] + search: str + +ChatListQuery = ChatListQuery_filters | ChatListQuery_search + +ChatListQuery_Tag = Literal["filters", "search"] + +ChatPeerType = Literal["human", "bot"] + +# Used in API commands. Chat scope can only be passed with groups. + +class ChatRef(TypedDict): + chatType: "ChatType" + chatId: int # int64 + chatScope: NotRequired["GroupChatScope"] + + +def ChatRef_cmd_string(self: ChatRef) -> str: + return ChatType_cmd_string(self['chatType']) + str(self['chatId']) + ((GroupChatScope_cmd_string(self.get('chatScope'))) if self.get('chatScope') is not None else '') + +class ChatSettings(TypedDict): + enableNtfs: "MsgFilter" + sendRcpts: NotRequired[bool] + favorite: bool + +class ChatStats(TypedDict): + unreadCount: int # int + unreadMentions: int # int + reportsCount: int # int + minUnreadItemId: int # int64 + unreadChat: bool + +ChatType = Literal["direct", "group", "local"] + + +def ChatType_cmd_string(self: ChatType) -> str: + return '@' if str(self) == 'direct' else '#' if str(self) == 'group' else '*' if str(self) == 'local' else '' + +class ChatWallpaper(TypedDict): + preset: NotRequired[str] + imageFile: NotRequired[str] + background: NotRequired[str] + tint: NotRequired[str] + scaleType: NotRequired["ChatWallpaperScale"] + scale: NotRequired[float] # double + +ChatWallpaperScale = Literal["fill", "fit", "repeat"] + +class ClientNotice(TypedDict): + ttl: NotRequired[int] # int64 + +Color = Literal["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white"] + +class CommandError_UNKNOWN(TypedDict): + type: Literal["UNKNOWN"] + +class CommandError_SYNTAX(TypedDict): + type: Literal["SYNTAX"] + +class CommandError_PROHIBITED(TypedDict): + type: Literal["PROHIBITED"] + +class CommandError_NO_AUTH(TypedDict): + type: Literal["NO_AUTH"] + +class CommandError_HAS_AUTH(TypedDict): + type: Literal["HAS_AUTH"] + +class CommandError_NO_ENTITY(TypedDict): + type: Literal["NO_ENTITY"] + +CommandError = ( + CommandError_UNKNOWN + | CommandError_SYNTAX + | CommandError_PROHIBITED + | CommandError_NO_AUTH + | CommandError_HAS_AUTH + | CommandError_NO_ENTITY +) + +CommandError_Tag = Literal["UNKNOWN", "SYNTAX", "PROHIBITED", "NO_AUTH", "HAS_AUTH", "NO_ENTITY"] + +class CommandErrorType_PROHIBITED(TypedDict): + type: Literal["PROHIBITED"] + +class CommandErrorType_SYNTAX(TypedDict): + type: Literal["SYNTAX"] + +class CommandErrorType_NO_CONN(TypedDict): + type: Literal["NO_CONN"] + +class CommandErrorType_SIZE(TypedDict): + type: Literal["SIZE"] + +class CommandErrorType_LARGE(TypedDict): + type: Literal["LARGE"] + +CommandErrorType = ( + CommandErrorType_PROHIBITED + | CommandErrorType_SYNTAX + | CommandErrorType_NO_CONN + | CommandErrorType_SIZE + | CommandErrorType_LARGE +) + +CommandErrorType_Tag = Literal["PROHIBITED", "SYNTAX", "NO_CONN", "SIZE", "LARGE"] + +class CommentsGroupPreference(TypedDict): + enable: "GroupFeatureEnabled" + duration: NotRequired[int] # int + +class ComposedMessage(TypedDict): + fileSource: NotRequired["CryptoFile"] + quotedItemId: NotRequired[int] # int64 + msgContent: "MsgContent" + mentions: dict[str, int] # str : int64 + +class ConnStatus_new(TypedDict): + type: Literal["new"] + +class ConnStatus_prepared(TypedDict): + type: Literal["prepared"] + +class ConnStatus_joined(TypedDict): + type: Literal["joined"] + +class ConnStatus_requested(TypedDict): + type: Literal["requested"] + +class ConnStatus_accepted(TypedDict): + type: Literal["accepted"] + +class ConnStatus_sndReady(TypedDict): + type: Literal["sndReady"] + +class ConnStatus_ready(TypedDict): + type: Literal["ready"] + +class ConnStatus_deleted(TypedDict): + type: Literal["deleted"] + +class ConnStatus_failed(TypedDict): + type: Literal["failed"] + connError: str + +ConnStatus = ( + ConnStatus_new + | ConnStatus_prepared + | ConnStatus_joined + | ConnStatus_requested + | ConnStatus_accepted + | ConnStatus_sndReady + | ConnStatus_ready + | ConnStatus_deleted + | ConnStatus_failed +) + +ConnStatus_Tag = Literal["new", "prepared", "joined", "requested", "accepted", "sndReady", "ready", "deleted", "failed"] + +ConnType = Literal["contact", "member", "user_contact"] + +class Connection(TypedDict): + connId: int # int64 + agentConnId: str + connChatVersion: int # int + peerChatVRange: "VersionRange" + connLevel: int # int + viaContact: NotRequired[int] # int64 + viaUserContactLink: NotRequired[int] # int64 + viaGroupLink: bool + groupLinkId: NotRequired[str] + xContactId: NotRequired[str] + customUserProfileId: NotRequired[int] # int64 + connType: "ConnType" + connStatus: "ConnStatus" + contactConnInitiated: bool + localAlias: str + entityId: NotRequired[int] # int64 + connectionCode: NotRequired["SecurityCode"] + pqSupport: bool + pqEncryption: bool + pqSndEnabled: NotRequired[bool] + pqRcvEnabled: NotRequired[bool] + authErrCounter: int # int + quotaErrCounter: int # int + createdAt: str # ISO-8601 timestamp + +class ConnectionEntity_rcvDirectMsgConnection(TypedDict): + type: Literal["rcvDirectMsgConnection"] + entityConnection: "Connection" + contact: NotRequired["Contact"] + +class ConnectionEntity_rcvGroupMsgConnection(TypedDict): + type: Literal["rcvGroupMsgConnection"] + entityConnection: "Connection" + groupInfo: "GroupInfo" + groupMember: "GroupMember" + +class ConnectionEntity_userContactConnection(TypedDict): + type: Literal["userContactConnection"] + entityConnection: "Connection" + userContact: "UserContact" + +ConnectionEntity = ( + ConnectionEntity_rcvDirectMsgConnection + | ConnectionEntity_rcvGroupMsgConnection + | ConnectionEntity_userContactConnection +) + +ConnectionEntity_Tag = Literal["rcvDirectMsgConnection", "rcvGroupMsgConnection", "userContactConnection"] + +class ConnectionErrorType_NOT_FOUND(TypedDict): + type: Literal["NOT_FOUND"] + +class ConnectionErrorType_DUPLICATE(TypedDict): + type: Literal["DUPLICATE"] + +class ConnectionErrorType_SIMPLEX(TypedDict): + type: Literal["SIMPLEX"] + +class ConnectionErrorType_NOT_ACCEPTED(TypedDict): + type: Literal["NOT_ACCEPTED"] + +class ConnectionErrorType_NOT_AVAILABLE(TypedDict): + type: Literal["NOT_AVAILABLE"] + +ConnectionErrorType = ( + ConnectionErrorType_NOT_FOUND + | ConnectionErrorType_DUPLICATE + | ConnectionErrorType_SIMPLEX + | ConnectionErrorType_NOT_ACCEPTED + | ConnectionErrorType_NOT_AVAILABLE +) + +ConnectionErrorType_Tag = Literal["NOT_FOUND", "DUPLICATE", "SIMPLEX", "NOT_ACCEPTED", "NOT_AVAILABLE"] + +ConnectionMode = Literal["INV", "CON"] + +class ConnectionPlan_invitationLink(TypedDict): + type: Literal["invitationLink"] + invitationLinkPlan: "InvitationLinkPlan" + +class ConnectionPlan_contactAddress(TypedDict): + type: Literal["contactAddress"] + contactAddressPlan: "ContactAddressPlan" + +class ConnectionPlan_groupLink(TypedDict): + type: Literal["groupLink"] + groupLinkPlan: "GroupLinkPlan" + +class ConnectionPlan_error(TypedDict): + type: Literal["error"] + chatError: "ChatError" + +ConnectionPlan = ( + ConnectionPlan_invitationLink + | ConnectionPlan_contactAddress + | ConnectionPlan_groupLink + | ConnectionPlan_error +) + +ConnectionPlan_Tag = Literal["invitationLink", "contactAddress", "groupLink", "error"] + +class Contact(TypedDict): + contactId: int # int64 + localDisplayName: str + profile: "LocalProfile" + activeConn: NotRequired["Connection"] + contactUsed: bool + contactStatus: "ContactStatus" + chatSettings: "ChatSettings" + userPreferences: "Preferences" + mergedPreferences: "ContactUserPreferences" + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + chatTs: NotRequired[str] # ISO-8601 timestamp + preparedContact: NotRequired["PreparedContact"] + contactRequestId: NotRequired[int] # int64 + contactGroupMemberId: NotRequired[int] # int64 + contactGrpInvSent: bool + groupDirectInv: NotRequired["GroupDirectInvitation"] + chatTags: list[int] # int64 + chatItemTTL: NotRequired[int] # int64 + uiThemes: NotRequired["UIThemeEntityOverrides"] + chatDeleted: bool + customData: NotRequired[dict[str, object]] + +class ContactAddressPlan_ok(TypedDict): + type: Literal["ok"] + contactSLinkData_: NotRequired["ContactShortLinkData"] + ownerVerification: NotRequired["OwnerVerification"] + +class ContactAddressPlan_ownLink(TypedDict): + type: Literal["ownLink"] + +class ContactAddressPlan_connectingConfirmReconnect(TypedDict): + type: Literal["connectingConfirmReconnect"] + +class ContactAddressPlan_connectingProhibit(TypedDict): + type: Literal["connectingProhibit"] + contact: "Contact" + +class ContactAddressPlan_known(TypedDict): + type: Literal["known"] + contact: "Contact" + +class ContactAddressPlan_contactViaAddress(TypedDict): + type: Literal["contactViaAddress"] + contact: "Contact" + +ContactAddressPlan = ( + ContactAddressPlan_ok + | ContactAddressPlan_ownLink + | ContactAddressPlan_connectingConfirmReconnect + | ContactAddressPlan_connectingProhibit + | ContactAddressPlan_known + | ContactAddressPlan_contactViaAddress +) + +ContactAddressPlan_Tag = Literal["ok", "ownLink", "connectingConfirmReconnect", "connectingProhibit", "known", "contactViaAddress"] + +class ContactShortLinkData(TypedDict): + profile: "Profile" + message: NotRequired["MsgContent"] + business: bool + +ContactStatus = Literal["active", "deleted", "deletedByUser"] + +class ContactUserPref_contact(TypedDict): + type: Literal["contact"] + preference: "SimplePreference" + +class ContactUserPref_user(TypedDict): + type: Literal["user"] + preference: "SimplePreference" + +ContactUserPref = ContactUserPref_contact | ContactUserPref_user + +ContactUserPref_Tag = Literal["contact", "user"] + +class ContactUserPreference(TypedDict): + enabled: "PrefEnabled" + userPreference: "ContactUserPref" + contactPreference: "SimplePreference" + +class ContactUserPreferences(TypedDict): + timedMessages: "ContactUserPreference" + fullDelete: "ContactUserPreference" + reactions: "ContactUserPreference" + voice: "ContactUserPreference" + files: "ContactUserPreference" + calls: "ContactUserPreference" + sessions: "ContactUserPreference" + commands: NotRequired[list["ChatBotCommand"]] + +class CreatedConnLink(TypedDict): + connFullLink: str + connShortLink: NotRequired[str] + + +def CreatedConnLink_cmd_string(self: CreatedConnLink) -> str: + return self['connFullLink'] + ((' ' + self.get('connShortLink')) if self.get('connShortLink') is not None else '') + +class CryptoFile(TypedDict): + filePath: str + cryptoArgs: NotRequired["CryptoFileArgs"] + +class CryptoFileArgs(TypedDict): + fileKey: str + fileNonce: str + +class DroppedMsg(TypedDict): + brokerTs: str # ISO-8601 timestamp + attempts: int # int + +class E2EInfo(TypedDict): + public: NotRequired[bool] + pqEnabled: NotRequired[bool] + +class ErrorType_BLOCK(TypedDict): + type: Literal["BLOCK"] + +class ErrorType_SESSION(TypedDict): + type: Literal["SESSION"] + +class ErrorType_CMD(TypedDict): + type: Literal["CMD"] + cmdErr: "CommandError" + +class ErrorType_PROXY(TypedDict): + type: Literal["PROXY"] + proxyErr: "ProxyError" + +class ErrorType_AUTH(TypedDict): + type: Literal["AUTH"] + +class ErrorType_BLOCKED(TypedDict): + type: Literal["BLOCKED"] + blockInfo: "BlockingInfo" + +class ErrorType_SERVICE(TypedDict): + type: Literal["SERVICE"] + +class ErrorType_CRYPTO(TypedDict): + type: Literal["CRYPTO"] + +class ErrorType_QUOTA(TypedDict): + type: Literal["QUOTA"] + +class ErrorType_STORE(TypedDict): + type: Literal["STORE"] + storeErr: str + +class ErrorType_NO_MSG(TypedDict): + type: Literal["NO_MSG"] + +class ErrorType_LARGE_MSG(TypedDict): + type: Literal["LARGE_MSG"] + +class ErrorType_EXPIRED(TypedDict): + type: Literal["EXPIRED"] + +class ErrorType_INTERNAL(TypedDict): + type: Literal["INTERNAL"] + +class ErrorType_DUPLICATE_(TypedDict): + type: Literal["DUPLICATE_"] + +ErrorType = ( + ErrorType_BLOCK + | ErrorType_SESSION + | ErrorType_CMD + | ErrorType_PROXY + | ErrorType_AUTH + | ErrorType_BLOCKED + | ErrorType_SERVICE + | ErrorType_CRYPTO + | ErrorType_QUOTA + | ErrorType_STORE + | ErrorType_NO_MSG + | ErrorType_LARGE_MSG + | ErrorType_EXPIRED + | ErrorType_INTERNAL + | ErrorType_DUPLICATE_ +) + +ErrorType_Tag = Literal["BLOCK", "SESSION", "CMD", "PROXY", "AUTH", "BLOCKED", "SERVICE", "CRYPTO", "QUOTA", "STORE", "NO_MSG", "LARGE_MSG", "EXPIRED", "INTERNAL", "DUPLICATE_"] + +FeatureAllowed = Literal["always", "yes", "no"] + +class FileDescr(TypedDict): + fileDescrText: str + fileDescrPartNo: int # int + fileDescrComplete: bool + +class FileError_auth(TypedDict): + type: Literal["auth"] + +class FileError_blocked(TypedDict): + type: Literal["blocked"] + server: str + blockInfo: "BlockingInfo" + +class FileError_noFile(TypedDict): + type: Literal["noFile"] + +class FileError_relay(TypedDict): + type: Literal["relay"] + srvError: "SrvError" + +class FileError_other(TypedDict): + type: Literal["other"] + fileError: str + +FileError = ( + FileError_auth + | FileError_blocked + | FileError_noFile + | FileError_relay + | FileError_other +) + +FileError_Tag = Literal["auth", "blocked", "noFile", "relay", "other"] + +class FileErrorType_NOT_APPROVED(TypedDict): + type: Literal["NOT_APPROVED"] + +class FileErrorType_SIZE(TypedDict): + type: Literal["SIZE"] + +class FileErrorType_REDIRECT(TypedDict): + type: Literal["REDIRECT"] + redirectError: str + +class FileErrorType_FILE_IO(TypedDict): + type: Literal["FILE_IO"] + fileIOError: str + +class FileErrorType_NO_FILE(TypedDict): + type: Literal["NO_FILE"] + +FileErrorType = ( + FileErrorType_NOT_APPROVED + | FileErrorType_SIZE + | FileErrorType_REDIRECT + | FileErrorType_FILE_IO + | FileErrorType_NO_FILE +) + +FileErrorType_Tag = Literal["NOT_APPROVED", "SIZE", "REDIRECT", "FILE_IO", "NO_FILE"] + +class FileInvitation(TypedDict): + fileName: str + fileSize: int # int64 + fileDigest: NotRequired[str] + fileConnReq: NotRequired[str] + fileInline: NotRequired["InlineFileMode"] + fileDescr: NotRequired["FileDescr"] + +FileProtocol = Literal["SMP", "XFTP", "LOCAL"] + +FileStatus = Literal["new", "accepted", "connected", "complete", "cancelled"] + +class FileTransferMeta(TypedDict): + fileId: int # int64 + xftpSndFile: NotRequired["XFTPSndFile"] + xftpRedirectFor: NotRequired[int] # int64 + fileName: str + filePath: str + fileSize: int # int64 + fileInline: NotRequired["InlineFileMode"] + chunkSize: int # int64 + cancelled: bool + +class Format_bold(TypedDict): + type: Literal["bold"] + +class Format_italic(TypedDict): + type: Literal["italic"] + +class Format_strikeThrough(TypedDict): + type: Literal["strikeThrough"] + +class Format_snippet(TypedDict): + type: Literal["snippet"] + +class Format_secret(TypedDict): + type: Literal["secret"] + +class Format_small(TypedDict): + type: Literal["small"] + +class Format_colored(TypedDict): + type: Literal["colored"] + color: "Color" + +class Format_uri(TypedDict): + type: Literal["uri"] + +class Format_hyperLink(TypedDict): + type: Literal["hyperLink"] + showText: NotRequired[str] + linkUri: str + +class Format_simplexLink(TypedDict): + type: Literal["simplexLink"] + showText: NotRequired[str] + linkType: "SimplexLinkType" + simplexUri: str + smpHosts: list[str] # non-empty + +class Format_command(TypedDict): + type: Literal["command"] + commandStr: str + +class Format_mention(TypedDict): + type: Literal["mention"] + memberName: str + +class Format_email(TypedDict): + type: Literal["email"] + +class Format_phone(TypedDict): + type: Literal["phone"] + +Format = ( + Format_bold + | Format_italic + | Format_strikeThrough + | Format_snippet + | Format_secret + | Format_small + | Format_colored + | Format_uri + | Format_hyperLink + | Format_simplexLink + | Format_command + | Format_mention + | Format_email + | Format_phone +) + +Format_Tag = Literal["bold", "italic", "strikeThrough", "snippet", "secret", "small", "colored", "uri", "hyperLink", "simplexLink", "command", "mention", "email", "phone"] + +class FormattedText(TypedDict): + format: NotRequired["Format"] + text: str + +class FullGroupPreferences(TypedDict): + timedMessages: "TimedMessagesGroupPreference" + directMessages: "RoleGroupPreference" + fullDelete: "GroupPreference" + reactions: "GroupPreference" + voice: "RoleGroupPreference" + files: "RoleGroupPreference" + simplexLinks: "RoleGroupPreference" + reports: "GroupPreference" + history: "GroupPreference" + support: "SupportGroupPreference" + sessions: "RoleGroupPreference" + comments: "CommentsGroupPreference" + commands: list["ChatBotCommand"] + +class FullPreferences(TypedDict): + timedMessages: "TimedMessagesPreference" + fullDelete: "SimplePreference" + reactions: "SimplePreference" + voice: "SimplePreference" + files: "SimplePreference" + calls: "SimplePreference" + sessions: "SimplePreference" + commands: list["ChatBotCommand"] + +class Group(TypedDict): + groupInfo: "GroupInfo" + members: list["GroupMember"] + +class GroupChatScope_memberSupport(TypedDict): + type: Literal["memberSupport"] + groupMemberId_: NotRequired[int] # int64 + +GroupChatScope = GroupChatScope_memberSupport + +GroupChatScope_Tag = Literal["memberSupport"] + + +def GroupChatScope_cmd_string(self: GroupChatScope) -> str: + return '(_support' + ((':' + str(self.get('groupMemberId_'))) if self.get('groupMemberId_') is not None else '') + ')' # type: ignore[typeddict-item] + +class GroupChatScopeInfo_memberSupport(TypedDict): + type: Literal["memberSupport"] + groupMember_: NotRequired["GroupMember"] + +GroupChatScopeInfo = GroupChatScopeInfo_memberSupport + +GroupChatScopeInfo_Tag = Literal["memberSupport"] + +class GroupDirectInvitation(TypedDict): + groupDirectInvLink: str + fromGroupId_: NotRequired[int] # int64 + fromGroupMemberId_: NotRequired[int] # int64 + fromGroupMemberConnId_: NotRequired[int] # int64 + groupDirectInvStartedConnection: bool + +GroupFeature = Literal["timedMessages", "directMessages", "fullDelete", "reactions", "voice", "files", "simplexLinks", "reports", "history", "support", "sessions", "comments"] + +GroupFeatureEnabled = Literal["on", "off"] + +class GroupInfo(TypedDict): + groupId: int # int64 + useRelays: bool + relayOwnStatus: NotRequired["RelayStatus"] + localDisplayName: str + groupProfile: "GroupProfile" + localAlias: str + businessChat: NotRequired["BusinessChatInfo"] + fullGroupPreferences: "FullGroupPreferences" + membership: "GroupMember" + chatSettings: "ChatSettings" + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + chatTs: NotRequired[str] # ISO-8601 timestamp + userMemberProfileSentAt: NotRequired[str] # ISO-8601 timestamp + preparedGroup: NotRequired["PreparedGroup"] + chatTags: list[int] # int64 + chatItemTTL: NotRequired[int] # int64 + uiThemes: NotRequired["UIThemeEntityOverrides"] + customData: NotRequired[dict[str, object]] + groupSummary: "GroupSummary" + membersRequireAttention: int # int + viaGroupLinkUri: NotRequired[str] + groupKeys: NotRequired["GroupKeys"] + +class GroupKeys(TypedDict): + publicGroupId: str + groupRootKey: "GroupRootKey" + memberPrivKey: str + +class GroupLink(TypedDict): + userContactLinkId: int # int64 + connLinkContact: "CreatedConnLink" + shortLinkDataSet: bool + shortLinkLargeDataSet: bool + groupLinkId: str + acceptMemberRole: "GroupMemberRole" + +class GroupLinkOwner(TypedDict): + memberId: str + memberKey: str + +class GroupLinkPlan_ok(TypedDict): + type: Literal["ok"] + groupSLinkInfo_: NotRequired["GroupShortLinkInfo"] + groupSLinkData_: NotRequired["GroupShortLinkData"] + ownerVerification: NotRequired["OwnerVerification"] + +class GroupLinkPlan_ownLink(TypedDict): + type: Literal["ownLink"] + groupInfo: "GroupInfo" + +class GroupLinkPlan_connectingConfirmReconnect(TypedDict): + type: Literal["connectingConfirmReconnect"] + +class GroupLinkPlan_connectingProhibit(TypedDict): + type: Literal["connectingProhibit"] + groupInfo_: NotRequired["GroupInfo"] + +class GroupLinkPlan_known(TypedDict): + type: Literal["known"] + groupInfo: "GroupInfo" + groupUpdated: bool + ownerVerification: NotRequired["OwnerVerification"] + linkOwners: list["GroupLinkOwner"] + +class GroupLinkPlan_noRelays(TypedDict): + type: Literal["noRelays"] + groupSLinkData_: NotRequired["GroupShortLinkData"] + +GroupLinkPlan = ( + GroupLinkPlan_ok + | GroupLinkPlan_ownLink + | GroupLinkPlan_connectingConfirmReconnect + | GroupLinkPlan_connectingProhibit + | GroupLinkPlan_known + | GroupLinkPlan_noRelays +) + +GroupLinkPlan_Tag = Literal["ok", "ownLink", "connectingConfirmReconnect", "connectingProhibit", "known", "noRelays"] + +class GroupMember(TypedDict): + groupMemberId: int # int64 + groupId: int # int64 + indexInGroup: int # int64 + memberId: str + memberRole: "GroupMemberRole" + memberCategory: "GroupMemberCategory" + memberStatus: "GroupMemberStatus" + memberSettings: "GroupMemberSettings" + blockedByAdmin: bool + invitedBy: "InvitedBy" + invitedByGroupMemberId: NotRequired[int] # int64 + localDisplayName: str + memberProfile: "LocalProfile" + memberContactId: NotRequired[int] # int64 + memberContactProfileId: int # int64 + activeConn: NotRequired["Connection"] + memberChatVRange: "VersionRange" + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + supportChat: NotRequired["GroupSupportChat"] + memberPubKey: NotRequired[str] + relayLink: NotRequired[str] + +class GroupMemberAdmission(TypedDict): + review: NotRequired["MemberCriteria"] + +GroupMemberCategory = Literal["user", "invitee", "host", "pre", "post"] + +class GroupMemberRef(TypedDict): + groupMemberId: int # int64 + profile: "Profile" + +GroupMemberRole = Literal["relay", "observer", "author", "member", "moderator", "admin", "owner"] + +class GroupMemberSettings(TypedDict): + showMessages: bool + +GroupMemberStatus = Literal["rejected", "removed", "left", "deleted", "unknown", "invited", "pending_approval", "pending_review", "introduced", "intro-inv", "accepted", "announced", "connected", "complete", "creator"] + +class GroupPreference(TypedDict): + enable: "GroupFeatureEnabled" + +class GroupPreferences(TypedDict): + timedMessages: NotRequired["TimedMessagesGroupPreference"] + directMessages: NotRequired["RoleGroupPreference"] + fullDelete: NotRequired["GroupPreference"] + reactions: NotRequired["GroupPreference"] + voice: NotRequired["RoleGroupPreference"] + files: NotRequired["RoleGroupPreference"] + simplexLinks: NotRequired["RoleGroupPreference"] + reports: NotRequired["GroupPreference"] + history: NotRequired["GroupPreference"] + support: NotRequired["SupportGroupPreference"] + sessions: NotRequired["RoleGroupPreference"] + comments: NotRequired["CommentsGroupPreference"] + commands: NotRequired[list["ChatBotCommand"]] + +class GroupProfile(TypedDict): + displayName: str + fullName: str + shortDescr: NotRequired[str] + description: NotRequired[str] + image: NotRequired[str] + publicGroup: NotRequired["PublicGroupProfile"] + groupPreferences: NotRequired["GroupPreferences"] + memberAdmission: NotRequired["GroupMemberAdmission"] + +class GroupRelay(TypedDict): + groupRelayId: int # int64 + groupMemberId: int # int64 + userChatRelay: "UserChatRelay" + relayStatus: "RelayStatus" + relayLink: NotRequired[str] + +class GroupRootKey_private(TypedDict): + type: Literal["private"] + rootPrivKey: str + +class GroupRootKey_public(TypedDict): + type: Literal["public"] + rootPubKey: str + +GroupRootKey = GroupRootKey_private | GroupRootKey_public + +GroupRootKey_Tag = Literal["private", "public"] + +class GroupShortLinkData(TypedDict): + groupProfile: "GroupProfile" + publicGroupData: NotRequired["PublicGroupData"] + +class GroupShortLinkInfo(TypedDict): + direct: bool + groupRelays: list[str] + publicGroupId: NotRequired[str] + +class GroupSummary(TypedDict): + currentMembers: int # int64 + publicMemberCount: NotRequired[int] # int64 + +class GroupSupportChat(TypedDict): + chatTs: str # ISO-8601 timestamp + unread: int # int64 + memberAttention: int # int64 + mentions: int # int64 + lastMsgFromMemberTs: NotRequired[str] # ISO-8601 timestamp + +GroupType = Literal["channel", "group"] + +HandshakeError = Literal["PARSE", "IDENTITY", "BAD_AUTH", "BAD_SERVICE"] + +InlineFileMode = Literal["offer", "sent"] + +class InvitationLinkPlan_ok(TypedDict): + type: Literal["ok"] + contactSLinkData_: NotRequired["ContactShortLinkData"] + ownerVerification: NotRequired["OwnerVerification"] + +class InvitationLinkPlan_ownLink(TypedDict): + type: Literal["ownLink"] + +class InvitationLinkPlan_connecting(TypedDict): + type: Literal["connecting"] + contact_: NotRequired["Contact"] + +class InvitationLinkPlan_known(TypedDict): + type: Literal["known"] + contact: "Contact" + +InvitationLinkPlan = ( + InvitationLinkPlan_ok + | InvitationLinkPlan_ownLink + | InvitationLinkPlan_connecting + | InvitationLinkPlan_known +) + +InvitationLinkPlan_Tag = Literal["ok", "ownLink", "connecting", "known"] + +class InvitedBy_contact(TypedDict): + type: Literal["contact"] + byContactId: int # int64 + +class InvitedBy_user(TypedDict): + type: Literal["user"] + +class InvitedBy_unknown(TypedDict): + type: Literal["unknown"] + +InvitedBy = InvitedBy_contact | InvitedBy_user | InvitedBy_unknown + +InvitedBy_Tag = Literal["contact", "user", "unknown"] + +class LinkContent_page(TypedDict): + type: Literal["page"] + +class LinkContent_image(TypedDict): + type: Literal["image"] + +class LinkContent_video(TypedDict): + type: Literal["video"] + duration: NotRequired[int] # int + +class LinkContent_unknown(TypedDict): + type: Literal["unknown"] + tag: str + json: dict[str, object] + +LinkContent = LinkContent_page | LinkContent_image | LinkContent_video | LinkContent_unknown + +LinkContent_Tag = Literal["page", "image", "video", "unknown"] + +class LinkOwnerSig(TypedDict): + ownerId: NotRequired[str] + chatBinding: str + ownerSig: str + +class LinkPreview(TypedDict): + uri: str + title: str + description: str + image: str + content: NotRequired["LinkContent"] + +class LocalProfile(TypedDict): + profileId: int # int64 + displayName: str + fullName: str + shortDescr: NotRequired[str] + image: NotRequired[str] + contactLink: NotRequired[str] + preferences: NotRequired["Preferences"] + peerType: NotRequired["ChatPeerType"] + localAlias: str + +MemberCriteria = Literal["all"] + +# Connection link sent in a message - only short links are allowed. + +class MsgChatLink_contact(TypedDict): + type: Literal["contact"] + connLink: str + profile: "Profile" + business: bool + +class MsgChatLink_invitation(TypedDict): + type: Literal["invitation"] + invLink: str + profile: "Profile" + +class MsgChatLink_group(TypedDict): + type: Literal["group"] + connLink: str + groupProfile: "GroupProfile" + +MsgChatLink = MsgChatLink_contact | MsgChatLink_invitation | MsgChatLink_group + +MsgChatLink_Tag = Literal["contact", "invitation", "group"] + +class MsgContent_text(TypedDict): + type: Literal["text"] + text: str + +class MsgContent_link(TypedDict): + type: Literal["link"] + text: str + preview: "LinkPreview" + +class MsgContent_image(TypedDict): + type: Literal["image"] + text: str + image: str + +class MsgContent_video(TypedDict): + type: Literal["video"] + text: str + image: str + duration: int # int + +class MsgContent_voice(TypedDict): + type: Literal["voice"] + text: str + duration: int # int + +class MsgContent_file(TypedDict): + type: Literal["file"] + text: str + +class MsgContent_report(TypedDict): + type: Literal["report"] + text: str + reason: "ReportReason" + +class MsgContent_chat(TypedDict): + type: Literal["chat"] + text: str + chatLink: "MsgChatLink" + ownerSig: NotRequired["LinkOwnerSig"] + +class MsgContent_unknown(TypedDict): + type: Literal["unknown"] + tag: str + text: str + json: dict[str, object] + +MsgContent = ( + MsgContent_text + | MsgContent_link + | MsgContent_image + | MsgContent_video + | MsgContent_voice + | MsgContent_file + | MsgContent_report + | MsgContent_chat + | MsgContent_unknown +) + +MsgContent_Tag = Literal["text", "link", "image", "video", "voice", "file", "report", "chat", "unknown"] + +MsgDecryptError = Literal["ratchetHeader", "tooManySkipped", "ratchetEarlier", "other", "ratchetSync"] + +MsgDirection = Literal["rcv", "snd"] + +class MsgErrorType_msgSkipped(TypedDict): + type: Literal["msgSkipped"] + fromMsgId: int # int64 + toMsgId: int # int64 + +class MsgErrorType_msgBadId(TypedDict): + type: Literal["msgBadId"] + msgId: int # int64 + +class MsgErrorType_msgBadHash(TypedDict): + type: Literal["msgBadHash"] + +class MsgErrorType_msgDuplicate(TypedDict): + type: Literal["msgDuplicate"] + +MsgErrorType = ( + MsgErrorType_msgSkipped + | MsgErrorType_msgBadId + | MsgErrorType_msgBadHash + | MsgErrorType_msgDuplicate +) + +MsgErrorType_Tag = Literal["msgSkipped", "msgBadId", "msgBadHash", "msgDuplicate"] + +MsgFilter = Literal["none", "all", "mentions"] + +class MsgReaction_emoji(TypedDict): + type: Literal["emoji"] + emoji: str + +class MsgReaction_unknown(TypedDict): + type: Literal["unknown"] + tag: str + json: dict[str, object] + +MsgReaction = MsgReaction_emoji | MsgReaction_unknown + +MsgReaction_Tag = Literal["emoji", "unknown"] + +MsgReceiptStatus = Literal["ok", "badMsgHash"] + +MsgSigStatus = Literal["verified", "signedNoKey"] + +class NetworkError_connectError(TypedDict): + type: Literal["connectError"] + connectError: str + +class NetworkError_tLSError(TypedDict): + type: Literal["tLSError"] + tlsError: str + +class NetworkError_unknownCAError(TypedDict): + type: Literal["unknownCAError"] + +class NetworkError_failedError(TypedDict): + type: Literal["failedError"] + +class NetworkError_timeoutError(TypedDict): + type: Literal["timeoutError"] + +class NetworkError_subscribeError(TypedDict): + type: Literal["subscribeError"] + subscribeError: str + +NetworkError = ( + NetworkError_connectError + | NetworkError_tLSError + | NetworkError_unknownCAError + | NetworkError_failedError + | NetworkError_timeoutError + | NetworkError_subscribeError +) + +NetworkError_Tag = Literal["connectError", "tLSError", "unknownCAError", "failedError", "timeoutError", "subscribeError"] + +class NewUser(TypedDict): + profile: NotRequired["Profile"] + pastTimestamp: bool + userChatRelay: bool + +class NoteFolder(TypedDict): + noteFolderId: int # int64 + userId: int # int64 + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + chatTs: str # ISO-8601 timestamp + favorite: bool + unread: bool + +class OwnerVerification_verified(TypedDict): + type: Literal["verified"] + +class OwnerVerification_failed(TypedDict): + type: Literal["failed"] + reason: str + +OwnerVerification = OwnerVerification_verified | OwnerVerification_failed + +OwnerVerification_Tag = Literal["verified", "failed"] + +class PaginationByTime_last(TypedDict): + type: Literal["last"] + count: int # int + +PaginationByTime = PaginationByTime_last + +PaginationByTime_Tag = Literal["last"] + + +def PaginationByTime_cmd_string(self: PaginationByTime) -> str: + return 'count=' + str(self['count']) # type: ignore[typeddict-item] + +class PendingContactConnection(TypedDict): + pccConnId: int # int64 + pccAgentConnId: str + pccConnStatus: "ConnStatus" + viaContactUri: bool + viaUserContactLink: NotRequired[int] # int64 + groupLinkId: NotRequired[str] + customUserProfileId: NotRequired[int] # int64 + connLinkInv: NotRequired["CreatedConnLink"] + localAlias: str + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + +class PrefEnabled(TypedDict): + forUser: bool + forContact: bool + +class Preferences(TypedDict): + timedMessages: NotRequired["TimedMessagesPreference"] + fullDelete: NotRequired["SimplePreference"] + reactions: NotRequired["SimplePreference"] + voice: NotRequired["SimplePreference"] + files: NotRequired["SimplePreference"] + calls: NotRequired["SimplePreference"] + sessions: NotRequired["SimplePreference"] + commands: NotRequired[list["ChatBotCommand"]] + +class PreparedContact(TypedDict): + connLinkToConnect: "CreatedConnLink" + uiConnLinkType: "ConnectionMode" + welcomeSharedMsgId: NotRequired[str] + requestSharedMsgId: NotRequired[str] + +class PreparedGroup(TypedDict): + connLinkToConnect: "CreatedConnLink" + connLinkPreparedConnection: bool + connLinkStartedConnection: bool + welcomeSharedMsgId: NotRequired[str] + requestSharedMsgId: NotRequired[str] + +class Profile(TypedDict): + displayName: str + fullName: str + shortDescr: NotRequired[str] + image: NotRequired[str] + contactLink: NotRequired[str] + preferences: NotRequired["Preferences"] + peerType: NotRequired["ChatPeerType"] + +class ProxyClientError_protocolError(TypedDict): + type: Literal["protocolError"] + protocolErr: "ErrorType" + +class ProxyClientError_unexpectedResponse(TypedDict): + type: Literal["unexpectedResponse"] + responseStr: str + +class ProxyClientError_responseError(TypedDict): + type: Literal["responseError"] + responseErr: "ErrorType" + +ProxyClientError = ( + ProxyClientError_protocolError + | ProxyClientError_unexpectedResponse + | ProxyClientError_responseError +) + +ProxyClientError_Tag = Literal["protocolError", "unexpectedResponse", "responseError"] + +class ProxyError_PROTOCOL(TypedDict): + type: Literal["PROTOCOL"] + protocolErr: "ErrorType" + +class ProxyError_BROKER(TypedDict): + type: Literal["BROKER"] + brokerErr: "BrokerErrorType" + +class ProxyError_BASIC_AUTH(TypedDict): + type: Literal["BASIC_AUTH"] + +class ProxyError_NO_SESSION(TypedDict): + type: Literal["NO_SESSION"] + +ProxyError = ProxyError_PROTOCOL | ProxyError_BROKER | ProxyError_BASIC_AUTH | ProxyError_NO_SESSION + +ProxyError_Tag = Literal["PROTOCOL", "BROKER", "BASIC_AUTH", "NO_SESSION"] + +class PublicGroupData(TypedDict): + publicMemberCount: int # int64 + +class PublicGroupProfile(TypedDict): + groupType: "GroupType" + groupLink: str + publicGroupId: str + +class RCErrorType_internal(TypedDict): + type: Literal["internal"] + internalErr: str + +class RCErrorType_identity(TypedDict): + type: Literal["identity"] + +class RCErrorType_noLocalAddress(TypedDict): + type: Literal["noLocalAddress"] + +class RCErrorType_newController(TypedDict): + type: Literal["newController"] + +class RCErrorType_notDiscovered(TypedDict): + type: Literal["notDiscovered"] + +class RCErrorType_tLSStartFailed(TypedDict): + type: Literal["tLSStartFailed"] + +class RCErrorType_exception(TypedDict): + type: Literal["exception"] + exception: str + +class RCErrorType_ctrlAuth(TypedDict): + type: Literal["ctrlAuth"] + +class RCErrorType_ctrlNotFound(TypedDict): + type: Literal["ctrlNotFound"] + +class RCErrorType_ctrlError(TypedDict): + type: Literal["ctrlError"] + ctrlErr: str + +class RCErrorType_invitation(TypedDict): + type: Literal["invitation"] + +class RCErrorType_version(TypedDict): + type: Literal["version"] + +class RCErrorType_encrypt(TypedDict): + type: Literal["encrypt"] + +class RCErrorType_decrypt(TypedDict): + type: Literal["decrypt"] + +class RCErrorType_blockSize(TypedDict): + type: Literal["blockSize"] + +class RCErrorType_syntax(TypedDict): + type: Literal["syntax"] + syntaxErr: str + +RCErrorType = ( + RCErrorType_internal + | RCErrorType_identity + | RCErrorType_noLocalAddress + | RCErrorType_newController + | RCErrorType_notDiscovered + | RCErrorType_tLSStartFailed + | RCErrorType_exception + | RCErrorType_ctrlAuth + | RCErrorType_ctrlNotFound + | RCErrorType_ctrlError + | RCErrorType_invitation + | RCErrorType_version + | RCErrorType_encrypt + | RCErrorType_decrypt + | RCErrorType_blockSize + | RCErrorType_syntax +) + +RCErrorType_Tag = Literal["internal", "identity", "noLocalAddress", "newController", "notDiscovered", "tLSStartFailed", "exception", "ctrlAuth", "ctrlNotFound", "ctrlError", "invitation", "version", "encrypt", "decrypt", "blockSize", "syntax"] + +RatchetSyncState = Literal["ok", "allowed", "required", "started", "agreed"] + +class RcvConnEvent_switchQueue(TypedDict): + type: Literal["switchQueue"] + phase: "SwitchPhase" + +class RcvConnEvent_ratchetSync(TypedDict): + type: Literal["ratchetSync"] + syncStatus: "RatchetSyncState" + +class RcvConnEvent_verificationCodeReset(TypedDict): + type: Literal["verificationCodeReset"] + +class RcvConnEvent_pqEnabled(TypedDict): + type: Literal["pqEnabled"] + enabled: bool + +RcvConnEvent = ( + RcvConnEvent_switchQueue + | RcvConnEvent_ratchetSync + | RcvConnEvent_verificationCodeReset + | RcvConnEvent_pqEnabled +) + +RcvConnEvent_Tag = Literal["switchQueue", "ratchetSync", "verificationCodeReset", "pqEnabled"] + +class RcvDirectEvent_contactDeleted(TypedDict): + type: Literal["contactDeleted"] + +class RcvDirectEvent_profileUpdated(TypedDict): + type: Literal["profileUpdated"] + fromProfile: "Profile" + toProfile: "Profile" + +class RcvDirectEvent_groupInvLinkReceived(TypedDict): + type: Literal["groupInvLinkReceived"] + groupProfile: "GroupProfile" + +RcvDirectEvent = ( + RcvDirectEvent_contactDeleted + | RcvDirectEvent_profileUpdated + | RcvDirectEvent_groupInvLinkReceived +) + +RcvDirectEvent_Tag = Literal["contactDeleted", "profileUpdated", "groupInvLinkReceived"] + +class RcvFileDescr(TypedDict): + fileDescrId: int # int64 + fileDescrText: str + fileDescrPartNo: int # int + fileDescrComplete: bool + +class RcvFileStatus_new(TypedDict): + type: Literal["new"] + +class RcvFileStatus_accepted(TypedDict): + type: Literal["accepted"] + filePath: str + +class RcvFileStatus_connected(TypedDict): + type: Literal["connected"] + filePath: str + +class RcvFileStatus_complete(TypedDict): + type: Literal["complete"] + filePath: str + +class RcvFileStatus_cancelled(TypedDict): + type: Literal["cancelled"] + filePath_: NotRequired[str] + +RcvFileStatus = ( + RcvFileStatus_new + | RcvFileStatus_accepted + | RcvFileStatus_connected + | RcvFileStatus_complete + | RcvFileStatus_cancelled +) + +RcvFileStatus_Tag = Literal["new", "accepted", "connected", "complete", "cancelled"] + +class RcvFileTransfer(TypedDict): + fileId: int # int64 + xftpRcvFile: NotRequired["XFTPRcvFile"] + fileInvitation: "FileInvitation" + fileStatus: "RcvFileStatus" + rcvFileInline: NotRequired["InlineFileMode"] + senderDisplayName: str + chunkSize: int # int64 + cancelled: bool + grpMemberId: NotRequired[int] # int64 + cryptoArgs: NotRequired["CryptoFileArgs"] + +class RcvGroupEvent_memberAdded(TypedDict): + type: Literal["memberAdded"] + groupMemberId: int # int64 + profile: "Profile" + +class RcvGroupEvent_memberConnected(TypedDict): + type: Literal["memberConnected"] + +class RcvGroupEvent_memberAccepted(TypedDict): + type: Literal["memberAccepted"] + groupMemberId: int # int64 + profile: "Profile" + +class RcvGroupEvent_userAccepted(TypedDict): + type: Literal["userAccepted"] + +class RcvGroupEvent_memberLeft(TypedDict): + type: Literal["memberLeft"] + +class RcvGroupEvent_memberRole(TypedDict): + type: Literal["memberRole"] + groupMemberId: int # int64 + profile: "Profile" + role: "GroupMemberRole" + +class RcvGroupEvent_memberBlocked(TypedDict): + type: Literal["memberBlocked"] + groupMemberId: int # int64 + profile: "Profile" + blocked: bool + +class RcvGroupEvent_userRole(TypedDict): + type: Literal["userRole"] + role: "GroupMemberRole" + +class RcvGroupEvent_memberDeleted(TypedDict): + type: Literal["memberDeleted"] + groupMemberId: int # int64 + profile: "Profile" + +class RcvGroupEvent_userDeleted(TypedDict): + type: Literal["userDeleted"] + +class RcvGroupEvent_groupDeleted(TypedDict): + type: Literal["groupDeleted"] + +class RcvGroupEvent_groupUpdated(TypedDict): + type: Literal["groupUpdated"] + groupProfile: "GroupProfile" + +class RcvGroupEvent_invitedViaGroupLink(TypedDict): + type: Literal["invitedViaGroupLink"] + +class RcvGroupEvent_memberCreatedContact(TypedDict): + type: Literal["memberCreatedContact"] + +class RcvGroupEvent_memberProfileUpdated(TypedDict): + type: Literal["memberProfileUpdated"] + fromProfile: "Profile" + toProfile: "Profile" + +class RcvGroupEvent_newMemberPendingReview(TypedDict): + type: Literal["newMemberPendingReview"] + +class RcvGroupEvent_msgBadSignature(TypedDict): + type: Literal["msgBadSignature"] + +RcvGroupEvent = ( + RcvGroupEvent_memberAdded + | RcvGroupEvent_memberConnected + | RcvGroupEvent_memberAccepted + | RcvGroupEvent_userAccepted + | RcvGroupEvent_memberLeft + | RcvGroupEvent_memberRole + | RcvGroupEvent_memberBlocked + | RcvGroupEvent_userRole + | RcvGroupEvent_memberDeleted + | RcvGroupEvent_userDeleted + | RcvGroupEvent_groupDeleted + | RcvGroupEvent_groupUpdated + | RcvGroupEvent_invitedViaGroupLink + | RcvGroupEvent_memberCreatedContact + | RcvGroupEvent_memberProfileUpdated + | RcvGroupEvent_newMemberPendingReview + | RcvGroupEvent_msgBadSignature +) + +RcvGroupEvent_Tag = Literal["memberAdded", "memberConnected", "memberAccepted", "userAccepted", "memberLeft", "memberRole", "memberBlocked", "userRole", "memberDeleted", "userDeleted", "groupDeleted", "groupUpdated", "invitedViaGroupLink", "memberCreatedContact", "memberProfileUpdated", "newMemberPendingReview", "msgBadSignature"] + +class RcvMsgError_dropped(TypedDict): + type: Literal["dropped"] + attempts: int # int + +class RcvMsgError_parseError(TypedDict): + type: Literal["parseError"] + parseError: str + +RcvMsgError = RcvMsgError_dropped | RcvMsgError_parseError + +RcvMsgError_Tag = Literal["dropped", "parseError"] + +class RelayProfile(TypedDict): + displayName: str + fullName: str + shortDescr: NotRequired[str] + image: NotRequired[str] + +RelayStatus = Literal["new", "invited", "accepted", "active", "inactive"] + +ReportReason = Literal["spam", "content", "community", "profile", "other"] + +class RoleGroupPreference(TypedDict): + enable: "GroupFeatureEnabled" + role: NotRequired["GroupMemberRole"] + +class SMPAgentError_A_MESSAGE(TypedDict): + type: Literal["A_MESSAGE"] + +class SMPAgentError_A_PROHIBITED(TypedDict): + type: Literal["A_PROHIBITED"] + prohibitedErr: str + +class SMPAgentError_A_VERSION(TypedDict): + type: Literal["A_VERSION"] + +class SMPAgentError_A_LINK(TypedDict): + type: Literal["A_LINK"] + linkErr: str + +class SMPAgentError_A_CRYPTO(TypedDict): + type: Literal["A_CRYPTO"] + cryptoErr: "AgentCryptoError" + +class SMPAgentError_A_DUPLICATE(TypedDict): + type: Literal["A_DUPLICATE"] + droppedMsg_: NotRequired["DroppedMsg"] + +class SMPAgentError_A_QUEUE(TypedDict): + type: Literal["A_QUEUE"] + queueErr: str + +SMPAgentError = ( + SMPAgentError_A_MESSAGE + | SMPAgentError_A_PROHIBITED + | SMPAgentError_A_VERSION + | SMPAgentError_A_LINK + | SMPAgentError_A_CRYPTO + | SMPAgentError_A_DUPLICATE + | SMPAgentError_A_QUEUE +) + +SMPAgentError_Tag = Literal["A_MESSAGE", "A_PROHIBITED", "A_VERSION", "A_LINK", "A_CRYPTO", "A_DUPLICATE", "A_QUEUE"] + +class SecurityCode(TypedDict): + securityCode: str + verifiedAt: str # ISO-8601 timestamp + +class SimplePreference(TypedDict): + allow: "FeatureAllowed" + +SimplexLinkType = Literal["contact", "invitation", "group", "channel", "relay"] + +SndCIStatusProgress = Literal["partial", "complete"] + +class SndConnEvent_switchQueue(TypedDict): + type: Literal["switchQueue"] + phase: "SwitchPhase" + member: NotRequired["GroupMemberRef"] + +class SndConnEvent_ratchetSync(TypedDict): + type: Literal["ratchetSync"] + syncStatus: "RatchetSyncState" + member: NotRequired["GroupMemberRef"] + +class SndConnEvent_pqEnabled(TypedDict): + type: Literal["pqEnabled"] + enabled: bool + +SndConnEvent = SndConnEvent_switchQueue | SndConnEvent_ratchetSync | SndConnEvent_pqEnabled + +SndConnEvent_Tag = Literal["switchQueue", "ratchetSync", "pqEnabled"] + +class SndError_auth(TypedDict): + type: Literal["auth"] + +class SndError_quota(TypedDict): + type: Literal["quota"] + +class SndError_expired(TypedDict): + type: Literal["expired"] + +class SndError_relay(TypedDict): + type: Literal["relay"] + srvError: "SrvError" + +class SndError_proxy(TypedDict): + type: Literal["proxy"] + proxyServer: str + srvError: "SrvError" + +class SndError_proxyRelay(TypedDict): + type: Literal["proxyRelay"] + proxyServer: str + srvError: "SrvError" + +class SndError_other(TypedDict): + type: Literal["other"] + sndError: str + +SndError = ( + SndError_auth + | SndError_quota + | SndError_expired + | SndError_relay + | SndError_proxy + | SndError_proxyRelay + | SndError_other +) + +SndError_Tag = Literal["auth", "quota", "expired", "relay", "proxy", "proxyRelay", "other"] + +class SndFileTransfer(TypedDict): + fileId: int # int64 + fileName: str + filePath: str + fileSize: int # int64 + chunkSize: int # int64 + recipientDisplayName: str + connId: int # int64 + agentConnId: str + groupMemberId: NotRequired[int] # int64 + fileStatus: "FileStatus" + fileDescrId: NotRequired[int] # int64 + fileInline: NotRequired["InlineFileMode"] + +class SndGroupEvent_memberRole(TypedDict): + type: Literal["memberRole"] + groupMemberId: int # int64 + profile: "Profile" + role: "GroupMemberRole" + +class SndGroupEvent_memberBlocked(TypedDict): + type: Literal["memberBlocked"] + groupMemberId: int # int64 + profile: "Profile" + blocked: bool + +class SndGroupEvent_userRole(TypedDict): + type: Literal["userRole"] + role: "GroupMemberRole" + +class SndGroupEvent_memberDeleted(TypedDict): + type: Literal["memberDeleted"] + groupMemberId: int # int64 + profile: "Profile" + +class SndGroupEvent_userLeft(TypedDict): + type: Literal["userLeft"] + +class SndGroupEvent_groupUpdated(TypedDict): + type: Literal["groupUpdated"] + groupProfile: "GroupProfile" + +class SndGroupEvent_memberAccepted(TypedDict): + type: Literal["memberAccepted"] + groupMemberId: int # int64 + profile: "Profile" + +class SndGroupEvent_userPendingReview(TypedDict): + type: Literal["userPendingReview"] + +SndGroupEvent = ( + SndGroupEvent_memberRole + | SndGroupEvent_memberBlocked + | SndGroupEvent_userRole + | SndGroupEvent_memberDeleted + | SndGroupEvent_userLeft + | SndGroupEvent_groupUpdated + | SndGroupEvent_memberAccepted + | SndGroupEvent_userPendingReview +) + +SndGroupEvent_Tag = Literal["memberRole", "memberBlocked", "userRole", "memberDeleted", "userLeft", "groupUpdated", "memberAccepted", "userPendingReview"] + +class SrvError_host(TypedDict): + type: Literal["host"] + +class SrvError_version(TypedDict): + type: Literal["version"] + +class SrvError_other(TypedDict): + type: Literal["other"] + srvError: str + +SrvError = SrvError_host | SrvError_version | SrvError_other + +SrvError_Tag = Literal["host", "version", "other"] + +class StoreError_duplicateName(TypedDict): + type: Literal["duplicateName"] + +class StoreError_userNotFound(TypedDict): + type: Literal["userNotFound"] + userId: int # int64 + +class StoreError_relayUserNotFound(TypedDict): + type: Literal["relayUserNotFound"] + +class StoreError_userNotFoundByName(TypedDict): + type: Literal["userNotFoundByName"] + contactName: str + +class StoreError_userNotFoundByContactId(TypedDict): + type: Literal["userNotFoundByContactId"] + contactId: int # int64 + +class StoreError_userNotFoundByGroupId(TypedDict): + type: Literal["userNotFoundByGroupId"] + groupId: int # int64 + +class StoreError_userNotFoundByFileId(TypedDict): + type: Literal["userNotFoundByFileId"] + fileId: int # int64 + +class StoreError_userNotFoundByContactRequestId(TypedDict): + type: Literal["userNotFoundByContactRequestId"] + contactRequestId: int # int64 + +class StoreError_contactNotFound(TypedDict): + type: Literal["contactNotFound"] + contactId: int # int64 + +class StoreError_contactNotFoundByName(TypedDict): + type: Literal["contactNotFoundByName"] + contactName: str + +class StoreError_contactNotFoundByMemberId(TypedDict): + type: Literal["contactNotFoundByMemberId"] + groupMemberId: int # int64 + +class StoreError_contactNotReady(TypedDict): + type: Literal["contactNotReady"] + contactName: str + +class StoreError_duplicateContactLink(TypedDict): + type: Literal["duplicateContactLink"] + +class StoreError_userContactLinkNotFound(TypedDict): + type: Literal["userContactLinkNotFound"] + +class StoreError_contactRequestNotFound(TypedDict): + type: Literal["contactRequestNotFound"] + contactRequestId: int # int64 + +class StoreError_contactRequestNotFoundByName(TypedDict): + type: Literal["contactRequestNotFoundByName"] + contactName: str + +class StoreError_invalidContactRequestEntity(TypedDict): + type: Literal["invalidContactRequestEntity"] + contactRequestId: int # int64 + +class StoreError_invalidBusinessChatContactRequest(TypedDict): + type: Literal["invalidBusinessChatContactRequest"] + +class StoreError_groupNotFound(TypedDict): + type: Literal["groupNotFound"] + groupId: int # int64 + +class StoreError_groupNotFoundByName(TypedDict): + type: Literal["groupNotFoundByName"] + groupName: str + +class StoreError_groupMemberNameNotFound(TypedDict): + type: Literal["groupMemberNameNotFound"] + groupId: int # int64 + groupMemberName: str + +class StoreError_groupMemberNotFound(TypedDict): + type: Literal["groupMemberNotFound"] + groupMemberId: int # int64 + +class StoreError_groupMemberNotFoundByIndex(TypedDict): + type: Literal["groupMemberNotFoundByIndex"] + groupMemberIndex: int # int64 + +class StoreError_memberRelationsVectorNotFound(TypedDict): + type: Literal["memberRelationsVectorNotFound"] + groupMemberId: int # int64 + +class StoreError_groupHostMemberNotFound(TypedDict): + type: Literal["groupHostMemberNotFound"] + groupId: int # int64 + +class StoreError_groupMemberNotFoundByMemberId(TypedDict): + type: Literal["groupMemberNotFoundByMemberId"] + memberId: str + +class StoreError_memberContactGroupMemberNotFound(TypedDict): + type: Literal["memberContactGroupMemberNotFound"] + contactId: int # int64 + +class StoreError_invalidMemberRelationUpdate(TypedDict): + type: Literal["invalidMemberRelationUpdate"] + +class StoreError_groupWithoutUser(TypedDict): + type: Literal["groupWithoutUser"] + +class StoreError_duplicateGroupMember(TypedDict): + type: Literal["duplicateGroupMember"] + +class StoreError_duplicateMemberId(TypedDict): + type: Literal["duplicateMemberId"] + +class StoreError_groupAlreadyJoined(TypedDict): + type: Literal["groupAlreadyJoined"] + +class StoreError_groupInvitationNotFound(TypedDict): + type: Literal["groupInvitationNotFound"] + +class StoreError_noteFolderAlreadyExists(TypedDict): + type: Literal["noteFolderAlreadyExists"] + noteFolderId: int # int64 + +class StoreError_noteFolderNotFound(TypedDict): + type: Literal["noteFolderNotFound"] + noteFolderId: int # int64 + +class StoreError_userNoteFolderNotFound(TypedDict): + type: Literal["userNoteFolderNotFound"] + +class StoreError_sndFileNotFound(TypedDict): + type: Literal["sndFileNotFound"] + fileId: int # int64 + +class StoreError_sndFileInvalid(TypedDict): + type: Literal["sndFileInvalid"] + fileId: int # int64 + +class StoreError_rcvFileNotFound(TypedDict): + type: Literal["rcvFileNotFound"] + fileId: int # int64 + +class StoreError_rcvFileDescrNotFound(TypedDict): + type: Literal["rcvFileDescrNotFound"] + fileId: int # int64 + +class StoreError_fileNotFound(TypedDict): + type: Literal["fileNotFound"] + fileId: int # int64 + +class StoreError_rcvFileInvalid(TypedDict): + type: Literal["rcvFileInvalid"] + fileId: int # int64 + +class StoreError_rcvFileInvalidDescrPart(TypedDict): + type: Literal["rcvFileInvalidDescrPart"] + +class StoreError_localFileNoTransfer(TypedDict): + type: Literal["localFileNoTransfer"] + fileId: int # int64 + +class StoreError_sharedMsgIdNotFoundByFileId(TypedDict): + type: Literal["sharedMsgIdNotFoundByFileId"] + fileId: int # int64 + +class StoreError_fileIdNotFoundBySharedMsgId(TypedDict): + type: Literal["fileIdNotFoundBySharedMsgId"] + sharedMsgId: str + +class StoreError_sndFileNotFoundXFTP(TypedDict): + type: Literal["sndFileNotFoundXFTP"] + agentSndFileId: str + +class StoreError_rcvFileNotFoundXFTP(TypedDict): + type: Literal["rcvFileNotFoundXFTP"] + agentRcvFileId: str + +class StoreError_connectionNotFound(TypedDict): + type: Literal["connectionNotFound"] + agentConnId: str + +class StoreError_connectionNotFoundById(TypedDict): + type: Literal["connectionNotFoundById"] + connId: int # int64 + +class StoreError_connectionNotFoundByMemberId(TypedDict): + type: Literal["connectionNotFoundByMemberId"] + groupMemberId: int # int64 + +class StoreError_pendingConnectionNotFound(TypedDict): + type: Literal["pendingConnectionNotFound"] + connId: int # int64 + +class StoreError_uniqueID(TypedDict): + type: Literal["uniqueID"] + +class StoreError_largeMsg(TypedDict): + type: Literal["largeMsg"] + +class StoreError_internalError(TypedDict): + type: Literal["internalError"] + message: str + +class StoreError_dBException(TypedDict): + type: Literal["dBException"] + message: str + +class StoreError_dBBusyError(TypedDict): + type: Literal["dBBusyError"] + message: str + +class StoreError_badChatItem(TypedDict): + type: Literal["badChatItem"] + itemId: int # int64 + itemTs: NotRequired[str] # ISO-8601 timestamp + +class StoreError_chatItemNotFound(TypedDict): + type: Literal["chatItemNotFound"] + itemId: int # int64 + +class StoreError_chatItemNotFoundByText(TypedDict): + type: Literal["chatItemNotFoundByText"] + text: str + +class StoreError_chatItemSharedMsgIdNotFound(TypedDict): + type: Literal["chatItemSharedMsgIdNotFound"] + sharedMsgId: str + +class StoreError_chatItemNotFoundByFileId(TypedDict): + type: Literal["chatItemNotFoundByFileId"] + fileId: int # int64 + +class StoreError_chatItemNotFoundByContactId(TypedDict): + type: Literal["chatItemNotFoundByContactId"] + contactId: int # int64 + +class StoreError_chatItemNotFoundByGroupId(TypedDict): + type: Literal["chatItemNotFoundByGroupId"] + groupId: int # int64 + +class StoreError_profileNotFound(TypedDict): + type: Literal["profileNotFound"] + profileId: int # int64 + +class StoreError_duplicateGroupLink(TypedDict): + type: Literal["duplicateGroupLink"] + groupInfo: "GroupInfo" + +class StoreError_groupLinkNotFound(TypedDict): + type: Literal["groupLinkNotFound"] + groupInfo: "GroupInfo" + +class StoreError_hostMemberIdNotFound(TypedDict): + type: Literal["hostMemberIdNotFound"] + groupId: int # int64 + +class StoreError_contactNotFoundByFileId(TypedDict): + type: Literal["contactNotFoundByFileId"] + fileId: int # int64 + +class StoreError_noGroupSndStatus(TypedDict): + type: Literal["noGroupSndStatus"] + itemId: int # int64 + groupMemberId: int # int64 + +class StoreError_duplicateGroupMessage(TypedDict): + type: Literal["duplicateGroupMessage"] + groupId: int # int64 + sharedMsgId: str + authorGroupMemberId: NotRequired[int] # int64 + forwardedByGroupMemberId: NotRequired[int] # int64 + +class StoreError_remoteHostNotFound(TypedDict): + type: Literal["remoteHostNotFound"] + remoteHostId: int # int64 + +class StoreError_remoteHostUnknown(TypedDict): + type: Literal["remoteHostUnknown"] + +class StoreError_remoteHostDuplicateCA(TypedDict): + type: Literal["remoteHostDuplicateCA"] + +class StoreError_remoteCtrlNotFound(TypedDict): + type: Literal["remoteCtrlNotFound"] + remoteCtrlId: int # int64 + +class StoreError_remoteCtrlDuplicateCA(TypedDict): + type: Literal["remoteCtrlDuplicateCA"] + +class StoreError_prohibitedDeleteUser(TypedDict): + type: Literal["prohibitedDeleteUser"] + userId: int # int64 + contactId: int # int64 + +class StoreError_operatorNotFound(TypedDict): + type: Literal["operatorNotFound"] + serverOperatorId: int # int64 + +class StoreError_usageConditionsNotFound(TypedDict): + type: Literal["usageConditionsNotFound"] + +class StoreError_userChatRelayNotFound(TypedDict): + type: Literal["userChatRelayNotFound"] + chatRelayId: int # int64 + +class StoreError_groupRelayNotFound(TypedDict): + type: Literal["groupRelayNotFound"] + groupRelayId: int # int64 + +class StoreError_groupRelayNotFoundByMemberId(TypedDict): + type: Literal["groupRelayNotFoundByMemberId"] + groupMemberId: int # int64 + +class StoreError_invalidQuote(TypedDict): + type: Literal["invalidQuote"] + +class StoreError_invalidMention(TypedDict): + type: Literal["invalidMention"] + +class StoreError_invalidDeliveryTask(TypedDict): + type: Literal["invalidDeliveryTask"] + taskId: int # int64 + +class StoreError_deliveryTaskNotFound(TypedDict): + type: Literal["deliveryTaskNotFound"] + taskId: int # int64 + +class StoreError_invalidDeliveryJob(TypedDict): + type: Literal["invalidDeliveryJob"] + jobId: int # int64 + +class StoreError_deliveryJobNotFound(TypedDict): + type: Literal["deliveryJobNotFound"] + jobId: int # int64 + +class StoreError_workItemError(TypedDict): + type: Literal["workItemError"] + errContext: str + +StoreError = ( + StoreError_duplicateName + | StoreError_userNotFound + | StoreError_relayUserNotFound + | StoreError_userNotFoundByName + | StoreError_userNotFoundByContactId + | StoreError_userNotFoundByGroupId + | StoreError_userNotFoundByFileId + | StoreError_userNotFoundByContactRequestId + | StoreError_contactNotFound + | StoreError_contactNotFoundByName + | StoreError_contactNotFoundByMemberId + | StoreError_contactNotReady + | StoreError_duplicateContactLink + | StoreError_userContactLinkNotFound + | StoreError_contactRequestNotFound + | StoreError_contactRequestNotFoundByName + | StoreError_invalidContactRequestEntity + | StoreError_invalidBusinessChatContactRequest + | StoreError_groupNotFound + | StoreError_groupNotFoundByName + | StoreError_groupMemberNameNotFound + | StoreError_groupMemberNotFound + | StoreError_groupMemberNotFoundByIndex + | StoreError_memberRelationsVectorNotFound + | StoreError_groupHostMemberNotFound + | StoreError_groupMemberNotFoundByMemberId + | StoreError_memberContactGroupMemberNotFound + | StoreError_invalidMemberRelationUpdate + | StoreError_groupWithoutUser + | StoreError_duplicateGroupMember + | StoreError_duplicateMemberId + | StoreError_groupAlreadyJoined + | StoreError_groupInvitationNotFound + | StoreError_noteFolderAlreadyExists + | StoreError_noteFolderNotFound + | StoreError_userNoteFolderNotFound + | StoreError_sndFileNotFound + | StoreError_sndFileInvalid + | StoreError_rcvFileNotFound + | StoreError_rcvFileDescrNotFound + | StoreError_fileNotFound + | StoreError_rcvFileInvalid + | StoreError_rcvFileInvalidDescrPart + | StoreError_localFileNoTransfer + | StoreError_sharedMsgIdNotFoundByFileId + | StoreError_fileIdNotFoundBySharedMsgId + | StoreError_sndFileNotFoundXFTP + | StoreError_rcvFileNotFoundXFTP + | StoreError_connectionNotFound + | StoreError_connectionNotFoundById + | StoreError_connectionNotFoundByMemberId + | StoreError_pendingConnectionNotFound + | StoreError_uniqueID + | StoreError_largeMsg + | StoreError_internalError + | StoreError_dBException + | StoreError_dBBusyError + | StoreError_badChatItem + | StoreError_chatItemNotFound + | StoreError_chatItemNotFoundByText + | StoreError_chatItemSharedMsgIdNotFound + | StoreError_chatItemNotFoundByFileId + | StoreError_chatItemNotFoundByContactId + | StoreError_chatItemNotFoundByGroupId + | StoreError_profileNotFound + | StoreError_duplicateGroupLink + | StoreError_groupLinkNotFound + | StoreError_hostMemberIdNotFound + | StoreError_contactNotFoundByFileId + | StoreError_noGroupSndStatus + | StoreError_duplicateGroupMessage + | StoreError_remoteHostNotFound + | StoreError_remoteHostUnknown + | StoreError_remoteHostDuplicateCA + | StoreError_remoteCtrlNotFound + | StoreError_remoteCtrlDuplicateCA + | StoreError_prohibitedDeleteUser + | StoreError_operatorNotFound + | StoreError_usageConditionsNotFound + | StoreError_userChatRelayNotFound + | StoreError_groupRelayNotFound + | StoreError_groupRelayNotFoundByMemberId + | StoreError_invalidQuote + | StoreError_invalidMention + | StoreError_invalidDeliveryTask + | StoreError_deliveryTaskNotFound + | StoreError_invalidDeliveryJob + | StoreError_deliveryJobNotFound + | StoreError_workItemError +) + +StoreError_Tag = Literal["duplicateName", "userNotFound", "relayUserNotFound", "userNotFoundByName", "userNotFoundByContactId", "userNotFoundByGroupId", "userNotFoundByFileId", "userNotFoundByContactRequestId", "contactNotFound", "contactNotFoundByName", "contactNotFoundByMemberId", "contactNotReady", "duplicateContactLink", "userContactLinkNotFound", "contactRequestNotFound", "contactRequestNotFoundByName", "invalidContactRequestEntity", "invalidBusinessChatContactRequest", "groupNotFound", "groupNotFoundByName", "groupMemberNameNotFound", "groupMemberNotFound", "groupMemberNotFoundByIndex", "memberRelationsVectorNotFound", "groupHostMemberNotFound", "groupMemberNotFoundByMemberId", "memberContactGroupMemberNotFound", "invalidMemberRelationUpdate", "groupWithoutUser", "duplicateGroupMember", "duplicateMemberId", "groupAlreadyJoined", "groupInvitationNotFound", "noteFolderAlreadyExists", "noteFolderNotFound", "userNoteFolderNotFound", "sndFileNotFound", "sndFileInvalid", "rcvFileNotFound", "rcvFileDescrNotFound", "fileNotFound", "rcvFileInvalid", "rcvFileInvalidDescrPart", "localFileNoTransfer", "sharedMsgIdNotFoundByFileId", "fileIdNotFoundBySharedMsgId", "sndFileNotFoundXFTP", "rcvFileNotFoundXFTP", "connectionNotFound", "connectionNotFoundById", "connectionNotFoundByMemberId", "pendingConnectionNotFound", "uniqueID", "largeMsg", "internalError", "dBException", "dBBusyError", "badChatItem", "chatItemNotFound", "chatItemNotFoundByText", "chatItemSharedMsgIdNotFound", "chatItemNotFoundByFileId", "chatItemNotFoundByContactId", "chatItemNotFoundByGroupId", "profileNotFound", "duplicateGroupLink", "groupLinkNotFound", "hostMemberIdNotFound", "contactNotFoundByFileId", "noGroupSndStatus", "duplicateGroupMessage", "remoteHostNotFound", "remoteHostUnknown", "remoteHostDuplicateCA", "remoteCtrlNotFound", "remoteCtrlDuplicateCA", "prohibitedDeleteUser", "operatorNotFound", "usageConditionsNotFound", "userChatRelayNotFound", "groupRelayNotFound", "groupRelayNotFoundByMemberId", "invalidQuote", "invalidMention", "invalidDeliveryTask", "deliveryTaskNotFound", "invalidDeliveryJob", "deliveryJobNotFound", "workItemError"] + +class SubscriptionStatus_active(TypedDict): + type: Literal["active"] + +class SubscriptionStatus_pending(TypedDict): + type: Literal["pending"] + +class SubscriptionStatus_removed(TypedDict): + type: Literal["removed"] + subError: str + +class SubscriptionStatus_noSub(TypedDict): + type: Literal["noSub"] + +SubscriptionStatus = ( + SubscriptionStatus_active + | SubscriptionStatus_pending + | SubscriptionStatus_removed + | SubscriptionStatus_noSub +) + +SubscriptionStatus_Tag = Literal["active", "pending", "removed", "noSub"] + +class SupportGroupPreference(TypedDict): + enable: "GroupFeatureEnabled" + +SwitchPhase = Literal["started", "confirmed", "secured", "completed"] + +class TimedMessagesGroupPreference(TypedDict): + enable: "GroupFeatureEnabled" + ttl: NotRequired[int] # int + +class TimedMessagesPreference(TypedDict): + allow: "FeatureAllowed" + ttl: NotRequired[int] # int + +class TransportError_badBlock(TypedDict): + type: Literal["badBlock"] + +class TransportError_version(TypedDict): + type: Literal["version"] + +class TransportError_largeMsg(TypedDict): + type: Literal["largeMsg"] + +class TransportError_badSession(TypedDict): + type: Literal["badSession"] + +class TransportError_noServerAuth(TypedDict): + type: Literal["noServerAuth"] + +class TransportError_handshake(TypedDict): + type: Literal["handshake"] + handshakeErr: "HandshakeError" + +TransportError = ( + TransportError_badBlock + | TransportError_version + | TransportError_largeMsg + | TransportError_badSession + | TransportError_noServerAuth + | TransportError_handshake +) + +TransportError_Tag = Literal["badBlock", "version", "largeMsg", "badSession", "noServerAuth", "handshake"] + +UIColorMode = Literal["light", "dark"] + +class UIColors(TypedDict): + accent: NotRequired[str] + accentVariant: NotRequired[str] + secondary: NotRequired[str] + secondaryVariant: NotRequired[str] + background: NotRequired[str] + menus: NotRequired[str] + title: NotRequired[str] + accentVariant2: NotRequired[str] + sentMessage: NotRequired[str] + sentReply: NotRequired[str] + receivedMessage: NotRequired[str] + receivedReply: NotRequired[str] + +class UIThemeEntityOverride(TypedDict): + mode: "UIColorMode" + wallpaper: NotRequired["ChatWallpaper"] + colors: "UIColors" + +class UIThemeEntityOverrides(TypedDict): + light: NotRequired["UIThemeEntityOverride"] + dark: NotRequired["UIThemeEntityOverride"] + +class UpdatedMessage(TypedDict): + msgContent: "MsgContent" + mentions: dict[str, int] # str : int64 + +class User(TypedDict): + userId: int # int64 + agentUserId: int # int64 + userContactId: int # int64 + localDisplayName: str + profile: "LocalProfile" + fullPreferences: "FullPreferences" + activeUser: bool + activeOrder: int # int64 + viewPwdHash: NotRequired["UserPwdHash"] + showNtfs: bool + sendRcptsContacts: bool + sendRcptsSmallGroups: bool + autoAcceptMemberContacts: bool + userMemberProfileUpdatedAt: NotRequired[str] # ISO-8601 timestamp + uiThemes: NotRequired["UIThemeEntityOverrides"] + userChatRelay: bool + +class UserChatRelay(TypedDict): + chatRelayId: int # int64 + address: str + relayProfile: "RelayProfile" + domains: list[str] + preset: bool + tested: NotRequired[bool] + enabled: bool + deleted: bool + +class UserContact(TypedDict): + userContactLinkId: int # int64 + connReqContact: str + groupId: NotRequired[int] # int64 + +class UserContactLink(TypedDict): + userContactLinkId: int # int64 + connLinkContact: "CreatedConnLink" + shortLinkDataSet: bool + shortLinkLargeDataSet: bool + addressSettings: "AddressSettings" + +class UserContactRequest(TypedDict): + contactRequestId: int # int64 + agentInvitationId: str + contactId_: NotRequired[int] # int64 + businessGroupId_: NotRequired[int] # int64 + userContactLinkId_: NotRequired[int] # int64 + cReqChatVRange: "VersionRange" + localDisplayName: str + profileId: int # int64 + profile: "Profile" + createdAt: str # ISO-8601 timestamp + updatedAt: str # ISO-8601 timestamp + xContactId: NotRequired[str] + pqSupport: bool + welcomeSharedMsgId: NotRequired[str] + requestSharedMsgId: NotRequired[str] + +class UserInfo(TypedDict): + user: "User" + unreadCount: int # int + +class UserProfileUpdateSummary(TypedDict): + updateSuccesses: int # int + updateFailures: int # int + changedContacts: list["Contact"] + +class UserPwdHash(TypedDict): + hash: str + salt: str + +class VersionRange(TypedDict): + minVersion: int # int + maxVersion: int # int + +class XFTPErrorType_BLOCK(TypedDict): + type: Literal["BLOCK"] + +class XFTPErrorType_SESSION(TypedDict): + type: Literal["SESSION"] + +class XFTPErrorType_HANDSHAKE(TypedDict): + type: Literal["HANDSHAKE"] + +class XFTPErrorType_CMD(TypedDict): + type: Literal["CMD"] + cmdErr: "CommandError" + +class XFTPErrorType_AUTH(TypedDict): + type: Literal["AUTH"] + +class XFTPErrorType_BLOCKED(TypedDict): + type: Literal["BLOCKED"] + blockInfo: "BlockingInfo" + +class XFTPErrorType_SIZE(TypedDict): + type: Literal["SIZE"] + +class XFTPErrorType_QUOTA(TypedDict): + type: Literal["QUOTA"] + +class XFTPErrorType_DIGEST(TypedDict): + type: Literal["DIGEST"] + +class XFTPErrorType_CRYPTO(TypedDict): + type: Literal["CRYPTO"] + +class XFTPErrorType_NO_FILE(TypedDict): + type: Literal["NO_FILE"] + +class XFTPErrorType_HAS_FILE(TypedDict): + type: Literal["HAS_FILE"] + +class XFTPErrorType_FILE_IO(TypedDict): + type: Literal["FILE_IO"] + +class XFTPErrorType_TIMEOUT(TypedDict): + type: Literal["TIMEOUT"] + +class XFTPErrorType_INTERNAL(TypedDict): + type: Literal["INTERNAL"] + +class XFTPErrorType_DUPLICATE_(TypedDict): + type: Literal["DUPLICATE_"] + +XFTPErrorType = ( + XFTPErrorType_BLOCK + | XFTPErrorType_SESSION + | XFTPErrorType_HANDSHAKE + | XFTPErrorType_CMD + | XFTPErrorType_AUTH + | XFTPErrorType_BLOCKED + | XFTPErrorType_SIZE + | XFTPErrorType_QUOTA + | XFTPErrorType_DIGEST + | XFTPErrorType_CRYPTO + | XFTPErrorType_NO_FILE + | XFTPErrorType_HAS_FILE + | XFTPErrorType_FILE_IO + | XFTPErrorType_TIMEOUT + | XFTPErrorType_INTERNAL + | XFTPErrorType_DUPLICATE_ +) + +XFTPErrorType_Tag = Literal["BLOCK", "SESSION", "HANDSHAKE", "CMD", "AUTH", "BLOCKED", "SIZE", "QUOTA", "DIGEST", "CRYPTO", "NO_FILE", "HAS_FILE", "FILE_IO", "TIMEOUT", "INTERNAL", "DUPLICATE_"] + +class XFTPRcvFile(TypedDict): + rcvFileDescription: "RcvFileDescr" + agentRcvFileId: NotRequired[str] + agentRcvFileDeleted: bool + userApprovedRelays: bool + +class XFTPSndFile(TypedDict): + agentSndFileId: str + privateSndFileDescr: NotRequired[str] + agentSndFileDeleted: bool + cryptoArgs: NotRequired["CryptoFileArgs"] diff --git a/packages/simplex-chat-python/src/simplex_chat/util.py b/packages/simplex-chat-python/src/simplex_chat/util.py new file mode 100644 index 0000000000..158bb72a79 --- /dev/null +++ b/packages/simplex-chat-python/src/simplex_chat/util.py @@ -0,0 +1,128 @@ +"""Reusable helpers for working with chat events, types, and message content. + +Mirrors the Node `util.ts` exports — provides the same primitives bot +authors typically reach for: command parsing, sender display strings, +message-content extraction, profile field cleanup, and ChatRef extraction +from a ChatInfo (handy when echoing into a different chat). +""" + +from __future__ import annotations + +import re +from typing import Any + +from .types import T + + +def chat_info_ref(c_info: T.ChatInfo) -> T.ChatRef | None: + """Extract a wire-format `ChatRef` from a `ChatInfo`. + + Returns `None` for non-chat infos (contactRequest, contactConnection) + that can't be the target of `api_send_messages`. For groups, the + `memberSupport` scope is forwarded so messages land in the right + thread; other scopes are dropped (matches Node `util.chatInfoRef`). + """ + t = c_info["type"] + if t == "direct": + return {"chatType": "direct", "chatId": c_info["contact"]["contactId"]} # type: ignore[index] + if t == "group": + ref: T.ChatRef = {"chatType": "group", "chatId": c_info["groupInfo"]["groupId"]} # type: ignore[index] + scope = c_info.get("groupChatScope") # type: ignore[union-attr] + if scope and scope.get("type") == "memberSupport": + member = scope.get("groupMember_") + ms_scope: T.GroupChatScope_memberSupport = {"type": "memberSupport"} + if member is not None: + ms_scope["groupMemberId_"] = member["groupMemberId"] + ref["chatScope"] = ms_scope + return ref + return None + + +def chat_info_name(c_info: T.ChatInfo) -> str: + """Display string for a chat: `@Alice`, `#GroupName`, `private notes`, etc.""" + t = c_info["type"] + if t == "direct": + return f"@{c_info['contact']['profile']['displayName']}" # type: ignore[index] + if t == "group": + scope = c_info.get("groupChatScope") # type: ignore[union-attr] + if scope and scope.get("type") == "memberSupport": + member = scope.get("groupMember_") + scope_name = f" {member['memberProfile']['displayName']}" if member else "" + return f"#{c_info['groupInfo']['groupProfile']['displayName']}(support{scope_name})" # type: ignore[index] + return f"#{c_info['groupInfo']['groupProfile']['displayName']}" # type: ignore[index] + if t == "local": + return "private notes" + if t == "contactRequest": + return f"request from @{c_info['contactRequest']['profile']['displayName']}" # type: ignore[index] + if t == "contactConnection": + alias = c_info["contactConnection"].get("localAlias") # type: ignore[index] + return f"pending connection ({alias})" if alias else "pending connection" + return f"<{t}>" + + +def sender_name(c_info: T.ChatInfo, chat_dir: T.CIDirection) -> str: + """Sender display: chat name plus group sender suffix when applicable.""" + base = chat_info_name(c_info) + if chat_dir["type"] == "groupRcv": + sender = chat_dir["groupMember"]["memberProfile"]["displayName"] # type: ignore[index] + return f"{base} @{sender}" + return base + + +def contact_address_str(link: T.CreatedConnLink) -> str: + """Prefer the short link, fall back to the full link.""" + return link.get("connShortLink") or link["connFullLink"] + + +def from_local_profile(local: T.LocalProfile) -> T.Profile: + """Strip extra LocalProfile fields (profileId, localAlias) and undefined values.""" + p: dict[str, Any] = {} + for key in ( + "displayName", + "fullName", + "shortDescr", + "image", + "contactLink", + "preferences", + "peerType", + ): + v = local.get(key) # type: ignore[misc] + if v is not None: + p[key] = v + return p # type: ignore[return-value] + + +def ci_content_text(chat_item: T.ChatItem) -> str | None: + """Extract the message text from a sent or received message item, if any.""" + content = chat_item["content"] + if content["type"] in ("sndMsgContent", "rcvMsgContent"): + msg = content.get("msgContent", {}) # type: ignore[union-attr] + return msg.get("text") + return None + + +_BOT_COMMAND_RE = re.compile(r"^/([^\s]+)(.*)$") + + +def ci_bot_command(chat_item: T.ChatItem) -> tuple[str, str] | None: + """Parse a `/keyword args...` slash-command from a chat item. + + Returns `(keyword, trimmed_params)` or `None` if the message isn't a + slash command. Mirrors Node `util.ciBotCommand` semantics. + """ + text = ci_content_text(chat_item) + if not text: + return None + text = text.strip() + m = _BOT_COMMAND_RE.match(text) + if not m: + return None + return m.group(1), m.group(2).strip() + + +def reaction_text(reaction: T.ACIReaction) -> str: + """Format an `ACIReaction` as the emoji character or tag string.""" + r = reaction["chatReaction"]["reaction"] # type: ignore[index] + if r["type"] == "emoji": + return r["emoji"] # type: ignore[index] + return r.get("tag", "") # type: ignore[union-attr] diff --git a/packages/simplex-chat-python/tests/test_bot_registration.py b/packages/simplex-chat-python/tests/test_bot_registration.py new file mode 100644 index 0000000000..7401d2ef5d --- /dev/null +++ b/packages/simplex-chat-python/tests/test_bot_registration.py @@ -0,0 +1,357 @@ +import pytest + +from simplex_chat import Bot, BotCommand, BotProfile, Middleware, SqliteDb +from simplex_chat.api import ChatApi + + +def _bot() -> Bot: + return Bot(profile=BotProfile(display_name="x"), db=SqliteDb(file_prefix="/tmp/test")) + + +def test_decorator_registers_message_handler(): + bot = _bot() + + @bot.on_message(content_type="text") + async def h(msg): + pass + + assert len(bot._message_handlers) == 1 + + +def test_decorator_registers_command_handler(): + bot = _bot() + + @bot.on_command("ping") + async def h(msg, cmd): + pass + + assert len(bot._command_handlers) == 1 + assert bot._command_handlers[0][0] == ("ping",) + + +def test_decorator_registers_event_handler(): + bot = _bot() + + @bot.on_event("newChatItems") + async def h(evt): + pass + + assert "newChatItems" in bot._event_handlers + assert len(bot._event_handlers["newChatItems"]) == 1 + + +def test_api_property_raises_before_init(): + bot = _bot() + with pytest.raises(RuntimeError, match="not initialized"): + _ = bot.api + + +def test_command_keyword_tuple(): + bot = _bot() + + @bot.on_command(("p", "ping")) + async def h(msg, cmd): + pass + + assert bot._command_handlers[0][0] == ("p", "ping") + + +def test_bot_profile_to_wire_default(): + """use_bot_profile=True (default) sets peerType=bot and disables calls/voice.""" + bot = _bot() + p = bot._bot_profile_to_wire() + assert p["displayName"] == "x" + assert p.get("peerType") == "bot" + prefs = p.get("preferences") or {} + assert prefs.get("calls", {}).get("allow") == "no" + assert prefs.get("voice", {}).get("allow") == "no" + assert prefs.get("files", {}).get("allow") == "no" # allow_files defaults to False + + +def test_bot_profile_to_wire_allow_files(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + allow_files=True, + ) + prefs = bot._bot_profile_to_wire().get("preferences") or {} + assert prefs.get("files", {}).get("allow") == "yes" + + +def test_bot_profile_to_wire_with_commands(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + commands=[BotCommand(keyword="ping", label="Ping bot"), BotCommand("help", "Show help")], + ) + cmds = bot._bot_profile_to_wire().get("preferences", {}).get("commands") or [] + assert len(cmds) == 2 + assert cmds[0] == {"type": "command", "keyword": "ping", "label": "Ping bot"} + assert cmds[1] == {"type": "command", "keyword": "help", "label": "Show help"} + + +def test_bot_profile_to_wire_no_bot_profile(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + use_bot_profile=False, + ) + p = bot._bot_profile_to_wire() + assert "peerType" not in p + assert "preferences" not in p + + +def test_commands_without_bot_profile_raises(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + use_bot_profile=False, + commands=[BotCommand("ping", "Ping bot")], + ) + with pytest.raises(ValueError, match="use_bot_profile=False"): + bot._bot_profile_to_wire() + + +def test_dispatch_message_first_match_wins(): + """Two matching message handlers — only the first registered fires.""" + import asyncio + import re + + bot = _bot() + calls: list[str] = [] + + @bot.on_message(content_type="text", text=re.compile(r"^\d+$")) + async def number(_msg): + calls.append("number") + + @bot.on_message(content_type="text") + async def fallback(_msg): + calls.append("fallback") + + class M: + pass + + m = M() + m.content = {"type": "text", "text": "42"} + m.chat_item = { + "chatItem": { + "content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "42"}} + }, + "chatInfo": {"type": "direct"}, + } + m.text = "42" + + asyncio.run(bot._dispatch_message(m)) # type: ignore[arg-type] + assert calls == ["number"], f"expected only 'number' for '42', got {calls}" + + +def test_dispatch_message_falls_to_second_when_first_doesnt_match(): + """If the first handler's filter doesn't match, the second one fires.""" + import asyncio + import re + + bot = _bot() + calls: list[str] = [] + + @bot.on_message(content_type="text", text=re.compile(r"^\d+$")) + async def number(_msg): + calls.append("number") + + @bot.on_message(content_type="text") + async def fallback(_msg): + calls.append("fallback") + + class M: + pass + + m = M() + m.content = {"type": "text", "text": "hello"} + m.chat_item = { + "chatItem": { + "content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "hello"}} + }, + "chatInfo": {"type": "direct"}, + } + m.text = "hello" + + asyncio.run(bot._dispatch_message(m)) # type: ignore[arg-type] + assert calls == ["fallback"], f"expected 'fallback' for 'hello', got {calls}" + + +def test_register_log_handlers_idempotent(): + """Calling _register_log_handlers twice doesn't duplicate handlers.""" + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + log_contacts=True, + log_network=True, + ) + bot._register_log_handlers() + counts1 = {tag: len(hs) for tag, hs in bot._event_handlers.items()} + bot._register_log_handlers() + counts2 = {tag: len(hs) for tag, hs in bot._event_handlers.items()} + assert counts1 == counts2, f"handler count changed across calls: {counts1} -> {counts2}" + + +def test_default_error_handlers_always_registered(): + """messageError/chatError/chatErrors get default loggers regardless of opts.""" + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + log_contacts=False, + log_network=False, + ) + bot._register_log_handlers() + assert "messageError" in bot._event_handlers + assert "chatError" in bot._event_handlers + assert "chatErrors" in bot._event_handlers + + +def test_dispatch_command_suppresses_matching_message_handlers(): + """A `/help` message routed to a command handler must NOT also fire the + generic on_message text handler.""" + import asyncio + + bot = _bot() + calls: list[str] = [] + + @bot.on_message(content_type="text") + async def fallback(_msg): + calls.append("message") + + @bot.on_command("help") + async def help_cmd(_msg, _cmd): + calls.append("command") + + # Build a minimal Message-shaped object (handlers only inspect chat_item / text). + class M: + pass + + m = M() + m.content = {"type": "text", "text": "/help"} + m.chat_item = { + "chatItem": { + "content": { + "type": "rcvMsgContent", + "msgContent": {"type": "text", "text": "/help"}, + } + }, + "chatInfo": {"type": "direct"}, + } + m.text = "/help" + + asyncio.run(bot._dispatch_message(m)) # type: ignore[arg-type] + assert calls == ["command"], f"expected only 'command' to fire for /help, got {calls}" + + +def test_dispatch_unknown_command_falls_through_to_message_handlers(): + """A `/unknown` slash-command with no handler should still fire on_message.""" + import asyncio + + bot = _bot() + calls: list[str] = [] + + @bot.on_message(content_type="text") + async def fallback(_msg): + calls.append("message") + + @bot.on_command("help") + async def help_cmd(_msg, _cmd): + calls.append("command") + + class M: + pass + + m = M() + m.content = {"type": "text", "text": "/unknown"} + m.chat_item = { + "chatItem": { + "content": { + "type": "rcvMsgContent", + "msgContent": {"type": "text", "text": "/unknown"}, + } + }, + "chatInfo": {"type": "direct"}, + } + m.text = "/unknown" + + asyncio.run(bot._dispatch_message(m)) # type: ignore[arg-type] + assert calls == ["message"], f"expected message fallback to fire for /unknown, got {calls}" + + +def test_chat_api_status_properties(): + """`initialized` and `started` reflect lifecycle state without invoking the FFI.""" + api = ChatApi(ctrl=12345) + assert api.initialized is True + assert api.started is False + assert api.ctrl == 12345 + # Simulate close: ctrl wiped, both properties false. + api._ctrl = None + api._started = False + assert api.initialized is False + assert api.started is False + with pytest.raises(RuntimeError, match="not initialized"): + _ = api.ctrl + + +def test_log_contacts_registers_handlers(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + log_contacts=True, + log_network=False, + ) + bot._register_log_handlers() + assert "contactConnected" in bot._event_handlers + assert "contactDeletedByContact" in bot._event_handlers + assert "hostConnected" not in bot._event_handlers + + +def test_log_network_registers_handlers(): + bot = Bot( + profile=BotProfile(display_name="x"), + db=SqliteDb(file_prefix="/tmp/test"), + log_contacts=False, + log_network=True, + ) + bot._register_log_handlers() + assert "hostConnected" in bot._event_handlers + assert "hostDisconnected" in bot._event_handlers + assert "subscriptionStatus" in bot._event_handlers + assert "contactConnected" not in bot._event_handlers + + +def test_middleware_registration_and_invocation_order(): + """Middleware registered first wraps middleware registered later (outer first).""" + bot = _bot() + calls: list[str] = [] + + class Outer(Middleware): + async def __call__(self, handler, message, data): + calls.append("outer-before") + await handler(message, data) + calls.append("outer-after") + + class Inner(Middleware): + async def __call__(self, handler, message, data): + calls.append("inner-before") + await handler(message, data) + calls.append("inner-after") + + bot.use(Outer()) + bot.use(Inner()) + assert len(bot._middleware) == 2 + + async def handler(msg): + calls.append("handler") + + import asyncio + + asyncio.run(bot._invoke_with_middleware(handler, message=object())) # type: ignore[arg-type] + assert calls == [ + "outer-before", + "inner-before", + "handler", + "inner-after", + "outer-after", + ] diff --git a/packages/simplex-chat-python/tests/test_codegen.py b/packages/simplex-chat-python/tests/test_codegen.py new file mode 100644 index 0000000000..509d919cfd --- /dev/null +++ b/packages/simplex-chat-python/tests/test_codegen.py @@ -0,0 +1,41 @@ +"""Sanity checks on auto-generated wire types — catches generator regressions.""" + +import typing + +from simplex_chat.types import CC, CEvt, CR, T + + +def test_types_module_imports(): + """Every generated module imports cleanly with no SyntaxError.""" + assert T is not None and CC is not None and CR is not None and CEvt is not None + + +def test_chat_type_is_literal_enum(): + """ChatType should be a Literal of expected member set.""" + args = typing.get_args(T.ChatType) + assert "direct" in args + assert "group" in args + assert "local" in args + + +def test_known_command_has_cmd_string(): + s = CC.APICreateMyAddress_cmd_string({"userId": 1}) + assert s == "/_address 1" + + +def test_chat_response_tag_alias_present(): + """ChatResponse_Tag union of literals exists.""" + assert hasattr(CR, "ChatResponse_Tag") + + +def test_chat_event_tag_alias_present(): + """ChatEvent_Tag exists; covers the on_event Literal annotation.""" + assert hasattr(CEvt, "ChatEvent_Tag") + args = typing.get_args(CEvt.ChatEvent_Tag) + assert "newChatItems" in args + + +def test_chat_ref_cmd_string_direct(): + """Sanity check the codegen fix for ChatRef-bearing commands.""" + assert T.ChatRef_cmd_string({"chatType": "direct", "chatId": 7}) == "@7" + assert T.ChatRef_cmd_string({"chatType": "group", "chatId": 42}) == "#42" diff --git a/packages/simplex-chat-python/tests/test_filters.py b/packages/simplex-chat-python/tests/test_filters.py new file mode 100644 index 0000000000..08fb66ed92 --- /dev/null +++ b/packages/simplex-chat-python/tests/test_filters.py @@ -0,0 +1,83 @@ +import re + +from simplex_chat.filters import compile_message_filter + + +def _msg(content_type="text", text=None, chat_type="direct", group_id=None): + """Build a minimal mock Message-like object for filter testing.""" + + class M: + pass + + m = M() + m.content = {"type": content_type, "text": text} if text is not None else {"type": content_type} + m.chat_item = { + "chatInfo": { + "type": chat_type, + **({"groupInfo": {"groupId": group_id}} if chat_type == "group" else {}), + } + } + return m + + +def test_no_filters_matches_all(): + f = compile_message_filter({}) + assert f(_msg(content_type="text")) + assert f(_msg(content_type="image")) + + +def test_content_type_singular(): + f = compile_message_filter({"content_type": "text"}) + assert f(_msg(content_type="text")) + assert not f(_msg(content_type="image")) + + +def test_content_type_tuple_or(): + f = compile_message_filter({"content_type": ("text", "image")}) + assert f(_msg(content_type="text")) + assert f(_msg(content_type="image")) + assert not f(_msg(content_type="voice")) + + +def test_text_exact(): + f = compile_message_filter({"text": "hello"}) + assert f(_msg(text="hello")) + assert not f(_msg(text="world")) + + +def test_text_regex(): + f = compile_message_filter({"text": re.compile(r"^\d+$")}) + assert f(_msg(text="123")) + assert not f(_msg(text="abc")) + + +def test_when_callable(): + f = compile_message_filter({"when": lambda m: m.content["type"] == "voice"}) + assert f(_msg(content_type="voice")) + assert not f(_msg(content_type="text")) + + +def test_combined_and(): + f = compile_message_filter({"content_type": "text", "text": re.compile(r"\d")}) + assert f(_msg(content_type="text", text="abc123")) + assert not f(_msg(content_type="text", text="abc")) + assert not f(_msg(content_type="image")) + + +def test_chat_type_filter(): + f = compile_message_filter({"chat_type": "group"}) + assert f(_msg(chat_type="group", group_id=1)) + assert not f(_msg(chat_type="direct")) + + +def test_group_id_filter(): + f = compile_message_filter({"group_id": 42}) + assert f(_msg(chat_type="group", group_id=42)) + assert not f(_msg(chat_type="group", group_id=99)) + assert not f(_msg(chat_type="direct")) + + +def test_group_id_tuple_or(): + f = compile_message_filter({"group_id": (1, 2, 3)}) + assert f(_msg(chat_type="group", group_id=2)) + assert not f(_msg(chat_type="group", group_id=99)) diff --git a/packages/simplex-chat-python/tests/test_native_cache.py b/packages/simplex-chat-python/tests/test_native_cache.py new file mode 100644 index 0000000000..bd3bc58da8 --- /dev/null +++ b/packages/simplex-chat-python/tests/test_native_cache.py @@ -0,0 +1,92 @@ +import zipfile +from pathlib import Path + +import pytest + +from simplex_chat._native import _cache_root, _resolve_libs_dir, _download + + +def test_cache_root_linux(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + assert _cache_root() == tmp_path / "simplex-chat" + + +def test_cache_root_macos(tmp_path, monkeypatch): + monkeypatch.setattr("sys.platform", "darwin") + monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path) + assert _cache_root() == tmp_path / "Library" / "Caches" / "simplex-chat" + + +def test_override_via_env(tmp_path, monkeypatch): + # _resolve_libs_dir intentionally does not validate the override directory — + # it returns it verbatim; the eventual ctypes.CDLL call surfaces any mistake. + monkeypatch.setenv("SIMPLEX_LIBS_DIR", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + assert _resolve_libs_dir("sqlite") == tmp_path + + +def test_resolve_downloads_when_missing(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "linux-x86_64") + + called = {} + + def fake_download(target_root: Path, backend: str) -> None: + called["target"] = target_root + called["backend"] = backend + target_root.mkdir(parents=True, exist_ok=True) + (target_root / "libsimplex.so").touch() + + monkeypatch.setattr("simplex_chat._native._download", fake_download) + libs_dir = _resolve_libs_dir("sqlite") + assert libs_dir == tmp_path / "simplex-chat" / "v6.5.1" / "sqlite" + assert called["backend"] == "sqlite" + assert (libs_dir / "libsimplex.so").exists() + + +def test_resolve_uses_cache_on_second_call(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + cached = tmp_path / "simplex-chat" / "v6.5.1" / "sqlite" + cached.mkdir(parents=True) + (cached / "libsimplex.so").touch() + # Should NOT call _download — use the cached file. + monkeypatch.setattr( + "simplex_chat._native._download", lambda *a: pytest.fail("download should not be called") + ) + assert _resolve_libs_dir("sqlite") == cached + + +def test_postgres_on_macos_rejected(monkeypatch): + monkeypatch.setattr("sys.platform", "darwin") + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "macos-aarch64") + with pytest.raises(RuntimeError, match="postgres.*linux-x86_64"): + _resolve_libs_dir("postgres") + + +def test_atomic_install(tmp_path, monkeypatch): + """Build a fake libs zip, mock _stream_to_file, verify extraction + atomic rename.""" + # Build zip: libs/libsimplex.so + libs/libHS-stub.so + src = tmp_path / "src" / "libs" + src.mkdir(parents=True) + (src / "libsimplex.so").write_text("fake-so") + (src / "libHS-stub.so").write_text("fake-hs") + zip_path = tmp_path / "fake-libs.zip" + with zipfile.ZipFile(zip_path, "w") as zf: + for f in src.iterdir(): + zf.write(f, f"libs/{f.name}") + + def fake_stream(url, dest, *, timeout=60.0): + import shutil + + shutil.copy(zip_path, dest) + + monkeypatch.setattr("simplex_chat._native._stream_to_file", fake_stream) + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "linux-x86_64") + + target = tmp_path / "out" + _download(target, "sqlite") + assert (target / "libsimplex.so").read_text() == "fake-so" + assert (target / "libHS-stub.so").read_text() == "fake-hs" diff --git a/packages/simplex-chat-python/tests/test_native_url.py b/packages/simplex-chat-python/tests/test_native_url.py new file mode 100644 index 0000000000..7b53fa3ff7 --- /dev/null +++ b/packages/simplex-chat-python/tests/test_native_url.py @@ -0,0 +1,55 @@ +from unittest.mock import patch +import pytest +from simplex_chat._native import _platform_tag, _libs_url, _libname + + +@patch("sys.platform", "linux") +@patch("platform.machine", return_value="x86_64") +def test_platform_linux_x64(_): + assert _platform_tag() == "linux-x86_64" + + +@patch("sys.platform", "darwin") +@patch("platform.machine", return_value="arm64") +def test_platform_macos_arm64(_): + assert _platform_tag() == "macos-aarch64" + + +@patch("sys.platform", "win32") +@patch("platform.machine", return_value="AMD64") +def test_platform_windows_x64(_): + assert _platform_tag() == "windows-x86_64" + + +@patch("sys.platform", "freebsd") +@patch("platform.machine", return_value="x86_64") +def test_platform_unsupported(_): + with pytest.raises(RuntimeError, match="Unsupported"): + _platform_tag() + + +def test_libname_per_platform(): + with patch("sys.platform", "linux"): + assert _libname() == "libsimplex.so" + with patch("sys.platform", "darwin"): + assert _libname() == "libsimplex.dylib" + with patch("sys.platform", "win32"): + assert _libname() == "libsimplex.dll" + + +@patch("simplex_chat._native._platform_tag", return_value="linux-x86_64") +def test_url_sqlite(_): + assert ( + _libs_url("sqlite") + == "https://github.com/simplex-chat/simplex-chat-libs/releases/download/" + "v6.5.1/simplex-chat-libs-linux-x86_64.zip" + ) + + +@patch("simplex_chat._native._platform_tag", return_value="linux-x86_64") +def test_url_postgres(_): + assert ( + _libs_url("postgres") + == "https://github.com/simplex-chat/simplex-chat-libs/releases/download/" + "v6.5.1/simplex-chat-libs-linux-x86_64-postgres.zip" + ) diff --git a/packages/simplex-chat-python/tests/test_util.py b/packages/simplex-chat-python/tests/test_util.py new file mode 100644 index 0000000000..983b1c2a56 --- /dev/null +++ b/packages/simplex-chat-python/tests/test_util.py @@ -0,0 +1,175 @@ +from simplex_chat import util + + +def test_chat_info_ref_direct(): + ci = {"type": "direct", "contact": {"contactId": 7}} + assert util.chat_info_ref(ci) == {"chatType": "direct", "chatId": 7} + + +def test_chat_info_ref_group(): + ci = {"type": "group", "groupInfo": {"groupId": 42}} + assert util.chat_info_ref(ci) == {"chatType": "group", "chatId": 42} + + +def test_chat_info_ref_group_with_member_support_scope(): + ci = { + "type": "group", + "groupInfo": {"groupId": 42}, + "groupChatScope": {"type": "memberSupport", "groupMember_": {"groupMemberId": 99}}, + } + ref = util.chat_info_ref(ci) + assert ref == { + "chatType": "group", + "chatId": 42, + "chatScope": {"type": "memberSupport", "groupMemberId_": 99}, + } + + +def test_chat_info_ref_group_with_member_support_scope_no_member(): + ci = { + "type": "group", + "groupInfo": {"groupId": 42}, + "groupChatScope": {"type": "memberSupport"}, + } + ref = util.chat_info_ref(ci) + # No groupMember_ → no groupMemberId_ in the wire scope. + assert ref == { + "chatType": "group", + "chatId": 42, + "chatScope": {"type": "memberSupport"}, + } + + +def test_chat_info_ref_returns_none_for_non_targets(): + assert util.chat_info_ref({"type": "contactRequest"}) is None + assert util.chat_info_ref({"type": "contactConnection"}) is None + + +def test_chat_info_name_direct(): + ci = {"type": "direct", "contact": {"profile": {"displayName": "Alice"}}} + assert util.chat_info_name(ci) == "@Alice" + + +def test_chat_info_name_group(): + ci = {"type": "group", "groupInfo": {"groupProfile": {"displayName": "MyGroup"}}} + assert util.chat_info_name(ci) == "#MyGroup" + + +def test_chat_info_name_group_with_member_support(): + ci = { + "type": "group", + "groupInfo": {"groupProfile": {"displayName": "MyGroup"}}, + "groupChatScope": { + "type": "memberSupport", + "groupMember_": {"memberProfile": {"displayName": "Carol"}}, + }, + } + assert util.chat_info_name(ci) == "#MyGroup(support Carol)" + + +def test_chat_info_name_local(): + assert util.chat_info_name({"type": "local"}) == "private notes" + + +def test_chat_info_name_contact_request(): + ci = {"type": "contactRequest", "contactRequest": {"profile": {"displayName": "Eve"}}} + assert util.chat_info_name(ci) == "request from @Eve" + + +def test_chat_info_name_contact_connection(): + assert util.chat_info_name({"type": "contactConnection", "contactConnection": {}}) == ( + "pending connection" + ) + assert ( + util.chat_info_name({"type": "contactConnection", "contactConnection": {"localAlias": "X"}}) + == "pending connection (X)" + ) + + +def test_sender_name_direct_uses_chat_name(): + ci = {"type": "direct", "contact": {"profile": {"displayName": "Alice"}}} + chat_dir = {"type": "directRcv"} + assert util.sender_name(ci, chat_dir) == "@Alice" + + +def test_sender_name_group_appends_member(): + ci = {"type": "group", "groupInfo": {"groupProfile": {"displayName": "MyGroup"}}} + chat_dir = {"type": "groupRcv", "groupMember": {"memberProfile": {"displayName": "Bob"}}} + assert util.sender_name(ci, chat_dir) == "#MyGroup @Bob" + + +def test_contact_address_str_prefers_short(): + assert util.contact_address_str({"connFullLink": "full", "connShortLink": "short"}) == "short" + + +def test_contact_address_str_falls_back_to_full(): + assert util.contact_address_str({"connFullLink": "full"}) == "full" + + +def test_from_local_profile_strips_extras_and_undefined(): + local = { + "displayName": "x", + "fullName": "X Y", + "shortDescr": None, + "image": "data:image/png;base64,...", + "contactLink": None, + "preferences": {}, + "peerType": "bot", + "profileId": 99, # extra LocalProfile field + "localAlias": "alias", # extra LocalProfile field + } + p = util.from_local_profile(local) + assert p == { + "displayName": "x", + "fullName": "X Y", + "image": "data:image/png;base64,...", + "preferences": {}, + "peerType": "bot", + } + + +def test_ci_content_text_rcv(): + ci = {"content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "hello"}}} + assert util.ci_content_text(ci) == "hello" + + +def test_ci_content_text_snd(): + ci = {"content": {"type": "sndMsgContent", "msgContent": {"type": "text", "text": "world"}}} + assert util.ci_content_text(ci) == "world" + + +def test_ci_content_text_other(): + ci = {"content": {"type": "rcvGroupEvent"}} + assert util.ci_content_text(ci) is None + + +def test_ci_bot_command_match(): + ci = {"content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "/ping"}}} + assert util.ci_bot_command(ci) == ("ping", "") + + +def test_ci_bot_command_with_args(): + ci = { + "content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "/echo hi "}} + } + assert util.ci_bot_command(ci) == ("echo", "hi") + + +def test_ci_bot_command_not_a_command(): + ci = {"content": {"type": "rcvMsgContent", "msgContent": {"type": "text", "text": "hello"}}} + assert util.ci_bot_command(ci) is None + + +def test_ci_bot_command_no_text(): + ci = {"content": {"type": "rcvGroupEvent"}} + assert util.ci_bot_command(ci) is None + + +def test_reaction_text_emoji(): + r = {"chatReaction": {"reaction": {"type": "emoji", "emoji": "🎉"}}} + assert util.reaction_text(r) == "🎉" + + +def test_reaction_text_tag(): + r = {"chatReaction": {"reaction": {"type": "unknown", "tag": "thumbs_up"}}} + assert util.reaction_text(r) == "thumbs_up" diff --git a/plans/2026-05-07-simplex-chat-python-design.md b/plans/2026-05-07-simplex-chat-python-design.md new file mode 100644 index 0000000000..240f88714c --- /dev/null +++ b/plans/2026-05-07-simplex-chat-python-design.md @@ -0,0 +1,575 @@ +# SimpleX Chat Python library design + +## Table of contents + +- [What](#what) +- [Why](#why) +- [How](#how) +- [Architecture](#architecture) +- [Type generation](#type-generation) +- [Native lib loading](#native-lib-loading) +- [Public API](#public-api) +- [Distribution and CI](#distribution-and-ci) +- [Testing](#testing) +- [Open questions](#open-questions) + +## What + +A Python 3 client library `simplex-chat` on PyPI for SimpleX bots. Same capability as the Node.js library at `packages/simplex-chat-nodejs/`. + +The user writes a Python script with decorator-registered handlers; the library does the rest: + +```python +from simplex_chat import Bot, BotProfile, SqliteDb, TextMessage + +bot = Bot(profile=BotProfile(display_name="Squarer"), + db=SqliteDb(file_prefix="./bot"), + welcome="Send a number, I'll square it.") + +@bot.on_message(content_type="text") +async def square(msg: TextMessage) -> None: + try: + n = float(msg.text) + await msg.reply(f"{n} * {n} = {n * n}") + except ValueError: + await msg.reply("Not a number.") + +if __name__ == "__main__": + bot.run() +``` + +`pip install simplex-chat`, run the script, done. + +## Why + +SimpleX has a Node.js library (`simplex-chat`) and Haskell-built native lib (`libsimplex.{so,dylib,dll}`) but no Python equivalent. Python is the dominant language for bot scripting, automation, and data integration. Without a Python client, those users either can't use SimpleX or have to bridge through Node. + +The native `libsimplex` already exists as prebuilt artifacts (`simplex-chat/simplex-chat-libs` GitHub releases, one zip per platform/backend). The Haskell type metadata that drives the Node lib's TypeScript types is already in `bots/src/API/Docs/`. Both can be reused — adding Python bindings is mostly wiring, not a new system. + +## How + +Three pieces: + +1. **Extend the existing Haskell type generator** in `bots/src/API/Docs/Generate/` to emit a Python types module alongside the existing TypeScript one. The metadata is the same; only the rendering changes. Already includes `pySyntaxText` (used today in COMMANDS.md docs) — just needs a Python codegen module. + +2. **A new Python package** `packages/simplex-chat-python/` that wraps the prebuilt `libsimplex.*` via `ctypes`, downloading it on first use from the existing GitHub release. Async-only (`asyncio`), Python 3.11+. Single `Bot` class with decorator-registered handlers. + +3. **One small CI job** appended to `.github/workflows/build.yml`, after the existing `release-nodejs-libs` job, that publishes the Python package to PyPI on each release tag. ~15 lines of YAML. + +No new infrastructure: no separate libs build, no per-platform wheels, no PyPI size waiver, no second CI workflow. The libs zips that already exist for the Node lib are reused unchanged. + +## Architecture + +``` +┌────────────────────────────────────────────────────────────┐ +│ bots/src/API/Docs/ (Haskell, existing) │ +│ ├── Types/Commands/Events/Responses │ +│ ├── Syntax.hs (already has pySyntaxText) │ +│ ├── Generate/TypeScript.hs (existing) │ +│ └── Generate/Python.hs ← new │ +└────────────────────┬───────────────────────────────────────┘ + │ tests/APIDocs.hs runs both generators + ▼ writes auto-gen Python type files +┌────────────────────────────────────────────────────────────┐ +│ packages/simplex-chat-python/ (new) │ +│ │ +│ Bot ←── public API: decorators, lifecycle │ +│ └── ChatApi ← escape hatch: raw command access │ +│ └── core ← internal: typed FFI wrapper │ +│ └── _native ← internal: ctypes + lazy DL │ +│ ↓ │ +│ libsimplex.{so,dylib,dll} │ +│ downloaded from simplex-chat-libs releases │ +└────────────────────────────────────────────────────────────┘ +``` + +The split lets each layer be tested independently: `Bot`'s filter-routing without a real libsimplex (mock `api`), `core`'s JSON handling without ctypes (mock `_native`), `_native`'s download/ctypes work with a stub `.so`. Same shape as the Node lib (`bot.ts` → `api.ts` → `core.ts` → `simplex.cc`). + +## Type generation + +### New module: `bots/src/API/Docs/Generate/Python.hs` + +Mirrors `Generate/TypeScript.hs` line-for-line. Reuses the existing data sources (`chatCommandsDocs`, `chatResponsesDocs`, `chatEventsDocs`, `chatTypesDocs`) and `pySyntaxText` from `Syntax.hs`. Output goes to `packages/simplex-chat-python/src/simplex_chat/types/` as four files: `_types.py`, `_commands.py`, `_responses.py`, `_events.py`. + +### Type representation + +Wire types are `TypedDict` + `Literal` discriminators (matches Node lib semantics — just shapes, no runtime cost; pyright narrows tagged unions correctly). + +| Haskell | Python | +|---|---| +| `STRecord` | `class Foo(TypedDict)`; optional fields use `NotRequired[...]`. | +| `STUnion` / `STUnion1` | One `class Foo_(TypedDict)` per member with `type: Literal[""]` discriminator. Type alias `Foo = Foo_A \| Foo_B \| …`. Tag alias `Foo_Tag = Literal["", "", …]`. | +| `STEnum` / `STEnum1` / `STEnum'` | Type alias `Foo = Literal["a", "b", "c"]`. | +| `ATPrim TBool` | `bool` | +| `ATPrim TString` | `str` | +| `ATPrim TInt`/`TInt64`/`TWord32` | `int` | +| `ATPrim TDouble` | `float` | +| `ATPrim TJSONObject` | `dict[str, object]` | +| `ATPrim TUTCTime` | `str` (ISO-8601, comment-annotated) | +| `ATOptional t` | `NotRequired[]` in TypedDict fields; ` \| None` elsewhere | +| `ATArray {nonEmpty=False}` | `list[]` | +| `ATArray {nonEmpty=True}` | `list[]` with trailing `# non-empty` comment | +| `ATMap (PT k) v` | `dict[, ]` | +| `ATDef` / `ATRef` | type name as forward-string reference `""` | + +### Command serialization + +Each command becomes a `TypedDict` plus a `_cmd_string(self) -> str` function. The function body is produced by `pySyntaxText` (which already emits Python expressions for the existing Markdown docs): + +```python +class APICreateMyAddress(TypedDict): + userId: int + +def APICreateMyAddress_cmd_string(self: APICreateMyAddress) -> str: + return '/_address ' + str(self['userId']) + +APICreateMyAddress_Response = CR.UserContactLinkCreated | CR.ChatCmdError +``` + +### Field-naming convention + +| Where | Style | Why | +|---|---|---| +| Auto-generated `types/_*.py` | **camelCase** | Round-trips JSON to/from libsimplex; the keys are the wire format. | +| Hand-written user-facing types (`SqliteDb`, `BotProfile`, `Message`, …) | **snake_case** | These are Python-side configs and wrappers, never reach `chat_send_cmd` directly. | +| `CryptoArgs` (`fileKey`, `fileNonce`) | **camelCase** | Returned by `chat_write_file` as JSON; round-trips wire format. | +| Method names | **snake_case** | PEP 8. | +| Type names | **PascalCase** | PEP 8 + Haskell parity. | + +Generator must emit field names verbatim from `APIRecordField.fieldName'` — never transform. + +### Wiring + +Extend `tests/APIDocs.hs` with four `testGenerate` calls: + +```haskell +describe "Python" $ do + it "generate python commands" $ testGenerate Py.commandsCodeFile Py.commandsCodeText + it "generate python responses" $ testGenerate Py.responsesCodeFile Py.responsesCodeText + it "generate python events" $ testGenerate Py.eventsCodeFile Py.eventsCodeText + it "generate python types" $ testGenerate Py.typesCodeFile Py.typesCodeText +``` + +`cabal test` regenerates all eight files (4 TS + 4 PY) and fails on drift — same enforcement loop that already governs the TypeScript files. + +Add `API.Docs.Generate.Python` to `simplex-chat.cabal:572-580`. + +## Native lib loading + +### Approach: lazy download on first use + +Single pure-Python wheel on PyPI (`simplex-chat`, ~100 KB). On first `Bot(...)` / `ChatApi.init(...)`, the library downloads the matching `libsimplex` zip from `simplex-chat/simplex-chat-libs` releases into a user cache, then `ctypes.CDLL`s it. Subsequent runs read from cache. + +**Why not platform wheels?** Two reasons. First, simpler CI: one `python -m build` job vs a 5-platform matrix that has to download libs zips and rebuild wheels per platform. Second, the libs are already published as the source of truth (existing `release-nodejs-libs` job) — wheels would be a wrapper around those, adding nothing but build complexity. Tradeoff: first run requires internet; air-gap users set `SIMPLEX_LIBS_DIR=/path/to/libs`. + +### Cache layout + +``` +~/.cache/simplex-chat/ # XDG_CACHE_HOME on Linux +└── v6.5.1/ # = LIBS_VERSION + ├── sqlite/libsimplex.so + libHS*.so + └── postgres/libsimplex.so + libHS*.so +``` + +Platform-specific cache base: Linux `$XDG_CACHE_HOME`, macOS `~/Library/Caches/`, Windows `%LOCALAPPDATA%`. + +### Version pinning + +`src/simplex_chat/_version.py`: + +```python +__version__ = "6.5.1" # PEP 440 — bumped with each Python package release +LIBS_VERSION = "6.5.1" # simplex-chat-libs release tag (no 'v' prefix) +``` + +`__version__` is read by hatchling for wheel metadata. `LIBS_VERSION` is read by `_native.py` for the download URL. Wrapper-only patch releases use a post-suffix (`__version__ = "6.5.1.post1"`, `LIBS_VERSION` unchanged). Same pattern as Node lib's `RELEASE_TAG = 'v6.5.1'`. + +### `_native.py` responsibilities + +1. **Detect platform.** `sys.platform` × `platform.machine()` → `linux-x86_64`, `linux-aarch64`, `macos-x86_64`, `macos-aarch64`, `windows-x86_64`. Unsupported combos raise immediately. +2. **Resolve backend** from the `Db` instance (`isinstance(db, SqliteDb)` → sqlite, `PostgresDb` → postgres). Module-level `threading.Lock` guards selection — first call wins for the process; subsequent call with a different backend raises (one libsimplex variant per process — Haskell RTS constraint). +3. **Resolve libs path.** If `SIMPLEX_LIBS_DIR` env is set, use it directly. Otherwise: `~/.cache/simplex-chat/v{LIBS_VERSION}/{backend}/`, downloading if missing. +4. **Download URL** (`LIBS_VERSION` is stored without 'v' prefix; URL re-adds it): + + ``` + https://github.com/simplex-chat/simplex-chat-libs/releases/download/v{LIBS_VERSION}/simplex-chat-libs-{platform}-{arch}{-postgres?}.zip + ``` + +5. **Atomic install.** Download to sibling tempdir → `zipfile.extractall` → `os.replace` the `libs/` subdir into cache. The libs zip contains only regular files (no symlinks — `build.yml:751-781` builds it via `cp *.so` which resolves them), so plain `extractall` is sufficient. Two processes racing safely both extract; whichever rename lands first wins, contents identical. +6. **Load and init.** `ctypes.CDLL(libs_dir / libname)` once per process; declare `restype`/`argtypes` for the 8 FFI functions; call `hs_init_with_rtsopts` exactly once with `+RTS -A64m -H64m -xn --install-signal-handlers=no` (or without `-xn` on Windows — matches `cpp/simplex.cc:13-32`). +7. **Buffer ownership.** Haskell allocates result strings; caller must `free()` after copying. Declare `restype = c_void_p` (NOT `c_char_p`, which auto-converts to bytes and discards the pointer needed for free): + + ```python + ptr = lib.chat_send_cmd(ctrl, cmd_bytes) + if not ptr: raise RuntimeError("null result") + try: result = ctypes.string_at(ptr).decode("utf-8") + finally: libc.free(ptr) + ``` + + `libc` is `ctypes.CDLL(None)` on Linux/macOS, `ctypes.CDLL("msvcrt")` on Windows. Mirrors `HandleCResult` in `cpp/simplex.cc:157-165`. + +### Override / pre-fetch + +```bash +# Skip download — for Docker / air-gapped +SIMPLEX_LIBS_DIR=/opt/simplex/libs python my_bot.py + +# Pre-fetch in Dockerfile RUN step (avoids redundant download per container start) +python -m simplex_chat install --backend=sqlite +python -m simplex_chat install --backend=postgres +``` + +### Failure modes + +| Condition | Behavior | +|---|---| +| Unsupported platform/arch | Raise on first FFI call with explicit list of supported combinations. | +| Postgres on non-Linux-x86_64 | Raise — matches existing constraint in `download-libs.js:15-18`. | +| Download network/HTTP error | Propagate `urllib.error.URLError` with the URL. | +| Two processes downloading simultaneously | Both extract to sibling temp dirs; rename is atomic; identical contents → safe. | +| Two `Bot()` / `ChatApi.init()` calls in same process with different backends | Second raises — one libsimplex variant per process. | +| Two `Bot()` instances same backend, same process | Permitted — each has its own controller (`chat_ctrl`). | + +## Public API + +See the [Architecture](#architecture) section for the layering. This section specifies each layer's surface. + +### Construction + +User-facing config types are `@dataclass(slots=True)`, snake_case fields. + +```python +@dataclass(slots=True) +class SqliteDb: + file_prefix: str + encryption_key: str | None = None + +@dataclass(slots=True) +class PostgresDb: + connection_string: str + schema_prefix: str | None = None + +Db = SqliteDb | PostgresDb # discriminated by isinstance() + +@dataclass(slots=True) +class BotProfile: + display_name: str + full_name: str = "" + short_descr: str | None = None + image: str | None = None + +@dataclass(slots=True) +class BotCommand: + keyword: str + label: str + +class Bot: + def __init__( + self, *, + profile: BotProfile, + db: Db, + welcome: str | T.MsgContent | None = None, + commands: list[BotCommand] | None = None, # None → [] + confirm_migrations: MigrationConfirmation = MigrationConfirmation.YES_UP, + # behavioral toggles — mirror BotOptions in Node lib + create_address: bool = True, + update_address: bool = True, + update_profile: bool = True, + auto_accept: bool = True, + business_address: bool = False, + allow_files: bool = False, + use_bot_profile: bool = True, + log_contacts: bool = True, + log_network: bool = False, + ) -> None: ... + + @property + def api(self) -> ChatApi: ... +``` + +### Handler registration + +Three decorators. Filters are kwargs combined with **AND**; tuples within a kwarg are **OR**; arbitrary predicates use `when=`. + +```python +class Bot: + def on_message(self, *, + content_type: T.MsgContent_Tag | tuple[T.MsgContent_Tag, ...] | None = None, + text: str | re.Pattern | None = None, # exact match or regex.search() + chat_type: T.ChatType | tuple[T.ChatType, ...] | None = None, # direct/group/local + from_role: T.GroupMemberRole | tuple[T.GroupMemberRole, ...] | None = None, + from_contact_id: int | tuple[int, ...] | None = None, + from_member_id: int | tuple[int, ...] | None = None, + group_id: int | tuple[int, ...] | None = None, + when: Callable[[Message], bool] | None = None, + ) -> Callable[[MessageHandler], MessageHandler]: ... + + def on_command(self, name: str | tuple[str, ...], *, + args: str | re.Pattern | None = None, # match command argument string + chat_type: T.ChatType | tuple[T.ChatType, ...] | None = None, + from_role: T.GroupMemberRole | tuple[T.GroupMemberRole, ...] | None = None, + from_contact_id: int | tuple[int, ...] | None = None, + group_id: int | tuple[int, ...] | None = None, + when: Callable[[Message], bool] | None = None, + ) -> Callable[[CommandHandler], CommandHandler]: ... + + # Multiple handlers per tag dispatch in registration order. + def on_event(self, event: CEvt.Tag, /, + ) -> Callable[[EventHandler], EventHandler]: ... + + def use(self, middleware: Middleware) -> None: ... + +MessageHandler = Callable[[Message], Awaitable[None] | None] +CommandHandler = Callable[[Message, ParsedCommand], Awaitable[None] | None] +EventHandler = Callable[[CEvt.ChatEvent], Awaitable[None] | None] +``` + +`from_role` on direct chats: silent skip (not a runtime error). + +### Message wrapper and content-narrowed types + +`Message[C]` is generic in its content variant; concrete subclass aliases (`TextMessage`, `ImageMessage`, …) bind to the auto-generated `T.MsgContent_*` types. Decorator overloads narrow the handler parameter when `content_type` is a single `Literal`, so pyright sees the right concrete type. + +```python +C = TypeVar("C", bound=T.MsgContent) # bound covers the unparameterized case + +@dataclass(slots=True, frozen=True) +class Message(Generic[C]): + chat_item: T.AChatItem # raw wire object — fields below this point are camelCase + content: C # narrowed when filter pins content_type + bot: Bot + + @property + def chat_info(self) -> T.ChatInfo: ... # shortcut for chat_item["chatInfo"] + @property + def text(self) -> str | None: ... # shortcut; non-Optional for TextMessage + + async def reply(self, text: str) -> Message: ... + async def reply_content(self, content: T.MsgContent) -> Message: ... + async def react(self, emoji: str) -> None: ... + async def delete(self) -> None: ... + async def forward(self, to: T.ChatRef) -> Message: ... + +# Concrete narrowed aliases — exported from simplex_chat/__init__.py +TextMessage = Message[T.MsgContent_Text] +ImageMessage = Message[T.MsgContent_Image] +FileMessage = Message[T.MsgContent_File] +VoiceMessage = Message[T.MsgContent_Voice] +# … one per MsgContent variant + +@dataclass(slots=True, frozen=True) +class ParsedCommand: + keyword: str + args: str +``` + +Decorator overloads (one per `T.MsgContent_*` variant — ~15 lines, hand-written in `bot.py`): + +```python +class Bot: + @overload + def on_message(self, *, content_type: Literal["text"], **rest: Any + ) -> Callable[[Callable[[TextMessage], Awaitable[None] | None]], ...]: ... + @overload + def on_message(self, *, content_type: Literal["image"], **rest: Any + ) -> Callable[[Callable[[ImageMessage], Awaitable[None] | None]], ...]: ... + # … one overload per MsgContent variant … + @overload + def on_message(self, *, content_type: tuple[T.MsgContent_Tag, ...] | None = None, + **rest: Any) -> Callable[[Callable[[Message], Awaitable[None] | None]], ...]: ... +``` + +`@bot.on_message(content_type="text")` → handler typed as `TextMessage`, so `msg.text: str` (non-Optional). + +**Field-naming boundary in `Message`.** Wrapper properties (`msg.chat_info`, `msg.content`, `msg.text`) are snake_case. Descending into raw wire data via `msg.chat_item[...]` reverts to camelCase — same as accessing `T.AChatItem` returned by `bot.api`. Property shortcuts cover the common paths so most handlers never touch `chat_item` directly. + +### Lifecycle + +```python +class Bot: + # Blocking convenience — runs asyncio.run(self.serve_forever()), installs SIGINT + # via loop.add_signal_handler() (POSIX) or signal.signal() (Windows). Recommended for scripts. + def run(self) -> None: ... + + # Embedding form — caller owns the loop and signal handling. + async def __aenter__(self) -> Bot: ... + async def __aexit__(self, *exc_info: object) -> None: ... + + # Concurrent calls raise RuntimeError("already serving"). Re-callable after a clean stop(). + async def serve_forever(self) -> None: ... + + # Marks bot for shutdown. Safe from signal handler, another coroutine, or another thread. + def stop(self) -> None: ... +``` + +### Middleware + +aiogram pattern. A class with `async __call__(handler, message, data)` wraps every **handler invocation** (per-message-per-matching-handler). `data: dict[str, object]` is the cross-cutting injection channel. + +```python +class Middleware: + async def __call__(self, + handler: Callable[[Message, dict[str, object]], Awaitable[None]], + message: Message, + data: dict[str, object]) -> None: + await handler(message, data) +``` + +- **Invocation count.** A `newChatItems` event with N items × M matching handlers triggers N×M middleware calls. Per-event hooks should use `on_event`. +- **Exception propagation.** Handler exceptions propagate outward through the middleware stack. The outermost middleware can catch and swallow. Uncaught exceptions are logged via `logging.getLogger("simplex_chat")` and the chain moves to the next handler — the bot does not stop on individual handler errors. The receive loop only stops on a fatal `_native`/`core` error or explicit `bot.stop()`. +- **Order.** Registered via `bot.use(...)`. Called in registration order (first registered = outermost wrap). + +### Event-loop semantics + +`Bot` runs one event-receiver coroutine looping `chat_recv_msg_wait` (in `asyncio.to_thread`): + +1. All `on_event(tag)` handlers for the event's `type` field — registration order, sequentially. +2. If event is `newChatItems`: for each chat item, run **all matching message handlers** (each through the middleware stack, in registration order). For each command-parseable text item, also run matching command handlers. + +Handlers run **sequentially within an event**. Events are processed **sequentially**. Long-running work that shouldn't block the next event must `asyncio.create_task(...)` explicitly. + +`bot.api.api_xxx(...)` calls are safe during `serve_forever` — same controller, serialized through `chat_send_cmd`. Calling them from inside a handler is the normal pattern (`msg.reply()` does exactly this). + +### `ChatApi` (escape hatch) + +Reached via `bot.api`. ~40 async methods, one per Node `apiXxx` (api.ts:344-958). Full enumeration deferred to implementation; representative examples: + +```python +class ChatApi: + @classmethod + async def init(cls, db: Db, + confirm: MigrationConfirmation = MigrationConfirmation.YES_UP) -> ChatApi: ... + async def start_chat(self) -> None: ... + async def stop_chat(self) -> None: ... + async def close(self) -> None: ... + async def send_chat_cmd(self, cmd: str) -> CR.ChatResponse: ... + async def recv_chat_event(self, wait_us: int = 5_000_000) -> CEvt.ChatEvent | None: ... + + async def api_create_user_address(self, user_id: int) -> T.CreatedConnLink: ... + async def api_send_text_message(self, chat: T.ChatRef, text: str, + in_reply_to: int | None = None) -> list[T.AChatItem]: ... + async def api_get_chats(self, user_id: int, pagination: T.PaginationByTime, + query: T.ChatListQuery | None = None) -> list[T.AChat]: ... + # ... etc +``` + +TS `apiCreateUserAddress` → Python `api_create_user_address` (PEP 8). Wire-format type names (`T.AChatItem`, `T.UserContactLink`, …) keep their Haskell/TS spelling to match JSON keys. + +### Embedding example + +```python +import asyncio +from simplex_chat import Bot, BotProfile, SqliteDb + +async def main(): + async with Bot(profile=..., db=...) as bot: + @bot.on_message(content_type="text") + async def echo(msg): + await msg.reply(msg.text) + await asyncio.gather(bot.serve_forever(), other_task()) + +asyncio.run(main()) +``` + +## Distribution and CI + +### Project layout + +``` +packages/simplex-chat-python/ +├── pyproject.toml # hatchling, requires-python >= 3.11, no runtime deps +├── README.md +├── LICENSE # AGPL-3.0 +├── src/simplex_chat/ +│ ├── __init__.py # exports Bot, BotProfile, BotCommand, SqliteDb, PostgresDb, +│ │ # Message + TextMessage/ImageMessage/… aliases, ParsedCommand, +│ │ # ChatApi, MigrationConfirmation, Middleware, ChatAPIError +│ ├── _version.py # __version__ + LIBS_VERSION +│ ├── _native.py # ctypes + lazy lib download (internal) +│ ├── __main__.py # python -m simplex_chat install ... +│ ├── core.py # internal typed FFI wrapper +│ ├── api.py # ChatApi class — escape hatch +│ ├── bot.py # Bot class, decorators, Message wrapper, lifecycle +│ ├── filters.py # filter kwarg compilation; predicate combinators +│ ├── util.py # stateless helpers (chat_info_ref, ci_content_text, reaction_text, …) +│ ├── py.typed # PEP 561 marker +│ └── types/ +│ ├── __init__.py # re-exports T, CC, CR, CEvt +│ ├── _types.py # AUTOGEN +│ ├── _commands.py # AUTOGEN +│ ├── _responses.py # AUTOGEN +│ └── _events.py # AUTOGEN +├── examples/ +│ └── squaring_bot.py +└── tests/ +``` + +### `pyproject.toml` + +```toml +[build-system] +requires = ["hatchling>=1.24"] +build-backend = "hatchling.build" + +[project] +name = "simplex-chat" +description = "SimpleX Chat Python library for chat bots" +license = "AGPL-3.0" +authors = [{name = "SimpleX Chat"}] +requires-python = ">=3.11" +dynamic = ["version"] + +[tool.hatch.version] +path = "src/simplex_chat/_version.py" + +[tool.hatch.build.targets.wheel] +packages = ["src/simplex_chat"] +``` + +No runtime Python dependencies (ctypes, urllib, zipfile are stdlib). + +### CI publishing + +One job appended to `.github/workflows/build.yml`, after `release-nodejs-libs`: + +```yaml +publish-python: + needs: [release-nodejs-libs] + if: startsWith(github.ref, 'refs/tags/v') + runs-on: ubuntu-latest + permissions: { id-token: write } # OIDC, no API key + steps: + - uses: actions/checkout@v6 + - uses: actions/setup-python@v5 + with: { python-version: "3.11" } + - run: pip install build && python -m build --wheel + working-directory: packages/simplex-chat-python + - uses: pypa/gh-action-pypi-publish@release/v1 + with: { packages-dir: packages/simplex-chat-python/dist } +``` + +Triggered by the same `vX.Y.Z` tag that already drives the desktop and libs releases. + +### One-time setup + +1. Verify PyPI package name `simplex-chat` is available; register it. +2. On PyPI project page, configure trusted publisher → repo `simplex-chat/simplex-chat`, workflow `build.yml`, job `publish-python`. + +## Testing + +Three levels: + +1. **Codegen drift** — `tests/APIDocs.hs` adds Python generators alongside TypeScript. Same `testGenerate` mechanism enforces that committed `_types.py` etc. equal the generator output. + +2. **Python unit tests** — `pytest`, no real libsimplex needed: + - `test_native.py`: mock `urllib.request.urlretrieve` + `zipfile.ZipFile`; assert correct URL, atomic rename, cache hit on second call, override-env behavior, postgres-on-mac rejection. + - `test_codegen.py`: import every type from `simplex_chat.types`, sanity-check that `T.ChatType` is `Literal[...]` of expected size, etc. Catches generator regressions. + - `test_smoke.py`: build a fake `.so` (small C file with stub `chat_send_cmd` returning canned JSON, compiled per-test), point `SIMPLEX_LIBS_DIR` at it, run `Bot.__aenter__` → handler dispatch. Verifies FFI plumbing without real Haskell. + +3. **Integration** — `examples/squaring_bot.py` runs against real libsimplex. Not in CI (needs network + persistent state). + +## Open questions + +1. **Linux ARM64.** Existing `simplex-chat-libs` releases ship `linux-x86_64`, `macos-x86_64`, `macos-aarch64`, `windows-x86_64` — no `linux-aarch64`. Python lib will fail with a clear message there. Adding it requires changes to the existing `release-nodejs-libs` job in `build.yml` (out of scope for this spec). + +2. **`asyncio.to_thread` pool sizing.** Long-blocking `chat_recv_msg_wait` calls (default 5 s) pin executor threads. The default asyncio pool is unbounded but recycled. Bots running many concurrent chats may need a custom executor — first-pass uses `asyncio.to_thread`; document recommended pool sizing in README if it becomes a problem. diff --git a/plans/2026-05-07-simplex-chat-python-implementation.md b/plans/2026-05-07-simplex-chat-python-implementation.md new file mode 100644 index 0000000000..b1ff5b951c --- /dev/null +++ b/plans/2026-05-07-simplex-chat-python-implementation.md @@ -0,0 +1,2348 @@ +# SimpleX Chat Python library — implementation plan + +> **For agentic workers:** Use superpowers-extended-cc:subagent-driven-development (if subagents available) or superpowers-extended-cc:executing-plans to implement this plan. Steps use checkbox (`- [ ]`) syntax for tracking. + +**Goal:** Ship `simplex-chat` on PyPI — a Python 3 client library for SimpleX bots with the same capability as the existing Node.js library. + +**Architecture:** Two repositories of work in this monorepo: extend the Haskell type generator (`bots/src/API/Docs/`) to emit Python types alongside TypeScript, and add a new Python package (`packages/simplex-chat-python/`) that wraps the existing prebuilt `libsimplex.{so,dylib,dll}` via ctypes. Lazy download of libs from `simplex-chat/simplex-chat-libs` GitHub releases. Async-only public API with decorator-registered handlers. Single PyPI wheel. + +**Tech Stack:** Haskell (existing codegen), Python 3.11+ (ctypes, asyncio, hatchling), GitHub Actions (publish-python job in existing build.yml). + +**Spec:** [`plans/2026-05-07-simplex-chat-python-design.md`](./2026-05-07-simplex-chat-python-design.md) + +--- + +## Plan structure + +Eight phases, executed in order: + +1. Type generation (Haskell) — `Generate/Python.hs` + `tests/APIDocs.hs` wiring. +2. Python package scaffold — `pyproject.toml`, `_version.py`, layout, hatch config. +3. Native FFI layer — `_native.py` (lazy download, ctypes, hs_init, buffer ownership). +4. Core wrapper — `core.py` (typed async FFI). +5. ChatApi — escape-hatch class with raw + ~40 high-level methods. +6. Bot class — decorators, filters, Message wrapper, lifecycle, middleware. +7. Tests + CLI — pytest suite, `python -m simplex_chat install`. +8. CI publishing — append `publish-python` job to `.github/workflows/build.yml`. + +Each phase is a chunk. Phase boundaries are natural review points. + +--- + +## Chunk 1: Type generation (Haskell) + +Add `bots/src/API/Docs/Generate/Python.hs`, mirror `Generate/TypeScript.hs`, wire into the test suite. + +### Task 1.1: Create `Generate/Python.hs` skeleton + +**Files:** +- Create: `bots/src/API/Docs/Generate/Python.hs` + +- [ ] **Step 1: Copy `Generate/TypeScript.hs` as starting point** + +```bash +cp bots/src/API/Docs/Generate/TypeScript.hs bots/src/API/Docs/Generate/Python.hs +``` + +- [ ] **Step 2: Rename module and update output paths** + +Edit the new file: +- Module: `module API.Docs.Generate.Python where` +- File constants: + ```haskell + commandsCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_commands.py" + responsesCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_responses.py" + eventsCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_events.py" + typesCodeFile = "./packages/simplex-chat-python/src/simplex_chat/types/_types.py" + ``` + +- [ ] **Step 3: Register module in cabal manifest** + +In `simplex-chat.cabal`, find the `other-modules:` list of the `simplex-chat-test` test-suite stanza (`type: exitcode-stdio-1.0`, `main-is: Test.hs`). Insert `API.Docs.Generate.Python` alphabetically between `API.Docs.Generate` and `API.Docs.Responses`. + +- [ ] **Step 4: Verify cabal compiles** + +``` +cabal build simplex-chat-test +``` +Expected: builds without error (the file is still TS-shaped but Haskell-valid). + +- [ ] **Step 5: Commit scaffolding** + +```bash +git add bots/src/API/Docs/Generate/Python.hs simplex-chat.cabal +git commit -m "feat(bots): add Python codegen module skeleton" +``` + +### Task 1.2: Implement Python type rendering + +Replace TypeScript-specific output with Python equivalents per the spec's [type-mapping rules](./2026-05-07-simplex-chat-python-design.md#type-representation). + +**Files:** +- Modify: `bots/src/API/Docs/Generate/Python.hs` + +- [ ] **Step 1: Rewrite `typesCodeText`** + +Output structure: +```python +# API Types +# This file is generated automatically. +from typing import Literal, NotRequired, TypedDict + +# … one class / type alias per chatTypesDocs entry … +``` + +Translation rules (mirror `TypeScript.hs` `typeCode`): +- `ATDRecord fields` → `class (TypedDict):` with translated field types. +- `ATDEnum cs` → ` = Literal["c1", "c2", …]`. +- `ATDUnion cs` → tagged TypedDicts + union alias + tag alias (see `unionTypeCode` in TS). + +Field-type translation (table in spec; mirrors `TypeScript.hs` `fieldsCode` `typeText`): +- `ATPrim` primitives → Python primitives (`bool`, `str`, `int`, `float`, `dict[str, object]`). +- `ATOptional t` inside TypedDict → `NotRequired[]`; elsewhere ` | None`. +- `ATArray {elemType, nonEmpty}` → `list[]`, append `# non-empty` comment when `nonEmpty=True`. +- `ATMap (PT k) v` → `dict[, ]`. +- `ATDef` / `ATRef` → forward-string reference `""`. + +- [ ] **Step 2: Rewrite `responsesCodeText` and `eventsCodeText`** + +Both produce union types — same shape as TS's `unionTypeCode`. Output structure: + +```python +# API Responses +# This file is generated automatically. +from . import _types as T + +ChatResponse_ = TypedDict(...) +ChatResponse_ = TypedDict(...) +… +ChatResponse = ChatResponse_ | ChatResponse_ | … +ChatResponse_Tag = Literal["", "", …] +``` + +- [ ] **Step 3: Rewrite `commandsCodeText`** + +Each command becomes a TypedDict + a `_cmd_string(self) -> str` function + a Response alias. Function body comes from `pySyntaxText` in `Syntax.hs:160` (no changes to that function — it's already correct and used today by Markdown docs). + +```python +# API Commands +# This file is generated automatically. +import json +from typing import TypedDict +from . import _types as T +from . import _responses as CR + +class APICreateMyAddress(TypedDict): + userId: int + +def APICreateMyAddress_cmd_string(self: APICreateMyAddress) -> str: + return '/_address ' + str(self['userId']) + +APICreateMyAddress_Response = CR.UserContactLinkCreated | CR.ChatCmdError +``` + +The `cmdString` body: invoke `pySyntaxText (constrName, params) syntax` analogously to TS's `funcCode`. + +- [ ] **Step 4: Build to verify Haskell compiles** + +``` +cabal build simplex-chat-test +``` +Expected: clean build. + +- [ ] **Step 5: Commit** + +```bash +git add bots/src/API/Docs/Generate/Python.hs +git commit -m "feat(bots): implement Python type generation" +``` + +### Task 1.3: Wire generators into `tests/APIDocs.hs` + +**Files:** +- Modify: `tests/APIDocs.hs` + +- [ ] **Step 1: Add import** + +Insert after line 11 (`import qualified API.Docs.Generate.TypeScript as TS`): + +```haskell +import qualified API.Docs.Generate.Python as Py +``` + +- [ ] **Step 2: Add four `testGenerate` calls** + +Inside `apiDocsTest`, after the existing `describe "TypeScript"` block (line 40-44), add: + +```haskell +describe "Python" $ do + it "generate python commands code" $ testGenerate Py.commandsCodeFile Py.commandsCodeText + it "generate python responses code" $ testGenerate Py.responsesCodeFile Py.responsesCodeText + it "generate python events code" $ testGenerate Py.eventsCodeFile Py.eventsCodeText + it "generate python types code" $ testGenerate Py.typesCodeFile Py.typesCodeText +``` + +- [ ] **Step 3: Create empty target directory** + +```bash +mkdir -p packages/simplex-chat-python/src/simplex_chat/types +``` + +- [ ] **Step 4: Run the API docs tests — they will write the four files** + +``` +cabal test simplex-chat-test --test-options="--match \"API\"" +``` + +First run: tests fail because the on-disk files are empty / missing. The `testGenerate` mechanism overwrites the file with generated content, so the second run passes. + +``` +cabal test simplex-chat-test --test-options="--match \"Python\"" +``` + +Expected: PASS on the second run. + +- [ ] **Step 5: Sanity-check generated output** + +Eyeball each of the four generated files: + +```bash +head -50 packages/simplex-chat-python/src/simplex_chat/types/_types.py +head -30 packages/simplex-chat-python/src/simplex_chat/types/_commands.py +head -30 packages/simplex-chat-python/src/simplex_chat/types/_responses.py +head -30 packages/simplex-chat-python/src/simplex_chat/types/_events.py +``` + +Verify: starts with `# This file is generated automatically.`, contains valid-looking Python, no obvious junk like `` markers. + +- [ ] **Step 6: Run them through Python parser to verify syntax** + +``` +python -c "import ast; [ast.parse(open(f).read()) for f in ['packages/simplex-chat-python/src/simplex_chat/types/_types.py', 'packages/simplex-chat-python/src/simplex_chat/types/_commands.py', 'packages/simplex-chat-python/src/simplex_chat/types/_responses.py', 'packages/simplex-chat-python/src/simplex_chat/types/_events.py']]" +``` + +Expected: no exception. Any `SyntaxError` indicates a generator bug — fix in `Generate/Python.hs` and re-run cabal test. + +- [ ] **Step 7: Commit generated artifacts** + +```bash +git add tests/APIDocs.hs packages/simplex-chat-python/src/simplex_chat/types/ +git commit -m "feat(bots): wire Python generators into APIDocs test suite" +``` + +### Task 1.4: Add `types/__init__.py` re-exporting namespaces + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/types/__init__.py` + +- [ ] **Step 1: Write namespace re-exports** + +```python +from . import _types as T +from . import _commands as CC +from . import _responses as CR +from . import _events as CEvt + +__all__ = ["T", "CC", "CR", "CEvt"] +``` + +- [ ] **Step 2: Verify import works** + +``` +python -c "from simplex_chat.types import T, CC, CR, CEvt; print(T, CC, CR, CEvt)" +``` + +(Run from `packages/simplex-chat-python/src/`.) + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/types/__init__.py +git commit -m "feat(python): add types namespace re-exports" +``` + +--- + +## Chunk 2: Python package scaffold + +Set up the package skeleton — `pyproject.toml`, version pinning, top-level `__init__.py`, AGPL license, README placeholder. + +### Task 2.1: Create `pyproject.toml` + +**Files:** +- Create: `packages/simplex-chat-python/pyproject.toml` + +- [ ] **Step 1: Write hatchling build config** + +```toml +[build-system] +requires = ["hatchling>=1.24"] +build-backend = "hatchling.build" + +[project] +name = "simplex-chat" +description = "SimpleX Chat Python library for chat bots" +readme = "README.md" +license = "AGPL-3.0-only" +authors = [{name = "SimpleX Chat"}] +requires-python = ">=3.11" +keywords = ["simplex", "messenger", "chat", "privacy", "security", "bots"] +classifiers = [ + "Development Status :: 4 - Beta", + "License :: OSI Approved :: GNU Affero General Public License v3", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Topic :: Communications :: Chat", +] +dynamic = ["version"] + +[project.urls] +Homepage = "https://github.com/simplex-chat/simplex-chat/tree/stable/packages/simplex-chat-python" +Issues = "https://github.com/simplex-chat/simplex-chat/issues" + +[project.optional-dependencies] +test = ["pytest>=8", "pytest-asyncio>=0.23"] +dev = ["pytest>=8", "pytest-asyncio>=0.23", "pyright>=1.1.380", "ruff>=0.6"] + +[tool.hatch.version] +path = "src/simplex_chat/_version.py" + +[tool.hatch.build.targets.wheel] +packages = ["src/simplex_chat"] + +[tool.pytest.ini_options] +asyncio_mode = "auto" +``` + +- [ ] **Step 2: Commit** + +```bash +git add packages/simplex-chat-python/pyproject.toml +git commit -m "feat(python): add pyproject.toml with hatchling backend" +``` + +### Task 2.2: Create `_version.py` + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/_version.py` + +- [ ] **Step 1: Write version constants** + +```python +"""Single source of truth for both the Python package version and the +simplex-chat-libs release tag we depend on. + +Bump both together for normal releases. For wrapper-only fixes use a PEP 440 +post-release: __version__ = "6.5.1.post1", LIBS_VERSION unchanged. +""" + +__version__ = "6.5.1" # PEP 440 — read by hatchling for wheel metadata +LIBS_VERSION = "6.5.1" # simplex-chat-libs release tag (no 'v' prefix) +``` + +- [ ] **Step 2: Verify hatchling can read the version** + +``` +cd packages/simplex-chat-python && python -m build --wheel +``` + +Expected: produces `dist/simplex_chat-6.5.1-py3-none-any.whl`. (Wheel will be incomplete — only the types module is in src/ at this point — but build should succeed.) + +Clean up: `rm -rf packages/simplex-chat-python/dist packages/simplex-chat-python/src/simplex_chat.egg-info` + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/_version.py +git commit -m "feat(python): add _version.py with package + libs version pinning" +``` + +### Task 2.3: Add `py.typed` marker, README placeholder, AGPL license + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/py.typed` +- Create: `packages/simplex-chat-python/README.md` +- Create: `packages/simplex-chat-python/LICENSE` + +- [ ] **Step 1: Touch the empty `py.typed` marker** + +```bash +touch packages/simplex-chat-python/src/simplex_chat/py.typed +``` + +- [ ] **Step 2: Copy AGPL license from a sibling package** + +```bash +cp packages/simplex-chat-nodejs/LICENSE packages/simplex-chat-python/LICENSE +``` + +- [ ] **Step 3: Write a minimal README** + +```markdown +# SimpleX Chat Python library + +Python 3 client library for [SimpleX Chat](https://simplex.chat) bots. + +Equivalent to the [Node.js library](https://www.npmjs.com/package/simplex-chat). + +## Installation + + pip install simplex-chat + +Requires Python 3.11+. + +## Quick start + +[example to be added] + +## License + +[AGPL-3.0](./LICENSE) +``` + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/py.typed \ + packages/simplex-chat-python/README.md \ + packages/simplex-chat-python/LICENSE +git commit -m "feat(python): add py.typed marker, README, AGPL license" +``` + +### Task 2.4: Top-level `__init__.py` with empty exports + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/__init__.py` + +- [ ] **Step 1: Write a placeholder that re-exports from submodules as they appear** + +```python +"""SimpleX Chat — Python client library for chat bots.""" + +from ._version import __version__ + +__all__ = ["__version__"] +``` + +(Will be expanded as `Bot`, `ChatApi`, etc. land in later phases.) + +- [ ] **Step 2: Verify import** + +``` +python -c "import simplex_chat; print(simplex_chat.__version__)" +``` + +Run from `packages/simplex-chat-python/src/`. + +Expected: prints `6.5.1`. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/__init__.py +git commit -m "feat(python): add top-level package __init__.py" +``` + +--- + +## Chunk 3: Native FFI layer (`_native.py`) + +Lazy lib download, platform detection, ctypes signatures, `hs_init_with_rtsopts`, atomic install, buffer ownership. Single most-error-prone piece of the package — give it tests. + +### Task 3.1: `_native.py` — platform detection + URL building + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/_native.py` +- Create: `packages/simplex-chat-python/tests/test_native_url.py` + +- [ ] **Step 1: Write platform-detection tests** + +```python +# tests/test_native_url.py +from unittest.mock import patch +import pytest +from simplex_chat._native import _platform_tag, _libs_url, _libname + +@patch("sys.platform", "linux") +@patch("platform.machine", return_value="x86_64") +def test_platform_linux_x64(_): + assert _platform_tag() == "linux-x86_64" + +@patch("sys.platform", "darwin") +@patch("platform.machine", return_value="arm64") +def test_platform_macos_arm64(_): + assert _platform_tag() == "macos-aarch64" + +@patch("sys.platform", "win32") +@patch("platform.machine", return_value="AMD64") +def test_platform_windows_x64(_): + assert _platform_tag() == "windows-x86_64" + +@patch("sys.platform", "freebsd") +@patch("platform.machine", return_value="x86_64") +def test_platform_unsupported(_): + with pytest.raises(RuntimeError, match="Unsupported"): + _platform_tag() + +def test_libname_per_platform(): + with patch("sys.platform", "linux"): + assert _libname() == "libsimplex.so" + with patch("sys.platform", "darwin"): + assert _libname() == "libsimplex.dylib" + with patch("sys.platform", "win32"): + assert _libname() == "libsimplex.dll" + +@patch("simplex_chat._native._platform_tag", return_value="linux-x86_64") +def test_url_sqlite(_): + assert _libs_url("sqlite") == \ + "https://github.com/simplex-chat/simplex-chat-libs/releases/download/" \ + "v6.5.1/simplex-chat-libs-linux-x86_64.zip" + +@patch("simplex_chat._native._platform_tag", return_value="linux-x86_64") +def test_url_postgres(_): + assert _libs_url("postgres") == \ + "https://github.com/simplex-chat/simplex-chat-libs/releases/download/" \ + "v6.5.1/simplex-chat-libs-linux-x86_64-postgres.zip" +``` + +- [ ] **Step 2: Write `_native.py` skeleton with platform + URL helpers** + +```python +"""Native libsimplex loader: platform detection, lazy download, ctypes setup. + +Internal — users interact with `Bot` / `ChatApi`, never with this module. +""" +from __future__ import annotations + +import ctypes +import errno +import os +import platform +import sys +import tempfile +import threading +import urllib.request +import zipfile +from ctypes import POINTER, c_char_p, c_int, c_uint8, c_void_p +from pathlib import Path +from typing import Literal + +from ._version import LIBS_VERSION + +Backend = Literal["sqlite", "postgres"] + +_GITHUB_REPO = "simplex-chat/simplex-chat-libs" + +_PLATFORM_MAP = { + "linux": ("linux", {"x86_64": "x86_64", "aarch64": "aarch64"}), + "darwin": ("macos", {"x86_64": "x86_64", "arm64": "aarch64"}), + "win32": ("windows", {"AMD64": "x86_64", "x86_64": "x86_64"}), +} + +_LIBNAME = {"linux": "libsimplex.so", "darwin": "libsimplex.dylib", "win32": "libsimplex.dll"} + +SUPPORTED = ( + "linux-x86_64", "linux-aarch64", + "macos-x86_64", "macos-aarch64", + "windows-x86_64", +) + + +def _platform_tag() -> str: + info = _PLATFORM_MAP.get(sys.platform) + if not info: + raise RuntimeError(f"Unsupported platform: {sys.platform}") + sysname, archs = info + arch = archs.get(platform.machine()) + if not arch: + raise RuntimeError(f"Unsupported architecture: {sys.platform}/{platform.machine()}") + tag = f"{sysname}-{arch}" + if tag not in SUPPORTED: + raise RuntimeError(f"Unsupported combination: {tag}; supported: {SUPPORTED}") + return tag + + +def _libname() -> str: + return _LIBNAME[sys.platform] + + +def _libs_url(backend: Backend) -> str: + suffix = "-postgres" if backend == "postgres" else "" + return ( + f"https://github.com/{_GITHUB_REPO}/releases/download/" + f"v{LIBS_VERSION}/simplex-chat-libs-{_platform_tag()}{suffix}.zip" + ) +``` + +- [ ] **Step 3: Run tests** + +``` +cd packages/simplex-chat-python && pip install -e . && pip install pytest +PYTHONPATH=src pytest tests/test_native_url.py -v +``` + +Expected: all PASS. + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/_native.py \ + packages/simplex-chat-python/tests/test_native_url.py +git commit -m "feat(python): _native platform detection + URL building" +``` + +### Task 3.2: `_native.py` — cache resolution + lazy download + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/_native.py` +- Create: `packages/simplex-chat-python/tests/test_native_cache.py` + +- [ ] **Step 1: Write cache-resolution tests** + +```python +# tests/test_native_cache.py +import zipfile +from pathlib import Path + +import pytest + +from simplex_chat._native import _cache_root, _resolve_libs_dir, _download + + +def test_cache_root_linux(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + assert _cache_root() == tmp_path / "simplex-chat" + +def test_cache_root_macos(tmp_path, monkeypatch): + monkeypatch.setattr("sys.platform", "darwin") + monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path) + assert _cache_root() == tmp_path / "Library" / "Caches" / "simplex-chat" + +def test_override_via_env(tmp_path, monkeypatch): + # _resolve_libs_dir intentionally does not validate the override directory — + # it returns it verbatim; the eventual ctypes.CDLL call surfaces any mistake. + monkeypatch.setenv("SIMPLEX_LIBS_DIR", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + assert _resolve_libs_dir("sqlite") == tmp_path + +def test_resolve_downloads_when_missing(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "linux-x86_64") + + called = {} + def fake_download(target_root: Path, backend: str) -> None: + called["target"] = target_root + called["backend"] = backend + target_root.mkdir(parents=True, exist_ok=True) + (target_root / "libsimplex.so").touch() + + monkeypatch.setattr("simplex_chat._native._download", fake_download) + libs_dir = _resolve_libs_dir("sqlite") + assert libs_dir == tmp_path / "simplex-chat" / "v6.5.1" / "sqlite" + assert called["backend"] == "sqlite" + assert (libs_dir / "libsimplex.so").exists() + +def test_resolve_uses_cache_on_second_call(tmp_path, monkeypatch): + monkeypatch.setenv("XDG_CACHE_HOME", str(tmp_path)) + monkeypatch.setattr("sys.platform", "linux") + cached = tmp_path / "simplex-chat" / "v6.5.1" / "sqlite" + cached.mkdir(parents=True) + (cached / "libsimplex.so").touch() + # Should NOT call _download — use the cached file. + monkeypatch.setattr("simplex_chat._native._download", + lambda *a: pytest.fail("download should not be called")) + assert _resolve_libs_dir("sqlite") == cached + +def test_postgres_on_macos_rejected(monkeypatch): + monkeypatch.setattr("sys.platform", "darwin") + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "macos-aarch64") + with pytest.raises(RuntimeError, match="postgres.*linux-x86_64"): + _resolve_libs_dir("postgres") + +def test_atomic_install(tmp_path, monkeypatch): + """Build a fake libs zip, mock urlretrieve, verify extraction + atomic rename.""" + # Build zip: libs/libsimplex.so + libs/libHS-stub.so + src = tmp_path / "src" / "libs" + src.mkdir(parents=True) + (src / "libsimplex.so").write_text("fake-so") + (src / "libHS-stub.so").write_text("fake-hs") + zip_path = tmp_path / "fake-libs.zip" + with zipfile.ZipFile(zip_path, "w") as zf: + for f in src.iterdir(): + zf.write(f, f"libs/{f.name}") + + def fake_urlretrieve(url: str, dest: str) -> None: + import shutil + shutil.copy(zip_path, dest) + + monkeypatch.setattr("urllib.request.urlretrieve", fake_urlretrieve) + monkeypatch.setattr("simplex_chat._native._platform_tag", lambda: "linux-x86_64") + + target = tmp_path / "out" + _download(target, "sqlite") + assert (target / "libsimplex.so").read_text() == "fake-so" + assert (target / "libHS-stub.so").read_text() == "fake-hs" +``` + +- [ ] **Step 2: Implement `_cache_root`, `_resolve_libs_dir`, `_download`** + +Append to `_native.py`: + +```python +def _cache_root() -> Path: + if sys.platform == "darwin": + return Path.home() / "Library" / "Caches" / "simplex-chat" + if sys.platform == "win32": + return Path(os.environ["LOCALAPPDATA"]) / "simplex-chat" + base = os.environ.get("XDG_CACHE_HOME") or str(Path.home() / ".cache") + return Path(base) / "simplex-chat" + + +def _resolve_libs_dir(backend: Backend) -> Path: + if override := os.environ.get("SIMPLEX_LIBS_DIR"): + return Path(override) + if backend == "postgres" and _platform_tag() != "linux-x86_64": + raise RuntimeError( + "postgres backend is only supported on linux-x86_64; " + f"current platform is {_platform_tag()}" + ) + target = _cache_root() / f"v{LIBS_VERSION}" / backend + if not (target / _libname()).exists(): + _download(target, backend) + return target + + +def _download(target: Path, backend: Backend) -> None: + """Download libs zip → atomic rename into `target`. Concurrent processes safe. + + Atomicity strategy: each process extracts to its own sibling tempdir on the same + filesystem, then `os.rename` the `libs/` subdir to `target`. POSIX `os.rename` + onto a NON-EXISTENT path is atomic; if the target exists (another process won + the race), `os.rename` fails on most platforms — we then verify the winner has + what we need and proceed. NEVER rmtree the target: that creates a TOCTOU + window where another process is reading/loading the file we're deleting. + """ + target.parent.mkdir(parents=True, exist_ok=True) + print( + f"Downloading libsimplex ({_platform_tag()}, {backend}) " + f"v{LIBS_VERSION} ...", + file=sys.stderr, + flush=True, + ) + with tempfile.TemporaryDirectory(dir=target.parent) as tmp: + zip_path = Path(tmp) / "libs.zip" + urllib.request.urlretrieve(_libs_url(backend), zip_path) + with zipfile.ZipFile(zip_path) as zf: + zf.extractall(tmp) + # zip layout: /libs/libsimplex.* + libHS*.* + extracted_libs = Path(tmp) / "libs" + if not extracted_libs.is_dir(): + raise RuntimeError(f"libs/ missing from {_libs_url(backend)}") + try: + os.rename(extracted_libs, target) + except OSError as e: + # EEXIST / ENOTEMPTY mean another process won the race — fall through + # and check that the winner left a usable libsimplex behind. Anything + # else (ENOSPC, EACCES, EROFS, Windows codes mapped to None) is a real + # failure and must propagate. Same VERSION cached → same content → + # safe to proceed once we've confirmed the file is there. + if e.errno not in (errno.EEXIST, errno.ENOTEMPTY): + raise + if not (target / _libname()).exists(): + raise RuntimeError( + f"another process partially populated {target} but libsimplex " + f"is missing; remove the directory manually and retry" + ) from e +``` + +- [ ] **Step 3: Run tests** + +``` +PYTHONPATH=src pytest tests/test_native_cache.py -v +``` + +Expected: all PASS. + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/_native.py \ + packages/simplex-chat-python/tests/test_native_cache.py +git commit -m "feat(python): _native cache resolution and lazy download" +``` + +### Task 3.3: `_native.py` — ctypes signatures, `hs_init`, lib loader + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/_native.py` + +- [ ] **Step 1: Append the loader** + +(Imports for `ctypes`, `threading`, and the `from ctypes import …` line were already hoisted to the top of `_native.py` in Task 3.1 — do not re-add them here.) + +```python +_lock = threading.Lock() +_lib: ctypes.CDLL | None = None +_libc: ctypes.CDLL | None = None +_backend: Backend | None = None + + +def _load_libc() -> ctypes.CDLL: + if sys.platform == "win32": + return ctypes.CDLL("msvcrt") + return ctypes.CDLL(None) # libc on POSIX is the process's own symbol table + + +def _setup_signatures(lib: ctypes.CDLL) -> None: + """Declare argtypes/restype for the 8 chat_* functions exported by libsimplex. + + All result strings come back as raw c_void_p so the caller can free them + after copying — matches HandleCResult in cpp/simplex.cc:157-165. + """ + lib.chat_migrate_init.argtypes = [c_char_p, c_char_p, c_char_p, POINTER(c_void_p)] + lib.chat_migrate_init.restype = c_void_p + lib.chat_close_store.argtypes = [c_void_p] + lib.chat_close_store.restype = c_void_p + lib.chat_send_cmd.argtypes = [c_void_p, c_char_p] + lib.chat_send_cmd.restype = c_void_p + lib.chat_recv_msg_wait.argtypes = [c_void_p, c_int] + lib.chat_recv_msg_wait.restype = c_void_p + lib.chat_write_file.argtypes = [c_void_p, c_char_p, POINTER(c_uint8), c_int] + lib.chat_write_file.restype = c_void_p + lib.chat_read_file.argtypes = [c_char_p, c_char_p, c_char_p] + lib.chat_read_file.restype = POINTER(c_uint8) + lib.chat_encrypt_file.argtypes = [c_void_p, c_char_p, c_char_p] + lib.chat_encrypt_file.restype = c_void_p + lib.chat_decrypt_file.argtypes = [c_char_p, c_char_p, c_char_p, c_char_p] + lib.chat_decrypt_file.restype = c_void_p + + +def _hs_init(lib: ctypes.CDLL) -> None: + """Initialize the Haskell runtime exactly once. Mirrors cpp/simplex.cc:13-32.""" + if sys.platform == "win32": + argv_strs = [b"simplex", b"+RTS", b"-A64m", b"-H64m", b"--install-signal-handlers=no"] + else: + argv_strs = [b"simplex", b"+RTS", b"-A64m", b"-H64m", b"-xn", b"--install-signal-handlers=no"] + argc = c_int(len(argv_strs)) + arr = (c_char_p * (len(argv_strs) + 1))(*argv_strs, None) + arr_ptr = ctypes.byref(ctypes.cast(arr, POINTER(c_char_p))) + lib.hs_init_with_rtsopts.argtypes = [POINTER(c_int), POINTER(POINTER(c_char_p))] + lib.hs_init_with_rtsopts.restype = None + lib.hs_init_with_rtsopts(ctypes.byref(argc), arr_ptr) + + +def lib_for(backend: Backend) -> ctypes.CDLL: + """Resolve, load, and initialize libsimplex for the given backend. + + Idempotent for the same backend; raises if called with a different backend. + Concurrent calls serialize on the module-level lock. + """ + global _lib, _libc, _backend + with _lock: + if _lib is not None: + if _backend != backend: + raise RuntimeError( + f"libsimplex already loaded with backend={_backend!r}; " + f"cannot switch to {backend!r} in the same process" + ) + return _lib + libs_dir = _resolve_libs_dir(backend) + lib = ctypes.CDLL(str(libs_dir / _libname())) + _setup_signatures(lib) + _hs_init(lib) + _libc = _load_libc() + _lib = lib + _backend = backend + return lib + + +def libc() -> ctypes.CDLL: + """libc — needed by `core` to free Haskell-allocated result strings.""" + if _libc is None: + raise RuntimeError("lib_for() must be called before libc()") + return _libc +``` + +- [ ] **Step 2: Sanity-check imports** + +``` +PYTHONPATH=src python -c "import simplex_chat._native; print('ok')" +``` + +Expected: prints `ok`. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/_native.py +git commit -m "feat(python): _native ctypes signatures, hs_init, lib loader" +``` + +--- + +## Chunk 4: Core wrapper (`core.py`) + +Typed async wrappers around the 8 FFI functions. Handles JSON parse, buffer free, error translation. No public API yet — that lands in `ChatApi`. + +### Task 4.1: `core.py` — exceptions, enums, `chat_send_cmd`, `chat_recv_msg_wait` + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/core.py` + +- [ ] **Step 1: Write the core module** + +```python +"""Internal typed async wrapper around libsimplex's 8 C ABI functions. + +Users interact with `Bot` / `ChatApi`. This module is exposed as +`simplex_chat.core` for tests and the api.ChatApi class only. +""" +from __future__ import annotations + +import asyncio +import ctypes +import json +from enum import StrEnum +from typing import TypedDict + +from . import _native +from .types import T, CR, CEvt + + +class ChatAPIError(Exception): + """Raised when chat_send_cmd / chat_recv_msg_wait returns a chat error.""" + def __init__(self, message: str, chat_error: T.ChatError | None = None): + super().__init__(message) + self.chat_error = chat_error + + +class ChatInitError(Exception): + """Raised when chat_migrate_init returns a DBMigrationResult error.""" + def __init__(self, message: str, db_migration_error): + super().__init__(message) + self.db_migration_error = db_migration_error + + +class MigrationConfirmation(StrEnum): + YES_UP = "yesUp" + YES_UP_DOWN = "yesUpDown" + CONSOLE = "console" + ERROR = "error" + + +class CryptoArgs(TypedDict): # wire-format JSON; camelCase fields + fileKey: str + fileNonce: str + + +def _read_and_free(ptr: int | None) -> str: + """Copy a Haskell-allocated null-terminated UTF-8 string and free its buffer. + + Mirrors HandleCResult in packages/simplex-chat-nodejs/cpp/simplex.cc:157-165. + """ + if not ptr: + raise RuntimeError("null pointer returned from libsimplex") + try: + return ctypes.string_at(ptr).decode("utf-8") + finally: + _native.libc().free(ctypes.c_void_p(ptr)) + + +async def chat_send_cmd(ctrl: int, cmd: str) -> CR.ChatResponse: + def _call() -> str: + ptr = _native._lib.chat_send_cmd(ctrl, cmd.encode("utf-8")) + return _read_and_free(ptr) + raw = await asyncio.to_thread(_call) + parsed = json.loads(raw) + if "result" in parsed and isinstance(parsed["result"], dict): + return parsed["result"] # type: ignore[return-value] + err = parsed.get("error") + if isinstance(err, dict): + raise ChatAPIError(f"chat command error: {err.get('type')}", err) + raise ChatAPIError(f"invalid chat command result: {raw[:200]}") + + +async def chat_recv_msg_wait(ctrl: int, wait_us: int = 5_000_000) -> CEvt.ChatEvent | None: + def _call() -> str: + ptr = _native._lib.chat_recv_msg_wait(ctrl, wait_us) + if not ptr: + return "" + return _read_and_free(ptr) + raw = await asyncio.to_thread(_call) + if not raw: + return None + parsed = json.loads(raw) + if "result" in parsed and isinstance(parsed["result"], dict): + return parsed["result"] # type: ignore[return-value] + err = parsed.get("error") + if isinstance(err, dict): + raise ChatAPIError(f"chat event error: {err.get('type')}", err) + raise ChatAPIError(f"invalid chat event: {raw[:200]}") +``` + +- [ ] **Step 2: Verify import** + +``` +PYTHONPATH=src python -c "from simplex_chat.core import chat_send_cmd, ChatAPIError, MigrationConfirmation; print('ok')" +``` + +Expected: `ok`. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/core.py +git commit -m "feat(python): core typed wrappers for chat_send_cmd + chat_recv_msg_wait" +``` + +### Task 4.2: `core.py` — remaining FFI functions (`chat_migrate_init`, `chat_close_store`, file ops) + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/core.py` + +- [ ] **Step 1: Append the remaining functions** + +```python +async def chat_migrate_init( + db_path: str, db_key: str, confirm: MigrationConfirmation +) -> int: + """Initialize chat controller. Returns opaque ctrl pointer as Python int.""" + def _call() -> tuple[int, str]: + ctrl = ctypes.c_void_p() + ptr = _native._lib.chat_migrate_init( + db_path.encode("utf-8"), + db_key.encode("utf-8"), + confirm.encode("utf-8"), + ctypes.byref(ctrl), + ) + return (ctrl.value or 0, _read_and_free(ptr)) + ctrl_val, raw = await asyncio.to_thread(_call) + parsed = json.loads(raw) + if parsed.get("type") == "ok": + return ctrl_val + raise ChatInitError( + "Database or migration error (see db_migration_error)", + parsed, + ) + + +async def chat_close_store(ctrl: int) -> None: + def _call() -> str: + ptr = _native._lib.chat_close_store(ctrl) + return _read_and_free(ptr) + res = await asyncio.to_thread(_call) + if res: + raise RuntimeError(res) + + +async def chat_write_file(ctrl: int, path: str, data: bytes) -> CryptoArgs: + def _call() -> str: + buf = (ctypes.c_uint8 * len(data)).from_buffer_copy(data) + ptr = _native._lib.chat_write_file(ctrl, path.encode("utf-8"), buf, len(data)) + return _read_and_free(ptr) + raw = await asyncio.to_thread(_call) + return _crypto_args_result(raw) + + +async def chat_read_file(path: str, args: CryptoArgs) -> bytes: + def _call() -> bytes: + ptr = _native._lib.chat_read_file( + path.encode("utf-8"), + args["fileKey"].encode("utf-8"), + args["fileNonce"].encode("utf-8"), + ) + if not ptr: + raise RuntimeError("chat_read_file returned null") + addr = ctypes.cast(ptr, ctypes.c_void_p).value or 0 + try: + status = ctypes.cast(addr, ctypes.POINTER(ctypes.c_uint8))[0] + if status == 1: + msg = ctypes.string_at(addr + 1).decode("utf-8") + raise RuntimeError(msg) + if status != 0: + raise RuntimeError(f"unexpected status {status} from chat_read_file") + length = ctypes.cast(addr + 1, ctypes.POINTER(ctypes.c_uint32))[0] + return ctypes.string_at(addr + 5, length) + finally: + _native.libc().free(ctypes.c_void_p(addr)) + return await asyncio.to_thread(_call) + + +async def chat_encrypt_file(ctrl: int, src: str, dst: str) -> CryptoArgs: + def _call() -> str: + ptr = _native._lib.chat_encrypt_file( + ctrl, src.encode("utf-8"), dst.encode("utf-8") + ) + return _read_and_free(ptr) + return _crypto_args_result(await asyncio.to_thread(_call)) + + +async def chat_decrypt_file(src: str, args: CryptoArgs, dst: str) -> None: + def _call() -> str: + ptr = _native._lib.chat_decrypt_file( + src.encode("utf-8"), + args["fileKey"].encode("utf-8"), + args["fileNonce"].encode("utf-8"), + dst.encode("utf-8"), + ) + return _read_and_free(ptr) + res = await asyncio.to_thread(_call) + if res: + raise RuntimeError(res) + + +def _crypto_args_result(raw: str) -> CryptoArgs: + parsed = json.loads(raw) + if parsed.get("type") == "result": + return parsed["cryptoArgs"] + if parsed.get("type") == "error": + raise RuntimeError(parsed.get("writeError", "unknown write error")) + raise RuntimeError(f"unexpected result: {raw[:200]}") +``` + +- [ ] **Step 2: Re-verify import** + +``` +PYTHONPATH=src python -c "from simplex_chat import core; print(dir(core))" +``` + +Expected: prints a list including `chat_migrate_init`, `chat_close_store`, all eight functions. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/core.py +git commit -m "feat(python): core wrappers for migrate, close, file ops" +``` + +--- + +## Chunk 5: ChatApi class + +Escape-hatch class with the 6 control methods plus ~40 `api_xxx` methods, one per Node `apiXxx`. Repetitive — done in batches grouped by domain. + +### Task 5.1: `api.py` — `ChatApi` class with control methods + `Db` config + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/api.py` + +- [ ] **Step 1: Write Db config dataclasses + ChatApi base** + +```python +"""Low-level escape-hatch API. Most users go through `Bot` instead.""" +from __future__ import annotations + +from dataclasses import dataclass +from typing import TYPE_CHECKING + +from . import core +from .core import ChatAPIError, ChatInitError, MigrationConfirmation +from .types import CC, CEvt, CR, T + + +@dataclass(slots=True) +class SqliteDb: + file_prefix: str + encryption_key: str | None = None + + +@dataclass(slots=True) +class PostgresDb: + connection_string: str + schema_prefix: str | None = None + + +Db = SqliteDb | PostgresDb + + +def _db_to_migrate_args(db: Db) -> tuple[str, str, str]: + """Returns (path-or-prefix, key-or-conn, backend).""" + if isinstance(db, SqliteDb): + return (db.file_prefix, db.encryption_key or "", "sqlite") + if isinstance(db, PostgresDb): + return (db.schema_prefix or "", db.connection_string, "postgres") + raise TypeError(f"Unknown db: {db!r}") + + +class ChatCommandError(Exception): + def __init__(self, message: str, response: CR.ChatResponse): + super().__init__(message) + self.response = response + + +class ChatApi: + def __init__(self, ctrl: int, backend: str): + self._ctrl = ctrl + self._backend = backend + + @classmethod + async def init( + cls, + db: Db, + confirm: MigrationConfirmation = MigrationConfirmation.YES_UP, + ) -> "ChatApi": + from . import _native + path_or_prefix, key_or_conn, backend = _db_to_migrate_args(db) + # Trigger lazy lib load with the right backend BEFORE chat_migrate_init. + _native.lib_for(backend) # type: ignore[arg-type] + ctrl = await core.chat_migrate_init(path_or_prefix, key_or_conn, confirm) + return cls(ctrl, backend) + + @property + def ctrl(self) -> int: + return self._ctrl + + async def start_chat(self) -> None: + r = await self.send_chat_cmd( + CC.StartChat_cmd_string({"mainApp": True, "enableSndFiles": True}) + ) + if r.get("type") not in ("chatStarted", "chatRunning"): + raise ChatCommandError("error starting chat", r) + + async def stop_chat(self) -> None: + r = await self.send_chat_cmd("/_stop") + if r.get("type") != "chatStopped": + raise ChatCommandError("error stopping chat", r) + + async def close(self) -> None: + await core.chat_close_store(self._ctrl) + + async def send_chat_cmd(self, cmd: str) -> CR.ChatResponse: + return await core.chat_send_cmd(self._ctrl, cmd) + + async def recv_chat_event(self, wait_us: int = 5_000_000) -> CEvt.ChatEvent | None: + return await core.chat_recv_msg_wait(self._ctrl, wait_us) +``` + +- [ ] **Step 2: Verify import** + +``` +PYTHONPATH=src python -c "from simplex_chat.api import ChatApi, SqliteDb, PostgresDb; print('ok')" +``` + +Expected: `ok`. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/api.py +git commit -m "feat(python): ChatApi base class with control methods + Db config" +``` + +### Task 5.2: `api.py` — implement ~40 `api_xxx` methods + +Mirror methods from `packages/simplex-chat-nodejs/src/api.ts:344-958`. Each method is the same shape: + +```python +async def api_(self, ...args) -> : + r = await self.send_chat_cmd(CC._cmd_string({"...": args, ...})) + if r["type"] == "": + return r[""] + raise ChatCommandError("error ", r) +``` + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/api.py` + +- [ ] **Step 1: Reference the Node lib while implementing** + +```bash +# Open the Node source side-by-side +sed -n '344,958p' packages/simplex-chat-nodejs/src/api.ts | less +``` + +- [ ] **Step 2: Implement methods in groups, one commit per group** + +The Node file groups by domain — port them in the same order: + +| Group | Methods (Node names → Python snake_case) | Lines in api.ts | +|---|---|---| +| Address | apiCreateUserAddress, apiDeleteUserAddress, apiGetUserAddress, apiSetProfileAddress, apiSetAddressSettings | 344-409 | +| Messages | apiSendMessages, apiSendTextMessage, apiSendTextReply, apiUpdateChatItem, apiDeleteChatItems, apiDeleteMemberChatItem, apiChatItemReaction | 411-505 | +| Files | apiReceiveFile, apiCancelFile | 507-525 | +| Groups | apiAddMember, apiJoinGroup, apiAcceptMember, apiSetMembersRole, apiBlockMembersForAll, apiRemoveMembers, apiLeaveGroup, apiListMembers, apiNewGroup, apiUpdateGroupProfile | 527-625 | +| Group links | apiCreateGroupLink, apiSetGroupLinkMemberRole, apiDeleteGroupLink, apiGetGroupLink, apiGetGroupLinkStr | 627-672 | +| Connections | apiCreateLink, apiConnectPlan, apiConnect, apiConnectActiveUser, apiAcceptContactRequest, apiRejectContactRequest | 674-746 | +| Chats | apiListContacts, apiListGroups, apiGetChats, apiDeleteChat, apiSetGroupCustomData, apiSetContactCustomData, apiSetAutoAcceptMemberContacts, apiGetChat | 748-841 | +| Users | apiGetActiveUser, apiCreateActiveUser, apiListUsers, apiSetActiveUser, apiDeleteUser, apiUpdateProfile, apiSetContactPrefs | 843-928 | +| Member contacts | apiCreateMemberContact, apiSendMemberContactInvitation | 930-957 | + +For each method: +- TS `apiCreateUserAddress(userId)` → Python `api_create_user_address(self, user_id: int) -> T.CreatedConnLink` +- Use the autogenerated `CC._cmd_string({...})` to build the command string. Field names inside the dict are camelCase wire format. +- Use type narrowing on `r["type"]` to extract the expected response field. + +Example port: + +```python +async def api_create_user_address(self, user_id: int) -> T.CreatedConnLink: + r = await self.send_chat_cmd(CC.APICreateMyAddress_cmd_string({"userId": user_id})) + if r["type"] == "userContactLinkCreated": + return r["connLinkContact"] + raise ChatCommandError("error creating user address", r) +``` + +Commit pattern: one commit per group from the table above. Commit messages: + +```bash +git commit -m "feat(python): ChatApi address methods" +git commit -m "feat(python): ChatApi message methods" +git commit -m "feat(python): ChatApi file methods" +# … etc, one per group +``` + +- [ ] **Step 3: After all groups land, verify all methods are present** + +```bash +grep -c "async def api_" packages/simplex-chat-python/src/simplex_chat/api.py +``` + +Expected: ≥40 (matches the count of `apiXxx` methods in api.ts). + +- [ ] **Step 4: Verify import after all groups** + +``` +PYTHONPATH=src python -c "from simplex_chat.api import ChatApi; api = ChatApi.__init__; print('ok')" +``` + +Expected: `ok`. + +--- + +## Chunk 6: Bot class + +User-facing `Bot`: decorator-registered handlers, kwarg filters, `Message` wrapper with content-narrowed subclasses, dual lifecycle, middleware. + +### Task 6.1: `Message` wrapper class + content-narrowed aliases + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/bot.py` + +- [ ] **Step 1: Write `Message` and aliases** + +```python +"""User-facing `Bot` API: decorators, filters, Message wrapper, lifecycle.""" +from __future__ import annotations + +import asyncio +import logging +import re +import signal as _signal +from dataclasses import dataclass +from typing import ( + Any, Awaitable, Callable, Generic, Literal, TYPE_CHECKING, TypeVar, overload +) + +from . import _native +from .api import ChatApi, ChatCommandError, Db, PostgresDb, SqliteDb +from .core import ChatAPIError, MigrationConfirmation +from .types import CC, CEvt, CR, T + +if TYPE_CHECKING: + from typing_extensions import TypeAlias + +log = logging.getLogger("simplex_chat") + +C = TypeVar("C", bound=T.MsgContent) + + +@dataclass(slots=True) +class BotProfile: + display_name: str + full_name: str = "" + short_descr: str | None = None + image: str | None = None + + +@dataclass(slots=True) +class BotCommand: + keyword: str + label: str + + +@dataclass(slots=True, frozen=True) +class ParsedCommand: + keyword: str + args: str + + +@dataclass(slots=True, frozen=True) +class Message(Generic[C]): + chat_item: T.AChatItem + content: C + bot: "Bot" + + @property + def chat_info(self) -> T.ChatInfo: + return self.chat_item["chatInfo"] + + @property + def text(self) -> str | None: + c = self.content + if isinstance(c, dict): + return c.get("text") # type: ignore[return-value] + return None + + async def reply(self, text: str) -> "Message": + items = await self.bot.api.api_send_text_reply(self.chat_item, text) + return Message(chat_item=items[0], content=items[0]["chatItem"]["content"], bot=self.bot) + + async def reply_content(self, content: T.MsgContent) -> "Message": + items = await self.bot.api.api_send_messages( + self.chat_info, [{"msgContent": content, "mentions": {}}] + ) + return Message(chat_item=items[0], content=items[0]["chatItem"]["content"], bot=self.bot) + + async def react(self, emoji: str) -> None: + # Implementation defers to ChatApi.api_chat_item_reaction + ... + + async def delete(self) -> None: ... + async def forward(self, to: T.ChatRef) -> "Message": ... + + +# Concrete narrowed aliases — exported from package __init__.py +TextMessage = Message[T.MsgContent_Text] +ImageMessage = Message[T.MsgContent_Image] +FileMessage = Message[T.MsgContent_File] +VoiceMessage = Message[T.MsgContent_Voice] +VideoMessage = Message[T.MsgContent_Video] +LinkMessage = Message[T.MsgContent_Link] +# … one per T.MsgContent_* variant; full list mirrors what's emitted in _types.py +``` + +- [ ] **Step 2: Verify import** + +``` +PYTHONPATH=src python -c "from simplex_chat.bot import Message, TextMessage, BotProfile; print('ok')" +``` + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/bot.py +git commit -m "feat(python): Bot module skeleton — Message wrapper + aliases" +``` + +### Task 6.2: Filter compilation (`filters.py`) + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/filters.py` +- Create: `packages/simplex-chat-python/tests/test_filters.py` + +- [ ] **Step 1: Write filter tests** + +```python +# tests/test_filters.py +import re +import pytest +from simplex_chat.filters import compile_message_filter + +def _msg(content_type="text", text=None, chat_type="direct", + from_role=None, from_contact_id=None, group_id=None): + """Build a minimal mock Message-like object for filter testing.""" + class M: + pass + m = M() + m.content = {"type": content_type, "text": text} if text is not None else {"type": content_type} + m.chat_item = {"chatInfo": { + "type": chat_type, + **({"groupInfo": {"groupId": group_id}} if chat_type == "group" else {}), + }} + # Sender extraction is implementation-detail of the filter — keep test fixture pragmatic. + m._from_role = from_role + m._from_contact_id = from_contact_id + return m + +def test_no_filters_matches_all(): + f = compile_message_filter({}) + assert f(_msg(content_type="text")) + assert f(_msg(content_type="image")) + +def test_content_type_singular(): + f = compile_message_filter({"content_type": "text"}) + assert f(_msg(content_type="text")) + assert not f(_msg(content_type="image")) + +def test_content_type_tuple_or(): + f = compile_message_filter({"content_type": ("text", "image")}) + assert f(_msg(content_type="text")) + assert f(_msg(content_type="image")) + assert not f(_msg(content_type="voice")) + +def test_text_exact(): + f = compile_message_filter({"text": "hello"}) + assert f(_msg(text="hello")) + assert not f(_msg(text="world")) + +def test_text_regex(): + f = compile_message_filter({"text": re.compile(r"^\d+$")}) + assert f(_msg(text="123")) + assert not f(_msg(text="abc")) + +def test_when_callable(): + f = compile_message_filter({"when": lambda m: m.content["type"] == "voice"}) + assert f(_msg(content_type="voice")) + assert not f(_msg(content_type="text")) + +def test_combined_and(): + f = compile_message_filter({"content_type": "text", "text": re.compile(r"\d")}) + assert f(_msg(content_type="text", text="abc123")) + assert not f(_msg(content_type="text", text="abc")) + assert not f(_msg(content_type="image")) +``` + +- [ ] **Step 2: Implement `compile_message_filter`** + +```python +# filters.py +from __future__ import annotations +import re +from typing import Any, Callable + +def compile_message_filter(kw: dict[str, Any]) -> Callable[[Any], bool]: + """Compile filter kwargs into a single predicate function. + + Multiple kwargs combine with AND; tuples within a kwarg combine with OR. + `when` is the last predicate evaluated. + """ + predicates: list[Callable[[Any], bool]] = [] + + if (ct := kw.get("content_type")) is not None: + ct_set = (ct,) if isinstance(ct, str) else tuple(ct) + predicates.append(lambda m: m.content.get("type") in ct_set) + + if (t := kw.get("text")) is not None: + if isinstance(t, re.Pattern): + predicates.append(lambda m: bool(t.search(m.content.get("text", "") or ""))) + else: + predicates.append(lambda m: m.content.get("text") == t) + + if (ct := kw.get("chat_type")) is not None: + ct_set = (ct,) if isinstance(ct, str) else tuple(ct) + predicates.append(lambda m: m.chat_item["chatInfo"]["type"] in ct_set) + + if (gid := kw.get("group_id")) is not None: + gid_set = (gid,) if isinstance(gid, int) else tuple(gid) + def gid_match(m: Any) -> bool: + ci = m.chat_item["chatInfo"] + return ci["type"] == "group" and ci["groupInfo"]["groupId"] in gid_set + predicates.append(gid_match) + + # from_role, from_contact_id, from_member_id are looked up via helpers in filters.py + # — defer to integration with ChatInfo / GroupMember accessors implemented later. + + if (when := kw.get("when")) is not None: + predicates.append(when) + + if not predicates: + return lambda _m: True + return lambda m: all(p(m) for p in predicates) +``` + +- [ ] **Step 3: Run filter tests** + +``` +PYTHONPATH=src pytest tests/test_filters.py -v +``` + +Expected: all PASS. + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/filters.py \ + packages/simplex-chat-python/tests/test_filters.py +git commit -m "feat(python): filter kwarg compilation + tests" +``` + +### Task 6.3: `Bot` class — construction, decorators, registration + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/bot.py` + +- [ ] **Step 1: Add `Bot` class with `__init__`, decorator methods, registration storage** + +```python +# Append to bot.py + +MessageHandler = Callable[[Message], Awaitable[None]] +CommandHandler = Callable[[Message, ParsedCommand], Awaitable[None]] +EventHandler = Callable[[CEvt.ChatEvent], Awaitable[None]] +# Note: handlers are async-only (matches spec). Users must `async def handler(...)`. + + +class Middleware: + async def __call__( + self, + handler: Callable[[Message, dict[str, object]], Awaitable[None]], + message: Message, + data: dict[str, object], + ) -> None: + await handler(message, data) + + +class Bot: + def __init__( + self, + *, + profile: BotProfile, + db: Db, + welcome: str | T.MsgContent | None = None, + commands: list[BotCommand] | None = None, + confirm_migrations: MigrationConfirmation = MigrationConfirmation.YES_UP, + create_address: bool = True, + update_address: bool = True, + update_profile: bool = True, + auto_accept: bool = True, + business_address: bool = False, + allow_files: bool = False, + use_bot_profile: bool = True, + log_contacts: bool = True, + log_network: bool = False, + ) -> None: + self._profile = profile + self._db = db + self._welcome = welcome + self._commands = commands or [] + self._confirm_migrations = confirm_migrations + self._opts = { + "create_address": create_address, + "update_address": update_address, + "update_profile": update_profile, + "auto_accept": auto_accept, + "business_address": business_address, + "allow_files": allow_files, + "use_bot_profile": use_bot_profile, + "log_contacts": log_contacts, + "log_network": log_network, + } + self._api: ChatApi | None = None + self._serving = False + self._stop_event = asyncio.Event() + self._message_handlers: list[tuple[Callable[[Message], bool], MessageHandler]] = [] + self._command_handlers: list[tuple[tuple[str, ...], Callable[[Message], bool], CommandHandler]] = [] + self._event_handlers: dict[str, list[EventHandler]] = {} + self._middleware: list[Middleware] = [] + + @property + def api(self) -> ChatApi: + if self._api is None: + raise RuntimeError("Bot not initialized — call bot.run() or use `async with bot:`") + return self._api + + # --- decorator registration (overloads omitted for brevity; see spec for full list) --- + + def on_message(self, **filter_kw: Any) -> Callable[[MessageHandler], MessageHandler]: + from .filters import compile_message_filter + predicate = compile_message_filter(filter_kw) + def deco(fn: MessageHandler) -> MessageHandler: + self._message_handlers.append((predicate, fn)) + return fn + return deco + + def on_command( + self, name: str | tuple[str, ...], **filter_kw: Any + ) -> Callable[[CommandHandler], CommandHandler]: + names = (name,) if isinstance(name, str) else tuple(name) + from .filters import compile_message_filter + predicate = compile_message_filter(filter_kw) + def deco(fn: CommandHandler) -> CommandHandler: + self._command_handlers.append((names, predicate, fn)) + return fn + return deco + + def on_event(self, event: CEvt.Tag, /) -> Callable[[EventHandler], EventHandler]: + def deco(fn: EventHandler) -> EventHandler: + self._event_handlers.setdefault(event, []).append(fn) + return fn + return deco + + def use(self, middleware: Middleware) -> None: + self._middleware.append(middleware) +``` + +- [ ] **Step 2: Discover the full `MsgContent` variant list from generated types** + +After Chunk 1 lands, the generated file lists every variant. Get the canonical list: + +```bash +grep -E '^class MsgContent_' packages/simplex-chat-python/src/simplex_chat/types/_types.py | sed 's/class \([^(]*\).*/\1/' +``` + +Expected output (approximately — exact list comes from Haskell `MsgContent` defined at `bots/src/API/Docs/Types.hs:309` with prefix `"MC"`): + +``` +MsgContent_Text +MsgContent_Link +MsgContent_Image +MsgContent_Video +MsgContent_Voice +MsgContent_File +MsgContent_Report +MsgContent_Chat +MsgContent_Unknown +``` + +For each variant in that list, define both a `Message` alias (in Step 1 above — extend the alias block to cover every variant) and one decorator overload (this step). + +- [ ] **Step 3: Add the typed overloads — one per variant from Step 2** + +After the plain `on_message` definition, add a typed overload for each variant. The pattern below shows two; repeat for every variant the previous step found: + +```python + # Type-only overloads — compiler-visible, no runtime effect. + # MUST cover every variant from `grep '^class MsgContent_' _types.py`. + @overload + def on_message(self, *, content_type: Literal["text"], **rest: Any + ) -> Callable[[Callable[[TextMessage], Awaitable[None]]], + Callable[[TextMessage], Awaitable[None]]]: ... + @overload + def on_message(self, *, content_type: Literal["image"], **rest: Any + ) -> Callable[[Callable[[ImageMessage], Awaitable[None]]], + Callable[[ImageMessage], Awaitable[None]]]: ... + # … one per MsgContent variant; verify count matches Step 2's grep output … + @overload + def on_message(self, **rest: Any + ) -> Callable[[MessageHandler], MessageHandler]: ... +``` + +The per-variant tag string (`"text"`, `"image"`, …) is the lowercased suffix after `MsgContent_` — see the `Literal[...]` member on each generated TypedDict's `type` field for the canonical spelling. + +- [ ] **Step 3: Verify import + decorator binding** + +```python +# tests/test_bot_registration.py +from simplex_chat.bot import Bot, BotProfile +from simplex_chat.api import SqliteDb + +def test_decorator_registers_handler(): + bot = Bot(profile=BotProfile(display_name="x"), db=SqliteDb(file_prefix="/tmp/test")) + @bot.on_message(content_type="text") + async def h(msg): pass + assert len(bot._message_handlers) == 1 +``` + +``` +PYTHONPATH=src pytest tests/test_bot_registration.py -v +``` + +Expected: PASS. + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/bot.py \ + packages/simplex-chat-python/tests/test_bot_registration.py +git commit -m "feat(python): Bot class — construction + decorator registration" +``` + +### Task 6.4: Lifecycle: `run()`, `serve_forever()`, `__aenter__/__aexit__`, `stop()` + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/bot.py` + +- [ ] **Step 1: Append lifecycle methods** + +```python +class Bot: + # … existing methods … + + async def __aenter__(self) -> "Bot": + self._api = await ChatApi.init(self._db, self._confirm_migrations) + await self._api.start_chat() + # TODO Task 6.5: profile + address sync via mkBotProfile/createOrUpdateAddress + return self + + async def __aexit__(self, *exc_info: object) -> None: + self.stop() + if self._api is not None: + try: + await self._api.stop_chat() + finally: + await self._api.close() + self._api = None + + def run(self) -> None: + """Blocking entry: runs serve_forever() with a SIGINT handler installed.""" + async def _main() -> None: + async with self: + loop = asyncio.get_running_loop() + if hasattr(_signal, "SIGINT"): + try: + loop.add_signal_handler(_signal.SIGINT, self.stop) + loop.add_signal_handler(_signal.SIGTERM, self.stop) + except NotImplementedError: # Windows + _signal.signal(_signal.SIGINT, lambda *_: self.stop()) + await self.serve_forever() + asyncio.run(_main()) + + async def serve_forever(self) -> None: + if self._serving: + raise RuntimeError("already serving") + self._serving = True + self._stop_event.clear() + try: + await self._receive_loop() + finally: + self._serving = False + + def stop(self) -> None: + self._stop_event.set() + + async def _receive_loop(self) -> None: + while not self._stop_event.is_set(): + try: + event = await self.api.recv_chat_event(wait_us=5_000_000) + except ChatAPIError as e: + log.error("chat event error: %s", e) + continue + if event is None: + continue + await self._dispatch_event(event) +``` + +- [ ] **Step 2: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/bot.py +git commit -m "feat(python): Bot lifecycle (run, serve_forever, async-context, stop)" +``` + +### Task 6.5: Event dispatch + handler invocation through middleware + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/bot.py` + +- [ ] **Step 1: Append `_dispatch_event` and helpers** + +```python +class Bot: + async def _dispatch_event(self, event: CEvt.ChatEvent) -> None: + # 1. Tag-targeted on_event handlers (registration order) + tag = event["type"] + for h in self._event_handlers.get(tag, []): + try: + await h(event) + except Exception: + log.exception("on_event handler failed") + # 2. If newChatItems → message + command dispatch. + # Tag check narrows the union; pyright sees event as CEvt.ChatEvent_NewChatItems below. + if tag == "newChatItems": + evt: CEvt.ChatEvent_NewChatItems = event # type: ignore[assignment] + for ci in evt["chatItems"]: + content = ci["chatItem"]["content"] + if content["type"] != "rcvMsgContent": + continue + msg_content = content["msgContent"] + msg = Message(chat_item=ci, content=msg_content, bot=self) + await self._dispatch_message(msg) + + async def _dispatch_message(self, msg: Message) -> None: + # Run all matching message handlers + for predicate, handler in self._message_handlers: + if predicate(msg): + await self._invoke_with_middleware(handler, msg) + # Then any matching command handlers + cmd = self._parse_command(msg) + if cmd is not None: + for names, predicate, handler in self._command_handlers: + if cmd.keyword in names and predicate(msg): + await self._invoke_command_with_middleware(handler, msg, cmd) + + async def _invoke_with_middleware( + self, handler: MessageHandler, message: Message + ) -> None: + async def call(m: Message, _data: dict[str, object]) -> None: + await handler(m) + + chain: Callable[[Message, dict[str, object]], Awaitable[None]] = call + # mw=mw, inner=inner bind the loop variable (late-binding fix) + for mw in reversed(self._middleware): + inner = chain + async def _wrapped(m: Message, d: dict[str, object], mw=mw, inner=inner) -> None: + await mw(inner, m, d) + chain = _wrapped + + try: + await chain(message, {}) + except Exception: + log.exception("message handler failed") + + async def _invoke_command_with_middleware( + self, handler: CommandHandler, message: Message, cmd: ParsedCommand + ) -> None: + # Same shape as _invoke_with_middleware but the inner call gets cmd too. + async def call(m: Message, _data: dict[str, object]) -> None: + await handler(m, cmd) + chain: Callable[[Message, dict[str, object]], Awaitable[None]] = call + for mw in reversed(self._middleware): + inner = chain + async def _wrapped(m: Message, d: dict[str, object], mw=mw, inner=inner) -> None: + await mw(inner, m, d) + chain = _wrapped + try: + await chain(message, {}) + except Exception: + log.exception("command handler failed") + + @staticmethod + def _parse_command(msg: Message) -> ParsedCommand | None: + text = msg.text + if not text or not text.startswith("/"): + return None + body = text[1:].lstrip() + if not body: + return None + if " " in body: + kw, args = body.split(" ", 1) + return ParsedCommand(keyword=kw, args=args.strip()) + return ParsedCommand(keyword=body, args="") +``` + +- [ ] **Step 2: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/bot.py +git commit -m "feat(python): Bot event dispatch + middleware chaining" +``` + +### Task 6.6: Profile + address sync (mirror Node `bot.ts:158-214`) + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/bot.py` + +- [ ] **Step 1: Implement initial profile + address sync inside `__aenter__`** + +Mirror `bot.ts` `createBotUser`, `createOrUpdateAddress`, `updateBotUserProfile`, `mkBotProfile`. Each is straightforward — fetch via `self._api.api_get_active_user()`, compare with `self._profile`, update if `update_profile=True`, etc. Reference: `packages/simplex-chat-nodejs/src/bot.ts:158-214`. + +- [ ] **Step 2: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/bot.py +git commit -m "feat(python): Bot profile + address sync on init" +``` + +### Task 6.7: Update package `__init__.py` to export public API + +**Files:** +- Modify: `packages/simplex-chat-python/src/simplex_chat/__init__.py` + +- [ ] **Step 1: Add exports** + +```python +"""SimpleX Chat — Python client library for chat bots.""" +from ._version import __version__ +from .api import ChatApi, ChatCommandError, Db, PostgresDb, SqliteDb +from .bot import ( + Bot, + BotCommand, + BotProfile, + FileMessage, + ImageMessage, + Message, + Middleware, + ParsedCommand, + TextMessage, + VoiceMessage, + # … all aliases … +) +from .core import ChatAPIError, ChatInitError, MigrationConfirmation, CryptoArgs + +__all__ = [ + "__version__", + # … all the names above … +] +``` + +- [ ] **Step 2: Verify clean import** + +``` +PYTHONPATH=src python -c "from simplex_chat import Bot, TextMessage, SqliteDb, BotProfile; print('ok')" +``` + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/__init__.py +git commit -m "feat(python): export public API from package __init__" +``` + +--- + +## Chunk 7: Tests + CLI + +Pytest suite covering native, codegen, smoke; pre-fetch CLI; example bot. + +### Task 7.1: `__main__.py` — `python -m simplex_chat install` + +**Files:** +- Create: `packages/simplex-chat-python/src/simplex_chat/__main__.py` + +- [ ] **Step 1: Write the CLI** + +```python +"""CLI: python -m simplex_chat install [--backend=sqlite|postgres]""" +from __future__ import annotations +import argparse +import sys +from . import _native + + +def main(argv: list[str] | None = None) -> int: + p = argparse.ArgumentParser(prog="simplex_chat") + sub = p.add_subparsers(dest="command", required=True) + install = sub.add_parser("install", help="Pre-fetch libsimplex into the user cache") + install.add_argument( + "--backend", choices=["sqlite", "postgres"], default="sqlite", + help="which libsimplex variant to download (default: sqlite)", + ) + args = p.parse_args(argv) + if args.command == "install": + try: + path = _native._resolve_libs_dir(args.backend) + print(f"libsimplex installed at: {path}") + return 0 + except Exception as e: + print(f"install failed: {e}", file=sys.stderr) + return 1 + return 1 + + +if __name__ == "__main__": + sys.exit(main()) +``` + +- [ ] **Step 2: Smoke-check the CLI exists** + +``` +PYTHONPATH=src python -m simplex_chat install --help +``` + +Expected: prints argparse help. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/src/simplex_chat/__main__.py +git commit -m "feat(python): python -m simplex_chat install CLI" +``` + +### Task 7.2: Codegen smoke test + +**Files:** +- Create: `packages/simplex-chat-python/tests/test_codegen.py` + +- [ ] **Step 1: Write the test** + +```python +"""Sanity checks on auto-generated wire types — catches generator regressions.""" +import typing +from simplex_chat.types import T, CC, CR, CEvt + + +def test_types_module_imports(): + """Every generated module imports cleanly with no SyntaxError.""" + assert T is not None and CC is not None and CR is not None and CEvt is not None + + +def test_chat_type_is_literal_enum(): + """ChatType should be a Literal of expected member set.""" + origin = typing.get_origin(T.ChatType) + args = typing.get_args(T.ChatType) + # Python ≥3.11 typing.Literal: origin is Literal, args is the tuple of values + assert "direct" in args + assert "group" in args + assert "local" in args + + +def test_known_command_has_cmd_string(): + s = CC.APICreateMyAddress_cmd_string({"userId": 1}) + assert s == "/_address 1" + + +def test_chat_response_tag_alias_present(): + """ChatResponse_Tag union of literals exists.""" + assert hasattr(CR, "ChatResponse_Tag") +``` + +- [ ] **Step 2: Run** + +``` +PYTHONPATH=src pytest tests/test_codegen.py -v +``` + +Expected: PASS. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/tests/test_codegen.py +git commit -m "test(python): codegen sanity checks" +``` + +### Task 7.3: Smoke test with stub libsimplex + +**Files:** +- Create: `packages/simplex-chat-python/tests/test_smoke.py` +- Create: `packages/simplex-chat-python/tests/_stub_libsimplex.c` + +- [ ] **Step 1: Write a tiny C stub that returns canned JSON** + +```c +// _stub_libsimplex.c — compile to libsimplex.so for smoke testing +#include +#include +#include + +void hs_init_with_rtsopts(int *argc, char ***argv) { (void)argc; (void)argv; } + +static char *dup_str(const char *s) { return strdup(s); } + +char *chat_migrate_init(const char *path, const char *key, const char *confirm, void **ctrl) { + (void)path; (void)key; (void)confirm; + *ctrl = (void*)0x1; + return dup_str("{\"type\":\"ok\"}"); +} + +char *chat_close_store(void *ctrl) { (void)ctrl; return dup_str(""); } + +char *chat_send_cmd(void *ctrl, const char *cmd) { + (void)ctrl; (void)cmd; + return dup_str("{\"result\":{\"type\":\"chatStarted\"}}"); +} + +char *chat_recv_msg_wait(void *ctrl, int wait) { + (void)ctrl; (void)wait; + return NULL; +} +// stubs for the file functions: +char *chat_write_file() { return dup_str("{\"type\":\"result\",\"cryptoArgs\":{\"fileKey\":\"k\",\"fileNonce\":\"n\"}}"); } +char *chat_read_file() { return NULL; } +char *chat_encrypt_file() { return dup_str("{\"type\":\"result\",\"cryptoArgs\":{\"fileKey\":\"k\",\"fileNonce\":\"n\"}}"); } +char *chat_decrypt_file() { return dup_str(""); } +``` + +- [ ] **Step 2: Write the smoke test that compiles and uses the stub** + +```python +import asyncio +import os +import shutil +import subprocess +import sys +import tempfile +from pathlib import Path +import pytest + + +@pytest.fixture +def stub_libs_dir(tmp_path): + """Compile _stub_libsimplex.c into a libsimplex.so in tmp_path.""" + src = Path(__file__).parent / "_stub_libsimplex.c" + if sys.platform == "win32": + pytest.skip("stub compilation not implemented for Windows") + libname = "libsimplex.dylib" if sys.platform == "darwin" else "libsimplex.so" + out = tmp_path / libname + cc = shutil.which("cc") or shutil.which("gcc") or pytest.skip("no C compiler") + subprocess.run([cc, "-shared", "-fPIC", str(src), "-o", str(out)], check=True) + return tmp_path + + +@pytest.mark.asyncio +async def test_chat_api_init_and_start(stub_libs_dir, monkeypatch): + monkeypatch.setenv("SIMPLEX_LIBS_DIR", str(stub_libs_dir)) + from simplex_chat.api import ChatApi, SqliteDb + api = await ChatApi.init(SqliteDb(file_prefix=str(stub_libs_dir / "db"))) + await api.start_chat() + await api.close() +``` + +- [ ] **Step 3: Run** + +`test` extras are already declared in `pyproject.toml` (Task 2.1). + +``` +pip install -e ".[test]" +PYTHONPATH=src pytest tests/test_smoke.py -v +``` + +Expected: PASS on Linux/macOS; SKIPPED on Windows. + +- [ ] **Step 4: Commit** + +```bash +git add packages/simplex-chat-python/tests/_stub_libsimplex.c \ + packages/simplex-chat-python/tests/test_smoke.py +git commit -m "test(python): smoke test against stub libsimplex" +``` + +### Task 7.4: Squaring-bot example + +**Files:** +- Create: `packages/simplex-chat-python/examples/squaring_bot.py` + +- [ ] **Step 1: Write the example from the spec** + +```python +"""Squaring bot — receives numbers, replies with their squares. + +Run: python examples/squaring_bot.py +""" +import re +from simplex_chat import ( + Bot, BotProfile, BotCommand, SqliteDb, TextMessage, Message, ParsedCommand +) + +bot = Bot( + profile=BotProfile(display_name="Squaring bot"), + db=SqliteDb(file_prefix="./squaring_bot"), + welcome="Send me a number, I'll square it.", + commands=[BotCommand(keyword="help", label="Show help")], +) + +NUMBER_RE = re.compile(r"^-?\d+(\.\d+)?$") + +@bot.on_message(content_type="text", text=NUMBER_RE) +async def square(msg: TextMessage) -> None: + n = float(msg.text) + await msg.reply(f"{n} * {n} = {n * n}") + +@bot.on_message(content_type="text") # fallback +async def fallback(msg: Message) -> None: + await msg.reply("Send me a number, like 7 or 3.14.") + +@bot.on_command("help") +async def help_cmd(msg: Message, _cmd: ParsedCommand) -> None: + await msg.reply("Send a number, I'll square it.") + +if __name__ == "__main__": + bot.run() +``` + +- [ ] **Step 2: Document in README** + +Replace the placeholder in `packages/simplex-chat-python/README.md` with the example and `pip install simplex-chat` quick-start. + +- [ ] **Step 3: Commit** + +```bash +git add packages/simplex-chat-python/examples/squaring_bot.py \ + packages/simplex-chat-python/README.md +git commit -m "docs(python): squaring bot example + README quick-start" +``` + +--- + +## Chunk 8: CI publishing + +Append `publish-python` job to existing `.github/workflows/build.yml` after `release-nodejs-libs`. Configure PyPI trusted publisher. + +### Task 8.1: Add `publish-python` job to `build.yml` + +**Files:** +- Modify: `.github/workflows/build.yml` (append new job after `release-nodejs-libs:`) + +- [ ] **Step 1: Append the job** + +After line ~803 (end of `release-nodejs-libs`), add: + +```yaml +# ========================= +# Python package release +# ========================= + +# Publishes simplex-chat to PyPI on release tags. +# Depends on release-nodejs-libs because the Python package downloads +# its libsimplex from the simplex-chat-libs release at runtime, so the +# libs release must exist before the Python package is published. +# +# Trusted publishing is configured on PyPI: no API token, OIDC only. + + publish-python: + runs-on: ubuntu-latest + needs: [release-nodejs-libs] + if: startsWith(github.ref, 'refs/tags/v') + permissions: + id-token: write # OIDC for trusted publishing + steps: + - uses: actions/checkout@v6 + - uses: actions/setup-python@v5 + with: + python-version: "3.11" + - run: pip install build + - run: python -m build --wheel + working-directory: packages/simplex-chat-python + - uses: pypa/gh-action-pypi-publish@release/v1 + with: + packages-dir: packages/simplex-chat-python/dist +``` + +- [ ] **Step 2: Validate YAML locally** + +``` +python -c "import yaml; yaml.safe_load(open('.github/workflows/build.yml'))" +``` + +Expected: no exception. + +- [ ] **Step 3: Commit** + +```bash +git add .github/workflows/build.yml +git commit -m "ci: add publish-python job for PyPI release on tag" +``` + +### Task 8.2: One-time PyPI setup (manual, document in README) + +- [ ] **Step 1: Verify package name `simplex-chat` is available on PyPI** + +```bash +curl -s https://pypi.org/pypi/simplex-chat/json | head -c 200 +``` + +If it 404s, the name is free. If it returns metadata, the name is taken — coordinate with the team. + +- [ ] **Step 2: On PyPI, create a pending publisher** + +Navigate to https://pypi.org/manage/account/publishing/ and add: + +| Field | Value | +|---|---| +| PyPI Project Name | simplex-chat | +| Owner | simplex-chat | +| Repository name | simplex-chat | +| Workflow name | build.yml | +| Environment name | (leave blank) | + +- [ ] **Step 3: Add a section to `packages/simplex-chat-python/README.md` documenting the release process** + +Brief checklist for the maintainer: +1. Bump `_version.py` `__version__` (and `LIBS_VERSION` if libs changed). +2. Tag with `vX.Y.Z` matching `__version__`. +3. Push the tag → CI runs the existing build matrix, then `release-nodejs-libs`, then `publish-python`. +4. Verify the wheel appears at https://pypi.org/project/simplex-chat/. + +- [ ] **Step 4: Commit doc update** + +```bash +git add packages/simplex-chat-python/README.md +git commit -m "docs(python): release process + PyPI trusted publisher setup" +``` + +--- + +## Final acceptance + +After all phases: + +- [ ] **Type generation parity.** `cabal test simplex-chat-test` passes for all four `Python` test cases. +- [ ] **Python package builds.** `cd packages/simplex-chat-python && python -m build --wheel` produces a single `.whl` ≤ 200 KB. +- [ ] **All Python tests pass.** `pytest packages/simplex-chat-python/tests` — green on Linux + macOS. +- [ ] **Pyright clean.** `pyright packages/simplex-chat-python/src` — zero errors. +- [ ] **Squaring bot smoke.** Run `python examples/squaring_bot.py` against a fresh database; verify (a) lazy lib download succeeds, (b) `bot.run()` blocks, (c) Ctrl-C exits cleanly. +- [ ] **CI dry run.** Push a `v0.0.0-test` tag to a fork; verify the `publish-python` job runs after `release-nodejs-libs` and the wheel uploads to TestPyPI (configure a separate test publisher if doing this). diff --git a/simplex-chat.cabal b/simplex-chat.cabal index 53946e8471..1e463dfe48 100644 --- a/simplex-chat.cabal +++ b/simplex-chat.cabal @@ -574,6 +574,7 @@ test-suite simplex-chat-test API.Docs.Commands API.Docs.Events API.Docs.Generate + API.Docs.Generate.Python API.Docs.Generate.TypeScript API.Docs.Responses API.Docs.Syntax diff --git a/src/Simplex/Chat/Store/SQLite/Migrations/agent_query_plans.txt b/src/Simplex/Chat/Store/SQLite/Migrations/agent_query_plans.txt index 78cd98705e..3a15c44bd8 100644 --- a/src/Simplex/Chat/Store/SQLite/Migrations/agent_query_plans.txt +++ b/src/Simplex/Chat/Store/SQLite/Migrations/agent_query_plans.txt @@ -273,16 +273,6 @@ Query: Plan: SEARCH connections USING PRIMARY KEY (conn_id=?) -Query: - SELECT user_id FROM users u - WHERE u.deleted = ? - AND NOT EXISTS (SELECT c.conn_id FROM connections c WHERE c.user_id = u.user_id) - -Plan: -SCAN u -CORRELATED SCALAR SUBQUERY 1 -SEARCH c USING COVERING INDEX idx_connections_user (user_id=?) - Query: SELECT user_id FROM users u WHERE u.user_id = ? @@ -535,21 +525,6 @@ Query: Plan: SEARCH conn_confirmations USING COVERING INDEX idx_conn_confirmations_conn_id (conn_id=?) -Query: - DELETE FROM encrypted_rcv_message_hashes - WHERE encrypted_rcv_message_hash_id IN ( - SELECT encrypted_rcv_message_hash_id - FROM encrypted_rcv_message_hashes - WHERE created_at < ? - ORDER BY created_at ASC - LIMIT ? - ) - -Plan: -SEARCH encrypted_rcv_message_hashes USING INTEGER PRIMARY KEY (rowid=?) -LIST SUBQUERY 1 -SEARCH encrypted_rcv_message_hashes USING COVERING INDEX idx_encrypted_rcv_message_hashes_created_at (created_at