create-egregore 0.19.31 → 0.19.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -13,6 +13,7 @@ from typing import Any
13
13
 
14
14
  ROOT = Path(__file__).resolve().parent.parent
15
15
  CORPUS_SCHEMA = "egregore-ingest-corpus/v1"
16
+ CONNECTOR_SCHEMA = "egregore-connector-lifecycle/v1"
16
17
  KNOWLEDGE_SCHEMA = "egregore-knowledge-projection/v1"
17
18
  PLAN_SCHEMA = "egregore-ingest-graph-plan/v2"
18
19
  SOURCE_KINDS = {"meeting", "interview", "google", "notion", "corpus", "document"}
@@ -730,7 +731,84 @@ RETURN a.id""",
730
731
  }
731
732
 
732
733
 
734
+ def connector_manifest(value: dict[str, Any]) -> bool:
735
+ return value.get("schema") == CONNECTOR_SCHEMA
736
+
737
+
738
+ def connector_plan(value: dict[str, Any], path: Path) -> dict[str, Any]:
739
+ """Project connector lifecycle: which providers this org has connected,
740
+ which accounts are authorized on each, and which ingest sources arrived
741
+ through which account. Connected mode only — the plan is executed by the
742
+ same runner as corpus plans and is idempotent (MERGE throughout)."""
743
+ provider = required_string(value.get("provider"), "provider")
744
+ if provider not in ("google", "notion"):
745
+ raise ContractError(f"unsupported connector provider: {provider}")
746
+ accounts = value.get("accounts")
747
+ if not isinstance(accounts, list) or not accounts:
748
+ raise ContractError("accounts must be a non-empty list")
749
+ recorded = required_string(value.get("recorded_at"), "recorded_at")
750
+ source_id = str(value.get("source_id") or "")
751
+ queries: list[dict[str, Any]] = []
752
+ queries.append(
753
+ query(
754
+ """MERGE (c:Connector {id: $provider})
755
+ SET c.provider = $provider,
756
+ c.updatedAt = datetime($recorded)
757
+ RETURN c.id""",
758
+ {"provider": provider, "recorded": recorded},
759
+ )
760
+ )
761
+ for entry in accounts:
762
+ if not isinstance(entry, dict):
763
+ raise ContractError("each account must be an object")
764
+ email = required_string(entry.get("email"), "accounts[].email")
765
+ status = str(entry.get("status") or "authorized")
766
+ if status not in ("authorized", "revoked"):
767
+ raise ContractError(f"unsupported account status: {status}")
768
+ account_id = f"{provider}:{email.lower()}"
769
+ queries.append(
770
+ query(
771
+ """MERGE (a:SourceAccount {id: $id})
772
+ SET a.email = $email,
773
+ a.provider = $provider,
774
+ a.status = $status,
775
+ a.updatedAt = datetime($recorded)
776
+ FOREACH (_ IN CASE WHEN $status = 'authorized' AND a.authorizedAt IS NULL THEN [1] ELSE [] END |
777
+ SET a.authorizedAt = datetime($recorded)
778
+ )
779
+ WITH a
780
+ MATCH (c:Connector {id: $provider})
781
+ MERGE (a)-[:ON]->(c)
782
+ WITH a
783
+ OPTIONAL MATCH (s:IngestSource {id: $sourceId})
784
+ FOREACH (_ IN CASE WHEN s IS NULL OR $sourceId = '' THEN [] ELSE [1] END |
785
+ MERGE (s)-[v:VIA_ACCOUNT]->(a)
786
+ SET v.recordedAt = datetime($recorded)
787
+ )
788
+ RETURN a.id""",
789
+ {
790
+ "id": account_id,
791
+ "email": email,
792
+ "provider": provider,
793
+ "status": status,
794
+ "recorded": recorded,
795
+ "sourceId": source_id,
796
+ },
797
+ )
798
+ )
799
+ return {
800
+ "schema": PLAN_SCHEMA,
801
+ "schema_family": "connector-lifecycle",
802
+ "manifest_id": f"connector:{provider}",
803
+ "source": {"kind": "connector", "id": provider},
804
+ "query_count": len(queries),
805
+ "queries": queries,
806
+ }
807
+
808
+
733
809
  def build_plan(value: dict[str, Any], path: Path) -> dict[str, Any]:
810
+ if connector_manifest(value):
811
+ return connector_plan(value, path)
734
812
  if corpus_manifest(value):
735
813
  return corpus_plan(value, path)
736
814
  return knowledge_plan(value, path)
@@ -232,6 +232,39 @@ class SelectionState:
232
232
  self.done.set()
233
233
  return self.result
234
234
 
235
+ def choose_connector(self, payload: dict) -> dict:
236
+ # A connector choice ends the local-selection flow. When the CLI
237
+ # pre-started an OAuth listener, the browser response carries its
238
+ # consent URL so THIS tab continues straight into Google's sign-in;
239
+ # otherwise the agent guides authorization from the terminal. No bytes
240
+ # are staged and no source contract is collected here.
241
+ with self.lock:
242
+ if self.done.is_set():
243
+ raise ValueError("selection already finished")
244
+ provider = str(payload.get("provider", "")).strip().lower()
245
+ if provider not in available_connectors():
246
+ raise ValueError(f"unknown connector: {provider or '(empty)'}")
247
+ redirect = connector_auth_urls().get(provider, "")
248
+ account = connector_accounts().get(provider, "")
249
+ self.result = {
250
+ "connector": provider,
251
+ "receipt_url": self.receipt_url,
252
+ }
253
+ if redirect:
254
+ self.result["auth"] = "in-browser"
255
+ elif account:
256
+ self.result["account"] = account
257
+ self.receipt = {
258
+ **self.result,
259
+ "status": "processing",
260
+ "message": "The agent is walking you through connecting this source in the terminal.",
261
+ }
262
+ self.done.set()
263
+ response = dict(self.result)
264
+ if redirect:
265
+ response["redirect"] = redirect
266
+ return response
267
+
235
268
  def finish_receipt(self, payload: dict) -> dict:
236
269
  with self.lock:
237
270
  if not self.done.is_set() or self.cancelled:
@@ -252,10 +285,53 @@ class SelectionState:
252
285
  shutil.rmtree(self.session_root, ignore_errors=True)
253
286
 
254
287
 
288
+ def available_connectors() -> list[str]:
289
+ # A connector is offered when its runtime shim ships with this instance.
290
+ # Authorization state is NOT checked here — the button's job is to route
291
+ # the user into the guided setup, which handles both fresh and connected.
292
+ here = Path(__file__).parent
293
+ providers = []
294
+ if (here / "connector-google.sh").exists():
295
+ providers.append("google")
296
+ if (here / "connector-notion.sh").exists():
297
+ providers.append("notion")
298
+ return providers
299
+
300
+
301
+ def _env_json_map(name: str) -> dict[str, str]:
302
+ try:
303
+ value = json.loads(os.environ.get(name, "") or "{}")
304
+ except json.JSONDecodeError:
305
+ return {}
306
+ if not isinstance(value, dict):
307
+ return {}
308
+ return {str(k): str(v) for k, v in value.items() if isinstance(v, (str, int))}
309
+
310
+
311
+ def connector_auth_urls() -> dict[str, str]:
312
+ # Consent URLs pre-fetched by the CLI (listener already running) so a
313
+ # connector click can redirect this same tab straight into sign-in.
314
+ # Only loopback/HTTPS URLs are accepted.
315
+ urls = {}
316
+ for provider, url in _env_json_map("EGREGORE_CONNECTOR_AUTH_URLS").items():
317
+ if url.startswith("https://") or url.startswith("http://127.0.0.1") or url.startswith("http://localhost"):
318
+ urls[provider] = url
319
+ return urls
320
+
321
+
322
+ def connector_accounts() -> dict[str, str]:
323
+ # Already-connected account per provider, for honest completion copy.
324
+ return _env_json_map("EGREGORE_CONNECTOR_ACCOUNTS")
325
+
326
+
255
327
  def handler_class(state: SelectionState, token: str, expected_origin: str, preference_file: Path | None = None):
256
328
  html_template = HTML_PATH.read_text(encoding="utf-8")
257
329
  design_css = DESIGN_CSS_PATH.read_text(encoding="utf-8")
258
- capabilities = {"pdftotext": bool(shutil.which("pdftotext"))}
330
+ capabilities = {
331
+ "pdftotext": bool(shutil.which("pdftotext")),
332
+ "connectors": available_connectors(),
333
+ "connector_accounts": connector_accounts(),
334
+ }
259
335
  preferences = load_preferences(preference_file or preferences_path())
260
336
  page = (
261
337
  html_template.replace("__EGREGORE_DESIGN_CSS__", design_css)
@@ -349,6 +425,10 @@ def handler_class(state: SelectionState, token: str, expected_origin: str, prefe
349
425
  result = state.confirm(self.read_json())
350
426
  self.send_json(200, result)
351
427
  return
428
+ if path == "/api/connector":
429
+ result = state.choose_connector(self.read_json())
430
+ self.send_json(200, result)
431
+ return
352
432
  if path == "/api/cancel":
353
433
  state.cancel()
354
434
  self.send_json(200, {"cancelled": True})
@@ -35,6 +35,7 @@ chmod 700 "$STATE_DIR" 2>/dev/null || true
35
35
  if [ -f "$SCRIPT_DIR/.env" ]; then
36
36
  EGREGORE_API_URL="${EGREGORE_API_URL:-$(grep '^EGREGORE_API_URL=' "$SCRIPT_DIR/.env" 2>/dev/null | cut -d'=' -f2- || true)}"
37
37
  EGREGORE_API_KEY="${EGREGORE_API_KEY:-$(grep '^EGREGORE_API_KEY=' "$SCRIPT_DIR/.env" 2>/dev/null | cut -d'=' -f2- || true)}"
38
+ SLACK_BOT_TOKEN="${SLACK_BOT_TOKEN:-$(grep '^SLACK_BOT_TOKEN=' "$SCRIPT_DIR/.env" 2>/dev/null | cut -d'=' -f2- || true)}"
38
39
  fi
39
40
  API_URL="${EGREGORE_API_URL:-$(jq -r '.api_url // empty' "$CONFIG" 2>/dev/null)}"
40
41
  API_KEY="${EGREGORE_API_KEY:-}"
@@ -178,6 +179,8 @@ write_plan() {
178
179
  }' "$path"
179
180
  }
180
181
 
182
+ # Channel-generic despite the name: reads any field from .egregore-state.json
183
+ # with egregore.json as the legacy fallback (telegram_*, slack_*, …).
181
184
  read_local_telegram_config() {
182
185
  local field="$1"
183
186
  local state_file="$SCRIPT_DIR/.egregore-state.json"
@@ -220,24 +223,38 @@ plan_local() {
220
223
  die "direct messages are unavailable in local mode; no group fallback was sent"
221
224
  fi
222
225
 
223
- local chat_id group_link destination
226
+ local chat_id group_link slack_channel deliveries
224
227
  chat_id="$(read_local_telegram_config telegram_chat_id)"
225
228
  group_link="$(read_local_telegram_config telegram_group_link)"
226
- [ -n "$chat_id" ] || [ -n "$group_link" ] ||
227
- die "no Telegram group is configured"
228
- destination="${group_link:-$chat_id}"
229
+ slack_channel="$(read_local_telegram_config slack_channel_id)"
230
+
231
+ deliveries="[]"
232
+ if [ -n "$chat_id" ] || [ -n "$group_link" ]; then
233
+ deliveries="$(printf '%s' "$deliveries" | jq -c \
234
+ --arg destination "${group_link:-$chat_id}" \
235
+ '. + [{channel:"telegram", destination:$destination, kind:"group"}]')"
236
+ fi
237
+ # Slack is local-direct (the org owns the app and token): plan it only when
238
+ # both halves of the config exist, so a half-configured Slack fails soft.
239
+ if [ -n "$slack_channel" ] && [ -n "${SLACK_BOT_TOKEN:-}" ]; then
240
+ deliveries="$(printf '%s' "$deliveries" | jq -c \
241
+ --arg destination "$slack_channel" \
242
+ '. + [{channel:"slack", destination:$destination, kind:"group"}]')"
243
+ fi
244
+ [ "$deliveries" != "[]" ] ||
245
+ die "no group notification channel is configured"
229
246
 
230
247
  local response
231
248
  response="$(jq -nc \
232
249
  --arg slug "$ORG_SLUG" \
233
250
  --arg org "$ORG_NAME" \
234
- --arg destination "$destination" \
251
+ --argjson deliveries "$deliveries" \
235
252
  '{
236
253
  status:"planned",
237
254
  org_slug:$slug,
238
255
  org_name:$org,
239
- channels:["telegram"],
240
- deliveries:[{channel:"telegram", destination:$destination, kind:"group"}],
256
+ channels:($deliveries | map(.channel)),
257
+ deliveries:$deliveries,
241
258
  no_fallback:true
242
259
  }')"
243
260
  write_plan "$response" "$kind" "$recipient" "$message"
@@ -342,11 +359,12 @@ dispatch_connected() {
342
359
  printf '%s\n' "$response"
343
360
  }
344
361
 
345
- dispatch_local() {
346
- local path="$1"
347
- local message destination chat_id group_link plan_slug plan_org response
348
- message="$(jq -r '.message' "$path")"
349
- destination="$(jq -r '.deliveries[0].destination' "$path")"
362
+ dispatch_local_telegram() {
363
+ local destination="$1"
364
+ local message="$2"
365
+ local plan_slug="$3"
366
+ local plan_org="$4"
367
+ local chat_id group_link
350
368
  case "$destination" in
351
369
  https://t.me/*)
352
370
  chat_id=""
@@ -357,9 +375,7 @@ dispatch_local() {
357
375
  group_link=""
358
376
  ;;
359
377
  esac
360
- plan_slug="$(jq -r '.org_slug' "$path")"
361
- plan_org="$(jq -r '.org_name' "$path")"
362
- response="$(curl -sS -X POST "${RELAY_URL}/api/notify/relay" \
378
+ curl -sS -X POST "${RELAY_URL}/api/notify/relay" \
363
379
  -H "Content-Type: application/json" \
364
380
  -d "$(jq -nc \
365
381
  --arg chat_id "$chat_id" \
@@ -368,12 +384,84 @@ dispatch_local() {
368
384
  --arg slug "$plan_slug" \
369
385
  --arg org_name "$plan_org" \
370
386
  '{chat_id:$chat_id,group_link:$group_link,message:$message,slug:$slug,org_name:$org_name}')" \
371
- --max-time 10)" || return 1
372
- printf '%s' "$response" | jq -e '.status == "sent"' >/dev/null 2>&1 || {
373
- printf '%s\n' "$response"
374
- return 1
387
+ --max-time 10
388
+ }
389
+
390
+ # Local-direct: the org owns the Slack app and token, so the message goes
391
+ # straight from this machine to Slack — never through the Egregore relay.
392
+ dispatch_local_slack() {
393
+ local destination="$1"
394
+ local message="$2"
395
+ if [ -z "${SLACK_BOT_TOKEN:-}" ]; then
396
+ jq -nc '{status:"error",detail:"SLACK_BOT_TOKEN is missing from .env"}'
397
+ return 0
398
+ fi
399
+ local response
400
+ response="$(curl -sS -X POST "https://slack.com/api/chat.postMessage" \
401
+ -H "Authorization: Bearer $SLACK_BOT_TOKEN" \
402
+ -H "Content-Type: application/json; charset=utf-8" \
403
+ -d "$(jq -nc \
404
+ --arg channel "$destination" \
405
+ --arg text "$message" \
406
+ '{channel:$channel,text:$text,unfurl_links:false}')" \
407
+ --max-time 10)" || {
408
+ jq -nc '{status:"error",detail:"Slack request failed"}'
409
+ return 0
375
410
  }
376
- printf '%s\n' "$response"
411
+ if printf '%s' "$response" | jq -e '.ok == true' >/dev/null 2>&1; then
412
+ jq -nc '{status:"sent"}'
413
+ else
414
+ jq -nc \
415
+ --arg detail "$(printf '%s' "$response" | jq -r '.error // "unknown"' 2>/dev/null || echo unknown)" \
416
+ '{status:"error",detail:$detail}'
417
+ fi
418
+ }
419
+
420
+ dispatch_local() {
421
+ local path="$1"
422
+ local message plan_slug plan_org count i channel destination result
423
+ message="$(jq -r '.message' "$path")"
424
+ plan_slug="$(jq -r '.org_slug' "$path")"
425
+ plan_org="$(jq -r '.org_name' "$path")"
426
+ count="$(jq -r '.deliveries | length' "$path")"
427
+
428
+ # One platform failing must not mask another succeeding (mirror of the
429
+ # server-side send_group semantics).
430
+ local results="{}"
431
+ i=0
432
+ while [ "$i" -lt "$count" ]; do
433
+ channel="$(jq -r ".deliveries[$i].channel" "$path")"
434
+ destination="$(jq -r ".deliveries[$i].destination" "$path")"
435
+ case "$channel" in
436
+ telegram)
437
+ result="$(dispatch_local_telegram "$destination" "$message" "$plan_slug" "$plan_org")" ||
438
+ result='{"status":"error","detail":"relay request failed"}'
439
+ printf '%s' "$result" | jq -e . >/dev/null 2>&1 ||
440
+ result='{"status":"error","detail":"relay returned an unreadable response"}'
441
+ ;;
442
+ slack)
443
+ result="$(dispatch_local_slack "$destination" "$message")"
444
+ ;;
445
+ *)
446
+ result='{"status":"error","detail":"Unsupported channel"}'
447
+ ;;
448
+ esac
449
+ results="$(printf '%s' "$results" | jq -c --arg ch "$channel" --argjson r "$result" '. + {($ch): $r}')"
450
+ i=$((i + 1))
451
+ done
452
+
453
+ local sent
454
+ sent="$(printf '%s' "$results" | jq -c '[to_entries[] | select(.value.status == "sent") | .key]')"
455
+ if [ "$sent" = "[]" ]; then
456
+ printf '%s\n' "$results"
457
+ return 1
458
+ fi
459
+ printf '%s' "$results" | jq -c --argjson sent "$sent" \
460
+ '{status:"sent", platforms:$sent}
461
+ + ([to_entries[] | select(.value.status != "sent")] as $f
462
+ | if ($f | length) > 0
463
+ then {failed: ($f | map({(.key): (.value.detail // "unknown")}) | add)}
464
+ else {} end)'
377
465
  }
378
466
 
379
467
  dispatch_plan() {
@@ -469,12 +557,14 @@ test_connection() {
469
557
  -H "Authorization: Bearer $API_KEY" \
470
558
  --max-time 10
471
559
  elif [ "$MODE" = "local" ]; then
472
- local group_link
560
+ local group_link slack_channel
473
561
  group_link="$(read_local_telegram_config telegram_group_link)"
474
- if [ -n "$group_link" ]; then
562
+ slack_channel="$(read_local_telegram_config slack_channel_id)"
563
+ if [ -n "$group_link" ] ||
564
+ { [ -n "$slack_channel" ] && [ -n "${SLACK_BOT_TOKEN:-}" ]; }; then
475
565
  echo '{"status":"configured","mode":"local"}'
476
566
  else
477
- echo '{"status":"offline","reason":"no_telegram_group"}'
567
+ echo '{"status":"offline","reason":"no_notification_channel"}'
478
568
  fi
479
569
  else
480
570
  echo '{"status":"offline","reason":"no_api_key"}'
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "source": "egregore-pi-meta-harness",
3
3
  "policy": "Pi-native lifecycle layer over the available OSS Codex skill corpus",
4
- "skillCount": 42,
4
+ "skillCount": 43,
5
5
  "skills": [
6
6
  "activity",
7
7
  "announce",
@@ -33,6 +33,7 @@
33
33
  "reflect",
34
34
  "review-pr",
35
35
  "save",
36
+ "slack-connect",
36
37
  "summon",
37
38
  "sync-repos",
38
39
  "telegram-connect",
@@ -84,6 +85,7 @@
84
85
  ".codex/skills/reflect/SKILL.md",
85
86
  ".codex/skills/review-pr/SKILL.md",
86
87
  ".codex/skills/save/SKILL.md",
88
+ ".codex/skills/slack-connect/SKILL.md",
87
89
  ".codex/skills/summon/SKILL.md",
88
90
  ".codex/skills/sync-repos/SKILL.md",
89
91
  ".codex/skills/telegram-connect/SKILL.md",