Python SDK for cq — shared agent knowledge commons
Project description
cq-sdk
Python SDK for cq — the shared agent knowledge commons.
Lets any Python application query, propose, confirm, and flag knowledge units against a remote cq API, or store locally when no remote is configured.
Installation
uv add cq-sdk
Or with pip:
pip install cq-sdk
Quick Start
from cq import Client, FlagReason
cq = Client() # Auto-discovers config; falls back to local-only.
# Query.
results = cq.query(domains=["api", "stripe"], language="python")
# Propose.
ku = cq.propose(
summary="Stripe 402 means card_declined",
detail="Check error.code, not error.type.",
action="Handle card_declined explicitly.",
domains=["api", "stripe"],
)
# Confirm / flag.
cq.confirm(ku.id)
cq.flag(ku.id, reason=FlagReason.STALE)
# Get the canonical agent prompts.
from cq import prompts
skill_prompt = prompts.skill()
reflect_prompt = prompts.reflect()
Configuration
The client reads configuration from environment variables:
| Variable | Description | Default |
|---|---|---|
CQ_ADDR |
Remote cq API address | None (local-only) |
CQ_API_KEY |
API key for the remote API | None |
CQ_LOCAL_DATABASE_URL |
Local store connection URL (e.g. sqlite:///abs/path/local.db) |
None (falls back to CQ_LOCAL_DB_PATH) |
CQ_LOCAL_DB_PATH |
Local SQLite file path | $XDG_DATA_HOME/cq/local.db |
XDG_DATA_HOME |
Base directory for the default database path (XDG spec) | ~/.local/share |
Or pass directly:
cq = Client(
addr="http://localhost:3000",
local_db_path=Path("~/.local/share/cq/local.db").expanduser(),
)
Store protocol
The local store is pluggable. The SDK defines a Store runtime-checkable Protocol that the Client depends on; the default SqliteStore satisfies it, and you can supply any implementation.
Selecting a store
The client resolves the local store in this precedence order:
store=argument — inject anycq.Storedirectly.CQ_LOCAL_DATABASE_URL— a connection-string URL resolved bycq.create_store. Accepted schemes:sqlite:///abs/pathorsqlite:path. Apostgresql://URL raisesNotImplementedErrornaming the plannedcq-sdk[postgres]extra.local_db_path=argument /CQ_LOCAL_DB_PATHenv var — path to a SQLite file.- XDG default —
$XDG_DATA_HOME/cq/local.db(typically~/.local/share/cq/local.db).
Interface
The cq.Store Protocol requires eight methods. Implementations must be safe for use across asyncio.to_thread executor threads.
| Method | Signature | Semantics |
|---|---|---|
get |
(unit_id: str) -> KnowledgeUnit | None |
Retrieve by ID, or None if absent. |
all |
() -> list[KnowledgeUnit] |
Return every unit in the store. |
insert |
(unit: KnowledgeUnit) -> None |
Insert a unit. Raise DuplicateUnitError on an existing ID; raise ValueError if domains are empty after normalization. |
update |
(unit: KnowledgeUnit) -> None |
Replace an existing unit. Raise KeyError when the ID is absent; raise ValueError if domains are empty. |
delete |
(unit_id: str) -> None |
Remove by ID. Raise KeyError when absent. |
query |
(params: QueryParams) -> StoreQueryResult |
Return units matching the query, ranked most-relevant first. |
stats |
(*, recent_limit: int = 5) -> StoreStats |
Return aggregated store statistics. |
close |
() -> None |
Release resources. Must be safe to call more than once. |
Built-in implementations
SqliteStore— the default. Opens a SQLite file with FTS5 full-text search, WAL journaling, and domain-tag indexing.InMemoryStore— map-backed, no persistence. Useful for tests and as a worked example for custom stores (domain-tag matching only, no full-text).
from cq import Client, InMemoryStore
client = Client(store=InMemoryStore())
Bring your own
Implement the cq.Store protocol and inject it via store=. Reuse the shared ranker cq.rank_candidates from your query implementation so ranking stays consistent across backends. Verify the implementation against the conformance suite in tests/conformance.py:
from tests.conformance import run_store_conformance
run_store_conformance(lambda: MyCustomStore())
Knowledge tiers
Every knowledge unit has a tier: local (on-disk SQLite, never leaves the machine), private (stored on the remote API at CQ_ADDR, visible to every client pointing at the same remote), or public (open commons; not yet available).
With a remote configured, cq.propose(...) sends the unit to the remote and returns it tagged private; with no remote, or if the remote is unreachable, it writes the unit locally as local.
See the top-level README for the full description.
Dev Setup
uv sync --group dev
Testing
make test
Linting
make lint
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file cq_sdk-0.16.0.tar.gz.
File metadata
- Download URL: cq_sdk-0.16.0.tar.gz
- Upload date:
- Size: 78.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
876bc569fe5fb7f9980b9f35c3c4351e54b756b86ed5457d7706cfb9b3654650
|
|
| MD5 |
4f3df21103bcedac6b3b273fc7316f97
|
|
| BLAKE2b-256 |
27c00a3eb487970210ba7f182e87eb91ecc88c476fd029d3f0833e61a8b24218
|
Provenance
The following attestation bundles were made for cq_sdk-0.16.0.tar.gz:
Publisher:
release-sdk-python.yaml on mozilla-ai/cq
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cq_sdk-0.16.0.tar.gz -
Subject digest:
876bc569fe5fb7f9980b9f35c3c4351e54b756b86ed5457d7706cfb9b3654650 - Sigstore transparency entry: 1927324517
- Sigstore integration time:
-
Permalink:
mozilla-ai/cq@50ae4a3b337a298a9e420a4878f2c1af9fb97e58 -
Branch / Tag:
refs/tags/sdk/python/0.16.0 - Owner: https://github.com/mozilla-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-sdk-python.yaml@50ae4a3b337a298a9e420a4878f2c1af9fb97e58 -
Trigger Event:
release
-
Statement type:
File details
Details for the file cq_sdk-0.16.0-py3-none-any.whl.
File metadata
- Download URL: cq_sdk-0.16.0-py3-none-any.whl
- Upload date:
- Size: 53.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e27872414dba8ca570519a9144dd2aa76722e5d4a55f999e84c57ca32106e643
|
|
| MD5 |
32eb4fe998001e672372c8f422aa2069
|
|
| BLAKE2b-256 |
a1bb9eea2bb5a25041d52d0071f513619347c87930a3d043352f7dd69f3f6f5c
|
Provenance
The following attestation bundles were made for cq_sdk-0.16.0-py3-none-any.whl:
Publisher:
release-sdk-python.yaml on mozilla-ai/cq
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
cq_sdk-0.16.0-py3-none-any.whl -
Subject digest:
e27872414dba8ca570519a9144dd2aa76722e5d4a55f999e84c57ca32106e643 - Sigstore transparency entry: 1927325074
- Sigstore integration time:
-
Permalink:
mozilla-ai/cq@50ae4a3b337a298a9e420a4878f2c1af9fb97e58 -
Branch / Tag:
refs/tags/sdk/python/0.16.0 - Owner: https://github.com/mozilla-ai
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release-sdk-python.yaml@50ae4a3b337a298a9e420a4878f2c1af9fb97e58 -
Trigger Event:
release
-
Statement type: