kamiwaza-cli 2.0.0__py3-none-any.whl

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 (247) hide show
  1. kamiwaza_cli/__init__.py +27 -0
  2. kamiwaza_cli/__main__.py +8 -0
  3. kamiwaza_cli/auth/__init__.py +39 -0
  4. kamiwaza_cli/auth/client_factory.py +127 -0
  5. kamiwaza_cli/auth/oidc.py +1074 -0
  6. kamiwaza_cli/auth/refresh.py +76 -0
  7. kamiwaza_cli/auth/token_cache.py +206 -0
  8. kamiwaza_cli/checks/__init__.py +130 -0
  9. kamiwaza_cli/checks/base.py +216 -0
  10. kamiwaza_cli/checks/batch_execution.py +99 -0
  11. kamiwaza_cli/checks/control_plane.py +53 -0
  12. kamiwaza_cli/checks/data_store.py +99 -0
  13. kamiwaza_cli/checks/identity.py +170 -0
  14. kamiwaza_cli/checks/ingress.py +102 -0
  15. kamiwaza_cli/checks/model_serving.py +326 -0
  16. kamiwaza_cli/cli.py +344 -0
  17. kamiwaza_cli/commands/__init__.py +7 -0
  18. kamiwaza_cli/commands/_common.py +112 -0
  19. kamiwaza_cli/commands/certs.py +1066 -0
  20. kamiwaza_cli/commands/context.py +732 -0
  21. kamiwaza_cli/commands/diagnose.py +289 -0
  22. kamiwaza_cli/commands/dns.py +261 -0
  23. kamiwaza_cli/commands/login.py +394 -0
  24. kamiwaza_cli/commands/logs.py +328 -0
  25. kamiwaza_cli/commands/models.py +425 -0
  26. kamiwaza_cli/commands/provider.py +253 -0
  27. kamiwaza_cli/commands/resources.py +573 -0
  28. kamiwaza_cli/commands/restart.py +328 -0
  29. kamiwaza_cli/commands/schema.py +96 -0
  30. kamiwaza_cli/commands/status.py +200 -0
  31. kamiwaza_cli/commands/support_bundle.py +910 -0
  32. kamiwaza_cli/commands/traffic.py +249 -0
  33. kamiwaza_cli/commands/ui.py +99 -0
  34. kamiwaza_cli/commands/user.py +900 -0
  35. kamiwaza_cli/commands/version.py +108 -0
  36. kamiwaza_cli/components/__init__.py +13 -0
  37. kamiwaza_cli/components/resolver.py +55 -0
  38. kamiwaza_cli/components/vocabulary.py +93 -0
  39. kamiwaza_cli/context/__init__.py +17 -0
  40. kamiwaza_cli/context/registry.py +267 -0
  41. kamiwaza_cli/exit_codes.py +81 -0
  42. kamiwaza_cli/http/__init__.py +9 -0
  43. kamiwaza_cli/http/tls.py +188 -0
  44. kamiwaza_cli/ops/__init__.py +16 -0
  45. kamiwaza_cli/ops/certs.py +1951 -0
  46. kamiwaza_cli/ops/dns.py +890 -0
  47. kamiwaza_cli/ops/documents.py +110 -0
  48. kamiwaza_cli/ops/models.py +171 -0
  49. kamiwaza_cli/ops/providers.py +424 -0
  50. kamiwaza_cli/ops/resources.py +151 -0
  51. kamiwaza_cli/ops/status.py +213 -0
  52. kamiwaza_cli/ops/traffic.py +954 -0
  53. kamiwaza_cli/output/__init__.py +19 -0
  54. kamiwaza_cli/output/errors.py +83 -0
  55. kamiwaza_cli/output/human.py +95 -0
  56. kamiwaza_cli/output/structured.py +40 -0
  57. kamiwaza_cli/substrate/__init__.py +30 -0
  58. kamiwaza_cli/substrate/_sdk_logging.py +74 -0
  59. kamiwaza_cli/substrate/base.py +136 -0
  60. kamiwaza_cli/substrate/detect.py +301 -0
  61. kamiwaza_cli/substrate/kube.py +79 -0
  62. kamiwaza_cli/substrate/nonorchestrated.py +381 -0
  63. kamiwaza_cli/substrate/orchestrated.py +824 -0
  64. kamiwaza_cli/support_bundle/__init__.py +10 -0
  65. kamiwaza_cli/support_bundle/analyze.py +371 -0
  66. kamiwaza_cli/support_bundle/analyzers/__init__.py +3 -0
  67. kamiwaza_cli/support_bundle/analyzers/_helpers.py +224 -0
  68. kamiwaza_cli/support_bundle/analyzers/base.py +107 -0
  69. kamiwaza_cli/support_bundle/analyzers/cert_expiry.py +170 -0
  70. kamiwaza_cli/support_bundle/analyzers/coredns_health.py +319 -0
  71. kamiwaza_cli/support_bundle/analyzers/diagnose_status.py +211 -0
  72. kamiwaza_cli/support_bundle/analyzers/event_reason_scan.py +248 -0
  73. kamiwaza_cli/support_bundle/analyzers/http_status.py +129 -0
  74. kamiwaza_cli/support_bundle/analyzers/json_path.py +318 -0
  75. kamiwaza_cli/support_bundle/analyzers/log_pattern.py +119 -0
  76. kamiwaza_cli/support_bundle/analyzers/node_pressure.py +163 -0
  77. kamiwaza_cli/support_bundle/analyzers/present.py +99 -0
  78. kamiwaza_cli/support_bundle/analyzers/registry.py +120 -0
  79. kamiwaza_cli/support_bundle/analyzers/restart_count.py +247 -0
  80. kamiwaza_cli/support_bundle/analyzers/secret_leak.py +107 -0
  81. kamiwaza_cli/support_bundle/analyzers/statefulset_quorum.py +233 -0
  82. kamiwaza_cli/support_bundle/analyzers/text_match.py +170 -0
  83. kamiwaza_cli/support_bundle/analyzers/time_skew.py +121 -0
  84. kamiwaza_cli/support_bundle/collectors/__init__.py +3 -0
  85. kamiwaza_cli/support_bundle/collectors/_io.py +201 -0
  86. kamiwaza_cli/support_bundle/collectors/base.py +181 -0
  87. kamiwaza_cli/support_bundle/collectors/cluster_info.py +127 -0
  88. kamiwaza_cli/support_bundle/collectors/cluster_time.py +167 -0
  89. kamiwaza_cli/support_bundle/collectors/config.py +168 -0
  90. kamiwaza_cli/support_bundle/collectors/data.py +157 -0
  91. kamiwaza_cli/support_bundle/collectors/diagnose_snapshot.py +110 -0
  92. kamiwaza_cli/support_bundle/collectors/events.py +162 -0
  93. kamiwaza_cli/support_bundle/collectors/host.py +424 -0
  94. kamiwaza_cli/support_bundle/collectors/host_file.py +587 -0
  95. kamiwaza_cli/support_bundle/collectors/http.py +246 -0
  96. kamiwaza_cli/support_bundle/collectors/k8s/__init__.py +59 -0
  97. kamiwaza_cli/support_bundle/collectors/k8s/_client.py +395 -0
  98. kamiwaza_cli/support_bundle/collectors/k8s/_common.py +317 -0
  99. kamiwaza_cli/support_bundle/collectors/k8s/_kubeconfig.py +448 -0
  100. kamiwaza_cli/support_bundle/collectors/k8s/_kubectl.py +645 -0
  101. kamiwaza_cli/support_bundle/collectors/k8s/_resolver.py +307 -0
  102. kamiwaza_cli/support_bundle/collectors/k8s/cert_resources.py +91 -0
  103. kamiwaza_cli/support_bundle/collectors/k8s/coredns.py +242 -0
  104. kamiwaza_cli/support_bundle/collectors/k8s/dns_probe.py +879 -0
  105. kamiwaza_cli/support_bundle/collectors/k8s/events.py +63 -0
  106. kamiwaza_cli/support_bundle/collectors/k8s/exec.py +313 -0
  107. kamiwaza_cli/support_bundle/collectors/k8s/image_inventory.py +81 -0
  108. kamiwaza_cli/support_bundle/collectors/k8s/nodes.py +52 -0
  109. kamiwaza_cli/support_bundle/collectors/k8s/pod_copy.py +641 -0
  110. kamiwaza_cli/support_bundle/collectors/k8s/pod_inventory.py +81 -0
  111. kamiwaza_cli/support_bundle/collectors/k8s/popeye.py +109 -0
  112. kamiwaza_cli/support_bundle/collectors/k8s/previous_logs.py +110 -0
  113. kamiwaza_cli/support_bundle/collectors/k8s/ray_status.py +85 -0
  114. kamiwaza_cli/support_bundle/collectors/k8s/statefulset_health.py +66 -0
  115. kamiwaza_cli/support_bundle/collectors/logs.py +191 -0
  116. kamiwaza_cli/support_bundle/collectors/meta.py +112 -0
  117. kamiwaza_cli/support_bundle/collectors/previous_bundle_diff.py +282 -0
  118. kamiwaza_cli/support_bundle/collectors/registry.py +186 -0
  119. kamiwaza_cli/support_bundle/collectors/secrets_inventory.py +217 -0
  120. kamiwaza_cli/support_bundle/discovery.py +324 -0
  121. kamiwaza_cli/support_bundle/exceptions.py +94 -0
  122. kamiwaza_cli/support_bundle/manifest.py +318 -0
  123. kamiwaza_cli/support_bundle/output.py +291 -0
  124. kamiwaza_cli/support_bundle/pipeline.py +792 -0
  125. kamiwaza_cli/support_bundle/redaction_engine.py +177 -0
  126. kamiwaza_cli/support_bundle/redactors/__init__.py +3 -0
  127. kamiwaza_cli/support_bundle/redactors/base.py +90 -0
  128. kamiwaza_cli/support_bundle/redactors/builtin.py +181 -0
  129. kamiwaza_cli/support_bundle/redactors/operator.py +41 -0
  130. kamiwaza_cli/support_bundle/redactors/secret_name.py +77 -0
  131. kamiwaza_cli/support_bundle/spec/README.md +15 -0
  132. kamiwaza_cli/support_bundle/spec/__init__.py +3 -0
  133. kamiwaza_cli/support_bundle/spec/bundle-spec.schema.json +898 -0
  134. kamiwaza_cli/support_bundle/spec/effective.py +178 -0
  135. kamiwaza_cli/support_bundle/spec/merge.py +116 -0
  136. kamiwaza_cli/support_bundle/spec/parser.py +779 -0
  137. kamiwaza_cli/support_bundle/spec/resolver.py +202 -0
  138. kamiwaza_cli/support_bundle/spec/types.py +760 -0
  139. kamiwaza_cli/support_bundle/spec/well_known.py +128 -0
  140. kamiwaza_cli/support_bundle/specs/__init__.py +7 -0
  141. kamiwaza_cli/support_bundle/specs/default-categories.yaml +46 -0
  142. kamiwaza_cli/support_bundle/specs/default-with-k8s.yaml +417 -0
  143. kamiwaza_cli/support_bundle/specs/default.yaml +331 -0
  144. kamiwaza_cli/support_bundle/synthesizers/__init__.py +25 -0
  145. kamiwaza_cli/support_bundle/synthesizers/summary_md.py +216 -0
  146. kamiwaza_cli/support_bundle/synthesizers/timeline_json.py +283 -0
  147. kamiwaza_cli/support_bundle/tarball.py +100 -0
  148. kamiwaza_cli/support_bundle/viewer/__init__.py +19 -0
  149. kamiwaza_cli/support_bundle/viewer/analysis_summary.py +197 -0
  150. kamiwaza_cli/support_bundle/viewer/assets/__init__.py +8 -0
  151. kamiwaza_cli/support_bundle/viewer/assets/dashboard.html +3273 -0
  152. kamiwaza_cli/support_bundle/viewer/loader.py +261 -0
  153. kamiwaza_cli/support_bundle/viewer/output.py +65 -0
  154. kamiwaza_cli/support_bundle/viewer/payload.py +58 -0
  155. kamiwaza_cli/support_bundle/viewer/run.py +105 -0
  156. kamiwaza_cli/support_bundle/viewer/template.py +104 -0
  157. kamiwaza_cli/support_bundle/viewer/types.py +103 -0
  158. kamiwaza_cli/tui/__init__.py +14 -0
  159. kamiwaza_cli/tui/access_log.py +66 -0
  160. kamiwaza_cli/tui/actions.py +159 -0
  161. kamiwaza_cli/tui/app.py +3337 -0
  162. kamiwaza_cli/tui/bootstrap.py +41 -0
  163. kamiwaza_cli/tui/bundle.py +171 -0
  164. kamiwaza_cli/tui/capacity.py +191 -0
  165. kamiwaza_cli/tui/catalog.py +2908 -0
  166. kamiwaza_cli/tui/chat.py +72 -0
  167. kamiwaza_cli/tui/clipboard.py +45 -0
  168. kamiwaza_cli/tui/context.py +128 -0
  169. kamiwaza_cli/tui/describe_render.py +244 -0
  170. kamiwaza_cli/tui/editing.py +337 -0
  171. kamiwaza_cli/tui/errors.py +195 -0
  172. kamiwaza_cli/tui/extension_starters.py +179 -0
  173. kamiwaza_cli/tui/external_editor.py +97 -0
  174. kamiwaza_cli/tui/health.py +503 -0
  175. kamiwaza_cli/tui/identity.py +66 -0
  176. kamiwaza_cli/tui/logstream/__init__.py +12 -0
  177. kamiwaza_cli/tui/logstream/aggregator.py +206 -0
  178. kamiwaza_cli/tui/logstream/correlate.py +111 -0
  179. kamiwaza_cli/tui/logstream/histogram.py +259 -0
  180. kamiwaza_cli/tui/logstream/normalize.py +356 -0
  181. kamiwaza_cli/tui/logstream/patterns.py +269 -0
  182. kamiwaza_cli/tui/logstream/timeindex.py +129 -0
  183. kamiwaza_cli/tui/masking.py +111 -0
  184. kamiwaza_cli/tui/notecards.py +646 -0
  185. kamiwaza_cli/tui/pod_shell.py +188 -0
  186. kamiwaza_cli/tui/providers/__init__.py +8 -0
  187. kamiwaza_cli/tui/providers/base.py +175 -0
  188. kamiwaza_cli/tui/providers/jobs.py +129 -0
  189. kamiwaza_cli/tui/providers/k8s.py +251 -0
  190. kamiwaza_cli/tui/providers/kr8s_accessor.py +780 -0
  191. kamiwaza_cli/tui/providers/sdk.py +558 -0
  192. kamiwaza_cli/tui/providers/substrate.py +165 -0
  193. kamiwaza_cli/tui/providers/trust.py +198 -0
  194. kamiwaza_cli/tui/recommendations.py +194 -0
  195. kamiwaza_cli/tui/registry.py +176 -0
  196. kamiwaza_cli/tui/run.py +66 -0
  197. kamiwaza_cli/tui/scheduler.py +139 -0
  198. kamiwaza_cli/tui/screens/__init__.py +26 -0
  199. kamiwaza_cli/tui/screens/access_log.py +129 -0
  200. kamiwaza_cli/tui/screens/add_context.py +390 -0
  201. kamiwaza_cli/tui/screens/analytics.py +364 -0
  202. kamiwaza_cli/tui/screens/chat.py +138 -0
  203. kamiwaza_cli/tui/screens/choose.py +78 -0
  204. kamiwaza_cli/tui/screens/confirm.py +133 -0
  205. kamiwaza_cli/tui/screens/context_switch.py +65 -0
  206. kamiwaza_cli/tui/screens/describe.py +271 -0
  207. kamiwaza_cli/tui/screens/detail.py +359 -0
  208. kamiwaza_cli/tui/screens/diagnostics.py +133 -0
  209. kamiwaza_cli/tui/screens/dns.py +355 -0
  210. kamiwaza_cli/tui/screens/framed.py +70 -0
  211. kamiwaza_cli/tui/screens/list.py +876 -0
  212. kamiwaza_cli/tui/screens/log_explorer.py +960 -0
  213. kamiwaza_cli/tui/screens/logs.py +282 -0
  214. kamiwaza_cli/tui/screens/model_pull.py +405 -0
  215. kamiwaza_cli/tui/screens/offline.py +96 -0
  216. kamiwaza_cli/tui/screens/overview.py +1322 -0
  217. kamiwaza_cli/tui/screens/recommendations.py +168 -0
  218. kamiwaza_cli/tui/screens/resources.py +115 -0
  219. kamiwaza_cli/tui/screens/subsystem.py +375 -0
  220. kamiwaza_cli/tui/screens/swagger.py +536 -0
  221. kamiwaza_cli/tui/screens/theme_picker.py +109 -0
  222. kamiwaza_cli/tui/screens/traffic.py +302 -0
  223. kamiwaza_cli/tui/screens/workroom.py +107 -0
  224. kamiwaza_cli/tui/serving.py +343 -0
  225. kamiwaza_cli/tui/state.py +46 -0
  226. kamiwaza_cli/tui/status.py +62 -0
  227. kamiwaza_cli/tui/swagger.py +284 -0
  228. kamiwaza_cli/tui/widgets/__init__.py +5 -0
  229. kamiwaza_cli/tui/widgets/action_menu.py +46 -0
  230. kamiwaza_cli/tui/widgets/aggregated_log_pane.py +515 -0
  231. kamiwaza_cli/tui/widgets/banner.py +54 -0
  232. kamiwaza_cli/tui/widgets/frame.py +110 -0
  233. kamiwaza_cli/tui/widgets/history_input.py +100 -0
  234. kamiwaza_cli/tui/widgets/log_pane.py +243 -0
  235. kamiwaza_cli/tui/widgets/log_syntax.py +116 -0
  236. kamiwaza_cli/tui/widgets/nav_option_list.py +74 -0
  237. kamiwaza_cli/tui/widgets/resource_table.py +196 -0
  238. kamiwaza_cli/tui/widgets/status_cell.py +44 -0
  239. kamiwaza_cli/tui/widgets/vim_scroll.py +35 -0
  240. kamiwaza_cli/tui/workroom.py +94 -0
  241. kamiwaza_cli-2.0.0.dist-info/METADATA +341 -0
  242. kamiwaza_cli-2.0.0.dist-info/RECORD +247 -0
  243. kamiwaza_cli-2.0.0.dist-info/WHEEL +5 -0
  244. kamiwaza_cli-2.0.0.dist-info/entry_points.txt +2 -0
  245. kamiwaza_cli-2.0.0.dist-info/licenses/LICENSE +202 -0
  246. kamiwaza_cli-2.0.0.dist-info/licenses/NOTICE +4 -0
  247. kamiwaza_cli-2.0.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,27 @@
1
+ """Kamiwaza operator CLI.
2
+
3
+ A standalone, cross-substrate operations CLI for Kamiwaza deployments.
4
+ Implementation reuses the ``kamiwaza-sdk`` Python package for all
5
+ client-side platform calls per Constitution Principle II v1.1.0.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import warnings
11
+ from importlib.metadata import PackageNotFoundError, version
12
+
13
+ # Authlib >=1.7 still imports its deprecated ``authlib.jose`` shim at
14
+ # import time; the warning is informational and irrelevant to operators
15
+ # of the CLI. Filter it once, before any submodule pulls authlib in.
16
+ warnings.filterwarnings(
17
+ "ignore",
18
+ message=r".*authlib\.jose module is deprecated.*",
19
+ category=DeprecationWarning,
20
+ )
21
+
22
+ try:
23
+ __version__ = version("kamiwaza-cli")
24
+ except PackageNotFoundError:
25
+ __version__ = "0.0.0"
26
+
27
+ __all__ = ["__version__"]
@@ -0,0 +1,8 @@
1
+ """Entry point so ``python -m kamiwaza_cli`` is equivalent to ``kamiwaza``."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from .cli import main
6
+
7
+ if __name__ == "__main__":
8
+ raise SystemExit(main())
@@ -0,0 +1,39 @@
1
+ """Authentication subsystem.
2
+
3
+ The CLI's auth boundary on top of ``kamiwaza-sdk``: the OIDC flow
4
+ produces a bearer token; the token cache subclasses
5
+ ``kamiwaza_sdk.token_store.FileTokenStore`` for per-context isolation,
6
+ file locking, and mode-0600 enforcement; the client factory hands the
7
+ bearer to ``KamiwazaClient(api_key=...)``.
8
+
9
+ The SDK's ``OAuthAuthenticator`` stub is intentionally not used at first
10
+ release; if upstream implements OIDC there we rebase onto it then.
11
+ """
12
+
13
+ from .client_factory import build_client, build_client_for_active_context
14
+ from .refresh import refresh_if_needed
15
+ from .token_cache import ExtendedStoredToken, PerContextTokenCache
16
+
17
+ __all__ = [
18
+ "ExtendedStoredToken",
19
+ "OIDCClient",
20
+ "OIDCFlowError",
21
+ "PerContextTokenCache",
22
+ "build_client",
23
+ "build_client_for_active_context",
24
+ "refresh_if_needed",
25
+ ]
26
+
27
+
28
+ def __getattr__(name: str):
29
+ """Lazy re-export of OIDC symbols.
30
+
31
+ ``OIDCClient``/``OIDCFlowError`` pull in :mod:`authlib`; importing
32
+ them eagerly inflates cold-start for every command (research.md R1).
33
+ """
34
+
35
+ if name in {"OIDCClient", "OIDCFlowError"}:
36
+ from . import oidc
37
+
38
+ return getattr(oidc, name)
39
+ raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
@@ -0,0 +1,127 @@
1
+ """Construct configured ``KamiwazaClient`` instances.
2
+
3
+ The single boundary between the CLI's auth layer and every SDK call.
4
+ Per Constitution Principle II v1.1.0 every command obtains its
5
+ ``KamiwazaClient`` through this factory; nothing else in the CLI is
6
+ allowed to call ``KamiwazaClient(...)`` directly.
7
+
8
+ Defensive logging note: the bearer token is forwarded into the SDK via
9
+ the ``api_key=`` kwarg and is never logged from this module. The token
10
+ also never appears in the ``KamiwazaClient.__repr__`` (the SDK does not
11
+ override it; the default ``object.__repr__`` shows only the address).
12
+ """
13
+
14
+ from __future__ import annotations
15
+
16
+ import logging
17
+ from urllib.parse import urlsplit
18
+
19
+ from kamiwaza_sdk import KamiwazaClient
20
+
21
+ from ..context.registry import Context, ContextRegistry
22
+ from ..exit_codes import CONFIG_ERROR, PERMISSION_DENIED
23
+ from ..http.tls import requests_verify_setting, using_context
24
+ from ..output.errors import CliError
25
+ from .refresh import refresh_if_needed
26
+ from .token_cache import ExtendedStoredToken, PerContextTokenCache
27
+
28
+
29
+ def _api_base(endpoint: str) -> str:
30
+ """Derive the SDK data-plane base URL from a context endpoint.
31
+
32
+ Operators register the deployment *root* (e.g. ``https://host``) —
33
+ the same URL that serves the dashboard and the bundled identity
34
+ provider, and the URL that OIDC discovery is built from. Every
35
+ ``KamiwazaClient`` data-plane call, though, targets the API mount,
36
+ which Kamiwaza serves under ``/api``. Pointing the SDK at the bare
37
+ root makes it receive the dashboard's HTML and raise
38
+ ``NonAPIResponseError``.
39
+
40
+ We append ``/api`` here, at the single point where the SDK client is
41
+ built, so the stored endpoint stays the deployment root (which OIDC
42
+ discovery needs) while the data plane still reaches the right path.
43
+
44
+ The append is conservative and idempotent: it only fires when the
45
+ endpoint carries no path component at all (the common bare-host
46
+ form). An endpoint that already names a path — ``/api`` or anything
47
+ else the operator pinned deliberately — is returned unchanged (minus
48
+ any trailing slash), so we never fight an explicit configuration.
49
+ """
50
+ base = endpoint.rstrip("/")
51
+ parts = urlsplit(base)
52
+ if parts.scheme and parts.netloc and parts.path == "":
53
+ return base + "/api"
54
+ return base
55
+
56
+
57
+ def build_client(
58
+ context: Context,
59
+ cache: PerContextTokenCache,
60
+ *,
61
+ log_level: int = logging.WARNING,
62
+ ) -> KamiwazaClient:
63
+ """Build a ``KamiwazaClient`` against ``context``.
64
+
65
+ Refreshes the bearer transparently if needed; raises
66
+ :class:`CliError` (exit 77) when no usable token is on disk.
67
+ """
68
+ with using_context(context, root=cache.path.parents[2]):
69
+ token = refresh_if_needed(context, cache)
70
+ if token is None:
71
+ raise CliError(
72
+ summary=f"No valid credentials for context '{context.name}'",
73
+ next_step=f"run `kamiwaza login --context {context.name}` to authenticate",
74
+ exit_code=PERMISSION_DENIED,
75
+ )
76
+ if not isinstance(token, ExtendedStoredToken):
77
+ token = ExtendedStoredToken(
78
+ access_token=token.access_token,
79
+ refresh_token=token.refresh_token,
80
+ expires_at=token.expires_at,
81
+ )
82
+ client = KamiwazaClient(
83
+ base_url=_api_base(context.endpoint),
84
+ api_key=token.access_token,
85
+ log_level=log_level,
86
+ )
87
+ _apply_tls_posture(client, context)
88
+ return client
89
+
90
+
91
+ def _apply_tls_posture(client: KamiwazaClient, context: Context) -> None:
92
+ """Apply the selected context's TLS verification policy to the SDK session."""
93
+ verify = requests_verify_setting(context)
94
+ session = getattr(client, "session", None)
95
+ if session is None:
96
+ return
97
+ session.verify = verify
98
+
99
+
100
+ def build_client_for_active_context(
101
+ *,
102
+ registry: ContextRegistry | None = None,
103
+ override_context: str | None = None,
104
+ log_level: int = logging.WARNING,
105
+ ) -> tuple[KamiwazaClient, Context]:
106
+ """Resolve the active context (or ``override_context``) and build a client.
107
+
108
+ Returns ``(client, context)`` so the caller can record telemetry or
109
+ update ``last_used_at`` on the registry.
110
+ """
111
+ reg = registry or ContextRegistry()
112
+ if override_context:
113
+ ctx = reg.get(override_context)
114
+ else:
115
+ ctx = reg.active()
116
+ if ctx is None:
117
+ raise CliError(
118
+ summary="No active context",
119
+ next_step=(
120
+ "run `kamiwaza context add <name> --endpoint <url>` and "
121
+ "`kamiwaza context use <name>` to set an active context"
122
+ ),
123
+ exit_code=CONFIG_ERROR,
124
+ )
125
+ cache = PerContextTokenCache(ctx.name, root=reg.root)
126
+ client = build_client(ctx, cache, log_level=log_level)
127
+ return client, ctx