File size: 44,733 Bytes
da590a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
// PicoClaw - Ultra-lightweight personal AI agent
// Inspired by and based on nanobot: https://github.com/HKUDS/nanobot
// License: MIT
//
// Copyright (c) 2026 PicoClaw contributors

package agent

import (
	"context"
	"encoding/json"
	"fmt"
	"os"
	"os/exec"
	"path/filepath"
	"regexp"
	"strings"
	"sync"
	"sync/atomic"
	"time"
	"unicode/utf8"

	"github.com/sipeed/picoclaw/pkg/bus"
	"github.com/sipeed/picoclaw/pkg/channels"
	"github.com/sipeed/picoclaw/pkg/config"
	"github.com/sipeed/picoclaw/pkg/constants"
	"github.com/sipeed/picoclaw/pkg/logger"
	"github.com/sipeed/picoclaw/pkg/metrics"
	"github.com/sipeed/picoclaw/pkg/providers"
	"github.com/sipeed/picoclaw/pkg/session"
	"github.com/sipeed/picoclaw/pkg/state"
	"github.com/sipeed/picoclaw/pkg/tools"
	"github.com/sipeed/picoclaw/pkg/utils"
)

type ToolCacheEntry struct {
	Result    *tools.ToolResult
	ExpiresAt time.Time
}

type AgentLoop struct {
	bus            *bus.MessageBus
	provider       providers.LLMProvider
	workspace      string
	model          string
	fallbackModels []string
	contextWindow  int // Maximum context window size in tokens
	maxIterations  int
	sessions       *session.SessionManager
	state          *state.Manager
	contextBuilder *ContextBuilder
	tools          *tools.ToolRegistry
	running        atomic.Bool
	summarizing    sync.Map // Tracks which sessions are currently being summarized
	channelManager *channels.Manager
	config         *config.Config
	cache          map[string]ToolCacheEntry
	cacheMutex     sync.RWMutex
}

// processOptions configures how a message is processed
type processOptions struct {
	SessionKey      string // Session identifier for history/context
	Channel         string // Target channel for tool execution
	ChatID          string // Target chat ID for tool execution
	UserMessage     string // User message content (may include prefix)
	DefaultResponse string // Response when LLM returns empty
	EnableSummary   bool   // Whether to trigger summarization
	SendResponse    bool   // Whether to send response via bus
	NoHistory       bool   // If true, don't load session history (for heartbeat)
	Media           []string // Attached media file paths
	MaxIterations   int    // Maximum iterations for this specific request
}

// createToolRegistry creates a tool registry with common tools.
// This is shared between main agent and subagents.
func createToolRegistry(workspace string, restrict bool, cfg *config.Config, msgBus *bus.MessageBus) *tools.ToolRegistry {
	registry := tools.NewToolRegistry()

	// File system tools
	registry.Register(tools.NewReadFileTool(workspace, restrict))
	registry.Register(tools.NewWriteFileTool(workspace, restrict))
	registry.Register(tools.NewListDirTool(workspace, restrict))
	registry.Register(tools.NewEditFileTool(workspace, restrict))
	registry.Register(tools.NewAppendFileTool(workspace, restrict))

	// Shell execution
	registry.Register(tools.NewExecTool(workspace, restrict, cfg.Tools.ResourceLimits))

	if searchTool := tools.NewWebSearchTool(tools.WebSearchToolOptions{
		BraveAPIKey:          cfg.Tools.Web.Brave.APIKey,
		BraveMaxResults:      cfg.Tools.Web.Brave.MaxResults,
		BraveEnabled:         cfg.Tools.Web.Brave.Enabled,
		DuckDuckGoMaxResults: cfg.Tools.Web.DuckDuckGo.MaxResults,
		DuckDuckGoEnabled:    cfg.Tools.Web.DuckDuckGo.Enabled,
	}); searchTool != nil {
		registry.Register(searchTool)
	}
	registry.Register(tools.NewWebFetchTool(50000))

	// Hardware tools (I2C, SPI) - Linux only, returns error on other platforms
	registry.Register(tools.NewI2CTool())
	registry.Register(tools.NewSPITool())

	// Message tool - available to both agent and subagent
	// Subagent uses it to communicate directly with user
	messageTool := tools.NewMessageTool()
	messageTool.SetSendCallback(func(channel, chatID, content string) error {
		var audioPath string

		// Check for [NO_VOICE] prefix to skip TTS
		if strings.HasPrefix(strings.TrimSpace(content), "[NO_VOICE]") {
			content = strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(content), "[NO_VOICE]"))
		} else {
			// Try to generate audio
			if path, err := generateTTS(workspace, content); err == nil {
				audioPath = path
			} else {
				logger.DebugCF("agent", "TTS generation skipped/failed (message tool)", map[string]interface{}{
					"error": err.Error(),
				})
			}
		}

		msgBus.PublishOutbound(bus.OutboundMessage{
			Channel:   channel,
			ChatID:    chatID,
			Content:   content,
			AudioPath: audioPath,
		})
		return nil
	})
	registry.Register(messageTool)

	return registry
}

func NewAgentLoop(cfg *config.Config, msgBus *bus.MessageBus, provider providers.LLMProvider) *AgentLoop {
	workspace := cfg.WorkspacePath()
	os.MkdirAll(workspace, 0755)

	restrict := cfg.Agents.Defaults.RestrictToWorkspace

	// Create tool registry for main agent
	toolsRegistry := createToolRegistry(workspace, restrict, cfg, msgBus)

	// Create subagent manager with its own tool registry
	subagentManager := tools.NewSubagentManager(provider, cfg.Agents.Defaults.Model, workspace, msgBus)
	subagentTools := createToolRegistry(workspace, restrict, cfg, msgBus)
	// Subagent doesn't need spawn/subagent tools to avoid recursion
	subagentManager.SetTools(subagentTools)

	// Register spawn tool (for main agent)
	spawnTool := tools.NewSpawnTool(subagentManager)
	toolsRegistry.Register(spawnTool)

	// Register subagent tool (synchronous execution)
	subagentTool := tools.NewSubagentTool(subagentManager)
	toolsRegistry.Register(subagentTool)

	sessionsManager := session.NewSessionManager(filepath.Join(workspace, "sessions"))

	// Create state manager for atomic state persistence
	stateManager := state.NewManager(workspace)

	// Create context builder and set tools registry
	contextBuilder := NewContextBuilder(workspace)
	contextBuilder.SetToolsRegistry(toolsRegistry)

	return &AgentLoop{
		config:         cfg,
		bus:            msgBus,
		provider:       provider,
		workspace:      workspace,
		model:          cfg.Agents.Defaults.Model,
		fallbackModels: cfg.Agents.Defaults.FallbackModels,
		contextWindow:  cfg.Agents.Defaults.MaxTokens, // Restore context window for summarization
		maxIterations:  cfg.Agents.Defaults.MaxToolIterations,
		sessions:       sessionsManager,
		state:          stateManager,
		contextBuilder: contextBuilder,
		tools:          toolsRegistry,
		summarizing:    sync.Map{},
		cache:          make(map[string]ToolCacheEntry),
	}
}

func generateTTS(workspace, text string) (string, error) {
	// Clean text for TTS (remove markdown symbols)
	// 1. Remove code blocks
	reCode := regexp.MustCompile("```[\\s\\S]*?```")
	cleanText := reCode.ReplaceAllString(text, "code block")

	// 2. Remove inline code
	reInline := regexp.MustCompile("`[^`]*`")
	cleanText = reInline.ReplaceAllString(cleanText, "code")

	// 3. Remove links [text](url) -> text
	reLink := regexp.MustCompile(`\[([^\]]+)\]\([^\)]+\)`)
	cleanText = reLink.ReplaceAllString(cleanText, "$1")

	// 4. Remove bold/italic/header markers (*, _, #)
	reSymbols := regexp.MustCompile(`[\*\_#]`)
	cleanText = reSymbols.ReplaceAllString(cleanText, "")

	// Search in workspace/skills and system skills
	skillPaths := []string{
		filepath.Join(workspace, "skills", "voice-tts"),
		"/picoclaw/skills/voice-tts",
		"workspace/skills/voice-tts",
	}

	var scriptPath string
	for _, p := range skillPaths {
		sp := filepath.Join(p, "scripts", "speak.py")
		if _, err := os.Stat(sp); err == nil {
			scriptPath = sp
			break
		}
	}

	if scriptPath == "" {
		return "", fmt.Errorf("voice-tts skill not found")
	}

	// Generate temp file
	outFile := filepath.Join(os.TempDir(), fmt.Sprintf("tts_%d.mp3", time.Now().UnixNano()))

	// Execute script
	// python3 script.py "text" --file outFile --no-play
	cmd := exec.Command("python3", scriptPath, cleanText, "--file", outFile, "--no-play")
	if out, err := cmd.CombinedOutput(); err != nil {
		return "", fmt.Errorf("tts execution failed: %v, output: %s", err, out)
	}

	return outFile, nil
}

func (al *AgentLoop) Run(ctx context.Context) error {
	al.running.Store(true)

	for al.running.Load() {
		select {
		case <-ctx.Done():
			return nil
		default:
			msg, ok := al.bus.ConsumeInbound(ctx)
			if !ok {
				continue
			}

			response, err := al.processMessage(ctx, msg)
			if err != nil {
				response = fmt.Sprintf("Error processing message: %v", err)
			}

			if response != "" {
				// Check if the message tool already sent a response during this round.
				// If so, skip publishing to avoid duplicate messages to the user.
				alreadySent := false
				if tool, ok := al.tools.Get("message"); ok {
					if mt, ok := tool.(*tools.MessageTool); ok {
						alreadySent = mt.HasSentInRound()
					}
				}

				if !alreadySent {
					var audioPath string
					// Try to generate audio
					if path, err := generateTTS(al.workspace, response); err == nil {
						audioPath = path
					} else {
						// Debug log if TTS fails (e.g. skill not found)
						logger.DebugCF("agent", "TTS generation skipped/failed", map[string]interface{}{
							"error": err.Error(),
						})
					}

					al.bus.PublishOutbound(bus.OutboundMessage{
						Channel:   msg.Channel,
						ChatID:    msg.ChatID,
						Content:   response,
						AudioPath: audioPath,
					})
				}
			}
		}
	}

	return nil
}

func (al *AgentLoop) Stop() {
	al.running.Store(false)
}

func (al *AgentLoop) RegisterTool(tool tools.Tool) {
	al.tools.Register(tool)
}

func (al *AgentLoop) SetChannelManager(cm *channels.Manager) {
	al.channelManager = cm
}

// RecordLastChannel records the last active channel for this workspace.
// This uses the atomic state save mechanism to prevent data loss on crash.
func (al *AgentLoop) RecordLastChannel(channel string) error {
	return al.state.SetLastChannel(channel)
}

// RecordLastChatID records the last active chat ID for this workspace.
// This uses the atomic state save mechanism to prevent data loss on crash.
func (al *AgentLoop) RecordLastChatID(chatID string) error {
	return al.state.SetLastChatID(chatID)
}

func (al *AgentLoop) ProcessDirect(ctx context.Context, content, sessionKey string) (string, error) {
	return al.ProcessDirectWithChannel(ctx, content, sessionKey, "cli", "direct")
}

func (al *AgentLoop) ProcessDirectWithChannel(ctx context.Context, content, sessionKey, channel, chatID string) (string, error) {
	msg := bus.InboundMessage{
		Channel:    channel,
		SenderID:   "cron",
		ChatID:     chatID,
		Content:    content,
		SessionKey: sessionKey,
	}

	return al.processMessage(ctx, msg)
}

// ProcessHeartbeat processes a heartbeat request without session history.
// Each heartbeat is independent and doesn't accumulate context.
func (al *AgentLoop) ProcessHeartbeat(ctx context.Context, content, channel, chatID string) (string, error) {
	return al.runAgentLoop(ctx, processOptions{
		SessionKey:      "heartbeat",
		Channel:         channel,
		ChatID:          chatID,
		UserMessage:     content,
		DefaultResponse: "Saya telah selesai memproses pesan, namun tidak ada respons tambahan.",
		EnableSummary:   false,
		SendResponse:    false,
		NoHistory:       true, // Don't load session history for heartbeat
	})
}

func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage) (string, error) {
	// Add message preview to log (show full content for error messages)
	var logContent string
	if strings.Contains(msg.Content, "Error:") || strings.Contains(msg.Content, "error") {
		logContent = msg.Content // Full content for errors
	} else {
		logContent = utils.Truncate(msg.Content, 80)
	}
	logger.InfoCF("agent", fmt.Sprintf("Processing message from %s:%s: %s", msg.Channel, msg.SenderID, logContent),
		map[string]interface{}{
			"channel":     msg.Channel,
			"chat_id":     msg.ChatID,
			"sender_id":   msg.SenderID,
			"session_key": msg.SessionKey,
		})

	// Route system messages to processSystemMessage
	if msg.Channel == "system" {
		return al.processSystemMessage(ctx, msg)
	}

	// Check for commands
	if response, handled := al.handleCommand(ctx, msg); handled {
		return response, nil
	}

	// Process as user message
	return al.runAgentLoop(ctx, processOptions{
		SessionKey:      msg.SessionKey,
		Channel:         msg.Channel,
		ChatID:          msg.ChatID,
		UserMessage:     msg.Content,
		DefaultResponse: "Saya telah selesai memproses pesan, namun tidak ada respons tambahan.",
		EnableSummary:   true,
		SendResponse:    false,
		Media:           msg.Media,
	})
}

func (al *AgentLoop) processSystemMessage(ctx context.Context, msg bus.InboundMessage) (string, error) {
	// Verify this is a system message
	if msg.Channel != "system" {
		return "", fmt.Errorf("processSystemMessage called with non-system message channel: %s", msg.Channel)
	}

	logger.InfoCF("agent", "Processing system message",
		map[string]interface{}{
			"sender_id": msg.SenderID,
			"chat_id":   msg.ChatID,
		})

	// Parse origin channel from chat_id (format: "channel:chat_id")
	var originChannel string
	if idx := strings.Index(msg.ChatID, ":"); idx > 0 {
		originChannel = msg.ChatID[:idx]
	} else {
		// Fallback
		originChannel = "cli"
	}

	// Extract subagent result from message content
	// Format: "Task 'label' completed.\n\nResult:\n<actual content>"
	content := msg.Content
	if idx := strings.Index(content, "Result:\n"); idx >= 0 {
		content = content[idx+8:] // Extract just the result part
	}

	// Skip internal channels - only log, don't send to user
	if constants.IsInternalChannel(originChannel) {
		logger.InfoCF("agent", "Subagent completed (internal channel)",
			map[string]interface{}{
				"sender_id":   msg.SenderID,
				"content_len": len(content),
				"channel":     originChannel,
			})
		return "", nil
	}

	// Agent acts as dispatcher only - subagent handles user interaction via message tool
	// Don't forward result here, subagent should use message tool to communicate with user
	logger.InfoCF("agent", "Subagent completed",
		map[string]interface{}{
			"sender_id":   msg.SenderID,
			"channel":     originChannel,
			"content_len": len(content),
		})

	// Agent only logs, does not respond to user
	return "", nil
}

// runAgentLoop is the core message processing logic.
// It handles context building, LLM calls, tool execution, and response handling.
func (al *AgentLoop) runAgentLoop(ctx context.Context, opts processOptions) (string, error) {
	// 0. Record last channel for heartbeat notifications (skip internal channels)
	if opts.Channel != "" && opts.ChatID != "" {
		// Don't record internal channels (cli, system, subagent)
		if !constants.IsInternalChannel(opts.Channel) {
			channelKey := fmt.Sprintf("%s:%s", opts.Channel, opts.ChatID)
			if err := al.RecordLastChannel(channelKey); err != nil {
				logger.WarnCF("agent", "Failed to record last channel: %v", map[string]interface{}{"error": err.Error()})
			}
		}
	}

	// 1. Update tool contexts
	al.updateToolContexts(opts.Channel, opts.ChatID)

	// Determine adaptive iteration limit
	if opts.MaxIterations == 0 {
		// Heuristic:
		// - Short messages (< 50 chars): likely simple queries -> 10 iterations
		// - Medium messages (< 200 chars): standard tasks -> 25 iterations
		// - Long messages or complex keywords: complex tasks -> al.maxIterations (usually 50)
		msgLen := len(opts.UserMessage)
		opts.MaxIterations = 25 // Default medium

		if msgLen < 50 {
			opts.MaxIterations = 10
		} else if msgLen >= 200 {
			opts.MaxIterations = al.maxIterations
		}

		// Check for complexity keywords
		keywords := []string{"plan", "analyze", "research", "scan", "crawl", "recursive", "complex"}
		lowerMsg := strings.ToLower(opts.UserMessage)
		for _, kw := range keywords {
			if strings.Contains(lowerMsg, kw) {
				opts.MaxIterations = al.maxIterations + 10 // Boost for complex tasks
				break
			}
		}

		// Ensure we don't exceed a hard safety limit (e.g., 100) unless configured
		if opts.MaxIterations > 100 {
			opts.MaxIterations = 100
		}
	}
	
	logger.InfoCF("agent", "Adaptive iteration limit set", map[string]interface{}{
		"limit": opts.MaxIterations,
		"msg_len": len(opts.UserMessage),
	})

	// 2. Build messages (skip history for heartbeat)
	var history []providers.Message
	var summary string
	if !opts.NoHistory {
		history = al.sessions.GetHistory(opts.SessionKey)
		summary = al.sessions.GetSummary(opts.SessionKey)
	}
	messages := al.contextBuilder.BuildMessages(
		history,
		summary,
		opts.UserMessage,
		opts.Media,
		opts.Channel,
		opts.ChatID,
	)

	// 3. Save user message to session
	al.sessions.AddMessage(opts.SessionKey, "user", opts.UserMessage)

	// 4. Run LLM iteration loop
	finalContent, iteration, err := al.runLLMIteration(ctx, messages, opts)
	if err != nil {
		return "", err
	}

	// If last tool had ForUser content and we already sent it, we might not need to send final response
	// This is controlled by the tool's Silent flag and ForUser content

	// 5. Handle empty response
	if finalContent == "" {
		// Try to construct a meaningful response from the last action
		history := al.sessions.GetHistory(opts.SessionKey)
		if len(history) > 0 {
			lastMsg := history[len(history)-1]
			if lastMsg.Role == "tool" {
				// Find tool name from previous assistant message
				var toolName string
				if len(history) > 1 {
					prevMsg := history[len(history)-2]
					if prevMsg.Role == "assistant" {
						for _, tc := range prevMsg.ToolCalls {
							if tc.ID == lastMsg.ToolCallID {
								toolName = tc.Name
								break
							}
						}
					}
				}

				contentPreview := utils.Truncate(lastMsg.Content, 200)
				if toolName != "" {
					finalContent = fmt.Sprintf("Saya telah selesai menjalankan **%s**.\n\nHasil:\n%s", toolName, contentPreview)
				} else {
					finalContent = fmt.Sprintf("Proses selesai.\n\nHasil:\n%s", contentPreview)
				}
			}
		}

		// Fallback to default if still empty
		if finalContent == "" {
			finalContent = opts.DefaultResponse
		}
	}

	// 6. Save final assistant message to session
	al.sessions.AddMessage(opts.SessionKey, "assistant", finalContent)
	al.sessions.Save(opts.SessionKey)

	// 7. Optional: summarization
	if opts.EnableSummary {
		al.maybeSummarize(opts.SessionKey, opts.Channel, opts.ChatID)
	}

	// 8. Optional: send response via bus
	if opts.SendResponse {
		al.bus.PublishOutbound(bus.OutboundMessage{
			Channel: opts.Channel,
			ChatID:  opts.ChatID,
			Content: finalContent,
		})
	}

	// 9. Log response
	responsePreview := utils.Truncate(finalContent, 120)
	logger.InfoCF("agent", fmt.Sprintf("Response: %s", responsePreview),
		map[string]interface{}{
			"session_key":  opts.SessionKey,
			"iterations":   iteration,
			"final_length": len(finalContent),
		})

	return finalContent, nil
}

// runLLMIteration executes the LLM call loop with tool handling.
// Returns the final content, iteration count, and any error.
func (al *AgentLoop) runLLMIteration(ctx context.Context, messages []providers.Message, opts processOptions) (string, int, error) {
	iteration := 0
	var finalContent string
	
	// Use config for dynamic values if not overridden
	maxIterations := opts.MaxIterations
	if maxIterations <= 0 {
		if al.config != nil {
			maxIterations = al.config.GetAgentDefaults().MaxToolIterations
		}
		if maxIterations <= 0 {
			maxIterations = al.maxIterations
		}
	}

	for iteration < maxIterations {
		iteration++
		metrics.AgentIterations.Inc()

		// Adaptive iteration limit:
		// If we are nearing the limit but the agent is still using tools effectively (not looping),
		// we might want to extend the limit slightly.
		// For now, let's implement a simple check: if we hit the limit, but the last action was a successful tool call
		// that produced new information (not an error), we could allow 1-2 more iterations.
		// However, to keep it simple and safe, we'll just log a warning if we hit the limit.
		if iteration == maxIterations {
			logger.WarnCF("agent", "Max iterations reached", map[string]interface{}{
				"limit": maxIterations,
			})
		}

		logger.DebugCF("agent", "LLM iteration",
			map[string]interface{}{
				"iteration": iteration,
				"max":       maxIterations,
			})

		// Build tool definitions
		providerToolDefs := al.tools.ToProviderDefs()

		// Log LLM request details
		logger.DebugCF("agent", "LLM request",
			map[string]interface{}{
				"iteration":         iteration,
				"model":             al.model,
				"messages_count":    len(messages),
				"tools_count":       len(providerToolDefs),
				"max_tokens":        8192,
				"temperature":       0.7,
				"system_prompt_len": len(messages[0].Content),
			})

		// Log full messages (detailed)
		logger.DebugCF("agent", "Full LLM request",
			map[string]interface{}{
				"iteration":     iteration,
				"messages_json": formatMessagesForLog(messages),
				"tools_json":    formatToolsForLog(providerToolDefs),
			})

		var response *providers.LLMResponse
		var err error

		// Create list of models to try: [current_model, fallback_1, fallback_2, ...]
		modelsToTry := []string{al.model}
		if len(al.fallbackModels) > 0 {
			modelsToTry = append(modelsToTry, al.fallbackModels...)
		} else {
			// Legacy/Fallback hardcoded if config is empty
			backupModels := []string{
				"arcee-ai/trinity-large-preview:free",
			}
			modelsToTry = append(modelsToTry, backupModels...)
		}

		modelSuccess := false

		for modelIdx, modelToUse := range modelsToTry {
			// Retry loop for context/token errors
			maxRetries := 2
			for retry := 0; retry <= maxRetries; retry++ {
				response, err = al.provider.Chat(ctx, messages, providerToolDefs, modelToUse, map[string]interface{}{
					"max_tokens":  8192,
					"temperature": 0.7,
				})

				if err == nil {
					modelSuccess = true
					metrics.LLMRequests.WithLabelValues(modelToUse, "success").Inc()
					break // Success
				}
				metrics.LLMRequests.WithLabelValues(modelToUse, "error").Inc()

				errMsg := strings.ToLower(err.Error())
				// Check for context window errors (provider specific, but usually contain "token" or "invalid")
				isContextError := strings.Contains(errMsg, "token") ||
					strings.Contains(errMsg, "context") ||
					strings.Contains(errMsg, "invalidparameter") ||
					strings.Contains(errMsg, "length")

				// Check for transient errors (network, server error)
				isTransientError := strings.Contains(errMsg, "timeout") ||
					strings.Contains(errMsg, "connection") ||
					strings.Contains(errMsg, "500") ||
					strings.Contains(errMsg, "502") ||
					strings.Contains(errMsg, "503") ||
					strings.Contains(errMsg, "504") ||
					strings.Contains(errMsg, "rate limit") ||
					strings.Contains(errMsg, "429")

				if (isContextError || isTransientError) && retry < maxRetries {
					logger.WarnCF("agent", "Recoverable error detected, retrying with backoff", map[string]interface{}{
						"error":        err.Error(),
						"retry":        retry,
						"model":        modelToUse,
						"is_context":   isContextError,
						"is_transient": isTransientError,
					})

					// Exponential backoff: 1s, 2s, 4s
					backoff := time.Duration(1<<retry) * time.Second
					time.Sleep(backoff)

					if isContextError {
						if retry == 0 && !constants.IsInternalChannel(opts.Channel) && opts.SendResponse {
							al.bus.PublishOutbound(bus.OutboundMessage{
								Channel: opts.Channel,
								ChatID:  opts.ChatID,
								Content: "⚠️ Context window exceeded. Compressing history and retrying...",
							})
						}
						// Apply compression logic if needed (currently simple retry)
						// Ideally call al.forceCompression(opts.SessionKey) here if we had access to modify history in-place
						// But history is passed as argument 'messages'. 
						// To fix context error, we MUST reduce 'messages'.
						
						// Simple truncation for retry
						if len(messages) > 2 {
							// Drop a few oldest messages from the middle (preserve system prompt and last few)
							// messages[0] is system.
							// Remove messages[1] and messages[2] if available
							if len(messages) > 4 {
								// Remove 2 messages (user+assistant pair usually)
								newMessages := make([]providers.Message, 0, len(messages)-2)
								newMessages = append(newMessages, messages[0])
								newMessages = append(newMessages, messages[3:]...)
								messages = newMessages
								logger.WarnCF("agent", "Dropped 2 oldest messages for retry", nil)
							}
						}
					}
				} else {
					// Non-recoverable or max retries reached
					break
				}
			}

			if modelSuccess {
				if modelToUse != al.model {
					logger.WarnCF("agent", "Switched to fallback model", map[string]interface{}{
						"original": al.model,
						"current":  modelToUse,
					})
				}
				break
			}
			
			logger.WarnCF("agent", "Model failed, checking fallbacks", map[string]interface{}{
				"model":       modelToUse,
				"error":       err.Error(),
				"has_more":    modelIdx < len(modelsToTry)-1,
			})
		}

		if !modelSuccess {
			logger.ErrorCF("agent", "LLM call failed after all models tried", map[string]interface{}{
				"error": err.Error(),
			})
			return "", iteration, fmt.Errorf("LLM call failed after all models tried: %w", err)
		}


		// Check if no tool calls - we're done
		if len(response.ToolCalls) == 0 {
			finalContent = response.Content
			logger.InfoCF("agent", "LLM response without tool calls (direct answer)",
				map[string]interface{}{
					"iteration":     iteration,
					"content_chars": len(finalContent),
				})
			break
		}

		// Log tool calls
		toolNames := make([]string, 0, len(response.ToolCalls))
		for _, tc := range response.ToolCalls {
			toolNames = append(toolNames, tc.Name)
		}
		logger.InfoCF("agent", "LLM requested tool calls",
			map[string]interface{}{
				"tools":     toolNames,
				"count":     len(response.ToolCalls),
				"iteration": iteration,
			})

		// Build assistant message with tool calls
		assistantMsg := providers.Message{
			Role:    "assistant",
			Content: response.Content,
		}
		for _, tc := range response.ToolCalls {
			argumentsJSON, _ := json.Marshal(tc.Arguments)
			assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, providers.ToolCall{
				ID:   tc.ID,
				Type: "function",
				Function: &providers.FunctionCall{
					Name:      tc.Name,
					Arguments: string(argumentsJSON),
				},
			})
		}
		messages = append(messages, assistantMsg)

		// Save assistant message with tool calls to session
		al.sessions.AddFullMessage(opts.SessionKey, assistantMsg)

		// Execute tool calls
		// We execute read-only tools in parallel, and side-effect tools sequentially to preserve causality.
		// Results must be appended to messages in the original order.
		results := make([]providers.Message, len(response.ToolCalls))
		executed := make([]bool, len(response.ToolCalls))

		// Helper to check if tool is safe for parallel execution
		isSafe := func(name string) bool {
			switch name {
			case "read_file", "list_dir", "web_fetch", "web_search", "weather":
				return true
			default:
				return false
			}
		}

		// Helper to check if tool result is cacheable
		isCacheable := func(name string) bool {
			switch name {
			case "read_file", "list_dir":
				return true
			default:
				return false
			}
		}

		i := 0
		for i < len(response.ToolCalls) {
			// Identify a batch of safe tools
			start := i
			end := i
			
			if isSafe(response.ToolCalls[i].Name) {
				// Extend batch while tools are safe
				for end < len(response.ToolCalls) && isSafe(response.ToolCalls[end].Name) {
					end++
				}
			} else {
				// Single unsafe tool
				end = i + 1
			}

			// Execute batch
			var wg sync.WaitGroup
			for j := start; j < end; j++ {
				wg.Add(1)
				go func(idx int, tc providers.ToolCall) {
					defer wg.Done()

					// Log tool call with arguments preview
					argsJSON, _ := json.Marshal(tc.Arguments)
					argsPreview := utils.Truncate(string(argsJSON), 200)
					logger.InfoCF("agent", fmt.Sprintf("Tool call: %s(%s)", tc.Name, argsPreview),
						map[string]interface{}{
							"tool":      tc.Name,
							"iteration": iteration,
						})

					var toolResult *tools.ToolResult
					var cacheKey string

					// Check cache for deterministic tools
					if isCacheable(tc.Name) {
						cacheKey = tc.Name + ":" + string(argsJSON)
						al.cacheMutex.RLock()
						if entry, ok := al.cache[cacheKey]; ok && time.Now().Before(entry.ExpiresAt) {
							toolResult = entry.Result
							logger.DebugCF("agent", "Cache hit for tool", map[string]interface{}{"tool": tc.Name})
							metrics.ToolCacheHits.WithLabelValues(tc.Name).Inc()
						} else {
							metrics.ToolCacheMisses.WithLabelValues(tc.Name).Inc()
						}
						al.cacheMutex.RUnlock()
					}

					if toolResult == nil {
						// Create async callback
						asyncCallback := func(callbackCtx context.Context, result *tools.ToolResult) {
							if !result.Silent && result.ForUser != "" {
								logger.InfoCF("agent", "Async tool completed, agent will handle notification",
									map[string]interface{}{
										"tool":        tc.Name,
										"content_len": len(result.ForUser),
									})
							}
						}

						// Retry logic for tool execution
						maxRetries := 2
						for retry := 0; retry <= maxRetries; retry++ {
							toolResult = al.tools.ExecuteWithContext(ctx, tc.Name, tc.Arguments, opts.Channel, opts.ChatID, asyncCallback)
							
							if toolResult.Err == nil {
								break
							}
							
							// Check for transient errors
							errMsg := strings.ToLower(toolResult.Err.Error())
							isTransient := strings.Contains(errMsg, "timeout") ||
								strings.Contains(errMsg, "connection") ||
								strings.Contains(errMsg, "rate limit") ||
								strings.Contains(errMsg, "429") ||
								strings.Contains(errMsg, "500") ||
								strings.Contains(errMsg, "502") ||
								strings.Contains(errMsg, "503") ||
								strings.Contains(errMsg, "504") ||
								strings.Contains(errMsg, "temporary")

							if !isTransient {
								break
							}
							
							if retry < maxRetries {
								logger.WarnCF("agent", "Tool execution failed, retrying", map[string]interface{}{
									"tool":  tc.Name,
									"error": toolResult.Err.Error(),
									"retry": retry,
								})
								time.Sleep(time.Duration(1<<retry) * time.Second)
							}
						}

						// Update cache if successful
						if isCacheable(tc.Name) && toolResult.Err == nil {
							al.cacheMutex.Lock()
							al.cache[cacheKey] = ToolCacheEntry{
								Result:    toolResult,
								ExpiresAt: time.Now().Add(60 * time.Second), // 60s TTL
							}
							al.cacheMutex.Unlock()
						}
					}

					// Send ForUser content to user immediately if not Silent
					if !toolResult.Silent && toolResult.ForUser != "" && opts.SendResponse {
						al.bus.PublishOutbound(bus.OutboundMessage{
							Channel: opts.Channel,
							ChatID:  opts.ChatID,
							Content: toolResult.ForUser,
						})
						logger.DebugCF("agent", "Sent tool result to user",
							map[string]interface{}{
								"tool":        tc.Name,
								"content_len": len(toolResult.ForUser),
							})
					}

					// Determine content for LLM based on tool result
					contentForLLM := toolResult.ForLLM
					if contentForLLM == "" && toolResult.Err != nil {
						contentForLLM = toolResult.Err.Error()
					}

					results[idx] = providers.Message{
						Role:       "tool",
						Content:    contentForLLM,
						ToolCallID: tc.ID,
					}
					executed[idx] = true
				}(j, response.ToolCalls[j])
			}
			wg.Wait()
			i = end
		}

		// Append results to messages and session in order
		for idx, res := range results {
			if !executed[idx] {
				// Should not happen if logic is correct
				logger.ErrorCF("agent", "Tool result missing for index", map[string]interface{}{"index": idx})
				continue
			}
			messages = append(messages, res)
			al.sessions.AddFullMessage(opts.SessionKey, res)
		}

		// Automatic iteration extension
		// If we reached the limit but just finished executing tools, the task is likely not done.
		// We extend the limit to allow the agent to finish, up to a hard safety limit.
		if iteration >= maxIterations {
			hardLimit := 100 // Default safety hard limit
			// If config has a higher limit, respect that + buffer
			if al.config != nil {
				configLimit := al.config.GetAgentDefaults().MaxToolIterations
				if configLimit > hardLimit {
					hardLimit = configLimit + 20
				}
			}

			if maxIterations < hardLimit {
				extension := 10
				newLimit := maxIterations + extension
				if newLimit > hardLimit {
					newLimit = hardLimit
				}

				if newLimit > maxIterations {
					logger.WarnCF("agent", "Auto-extending iteration limit", map[string]interface{}{
						"old_limit": maxIterations,
						"new_limit": newLimit,
						"reason":    "task ongoing",
					})
					maxIterations = newLimit
					
					// Notify user slightly if it's a significant extension
					if maxIterations > 30 && !constants.IsInternalChannel(opts.Channel) && opts.SendResponse {
						// Only send a subtle notification once or if really long
						if maxIterations == 40 || maxIterations == 70 {
							al.bus.PublishOutbound(bus.OutboundMessage{
								Channel: opts.Channel,
								ChatID:  opts.ChatID,
								Content: fmt.Sprintf("🔄 Memperpanjang waktu proses (%d langkah)...", maxIterations),
							})
						}
					}
				}
			}
		}
	}

	return finalContent, iteration, nil
}



// updateToolContexts updates the context for tools that need channel/chatID info.
func (al *AgentLoop) updateToolContexts(channel, chatID string) {
	// Use ContextualTool interface instead of type assertions
	if tool, ok := al.tools.Get("message"); ok {
		if mt, ok := tool.(tools.ContextualTool); ok {
			mt.SetContext(channel, chatID)
		}
	}
	if tool, ok := al.tools.Get("spawn"); ok {
		if st, ok := tool.(tools.ContextualTool); ok {
			st.SetContext(channel, chatID)
		}
	}
	if tool, ok := al.tools.Get("subagent"); ok {
		if st, ok := tool.(tools.ContextualTool); ok {
			st.SetContext(channel, chatID)
		}
	}
}

// maybeSummarize triggers summarization if the session history exceeds thresholds.
func (al *AgentLoop) maybeSummarize(sessionKey, channel, chatID string) {
	newHistory := al.sessions.GetHistory(sessionKey)
	tokenEstimate := al.estimateTokens(newHistory)
	threshold := al.contextWindow * 75 / 100

	if len(newHistory) > 20 || tokenEstimate > threshold {
		if _, loading := al.summarizing.LoadOrStore(sessionKey, true); !loading {
			go func() {
				defer al.summarizing.Delete(sessionKey)
				// Notify user about optimization if not an internal channel
				if !constants.IsInternalChannel(channel) {
					al.bus.PublishOutbound(bus.OutboundMessage{
						Channel: channel,
						ChatID:  chatID,
						Content: "🧹 Sedang merapikan ingatan percakapan untuk menjaga performa...",
					})
				}
				al.summarizeSession(sessionKey)
			}()
		}
	}
}

// forceCompression aggressively reduces context when the limit is hit.
// It drops the oldest 50% of messages (keeping system prompt and last user message).
func (al *AgentLoop) forceCompression(sessionKey string) {
	history := al.sessions.GetHistory(sessionKey)
	if len(history) <= 4 {
		return
	}

	// Keep system prompt (usually [0]) and the very last message (user's trigger)
	// We want to drop the oldest half of the *conversation*
	// Assuming [0] is system, [1:] is conversation
	conversation := history[1 : len(history)-1]
	if len(conversation) == 0 {
		return
	}

	// Helper to find the mid-point of the conversation
	mid := len(conversation) / 2

	// New history structure:
	// 1. System Prompt
	// 2. [Summary of dropped part] - synthesized
	// 3. Second half of conversation
	// 4. Last message

	// Simplified approach for emergency: Drop first half of conversation
	// and rely on existing summary if present, or create a placeholder.

	droppedCount := mid
	keptConversation := conversation[mid:]

	newHistory := make([]providers.Message, 0)
	newHistory = append(newHistory, history[0]) // System prompt

	// Add a note about compression
	compressionNote := fmt.Sprintf("[System: Emergency compression dropped %d oldest messages due to context limit]", droppedCount)
	// If there was an existing summary, we might lose it if it was in the dropped part (which is just messages).
	// The summary is stored separately in session.Summary, so it persists!
	// We just need to ensure the user knows there's a gap.

	// We only modify the messages list here
	newHistory = append(newHistory, providers.Message{
		Role:    "system",
		Content: compressionNote,
	})

	newHistory = append(newHistory, keptConversation...)
	newHistory = append(newHistory, history[len(history)-1]) // Last message

	// Update session
	al.sessions.SetHistory(sessionKey, newHistory)
	al.sessions.Save(sessionKey)

	logger.WarnCF("agent", "Forced compression executed", map[string]interface{}{
		"session_key":  sessionKey,
		"dropped_msgs": droppedCount,
		"new_count":    len(newHistory),
	})
}

// GetStartupInfo returns information about loaded tools and skills for logging.
func (al *AgentLoop) GetStartupInfo() map[string]interface{} {
	info := make(map[string]interface{})

	// Tools info
	tools := al.tools.List()
	info["tools"] = map[string]interface{}{
		"count": len(tools),
		"names": tools,
	}

	// Skills info
	info["skills"] = al.contextBuilder.GetSkillsInfo()

	return info
}

// formatMessagesForLog formats messages for logging
func formatMessagesForLog(messages []providers.Message) string {
	if len(messages) == 0 {
		return "[]"
	}

	buf := utils.GetBuffer()
	defer utils.PutBuffer(buf)

	buf.WriteString("[\n")
	for i, msg := range messages {
		buf.WriteString(fmt.Sprintf("  [%d] Role: %s\n", i, msg.Role))
		if len(msg.ToolCalls) > 0 {
			buf.WriteString("  ToolCalls:\n")
			for _, tc := range msg.ToolCalls {
				buf.WriteString(fmt.Sprintf("    - ID: %s, Type: %s, Name: %s\n", tc.ID, tc.Type, tc.Name))
				if tc.Function != nil {
					buf.WriteString(fmt.Sprintf("      Arguments: %s\n", utils.Truncate(tc.Function.Arguments, 200)))
				}
			}
		}
		if msg.Content != "" {
			content := utils.Truncate(msg.Content, 200)
			buf.WriteString(fmt.Sprintf("  Content: %s\n", content))
		}
		if msg.ToolCallID != "" {
			buf.WriteString(fmt.Sprintf("  ToolCallID: %s\n", msg.ToolCallID))
		}
		buf.WriteString("\n")
	}
	buf.WriteString("]")
	return buf.String()
}

// formatToolsForLog formats tool definitions for logging
func formatToolsForLog(tools []providers.ToolDefinition) string {
	if len(tools) == 0 {
		return "[]"
	}

	buf := utils.GetBuffer()
	defer utils.PutBuffer(buf)

	buf.WriteString("[\n")
	for i, tool := range tools {
		buf.WriteString(fmt.Sprintf("  [%d] Type: %s, Name: %s\n", i, tool.Type, tool.Function.Name))
		buf.WriteString(fmt.Sprintf("      Description: %s\n", tool.Function.Description))
		if len(tool.Function.Parameters) > 0 {
			buf.WriteString(fmt.Sprintf("      Parameters: %s\n", utils.Truncate(fmt.Sprintf("%v", tool.Function.Parameters), 200)))
		}
	}
	buf.WriteString("]")
	return buf.String()
}

// summarizeSession summarizes the conversation history for a session.
func (al *AgentLoop) summarizeSession(sessionKey string) {
	ctx, cancel := context.WithTimeout(context.Background(), 120*time.Second)
	defer cancel()

	history := al.sessions.GetHistory(sessionKey)
	summary := al.sessions.GetSummary(sessionKey)

	// Keep last 4 messages for continuity
	if len(history) <= 4 {
		return
	}

	toSummarize := history[:len(history)-4]

	// Oversized Message Guard
	// Skip messages larger than 50% of context window to prevent summarizer overflow
	maxMessageTokens := al.contextWindow / 2
	validMessages := make([]providers.Message, 0)
	omitted := false

	for _, m := range toSummarize {
		if m.Role != "user" && m.Role != "assistant" {
			continue
		}
		// Estimate tokens for this message
		msgTokens := len(m.Content) / 2 // Use safer estimate here too (2.5 -> 2 for integer division safety)
		if msgTokens > maxMessageTokens {
			omitted = true
			continue
		}
		validMessages = append(validMessages, m)
	}

	if len(validMessages) == 0 {
		return
	}

	// Multi-Part Summarization
	// Split into two parts if history is significant
	var finalSummary string
	if len(validMessages) > 10 {
		mid := len(validMessages) / 2
		part1 := validMessages[:mid]
		part2 := validMessages[mid:]

		s1, _ := al.summarizeBatch(ctx, part1, "")
		s2, _ := al.summarizeBatch(ctx, part2, "")

		// Merge them
		mergePrompt := fmt.Sprintf("Merge these two conversation summaries into one cohesive summary:\n\n1: %s\n\n2: %s", s1, s2)
		resp, err := al.provider.Chat(ctx, []providers.Message{{Role: "user", Content: mergePrompt}}, nil, al.model, map[string]interface{}{
			"max_tokens":  1024,
			"temperature": 0.3,
		})
		if err == nil {
			finalSummary = resp.Content
		} else {
			finalSummary = s1 + " " + s2
		}
	} else {
		finalSummary, _ = al.summarizeBatch(ctx, validMessages, summary)
	}

	if omitted && finalSummary != "" {
		finalSummary += "\n[Note: Some oversized messages were omitted from this summary for efficiency.]"
	}

	if finalSummary != "" {
		al.sessions.SetSummary(sessionKey, finalSummary)
		al.sessions.TruncateHistory(sessionKey, 4)
		al.sessions.Save(sessionKey)
	}
}

// summarizeBatch summarizes a batch of messages.
func (al *AgentLoop) summarizeBatch(ctx context.Context, batch []providers.Message, existingSummary string) (string, error) {
	prompt := "Provide a concise summary of this conversation segment, preserving core context and key points.\n"
	if existingSummary != "" {
		prompt += "Existing context: " + existingSummary + "\n"
	}
	prompt += "\nCONVERSATION:\n"
	for _, m := range batch {
		prompt += fmt.Sprintf("%s: %s\n", m.Role, m.Content)
	}

	response, err := al.provider.Chat(ctx, []providers.Message{{Role: "user", Content: prompt}}, nil, al.model, map[string]interface{}{
		"max_tokens":  1024,
		"temperature": 0.3,
	})
	if err != nil {
		return "", err
	}
	return response.Content, nil
}

// estimateTokens estimates the number of tokens in a message list.
// Uses a safe heuristic of 2.5 characters per token to account for CJK and other
// overheads better than the previous 3 chars/token.
func (al *AgentLoop) estimateTokens(messages []providers.Message) int {
	totalChars := 0
	for _, m := range messages {
		totalChars += utf8.RuneCountInString(m.Content)
	}
	// 2.5 chars per token = totalChars * 2 / 5
	return totalChars * 2 / 5
}

func (al *AgentLoop) handleCommand(ctx context.Context, msg bus.InboundMessage) (string, bool) {
	content := strings.TrimSpace(msg.Content)
	if !strings.HasPrefix(content, "/") {
		return "", false
	}

	parts := strings.Fields(content)
	if len(parts) == 0 {
		return "", false
	}

	cmd := parts[0]
	args := parts[1:]

	switch cmd {
	case "/show":
		if len(args) < 1 {
			return "Usage: /show [model|channel]", true
		}
		switch args[0] {
		case "model":
			return fmt.Sprintf("Current model: %s", al.model), true
		case "channel":
			return fmt.Sprintf("Current channel: %s", msg.Channel), true
		default:
			return fmt.Sprintf("Unknown show target: %s", args[0]), true
		}

	case "/list":
		if len(args) < 1 {
			return "Usage: /list [models|channels]", true
		}
		switch args[0] {
		case "models":
			// TODO: Fetch available models dynamically if possible
			return "Available models: glm-4.7, claude-3-5-sonnet, gpt-4o (configured in config.json/env)", true
		case "channels":
			if al.channelManager == nil {
				return "Channel manager not initialized", true
			}
			channels := al.channelManager.GetEnabledChannels()
			if len(channels) == 0 {
				return "No channels enabled", true
			}
			return fmt.Sprintf("Enabled channels: %s", strings.Join(channels, ", ")), true
		default:
			return fmt.Sprintf("Unknown list target: %s", args[0]), true
		}

	case "/switch":
		if len(args) < 3 || args[1] != "to" {
			return "Usage: /switch [model|channel] to <name>", true
		}
		target := args[0]
		value := args[2]

		switch target {
		case "model":
			oldModel := al.model
			al.model = value
			return fmt.Sprintf("Switched model from %s to %s", oldModel, value), true
		case "channel":
			// This changes the 'default' channel for some operations, or effectively redirects output?
			// For now, let's just validate if the channel exists
			if al.channelManager == nil {
				return "Channel manager not initialized", true
			}
			if _, exists := al.channelManager.GetChannel(value); !exists && value != "cli" {
				return fmt.Sprintf("Channel '%s' not found or not enabled", value), true
			}

			// If message came from CLI, maybe we want to redirect CLI output to this channel?
			// That would require state persistence about "redirected channel"
			// For now, just acknowledged.
			return fmt.Sprintf("Switched target channel to %s (Note: this currently only validates existence)", value), true
		default:
			return fmt.Sprintf("Unknown switch target: %s", target), true
		}
	}

	return "", false
}