shipwright-kit 0.10.0__tar.gz → 0.11.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 (40) hide show
  1. {shipwright_kit-0.10.0/shipwright_kit.egg-info → shipwright_kit-0.11.0}/PKG-INFO +1 -1
  2. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/pyproject.toml +1 -1
  3. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/__init__.py +1 -1
  4. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/llm.py +46 -59
  5. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0/shipwright_kit.egg-info}/PKG-INFO +1 -1
  6. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/tests/test_llm.py +150 -144
  7. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/LICENSE +0 -0
  8. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/README.md +0 -0
  9. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/setup.cfg +0 -0
  10. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/cli.py +0 -0
  11. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/config.py +0 -0
  12. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/design/__init__.py +0 -0
  13. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/design/banner.py +0 -0
  14. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/design/console.py +0 -0
  15. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/design/glyphs.py +0 -0
  16. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/design/output.py +0 -0
  17. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/design/palette.py +0 -0
  18. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/design/tiers.py +0 -0
  19. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/eval/__init__.py +0 -0
  20. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/eval/corpus.py +0 -0
  21. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/eval/harness.py +0 -0
  22. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/eval/metrics.py +0 -0
  23. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/py.typed +0 -0
  24. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/security/__init__.py +0 -0
  25. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/security/eval.py +0 -0
  26. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/security/injection.py +0 -0
  27. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/security/ssrf.py +0 -0
  28. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit/security/theme.py +0 -0
  29. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit.egg-info/SOURCES.txt +0 -0
  30. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit.egg-info/dependency_links.txt +0 -0
  31. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit.egg-info/entry_points.txt +0 -0
  32. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit.egg-info/requires.txt +0 -0
  33. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/shipwright_kit.egg-info/top_level.txt +0 -0
  34. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/tests/test_cli.py +0 -0
  35. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/tests/test_config.py +0 -0
  36. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/tests/test_packaging.py +0 -0
  37. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/tests/test_packs_entrypoint.py +0 -0
  38. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/tests/test_release_config.py +0 -0
  39. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/tests/test_template_wiring.py +0 -0
  40. {shipwright_kit-0.10.0 → shipwright_kit-0.11.0}/tests/test_tooling.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: shipwright-kit
3
- Version: 0.10.0
3
+ Version: 0.11.0
4
4
  Summary: Shipwright — AI-agent dev framework + import-light design/eval/security library
5
5
  Author: Christian Huhn
6
6
  License-Expression: MIT
@@ -7,7 +7,7 @@ build-backend = "setuptools.build_meta"
7
7
  # PyPI distribution name. The bare `shipwright` is taken on PyPI (unrelated 6si
8
8
  # tool), so the dist is `shipwright-kit`; the IMPORT name is `shipwright_kit`.
9
9
  name = "shipwright-kit"
10
- version = "0.10.0"
10
+ version = "0.11.0"
11
11
  description = "Shipwright — AI-agent dev framework + import-light design/eval/security library"
12
12
  readme = "README.md"
13
13
  requires-python = ">=3.11"
@@ -1,3 +1,3 @@
1
1
  """Shipwright — design-token + tooling library."""
2
2
 
3
- __version__ = "0.10.0"
3
+ __version__ = "0.11.0"
@@ -4,8 +4,16 @@ Owns the *mechanism* only — "build request -> call SDK/HTTP -> return raw
4
4
  text" for Anthropic, OpenAI, and a local Ollama server. Mirrors
5
5
  ``shipwright_kit.config``'s shape: shared mechanism, per-tool schema. The
6
6
  per-tool bits (prompt content, response JSON-parse/validate/fence-strip,
7
- template fallback, the ``SummarizerProtocol``/``ExplainerProtocol``, and every
8
- try/except around these calls) stay in each tool.
7
+ template fallback, the ``SummarizerProtocol``/``ExplainerProtocol``, client
8
+ construction/import-guarding, and every try/except around these calls) stay
9
+ in each tool.
10
+
11
+ The SDK-backed providers (``anthropic_complete``, ``openai_complete``) take
12
+ an INJECTED ``client``: the caller constructs its own SDK client (with its
13
+ own install-hint/``ImportError`` handling in the tool's ``__init__``) and
14
+ passes it in. This module never imports ``anthropic``/``openai`` and never
15
+ constructs a client itself — the client is duck-typed, so import-lightness
16
+ here is structural (no SDK import exists to lazily defer), not merely lazy.
9
17
 
10
18
  These are DUMB TRANSPORTS. They do NOT redact, sanitize, or scan ``system``/
11
19
  ``user`` text in any way — whatever the caller passes is sent to the provider
@@ -16,11 +24,9 @@ Exception-transparent by design: none of the three public functions contains
16
24
  a ``try``/``except``. SDK, HTTP, and JSON/KeyError failures propagate to the
17
25
  caller unchanged so each tool can keep its own existing error handling
18
26
  (re-raise as ``RuntimeError``, swallow-and-fall-back-to-template, etc.) without
19
- this module making that policy choice for them. The only exceptions raised
20
- *by* this module itself are ``ValueError`` for an unrecognized ``extract``/
21
- ``system_mode`` literal, and ``ImportError`` (translated to a caller-supplied,
22
- tool-specific install hint) when the ``anthropic``/``openai`` packages are not
23
- installed — both are input-validation guards, not error-swallowing.
27
+ this module making that policy choice for them. The only exception raised
28
+ *by* this module itself is ``ValueError`` for an unrecognized ``extract``/
29
+ ``system_mode`` literal — an input-validation guard, not error-swallowing.
24
30
 
25
31
  No ``max_tokens``/``temperature`` default is baked in here (the config.py
26
32
  lesson: no schema in the mechanism) — callers supply every value.
@@ -38,29 +44,6 @@ from typing import Any
38
44
  __all__ = ["anthropic_complete", "ollama_generate", "openai_complete"]
39
45
 
40
46
 
41
- # ---------------------------------------------------------------------------
42
- # Lazy SDK imports — kept out of the 3 public functions so those stay
43
- # try/except-free; `import shipwright_kit.llm` itself never touches
44
- # anthropic/openai and stays stdlib-only.
45
- # ---------------------------------------------------------------------------
46
-
47
-
48
- def _import_anthropic(install_hint: str) -> Any:
49
- try:
50
- import anthropic # noqa: PLC0415
51
- except ImportError as exc:
52
- raise ImportError(install_hint) from exc
53
- return anthropic
54
-
55
-
56
- def _import_openai(install_hint: str) -> Any:
57
- try:
58
- import openai # noqa: PLC0415
59
- except ImportError as exc:
60
- raise ImportError(install_hint) from exc
61
- return openai
62
-
63
-
64
47
  # ---------------------------------------------------------------------------
65
48
  # Anthropic
66
49
  # ---------------------------------------------------------------------------
@@ -68,19 +51,21 @@ def _import_openai(install_hint: str) -> Any:
68
51
 
69
52
  def anthropic_complete(
70
53
  *,
71
- api_key: str | None,
54
+ client: Any,
72
55
  model: str,
73
56
  max_tokens: int,
74
57
  system: str,
75
58
  user: str,
76
- install_hint: str,
77
59
  temperature: float | None = None,
78
60
  extract: str = "first_text_block",
79
61
  ) -> str:
80
- """Call the Anthropic Messages API and return the extracted text.
62
+ """Call the Anthropic Messages API on a caller-supplied client and return
63
+ the extracted text.
81
64
 
82
- Literal move of ``anthropic.Anthropic(api_key=...).messages.create(...)``
83
- plus response-text extraction. ``extract`` reconciles the two known
65
+ ``client`` is a pre-built Anthropic SDK client (e.g.
66
+ ``anthropic.Anthropic(api_key=...)``) constructed and owned by the
67
+ caller; this function only calls ``client.messages.create(...)`` and
68
+ extracts text from the response. ``extract`` reconciles the two known
84
69
  current extraction behaviors:
85
70
 
86
71
  - ``"first_text_block"`` (sift's current behavior): scan
@@ -92,17 +77,10 @@ def anthropic_complete(
92
77
  behavior is preserved on purpose (named follow-up F2 owns fixing it).
93
78
 
94
79
  Raises:
95
- ImportError: the ``anthropic`` package is not installed; the message
96
- is exactly ``install_hint`` (each tool supplies its own text so
97
- this function never bakes in tool-specific wording).
98
80
  ValueError: ``extract`` is not one of the two known modes.
99
- Exception: any exception raised by ``anthropic.Anthropic(...)`` or
100
- ``.messages.create(...)`` propagates unchanged — no try/except
101
- here.
81
+ Exception: any exception raised by ``client.messages.create(...)``
82
+ propagates unchanged — no try/except here.
102
83
  """
103
- anthropic = _import_anthropic(install_hint)
104
- client = anthropic.Anthropic(api_key=api_key)
105
-
106
84
  kwargs: dict[str, Any] = {
107
85
  "model": model,
108
86
  "max_tokens": max_tokens,
@@ -131,31 +109,28 @@ def anthropic_complete(
131
109
 
132
110
  def openai_complete(
133
111
  *,
134
- api_key: str | None,
112
+ client: Any,
135
113
  model: str,
136
114
  max_tokens: int,
137
115
  system: str,
138
116
  user: str,
139
- install_hint: str,
140
117
  temperature: float | None = None,
141
118
  ) -> str:
142
- """Call the OpenAI Chat Completions API and return the response text.
119
+ """Call the OpenAI Chat Completions API on a caller-supplied client and
120
+ return the response text.
143
121
 
144
- Literal move of ``openai.OpenAI(api_key=...).chat.completions.create(...)``
145
- plus response-text extraction (``response.choices[0].message.content or
146
- ""`` — a ``None`` content, e.g. a tool-call-only response, degrades to
147
- ``""`` rather than raising, matching both sift and barb today).
122
+ ``client`` is a pre-built OpenAI SDK client (e.g.
123
+ ``openai.OpenAI(api_key=...)``) constructed and owned by the caller; this
124
+ function only calls ``client.chat.completions.create(...)`` and extracts
125
+ text from the response (``response.choices[0].message.content or ""`` —
126
+ a ``None`` content, e.g. a tool-call-only response, degrades to ``""``
127
+ rather than raising, matching both sift and barb today).
148
128
 
149
129
  Raises:
150
- ImportError: the ``openai`` package is not installed; the message is
151
- exactly ``install_hint``.
152
- Exception: any exception raised by ``openai.OpenAI(...)`` or
153
- ``.chat.completions.create(...)`` propagates unchanged — no
154
- try/except here.
130
+ Exception: any exception raised by
131
+ ``client.chat.completions.create(...)`` propagates unchanged —
132
+ no try/except here.
155
133
  """
156
- openai = _import_openai(install_hint)
157
- client = openai.OpenAI(api_key=api_key)
158
-
159
134
  kwargs: dict[str, Any] = {
160
135
  "model": model,
161
136
  "max_tokens": max_tokens,
@@ -184,6 +159,7 @@ def ollama_generate(
184
159
  user: str,
185
160
  timeout: float | None,
186
161
  system_mode: str,
162
+ options: dict[str, Any] | None = None,
187
163
  ) -> str:
188
164
  """POST to a local Ollama ``/api/generate`` endpoint and return the raw
189
165
  ``response`` field, unstripped.
@@ -200,6 +176,14 @@ def ollama_generate(
200
176
  top-level payload key, ``prompt`` is ``user`` alone. Payload keys:
201
177
  ``{"model", "system", "prompt", "stream"}``.
202
178
 
179
+ ``options`` is an ADDITIVE, OPTIONAL passthrough for Ollama's
180
+ per-request generation options (vex's current behavior, e.g.
181
+ ``{"num_predict": ..., "temperature": ...}``). When given (not
182
+ ``None``), it is included verbatim as the ``"options"`` payload key.
183
+ When omitted (the default, ``None``), no ``"options"`` key is added and
184
+ the payload is byte-identical to today — sift and barb, which never
185
+ pass this argument, are unaffected.
186
+
203
187
  Note: barb's caller additionally does ``.strip()`` on the returned text
204
188
  today; that is NOT done here (sift's raw extraction never strips) — each
205
189
  tool's own call site is responsible for any such post-processing.
@@ -222,6 +206,9 @@ def ollama_generate(
222
206
  else:
223
207
  raise ValueError(f"unknown system_mode: {system_mode!r}")
224
208
 
209
+ if options is not None:
210
+ payload["options"] = options
211
+
225
212
  data = json.dumps(payload).encode("utf-8")
226
213
  req = urllib.request.Request(
227
214
  generate_url,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: shipwright-kit
3
- Version: 0.10.0
3
+ Version: 0.11.0
4
4
  Summary: Shipwright — AI-agent dev framework + import-light design/eval/security library
5
5
  Author: Christian Huhn
6
6
  License-Expression: MIT
@@ -4,8 +4,10 @@ Pins the exact request shapes both sift's characterization tests
4
4
  (``sift/tests/test_llm_provider_requests.py``) and barb's
5
5
  (``barb/tests/test_explain_llm_providers.py``) expect, so that when each tool
6
6
  is retrofitted onto this module in a later phase, those suites pass
7
- byte-identical. All external clients/HTTP are mocked — no live network, no
8
- real API keys.
7
+ byte-identical. All clients are plain mocks passed in directly (client
8
+ INJECTION, not internal construction) — no live network, no real API keys,
9
+ and no faking of ``sys.modules['anthropic']``/``['openai']`` since this
10
+ module no longer imports either SDK.
9
11
  """
10
12
 
11
13
  from __future__ import annotations
@@ -14,7 +16,6 @@ import json
14
16
  import subprocess
15
17
  import sys
16
18
  import urllib.error
17
- from types import SimpleNamespace
18
19
  from unittest.mock import MagicMock
19
20
 
20
21
  import pytest
@@ -26,31 +27,25 @@ from shipwright_kit.llm import anthropic_complete, ollama_generate, openai_compl
26
27
  # ---------------------------------------------------------------------------
27
28
 
28
29
 
29
- def _install_fake_anthropic(monkeypatch, response_text: str = "ok") -> tuple[MagicMock, MagicMock]:
30
- """Install a fake ``anthropic`` module in sys.modules so the function's
31
- lazy ``import anthropic`` picks it up, and return the mock client class
32
- so tests can assert on ``Anthropic(...)`` / ``.messages.create(...)``."""
30
+ def _make_anthropic_client(response_text: str = "ok") -> MagicMock:
31
+ """Build a mock Anthropic SDK client (what a tool's ``__init__`` would
32
+ construct and inject as ``self._client``)."""
33
33
  mock_client = MagicMock()
34
34
  mock_response = MagicMock()
35
35
  mock_response.content = [MagicMock(text=response_text)]
36
36
  mock_client.messages.create.return_value = mock_response
37
-
38
- mock_anthropic_cls = MagicMock(return_value=mock_client)
39
- fake_module = SimpleNamespace(Anthropic=mock_anthropic_cls)
40
- monkeypatch.setitem(sys.modules, "anthropic", fake_module)
41
- return mock_client, mock_anthropic_cls
37
+ return mock_client
42
38
 
43
39
 
44
40
  class TestAnthropicRequestConstruction:
45
- def test_sends_model_max_tokens_system_and_user_message(self, monkeypatch):
46
- mock_client, _ = _install_fake_anthropic(monkeypatch)
41
+ def test_sends_model_max_tokens_system_and_user_message(self):
42
+ mock_client = _make_anthropic_client()
47
43
  anthropic_complete(
48
- api_key="fake-key",
44
+ client=mock_client,
49
45
  model="claude-sonnet-4-6",
50
46
  max_tokens=2048,
51
47
  system="sys prompt",
52
48
  user="user prompt",
53
- install_hint="pip install x[llm]",
54
49
  )
55
50
  kwargs = mock_client.messages.create.call_args.kwargs
56
51
  assert kwargs["model"] == "claude-sonnet-4-6"
@@ -58,54 +53,29 @@ class TestAnthropicRequestConstruction:
58
53
  assert kwargs["system"] == "sys prompt"
59
54
  assert kwargs["messages"] == [{"role": "user", "content": "user prompt"}]
60
55
 
61
- def test_temperature_omitted_when_none(self, monkeypatch):
62
- mock_client, _ = _install_fake_anthropic(monkeypatch)
63
- anthropic_complete(
64
- api_key="fake-key",
65
- model="m",
66
- max_tokens=10,
67
- system="s",
68
- user="u",
69
- install_hint="hint",
70
- )
56
+ def test_temperature_omitted_when_none(self):
57
+ mock_client = _make_anthropic_client()
58
+ anthropic_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
71
59
  assert "temperature" not in mock_client.messages.create.call_args.kwargs
72
60
 
73
- def test_temperature_included_when_given(self, monkeypatch):
74
- mock_client, _ = _install_fake_anthropic(monkeypatch)
75
- anthropic_complete(
76
- api_key="fake-key",
77
- model="m",
78
- max_tokens=10,
79
- system="s",
80
- user="u",
81
- install_hint="hint",
82
- temperature=0.42,
83
- )
61
+ def test_temperature_included_when_given(self):
62
+ mock_client = _make_anthropic_client()
63
+ anthropic_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u", temperature=0.42)
84
64
  assert mock_client.messages.create.call_args.kwargs["temperature"] == 0.42
85
65
 
86
- def test_no_response_format_or_tools_param_sent(self, monkeypatch):
87
- mock_client, _ = _install_fake_anthropic(monkeypatch)
88
- anthropic_complete(api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h")
66
+ def test_no_response_format_or_tools_param_sent(self):
67
+ mock_client = _make_anthropic_client()
68
+ anthropic_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
89
69
  kwargs = mock_client.messages.create.call_args.kwargs
90
70
  assert "response_format" not in kwargs
91
71
  assert "tools" not in kwargs
92
72
 
93
- def test_client_constructed_with_given_api_key(self, monkeypatch):
94
- _, mock_anthropic_cls = _install_fake_anthropic(monkeypatch)
95
- anthropic_complete(api_key="fake-key", model="m", max_tokens=10, system="s", user="u", install_hint="h")
96
- mock_anthropic_cls.assert_called_once_with(api_key="fake-key")
97
-
98
- def test_import_error_uses_caller_supplied_install_hint(self, monkeypatch):
99
- monkeypatch.setitem(sys.modules, "anthropic", None) # forces ImportError on `import anthropic`
100
- with pytest.raises(ImportError, match="pip install sift-triage\\[llm\\]"):
101
- anthropic_complete(
102
- api_key="k",
103
- model="m",
104
- max_tokens=10,
105
- system="s",
106
- user="u",
107
- install_hint="pip install sift-triage[llm]",
108
- )
73
+ def test_uses_the_injected_client_not_a_new_one(self):
74
+ """No internal client construction: the exact object passed in is the
75
+ one whose .messages.create(...) gets called."""
76
+ mock_client = _make_anthropic_client()
77
+ anthropic_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
78
+ mock_client.messages.create.assert_called_once()
109
79
 
110
80
 
111
81
  # ---------------------------------------------------------------------------
@@ -114,64 +84,58 @@ class TestAnthropicRequestConstruction:
114
84
 
115
85
 
116
86
  class TestAnthropicExtraction:
117
- def test_first_text_block_skips_leading_non_text_block(self, monkeypatch):
118
- mock_client, _ = _install_fake_anthropic(monkeypatch)
87
+ def test_first_text_block_skips_leading_non_text_block(self):
88
+ mock_client = _make_anthropic_client()
119
89
 
120
90
  class NoTextBlock:
121
91
  type = "tool_use"
122
92
 
123
93
  mock_client.messages.create.return_value.content = [NoTextBlock(), MagicMock(text="the answer")]
124
94
  result = anthropic_complete(
125
- api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h", extract="first_text_block"
95
+ client=mock_client, model="m", max_tokens=10, system="s", user="u", extract="first_text_block"
126
96
  )
127
97
  assert result == "the answer"
128
98
 
129
- def test_first_text_block_returns_empty_string_when_no_text_block(self, monkeypatch):
130
- mock_client, _ = _install_fake_anthropic(monkeypatch)
99
+ def test_first_text_block_returns_empty_string_when_no_text_block(self):
100
+ mock_client = _make_anthropic_client()
131
101
 
132
102
  class NoTextBlock:
133
103
  type = "tool_use"
134
104
 
135
105
  mock_client.messages.create.return_value.content = [NoTextBlock()]
136
106
  result = anthropic_complete(
137
- api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h", extract="first_text_block"
107
+ client=mock_client, model="m", max_tokens=10, system="s", user="u", extract="first_text_block"
138
108
  )
139
109
  assert result == ""
140
110
 
141
- def test_index0_returns_content_0_text(self, monkeypatch):
142
- mock_client, _ = _install_fake_anthropic(monkeypatch, response_text="direct")
111
+ def test_index0_returns_content_0_text(self):
112
+ mock_client = _make_anthropic_client(response_text="direct")
143
113
  result = anthropic_complete(
144
- api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h", extract="index0"
114
+ client=mock_client, model="m", max_tokens=10, system="s", user="u", extract="index0"
145
115
  )
146
116
  assert result == "direct"
147
117
 
148
- def test_index0_raises_index_error_on_empty_content_uncaught(self, monkeypatch):
118
+ def test_index0_raises_index_error_on_empty_content_uncaught(self):
149
119
  """Preserves barb's current crash-on-empty; NOT fixed here (follow-up F2)."""
150
- mock_client, _ = _install_fake_anthropic(monkeypatch)
120
+ mock_client = _make_anthropic_client()
151
121
  mock_client.messages.create.return_value.content = []
152
122
  with pytest.raises(IndexError):
153
- anthropic_complete(
154
- api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h", extract="index0"
155
- )
123
+ anthropic_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u", extract="index0")
156
124
 
157
- def test_index0_raises_attribute_error_when_first_block_has_no_text(self, monkeypatch):
158
- mock_client, _ = _install_fake_anthropic(monkeypatch)
125
+ def test_index0_raises_attribute_error_when_first_block_has_no_text(self):
126
+ mock_client = _make_anthropic_client()
159
127
 
160
128
  class NoTextBlock:
161
129
  type = "tool_use"
162
130
 
163
131
  mock_client.messages.create.return_value.content = [NoTextBlock()]
164
132
  with pytest.raises(AttributeError):
165
- anthropic_complete(
166
- api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h", extract="index0"
167
- )
133
+ anthropic_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u", extract="index0")
168
134
 
169
- def test_unknown_extract_mode_raises_value_error(self, monkeypatch):
170
- _install_fake_anthropic(monkeypatch)
135
+ def test_unknown_extract_mode_raises_value_error(self):
136
+ mock_client = _make_anthropic_client()
171
137
  with pytest.raises(ValueError, match="unknown extract mode"):
172
- anthropic_complete(
173
- api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h", extract="bogus"
174
- )
138
+ anthropic_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u", extract="bogus")
175
139
 
176
140
 
177
141
  # ---------------------------------------------------------------------------
@@ -180,19 +144,24 @@ class TestAnthropicExtraction:
180
144
 
181
145
 
182
146
  class TestAnthropicExceptionTransparency:
183
- def test_sdk_error_from_create_propagates_uncaught(self, monkeypatch):
184
- mock_client, _ = _install_fake_anthropic(monkeypatch)
147
+ def test_sdk_error_from_create_propagates_uncaught(self):
148
+ mock_client = _make_anthropic_client()
185
149
  mock_client.messages.create.side_effect = RuntimeError("boom from SDK")
186
150
  with pytest.raises(RuntimeError, match="boom from SDK"):
187
- anthropic_complete(api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h")
188
-
189
- def test_api_key_never_appears_in_propagated_exception(self, monkeypatch):
190
- mock_client, _ = _install_fake_anthropic(monkeypatch)
191
- mock_client.messages.create.side_effect = RuntimeError("rate limited")
192
- secret = "FAKE-anthropic-key-value-do-not-leak" # not a real key shape (avoids secret-scanner FP)
151
+ anthropic_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
152
+
153
+ def test_client_raised_exception_propagates_without_wrapping(self):
154
+ """llm.py no longer takes an api_key (client construction/credentials
155
+ are entirely the caller's concern), so there is nothing for this
156
+ function to leak into an exception message. What's left to pin: a
157
+ client-raised exception propagates completely unmodified — same
158
+ type, same message, no wrapping/added context from this module."""
159
+ mock_client = _make_anthropic_client()
160
+ original = RuntimeError("rate limited")
161
+ mock_client.messages.create.side_effect = original
193
162
  with pytest.raises(RuntimeError) as excinfo:
194
- anthropic_complete(api_key=secret, model="m", max_tokens=10, system="s", user="u", install_hint="h")
195
- assert secret not in str(excinfo.value)
163
+ anthropic_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
164
+ assert excinfo.value is original
196
165
 
197
166
 
198
167
  # ---------------------------------------------------------------------------
@@ -200,28 +169,23 @@ class TestAnthropicExceptionTransparency:
200
169
  # ---------------------------------------------------------------------------
201
170
 
202
171
 
203
- def _install_fake_openai(monkeypatch, content: str | None = "ok") -> tuple[MagicMock, MagicMock]:
172
+ def _make_openai_client(content: str | None = "ok") -> MagicMock:
204
173
  mock_client = MagicMock()
205
174
  mock_response = MagicMock()
206
175
  mock_response.choices = [MagicMock(message=MagicMock(content=content))]
207
176
  mock_client.chat.completions.create.return_value = mock_response
208
-
209
- mock_openai_cls = MagicMock(return_value=mock_client)
210
- fake_module = SimpleNamespace(OpenAI=mock_openai_cls)
211
- monkeypatch.setitem(sys.modules, "openai", fake_module)
212
- return mock_client, mock_openai_cls
177
+ return mock_client
213
178
 
214
179
 
215
180
  class TestOpenAIRequestConstruction:
216
- def test_sends_model_max_tokens_and_two_role_messages(self, monkeypatch):
217
- mock_client, _ = _install_fake_openai(monkeypatch)
181
+ def test_sends_model_max_tokens_and_two_role_messages(self):
182
+ mock_client = _make_openai_client()
218
183
  openai_complete(
219
- api_key="k",
184
+ client=mock_client,
220
185
  model="gpt-4o",
221
186
  max_tokens=1024,
222
187
  system="sys prompt",
223
188
  user="user prompt",
224
- install_hint="h",
225
189
  )
226
190
  kwargs = mock_client.chat.completions.create.call_args.kwargs
227
191
  assert kwargs["model"] == "gpt-4o"
@@ -231,35 +195,28 @@ class TestOpenAIRequestConstruction:
231
195
  {"role": "user", "content": "user prompt"},
232
196
  ]
233
197
 
234
- def test_temperature_omitted_when_none(self, monkeypatch):
235
- mock_client, _ = _install_fake_openai(monkeypatch)
236
- openai_complete(api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h")
198
+ def test_temperature_omitted_when_none(self):
199
+ mock_client = _make_openai_client()
200
+ openai_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
237
201
  assert "temperature" not in mock_client.chat.completions.create.call_args.kwargs
238
202
 
239
- def test_temperature_included_when_given(self, monkeypatch):
240
- mock_client, _ = _install_fake_openai(monkeypatch)
241
- openai_complete(api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h", temperature=0.1)
203
+ def test_temperature_included_when_given(self):
204
+ mock_client = _make_openai_client()
205
+ openai_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u", temperature=0.1)
242
206
  assert mock_client.chat.completions.create.call_args.kwargs["temperature"] == 0.1
243
207
 
244
- def test_no_response_format_or_tools_param_sent(self, monkeypatch):
245
- mock_client, _ = _install_fake_openai(monkeypatch)
246
- openai_complete(api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h")
208
+ def test_no_response_format_or_tools_param_sent(self):
209
+ mock_client = _make_openai_client()
210
+ openai_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
247
211
  kwargs = mock_client.chat.completions.create.call_args.kwargs
248
212
  assert "response_format" not in kwargs
249
213
  assert "tools" not in kwargs
250
214
  assert "functions" not in kwargs
251
215
 
252
- def test_client_constructed_with_given_api_key(self, monkeypatch):
253
- _, mock_openai_cls = _install_fake_openai(monkeypatch)
254
- openai_complete(api_key="fake-key", model="m", max_tokens=10, system="s", user="u", install_hint="h")
255
- mock_openai_cls.assert_called_once_with(api_key="fake-key")
256
-
257
- def test_import_error_uses_caller_supplied_install_hint(self, monkeypatch):
258
- monkeypatch.setitem(sys.modules, "openai", None)
259
- with pytest.raises(ImportError, match="pip install barb-phish\\[llm\\]"):
260
- openai_complete(
261
- api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="pip install barb-phish[llm]"
262
- )
216
+ def test_uses_the_injected_client_not_a_new_one(self):
217
+ mock_client = _make_openai_client()
218
+ openai_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
219
+ mock_client.chat.completions.create.assert_called_once()
263
220
 
264
221
 
265
222
  # ---------------------------------------------------------------------------
@@ -268,14 +225,14 @@ class TestOpenAIRequestConstruction:
268
225
 
269
226
 
270
227
  class TestOpenAIExtraction:
271
- def test_returns_choices_0_message_content(self, monkeypatch):
272
- _install_fake_openai(monkeypatch, content="direct answer")
273
- result = openai_complete(api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h")
228
+ def test_returns_choices_0_message_content(self):
229
+ mock_client = _make_openai_client(content="direct answer")
230
+ result = openai_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
274
231
  assert result == "direct answer"
275
232
 
276
- def test_none_content_degrades_to_empty_string(self, monkeypatch):
277
- _install_fake_openai(monkeypatch, content=None)
278
- result = openai_complete(api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h")
233
+ def test_none_content_degrades_to_empty_string(self):
234
+ mock_client = _make_openai_client(content=None)
235
+ result = openai_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
279
236
  assert result == ""
280
237
 
281
238
 
@@ -285,19 +242,22 @@ class TestOpenAIExtraction:
285
242
 
286
243
 
287
244
  class TestOpenAIExceptionTransparency:
288
- def test_sdk_error_from_create_propagates_uncaught(self, monkeypatch):
289
- mock_client, _ = _install_fake_openai(monkeypatch)
245
+ def test_sdk_error_from_create_propagates_uncaught(self):
246
+ mock_client = _make_openai_client()
290
247
  mock_client.chat.completions.create.side_effect = RuntimeError("boom from SDK")
291
248
  with pytest.raises(RuntimeError, match="boom from SDK"):
292
- openai_complete(api_key="k", model="m", max_tokens=10, system="s", user="u", install_hint="h")
293
-
294
- def test_api_key_never_appears_in_propagated_exception(self, monkeypatch):
295
- mock_client, _ = _install_fake_openai(monkeypatch)
296
- mock_client.chat.completions.create.side_effect = RuntimeError("rate limited")
297
- secret = "FAKE-openai-key-value-do-not-leak" # not a real key shape (avoids secret-scanner FP)
249
+ openai_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
250
+
251
+ def test_client_raised_exception_propagates_without_wrapping(self):
252
+ """Mirrors the Anthropic case: no api_key in this API anymore, so the
253
+ pinned contract is that a client-raised exception passes through
254
+ this module completely unmodified."""
255
+ mock_client = _make_openai_client()
256
+ original = RuntimeError("rate limited")
257
+ mock_client.chat.completions.create.side_effect = original
298
258
  with pytest.raises(RuntimeError) as excinfo:
299
- openai_complete(api_key=secret, model="m", max_tokens=10, system="s", user="u", install_hint="h")
300
- assert secret not in str(excinfo.value)
259
+ openai_complete(client=mock_client, model="m", max_tokens=10, system="s", user="u")
260
+ assert excinfo.value is original
301
261
 
302
262
 
303
263
  # ---------------------------------------------------------------------------
@@ -409,6 +369,50 @@ class TestOllamaRequestConstruction:
409
369
  base_url="http://localhost:11434", model="m", system="s", user="u", timeout=None, system_mode="bogus"
410
370
  )
411
371
 
372
+ def test_options_included_verbatim_when_given(self, monkeypatch):
373
+ """vex's shape: a per-request options dict is passed through as-is."""
374
+ captured = _install_fake_urlopen(monkeypatch, {"response": "ok"})
375
+ opts = {"num_predict": 500, "temperature": 0.3}
376
+ ollama_generate(
377
+ base_url="http://localhost:11434",
378
+ model="m",
379
+ system="s",
380
+ user="u",
381
+ timeout=None,
382
+ system_mode="fold",
383
+ options=opts,
384
+ )
385
+ payload = captured["payload"]
386
+ assert payload["options"] == opts
387
+
388
+ def test_options_omitted_key_when_none(self, monkeypatch):
389
+ """Regression guard: sift/barb never pass options, so with the default
390
+ (None) the payload must have no "options" key at all — byte-identical
391
+ to before this parameter existed."""
392
+ captured = _install_fake_urlopen(monkeypatch, {"response": "ok"})
393
+ ollama_generate(
394
+ base_url="http://localhost:11434", model="m", system="s", user="u", timeout=None, system_mode="fold"
395
+ )
396
+ assert "options" not in captured["payload"]
397
+
398
+ def test_options_combined_with_field_system_mode(self, monkeypatch):
399
+ """vex's actual shape: system_mode="field" + options together produce
400
+ exactly {model, prompt, stream, options, system}."""
401
+ captured = _install_fake_urlopen(monkeypatch, {"response": "ok"})
402
+ opts = {"num_predict": 200, "temperature": 0.7}
403
+ ollama_generate(
404
+ base_url="http://localhost:11434",
405
+ model="m",
406
+ system="SYS",
407
+ user="USER",
408
+ timeout=None,
409
+ system_mode="field",
410
+ options=opts,
411
+ )
412
+ payload = captured["payload"]
413
+ assert set(payload.keys()) == {"model", "system", "prompt", "stream", "options"}
414
+ assert payload["options"] == opts
415
+
412
416
 
413
417
  # ---------------------------------------------------------------------------
414
418
  # Ollama — response extraction
@@ -486,29 +490,27 @@ class TestDumbTransportNoRedaction:
486
490
  assert "DUMB TRANSPORT" in doc
487
491
  assert "do NOT redact" in doc or "does NOT redact" in doc
488
492
 
489
- def test_anthropic_passes_sensitive_text_through_unchanged(self, monkeypatch):
490
- mock_client, _ = _install_fake_anthropic(monkeypatch)
493
+ def test_anthropic_passes_sensitive_text_through_unchanged(self):
494
+ mock_client = _make_anthropic_client()
491
495
  anthropic_complete(
492
- api_key="k",
496
+ client=mock_client,
493
497
  model="m",
494
498
  max_tokens=10,
495
499
  system=self.SENSITIVE_TEXT,
496
500
  user=self.SENSITIVE_TEXT,
497
- install_hint="h",
498
501
  )
499
502
  kwargs = mock_client.messages.create.call_args.kwargs
500
503
  assert kwargs["system"] == self.SENSITIVE_TEXT
501
504
  assert kwargs["messages"][0]["content"] == self.SENSITIVE_TEXT
502
505
 
503
- def test_openai_passes_sensitive_text_through_unchanged(self, monkeypatch):
504
- mock_client, _ = _install_fake_openai(monkeypatch)
506
+ def test_openai_passes_sensitive_text_through_unchanged(self):
507
+ mock_client = _make_openai_client()
505
508
  openai_complete(
506
- api_key="k",
509
+ client=mock_client,
507
510
  model="m",
508
511
  max_tokens=10,
509
512
  system=self.SENSITIVE_TEXT,
510
513
  user=self.SENSITIVE_TEXT,
511
- install_hint="h",
512
514
  )
513
515
  messages = mock_client.chat.completions.create.call_args.kwargs["messages"]
514
516
  assert messages[0]["content"] == self.SENSITIVE_TEXT
@@ -534,6 +536,10 @@ class TestDumbTransportNoRedaction:
534
536
 
535
537
 
536
538
  def test_import_light_no_anthropic_or_openai_loaded():
539
+ """Stronger than a merely-lazy-import claim: llm.py contains no reference
540
+ to anthropic/openai anywhere (no lazy-import helpers either, since the
541
+ SDK providers now take an injected, duck-typed client) — so the SDKs are
542
+ never loaded into sys.modules just by importing this module."""
537
543
  code = (
538
544
  "import importlib, sys; "
539
545
  "importlib.import_module('shipwright_kit.llm'); "
File without changes