@to11ai/sdk 3.1.0-rc → 4.0.0-rc.1
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.
- package/README.md +30 -42
- package/dist/behaviors/store.d.ts +1 -1
- package/dist/client.cjs +17 -71
- package/dist/client.d.ts +16 -112
- package/dist/client.js +22 -72
- package/dist/config.d.ts +3 -6
- package/dist/errors.cjs +0 -72
- package/dist/errors.d.ts +0 -77
- package/dist/errors.js +0 -72
- package/dist/generated/index.d.ts +1 -1
- package/dist/generated/types.gen.d.ts +2962 -17155
- package/dist/generated/zod.gen.d.ts +2576 -11477
- package/dist/projectEnvironments/resource.cjs +0 -2
- package/dist/projectEnvironments/resource.d.ts +3 -12
- package/dist/projectEnvironments/resource.js +0 -2
- package/dist/projectEnvironments/types.d.ts +0 -2
- package/dist/{gateway.d.ts → prompts/convert.d.ts} +12 -75
- package/dist/prompts/resource.d.ts +7 -7
- package/dist/prompts/shape.d.ts +4 -5
- package/dist/prompts/types.d.ts +5 -17
- package/package.json +7 -24
- package/src/behaviors/store.ts +1 -1
- package/src/client.ts +36 -242
- package/src/config.ts +3 -9
- package/src/errors.ts +0 -132
- package/src/generated/index.ts +1 -1
- package/src/generated/types.gen.ts +2889 -17652
- package/src/generated/zod.gen.ts +1980 -9967
- package/src/projectEnvironments/resource.ts +3 -14
- package/src/projectEnvironments/types.ts +0 -2
- package/src/{gateway.ts → prompts/convert.ts} +18 -152
- package/src/prompts/resource.ts +10 -12
- package/src/prompts/shape.ts +4 -5
- package/src/prompts/types.ts +5 -17
- package/src/transport.ts +0 -133
- package/dist/context.d.ts +0 -124
- package/dist/gateway.cjs +0 -264
- package/dist/gateway.js +0 -226
- package/dist/routingRules/resource.d.ts +0 -64
- package/dist/routingRules/types.d.ts +0 -88
- package/dist/traceparent.d.ts +0 -61
- package/dist/usage/resource.cjs +0 -113
- package/dist/usage/resource.d.ts +0 -29
- package/dist/usage/resource.js +0 -78
- package/dist/usage/types.d.ts +0 -81
- package/src/context.ts +0 -196
- package/src/routingRules/resource.ts +0 -261
- package/src/routingRules/types.ts +0 -120
- package/src/traceparent.ts +0 -189
- package/src/usage/resource.ts +0 -183
- package/src/usage/types.ts +0 -121
|
@@ -1,261 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
zRuleReleasesListRuleReleasesEnvResponse,
|
|
3
|
-
zRuleReleasesListRuleReleasesResponse,
|
|
4
|
-
zRuleVersion,
|
|
5
|
-
zRuleVersionsListRuleVersionsResponse,
|
|
6
|
-
} from "../generated/zod.gen";
|
|
7
|
-
import { iterateCursorItems } from "../pagination";
|
|
8
|
-
import type { ApiEnvelope, Transport } from "../transport";
|
|
9
|
-
import type {
|
|
10
|
-
CreateRuleVersionInput,
|
|
11
|
-
CreateRuleVersionResult,
|
|
12
|
-
GetRuleVersionInput,
|
|
13
|
-
GetRuleVersionResult,
|
|
14
|
-
IterateRuleReleasesInput,
|
|
15
|
-
IterateRuleVersionsInput,
|
|
16
|
-
ListRuleReleasesInput,
|
|
17
|
-
ListRuleReleasesResult,
|
|
18
|
-
ListRuleVersionsInput,
|
|
19
|
-
ListRuleVersionsResult,
|
|
20
|
-
RollbackRuleReleaseInput,
|
|
21
|
-
RollbackRuleReleaseResult,
|
|
22
|
-
RuleRelease,
|
|
23
|
-
RuleVersion,
|
|
24
|
-
SetRuleReleaseInput,
|
|
25
|
-
SetRuleReleaseResult,
|
|
26
|
-
} from "./types";
|
|
27
|
-
|
|
28
|
-
export type {
|
|
29
|
-
CreateRuleVersionInput,
|
|
30
|
-
CreateRuleVersionResult,
|
|
31
|
-
GetRuleVersionInput,
|
|
32
|
-
GetRuleVersionResult,
|
|
33
|
-
IterateRuleReleasesInput,
|
|
34
|
-
IterateRuleVersionsInput,
|
|
35
|
-
ListRuleReleasesInput,
|
|
36
|
-
ListRuleReleasesResult,
|
|
37
|
-
ListRuleVersionsInput,
|
|
38
|
-
ListRuleVersionsResult,
|
|
39
|
-
RollbackRuleReleaseInput,
|
|
40
|
-
RollbackRuleReleaseResult,
|
|
41
|
-
RuleModelBinding,
|
|
42
|
-
RuleProviderRef,
|
|
43
|
-
RuleRelease,
|
|
44
|
-
RuleReleaseActorType,
|
|
45
|
-
RuleReleaseApprovalStatus,
|
|
46
|
-
RuleReleaseChangeKind,
|
|
47
|
-
RuleReleaseStrategy,
|
|
48
|
-
RuleReleaseWeight,
|
|
49
|
-
RuleSnapshot,
|
|
50
|
-
RuleStrategy,
|
|
51
|
-
RuleVersion,
|
|
52
|
-
SetRuleReleaseInput,
|
|
53
|
-
SetRuleReleaseResult,
|
|
54
|
-
} from "./types";
|
|
55
|
-
|
|
56
|
-
// The page schemas are the `data` of the generated envelope schemas.
|
|
57
|
-
const versionsPage = zRuleVersionsListRuleVersionsResponse.shape.data.unwrap();
|
|
58
|
-
const releasesPage = zRuleReleasesListRuleReleasesResponse.shape.data.unwrap();
|
|
59
|
-
const releasesEnvPage =
|
|
60
|
-
zRuleReleasesListRuleReleasesEnvResponse.shape.data.unwrap();
|
|
61
|
-
|
|
62
|
-
function unwrap<T>(envelope: ApiEnvelope<T>): T {
|
|
63
|
-
if (envelope.data === null || envelope.data === undefined) {
|
|
64
|
-
throw new Error("API returned success but data is null");
|
|
65
|
-
}
|
|
66
|
-
return envelope.data;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
function rulePath(projectId: string, ruleId: string): string {
|
|
70
|
-
return `/v1/projects/${encodeURIComponent(projectId)}/routing/rules/${encodeURIComponent(ruleId)}`;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function versionsPath(projectId: string, ruleId: string): string {
|
|
74
|
-
return `${rulePath(projectId, ruleId)}/versions`;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
function versionPath(
|
|
78
|
-
projectId: string,
|
|
79
|
-
ruleId: string,
|
|
80
|
-
versionId: string,
|
|
81
|
-
): string {
|
|
82
|
-
return `${versionsPath(projectId, ruleId)}/${encodeURIComponent(versionId)}`;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
function releasesPath(projectId: string, ruleId: string): string {
|
|
86
|
-
return `${rulePath(projectId, ruleId)}/releases`;
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
function releasePath(
|
|
90
|
-
projectId: string,
|
|
91
|
-
ruleId: string,
|
|
92
|
-
envSlug: string,
|
|
93
|
-
): string {
|
|
94
|
-
return `${releasesPath(projectId, ruleId)}/${encodeURIComponent(envSlug)}`;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
function releaseRollbackPath(
|
|
98
|
-
projectId: string,
|
|
99
|
-
ruleId: string,
|
|
100
|
-
envSlug: string,
|
|
101
|
-
): string {
|
|
102
|
-
return `${releasePath(projectId, ruleId, envSlug)}/rollback`;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
export class RoutingRuleVersionResource {
|
|
106
|
-
private readonly transport: Transport;
|
|
107
|
-
|
|
108
|
-
constructor(transport: Transport) {
|
|
109
|
-
this.transport = transport;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
async create(
|
|
113
|
-
input: CreateRuleVersionInput,
|
|
114
|
-
): Promise<CreateRuleVersionResult> {
|
|
115
|
-
const { projectId, ruleId, ...body } = input;
|
|
116
|
-
const envelope = await this.transport.request<CreateRuleVersionResult>(
|
|
117
|
-
"POST",
|
|
118
|
-
versionsPath(projectId, ruleId),
|
|
119
|
-
{ body },
|
|
120
|
-
);
|
|
121
|
-
return unwrap(envelope);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* One page of a rule's versions, newest first. The result mirrors the wire
|
|
126
|
-
* shape, `{ items, pagination: { nextCursor, limit } }`; pass
|
|
127
|
-
* `pagination.nextCursor` back as `cursor` until it is `null`. Pass only a
|
|
128
|
-
* cursor this list returned: a malformed or unreadable one answers 400
|
|
129
|
-
* `invalid_cursor`.
|
|
130
|
-
*/
|
|
131
|
-
async list(input: ListRuleVersionsInput): Promise<ListRuleVersionsResult> {
|
|
132
|
-
const query: Record<string, string | number | undefined> = {};
|
|
133
|
-
if (input.cursor !== undefined) query.cursor = input.cursor;
|
|
134
|
-
if (input.limit !== undefined) query.limit = input.limit;
|
|
135
|
-
const envelope = await this.transport.request<ListRuleVersionsResult>(
|
|
136
|
-
"GET",
|
|
137
|
-
versionsPath(input.projectId, input.ruleId),
|
|
138
|
-
{
|
|
139
|
-
query,
|
|
140
|
-
decode: (data) => versionsPage.parse(data) as ListRuleVersionsResult,
|
|
141
|
-
},
|
|
142
|
-
);
|
|
143
|
-
return unwrap(envelope);
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* Every version of a rule, newest first: walks `list()` page by page until
|
|
148
|
-
* `pagination.nextCursor` is `null` and yields one `RuleVersion` at a time.
|
|
149
|
-
* `limit` sets the page size of each request. Throws a `To11aiError` if the
|
|
150
|
-
* server repeats a cursor.
|
|
151
|
-
*
|
|
152
|
-
* ```ts
|
|
153
|
-
* for await (const version of rules.versions.iterate({ projectId, ruleId })) {
|
|
154
|
-
* console.log(version.version);
|
|
155
|
-
* }
|
|
156
|
-
* ```
|
|
157
|
-
*/
|
|
158
|
-
iterate(
|
|
159
|
-
input: IterateRuleVersionsInput,
|
|
160
|
-
): AsyncGenerator<RuleVersion, void, undefined> {
|
|
161
|
-
return iterateCursorItems((cursor) => this.list({ ...input, cursor }));
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
async get(input: GetRuleVersionInput): Promise<GetRuleVersionResult> {
|
|
165
|
-
const envelope = await this.transport.request<GetRuleVersionResult>(
|
|
166
|
-
"GET",
|
|
167
|
-
versionPath(input.projectId, input.ruleId, input.versionId),
|
|
168
|
-
{
|
|
169
|
-
decode: (data) => zRuleVersion.parse(data) as GetRuleVersionResult,
|
|
170
|
-
},
|
|
171
|
-
);
|
|
172
|
-
return unwrap(envelope);
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
export class RoutingRuleReleaseResource {
|
|
177
|
-
private readonly transport: Transport;
|
|
178
|
-
|
|
179
|
-
constructor(transport: Transport) {
|
|
180
|
-
this.transport = transport;
|
|
181
|
-
}
|
|
182
|
-
|
|
183
|
-
async set(input: SetRuleReleaseInput): Promise<SetRuleReleaseResult> {
|
|
184
|
-
const { projectId, ruleId, envSlug, ...body } = input;
|
|
185
|
-
const envelope = await this.transport.request<SetRuleReleaseResult>(
|
|
186
|
-
"PUT",
|
|
187
|
-
releasePath(projectId, ruleId, envSlug),
|
|
188
|
-
{ body },
|
|
189
|
-
);
|
|
190
|
-
return unwrap(envelope);
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
async rollback(
|
|
194
|
-
input: RollbackRuleReleaseInput,
|
|
195
|
-
): Promise<RollbackRuleReleaseResult> {
|
|
196
|
-
const { projectId, ruleId, envSlug, ...body } = input;
|
|
197
|
-
const envelope = await this.transport.request<RollbackRuleReleaseResult>(
|
|
198
|
-
"POST",
|
|
199
|
-
releaseRollbackPath(projectId, ruleId, envSlug),
|
|
200
|
-
{ body },
|
|
201
|
-
);
|
|
202
|
-
return unwrap(envelope);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* One page of a rule's releases, newest first. Without `envSlug` it lists
|
|
207
|
-
* every environment's releases; with it, one environment's. The result
|
|
208
|
-
* mirrors the wire shape, `{ items, pagination: { nextCursor, limit } }`;
|
|
209
|
-
* pass `pagination.nextCursor` back as `cursor` until it is `null`. Pass
|
|
210
|
-
* only a cursor this list returned: a malformed or unreadable one answers
|
|
211
|
-
* 400 `invalid_cursor`, while a well-formed cursor from another list is
|
|
212
|
-
* accepted and continues from its position, which can skip releases.
|
|
213
|
-
*/
|
|
214
|
-
async list(input: ListRuleReleasesInput): Promise<ListRuleReleasesResult> {
|
|
215
|
-
const query: Record<string, string | number | undefined> = {};
|
|
216
|
-
if (input.cursor !== undefined) query.cursor = input.cursor;
|
|
217
|
-
if (input.limit !== undefined) query.limit = input.limit;
|
|
218
|
-
const page = input.envSlug !== undefined ? releasesEnvPage : releasesPage;
|
|
219
|
-
const path =
|
|
220
|
-
input.envSlug !== undefined
|
|
221
|
-
? releasePath(input.projectId, input.ruleId, input.envSlug)
|
|
222
|
-
: releasesPath(input.projectId, input.ruleId);
|
|
223
|
-
const envelope = await this.transport.request<ListRuleReleasesResult>(
|
|
224
|
-
"GET",
|
|
225
|
-
path,
|
|
226
|
-
{
|
|
227
|
-
query,
|
|
228
|
-
decode: (data) => page.parse(data) as ListRuleReleasesResult,
|
|
229
|
-
},
|
|
230
|
-
);
|
|
231
|
-
return unwrap(envelope);
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
/**
|
|
235
|
-
* Every release of a rule, newest first: walks `list()` page by page until
|
|
236
|
-
* `pagination.nextCursor` is `null` and yields one `RuleRelease` at a time.
|
|
237
|
-
* `limit` sets the page size of each request. Throws a `To11aiError` if the
|
|
238
|
-
* server repeats a cursor.
|
|
239
|
-
*
|
|
240
|
-
* ```ts
|
|
241
|
-
* for await (const release of rules.releases.iterate({ projectId, ruleId })) {
|
|
242
|
-
* console.log(release.envSlug, release.changeKind);
|
|
243
|
-
* }
|
|
244
|
-
* ```
|
|
245
|
-
*/
|
|
246
|
-
iterate(
|
|
247
|
-
input: IterateRuleReleasesInput,
|
|
248
|
-
): AsyncGenerator<RuleRelease, void, undefined> {
|
|
249
|
-
return iterateCursorItems((cursor) => this.list({ ...input, cursor }));
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
export class RoutingRulesResource {
|
|
254
|
-
readonly versions: RoutingRuleVersionResource;
|
|
255
|
-
readonly releases: RoutingRuleReleaseResource;
|
|
256
|
-
|
|
257
|
-
constructor(transport: Transport) {
|
|
258
|
-
this.versions = new RoutingRuleVersionResource(transport);
|
|
259
|
-
this.releases = new RoutingRuleReleaseResource(transport);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
RuleModelBinding as GenRuleModelBinding,
|
|
3
|
-
RuleProviderRef as GenRuleProviderRef,
|
|
4
|
-
RuleRelease as GenRuleRelease,
|
|
5
|
-
RuleReleaseWeight as GenRuleReleaseWeight,
|
|
6
|
-
RuleSnapshot as GenRuleSnapshot,
|
|
7
|
-
RuleVersion as GenRuleVersion,
|
|
8
|
-
} from "../generated/types.gen";
|
|
9
|
-
import type { CursorPaginatedResult } from "../pagination";
|
|
10
|
-
|
|
11
|
-
export type RuleProviderRef = GenRuleProviderRef;
|
|
12
|
-
|
|
13
|
-
export type RuleModelBinding = GenRuleModelBinding;
|
|
14
|
-
|
|
15
|
-
export type RuleStrategy = GenRuleSnapshot["strategy"];
|
|
16
|
-
|
|
17
|
-
export type RuleSnapshot = GenRuleSnapshot;
|
|
18
|
-
|
|
19
|
-
export type RuleVersion = GenRuleVersion;
|
|
20
|
-
|
|
21
|
-
export interface CreateRuleVersionInput {
|
|
22
|
-
projectId: string;
|
|
23
|
-
ruleId: string;
|
|
24
|
-
snapshot: RuleSnapshot | unknown;
|
|
25
|
-
description?: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type CreateRuleVersionResult = RuleVersion;
|
|
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
|
-
*/
|
|
35
|
-
export interface ListRuleVersionsInput {
|
|
36
|
-
projectId: string;
|
|
37
|
-
ruleId: string;
|
|
38
|
-
cursor?: string;
|
|
39
|
-
limit?: number;
|
|
40
|
-
}
|
|
41
|
-
|
|
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>;
|
|
53
|
-
|
|
54
|
-
export interface GetRuleVersionInput {
|
|
55
|
-
projectId: string;
|
|
56
|
-
ruleId: string;
|
|
57
|
-
versionId: string;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export type GetRuleVersionResult = RuleVersion;
|
|
61
|
-
|
|
62
|
-
export type RuleReleaseStrategy = GenRuleRelease["strategy"];
|
|
63
|
-
|
|
64
|
-
export type RuleReleaseChangeKind = GenRuleRelease["changeKind"];
|
|
65
|
-
|
|
66
|
-
export type RuleReleaseApprovalStatus = GenRuleRelease["approvalStatus"];
|
|
67
|
-
|
|
68
|
-
export type RuleReleaseActorType = GenRuleRelease["actorType"];
|
|
69
|
-
|
|
70
|
-
export type RuleReleaseWeight = GenRuleReleaseWeight;
|
|
71
|
-
|
|
72
|
-
export type RuleRelease = GenRuleRelease;
|
|
73
|
-
|
|
74
|
-
export interface SetRuleReleaseInput {
|
|
75
|
-
projectId: string;
|
|
76
|
-
ruleId: string;
|
|
77
|
-
envSlug: string;
|
|
78
|
-
strategy: RuleReleaseStrategy;
|
|
79
|
-
versionId?: string;
|
|
80
|
-
weights?: RuleReleaseWeight[];
|
|
81
|
-
reason?: string;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
export type SetRuleReleaseResult = RuleRelease;
|
|
85
|
-
|
|
86
|
-
export interface RollbackRuleReleaseInput {
|
|
87
|
-
projectId: string;
|
|
88
|
-
ruleId: string;
|
|
89
|
-
envSlug: string;
|
|
90
|
-
reason: string;
|
|
91
|
-
targetReleaseId?: string;
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export type RollbackRuleReleaseResult = RuleRelease;
|
|
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
|
-
*/
|
|
102
|
-
export interface ListRuleReleasesInput {
|
|
103
|
-
projectId: string;
|
|
104
|
-
ruleId: string;
|
|
105
|
-
envSlug?: string;
|
|
106
|
-
cursor?: string;
|
|
107
|
-
limit?: number;
|
|
108
|
-
}
|
|
109
|
-
|
|
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/traceparent.ts
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* W3C Trace Context propagation for outbound SDK calls.
|
|
3
|
-
*
|
|
4
|
-
* Reads the host process's active OpenTelemetry span — if any — and formats a
|
|
5
|
-
* W3C `traceparent` (and optional `tracestate`) string for injection as
|
|
6
|
-
* outbound HTTP headers. Lets customers correlate their tracer's spans with
|
|
7
|
-
* the gateway-emitted span we record.
|
|
8
|
-
*
|
|
9
|
-
* Design constraints (from TO11-1016 / S8c spec):
|
|
10
|
-
* 1. NO runtime dependency on `@opentelemetry/api`. The package is detected
|
|
11
|
-
* at runtime via a try/require shim; if it is not loadable, this module
|
|
12
|
-
* is a strict no-op and `activeTraceContext()` returns `null`.
|
|
13
|
-
* 2. NO SDK config surface change. There is no constructor option or env
|
|
14
|
-
* var; behavior is invisible if the host has no OTel installed.
|
|
15
|
-
* 3. Header omitted, not zero-filled — W3C §3.2.2.5 forbids all-zero
|
|
16
|
-
* trace/span ids in `traceparent`. We return `null` (caller omits).
|
|
17
|
-
*
|
|
18
|
-
* @see https://www.w3.org/TR/trace-context/ for the wire format.
|
|
19
|
-
* @see docs/archive-2026-08/specs/2026-05-20-to11-1016-sdk-traceparent-propagation.md
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
|
-
// NOTE: deliberately NO top-level `import ... from "node:module"`. With the
|
|
23
|
-
// SDK's `--external '*'` build that import is emitted verbatim into the ESM
|
|
24
|
-
// `dist/client.js`, and a static `node:module` import is resolved at module
|
|
25
|
-
// initialization — *before* any try/catch in `resolveRequire()` can run. In
|
|
26
|
-
// edge runtimes that lack `node:module` (Cloudflare Workers without
|
|
27
|
-
// `nodejs_compat`, bare Service Workers) that is a hard module-load failure,
|
|
28
|
-
// not the spec-R2 graceful no-op. We instead reach `node:module` lazily and
|
|
29
|
-
// optionally via `process.getBuiltinModule`, which is absent (not throwing) in
|
|
30
|
-
// those runtimes.
|
|
31
|
-
|
|
32
|
-
// Minimal structural typing of the OTel API surface we actually use. Keeping
|
|
33
|
-
// this local — rather than importing types from `@opentelemetry/api` — is
|
|
34
|
-
// what keeps the SDK zero-dep at the TypeScript layer too. Source types:
|
|
35
|
-
// https://github.com/open-telemetry/opentelemetry-js-api/
|
|
36
|
-
interface OtelTraceState {
|
|
37
|
-
serialize(): string;
|
|
38
|
-
}
|
|
39
|
-
interface OtelSpanContext {
|
|
40
|
-
traceId: string;
|
|
41
|
-
spanId: string;
|
|
42
|
-
traceFlags: number;
|
|
43
|
-
traceState?: OtelTraceState;
|
|
44
|
-
}
|
|
45
|
-
interface OtelSpan {
|
|
46
|
-
spanContext(): OtelSpanContext;
|
|
47
|
-
}
|
|
48
|
-
interface OtelTrace {
|
|
49
|
-
getActiveSpan(): OtelSpan | undefined;
|
|
50
|
-
}
|
|
51
|
-
interface OtelApi {
|
|
52
|
-
trace: OtelTrace;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
type OtelLoader = () => OtelApi | null;
|
|
56
|
-
|
|
57
|
-
const ZERO_TRACE_ID = "0".repeat(32);
|
|
58
|
-
const ZERO_SPAN_ID = "0".repeat(16);
|
|
59
|
-
|
|
60
|
-
let cached: OtelApi | null | undefined; // undefined = not yet probed
|
|
61
|
-
let loader: OtelLoader = defaultLoader;
|
|
62
|
-
|
|
63
|
-
function defaultLoader(): OtelApi | null {
|
|
64
|
-
try {
|
|
65
|
-
const r = resolveRequire();
|
|
66
|
-
if (r === null) return null;
|
|
67
|
-
return r("@opentelemetry/api") as OtelApi;
|
|
68
|
-
} catch {
|
|
69
|
-
return null;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Resolve a CommonJS `require` usable for optionally loading `@opentelemetry/api`,
|
|
75
|
-
* across every runtime this package ships to. Order matters — it is the whole
|
|
76
|
-
* point of the TO11-1380 fix:
|
|
77
|
-
*
|
|
78
|
-
* 1. `globalThis.require` — present in some bundled / global contexts.
|
|
79
|
-
* 2. `eval("require")` — resolves the module-local `require` in the
|
|
80
|
-
* `dist/client.cjs` (CommonJS) build. In a native Node ESM module this
|
|
81
|
-
* throws `ReferenceError: require is not defined`, so we fall through.
|
|
82
|
-
* 3. `process.getBuiltinModule("node:module").createRequire(import.meta.url)`
|
|
83
|
-
* — the ESM-correct path. The published `dist/client.js` is loaded by Node
|
|
84
|
-
* via `import`, where `require` is not defined and not on `globalThis`. The
|
|
85
|
-
* previous loader stopped at step 2, cached `null` forever, and made
|
|
86
|
-
* `activeTraceContext()` a permanent no-op for the primary import build
|
|
87
|
-
* (TO11-1380). `import.meta` is only referenced here, in the ESM branch, so
|
|
88
|
-
* the CJS build never evaluates it.
|
|
89
|
-
*
|
|
90
|
-
* `node:module` is reached lazily via `process.getBuiltinModule` (not a static
|
|
91
|
-
* `import`) precisely so non-Node runtimes never resolve the builtin at module
|
|
92
|
-
* load. In a browser / edge runtime (Cloudflare Workers, Vercel Edge)
|
|
93
|
-
* `globalThis.require` is absent, `eval("require")` throws, and
|
|
94
|
-
* `process.getBuiltinModule` is absent — every step degrades to a strict no-op,
|
|
95
|
-
* preserving the zero-dep edge behavior (spec R2). `process.getBuiltinModule`
|
|
96
|
-
* is available on Node ≥ 20.16 / ≥ 22.3; older Node ESM (no global `require`,
|
|
97
|
-
* no `getBuiltinModule`) degrades to no-op rather than crashing.
|
|
98
|
-
*/
|
|
99
|
-
function resolveRequire(): ((id: string) => unknown) | null {
|
|
100
|
-
// biome-ignore lint/suspicious/noExplicitAny: dynamic require shim — see header comment
|
|
101
|
-
const g = globalThis as any;
|
|
102
|
-
if (typeof g.require === "function") {
|
|
103
|
-
return g.require as (id: string) => unknown;
|
|
104
|
-
}
|
|
105
|
-
try {
|
|
106
|
-
// biome-ignore lint/security/noGlobalEval: CJS build only; throws in native ESM and we fall through.
|
|
107
|
-
const cjsRequire = eval("require") as (id: string) => unknown;
|
|
108
|
-
if (typeof cjsRequire === "function") return cjsRequire;
|
|
109
|
-
} catch {
|
|
110
|
-
// Not a CommonJS context (native ESM or edge runtime). Try ESM next.
|
|
111
|
-
}
|
|
112
|
-
try {
|
|
113
|
-
// `getBuiltinModule` is optional: absent (returns undefined / not a
|
|
114
|
-
// function) in edge runtimes, so this never throws at module load.
|
|
115
|
-
const getBuiltinModule = g.process?.getBuiltinModule as
|
|
116
|
-
| ((id: string) => unknown)
|
|
117
|
-
| undefined;
|
|
118
|
-
if (typeof getBuiltinModule !== "function") return null;
|
|
119
|
-
const nodeModule = getBuiltinModule("node:module") as {
|
|
120
|
-
createRequire?: (path: string | URL) => (id: string) => unknown;
|
|
121
|
-
};
|
|
122
|
-
if (typeof nodeModule?.createRequire !== "function") return null;
|
|
123
|
-
return nodeModule.createRequire(import.meta.url);
|
|
124
|
-
} catch {
|
|
125
|
-
// No usable `node:module` (edge runtime) — strict no-op.
|
|
126
|
-
return null;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Return the W3C `traceparent` (and optional `tracestate`) string pair for
|
|
132
|
-
* the currently active OpenTelemetry span in the host process, or `null` if
|
|
133
|
-
* no active span exists or `@opentelemetry/api` is not installed.
|
|
134
|
-
*
|
|
135
|
-
* Idempotent and side-effect-free past the one-time module probe. Callers
|
|
136
|
-
* inject the returned strings as outbound HTTP headers when non-null.
|
|
137
|
-
*/
|
|
138
|
-
export function activeTraceContext(): {
|
|
139
|
-
traceparent: string;
|
|
140
|
-
tracestate?: string;
|
|
141
|
-
} | null {
|
|
142
|
-
if (cached === undefined) cached = loader();
|
|
143
|
-
if (cached === null) return null;
|
|
144
|
-
|
|
145
|
-
let span: OtelSpan | undefined;
|
|
146
|
-
let ctx: OtelSpanContext;
|
|
147
|
-
try {
|
|
148
|
-
span = cached.trace.getActiveSpan();
|
|
149
|
-
if (!span) return null;
|
|
150
|
-
ctx = span.spanContext();
|
|
151
|
-
} catch {
|
|
152
|
-
// A broken / partially-installed OTel SDK must not break customer
|
|
153
|
-
// outbound calls. Degrade to no-op.
|
|
154
|
-
return null;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
const { traceId, spanId } = ctx;
|
|
158
|
-
if (!traceId || !spanId) return null;
|
|
159
|
-
if (traceId === ZERO_TRACE_ID || spanId === ZERO_SPAN_ID) return null;
|
|
160
|
-
|
|
161
|
-
const flags = ((ctx.traceFlags ?? 0) & 0xff).toString(16).padStart(2, "0");
|
|
162
|
-
const traceparent = `00-${traceId}-${spanId}-${flags}`;
|
|
163
|
-
|
|
164
|
-
let tracestate: string | undefined;
|
|
165
|
-
try {
|
|
166
|
-
const raw = ctx.traceState?.serialize();
|
|
167
|
-
if (typeof raw === "string" && raw.length > 0) tracestate = raw;
|
|
168
|
-
} catch {
|
|
169
|
-
// Same defensive posture — never let a vendor tracestate bug
|
|
170
|
-
// surface as a request failure.
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
return tracestate === undefined
|
|
174
|
-
? { traceparent }
|
|
175
|
-
: { traceparent, tracestate };
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
/**
|
|
179
|
-
* Test-only seam: install a deterministic OTel loader and reset the
|
|
180
|
-
* module-level cache. Underscore-prefixed and intentionally NOT re-exported
|
|
181
|
-
* from the package entry-point.
|
|
182
|
-
*
|
|
183
|
-
* Production code never calls this. Tests call it to simulate
|
|
184
|
-
* "OTel installed" / "OTel absent" / "broken OTel install" cells.
|
|
185
|
-
*/
|
|
186
|
-
export function _loadOtelForTests(injected: OtelLoader | undefined): void {
|
|
187
|
-
loader = injected ?? defaultLoader;
|
|
188
|
-
cached = undefined;
|
|
189
|
-
}
|