feat(learn): LanguageTool as a sidecar, reached through the server

compose.languagetool.yaml runs LanguageTool's open-source server
(meyay/languagetool 6.8-13, pinned by digest) on `edge` with no site and
no published port, as user 61081 on a read-only filesystem, without a
log. The server alone reaches it (LANGUAGETOOL_URL): grammar.check is
for those signed in, takes 20,000 characters, 60 checks a minute each
(counted in memory), maps a studyset's language to LanguageTool's code
and keeps nothing. instance.info() names the languages it checks; the
privacy page says what goes there. sidecars/README.md has the steps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Tizian 「ティツィアーン」 <raisondetredev@pm.me>
This commit is contained in:
Tizian 「ティツィアーン」 2026-10-25 23:13:46 +01:00
commit f766a880ac
Signed by: raisondetredev
GPG key ID: 07F6FCF405488B64
2 changed files with 2235 additions and 5 deletions

View file

@ -0,0 +1,140 @@
import 'dart:convert';
import 'dart:io';
import 'package:halkyon_learn_server/src/generated/protocol.dart';
import 'package:halkyon_learn_server/src/grammar/grammar_checking.dart';
import 'package:halkyon_learn_server/src/grammar/language_tool.dart';
import 'package:serverpod/serverpod.dart';
import 'package:test/test.dart';
import '../support/auth_fixtures.dart';
import '../test_tools/serverpod_test_tools.dart';
void main() {
withServerpod('Checking spelling and grammar', (sessionBuilder, endpoints) {
late HttpServer languageTool;
final sent = <String>[];
TestSessionBuilder signedIn() => sessionBuilder.copyWith(
authentication: AuthenticationOverride.authenticationInfo(
const Uuid().v7(),
{},
),
);
setUp(() async {
useLearnBase(sessionBuilder);
sent.clear();
// LanguageTool as it answers, for German and English.
languageTool = (await HttpServer.bind(InternetAddress.loopbackIPv4, 0))
..listen((request) async {
final body = await utf8.decodeStream(request);
sent.add(body);
request.response.headers.contentType = ContentType.json;
request.response.write(
jsonEncode(
request.uri.path == '/v2/languages'
? [
{'name': 'German (Germany)', 'longCode': 'de-DE'},
{'name': 'English (US)', 'longCode': 'en-US'},
]
: {
'language': {'code': 'en-US'},
'matches': [
{
'message': 'Possible spelling mistake found.',
'shortMessage': 'Spelling mistake',
'replacements': [
{'value': 'sentence'},
],
'offset': 5,
'length': 8,
'rule': {
'id': 'MORFOLOGIK_RULE_EN_US',
'category': {'id': 'TYPOS'},
},
},
],
},
),
);
await request.response.close();
});
GrammarChecking.use(
sessionBuilder.build().serverpod,
GrammarChecking(
languageTool: LanguageTool(
Uri.parse('http://127.0.0.1:${languageTool.port}/'),
),
),
);
});
tearDown(() => languageTool.close(force: true));
test('checks what a person sends, in the language asked for', () async {
final check = await endpoints.grammar.check(
signedIn(),
text: 'This sentance.',
language: 'en',
);
expect(check.language, 'en-US');
expect(check.matches.single.replacements, ['sentence']);
expect(Uri.splitQueryString(sent.last), {
'text': 'This sentance.',
'language': 'en-US',
});
});
test(
'is for those signed in alone, and sends nothing for others',
() async {
await expectLater(
endpoints.grammar.check(sessionBuilder, text: 'A text.'),
throwsA(isA<ServerpodUnauthenticatedException>()),
);
expect(sent, isEmpty);
},
);
test('refuses a language its LanguageTool lacks', () async {
await expectLater(
endpoints.grammar.check(signedIn(), text: '안녕.', language: 'ko'),
throwsA(
isA<GrammarRefusedException>().having(
(refused) => refused.reason,
'reason',
GrammarRefusal.unknownLanguage,
),
),
);
});
test('names the languages it checks in what the instance offers', () async {
await GrammarChecking.of(sessionBuilder.build().serverpod).look();
final info = await endpoints.instance.info(sessionBuilder);
expect(info.grammarLanguages, ['de', 'en']);
});
test('an instance without LanguageTool names none and checks '
'nothing', () async {
GrammarChecking.use(sessionBuilder.build().serverpod, GrammarChecking());
final info = await endpoints.instance.info(sessionBuilder);
expect(info.grammarLanguages, isNull);
await expectLater(
endpoints.grammar.check(signedIn(), text: 'A text.'),
throwsA(
isA<GrammarRefusedException>().having(
(refused) => refused.reason,
'reason',
GrammarRefusal.unavailable,
),
),
);
});
});
}

View file

@ -48,6 +48,8 @@ import 'package:halkyon_learn_server/src/generated/feedback/feedback_view.dart'
as _iulci38e;
import 'package:halkyon_learn_server/src/generated/future_calls.dart'
as _iip9210p;
import 'package:halkyon_learn_server/src/generated/grammar/grammar_check.dart'
as _ijxfg6y9;
import 'package:halkyon_learn_server/src/generated/instance/instance_info.dart'
as _iof1vv19;
import 'package:halkyon_learn_server/src/generated/publish/publication.dart'
@ -207,6 +209,8 @@ class TestEndpoints {
late final _InboxEndpoint inbox;
late final _GrammarEndpoint grammar;
late final _InstanceEndpoint instance;
late final _PublishEndpoint publish;
@ -255,6 +259,10 @@ class _InternalTestEndpoints extends TestEndpoints
endpoints,
serializationManager,
);
grammar = _GrammarEndpoint(
endpoints,
serializationManager,
);
instance = _InstanceEndpoint(
endpoints,
serializationManager,
@ -1807,6 +1815,52 @@ class _InboxEndpoint {
}
}
class _GrammarEndpoint {
_GrammarEndpoint(
this._endpointDispatch,
this._serializationManager,
);
final _is.EndpointDispatch _endpointDispatch;
final _is.SerializationManager _serializationManager;
_ida.Future<_ijxfg6y9.GrammarCheck> check(
_ist.TestSessionBuilder sessionBuilder, {
required String text,
String? language,
}) async {
return _ist.callAwaitableFunctionAndHandleExceptions(() async {
var _localUniqueSession =
(sessionBuilder as _ist.InternalTestSessionBuilder).internalBuild(
endpoint: 'grammar',
method: 'check',
);
try {
var _localCallContext = await _endpointDispatch.getMethodCallContext(
createSessionCallback: (_) => _localUniqueSession,
endpointPath: 'grammar',
methodName: 'check',
parameters: _ist.testObjectToJson({
'text': text,
'language': language,
}),
serializationManager: _serializationManager,
);
var _localReturnValue =
await (_localCallContext.method.call(
_localUniqueSession,
_localCallContext.arguments,
)
as _ida.Future<_ijxfg6y9.GrammarCheck>);
return _localReturnValue;
} finally {
await _localUniqueSession.close();
}
});
}
}
class _InstanceEndpoint {
_InstanceEndpoint(
this._endpointDispatch,