Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

feat(cli): friendly contact flags + vCard import/export commands

rubenluetke10-beep85b5c2451d30
diff --git a/jarvis/cli_ctl/commands/contacts.py b/jarvis/cli_ctl/commands/contacts.py
index a721605086..cfb4d80e9e 100644
--- a/jarvis/cli_ctl/commands/contacts.py
+++ b/jarvis/cli_ctl/commands/contacts.py
@@ -1,20 +1,28 @@
-"""contacts: address-book CRUD (/api/contacts).
+"""contacts: address-book CRUD + vCard import/export (/api/contacts).
-Placing a call is not a single REST endpoint (it is a brain tool that composes a
-contact's number with telephony); use `jarvis telephony outbound` with a number,
-or the running assistant, to place a call.
+``add``/``edit`` take friendly flags (--name/--email/--phone/…) for humans and
+scripts alike; ``--json-body`` remains for full-record round-trips. Placing a
+call is not a single REST endpoint (it is a brain tool that composes a
+contact's number with telephony); use `jarvis telephony outbound` with a
+number, or the running assistant, to place a call.
"""
from __future__ import annotations
import json
import sys
+from pathlib import Path
+from typing import Annotated
import typer
from jarvis.cli_ctl import invoke, options, render
+from jarvis.cli_ctl.client import ApiError
-app = typer.Typer(no_args_is_help=True, help="Contacts: list, show, add, edit, delete.")
+app = typer.Typer(
+ no_args_is_help=True,
+ help="Contacts: list, show, add, edit, delete, import/export vCard.",
+)
@app.command("list")
@@ -38,32 +46,173 @@ def _body_from(json_body: str) -> dict:
raise typer.Exit(code=2) from exc
+def _body_from_flags(
+ *,
+ name: str | None,
+ alias: list[str] | None,
+ relationship: str | None,
+ email: list[str] | None,
+ phone: list[str] | None,
+ url: list[str] | None,
+ tag: list[str] | None,
+ organization: str | None,
+ role: str | None,
+ birthday: str | None,
+ note: str | None,
+ favorite: bool | None,
+) -> dict:
+ """Collect only the flags that were actually given (PATCH-friendly)."""
+ body: dict = {}
+ if name is not None:
+ body["name"] = name
+ if alias:
+ body["aliases"] = list(alias)
+ if relationship is not None:
+ body["relationship"] = relationship
+ if email:
+ body["emails"] = list(email)
+ if phone:
+ body["phones"] = list(phone)
+ if url:
+ body["urls"] = list(url)
+ if tag:
+ body["tags"] = list(tag)
+ if organization is not None:
+ body["organization"] = organization
+ if role is not None:
+ body["role"] = role
+ if birthday is not None:
+ body["birthday"] = birthday
+ if note is not None:
+ body["note"] = note
+ if favorite is not None:
+ body["favorite"] = favorite
+ return body
+
+
@app.command()
def add(
- json_body: str = typer.Option(
- ..., "--json-body", help="Contact JSON ('-' reads stdin): {name, emails?, phones?, ...}."
+ name: str | None = typer.Option(None, "--name", help="Contact name."),
+ alias: Annotated[
+ list[str] | None, typer.Option("--alias", help="Alias (repeatable).")
+ ] = None,
+ relationship: str | None = typer.Option(
+ None,
+ "--relationship",
+ help="family|friend|colleague|partner|acquaintance|other",
+ ),
+ email: Annotated[
+ list[str] | None, typer.Option("--email", help="E-mail (repeatable).")
+ ] = None,
+ phone: Annotated[
+ list[str] | None, typer.Option("--phone", help="Phone (repeatable).")
+ ] = None,
+ url: Annotated[
+ list[str] | None, typer.Option("--url", help="Web link (repeatable).")
+ ] = None,
+ tag: Annotated[
+ list[str] | None, typer.Option("--tag", help="Free-form tag (repeatable).")
+ ] = None,
+ organization: str | None = typer.Option(None, "--organization", help="Company."),
+ role: str | None = typer.Option(None, "--role", help="Job title / role."),
+ birthday: str | None = typer.Option(
+ None, "--birthday", help="ISO date, e.g. 1990-04-12."
+ ),
+ note: str | None = typer.Option(None, "--note", help="Markdown README."),
+ favorite: bool = typer.Option(False, "--favorite", help="Pin as favorite."),
+ json_body: str | None = typer.Option(
+ None,
+ "--json-body",
+ help="Raw contact JSON ('-' reads stdin); overrides all field flags.",
),
yes: bool = options.yes_opt(),
dry_run: bool = options.dry_opt(),
) -> None:
- """Add a contact."""
- invoke.run("POST", "/api/contacts", body=_body_from(json_body), assume_yes=yes, dry_run=dry_run)
+ """Add a contact (field flags, or --json-body for the full record)."""
+ if json_body is not None:
+ body = _body_from(json_body)
+ else:
+ body = _body_from_flags(
+ name=name,
+ alias=alias,
+ relationship=relationship,
+ email=email,
+ phone=phone,
+ url=url,
+ tag=tag,
+ organization=organization,
+ role=role,
+ birthday=birthday,
+ note=note,
+ favorite=favorite or None,
+ )
+ if "name" not in body:
+ render.error("--name is required (or pass --json-body).")
+ raise typer.Exit(code=2)
+ invoke.run("POST", "/api/contacts", body=body, assume_yes=yes, dry_run=dry_run)
@app.command()
def edit(
slug: str = typer.Argument(...),
- json_body: str = typer.Option(
- ..., "--json-body", help="Partial contact JSON ('-' reads stdin)."
+ name: str | None = typer.Option(None, "--name"),
+ alias: Annotated[
+ list[str] | None,
+ typer.Option("--alias", help="Replaces the alias list (repeatable)."),
+ ] = None,
+ relationship: str | None = typer.Option(None, "--relationship"),
+ email: Annotated[
+ list[str] | None,
+ typer.Option("--email", help="Replaces the e-mail list (repeatable)."),
+ ] = None,
+ phone: Annotated[
+ list[str] | None,
+ typer.Option("--phone", help="Replaces the phone list (repeatable)."),
+ ] = None,
+ url: Annotated[
+ list[str] | None, typer.Option("--url", help="Replaces the URL list.")
+ ] = None,
+ tag: Annotated[
+ list[str] | None, typer.Option("--tag", help="Replaces the tag list.")
+ ] = None,
+ organization: str | None = typer.Option(None, "--organization"),
+ role: str | None = typer.Option(None, "--role"),
+ birthday: str | None = typer.Option(None, "--birthday", help="ISO date."),
+ note: str | None = typer.Option(None, "--note"),
+ favorite: bool | None = typer.Option(
+ None, "--favorite/--no-favorite", help="Pin/unpin as favorite."
+ ),
+ json_body: str | None = typer.Option(
+ None, "--json-body", help="Partial contact JSON ('-' reads stdin)."
),
yes: bool = options.yes_opt(),
dry_run: bool = options.dry_opt(),
) -> None:
- """Edit a contact (partial)."""
+ """Edit a contact — only the given flags change (partial PATCH)."""
+ if json_body is not None:
+ body = _body_from(json_body)
+ else:
+ body = _body_from_flags(
+ name=name,
+ alias=alias,
+ relationship=relationship,
+ email=email,
+ phone=phone,
+ url=url,
+ tag=tag,
+ organization=organization,
+ role=role,
+ birthday=birthday,
+ note=note,
+ favorite=favorite,
+ )
+ if not body:
+ render.error("Nothing to change — pass at least one field flag.")
+ raise typer.Exit(code=2)
invoke.run(
"PATCH",
f"/api/contacts/{slug}",
- body=_body_from(json_body),
+ body=body,
assume_yes=yes,
dry_run=dry_run,
)
@@ -77,3 +226,50 @@ def delete(
) -> None:
"""Delete a contact."""
invoke.run("DELETE", f"/api/contacts/{slug}", assume_yes=yes, dry_run=dry_run)
+
+
+def import_vcf(
+ file: str = typer.Argument(..., help="Path to a .vcf file."),
+ yes: bool = options.yes_opt(),
+ dry_run: bool = options.dry_opt(),
+) -> None:
+ """Import contacts from a vCard file (merges, never clobbers)."""
+ path = Path(file)
+ if not path.is_file():
+ render.error(f"Not a readable file: {file}")
+ raise typer.Exit(code=2)
+ text = path.read_text(encoding="utf-8", errors="replace")
+ invoke.run(
+ "POST",
+ "/api/contacts/import",
+ body={"vcf": text},
+ assume_yes=yes,
+ dry_run=dry_run,
+ )
+
+
+def export(
+ out: str | None = typer.Option(
+ None, "--out", help="Write the .vcf to this file instead of stdout."
+ ),
+) -> None:
+ """Export all contacts as vCard 3.0 (.vcf)."""
+ if out is None:
+ invoke.run("GET", "/api/contacts/export")
+ return
+ # --out: fetch without invoke.run so the whole book is not ALSO printed.
+ from jarvis.cli_ctl.__main__ import as_json, make_client
+
+ client = make_client()
+ try:
+ try:
+ text = client.request("GET", "/api/contacts/export")
+ except ApiError as exc:
+ render.error(exc.message)
+ raise typer.Exit(code=1) from exc
+ Path(out).write_text(str(text), encoding="utf-8")
+ render.emit({"ok": True, "wrote": out}, as_json=as_json())
+ finally:
+ client.close()
diff --git a/tests/unit/cli_ctl/test_commands_contacts.py b/tests/unit/cli_ctl/test_commands_contacts.py
new file mode 100644
index 0000000000..40f94ff147
--- /dev/null
+++ b/tests/unit/cli_ctl/test_commands_contacts.py
@@ -0,0 +1,102 @@
+"""Tests for the contacts commands (CRUD flags + vCard import/export)."""
+from __future__ import annotations
+
+from pathlib import Path
+
+from typer.testing import CliRunner
+
+from jarvis.cli_ctl.__main__ import app
+
+runner = CliRunner()
+
+
+def test_list(capture_api):
+ runner.invoke(app, ["contacts", "list"])
+ assert capture_api["calls"][-1]["path"] == "/api/contacts"
+
+
+def test_show(capture_api):
+ runner.invoke(app, ["contacts", "show", "anna"])
+ assert capture_api["calls"][-1]["path"] == "/api/contacts/anna"
+
+
+def test_add_builds_body_from_flags(capture_api):
+ res = runner.invoke(
+ app,
+ [
+ "contacts", "add", "--yes",
+ "--name", "Anna Schmidt",
+ "--email", "[email protected]",
+ "--email", "[email protected]",
+ "--phone", "+49 151 1",
+ "--tag", "uni",
+ "--organization", "ACME",
+ "--birthday", "1990-04-12",
+ "--favorite",
+ ],
+ )
+ assert res.exit_code == 0, res.output
+ call = capture_api["calls"][-1]
+ assert call["method"] == "POST" and call["path"] == "/api/contacts"
+ assert call["body"] == {
+ "name": "Anna Schmidt",
+ "phones": ["+49 151 1"],
+ "tags": ["uni"],
+ "organization": "ACME",
+ "birthday": "1990-04-12",
+ "favorite": True,
+ }
+
+
+def test_add_requires_name_or_json_body(capture_api):
+ res = runner.invoke(app, ["contacts", "add", "--yes", "--email", "[email protected]"])
+ assert res.exit_code == 2
+ assert capture_api["calls"] == []
+
+
+def test_edit_sends_only_given_flags(capture_api):
+ res = runner.invoke(
+ app,
+ ["contacts", "edit", "anna", "--yes", "--no-favorite", "--role", "CTO"],
+ )
+ assert res.exit_code == 0, res.output
+ call = capture_api["calls"][-1]
+ assert call["method"] == "PATCH" and call["path"] == "/api/contacts/anna"
+ assert call["body"] == {"favorite": False, "role": "CTO"}
+
+
+def test_edit_with_no_flags_is_an_error(capture_api):
+ res = runner.invoke(app, ["contacts", "edit", "anna", "--yes"])
+ assert res.exit_code == 2
+ assert capture_api["calls"] == []
+
+
+def test_import_reads_file_and_posts(capture_api, tmp_path: Path):
+ vcf = tmp_path / "book.vcf"
+ vcf.write_text("BEGIN:VCARD\nFN:Anna\nEND:VCARD\n", encoding="utf-8")
+ res = runner.invoke(app, ["contacts", "import", str(vcf), "--yes"])
+ assert res.exit_code == 0, res.output
+ call = capture_api["calls"][-1]
+ assert call["method"] == "POST" and call["path"] == "/api/contacts/import"
+ assert "BEGIN:VCARD" in call["body"]["vcf"]
+
+
+def test_import_missing_file_is_an_error(capture_api, tmp_path: Path):
+ res = runner.invoke(app, ["contacts", "import", str(tmp_path / "nope.vcf")])
+ assert res.exit_code == 2
+ assert capture_api["calls"] == []
+
+
+def test_export_prints_by_default(capture_api):
+ res = runner.invoke(app, ["contacts", "export"])
+ assert res.exit_code == 0, res.output
+ assert capture_api["calls"][-1]["path"] == "/api/contacts/export"
+
+
+def test_export_out_writes_file(capture_api, tmp_path: Path):
+ target = tmp_path / "book.vcf"
+ res = runner.invoke(app, ["contacts", "export", "--out", str(target)])
+ assert res.exit_code == 0, res.output
+ assert capture_api["calls"][-1]["path"] == "/api/contacts/export"
+ assert target.is_file()