@opengeni/jev 0.1.0-canary.36199476632001
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 +190 -0
- package/README.md +140 -0
- package/dist/circuit-breaker.d.ts +58 -0
- package/dist/client.d.ts +173 -0
- package/dist/code-search/config.d.ts +142 -0
- package/dist/code-search/judge.d.ts +169 -0
- package/dist/code-search/leads.d.ts +96 -0
- package/dist/code-search/pack.d.ts +106 -0
- package/dist/code-search/recall.d.ts +155 -0
- package/dist/code-search/search.d.ts +93 -0
- package/dist/code-search/session.d.ts +33 -0
- package/dist/code-search/text.d.ts +35 -0
- package/dist/code-search/tool.d.ts +34 -0
- package/dist/code-search/windows.d.ts +85 -0
- package/dist/code-search/workspace.d.ts +51 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +3110 -0
- package/dist/index.js.map +1 -0
- package/package.json +39 -0
- package/src/circuit-breaker.ts +135 -0
- package/src/client.ts +577 -0
- package/src/code-search/config.ts +282 -0
- package/src/code-search/judge.ts +413 -0
- package/src/code-search/leads.ts +442 -0
- package/src/code-search/pack.ts +354 -0
- package/src/code-search/recall.ts +648 -0
- package/src/code-search/search.ts +773 -0
- package/src/code-search/session.ts +89 -0
- package/src/code-search/text.ts +159 -0
- package/src/code-search/tool.ts +209 -0
- package/src/code-search/windows.ts +617 -0
- package/src/code-search/workspace.ts +55 -0
- package/src/index.ts +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
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
|
+
Copyright 2026 Cloudgeni-ai
|
|
179
|
+
|
|
180
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
181
|
+
you may not use this file except in compliance with the License.
|
|
182
|
+
You may obtain a copy of the License at
|
|
183
|
+
|
|
184
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
185
|
+
|
|
186
|
+
Unless required by applicable law or agreed to in writing, software
|
|
187
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
188
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
189
|
+
See the License for the specific language governing permissions and
|
|
190
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
# `@opengeni/jev`
|
|
2
|
+
|
|
3
|
+
`@opengeni/jev` holds OpenGeni's client for TypeSafe Jev, a fast judge model, and the Jev-ranked
|
|
4
|
+
`code_search` engine behind the worker's `code_search` agent tool. The package has no runtime
|
|
5
|
+
dependencies. It uses global `fetch`, runs under Node 18+ and Bun, and makes no filesystem or process
|
|
6
|
+
calls of its own.
|
|
7
|
+
|
|
8
|
+
## Jev client
|
|
9
|
+
|
|
10
|
+
```ts
|
|
11
|
+
const jev = new JevClient({ apiKey });
|
|
12
|
+
const r = await jev.ask(state, { urgent: noul("Does this convey urgency?") }, { signal });
|
|
13
|
+
r.answers.urgent.probability; // 0..1
|
|
14
|
+
r.costUsd; // $0.042 per 1M input tokens; output is free
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
The client calls `POST {baseUrl}/v1/systemone` with `{state, model, questions}`. When the state plus
|
|
18
|
+
the longest question would exceed 32k tokens, or the state plus all questions would exceed 64k tokens,
|
|
19
|
+
it splits the questions across several requests. It runs those requests concurrently (16 at a time by
|
|
20
|
+
default) and merges the answers.
|
|
21
|
+
|
|
22
|
+
Answers are normalized as follows:
|
|
23
|
+
|
|
24
|
+
- A noul answer becomes `{type: "noul", probability}`.
|
|
25
|
+
- A choice answer becomes `{type: "choice", option, probabilities?, confidence?}`.
|
|
26
|
+
- A score answer is passed through unchanged.
|
|
27
|
+
|
|
28
|
+
Retries and errors work like this:
|
|
29
|
+
|
|
30
|
+
- The client retries only network errors (including the per-attempt timeout), HTTP 429 and 5xx. The
|
|
31
|
+
backoff is bounded, and a `retry-after` hint is honoured up to `maxRetryAfterMs` (2 s by default).
|
|
32
|
+
- `JevUnavailableError` covers network errors, timeouts, 5xx, 429 after the last retry, and 401, 402
|
|
33
|
+
or 403 (auth or billing, never retried). `status` carries the HTTP status.
|
|
34
|
+
- `JevRequestError` covers every other 4xx (including `400 max_tokens_exceeded`) and a state too large
|
|
35
|
+
to fit the limits (`code: "state_too_large"`). These are never retried.
|
|
36
|
+
- The caller's `AbortSignal` cancels queued and in-flight requests, and the call rejects with the
|
|
37
|
+
signal's reason.
|
|
38
|
+
|
|
39
|
+
`JevCircuitBreaker` is an in-process breaker, and the worker keeps one per process:
|
|
40
|
+
|
|
41
|
+
- 3 consecutive `JevUnavailableError`s open it for 5 minutes. If the failure that opens it is a 401,
|
|
42
|
+
402 or 403, it opens for 30 minutes instead.
|
|
43
|
+
- `tryAcquire()` returns a lease (`{id, trial}`) for each admitted call, or `null` when it refuses the
|
|
44
|
+
call. The caller settles the lease exactly once, with `recordSuccess(lease)`,
|
|
45
|
+
`recordFailure(error, now, lease)` or `release(lease)` (the call never reached Jev).
|
|
46
|
+
- After the cooldown the breaker is half-open, and `tryAcquire()` admits one trial call at a time.
|
|
47
|
+
Others are refused until the trial ends or has run for 10 minutes. That is longer than one Jev
|
|
48
|
+
retry cycle at the largest allowed request timeout (3 attempts of 120 s), plus backoff and sandbox
|
|
49
|
+
recall, so a slow trial is not mistaken for a stuck one.
|
|
50
|
+
- A success from any call closes the breaker, and an unavailable failure from any call counts. While
|
|
51
|
+
half-open, one more unavailable failure reopens it at once.
|
|
52
|
+
- `release()` and other errors neither count nor reset the streak. They end the trial only when their
|
|
53
|
+
lease is the trial's, so a call admitted while closed cannot free the slot of a trial running now.
|
|
54
|
+
Leases are matched by identity.
|
|
55
|
+
- `isOpen()` and `status()` are for metrics and diagnostics. Never use them to decide which tools a
|
|
56
|
+
turn is offered: the tool list is part of the model's cached prompt prefix, and a breaker is
|
|
57
|
+
per process, so tools that come and go with it break the provider's prompt cache.
|
|
58
|
+
|
|
59
|
+
## code_search
|
|
60
|
+
|
|
61
|
+
`runCodeSearch({question, keywords, subQuestions?, paths?, workspace, jev, signal?, budgetTokens?})`
|
|
62
|
+
ports the validated research tool scout-0.3.1. The ranking, thresholds and defaults
|
|
63
|
+
(`DEFAULT_CODE_SEARCH_CONFIG`) are unchanged. The pipeline has five stages:
|
|
64
|
+
|
|
65
|
+
1. **Recall:** one ripgrep pass over the keyword variants, then IDF scoring per file.
|
|
66
|
+
2. **Wave 1:** Jev triages the candidate files, with a lexical guard that keeps the top lexical files.
|
|
67
|
+
3. **Wave 2:** Jev verifies line-numbered passages, including coverage of each sub-question.
|
|
68
|
+
4. **Wave 3:** Jev scores one round of leads (definitions referenced by the evidence), and the chosen
|
|
69
|
+
definitions are verified the same way.
|
|
70
|
+
5. **Pack:** the verified passages are packed within the token budget (12,000 by default), and one
|
|
71
|
+
Jev sufficiency check sets the status.
|
|
72
|
+
|
|
73
|
+
`text` is the rendered pack. Its first line is a status header such as
|
|
74
|
+
`code_search: evidence rating 0.55 (s1 0.81) | 9 passages from 6 files, ~7.9k tokens | 3.1s`.
|
|
75
|
+
`status` gives the label and scores, and `stats` gives wall time, stage times, counts, workspace calls
|
|
76
|
+
and Jev requests, tokens and cost.
|
|
77
|
+
|
|
78
|
+
A Jev failure makes the whole search throw. There is no keyword-only fallback, because it lowered
|
|
79
|
+
answer quality in the study. The one exception is a failure of the final sufficiency check: the
|
|
80
|
+
fully Jev-scored pack is still returned, with `evidence rating unknown (check failed)` and the
|
|
81
|
+
error in `statusCheckError`.
|
|
82
|
+
|
|
83
|
+
### Workspace contract
|
|
84
|
+
|
|
85
|
+
All file and process access goes through `CodeSearchWorkspace`: `ripgrep`, `readText` and
|
|
86
|
+
`pathKinds`, all with workspace-relative paths.
|
|
87
|
+
|
|
88
|
+
The engine passes ripgrep only these flags, so an adapter can enforce an allowlist: `--files`,
|
|
89
|
+
`--null`, `--line-number`, `--with-filename`, `--no-heading`, `--color never`, `-i`, `-w`,
|
|
90
|
+
`--no-require-git`, `--hidden`, `-m N`, `--max-columns N`, `--max-filesize N`, `-g GLOB` and
|
|
91
|
+
`-e PATTERN`. These are followed by `--` and then `.` or workspace-relative paths.
|
|
92
|
+
|
|
93
|
+
A pattern is at most `CODE_SEARCH_MAX_PATTERN_CHARS` (16,000) characters. The engine splits a longer
|
|
94
|
+
keyword union or definition search into several ripgrep calls, runs 4 at a time and merges their
|
|
95
|
+
output, so the result is the same as one call.
|
|
96
|
+
|
|
97
|
+
If ripgrep output is `truncated` or `timedOut`, the search continues with what it got, and the header
|
|
98
|
+
reports it, for example `code_search (partial search: ...)`.
|
|
99
|
+
|
|
100
|
+
An adapter reports problems as follows:
|
|
101
|
+
|
|
102
|
+
- `readText` returns `null` for a missing file. Per-file problems should also be returned as `null`
|
|
103
|
+
rather than thrown.
|
|
104
|
+
- An adapter throws `CodeSearchWorkspaceError` when the workspace is unreachable. When ripgrep is
|
|
105
|
+
missing, it throws `CodeSearchRipgrepMissingError`, or a `CodeSearchWorkspaceError` whose message
|
|
106
|
+
says ripgrep is not installed.
|
|
107
|
+
- ripgrep exit code 2 with no output counts as no match for the file listing and the searches. That is
|
|
108
|
+
what ripgrep returns when nothing matched and some path was unreadable. Only an invalid pattern
|
|
109
|
+
still fails.
|
|
110
|
+
|
|
111
|
+
### Tool surface
|
|
112
|
+
|
|
113
|
+
The package exports the pieces the worker needs to register the tool:
|
|
114
|
+
|
|
115
|
+
- `CODE_SEARCH_TOOL_NAME`, `CODE_SEARCH_TOOL_DESCRIPTION` and `codeSearchInputSchema`.
|
|
116
|
+
- `parseCodeSearchArguments`, which trims, dedupes and normalizes paths, and rejects absolute paths,
|
|
117
|
+
`..` and unknown keys.
|
|
118
|
+
- `renderCodeSearchError`, which turns a failure into short model-facing text.
|
|
119
|
+
|
|
120
|
+
### Changes from scout-0.3.1
|
|
121
|
+
|
|
122
|
+
- There is no lexical judge and no whole-run lexical fallback.
|
|
123
|
+
- There are no Jev caches, file traces, CPU accounting or CLI. The optional `onStage` callback
|
|
124
|
+
replaces the trace.
|
|
125
|
+
- The header names `code_search` instead of `scout`, and the footer notes say "paths" instead of `-p`.
|
|
126
|
+
- Truncated or timed-out ripgrep output is reported in the header.
|
|
127
|
+
- File reads and binary checks run in parallel (8 at a time) through the workspace. Selecting
|
|
128
|
+
candidates gives the same result as the original sequential scan.
|
|
129
|
+
- ripgrep patterns over 16,000 characters are split into several calls with the same merged result.
|
|
130
|
+
- A file that reads shorter than ripgrep saw it, or not at all (for example UTF-16), no longer fails
|
|
131
|
+
the search. Hits and lead definitions past its end are skipped.
|
|
132
|
+
|
|
133
|
+
## Tests
|
|
134
|
+
|
|
135
|
+
```bash
|
|
136
|
+
bun test ./packages/jev/test
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The tests use a fixture repository, a `LocalCodeSearchWorkspace` (local `rg` via `Bun.spawn`, with the
|
|
140
|
+
ripgrep allowlist enforced) and a deterministic fake Jev. They never call the real API.
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
export interface JevCircuitBreakerOptions {
|
|
2
|
+
/** Consecutive unavailable failures that open the breaker. */
|
|
3
|
+
failureThreshold?: number | undefined;
|
|
4
|
+
/** Cooldown after an outage-style failure. */
|
|
5
|
+
cooldownMs?: number | undefined;
|
|
6
|
+
/** Cooldown when the opening failure was HTTP 401/402/403 (key or billing problem). */
|
|
7
|
+
authCooldownMs?: number | undefined;
|
|
8
|
+
/** A half-open trial that has not ended after this long no longer blocks the next trial. */
|
|
9
|
+
trialTimeoutMs?: number | undefined;
|
|
10
|
+
}
|
|
11
|
+
/** One admitted call. Leases are matched by identity, so only the issued object ends its trial. */
|
|
12
|
+
export interface JevCircuitLease {
|
|
13
|
+
readonly id: number;
|
|
14
|
+
/** This call is the half-open trial. */
|
|
15
|
+
readonly trial: boolean;
|
|
16
|
+
}
|
|
17
|
+
export type JevCircuitState = "closed" | "open" | "half_open";
|
|
18
|
+
export interface JevCircuitStatus {
|
|
19
|
+
state: JevCircuitState;
|
|
20
|
+
consecutiveFailures: number;
|
|
21
|
+
/** Epoch ms until which calls are refused; null when not open. */
|
|
22
|
+
openUntil: number | null;
|
|
23
|
+
/** Half-open and the one trial call is running. */
|
|
24
|
+
trialInFlight: boolean;
|
|
25
|
+
lastFailure: {
|
|
26
|
+
message: string;
|
|
27
|
+
status: number | null;
|
|
28
|
+
at: number;
|
|
29
|
+
} | null;
|
|
30
|
+
}
|
|
31
|
+
export declare class JevCircuitBreaker {
|
|
32
|
+
private readonly failureThreshold;
|
|
33
|
+
private readonly cooldownMs;
|
|
34
|
+
private readonly authCooldownMs;
|
|
35
|
+
private readonly trialTimeoutMs;
|
|
36
|
+
private consecutiveFailures;
|
|
37
|
+
private openUntil;
|
|
38
|
+
private trial;
|
|
39
|
+
private nextLeaseId;
|
|
40
|
+
private lastFailure;
|
|
41
|
+
constructor(options?: JevCircuitBreakerOptions);
|
|
42
|
+
/** True while calls should be refused without contacting Jev (the cooldown is running). */
|
|
43
|
+
isOpen(now?: number): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* Admit one call and return its lease, or null when refused. Closed: always. Open: never. Half-open:
|
|
46
|
+
* only the first caller, as the trial, until its lease is settled or it has run for trialTimeoutMs, so
|
|
47
|
+
* a trial that never ends cannot block the tool for good.
|
|
48
|
+
*/
|
|
49
|
+
tryAcquire(now?: number): JevCircuitLease | null;
|
|
50
|
+
/** End an admitted call that did not contact Jev, so its outcome says nothing about Jev. */
|
|
51
|
+
release(lease: JevCircuitLease): void;
|
|
52
|
+
/** Any call's success proves Jev works, so the lease does not change the outcome. */
|
|
53
|
+
recordSuccess(_lease?: JevCircuitLease | null): void;
|
|
54
|
+
/** Settle a failed call. A failure without a lease counts the same but never ends a trial. */
|
|
55
|
+
recordFailure(error: unknown, now?: number, lease?: JevCircuitLease | null): void;
|
|
56
|
+
status(now?: number): JevCircuitStatus;
|
|
57
|
+
private issue;
|
|
58
|
+
}
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal client for TypeSafe Jev ("System One"): POST {baseUrl}/v1/systemone with
|
|
3
|
+
* {state, model, questions} and get one typed answer per question back.
|
|
4
|
+
*
|
|
5
|
+
* - Splits a large question map across several requests so each stays inside the documented
|
|
6
|
+
* limits (32k tokens for state + longest question, 64k for state + all questions).
|
|
7
|
+
* - Retries only network errors (including per-attempt timeouts), 429 and 5xx, with bounded
|
|
8
|
+
* backoff that honours `retry-after` up to `maxRetryAfterMs`. Other 4xx are never retried.
|
|
9
|
+
* - The caller's AbortSignal cancels queued and in-flight requests and rejects with its reason.
|
|
10
|
+
* - The API key is only ever sent in the Authorization header; it never appears in errors.
|
|
11
|
+
*/
|
|
12
|
+
export declare const JEV_DEFAULT_BASE_URL = "https://api.typesafe.ai";
|
|
13
|
+
export declare const JEV_DEFAULT_MODEL = "jev-latest";
|
|
14
|
+
/** USD per 1M input tokens; output tokens are free. */
|
|
15
|
+
export declare const JEV_PRICE_PER_MILLION_INPUT_TOKENS_USD = 0.042;
|
|
16
|
+
/** Instructions and criteria may be a string or structured JSON. */
|
|
17
|
+
export type JevInstructions = string | Record<string, unknown> | unknown[];
|
|
18
|
+
export interface JevNoulQuestion {
|
|
19
|
+
type: "noul";
|
|
20
|
+
instructions: JevInstructions;
|
|
21
|
+
criteria?: {
|
|
22
|
+
true?: JevInstructions;
|
|
23
|
+
false?: JevInstructions;
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export interface JevChoiceQuestion {
|
|
27
|
+
type: "choice";
|
|
28
|
+
instructions: JevInstructions;
|
|
29
|
+
criteria: Record<string, JevInstructions | null>;
|
|
30
|
+
}
|
|
31
|
+
export interface JevScoreQuestion {
|
|
32
|
+
type: "score";
|
|
33
|
+
instructions: JevInstructions;
|
|
34
|
+
criteria: JevInstructions[];
|
|
35
|
+
}
|
|
36
|
+
export type JevQuestion = JevNoulQuestion | JevChoiceQuestion | JevScoreQuestion;
|
|
37
|
+
export interface JevNoulAnswer {
|
|
38
|
+
type: "noul";
|
|
39
|
+
/** Probability of "yes" in [0, 1]; NaN when the API returned no usable number. */
|
|
40
|
+
probability: number;
|
|
41
|
+
}
|
|
42
|
+
export interface JevChoiceAnswer {
|
|
43
|
+
type: "choice";
|
|
44
|
+
option: string;
|
|
45
|
+
probabilities?: Record<string, number>;
|
|
46
|
+
confidence?: number;
|
|
47
|
+
}
|
|
48
|
+
/** Passed through as the API returns it. */
|
|
49
|
+
export interface JevScoreAnswer {
|
|
50
|
+
type: "score";
|
|
51
|
+
score: number;
|
|
52
|
+
legend?: Record<string, string>;
|
|
53
|
+
probabilities?: Record<string, number>;
|
|
54
|
+
confidence?: number;
|
|
55
|
+
}
|
|
56
|
+
export type JevAnswer = JevNoulAnswer | JevChoiceAnswer | JevScoreAnswer;
|
|
57
|
+
export type JevAnswerFor<Q> = Q extends JevNoulQuestion ? JevNoulAnswer : Q extends JevChoiceQuestion ? JevChoiceAnswer : Q extends JevScoreQuestion ? JevScoreAnswer : JevAnswer;
|
|
58
|
+
export interface JevAskResult<Q extends Record<string, JevQuestion>> {
|
|
59
|
+
answers: {
|
|
60
|
+
[K in keyof Q]: JevAnswerFor<Q[K]>;
|
|
61
|
+
};
|
|
62
|
+
/** Versioned model id that answered (for example jev-1.13.0). */
|
|
63
|
+
model: string;
|
|
64
|
+
usage: {
|
|
65
|
+
inputTokens: number;
|
|
66
|
+
};
|
|
67
|
+
/** HTTP requests (chunks) that made up this ask. */
|
|
68
|
+
requests: number;
|
|
69
|
+
costUsd: number;
|
|
70
|
+
}
|
|
71
|
+
export interface JevAskOptions {
|
|
72
|
+
/** Short label used in error messages (for example "code_search:wave1"). */
|
|
73
|
+
tag?: string | undefined;
|
|
74
|
+
signal?: AbortSignal | undefined;
|
|
75
|
+
}
|
|
76
|
+
export declare function noul(instructions: JevInstructions, criteria?: {
|
|
77
|
+
true?: JevInstructions;
|
|
78
|
+
false?: JevInstructions;
|
|
79
|
+
}): JevNoulQuestion;
|
|
80
|
+
export declare function jevCostUsd(inputTokens: number): number;
|
|
81
|
+
export declare class JevError extends Error {
|
|
82
|
+
/** HTTP status of the failed response, when there was one. */
|
|
83
|
+
readonly status: number | undefined;
|
|
84
|
+
/** Machine-readable reason when known (for example "max_tokens_exceeded" or "state_too_large"). */
|
|
85
|
+
readonly code: string | undefined;
|
|
86
|
+
constructor(message: string, options?: {
|
|
87
|
+
status?: number;
|
|
88
|
+
code?: string;
|
|
89
|
+
cause?: unknown;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
/** Jev cannot be used right now: network, timeout, 5xx, 429 after retries, or 401/402/403 auth and billing. */
|
|
93
|
+
export declare class JevUnavailableError extends JevError {
|
|
94
|
+
}
|
|
95
|
+
/** Jev rejected the request itself (other 4xx), or the request cannot fit the limits. Never retried. */
|
|
96
|
+
export declare class JevRequestError extends JevError {
|
|
97
|
+
}
|
|
98
|
+
export interface JevLimits {
|
|
99
|
+
/** Documented: state + longest question. */
|
|
100
|
+
stateLongestMax: number;
|
|
101
|
+
/** Documented: state + all questions. */
|
|
102
|
+
stateAllMax: number;
|
|
103
|
+
/** Fraction of each limit used (token estimates are approximate). */
|
|
104
|
+
safety: number;
|
|
105
|
+
/** Measured fixed overhead per request (~265 tokens). */
|
|
106
|
+
perRequestOverhead: number;
|
|
107
|
+
/** Measured framing overhead per question (~7-10 tokens) on top of its own text. */
|
|
108
|
+
perQuestionOverhead: number;
|
|
109
|
+
/** No documented cap; keeps one request reasonable. */
|
|
110
|
+
maxQuestionsPerRequest: number;
|
|
111
|
+
}
|
|
112
|
+
export declare const JEV_DEFAULT_LIMITS: JevLimits;
|
|
113
|
+
/** Measured: TS code 3.2-3.5 chars/token, prose 4.4-4.7; 3.0 keeps the limit math conservative. */
|
|
114
|
+
export declare const JEV_CHARS_PER_TOKEN = 3;
|
|
115
|
+
export declare function estimateJevTokens(value: unknown, charsPerToken?: number): number;
|
|
116
|
+
/**
|
|
117
|
+
* Greedy packing of question ids into requests that respect both limits.
|
|
118
|
+
* Throws JevRequestError (code state_too_large) when the state plus one question cannot fit.
|
|
119
|
+
*/
|
|
120
|
+
export declare function planChunks(stateTokens: number, questionTokens: ReadonlyArray<readonly [string, number]>, limits?: JevLimits): string[][];
|
|
121
|
+
export declare class JevLimiter {
|
|
122
|
+
readonly max: number;
|
|
123
|
+
private active;
|
|
124
|
+
private readonly queue;
|
|
125
|
+
constructor(max: number);
|
|
126
|
+
run<T>(fn: () => Promise<T>, signal?: AbortSignal): Promise<T>;
|
|
127
|
+
private acquire;
|
|
128
|
+
/** A released slot passes straight to the next waiter, so `active` never overshoots `max`. */
|
|
129
|
+
private release;
|
|
130
|
+
}
|
|
131
|
+
export type JevFetch = (url: string, init: RequestInit) => Promise<Response>;
|
|
132
|
+
export interface JevClientOptions {
|
|
133
|
+
apiKey: string;
|
|
134
|
+
baseUrl?: string | undefined;
|
|
135
|
+
model?: string | undefined;
|
|
136
|
+
/** Per-attempt timeout. */
|
|
137
|
+
timeoutMs?: number | undefined;
|
|
138
|
+
/** Retries after the first attempt. */
|
|
139
|
+
maxRetries?: number | undefined;
|
|
140
|
+
/** Cap on a server `retry-after` hint; an interactive tool fails fast rather than sleeping 30 s. */
|
|
141
|
+
maxRetryAfterMs?: number | undefined;
|
|
142
|
+
/** Max concurrent HTTP requests from this client. */
|
|
143
|
+
concurrency?: number | undefined;
|
|
144
|
+
/** First backoff delay; doubles per attempt up to maxRetryAfterMs. */
|
|
145
|
+
retryBaseDelayMs?: number | undefined;
|
|
146
|
+
fetch?: JevFetch | undefined;
|
|
147
|
+
}
|
|
148
|
+
export declare class JevClient {
|
|
149
|
+
readonly baseUrl: string;
|
|
150
|
+
readonly model: string;
|
|
151
|
+
private readonly apiKey;
|
|
152
|
+
private readonly timeoutMs;
|
|
153
|
+
private readonly maxRetries;
|
|
154
|
+
private readonly maxRetryAfterMs;
|
|
155
|
+
private readonly retryBaseDelayMs;
|
|
156
|
+
private readonly fetchImpl;
|
|
157
|
+
private readonly limiter;
|
|
158
|
+
private lastActivityAt;
|
|
159
|
+
constructor(options: JevClientOptions);
|
|
160
|
+
/**
|
|
161
|
+
* Ask many questions about one state. Splits the questions into several requests when they exceed the
|
|
162
|
+
* limits, runs them concurrently (bounded by `concurrency`) and merges the answers.
|
|
163
|
+
*/
|
|
164
|
+
ask<Q extends Record<string, JevQuestion>>(state: unknown, questions: Q, options?: JevAskOptions): Promise<JevAskResult<Q>>;
|
|
165
|
+
/**
|
|
166
|
+
* Open keep-alive connections before a burst of parallel requests (a cold TLS connection costs ~0.7 s).
|
|
167
|
+
* GET /healthz is free. Skipped when the client was active within the last few seconds; never throws.
|
|
168
|
+
*/
|
|
169
|
+
warmUp(connections: number): void;
|
|
170
|
+
/** One HTTP request with retries. Returns the parsed JSON body of a 2xx response. */
|
|
171
|
+
private post;
|
|
172
|
+
private backoff;
|
|
173
|
+
}
|