omega-tool-model
First-party Omega tool-use model source, serving harnesses, and verification records
Brought by drakestapleton
omega-tool-model / deployment / conversation / specialize.py
| 1 | """External constant-text specialization for the existing pinned Omega compiler. |
| 2 | The release compiler does not accept unsized views. All runtime logic stays Omega; |
| 3 | each constant text argument is bound into a native helper at build time. |
| 4 | """ |
| 5 | import re |
| 6 | NAMES={'wt':0,'wo':0,'wm':1,'wconsume':1,'wevent':0,'wfinish':1,'wtoken':0,'wfield':1,'wanswer':0} |
| 7 | TOK=re.compile(r'"(?:\\.|[^"\\])*"|\S+') |
| 8 | def specialize(source): |
| 9 | templates={} |
| 10 | for name in NAMES: |
| 11 | match=re.search(r'^fn '+name+r' .*?(?=^fn |\Z)',source,re.M|re.S) |
| 12 | assert match,name |
| 13 | text=match.group();head=TOK.findall(text.splitlines()[0]);pos=head.index('view')-1;param=head[pos] |
| 14 | templates[name]=(text,head,pos,param) |
| 15 | source=source[:match.start()]+source[match.end():] |
| 16 | globals_=set(re.findall(r'^text (\w+) ',source,re.M));texts={};variants={};pending=[] |
| 17 | def transform(text): |
| 18 | output=[] |
| 19 | for line in text.splitlines(): |
| 20 | if line.startswith(('text ','note ','fn ')):output.append(line);continue |
| 21 | words=TOK.findall(line);i=0 |
| 22 | while i<len(words): |
| 23 | name=words[i] |
| 24 | if name in NAMES: |
| 25 | at=i+1+NAMES[name];value=words[at] |
| 26 | assert value.startswith('"') or value in globals_,(name,value,line) |
| 27 | if value.startswith('"'): |
| 28 | if value not in texts: |
| 29 | texts[value]='z'+str(len(texts));globals_.add(texts[value]) |
| 30 | value=texts[value] |
| 31 | key=(name,value) |
| 32 | if key not in variants: |
| 33 | variant=name+'c'+str(len(variants));variants[key]=variant;pending.append(key) |
| 34 | words[i]=variants[key];del words[at] |
| 35 | i+=1 |
| 36 | output.append(' '.join(words)) |
| 37 | return '\n'.join(output)+'\n' |
| 38 | source=transform(source);clones=[] |
| 39 | while pending: |
| 40 | name,value=pending.pop(0);text,head,pos,param=templates[name];head=head.copy();head[1]=variants[name,value];del head[pos:pos+4] |
| 41 | lines=[' '.join(head)] |
| 42 | for line in text.splitlines()[1:]:lines.append(' '.join(value if token==param else token for token in TOK.findall(line))) |
| 43 | clones.append(transform('\n'.join(lines))) |
| 44 | declarations=''.join(f'text {name} {value}\n' for value,name in texts.items()) |
| 45 | # Text constants must be declared before helpers use them. |
| 46 | return declarations+source+'\n'.join(clones) |