forked from Doriandarko/gemini-writer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwriter.py
More file actions
executable file
·388 lines (322 loc) · 13.1 KB
/
Copy pathwriter.py
File metadata and controls
executable file
·388 lines (322 loc) · 13.1 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
"""
GLM Writing Agent - An autonomous agent for creative writing tasks.
This agent uses the GLM-4.7 model to create novels, books,
and short story collections based on user prompts.
"""
import os
import sys
import json
import argparse
from dotenv import load_dotenv
from zai import ZaiClient
from typing import List, Dict, Any
# Load environment variables from .env file
load_dotenv()
from utils import (
estimate_token_count,
get_tool_definitions,
get_tool_map,
get_system_prompt,
)
from tools.compression import compress_context_impl
# Constants
MAX_ITERATIONS = 300
TOKEN_LIMIT = 200000 # GLM-4.7 context window
COMPRESSION_THRESHOLD = 180000 # Trigger compression at 90% of limit
MODEL_NAME = "glm-4.7"
BACKUP_INTERVAL = 50 # Save backup summary every N iterations
def load_context_from_file(file_path: str) -> str:
"""
Loads context from a summary file for recovery.
Args:
file_path: Path to the context summary file
Returns:
Content of the file as string
"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
print(f"✓ Loaded context from: {file_path}\n")
return content
except Exception as e:
print(f"✗ Error loading context file: {e}")
sys.exit(1)
def get_user_input() -> tuple[str, bool]:
"""
Gets user input from command line, either as a prompt or recovery file.
Returns:
Tuple of (prompt/context, is_recovery_mode)
"""
parser = argparse.ArgumentParser(
description="GLM Writing Agent - Create novels, books, and short stories",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Fresh start with inline prompt
python writer.py "Create a collection of sci-fi short stories"
# Recovery mode from previous context
python writer.py --recover my_project/.context_summary_20250107_143022.md
"""
)
parser.add_argument(
'prompt',
nargs='?',
help='Your writing request (e.g., "Create a mystery novel")'
)
parser.add_argument(
'--recover',
type=str,
help='Path to a context summary file to continue from'
)
args = parser.parse_args()
# Check if recovery mode
if args.recover:
context = load_context_from_file(args.recover)
return context, True
# Check if prompt provided as argument
if args.prompt:
return args.prompt, False
# Interactive prompt
print("=" * 60)
print("GLM Writing Agent")
print("=" * 60)
print("\nEnter your writing request (or 'quit' to exit):")
print("Example: Create a collection of 15 sci-fi short stories\n")
prompt = input("> ").strip()
if prompt.lower() in ['quit', 'exit', 'q']:
print("Goodbye!")
sys.exit(0)
if not prompt:
print("Error: Empty prompt. Please provide a writing request.")
sys.exit(1)
return prompt, False
def main():
"""Main agent loop."""
# Get API key
api_key = os.getenv("ZAI_API_KEY")
if not api_key:
print("Error: ZAI_API_KEY environment variable not set.")
print("Please set your API key: export ZAI_API_KEY='your-key-here'")
sys.exit(1)
# Debug: Show that key is loaded (masked for security)
if len(api_key) > 8:
print(f"✓ API Key loaded: {api_key[:4]}...{api_key[-4:]}")
else:
print(f"⚠️ Warning: API key seems too short ({len(api_key)} chars)")
# Initialize GLM client (coding plan endpoint)
client = ZaiClient(
api_key=api_key,
base_url="https://api.z.ai/api/coding/paas/v4"
)
print(f"✓ GLM-4.7 client initialized\n")
# Get user input
user_prompt, is_recovery = get_user_input()
# Initialize messages list with dictionaries (OpenAI format)
messages: List[Dict[str, Any]] = []
# Add initial user message
if is_recovery:
initial_message = f"[RECOVERED CONTEXT]\n\n{user_prompt}\n\n[END RECOVERED CONTEXT]\n\nPlease continue the work from where we left off."
print("🔄 Recovery mode: Continuing from previous context\n")
else:
initial_message = user_prompt
print(f"\n📝 Task: {user_prompt}\n")
messages.append({
"role": "user",
"content": initial_message
})
# Get tool definitions and mapping
tools = get_tool_definitions()
tool_map = get_tool_map()
# Get system prompt
system_instruction = get_system_prompt()
print("=" * 60)
print("Starting GLM Writing Agent")
print("=" * 60)
print(f"Model: {MODEL_NAME}")
print(f"Max iterations: {MAX_ITERATIONS}")
print(f"Context limit: {TOKEN_LIMIT:,} tokens")
print(f"Auto-compression at: {COMPRESSION_THRESHOLD:,} tokens")
print("=" * 60 + "\n")
# Main agent loop
for iteration in range(1, MAX_ITERATIONS + 1):
print(f"\n{'─' * 60}")
print(f"Iteration {iteration}/{MAX_ITERATIONS}")
print(f"{'─' * 60}")
# Check token count before making API call
try:
token_count = estimate_token_count(messages)
print(f"📊 Current tokens: {token_count:,}/{TOKEN_LIMIT:,} ({token_count/TOKEN_LIMIT*100:.1f}%)")
# Trigger compression if approaching limit
if token_count >= COMPRESSION_THRESHOLD:
print(f"\n⚠️ Approaching token limit! Compressing context...")
compression_result = compress_context_impl(
messages=[{"role": "system", "content": system_instruction}] + messages,
client=client,
model=MODEL_NAME,
keep_recent=10
)
if "compressed_messages" in compression_result:
# Rebuild messages from compressed messages (remove system message)
new_messages = []
for msg in compression_result["compressed_messages"]:
if msg.get("role") == "system":
continue
new_messages.append(msg)
messages = new_messages
print(f"✓ {compression_result['message']}")
print(f"✓ Estimated tokens saved: ~{compression_result.get('tokens_saved', 0):,}")
token_count = estimate_token_count(messages)
print(f"📊 New token count: {token_count:,}/{TOKEN_LIMIT:,}\n")
except Exception as e:
print(f"⚠️ Warning: Could not estimate token count: {e}")
token_count = 0
# Auto-backup every N iterations
if iteration % BACKUP_INTERVAL == 0:
print(f"💾 Auto-backup (iteration {iteration})...")
try:
compression_result = compress_context_impl(
messages=[{"role": "system", "content": system_instruction}] + messages,
client=client,
model=MODEL_NAME,
keep_recent=len(messages)
)
if compression_result.get("summary_file"):
print(f"✓ Backup saved: {os.path.basename(compression_result['summary_file'])}\n")
except Exception as e:
print(f"⚠️ Warning: Backup failed: {e}\n")
# Build full messages with system instruction
full_messages = [{"role": "system", "content": system_instruction}] + messages
# Call the model
try:
print("🤖 Calling GLM-4.7 model...\n")
response = client.chat.completions.create(
model=MODEL_NAME,
messages=full_messages,
temperature=1.0,
extra_body={
"tools": tools
}
)
# Process the response
content_text = ""
function_calls_list = []
# Get the first choice's message
choice = response.choices[0]
message = choice.message
# Extract content
if message.content:
content_text = message.content
# Extract tool calls
if message.tool_calls:
for tool_call in message.tool_calls:
function_calls_list.append({
"id": tool_call.id,
"name": tool_call.function.name,
"args": json.loads(tool_call.function.arguments)
})
# Display content
if content_text:
print("💬 Response:")
print("-" * 60)
print(content_text)
print("-" * 60 + "\n")
# Display function calls
if function_calls_list:
print("🔧 Function calls detected:")
print("─" * 60)
for fc in function_calls_list:
print(f" → {fc['name']}")
# Append assistant message to conversation (without tool_calls to avoid serialization issues)
messages.append({
"role": "assistant",
"content": content_text
})
# Check if the model called any functions
if not function_calls_list:
print("=" * 60)
print("✅ TASK COMPLETED")
print("=" * 60)
print(f"Completed in {iteration} iteration(s)")
print("=" * 60)
break
# Handle function calls
print(f"\n🔧 Model decided to call {len(function_calls_list)} tool(s):")
# Process each tool call
for fc in function_calls_list:
func_name = fc["name"]
func_args = fc["args"]
tool_call_id = fc["id"]
print(f"\n → {func_name}")
print(f" Arguments: {json.dumps(func_args, ensure_ascii=False, indent=6)}")
# Get the tool implementation
tool_func = tool_map.get(func_name)
if not tool_func:
result = f"Error: Unknown tool '{func_name}'"
print(f" ✗ {result}")
else:
# Special handling for compress_context (needs extra params)
if func_name == "compress_context":
result_data = compress_context_impl(
messages=[{"role": "system", "content": system_instruction}] + messages,
client=client,
model=MODEL_NAME,
keep_recent=10
)
result = result_data.get("message", "Compression completed")
else:
# Call the tool with its arguments
result = tool_func(**func_args)
# Print result (truncate if too long)
if len(str(result)) > 200:
print(f" ✓ {str(result)[:200]}...")
else:
print(f" ✓ {result}")
# Add tool result as a tool role message
messages.append({
"role": "tool",
"tool_call_id": tool_call_id,
"content": str(result)
})
except KeyboardInterrupt:
print("\n\n⚠️ Interrupted by user. Saving context...")
try:
compression_result = compress_context_impl(
messages=[{"role": "system", "content": system_instruction}] + messages,
client=client,
model=MODEL_NAME,
keep_recent=len(messages)
)
if compression_result.get("summary_file"):
print(f"✓ Context saved to: {compression_result['summary_file']}")
print(f"\nTo resume, run:")
print(f" python writer.py --recover {compression_result['summary_file']}")
except:
pass
sys.exit(0)
except Exception as e:
print(f"\n✗ Error during iteration {iteration}: {e}")
print(f"Attempting to continue...\n")
continue
# If we hit max iterations
if iteration >= MAX_ITERATIONS:
print("\n" + "=" * 60)
print("⚠️ MAX ITERATIONS REACHED")
print("=" * 60)
print(f"\nReached maximum of {MAX_ITERATIONS} iterations.")
print("Saving final context...")
try:
compression_result = compress_context_impl(
messages=[{"role": "system", "content": system_instruction}] + messages,
client=client,
model=MODEL_NAME,
keep_recent=len(messages)
)
if compression_result.get("summary_file"):
print(f"✓ Context saved to: {compression_result['summary_file']}")
print(f"\nTo resume, run:")
print(f" python writer.py --recover {compression_result['summary_file']}")
except Exception as e:
print(f"✗ Error saving context: {e}")
if __name__ == "__main__":
main()