raindrop-ai 0.9.1 → 1.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/dist/{chunk-WLBBPTI6.mjs → chunk-GQKFYSJ5.mjs} +3 -3
- package/dist/{chunk-SUW3YBV3.mjs → chunk-MVJNJ4TS.mjs} +14 -6
- package/dist/{chunk-UY6537WW.mjs → chunk-X637VXF6.mjs} +84 -18
- package/dist/evals/cli.js +89 -21
- package/dist/evals/cli.mjs +3 -3
- package/dist/{index-C5Aj6zuo.d.ts → index-BBP9eqvk.d.mts} +12866 -29
- package/dist/{index-C5Aj6zuo.d.mts → index-BBP9eqvk.d.ts} +12866 -29
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +223 -116
- package/dist/index.mjs +7 -3
- package/dist/portable-runtime-IIFK7U5Q.mjs +231 -0
- package/dist/tracing/index.d.mts +1 -1
- package/dist/tracing/index.d.ts +1 -1
- package/dist/tracing/index.js +3 -3
- package/dist/tracing/index.mjs +1 -1
- package/package.json +3 -3
- package/dist/portable-runtime-F7W4STGH.mjs +0 -198
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PortableEvalLimitsSchema,
|
|
3
|
+
parsePortableEvalResult
|
|
4
|
+
} from "./chunk-X637VXF6.mjs";
|
|
5
|
+
import "./chunk-UJCSKKID.mjs";
|
|
6
|
+
|
|
7
|
+
// src/evals/portable-runtime.ts
|
|
8
|
+
import variant from "@jitl/quickjs-singlefile-cjs-release-sync";
|
|
9
|
+
import {
|
|
10
|
+
newQuickJSWASMModuleFromVariant,
|
|
11
|
+
shouldInterruptAfterDeadline
|
|
12
|
+
} from "quickjs-emscripten-core";
|
|
13
|
+
import { z } from "zod";
|
|
14
|
+
var DEFAULT_MEMORY_BYTES = 32 * 1024 * 1024;
|
|
15
|
+
var DEFAULT_STACK_BYTES = 64 * 1024;
|
|
16
|
+
var DEFAULT_TIMEOUT_MS = 1e3;
|
|
17
|
+
var DEFAULT_JUDGE_TIMEOUT_MS = 6e4;
|
|
18
|
+
var DEFAULT_JUDGE_REQUEST_TIMEOUT_MS = 3e4;
|
|
19
|
+
var DEFAULT_JUDGE_CONCURRENCY = 4;
|
|
20
|
+
var DEFAULT_MAX_INPUT_BYTES = 4 * 1024 * 1024;
|
|
21
|
+
var DEFAULT_MAX_OUTPUT_BYTES = 4 * 1024 * 1024;
|
|
22
|
+
var JudgeRequestSchema = z.object({
|
|
23
|
+
type: z.literal("judge_request"),
|
|
24
|
+
requestId: z.string(),
|
|
25
|
+
traceId: z.string(),
|
|
26
|
+
rubric: z.string().min(1).max(4e4),
|
|
27
|
+
text: z.string().max(4 * 1024 * 1024),
|
|
28
|
+
outputType: z.enum(["boolean", "score"])
|
|
29
|
+
});
|
|
30
|
+
async function runPortableEvaluator(evaluator, input) {
|
|
31
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
32
|
+
const limits = PortableEvalLimitsSchema.parse((_a = evaluator.limits) != null ? _a : {});
|
|
33
|
+
const inputJson = JSON.stringify(input);
|
|
34
|
+
const maxInputBytes = (_b = limits.maxInputBytes) != null ? _b : DEFAULT_MAX_INPUT_BYTES;
|
|
35
|
+
if (byteLength(inputJson) > maxInputBytes) {
|
|
36
|
+
throw new Error(`Raindrop portable evaluator input exceeds ${maxInputBytes} bytes`);
|
|
37
|
+
}
|
|
38
|
+
const QuickJS = await newQuickJSWASMModuleFromVariant(variant);
|
|
39
|
+
const runtime = QuickJS.newRuntime();
|
|
40
|
+
try {
|
|
41
|
+
runtime.setMemoryLimit((_c = limits.memoryBytes) != null ? _c : DEFAULT_MEMORY_BYTES);
|
|
42
|
+
runtime.setMaxStackSize((_d = limits.stackBytes) != null ? _d : DEFAULT_STACK_BYTES);
|
|
43
|
+
const context = runtime.newContext();
|
|
44
|
+
try {
|
|
45
|
+
const timeoutMs = (_e = limits.timeoutMs) != null ? _e : evaluator.artifact.evaluator.executionMode === "judge" ? DEFAULT_JUDGE_TIMEOUT_MS : DEFAULT_TIMEOUT_MS;
|
|
46
|
+
const deadline = Date.now() + timeoutMs;
|
|
47
|
+
const cancellation = new AbortController();
|
|
48
|
+
const deadlineTimer = setTimeout(() => cancellation.abort(), timeoutMs);
|
|
49
|
+
runtime.setInterruptHandler(shouldInterruptAfterDeadline(deadline));
|
|
50
|
+
const deferreds = [];
|
|
51
|
+
const hostCalls = /* @__PURE__ */ new Set();
|
|
52
|
+
const tracesById = new Map(input.traces.map((trace) => [trace.event.id, trace]));
|
|
53
|
+
const casesByTraceId = new Map(
|
|
54
|
+
input.cases.map((entry) => [entry.candidate.trace.event.id, entry])
|
|
55
|
+
);
|
|
56
|
+
let activeJudgeCalls = 0;
|
|
57
|
+
const judgeWaiters = [];
|
|
58
|
+
const judgeConcurrency = (_f = limits.judgeConcurrency) != null ? _f : DEFAULT_JUDGE_CONCURRENCY;
|
|
59
|
+
const acquire = async () => {
|
|
60
|
+
if (activeJudgeCalls >= judgeConcurrency) {
|
|
61
|
+
await new Promise((resolve) => judgeWaiters.push(resolve));
|
|
62
|
+
} else {
|
|
63
|
+
activeJudgeCalls += 1;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const release = () => {
|
|
67
|
+
const next = judgeWaiters.shift();
|
|
68
|
+
if (next) next();
|
|
69
|
+
else activeJudgeCalls -= 1;
|
|
70
|
+
};
|
|
71
|
+
try {
|
|
72
|
+
const bridge = context.newFunction("__raindrop_eval_judge", (requestHandle) => {
|
|
73
|
+
const deferred = context.newPromise();
|
|
74
|
+
deferreds.push(deferred);
|
|
75
|
+
const requestJson = context.getString(requestHandle);
|
|
76
|
+
let acquired = false;
|
|
77
|
+
const hostCall = resolveJudge(
|
|
78
|
+
evaluator,
|
|
79
|
+
requestJson,
|
|
80
|
+
deadline,
|
|
81
|
+
tracesById,
|
|
82
|
+
casesByTraceId,
|
|
83
|
+
cancellation.signal,
|
|
84
|
+
async () => {
|
|
85
|
+
await acquire();
|
|
86
|
+
acquired = true;
|
|
87
|
+
}
|
|
88
|
+
).then((responseJson) => {
|
|
89
|
+
const response = context.newString(responseJson);
|
|
90
|
+
try {
|
|
91
|
+
deferred.resolve(response);
|
|
92
|
+
} finally {
|
|
93
|
+
response.dispose();
|
|
94
|
+
}
|
|
95
|
+
}).finally(() => {
|
|
96
|
+
if (acquired) release();
|
|
97
|
+
hostCalls.delete(hostCall);
|
|
98
|
+
});
|
|
99
|
+
hostCalls.add(hostCall);
|
|
100
|
+
return deferred.handle;
|
|
101
|
+
});
|
|
102
|
+
context.setProp(context.global, "__raindrop_eval_judge", bridge);
|
|
103
|
+
bridge.dispose();
|
|
104
|
+
const loaded = context.evalCode(evaluator.artifact.source, "raindrop-evaluator.js");
|
|
105
|
+
context.unwrapResult(loaded).dispose();
|
|
106
|
+
const entrypoint = context.getProp(context.global, "__raindrop_eval_run");
|
|
107
|
+
const inputHandle = context.newString(inputJson);
|
|
108
|
+
let promiseHandle;
|
|
109
|
+
try {
|
|
110
|
+
promiseHandle = context.unwrapResult(
|
|
111
|
+
context.callFunction(entrypoint, context.global, inputHandle)
|
|
112
|
+
);
|
|
113
|
+
} finally {
|
|
114
|
+
inputHandle.dispose();
|
|
115
|
+
entrypoint.dispose();
|
|
116
|
+
}
|
|
117
|
+
try {
|
|
118
|
+
const result = await drivePromise(runtime, context, promiseHandle, deadline);
|
|
119
|
+
const outputHandle = context.unwrapResult(result);
|
|
120
|
+
try {
|
|
121
|
+
const outputJson = context.getString(outputHandle);
|
|
122
|
+
const maxOutputBytes = (_g = limits.maxOutputBytes) != null ? _g : DEFAULT_MAX_OUTPUT_BYTES;
|
|
123
|
+
if (byteLength(outputJson) > maxOutputBytes) {
|
|
124
|
+
throw new Error(`Raindrop portable evaluator output exceeds ${maxOutputBytes} bytes`);
|
|
125
|
+
}
|
|
126
|
+
const envelope = JSON.parse(outputJson);
|
|
127
|
+
if (typeof envelope !== "object" || envelope === null || !("type" in envelope) || envelope.type !== "result" || !("result" in envelope)) {
|
|
128
|
+
throw new Error("Raindrop portable evaluator returned an invalid result envelope");
|
|
129
|
+
}
|
|
130
|
+
return parsePortableEvalResult(envelope.result);
|
|
131
|
+
} finally {
|
|
132
|
+
outputHandle.dispose();
|
|
133
|
+
}
|
|
134
|
+
} finally {
|
|
135
|
+
promiseHandle.dispose();
|
|
136
|
+
}
|
|
137
|
+
} catch (cause) {
|
|
138
|
+
const message = cause instanceof Error ? cause.message : String(cause);
|
|
139
|
+
if (Date.now() >= deadline || message.includes("interrupted")) {
|
|
140
|
+
throw new Error(`Raindrop portable evaluator exceeded ${timeoutMs}ms`);
|
|
141
|
+
}
|
|
142
|
+
throw new Error(`Raindrop portable evaluator failed: ${message}`);
|
|
143
|
+
} finally {
|
|
144
|
+
cancellation.abort();
|
|
145
|
+
clearTimeout(deadlineTimer);
|
|
146
|
+
for (const resume of judgeWaiters.splice(0)) resume();
|
|
147
|
+
await Promise.allSettled([...hostCalls]);
|
|
148
|
+
for (const deferred of deferreds) deferred.dispose();
|
|
149
|
+
}
|
|
150
|
+
} finally {
|
|
151
|
+
context.dispose();
|
|
152
|
+
}
|
|
153
|
+
} finally {
|
|
154
|
+
runtime.dispose();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async function drivePromise(runtime, context, promise, deadline) {
|
|
158
|
+
while (true) {
|
|
159
|
+
runtime.executePendingJobs(100).unwrap();
|
|
160
|
+
const state = context.getPromiseState(promise);
|
|
161
|
+
if (state.type === "fulfilled") return { value: state.value };
|
|
162
|
+
if (state.type === "rejected") return { error: state.error };
|
|
163
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
164
|
+
if (Date.now() >= deadline) throw new Error("interrupted");
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
async function resolveJudge(evaluator, rawRequest, deadline, tracesById, casesByTraceId, signal, acquire) {
|
|
168
|
+
var _a, _b;
|
|
169
|
+
let requestId = "unknown";
|
|
170
|
+
try {
|
|
171
|
+
const request = JudgeRequestSchema.parse(JSON.parse(rawRequest));
|
|
172
|
+
requestId = request.requestId;
|
|
173
|
+
const trace = tracesById.get(request.traceId);
|
|
174
|
+
if (!trace) throw new Error("Judge trace is outside this run");
|
|
175
|
+
if (!evaluator.judge) throw new Error("Judge unavailable");
|
|
176
|
+
await acquire();
|
|
177
|
+
signal.throwIfAborted();
|
|
178
|
+
const remainingMs = deadline - Date.now();
|
|
179
|
+
if (remainingMs <= 0) throw new Error("Judge deadline exceeded");
|
|
180
|
+
const controller = new AbortController();
|
|
181
|
+
const timeoutMs = (_b = (_a = evaluator.limits) == null ? void 0 : _a.judgeTimeoutMs) != null ? _b : DEFAULT_JUDGE_REQUEST_TIMEOUT_MS;
|
|
182
|
+
const abort = () => controller.abort();
|
|
183
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
184
|
+
const timeout = setTimeout(abort, timeoutMs);
|
|
185
|
+
const result = await Promise.race([
|
|
186
|
+
Promise.resolve().then(
|
|
187
|
+
() => {
|
|
188
|
+
var _a2;
|
|
189
|
+
return (_a2 = evaluator.judge) == null ? void 0 : _a2.call(evaluator, {
|
|
190
|
+
trace,
|
|
191
|
+
text: request.text,
|
|
192
|
+
outputType: request.outputType,
|
|
193
|
+
tracePair: rowTracePair(casesByTraceId.get(request.traceId)),
|
|
194
|
+
rubric: request.rubric.slice(0, 4e4),
|
|
195
|
+
signal: controller.signal
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
),
|
|
199
|
+
new Promise((_resolve, reject) => {
|
|
200
|
+
controller.signal.addEventListener(
|
|
201
|
+
"abort",
|
|
202
|
+
() => reject(new Error(`Judge exceeded ${timeoutMs}ms`)),
|
|
203
|
+
{ once: true }
|
|
204
|
+
);
|
|
205
|
+
})
|
|
206
|
+
]).finally(() => {
|
|
207
|
+
clearTimeout(timeout);
|
|
208
|
+
signal.removeEventListener("abort", abort);
|
|
209
|
+
});
|
|
210
|
+
if (typeof result !== "object" || result === null) throw new Error("invalid judge response");
|
|
211
|
+
return JSON.stringify({ type: "judge_response", requestId, status: "completed", result });
|
|
212
|
+
} catch (e) {
|
|
213
|
+
return JSON.stringify({
|
|
214
|
+
type: "judge_response",
|
|
215
|
+
requestId,
|
|
216
|
+
status: "unavailable",
|
|
217
|
+
message: "Judge unavailable"
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function byteLength(value) {
|
|
222
|
+
return new TextEncoder().encode(value).byteLength;
|
|
223
|
+
}
|
|
224
|
+
function rowTracePair(pair) {
|
|
225
|
+
if (!pair) return void 0;
|
|
226
|
+
const { caseId, ...evidence } = pair;
|
|
227
|
+
return { ...evidence, rowId: caseId };
|
|
228
|
+
}
|
|
229
|
+
export {
|
|
230
|
+
runPortableEvaluator
|
|
231
|
+
};
|
package/dist/tracing/index.d.mts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Instrumentation } from '@opentelemetry/instrumentation';
|
|
2
|
-
import { R as Raindrop, I as InitializeOptions, S as SpanProcessorOptions, a as RaindropSpanProcessor, B as BeginParams, b as Interaction, A as AiTrackUsage, c as AppGitSnapshot, C as CarrierHeaders, T as TraceCarrier, d as SubagentResumeOptions, e as SubagentRun, f as Tracer } from '../index-
|
|
2
|
+
import { R as Raindrop, I as InitializeOptions, S as SpanProcessorOptions, a as RaindropSpanProcessor, B as BeginParams, b as Interaction, A as AiTrackUsage, c as AppGitSnapshot, C as CarrierHeaders, T as TraceCarrier, d as SubagentResumeOptions, e as SubagentRun, f as Tracer } from '../index-BBP9eqvk.mjs';
|
|
3
3
|
import '@opentelemetry/api';
|
|
4
4
|
import 'zod';
|
|
5
5
|
import '@opentelemetry/sdk-trace-base';
|
package/dist/tracing/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Instrumentation } from '@opentelemetry/instrumentation';
|
|
2
|
-
import { R as Raindrop, I as InitializeOptions, S as SpanProcessorOptions, a as RaindropSpanProcessor, B as BeginParams, b as Interaction, A as AiTrackUsage, c as AppGitSnapshot, C as CarrierHeaders, T as TraceCarrier, d as SubagentResumeOptions, e as SubagentRun, f as Tracer } from '../index-
|
|
2
|
+
import { R as Raindrop, I as InitializeOptions, S as SpanProcessorOptions, a as RaindropSpanProcessor, B as BeginParams, b as Interaction, A as AiTrackUsage, c as AppGitSnapshot, C as CarrierHeaders, T as TraceCarrier, d as SubagentResumeOptions, e as SubagentRun, f as Tracer } from '../index-BBP9eqvk.js';
|
|
3
3
|
import '@opentelemetry/api';
|
|
4
4
|
import 'zod';
|
|
5
5
|
import '@opentelemetry/sdk-trace-base';
|
package/dist/tracing/index.js
CHANGED
|
@@ -1594,7 +1594,7 @@ function base64ToHex2(base64) {
|
|
|
1594
1594
|
// package.json
|
|
1595
1595
|
var package_default = {
|
|
1596
1596
|
name: "raindrop-ai",
|
|
1597
|
-
version: "0.
|
|
1597
|
+
version: "1.0.0",
|
|
1598
1598
|
main: "dist/index.js",
|
|
1599
1599
|
module: "dist/index.mjs",
|
|
1600
1600
|
types: "dist/index.d.ts",
|
|
@@ -1661,8 +1661,8 @@ var package_default = {
|
|
|
1661
1661
|
tsup: "^8.4.0",
|
|
1662
1662
|
tsx: "^4.20.3",
|
|
1663
1663
|
typescript: "^5.3.3",
|
|
1664
|
-
vite: "^7.3.
|
|
1665
|
-
vitest: "^4.
|
|
1664
|
+
vite: "^7.3.5",
|
|
1665
|
+
vitest: "^4.1.11"
|
|
1666
1666
|
},
|
|
1667
1667
|
dependencies: {
|
|
1668
1668
|
"@jitl/quickjs-singlefile-cjs-release-sync": "0.32.0",
|
package/dist/tracing/index.mjs
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "raindrop-ai",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"module": "dist/index.mjs",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"tsup": "^8.4.0",
|
|
47
47
|
"tsx": "^4.20.3",
|
|
48
48
|
"typescript": "^5.3.3",
|
|
49
|
-
"vite": "^7.3.
|
|
50
|
-
"vitest": "^4.
|
|
49
|
+
"vite": "^7.3.5",
|
|
50
|
+
"vitest": "^4.1.11",
|
|
51
51
|
"@raindrop-ai/core": "0.3.1",
|
|
52
52
|
"@raindrop-ai/redact-pii": "0.1.2",
|
|
53
53
|
"@raindrop-ai/schemas": "0.1.0"
|
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
parsePortableEvalResult
|
|
3
|
-
} from "./chunk-UY6537WW.mjs";
|
|
4
|
-
import "./chunk-UJCSKKID.mjs";
|
|
5
|
-
|
|
6
|
-
// src/evals/portable-runtime.ts
|
|
7
|
-
import variant from "@jitl/quickjs-singlefile-cjs-release-sync";
|
|
8
|
-
import {
|
|
9
|
-
newQuickJSWASMModuleFromVariant,
|
|
10
|
-
shouldInterruptAfterDeadline
|
|
11
|
-
} from "quickjs-emscripten-core";
|
|
12
|
-
import { z } from "zod";
|
|
13
|
-
var DEFAULT_MEMORY_BYTES = 32 * 1024 * 1024;
|
|
14
|
-
var DEFAULT_STACK_BYTES = 64 * 1024;
|
|
15
|
-
var DEFAULT_TIMEOUT_MS = 1e3;
|
|
16
|
-
var DEFAULT_JUDGE_TIMEOUT_MS = 6e4;
|
|
17
|
-
var DEFAULT_JUDGE_REQUEST_TIMEOUT_MS = 3e4;
|
|
18
|
-
var DEFAULT_JUDGE_CONCURRENCY = 4;
|
|
19
|
-
var DEFAULT_MAX_INPUT_BYTES = 4 * 1024 * 1024;
|
|
20
|
-
var DEFAULT_MAX_OUTPUT_BYTES = 4 * 1024 * 1024;
|
|
21
|
-
var JudgeRequestSchema = z.object({
|
|
22
|
-
type: z.literal("judge_request"),
|
|
23
|
-
requestId: z.string(),
|
|
24
|
-
traceId: z.string(),
|
|
25
|
-
rubric: z.string()
|
|
26
|
-
});
|
|
27
|
-
async function runPortableEvaluator(evaluator, input) {
|
|
28
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
|
|
29
|
-
const inputJson = JSON.stringify(input);
|
|
30
|
-
const maxInputBytes = (_b = (_a = evaluator.limits) == null ? void 0 : _a.maxInputBytes) != null ? _b : DEFAULT_MAX_INPUT_BYTES;
|
|
31
|
-
if (byteLength(inputJson) > maxInputBytes) {
|
|
32
|
-
throw new Error(`Raindrop portable evaluator input exceeds ${maxInputBytes} bytes`);
|
|
33
|
-
}
|
|
34
|
-
const QuickJS = await newQuickJSWASMModuleFromVariant(variant);
|
|
35
|
-
const runtime = QuickJS.newRuntime();
|
|
36
|
-
runtime.setMemoryLimit((_d = (_c = evaluator.limits) == null ? void 0 : _c.memoryBytes) != null ? _d : DEFAULT_MEMORY_BYTES);
|
|
37
|
-
runtime.setMaxStackSize((_f = (_e = evaluator.limits) == null ? void 0 : _e.stackBytes) != null ? _f : DEFAULT_STACK_BYTES);
|
|
38
|
-
const timeoutMs = (_h = (_g = evaluator.limits) == null ? void 0 : _g.timeoutMs) != null ? _h : evaluator.artifact.evaluator.executionMode === "judge" ? DEFAULT_JUDGE_TIMEOUT_MS : DEFAULT_TIMEOUT_MS;
|
|
39
|
-
const deadline = Date.now() + timeoutMs;
|
|
40
|
-
runtime.setInterruptHandler(shouldInterruptAfterDeadline(deadline));
|
|
41
|
-
const context = runtime.newContext();
|
|
42
|
-
const deferreds = [];
|
|
43
|
-
const hostCalls = /* @__PURE__ */ new Set();
|
|
44
|
-
const tracesById = new Map(input.traces.map((trace) => [trace.event.id, trace]));
|
|
45
|
-
const casesByTraceId = new Map(
|
|
46
|
-
input.cases.map((entry) => [entry.candidate.trace.event.id, entry])
|
|
47
|
-
);
|
|
48
|
-
let activeJudgeCalls = 0;
|
|
49
|
-
try {
|
|
50
|
-
const bridge = context.newFunction("__raindrop_eval_judge", (requestHandle) => {
|
|
51
|
-
var _a2, _b2;
|
|
52
|
-
const deferred = context.newPromise();
|
|
53
|
-
deferreds.push(deferred);
|
|
54
|
-
const requestJson = context.getString(requestHandle);
|
|
55
|
-
const judgeConcurrency = (_b2 = (_a2 = evaluator.limits) == null ? void 0 : _a2.judgeConcurrency) != null ? _b2 : DEFAULT_JUDGE_CONCURRENCY;
|
|
56
|
-
let acquired = false;
|
|
57
|
-
const hostCall = resolveJudge(
|
|
58
|
-
evaluator,
|
|
59
|
-
requestJson,
|
|
60
|
-
Math.max(1, deadline - Date.now()),
|
|
61
|
-
tracesById,
|
|
62
|
-
casesByTraceId,
|
|
63
|
-
() => {
|
|
64
|
-
if (activeJudgeCalls >= judgeConcurrency) return false;
|
|
65
|
-
activeJudgeCalls += 1;
|
|
66
|
-
acquired = true;
|
|
67
|
-
return true;
|
|
68
|
-
}
|
|
69
|
-
).then((responseJson) => {
|
|
70
|
-
const response = context.newString(responseJson);
|
|
71
|
-
try {
|
|
72
|
-
deferred.resolve(response);
|
|
73
|
-
} finally {
|
|
74
|
-
response.dispose();
|
|
75
|
-
}
|
|
76
|
-
}).finally(() => {
|
|
77
|
-
if (acquired) activeJudgeCalls -= 1;
|
|
78
|
-
hostCalls.delete(hostCall);
|
|
79
|
-
});
|
|
80
|
-
hostCalls.add(hostCall);
|
|
81
|
-
return deferred.handle;
|
|
82
|
-
});
|
|
83
|
-
context.setProp(context.global, "__raindrop_eval_judge", bridge);
|
|
84
|
-
bridge.dispose();
|
|
85
|
-
const loaded = context.evalCode(evaluator.artifact.source, "raindrop-evaluator.js");
|
|
86
|
-
context.unwrapResult(loaded).dispose();
|
|
87
|
-
const entrypoint = context.getProp(context.global, "__raindrop_eval_run");
|
|
88
|
-
const inputHandle = context.newString(inputJson);
|
|
89
|
-
let promiseHandle;
|
|
90
|
-
try {
|
|
91
|
-
promiseHandle = context.unwrapResult(
|
|
92
|
-
context.callFunction(entrypoint, context.global, inputHandle)
|
|
93
|
-
);
|
|
94
|
-
} finally {
|
|
95
|
-
inputHandle.dispose();
|
|
96
|
-
entrypoint.dispose();
|
|
97
|
-
}
|
|
98
|
-
try {
|
|
99
|
-
const result = await drivePromise(runtime, context, promiseHandle, deadline);
|
|
100
|
-
const outputHandle = context.unwrapResult(result);
|
|
101
|
-
try {
|
|
102
|
-
const outputJson = context.getString(outputHandle);
|
|
103
|
-
const maxOutputBytes = (_j = (_i = evaluator.limits) == null ? void 0 : _i.maxOutputBytes) != null ? _j : DEFAULT_MAX_OUTPUT_BYTES;
|
|
104
|
-
if (byteLength(outputJson) > maxOutputBytes) {
|
|
105
|
-
throw new Error(`Raindrop portable evaluator output exceeds ${maxOutputBytes} bytes`);
|
|
106
|
-
}
|
|
107
|
-
const envelope = JSON.parse(outputJson);
|
|
108
|
-
if (typeof envelope !== "object" || envelope === null || !("type" in envelope) || envelope.type !== "result" || !("result" in envelope)) {
|
|
109
|
-
throw new Error("Raindrop portable evaluator returned an invalid result envelope");
|
|
110
|
-
}
|
|
111
|
-
return parsePortableEvalResult(envelope.result);
|
|
112
|
-
} finally {
|
|
113
|
-
outputHandle.dispose();
|
|
114
|
-
}
|
|
115
|
-
} finally {
|
|
116
|
-
promiseHandle.dispose();
|
|
117
|
-
}
|
|
118
|
-
} catch (cause) {
|
|
119
|
-
const message = cause instanceof Error ? cause.message : String(cause);
|
|
120
|
-
if (Date.now() >= deadline || message.includes("interrupted")) {
|
|
121
|
-
throw new Error(`Raindrop portable evaluator exceeded ${timeoutMs}ms`);
|
|
122
|
-
}
|
|
123
|
-
throw new Error(`Raindrop portable evaluator failed: ${message}`);
|
|
124
|
-
} finally {
|
|
125
|
-
await Promise.allSettled([...hostCalls]);
|
|
126
|
-
for (const deferred of deferreds) deferred.dispose();
|
|
127
|
-
context.dispose();
|
|
128
|
-
runtime.dispose();
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
async function drivePromise(runtime, context, promise, deadline) {
|
|
132
|
-
while (true) {
|
|
133
|
-
runtime.executePendingJobs(100).unwrap();
|
|
134
|
-
const state = context.getPromiseState(promise);
|
|
135
|
-
if (state.type === "fulfilled") return { value: state.value };
|
|
136
|
-
if (state.type === "rejected") return { error: state.error };
|
|
137
|
-
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
138
|
-
if (Date.now() >= deadline) throw new Error("interrupted");
|
|
139
|
-
}
|
|
140
|
-
}
|
|
141
|
-
async function resolveJudge(evaluator, rawRequest, remainingMs, tracesById, casesByTraceId, acquire) {
|
|
142
|
-
var _a, _b;
|
|
143
|
-
let requestId = "unknown";
|
|
144
|
-
try {
|
|
145
|
-
const request = JudgeRequestSchema.parse(JSON.parse(rawRequest));
|
|
146
|
-
requestId = request.requestId;
|
|
147
|
-
const trace = tracesById.get(request.traceId);
|
|
148
|
-
if (!trace) throw new Error("Judge trace is outside this run");
|
|
149
|
-
if (!evaluator.judge) throw new Error("Judge unavailable");
|
|
150
|
-
if (!acquire()) throw new Error("Judge concurrency exceeded");
|
|
151
|
-
const controller = new AbortController();
|
|
152
|
-
const timeoutMs = Math.min(
|
|
153
|
-
(_b = (_a = evaluator.limits) == null ? void 0 : _a.judgeTimeoutMs) != null ? _b : DEFAULT_JUDGE_REQUEST_TIMEOUT_MS,
|
|
154
|
-
remainingMs
|
|
155
|
-
);
|
|
156
|
-
const timeout = setTimeout(() => controller.abort(), timeoutMs);
|
|
157
|
-
const result = await Promise.race([
|
|
158
|
-
Promise.resolve().then(
|
|
159
|
-
() => {
|
|
160
|
-
var _a2;
|
|
161
|
-
return (_a2 = evaluator.judge) == null ? void 0 : _a2.call(evaluator, {
|
|
162
|
-
trace,
|
|
163
|
-
tracePair: rowTracePair(casesByTraceId.get(request.traceId)),
|
|
164
|
-
rubric: request.rubric.slice(0, 4e4),
|
|
165
|
-
signal: controller.signal
|
|
166
|
-
});
|
|
167
|
-
}
|
|
168
|
-
),
|
|
169
|
-
new Promise((_resolve, reject) => {
|
|
170
|
-
controller.signal.addEventListener(
|
|
171
|
-
"abort",
|
|
172
|
-
() => reject(new Error(`Judge exceeded ${timeoutMs}ms`)),
|
|
173
|
-
{ once: true }
|
|
174
|
-
);
|
|
175
|
-
})
|
|
176
|
-
]).finally(() => clearTimeout(timeout));
|
|
177
|
-
if (typeof result !== "object" || result === null) throw new Error("invalid judge response");
|
|
178
|
-
return JSON.stringify({ type: "judge_response", requestId, status: "completed", result });
|
|
179
|
-
} catch (e) {
|
|
180
|
-
return JSON.stringify({
|
|
181
|
-
type: "judge_response",
|
|
182
|
-
requestId,
|
|
183
|
-
status: "unavailable",
|
|
184
|
-
message: "Judge unavailable"
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
function byteLength(value) {
|
|
189
|
-
return new TextEncoder().encode(value).byteLength;
|
|
190
|
-
}
|
|
191
|
-
function rowTracePair(pair) {
|
|
192
|
-
if (!pair) return void 0;
|
|
193
|
-
const { caseId, ...evidence } = pair;
|
|
194
|
-
return { ...evidence, rowId: caseId };
|
|
195
|
-
}
|
|
196
|
-
export {
|
|
197
|
-
runPortableEvaluator
|
|
198
|
-
};
|