nexus-ai-pro 2.0.0 → 2.2.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/API_STABILITY.md +65 -0
- package/CHANGELOG.md +267 -1
- package/MIGRATING.md +1 -1
- package/README.md +17 -8
- package/dist/agent/create-agent.d.ts +9 -1
- package/dist/agent/create-agent.js +40 -1
- package/dist/cache/memory-cache.d.ts +8 -0
- package/dist/cache/memory-cache.js +9 -0
- package/dist/cache/semantic-cache.d.ts +6 -3
- package/dist/cache/semantic-cache.js +5 -2
- package/dist/cli/args.js +3 -0
- package/dist/cli/db.d.ts +16 -4
- package/dist/cli/db.js +169 -22
- package/dist/cli/doctor.d.ts +17 -0
- package/dist/cli/doctor.js +53 -0
- package/dist/cli/graph.d.ts +9 -0
- package/dist/cli/graph.js +47 -0
- package/dist/cli.js +21 -2
- package/dist/core/lifecycle.js +11 -6
- package/dist/core/nexus.d.ts +12 -2
- package/dist/core/nexus.js +106 -23
- package/dist/doctor/index.d.ts +90 -0
- package/dist/doctor/index.js +237 -0
- package/dist/evals/judge.d.ts +11 -0
- package/dist/evals/judge.js +33 -0
- package/dist/evaluate/index.d.ts +1 -0
- package/dist/evaluate/index.js +1 -0
- package/dist/evaluate/provenance.d.ts +22 -0
- package/dist/evaluate/provenance.js +10 -0
- package/dist/evaluate/run.js +20 -4
- package/dist/graph/durability.d.ts +27 -0
- package/dist/graph/durability.js +66 -0
- package/dist/graph/errors.d.ts +30 -4
- package/dist/graph/errors.js +18 -2
- package/dist/graph/event-stream.d.ts +75 -0
- package/dist/graph/event-stream.js +289 -0
- package/dist/graph/functional.d.ts +80 -7
- package/dist/graph/functional.js +138 -35
- package/dist/graph/graph.d.ts +56 -8
- package/dist/graph/graph.js +331 -49
- package/dist/graph/index.d.ts +4 -2
- package/dist/graph/index.js +3 -1
- package/dist/graph/lint.d.ts +57 -0
- package/dist/graph/lint.js +141 -0
- package/dist/graph/run-control.d.ts +28 -0
- package/dist/graph/run-control.js +26 -0
- package/dist/images/evals.js +4 -2
- package/dist/operations/adapters.d.ts +52 -4
- package/dist/operations/adapters.js +194 -7
- package/dist/operations/runner.js +14 -15
- package/dist/operations/stats.js +8 -1
- package/dist/operations/store.d.ts +5 -1
- package/dist/operations/store.js +7 -0
- package/dist/ops/circuit-breaker.d.ts +52 -0
- package/dist/ops/circuit-breaker.js +4 -0
- package/dist/ops/circuit-store.d.ts +13 -2
- package/dist/ops/circuit-store.js +68 -0
- package/dist/ops/circuit-sync.d.ts +17 -0
- package/dist/ops/circuit-sync.js +86 -0
- package/dist/ops/health.d.ts +24 -0
- package/dist/ops/health.js +38 -10
- package/dist/ops/index.d.ts +1 -1
- package/dist/ops/index.js +1 -1
- package/dist/ops/rate-limit-adapters.d.ts +23 -0
- package/dist/ops/rate-limit-adapters.js +34 -0
- package/dist/ops/rate-limiter.d.ts +14 -2
- package/dist/ops/rate-limiter.js +31 -0
- package/dist/postgres/circuits.d.ts +21 -4
- package/dist/postgres/circuits.js +65 -4
- package/dist/postgres/client.d.ts +0 -2
- package/dist/postgres/client.js +0 -4
- package/dist/postgres/evaluate.d.ts +16 -4
- package/dist/postgres/evaluate.js +17 -5
- package/dist/postgres/index.d.ts +32 -11
- package/dist/postgres/index.js +65 -31
- package/dist/postgres/migrations.d.ts +60 -0
- package/dist/postgres/migrations.js +121 -0
- package/dist/postgres/operations.d.ts +14 -3
- package/dist/postgres/operations.js +45 -15
- package/dist/postgres/prompts.d.ts +11 -2
- package/dist/postgres/prompts.js +14 -3
- package/dist/postgres/rollups.d.ts +35 -0
- package/dist/postgres/rollups.js +129 -0
- package/dist/postgres/store.d.ts +11 -2
- package/dist/postgres/store.js +14 -3
- package/dist/postgres/traces.d.ts +13 -3
- package/dist/postgres/traces.js +25 -3
- package/dist/postgres/vectors.d.ts +11 -2
- package/dist/postgres/vectors.js +14 -3
- package/dist/router/auto-router.d.ts +34 -7
- package/dist/router/auto-router.js +201 -68
- package/dist/server/assistant.d.ts +44 -0
- package/dist/server/assistant.js +39 -0
- package/dist/server/errors.d.ts +12 -0
- package/dist/server/errors.js +7 -0
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/runs.d.ts +28 -1
- package/dist/server/runs.js +110 -14
- package/dist/sqlite/index.d.ts +16 -3
- package/dist/sqlite/index.js +16 -3
- package/dist/sqlite/migrations.d.ts +26 -0
- package/dist/sqlite/migrations.js +63 -0
- package/dist/sqlite/operations.d.ts +14 -3
- package/dist/sqlite/operations.js +42 -15
- package/dist/sqlite/store.d.ts +9 -2
- package/dist/sqlite/store.js +13 -3
- package/dist/sqlite/vectors.d.ts +11 -2
- package/dist/sqlite/vectors.js +13 -2
- package/dist/store/tenant.d.ts +12 -0
- package/dist/store/tenant.js +29 -0
- package/dist/tenancy/index.d.ts +113 -0
- package/dist/tenancy/index.js +233 -0
- package/dist/testing/record.d.ts +42 -0
- package/dist/testing/record.js +57 -5
- package/dist/tracing/index.d.ts +3 -3
- package/dist/tracing/index.js +2 -2
- package/dist/tracing/instrument.js +30 -0
- package/dist/tracing/otlp.d.ts +91 -0
- package/dist/tracing/otlp.js +124 -0
- package/dist/tracing/rollups.d.ts +100 -0
- package/dist/tracing/rollups.js +139 -0
- package/dist/tracing/stores.d.ts +17 -0
- package/dist/tracing/stores.js +23 -0
- package/dist/tracing/tracer.d.ts +50 -4
- package/dist/tracing/tracer.js +85 -6
- package/dist/types/config.d.ts +33 -2
- package/dist/types/evaluate.d.ts +37 -0
- package/dist/types/graph.d.ts +354 -9
- package/dist/types/graph.js +3 -1
- package/dist/types/messages.d.ts +5 -0
- package/dist/types/operations.d.ts +22 -2
- package/dist/types/response.d.ts +20 -0
- package/dist/types/server.d.ts +18 -0
- package/dist/types/tracing.d.ts +11 -0
- package/dist/utils/schema-migrations.d.ts +97 -0
- package/dist/utils/schema-migrations.js +91 -0
- package/dist/utils/signals.d.ts +17 -0
- package/dist/utils/signals.js +26 -0
- package/dist/utils/tenant.d.ts +14 -0
- package/dist/utils/tenant.js +16 -0
- package/dist/version.d.ts +5 -0
- package/dist/version.js +1 -0
- package/dist-cjs/agent/create-agent.d.ts +9 -1
- package/dist-cjs/agent/create-agent.js +40 -1
- package/dist-cjs/cache/memory-cache.d.ts +8 -0
- package/dist-cjs/cache/memory-cache.js +9 -0
- package/dist-cjs/cache/semantic-cache.d.ts +6 -3
- package/dist-cjs/cache/semantic-cache.js +5 -2
- package/dist-cjs/cli/args.js +3 -0
- package/dist-cjs/cli/db.d.ts +16 -4
- package/dist-cjs/cli/db.js +200 -20
- package/dist-cjs/cli/doctor.d.ts +17 -0
- package/dist-cjs/cli/doctor.js +56 -0
- package/dist-cjs/cli/graph.d.ts +9 -0
- package/dist-cjs/cli/graph.js +50 -0
- package/dist-cjs/core/lifecycle.js +11 -6
- package/dist-cjs/core/nexus.d.ts +12 -2
- package/dist-cjs/core/nexus.js +106 -23
- package/dist-cjs/doctor/index.d.ts +90 -0
- package/dist-cjs/doctor/index.js +276 -0
- package/dist-cjs/evals/judge.d.ts +11 -0
- package/dist-cjs/evals/judge.js +33 -0
- package/dist-cjs/evaluate/index.d.ts +1 -0
- package/dist-cjs/evaluate/index.js +3 -1
- package/dist-cjs/evaluate/provenance.d.ts +22 -0
- package/dist-cjs/evaluate/provenance.js +14 -0
- package/dist-cjs/evaluate/run.js +20 -4
- package/dist-cjs/graph/durability.d.ts +27 -0
- package/dist-cjs/graph/durability.js +70 -0
- package/dist-cjs/graph/errors.d.ts +30 -4
- package/dist-cjs/graph/errors.js +20 -3
- package/dist-cjs/graph/event-stream.d.ts +75 -0
- package/dist-cjs/graph/event-stream.js +295 -0
- package/dist-cjs/graph/functional.d.ts +80 -7
- package/dist-cjs/graph/functional.js +137 -34
- package/dist-cjs/graph/graph.d.ts +56 -8
- package/dist-cjs/graph/graph.js +330 -48
- package/dist-cjs/graph/index.d.ts +4 -2
- package/dist-cjs/graph/index.js +7 -1
- package/dist-cjs/graph/lint.d.ts +57 -0
- package/dist-cjs/graph/lint.js +144 -0
- package/dist-cjs/graph/run-control.d.ts +28 -0
- package/dist-cjs/graph/run-control.js +30 -0
- package/dist-cjs/images/evals.js +4 -2
- package/dist-cjs/operations/adapters.d.ts +52 -4
- package/dist-cjs/operations/adapters.js +193 -6
- package/dist-cjs/operations/runner.js +14 -15
- package/dist-cjs/operations/stats.js +8 -1
- package/dist-cjs/operations/store.d.ts +5 -1
- package/dist-cjs/operations/store.js +7 -0
- package/dist-cjs/ops/circuit-breaker.d.ts +52 -0
- package/dist-cjs/ops/circuit-breaker.js +4 -0
- package/dist-cjs/ops/circuit-store.d.ts +13 -2
- package/dist-cjs/ops/circuit-store.js +68 -0
- package/dist-cjs/ops/circuit-sync.d.ts +17 -0
- package/dist-cjs/ops/circuit-sync.js +87 -0
- package/dist-cjs/ops/health.d.ts +24 -0
- package/dist-cjs/ops/health.js +38 -10
- package/dist-cjs/ops/index.d.ts +1 -1
- package/dist-cjs/ops/rate-limit-adapters.d.ts +23 -0
- package/dist-cjs/ops/rate-limit-adapters.js +35 -1
- package/dist-cjs/ops/rate-limiter.d.ts +14 -2
- package/dist-cjs/ops/rate-limiter.js +32 -0
- package/dist-cjs/postgres/circuits.d.ts +21 -4
- package/dist-cjs/postgres/circuits.js +98 -3
- package/dist-cjs/postgres/client.d.ts +0 -2
- package/dist-cjs/postgres/client.js +0 -5
- package/dist-cjs/postgres/evaluate.d.ts +16 -4
- package/dist-cjs/postgres/evaluate.js +50 -4
- package/dist-cjs/postgres/index.d.ts +32 -11
- package/dist-cjs/postgres/index.js +69 -20
- package/dist-cjs/postgres/migrations.d.ts +60 -0
- package/dist-cjs/postgres/migrations.js +129 -0
- package/dist-cjs/postgres/operations.d.ts +14 -3
- package/dist-cjs/postgres/operations.js +78 -14
- package/dist-cjs/postgres/prompts.d.ts +11 -2
- package/dist-cjs/postgres/prompts.js +47 -2
- package/dist-cjs/postgres/rollups.d.ts +35 -0
- package/dist-cjs/postgres/rollups.js +168 -0
- package/dist-cjs/postgres/store.d.ts +11 -2
- package/dist-cjs/postgres/store.js +47 -2
- package/dist-cjs/postgres/traces.d.ts +13 -3
- package/dist-cjs/postgres/traces.js +58 -2
- package/dist-cjs/postgres/vectors.d.ts +11 -2
- package/dist-cjs/postgres/vectors.js +47 -2
- package/dist-cjs/router/auto-router.d.ts +34 -7
- package/dist-cjs/router/auto-router.js +201 -68
- package/dist-cjs/server/assistant.d.ts +44 -0
- package/dist-cjs/server/assistant.js +39 -0
- package/dist-cjs/server/errors.d.ts +12 -0
- package/dist-cjs/server/errors.js +9 -1
- package/dist-cjs/server/index.d.ts +1 -1
- package/dist-cjs/server/index.js +2 -1
- package/dist-cjs/server/runs.d.ts +28 -1
- package/dist-cjs/server/runs.js +109 -13
- package/dist-cjs/sqlite/index.d.ts +16 -3
- package/dist-cjs/sqlite/index.js +31 -10
- package/dist-cjs/sqlite/migrations.d.ts +26 -0
- package/dist-cjs/sqlite/migrations.js +71 -0
- package/dist-cjs/sqlite/operations.d.ts +14 -3
- package/dist-cjs/sqlite/operations.js +76 -15
- package/dist-cjs/sqlite/store.d.ts +9 -2
- package/dist-cjs/sqlite/store.js +47 -3
- package/dist-cjs/sqlite/vectors.d.ts +11 -2
- package/dist-cjs/sqlite/vectors.js +47 -2
- package/dist-cjs/store/tenant.d.ts +12 -0
- package/dist-cjs/store/tenant.js +33 -0
- package/dist-cjs/tenancy/index.d.ts +113 -0
- package/dist-cjs/tenancy/index.js +250 -0
- package/dist-cjs/testing/record.d.ts +42 -0
- package/dist-cjs/testing/record.js +57 -5
- package/dist-cjs/tracing/index.d.ts +3 -3
- package/dist-cjs/tracing/index.js +4 -1
- package/dist-cjs/tracing/instrument.js +30 -0
- package/dist-cjs/tracing/otlp.d.ts +91 -0
- package/dist-cjs/tracing/otlp.js +129 -0
- package/dist-cjs/tracing/rollups.d.ts +100 -0
- package/dist-cjs/tracing/rollups.js +147 -0
- package/dist-cjs/tracing/stores.d.ts +17 -0
- package/dist-cjs/tracing/stores.js +24 -0
- package/dist-cjs/tracing/tracer.d.ts +50 -4
- package/dist-cjs/tracing/tracer.js +87 -6
- package/dist-cjs/types/config.d.ts +33 -2
- package/dist-cjs/types/evaluate.d.ts +37 -0
- package/dist-cjs/types/graph.d.ts +354 -9
- package/dist-cjs/types/graph.js +3 -1
- package/dist-cjs/types/messages.d.ts +5 -0
- package/dist-cjs/types/operations.d.ts +22 -2
- package/dist-cjs/types/response.d.ts +20 -0
- package/dist-cjs/types/server.d.ts +18 -0
- package/dist-cjs/types/tracing.d.ts +11 -0
- package/dist-cjs/utils/schema-migrations.d.ts +97 -0
- package/dist-cjs/utils/schema-migrations.js +102 -0
- package/dist-cjs/utils/signals.d.ts +17 -0
- package/dist-cjs/utils/signals.js +29 -0
- package/dist-cjs/utils/tenant.d.ts +14 -0
- package/dist-cjs/utils/tenant.js +22 -0
- package/dist-cjs/version.d.ts +5 -0
- package/dist-cjs/version.js +4 -0
- package/llms.txt +4 -3
- package/package.json +110 -2
package/dist/core/nexus.js
CHANGED
|
@@ -154,13 +154,26 @@ export class NexusAI {
|
|
|
154
154
|
}
|
|
155
155
|
await this.applyBudgets(context, ticket);
|
|
156
156
|
const responseFormat = context.request.responseFormat || this.config.responseFormat;
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
157
|
+
const cachePlan = this.isCacheEnabled() ? this.cachePlan(context.request, responseFormat) : undefined;
|
|
158
|
+
let cacheOutcome;
|
|
159
|
+
if (cachePlan && !cachePlan.read) {
|
|
160
|
+
cacheOutcome = {
|
|
161
|
+
outcome: 'bypass',
|
|
162
|
+
reason: context.request.responseCache === 'refresh'
|
|
163
|
+
? 'the request asked for a fresh answer'
|
|
164
|
+
: 'the request skipped the cache',
|
|
165
|
+
...(cachePlan.namespace ? { namespace: cachePlan.namespace } : {}),
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
if (cachePlan?.read) {
|
|
169
|
+
const plan = cachePlan;
|
|
170
|
+
const lookup = await this.pipeline.trace(context, 'cacheLookup', () => this.getCachedResponse(plan, context.request));
|
|
171
|
+
cacheOutcome = lookup.outcome;
|
|
172
|
+
const cached = lookup.response;
|
|
160
173
|
if (cached) {
|
|
161
174
|
let cachedResponse = this.attachTrace({
|
|
162
175
|
...cached,
|
|
163
|
-
meta: { ...cached.meta, requestId: ticket.descriptor.requestId, cacheHit: true },
|
|
176
|
+
meta: { ...cached.meta, requestId: ticket.descriptor.requestId, cacheHit: true, cache: lookup.outcome },
|
|
164
177
|
}, this.pipeline.finish(context));
|
|
165
178
|
if (this.isSecurityEnabled()) {
|
|
166
179
|
const outputResult = this.security.protectOutput(cachedResponse);
|
|
@@ -222,12 +235,24 @@ export class NexusAI {
|
|
|
222
235
|
});
|
|
223
236
|
context.response = formattedResponse;
|
|
224
237
|
}
|
|
225
|
-
if (
|
|
238
|
+
if (cachePlan?.write) {
|
|
226
239
|
const responseToCache = context.response;
|
|
227
240
|
if (!responseToCache) {
|
|
228
241
|
throw new Error('Cannot cache an empty response.');
|
|
229
242
|
}
|
|
230
|
-
|
|
243
|
+
const plan = cachePlan;
|
|
244
|
+
try {
|
|
245
|
+
await this.pipeline.trace(context, 'cacheWrite', () => this.setCachedResponse(plan, context.request, responseToCache));
|
|
246
|
+
}
|
|
247
|
+
catch (error) {
|
|
248
|
+
cacheOutcome = {
|
|
249
|
+
...(cacheOutcome ?? { outcome: 'miss', reason: 'no entry for this request' }),
|
|
250
|
+
writeError: error instanceof Error ? error.message : String(error),
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (cacheOutcome && context.response) {
|
|
255
|
+
context.response = { ...context.response, meta: { ...context.response.meta, cache: cacheOutcome } };
|
|
231
256
|
}
|
|
232
257
|
context = await this.pipeline.runHook('beforeReturn', context);
|
|
233
258
|
let responseToReturn = context.response;
|
|
@@ -547,9 +572,11 @@ export class NexusAI {
|
|
|
547
572
|
getProviderHealth() {
|
|
548
573
|
return this.health.snapshot();
|
|
549
574
|
}
|
|
550
|
-
async checkProviders() {
|
|
575
|
+
async checkProviders(options = {}) {
|
|
551
576
|
const results = [];
|
|
552
577
|
for (const [providerName, provider] of this.providers) {
|
|
578
|
+
if (options.staleOnly && !this.health.isUnknown(providerName))
|
|
579
|
+
continue;
|
|
553
580
|
const started = Date.now();
|
|
554
581
|
try {
|
|
555
582
|
const ok = await provider.healthCheck();
|
|
@@ -792,35 +819,86 @@ export class NexusAI {
|
|
|
792
819
|
});
|
|
793
820
|
return this.semanticCacheLoading;
|
|
794
821
|
}
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
822
|
+
cachePlan(request, responseFormat) {
|
|
823
|
+
const configured = this.config.cache?.namespace;
|
|
824
|
+
const chosen = typeof configured === 'function' ? configured(request) : configured;
|
|
825
|
+
const namespace = chosen ? namespaceKey({ ...chosen, tenant: chosen.tenant ?? request.tenantId }) : undefined;
|
|
826
|
+
const base = createCacheKey({ request: { ...request, responseCache: undefined }, responseFormat });
|
|
827
|
+
return {
|
|
828
|
+
key: namespace ? `${namespace}\u0000${base}` : base,
|
|
829
|
+
...(namespace ? { namespace } : {}),
|
|
830
|
+
scope: namespace ?? namespaceKey({ tenant: request.tenantId }),
|
|
831
|
+
read: request.responseCache === undefined,
|
|
832
|
+
write: request.responseCache !== 'bypass',
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
async getCachedResponse(plan, request) {
|
|
836
|
+
const namespace = plan.namespace ? { namespace: plan.namespace } : {};
|
|
837
|
+
const cache = this.config.cache ?? {};
|
|
838
|
+
let layer = this.usesSemanticCache() ? 'semantic' : 'exact';
|
|
839
|
+
try {
|
|
840
|
+
if (this.usesSemanticCache()) {
|
|
841
|
+
const semantic = await (await this.loadSemanticCache()).get(request, plan.scope);
|
|
842
|
+
if (semantic) {
|
|
843
|
+
return {
|
|
844
|
+
response: semantic,
|
|
845
|
+
outcome: { outcome: 'hit', layer: 'semantic', reason: 'a similar request was answered', ...namespace },
|
|
846
|
+
};
|
|
847
|
+
}
|
|
848
|
+
if (cache.strategy === 'semantic') {
|
|
849
|
+
return {
|
|
850
|
+
outcome: {
|
|
851
|
+
outcome: 'miss',
|
|
852
|
+
layer: 'semantic',
|
|
853
|
+
reason: 'no similar request above the threshold',
|
|
854
|
+
...namespace,
|
|
855
|
+
},
|
|
856
|
+
};
|
|
857
|
+
}
|
|
858
|
+
}
|
|
859
|
+
layer = 'exact';
|
|
860
|
+
if (cache.adapter) {
|
|
861
|
+
const value = (await cache.adapter.get(plan.key));
|
|
862
|
+
return value
|
|
863
|
+
? { response: value, outcome: { outcome: 'hit', layer, reason: 'this request was answered', ...namespace } }
|
|
864
|
+
: { outcome: { outcome: 'miss', layer, reason: 'no entry for this request', ...namespace } };
|
|
865
|
+
}
|
|
866
|
+
const { value, stale } = this.cache.lookup(plan.key);
|
|
867
|
+
if (value) {
|
|
868
|
+
return {
|
|
869
|
+
response: value,
|
|
870
|
+
outcome: { outcome: 'hit', layer, reason: 'this request was answered', ...namespace },
|
|
871
|
+
};
|
|
872
|
+
}
|
|
873
|
+
return stale
|
|
874
|
+
? { outcome: { outcome: 'stale', layer, reason: 'the entry for this request had expired', ...namespace } }
|
|
875
|
+
: { outcome: { outcome: 'miss', layer, reason: 'no entry for this request', ...namespace } };
|
|
802
876
|
}
|
|
803
|
-
|
|
804
|
-
return
|
|
805
|
-
|
|
806
|
-
|
|
877
|
+
catch (error) {
|
|
878
|
+
return {
|
|
879
|
+
outcome: {
|
|
880
|
+
outcome: 'error',
|
|
881
|
+
layer,
|
|
882
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
883
|
+
...namespace,
|
|
884
|
+
},
|
|
885
|
+
};
|
|
807
886
|
}
|
|
808
|
-
return this.cache.get(cacheKey);
|
|
809
887
|
}
|
|
810
|
-
async setCachedResponse(
|
|
888
|
+
async setCachedResponse(plan, request, response) {
|
|
811
889
|
if (!this.config.cache?.enabled)
|
|
812
890
|
return;
|
|
813
891
|
const ttl = this.config.cache.ttlSeconds || 300;
|
|
814
892
|
if (this.usesSemanticCache()) {
|
|
815
|
-
await (await this.loadSemanticCache()).set(
|
|
893
|
+
await (await this.loadSemanticCache()).set(plan.key, request, response, plan.scope);
|
|
816
894
|
}
|
|
817
895
|
if (this.config.cache.strategy === 'semantic')
|
|
818
896
|
return;
|
|
819
897
|
if (this.config.cache.adapter) {
|
|
820
|
-
await this.config.cache.adapter.set(
|
|
898
|
+
await this.config.cache.adapter.set(plan.key, response, ttl);
|
|
821
899
|
return;
|
|
822
900
|
}
|
|
823
|
-
this.cache.set(
|
|
901
|
+
this.cache.set(plan.key, response, ttl);
|
|
824
902
|
}
|
|
825
903
|
estimateRequestCost(request) {
|
|
826
904
|
return estimateCost({
|
|
@@ -854,3 +932,8 @@ function streamStopped() {
|
|
|
854
932
|
error.name = 'AbortError';
|
|
855
933
|
return error;
|
|
856
934
|
}
|
|
935
|
+
function namespaceKey(namespace) {
|
|
936
|
+
return [namespace.tenant, namespace.environment, namespace.modelRevision, namespace.contextVersion]
|
|
937
|
+
.map((part) => encodeURIComponent(part ?? ''))
|
|
938
|
+
.join('/');
|
|
939
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import type { SchemaMigration } from '../utils/schema-migrations.js';
|
|
2
|
+
/** How one check came out: `fail` breaks the deployment, `warn` needs a look, `skip` had nothing to check. */
|
|
3
|
+
export type DoctorStatus = 'ok' | 'warn' | 'fail' | 'skip';
|
|
4
|
+
/** One check's result. */
|
|
5
|
+
export interface DoctorCheck {
|
|
6
|
+
/** A stable id, such as `node` or `migrations`. */
|
|
7
|
+
id: string;
|
|
8
|
+
/** What was checked, in a few words. */
|
|
9
|
+
title: string;
|
|
10
|
+
/** How it came out. */
|
|
11
|
+
status: DoctorStatus;
|
|
12
|
+
/** What was found. Never a secret's value. */
|
|
13
|
+
detail: string;
|
|
14
|
+
/** What to do about a warning or a failure. */
|
|
15
|
+
hint?: string;
|
|
16
|
+
}
|
|
17
|
+
/** Every check's result, and whether the deployment can run. */
|
|
18
|
+
export interface DoctorReport {
|
|
19
|
+
/** The checks, in the order they ran. */
|
|
20
|
+
checks: DoctorCheck[];
|
|
21
|
+
/** True when nothing failed. */
|
|
22
|
+
ok: boolean;
|
|
23
|
+
/** Checks that failed. */
|
|
24
|
+
failures: number;
|
|
25
|
+
/** Checks that warned. */
|
|
26
|
+
warnings: number;
|
|
27
|
+
}
|
|
28
|
+
/** A database for the doctor to reach, as `nexus db` reaches one. */
|
|
29
|
+
export interface DoctorDatabase {
|
|
30
|
+
/** A Postgres client (`query`) or a SQLite handle or client. */
|
|
31
|
+
client: unknown;
|
|
32
|
+
/** Which kind it is. Detected from the client when omitted. */
|
|
33
|
+
dialect?: 'postgres' | 'sqlite';
|
|
34
|
+
/** The migrations it should have. Defaults to every bundled adapter's on its default tables. */
|
|
35
|
+
migrations?: readonly SchemaMigration[];
|
|
36
|
+
/** The table that records applied migrations. */
|
|
37
|
+
table?: string;
|
|
38
|
+
}
|
|
39
|
+
/** What the doctor knows about how the application is deployed. */
|
|
40
|
+
export interface DoctorDeployment {
|
|
41
|
+
/** How many replicas run it. Settings that keep state in one process are checked only above 1. */
|
|
42
|
+
replicas: number;
|
|
43
|
+
/**
|
|
44
|
+
* The stores and shared state it uses, by any name: a checkpointer, the operation store, the
|
|
45
|
+
* server's state, tenant usage, a rate-limit store, a circuit store, a cache. Each is reported when
|
|
46
|
+
* it keeps its state in process memory, where each replica would see only its own.
|
|
47
|
+
*/
|
|
48
|
+
stores?: Record<string, unknown>;
|
|
49
|
+
}
|
|
50
|
+
/** What `diagnose()` checks. Everything is optional; a check without its input is skipped. */
|
|
51
|
+
export interface DoctorOptions {
|
|
52
|
+
/** The Node.js version to check. Defaults to the running one. */
|
|
53
|
+
nodeVersion?: string;
|
|
54
|
+
/** Where credentials are read from. Defaults to `process.env`. Only names are reported. */
|
|
55
|
+
env?: Record<string, string | undefined>;
|
|
56
|
+
/** Whether a package is installed. Defaults to resolving it from the working directory. */
|
|
57
|
+
resolvePackage?: (name: string) => boolean;
|
|
58
|
+
/** A database: connectivity, and migrations pending or changed. */
|
|
59
|
+
database?: DoctorDatabase;
|
|
60
|
+
/** A Redis client to ping. */
|
|
61
|
+
redis?: {
|
|
62
|
+
ping(): unknown;
|
|
63
|
+
};
|
|
64
|
+
/** An operation store, read for leases that lapsed and a queue nobody drains. */
|
|
65
|
+
operations?: {
|
|
66
|
+
stats?(now: string): unknown;
|
|
67
|
+
list?(): unknown;
|
|
68
|
+
};
|
|
69
|
+
/** How long the oldest queued operation may wait before the queue counts as stalled. Defaults to 15 minutes. */
|
|
70
|
+
maxQueueAgeMs?: number;
|
|
71
|
+
/** A client, read for provider health. */
|
|
72
|
+
ai?: {
|
|
73
|
+
getProviderHealth(): ReadonlyArray<{
|
|
74
|
+
providerName: string;
|
|
75
|
+
healthy: boolean;
|
|
76
|
+
stale?: boolean;
|
|
77
|
+
}>;
|
|
78
|
+
};
|
|
79
|
+
/** How old the bundled model registry may be, in days. Defaults to the registry's own window. */
|
|
80
|
+
registryMaxAgeDays?: number;
|
|
81
|
+
/** How the application is deployed. */
|
|
82
|
+
deployment?: DoctorDeployment;
|
|
83
|
+
/** Replaces the clock, for tests. */
|
|
84
|
+
now?: () => Date;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Runs every check its options allow and reports each, never throwing: a check that cannot run is a
|
|
88
|
+
* failure with the reason.
|
|
89
|
+
*/
|
|
90
|
+
export declare function diagnose(options?: DoctorOptions): Promise<DoctorReport>;
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
import { createRequire } from 'node:module';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
const PEERS = {
|
|
4
|
+
openai: 'the OpenAI SDK, used only where an application passes one in',
|
|
5
|
+
'@anthropic-ai/sdk': 'the Anthropic SDK, likewise',
|
|
6
|
+
ollama: 'the Ollama client',
|
|
7
|
+
zod: 'zod schemas for tools and structured output',
|
|
8
|
+
ajv: 'JSON Schema validation of structured output',
|
|
9
|
+
'ajv-formats': 'formats for ajv',
|
|
10
|
+
};
|
|
11
|
+
const CREDENTIALS = {
|
|
12
|
+
openai: ['OPENAI_API_KEY'],
|
|
13
|
+
anthropic: ['ANTHROPIC_API_KEY'],
|
|
14
|
+
google: ['GOOGLE_API_KEY', 'GEMINI_API_KEY'],
|
|
15
|
+
'azure-openai': ['AZURE_OPENAI_API_KEY'],
|
|
16
|
+
groq: ['GROQ_API_KEY'],
|
|
17
|
+
mistral: ['MISTRAL_API_KEY'],
|
|
18
|
+
cohere: ['COHERE_API_KEY', 'CO_API_KEY'],
|
|
19
|
+
deepseek: ['DEEPSEEK_API_KEY'],
|
|
20
|
+
openrouter: ['OPENROUTER_API_KEY'],
|
|
21
|
+
};
|
|
22
|
+
const PROCESS_LOCAL = {
|
|
23
|
+
MemoryGraphCheckpointer: { status: 'fail', why: 'a thread started on one replica cannot be resumed on another' },
|
|
24
|
+
MemoryOperationStore: { status: 'fail', why: 'operations and server runs are invisible to other replicas' },
|
|
25
|
+
MemoryServerStore: { status: 'fail', why: 'threads and runs exist only on the replica that created them' },
|
|
26
|
+
MemoryRunEventLog: { status: 'fail', why: 'a client reconnecting to another replica cannot follow its run' },
|
|
27
|
+
MemoryTenantUsage: { status: 'fail', why: 'each replica counts its own tenant limits, multiplying them' },
|
|
28
|
+
MemoryRateLimitStore: { status: 'warn', why: 'each replica enforces its own limit, multiplying it' },
|
|
29
|
+
MemoryCircuitStateStore: { status: 'warn', why: 'a circuit opened on one replica stays closed on the others' },
|
|
30
|
+
MemoryStore: { status: 'warn', why: 'long-term memory written on one replica is missing on the others' },
|
|
31
|
+
MemoryTraceStore: { status: 'warn', why: 'each replica keeps only its own traces' },
|
|
32
|
+
MemoryPromptStore: { status: 'warn', why: 'a prompt promoted on one replica is not served by the others' },
|
|
33
|
+
MemoryCacheAdapter: { status: 'warn', why: 'each replica caches separately' },
|
|
34
|
+
MemoryRollupStore: { status: 'warn', why: 'each replica sums only its own runs' },
|
|
35
|
+
};
|
|
36
|
+
export async function diagnose(options = {}) {
|
|
37
|
+
const now = options.now ?? (() => new Date());
|
|
38
|
+
const checks = [];
|
|
39
|
+
const run = async (id, title, check) => {
|
|
40
|
+
try {
|
|
41
|
+
checks.push({ id, title, ...(await check()) });
|
|
42
|
+
}
|
|
43
|
+
catch (error) {
|
|
44
|
+
checks.push({ id, title, status: 'fail', detail: error instanceof Error ? error.message : String(error) });
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
await run('node', 'Node.js version', () => nodeCheck(options.nodeVersion ?? process.versions.node));
|
|
48
|
+
await run('peers', 'Optional packages', () => peersCheck(options.resolvePackage ?? defaultResolver()));
|
|
49
|
+
await run('credentials', 'Provider credentials', () => credentialsCheck(options.env ?? process.env));
|
|
50
|
+
await run('registry', 'Model registry age', () => registryCheck(now(), options.registryMaxAgeDays));
|
|
51
|
+
await run('database', 'Database connectivity', () => databaseCheck(options.database));
|
|
52
|
+
await run('migrations', 'Schema migrations', () => migrationsCheck(options.database));
|
|
53
|
+
await run('redis', 'Redis', () => redisCheck(options.redis));
|
|
54
|
+
await run('queue', 'Operation queue and leases', () => queueCheck(options.operations, now(), options.maxQueueAgeMs));
|
|
55
|
+
await run('health', 'Provider health', () => healthCheck(options.ai));
|
|
56
|
+
await run('topology', 'Settings that only work in one process', () => topologyCheck(options.deployment));
|
|
57
|
+
const failures = checks.filter((check) => check.status === 'fail').length;
|
|
58
|
+
const warnings = checks.filter((check) => check.status === 'warn').length;
|
|
59
|
+
return { checks, ok: failures === 0, failures, warnings };
|
|
60
|
+
}
|
|
61
|
+
function nodeCheck(version) {
|
|
62
|
+
const major = Number(version.replace(/^v/, '').split('.')[0]);
|
|
63
|
+
return major >= 22
|
|
64
|
+
? { status: 'ok', detail: `Node.js ${version}` }
|
|
65
|
+
: { status: 'fail', detail: `Node.js ${version}; this package needs 22 or newer`, hint: 'Upgrade Node.js.' };
|
|
66
|
+
}
|
|
67
|
+
function defaultResolver() {
|
|
68
|
+
const resolve = createRequire(path.join(process.cwd(), 'package.json')).resolve;
|
|
69
|
+
return (name) => {
|
|
70
|
+
try {
|
|
71
|
+
resolve(name);
|
|
72
|
+
return true;
|
|
73
|
+
}
|
|
74
|
+
catch {
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function peersCheck(resolve) {
|
|
80
|
+
const installed = Object.keys(PEERS).filter((name) => resolve(name));
|
|
81
|
+
const missing = Object.keys(PEERS).filter((name) => !installed.includes(name));
|
|
82
|
+
return {
|
|
83
|
+
status: 'ok',
|
|
84
|
+
detail: `Installed: ${installed.length ? installed.join(', ') : 'none'}. Not installed: ${missing.length ? missing.join(', ') : 'none'}. Every one is optional; install only what you use.`,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
function credentialsCheck(env) {
|
|
88
|
+
const found = Object.entries(CREDENTIALS)
|
|
89
|
+
.filter(([, names]) => names.some((name) => Boolean(env[name]?.trim())))
|
|
90
|
+
.map(([provider]) => provider);
|
|
91
|
+
return found.length
|
|
92
|
+
? { status: 'ok', detail: `Credentials found for ${found.join(', ')}.` }
|
|
93
|
+
: {
|
|
94
|
+
status: 'warn',
|
|
95
|
+
detail: 'No provider credential found in the environment.',
|
|
96
|
+
hint: 'Set the API key of each hosted provider you use, such as OPENAI_API_KEY. Local providers need none.',
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
async function registryCheck(now, maxAgeDays) {
|
|
100
|
+
const { checkRegistryFreshness } = await import('../models/registry.js');
|
|
101
|
+
const freshness = checkRegistryFreshness({ now, ...(maxAgeDays === undefined ? {} : { maxAgeDays }) });
|
|
102
|
+
return freshness.stale
|
|
103
|
+
? {
|
|
104
|
+
status: 'warn',
|
|
105
|
+
detail: `The bundled registry was verified ${freshness.ageDays} days ago (${freshness.verifiedAt}), past its ${freshness.maxAgeDays}-day window.`,
|
|
106
|
+
hint: 'Upgrade nexus-ai-pro, or override stale entries with models.registry.',
|
|
107
|
+
}
|
|
108
|
+
: { status: 'ok', detail: `Verified ${freshness.ageDays} days ago (${freshness.verifiedAt}).` };
|
|
109
|
+
}
|
|
110
|
+
function dialectOf(database) {
|
|
111
|
+
if (database.dialect)
|
|
112
|
+
return database.dialect;
|
|
113
|
+
const client = database.client;
|
|
114
|
+
if (client && typeof client.query === 'function')
|
|
115
|
+
return 'postgres';
|
|
116
|
+
return 'sqlite';
|
|
117
|
+
}
|
|
118
|
+
async function databaseCheck(database) {
|
|
119
|
+
if (!database)
|
|
120
|
+
return { status: 'skip', detail: 'No database given.' };
|
|
121
|
+
const dialect = dialectOf(database);
|
|
122
|
+
if (dialect === 'postgres') {
|
|
123
|
+
await database.client.query('SELECT 1');
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
const { toSqliteClient } = await import('../sqlite/client.js');
|
|
127
|
+
await toSqliteClient(database.client).all('SELECT 1', []);
|
|
128
|
+
}
|
|
129
|
+
return { status: 'ok', detail: `Reached the ${dialect === 'postgres' ? 'Postgres' : 'SQLite'} database.` };
|
|
130
|
+
}
|
|
131
|
+
async function migrationsCheck(database) {
|
|
132
|
+
if (!database)
|
|
133
|
+
return { status: 'skip', detail: 'No database given.' };
|
|
134
|
+
const dialect = dialectOf(database);
|
|
135
|
+
const options = database.table ? { table: database.table } : {};
|
|
136
|
+
let status;
|
|
137
|
+
if (dialect === 'postgres') {
|
|
138
|
+
const [{ postgresMigrations }, { postgresMigrationStatus }] = await Promise.all([
|
|
139
|
+
import('../postgres/index.js'),
|
|
140
|
+
import('../postgres/migrations.js'),
|
|
141
|
+
]);
|
|
142
|
+
status = await postgresMigrationStatus(database.client, database.migrations ?? postgresMigrations(), options);
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
const [{ sqliteMigrations }, { sqliteMigrationStatus }] = await Promise.all([
|
|
146
|
+
import('../sqlite/index.js'),
|
|
147
|
+
import('../sqlite/migrations.js'),
|
|
148
|
+
]);
|
|
149
|
+
status = await sqliteMigrationStatus(database.client, database.migrations ?? sqliteMigrations(), options);
|
|
150
|
+
}
|
|
151
|
+
if (status.changed.length) {
|
|
152
|
+
return {
|
|
153
|
+
status: 'fail',
|
|
154
|
+
detail: `${status.changed.length} migration${status.changed.length === 1 ? '' : 's'} changed after running: ${status.changed.map(({ migration }) => `${migration.component} v${migration.version}`).join(', ')}.`,
|
|
155
|
+
hint: 'A migration never changes once applied. Check the adapter options match those it ran with.',
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
if (status.pending.length) {
|
|
159
|
+
return {
|
|
160
|
+
status: 'fail',
|
|
161
|
+
detail: `${status.pending.length} pending: ${status.pending.map((migration) => `${migration.component} v${migration.version}`).join(', ')}.`,
|
|
162
|
+
hint: 'Run: nexus db migrate --client db.mjs',
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
const newer = status.unknown.length ? ` ${status.unknown.length} recorded by a newer release.` : '';
|
|
166
|
+
return { status: 'ok', detail: `${status.applied.length} applied, none pending.${newer}` };
|
|
167
|
+
}
|
|
168
|
+
async function redisCheck(redis) {
|
|
169
|
+
if (!redis)
|
|
170
|
+
return { status: 'skip', detail: 'No Redis client given.' };
|
|
171
|
+
const reply = await redis.ping();
|
|
172
|
+
return { status: 'ok', detail: `PING answered ${String(reply)}.` };
|
|
173
|
+
}
|
|
174
|
+
async function queueCheck(operations, now, maxQueueAgeMs = 15 * 60_000) {
|
|
175
|
+
if (!operations)
|
|
176
|
+
return { status: 'skip', detail: 'No operation store given.' };
|
|
177
|
+
const { operationStats } = await import('../operations/stats.js');
|
|
178
|
+
const stats = await operationStats(operations, { now });
|
|
179
|
+
const problems = [];
|
|
180
|
+
if (stats.lapsedLeases > 0) {
|
|
181
|
+
problems.push(`${stats.lapsedLeases} running operation${stats.lapsedLeases === 1 ? '' : 's'} hold a lease that lapsed`);
|
|
182
|
+
}
|
|
183
|
+
const age = stats.oldestQueuedAt ? now.getTime() - Date.parse(stats.oldestQueuedAt) : 0;
|
|
184
|
+
if (age > maxQueueAgeMs)
|
|
185
|
+
problems.push(`the oldest queued operation has waited ${Math.round(age / 60_000)} minutes`);
|
|
186
|
+
const counts = Object.entries(stats.byStatus)
|
|
187
|
+
.map(([status, count]) => `${count} ${status}`)
|
|
188
|
+
.join(', ');
|
|
189
|
+
return problems.length
|
|
190
|
+
? {
|
|
191
|
+
status: 'warn',
|
|
192
|
+
detail: `${problems.join('; ')}. (${counts || 'nothing unfinished'})`,
|
|
193
|
+
hint: 'Run recovery — OperationRunner.recover(), or the agent server with recoverEveryMs — and a worker that claims the queue.',
|
|
194
|
+
}
|
|
195
|
+
: { status: 'ok', detail: counts ? `Unfinished: ${counts}. No lapsed lease.` : 'Nothing unfinished.' };
|
|
196
|
+
}
|
|
197
|
+
function healthCheck(ai) {
|
|
198
|
+
if (!ai)
|
|
199
|
+
return { status: 'skip', detail: 'No client given.' };
|
|
200
|
+
const snapshots = ai.getProviderHealth();
|
|
201
|
+
if (snapshots.length === 0)
|
|
202
|
+
return { status: 'skip', detail: 'Health tracking is off, or no provider has been called.' };
|
|
203
|
+
const stale = snapshots.filter((item) => item.stale).map((item) => item.providerName);
|
|
204
|
+
const unhealthy = snapshots.filter((item) => !item.healthy && !item.stale).map((item) => item.providerName);
|
|
205
|
+
const issues = [
|
|
206
|
+
...(stale.length ? [`stale: ${stale.join(', ')}`] : []),
|
|
207
|
+
...(unhealthy.length ? [`unhealthy: ${unhealthy.join(', ')}`] : []),
|
|
208
|
+
];
|
|
209
|
+
return issues.length
|
|
210
|
+
? {
|
|
211
|
+
status: 'warn',
|
|
212
|
+
detail: `${issues.join('; ')}.`,
|
|
213
|
+
hint: 'Refresh stale entries with ai.checkProviders({ staleOnly: true }).',
|
|
214
|
+
}
|
|
215
|
+
: { status: 'ok', detail: `${snapshots.length} provider${snapshots.length === 1 ? '' : 's'} healthy.` };
|
|
216
|
+
}
|
|
217
|
+
function topologyCheck(deployment) {
|
|
218
|
+
if (!deployment)
|
|
219
|
+
return { status: 'skip', detail: 'No deployment described.' };
|
|
220
|
+
if (deployment.replicas <= 1)
|
|
221
|
+
return { status: 'ok', detail: 'One replica: in-process state is shared by everything.' };
|
|
222
|
+
const findings = [];
|
|
223
|
+
for (const [name, value] of Object.entries(deployment.stores ?? {})) {
|
|
224
|
+
const kind = value?.constructor?.name;
|
|
225
|
+
const known = kind ? PROCESS_LOCAL[kind] : undefined;
|
|
226
|
+
if (known)
|
|
227
|
+
findings.push({ status: known.status, text: `${name} is a ${kind}: ${known.why}` });
|
|
228
|
+
}
|
|
229
|
+
if (findings.length === 0) {
|
|
230
|
+
return { status: 'ok', detail: `${deployment.replicas} replicas, and nothing keeps its state in one process.` };
|
|
231
|
+
}
|
|
232
|
+
return {
|
|
233
|
+
status: findings.some((finding) => finding.status === 'fail') ? 'fail' : 'warn',
|
|
234
|
+
detail: `${deployment.replicas} replicas: ${findings.map((finding) => finding.text).join('; ')}.`,
|
|
235
|
+
hint: 'Use the Redis, Postgres, or SQLite adapter for each, or run one replica.',
|
|
236
|
+
};
|
|
237
|
+
}
|
package/dist/evals/judge.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { EvaluationContext, Evaluator } from '../types/evaluate.js';
|
|
1
2
|
import type { CompletionRequest } from '../types/messages.js';
|
|
2
3
|
import type { NexusResponse } from '../types/response.js';
|
|
3
4
|
import type { EvalCase, EvalJudge } from './runner.js';
|
|
@@ -75,6 +76,16 @@ export declare class LLMJudge<Response = NexusResponse> {
|
|
|
75
76
|
constructor(options: LLMJudgeOptions<Response>);
|
|
76
77
|
/** Judges one answer. */
|
|
77
78
|
evaluate(input: LLMJudgeInput): Promise<LLMJudgeResult>;
|
|
79
|
+
/**
|
|
80
|
+
* The judge as an evaluator for `evaluate()`, scoring each output under `key` (`judge` by
|
|
81
|
+
* default). `input` turns an evaluation context into what the judge sees; by default the output is
|
|
82
|
+
* the answer, the example's expected output the reference, and its inputs the question. The
|
|
83
|
+
* experiment records the judge's model, temperature, rubric, and a hash of its instructions.
|
|
84
|
+
*/
|
|
85
|
+
asEvaluator(options?: {
|
|
86
|
+
key?: string;
|
|
87
|
+
input?: (context: EvaluationContext) => LLMJudgeInput;
|
|
88
|
+
}): Evaluator;
|
|
78
89
|
/**
|
|
79
90
|
* Adapts the judge to an `EvalRunner` case, optionally with a mapper that picks what the judge
|
|
80
91
|
* sees.
|
package/dist/evals/judge.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import { withProvenance } from '../evaluate/provenance.js';
|
|
1
3
|
const DEFAULT_SYSTEM_PROMPT = [
|
|
2
4
|
'You are a careful evaluation judge.',
|
|
3
5
|
'Return only JSON with these fields: score, passed, rationale, labels.',
|
|
@@ -43,6 +45,34 @@ export class LLMJudge {
|
|
|
43
45
|
modelUsed: modelOf(response),
|
|
44
46
|
});
|
|
45
47
|
}
|
|
48
|
+
asEvaluator(options = {}) {
|
|
49
|
+
const evaluator = async (context) => {
|
|
50
|
+
const input = options.input?.(context) ?? {
|
|
51
|
+
actual: text(context.output),
|
|
52
|
+
...(context.example.expected === undefined ? {} : { expected: text(context.example.expected) }),
|
|
53
|
+
query: text(context.example.inputs),
|
|
54
|
+
};
|
|
55
|
+
const verdict = await this.evaluate(input);
|
|
56
|
+
return {
|
|
57
|
+
key: options.key ?? 'judge',
|
|
58
|
+
score: verdict.score,
|
|
59
|
+
passed: verdict.passed,
|
|
60
|
+
...(verdict.rationale ? { comment: verdict.rationale } : {}),
|
|
61
|
+
};
|
|
62
|
+
};
|
|
63
|
+
return withProvenance(evaluator, {
|
|
64
|
+
name: options.key ?? 'judge',
|
|
65
|
+
judge: {
|
|
66
|
+
model: this.options.model,
|
|
67
|
+
promptVersion: createHash('sha256')
|
|
68
|
+
.update(this.options.systemPrompt ?? 'default')
|
|
69
|
+
.digest('hex')
|
|
70
|
+
.slice(0, 12),
|
|
71
|
+
temperature: this.options.temperature ?? 0,
|
|
72
|
+
},
|
|
73
|
+
...(this.options.rubric ? { rubric: this.options.rubric } : {}),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
46
76
|
asEvalJudge(mapper) {
|
|
47
77
|
return async (response, testCase) => {
|
|
48
78
|
const input = mapper
|
|
@@ -150,3 +180,6 @@ function clamp(value, [min, max]) {
|
|
|
150
180
|
function midpoint([min, max]) {
|
|
151
181
|
return min + (max - min) / 2;
|
|
152
182
|
}
|
|
183
|
+
function text(value) {
|
|
184
|
+
return typeof value === 'string' ? value : JSON.stringify(value ?? '');
|
|
185
|
+
}
|
package/dist/evaluate/index.d.ts
CHANGED
|
@@ -5,3 +5,4 @@ export { AnnotationQueue, type AnnotationQueueOptions, evaluateOnline, type Onli
|
|
|
5
5
|
export { evaluate, type EvaluateOptions, type EvaluationTarget, stats, summarize } from './run.js';
|
|
6
6
|
export { type CachedOutput, type EvaluationCache, evaluationCacheKey, FileEvaluationCache, fingerprintOf, MemoryEvaluationCache, type MemoryEvaluationCacheOptions, } from './cache.js';
|
|
7
7
|
export type { Dataset, DatasetExample, DatasetStore, EvaluationContext, EvaluationScore, Evaluator, ExampleResult, Experiment, ExperimentStore, MetricSummary, SummaryEvaluator, } from '../types/evaluate.js';
|
|
8
|
+
export { withProvenance } from './provenance.js';
|
package/dist/evaluate/index.js
CHANGED
|
@@ -4,3 +4,4 @@ export { completed, contains, embeddingSimilarity, exactMatch, mustNotMatch, pai
|
|
|
4
4
|
export { AnnotationQueue, evaluateOnline, } from './review.js';
|
|
5
5
|
export { evaluate, stats, summarize } from './run.js';
|
|
6
6
|
export { evaluationCacheKey, FileEvaluationCache, fingerprintOf, MemoryEvaluationCache, } from './cache.js';
|
|
7
|
+
export { withProvenance } from './provenance.js';
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { Evaluator, EvaluatorProvenance } from '../types/evaluate.js';
|
|
2
|
+
/**
|
|
3
|
+
* Says where an evaluator's scores come from: its name and version, the judge model and prompt
|
|
4
|
+
* version behind it, and its rubric. `evaluate()` records it on the experiment, so a score of 0.87
|
|
5
|
+
* can be traced to exactly what produced it, and two experiments scored by different judges are not
|
|
6
|
+
* compared as if they were alike.
|
|
7
|
+
*
|
|
8
|
+
* Returns the same evaluator, marked; it runs exactly as before.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```ts
|
|
12
|
+
* const grounded = withProvenance(groundedness, {
|
|
13
|
+
* name: 'groundedness',
|
|
14
|
+
* version: '3',
|
|
15
|
+
* judge: { model: 'claude-sonnet-5-5', promptVersion: 'grounded-v3', temperature: 0 },
|
|
16
|
+
* rubric: 'Every claim is supported by a cited passage.',
|
|
17
|
+
* });
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
export declare function withProvenance<I = unknown, O = unknown>(evaluator: Evaluator<I, O>, provenance: Omit<EvaluatorProvenance, 'keys' | 'position'>): Evaluator<I, O>;
|
|
21
|
+
/** What an evaluator declared through `withProvenance()`, if anything. */
|
|
22
|
+
export declare function declaredProvenance(evaluator: unknown): Omit<EvaluatorProvenance, 'keys' | 'position'> | undefined;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const PROVENANCE = Symbol('nexus.evaluate.provenance');
|
|
2
|
+
export function withProvenance(evaluator, provenance) {
|
|
3
|
+
Object.defineProperty(evaluator, PROVENANCE, { value: provenance, configurable: true });
|
|
4
|
+
return evaluator;
|
|
5
|
+
}
|
|
6
|
+
export function declaredProvenance(evaluator) {
|
|
7
|
+
if (typeof evaluator !== 'function')
|
|
8
|
+
return undefined;
|
|
9
|
+
return evaluator[PROVENANCE];
|
|
10
|
+
}
|