← All ivy-nodes
IVYXSTUDIO · IVY NODE
S
Social: pack the reply result
ivy.node.reply-pack · v0.1.0
ivyx✓
Packs the responder's answer as one record of kind reply-result for an IVYX Social conversation: sent or not, and why. No model, no network.
#social#reply#ivyx-social
Inputs
| Field | Type | Description |
|---|---|---|
| conversation_id | string | Not declared |
| reason | string | Not declared |
| reply | object | Not declared |
| reply_a | object | Not declared |
| reply_b | object | Not declared |
| reply_c | object | Not declared |
| reply_d | object | Not declared |
Outputs
| Field | Type | Description |
|---|---|---|
| kindrequired | string | Always reply-result. |
| conversation_id | string | Not declared |
| sent | boolean | Not declared |
| id | string | Not declared |
| at | integer | Not declared |
| reason | string | Not declared |
Source
python
# Input preparation
inp = __ivy_ctx__["nodes"][__ivy_node_id__]["input"]
# Compute
# Shared by every social node (built into each by build.mjs): dates, blocks, merging.
import datetime, json, re, statistics
def day(s):
s = str(s or "")[:10]
return s if re.fullmatch(r"\d{4}-\d{2}-\d{2}", s) else None
def now_iso():
return datetime.datetime.utcnow().replace(microsecond=0).isoformat() + "Z"
def ms_of(s):
if s is None or s == "":
return None
if isinstance(s, (int, float)):
return int(s * 1000) if s < 1e12 else int(s)
t = str(s).replace("Z", "+00:00")
t = re.sub(r"([+-]\d{2})(\d{2})$", r"\1:\2", t)
try:
return int(datetime.datetime.fromisoformat(t).timestamp() * 1000)
except ValueError:
return None
def rows_of(block):
"""A {fields, rows} block as dicts."""
if not isinstance(block, dict) or not block.get("fields"):
return []
f = block["fields"]
return [dict(zip(f, r)) for r in block.get("rows") or []]
def block_of(dicts, first):
fields = list(first)
for d in dicts:
for k in d:
if k not in fields:
fields.append(k)
return {"fields": fields, "rows": [[d.get(k) for k in fields] for d in dicts]}
def listed(v):
return [x for x in (v if isinstance(v, list) else [v] if v else []) if x is not None]
def text_of(v, limit=4000):
s = " ".join(str(v).split()) if isinstance(v, (str, int, float)) else ""
return s[:limit]
class ReplyPack:
"Social: pack the reply result"
def run(self, inp):
out = {}
# The responder's answer as one record for the conversation.
r = next((inp.get(k) for k in ("reply", "reply_a", "reply_b", "reply_c", "reply_d") if isinstance(inp.get(k), dict) and inp[k].get("sent")), None)
out.update({"kind": "reply-result", "conversation_id": inp.get("conversation_id") or "", "sent": bool(r), "id": (r or {}).get("id"), "at": (r or {}).get("at"), "reason": "" if r else (inp.get("reason") or "No responder answered.")})
return out
result = ReplyPack().run(inp)
# Output collection (the runner reads it here)
out = __ivy_ctx__["nodes"][__ivy_node_id__]["output"]
out.update(result)Tests
Requires: python:3.9
- sent
The responder that sent is the answer.
- none
No responder sending is a reply not sent, with the plan's reason.