@to11ai/sdk 3.0.0-rc.8 → 3.0.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 (42) hide show
  1. package/README.md +31 -4
  2. package/dist/client.cjs +29 -21
  3. package/dist/client.d.ts +6 -0
  4. package/dist/client.js +27 -19
  5. package/dist/errors.cjs +15 -16
  6. package/dist/errors.js +15 -15
  7. package/dist/gateway.cjs +11 -11
  8. package/dist/gateway.js +11 -11
  9. package/dist/generated/index.d.ts +1 -1
  10. package/dist/generated/types.gen.d.ts +9495 -4795
  11. package/dist/generated/zod.gen.d.ts +8260 -4911
  12. package/dist/pagination.d.ts +32 -0
  13. package/dist/projectEnvironments/resource.cjs +20 -2
  14. package/dist/projectEnvironments/resource.d.ts +21 -5
  15. package/dist/projectEnvironments/resource.js +20 -2
  16. package/dist/projectEnvironments/types.d.ts +14 -1
  17. package/dist/prompts/resource.cjs +49 -3
  18. package/dist/prompts/resource.d.ts +41 -2
  19. package/dist/prompts/resource.js +61 -5
  20. package/dist/prompts/types.d.ts +40 -15
  21. package/dist/routingRules/resource.d.ts +44 -2
  22. package/dist/routingRules/types.d.ts +33 -3
  23. package/dist/transport.d.ts +5 -1
  24. package/dist/usage/resource.cjs +113 -0
  25. package/dist/usage/resource.d.ts +29 -0
  26. package/dist/usage/resource.js +78 -0
  27. package/dist/usage/types.d.ts +81 -0
  28. package/package.json +14 -6
  29. package/src/client.ts +12 -0
  30. package/src/generated/index.ts +1 -1
  31. package/src/generated/types.gen.ts +9670 -4790
  32. package/src/generated/zod.gen.ts +6112 -3115
  33. package/src/pagination.ts +64 -0
  34. package/src/projectEnvironments/resource.ts +44 -9
  35. package/src/projectEnvironments/types.ts +19 -1
  36. package/src/prompts/resource.ts +149 -4
  37. package/src/prompts/types.ts +56 -15
  38. package/src/routingRules/resource.ts +77 -5
  39. package/src/routingRules/types.ts +35 -3
  40. package/src/transport.ts +14 -2
  41. package/src/usage/resource.ts +183 -0
  42. package/src/usage/types.ts +121 -0
@@ -6,6 +6,7 @@ import type {
6
6
  RuleSnapshot as GenRuleSnapshot,
7
7
  RuleVersion as GenRuleVersion,
8
8
  } from "../generated/types.gen";
9
+ import type { CursorPaginatedResult } from "../pagination";
9
10
 
10
11
  export type RuleProviderRef = GenRuleProviderRef;
11
12
 
@@ -26,14 +27,29 @@ export interface CreateRuleVersionInput {
26
27
 
27
28
  export type CreateRuleVersionResult = RuleVersion;
28
29
 
30
+ /**
31
+ * Input of `versions.list()`. `cursor` is the previous page's
32
+ * `pagination.nextCursor`; `limit` is the page size (the server clamps it
33
+ * into 1..100 and defaults to 50).
34
+ */
29
35
  export interface ListRuleVersionsInput {
30
36
  projectId: string;
31
37
  ruleId: string;
32
- cursor?: number;
38
+ cursor?: string;
33
39
  limit?: number;
34
40
  }
35
41
 
36
- export type ListRuleVersionsResult = RuleVersion[];
42
+ /**
43
+ * Input of `versions.iterate()`: `versions.list()` without `cursor`, which the
44
+ * iterator manages. `limit` is the page size of each request.
45
+ */
46
+ export type IterateRuleVersionsInput = Omit<ListRuleVersionsInput, "cursor">;
47
+
48
+ /**
49
+ * One page of a rule's versions, newest first (`version` descending):
50
+ * `{ items, pagination: { nextCursor, limit } }`.
51
+ */
52
+ export type ListRuleVersionsResult = CursorPaginatedResult<RuleVersion>;
37
53
 
38
54
  export interface GetRuleVersionInput {
39
55
  projectId: string;
@@ -77,6 +93,12 @@ export interface RollbackRuleReleaseInput {
77
93
 
78
94
  export type RollbackRuleReleaseResult = RuleRelease;
79
95
 
96
+ /**
97
+ * Input of `releases.list()`. Without `envSlug` the call lists the rule's
98
+ * releases across every environment; with it, one environment's. `cursor` is
99
+ * the previous page's `pagination.nextCursor`; `limit` is the page size (the
100
+ * server clamps it into 1..100 and defaults to 50).
101
+ */
80
102
  export interface ListRuleReleasesInput {
81
103
  projectId: string;
82
104
  ruleId: string;
@@ -85,4 +107,14 @@ export interface ListRuleReleasesInput {
85
107
  limit?: number;
86
108
  }
87
109
 
88
- export type ListRuleReleasesResult = RuleRelease[];
110
+ /**
111
+ * Input of `releases.iterate()`: `releases.list()` without `cursor`, which the
112
+ * iterator manages. `limit` is the page size of each request.
113
+ */
114
+ export type IterateRuleReleasesInput = Omit<ListRuleReleasesInput, "cursor">;
115
+
116
+ /**
117
+ * One page of a rule's releases, newest first:
118
+ * `{ items, pagination: { nextCursor, limit } }`.
119
+ */
120
+ export type ListRuleReleasesResult = CursorPaginatedResult<RuleRelease>;
package/src/transport.ts CHANGED
@@ -393,7 +393,14 @@ export class Transport {
393
393
  method: string,
394
394
  path: string,
395
395
  options?: {
396
- query?: Record<string, string | number | boolean | undefined>;
396
+ /**
397
+ * Query params. An array value repeats the param once per element
398
+ * (`?groupBy=model&groupBy=project`); `undefined` or `null` omits it.
399
+ */
400
+ query?: Record<
401
+ string,
402
+ string | number | boolean | ReadonlyArray<string> | null | undefined
403
+ >;
397
404
  body?: unknown;
398
405
  /**
399
406
  * Runtime validator for the envelope `data` on a 2xx response. When
@@ -408,7 +415,12 @@ export class Transport {
408
415
 
409
416
  if (options?.query) {
410
417
  for (const [key, value] of Object.entries(options.query)) {
411
- if (value !== undefined) {
418
+ // `null` too: an untyped caller's null means "not set", and
419
+ // `typeof null` is "object", which the array branch would iterate.
420
+ if (value === undefined || value === null) continue;
421
+ if (Array.isArray(value)) {
422
+ for (const item of value) url.searchParams.append(key, item);
423
+ } else {
412
424
  url.searchParams.set(key, String(value));
413
425
  }
414
426
  }
@@ -0,0 +1,183 @@
1
+ // ---------------------------------------------------------------------------
2
+ // UsageResource — the organization's usage reads (TO11-3689 / TO11-3690).
3
+ // ---------------------------------------------------------------------------
4
+ // Spec: docs/specs/2026-10-20-to11-assistant/SPEC.md § Tools
5
+ //
6
+ // Org-scoped: every method takes the `orgId` it reads. Figures carry both
7
+ // what was billed and the API-price equivalent; every response lists the
8
+ // readable projects it left out (`excludedProjects`).
9
+ // ---------------------------------------------------------------------------
10
+
11
+ import { To11aiValidationError } from "../errors";
12
+ import {
13
+ zUsageGetUsageCostsResponse,
14
+ zUsageListTopTracesResponse,
15
+ zUsageListUsagePrincipalsResponse,
16
+ zUsageListUsageSessionsResponse,
17
+ } from "../generated/zod.gen";
18
+ import type { ApiEnvelope, Transport } from "../transport";
19
+ import type {
20
+ FindPrincipalsInput,
21
+ FindPrincipalsResult,
22
+ GetCostsInput,
23
+ GetCostsResult,
24
+ ListSessionsInput,
25
+ ListSessionsResult,
26
+ ListTopTracesInput,
27
+ ListTopTracesResult,
28
+ UsageWindow,
29
+ } from "./types";
30
+
31
+ export type {
32
+ CostGroup,
33
+ CostGroupBy,
34
+ FindPrincipalsInput,
35
+ FindPrincipalsResult,
36
+ GetCostsInput,
37
+ GetCostsResult,
38
+ ListSessionsInput,
39
+ ListSessionsResult,
40
+ ListTopTracesInput,
41
+ ListTopTracesResult,
42
+ ResolvedUsageWindow,
43
+ TopTrace,
44
+ TopTraceStatus,
45
+ TopTracesSort,
46
+ UsageBillingMode,
47
+ UsageExcludedProject,
48
+ UsagePrincipal,
49
+ UsageRunRow,
50
+ UsageSessionRow,
51
+ UsageSessionsGroupBy,
52
+ UsageWindow,
53
+ UsageWindowPreset,
54
+ } from "./types";
55
+
56
+ // The response schemas are the `data` of the generated envelope schemas, so a
57
+ // wrong-shaped response raises To11aiResponseValidationError.
58
+ const principalsData = zUsageListUsagePrincipalsResponse.shape.data.unwrap();
59
+ const sessionsData = zUsageListUsageSessionsResponse.shape.data.unwrap();
60
+ const costsData = zUsageGetUsageCostsResponse.shape.data.unwrap();
61
+ const topTracesData = zUsageListTopTracesResponse.shape.data.unwrap();
62
+
63
+ type Query = Record<
64
+ string,
65
+ string | number | boolean | ReadonlyArray<string> | undefined
66
+ >;
67
+
68
+ function unwrap<T>(envelope: ApiEnvelope<T>): T {
69
+ if (envelope.data === null || envelope.data === undefined) {
70
+ throw new Error("API returned success but data is null");
71
+ }
72
+ return envelope.data;
73
+ }
74
+
75
+ function orgPath(orgId: string, rest: string): string {
76
+ return `/v1/orgs/${encodeURIComponent(orgId)}/${rest}`;
77
+ }
78
+
79
+ /** The window as the API's query params: `from`+`to`, or `preset` (+`n`) + `timezone`. */
80
+ function windowQuery(window: UsageWindow): Query {
81
+ if ("from" in window) return { from: window.from, to: window.to };
82
+ return { preset: window.preset, n: window.n, timezone: window.timezone };
83
+ }
84
+
85
+ /**
86
+ * The lookup's one query param, `query` (a name or part of one). The API
87
+ * dropped the `email` form (TO11-3860); a missing query is refused before
88
+ * any request.
89
+ */
90
+ function principalsQuery(input: FindPrincipalsInput): Query {
91
+ const { query } = input as { readonly query?: string | null };
92
+ if (typeof query === "string") return { query };
93
+ throw new To11aiValidationError(
94
+ "[to11ai/sdk] findPrincipals() needs `query`",
95
+ "invalid_principal_lookup",
96
+ { query: false },
97
+ );
98
+ }
99
+
100
+ /**
101
+ * SDK resource for the organization usage reads. Constructed by
102
+ * `createClient` and exposed as `client.usage`.
103
+ */
104
+ export class UsageResource {
105
+ private readonly transport: Transport;
106
+
107
+ constructor(transport: Transport) {
108
+ this.transport = transport;
109
+ }
110
+
111
+ /**
112
+ * Members of the organization by name (`query`), as
113
+ * `{ principalId, displayName }`. Pass a `principalId` to the other reads.
114
+ */
115
+ async findPrincipals(
116
+ input: FindPrincipalsInput,
117
+ ): Promise<FindPrincipalsResult> {
118
+ const query = principalsQuery(input);
119
+ const envelope = await this.transport.request<FindPrincipalsResult>(
120
+ "GET",
121
+ orgPath(input.orgId, "usage/principals"),
122
+ {
123
+ query,
124
+ decode: (data) => principalsData.parse(data) as FindPrincipalsResult,
125
+ },
126
+ );
127
+ return unwrap(envelope);
128
+ }
129
+
130
+ /**
131
+ * Sessions (or runs, with `groupBy: "run"`) with usage in the window, one
132
+ * page at a time, plus totals over all of them and the usage outside any
133
+ * session. Pass `pagination.nextCursor` back as `cursor` until it is `null`.
134
+ */
135
+ async listSessions(input: ListSessionsInput): Promise<ListSessionsResult> {
136
+ const { orgId, window, ...filters } = input;
137
+ const envelope = await this.transport.request<ListSessionsResult>(
138
+ "GET",
139
+ orgPath(orgId, "usage/sessions"),
140
+ {
141
+ query: { ...windowQuery(window), ...filters },
142
+ decode: (data) => sessionsData.parse(data) as ListSessionsResult,
143
+ },
144
+ );
145
+ return unwrap(envelope);
146
+ }
147
+
148
+ /**
149
+ * Billed cost and API-price equivalent in the window, grouped by up to six
150
+ * keys (`groupBy`), with totals and a per-billing-mode split.
151
+ */
152
+ async getCosts(input: GetCostsInput): Promise<GetCostsResult> {
153
+ const { orgId, window, groupBy, ...filters } = input;
154
+ const envelope = await this.transport.request<GetCostsResult>(
155
+ "GET",
156
+ orgPath(orgId, "usage/costs"),
157
+ {
158
+ query: {
159
+ ...windowQuery(window),
160
+ // Repeated param (`?groupBy=model&groupBy=project`).
161
+ groupBy: typeof groupBy === "string" ? [groupBy] : groupBy,
162
+ ...filters,
163
+ },
164
+ decode: (data) => costsData.parse(data) as GetCostsResult,
165
+ },
166
+ );
167
+ return unwrap(envelope);
168
+ }
169
+
170
+ /** The slowest, costliest, or largest traces in the window (at most 60 days). */
171
+ async listTopTraces(input: ListTopTracesInput): Promise<ListTopTracesResult> {
172
+ const { orgId, window, ...filters } = input;
173
+ const envelope = await this.transport.request<ListTopTracesResult>(
174
+ "GET",
175
+ orgPath(orgId, "traces/top"),
176
+ {
177
+ query: { ...windowQuery(window), ...filters },
178
+ decode: (data) => topTracesData.parse(data) as ListTopTracesResult,
179
+ },
180
+ );
181
+ return unwrap(envelope);
182
+ }
183
+ }
@@ -0,0 +1,121 @@
1
+ import type {
2
+ TopTracesResponse,
3
+ UsageCostsResponse,
4
+ UsageListUsageSessionsData,
5
+ UsagePrincipalsResponse,
6
+ UsageSessionsResponse,
7
+ } from "../generated/types.gen";
8
+
9
+ // ─── Window ────────────────────────────────────────────────────────────────
10
+
11
+ /** A preset the server resolves in the window's `timezone`. Weeks start on Monday. */
12
+ export type UsageWindowPreset =
13
+ | "last_n_hours"
14
+ | "last_n_days"
15
+ | "today"
16
+ | "yesterday"
17
+ | "this_week"
18
+ | "last_week"
19
+ | "this_month"
20
+ | "last_month";
21
+
22
+ /**
23
+ * The time window of a usage read: absolute (`from`/`to`, ISO 8601 with a
24
+ * zone offset, `[from, to)`), or a preset the server resolves in an IANA
25
+ * `timezone`. `n` is required by `last_n_hours` / `last_n_days` and rejected
26
+ * by the calendar presets. Every response echoes the absolute window it used.
27
+ */
28
+ export type UsageWindow =
29
+ | { readonly from: string; readonly to: string }
30
+ | {
31
+ readonly preset: UsageWindowPreset;
32
+ readonly n?: number;
33
+ readonly timezone: string;
34
+ };
35
+
36
+ // ─── Shared filters ────────────────────────────────────────────────────────
37
+
38
+ export type UsageBillingMode = "subscription" | "api" | "unknown";
39
+ export type UsageSessionsGroupBy = NonNullable<
40
+ NonNullable<UsageListUsageSessionsData["query"]>["groupBy"]
41
+ >;
42
+ export type CostGroupBy =
43
+ | "principal"
44
+ | "vendor"
45
+ | "provider"
46
+ | "model"
47
+ | "project"
48
+ | "billingMode";
49
+ export type TopTracesSort = TopTracesResponse["sort"];
50
+ export type TopTraceStatus = TopTracesResponse["items"][number]["status"];
51
+
52
+ // ─── Results ───────────────────────────────────────────────────────────────
53
+
54
+ export type FindPrincipalsResult = UsagePrincipalsResponse;
55
+ export type UsagePrincipal = FindPrincipalsResult["items"][number];
56
+ export type ListSessionsResult = UsageSessionsResponse;
57
+ export type UsageSessionRow = NonNullable<
58
+ ListSessionsResult["sessions"]
59
+ >[number];
60
+ export type UsageRunRow = NonNullable<ListSessionsResult["runs"]>[number];
61
+ export type GetCostsResult = UsageCostsResponse;
62
+ export type CostGroup = GetCostsResult["groups"][number];
63
+ export type ListTopTracesResult = TopTracesResponse;
64
+ export type TopTrace = ListTopTracesResult["items"][number];
65
+ /** The absolute window a usage response used, plus the preset it came from. */
66
+ export type ResolvedUsageWindow = GetCostsResult["window"];
67
+ /** A readable project a read left out, so totals are never silently partial. */
68
+ export type UsageExcludedProject =
69
+ FindPrincipalsResult["excludedProjects"][number];
70
+
71
+ // ─── Inputs ────────────────────────────────────────────────────────────────
72
+
73
+ /** Look a member up by name (`query`, matched in the API). */
74
+ export type FindPrincipalsInput = {
75
+ readonly orgId: string;
76
+ readonly query: string;
77
+ };
78
+
79
+ export interface ListSessionsInput {
80
+ readonly orgId: string;
81
+ readonly window: UsageWindow;
82
+ /** `session` (default) or `run`. */
83
+ readonly groupBy?: UsageSessionsGroupBy;
84
+ readonly principalId?: string;
85
+ readonly projectId?: string;
86
+ readonly billingMode?: UsageBillingMode;
87
+ /** `pagination.nextCursor` of the previous page. */
88
+ readonly cursor?: string;
89
+ /** Clamped by the server into [1, 50]; default 20. */
90
+ readonly limit?: number;
91
+ }
92
+
93
+ export interface GetCostsInput {
94
+ readonly orgId: string;
95
+ readonly window: UsageWindow;
96
+ /** One key or several; the groups are the cross product. */
97
+ readonly groupBy?: CostGroupBy | ReadonlyArray<CostGroupBy>;
98
+ readonly principalId?: string;
99
+ readonly vendor?: string;
100
+ readonly provider?: string;
101
+ readonly model?: string;
102
+ readonly projectId?: string;
103
+ readonly billingMode?: UsageBillingMode;
104
+ /** 1–50; default 10. Groups beyond it count in `totals` and `otherGroups`. */
105
+ readonly limit?: number;
106
+ }
107
+
108
+ export interface ListTopTracesInput {
109
+ readonly orgId: string;
110
+ /** At most 60 days. */
111
+ readonly window: UsageWindow;
112
+ readonly sort: TopTracesSort;
113
+ readonly principalId?: string;
114
+ readonly projectId?: string;
115
+ readonly vendor?: string;
116
+ readonly provider?: string;
117
+ readonly model?: string;
118
+ readonly status?: TopTraceStatus;
119
+ /** 1–20; default 10. */
120
+ readonly limit?: number;
121
+ }