@o11y-one/sdk 0.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.
- package/LICENSE +201 -0
- package/README.md +84 -0
- package/dist/auth.d.ts +51 -0
- package/dist/auth.js +64 -0
- package/dist/client.d.ts +105 -0
- package/dist/client.js +107 -0
- package/dist/errors.d.ts +47 -0
- package/dist/errors.js +83 -0
- package/dist/evaluation.d.ts +253 -0
- package/dist/evaluation.js +455 -0
- package/dist/index.d.ts +23 -0
- package/dist/index.js +24 -0
- package/dist/refusals.d.ts +116 -0
- package/dist/refusals.js +125 -0
- package/dist/result.d.ts +38 -0
- package/dist/result.js +40 -0
- package/dist/validate.d.ts +58 -0
- package/dist/validate.js +103 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# `@o11y-one/sdk`
|
|
2
|
+
|
|
3
|
+
The TypeScript client for the O11y One API. A thin, hand-written layer over the
|
|
4
|
+
generated [`@o11y-one/api-agentic`](../gen-ts-agentic) package.
|
|
5
|
+
|
|
6
|
+
## Install
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
pnpm add @o11y-one/sdk
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
## Use
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { O11yClient, classify } from "@o11y-one/sdk";
|
|
16
|
+
import { AgenticEvaluationService } from "@o11y-one/api-agentic/o11y_one/agentic/v1/evaluation_pb";
|
|
17
|
+
|
|
18
|
+
const o11y = new O11yClient({
|
|
19
|
+
baseUrl: "https://api.o11y.one",
|
|
20
|
+
credential: process.env.O11Y_API_KEY, // o11y_mach.<selector>.<secret>
|
|
21
|
+
orgId: process.env.O11Y_ORG_ID,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const evals = o11y.service(AgenticEvaluationService);
|
|
25
|
+
|
|
26
|
+
try {
|
|
27
|
+
const defs = await evals.listEvaluationDefinitions({});
|
|
28
|
+
} catch (err) {
|
|
29
|
+
const failure = classify(err);
|
|
30
|
+
if (failure.disposition === "insufficient-scope") {
|
|
31
|
+
throw new Error(`credential is missing scope ${failure.missingScope}`);
|
|
32
|
+
}
|
|
33
|
+
throw err;
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## What is here, and what deliberately is not
|
|
38
|
+
|
|
39
|
+
Three modules and nothing else:
|
|
40
|
+
|
|
41
|
+
| module | responsibility |
|
|
42
|
+
|---|---|
|
|
43
|
+
| `client.ts` | transport construction, credential and scoping header injection |
|
|
44
|
+
| `auth.ts` | the credential wire format and local structural validation |
|
|
45
|
+
| `errors.ts` | the failure taxonomy: reauthenticate / insufficient-scope / version-skew / retry / unclassified |
|
|
46
|
+
|
|
47
|
+
Service methods are **not** wrapped. connect-es turns any generated descriptor
|
|
48
|
+
into a fully typed client; a hand-written facade over every service would be a
|
|
49
|
+
second API surface to keep in sync with the proto, and it would rot the first
|
|
50
|
+
time a field is added upstream.
|
|
51
|
+
|
|
52
|
+
## The distinction this package exists to preserve
|
|
53
|
+
|
|
54
|
+
`UNAUTHENTICATED` and `PERMISSION_DENIED` are different problems:
|
|
55
|
+
|
|
56
|
+
* **`UNAUTHENTICATED`** — the credential is absent, malformed, unknown, expired,
|
|
57
|
+
or revoked. Re-auth. Retrying cannot change the answer.
|
|
58
|
+
* **`PERMISSION_DENIED`** — the credential is fine; it lacks a scope (the server
|
|
59
|
+
names which one) or it was presented to a non-machine surface.
|
|
60
|
+
|
|
61
|
+
`classify()` keeps them apart, along with `UNAVAILABLE` (auth backend down —
|
|
62
|
+
retry with backoff) and `INVALID_ARGUMENT` (SDK/server version skew). Collapsing
|
|
63
|
+
these into a single "auth error" is the failure mode this module exists to
|
|
64
|
+
prevent.
|
|
65
|
+
|
|
66
|
+
## Credentials
|
|
67
|
+
|
|
68
|
+
A machine credential is `o11y_mach.<selector>.<secret>`, presented in the
|
|
69
|
+
`x-o11y-key` header. It is returned exactly once at creation (no read-back RPC,
|
|
70
|
+
no recovery path), rotated create-then-revoke rather than atomically, revoked
|
|
71
|
+
effective on the next request, and expires at +365 days by default with a
|
|
72
|
+
three-year cap.
|
|
73
|
+
|
|
74
|
+
Never send it as `authorization: Bearer` — that path carries the browser session
|
|
75
|
+
JWT and a machine credential presented there is rejected.
|
|
76
|
+
|
|
77
|
+
## Runtime support
|
|
78
|
+
|
|
79
|
+
The default transport is Connect over HTTP via `@connectrpc/connect-web`, which
|
|
80
|
+
needs only a spec-compatible `fetch`. That covers browsers, Node 26, Deno, Bun
|
|
81
|
+
and Cloudflare Workers. `@connectrpc/connect-node` is a dependency for callers
|
|
82
|
+
who want the Node-specific HTTP/2 transport and construct it themselves; pass
|
|
83
|
+
the resulting transport's options through `ClientOptions` or build your own
|
|
84
|
+
transport and install `credentialInterceptor()` on it.
|
package/dist/auth.d.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credential handling for machine principals.
|
|
3
|
+
*
|
|
4
|
+
* The wire contract below is SETTLED — it is read out of the running server, not
|
|
5
|
+
* designed here. Source: lane 50A's credential-shape note (w50d handoff), which
|
|
6
|
+
* in turn cites `shared/src/lib.rs:70` for the header name and
|
|
7
|
+
* `server/src/services/api_tokens/crypto.rs` for the encoding.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* The header a machine credential is presented in.
|
|
11
|
+
*
|
|
12
|
+
* `O11Y_ONE_API_KEY_HEADER_NAME` in o11y-api (`shared/src/lib.rs:70`).
|
|
13
|
+
* `x-api-key` is accepted server-side as a fallback alias; this SDK always sends
|
|
14
|
+
* the canonical name.
|
|
15
|
+
*
|
|
16
|
+
* Deliberately NOT `authorization: Bearer` — that path carries the browser
|
|
17
|
+
* session JWT, and a machine credential presented there is rejected.
|
|
18
|
+
*/
|
|
19
|
+
export declare const CREDENTIAL_HEADER = "x-o11y-key";
|
|
20
|
+
/** Org scoping header (`O11Y_ONE_ORG_ID_HEADER_NAME`). */
|
|
21
|
+
export declare const ORG_ID_HEADER = "x-o11y-org-id";
|
|
22
|
+
/** Tenant scoping header (`O11Y_ONE_TENANT_ID_HEADER_NAME`). */
|
|
23
|
+
export declare const TENANT_ID_HEADER = "x-o11y-tenant-id";
|
|
24
|
+
/** Prefix carried by machine-principal credentials (`APITokenType::prefix()`). */
|
|
25
|
+
export declare const MACHINE_CREDENTIAL_PREFIX = "o11y_mach";
|
|
26
|
+
/** Server-side cap on the presented credential string (`API_TOKEN_MAX_LEN`). */
|
|
27
|
+
export declare const CREDENTIAL_MAX_LEN = 128;
|
|
28
|
+
/**
|
|
29
|
+
* A credential string, opaque to the client.
|
|
30
|
+
*
|
|
31
|
+
* Shape is `o11y_mach.<selector>.<secret>` where selector is 16 random bytes and
|
|
32
|
+
* secret is 32 random bytes, both base64url without padding. The SDK does not
|
|
33
|
+
* parse past the prefix and must never try to: verification is Argon2id +
|
|
34
|
+
* blake3 on the server and none of that is the client's business.
|
|
35
|
+
*
|
|
36
|
+
* It is returned exactly once, by `CreateMachineCredential`. There is no
|
|
37
|
+
* read-back RPC and no recovery path.
|
|
38
|
+
*/
|
|
39
|
+
export type MachineCredential = string & {
|
|
40
|
+
readonly __brand: "MachineCredential";
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Cheap, local, structural validation. Catches the overwhelmingly common
|
|
44
|
+
* mistakes — an empty env var, a session JWT pasted into the wrong variable, a
|
|
45
|
+
* credential truncated by a copy-paste — before a network round trip turns them
|
|
46
|
+
* into an opaque UNAUTHENTICATED.
|
|
47
|
+
*
|
|
48
|
+
* It is NOT authentication. A string that passes here can still be revoked,
|
|
49
|
+
* expired, or unknown; only the server can say.
|
|
50
|
+
*/
|
|
51
|
+
export declare function assertLooksLikeMachineCredential(raw: string): MachineCredential;
|
package/dist/auth.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Credential handling for machine principals.
|
|
3
|
+
*
|
|
4
|
+
* The wire contract below is SETTLED — it is read out of the running server, not
|
|
5
|
+
* designed here. Source: lane 50A's credential-shape note (w50d handoff), which
|
|
6
|
+
* in turn cites `shared/src/lib.rs:70` for the header name and
|
|
7
|
+
* `server/src/services/api_tokens/crypto.rs` for the encoding.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* The header a machine credential is presented in.
|
|
11
|
+
*
|
|
12
|
+
* `O11Y_ONE_API_KEY_HEADER_NAME` in o11y-api (`shared/src/lib.rs:70`).
|
|
13
|
+
* `x-api-key` is accepted server-side as a fallback alias; this SDK always sends
|
|
14
|
+
* the canonical name.
|
|
15
|
+
*
|
|
16
|
+
* Deliberately NOT `authorization: Bearer` — that path carries the browser
|
|
17
|
+
* session JWT, and a machine credential presented there is rejected.
|
|
18
|
+
*/
|
|
19
|
+
export const CREDENTIAL_HEADER = "x-o11y-key";
|
|
20
|
+
/** Org scoping header (`O11Y_ONE_ORG_ID_HEADER_NAME`). */
|
|
21
|
+
export const ORG_ID_HEADER = "x-o11y-org-id";
|
|
22
|
+
/** Tenant scoping header (`O11Y_ONE_TENANT_ID_HEADER_NAME`). */
|
|
23
|
+
export const TENANT_ID_HEADER = "x-o11y-tenant-id";
|
|
24
|
+
/** Prefix carried by machine-principal credentials (`APITokenType::prefix()`). */
|
|
25
|
+
export const MACHINE_CREDENTIAL_PREFIX = "o11y_mach";
|
|
26
|
+
/** Server-side cap on the presented credential string (`API_TOKEN_MAX_LEN`). */
|
|
27
|
+
export const CREDENTIAL_MAX_LEN = 128;
|
|
28
|
+
/**
|
|
29
|
+
* Cheap, local, structural validation. Catches the overwhelmingly common
|
|
30
|
+
* mistakes — an empty env var, a session JWT pasted into the wrong variable, a
|
|
31
|
+
* credential truncated by a copy-paste — before a network round trip turns them
|
|
32
|
+
* into an opaque UNAUTHENTICATED.
|
|
33
|
+
*
|
|
34
|
+
* It is NOT authentication. A string that passes here can still be revoked,
|
|
35
|
+
* expired, or unknown; only the server can say.
|
|
36
|
+
*/
|
|
37
|
+
export function assertLooksLikeMachineCredential(raw) {
|
|
38
|
+
// The server trims whitespace and strips one layer of surrounding quotes
|
|
39
|
+
// (`normalize_api_token_input`) so a value pasted out of a shell still works.
|
|
40
|
+
// We normalise the same way rather than relying on it.
|
|
41
|
+
let value = raw.trim();
|
|
42
|
+
if (value.length >= 2) {
|
|
43
|
+
const first = value[0];
|
|
44
|
+
const last = value[value.length - 1];
|
|
45
|
+
if ((first === '"' && last === '"') || (first === "'" && last === "'")) {
|
|
46
|
+
value = value.slice(1, -1).trim();
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (value.length === 0) {
|
|
50
|
+
throw new Error("o11y credential is empty — set the credential env var or pass it explicitly");
|
|
51
|
+
}
|
|
52
|
+
if (value.length > CREDENTIAL_MAX_LEN) {
|
|
53
|
+
throw new Error(`o11y credential is ${value.length} bytes, over the ${CREDENTIAL_MAX_LEN}-byte server limit; ` +
|
|
54
|
+
"this is not a valid credential");
|
|
55
|
+
}
|
|
56
|
+
if (!value.startsWith(`${MACHINE_CREDENTIAL_PREFIX}.`)) {
|
|
57
|
+
throw new Error(`o11y credential does not start with "${MACHINE_CREDENTIAL_PREFIX}." — ` +
|
|
58
|
+
"machine credentials do. If this is a browser session token it will not work here.");
|
|
59
|
+
}
|
|
60
|
+
if (value.split(".").length !== 3) {
|
|
61
|
+
throw new Error("o11y credential is malformed: expected <prefix>.<selector>.<secret>, three dot-separated parts");
|
|
62
|
+
}
|
|
63
|
+
return value;
|
|
64
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport construction and credential injection.
|
|
3
|
+
*
|
|
4
|
+
* Scope discipline: this file builds a transport and attaches headers. It does
|
|
5
|
+
* NOT wrap RPCs in convenience methods. `@o11y-one/api-agentic` already exposes
|
|
6
|
+
* every service descriptor, and connect-es v2 turns a descriptor into a typed
|
|
7
|
+
* client with `createClient(Desc, transport)`. A hand-written facade over them
|
|
8
|
+
* would be a second API surface to keep in sync with the proto, and it would
|
|
9
|
+
* rot the first time a field is added upstream.
|
|
10
|
+
*/
|
|
11
|
+
import { type Client, type Interceptor, type Transport } from "@connectrpc/connect";
|
|
12
|
+
import { type ConnectTransportOptions } from "@connectrpc/connect-web";
|
|
13
|
+
import type { DescService } from "@bufbuild/protobuf";
|
|
14
|
+
export interface ClientOptions {
|
|
15
|
+
/**
|
|
16
|
+
* Base URL of the O11y One Connect endpoint, e.g. `https://api.o11y.one`.
|
|
17
|
+
* No trailing path — connect-es appends `/<package>.<Service>/<Method>`.
|
|
18
|
+
*/
|
|
19
|
+
baseUrl: string;
|
|
20
|
+
/**
|
|
21
|
+
* Machine credential, `o11y_mach.<selector>.<secret>`.
|
|
22
|
+
*
|
|
23
|
+
* Optional so that unauthenticated surfaces (capability discovery, health)
|
|
24
|
+
* can be reached without one. Every authenticated call will fail with
|
|
25
|
+
* UNAUTHENTICATED if it is absent — see `classify()` in errors.ts.
|
|
26
|
+
*/
|
|
27
|
+
credential?: string;
|
|
28
|
+
/** Org to scope the call to (`x-o11y-org-id`). */
|
|
29
|
+
orgId?: string;
|
|
30
|
+
/** Tenant to scope the call to (`x-o11y-tenant-id`). */
|
|
31
|
+
tenantId?: string;
|
|
32
|
+
/** Extra interceptors, applied after the credential interceptor. */
|
|
33
|
+
interceptors?: Interceptor[];
|
|
34
|
+
/**
|
|
35
|
+
* Override the fetch implementation. Defaults to the ambient `globalThis.fetch`;
|
|
36
|
+
* Node 26 has one. This exists for tests and for runtimes that wrap fetch.
|
|
37
|
+
*
|
|
38
|
+
* The type is borrowed from connect-web rather than spelled
|
|
39
|
+
* `typeof globalThis.fetch`, which would force the DOM lib into this package's
|
|
40
|
+
* compilation for the sake of one type and drag every DOM global in with it.
|
|
41
|
+
*/
|
|
42
|
+
fetch?: ConnectTransportOptions["fetch"];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* An interceptor that stamps the credential and scoping headers onto every
|
|
46
|
+
* outbound request.
|
|
47
|
+
*
|
|
48
|
+
* This is THE auth-injection point for the TypeScript SDK. Everything about the
|
|
49
|
+
* shape it injects is settled (see auth.ts). What is NOT settled, and is the
|
|
50
|
+
* one open dependency this package has on the backend:
|
|
51
|
+
*
|
|
52
|
+
* TODO(w50a-auth-shape / lane 50A): the machine-principal management surface —
|
|
53
|
+
* CreateMachinePrincipal, ListMachinePrincipals, CreateMachineCredential,
|
|
54
|
+
* RevokeMachineCredential, RevokeMachinePrincipal, GetCallerPrincipal
|
|
55
|
+
* — does not exist in the proto snapshot at PROTO_PIN (fb8f5098). auth.proto
|
|
56
|
+
* there carries only AuthFoundationService.ListAuthMethods. The RPC NAMES are
|
|
57
|
+
* held by 50A and will not move, so when lane A lands them:
|
|
58
|
+
* 1. bump PROTO_PIN and run tools/sync-proto.sh,
|
|
59
|
+
* 2. add `O11yClient.whoami()` over GetCallerPrincipal — the introspection
|
|
60
|
+
* call that answers "is my credential valid, and which scopes does it
|
|
61
|
+
* carry?" BEFORE a run, which is what makes `o11y-eval doctor` possible
|
|
62
|
+
* and what turns an UNAUTHENTICATED at minute 40 into an error at
|
|
63
|
+
* minute 0.
|
|
64
|
+
* Nothing else in this file changes: the header, the encoding and the error
|
|
65
|
+
* taxonomy are already the final ones.
|
|
66
|
+
*/
|
|
67
|
+
export declare function credentialInterceptor(options: {
|
|
68
|
+
credential?: string;
|
|
69
|
+
orgId?: string;
|
|
70
|
+
tenantId?: string;
|
|
71
|
+
}): Interceptor;
|
|
72
|
+
/**
|
|
73
|
+
* Build a Connect transport with the credential interceptor installed.
|
|
74
|
+
*
|
|
75
|
+
* Protocol note: Connect over HTTP, not gRPC. It is the protocol o11y-web
|
|
76
|
+
* already speaks and the one that survives proxies and CI egress rules without
|
|
77
|
+
* HTTP/2 prior knowledge. `createConnectTransport` from `@connectrpc/connect-web`
|
|
78
|
+
* works in browsers, in Node 26 (whose global fetch is sufficient), and in
|
|
79
|
+
* Workers; `@connectrpc/connect-node` is a dependency only for callers who need
|
|
80
|
+
* the Node-specific HTTP/2 transport and reach for it themselves.
|
|
81
|
+
*/
|
|
82
|
+
export declare function createTransport(options: ClientOptions): Transport;
|
|
83
|
+
/**
|
|
84
|
+
* A thin holder for the transport, so callers construct credentials once and
|
|
85
|
+
* mint typed clients per service.
|
|
86
|
+
*
|
|
87
|
+
* ```ts
|
|
88
|
+
* import { O11yClient } from "@o11y-one/sdk";
|
|
89
|
+
* import { AgenticEvaluationService } from "@o11y-one/api-agentic/o11y_one/agentic/v1/evaluation_pb";
|
|
90
|
+
*
|
|
91
|
+
* const o11y = new O11yClient({ baseUrl, credential: process.env.O11Y_API_KEY });
|
|
92
|
+
* const evals = o11y.service(AgenticEvaluationService);
|
|
93
|
+
* const defs = await evals.listEvaluationDefinitions({});
|
|
94
|
+
* ```
|
|
95
|
+
*
|
|
96
|
+
* `@o11y-one/sdk` depends on `@o11y-one/api-agentic` — the agentic + common
|
|
97
|
+
* subset, which is the whole published surface. The transport itself is
|
|
98
|
+
* domain-agnostic: any generated `DescService` works here.
|
|
99
|
+
*/
|
|
100
|
+
export declare class O11yClient {
|
|
101
|
+
readonly transport: Transport;
|
|
102
|
+
constructor(options: ClientOptions);
|
|
103
|
+
/** Mint a typed client for any generated service descriptor. */
|
|
104
|
+
service<T extends DescService>(desc: T): Client<T>;
|
|
105
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transport construction and credential injection.
|
|
3
|
+
*
|
|
4
|
+
* Scope discipline: this file builds a transport and attaches headers. It does
|
|
5
|
+
* NOT wrap RPCs in convenience methods. `@o11y-one/api-agentic` already exposes
|
|
6
|
+
* every service descriptor, and connect-es v2 turns a descriptor into a typed
|
|
7
|
+
* client with `createClient(Desc, transport)`. A hand-written facade over them
|
|
8
|
+
* would be a second API surface to keep in sync with the proto, and it would
|
|
9
|
+
* rot the first time a field is added upstream.
|
|
10
|
+
*/
|
|
11
|
+
import { createClient } from "@connectrpc/connect";
|
|
12
|
+
import { createConnectTransport, } from "@connectrpc/connect-web";
|
|
13
|
+
import { assertLooksLikeMachineCredential, CREDENTIAL_HEADER, ORG_ID_HEADER, TENANT_ID_HEADER, } from "./auth.js";
|
|
14
|
+
/**
|
|
15
|
+
* An interceptor that stamps the credential and scoping headers onto every
|
|
16
|
+
* outbound request.
|
|
17
|
+
*
|
|
18
|
+
* This is THE auth-injection point for the TypeScript SDK. Everything about the
|
|
19
|
+
* shape it injects is settled (see auth.ts). What is NOT settled, and is the
|
|
20
|
+
* one open dependency this package has on the backend:
|
|
21
|
+
*
|
|
22
|
+
* TODO(w50a-auth-shape / lane 50A): the machine-principal management surface —
|
|
23
|
+
* CreateMachinePrincipal, ListMachinePrincipals, CreateMachineCredential,
|
|
24
|
+
* RevokeMachineCredential, RevokeMachinePrincipal, GetCallerPrincipal
|
|
25
|
+
* — does not exist in the proto snapshot at PROTO_PIN (fb8f5098). auth.proto
|
|
26
|
+
* there carries only AuthFoundationService.ListAuthMethods. The RPC NAMES are
|
|
27
|
+
* held by 50A and will not move, so when lane A lands them:
|
|
28
|
+
* 1. bump PROTO_PIN and run tools/sync-proto.sh,
|
|
29
|
+
* 2. add `O11yClient.whoami()` over GetCallerPrincipal — the introspection
|
|
30
|
+
* call that answers "is my credential valid, and which scopes does it
|
|
31
|
+
* carry?" BEFORE a run, which is what makes `o11y-eval doctor` possible
|
|
32
|
+
* and what turns an UNAUTHENTICATED at minute 40 into an error at
|
|
33
|
+
* minute 0.
|
|
34
|
+
* Nothing else in this file changes: the header, the encoding and the error
|
|
35
|
+
* taxonomy are already the final ones.
|
|
36
|
+
*/
|
|
37
|
+
export function credentialInterceptor(options) {
|
|
38
|
+
const credential = options.credential
|
|
39
|
+
? assertLooksLikeMachineCredential(options.credential)
|
|
40
|
+
: undefined;
|
|
41
|
+
return (next) => async (req) => {
|
|
42
|
+
if (credential !== undefined) {
|
|
43
|
+
req.header.set(CREDENTIAL_HEADER, credential);
|
|
44
|
+
}
|
|
45
|
+
if (options.orgId !== undefined) {
|
|
46
|
+
req.header.set(ORG_ID_HEADER, options.orgId);
|
|
47
|
+
}
|
|
48
|
+
if (options.tenantId !== undefined) {
|
|
49
|
+
req.header.set(TENANT_ID_HEADER, options.tenantId);
|
|
50
|
+
}
|
|
51
|
+
return await next(req);
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Build a Connect transport with the credential interceptor installed.
|
|
56
|
+
*
|
|
57
|
+
* Protocol note: Connect over HTTP, not gRPC. It is the protocol o11y-web
|
|
58
|
+
* already speaks and the one that survives proxies and CI egress rules without
|
|
59
|
+
* HTTP/2 prior knowledge. `createConnectTransport` from `@connectrpc/connect-web`
|
|
60
|
+
* works in browsers, in Node 26 (whose global fetch is sufficient), and in
|
|
61
|
+
* Workers; `@connectrpc/connect-node` is a dependency only for callers who need
|
|
62
|
+
* the Node-specific HTTP/2 transport and reach for it themselves.
|
|
63
|
+
*/
|
|
64
|
+
export function createTransport(options) {
|
|
65
|
+
return createConnectTransport({
|
|
66
|
+
baseUrl: options.baseUrl,
|
|
67
|
+
// Binary wire format: smaller, and it round-trips proto3 field presence
|
|
68
|
+
// without the JSON mapping's ambiguities.
|
|
69
|
+
useBinaryFormat: true,
|
|
70
|
+
interceptors: [
|
|
71
|
+
credentialInterceptor({
|
|
72
|
+
credential: options.credential,
|
|
73
|
+
orgId: options.orgId,
|
|
74
|
+
tenantId: options.tenantId,
|
|
75
|
+
}),
|
|
76
|
+
...(options.interceptors ?? []),
|
|
77
|
+
],
|
|
78
|
+
...(options.fetch ? { fetch: options.fetch } : {}),
|
|
79
|
+
});
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* A thin holder for the transport, so callers construct credentials once and
|
|
83
|
+
* mint typed clients per service.
|
|
84
|
+
*
|
|
85
|
+
* ```ts
|
|
86
|
+
* import { O11yClient } from "@o11y-one/sdk";
|
|
87
|
+
* import { AgenticEvaluationService } from "@o11y-one/api-agentic/o11y_one/agentic/v1/evaluation_pb";
|
|
88
|
+
*
|
|
89
|
+
* const o11y = new O11yClient({ baseUrl, credential: process.env.O11Y_API_KEY });
|
|
90
|
+
* const evals = o11y.service(AgenticEvaluationService);
|
|
91
|
+
* const defs = await evals.listEvaluationDefinitions({});
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* `@o11y-one/sdk` depends on `@o11y-one/api-agentic` — the agentic + common
|
|
95
|
+
* subset, which is the whole published surface. The transport itself is
|
|
96
|
+
* domain-agnostic: any generated `DescService` works here.
|
|
97
|
+
*/
|
|
98
|
+
export class O11yClient {
|
|
99
|
+
transport;
|
|
100
|
+
constructor(options) {
|
|
101
|
+
this.transport = createTransport(options);
|
|
102
|
+
}
|
|
103
|
+
/** Mint a typed client for any generated service descriptor. */
|
|
104
|
+
service(desc) {
|
|
105
|
+
return createClient(desc, this.transport);
|
|
106
|
+
}
|
|
107
|
+
}
|
package/dist/errors.d.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The O11y One error taxonomy, as the SDK must render it.
|
|
3
|
+
*
|
|
4
|
+
* From lane 50A's note: `UNAUTHENTICATED` vs `PERMISSION_DENIED` is the whole
|
|
5
|
+
* reason this layer exists. "Your token is invalid" and "your token is fine but
|
|
6
|
+
* this capability is not shipped / not granted" are different problems with
|
|
7
|
+
* different fixes, and collapsing them into "auth error" is the failure mode
|
|
8
|
+
* this file prevents.
|
|
9
|
+
*/
|
|
10
|
+
import { Code } from "@connectrpc/connect";
|
|
11
|
+
/** What a caller should DO about a failure. */
|
|
12
|
+
export type FailureDisposition =
|
|
13
|
+
/** Credential is absent, malformed, unknown, expired, or revoked. Re-auth. Never retry. */
|
|
14
|
+
"reauthenticate"
|
|
15
|
+
/** Credential is valid but lacks the scope, or was presented to a non-machine surface. Stop. */
|
|
16
|
+
| "insufficient-scope"
|
|
17
|
+
/** SDK/server version skew (unknown scope discriminant on a write). Stop. */
|
|
18
|
+
| "version-skew"
|
|
19
|
+
/** Auth backend is down. Retryable with backoff — deliberately not cached server-side. */
|
|
20
|
+
| "retry-with-backoff"
|
|
21
|
+
/** Anything else. The SDK does not guess. */
|
|
22
|
+
| "unclassified";
|
|
23
|
+
export interface ClassifiedFailure {
|
|
24
|
+
readonly disposition: FailureDisposition;
|
|
25
|
+
readonly code: Code | undefined;
|
|
26
|
+
readonly message: string;
|
|
27
|
+
/**
|
|
28
|
+
* For `insufficient-scope`, the canonical scope string the server named in
|
|
29
|
+
* its message, when it named one: `eval:read`, `run:execute`, `dataset:write`,
|
|
30
|
+
* `lease:submit`, `platform-annotation:write`.
|
|
31
|
+
*/
|
|
32
|
+
readonly missingScope: string | undefined;
|
|
33
|
+
readonly retryable: boolean;
|
|
34
|
+
}
|
|
35
|
+
declare const CANONICAL_SCOPES: readonly ["eval:read", "run:execute", "dataset:write", "lease:submit", "platform-annotation:write"];
|
|
36
|
+
/** The five canonical machine-principal scope strings (50A, frozen taxonomy). */
|
|
37
|
+
export type MachineScope = (typeof CANONICAL_SCOPES)[number];
|
|
38
|
+
export declare const MACHINE_SCOPES: readonly MachineScope[];
|
|
39
|
+
/**
|
|
40
|
+
* Classify a thrown error into the taxonomy above.
|
|
41
|
+
*
|
|
42
|
+
* Non-Connect errors pass through as `unclassified` rather than being forced
|
|
43
|
+
* into a bucket — a DNS failure is not an auth failure and pretending otherwise
|
|
44
|
+
* sends people down the wrong path.
|
|
45
|
+
*/
|
|
46
|
+
export declare function classify(err: unknown): ClassifiedFailure;
|
|
47
|
+
export {};
|