async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)
async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)
async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}
def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}
def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}
def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}
def watchdog():
metrics = registry.active()
for m in metrics:
recent = stream.read(m.id, window="5m")
if anomaly.detect(recent, m.baseline):
notify(
tenant=m.tenant,
metric=m.id,
severity=anomaly.severity(recent),
)
def lock_lines(order_id, lines):
with table.transaction() as tx:
for line in lines:
tx.put_item(
Item={
"pk": f"order#{order_id}",
"sk": f"line#{line.id}",
"locked_at": now(),
"stripe_pi": line.payment_intent,
},
ConditionExpression="attribute_not_exists(sk)",
)
async def assist(query, ctx):
plan = await planner.draft(query, ctx)
if plan.requires_approval:
return plan.to_response()
docs = await retriever.search(query, k=8)
answer = await llm.complete(
prompt=template(query, docs),
model=ctx.model or "gpt-4o-mini",
stream=True,
)
return stream(answer, evidence=docs)
def score_churn(window=90):
profiles = store.scan(tenant=tenant)
for batch in chunks(profiles, 500):
feats = features.build(
batch,
recency=True,
frequency=True,
trend=True,
)
scores = model.predict(feats)
for profile, score in zip(batch, scores):
profile.predictions.churn = score
store.put(profile)
def evaluate(profile, event):
rules = registry.for_tenant(profile.tenant)
for rule in rules:
if rule.condition.match(event, profile):
dispatch(
action=rule.action,
target=rule.target,
payload=rule.render(profile, event),
retry=rule.retry_policy,
dlq=rule.dlq,
)
audit.append(rule.id, profile.id, event.id)
async def handler(event, ctx):
body = json.loads(event["body"])
profile = await profiles.upsert(
body["profile_id"],
traits=body.get("traits"),
tenant=ctx.tenant,
)
await events.append(
profile_id=profile.id,
kind=body["kind"],
payload=body.get("payload", {}),
ts=now(),
)
await rules.evaluate(profile, body)
return {"statusCode": 202}