primitive-functions 0.3.0-alpha.6 → 0.3.0-alpha.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 (3) hide show
  1. package/index.d.ts +131 -6
  2. package/index.js +11 -0
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -114,7 +114,7 @@ export type FunctionTrigger =
114
114
  *
115
115
  * `ctx.user` is the turn's initiator, whose `access` gate the call
116
116
  * passed, and `ctx.tool` carries the session's ids, members and
117
- * variables. `callId` is the same on every attempt of the call, so use
117
+ * variables, and the turn's context, timezone and locale. `callId` is the same on every attempt of the call, so use
118
118
  * it as an idempotency key. `toolName` is the declared tool name, which
119
119
  * may differ from the function's key.
120
120
  */
@@ -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"`.
@@ -155,6 +158,24 @@ export interface FunctionToolContext {
155
158
  };
156
159
  /** The session's variables, set when it was created. */
157
160
  readonly variables: Record<string, unknown>;
161
+ /**
162
+ * The turn's validated context: the object the member's send carried,
163
+ * checked against the agent's `turnContext.schema`, or the return of the
164
+ * agent's context function. The same values the system prompt rendered as
165
+ * `turn.*`. Null when the turn carried none.
166
+ */
167
+ readonly context: Record<string, unknown> | null;
168
+ /**
169
+ * The IANA timezone the turn's send carried, such as
170
+ * `"Pacific/Auckland"`; the prompt's `turn.timezone`. Null when it
171
+ * carried none.
172
+ */
173
+ readonly timezone: string | null;
174
+ /**
175
+ * The BCP 47 locale the turn's send carried, such as `"en-NZ"`; the
176
+ * prompt's `turn.locale`. Null when it carried none.
177
+ */
178
+ readonly locale: string | null;
158
179
  /**
159
180
  * Attach an app-only artifact to this call's row, replacing any earlier
160
181
  * one. The model never sees it; your app reads it from the call's row —
@@ -235,9 +256,10 @@ export interface FunctionApi {
235
256
  "create"(args: {
236
257
  body: unknown;
237
258
  }): Promise<any>;
238
- /** `agentSessions.delete` — DELETE /agent-sessions/{sessionId}. Delete an agent session */
259
+ /** `agentSessions.delete` — DELETE /agent-sessions/{sessionId}. Delete an agent session (ctx.agents.deleteSession) */
239
260
  "delete"(args: {
240
261
  "sessionId": string | number;
262
+ "userId"?: string | number;
241
263
  }): Promise<any>;
242
264
  /** `agentSessions.get` — GET /agent-sessions/{sessionId}. Get an agent session */
243
265
  "get"(args: {
@@ -1057,6 +1079,7 @@ export interface FunctionApi {
1057
1079
  "limit"?: string | number | boolean;
1058
1080
  "cursor"?: string | number | boolean;
1059
1081
  "filter"?: Record<string, unknown> | string;
1082
+ "sort"?: Record<string, unknown> | string;
1060
1083
  }): Promise<any>;
1061
1084
  /** `documents.records.save` — POST /documents/{documentId}/records/{model}. Create or replace a record in a document model */
1062
1085
  "save"(args: {
@@ -1641,6 +1664,24 @@ export type DocumentModelRow<M extends string> = M extends keyof DocumentSchemas
1641
1664
  /** The options a document model read takes: a filter object (or its JSON), a page size, a cursor. */
1642
1665
  export interface DocumentQueryOptions {
1643
1666
  filter?: Record<string, unknown> | string;
1667
+ /**
1668
+ * The order of the rows: each field maps to 1 (ascending) or -1
1669
+ * (descending), and key order is precedence, as in { createdAt: -1 }. `id`
1670
+ * breaks ties unless you name it. `limit` and `cursor` apply to the
1671
+ * ordered rows; pass the same sort with a `cursor`, since a cursor from
1672
+ * another sort is refused with a 400.
1673
+ */
1674
+ sort?: Record<string, 1 | -1>;
1675
+ /**
1676
+ * Records per page: 100 when omitted, at most 10,000; a larger value is
1677
+ * refused with a 400 naming the maximum, never clamped. A page also ends
1678
+ * before the record that would take it past 8 MiB (the sum of each
1679
+ * record's JSON in UTF-8 bytes; the envelope is not counted), answering
1680
+ * `hasMore: true` and a `nextCursor` that continues from the last record
1681
+ * returned; a single record larger than 8 MiB is answered alone. A page
1682
+ * that fills `limit` exactly also answers `hasMore: true`; loop on
1683
+ * `hasMore` rather than counting rows.
1684
+ */
1644
1685
  limit?: number;
1645
1686
  cursor?: string;
1646
1687
  /**
@@ -1689,6 +1730,12 @@ export interface DocumentBulkResult {
1689
1730
  }
1690
1731
 
1691
1732
  export interface DocumentModelHandle<TRow> {
1733
+ /**
1734
+ * One page of the model's records: 100 by default, up to 10,000 with
1735
+ * `limit`, and at most 8 MiB of record JSON unless a single larger record
1736
+ * is answered alone. Pass `nextCursor` back as `cursor` while
1737
+ * `hasMore` is true.
1738
+ */
1692
1739
  query(options?: DocumentQueryOptions): Promise<{ items: TRow[]; hasMore: boolean; nextCursor?: string }>;
1693
1740
  count(options?: { filter?: Record<string, unknown> | string }): Promise<{ count: number }>;
1694
1741
  aggregate(body: Record<string, unknown>): Promise<any>;
@@ -1947,9 +1994,9 @@ export interface AgentSessionListOptions {
1947
1994
  }
1948
1995
 
1949
1996
  /**
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.
1997
+ * Which member `ctx.agents.getSession`, `recordEvent` and
1998
+ * `deleteSession` act as. Omitted, the invoking user; a run with no caller
1999
+ * must name one, and naming another follows the header's subject rule.
1953
2000
  */
1954
2001
  export interface AgentSessionSubjectOptions {
1955
2002
  userId?: string;
@@ -1982,6 +2029,13 @@ export interface AgentSessionEventResult {
1982
2029
  duplicate?: boolean;
1983
2030
  }
1984
2031
 
2032
+ /** What `ctx.agents.deleteSession` answers: the session and the document deleted with it. */
2033
+ export interface AgentSessionDeleteResult {
2034
+ deleted: true;
2035
+ sessionId: string;
2036
+ documentId: string;
2037
+ }
2038
+
1985
2039
  /** One agent's client tools, by name: each one's `input` and `output` (#3798). */
1986
2040
  export type AgentClientTools<K extends AgentKeys> = PromptSchemas[K] extends {
1987
2041
  agent: { clientTools: infer T };
@@ -2278,6 +2332,10 @@ export interface FunctionContext {
2278
2332
  * The agent session this tool call belongs to, when an agent turn called
2279
2333
  * the function as a tool (`ctx.trigger.kind === "tool"`); otherwise
2280
2334
  * absent.
2335
+ *
2336
+ * Checking `ctx.trigger.kind` narrows `ctx.trigger` and not this
2337
+ * member, so call `assertTrigger(ctx, "tool")` first to read it without a
2338
+ * guard.
2281
2339
  */
2282
2340
  tool?: FunctionToolContext;
2283
2341
  /**
@@ -2414,7 +2472,16 @@ export interface FunctionContext {
2414
2472
  * the ones they own or are a member of — as that member, with no admin arm
2415
2473
  * whoever invoked the function: a session they are not in answers
2416
2474
  * `AGENT_SESSION_NOT_FOUND`, and function code never sees a session's
2417
- * `object`, `turnContexts` or `lastTurn`. Renaming, deleting and
2475
+ * `object`, `turnContexts` or `lastTurn`.
2476
+ *
2477
+ * `deleteSession` deletes a session the member owns, with the same
2478
+ * outcome as the owner's delete from a client: the session, its document
2479
+ * and every member's listing go, and a repeat answers the same. A session
2480
+ * whose document is already gone can still be deleted. A participant or
2481
+ * viewer is refused `AGENT_SESSION_OWNER_ONLY`, anyone else
2482
+ * `AGENT_SESSION_NOT_FOUND`, with no admin arm. A `userId` that is
2483
+ * present but not a string (`null`, a number) is refused
2484
+ * `SUBJECT_USER_INVALID`, never read as the invoking user. Renaming and
2418
2485
  * changing members are the owner's, from a client.
2419
2486
  *
2420
2487
  * `recordEvent` (#3807) records an event as the member, who must be the
@@ -2438,6 +2505,10 @@ export interface FunctionContext {
2438
2505
  event: AgentSessionEvent,
2439
2506
  options?: AgentSessionSubjectOptions
2440
2507
  ): Promise<AgentSessionEventResult>;
2508
+ deleteSession(
2509
+ sessionId: string,
2510
+ options?: AgentSessionSubjectOptions
2511
+ ): Promise<AgentSessionDeleteResult>;
2441
2512
  };
2442
2513
  /**
2443
2514
  * The value of one app secret, under a `secret:<NAME>` grant (#3183).
@@ -2547,6 +2618,27 @@ export interface FunctionContext {
2547
2618
  };
2548
2619
  }
2549
2620
 
2621
+ /**
2622
+ * `FunctionContext` as `assertTrigger(ctx, kind)` leaves it: `trigger`
2623
+ * is the arm for `K`, `tool` is set for a tool call and `undefined` for
2624
+ * every other door, and `user` is the turn's initiator, never null, for the
2625
+ * three doors an agent turn opens (`tool`, `context` and `history`).
2626
+ *
2627
+ * Annotate a helper's parameter with it to take a context the caller has
2628
+ * already asserted:
2629
+ *
2630
+ * ```ts
2631
+ * async function lookup(ctx: FunctionContextFor<"tool">) {
2632
+ * return ctx.tool.variables.householdId;
2633
+ * }
2634
+ * ```
2635
+ */
2636
+ export type FunctionContextFor<K extends FunctionTriggerKind> = FunctionContext & {
2637
+ trigger: Extract<FunctionTrigger, { kind: K }>;
2638
+ tool: K extends "tool" ? FunctionToolContext : undefined;
2639
+ user: K extends "tool" | "context" | "history" ? FunctionUser : FunctionUser | null;
2640
+ };
2641
+
2550
2642
  /** How the wait between one step's retries grows. */
2551
2643
  export type StepBackoff = "constant" | "linear" | "exponential";
2552
2644
 
@@ -2647,6 +2739,38 @@ export declare class FunctionRuntimeError extends Error {
2647
2739
  */
2648
2740
  export declare function assertRuntime(required: "request" | "task"): void;
2649
2741
 
2742
+ /**
2743
+ * Refuse every door but the one this function was written for, and narrow
2744
+ * `ctx` to it.
2745
+ *
2746
+ * Make it the first line of a tool function: an agent turn calls it with
2747
+ * `ctx.tool` set, but the same function is also reachable over plain HTTP.
2748
+ * After the call, `ctx.trigger` is the named arm, `ctx.tool` needs no
2749
+ * guard, and `ctx.user` is the turn's initiator:
2750
+ *
2751
+ * ```ts
2752
+ * import { assertTrigger, defineFunction } from "primitive-functions";
2753
+ *
2754
+ * export default defineFunction(async (_input, ctx) => {
2755
+ * assertTrigger(ctx, "tool");
2756
+ * const { householdId } = ctx.tool.variables as { householdId: string };
2757
+ * await assertHouseholdMember(ctx.user.userId, householdId);
2758
+ * return { callId: ctx.trigger.callId };
2759
+ * });
2760
+ * ```
2761
+ *
2762
+ * Returns when `ctx.trigger.kind` is `kind`. Otherwise it throws an
2763
+ * `Error` naming both kinds, which the platform settles as
2764
+ * `status: "failed"` with `errorCode: "FUNCTION_THREW"`. A `kind` that is
2765
+ * not a trigger kind, or a first argument that is not a context, throws a
2766
+ * `TypeError`. It takes `ctx`, so it runs inside the handler, never at
2767
+ * module scope.
2768
+ */
2769
+ export declare function assertTrigger<K extends FunctionTriggerKind>(
2770
+ ctx: FunctionContext,
2771
+ kind: K
2772
+ ): asserts ctx is FunctionContextFor<K>;
2773
+
2650
2774
  /**
2651
2775
  * The handler a function exports, typed for one declared key.
2652
2776
  *
@@ -2893,6 +3017,7 @@ declare const _default: {
2893
3017
  pMap: typeof pMap;
2894
3018
  ulid: typeof ulid;
2895
3019
  assertRuntime: typeof assertRuntime;
3020
+ assertTrigger: typeof assertTrigger;
2896
3021
  FunctionRuntimeError: typeof FunctionRuntimeError;
2897
3022
  };
2898
3023
  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.6",
3
+ "version": "0.3.0-alpha.8",
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",