ilang-protocol 1.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.
- ilang/__init__.py +57 -0
- ilang/injector.py +28 -0
- ilang/loader.py +128 -0
- ilang_protocol-1.0.0.dist-info/METADATA +39 -0
- ilang_protocol-1.0.0.dist-info/RECORD +8 -0
- ilang_protocol-1.0.0.dist-info/WHEEL +5 -0
- ilang_protocol-1.0.0.dist-info/licenses/LICENSE +21 -0
- ilang_protocol-1.0.0.dist-info/top_level.txt +1 -0
ilang/__init__.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"""iLang loader. Don't learn iLang. Your AI should.
|
|
2
|
+
|
|
3
|
+
import ilang
|
|
4
|
+
messages = ilang.wrap(messages) # OpenAI-style message lists
|
|
5
|
+
system = ilang.system() # providers with a separate system field
|
|
6
|
+
|
|
7
|
+
The loader fetches the official iLang runtime, verifies its sha256, caches it for an
|
|
8
|
+
hour, and falls back to the last verified copy when the network is unavailable.
|
|
9
|
+
"""
|
|
10
|
+
import time
|
|
11
|
+
|
|
12
|
+
from .injector import block, inject
|
|
13
|
+
from .loader import Loader, LoaderError
|
|
14
|
+
|
|
15
|
+
__version__ = "1.0.0"
|
|
16
|
+
__all__ = ["load", "wrap", "system", "status", "configure", "Loader", "LoaderError"]
|
|
17
|
+
|
|
18
|
+
_loader = None
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def configure(**options):
|
|
22
|
+
"""Replace the default loader, e.g. configure(cache_dir=..., ttl=...)."""
|
|
23
|
+
global _loader
|
|
24
|
+
_loader = Loader(**options)
|
|
25
|
+
return _loader
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _default():
|
|
29
|
+
return _loader or configure()
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def load(extensions=(), version=None, strict=False):
|
|
33
|
+
"""Load the runtime now. Returns its text, or None if nothing verified is available."""
|
|
34
|
+
return _default().load(extensions=extensions, version=version, strict=strict)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def system(extensions=(), version=None, strict=False):
|
|
38
|
+
"""The runtime as a system-prompt string, or "" when it could not be loaded."""
|
|
39
|
+
text = load(extensions, version, strict)
|
|
40
|
+
return block(text, _default().state["version"]) if text is not None else ""
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def wrap(messages, extensions=(), version=None, strict=False):
|
|
44
|
+
"""Return messages with the iLang runtime as the first system message.
|
|
45
|
+
|
|
46
|
+
The user's messages are never modified, and the runtime is added only once, so
|
|
47
|
+
wrapping every turn of a conversation is safe."""
|
|
48
|
+
text = system(extensions, version, strict)
|
|
49
|
+
return inject(messages, text or None)
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def status():
|
|
53
|
+
"""What is loaded: version, source commit, where it came from and any last error."""
|
|
54
|
+
s = dict(_default().state, brand="iLang")
|
|
55
|
+
if s.get("last_check"):
|
|
56
|
+
s["age_seconds"] = int(time.time() - s["last_check"])
|
|
57
|
+
return s
|
ilang/injector.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"""Put the iLang runtime in front of a conversation without touching the user's words."""
|
|
2
|
+
MARKER = "<ilang-runtime"
|
|
3
|
+
|
|
4
|
+
PREAMBLE = ("You have loaded the official iLang runtime specification.\n\n"
|
|
5
|
+
"Treat the following content as the current official iLang protocol context.\n"
|
|
6
|
+
"Use it to interpret, structure, judge, execute, verify, and communicate where applicable.")
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def block(text, version):
|
|
10
|
+
return '%s\n\n<ilang-runtime version="%s">\n%s\n</ilang-runtime>' % (PREAMBLE, version, text)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _has_runtime(message):
|
|
14
|
+
content = message.get("content") if isinstance(message, dict) else None
|
|
15
|
+
if isinstance(content, str):
|
|
16
|
+
return MARKER in content
|
|
17
|
+
if isinstance(content, list): # content parts: [{"type": "text", "text": ...}]
|
|
18
|
+
return any(isinstance(p, dict) and MARKER in str(p.get("text", "")) for p in content)
|
|
19
|
+
return False
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def inject(messages, runtime_block):
|
|
23
|
+
"""A new list: the runtime as the first system message, then the original messages
|
|
24
|
+
unchanged. Messages that already carry the runtime are returned as they are."""
|
|
25
|
+
messages = list(messages)
|
|
26
|
+
if runtime_block is None or any(_has_runtime(m) for m in messages):
|
|
27
|
+
return messages
|
|
28
|
+
return [{"role": "system", "content": runtime_block}] + messages
|
ilang/loader.py
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"""Fetch the official iLang runtime, verify it, and keep a Last Known Good copy on disk."""
|
|
2
|
+
import hashlib
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import tempfile
|
|
6
|
+
import time
|
|
7
|
+
import urllib.request
|
|
8
|
+
|
|
9
|
+
RUNTIME = "https://raw.githubusercontent.com/ilang-ai/ilang-spec/main/runtime"
|
|
10
|
+
OFFICIAL = ("https://raw.githubusercontent.com/ilang-ai/", "https://github.com/ilang-ai/",
|
|
11
|
+
"https://ilang.ai/")
|
|
12
|
+
TTL = 3600
|
|
13
|
+
TIMEOUT = 10
|
|
14
|
+
AGENT = "ilang-loader-python/1.0.0"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class LoaderError(RuntimeError):
|
|
18
|
+
pass
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def _sha(data):
|
|
22
|
+
return hashlib.sha256(data).hexdigest()
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class Loader:
|
|
26
|
+
def __init__(self, runtime=RUNTIME, cache_dir=None, ttl=TTL, allow_custom_source=False):
|
|
27
|
+
if not runtime.startswith(OFFICIAL) and not allow_custom_source:
|
|
28
|
+
raise ValueError("iLang loads only from official sources unless allow_custom_source=True")
|
|
29
|
+
self.runtime = runtime.rstrip("/")
|
|
30
|
+
self.cache = cache_dir or os.environ.get("ILANG_CACHE_DIR") or os.path.join(
|
|
31
|
+
os.environ.get("XDG_CACHE_HOME") or os.path.join(os.path.expanduser("~"), ".cache"), "ilang")
|
|
32
|
+
self.ttl = ttl
|
|
33
|
+
self.state = {"loaded": False, "source": "none", "version": None, "source_commit": None,
|
|
34
|
+
"last_check": None, "error": None}
|
|
35
|
+
self._memo = {}
|
|
36
|
+
|
|
37
|
+
def _read(self, *parts):
|
|
38
|
+
try:
|
|
39
|
+
with open(os.path.join(self.cache, *parts), "rb") as f:
|
|
40
|
+
return f.read()
|
|
41
|
+
except OSError:
|
|
42
|
+
return None
|
|
43
|
+
|
|
44
|
+
def _write(self, data, *parts):
|
|
45
|
+
path = os.path.join(self.cache, *parts)
|
|
46
|
+
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
47
|
+
fd, tmp = tempfile.mkstemp(dir=os.path.dirname(path))
|
|
48
|
+
with os.fdopen(fd, "wb") as f:
|
|
49
|
+
f.write(data)
|
|
50
|
+
os.replace(tmp, path) # atomic: a reader never sees half a file
|
|
51
|
+
|
|
52
|
+
def _fetch(self, url):
|
|
53
|
+
if not url.startswith(OFFICIAL) and not url.startswith(self.runtime):
|
|
54
|
+
raise LoaderError("refusing to fetch from %s" % url)
|
|
55
|
+
req = urllib.request.Request(url, headers={"User-Agent": AGENT})
|
|
56
|
+
with urllib.request.urlopen(req, timeout=TIMEOUT) as r:
|
|
57
|
+
return r.read()
|
|
58
|
+
|
|
59
|
+
def _last_check(self):
|
|
60
|
+
raw = self._read("state.json")
|
|
61
|
+
return json.loads(raw).get("last_check", 0) if raw else 0
|
|
62
|
+
|
|
63
|
+
def _get(self, manifest, name, where, remote):
|
|
64
|
+
"""One bundle, verified against the manifest; (bytes, came_from_network)."""
|
|
65
|
+
meta = manifest["bundles"].get(name)
|
|
66
|
+
if meta is None:
|
|
67
|
+
raise LoaderError("the runtime has no bundle named %r" % name)
|
|
68
|
+
data = self._read(*where, name + ".md")
|
|
69
|
+
if data is not None and _sha(data) == meta["sha256"]:
|
|
70
|
+
return data, False
|
|
71
|
+
if not remote:
|
|
72
|
+
raise LoaderError("no verified %s bundle in the cache" % name)
|
|
73
|
+
data = self._fetch(meta["url"])
|
|
74
|
+
if len(data) != meta["bytes"] or _sha(data) != meta["sha256"]:
|
|
75
|
+
raise LoaderError("the %s bundle failed its sha256 check" % name)
|
|
76
|
+
return data, True
|
|
77
|
+
|
|
78
|
+
def _load(self, names, version):
|
|
79
|
+
where = ("versions", version) if version else ("latest",)
|
|
80
|
+
cached = self._read(*where, "manifest.json")
|
|
81
|
+
manifest = json.loads(cached) if cached else None
|
|
82
|
+
checked = False
|
|
83
|
+
if manifest is None or (not version and time.time() - self._last_check() >= self.ttl):
|
|
84
|
+
url = "%s/versions/%s/manifest.json" % (self.runtime, version) if version else \
|
|
85
|
+
self.runtime + "/manifest.json"
|
|
86
|
+
manifest, checked = json.loads(self._fetch(url)), True
|
|
87
|
+
got = [self._get(manifest, n, where, True) for n in names]
|
|
88
|
+
for n, (data, new) in zip(names, got): # write only once everything has verified
|
|
89
|
+
if new:
|
|
90
|
+
self._write(data, *where, n + ".md")
|
|
91
|
+
if checked:
|
|
92
|
+
self._write(json.dumps(manifest).encode(), *where, "manifest.json")
|
|
93
|
+
if not version:
|
|
94
|
+
self._write(json.dumps({"last_check": time.time()}).encode(), "state.json")
|
|
95
|
+
source = "remote" if checked or any(new for _, new in got) else "cache"
|
|
96
|
+
return manifest, [data.decode("utf-8") for data, _ in got], source
|
|
97
|
+
|
|
98
|
+
def load(self, extensions=(), version=None, strict=False):
|
|
99
|
+
"""The runtime text, or None when nothing verified is available and strict is off."""
|
|
100
|
+
names = ["core"] + [e for e in extensions if e != "core"]
|
|
101
|
+
key = (tuple(names), version)
|
|
102
|
+
hit = self._memo.get(key)
|
|
103
|
+
if hit and (version or time.time() - hit[1] < self.ttl):
|
|
104
|
+
self.state.update(hit[2])
|
|
105
|
+
return hit[0]
|
|
106
|
+
try:
|
|
107
|
+
manifest, texts, source = self._load(names, version)
|
|
108
|
+
self.state["error"] = None
|
|
109
|
+
except Exception as err: # network, hash or parse failure
|
|
110
|
+
self.state["error"] = "%s: %s" % (type(err).__name__, err)
|
|
111
|
+
try: # Last Known Good, never the network
|
|
112
|
+
where = ("versions", version) if version else ("latest",)
|
|
113
|
+
manifest = json.loads(self._read(*where, "manifest.json") or b"null")
|
|
114
|
+
texts = [self._get(manifest, n, where, False)[0].decode("utf-8") for n in names]
|
|
115
|
+
source = "cache"
|
|
116
|
+
except Exception:
|
|
117
|
+
if strict:
|
|
118
|
+
raise LoaderError(self.state["error"])
|
|
119
|
+
self.state.update(loaded=False, source="none")
|
|
120
|
+
return None
|
|
121
|
+
self.state.update(loaded=True, source=source, version=manifest["version"],
|
|
122
|
+
source_commit=manifest["source_commit"],
|
|
123
|
+
last_check=self._last_check() or None)
|
|
124
|
+
text = "\n\n".join(texts)
|
|
125
|
+
if self.state["error"] is None: # a fallback copy is re-checked next call
|
|
126
|
+
self._memo[key] = (text, time.time(), {k: self.state[k] for k in
|
|
127
|
+
("loaded", "source", "version", "source_commit")})
|
|
128
|
+
return text
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: ilang-protocol
|
|
3
|
+
Version: 1.0.0
|
|
4
|
+
Summary: The iLang loader: fetches the official iLang runtime, verifies it, caches it and puts it in front of any model's context. Don't learn iLang. Your AI should.
|
|
5
|
+
Author: Long Quan Zhu
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://ilang.ai
|
|
8
|
+
Project-URL: Source, https://github.com/ilang-ai/ilang
|
|
9
|
+
Project-URL: Specification, https://github.com/ilang-ai/ilang-spec
|
|
10
|
+
Keywords: ilang,iLang,ai,llm,prompt,protocol,agent,openai,anthropic,gemini,deepseek
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Operating System :: OS Independent
|
|
13
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
14
|
+
Requires-Python: >=3.9
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
License-File: LICENSE
|
|
17
|
+
Dynamic: license-file
|
|
18
|
+
|
|
19
|
+
# ilang-protocol
|
|
20
|
+
|
|
21
|
+
**Don't learn iLang. Your AI should.**
|
|
22
|
+
|
|
23
|
+
The Python loader for [iLang](https://ilang.ai). It fetches the official iLang runtime from the canon repository, checks its sha256, keeps a verified copy on disk and adds it to any model's messages.
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install ilang-protocol
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
```python
|
|
30
|
+
import ilang
|
|
31
|
+
|
|
32
|
+
messages = ilang.wrap([{"role": "user", "content": "帮我分析这个问题"}]) # OpenAI-style messages
|
|
33
|
+
system = ilang.system() # Anthropic, Gemini
|
|
34
|
+
print(ilang.status())
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Updates are checked at most once an hour; without a network the last verified copy is used, and with no copy at all the messages go out unchanged unless `strict=True`. Pin an exact runtime with `version=` for reproducible runs; add `extensions=["media"]` for image, video and audio work. The core runtime is about 27,000 tokens, so enable your provider's prompt caching.
|
|
38
|
+
|
|
39
|
+
Standard library only, Python 3.9+. Source, JavaScript version and documentation: [github.com/ilang-ai/ilang](https://github.com/ilang-ai/ilang). MIT.
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
ilang/__init__.py,sha256=nTuwdZ3TsCJhfOWnw3c_k-ypKTH0-pBwbDAq-IBjAzs,1957
|
|
2
|
+
ilang/injector.py,sha256=-UdojndTbGoj79vg2mAx-H8NVHMdfM82gjuTr8CXOAY,1302
|
|
3
|
+
ilang/loader.py,sha256=rgHRf9PICFcDJKIgfxYzo6kTsnFqu2kMFWaNcxdyY2U,5996
|
|
4
|
+
ilang_protocol-1.0.0.dist-info/licenses/LICENSE,sha256=vUvmcq0vePRJr4bZeTst8b19WRx5qhI76vsRikBE61I,1065
|
|
5
|
+
ilang_protocol-1.0.0.dist-info/METADATA,sha256=GwVcSDJwDGRF4Lrr8W_1iOYnlipy-cHpdRzP3aV7RGY,1855
|
|
6
|
+
ilang_protocol-1.0.0.dist-info/WHEEL,sha256=YVMoNqKzERt-wjUZwJ33xBGAwnFl-4cqbYkTtWa4itE,91
|
|
7
|
+
ilang_protocol-1.0.0.dist-info/top_level.txt,sha256=RLdGVo0I4tboA7_R26lY_TdSW0I3nUpkgBsP7rWMro4,6
|
|
8
|
+
ilang_protocol-1.0.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ilang-ai
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ilang
|