#!/usr/bin/env python3 """Generates the typing config's limits for the phone and the agent package from one table. python3 scripts/build-typing-config.py # write the generated files python3 scripts/build-typing-config.py --check # exit 1 if any is out of date Reads TypingConfig/limits.json and writes: - HEXShared/FullKeyboard/TypingConfig/TypingConfigLimits.swift: the clamps the keyboard and the app apply when they read a config; - TypingConfig/package/scripts/limits.py: the same numbers for the package's check_config.py; - TypingConfig/typing-config.schema.json: the JSON schema an agent's config is checked against, and the same in the package's reference/; - TypingConfig/package/reference/log-format.md: docs/typing-logs.md, as the package carries it. """ import json import sys from pathlib import Path IOS = Path(__file__).resolve().parents[1] TABLE = IOS / 'TypingConfig/limits.json' SWIFT = IOS / 'HEXShared/FullKeyboard/TypingConfig/TypingConfigLimits.swift' PYTHON = IOS / 'TypingConfig/package/scripts/limits.py' SCHEMA = IOS / 'TypingConfig/typing-config.schema.json' PACKAGE_SCHEMA = IOS / 'TypingConfig/package/reference/typing-config.schema.json' LOG_DOC = IOS / 'docs/typing-logs.md' PACKAGE_LOG_DOC = IOS / 'TypingConfig/package/reference/log-format.md' HEADER = 'Generated by ios/scripts/build-typing-config.py from ios/TypingConfig/limits.json. Do not edit.' def swift(t): w, k = t['words'], t['keys'] a, z = k['aim'], k['zones'] edges = ', '.join('"%s": [%s]' % (key, ', '.join('.%s' % e for e in sides)) for key, sides in z['edges'].items()) lines = [ '// ' + HEADER, 'import CoreGraphics', '', '/// The typing config\'s limits (format %d): what a reader clamps to and what it skips.' % t['version'], 'nonisolated enum TypingConfigLimits {', ' static let format = "%s"' % t['format'], ' static let version = %d' % t['version'], ' static let maxBytes = %d' % t['file']['maxBytes'], ' static let noteMaxCharacters = %d' % t['file']['noteMaxCharacters'], ' static let wordLetters = %d...%d' % (w['minLetters'], w['maxLetters']), ' static let maxKeep = %d' % w['keep']['maxEntries'], ' static let maxNeverCorrectTo = %d' % w['neverCorrectTo']['maxEntries'], ' static let maxFixes = %d' % w['fixes']['maxEntries'], ' static let maxDictation = %d' % w['dictation']['maxEntries'], ' static let dictationMaxWords = %d' % w['dictation']['maxWords'], ' static let maxContexts = %d' % k['maxContexts'], ' /// Contexts match on width rounded to this many points.', ' static let contextWidthRound: CGFloat = %g' % k['context']['widthRound'], ' /// Keycap height and row pitch may differ from the context\'s by this share.', ' static let contextGeometryTolerance: CGFloat = %g' % k['context']['geometryTolerance'], ' /// Each axis of an aim offset, in keycaps.', ' static let aimBound: CGFloat = %g' % a['bound'], ' /// A zone moves an edge at most this many letter keycaps.', ' static let zoneMaxMove: CGFloat = %g' % z['maxMove'], ' /// Every key keeps its keycap\'s centre plus this share of its keycap each way.', ' static let zoneKeepFromCentre: CGFloat = %g' % z['keepFromCentre'], ' /// The edges a zone may move, by key ("letter" is every letter key).', ' static let zoneEdges: [String: [KeyboardZone.Edge]] = [%s]' % edges, ' /// The least evidence a part may claim (a reader skips one that claims less).', ' static let keepMinTimes = %d' % w['keep']['minTimes'], ' static let keepMinSessions = %d' % w['keep']['minSessions'], ' static let neverCorrectToMinTakenBack = %d' % w['neverCorrectTo']['minTakenBack'], ' static let fixMinTimes = %d' % w['fixes']['minTimes'], ' static let fixMinSessions = %d' % w['fixes']['minSessions'], ' static let dictationMinTimes = %d' % w['dictation']['minTimes'], ' static let aimMinTunedTaps = %d' % a['minTunedTaps'], ' static let aimMinCheckedTaps = %d' % a['minCheckedTaps'], ' static let zoneMinTunedTaps = %d' % z['minTunedTaps'], ' static let zoneMinCheckedTaps = %d' % z['minCheckedTaps'], ' /// A zone\'s words made right less words made wrong on the check half (expected counts).', ' static let zoneMinNetCheckedWords: Double = %g' % z['minNetCheckedWords'], ' static let zoneMaxWrongShare: Double = %g' % z['maxWrongShare'], '}', '', ] return '\n'.join(lines) def python(t): return '# %s\nLIMITS = %s\n' % (HEADER, json.dumps(t, indent=1, sort_keys=True)) def schema(t): w, k = t['words'], t['keys'] word = {'type': 'string', 'minLength': w['minLetters'], 'maxLength': w['maxLetters']} count = {'type': 'integer', 'minimum': 0} def entries(props, required, n, evidence): return {'type': 'array', 'maxItems': n, 'items': {'type': 'object', 'additionalProperties': False, 'required': required + ['evidence'], 'properties': dict(props, evidence={'type': 'object', 'required': evidence, 'properties': {e: count for e in evidence}})}} pair = {'type': 'array', 'items': {'type': 'number', 'minimum': -k['aim']['bound'], 'maximum': k['aim']['bound']}, 'minItems': 2, 'maxItems': 2} tally = {'type': 'object', 'additionalProperties': True, 'required': ['gained', 'lost'], 'properties': {'taps': count, 'gained': count, 'lost': count}} words = {'type': 'number', 'minimum': 0} zone_tally = {'type': 'object', 'additionalProperties': True, 'required': ['taps', 'gained', 'lost', 'wordsRight', 'wordsWrong'], 'properties': {'taps': count, 'gained': count, 'lost': count, 'wordsRight': words, 'wordsWrong': words}} edge_names = sorted({e for sides in k['zones']['edges'].values() for e in sides}) return { '$schema': 'https://json-schema.org/draft/2020-12/schema', '$id': 'https://hex.anomaly.co/typing-config/%d.schema.json' % t['version'], 'title': 'Hex typing config, format %d' % t['version'], 'description': HEADER + ' Strict: what an agent must write. Hex itself reads leniently (reference/typing-config.md, "Reading").', 'type': 'object', 'additionalProperties': False, 'required': ['format', 'version', 'id', 'basedOn', 'madeFrom', 'madeFor', 'note'], 'properties': { 'format': {'const': t['format']}, 'version': {'const': t['version']}, 'id': {'type': 'string', 'pattern': '^cfg-[0-9a-f]{16}$'}, 'basedOn': {'type': ['string', 'null']}, 'madeFrom': {'type': 'object', 'required': ['exportId', 'hexVersion', 'hexBuild', 'logDays', 'heldOut'], 'properties': { 'exportId': {'type': 'string'}, 'hexVersion': {'type': 'string'}, 'hexBuild': {'type': 'string'}, 'logDays': {'type': 'array', 'items': {'type': 'string'}}, 'heldOut': {'type': 'object', 'required': ['tunedOn', 'checkedOn'], 'properties': {'tunedOn': {'enum': ['A', 'B']}, 'checkedOn': {'enum': ['A', 'B']}}}}}, 'madeFor': {'type': 'object', 'required': ['device', 'language'], 'properties': {'device': {'type': 'string'}, 'language': {'type': 'string'}}}, 'note': {'type': 'string', 'maxLength': t['file']['noteMaxCharacters']}, 'words': {'type': 'object', 'additionalProperties': False, 'properties': { 'keep': entries({'word': word}, ['word'], w['keep']['maxEntries'], ['times', 'sessions']), 'neverCorrectTo': entries({'word': word}, ['word'], w['neverCorrectTo']['maxEntries'], ['takenBack']), 'fixes': entries({'typo': word, 'word': word, 'mode': {'enum': ['apply', 'suggest']}}, ['typo', 'word', 'mode'], w['fixes']['maxEntries'], ['times', 'sessions']), 'dictation': entries({'heard': {'type': 'string', 'minLength': 1, 'maxLength': 96}, 'meant': {'type': 'string', 'minLength': 1, 'maxLength': 96}}, ['heard', 'meant'], w['dictation']['maxEntries'], ['times']), }}, 'keys': {'type': 'array', 'maxItems': k['maxContexts'], 'items': {'type': 'object', 'additionalProperties': False, 'required': ['context'], 'properties': { 'context': {'type': 'object', 'additionalProperties': False, 'required': ['width', 'orientation', 'posture', 'language', 'letters', 'split', 'hexPlacement', 'numberRow', 'keyHeight', 'rowPitch', 'letterWidth', 'hitModel'], 'properties': {'width': {'type': 'number'}, 'orientation': {'enum': ['portrait', 'landscape']}, 'posture': {'type': 'string'}, 'language': {'type': 'string'}, 'letters': {'type': 'string'}, 'split': {'type': 'boolean'}, 'hexPlacement': {'type': 'string'}, 'numberRow': {'type': 'boolean'}, 'keyHeight': {'type': 'number'}, 'rowPitch': {'type': 'number'}, 'letterWidth': {'type': 'number'}, 'hitModel': {'type': 'integer'}}}, 'aim': {'type': 'object', 'propertyNames': {'pattern': '^.$'}, 'additionalProperties': {'type': 'object', 'additionalProperties': False, 'required': ['offset', 'evidence'], 'properties': { 'offset': pair, 'evidence': {'type': 'object', 'required': ['tuned', 'checked'], 'properties': {'tuned': tally, 'checked': tally}}}}}, 'zones': {'type': 'array', 'items': {'type': 'object', 'additionalProperties': False, 'required': ['key', 'edge', 'move', 'evidence'], 'properties': { 'key': {'type': 'string'}, 'edge': {'enum': edge_names}, 'move': {'type': 'number', 'minimum': -k['zones']['maxMove'], 'maximum': k['zones']['maxMove']}, 'evidence': {'type': 'object', 'required': ['tuned', 'checked'], 'properties': {'tuned': zone_tally, 'checked': zone_tally}}}}}, }}}, }, } def main(): t = json.loads(TABLE.read_text(encoding='utf-8')) schema_text = json.dumps(schema(t), indent=2) + '\n' log_doc = '\n' % HEADER.replace(' from ios/TypingConfig/limits.json', '') + LOG_DOC.read_text(encoding='utf-8').replace( 'Part of [Hex for iPhone](../README.md).\n\n', '') outputs = {SWIFT: swift(t), PYTHON: python(t), SCHEMA: schema_text, PACKAGE_SCHEMA: schema_text, PACKAGE_LOG_DOC: log_doc} stale = [p for p, text in outputs.items() if not p.exists() or p.read_text(encoding='utf-8') != text] if '--check' in sys.argv: for p in stale: print('out of date: %s (run python3 ios/scripts/build-typing-config.py)' % p.relative_to(IOS.parent)) sys.exit(1 if stale else 0) for p, text in outputs.items(): p.parent.mkdir(parents=True, exist_ok=True) p.write_text(text, encoding='utf-8') print('wrote', p.relative_to(IOS.parent)) if __name__ == '__main__': main()