riffsdk 0.26.0__tar.gz → 0.27.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. {riffsdk-0.26.0 → riffsdk-0.27.0}/AGENTS.md +3 -0
  2. {riffsdk-0.26.0 → riffsdk-0.27.0}/CHANGELOG.md +25 -0
  3. {riffsdk-0.26.0 → riffsdk-0.27.0}/LOCAL-TESTING.md +9 -2
  4. {riffsdk-0.26.0 → riffsdk-0.27.0}/PKG-INFO +122 -1
  5. {riffsdk-0.26.0 → riffsdk-0.27.0}/README.md +121 -0
  6. riffsdk-0.27.0/examples/query_metrics.py +72 -0
  7. {riffsdk-0.26.0 → riffsdk-0.27.0}/new-sdk-url-schema.md +1 -0
  8. {riffsdk-0.26.0 → riffsdk-0.27.0}/pyproject.toml +1 -1
  9. riffsdk-0.27.0/src/riffsdk/metrics/__init__.py +16 -0
  10. riffsdk-0.27.0/src/riffsdk/metrics/v0.py +596 -0
  11. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_imports.py +53 -1
  12. riffsdk-0.27.0/tests/test_metrics_v0.py +764 -0
  13. {riffsdk-0.26.0 → riffsdk-0.27.0}/.github/workflows/ci.yml +0 -0
  14. {riffsdk-0.26.0 → riffsdk-0.27.0}/.github/workflows/pr-comment.yml +0 -0
  15. {riffsdk-0.26.0 → riffsdk-0.27.0}/.github/workflows/release.yml +0 -0
  16. {riffsdk-0.26.0 → riffsdk-0.27.0}/.gitignore +0 -0
  17. {riffsdk-0.26.0 → riffsdk-0.27.0}/.python-version +0 -0
  18. {riffsdk-0.26.0 → riffsdk-0.27.0}/CLAUDE.md +0 -0
  19. {riffsdk-0.26.0 → riffsdk-0.27.0}/LICENCE +0 -0
  20. {riffsdk-0.26.0 → riffsdk-0.27.0}/USAGE_PROMPT.md +0 -0
  21. {riffsdk-0.26.0 → riffsdk-0.27.0}/examples/async_client.py +0 -0
  22. {riffsdk-0.26.0 → riffsdk-0.27.0}/examples/basic_crud.py +0 -0
  23. {riffsdk-0.26.0 → riffsdk-0.27.0}/examples/email_notice.py +0 -0
  24. {riffsdk-0.26.0 → riffsdk-0.27.0}/examples/file_upload_download.py +0 -0
  25. {riffsdk-0.26.0 → riffsdk-0.27.0}/examples/get_task.py +0 -0
  26. {riffsdk-0.26.0 → riffsdk-0.27.0}/examples/interpret_document.py +0 -0
  27. {riffsdk-0.26.0 → riffsdk-0.27.0}/examples/optimistic_concurrency.py +0 -0
  28. {riffsdk-0.26.0 → riffsdk-0.27.0}/mise.toml +0 -0
  29. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/__init__.py +0 -0
  30. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/__main__.py +0 -0
  31. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/_cli.py +0 -0
  32. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/_internal/__init__.py +0 -0
  33. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/_internal/auth.py +0 -0
  34. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/_internal/config.py +0 -0
  35. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/_internal/transport.py +0 -0
  36. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/ai/__init__.py +0 -0
  37. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/ai/v0.py +0 -0
  38. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/client.py +0 -0
  39. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/context.py +0 -0
  40. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/email/__init__.py +0 -0
  41. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/email/v0.py +0 -0
  42. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/__init__.py +0 -0
  43. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/_async_client.py +0 -0
  44. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/_client.py +0 -0
  45. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/_common.py +0 -0
  46. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/_http.py +0 -0
  47. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/_mime.py +0 -0
  48. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/_models.py +0 -0
  49. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/_session.py +0 -0
  50. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/_streaming.py +0 -0
  51. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/_upload.py +0 -0
  52. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/exceptions.py +0 -0
  53. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/storage/py.typed +0 -0
  54. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/tasks/__init__.py +0 -0
  55. {riffsdk-0.26.0 → riffsdk-0.27.0}/src/riffsdk/tasks/v0.py +0 -0
  56. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/__init__.py +0 -0
  57. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/conftest.py +0 -0
  58. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_ai_v0.py +0 -0
  59. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_async_client.py +0 -0
  60. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_auth.py +0 -0
  61. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_cli.py +0 -0
  62. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_client.py +0 -0
  63. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_config.py +0 -0
  64. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_context.py +0 -0
  65. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_email_v0.py +0 -0
  66. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_exceptions.py +0 -0
  67. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_http.py +0 -0
  68. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_importance.py +0 -0
  69. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_integration.py +0 -0
  70. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_mime.py +0 -0
  71. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_models.py +0 -0
  72. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_session.py +0 -0
  73. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_single_put_upload.py +0 -0
  74. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_streaming.py +0 -0
  75. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_tasks_v0.py +0 -0
  76. {riffsdk-0.26.0 → riffsdk-0.27.0}/tests/test_upload.py +0 -0
  77. {riffsdk-0.26.0 → riffsdk-0.27.0}/uv.lock +0 -0
@@ -64,6 +64,9 @@ src/riffsdk/
64
64
  ai/
65
65
  __init__.py # Docstring only - importing it loads no version
66
66
  v0.py # interpret_document - extract text from a stored document (LLM call)
67
+ metrics/
68
+ __init__.py # Docstring only - importing it loads no version
69
+ v0.py # query, get_schema, measure/filter helpers - Insights-style metrics about the app's own agent
67
70
  _internal/
68
71
  auth.py # RiffAuth - authentication
69
72
  config.py # Configuration handling
@@ -2,6 +2,31 @@
2
2
 
3
3
  <!-- version list -->
4
4
 
5
+ ## v0.27.0 (2026-12-01)
6
+
7
+ ### Bug Fixes
8
+
9
+ - **metrics**: Retry a 429 several times, apart from the failure attempts
10
+ ([#57](https://github.com/databutton/riff-sdk-python/pull/57),
11
+ [`47cd41e`](https://github.com/databutton/riff-sdk-python/commit/47cd41e9932864e47dd084a0b443277c74f47424))
12
+
13
+ - **metrics**: Two attempts per query, document 422/429 and the shared schema lists
14
+ ([#57](https://github.com/databutton/riff-sdk-python/pull/57),
15
+ [`47cd41e`](https://github.com/databutton/riff-sdk-python/commit/47cd41e9932864e47dd084a0b443277c74f47424))
16
+
17
+ ### Documentation
18
+
19
+ - **metrics**: Examples break down by task metadata, not tool input
20
+ ([#57](https://github.com/databutton/riff-sdk-python/pull/57),
21
+ [`47cd41e`](https://github.com/databutton/riff-sdk-python/commit/47cd41e9932864e47dd084a0b443277c74f47424))
22
+
23
+ ### Features
24
+
25
+ - **metrics**: Add riffsdk.metrics.v0, Insights-style queries over the app's own agent
26
+ ([#57](https://github.com/databutton/riff-sdk-python/pull/57),
27
+ [`47cd41e`](https://github.com/databutton/riff-sdk-python/commit/47cd41e9932864e47dd084a0b443277c74f47424))
28
+
29
+
5
30
  ## v0.26.0 (2026-11-17)
6
31
 
7
32
  ### Chores
@@ -60,5 +60,12 @@ Not applicable — riffsdk doesn't join the orchestrated stack as a service.
60
60
 
61
61
  ## Known limitations / degraded modes
62
62
 
63
- Integration tests only exercise the `storage` domain today. `tasks`/`ai` have no
64
- integration coverage yet — their tests all mock the transport.
63
+ Integration tests only exercise the `storage` domain today. `tasks`/`ai`/`metrics`
64
+ have no integration coverage yet — their tests all mock the transport.
65
+
66
+ To try `riffsdk.metrics.v0` by hand against a local stack, point
67
+ `API_RIFF_NEW_URL` at the front proxy and run `examples/query_metrics.py` from a
68
+ project whose id is in `RIFF_PROJECT_ID`. The orchestrator answers only the
69
+ devx and prodx appbutler roles (it takes the project and the environment from
70
+ them), so a person's or an API key's token gets a 400 from `/query` and
71
+ `/schema`, by design.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: riffsdk
3
- Version: 0.26.0
3
+ Version: 0.27.0
4
4
  Summary: Python SDK for the Riff Storage API
5
5
  Project-URL: Homepage, https://riff.ai
6
6
  Author-email: Martin Sandve Alnæs <msa@databutton.io>
@@ -197,6 +197,127 @@ version, and the one you imported keeps behaving as it did. Importing
197
197
  the client's own default: this proxies to an LLM call through
198
198
  riff-orchestrator, whose own timeout defaults to 120s.
199
199
 
200
+ ## Metrics
201
+
202
+ Ask for numbers about the app's own agent -- its tasks, the decisions it asked
203
+ people for, the tool calls it made and the hooks that fired -- with a query,
204
+ instead of a new endpoint per number. The query has the shape of an analytics
205
+ "insights" query: an event, how to measure it, filters, a breakdown, and a time
206
+ range bucketed by a unit.
207
+
208
+ ```python
209
+ import riffsdk.metrics.v0 as metrics
210
+
211
+ result = metrics.query(
212
+ event="decision_resolved",
213
+ measure=metrics.median("wait_seconds"), # default: metrics.count()
214
+ filters=[metrics.where("type", "eq", "approval")],
215
+ breakdown="task.metadata.supplier", # up to two properties
216
+ last_days=30, # or from_=/to= (datetime or ISO 8601)
217
+ unit="week", # hour | day | week | month | total
218
+ )
219
+ for row in result.rows("a"): # a single series is called "a"
220
+ print(row["bucket"], row["breakdown"], row["value"])
221
+ ```
222
+
223
+ Call `metrics.get_schema()` first to see the events, their properties and
224
+ types, the measures, filter operators and units (one list shared by all events),
225
+ the limits, and the project's task metadata keys, rather than guessing names.
226
+
227
+ Several series (one to four) can be read side by side or combined with a
228
+ `formula` of numbers, series ids and `+ - * / ( )`, evaluated per bucket and
229
+ breakdown row. The result then has one more series, `"formula"`, whose `value`
230
+ is `None` where it divided by zero:
231
+
232
+ ```python
233
+ result = metrics.query(
234
+ series=[
235
+ {"id": "a", "event": "decision_requested", "measure": "count",
236
+ "filters": [metrics.where("type", "eq", "escalation")]},
237
+ {"id": "b", "event": "task_created", "measure": "count"},
238
+ ],
239
+ formula="a / b",
240
+ last_days=30,
241
+ unit="total",
242
+ )
243
+ escalation_rate = result.rows("formula")[0]["value"]
244
+ ```
245
+
246
+ A view's backend is the usual caller. In `backend/app/views/<name>.py`, combine
247
+ the agent's numbers with the app's own data:
248
+
249
+ ```python
250
+ import riffsdk.metrics.v0 as metrics
251
+
252
+ def handler(params):
253
+ sent = metrics.query(
254
+ event="tool_called",
255
+ filters=[
256
+ metrics.where("tool_name", "eq", "send_chase"),
257
+ metrics.where("output.status", "eq", "sent"),
258
+ ],
259
+ last_days=90,
260
+ unit="week",
261
+ )
262
+ weekly = [{"week": r["bucket"], "sent": r["value"]} for r in sent.rows("a")]
263
+ return {"weekly_chases": weekly, "open_lines": load_open_lines()} # your own ERP data
264
+ ```
265
+
266
+ `input.<key>` and `output.<key>` of a tool call can be used in filters and
267
+ measures but not in `breakdown`; split by a task metadata key
268
+ (`task.metadata.<key>`) instead.
269
+
270
+ This is for what the orchestrator knows. ERP and other app data stay in the
271
+ app, and raw rows (a list of tasks or tool calls) are not metrics.
272
+
273
+ A project reads only its own metrics. The project, the account and the
274
+ environment are decided by the credentials the request is made with: a
275
+ deployed app sees its production data, the same app in the workspace sees the
276
+ workspace's. Nothing in a query names them, and there is no way to reach
277
+ another's.
278
+
279
+ Import the version you are writing against, bound to a short name as above. A
280
+ version does not change under you: when the API changes it appears as a new
281
+ version, and the one you imported keeps behaving as it did. Importing `riffsdk`
282
+ or `riffsdk.metrics` loads no version.
283
+
284
+ ### API
285
+
286
+ - `query(*, series=None, event=None, measure=None, filters=None, from_=None,
287
+ to=None, last_days=None, unit=None, breakdown=None, formula=None, limit=None,
288
+ client=None)` -- returns a `MetricsResult`. Either `event=` (with `measure=`,
289
+ default `count()`, and `filters=`) for one series, or `series=[{"id", "event",
290
+ "measure", "filters"?}, ...]`. Give the time as `from_` (and optionally `to`,
291
+ exclusive) -- a `datetime` (naive ones are taken as UTC) or an ISO 8601
292
+ string -- or as `last_days=N`. `limit` keeps the top N breakdown combinations.
293
+ `query_async` is the async form.
294
+ - `get_schema(*, client=None)` -- what can be queried. `get_schema_async` is the
295
+ async form.
296
+ - `MetricsResult` is the answer as JSON plus the `version` of that shape. Read
297
+ it by key (`result["meta"]`, `result.get("series")`, or `result.data`), so
298
+ the server gaining a field needs no SDK release. `result.series` is the list
299
+ of series and `result.rows(series_id)` the rows of one, each
300
+ `{"bucket", "breakdown", "value"}`.
301
+ - Measures: `count()`, `unique(p)`, `sum_(p)`, `avg(p)`, `median(p)`, `p90(p)`,
302
+ `min_(p)`, `max_(p)`. They are only strings (`"avg(wait_seconds)"`), as a
303
+ filter is only a dict (`where(p, op, value)` builds
304
+ `{"property", "op", "value"}`), so plain ones work as well.
305
+ - Raises `ValueError`, without sending anything, for a query that cannot be
306
+ valid (no series or more than four, more than two breakdowns, `last_days`
307
+ together with `from_`). Raises `MetricsError` (`status_code`, `message`) if
308
+ the server rejected the query -- a 400 whose message says what to change,
309
+ e.g. an unknown event or property, or a range that is too long; or a 422 if it
310
+ is too expensive to run (shorten the range, add filters, use a coarser unit)
311
+ -- or did not answer, including on a persistent 429/5xx. A timeout or 5xx gets two
312
+ attempts in all, so a slow query cannot be re-run several times on the server.
313
+ A 429 (the project already has queries running, as when a view loads several
314
+ tiles at once) is retried up to eight times, about a second apart, before it
315
+ reaches you.
316
+ - `client` lets many calls share one connection; see `riffsdk.tasks`' API
317
+ notes above -- the modules default to and can share the same
318
+ `RiffClient`/`AsyncRiffClient`. A query runs under a server-side statement
319
+ timeout of about 10s; the call's own timeout is 30s.
320
+
200
321
  ## Email
201
322
 
202
323
  The AI notice that goes at the end of an email an agent sends. Recipients have to
@@ -183,6 +183,127 @@ version, and the one you imported keeps behaving as it did. Importing
183
183
  the client's own default: this proxies to an LLM call through
184
184
  riff-orchestrator, whose own timeout defaults to 120s.
185
185
 
186
+ ## Metrics
187
+
188
+ Ask for numbers about the app's own agent -- its tasks, the decisions it asked
189
+ people for, the tool calls it made and the hooks that fired -- with a query,
190
+ instead of a new endpoint per number. The query has the shape of an analytics
191
+ "insights" query: an event, how to measure it, filters, a breakdown, and a time
192
+ range bucketed by a unit.
193
+
194
+ ```python
195
+ import riffsdk.metrics.v0 as metrics
196
+
197
+ result = metrics.query(
198
+ event="decision_resolved",
199
+ measure=metrics.median("wait_seconds"), # default: metrics.count()
200
+ filters=[metrics.where("type", "eq", "approval")],
201
+ breakdown="task.metadata.supplier", # up to two properties
202
+ last_days=30, # or from_=/to= (datetime or ISO 8601)
203
+ unit="week", # hour | day | week | month | total
204
+ )
205
+ for row in result.rows("a"): # a single series is called "a"
206
+ print(row["bucket"], row["breakdown"], row["value"])
207
+ ```
208
+
209
+ Call `metrics.get_schema()` first to see the events, their properties and
210
+ types, the measures, filter operators and units (one list shared by all events),
211
+ the limits, and the project's task metadata keys, rather than guessing names.
212
+
213
+ Several series (one to four) can be read side by side or combined with a
214
+ `formula` of numbers, series ids and `+ - * / ( )`, evaluated per bucket and
215
+ breakdown row. The result then has one more series, `"formula"`, whose `value`
216
+ is `None` where it divided by zero:
217
+
218
+ ```python
219
+ result = metrics.query(
220
+ series=[
221
+ {"id": "a", "event": "decision_requested", "measure": "count",
222
+ "filters": [metrics.where("type", "eq", "escalation")]},
223
+ {"id": "b", "event": "task_created", "measure": "count"},
224
+ ],
225
+ formula="a / b",
226
+ last_days=30,
227
+ unit="total",
228
+ )
229
+ escalation_rate = result.rows("formula")[0]["value"]
230
+ ```
231
+
232
+ A view's backend is the usual caller. In `backend/app/views/<name>.py`, combine
233
+ the agent's numbers with the app's own data:
234
+
235
+ ```python
236
+ import riffsdk.metrics.v0 as metrics
237
+
238
+ def handler(params):
239
+ sent = metrics.query(
240
+ event="tool_called",
241
+ filters=[
242
+ metrics.where("tool_name", "eq", "send_chase"),
243
+ metrics.where("output.status", "eq", "sent"),
244
+ ],
245
+ last_days=90,
246
+ unit="week",
247
+ )
248
+ weekly = [{"week": r["bucket"], "sent": r["value"]} for r in sent.rows("a")]
249
+ return {"weekly_chases": weekly, "open_lines": load_open_lines()} # your own ERP data
250
+ ```
251
+
252
+ `input.<key>` and `output.<key>` of a tool call can be used in filters and
253
+ measures but not in `breakdown`; split by a task metadata key
254
+ (`task.metadata.<key>`) instead.
255
+
256
+ This is for what the orchestrator knows. ERP and other app data stay in the
257
+ app, and raw rows (a list of tasks or tool calls) are not metrics.
258
+
259
+ A project reads only its own metrics. The project, the account and the
260
+ environment are decided by the credentials the request is made with: a
261
+ deployed app sees its production data, the same app in the workspace sees the
262
+ workspace's. Nothing in a query names them, and there is no way to reach
263
+ another's.
264
+
265
+ Import the version you are writing against, bound to a short name as above. A
266
+ version does not change under you: when the API changes it appears as a new
267
+ version, and the one you imported keeps behaving as it did. Importing `riffsdk`
268
+ or `riffsdk.metrics` loads no version.
269
+
270
+ ### API
271
+
272
+ - `query(*, series=None, event=None, measure=None, filters=None, from_=None,
273
+ to=None, last_days=None, unit=None, breakdown=None, formula=None, limit=None,
274
+ client=None)` -- returns a `MetricsResult`. Either `event=` (with `measure=`,
275
+ default `count()`, and `filters=`) for one series, or `series=[{"id", "event",
276
+ "measure", "filters"?}, ...]`. Give the time as `from_` (and optionally `to`,
277
+ exclusive) -- a `datetime` (naive ones are taken as UTC) or an ISO 8601
278
+ string -- or as `last_days=N`. `limit` keeps the top N breakdown combinations.
279
+ `query_async` is the async form.
280
+ - `get_schema(*, client=None)` -- what can be queried. `get_schema_async` is the
281
+ async form.
282
+ - `MetricsResult` is the answer as JSON plus the `version` of that shape. Read
283
+ it by key (`result["meta"]`, `result.get("series")`, or `result.data`), so
284
+ the server gaining a field needs no SDK release. `result.series` is the list
285
+ of series and `result.rows(series_id)` the rows of one, each
286
+ `{"bucket", "breakdown", "value"}`.
287
+ - Measures: `count()`, `unique(p)`, `sum_(p)`, `avg(p)`, `median(p)`, `p90(p)`,
288
+ `min_(p)`, `max_(p)`. They are only strings (`"avg(wait_seconds)"`), as a
289
+ filter is only a dict (`where(p, op, value)` builds
290
+ `{"property", "op", "value"}`), so plain ones work as well.
291
+ - Raises `ValueError`, without sending anything, for a query that cannot be
292
+ valid (no series or more than four, more than two breakdowns, `last_days`
293
+ together with `from_`). Raises `MetricsError` (`status_code`, `message`) if
294
+ the server rejected the query -- a 400 whose message says what to change,
295
+ e.g. an unknown event or property, or a range that is too long; or a 422 if it
296
+ is too expensive to run (shorten the range, add filters, use a coarser unit)
297
+ -- or did not answer, including on a persistent 429/5xx. A timeout or 5xx gets two
298
+ attempts in all, so a slow query cannot be re-run several times on the server.
299
+ A 429 (the project already has queries running, as when a view loads several
300
+ tiles at once) is retried up to eight times, about a second apart, before it
301
+ reaches you.
302
+ - `client` lets many calls share one connection; see `riffsdk.tasks`' API
303
+ notes above -- the modules default to and can share the same
304
+ `RiffClient`/`AsyncRiffClient`. A query runs under a server-side statement
305
+ timeout of about 10s; the call's own timeout is 30s.
306
+
186
307
  ## Email
187
308
 
188
309
  The AI notice that goes at the end of an email an agent sends. Recipients have to
@@ -0,0 +1,72 @@
1
+ """Ask the orchestrator for numbers about the app's own agent.
2
+
3
+ The case this exists for: a KPI view ("how many chase e-mails went out each
4
+ week", "how long do people take to answer the agent", "what share of tasks
5
+ needed an escalation") used to need its own endpoint per number. One query
6
+ now says which events to count, how to measure them, and how to split them.
7
+
8
+ Run this from an app's backend, where the environment it needs is already set.
9
+ A project reads only its own metrics: nothing in a query names the project, the
10
+ account or the environment, and there is no way to reach another one.
11
+ """
12
+
13
+ import riffsdk.metrics.v0 as metrics
14
+
15
+ # Look at what can be queried before guessing at names.
16
+ schema = metrics.get_schema()
17
+ print(f"payload version {schema.version}")
18
+ print("events:", [e["name"] for e in schema["events"]])
19
+
20
+ # 1. One series: chase e-mails actually sent, per week, per supplier (a key in
21
+ # the task's metadata; tool input/output can filter but never break down).
22
+ sent = metrics.query(
23
+ event="tool_called",
24
+ measure=metrics.count(),
25
+ filters=[
26
+ metrics.where("tool_name", "eq", "send_chase"),
27
+ metrics.where("output.status", "eq", "sent"),
28
+ metrics.where("is_error", "eq", False),
29
+ ],
30
+ breakdown=["task.metadata.supplier"],
31
+ last_days=90,
32
+ unit="week",
33
+ )
34
+ for row in sent.rows("a"):
35
+ print(row["bucket"], row["breakdown"], row["value"])
36
+
37
+ # 2. How long people take to answer the agent: the median, per week.
38
+ answer_time = metrics.query(
39
+ event="decision_resolved",
40
+ measure=metrics.median("wait_seconds"),
41
+ last_days=30,
42
+ unit="week",
43
+ )
44
+ for row in answer_time.rows("a"):
45
+ print(row["bucket"], row["value"])
46
+
47
+ # 3. Two series and a formula: the escalation rate, per supplier.
48
+ rate = metrics.query(
49
+ series=[
50
+ {
51
+ "id": "a",
52
+ "event": "decision_requested",
53
+ "measure": metrics.count(),
54
+ "filters": [metrics.where("type", "eq", "escalation")],
55
+ },
56
+ {"id": "b", "event": "task_created", "measure": metrics.count()},
57
+ ],
58
+ formula="a / b",
59
+ breakdown="task.metadata.supplier",
60
+ last_days=30,
61
+ unit="total",
62
+ )
63
+ for row in rate.rows("formula"):
64
+ # value is None where the formula divided by zero
65
+ print(row["breakdown"], row["value"])
66
+
67
+ # A query the server rejects (an unknown event or property) raises
68
+ # MetricsError with its reason; metrics.query_async is the async form.
69
+ try:
70
+ metrics.query(event="no_such_event", last_days=1)
71
+ except metrics.MetricsError as e:
72
+ print(f"rejected ({e.status_code}): {e.message}")
@@ -46,6 +46,7 @@ is introduced later, that would move to `/sdk/v1/tasks`).
46
46
  | storage | `/_projects/{id}/sdk/v0/storage/<tail>` | `$DBAPI_SERVICE_URL` (riff-api, same backend as today) | `/riff-api/storage/v1/<tail>` |
47
47
  | tasks | `/_projects/{id}/sdk/v0/tasks/<tail>` | `$ORCHESTRATOR_SERVICE_URL` (riff-orchestrator, **directly** — bypasses riff-api) | `/api/v0/riffsdk/tasks/<tail>` |
48
48
  | ai | `/_projects/{id}/sdk/v0/ai/<tail>` | `$ORCHESTRATOR_SERVICE_URL` (riff-orchestrator, **directly** — bypasses riff-api) | `/api/v0/riffsdk/ai/<tail>` |
49
+ | metrics | `/_projects/{id}/sdk/v0/metrics/<tail>` | `$ORCHESTRATOR_SERVICE_URL` (riff-orchestrator, **directly** — bypasses riff-api) | `/api/v0/riffsdk/metrics/<tail>` |
49
50
 
50
51
  The proxy does a literal prefix substitution (`/_projects/{id}/sdk/v0/tasks`
51
52
  → `/api/v0/riffsdk/tasks`, etc.) and leaves everything after that prefix
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "riffsdk"
7
- version = "0.26.0"
7
+ version = "0.27.0"
8
8
  description = "Python SDK for the Riff Storage API"
9
9
  readme = "README.md"
10
10
  license = "MIT"
@@ -0,0 +1,16 @@
1
+ """Query the metrics of the app's agent: tasks, decisions, tool calls and hooks.
2
+
3
+ Import the version you are writing against, bound to a short name::
4
+
5
+ import riffsdk.metrics.v0 as metrics
6
+
7
+ result = metrics.query(
8
+ event="decision_resolved",
9
+ measure=metrics.median("wait_seconds"),
10
+ last_days=30,
11
+ unit="week",
12
+ )
13
+
14
+ A version does not change under you. When the API changes it appears as a new
15
+ version, and the one you imported keeps behaving as it did.
16
+ """