@zackbart/connecta 0.7.6 → 0.7.8

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 (62) hide show
  1. package/CHANGELOG.md +99 -0
  2. package/dist/activity.d.ts +17 -0
  3. package/dist/activity.d.ts.map +1 -1
  4. package/dist/activity.js.map +1 -1
  5. package/dist/auth/clerk.d.ts.map +1 -1
  6. package/dist/auth/clerk.js +101 -0
  7. package/dist/auth/clerk.js.map +1 -1
  8. package/dist/auth/downstream-oauth.d.ts +57 -20
  9. package/dist/auth/downstream-oauth.d.ts.map +1 -1
  10. package/dist/auth/downstream-oauth.js +275 -67
  11. package/dist/auth/downstream-oauth.js.map +1 -1
  12. package/dist/catalog.d.ts +5 -3
  13. package/dist/catalog.d.ts.map +1 -1
  14. package/dist/catalog.js +13 -4
  15. package/dist/catalog.js.map +1 -1
  16. package/dist/connectors/remote-mcp.d.ts.map +1 -1
  17. package/dist/connectors/remote-mcp.js +165 -103
  18. package/dist/connectors/remote-mcp.js.map +1 -1
  19. package/dist/execute.d.ts.map +1 -1
  20. package/dist/execute.js +27 -15
  21. package/dist/execute.js.map +1 -1
  22. package/dist/index.d.ts +9 -2
  23. package/dist/index.d.ts.map +1 -1
  24. package/dist/index.js +1 -0
  25. package/dist/index.js.map +1 -1
  26. package/dist/meta-tools.d.ts +4 -4
  27. package/dist/meta-tools.d.ts.map +1 -1
  28. package/dist/meta-tools.js +85 -25
  29. package/dist/meta-tools.js.map +1 -1
  30. package/dist/registry.d.ts +24 -0
  31. package/dist/registry.d.ts.map +1 -1
  32. package/dist/registry.js +87 -17
  33. package/dist/registry.js.map +1 -1
  34. package/dist/server.d.ts.map +1 -1
  35. package/dist/server.js +238 -19
  36. package/dist/server.js.map +1 -1
  37. package/dist/storage/file.d.ts.map +1 -1
  38. package/dist/storage/file.js +8 -0
  39. package/dist/storage/file.js.map +1 -1
  40. package/dist/types.d.ts +21 -0
  41. package/dist/types.d.ts.map +1 -1
  42. package/dist/ui.d.ts +7 -1
  43. package/dist/ui.d.ts.map +1 -1
  44. package/dist/ui.js +118 -4
  45. package/dist/ui.js.map +1 -1
  46. package/dist/version.d.ts +1 -1
  47. package/dist/version.js +1 -1
  48. package/package.json +1 -1
  49. package/src/activity.ts +20 -0
  50. package/src/auth/clerk.ts +124 -0
  51. package/src/auth/downstream-oauth.ts +359 -68
  52. package/src/catalog.ts +24 -4
  53. package/src/connectors/remote-mcp.ts +172 -104
  54. package/src/execute.ts +29 -13
  55. package/src/index.ts +12 -1
  56. package/src/meta-tools.ts +89 -25
  57. package/src/registry.ts +115 -20
  58. package/src/server.ts +306 -16
  59. package/src/storage/file.ts +7 -0
  60. package/src/types.ts +23 -0
  61. package/src/ui.ts +124 -3
  62. package/src/version.ts +1 -1
@@ -393,7 +393,7 @@ interface ConnectionState {
393
393
  connecting: Promise<void> | null;
394
394
  authRequired: boolean;
395
395
  provider: KvOAuthProvider | null;
396
- connectedGeneration: number | null;
396
+ connectedGeneration: string | null;
397
397
  /**
398
398
  * One-way latch: set by closeScope and never cleared, so neither a late
399
399
  * connect nor a `reset()` can cache a client into a scope that is already
@@ -451,6 +451,16 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
451
451
  { cause },
452
452
  );
453
453
 
454
+ class OperatorDisconnectedError extends ConnectorCallError {
455
+ constructor() {
456
+ super(
457
+ "auth_required",
458
+ `Connector "${id}" was disconnected by an operator — explicitly start authorization to reconnect it.`,
459
+ );
460
+ }
461
+ }
462
+ const operatorDisconnectedError = () => new OperatorDisconnectedError();
463
+
454
464
  const scopeEndedError = () =>
455
465
  new Error(`Connector "${id}" scope ended during connection.`);
456
466
 
@@ -518,16 +528,23 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
518
528
  return state.provider;
519
529
  };
520
530
 
531
+ const newProvider = (ctx: ConnectorContext): KvOAuthProvider =>
532
+ new KvOAuthProvider(
533
+ id,
534
+ ctx.storage,
535
+ `${ctx.baseUrl}/oauth/callback/${id}`,
536
+ );
537
+
521
538
  const buildTransport = (
522
539
  ctx: ConnectorContext,
523
- state: ConnectionState,
540
+ provider: KvOAuthProvider | null,
524
541
  ): Transport => {
525
542
  if (opts._transportFactory) return opts._transportFactory(ctx);
526
543
  const url = new URL(opts.url);
527
544
  const guardedFetch = redirectSafeFetch(id, opts.redirects);
528
545
  if (opts.auth?.type === "oauth") {
529
546
  return new StreamableHTTPClientTransport(url, {
530
- authProvider: getProvider(ctx, state),
547
+ authProvider: provider ?? newProvider(ctx),
531
548
  fetch: guardedFetch,
532
549
  });
533
550
  }
@@ -547,6 +564,7 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
547
564
  state.transport = null;
548
565
  state.connecting = null;
549
566
  state.authRequired = false;
567
+ state.provider = null;
550
568
  state.connectedGeneration = null;
551
569
  // `closed` is deliberately not cleared — see ConnectionState.
552
570
  };
@@ -563,97 +581,160 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
563
581
  new UnauthorizedError("Downstream authorization is no longer valid."),
564
582
  );
565
583
  }
584
+ // Read the OAuth epoch before trusting either a cached client or starting a
585
+ // transport. A disconnected epoch is a durable operator instruction, not
586
+ // merely the absence of credentials: passive status/tool probes must not
587
+ // turn it back into a pending consent flow.
588
+ let oauthGeneration: string | undefined;
589
+ if (isOauth && (state.client || state.connecting)) {
590
+ const provider = getProvider(ctx, state);
591
+ oauthGeneration = await provider.generation();
592
+ if (provider.isOperatorDisconnectedGeneration(oauthGeneration)) {
593
+ const connecting = state.connecting;
594
+ const client = state.client;
595
+ const transport = state.transport;
596
+ reset(state);
597
+ void connecting?.catch(() => {});
598
+ try {
599
+ if (client) await client.close();
600
+ else await transport?.close();
601
+ } catch {
602
+ // The disconnected epoch is authoritative even if local close fails.
603
+ }
604
+ throw operatorDisconnectedError();
605
+ }
606
+ }
566
607
  // Cross-isolate force re-auth: another isolate bumped the KV generation and
567
608
  // wiped credentials. This request's cached client still speaks the old
568
609
  // token — drop it so the next connect runs against current state.
569
- if (state.client && isOauth && state.connectedGeneration !== null) {
570
- const generation = await getProvider(ctx, state).generation();
610
+ if (state.client && oauthGeneration !== undefined && state.connectedGeneration !== null) {
571
611
  if (state.closed) throw scopeEndedError();
572
- if (generation !== state.connectedGeneration) {
612
+ if (oauthGeneration !== state.connectedGeneration) {
573
613
  reset(state);
574
614
  }
575
615
  }
576
616
  if (state.closed) throw scopeEndedError();
577
617
  if (state.client) return;
578
- state.connecting ??= (async () => {
579
- const provider = isOauth ? getProvider(ctx, state) : null;
580
- const genAtStart = provider ? await provider.generation() : 0;
581
- if (state.closed) throw scopeEndedError();
582
- // Stamp the provider so any saveTokens/saveClientInformation the SDK fires
583
- // during this connect (code exchange, DCR) or during a later refresh on
584
- // the resulting client — is dropped if a concurrent force bumps the
585
- // generation past this point, instead of re-persisting wiped credentials.
586
- provider?.captureGeneration(genAtStart);
587
- // The SDK defaults to AJV, which compiles every advertised outputSchema
588
- // with `new Function`. Cloudflare Workers prohibit dynamic code
589
- // generation, so a remote such as Stripe fails during tools/list unless
590
- // the SDK's edge-safe validator is selected explicitly.
591
- const c = new Client(
592
- { name: "connecta", version: CONNECTA_VERSION },
593
- { jsonSchemaValidator: new CfWorkerJsonSchemaValidator() },
594
- );
595
- const t = buildTransport(ctx, state);
596
- state.transport = t;
597
- try {
598
- await c.connect(t);
599
- // A probe deadline can end its scope while connect is still in flight.
600
- // The transport is closed immediately by closeScope; if connect wins
601
- // that race anyway, close the resulting client rather than resurrecting
602
- // a session in the detached state object.
603
- if (state.closed) {
618
+ if (!state.connecting) {
619
+ let attempt!: Promise<void>;
620
+ attempt = (async () => {
621
+ const ownsAttempt = () =>
622
+ state.connecting === attempt && !state.closed;
623
+ const abandon = async (owner: Client | Transport) => {
604
624
  try {
605
- await c.close();
625
+ await owner.close();
606
626
  } catch {
607
- // The scope has already been discarded either way.
627
+ // The attempt is detached either way.
608
628
  }
609
629
  throw scopeEndedError();
630
+ };
631
+ // Let the assignment immediately below this async IIFE publish
632
+ // `state.connecting = attempt` before ownership is checked. OAuth's
633
+ // generation read naturally yields; unauthenticated transports do not.
634
+ await Promise.resolve();
635
+ // A provider belongs to exactly one connect attempt. A force reset can
636
+ // abandon that attempt while its transport still holds the provider;
637
+ // the replacement must never mutate the abandoned provider's epoch.
638
+ const provider = isOauth ? newProvider(ctx) : null;
639
+ const genAtStart = provider ? await provider.generation() : "";
640
+ if (!ownsAttempt()) throw scopeEndedError();
641
+ if (provider?.isOperatorDisconnectedGeneration(genAtStart)) {
642
+ throw operatorDisconnectedError();
610
643
  }
611
- // A force re-auth that landed WHILE we were connecting wiped the creds
612
- // this client just bound to. Discard it rather than cache a connection
613
- // that resurrects the wiped-and-reauthorized connector from a stale
614
- // isolate. Surfaces as auth_required the connector genuinely needs
615
- // re-consent now.
616
- if (provider) {
617
- const generation = await provider.generation();
618
- // closeScope can land while the generation read is pending, after
619
- // connect succeeded but before this client is cached. Discard the
620
- // client on that side of the await too.
621
- if (state.closed) {
622
- try {
623
- await c.close();
624
- } catch {
625
- // The scope has already been discarded either way.
644
+ provider?.captureGeneration(genAtStart);
645
+ // The SDK defaults to AJV, which compiles every advertised outputSchema
646
+ // with `new Function`. Cloudflare Workers prohibit dynamic code
647
+ // generation, so a remote such as Stripe fails during tools/list unless
648
+ // the SDK's edge-safe validator is selected explicitly.
649
+ const c = new Client(
650
+ { name: "connecta", version: CONNECTA_VERSION },
651
+ { jsonSchemaValidator: new CfWorkerJsonSchemaValidator() },
652
+ );
653
+ const t = buildTransport(ctx, provider);
654
+ if (!ownsAttempt()) await abandon(t);
655
+ state.transport = t;
656
+ try {
657
+ await c.connect(t);
658
+ // A probe deadline can end its scope while connect is still in flight.
659
+ // The transport is closed immediately by closeScope; if connect wins
660
+ // that race anyway, close the resulting client rather than
661
+ // resurrecting a session in the detached state object.
662
+ if (!ownsAttempt()) await abandon(c);
663
+ // A force re-auth that landed WHILE we were connecting wiped the
664
+ // credentials this client just bound to. Discard it rather than
665
+ // cache a stale-isolate connection.
666
+ if (provider) {
667
+ const generation = await provider.generation();
668
+ // closeScope can land while the generation read is pending, after
669
+ // connect succeeded but before this client is cached. Discard the
670
+ // client on that side of the await too.
671
+ if (!ownsAttempt()) await abandon(c);
672
+ if (generation !== genAtStart) {
673
+ try {
674
+ await c.close();
675
+ } catch {
676
+ // discarding either way
677
+ }
678
+ throw new UnauthorizedError(
679
+ "Connector was re-authorized during connect; reconnect required.",
680
+ );
626
681
  }
627
- throw scopeEndedError();
628
682
  }
629
- if (generation !== genAtStart) {
630
- try {
631
- await c.close();
632
- } catch {
633
- // discarding either way
634
- }
635
- throw new UnauthorizedError(
636
- "Connector was re-authorized during connect; reconnect required.",
637
- );
683
+ if (!ownsAttempt()) await abandon(c);
684
+ state.client = c;
685
+ state.connectedGeneration = genAtStart;
686
+ state.authRequired = false;
687
+ } catch (err) {
688
+ // Only a real 401/UnauthorizedError means auth is the problem — a
689
+ // network error on an oauth connector must surface as "error", not
690
+ // "auth_required".
691
+ if (err instanceof UnauthorizedError && ownsAttempt()) {
692
+ state.authRequired = true;
638
693
  }
694
+ if (err instanceof UnauthorizedError) {
695
+ throw authRequiredError(err);
696
+ }
697
+ throw err;
698
+ } finally {
699
+ // Force reset may have abandoned this attempt and installed a new one
700
+ // in the same request scope. An old completion must not erase the new
701
+ // promise and allow a third concurrent connect.
702
+ if (state.connecting === attempt) state.connecting = null;
639
703
  }
640
- state.client = c;
641
- state.connectedGeneration = genAtStart;
642
- state.authRequired = false;
643
- } catch (err) {
644
- // Only a real 401/UnauthorizedError means auth is the problem — a
645
- // network error on an oauth connector must surface as "error", not
646
- // "auth_required".
647
- if (err instanceof UnauthorizedError) {
648
- state.authRequired = true;
649
- throw authRequiredError(err);
704
+ })();
705
+ state.connecting = attempt;
706
+ }
707
+ return state.connecting;
708
+ };
709
+
710
+ const disconnectAuthorization = async (
711
+ ctx: ConnectorContext,
712
+ state: ConnectionState,
713
+ operatorDisconnected = false,
714
+ ): Promise<void> => {
715
+ const provider = getProvider(ctx, state);
716
+ // Publish the replacement epoch before waiting on or closing any
717
+ // request-local transport. A hung connect therefore cannot delay the
718
+ // fence, and every late OAuth write stays in the older namespace.
719
+ const connecting = state.connecting;
720
+ try {
721
+ await provider.resetAuthorization(operatorDisconnected);
722
+ } finally {
723
+ // Consume the abandoned connect and close whichever half of the
724
+ // client/transport exists. Reset is unconditional because KV may already
725
+ // be fenced behind a newer epoch after a cleanup error.
726
+ void connecting?.catch(() => {});
727
+ try {
728
+ if (state.client) {
729
+ await state.client.close();
730
+ } else {
731
+ await state.transport?.close();
650
732
  }
651
- throw err;
652
- } finally {
653
- state.connecting = null;
733
+ } catch {
734
+ // best-effort; the state is discarded either way
654
735
  }
655
- })();
656
- return state.connecting;
736
+ reset(state);
737
+ }
657
738
  };
658
739
 
659
740
  const connector: Connector = {
@@ -760,7 +841,7 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
760
841
  // Classify that exactly like connect-time and call-time authorization
761
842
  // failures, and latch it for the rest of this request scope.
762
843
  if (err instanceof UnauthorizedError) {
763
- state.authRequired = true;
844
+ if (state.client === client) state.authRequired = true;
764
845
  throw authRequiredError(err);
765
846
  }
766
847
  throw err;
@@ -788,8 +869,9 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
788
869
  async callTool(name, args, ctx) {
789
870
  const state = stateFor(ctx);
790
871
  await ensureConnected(ctx, state);
872
+ const client = state.client!;
791
873
  try {
792
- return await state.client!.callTool(
874
+ return await client.callTool(
793
875
  {
794
876
  name,
795
877
  arguments: (args ?? {}) as Record<string, unknown>,
@@ -805,7 +887,7 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
805
887
  } catch (err) {
806
888
  // A grant revoked after connect surfaces here, not in ensureConnected.
807
889
  if (err instanceof UnauthorizedError) {
808
- state.authRequired = true;
890
+ if (state.client === client) state.authRequired = true;
809
891
  throw authRequiredError(err);
810
892
  }
811
893
  throw err;
@@ -860,6 +942,9 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
860
942
  message: "Authorization required — open the URL to connect.",
861
943
  };
862
944
  }
945
+ if (err instanceof OperatorDisconnectedError) {
946
+ return { state: "auth_required", message: err.message };
947
+ }
863
948
  return { state: "error", message: msg(err) };
864
949
  }
865
950
  },
@@ -867,12 +952,11 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
867
952
  async finishAuth(code, ctx) {
868
953
  const state = stateFor(ctx);
869
954
  const provider = getProvider(ctx, state);
870
- // The provider here may carry no captured generation, so its token saves
871
- // fail open. That is safe only because generation advances solely via the
872
- // force path, which always wipes oauth:state so verifyState rejects any
873
- // pre-force callback before this runs. Keep those two facts coupled.
955
+ // verifyState ran on this request-scoped provider first and captured the
956
+ // pending flow's generation. If force reset races the exchange, any late
957
+ // token write remains tagged with that older generation and is unreadable.
874
958
  const t = (state.transport ??
875
- buildTransport(ctx, state)) as StreamableHTTPClientTransport;
959
+ buildTransport(ctx, provider)) as StreamableHTTPClientTransport;
876
960
  await t.finishAuth(code);
877
961
  await provider.clearPending();
878
962
  // Reset so the next use reconnects with the freshly stored tokens.
@@ -895,31 +979,15 @@ export function remoteMcp(id: string, opts: RemoteMcpOptions): Connector {
895
979
  return getProvider(ctx, state).verifyState(oauthState);
896
980
  };
897
981
 
982
+ connector.disconnectAuth = async (ctx) => {
983
+ await disconnectAuthorization(ctx, stateFor(ctx), true);
984
+ };
985
+
898
986
  connector.startAuth = async (ctx, startOpts) => {
899
987
  const state = stateFor(ctx);
900
988
  const p = getProvider(ctx, state);
901
- if (startOpts?.force) {
902
- // Wipe stored credentials and drop the live connection so the next
903
- // connect attempt runs the flow from scratch (DCR + PKCE + consent).
904
- // Fence the in-flight connect first: a late-completing attempt must not
905
- // resurrect the credentials we're about to wipe, nor leave `client` set
906
- // (which would make ensureConnected below report already-authorized and
907
- // silently defeat force).
908
- await state.connecting?.catch(() => {});
909
- try {
910
- await state.client?.close();
911
- } catch {
912
- // best-effort; the connection is being discarded either way
913
- }
914
- // Bump the shared generation FIRST so any other isolate — one mid-
915
- // connect, or on its next tool call — sees the advance and drops its
916
- // client instead of keeping the token we're about to revoke.
917
- await p.bumpGeneration();
918
- // Wipe KV before dropping in-memory state so nothing racing back in can
919
- // write tokens over a half-cleared slot.
920
- await p.invalidateCredentials("all");
921
- await p.clearPending();
922
- reset(state);
989
+ if (startOpts?.force || (await p.operatorDisconnected())) {
990
+ await disconnectAuthorization(ctx, state);
923
991
  } else {
924
992
  // A consent URL already outstanding? Re-issue it rather than re-running
925
993
  // the SDK flow, which would overwrite the PKCE verifier and invalidate
package/src/execute.ts CHANGED
@@ -369,20 +369,33 @@ export async function buildSandboxProviders(
369
369
  score: number;
370
370
  order: number;
371
371
  }> = [];
372
- let orderBase = 0;
373
- for (const connector of connectors) {
374
- if (args.connector && connector.id !== args.connector) continue;
375
- const tools = catalogs.get(connector.id);
376
- if (!tools) continue;
377
- for (const ranked of rankTools(tools, args.query ?? "")) {
378
- matches.push({
379
- connector: connector.id,
380
- tool: ranked.tool,
381
- score: ranked.score,
382
- order: orderBase + ranked.order,
383
- });
372
+ let matchMode: "all" | "partial" = "all";
373
+ const collectMatches = (mode: "all" | "partial") => {
374
+ matches.length = 0;
375
+ let orderBase = 0;
376
+ for (const connector of connectors) {
377
+ if (args.connector && connector.id !== args.connector) continue;
378
+ const tools = catalogs.get(connector.id);
379
+ if (!tools) continue;
380
+ for (const ranked of rankTools(
381
+ tools,
382
+ args.query ?? "",
383
+ mode,
384
+ )) {
385
+ matches.push({
386
+ connector: connector.id,
387
+ tool: ranked.tool,
388
+ score: ranked.score,
389
+ order: orderBase + ranked.order,
390
+ });
391
+ }
392
+ orderBase += tools.length;
384
393
  }
385
- orderBase += tools.length;
394
+ };
395
+ collectMatches("all");
396
+ if ((args.query ?? "").trim() && matches.length === 0) {
397
+ matchMode = "partial";
398
+ collectMatches(matchMode);
386
399
  }
387
400
  matches.sort((a, b) => b.score - a.score || a.order - b.order);
388
401
  const offset = Math.max(0, Math.trunc(args.offset ?? 0));
@@ -428,6 +441,9 @@ export async function buildSandboxProviders(
428
441
  limit,
429
442
  hasMore: nextOffset !== undefined,
430
443
  ...(nextOffset !== undefined ? { nextOffset } : {}),
444
+ ...(matchMode === "partial" && matches.length > 0
445
+ ? { matchMode }
446
+ : {}),
431
447
  };
432
448
  assertDiscoveryResultSize(
433
449
  result,
package/src/index.ts CHANGED
@@ -94,7 +94,7 @@ export interface ConnectaDiscoveryConfig {
94
94
  probeTimeoutMs?: number;
95
95
  }
96
96
 
97
- /** Deployment-wide call deadlines and inline-result paging threshold. */
97
+ /** Deployment-wide call deadlines and inline-result paging thresholds. */
98
98
  export interface ConnectaCallsConfig {
99
99
  /**
100
100
  * Deadline (ms) for `call_tool`/`batch_call` calls that pass no `timeoutMs`.
@@ -111,6 +111,13 @@ export interface ConnectaCallsConfig {
111
111
  * 50_000. Connectors may override it individually.
112
112
  */
113
113
  maxResultBytes?: number;
114
+ /**
115
+ * Max serialized `batch_call` envelope size (bytes) before the full batch is
116
+ * stashed for `get_result` and only an ordered outcome summary is returned
117
+ * inline. Must be a finite whole number >= 1; invalid values warn and fall
118
+ * back to 100_000. This cap is independent of per-connector child caps.
119
+ */
120
+ maxBatchResultBytes?: number;
114
121
  }
115
122
 
116
123
  export interface AdmissionPoolConfig {
@@ -544,6 +551,7 @@ export function createConnecta(config: ConnectaConfig): Connecta {
544
551
  persistToolCatalog: config.discovery?.persistCatalog,
545
552
  toolCatalogStaleSeconds: config.discovery?.staleCatalogSeconds,
546
553
  maxResultBytes: config.calls?.maxResultBytes,
554
+ maxBatchResultBytes: config.calls?.maxBatchResultBytes,
547
555
  credentialHealth: config.credentials?.health,
548
556
  });
549
557
  // Throws on every structural mistake it can see (see resolveToolkits): a
@@ -719,8 +727,11 @@ export type {
719
727
  ActivityCallSource,
720
728
  ActivityOutcome,
721
729
  ActivityPage,
730
+ ActivityReadActor,
731
+ ActivityReadEvent,
722
732
  ActivityReader,
723
733
  ActivityReadGate,
734
+ ActivityReadPage,
724
735
  ActivitySink,
725
736
  ActivityStore,
726
737
  ToolCallActivityEvent,
package/src/meta-tools.ts CHANGED
@@ -406,6 +406,15 @@ async function stashResult(
406
406
  };
407
407
  }
408
408
 
409
+ /** Keep an oversized batch's inline outcome summary at fixed string overhead. */
410
+ function batchSummaryString(value: string): string {
411
+ const bytes = enc.encode(value);
412
+ const maxBytes = 512;
413
+ if (bytes.length <= maxBytes) return value;
414
+ const end = alignEndToCharBoundary(bytes, 0, maxBytes, bytes.length);
415
+ return `${dec.decode(bytes.slice(0, end))}…`;
416
+ }
417
+
409
418
  /**
410
419
  * Return `text` as a single content block; if it exceeds `cap` bytes, stash the
411
420
  * full text and return the first `cap` bytes followed by a JSON truncation
@@ -575,10 +584,10 @@ export interface SkillArgs {
575
584
  * supplies a deadline for calls that don't carry one. (execute_code, the
576
585
  * optional tenth tool, is registered separately by registerExecuteTool.)
577
586
  *
578
- * The deployment-wide result-size cap is read off the registry view rather than
579
- * passed in: `ConnectaConfig.calls.maxResultBytes` and the per-connector
580
- * override are the only places a cap is set, so there is one answer to where a
581
- * deployment sets it (issue #44).
587
+ * Deployment-wide result-size caps are read off the registry view rather than
588
+ * passed in: `ConnectaConfig.calls.maxResultBytes`, its per-connector override,
589
+ * and the independent `calls.maxBatchResultBytes` final-envelope boundary each
590
+ * have one runtime source of truth.
582
591
  */
583
592
  export function createMetaTools(
584
593
  registry: RegistryView,
@@ -595,6 +604,7 @@ export function createMetaTools(
595
604
  ) {
596
605
  // Already normalized and warned about at registry construction.
597
606
  const globalCap = registry.maxResultBytes;
607
+ const batchCap = registry.maxBatchResultBytes;
598
608
  const defaultToolTimeoutMs = normalizeTimeoutMs(opts.defaultToolTimeoutMs);
599
609
  const probeTimeoutMs =
600
610
  normalizeTimeoutMs(opts.probeTimeoutMs) ?? DEFAULT_PROBE_TIMEOUT_MS;
@@ -1141,26 +1151,36 @@ export function createMetaTools(
1141
1151
  ),
1142
1152
  ),
1143
1153
  );
1144
- let orderBase = 0;
1145
- catalogs.forEach((catalog, connectorIndex) => {
1146
- const c = conns[connectorIndex];
1147
- if (catalog.status === "fulfilled") {
1148
- for (const ranked of rankTools(catalog.value, q)) {
1149
- matches.push({
1150
- connectorId: c.id,
1151
- connectorTitle: c.title,
1152
- connectorDescription: c.description,
1153
- ...(connectorGuide(c)
1154
- ? { connectorGuideSkill: connectorSkillName(c.id) }
1155
- : {}),
1156
- tool: ranked.tool,
1157
- score: ranked.score,
1158
- order: orderBase + ranked.order,
1159
- });
1154
+ let matchMode: "all" | "partial" = "all";
1155
+ const collectMatches = (mode: "all" | "partial") => {
1156
+ matches.length = 0;
1157
+ let orderBase = 0;
1158
+ catalogs.forEach((catalog, connectorIndex) => {
1159
+ const c = conns[connectorIndex];
1160
+ if (catalog.status === "fulfilled") {
1161
+ for (const ranked of rankTools(catalog.value, q, mode)) {
1162
+ matches.push({
1163
+ connectorId: c.id,
1164
+ connectorTitle: c.title,
1165
+ connectorDescription: c.description,
1166
+ ...(connectorGuide(c)
1167
+ ? { connectorGuideSkill: connectorSkillName(c.id) }
1168
+ : {}),
1169
+ tool: ranked.tool,
1170
+ score: ranked.score,
1171
+ order: orderBase + ranked.order,
1172
+ });
1173
+ }
1160
1174
  }
1161
- }
1162
- orderBase += catalog.status === "fulfilled" ? catalog.value.length : 1;
1163
- });
1175
+ orderBase +=
1176
+ catalog.status === "fulfilled" ? catalog.value.length : 1;
1177
+ });
1178
+ };
1179
+ collectMatches("all");
1180
+ if (q.trim() && matches.length === 0) {
1181
+ matchMode = "partial";
1182
+ collectMatches(matchMode);
1183
+ }
1164
1184
  matches.sort((a, b) => b.score - a.score || a.order - b.order);
1165
1185
  const page = matches.slice(offset, offset + limit);
1166
1186
  const groups: {
@@ -1235,6 +1255,9 @@ export function createMetaTools(
1235
1255
  limit,
1236
1256
  hasMore: nextOffset !== undefined,
1237
1257
  ...(nextOffset !== undefined ? { nextOffset } : {}),
1258
+ ...(matchMode === "partial" && matches.length > 0
1259
+ ? { matchMode }
1260
+ : {}),
1238
1261
  },
1239
1262
  "Request a smaller limit, omit fullDescriptions, or use compact schemas.",
1240
1263
  );
@@ -1457,9 +1480,50 @@ export function createMetaTools(
1457
1480
  : {}),
1458
1481
  };
1459
1482
  });
1460
- return jsonResult({
1483
+ const envelope = {
1461
1484
  results,
1462
1485
  durationMs: Date.now() - batchStarted,
1486
+ };
1487
+ const text = serializeResultText(envelope);
1488
+ const bytes = enc.encode(text);
1489
+ if (bytes.length <= batchCap) return jsonResult(envelope);
1490
+
1491
+ const notice = await stashResult(
1492
+ text,
1493
+ registry.resultsStorage(),
1494
+ bytes.length,
1495
+ );
1496
+ return jsonResult({
1497
+ results: results.map((result) => {
1498
+ const common = {
1499
+ address: batchSummaryString(result.address),
1500
+ ok: !("error" in result),
1501
+ ...("durationMs" in result
1502
+ ? { durationMs: result.durationMs }
1503
+ : {}),
1504
+ ...("attempts" in result ? { attempts: result.attempts } : {}),
1505
+ ...("timing" in result ? { timing: result.timing } : {}),
1506
+ };
1507
+ if (!("error" in result)) return common;
1508
+ const error = result.error ?? "Batch call failed";
1509
+ const details =
1510
+ result.errorDetails ??
1511
+ errorDetails("batch_call_failed", error);
1512
+ return {
1513
+ ...common,
1514
+ error: batchSummaryString(error),
1515
+ errorDetails: {
1516
+ code: batchSummaryString(details.code),
1517
+ message: batchSummaryString(details.message),
1518
+ retryable: details.retryable,
1519
+ ...(details.retryAfterMs !== undefined
1520
+ ? { retryAfterMs: details.retryAfterMs }
1521
+ : {}),
1522
+ },
1523
+ };
1524
+ }),
1525
+ durationMs: envelope.durationMs,
1526
+ ...notice,
1463
1527
  });
1464
1528
  },
1465
1529
 
@@ -1527,7 +1591,7 @@ const CALL_DESTRUCTIVE_DESC =
1527
1591
  const GET_RESULT_DESC =
1528
1592
  "Page a truncated result stashed by call_tool/batch_call. Input { id, offset?, maxBytes? } → { text, offset, nextOffset?, totalBytes } sliced by byte offset. maxBytes is a whole number of bytes >= 1 (omit for the deployment default) and offset a whole number of bytes >= 0; an offset inside a multi-byte character is moved back to that character's first byte and the offset served is returned. Unknown/expired id is an error.";
1529
1593
  const BATCH_DESC =
1530
- "Use for 2–10 independent tools explicitly annotated readOnlyHint: true. Calls run in parallel with shared request-scoped clients; use execute_code when available instead for dependencies or in-sandbox reduction. Unannotated, write-capable, and destructive tools are refused. Batch timeout, safe retry, result mode, and diagnostics defaults may be overridden per call.";
1594
+ "Use for 2–10 independent tools explicitly annotated readOnlyHint: true. Calls run in parallel with shared request-scoped clients; use execute_code when available instead for dependencies or in-sandbox reduction. Unannotated, write-capable, and destructive tools are refused. Batch timeout, safe retry, result mode, and diagnostics defaults may be overridden per call. An oversized final envelope returns ordered outcome summaries plus a get_result page handle.";
1531
1595
  const AUTHORIZE_DESC =
1532
1596
  "Use after a connector reports auth_required. Starts downstream OAuth and returns an authorizationUrl for the operator to open. force=true wipes stored credentials first and restarts consent.";
1533
1597
  const SKILLS_DESC =