@particle-academy/agent-integrations 0.19.0 → 0.21.0

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.
Files changed (36) hide show
  1. package/dist/bridges/catalog.d.cts +150 -0
  2. package/dist/bridges/catalog.d.ts +150 -0
  3. package/dist/bridges/features.d.cts +108 -0
  4. package/dist/bridges/features.d.ts +108 -0
  5. package/dist/bridges-catalog.cjs +428 -0
  6. package/dist/bridges-catalog.cjs.map +1 -0
  7. package/dist/bridges-catalog.js +7 -0
  8. package/dist/bridges-catalog.js.map +1 -0
  9. package/dist/bridges-features.cjs +341 -0
  10. package/dist/bridges-features.cjs.map +1 -0
  11. package/dist/bridges-features.js +7 -0
  12. package/dist/bridges-features.js.map +1 -0
  13. package/dist/chunk-267JS64O.js +308 -0
  14. package/dist/chunk-267JS64O.js.map +1 -0
  15. package/dist/{chunk-CPNOF4HI.js → chunk-FUI7KXE7.js} +41 -18
  16. package/dist/chunk-FUI7KXE7.js.map +1 -0
  17. package/dist/{chunk-YEEOTIGY.js → chunk-HKQBG2HZ.js} +3 -3
  18. package/dist/{chunk-YEEOTIGY.js.map → chunk-HKQBG2HZ.js.map} +1 -1
  19. package/dist/chunk-XQZGB4FP.js +221 -0
  20. package/dist/chunk-XQZGB4FP.js.map +1 -0
  21. package/dist/components-shared-whiteboard.cjs +39 -16
  22. package/dist/components-shared-whiteboard.cjs.map +1 -1
  23. package/dist/components-shared-whiteboard.js +2 -2
  24. package/dist/index.cjs +558 -18
  25. package/dist/index.cjs.map +1 -1
  26. package/dist/index.d.cts +2 -0
  27. package/dist/index.d.ts +2 -0
  28. package/dist/index.js +9 -7
  29. package/dist/index.js.map +1 -1
  30. package/dist/sharing/index.d.cts +7 -1
  31. package/dist/sharing/index.d.ts +7 -1
  32. package/dist/sharing.cjs +39 -16
  33. package/dist/sharing.cjs.map +1 -1
  34. package/dist/sharing.js +1 -1
  35. package/package.json +36 -1
  36. package/dist/chunk-CPNOF4HI.js.map +0 -1
@@ -1,5 +1,5 @@
1
- import { ShareControls, AgentCursor, AgentActivityHighlight, AgentPanel } from './chunk-YEEOTIGY.js';
2
- import { createSessionDescriptor, attachSseRelay } from './chunk-CPNOF4HI.js';
1
+ import { ShareControls, AgentCursor, AgentActivityHighlight, AgentPanel } from './chunk-HKQBG2HZ.js';
2
+ import { createSessionDescriptor, attachSseRelay } from './chunk-FUI7KXE7.js';
3
3
  import { attachInProcess } from './chunk-AFUULW5E.js';
4
4
  import { registerWhiteboardBridge } from './chunk-3QJSOS7G.js';
5
5
  import './chunk-KJ5AOOV7.js';
package/dist/index.cjs CHANGED
@@ -2431,6 +2431,521 @@ function registerNavigationBridge(host, options) {
2431
2431
  };
2432
2432
  }
2433
2433
 
2434
+ // src/bridges/catalog.ts
2435
+ var DEFAULT_AGENT10 = { id: "agent", name: "Agent", color: "#a855f7" };
2436
+ var str2 = (v, fallback = "") => typeof v === "string" ? v : fallback;
2437
+ var num2 = (v, fallback = 0) => typeof v === "number" && Number.isFinite(v) ? v : fallback;
2438
+ function registerCatalogBridge(host, options) {
2439
+ const { adapter } = options;
2440
+ const agent = { ...DEFAULT_AGENT10, ...options.agent ?? {} };
2441
+ const pendingMode = options.pendingMode ?? true;
2442
+ const catalogId = adapter.id ?? "catalog";
2443
+ const disposers = [];
2444
+ ensureUndoToolsRegistered(host, { defaultAgentId: agent.id });
2445
+ const target = (elementId, label) => ({
2446
+ kind: "custom",
2447
+ screenId: adapter.screenId,
2448
+ elementId,
2449
+ label: label ?? catalogId
2450
+ });
2451
+ const reg = (name, description, properties, required, handler, resolveTarget) => {
2452
+ const wrapped = async (args) => {
2453
+ try {
2454
+ return await handler(args);
2455
+ } catch (e) {
2456
+ return errorResult(e instanceof Error ? e.message : String(e));
2457
+ }
2458
+ };
2459
+ const final = resolveTarget ? wrapToolWithActivity(wrapped, {
2460
+ toolName: name,
2461
+ agent,
2462
+ kind: "custom",
2463
+ screenId: adapter.screenId,
2464
+ resolveTarget: ({ args }) => resolveTarget(args)
2465
+ }) : wrapped;
2466
+ disposers.push(
2467
+ host.registerTool(
2468
+ { name, description, inputSchema: { type: "object", properties, required, additionalProperties: false } },
2469
+ final
2470
+ )
2471
+ );
2472
+ };
2473
+ const guardDestructive = async (action, id, label, hasConfirmArg) => {
2474
+ if (!pendingMode) return null;
2475
+ if (options.confirm) {
2476
+ const ok = await options.confirm({ action, id, label });
2477
+ return ok ? null : errorResult(`Declined: ${action} ${id} (human did not confirm).`);
2478
+ }
2479
+ if (!hasConfirmArg) {
2480
+ return errorResult(`${action} is staged (pendingMode). Re-call with confirm:true to apply, or wire a host confirm hook.`);
2481
+ }
2482
+ return null;
2483
+ };
2484
+ reg(
2485
+ "catalog_list_products",
2486
+ "List products: { id, name, active, lookupKey, priceCount }. Pass withTrashed:true to include soft-deleted.",
2487
+ { withTrashed: { type: "boolean" } },
2488
+ [],
2489
+ async (args) => {
2490
+ const products = await adapter.products.all({ withTrashed: args.withTrashed === true });
2491
+ const list = await Promise.all(
2492
+ products.map(async (p) => ({
2493
+ id: p.id,
2494
+ name: p.name,
2495
+ active: p.active ?? true,
2496
+ lookupKey: p.lookupKey ?? null,
2497
+ priceCount: (await adapter.prices.forProduct(p.id)).length
2498
+ }))
2499
+ );
2500
+ return textResult(JSON.stringify(list, null, 2), list);
2501
+ },
2502
+ false
2503
+ );
2504
+ reg(
2505
+ "catalog_get_product",
2506
+ "Read a product's full JSON plus its prices.",
2507
+ { id: { type: "string" }, withTrashed: { type: "boolean" } },
2508
+ ["id"],
2509
+ async (args) => {
2510
+ const id = str2(args.id);
2511
+ const product = await adapter.products.find(id, { withTrashed: args.withTrashed === true });
2512
+ if (!product) return errorResult(`No product with id ${id}`);
2513
+ const prices = await adapter.prices.forProduct(id, { withTrashed: args.withTrashed === true });
2514
+ const out = { product, prices };
2515
+ return textResult(JSON.stringify(out, null, 2), out);
2516
+ },
2517
+ false
2518
+ );
2519
+ reg(
2520
+ "catalog_list_prices",
2521
+ "List prices for a product (pass productId), or every price (omit it).",
2522
+ { productId: { type: "string" }, withTrashed: { type: "boolean" } },
2523
+ [],
2524
+ async (args) => {
2525
+ const withTrashed = args.withTrashed === true;
2526
+ const prices = args.productId !== void 0 ? await adapter.prices.forProduct(str2(args.productId), { withTrashed }) : await adapter.prices.all({ withTrashed });
2527
+ const list = prices.map((p) => ({
2528
+ id: p.id,
2529
+ productId: p.productId,
2530
+ currency: p.currency,
2531
+ unitAmount: p.unitAmount,
2532
+ type: p.type,
2533
+ active: p.active ?? true,
2534
+ externalId: p.externalId ?? null
2535
+ }));
2536
+ return textResult(JSON.stringify(list, null, 2), list);
2537
+ },
2538
+ false
2539
+ );
2540
+ reg(
2541
+ "catalog_create_product",
2542
+ "Create a product. `name` is required; id (ULID) is auto-assigned. Returns the created product.",
2543
+ {
2544
+ name: { type: "string" },
2545
+ description: { type: "string" },
2546
+ active: { type: "boolean" },
2547
+ lookupKey: { type: "string" },
2548
+ metadata: { type: "object" }
2549
+ },
2550
+ ["name"],
2551
+ async (args) => {
2552
+ const name = str2(args.name);
2553
+ if (!name) return errorResult("name is required.");
2554
+ const product = await adapter.createProduct({
2555
+ name,
2556
+ ...args.description !== void 0 ? { description: str2(args.description) } : {},
2557
+ ...args.active !== void 0 ? { active: args.active === true } : {},
2558
+ ...args.lookupKey !== void 0 ? { lookupKey: str2(args.lookupKey) } : {},
2559
+ ...args.metadata && typeof args.metadata === "object" ? { metadata: args.metadata } : {}
2560
+ });
2561
+ fancyAutoCommon.pushUndoEntry(agent.id, {
2562
+ timestamp: Date.now(),
2563
+ bridgeId: catalogId,
2564
+ action: "catalog_create_product",
2565
+ label: `Created product ${product.id} (${product.name})`,
2566
+ undo: () => void adapter.products.remove(product.id),
2567
+ redo: () => void adapter.createProduct({ ...product })
2568
+ });
2569
+ return textResult(`Created product ${product.id} (${product.name})`, product);
2570
+ },
2571
+ (args) => target(void 0, `create product ${str2(args.name)}`)
2572
+ );
2573
+ reg(
2574
+ "catalog_create_price",
2575
+ "Create a price under a product. unitAmount is integer minor units (cents). type is 'recurring' | 'one_time'.",
2576
+ {
2577
+ productId: { type: "string" },
2578
+ currency: { type: "string", description: "ISO-4217, e.g. 'USD'." },
2579
+ unitAmount: { type: "number", description: "Price in cents." },
2580
+ type: { type: "string", enum: ["recurring", "one_time"] },
2581
+ recurringInterval: { type: "string", description: "'month' | 'year' | \u2026 (recurring only)." },
2582
+ nickname: { type: "string" },
2583
+ lookupKey: { type: "string" },
2584
+ metadata: { type: "object" }
2585
+ },
2586
+ ["productId", "currency", "unitAmount", "type"],
2587
+ async (args) => {
2588
+ const productId = str2(args.productId);
2589
+ const product = await adapter.products.find(productId);
2590
+ if (!product) return errorResult(`No product with id ${productId}`);
2591
+ const type = str2(args.type);
2592
+ if (type !== "recurring" && type !== "one_time") return errorResult("type must be 'recurring' or 'one_time'.");
2593
+ const price = await adapter.createPrice({
2594
+ productId,
2595
+ currency: str2(args.currency),
2596
+ unitAmount: num2(args.unitAmount),
2597
+ type,
2598
+ ...args.recurringInterval !== void 0 ? { recurringInterval: str2(args.recurringInterval) } : {},
2599
+ ...args.nickname !== void 0 ? { nickname: str2(args.nickname) } : {},
2600
+ ...args.lookupKey !== void 0 ? { lookupKey: str2(args.lookupKey) } : {},
2601
+ ...args.metadata && typeof args.metadata === "object" ? { metadata: args.metadata } : {}
2602
+ });
2603
+ fancyAutoCommon.pushUndoEntry(agent.id, {
2604
+ timestamp: Date.now(),
2605
+ bridgeId: catalogId,
2606
+ action: "catalog_create_price",
2607
+ label: `Created price ${price.id} on ${productId}`,
2608
+ undo: () => void adapter.prices.remove(price.id),
2609
+ redo: () => void adapter.createPrice({ ...price })
2610
+ });
2611
+ return textResult(`Created ${type} price ${price.id} on ${productId}`, price);
2612
+ },
2613
+ (args) => target(str2(args.productId), `create price on ${str2(args.productId)}`)
2614
+ );
2615
+ reg(
2616
+ "catalog_delete_product",
2617
+ "Soft-delete a product (preserves financial history). Staged in pendingMode \u2014 pass confirm:true or wire a host confirm hook.",
2618
+ { id: { type: "string" }, confirm: { type: "boolean" } },
2619
+ ["id"],
2620
+ async (args) => {
2621
+ const id = str2(args.id);
2622
+ const product = await adapter.products.find(id, { withTrashed: true });
2623
+ if (!product) return errorResult(`No product with id ${id}`);
2624
+ const blocked = await guardDestructive("catalog_delete_product", id, product.name, args.confirm === true);
2625
+ if (blocked) return blocked;
2626
+ const snapshot = { ...product };
2627
+ await adapter.products.remove(id);
2628
+ fancyAutoCommon.pushUndoEntry(agent.id, {
2629
+ timestamp: Date.now(),
2630
+ bridgeId: catalogId,
2631
+ action: "catalog_delete_product",
2632
+ label: `Deleted product ${id} (${product.name})`,
2633
+ undo: () => void adapter.createProduct({ ...snapshot }),
2634
+ redo: () => void adapter.products.remove(id)
2635
+ });
2636
+ return textResult(`Deleted product ${id}`, { id });
2637
+ },
2638
+ (args) => target(str2(args.id), `delete product ${str2(args.id)}`)
2639
+ );
2640
+ reg(
2641
+ "catalog_delete_price",
2642
+ "Soft-delete a price (mirrors Stripe archiving). Staged in pendingMode \u2014 pass confirm:true or wire a host confirm hook.",
2643
+ { id: { type: "string" }, confirm: { type: "boolean" } },
2644
+ ["id"],
2645
+ async (args) => {
2646
+ const id = str2(args.id);
2647
+ const price = await adapter.prices.find(id, { withTrashed: true });
2648
+ if (!price) return errorResult(`No price with id ${id}`);
2649
+ const blocked = await guardDestructive("catalog_delete_price", id, id, args.confirm === true);
2650
+ if (blocked) return blocked;
2651
+ const snapshot = { ...price };
2652
+ await adapter.prices.remove(id);
2653
+ fancyAutoCommon.pushUndoEntry(agent.id, {
2654
+ timestamp: Date.now(),
2655
+ bridgeId: catalogId,
2656
+ action: "catalog_delete_price",
2657
+ label: `Deleted price ${id}`,
2658
+ undo: () => void adapter.createPrice({ ...snapshot }),
2659
+ redo: () => void adapter.prices.remove(id)
2660
+ });
2661
+ return textResult(`Deleted price ${id}`, { id });
2662
+ },
2663
+ (args) => target(str2(args.id), `delete price ${str2(args.id)}`)
2664
+ );
2665
+ reg(
2666
+ "catalog_sync_product",
2667
+ "Push a product + its prices to Stripe (creates/updates the Stripe Product + Prices, stamps external ids).",
2668
+ { id: { type: "string" } },
2669
+ ["id"],
2670
+ async (args) => {
2671
+ if (!adapter.syncProductAndPrices) return errorResult("Host did not wire Stripe sync.");
2672
+ const id = str2(args.id);
2673
+ const product = await adapter.products.find(id);
2674
+ if (!product) return errorResult(`No product with id ${id}`);
2675
+ const synced = await adapter.syncProductAndPrices(product);
2676
+ return textResult(`Synced product ${id} to Stripe (${synced.externalId ?? "no external id"})`, synced);
2677
+ },
2678
+ (args) => target(str2(args.id), `sync product ${str2(args.id)}`)
2679
+ );
2680
+ reg(
2681
+ "catalog_test_connection",
2682
+ "Verify the Stripe connection \u2014 returns { success, message, productCount? }.",
2683
+ {},
2684
+ [],
2685
+ async () => {
2686
+ if (!adapter.testConnection) return errorResult("Host did not wire a Stripe connection test.");
2687
+ const result = await adapter.testConnection();
2688
+ return textResult(JSON.stringify(result, null, 2), result);
2689
+ },
2690
+ false
2691
+ );
2692
+ reg(
2693
+ "catalog_create_checkout",
2694
+ "Build a hosted Stripe Checkout URL for a price. Recurring prices \u2192 subscription; one-time \u2192 payment (pass quantity). Requires the price be synced to Stripe first.",
2695
+ {
2696
+ priceId: { type: "string" },
2697
+ successUrl: { type: "string" },
2698
+ cancelUrl: { type: "string" },
2699
+ customer: { type: "string", description: "Stripe customer id (optional \u2014 Checkout collects one if omitted)." },
2700
+ quantity: { type: "number", description: "One-time checkouts only. Default 1." },
2701
+ metadata: { type: "object" }
2702
+ },
2703
+ ["priceId", "successUrl", "cancelUrl"],
2704
+ async (args) => {
2705
+ const priceId = str2(args.priceId);
2706
+ const price = await adapter.prices.find(priceId);
2707
+ if (!price) return errorResult(`No price with id ${priceId}`);
2708
+ const checkoutArgs = {
2709
+ successUrl: str2(args.successUrl),
2710
+ cancelUrl: str2(args.cancelUrl),
2711
+ ...args.customer !== void 0 ? { customer: str2(args.customer) } : {},
2712
+ ...args.metadata && typeof args.metadata === "object" ? { metadata: args.metadata } : {}
2713
+ };
2714
+ let url;
2715
+ if (price.type === "recurring") {
2716
+ if (!adapter.getSubscriptionCheckoutUrl) return errorResult("Host did not wire subscription checkout.");
2717
+ url = await adapter.getSubscriptionCheckoutUrl(price, checkoutArgs);
2718
+ } else {
2719
+ if (!adapter.getOneTimeCheckoutUrl) return errorResult("Host did not wire one-time checkout.");
2720
+ url = await adapter.getOneTimeCheckoutUrl(price, { ...checkoutArgs, quantity: num2(args.quantity, 1) });
2721
+ }
2722
+ return textResult(url || "(no url)", { priceId, type: price.type, url });
2723
+ },
2724
+ (args) => target(str2(args.priceId), `checkout ${str2(args.priceId)}`)
2725
+ );
2726
+ return {
2727
+ id: `catalog:${catalogId}`,
2728
+ title: adapter.title ?? "Catalog",
2729
+ dispose: () => {
2730
+ for (const d of disposers.splice(0)) d();
2731
+ }
2732
+ };
2733
+ }
2734
+
2735
+ // src/bridges/features.ts
2736
+ var DEFAULT_AGENT11 = { id: "agent", name: "Agent", color: "#a855f7" };
2737
+ var str3 = (v, fallback = "") => typeof v === "string" ? v : fallback;
2738
+ var num3 = (v, fallback = 0) => typeof v === "number" && Number.isFinite(v) ? v : fallback;
2739
+ function registerFeaturesBridge(host, options) {
2740
+ const { adapter } = options;
2741
+ const agent = { ...DEFAULT_AGENT11, ...options.agent ?? {} };
2742
+ const pendingMode = options.pendingMode ?? true;
2743
+ const featuresId = adapter.id ?? "features";
2744
+ const disposers = [];
2745
+ ensureUndoToolsRegistered(host, { defaultAgentId: agent.id });
2746
+ const target = (elementId, label) => ({
2747
+ kind: "custom",
2748
+ screenId: adapter.screenId,
2749
+ elementId,
2750
+ label: label ?? featuresId
2751
+ });
2752
+ const reg = (name, description, properties, required, handler, resolveTarget) => {
2753
+ const wrapped = async (args) => {
2754
+ try {
2755
+ return await handler(args);
2756
+ } catch (e) {
2757
+ return errorResult(e instanceof Error ? e.message : String(e));
2758
+ }
2759
+ };
2760
+ const final = resolveTarget ? wrapToolWithActivity(wrapped, {
2761
+ toolName: name,
2762
+ agent,
2763
+ kind: "custom",
2764
+ screenId: adapter.screenId,
2765
+ resolveTarget: ({ args }) => resolveTarget(args)
2766
+ }) : wrapped;
2767
+ disposers.push(
2768
+ host.registerTool(
2769
+ { name, description, inputSchema: { type: "object", properties, required, additionalProperties: false } },
2770
+ final
2771
+ )
2772
+ );
2773
+ };
2774
+ const guardGrant = async (action, subject, groupKey, hasConfirmArg) => {
2775
+ if (!pendingMode) return null;
2776
+ if (options.confirm) {
2777
+ const ok = await options.confirm({ action, subject, groupKey });
2778
+ return ok ? null : errorResult(`Declined: ${action} ${groupKey} (human did not confirm).`);
2779
+ }
2780
+ if (!hasConfirmArg) {
2781
+ return errorResult(`${action} is staged (pendingMode). Re-call with confirm:true to apply, or wire a host confirm hook.`);
2782
+ }
2783
+ return null;
2784
+ };
2785
+ reg(
2786
+ "features_list",
2787
+ "List the feature keys enabled for a subject. Pass `subject` (a string id or object).",
2788
+ { subject: { description: "Opaque subject \u2014 string id or { id, \u2026 } object." }, context: { description: "Optional resolution context." } },
2789
+ [],
2790
+ async (args) => {
2791
+ const keys = await adapter.enabled(args.subject, args.context);
2792
+ return textResult(JSON.stringify(keys, null, 2), { subject: args.subject, enabled: keys });
2793
+ },
2794
+ false
2795
+ );
2796
+ reg(
2797
+ "features_check",
2798
+ "Check whether a subject can access a feature. For resource features also returns the remaining quota.",
2799
+ { feature: { type: "string" }, subject: { description: "Opaque subject." }, context: { description: "Optional context." } },
2800
+ ["feature"],
2801
+ async (args) => {
2802
+ const feature = str3(args.feature);
2803
+ const allowed = await adapter.canAccess(feature, args.subject, args.context);
2804
+ const remaining = await adapter.remaining(feature, args.subject, args.context);
2805
+ const out = { feature, allowed, remaining };
2806
+ return textResult(JSON.stringify(out), out);
2807
+ },
2808
+ false
2809
+ );
2810
+ reg(
2811
+ "features_explain",
2812
+ "Trace why a feature is on/off for a subject \u2014 returns the AccessResult (source, remaining, limit, used).",
2813
+ { feature: { type: "string" }, subject: { description: "Opaque subject." }, context: { description: "Optional context." } },
2814
+ ["feature"],
2815
+ async (args) => {
2816
+ if (!adapter.explain) return errorResult("Host did not wire explain().");
2817
+ const result = await adapter.explain(str3(args.feature), args.subject, args.context);
2818
+ return textResult(JSON.stringify(result, null, 2), result);
2819
+ },
2820
+ false
2821
+ );
2822
+ reg(
2823
+ "features_groups",
2824
+ "List the feature groups a subject is assigned to.",
2825
+ { subject: { description: "Opaque subject." } },
2826
+ ["subject"],
2827
+ async (args) => {
2828
+ const groups = await adapter.listGroups(args.subject);
2829
+ return textResult(JSON.stringify(groups, null, 2), { subject: args.subject, groups });
2830
+ },
2831
+ false
2832
+ );
2833
+ reg(
2834
+ "features_define",
2835
+ "Register a feature definition. type 'boolean' (on/off) or 'resource' (metered with a `limit`). `enabled` sets the default gate.",
2836
+ {
2837
+ key: { type: "string" },
2838
+ name: { type: "string" },
2839
+ description: { type: "string" },
2840
+ type: { type: "string", enum: ["boolean", "resource"] },
2841
+ enabled: { type: "boolean" },
2842
+ limit: { type: "number", description: "Resource quota per period (resource type)." }
2843
+ },
2844
+ ["key"],
2845
+ (args) => {
2846
+ const key = str3(args.key);
2847
+ if (!key) return errorResult("key is required.");
2848
+ const existed = adapter.registryKeys().includes(key);
2849
+ const definition = {
2850
+ ...args.name !== void 0 ? { name: str3(args.name) } : {},
2851
+ ...args.description !== void 0 ? { description: str3(args.description) } : {},
2852
+ ...args.type !== void 0 ? { type: str3(args.type) } : {},
2853
+ ...args.enabled !== void 0 ? { enabled: args.enabled === true } : {},
2854
+ ...args.limit !== void 0 ? { limit: num3(args.limit) } : {}
2855
+ };
2856
+ adapter.registerFeature(key, definition);
2857
+ fancyAutoCommon.pushUndoEntry(agent.id, {
2858
+ timestamp: Date.now(),
2859
+ bridgeId: featuresId,
2860
+ action: "features_define",
2861
+ // Registry has no unregister; undo re-registers the prior definition when we had one.
2862
+ label: existed ? `Redefined feature ${key}` : `Defined feature ${key}`,
2863
+ undo: () => {
2864
+ },
2865
+ redo: () => adapter.registerFeature(key, definition)
2866
+ });
2867
+ return textResult(`${existed ? "Redefined" : "Defined"} feature ${key} (${definition.type ?? "boolean"})`, { key, definition });
2868
+ },
2869
+ (args) => target(str3(args.key), `define ${str3(args.key)}`)
2870
+ );
2871
+ reg(
2872
+ "features_grant",
2873
+ "Grant a subject access by assigning it to a feature group. Staged in pendingMode \u2014 pass confirm:true or wire a host confirm hook.",
2874
+ { subject: { description: "Opaque subject." }, group: { type: "string", description: "Feature group key." }, confirm: { type: "boolean" } },
2875
+ ["subject", "group"],
2876
+ async (args) => {
2877
+ const groupKey = str3(args.group);
2878
+ if (!groupKey) return errorResult("group is required.");
2879
+ const blocked = await guardGrant("features_grant", args.subject, groupKey, args.confirm === true);
2880
+ if (blocked) return blocked;
2881
+ const already = (await adapter.listGroups(args.subject)).includes(groupKey);
2882
+ await adapter.assignGroup(args.subject, groupKey);
2883
+ fancyAutoCommon.pushUndoEntry(agent.id, {
2884
+ timestamp: Date.now(),
2885
+ bridgeId: featuresId,
2886
+ action: "features_grant",
2887
+ label: `Granted group ${groupKey}`,
2888
+ // Only reverse if WE added it (idempotent assign — don't revoke a pre-existing grant).
2889
+ undo: () => {
2890
+ if (!already) void adapter.detachGroup(args.subject, groupKey);
2891
+ },
2892
+ redo: () => void adapter.assignGroup(args.subject, groupKey)
2893
+ });
2894
+ return textResult(`Granted group ${groupKey}`, { subject: args.subject, group: groupKey });
2895
+ },
2896
+ (args) => target(str3(args.group), `grant ${str3(args.group)}`)
2897
+ );
2898
+ reg(
2899
+ "features_revoke",
2900
+ "Revoke access by detaching a subject from a feature group. Staged in pendingMode \u2014 pass confirm:true or wire a host confirm hook.",
2901
+ { subject: { description: "Opaque subject." }, group: { type: "string" }, confirm: { type: "boolean" } },
2902
+ ["subject", "group"],
2903
+ async (args) => {
2904
+ const groupKey = str3(args.group);
2905
+ if (!groupKey) return errorResult("group is required.");
2906
+ const blocked = await guardGrant("features_revoke", args.subject, groupKey, args.confirm === true);
2907
+ if (blocked) return blocked;
2908
+ const had = (await adapter.listGroups(args.subject)).includes(groupKey);
2909
+ await adapter.detachGroup(args.subject, groupKey);
2910
+ fancyAutoCommon.pushUndoEntry(agent.id, {
2911
+ timestamp: Date.now(),
2912
+ bridgeId: featuresId,
2913
+ action: "features_revoke",
2914
+ label: `Revoked group ${groupKey}`,
2915
+ undo: () => {
2916
+ if (had) void adapter.assignGroup(args.subject, groupKey);
2917
+ },
2918
+ redo: () => void adapter.detachGroup(args.subject, groupKey)
2919
+ });
2920
+ return textResult(`Revoked group ${groupKey}`, { subject: args.subject, group: groupKey });
2921
+ },
2922
+ (args) => target(str3(args.group), `revoke ${str3(args.group)}`)
2923
+ );
2924
+ reg(
2925
+ "features_consume",
2926
+ "Meter a resource feature: atomic check-and-increment. Returns ok:false if the quota would be exceeded (nothing recorded).",
2927
+ { feature: { type: "string" }, subject: { description: "Opaque subject." }, amount: { type: "number", description: "Units to consume. Default 1." }, context: { description: "Optional context." } },
2928
+ ["feature", "subject"],
2929
+ async (args) => {
2930
+ if (!adapter.tryConsume) return errorResult("Host did not wire usage metering (tryConsume).");
2931
+ const feature = str3(args.feature);
2932
+ const amount = num3(args.amount, 1);
2933
+ const ok = await adapter.tryConsume(feature, args.subject, amount, args.context);
2934
+ const remaining = await adapter.remaining(feature, args.subject, args.context);
2935
+ const out = { feature, ok, amount, remaining };
2936
+ return textResult(JSON.stringify(out), out);
2937
+ },
2938
+ (args) => target(str3(args.feature), `consume ${str3(args.feature)}`)
2939
+ );
2940
+ return {
2941
+ id: `features:${featuresId}`,
2942
+ title: adapter.title ?? "Features",
2943
+ dispose: () => {
2944
+ for (const d of disposers.splice(0)) d();
2945
+ }
2946
+ };
2947
+ }
2948
+
2434
2949
  // src/sharing/token.ts
2435
2950
  var TOKEN_BYTES = 24;
2436
2951
  function createSessionDescriptor() {
@@ -2490,6 +3005,7 @@ function constantTimeEqual(a, b) {
2490
3005
  // src/sharing/sse-relay.ts
2491
3006
  var SseRelayTransport = class {
2492
3007
  constructor(options) {
3008
+ this.disposed = false;
2493
3009
  this.sendQueue = [];
2494
3010
  this.connected = false;
2495
3011
  this.listeners = /* @__PURE__ */ new Set();
@@ -2500,26 +3016,45 @@ var SseRelayTransport = class {
2500
3016
  bindServer(server) {
2501
3017
  this.server = server;
2502
3018
  }
2503
- /** Open the SSE channel. Idempotent. */
3019
+ /** Open the receive channel. Idempotent. */
2504
3020
  start() {
2505
- if (this.connected || typeof window === "undefined") return;
2506
- const url = `${this.opts.baseUrl}/${encodeURIComponent(this.opts.sessionId)}/events?token=${encodeURIComponent(this.opts.token)}`;
3021
+ if (this.connected || this.disposed || typeof window === "undefined") return;
2507
3022
  this.setState("connecting");
3023
+ void import('@particle-academy/fancy-cf-relay').then(({ createRelayChannel }) => {
3024
+ if (this.connected || this.disposed) return;
3025
+ this.channel = createRelayChannel({
3026
+ baseUrl: this.opts.baseUrl,
3027
+ session: this.opts.sessionId,
3028
+ token: this.opts.token,
3029
+ transport: "auto",
3030
+ receiveDirection: "inbound",
3031
+ sendPath: "outbox",
3032
+ onFrame: (raw) => this.handleInbound(raw),
3033
+ onOpen: () => this.markOpen(),
3034
+ onError: () => this.setState("error"),
3035
+ fetchImpl: this.opts.fetch
3036
+ });
3037
+ this.channel.start();
3038
+ }).catch(() => {
3039
+ if (!this.disposed) this.startSse();
3040
+ });
3041
+ }
3042
+ /** Fallback receive leg: a plain EventSource (no fancy-cf-relay installed). */
3043
+ startSse() {
3044
+ if (this.connected || this.disposed) return;
3045
+ const url = `${this.opts.baseUrl}/${encodeURIComponent(this.opts.sessionId)}/events?token=${encodeURIComponent(this.opts.token)}`;
2508
3046
  const es = new EventSource(url, { withCredentials: false });
2509
3047
  this.es = es;
2510
- es.addEventListener("open", () => {
2511
- this.connected = true;
2512
- this.setState("open");
2513
- const queued = this.sendQueue.splice(0);
2514
- for (const msg of queued) this.postOut(msg);
2515
- });
2516
- es.addEventListener("mcp", (ev) => {
2517
- const raw = ev.data;
2518
- this.handleInbound(raw);
2519
- });
2520
- es.addEventListener("error", () => {
2521
- this.setState("error");
2522
- });
3048
+ es.addEventListener("open", () => this.markOpen());
3049
+ es.addEventListener("mcp", (ev) => this.handleInbound(ev.data));
3050
+ es.addEventListener("error", () => this.setState("error"));
3051
+ }
3052
+ /** Mark the channel live + flush any queued outbound frames. */
3053
+ markOpen() {
3054
+ this.connected = true;
3055
+ this.setState("open");
3056
+ const queued = this.sendQueue.splice(0);
3057
+ for (const msg of queued) this.postOut(msg);
2523
3058
  }
2524
3059
  send(message) {
2525
3060
  if (!this.connected) {
@@ -2529,6 +3064,9 @@ var SseRelayTransport = class {
2529
3064
  this.postOut(message);
2530
3065
  }
2531
3066
  close() {
3067
+ this.disposed = true;
3068
+ this.channel?.stop();
3069
+ this.channel = void 0;
2532
3070
  this.es?.close();
2533
3071
  this.es = void 0;
2534
3072
  this.connected = false;
@@ -2602,11 +3140,11 @@ function attachSseRelay(server, options) {
2602
3140
 
2603
3141
  // src/sharing/use-co-browse-session.ts
2604
3142
  init_registry();
2605
- var DEFAULT_AGENT10 = { id: "agent", name: "Agent", color: "#a855f7" };
3143
+ var DEFAULT_AGENT12 = { id: "agent", name: "Agent", color: "#a855f7" };
2606
3144
  var USER = { id: "human", name: "You" };
2607
3145
  function useCoBrowseSession(options) {
2608
3146
  const { adapter, extraBridges } = options;
2609
- const agent = { ...DEFAULT_AGENT10, ...options.agent ?? {} };
3147
+ const agent = { ...DEFAULT_AGENT12, ...options.agent ?? {} };
2610
3148
  const relayBaseUrl = options.relayBaseUrl ?? "/agent-relay";
2611
3149
  const serverRef = react.useRef(null);
2612
3150
  const relayRef = react.useRef(null);
@@ -3828,8 +4366,10 @@ exports.ensureUndoToolsRegistered = ensureUndoToolsRegistered;
3828
4366
  exports.errorResult = errorResult;
3829
4367
  exports.mapActivityToHeuristicsEvent = mapActivityToEvent;
3830
4368
  exports.readSessionFromUrl = readSessionFromUrl;
4369
+ exports.registerCatalogBridge = registerCatalogBridge;
3831
4370
  exports.registerChartsBridge = registerChartsBridge;
3832
4371
  exports.registerCodeBridge = registerCodeBridge;
4372
+ exports.registerFeaturesBridge = registerFeaturesBridge;
3833
4373
  exports.registerFormBridge = registerFormBridge;
3834
4374
  exports.registerNavigationBridge = registerNavigationBridge;
3835
4375
  exports.registerSceneBridge = registerSceneBridge;