forked from nanocoai/nanoclaw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.yml
More file actions
186 lines (162 loc) · 6.41 KB
/
Copy pathaction.yml
File metadata and controls
186 lines (162 loc) · 6.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
name: Repo Tokens
description: Count codebase tokens with tiktoken and update a README badge
inputs:
include:
description: 'Glob patterns for files to count (space-separated)'
required: true
exclude:
description: 'Glob patterns to exclude (space-separated)'
required: false
default: ''
context-window:
description: 'Context window size for percentage calculation'
required: false
default: '200000'
readme:
description: 'Path to README file'
required: false
default: 'README.md'
encoding:
description: 'Tiktoken encoding name'
required: false
default: 'cl100k_base'
marker:
description: 'HTML comment marker name'
required: false
default: 'token-count'
badge-path:
description: 'Path to write SVG badge (empty = no SVG)'
required: false
default: ''
outputs:
tokens:
description: 'Total token count'
value: ${{ steps.count.outputs.tokens }}
percentage:
description: 'Percentage of context window'
value: ${{ steps.count.outputs.percentage }}
badge:
description: 'Badge text that was inserted'
value: ${{ steps.count.outputs.badge }}
runs:
using: composite
steps:
- name: Install tiktoken
shell: bash
run: pip install tiktoken
- name: Count tokens and update README
id: count
shell: python
env:
INPUT_INCLUDE: ${{ inputs.include }}
INPUT_EXCLUDE: ${{ inputs.exclude }}
INPUT_CONTEXT_WINDOW: ${{ inputs.context-window }}
INPUT_README: ${{ inputs.readme }}
INPUT_ENCODING: ${{ inputs.encoding }}
INPUT_MARKER: ${{ inputs.marker }}
INPUT_BADGE_PATH: ${{ inputs.badge-path }}
run: |
import glob, os, re, tiktoken
include_patterns = os.environ["INPUT_INCLUDE"].split()
exclude_patterns = os.environ["INPUT_EXCLUDE"].split()
context_window = int(os.environ["INPUT_CONTEXT_WINDOW"])
readme_path = os.environ["INPUT_README"]
encoding_name = os.environ["INPUT_ENCODING"]
marker = os.environ["INPUT_MARKER"]
badge_path = os.environ.get("INPUT_BADGE_PATH", "").strip()
# Expand globs
included = set()
for pattern in include_patterns:
included.update(glob.glob(pattern, recursive=True))
excluded = set()
for pattern in exclude_patterns:
excluded.update(glob.glob(pattern, recursive=True))
files = sorted(included - excluded)
files = [f for f in files if os.path.isfile(f)]
# Count tokens
enc = tiktoken.get_encoding(encoding_name)
total = 0
for path in files:
try:
with open(path, "r", encoding="utf-8", errors="ignore") as f:
total += len(enc.encode(f.read()))
except Exception as e:
print(f"Skipping {path}: {e}")
# Format
if total >= 100000:
display = f"{round(total / 1000)}k"
elif total >= 1000:
display = f"{total / 1000:.1f}k"
else:
display = str(total)
pct = round(total / context_window * 100)
badge = f"{display} tokens \u00b7 {pct}% of context window"
print(f"Files: {len(files)}, Tokens: {total}, Badge: {badge}")
# Update README (text between markers)
marker_re = re.compile(
rf"(<!--\s*{re.escape(marker)}\s*-->).*?(<!--\s*/{re.escape(marker)}\s*-->)",
re.DOTALL,
)
with open(readme_path, "r", encoding="utf-8") as f:
content = f.read()
repo_tokens_url = "https://github.com/qwibitai/nanoclaw/tree/main/repo-tokens"
linked_badge = f'<a href="{repo_tokens_url}">{badge}</a>'
new_content = marker_re.sub(rf"\1{linked_badge}\2", content)
if new_content != content:
with open(readme_path, "w", encoding="utf-8") as f:
f.write(new_content)
print("README updated")
else:
print("No change to README")
# Generate SVG badge
if badge_path:
label_text = "tokens"
value_text = display
full_desc = f"{display} tokens, {pct}% of context window"
cw = 7.0
label_w = round(len(label_text) * cw) + 10
value_w = round(len(value_text) * cw) + 10
total_w = label_w + value_w
if pct < 30:
color = "#4c1"
elif pct < 50:
color = "#97ca00"
elif pct < 70:
color = "#dfb317"
else:
color = "#e05d44"
lx = label_w // 2
vx = label_w + value_w // 2
repo_tokens_url = "https://github.com/qwibitai/nanoclaw/tree/main/repo-tokens"
svg = f'''<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="{total_w}" height="20" role="img" aria-label="{full_desc}">
<title>{full_desc}</title>
<linearGradient id="s" x2="0" y2="100%">
<stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
<stop offset="1" stop-opacity=".1"/>
</linearGradient>
<clipPath id="r">
<rect width="{total_w}" height="20" rx="3" fill="#fff"/>
</clipPath>
<a xlink:href="{repo_tokens_url}">
<g clip-path="url(#r)">
<rect width="{label_w}" height="20" fill="#555"/>
<rect x="{label_w}" width="{value_w}" height="20" fill="{color}"/>
<rect width="{total_w}" height="20" fill="url(#s)"/>
<g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" font-size="11">
<text aria-hidden="true" x="{lx}" y="15" fill="#010101" fill-opacity=".3">{label_text}</text>
<text x="{lx}" y="14">{label_text}</text>
<text aria-hidden="true" x="{vx}" y="15" fill="#010101" fill-opacity=".3">{value_text}</text>
<text x="{vx}" y="14">{value_text}</text>
</g>
</g>
</a>
</svg>'''
os.makedirs(os.path.dirname(badge_path) or ".", exist_ok=True)
with open(badge_path, "w", encoding="utf-8") as f:
f.write(svg)
print(f"Badge SVG written to {badge_path}")
# Set outputs
with open(os.environ["GITHUB_OUTPUT"], "a") as f:
f.write(f"tokens={total}\n")
f.write(f"percentage={pct}\n")
f.write(f"badge={badge}\n")