-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.py
More file actions
1379 lines (1119 loc) · 48.4 KB
/
cli.py
File metadata and controls
1379 lines (1119 loc) · 48.4 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
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
"""CLI interface for Content Engine."""
import sys
from datetime import datetime
from typing import Optional
import click
import yaml
from sqlalchemy import select
from lib.context_capture import read_project_notes, read_session_history
from lib.context_synthesizer import save_context, synthesize_daily_context
from lib.database import init_db, get_db, Post, PostStatus, Platform, OAuthToken, ContentPlan, ContentPlanStatus
from lib.errors import AIError
from lib.logger import setup_logger
from lib.blueprint_loader import list_blueprints, load_framework, load_workflow, load_constraints
from lib.blueprint_engine import execute_workflow
from agents.linkedin.post import post_to_linkedin
from agents.linkedin.content_generator import generate_post
from agents.linkedin.post_validator import validate_post
from agents.brand_planner import BrandPlanner, ContentBrief, Game
logger = setup_logger(__name__)
@click.group()
def cli() -> None:
"""Content Engine - AI-powered content posting system."""
init_db()
@cli.command()
@click.argument("content")
@click.option("--platform", type=click.Choice(["linkedin", "twitter", "blog"]), default="linkedin")
def draft(content: str, platform: str) -> None:
"""Create a new draft post."""
db = get_db()
post = Post(
content=content,
platform=Platform(platform),
status=PostStatus.DRAFT,
)
db.add(post)
db.commit()
db.refresh(post)
click.echo(f"✅ Draft created (ID: {post.id})")
click.echo(f"Platform: {post.platform.value}")
click.echo(f"Content: {post.content[:100]}..." if len(post.content) > 100 else f"Content: {post.content}")
db.close()
@cli.command()
@click.option("--status", type=click.Choice(["draft", "approved", "scheduled", "posted", "failed", "rejected"]))
@click.option("--platform", type=click.Choice(["linkedin", "twitter", "blog"]))
@click.option("--limit", type=int, default=10)
def list(status: Optional[str], platform: Optional[str], limit: int) -> None:
"""List posts."""
db = get_db()
query = select(Post).order_by(Post.created_at.desc()).limit(limit)
if status:
query = query.where(Post.status == PostStatus(status))
if platform:
query = query.where(Post.platform == Platform(platform))
posts = db.execute(query).scalars().all()
if not posts:
click.echo("No posts found.")
db.close()
return
click.echo(f"\n{'ID':<5} {'Platform':<10} {'Status':<12} {'Created':<20} {'Content':<50}")
click.echo("=" * 100)
for post in posts:
content_preview = post.content[:47] + "..." if len(post.content) > 50 else post.content
created = post.created_at.strftime("%Y-%m-%d %H:%M")
click.echo(f"{post.id:<5} {post.platform.value:<10} {post.status.value:<12} {created:<20} {content_preview:<50}")
db.close()
@cli.command()
@click.argument("post_id", type=int)
def show(post_id: int) -> None:
"""Show full post details."""
db = get_db()
post = db.get(Post, post_id)
if not post:
click.echo(f"❌ Post {post_id} not found")
db.close()
sys.exit(1)
click.echo(f"\n{'='*60}")
click.echo(f"Post ID: {post.id}")
click.echo(f"Platform: {post.platform.value}")
click.echo(f"Status: {post.status.value}")
click.echo(f"Created: {post.created_at}")
click.echo(f"Updated: {post.updated_at}")
if post.scheduled_at:
click.echo(f"Scheduled: {post.scheduled_at}")
if post.posted_at:
click.echo(f"Posted: {post.posted_at}")
if post.external_id:
click.echo(f"External ID: {post.external_id}")
if post.error_message:
click.echo(f"Error: {post.error_message}")
click.echo(f"\nContent:\n{post.content}")
click.echo(f"{'='*60}\n")
db.close()
@cli.command()
@click.argument("post_id", type=int)
@click.option("--dry-run", is_flag=True, help="Test without actually posting")
def approve(post_id: int, dry_run: bool) -> None:
"""Approve a draft and post immediately."""
db = get_db()
post = db.get(Post, post_id)
if not post:
click.echo(f"❌ Post {post_id} not found")
db.close()
sys.exit(1)
if post.status != PostStatus.DRAFT:
click.echo(f"❌ Post must be in DRAFT status (currently: {post.status.value})")
db.close()
sys.exit(1)
# Get OAuth token
token_query = select(OAuthToken).where(OAuthToken.platform == post.platform)
oauth_token = db.execute(token_query).scalar_one_or_none()
if not oauth_token:
click.echo(f"❌ No OAuth token found for {post.platform.value}")
click.echo("Run OAuth flow first: uv run python -m agents.linkedin.oauth_server")
db.close()
sys.exit(1)
click.echo(f"📤 Posting to {post.platform.value}...")
try:
if post.platform == Platform.LINKEDIN:
external_id = post_to_linkedin(
content=post.content,
access_token=oauth_token.access_token,
user_sub=oauth_token.user_sub or "",
dry_run=dry_run,
)
post.status = PostStatus.POSTED
post.posted_at = datetime.utcnow()
post.external_id = external_id
else:
click.echo(f"❌ Platform {post.platform.value} not yet supported")
db.close()
sys.exit(1)
db.commit()
click.echo(f"\n✅ Post {post_id} published successfully!")
if not dry_run and post.platform == Platform.LINKEDIN:
click.echo("View at: https://www.linkedin.com/feed/")
except Exception as e:
post.status = PostStatus.FAILED
post.error_message = str(e)
db.commit()
click.echo(f"❌ Failed to post: {e}")
db.close()
sys.exit(1)
db.close()
@cli.command()
@click.argument("post_id", type=int)
@click.argument("scheduled_time")
def schedule(post_id: int, scheduled_time: str) -> None:
"""
Schedule a draft for later posting.
TIME format: YYYY-MM-DD HH:MM (e.g., 2024-01-15 09:00)
"""
db = get_db()
post = db.get(Post, post_id)
if not post:
click.echo(f"❌ Post {post_id} not found")
db.close()
sys.exit(1)
if post.status != PostStatus.DRAFT:
click.echo(f"❌ Post must be in DRAFT status (currently: {post.status.value})")
db.close()
sys.exit(1)
try:
scheduled_dt = datetime.strptime(scheduled_time, "%Y-%m-%d %H:%M")
except ValueError:
click.echo("❌ Invalid time format. Use: YYYY-MM-DD HH:MM (e.g., 2024-01-15 09:00)")
db.close()
sys.exit(1)
if scheduled_dt < datetime.utcnow():
click.echo("❌ Scheduled time must be in the future")
db.close()
sys.exit(1)
post.status = PostStatus.SCHEDULED
post.scheduled_at = scheduled_dt
db.commit()
click.echo(f"✅ Post {post_id} scheduled for {scheduled_dt}")
click.echo("Run worker to publish: uv run python -m worker")
db.close()
@cli.command()
@click.argument("post_id", type=int)
def reject(post_id: int) -> None:
"""Reject a draft post."""
db = get_db()
post = db.get(Post, post_id)
if not post:
click.echo(f"❌ Post {post_id} not found")
db.close()
sys.exit(1)
post.status = PostStatus.REJECTED
db.commit()
click.echo(f"✅ Post {post_id} rejected")
db.close()
@cli.command("capture-context")
@click.option("--date", help="Date to capture context for (YYYY-MM-DD), defaults to today")
@click.option("--sessions-dir", help="Custom session history directory")
@click.option("--projects-dir", help="Custom projects directory")
@click.option("--output-dir", default="context", help="Output directory for context files")
def capture_context(
date: Optional[str],
sessions_dir: Optional[str],
projects_dir: Optional[str],
output_dir: str,
) -> None:
"""
Capture daily context from sessions and projects.
Reads PAI session history and project notes, synthesizes with local LLM,
and saves structured context to JSON file.
"""
# Determine date
if date:
try:
context_date = datetime.strptime(date, "%Y-%m-%d")
date_str = date
except ValueError:
click.echo("❌ Invalid date format. Use YYYY-MM-DD")
sys.exit(1)
else:
context_date = datetime.now()
date_str = context_date.strftime("%Y-%m-%d")
click.echo(f"📅 Capturing context for {date_str}...")
try:
# Read session history
click.echo("📖 Reading session history...")
sessions = read_session_history(sessions_dir)
click.echo(f" Found {len(sessions)} sessions")
# Read project notes
click.echo("📁 Reading project notes...")
try:
projects = read_project_notes(projects_dir)
click.echo(f" Found {len(projects)} projects")
except FileNotFoundError:
click.echo(" ⚠️ Projects directory not found, continuing without projects")
projects = []
# Synthesize with LLM
click.echo("🤖 Synthesizing with Ollama...")
daily_context = synthesize_daily_context(
sessions=sessions, projects=projects, date=date_str
)
# Save to file
click.echo("💾 Saving context...")
file_path = save_context(daily_context, output_dir)
# Print summary
click.echo("\n✅ Context captured successfully!")
click.echo(f" Sessions: {len(sessions)}")
click.echo(f" Projects: {len(projects)}")
click.echo(f" Themes: {len(daily_context.themes)}")
click.echo(f" Decisions: {len(daily_context.decisions)}")
click.echo(f" Progress: {len(daily_context.progress)}")
click.echo(f"\n📄 Saved to: {file_path}")
# Show themes preview
if daily_context.themes:
click.echo("\n🔍 Key themes:")
for theme in daily_context.themes[:3]:
click.echo(f" • {theme}")
except FileNotFoundError as e:
click.echo(f"❌ {e}")
sys.exit(1)
except AIError as e:
click.echo(f"❌ AI synthesis failed: {e}")
click.echo("\n💡 Make sure Ollama is running: ollama serve")
sys.exit(1)
except Exception as e:
click.echo(f"❌ Failed to capture context: {e}")
logger.exception("Context capture failed")
sys.exit(1)
@cli.group()
def blueprints() -> None:
"""Manage content blueprints (frameworks, workflows, constraints)."""
pass
@blueprints.command("list")
@click.option("--category", type=click.Choice(["frameworks", "workflows", "constraints"]), default=None, help="Filter by category")
def list_blueprints_cmd(category: Optional[str]) -> None:
"""List all available blueprints."""
try:
blueprint_list = list_blueprints(category=category)
if not blueprint_list:
click.echo("No blueprints found.")
return
# Print header
click.echo("\n📋 Available Blueprints\n")
# Group and display by category
for cat, items in sorted(blueprint_list.items()):
click.echo(f" {cat.upper()}:")
if items:
for item in items:
click.echo(f" • {item}")
else:
click.echo(" (none)")
click.echo()
except Exception as e:
click.echo(f"❌ Failed to list blueprints: {e}")
logger.exception("Blueprint listing failed")
sys.exit(1)
@blueprints.command("show")
@click.argument("blueprint_name")
@click.option(
"--platform",
type=str,
default="linkedin",
help="Platform for framework blueprints (default: linkedin)",
)
def show_blueprint(blueprint_name: str, platform: str) -> None:
"""Show detailed blueprint information.
Display the full blueprint structure including validation rules,
sections, examples, and best practices.
Examples:
uv run content-engine blueprints show STF
uv run content-engine blueprints show BrandVoice
uv run content-engine blueprints show SundayPowerHour
"""
try:
# Try loading as framework first (with platform)
blueprint = None
blueprint_type = None
try:
blueprint = load_framework(blueprint_name, platform)
blueprint_type = "Framework"
except FileNotFoundError:
pass
# Try loading as workflow
if blueprint is None:
try:
blueprint = load_workflow(blueprint_name)
blueprint_type = "Workflow"
except FileNotFoundError:
pass
# Try loading as constraint
if blueprint is None:
try:
blueprint = load_constraints(blueprint_name)
blueprint_type = "Constraint"
except FileNotFoundError:
pass
# If still not found, error
if blueprint is None:
click.echo(click.style(f"\n❌ Blueprint '{blueprint_name}' not found", fg="red"))
click.echo("\nTry: uv run content-engine blueprints list")
sys.exit(1)
# Print header
click.echo(f"\n{'='*60}")
click.echo(click.style(f"{blueprint_type}: {blueprint_name}", fg="cyan", bold=True))
if blueprint_type == "Framework":
click.echo(f"Platform: {platform}")
click.echo(f"{'='*60}\n")
# Print formatted YAML
yaml_output = yaml.dump(blueprint, default_flow_style=False, sort_keys=False)
click.echo(yaml_output)
# Print footer with helpful info
click.echo(f"{'='*60}\n")
if blueprint_type == "Framework":
sections = blueprint.get("structure", {}).get("sections", [])
click.echo(click.style("📐 Structure:", fg="blue", bold=True))
click.echo(f" Sections: {len(sections)}")
if sections:
for section in sections:
section_name = section.get("id", "unknown")
click.echo(f" • {section_name}")
validation = blueprint.get("validation", {})
if validation:
click.echo(click.style("\n✓ Validation Rules:", fg="blue", bold=True))
if "min_chars" in validation:
click.echo(f" Min characters: {validation['min_chars']}")
if "max_chars" in validation:
click.echo(f" Max characters: {validation['max_chars']}")
if "min_sections" in validation:
click.echo(f" Min sections: {validation['min_sections']}")
examples = blueprint.get("examples", [])
if examples:
click.echo(click.style(f"\n📝 Examples: {len(examples)} provided", fg="blue", bold=True))
elif blueprint_type == "Workflow":
steps = blueprint.get("steps", [])
click.echo(click.style("🔄 Workflow Steps:", fg="blue", bold=True))
click.echo(f" Total: {len(steps)}")
total_duration = sum(step.get("duration_minutes", 0) for step in steps)
click.echo(f" Duration: {total_duration} minutes")
for i, step in enumerate(steps, 1):
step_name = step.get("name", "unknown")
duration = step.get("duration_minutes", 0)
click.echo(f" {i}. {step_name} ({duration}min)")
elif blueprint_type == "Constraint":
if "characteristics" in blueprint:
chars = blueprint["characteristics"]
click.echo(click.style(f"⚡ Characteristics: {len(chars)}", fg="blue", bold=True))
if "pillars" in blueprint:
pillars = blueprint["pillars"]
click.echo(click.style(f"\n📊 Pillars: {len(pillars)}", fg="blue", bold=True))
for pillar_id, pillar_data in pillars.items():
percentage = pillar_data.get("percentage", 0)
name = pillar_data.get("name", pillar_id)
click.echo(f" • {name}: {percentage}%")
if "forbidden_phrases" in blueprint:
categories = blueprint["forbidden_phrases"]
total_phrases = sum(len(phrases) for phrases in categories.values())
click.echo(click.style(f"\n🚫 Forbidden Phrases: {total_phrases} total", fg="blue", bold=True))
click.echo()
except Exception as e:
click.echo(click.style(f"\n❌ Failed to show blueprint: {e}", fg="red"))
logger.exception("Blueprint show failed")
sys.exit(1)
@cli.command()
@click.option(
"--pillar",
type=click.Choice(["what_building", "what_learning", "sales_tech", "problem_solution"]),
required=True,
help="Content pillar to use for generation",
)
@click.option(
"--framework",
type=click.Choice(["STF", "MRS", "SLA", "PIF"]),
default=None,
help="Framework to use (auto-selected if not specified)",
)
@click.option(
"--date",
default=None,
help="Date for context capture (YYYY-MM-DD), defaults to today",
)
@click.option(
"--model",
default="llama3:8b",
help="Ollama model to use for generation",
)
def generate(pillar: str, framework: Optional[str], date: Optional[str], model: str) -> None:
"""Generate a LinkedIn post using blueprints and context."""
# Determine date
if date:
try:
context_date = datetime.strptime(date, "%Y-%m-%d")
date_str = date
except ValueError:
click.echo("❌ Invalid date format. Use YYYY-MM-DD")
sys.exit(1)
else:
context_date = datetime.now()
date_str = context_date.strftime("%Y-%m-%d")
click.echo(f"📅 Generating content for {date_str}...")
click.echo(f" Pillar: {pillar}")
click.echo(f" Framework: {framework or 'auto-select'}")
try:
# Read session history and project notes
click.echo("\n📖 Reading context...")
sessions = read_session_history()
try:
projects = read_project_notes()
except FileNotFoundError:
click.echo(" ⚠️ Projects directory not found, continuing without projects")
projects = []
# Synthesize daily context
click.echo("🤖 Synthesizing context with Ollama...")
daily_context = synthesize_daily_context(
sessions=sessions, projects=projects, date=date_str
)
click.echo(f" Themes: {len(daily_context.themes)}")
click.echo(f" Decisions: {len(daily_context.decisions)}")
click.echo(f" Progress: {len(daily_context.progress)}")
# Convert DailyContext to dict for generate_post
context_dict = {
"themes": daily_context.themes,
"decisions": daily_context.decisions,
"progress": daily_context.progress,
}
# Generate post
click.echo(f"\n✍️ Generating post with {model}...")
result = generate_post(
context=context_dict,
pillar=pillar,
framework=framework,
model=model,
)
click.echo(f" Framework used: {result.framework_used}")
click.echo(f" Validation score: {result.validation_score:.2f}")
click.echo(f" Iterations: {result.iterations}")
# Show validation warnings if any
if result.violations:
click.echo("\n⚠️ Validation warnings:")
for violation in result.violations:
click.echo(f" • {violation}")
# Save to database
db = get_db()
post = Post(
content=result.content,
platform=Platform.LINKEDIN,
status=PostStatus.DRAFT,
)
db.add(post)
db.commit()
db.refresh(post)
click.echo(f"\n✅ Draft created (ID: {post.id})")
click.echo(f"\n{'='*60}")
click.echo("Content Preview:")
click.echo(f"{'='*60}")
# Show first 500 chars
preview = result.content[:500] + "..." if len(result.content) > 500 else result.content
click.echo(preview)
click.echo(f"{'='*60}")
click.echo("\n💡 Next steps:")
click.echo(f" • Review: uv run content-engine show {post.id}")
click.echo(f" • Approve: uv run content-engine approve {post.id}")
db.close()
except FileNotFoundError as e:
click.echo(f"❌ {e}")
sys.exit(1)
except AIError as e:
click.echo(f"❌ AI generation failed: {e}")
click.echo("\n💡 Make sure Ollama is running: ollama serve")
sys.exit(1)
except Exception as e:
click.echo(f"❌ Failed to generate post: {e}")
logger.exception("Post generation failed")
sys.exit(1)
@cli.command("sunday-power-hour")
def sunday_power_hour() -> None:
"""Execute Sunday Power Hour workflow to generate 10 content ideas.
This workflow:
- Analyzes the last 7 days of session history and projects
- Generates 10 content ideas distributed across pillars (35/30/20/15%)
- Assigns frameworks (STF/MRS/SLA/PIF) to each idea
- Creates ContentPlan records ready for batch generation
- Saves 92 minutes/week via batching vs ad-hoc posting
"""
from datetime import timedelta
click.echo("🚀 Starting Sunday Power Hour workflow...\n")
try:
# Calculate date range (last 7 days)
end_date = datetime.now()
start_date = end_date - timedelta(days=7)
week_start = start_date.strftime("%Y-%m-%d")
click.echo(f"📅 Analyzing: {start_date.strftime('%Y-%m-%d')} to {end_date.strftime('%Y-%m-%d')}")
# Read session history and project notes
click.echo("\n📖 Reading context...")
sessions = read_session_history()
try:
projects = read_project_notes()
click.echo(f" Sessions: {len(sessions)}")
click.echo(f" Projects: {len(projects)}")
except FileNotFoundError:
click.echo(" ⚠️ Projects directory not found, continuing without projects")
projects = []
click.echo(f" Sessions: {len(sessions)}")
# Execute workflow
click.echo("\n⚙️ Executing workflow...")
workflow_inputs = {
"sessions": sessions,
"projects": projects,
"week_start_date": week_start,
}
result = execute_workflow("SundayPowerHour", workflow_inputs)
if not result.success:
click.echo("\n❌ Workflow execution failed:")
for error in result.errors:
click.echo(f" • {error}")
sys.exit(1)
click.echo(f" ✓ Completed {result.steps_completed}/{result.total_steps} steps")
# For MVP: Generate placeholder content plans
# In a real implementation, the workflow would use LLM to generate actual ideas
# For now, we'll create 10 sample plans following the distribution
click.echo("\n📝 Creating content plans...")
pillar_distribution = [
("what_building", "STF"),
("what_building", "SLA"),
("what_building", "STF"),
("what_building", "SLA"),
("what_learning", "MRS"),
("what_learning", "SLA"),
("what_learning", "MRS"),
("sales_tech", "STF"),
("sales_tech", "PIF"),
("problem_solution", "STF"),
]
db = get_db()
created_plans = []
for i, (pillar, framework) in enumerate(pillar_distribution, 1):
plan = ContentPlan(
week_start_date=week_start,
pillar=pillar,
framework=framework,
idea=f"Content idea {i} for {pillar} using {framework} framework",
status=ContentPlanStatus.PLANNED,
)
db.add(plan)
created_plans.append(plan)
db.commit()
# Refresh to get IDs
for plan in created_plans:
db.refresh(plan)
# Print summary
click.echo("\n✅ Sunday Power Hour complete!")
click.echo("\n📊 Summary:")
click.echo(f" Total plans created: {len(created_plans)}")
# Count by pillar
pillar_counts: dict[str, int] = {}
framework_counts: dict[str, int] = {}
for plan in created_plans:
pillar_counts[plan.pillar] = pillar_counts.get(plan.pillar, 0) + 1
framework_counts[plan.framework] = framework_counts.get(plan.framework, 0) + 1
click.echo("\n Distribution by pillar:")
for pillar in ["what_building", "what_learning", "sales_tech", "problem_solution"]:
count = pillar_counts.get(pillar, 0)
percentage = (count / len(created_plans)) * 100
click.echo(f" • {pillar}: {count} ({percentage:.0f}%)")
click.echo("\n Frameworks used:")
for framework, count in sorted(framework_counts.items()):
click.echo(f" • {framework}: {count}")
click.echo("\n💡 Next steps:")
click.echo(f" • Review plans: SELECT * FROM content_plans WHERE week_start_date = '{week_start}'")
click.echo(" • Generate posts: Use 'generate' command for each plan")
click.echo(" • Time saved: ~92 minutes via batching!")
db.close()
except FileNotFoundError as e:
click.echo(f"\n❌ {e}")
sys.exit(1)
except AIError as e:
click.echo(f"\n❌ AI workflow failed: {e}")
click.echo("\n💡 Make sure Ollama is running: ollama serve")
sys.exit(1)
except Exception as e:
click.echo(f"\n❌ Failed to execute Sunday Power Hour: {e}")
logger.exception("Sunday Power Hour failed")
sys.exit(1)
@cli.command()
@click.argument("post_id", type=int)
@click.option(
"--framework",
type=click.Choice(["STF", "MRS", "SLA", "PIF"]),
default="STF",
help="Framework to validate against",
)
def validate(post_id: int, framework: str) -> None:
"""Validate a post against all constraints.
Checks framework structure, brand voice, and platform rules.
Provides detailed feedback with error/warning/suggestion severity levels.
"""
try:
db = get_db()
# Load post
post = db.get(Post, post_id)
if not post:
click.echo(click.style(f"\n❌ Post {post_id} not found", fg="red"))
sys.exit(1)
# Validate post
report = validate_post(post, framework=framework)
# Print header
click.echo(f"\n{'='*60}")
click.echo(f"Validation Report - Post #{post_id}")
click.echo(f"Framework: {framework}")
click.echo(f"{'='*60}\n")
# Print overall status
if report.is_valid:
click.echo(click.style("✅ PASS", fg="green", bold=True))
else:
click.echo(click.style("❌ FAIL", fg="red", bold=True))
click.echo(f"Validation Score: {report.score:.2f}/1.00\n")
# Print violations by severity
if report.errors:
click.echo(click.style("🔴 ERRORS (must fix):", fg="red", bold=True))
for error in report.errors:
click.echo(f" • {error.message}")
if error.suggestion:
click.echo(click.style(f" → {error.suggestion}", fg="yellow"))
click.echo()
if report.warnings:
click.echo(click.style("🟡 WARNINGS (should fix):", fg="yellow", bold=True))
for warning in report.warnings:
click.echo(f" • {warning.message}")
if warning.suggestion:
click.echo(click.style(f" → {warning.suggestion}", fg="cyan"))
click.echo()
if report.suggestions:
click.echo(click.style("💡 SUGGESTIONS (optional):", fg="cyan", bold=True))
for suggestion in report.suggestions:
click.echo(f" • {suggestion.message}")
if suggestion.suggestion:
click.echo(click.style(f" → {suggestion.suggestion}", fg="blue"))
click.echo()
# Print summary
if not report.violations:
click.echo(click.style("🎉 Perfect! No issues found.", fg="green"))
else:
click.echo(f"Total violations: {len(report.violations)}")
click.echo(f" Errors: {len(report.errors)}")
click.echo(f" Warnings: {len(report.warnings)}")
click.echo(f" Suggestions: {len(report.suggestions)}")
# Exit with appropriate code
sys.exit(0 if report.is_valid else 1)
except Exception as e:
click.echo(click.style(f"\n❌ Validation failed: {e}", fg="red"))
logger.exception("Validation failed")
sys.exit(1)
@cli.command("collect-analytics")
@click.option("--days-back", default=7, type=int, help="Fetch analytics for posts from last N days")
@click.option("--test-post", type=str, help="Fetch analytics for a single post URN")
def collect_analytics(days_back: int, test_post: Optional[str]) -> None:
"""Collect LinkedIn post analytics and update posts.jsonl."""
import os
from pathlib import Path
from agents.linkedin.analytics import LinkedInAnalytics
click.echo("=" * 60)
click.echo("LinkedIn Analytics Collection")
click.echo("=" * 60)
# Load access token
access_token = os.getenv("LINKEDIN_ACCESS_TOKEN")
if not access_token:
# Try loading from database
try:
db = get_db()
stmt = select(OAuthToken).where(OAuthToken.platform == Platform.LINKEDIN)
result = db.execute(stmt)
oauth_token = result.scalar_one_or_none()
if oauth_token:
access_token = oauth_token.access_token
except Exception:
pass
if not access_token:
click.echo(click.style("\n❌ Error: LINKEDIN_ACCESS_TOKEN not found", fg="red"))
click.echo("\nPlease set the environment variable:")
click.echo(" export LINKEDIN_ACCESS_TOKEN='your_token_here'")
click.echo("\nOr store it in the database:")
click.echo(" uv run content-engine oauth linkedin")
sys.exit(1)
analytics = LinkedInAnalytics(access_token)
# Test single post
if test_post:
click.echo(f"\n📊 Fetching analytics for: {test_post}")
metrics = analytics.get_post_analytics(test_post)
if metrics:
click.echo(click.style("\n✓ Analytics fetched successfully!", fg="green"))
click.echo(f" Impressions: {metrics.impressions:,}")
click.echo(f" Likes: {metrics.likes}")
click.echo(f" Comments: {metrics.comments}")
click.echo(f" Shares: {metrics.shares}")
click.echo(f" Clicks: {metrics.clicks}")
click.echo(f" Engagement Rate: {metrics.engagement_rate:.2%}")
else:
click.echo(click.style("\n✗ Failed to fetch analytics", fg="red"))
click.echo(" Make sure:")
click.echo(" - The post URN is correct")
click.echo(" - Your access token has analytics permissions")
click.echo(" - The post exists and you have access to it")
sys.exit(1)
else:
# Update all posts
posts_file = Path("data/posts.jsonl")
if not posts_file.exists():
click.echo(click.style(f"\n❌ Error: {posts_file} not found", fg="red"))
click.echo("\nCreate the file first or run:")
click.echo(" mkdir -p data && touch data/posts.jsonl")
sys.exit(1)
click.echo(f"\n📊 Fetching analytics for posts from last {days_back} days...")
click.echo(f" File: {posts_file}")
try:
updated_count = analytics.update_posts_with_analytics(posts_file, days_back=days_back)
click.echo(click.style(f"\n✓ Updated analytics for {updated_count} posts", fg="green"))
if updated_count == 0:
click.echo("\n💡 No posts needed updates. This could mean:")
click.echo(" - All recent posts already have analytics")
click.echo(" - No posts in the specified time window")
click.echo(" - Posts file is empty")
except Exception as e:
click.echo(click.style(f"\n❌ Error updating analytics: {e}", fg="red"))
logger.exception("Analytics collection failed")
sys.exit(1)
@cli.command("plan-content")
@click.option("--days", default=7, type=int, help="Days of context to aggregate (default: 7)")
@click.option("--posts", default=10, type=int, help="Target posts to plan (default: 10)")
@click.option("--dry-run", is_flag=True, help="Preview without saving to database")
@click.option("--model", default="llama3:8b", help="Ollama model for planning (default: llama3:8b)")
def plan_content(days: int, posts: int, dry_run: bool, model: str) -> None:
"""Plan content using Brand Planner agent.
Analyzes context from the past N days and generates content plans
with strategic decisions about pillars, frameworks, and game strategy.
The Brand Planner:
- Extracts content ideas from daily context
- Assigns pillars based on 35/30/20/15 distribution
- Decides game strategy (traffic vs building-in-public)
- Selects appropriate frameworks (STF/MRS/SLA/PIF)
"""
from datetime import timedelta
from pathlib import Path
import json
click.echo("🧠 Starting Brand Planner...\n")
try:
# Calculate date range
end_date = datetime.now()
start_date = end_date - timedelta(days=days)
week_start = start_date.strftime("%Y-%m-%d")
click.echo(f"📅 Analyzing: {start_date.strftime('%Y-%m-%d')} to {end_date.strftime('%Y-%m-%d')}")
click.echo(f"🎯 Target: {posts} posts")
# Load context files
context_dir = Path("context")
contexts = []
if context_dir.exists():
click.echo(f"\n📖 Loading context from {context_dir}/...")
for i in range(days):
date = (end_date - timedelta(days=i)).strftime("%Y-%m-%d")
context_file = context_dir / f"{date}.json"
if context_file.exists():
try:
with open(context_file, "r") as f:
data = json.load(f)
from lib.context_synthesizer import DailyContext
ctx = DailyContext(
themes=data.get("themes", []),
decisions=data.get("decisions", []),
progress=data.get("progress", []),
date=data.get("date", date),
raw_data=data.get("raw_data", {}),
)
contexts.append(ctx)
click.echo(f" ✓ Loaded {date}")
except Exception as e:
click.echo(f" ⚠️ Failed to load {date}: {e}")
if not contexts:
click.echo("\n⚠️ No context files found. Generating from session history...")
# Fall back to capturing context on the fly
sessions = read_session_history()
try:
projects = read_project_notes()
except FileNotFoundError:
projects = []
from lib.context_synthesizer import synthesize_daily_context
ctx = synthesize_daily_context(