@to11ai/sdk 3.0.0-rc.9 → 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 (40) hide show
  1. package/README.md +31 -4
  2. package/dist/client.cjs +28 -23
  3. package/dist/client.d.ts +4 -0
  4. package/dist/client.js +26 -21
  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 +9480 -4788
  11. package/dist/generated/zod.gen.d.ts +8445 -5096
  12. package/dist/projectEnvironments/resource.cjs +20 -2
  13. package/dist/projectEnvironments/resource.d.ts +21 -5
  14. package/dist/projectEnvironments/resource.js +20 -2
  15. package/dist/projectEnvironments/types.d.ts +14 -1
  16. package/dist/prompts/resource.cjs +45 -3
  17. package/dist/prompts/resource.d.ts +28 -2
  18. package/dist/prompts/resource.js +57 -5
  19. package/dist/prompts/types.d.ts +33 -2
  20. package/dist/routingRules/resource.d.ts +44 -2
  21. package/dist/routingRules/types.d.ts +33 -3
  22. package/dist/transport.d.ts +5 -1
  23. package/dist/usage/resource.cjs +113 -0
  24. package/dist/usage/resource.d.ts +29 -0
  25. package/dist/usage/resource.js +78 -0
  26. package/dist/usage/types.d.ts +81 -0
  27. package/package.json +14 -6
  28. package/src/client.ts +6 -0
  29. package/src/generated/index.ts +1 -1
  30. package/src/generated/types.gen.ts +9720 -4848
  31. package/src/generated/zod.gen.ts +6103 -3109
  32. package/src/projectEnvironments/resource.ts +44 -9
  33. package/src/projectEnvironments/types.ts +19 -1
  34. package/src/prompts/resource.ts +125 -4
  35. package/src/prompts/types.ts +43 -1
  36. package/src/routingRules/resource.ts +77 -5
  37. package/src/routingRules/types.ts +35 -3
  38. package/src/transport.ts +14 -2
  39. package/src/usage/resource.ts +183 -0
  40. package/src/usage/types.ts +121 -0
@@ -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
+ }