primitive-functions 0.3.0-alpha.5 → 0.3.0-alpha.7

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 (3) hide show
  1. package/index.d.ts +87 -5
  2. package/index.js +11 -0
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -130,6 +130,9 @@ export type FunctionTrigger =
130
130
  /** An agent turn asked this function for the history to send the model. */
131
131
  | { kind: "history"; sessionId: string; turnId: string };
132
132
 
133
+ /** The door names `ctx.trigger.kind` can hold. */
134
+ export type FunctionTriggerKind = FunctionTrigger["kind"];
135
+
133
136
  /**
134
137
  * The agent session a tool call belongs to — `ctx.tool`, set only when
135
138
  * `ctx.trigger.kind === "tool"`.
@@ -235,9 +238,10 @@ export interface FunctionApi {
235
238
  "create"(args: {
236
239
  body: unknown;
237
240
  }): Promise<any>;
238
- /** `agentSessions.delete` — DELETE /agent-sessions/{sessionId}. Delete an agent session */
241
+ /** `agentSessions.delete` — DELETE /agent-sessions/{sessionId}. Delete an agent session (ctx.agents.deleteSession) */
239
242
  "delete"(args: {
240
243
  "sessionId": string | number;
244
+ "userId"?: string | number;
241
245
  }): Promise<any>;
242
246
  /** `agentSessions.get` — GET /agent-sessions/{sessionId}. Get an agent session */
243
247
  "get"(args: {
@@ -1947,9 +1951,9 @@ export interface AgentSessionListOptions {
1947
1951
  }
1948
1952
 
1949
1953
  /**
1950
- * Which member `ctx.agents.getSession` and `recordEvent` act as. Omitted,
1951
- * the invoking user; a run with no caller must name one, and naming another
1952
- * follows the header's subject rule.
1954
+ * Which member `ctx.agents.getSession`, `recordEvent` and
1955
+ * `deleteSession` act as. Omitted, the invoking user; a run with no caller
1956
+ * must name one, and naming another follows the header's subject rule.
1953
1957
  */
1954
1958
  export interface AgentSessionSubjectOptions {
1955
1959
  userId?: string;
@@ -1982,6 +1986,13 @@ export interface AgentSessionEventResult {
1982
1986
  duplicate?: boolean;
1983
1987
  }
1984
1988
 
1989
+ /** What `ctx.agents.deleteSession` answers: the session and the document deleted with it. */
1990
+ export interface AgentSessionDeleteResult {
1991
+ deleted: true;
1992
+ sessionId: string;
1993
+ documentId: string;
1994
+ }
1995
+
1985
1996
  /** One agent's client tools, by name: each one's `input` and `output` (#3798). */
1986
1997
  export type AgentClientTools<K extends AgentKeys> = PromptSchemas[K] extends {
1987
1998
  agent: { clientTools: infer T };
@@ -2278,6 +2289,10 @@ export interface FunctionContext {
2278
2289
  * The agent session this tool call belongs to, when an agent turn called
2279
2290
  * the function as a tool (`ctx.trigger.kind === "tool"`); otherwise
2280
2291
  * absent.
2292
+ *
2293
+ * Checking `ctx.trigger.kind` narrows `ctx.trigger` and not this
2294
+ * member, so call `assertTrigger(ctx, "tool")` first to read it without a
2295
+ * guard.
2281
2296
  */
2282
2297
  tool?: FunctionToolContext;
2283
2298
  /**
@@ -2414,7 +2429,16 @@ export interface FunctionContext {
2414
2429
  * the ones they own or are a member of — as that member, with no admin arm
2415
2430
  * whoever invoked the function: a session they are not in answers
2416
2431
  * `AGENT_SESSION_NOT_FOUND`, and function code never sees a session's
2417
- * `object`, `turnContexts` or `lastTurn`. Renaming, deleting and
2432
+ * `object`, `turnContexts` or `lastTurn`.
2433
+ *
2434
+ * `deleteSession` deletes a session the member owns, with the same
2435
+ * outcome as the owner's delete from a client: the session, its document
2436
+ * and every member's listing go, and a repeat answers the same. A session
2437
+ * whose document is already gone can still be deleted. A participant or
2438
+ * viewer is refused `AGENT_SESSION_OWNER_ONLY`, anyone else
2439
+ * `AGENT_SESSION_NOT_FOUND`, with no admin arm. A `userId` that is
2440
+ * present but not a string (`null`, a number) is refused
2441
+ * `SUBJECT_USER_INVALID`, never read as the invoking user. Renaming and
2418
2442
  * changing members are the owner's, from a client.
2419
2443
  *
2420
2444
  * `recordEvent` (#3807) records an event as the member, who must be the
@@ -2438,6 +2462,10 @@ export interface FunctionContext {
2438
2462
  event: AgentSessionEvent,
2439
2463
  options?: AgentSessionSubjectOptions
2440
2464
  ): Promise<AgentSessionEventResult>;
2465
+ deleteSession(
2466
+ sessionId: string,
2467
+ options?: AgentSessionSubjectOptions
2468
+ ): Promise<AgentSessionDeleteResult>;
2441
2469
  };
2442
2470
  /**
2443
2471
  * The value of one app secret, under a `secret:<NAME>` grant (#3183).
@@ -2547,6 +2575,27 @@ export interface FunctionContext {
2547
2575
  };
2548
2576
  }
2549
2577
 
2578
+ /**
2579
+ * `FunctionContext` as `assertTrigger(ctx, kind)` leaves it: `trigger`
2580
+ * is the arm for `K`, `tool` is set for a tool call and `undefined` for
2581
+ * every other door, and `user` is the turn's initiator, never null, for the
2582
+ * three doors an agent turn opens (`tool`, `context` and `history`).
2583
+ *
2584
+ * Annotate a helper's parameter with it to take a context the caller has
2585
+ * already asserted:
2586
+ *
2587
+ * ```ts
2588
+ * async function lookup(ctx: FunctionContextFor<"tool">) {
2589
+ * return ctx.tool.variables.householdId;
2590
+ * }
2591
+ * ```
2592
+ */
2593
+ export type FunctionContextFor<K extends FunctionTriggerKind> = FunctionContext & {
2594
+ trigger: Extract<FunctionTrigger, { kind: K }>;
2595
+ tool: K extends "tool" ? FunctionToolContext : undefined;
2596
+ user: K extends "tool" | "context" | "history" ? FunctionUser : FunctionUser | null;
2597
+ };
2598
+
2550
2599
  /** How the wait between one step's retries grows. */
2551
2600
  export type StepBackoff = "constant" | "linear" | "exponential";
2552
2601
 
@@ -2647,6 +2696,38 @@ export declare class FunctionRuntimeError extends Error {
2647
2696
  */
2648
2697
  export declare function assertRuntime(required: "request" | "task"): void;
2649
2698
 
2699
+ /**
2700
+ * Refuse every door but the one this function was written for, and narrow
2701
+ * `ctx` to it.
2702
+ *
2703
+ * Make it the first line of a tool function: an agent turn calls it with
2704
+ * `ctx.tool` set, but the same function is also reachable over plain HTTP.
2705
+ * After the call, `ctx.trigger` is the named arm, `ctx.tool` needs no
2706
+ * guard, and `ctx.user` is the turn's initiator:
2707
+ *
2708
+ * ```ts
2709
+ * import { assertTrigger, defineFunction } from "primitive-functions";
2710
+ *
2711
+ * export default defineFunction(async (_input, ctx) => {
2712
+ * assertTrigger(ctx, "tool");
2713
+ * const { householdId } = ctx.tool.variables as { householdId: string };
2714
+ * await assertHouseholdMember(ctx.user.userId, householdId);
2715
+ * return { callId: ctx.trigger.callId };
2716
+ * });
2717
+ * ```
2718
+ *
2719
+ * Returns when `ctx.trigger.kind` is `kind`. Otherwise it throws an
2720
+ * `Error` naming both kinds, which the platform settles as
2721
+ * `status: "failed"` with `errorCode: "FUNCTION_THREW"`. A `kind` that is
2722
+ * not a trigger kind, or a first argument that is not a context, throws a
2723
+ * `TypeError`. It takes `ctx`, so it runs inside the handler, never at
2724
+ * module scope.
2725
+ */
2726
+ export declare function assertTrigger<K extends FunctionTriggerKind>(
2727
+ ctx: FunctionContext,
2728
+ kind: K
2729
+ ): asserts ctx is FunctionContextFor<K>;
2730
+
2650
2731
  /**
2651
2732
  * The handler a function exports, typed for one declared key.
2652
2733
  *
@@ -2893,6 +2974,7 @@ declare const _default: {
2893
2974
  pMap: typeof pMap;
2894
2975
  ulid: typeof ulid;
2895
2976
  assertRuntime: typeof assertRuntime;
2977
+ assertTrigger: typeof assertTrigger;
2896
2978
  FunctionRuntimeError: typeof FunctionRuntimeError;
2897
2979
  };
2898
2980
  export default _default;
package/index.js CHANGED
@@ -110,6 +110,16 @@ export function assertRuntime() {
110
110
  throw new Error(NOT_PROVIDED);
111
111
  }
112
112
 
113
+ /**
114
+ * Refuse every door but the one this function was written for — #4442.
115
+ *
116
+ * The platform copy compares \`ctx.trigger.kind\` with the kind named. This
117
+ * one refuses, loudly, like every other function here.
118
+ */
119
+ export function assertTrigger() {
120
+ throw new Error(NOT_PROVIDED);
121
+ }
122
+
113
123
  export default {
114
124
  SDK_STUB_VERSION,
115
125
  defineFunction,
@@ -118,5 +128,6 @@ export default {
118
128
  pMap,
119
129
  ulid,
120
130
  assertRuntime,
131
+ assertTrigger,
121
132
  FunctionRuntimeError,
122
133
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "primitive-functions",
3
- "version": "0.3.0-alpha.5",
3
+ "version": "0.3.0-alpha.7",
4
4
  "description": "The module a Primitive server function imports. Supplied by the platform at invoke time; reserved here so the name is ours before the runtime lands.",
5
5
  "license": "UNLICENSED",
6
6
  "author": "Primitive LLC",