feat(llm): halkyon_llm, language models' APIs streamed over plain HTTP (ADR 0021, step 2)
A pure Dart package, no SDKs, one interface (LlmClient) over Anthropic's
Messages API, OpenAI's chat completions, in each known host's dialect and
the local servers' (D45), and OpenAI's Responses API, stateless (D46). A
model's turn keeps what its provider wants back with the tools' results:
Anthropic's thinking blocks, the Responses API's reasoning items and
message phase, Gemini's thought signatures. Failures come by kind, the
provider's words cut and without keys.
Tested on replies written from the providers' documentation, replayed in
pieces of 1, 7 and 4,096 bytes; tool/record_llm.dart records real ones
with a key of one's own, which the tests then run on too (D36).
FakeLlmClient for the code that uses it. BSD-3-Clause, as halkyon_oidc.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
2026-10-25 17:38:53 +01:00
|
|
|
import 'package:halkyon_llm/halkyon_llm.dart';
|
|
|
|
|
import 'package:halkyon_llm/testing.dart';
|
|
|
|
|
import 'package:test/test.dart';
|
|
|
|
|
|
|
|
|
|
final _endpoint = LlmEndpoint(
|
|
|
|
|
protocol: LlmProtocol.openai,
|
|
|
|
|
model: 'm',
|
|
|
|
|
apiKey: 'k',
|
|
|
|
|
);
|
|
|
|
|
final _request = LlmRequest(
|
|
|
|
|
messages: [
|
|
|
|
|
LlmMessage.user([const LlmText('?')]),
|
|
|
|
|
],
|
|
|
|
|
maxOutputTokens: 10,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
void main() {
|
|
|
|
|
group('FakeLlmClient', () {
|
|
|
|
|
test('answers with the text scripted, in pieces, and keeps what it was '
|
|
|
|
|
'sent', () async {
|
|
|
|
|
final client = FakeLlmClient()..replyText('Hallo Welt');
|
|
|
|
|
final events = await client.send(_endpoint, _request).toList();
|
|
|
|
|
expect(
|
|
|
|
|
events.whereType<LlmTextDelta>().map((delta) => delta.text).join(),
|
|
|
|
|
'Hallo Welt',
|
|
|
|
|
);
|
|
|
|
|
expect((events.last as LlmFinished).message.text, 'Hallo Welt');
|
|
|
|
|
expect(client.sent.single, (_endpoint, _request));
|
|
|
|
|
expect(client.allReplied, isTrue);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('fails as scripted, with what was counted', () {
|
|
|
|
|
final client = FakeLlmClient()
|
|
|
|
|
..fail(
|
|
|
|
|
LlmFailureKind.rateLimited,
|
|
|
|
|
usage: const LlmUsage(inputTokens: 5, outputTokens: 0),
|
|
|
|
|
);
|
|
|
|
|
expect(
|
|
|
|
|
client.send(_endpoint, _request).toList(),
|
|
|
|
|
throwsA(
|
|
|
|
|
isA<LlmFailure>()
|
|
|
|
|
.having((f) => f.kind, 'kind', LlmFailureKind.rateLimited)
|
|
|
|
|
.having((f) => f.usage.inputTokens, 'input', 5),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('counts answers cancelled before they ended', () async {
|
|
|
|
|
final client = FakeLlmClient()..replyText('Hallo Welt');
|
|
|
|
|
await client.send(_endpoint, _request).first;
|
|
|
|
|
expect(client.cancelled, 1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
test('refuses a request nothing was scripted for', () {
|
|
|
|
|
expect(
|
|
|
|
|
() => FakeLlmClient().send(_endpoint, _request),
|
|
|
|
|
throwsStateError,
|
|
|
|
|
);
|
|
|
|
|
});
|
feat(llm): a request carries how much the model thinks, in each provider's field
LlmRequest.effort (LlmEffort: low, medium, high, xhigh, max, Anthropic's
names) goes as Anthropic's output_config.effort, as reasoning.effort to
the Responses API and OpenRouter, and as reasoning_effort to OpenAI's chat
completions, Gemini and any other host. endpoint.efforts says which
levels a host takes (Gemini low to high, Mistral none); send refuses
another before anything goes, the fake client too. LlmEffort.outputCeiling
gives the room for output each level wants, as thinking counts towards
the ceiling (Halkyon Learn's ADR 0021, D63).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
2026-10-26 14:53:48 +01:00
|
|
|
|
|
|
|
|
test("refuses an effort the endpoint's host takes not, as HTTP's does", () {
|
|
|
|
|
final client = FakeLlmClient()..replyText('Hallo');
|
|
|
|
|
expect(
|
|
|
|
|
() => client.send(
|
|
|
|
|
LlmEndpoint(
|
|
|
|
|
protocol: LlmProtocol.openai,
|
|
|
|
|
model: 'm',
|
|
|
|
|
apiKey: 'k',
|
|
|
|
|
baseUrl: 'https://api.mistral.ai/v1',
|
|
|
|
|
),
|
|
|
|
|
LlmRequest(
|
|
|
|
|
messages: _request.messages,
|
|
|
|
|
maxOutputTokens: 10,
|
|
|
|
|
effort: LlmEffort.low,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
throwsArgumentError,
|
|
|
|
|
);
|
|
|
|
|
expect(client.sent, isEmpty);
|
|
|
|
|
});
|
feat(llm): halkyon_llm, language models' APIs streamed over plain HTTP (ADR 0021, step 2)
A pure Dart package, no SDKs, one interface (LlmClient) over Anthropic's
Messages API, OpenAI's chat completions, in each known host's dialect and
the local servers' (D45), and OpenAI's Responses API, stateless (D46). A
model's turn keeps what its provider wants back with the tools' results:
Anthropic's thinking blocks, the Responses API's reasoning items and
message phase, Gemini's thought signatures. Failures come by kind, the
provider's words cut and without keys.
Tested on replies written from the providers' documentation, replayed in
pieces of 1, 7 and 4,096 bytes; tool/record_llm.dart records real ones
with a key of one's own, which the tests then run on too (D36).
FakeLlmClient for the code that uses it. BSD-3-Clause, as halkyon_oidc.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
2026-10-25 17:38:53 +01:00
|
|
|
});
|
|
|
|
|
}
|