generalcontext 0.0.0-stage → 0.3.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 +202 -0
- package/NOTICE +5 -0
- package/README.md +64 -2
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +175 -0
- package/dist/index.d.ts +125 -0
- package/dist/index.js +1567 -0
- package/package.json +61 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/NOTICE
ADDED
package/README.md
CHANGED
|
@@ -1,3 +1,65 @@
|
|
|
1
|
-
#
|
|
1
|
+
# generalcontext
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Record your AI agent's model calls for [General Context](https://generalcontextlabs.com/docs). We read your agent's production conversations and find where it goes wrong, and each failure you confirm becomes an RL and eval task built from the conversation. Your tasks together are your benchmark.
|
|
4
|
+
|
|
5
|
+
Recording never gets in your agent's way: calls are sent in the background, a failure on our side is logged and dropped, never thrown, and there are no runtime dependencies.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```sh
|
|
10
|
+
npm install generalcontext
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Set `GENERALCONTEXT_API_KEY` to the key from your workspace's Connect page.
|
|
14
|
+
|
|
15
|
+
## Record
|
|
16
|
+
|
|
17
|
+
Call `initialize()` once per process, before your agent makes model calls. Pass the client modules you use: it is required whenever a bundler is involved.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import OpenAI from 'openai';
|
|
21
|
+
import Anthropic from '@anthropic-ai/sdk';
|
|
22
|
+
import { initialize, withSession, flush } from 'generalcontext';
|
|
23
|
+
|
|
24
|
+
initialize({ appName: 'support-agent', instrumentModules: { openAI: OpenAI, anthropic: Anthropic } });
|
|
25
|
+
|
|
26
|
+
// Group every call in one conversation under the id you already use for it
|
|
27
|
+
await withSession({ sessionId: conversationId, userId: customerId, channel: 'chat' }, () => handle(message));
|
|
28
|
+
|
|
29
|
+
await flush(); // before a serverless function, job or script exits
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
If your app serves several companies of its own, pass `account` to `withSession` (the id or name you already use for that company) so conversations are grouped by account.
|
|
33
|
+
|
|
34
|
+
| Provider | Package | `instrumentModules` key |
|
|
35
|
+
| - | - | - |
|
|
36
|
+
| OpenAI, including Azure OpenAI | `openai` | `openAI` |
|
|
37
|
+
| Anthropic, including Bedrock, Vertex and beta | `@anthropic-ai/sdk` | `anthropic` |
|
|
38
|
+
| Google Gemini | `@google/genai` | `googleGenAI` |
|
|
39
|
+
| AWS Bedrock Converse | `@aws-sdk/client-bedrock-runtime` | `bedrockRuntime` |
|
|
40
|
+
| Anything else | Send [OpenTelemetry](https://generalcontextlabs.com/docs/opentelemetry) | |
|
|
41
|
+
|
|
42
|
+
**Voice (OpenAI Realtime).** `OpenAIRealtimeWS` (`openai/realtime/ws`) and `OpenAIRealtimeWebSocket`
|
|
43
|
+
(`openai/realtime/websocket`) connections are recorded too. Create the connection inside `withSession(...)` and
|
|
44
|
+
the whole call lands in that conversation. Each `response.done` is recorded as one `openai-realtime` call shaped
|
|
45
|
+
like a Responses call: the instructions and tools, the conversation so far as text (caller turns from the input
|
|
46
|
+
transcription, agent turns from the output transcript, tool calls and their results), and a `voice` field with
|
|
47
|
+
`speech_stopped_at`, `first_audio_at`, `interrupted` and `truncated_at_ms`. Turn on input audio transcription in
|
|
48
|
+
the session to record what the caller said. Audio is never recorded. Pass a connection to `wrap()` to record only
|
|
49
|
+
that one.
|
|
50
|
+
|
|
51
|
+
Node 18 to 24, Node.js runtime only (not Edge). Supported client versions, Next.js, proxies, redaction, outcomes and events, and every option: [SDK reference](https://generalcontextlabs.com/docs/sdk).
|
|
52
|
+
|
|
53
|
+
## Command line
|
|
54
|
+
|
|
55
|
+
```sh
|
|
56
|
+
npx generalcontext verify # the key works, and when the last call arrived
|
|
57
|
+
npx generalcontext verify --session conv-123 # that one conversation arrived
|
|
58
|
+
npx generalcontext benchmark pull --out benchmark.json
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
Running the benchmark is in the Python package. See the [command line docs](https://generalcontextlabs.com/docs/cli).
|
|
62
|
+
|
|
63
|
+
## License
|
|
64
|
+
|
|
65
|
+
Apache License 2.0. See [LICENSE](LICENSE) and [NOTICE](NOTICE).
|
package/dist/cli.d.ts
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// Copyright 2026 General Context Labs, Inc.
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
// generalcontext verify [--session ID] [--json]
|
|
5
|
+
// generalcontext benchmark pull [--out FILE]
|
|
6
|
+
// Exit 0 on success, 1 on a failed check, 2 on a usage or configuration error. No runtime dependencies.
|
|
7
|
+
import { writeFileSync } from 'node:fs';
|
|
8
|
+
const DEFAULT_BASE_URL = 'https://generalcontextlabs.com';
|
|
9
|
+
const TIMEOUT_MS = 30_000;
|
|
10
|
+
const USAGE = `Usage:
|
|
11
|
+
generalcontext verify [--session ID] [--json]
|
|
12
|
+
generalcontext benchmark pull [--out FILE]
|
|
13
|
+
Uses GENERALCONTEXT_API_KEY and GENERALCONTEXT_BASE_URL.`;
|
|
14
|
+
const PYTHON_HINT = 'Run the benchmark with Python: pip install generalcontext && generalcontext evals run ...';
|
|
15
|
+
class Exit extends Error {
|
|
16
|
+
code;
|
|
17
|
+
constructor(code, message) {
|
|
18
|
+
super(message);
|
|
19
|
+
this.code = code;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const trimSlashes = (url) => { let end = url.length; while (end > 0 && url[end - 1] === '/')
|
|
23
|
+
end--; return url.slice(0, end); };
|
|
24
|
+
// Same rule as the SDK: https, or plain http to this machine only.
|
|
25
|
+
function secureUrl(url) {
|
|
26
|
+
try {
|
|
27
|
+
const u = new URL(url);
|
|
28
|
+
return u.protocol === 'https:' || (u.protocol === 'http:' && ['localhost', '127.0.0.1', '[::1]'].includes(u.hostname));
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
function settings() {
|
|
35
|
+
const key = (process.env.GENERALCONTEXT_API_KEY ?? '').trim();
|
|
36
|
+
if (!key)
|
|
37
|
+
throw new Exit(2, 'GENERALCONTEXT_API_KEY is not set. Create a key on the API keys page and export it.');
|
|
38
|
+
const base = trimSlashes((process.env.GENERALCONTEXT_BASE_URL ?? '').trim() || DEFAULT_BASE_URL);
|
|
39
|
+
if (!secureUrl(base))
|
|
40
|
+
throw new Exit(2, `GENERALCONTEXT_BASE_URL ${base} is not https. Use https, or http://localhost for local development.`);
|
|
41
|
+
return { key, base };
|
|
42
|
+
}
|
|
43
|
+
async function get(path) {
|
|
44
|
+
const { key, base } = settings();
|
|
45
|
+
let response;
|
|
46
|
+
try {
|
|
47
|
+
response = await fetch(`${base}/api/platform-collect${path}`, {
|
|
48
|
+
headers: { Authorization: `Bearer ${key}`, Accept: 'application/json' },
|
|
49
|
+
redirect: 'error', // never forward the key to another host
|
|
50
|
+
signal: AbortSignal.timeout(TIMEOUT_MS),
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
catch (error) {
|
|
54
|
+
throw new Exit(1, `Could not reach ${base}: ${error.message}`);
|
|
55
|
+
}
|
|
56
|
+
const text = await response.text();
|
|
57
|
+
let body = null;
|
|
58
|
+
try {
|
|
59
|
+
body = text ? JSON.parse(text) : null;
|
|
60
|
+
}
|
|
61
|
+
catch { /* not JSON */ }
|
|
62
|
+
return { status: response.status, body };
|
|
63
|
+
}
|
|
64
|
+
const errorText = (status, body) => typeof body?.error === 'string' ? body.error : `HTTP ${status}`;
|
|
65
|
+
function failed(status, body) {
|
|
66
|
+
if (status === 401)
|
|
67
|
+
throw new Exit(1, 'The API key was not accepted. Check GENERALCONTEXT_API_KEY.');
|
|
68
|
+
throw new Exit(1, `Request failed: ${errorText(status, body)}`);
|
|
69
|
+
}
|
|
70
|
+
function parseFlags(args, known) {
|
|
71
|
+
const out = {};
|
|
72
|
+
for (let i = 0; i < args.length; i++) {
|
|
73
|
+
let arg = args[i], value;
|
|
74
|
+
const eq = arg.indexOf('=');
|
|
75
|
+
if (arg.startsWith('--') && eq > 0) {
|
|
76
|
+
value = arg.slice(eq + 1);
|
|
77
|
+
arg = arg.slice(0, eq);
|
|
78
|
+
}
|
|
79
|
+
const kind = known[arg];
|
|
80
|
+
if (!kind)
|
|
81
|
+
throw new Exit(2, `Unknown argument ${arg}.\n${USAGE}`);
|
|
82
|
+
if (kind === 'bool') {
|
|
83
|
+
out[arg] = true;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
value ??= args[++i];
|
|
87
|
+
if (value === undefined || value === '')
|
|
88
|
+
throw new Exit(2, `${arg} needs a value.\n${USAGE}`);
|
|
89
|
+
out[arg] = value;
|
|
90
|
+
}
|
|
91
|
+
return out;
|
|
92
|
+
}
|
|
93
|
+
const scopeWords = { ingest: 'sends traffic', evals: 'runs the benchmark' };
|
|
94
|
+
async function verify(args) {
|
|
95
|
+
const flags = parseFlags(args, { '--session': 'value', '--json': 'bool' });
|
|
96
|
+
const session = flags['--session'];
|
|
97
|
+
const { status, body } = await get(`/status${session ? `?session=${encodeURIComponent(session)}` : ''}`);
|
|
98
|
+
if (status !== 200 || !body)
|
|
99
|
+
failed(status, body);
|
|
100
|
+
const found = session ? Boolean(body.session?.found) : true;
|
|
101
|
+
if (flags['--json']) {
|
|
102
|
+
console.log(JSON.stringify(body, null, 2));
|
|
103
|
+
return found ? 0 : 1;
|
|
104
|
+
}
|
|
105
|
+
const scopes = Array.isArray(body.key?.scopes) ? body.key.scopes : [];
|
|
106
|
+
const label = body.key?.label ? ` "${body.key.label}"` : '';
|
|
107
|
+
console.log(`Workspace ${body.workspace}. Key${label} ${scopes.length ? scopes.map(s => scopeWords[s] ?? s).join(' and ') : 'has no scopes'}.`);
|
|
108
|
+
console.log(body.last_call_at ? `Last call arrived ${body.last_call_at} from ${body.last_source ?? 'unknown'}.` : 'No calls have arrived yet.');
|
|
109
|
+
if (session) {
|
|
110
|
+
const s = body.session ?? {};
|
|
111
|
+
if (!found) {
|
|
112
|
+
console.log(`Conversation ${session}: not found yet.`);
|
|
113
|
+
return 1;
|
|
114
|
+
}
|
|
115
|
+
const models = Array.isArray(s.models) ? s.models : [];
|
|
116
|
+
console.log(`Conversation ${session}: arrived, ${s.calls ?? 0} call${s.calls === 1 ? '' : 's'}${models.length ? ` (${models.join(', ')})` : ''}.`);
|
|
117
|
+
}
|
|
118
|
+
return 0;
|
|
119
|
+
}
|
|
120
|
+
async function benchmark(args) {
|
|
121
|
+
const [sub, ...rest] = args;
|
|
122
|
+
if (sub !== 'pull')
|
|
123
|
+
throw new Exit(2, USAGE);
|
|
124
|
+
const flags = parseFlags(rest, { '--out': 'value' });
|
|
125
|
+
const out = flags['--out'] ?? 'benchmark.json';
|
|
126
|
+
const { status, body } = await get('/benchmark');
|
|
127
|
+
if (status === 403)
|
|
128
|
+
throw new Exit(1, errorText(status, body) === `HTTP 403`
|
|
129
|
+
? 'This key cannot download the benchmark. On API keys, create a key that can run the benchmark.' : errorText(status, body));
|
|
130
|
+
if (status !== 200 || !body)
|
|
131
|
+
failed(status, body);
|
|
132
|
+
try {
|
|
133
|
+
writeFileSync(out, JSON.stringify(body, null, 2) + '\n');
|
|
134
|
+
}
|
|
135
|
+
catch (error) {
|
|
136
|
+
throw new Exit(1, `Could not write ${out}: ${error.message}`);
|
|
137
|
+
}
|
|
138
|
+
const tasks = Array.isArray(body.tasks) ? body.tasks : [];
|
|
139
|
+
const splits = new Map();
|
|
140
|
+
for (const t of tasks) {
|
|
141
|
+
const s = typeof t?.split === 'string' && t.split ? t.split : 'unsplit';
|
|
142
|
+
splits.set(s, (splits.get(s) ?? 0) + 1);
|
|
143
|
+
}
|
|
144
|
+
const bySplit = [...splits].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)).map(([s, n]) => `${n} ${s}`).join(', ');
|
|
145
|
+
console.log(`Wrote ${out}: ${tasks.length} task${tasks.length === 1 ? '' : 's'}${bySplit ? ` (${bySplit})` : ''}.`);
|
|
146
|
+
return 0;
|
|
147
|
+
}
|
|
148
|
+
async function main(argv) {
|
|
149
|
+
const [command, ...args] = argv;
|
|
150
|
+
try {
|
|
151
|
+
if (command === 'verify')
|
|
152
|
+
return await verify(args);
|
|
153
|
+
if (command === 'benchmark')
|
|
154
|
+
return await benchmark(args);
|
|
155
|
+
if (command === 'evals') {
|
|
156
|
+
console.error(PYTHON_HINT);
|
|
157
|
+
return 2;
|
|
158
|
+
}
|
|
159
|
+
if (command === '-h' || command === '--help' || command === 'help') {
|
|
160
|
+
console.log(USAGE);
|
|
161
|
+
return 0;
|
|
162
|
+
}
|
|
163
|
+
console.error(USAGE);
|
|
164
|
+
return 2;
|
|
165
|
+
}
|
|
166
|
+
catch (error) {
|
|
167
|
+
if (error instanceof Exit) {
|
|
168
|
+
(error.code === 0 ? console.log : console.error)(error.message);
|
|
169
|
+
return error.code;
|
|
170
|
+
}
|
|
171
|
+
console.error(`generalcontext: ${error?.message ?? error}`);
|
|
172
|
+
return 1;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
main(process.argv.slice(2)).then(code => { process.exitCode = code; });
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
export declare const VERSION = "0.3.0";
|
|
2
|
+
type Metadata = Record<string, string | number | boolean>;
|
|
3
|
+
export type Session = {
|
|
4
|
+
sessionId: string;
|
|
5
|
+
userId?: string;
|
|
6
|
+
channel?: string;
|
|
7
|
+
product?: string;
|
|
8
|
+
account?: string;
|
|
9
|
+
metadata?: Metadata;
|
|
10
|
+
};
|
|
11
|
+
type Tags = {
|
|
12
|
+
id: string;
|
|
13
|
+
user_id?: string;
|
|
14
|
+
channel?: string;
|
|
15
|
+
product?: string;
|
|
16
|
+
account?: string;
|
|
17
|
+
metadata?: Metadata;
|
|
18
|
+
};
|
|
19
|
+
type Api = 'openai-chat' | 'openai-responses' | 'anthropic' | 'gemini' | 'bedrock-converse' | 'openai-realtime';
|
|
20
|
+
/** Voice timing on 'openai-realtime' calls. Timestamps are ISO strings, null when not seen. */
|
|
21
|
+
export type VoiceTiming = {
|
|
22
|
+
speech_stopped_at: string | null;
|
|
23
|
+
first_audio_at: string | null;
|
|
24
|
+
interrupted: boolean;
|
|
25
|
+
truncated_at_ms?: number;
|
|
26
|
+
};
|
|
27
|
+
export type Call = {
|
|
28
|
+
api: Api;
|
|
29
|
+
conversation: Tags | null;
|
|
30
|
+
task: string | null;
|
|
31
|
+
request: unknown;
|
|
32
|
+
response: unknown;
|
|
33
|
+
started_at: string;
|
|
34
|
+
ended_at: string;
|
|
35
|
+
error: string | null;
|
|
36
|
+
voice?: VoiceTiming;
|
|
37
|
+
};
|
|
38
|
+
type Scrub = (text: string) => string;
|
|
39
|
+
type BeforeSend = (call: Call) => Call | null | undefined;
|
|
40
|
+
export type InitializeOptions = {
|
|
41
|
+
apiKey?: string;
|
|
42
|
+
/** Collector URL. Must be https://, except http://localhost, 127.0.0.1 or [::1]. */
|
|
43
|
+
baseUrl?: string;
|
|
44
|
+
appName?: string;
|
|
45
|
+
/** true for built-in email/phone/card/SSN placeholders, or your own (text) => text. */
|
|
46
|
+
redact?: boolean | Scrub;
|
|
47
|
+
/** Runs at send time on each call, before redaction. Return the call to send, or null to drop it. */
|
|
48
|
+
beforeSend?: BeforeSend;
|
|
49
|
+
/**
|
|
50
|
+
* SDK classes or modules to patch, e.g. { openAI: OpenAI, anthropic: Anthropic }. Also accepts
|
|
51
|
+
* googleGenAI (the '@google/genai' module) and bedrockRuntime (the '@aws-sdk/client-bedrock-runtime' module).
|
|
52
|
+
*/
|
|
53
|
+
instrumentModules?: Modules;
|
|
54
|
+
/** Also record raw fetch POSTs to any URL ending /chat/completions. Default false. */
|
|
55
|
+
instrumentFetch?: boolean;
|
|
56
|
+
/** Send each call right away instead of batching. */
|
|
57
|
+
disableBatch?: boolean;
|
|
58
|
+
};
|
|
59
|
+
type Modules = {
|
|
60
|
+
openAI?: unknown;
|
|
61
|
+
anthropic?: unknown;
|
|
62
|
+
googleGenAI?: unknown;
|
|
63
|
+
bedrockRuntime?: unknown;
|
|
64
|
+
};
|
|
65
|
+
/** Configure recording once per process, before your agent makes model calls. */
|
|
66
|
+
export declare function initialize(options?: InitializeOptions): void;
|
|
67
|
+
/**
|
|
68
|
+
* Resolves once initialize() has patched every SDK build it found, including the ES module builds it loads
|
|
69
|
+
* asynchronously. Await it when a model call may run before the app's next await after initialize(). Never rejects.
|
|
70
|
+
*/
|
|
71
|
+
export declare function ready(): Promise<void>;
|
|
72
|
+
/** Tag every call made inside `run` with one session. `conversation` is the same function. */
|
|
73
|
+
export declare function withSession<T>(session: Session | string, run: () => T): T;
|
|
74
|
+
/** Older name for withSession. Accepts { id } as well as { sessionId }. */
|
|
75
|
+
export declare function conversation<T>(tags: Session | {
|
|
76
|
+
id: string;
|
|
77
|
+
channel?: string;
|
|
78
|
+
product?: string;
|
|
79
|
+
account?: string;
|
|
80
|
+
metadata?: Metadata;
|
|
81
|
+
} | string, run: () => T): T;
|
|
82
|
+
/** Tag every call made inside `run` with a task name, such as one step of your agent loop. */
|
|
83
|
+
export declare function task<T>(name: string, run: () => T): T;
|
|
84
|
+
/**
|
|
85
|
+
* The copy of a call that is recorded. Never changes `value`, and never touches what the caller sends to the provider.
|
|
86
|
+
* Agents resend the whole run on every call, so without this, later calls pass the per-call limit and are lost.
|
|
87
|
+
*
|
|
88
|
+
* - Inline binary becomes a placeholder that keeps the shape: a base64 data URL keeps its prefix
|
|
89
|
+
* (`data:image/png;base64,[omitted N bytes sha256:ab12cd34ef56]`), and a base64 `data`, `result` or
|
|
90
|
+
* `b64_json` field (Anthropic `source.data`, Gemini `inlineData.data`, OpenAI `input_audio.data`, Responses
|
|
91
|
+
* `image_generation_call.result`) becomes `[omitted N bytes sha256:...]`. N is the length of the base64 text
|
|
92
|
+
* and the hash is the first 12 hex digits of the sha256 of that text, so the same image gets the same placeholder
|
|
93
|
+
* in every call. Media types stay. Binary is never recorded: it does not help review and it is a privacy risk.
|
|
94
|
+
* - Any string over 200,000 characters keeps its first 100,000 and last 20,000 characters around
|
|
95
|
+
* `\n[... clipped N characters ...]\n`.
|
|
96
|
+
*
|
|
97
|
+
* Runs before beforeSend and redaction. A call still over the limit afterwards is dropped.
|
|
98
|
+
*/
|
|
99
|
+
export declare function compactForRecording(value: unknown, key?: string, depth?: number): unknown;
|
|
100
|
+
export declare function maskSecrets(text: string): string;
|
|
101
|
+
/**
|
|
102
|
+
* Wait until recorded calls are sent, for at most `timeoutMs` (default 10 s). Call before a serverless
|
|
103
|
+
* function or script exits. Resolves true when the queue was sent, false at the deadline. Never throws.
|
|
104
|
+
* Sending continues in the background after the deadline.
|
|
105
|
+
*/
|
|
106
|
+
export declare function flush(timeoutMs?: number): Promise<boolean>;
|
|
107
|
+
/** Mark how a session ended. Returns true when the platform accepted it. Never throws. */
|
|
108
|
+
export declare function reportOutcome(sessionId: string, outcome: 'success' | 'fail', note?: string): Promise<boolean>;
|
|
109
|
+
export type AppEvent = {
|
|
110
|
+
sessionId: string;
|
|
111
|
+
eventType: string;
|
|
112
|
+
properties?: Metadata;
|
|
113
|
+
eventId?: string;
|
|
114
|
+
timestamp?: string | Date;
|
|
115
|
+
};
|
|
116
|
+
/** Record something that happened in your systems, such as 'order.refunded'. Pass a stable eventId so retries dedupe. Never throws. */
|
|
117
|
+
export declare function logEvent(event: AppEvent): Promise<boolean>;
|
|
118
|
+
/**
|
|
119
|
+
* Record one client instance only, without initialize(). Returns the same client.
|
|
120
|
+
* `redact` applies to this client only. If initialize() already patched the SDK, its settings apply instead.
|
|
121
|
+
*/
|
|
122
|
+
export declare function wrap<C extends object>(client: C, options?: {
|
|
123
|
+
redact?: boolean | Scrub;
|
|
124
|
+
}): C;
|
|
125
|
+
export {};
|