-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy paththinkchain_cli.py
More file actions
521 lines (432 loc) · 21 KB
/
thinkchain_cli.py
File metadata and controls
521 lines (432 loc) · 21 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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
#!/usr/bin/env python3
# /// script
# dependencies = [
# "anthropic>=0.25.0",
# "sseclient-py",
# "pydantic",
# "python-dotenv",
# "requests",
# "beautifulsoup4",
# "mcp",
# "httpx",
# ]
# ///
"""
ThinkChain CLI Interface
-----------------------------------------
Command-line interface for ThinkChain featuring:
▸ interleaved + extended thinking
▸ fine‑grained tool streaming
▸ early interception of tool_use blocks
▸ multiple tool calls per assistant turn
▸ minimal dependencies (no rich/prompt_toolkit)
"""
import os, json, itertools
from collections import defaultdict
import anthropic # ≥0.25.0
from sseclient import SSEClient # pip install sseclient‑py
from dotenv import load_dotenv
# Import our tool discovery system
from tool_discovery import (
get_tool_discovery, get_claude_tools, execute_tool_sync,
refresh_tools, list_tools, initialize_tool_discovery
)
# Load environment variables from .env file
load_dotenv()
# ────────────────────────────── 0. Config ────────────────────────────── #
# Default configuration - can be changed via /config command
CONFIG = {
"model": "claude-sonnet-4-20250514", # or "claude-opus-4-20250514"
"thinking_budget": 1024, # 1024-16000
"max_tokens": 1024
}
AVAILABLE_MODELS = [
"claude-sonnet-4-20250514",
"claude-opus-4-20250514"
]
BETA_HEADERS = ",".join([
"interleaved-thinking-2025-05-14",
"fine-grained-tool-streaming-2025-05-14",
])
client = anthropic.Anthropic(api_key=os.getenv("ANTHROPIC_API_KEY"))
# ────────────────────────────── 1. Tools ─────────────────────────────── #
# All tools are now discovered from the tools directory and MCP servers
# Initialize tool discovery and get all available tools
print("🔍 Discovering tools...")
tool_discovery = get_tool_discovery()
# Initialize MCP integration
async def initialize_all_tools():
total_tools = await initialize_tool_discovery()
return total_tools
# Run initialization
try:
import asyncio
if hasattr(asyncio, 'get_running_loop'):
try:
loop = asyncio.get_running_loop()
# We're in an async context, schedule the initialization
print("⚡ Scheduling MCP initialization...")
total_tools = 0 # Will be updated later
except RuntimeError:
# No running loop, we can use asyncio.run
total_tools = asyncio.run(initialize_all_tools())
print(f"✅ Initialized {total_tools} total tools (local + MCP)")
else:
# Fallback for older Python versions
total_tools = asyncio.run(initialize_all_tools())
print(f"✅ Initialized {total_tools} total tools (local + MCP)")
except Exception as e:
print(f"⚠️ MCP initialization failed: {e}")
print("🔧 Continuing with local tools only")
total_tools = len(get_claude_tools())
# Get all available tools (local + MCP)
TOOLS = get_claude_tools()
print(f"🛠️ Total tools available: {len(TOOLS)}")
# ────────────────────────────── 2. Helpers ───────────────────────────── #
def beta_headers() -> dict[str, str]:
"""Return extra headers for every call."""
return {"anthropic-beta": BETA_HEADERS}
def run_tool(name: str, args: dict) -> str:
"""Dispatch to discovered tool function and stringify the result."""
try:
result = execute_tool_sync(name, args)
return result
except Exception as exc:
return f"<error>Tool {name} execution failed: {exc}</error>"
# ────────────────────────────── 3. Streaming loop ────────────────────── #
def stream_once(transcript: list[dict]) -> dict:
"""
Send one request, handle tools if any, and return the final message.
Shows thinking process including after tool execution.
Returns the final assistant Message object.
"""
try:
# Create a streaming request with thinking enabled
with client.messages.stream(
model=CONFIG["model"],
max_tokens=CONFIG["max_tokens"],
tools=TOOLS,
messages=transcript,
extra_headers=beta_headers(),
thinking={"type": "enabled", "budget_tokens": CONFIG["thinking_budget"]},
) as stream:
thinking_content = []
in_thinking = False
# Handle the stream events
for chunk in stream:
if chunk.type == 'message_start':
continue
elif chunk.type == 'content_block_start':
if hasattr(chunk.content_block, 'type'):
if chunk.content_block.type == 'tool_use':
print(f"\n[tool_use:{chunk.content_block.name}] ▶", flush=True)
elif chunk.content_block.type == 'thinking':
print(f"\n[thinking] ", end='', flush=True)
in_thinking = True
elif chunk.type == 'content_block_delta':
if hasattr(chunk.delta, 'text') and chunk.delta.text:
if in_thinking:
print(chunk.delta.text, end='', flush=True)
thinking_content.append(chunk.delta.text)
else:
print(chunk.delta.text, end='', flush=True)
elif chunk.type == 'content_block_stop':
if in_thinking:
print("\n", flush=True)
in_thinking = False
elif chunk.type == 'message_delta':
continue
elif chunk.type == 'message_stop':
break
# Get the final message
final_message = stream.get_final_message()
# Check if there are tool uses that need to be handled
tool_uses = [block for block in final_message.content if block.type == 'tool_use']
if tool_uses:
# Add the assistant message with tool uses
transcript.append({
"role": "assistant",
"content": [block.model_dump() for block in final_message.content]
})
# Process each tool use
tool_results = []
for tool_use in tool_uses:
print(f"\n[tool_use:{tool_use.name}] args → {tool_use.input}", flush=True)
# Run the tool
result = run_tool(tool_use.name, tool_use.input)
print(f"[tool_result] {result}", flush=True)
tool_results.append({
"type": "tool_result",
"tool_use_id": tool_use.id,
"content": result,
})
# Add tool results to transcript
transcript.append({
"role": "user",
"content": tool_results
})
print("\n[continuing with tool results...]\n", flush=True)
# Continue the conversation with tool results - this should trigger more thinking
return stream_once(transcript)
return final_message
except Exception as e:
print(f"\nError in stream: {e}")
# Fallback to non-streaming with thinking
response = client.messages.create(
model=CONFIG["model"],
max_tokens=CONFIG["max_tokens"],
tools=TOOLS,
messages=transcript,
extra_headers=beta_headers(),
thinking={"type": "enabled", "budget_tokens": CONFIG["thinking_budget"]},
)
return response
# Legacy function removed - no longer needed without mock tools
# ────────────────────────────── 4. Driver ---------------------------------- #
def ask(prompt: str, chat_history: list[dict]):
# Add user message
chat_history.append({"role": "user", "content": prompt})
# For weather/search queries, add an explicit tool reminder
if any(keyword in prompt.lower() for keyword in ['weather', 'temperature', 'restaurant', 'eat', 'food', 'search', 'find']):
tool_reminder = "REMINDER: Use 'weathertool' for weather queries and 'duckduckgotool' for search/restaurant queries."
chat_history.append({"role": "user", "content": tool_reminder})
final_msg = stream_once(chat_history)
# Handle both dict and object responses
if hasattr(final_msg, 'content'):
content = final_msg.content
else:
content = final_msg.get('content', [])
# Extract text content and add to history
text_parts = []
for block in content:
if hasattr(block, 'type') and hasattr(block, 'text'):
if block.type == 'text':
text_parts.append(block.text)
elif isinstance(block, dict) and block.get('type') == 'text':
text_parts.append(block.get('text', ''))
assistant_response = "".join(text_parts)
print("\n\n[assistant] " + assistant_response)
# Add assistant response to history
chat_history.append({"role": "assistant", "content": assistant_response})
return chat_history
def show_config():
"""Display current configuration."""
print("\n⚙️ Current Configuration:")
print(f" Model: {CONFIG['model']}")
print(f" Thinking Budget: {CONFIG['thinking_budget']} tokens")
print(f" Max Tokens: {CONFIG['max_tokens']}")
print(f" Available Models: {', '.join(AVAILABLE_MODELS)}")
print("\n💡 Use /config model <model_name> to change model")
print("💡 Use /config thinking <1024-16000> to change thinking budget")
def handle_config_command(args):
"""Handle /config command with subcommands."""
if not args:
show_config()
return
if args[0] == "model":
if len(args) < 2:
print("❌ Usage: /config model <model_name>")
print(f"Available models: {', '.join(AVAILABLE_MODELS)}")
return
new_model = args[1]
if new_model not in AVAILABLE_MODELS:
print(f"❌ Unknown model: {new_model}")
print(f"Available models: {', '.join(AVAILABLE_MODELS)}")
return
CONFIG["model"] = new_model
print(f"✅ Model changed to: {new_model}")
elif args[0] == "thinking":
if len(args) < 2:
print("❌ Usage: /config thinking <1024-16000>")
return
try:
new_budget = int(args[1])
if not (1024 <= new_budget <= 16000):
print("❌ Thinking budget must be between 1024 and 16000")
return
CONFIG["thinking_budget"] = new_budget
print(f"✅ Thinking budget changed to: {new_budget} tokens")
except ValueError:
print("❌ Thinking budget must be a number")
else:
print(f"❌ Unknown config option: {args[0]}")
print("Available options: model, thinking")
def create_tool_awareness_message() -> str:
"""Create a message that informs Claude about available tools."""
available_tools = get_claude_tools()
if not available_tools:
return "You currently have no tools available."
# Focus on the key tools for user queries
key_tools = []
other_tools = []
for tool in available_tools:
name = tool['name']
desc = tool['description'].strip().replace('\n', ' ')[:80]
if 'weather' in name.lower():
key_tools.append(f"- **{name}**: For weather information and forecasts")
elif 'duck' in name.lower() or 'search' in name.lower():
key_tools.append(f"- **{name}**: For internet searches, restaurants, businesses")
else:
other_tools.append(f"- **{name}**: {desc}")
tool_sections = []
if key_tools:
tool_sections.append("**🌟 KEY TOOLS FOR USER QUERIES:**\n" + "\n".join(key_tools))
if other_tools:
tool_sections.append("**🔧 OTHER AVAILABLE TOOLS:**\n" + "\n".join(other_tools))
message = f"""IMPORTANT: You have {len(available_tools)} tools available.
{chr(10).join(tool_sections)}
CRITICAL: Use these EXACT tool names (e.g., 'weathertool', 'duckduckgotool').
When users ask about weather or search for information, USE THE TOOLS ABOVE."""
return message
def interactive_chat():
"""Interactive chat loop with persistent history."""
global TOOLS # Declare at function start
print("🤖 ThinkChain - Claude Chat Interface with Tool Discovery")
print("Type '/exit', '/quit', or press Ctrl+C to end the conversation.")
print("Special commands:")
print(" - /refresh or /reload: Refresh tool discovery")
print(" - /tools: List all available tools")
print(" - /config: Show/change configuration (model, thinking budget)")
print(" - /help: Show this help message\n")
show_config() # Show initial config
# Initialize chat history with tool awareness
tool_awareness_msg = create_tool_awareness_message()
print(f"\n🔧 Initializing with tool awareness for {len(TOOLS)} tools...")
chat_history = [
{
"role": "user",
"content": tool_awareness_msg
},
{
"role": "assistant",
"content": "I understand. I now have access to the tools you've listed and will use their exact names when calling them. I'm ready to help you with any tasks that can be accomplished using these tools!"
}
]
try:
while True:
user_input = input("\n[you] ").strip()
# Handle slash commands
if user_input.startswith('/'):
command_parts = user_input[1:].split()
command = command_parts[0].lower()
args = command_parts[1:] if len(command_parts) > 1 else []
if command in ['exit', 'quit']:
print("\n👋 Goodbye!")
break
elif command in ['refresh', 'reload']:
print("🔄 Refreshing tool discovery...")
try:
tool_count = asyncio.run(refresh_tools())
TOOLS = get_claude_tools()
# Update Claude's tool awareness
tool_awareness_msg = create_tool_awareness_message()
chat_history.append({
"role": "user",
"content": f"🔄 **Tool Discovery Refresh**: {tool_awareness_msg}"
})
chat_history.append({
"role": "assistant",
"content": "Tools refreshed! I'm now aware of the updated tool list and will use these exact tool names."
})
print(f"✅ Refreshed! Found {tool_count} total tools (local + MCP)")
print("🧠 Claude has been updated with the new tool list")
except Exception as e:
print(f"⚠️ Refresh failed: {e}")
print("🔧 Continuing with current tools")
continue
elif command == 'tools':
print("📋 Available tools:")
# Separate local and MCP tools
all_tools = list_tools()
local_tools = [t for t in all_tools if not t.startswith('mcp_')]
mcp_tools = [t for t in all_tools if t.startswith('mcp_')]
if local_tools:
print(f"Local tools: {', '.join(local_tools)}")
else:
print("No local tools found")
if mcp_tools:
print(f"MCP tools: {', '.join(mcp_tools)}")
else:
print("No MCP tools found")
print(f"Total: {len(TOOLS)} tools")
continue
elif command == 'config':
handle_config_command(args)
continue
elif command == 'help':
print("🤖 ThinkChain - Claude Chat Interface with Tool Discovery")
print("Special commands:")
print(" - /refresh or /reload: Refresh tool discovery")
print(" - /tools: List all available tools")
print(" - /config: Show/change configuration")
print(" - /config model <model_name>: Change model")
print(" - /config thinking <1024-16000>: Change thinking budget")
print(" - /help: Show this help message")
print(" - /exit or /quit: End conversation")
continue
else:
print(f"❌ Unknown command: /{command}")
print("Type /help for available commands")
continue
# Handle legacy commands (without slash) for backward compatibility
elif user_input.lower() in ['exit', 'quit']:
print("\n👋 Goodbye! (Tip: use /exit for slash commands)")
break
elif not user_input:
continue
# Handle legacy commands
elif user_input.lower() in ['refresh', 'reload']:
print("🔄 Refreshing tool discovery... (Tip: use /refresh for slash commands)")
try:
tool_count = asyncio.run(refresh_tools())
TOOLS = get_claude_tools()
# Update Claude's tool awareness
tool_awareness_msg = create_tool_awareness_message()
chat_history.append({
"role": "user",
"content": f"🔄 **Tool Discovery Refresh**: {tool_awareness_msg}"
})
chat_history.append({
"role": "assistant",
"content": "Tools refreshed! I'm now aware of the updated tool list and will use these exact tool names."
})
print(f"✅ Refreshed! Found {tool_count} total tools (local + MCP)")
print("🧠 Claude has been updated with the new tool list")
except Exception as e:
print(f"⚠️ Refresh failed: {e}")
print("🔧 Continuing with current tools")
continue
elif user_input.lower() in ['list tools', 'tools']:
print("📋 Available tools... (Tip: use /tools for slash commands)")
# Separate local and MCP tools
all_tools = list_tools()
local_tools = [t for t in all_tools if not t.startswith('mcp_')]
mcp_tools = [t for t in all_tools if t.startswith('mcp_')]
if local_tools:
print(f"Local tools: {', '.join(local_tools)}")
else:
print("No local tools found")
if mcp_tools:
print(f"MCP tools: {', '.join(mcp_tools)}")
else:
print("No MCP tools found")
print(f"Total: {len(TOOLS)} tools")
continue
elif user_input.lower() == 'help':
print("🤖 ThinkChain - Claude Chat Interface with Tool Discovery (Legacy)")
print("Special commands (legacy - use /command for new format):")
print(" - refresh or reload: Refresh tool discovery")
print(" - list tools or tools: List all available tools")
print(" - help: Show this help message")
print(" - exit or quit: End conversation")
print("\n💡 Try the new slash commands: /help, /tools, /config, etc.")
continue
# Process as regular chat message
else:
chat_history = ask(user_input, chat_history)
except KeyboardInterrupt:
print("\n\n👋 Goodbye!")
except Exception as e:
print(f"\n❌ Error: {e}")
if __name__ == "__main__":
interactive_chat()