@feltdb/feltkit 0.5.1 → 0.5.3

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.
@@ -622,7 +622,7 @@ export class FeltKitRuntime {
622
622
  },
623
623
  },
624
624
  // A worker needs a grant for everything but the protocol's own description and its own calls' status.
625
- delegatedAuthorityAuthorizer({ grants: this.workers, approvals: this.workers, require: (request) => typeof (request.principal ?? request.session?.principal)?.attributes?.feltkitWorker === "string" && !WORKER_UNGRANTED.includes(request.capability.name), now: () => this.clock.now() }),
625
+ delegatedAuthorityAuthorizer({ grants: this.workers, approvals: this.workers, require: (request) => typeof (request.principal ?? request.session?.principal)?.attributes?.feltkitWorker === "string" && !WORKER_UNGRANTED.includes(request.capability.name) && !this.#nestedCall.getStore(), now: () => this.clock.now() }),
626
626
  ]),
627
627
  // A capability that publishes `integration.approval.required` runs only with an approval the authorizer verified.
628
628
  enforceApprovals: true,
@@ -798,7 +798,11 @@ export class FeltKitRuntime {
798
798
  throw new FeltKitError("FORBIDDEN", "Connections belong to a signed-in person, who manages only their own.");
799
799
  return principal.id;
800
800
  }
801
- #integrationInvoker(principal) {
801
+ #integrationInvoker(principal, verified) {
802
+ // An action whose own invocation was verified (a worker under a person's grant that covers this action) calls a
803
+ // provider capability as its implementation, like a nested runQuery/runMutation: the grant is not asked to name the
804
+ // provider's capability, roles still decide, and the effect is still recorded. Only this runtime marks such a call.
805
+ const nested = verified && Object.keys(verified).length ? verified : undefined;
802
806
  return {
803
807
  connections: {
804
808
  list: async () => this.oauth.connections(this.#connectionOwner(principal)),
@@ -819,7 +823,7 @@ export class FeltKitRuntime {
819
823
  if (this.providers.provides(capability)) {
820
824
  if (options.onBehalfOf !== undefined)
821
825
  throw new FeltKitError("INVALID_ARGUMENT", `${capability} is a provider capability; onBehalfOf applies to a person's integration connection.`);
822
- return this.invokeCapability(capability, input, principal, { via: "function", ...(options.idempotencyKey ? { idempotencyKey: options.idempotencyKey } : {}) });
826
+ return this.invokeCapability(capability, input, principal, { via: "function", ...(options.idempotencyKey ? { idempotencyKey: options.idempotencyKey } : {}), ...(nested ? { nestedUnder: nested } : {}) });
823
827
  }
824
828
  const found = this.integrations.operationFor(capability);
825
829
  if (!found)
@@ -850,7 +854,8 @@ export class FeltKitRuntime {
850
854
  async invokeCapability(capability, input, principal, options) {
851
855
  if (!principal)
852
856
  throw new FeltKitError("UNAUTHORIZED", `${capability} needs an authenticated caller.`);
853
- const response = (await this.appport.handleRequest({ protocol: "appport/1", type: "request", requestId: `req_${randomUUID()}`, capability: { name: capability, version: options.version ?? 1 }, input, ...(options.idempotencyKey ? { idempotencyKey: options.idempotencyKey } : {}), metadata: { transport: options.via } }, { principal: toAppPort(principal), transport: options.via }));
857
+ const send = () => this.appport.handleRequest({ protocol: "appport/1", type: "request", requestId: `req_${randomUUID()}`, capability: { name: capability, version: options.version ?? 1 }, input, ...(options.idempotencyKey ? { idempotencyKey: options.idempotencyKey } : {}), metadata: { transport: options.via } }, { principal: toAppPort(principal), transport: options.via });
858
+ const response = (await (options.nestedUnder ? this.#nestedCall.run({ verified: options.nestedUnder }, send) : send()));
854
859
  if (response.ok)
855
860
  return response.output;
856
861
  throw providerError(response.error);
@@ -893,6 +898,8 @@ export class FeltKitRuntime {
893
898
  }
894
899
  /** The caller of the AppPort request being dispatched (see #gate). */
895
900
  #appportCaller = new AsyncLocalStorage();
901
+ /** A provider capability called by an action whose own invocation was verified (#integrationInvoker): set only here. */
902
+ #nestedCall = new AsyncLocalStorage();
896
903
  /**
897
904
  * AppPort's authorizer, extended for an agent working for a person (runtime/delegation.ts). A request
898
905
  * without a person is decided by `inner` unchanged. One with a person is decided here, before AppPort
@@ -1960,6 +1967,16 @@ export class FeltKitRuntime {
1960
1967
  }
1961
1968
  /** ctx.grants for one call: guest links and agent grants for records the caller can see, issued as a person. */
1962
1969
  /** `ctx.grants` as this principal, outside a function: the same rules (FeltKit's own surfaces, e.g. eve agents). */
1970
+ #projectAgentSessions = null;
1971
+ /** The project agents' host (server/eve.ts) offers delegated sessions to application code (ctx.agent.startSession). */
1972
+ useProjectAgentSessions(sessions) {
1973
+ this.#projectAgentSessions = sessions;
1974
+ }
1975
+ #agentSessions() {
1976
+ if (!this.#projectAgentSessions)
1977
+ throw new FeltKitError("FAILED_PRECONDITION", "This application runs no project agents here (feltkit({ eve }) on a FeltKit server).");
1978
+ return this.#projectAgentSessions;
1979
+ }
1963
1980
  grantsFor(principal) {
1964
1981
  return this.#grantsApi(principal, applicationView(new Reader(this.instance.handle("app"), this.#resolver)));
1965
1982
  }
@@ -2640,7 +2657,7 @@ export class FeltKitRuntime {
2640
2657
  },
2641
2658
  },
2642
2659
  authority: this.#applicationAuthority(principal, requestId),
2643
- integrations: this.#integrationInvoker(principal),
2660
+ integrations: this.#integrationInvoker(principal, authorityRecord.invocation?.verified),
2644
2661
  grants: this.#grantsApi(principal, applicationView(new Reader(this.instance.handle("app"), this.#resolver))),
2645
2662
  services: { files: explainServiceDenials(this.#services.bind(principal, requestId, new Set(), true), name, principal, this.#authority).files },
2646
2663
  invocations: this.#invocationsApi(workItemScoped(principal, authorityRecord.invocation?.verified), null, { via: String(options.via ?? "function"), requestId }),
@@ -2667,6 +2684,8 @@ export class FeltKitRuntime {
2667
2684
  actions: async (taskId) => (await agent.turnActions(taskId, signedIn())).map(({ at: _at, ...a }) => a),
2668
2685
  checkpoint: (taskId) => agent.control.checkpoint(taskId, signedIn()),
2669
2686
  acknowledge: (taskId, instructionId, input) => agent.control.acknowledge(taskId, instructionId, input, signedIn()),
2687
+ startSession: (agentName, input) => this.#agentSessions().start(agentName, principal, input),
2688
+ endSession: (agentName, sessionId, reason) => this.#agentSessions().end(agentName, principal, sessionId, reason),
2670
2689
  propose: async (taskId, proposal) => {
2671
2690
  const p = signedIn();
2672
2691
  await agent.propose(taskId, proposal, p);