@pikku/core 0.12.80 → 0.12.83
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/CHANGELOG.md +345 -0
- package/dist/errors/index.d.ts +1 -1
- package/dist/errors/index.js +1 -1
- package/dist/function/function-runner.js +2 -5
- package/dist/function/index.d.ts +1 -1
- package/dist/index.d.ts +11 -11
- package/dist/index.js +3 -3
- package/dist/pikku-state.js +4 -0
- package/dist/services/ai-agent-runner-service.d.ts +7 -0
- package/dist/services/ai-run-state-service.d.ts +10 -0
- package/dist/services/in-memory-ai-run-state-service.d.ts +5 -1
- package/dist/services/in-memory-ai-run-state-service.js +9 -0
- package/dist/services/index.d.ts +15 -16
- package/dist/services/index.js +5 -5
- package/dist/services/meta-service.d.ts +2 -1
- package/dist/services/scoped-credential-service.d.ts +21 -0
- package/dist/services/scoped-credential-service.js +53 -0
- package/dist/testing/service-tests/ai-storage-service-tests.js +76 -0
- package/dist/types/core.types.d.ts +2 -3
- package/dist/types/state.types.d.ts +19 -1
- package/dist/wirings/actor-flow/index.d.ts +1 -1
- package/dist/wirings/ai-agent/ai-agent-finalize.d.ts +58 -0
- package/dist/wirings/ai-agent/ai-agent-finalize.js +138 -0
- package/dist/wirings/ai-agent/ai-agent-interrupt.js +1 -0
- package/dist/wirings/ai-agent/ai-agent-memory.d.ts +2 -8
- package/dist/wirings/ai-agent/ai-agent-memory.js +34 -17
- package/dist/wirings/ai-agent/ai-agent-model-config.d.ts +7 -0
- package/dist/wirings/ai-agent/ai-agent-model-config.js +44 -1
- package/dist/wirings/ai-agent/ai-agent-prepare.js +4 -0
- package/dist/wirings/ai-agent/ai-agent-runner.js +61 -40
- package/dist/wirings/ai-agent/ai-agent-stream.js +89 -36
- package/dist/wirings/ai-agent/ai-agent-turn.d.ts +1 -0
- package/dist/wirings/ai-agent/ai-agent-turn.js +1 -0
- package/dist/wirings/ai-agent/ai-agent.types.d.ts +46 -1
- package/dist/wirings/ai-agent/index.d.ts +8 -7
- package/dist/wirings/ai-agent/index.js +5 -4
- package/dist/wirings/ai-scorer/ai-scorer-grade.d.ts +26 -0
- package/dist/wirings/ai-scorer/ai-scorer-grade.js +33 -0
- package/dist/wirings/ai-scorer/ai-scorer-judge.d.ts +17 -0
- package/dist/wirings/ai-scorer/ai-scorer-judge.js +92 -0
- package/dist/wirings/ai-scorer/ai-scorer-live.d.ts +15 -0
- package/dist/wirings/ai-scorer/ai-scorer-live.js +38 -0
- package/dist/wirings/ai-scorer/ai-scorer-registry.d.ts +18 -0
- package/dist/wirings/ai-scorer/ai-scorer-registry.js +46 -0
- package/dist/wirings/ai-scorer/ai-scorer-sampling.d.ts +8 -0
- package/dist/wirings/ai-scorer/ai-scorer-sampling.js +31 -0
- package/dist/wirings/ai-scorer/ai-scorer-snapshots.d.ts +10 -0
- package/dist/wirings/ai-scorer/ai-scorer-snapshots.js +40 -0
- package/dist/wirings/ai-scorer/ai-scorer-worker.d.ts +15 -0
- package/dist/wirings/ai-scorer/ai-scorer-worker.js +58 -0
- package/dist/wirings/ai-scorer/ai-scorer.d.ts +39 -0
- package/dist/wirings/ai-scorer/ai-scorer.js +40 -0
- package/dist/wirings/ai-scorer/ai-scorer.types.d.ts +90 -0
- package/dist/wirings/ai-scorer/ai-scorer.types.js +4 -0
- package/dist/wirings/ai-scorer/index.d.ts +6 -0
- package/dist/wirings/ai-scorer/index.js +5 -0
- package/dist/wirings/channel/index.d.ts +5 -6
- package/dist/wirings/channel/index.js +3 -4
- package/dist/wirings/channel/local/local-channel-runner.js +8 -1
- package/dist/wirings/cli/channel/cli-raw-channel-runner.js +9 -1
- package/dist/wirings/cli/channel/index.d.ts +1 -2
- package/dist/wirings/cli/channel/index.js +0 -1
- package/dist/wirings/cli/cli-runner.js +13 -1
- package/dist/wirings/credential/index.d.ts +1 -1
- package/dist/wirings/gateway/index.d.ts +1 -1
- package/dist/wirings/http/http-runner.js +8 -2
- package/dist/wirings/http/index.d.ts +1 -2
- package/dist/wirings/mcp/index.d.ts +1 -1
- package/dist/wirings/mcp/mcp-runner.d.ts +15 -0
- package/dist/wirings/mcp/mcp-runner.js +18 -5
- package/dist/wirings/persona/index.d.ts +3 -4
- package/dist/wirings/persona/index.js +2 -3
- package/dist/wirings/queue/index.d.ts +1 -3
- package/dist/wirings/queue/index.js +1 -3
- package/dist/wirings/rpc/addon-runner.d.ts +8 -0
- package/dist/wirings/rpc/addon-runner.js +31 -3
- package/dist/wirings/rpc/rpc-runner.js +4 -0
- package/dist/wirings/rpc/rpc-types.d.ts +8 -0
- package/dist/wirings/rpc/wire-addon.d.ts +25 -0
- package/dist/wirings/rpc/wire-addon.js +8 -0
- package/dist/wirings/scheduler/index.d.ts +1 -1
- package/dist/wirings/trigger/index.d.ts +1 -1
- package/dist/wirings/virtual-user/index.d.ts +5 -6
- package/dist/wirings/virtual-user/index.js +2 -4
- package/dist/wirings/workflow/dsl/workflow-dsl.types.d.ts +85 -15
- package/dist/wirings/workflow/feature.d.ts +2 -1
- package/dist/wirings/workflow/index.d.ts +5 -16
- package/dist/wirings/workflow/index.js +1 -9
- package/dist/wirings/workflow/pikku-scenario-service.d.ts +17 -7
- package/dist/wirings/workflow/pikku-scenario-service.js +48 -13
- package/dist/wirings/workflow/pikku-workflow-service.js +17 -3
- package/dist/wirings/workflow/scenario-step.types.d.ts +8 -0
- package/dist/wirings/workflow/scenario.types.d.ts +37 -0
- package/dist/wirings/workflow/workflow-approval-audit.d.ts +16 -0
- package/dist/wirings/workflow/workflow-approval-audit.js +40 -0
- package/dist/wirings/workflow/workflow-approval-policy.d.ts +20 -0
- package/dist/wirings/workflow/workflow-approval-policy.js +48 -0
- package/dist/wirings/workflow/workflow-approval.d.ts +29 -1
- package/dist/wirings/workflow/workflow-approval.js +65 -2
- package/dist/wirings/workflow/workflow-run-ownership.d.ts +2 -1
- package/dist/wirings/workflow/workflow-run-ownership.js +2 -1
- package/dist/wirings/workflow/workflow.types.d.ts +2 -37
- package/knowledge/decisions/internals/addon-pikku-meta-ships-at-the-package-root-or-under-dist.md +32 -0
- package/knowledge/decisions/internals/an-addon-scope-root-loses-to-a-root-the-host-already-declares.md +39 -0
- package/knowledge/decisions/internals/index.md +30 -3
- package/knowledge/decisions/internals/validate-runs-checks-by-precondition.md +115 -0
- package/knowledge/decisions/security/a-function-never-receives-the-secret-service.md +37 -0
- package/knowledge/decisions/security/a-workflow-run-is-read-and-approved-by-its-owner.md +30 -14
- package/knowledge/decisions/security/an-approval-answer-outlives-the-run-it-answered.md +59 -0
- package/knowledge/decisions/security/index.md +3 -1
- package/knowledge/questions/index.md +1 -1
- package/package.json +3 -2
- package/scripts/generate-api-report.mts +143 -18
- package/src/api-report.test.ts +2 -2
- package/src/errors/index.ts +1 -1
- package/src/function/function-runner.test.ts +52 -0
- package/src/function/function-runner.ts +5 -9
- package/src/function/index.ts +0 -2
- package/src/index.ts +0 -35
- package/src/pikku-state.ts +5 -0
- package/src/public-surface.json +81 -118
- package/src/services/ai-agent-runner-service.ts +12 -1
- package/src/services/ai-run-state-service.ts +11 -0
- package/src/services/in-memory-ai-run-state-service.ts +13 -0
- package/src/services/index.ts +7 -58
- package/src/services/meta-service.ts +2 -4
- package/src/services/scoped-credential-service.test.ts +86 -0
- package/src/services/scoped-credential-service.ts +63 -0
- package/src/testing/service-tests/ai-storage-service-tests.ts +93 -0
- package/src/types/core.types.ts +4 -7
- package/src/types/state.types.ts +21 -1
- package/src/wirings/actor-flow/index.ts +0 -3
- package/src/wirings/ai-agent/ai-agent-finalize.test.ts +186 -0
- package/src/wirings/ai-agent/ai-agent-finalize.ts +197 -0
- package/src/wirings/ai-agent/ai-agent-interrupt.ts +1 -0
- package/src/wirings/ai-agent/ai-agent-memory.ts +54 -38
- package/src/wirings/ai-agent/ai-agent-model-config.test.ts +72 -3
- package/src/wirings/ai-agent/ai-agent-model-config.ts +49 -1
- package/src/wirings/ai-agent/ai-agent-prepare.ts +4 -0
- package/src/wirings/ai-agent/ai-agent-runner.ts +71 -40
- package/src/wirings/ai-agent/ai-agent-stream-output-hooks.test.ts +353 -0
- package/src/wirings/ai-agent/ai-agent-stream.ts +116 -54
- package/src/wirings/ai-agent/ai-agent-turn.test.ts +67 -0
- package/src/wirings/ai-agent/ai-agent-turn.ts +1 -0
- package/src/wirings/ai-agent/ai-agent.types.ts +64 -4
- package/src/wirings/ai-agent/index.ts +2 -16
- package/src/wirings/ai-scorer/ai-scorer-grade.test.ts +106 -0
- package/src/wirings/ai-scorer/ai-scorer-grade.ts +55 -0
- package/src/wirings/ai-scorer/ai-scorer-judge.test.ts +143 -0
- package/src/wirings/ai-scorer/ai-scorer-judge.ts +120 -0
- package/src/wirings/ai-scorer/ai-scorer-live.test.ts +174 -0
- package/src/wirings/ai-scorer/ai-scorer-live.ts +56 -0
- package/src/wirings/ai-scorer/ai-scorer-registry.ts +63 -0
- package/src/wirings/ai-scorer/ai-scorer-sampling.test.ts +34 -0
- package/src/wirings/ai-scorer/ai-scorer-sampling.ts +36 -0
- package/src/wirings/ai-scorer/ai-scorer-snapshots.test.ts +49 -0
- package/src/wirings/ai-scorer/ai-scorer-snapshots.ts +46 -0
- package/src/wirings/ai-scorer/ai-scorer-worker.test.ts +122 -0
- package/src/wirings/ai-scorer/ai-scorer-worker.ts +69 -0
- package/src/wirings/ai-scorer/ai-scorer.ts +76 -0
- package/src/wirings/ai-scorer/ai-scorer.types.ts +107 -0
- package/src/wirings/ai-scorer/index.ts +24 -0
- package/src/wirings/channel/index.ts +1 -20
- package/src/wirings/channel/local/local-channel-runner.test.ts +68 -0
- package/src/wirings/channel/local/local-channel-runner.ts +8 -1
- package/src/wirings/cli/channel/cli-raw-channel-runner.test.ts +23 -0
- package/src/wirings/cli/channel/cli-raw-channel-runner.ts +12 -1
- package/src/wirings/cli/channel/index.ts +0 -7
- package/src/wirings/cli/cli-runner.test.ts +68 -0
- package/src/wirings/cli/cli-runner.ts +18 -1
- package/src/wirings/credential/index.ts +0 -1
- package/src/wirings/gateway/index.ts +0 -3
- package/src/wirings/http/http-runner.test.ts +66 -0
- package/src/wirings/http/http-runner.ts +10 -2
- package/src/wirings/http/index.ts +1 -1
- package/src/wirings/mcp/index.ts +0 -1
- package/src/wirings/mcp/mcp-runner.test.ts +181 -0
- package/src/wirings/mcp/mcp-runner.ts +35 -5
- package/src/wirings/persona/index.ts +0 -8
- package/src/wirings/queue/index.ts +0 -14
- package/src/wirings/rpc/addon-runner.ts +62 -3
- package/src/wirings/rpc/addon-secrets.test.ts +391 -0
- package/src/wirings/rpc/rpc-runner.test.ts +2 -0
- package/src/wirings/rpc/rpc-runner.ts +4 -0
- package/src/wirings/rpc/rpc-types.ts +8 -0
- package/src/wirings/rpc/wire-addon.ts +33 -0
- package/src/wirings/scheduler/index.ts +0 -1
- package/src/wirings/trigger/index.ts +0 -1
- package/src/wirings/virtual-user/index.ts +0 -16
- package/src/wirings/workflow/dsl/workflow-dsl.types.ts +96 -16
- package/src/wirings/workflow/feature.ts +2 -5
- package/src/wirings/workflow/graph/graph-runner.test.ts +72 -0
- package/src/wirings/workflow/index.ts +2 -68
- package/src/wirings/workflow/pikku-scenario-service.ts +81 -16
- package/src/wirings/workflow/pikku-workflow-service.test.ts +13 -12
- package/src/wirings/workflow/pikku-workflow-service.ts +28 -4
- package/src/wirings/workflow/scenario-expectations.test.ts +75 -0
- package/src/wirings/workflow/scenario-hooks.test.ts +3 -2
- package/src/wirings/workflow/scenario-step.types.ts +8 -0
- package/src/wirings/workflow/scenario.types.ts +63 -0
- package/src/wirings/workflow/workflow-approval-audit.ts +47 -0
- package/src/wirings/workflow/workflow-approval-policy.test.ts +524 -0
- package/src/wirings/workflow/workflow-approval-policy.ts +68 -0
- package/src/wirings/workflow/workflow-approval.ts +113 -9
- package/src/wirings/workflow/workflow-run-authority.test.ts +12 -15
- package/src/wirings/workflow/workflow-run-ownership.ts +2 -1
- package/src/wirings/workflow/workflow.types.ts +1 -63
- package/src/wirings-stay-decoupled.test.ts +6 -2
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/internal.d.ts +0 -3
- package/dist/internal.js +0 -2
- package/dist/middleware/timeout.d.ts +0 -9
- package/dist/middleware/timeout.js +0 -15
- package/dist/pikku-response.d.ts +0 -6
- package/dist/pikku-response.js +0 -6
- package/dist/services/gopass-secrets.d.ts +0 -15
- package/dist/services/gopass-secrets.js +0 -76
- package/dist/services/http-scenario-actors.d.ts +0 -75
- package/dist/services/http-scenario-actors.js +0 -195
- package/dist/services/http-user-flow-actors.d.ts +0 -67
- package/dist/services/http-user-flow-actors.js +0 -193
- package/dist/services/scenario-actors-service.d.ts +0 -127
- package/dist/services/scenario-actors-service.js +0 -40
- package/dist/services/user-flow-actors-service.d.ts +0 -39
- package/dist/wirings/credential/wire-credential.d.ts +0 -48
- package/dist/wirings/credential/wire-credential.js +0 -47
- package/dist/wirings/oauth2/oauth2-client.d.ts +0 -47
- package/dist/wirings/oauth2/oauth2-client.js +0 -263
- package/dist/wirings/oauth2/oauth2-routes.d.ts +0 -35
- package/dist/wirings/oauth2/oauth2-routes.js +0 -146
- package/dist/wirings/scope/wire-scope.d.ts +0 -33
- package/dist/wirings/scope/wire-scope.js +0 -32
- package/dist/wirings/workflow/dsl/index.d.ts +0 -5
- package/dist/wirings/workflow/dsl/index.js +0 -4
- package/dist/wirings/workflow/graph/index.d.ts +0 -5
- package/dist/wirings/workflow/graph/index.js +0 -4
- /package/dist/{services/user-flow-actors-service.js → wirings/workflow/scenario.types.js} +0 -0
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { runJudge } from './ai-scorer-judge.js';
|
|
2
|
+
import { resolveAIScorer } from './ai-scorer-registry.js';
|
|
3
|
+
/**
|
|
4
|
+
* Grade one run with one scorer.
|
|
5
|
+
*
|
|
6
|
+
* The single path both callers take — the lane worker on live traffic and the
|
|
7
|
+
* scenario grading RPC — so a scenario's grade is the same computation the
|
|
8
|
+
* production sampler would have made, not an approximation of it.
|
|
9
|
+
*
|
|
10
|
+
* Persisting is optional because the two callers differ on it: a live grade is
|
|
11
|
+
* only useful once recorded, while a scenario asserts on the returned value and
|
|
12
|
+
* runs against servers that may have no run-state adapter at all.
|
|
13
|
+
*/
|
|
14
|
+
export const gradeRun = async (job, services, options) => {
|
|
15
|
+
const { scorerName, ...input } = job;
|
|
16
|
+
const scorer = resolveAIScorer(scorerName);
|
|
17
|
+
const result = scorer.score
|
|
18
|
+
? await scorer.score(input, services)
|
|
19
|
+
: await runJudge(scorer, input, services.aiAgentRunner);
|
|
20
|
+
if (options.persist) {
|
|
21
|
+
if (!services.aiRunState) {
|
|
22
|
+
throw new Error(`AI run state service not initialized: cannot record the '${scorerName}' grade of run ${job.runId}`);
|
|
23
|
+
}
|
|
24
|
+
await services.aiRunState.saveScore({
|
|
25
|
+
runId: job.runId,
|
|
26
|
+
scorerName,
|
|
27
|
+
score: result.score,
|
|
28
|
+
...(result.reason !== undefined ? { reason: result.reason } : {}),
|
|
29
|
+
...(result.metadata !== undefined ? { metadata: result.metadata } : {}),
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AIAgentRunnerService } from '../../services/ai-agent-runner-service.js';
|
|
2
|
+
import type { PikkuAIScorer, ScorerInput, ScorerJudgeConfig, ScorerOutput } from './ai-scorer.types.js';
|
|
3
|
+
/**
|
|
4
|
+
* The message a judge grades when its scorer supplies no `prompt`.
|
|
5
|
+
*
|
|
6
|
+
* A reference-based judge is shown the answer key; a reference-free one is not,
|
|
7
|
+
* and grades the answer on its own merits.
|
|
8
|
+
*/
|
|
9
|
+
export declare const buildJudgePrompt: (judge: ScorerJudgeConfig, input: ScorerInput) => string;
|
|
10
|
+
/**
|
|
11
|
+
* Run an LLM judge and force the `{ score, reason }` contract, so a scorer
|
|
12
|
+
* author writes a rubric rather than a parser.
|
|
13
|
+
*
|
|
14
|
+
* A judge has no tools, which is the one case where an output schema is
|
|
15
|
+
* reliably honoured.
|
|
16
|
+
*/
|
|
17
|
+
export declare const runJudge: (scorer: PikkuAIScorer<any>, input: ScorerInput, aiAgentRunner: AIAgentRunnerService | undefined) => Promise<ScorerOutput>;
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { randomUUID } from '../ai-agent/ai-agent-utils.js';
|
|
2
|
+
const JUDGE_OUTPUT_SCHEMA = {
|
|
3
|
+
type: 'object',
|
|
4
|
+
properties: {
|
|
5
|
+
score: {
|
|
6
|
+
type: 'number',
|
|
7
|
+
minimum: 0,
|
|
8
|
+
maximum: 1,
|
|
9
|
+
description: 'How well the answer meets the rubric. 1 is best.',
|
|
10
|
+
},
|
|
11
|
+
reason: {
|
|
12
|
+
type: 'string',
|
|
13
|
+
description: 'One or two sentences explaining the score.',
|
|
14
|
+
},
|
|
15
|
+
},
|
|
16
|
+
required: ['score', 'reason'],
|
|
17
|
+
additionalProperties: false,
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* The message a judge grades when its scorer supplies no `prompt`.
|
|
21
|
+
*
|
|
22
|
+
* A reference-based judge is shown the answer key; a reference-free one is not,
|
|
23
|
+
* and grades the answer on its own merits.
|
|
24
|
+
*/
|
|
25
|
+
export const buildJudgePrompt = (judge, input) => {
|
|
26
|
+
if (judge.prompt)
|
|
27
|
+
return judge.prompt(input);
|
|
28
|
+
const sections = [`User asked:\n${input.input}`];
|
|
29
|
+
if (input.reference !== undefined) {
|
|
30
|
+
sections.push(`Reference answer:\n${input.reference}`);
|
|
31
|
+
}
|
|
32
|
+
sections.push(`Assistant answered:\n${input.output}`);
|
|
33
|
+
sections.push(judge.goal);
|
|
34
|
+
sections.push('Respond with a score 0..1 and a brief reason.');
|
|
35
|
+
return sections.join('\n\n');
|
|
36
|
+
};
|
|
37
|
+
/**
|
|
38
|
+
* Clamp rather than reject: a model that answers 1.2 has still made a legible
|
|
39
|
+
* judgement, and failing the job would lose it. A non-number has not.
|
|
40
|
+
*/
|
|
41
|
+
const normalizeScore = (scorerName, value) => {
|
|
42
|
+
if (typeof value !== 'number' || Number.isNaN(value)) {
|
|
43
|
+
throw new Error(`Judge '${scorerName}' returned a non-numeric score: ${JSON.stringify(value)}`);
|
|
44
|
+
}
|
|
45
|
+
return Math.min(1, Math.max(0, value));
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Run an LLM judge and force the `{ score, reason }` contract, so a scorer
|
|
49
|
+
* author writes a rubric rather than a parser.
|
|
50
|
+
*
|
|
51
|
+
* A judge has no tools, which is the one case where an output schema is
|
|
52
|
+
* reliably honoured.
|
|
53
|
+
*/
|
|
54
|
+
export const runJudge = async (scorer, input, aiAgentRunner) => {
|
|
55
|
+
const judge = scorer.judge;
|
|
56
|
+
if (!judge) {
|
|
57
|
+
throw new Error(`Scorer '${scorer.name}' is not a judge`);
|
|
58
|
+
}
|
|
59
|
+
if (!aiAgentRunner) {
|
|
60
|
+
throw new Error(`Judge '${scorer.name}' needs an AI provider, but no aiAgentRunner is registered in this process. ` +
|
|
61
|
+
`A worker deployed apart from the API has to register one to run the slow scoring lane.`);
|
|
62
|
+
}
|
|
63
|
+
if (scorer.requiresReference && input.reference === undefined) {
|
|
64
|
+
throw new Error(`Judge '${scorer.name}' grades against a reference answer, but none was supplied`);
|
|
65
|
+
}
|
|
66
|
+
const messages = [
|
|
67
|
+
{
|
|
68
|
+
id: randomUUID(),
|
|
69
|
+
role: 'user',
|
|
70
|
+
content: buildJudgePrompt(judge, input),
|
|
71
|
+
createdAt: new Date(),
|
|
72
|
+
},
|
|
73
|
+
];
|
|
74
|
+
const result = await aiAgentRunner.run({
|
|
75
|
+
model: judge.model,
|
|
76
|
+
instructions: judge.goal,
|
|
77
|
+
messages,
|
|
78
|
+
tools: [],
|
|
79
|
+
maxSteps: 1,
|
|
80
|
+
toolChoice: 'none',
|
|
81
|
+
outputSchema: JUDGE_OUTPUT_SCHEMA,
|
|
82
|
+
});
|
|
83
|
+
const object = (result.object ?? {});
|
|
84
|
+
return {
|
|
85
|
+
score: normalizeScore(scorer.name, object.score),
|
|
86
|
+
...(typeof object.reason === 'string' ? { reason: object.reason } : {}),
|
|
87
|
+
metadata: {
|
|
88
|
+
judgeModel: judge.model,
|
|
89
|
+
judgeTokens: result.usage.inputTokens + result.usage.outputTokens,
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CoreSingletonServices } from '../../types/core.types.js';
|
|
2
|
+
import { type ScorerInput } from './ai-scorer.types.js';
|
|
3
|
+
/**
|
|
4
|
+
* Grade one finished run on live traffic.
|
|
5
|
+
*
|
|
6
|
+
* Called as the terminal step of `finalizeAgentRun`, so it is structurally last
|
|
7
|
+
* — a developer cannot register anything after it, and cannot reorder it. It is
|
|
8
|
+
* also strictly best-effort: the client already has its answer, so nothing here
|
|
9
|
+
* may fail the run.
|
|
10
|
+
*/
|
|
11
|
+
export declare const scoreFinishedRun: (run: ScorerInput, services: Pick<CoreSingletonServices, "logger"> & {
|
|
12
|
+
queueService?: {
|
|
13
|
+
add: (queueName: string, data: unknown) => Promise<any>;
|
|
14
|
+
};
|
|
15
|
+
}) => Promise<void>;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { scorersForAgent } from './ai-scorer-registry.js';
|
|
2
|
+
import { isSampled } from './ai-scorer-sampling.js';
|
|
3
|
+
import { SCORER_LANE_QUEUES } from './ai-scorer.types.js';
|
|
4
|
+
/**
|
|
5
|
+
* Grade one finished run on live traffic.
|
|
6
|
+
*
|
|
7
|
+
* Called as the terminal step of `finalizeAgentRun`, so it is structurally last
|
|
8
|
+
* — a developer cannot register anything after it, and cannot reorder it. It is
|
|
9
|
+
* also strictly best-effort: the client already has its answer, so nothing here
|
|
10
|
+
* may fail the run.
|
|
11
|
+
*/
|
|
12
|
+
export const scoreFinishedRun = async (run, services) => {
|
|
13
|
+
const scorers = scorersForAgent(run.agentName, services.logger).filter(
|
|
14
|
+
// A reference-based judge grades against an answer key, and live traffic
|
|
15
|
+
// has none.
|
|
16
|
+
(scorer) => !scorer.requiresReference);
|
|
17
|
+
if (scorers.length === 0)
|
|
18
|
+
return;
|
|
19
|
+
if (!services.queueService) {
|
|
20
|
+
services.logger?.warn(`[pikku] Agent '${run.agentName}' declares scorers but no queue service is registered — skipping live scoring`);
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
const sampled = scorers.filter((scorer) => isSampled(run.runId, scorer.name, scorer.sampleRate));
|
|
24
|
+
// One message per scorer, so each gets its own retry, isolation and lane.
|
|
25
|
+
await Promise.all(sampled.map(async (scorer) => {
|
|
26
|
+
try {
|
|
27
|
+
await services.queueService.add(SCORER_LANE_QUEUES[scorer.lane], {
|
|
28
|
+
...run,
|
|
29
|
+
scorerName: scorer.name,
|
|
30
|
+
// No reference: a live run has no answer key.
|
|
31
|
+
reference: undefined,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
services.logger?.error(`[pikku] Failed to enqueue the '${scorer.name}' grade of run ${run.runId}`, { error });
|
|
36
|
+
}
|
|
37
|
+
}));
|
|
38
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { PikkuAIScorer } from './ai-scorer.types.js';
|
|
2
|
+
export declare const addAIScorer: (scorerName: string, scorer: PikkuAIScorer<any>, packageName?: string | null) => void;
|
|
3
|
+
export declare const getAIScorers: () => Map<string, PikkuAIScorer>;
|
|
4
|
+
export declare const getAIScorersMeta: () => import("./ai-scorer.types.js").ScorerMeta;
|
|
5
|
+
/**
|
|
6
|
+
* Resolve a scorer by name across every registered package, the way an agent's
|
|
7
|
+
* tools are resolved: a scorer declared in an addon is nameable by an app agent.
|
|
8
|
+
*/
|
|
9
|
+
export declare const resolveAIScorer: (scorerName: string) => PikkuAIScorer<any>;
|
|
10
|
+
/**
|
|
11
|
+
* The scorers an agent asked to be graded by.
|
|
12
|
+
*
|
|
13
|
+
* A name that resolves to nothing is warned about rather than thrown: a missing
|
|
14
|
+
* scorer must not take down a run that has already answered the user.
|
|
15
|
+
*/
|
|
16
|
+
export declare const scorersForAgent: (agentName: string, logger?: {
|
|
17
|
+
warn: (message: string) => void;
|
|
18
|
+
}) => PikkuAIScorer<any>[];
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { pikkuState } from '../../pikku-state.js';
|
|
2
|
+
export const addAIScorer = (scorerName, scorer, packageName = null) => {
|
|
3
|
+
const scorersMeta = pikkuState(packageName, 'agent', 'scorersMeta');
|
|
4
|
+
if (!scorersMeta[scorerName]) {
|
|
5
|
+
console.warn(`[pikku] Skipping AI scorer '${scorerName}' — metadata not found. Scorers must be declared in a *.scorer.ts file.`);
|
|
6
|
+
return;
|
|
7
|
+
}
|
|
8
|
+
const scorers = pikkuState(packageName, 'agent', 'scorers');
|
|
9
|
+
if (scorers.has(scorerName)) {
|
|
10
|
+
throw new Error(`AI scorer already exists: ${scorerName}`);
|
|
11
|
+
}
|
|
12
|
+
scorers.set(scorerName, scorer);
|
|
13
|
+
};
|
|
14
|
+
export const getAIScorers = () => pikkuState(null, 'agent', 'scorers');
|
|
15
|
+
export const getAIScorersMeta = () => pikkuState(null, 'agent', 'scorersMeta');
|
|
16
|
+
/**
|
|
17
|
+
* Resolve a scorer by name across every registered package, the way an agent's
|
|
18
|
+
* tools are resolved: a scorer declared in an addon is nameable by an app agent.
|
|
19
|
+
*/
|
|
20
|
+
export const resolveAIScorer = (scorerName) => {
|
|
21
|
+
const scorer = pikkuState(null, 'agent', 'scorers').get(scorerName);
|
|
22
|
+
if (!scorer) {
|
|
23
|
+
throw new Error(`AI scorer not found: ${scorerName}`);
|
|
24
|
+
}
|
|
25
|
+
return scorer;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The scorers an agent asked to be graded by.
|
|
29
|
+
*
|
|
30
|
+
* A name that resolves to nothing is warned about rather than thrown: a missing
|
|
31
|
+
* scorer must not take down a run that has already answered the user.
|
|
32
|
+
*/
|
|
33
|
+
export const scorersForAgent = (agentName, logger) => {
|
|
34
|
+
const agent = pikkuState(null, 'agent', 'agents').get(agentName);
|
|
35
|
+
const names = agent?.scorers ?? [];
|
|
36
|
+
const scorers = [];
|
|
37
|
+
for (const name of names) {
|
|
38
|
+
const scorer = pikkuState(null, 'agent', 'scorers').get(name);
|
|
39
|
+
if (!scorer) {
|
|
40
|
+
logger?.warn(`[pikku] Agent '${agentName}' names scorer '${name}', which is not registered — skipping it`);
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
scorers.push(scorer);
|
|
44
|
+
}
|
|
45
|
+
return scorers;
|
|
46
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decide whether one run is in a scorer's sample.
|
|
3
|
+
*
|
|
4
|
+
* Deterministic on `(runId, scorerName)` rather than random, so re-running the
|
|
5
|
+
* decision — a retried job, a replayed run, a test — always lands the same way,
|
|
6
|
+
* and so two scorers at the same rate do not sample the same runs.
|
|
7
|
+
*/
|
|
8
|
+
export declare const isSampled: (runId: string, scorerName: string, sampleRate: number) => boolean;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decide whether one run is in a scorer's sample.
|
|
3
|
+
*
|
|
4
|
+
* Deterministic on `(runId, scorerName)` rather than random, so re-running the
|
|
5
|
+
* decision — a retried job, a replayed run, a test — always lands the same way,
|
|
6
|
+
* and so two scorers at the same rate do not sample the same runs.
|
|
7
|
+
*/
|
|
8
|
+
export const isSampled = (runId, scorerName, sampleRate) => {
|
|
9
|
+
if (sampleRate >= 1)
|
|
10
|
+
return true;
|
|
11
|
+
if (sampleRate <= 0)
|
|
12
|
+
return false;
|
|
13
|
+
const key = `${scorerName}:${runId}`;
|
|
14
|
+
// FNV-1a: no crypto dependency, and stable across processes and platforms,
|
|
15
|
+
// which a language-level string hash is not guaranteed to be.
|
|
16
|
+
let hash = 0x811c9dc5;
|
|
17
|
+
for (let i = 0; i < key.length; i++) {
|
|
18
|
+
hash ^= key.charCodeAt(i);
|
|
19
|
+
hash = Math.imul(hash, 0x01000193) >>> 0;
|
|
20
|
+
}
|
|
21
|
+
// FNV-1a alone leaves its high bits barely moved between keys that differ
|
|
22
|
+
// only in their last characters — sequential run ids being exactly that —
|
|
23
|
+
// which biases the fraction actually sampled well away from the rate asked
|
|
24
|
+
// for. MurmurHash3's finalizer spreads the low bits back over the whole word.
|
|
25
|
+
hash ^= hash >>> 16;
|
|
26
|
+
hash = Math.imul(hash, 0x85ebca6b) >>> 0;
|
|
27
|
+
hash ^= hash >>> 13;
|
|
28
|
+
hash = Math.imul(hash, 0xc2b2ae35) >>> 0;
|
|
29
|
+
hash ^= hash >>> 16;
|
|
30
|
+
return (hash >>> 0) / 0x100000000 < sampleRate;
|
|
31
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ScorerInput } from './ai-scorer.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Start retaining snapshots. Called where the scenario grading RPC is
|
|
4
|
+
* registered, so the buffer exists in exactly the processes that can read it —
|
|
5
|
+
* a development server — and never in a deployed bundle.
|
|
6
|
+
*/
|
|
7
|
+
export declare const enableScoreSnapshots: (maxRuns?: number) => void;
|
|
8
|
+
export declare const recordScoreSnapshot: (run: ScorerInput) => void;
|
|
9
|
+
export declare const getScoreSnapshot: (runId: string) => ScorerInput | undefined;
|
|
10
|
+
export declare const resetScoreSnapshots: () => void;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The last few finished runs, held so a scenario can grade one.
|
|
3
|
+
*
|
|
4
|
+
* A scenario cannot rebuild a `ScorerInput` from storage: the run record holds
|
|
5
|
+
* status and usage, and the prompt, answer and tool calls are spread across the
|
|
6
|
+
* thread's messages, where the boundary of a single run is not recoverable. So
|
|
7
|
+
* the runtime keeps the snapshot it already took at finalize — which is also
|
|
8
|
+
* what makes a scenario's grade comparable to a live one, since both grade the
|
|
9
|
+
* identical object, redactions included.
|
|
10
|
+
*
|
|
11
|
+
* Off unless something turns it on, and bounded when it is. A process that
|
|
12
|
+
* grades no scenarios holds nothing, and one that does cannot accumulate run
|
|
13
|
+
* content without limit.
|
|
14
|
+
*/
|
|
15
|
+
const snapshots = new Map();
|
|
16
|
+
let limit = 0;
|
|
17
|
+
/**
|
|
18
|
+
* Start retaining snapshots. Called where the scenario grading RPC is
|
|
19
|
+
* registered, so the buffer exists in exactly the processes that can read it —
|
|
20
|
+
* a development server — and never in a deployed bundle.
|
|
21
|
+
*/
|
|
22
|
+
export const enableScoreSnapshots = (maxRuns = 50) => {
|
|
23
|
+
limit = maxRuns;
|
|
24
|
+
};
|
|
25
|
+
export const recordScoreSnapshot = (run) => {
|
|
26
|
+
if (limit === 0)
|
|
27
|
+
return;
|
|
28
|
+
snapshots.set(run.runId, run);
|
|
29
|
+
while (snapshots.size > limit) {
|
|
30
|
+
const oldest = snapshots.keys().next().value;
|
|
31
|
+
if (oldest === undefined)
|
|
32
|
+
break;
|
|
33
|
+
snapshots.delete(oldest);
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
export const getScoreSnapshot = (runId) => snapshots.get(runId);
|
|
37
|
+
export const resetScoreSnapshots = () => {
|
|
38
|
+
snapshots.clear();
|
|
39
|
+
limit = 0;
|
|
40
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { type ScoreJob } from './ai-scorer.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The one worker behind both lanes: it resolves the scorer by name and grades.
|
|
4
|
+
* The lane a job arrived on decides nothing except how long it may take.
|
|
5
|
+
*/
|
|
6
|
+
export declare function pikkuAIScoreWorkerFunc(_services: Record<string, unknown>, job: ScoreJob): Promise<void>;
|
|
7
|
+
/**
|
|
8
|
+
* Bind the two lane queues to the shared worker.
|
|
9
|
+
*
|
|
10
|
+
* Registered programmatically rather than emitted into the user's bootstrap:
|
|
11
|
+
* `wireQueueWorker` warns and returns for a name codegen produced no metadata
|
|
12
|
+
* for, so the metadata is synthesised here alongside the registration. This
|
|
13
|
+
* mirrors how workflows wire their own queues.
|
|
14
|
+
*/
|
|
15
|
+
export declare const wireAIScorerQueueWorkers: () => void;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { pikkuState } from '../../pikku-state.js';
|
|
2
|
+
import { addFunction } from '../../function/function-runner.js';
|
|
3
|
+
import { wireQueueWorker } from '../queue/queue-runner.js';
|
|
4
|
+
import { gradeRun } from './ai-scorer-grade.js';
|
|
5
|
+
import { SCORER_LANE_QUEUES } from './ai-scorer.types.js';
|
|
6
|
+
/**
|
|
7
|
+
* The one worker behind both lanes: it resolves the scorer by name and grades.
|
|
8
|
+
* The lane a job arrived on decides nothing except how long it may take.
|
|
9
|
+
*/
|
|
10
|
+
export async function pikkuAIScoreWorkerFunc(_services, job) {
|
|
11
|
+
const services = pikkuState(null, 'package', 'singletonServices');
|
|
12
|
+
if (!services) {
|
|
13
|
+
throw new Error(`Singleton services not initialized: cannot run the '${job.scorerName}' grade of run ${job.runId}`);
|
|
14
|
+
}
|
|
15
|
+
await gradeRun(job, services, { persist: true });
|
|
16
|
+
}
|
|
17
|
+
const SCORE_WORKER_FUNC_ID = 'pikkuAIScoreWorker';
|
|
18
|
+
/**
|
|
19
|
+
* Bind the two lane queues to the shared worker.
|
|
20
|
+
*
|
|
21
|
+
* Registered programmatically rather than emitted into the user's bootstrap:
|
|
22
|
+
* `wireQueueWorker` warns and returns for a name codegen produced no metadata
|
|
23
|
+
* for, so the metadata is synthesised here alongside the registration. This
|
|
24
|
+
* mirrors how workflows wire their own queues.
|
|
25
|
+
*/
|
|
26
|
+
export const wireAIScorerQueueWorkers = () => {
|
|
27
|
+
// No scorers means no lanes: a deployment that grades nothing should not be
|
|
28
|
+
// left holding two queues nothing ever writes to.
|
|
29
|
+
if (pikkuState(null, 'agent', 'scorers').size === 0)
|
|
30
|
+
return;
|
|
31
|
+
const functions = pikkuState(null, 'function', 'functions');
|
|
32
|
+
const functionsMeta = pikkuState(null, 'function', 'meta');
|
|
33
|
+
const queueMeta = pikkuState(null, 'queue', 'meta');
|
|
34
|
+
if (!functions.has(SCORE_WORKER_FUNC_ID)) {
|
|
35
|
+
addFunction(SCORE_WORKER_FUNC_ID, { func: pikkuAIScoreWorkerFunc });
|
|
36
|
+
}
|
|
37
|
+
if (!functionsMeta[SCORE_WORKER_FUNC_ID]) {
|
|
38
|
+
functionsMeta[SCORE_WORKER_FUNC_ID] = {
|
|
39
|
+
pikkuFuncId: SCORE_WORKER_FUNC_ID,
|
|
40
|
+
sessionless: true,
|
|
41
|
+
functionType: 'helper',
|
|
42
|
+
inputSchemaName: null,
|
|
43
|
+
outputSchemaName: null,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
for (const queueName of Object.values(SCORER_LANE_QUEUES)) {
|
|
47
|
+
if (!queueMeta[queueName]) {
|
|
48
|
+
queueMeta[queueName] = {
|
|
49
|
+
pikkuFuncId: SCORE_WORKER_FUNC_ID,
|
|
50
|
+
name: queueName,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
wireQueueWorker({
|
|
54
|
+
name: queueName,
|
|
55
|
+
func: { func: pikkuAIScoreWorkerFunc },
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { PikkuAIScorer, ScorerInput, ScorerOutput } from './ai-scorer.types.js';
|
|
2
|
+
/**
|
|
3
|
+
* A heuristic scorer: pure code over the finished run, no model call, so it
|
|
4
|
+
* grades on the fast lane.
|
|
5
|
+
*/
|
|
6
|
+
export declare const pikkuAIScorer: <Services = any>(config: {
|
|
7
|
+
name: string;
|
|
8
|
+
description: string;
|
|
9
|
+
/** 0..1 fraction of live runs to grade. Defaults to all of them. */
|
|
10
|
+
sampleRate?: number;
|
|
11
|
+
/**
|
|
12
|
+
* Grades against a known-correct answer. Such a scorer is test-only — live
|
|
13
|
+
* traffic has no answer key, so the runtime never samples it.
|
|
14
|
+
*/
|
|
15
|
+
requiresReference?: boolean;
|
|
16
|
+
score: (input: ScorerInput, services: Services) => ScorerOutput | Promise<ScorerOutput>;
|
|
17
|
+
}) => PikkuAIScorer<Services>;
|
|
18
|
+
/**
|
|
19
|
+
* An LLM-judge scorer: the runtime makes the model call and forces a structured
|
|
20
|
+
* `{ score, reason }`, so a judge is a rubric rather than a prompt to parse.
|
|
21
|
+
*
|
|
22
|
+
* The rubric field is `goal`, matching `pikkuAIAgent`'s prompt vocabulary — a
|
|
23
|
+
* judge is a degenerate agent, and should use the same word for the same thing.
|
|
24
|
+
* `prompt` is the escape hatch for non-standard framing.
|
|
25
|
+
*/
|
|
26
|
+
export declare const pikkuAIJudge: <Services = any>(config: {
|
|
27
|
+
name: string;
|
|
28
|
+
description: string;
|
|
29
|
+
/** 0..1 fraction of live runs to grade. Defaults to all of them. */
|
|
30
|
+
sampleRate?: number;
|
|
31
|
+
/**
|
|
32
|
+
* Grades against a known-correct answer. Such a judge is test-only — live
|
|
33
|
+
* traffic has no answer key, so the runtime never samples it.
|
|
34
|
+
*/
|
|
35
|
+
requiresReference?: boolean;
|
|
36
|
+
model: string;
|
|
37
|
+
goal: string;
|
|
38
|
+
prompt?: (input: ScorerInput) => string;
|
|
39
|
+
}) => PikkuAIScorer<Services>;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const assertSampleRate = (name, sampleRate) => {
|
|
2
|
+
if (sampleRate === undefined)
|
|
3
|
+
return 1;
|
|
4
|
+
if (sampleRate < 0 || sampleRate > 1) {
|
|
5
|
+
throw new Error(`Scorer '${name}' has a sampleRate of ${sampleRate} — it is a fraction of live runs to grade, so it must be between 0 and 1`);
|
|
6
|
+
}
|
|
7
|
+
return sampleRate;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* A heuristic scorer: pure code over the finished run, no model call, so it
|
|
11
|
+
* grades on the fast lane.
|
|
12
|
+
*/
|
|
13
|
+
export const pikkuAIScorer = (config) => ({
|
|
14
|
+
name: config.name,
|
|
15
|
+
description: config.description,
|
|
16
|
+
lane: 'fast',
|
|
17
|
+
sampleRate: assertSampleRate(config.name, config.sampleRate),
|
|
18
|
+
requiresReference: config.requiresReference ?? false,
|
|
19
|
+
score: config.score,
|
|
20
|
+
});
|
|
21
|
+
/**
|
|
22
|
+
* An LLM-judge scorer: the runtime makes the model call and forces a structured
|
|
23
|
+
* `{ score, reason }`, so a judge is a rubric rather than a prompt to parse.
|
|
24
|
+
*
|
|
25
|
+
* The rubric field is `goal`, matching `pikkuAIAgent`'s prompt vocabulary — a
|
|
26
|
+
* judge is a degenerate agent, and should use the same word for the same thing.
|
|
27
|
+
* `prompt` is the escape hatch for non-standard framing.
|
|
28
|
+
*/
|
|
29
|
+
export const pikkuAIJudge = (config) => ({
|
|
30
|
+
name: config.name,
|
|
31
|
+
description: config.description,
|
|
32
|
+
lane: 'slow',
|
|
33
|
+
sampleRate: assertSampleRate(config.name, config.sampleRate),
|
|
34
|
+
requiresReference: config.requiresReference ?? false,
|
|
35
|
+
judge: {
|
|
36
|
+
model: config.model,
|
|
37
|
+
goal: config.goal,
|
|
38
|
+
...(config.prompt ? { prompt: config.prompt } : {}),
|
|
39
|
+
},
|
|
40
|
+
});
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a scorer is shown about a finished run.
|
|
3
|
+
*
|
|
4
|
+
* One snapshot, taken after the output middleware has resolved, so a scorer and
|
|
5
|
+
* the persisted run record see the same thing — including the redactions.
|
|
6
|
+
*/
|
|
7
|
+
export interface ScorerInput {
|
|
8
|
+
runId: string;
|
|
9
|
+
agentName: string;
|
|
10
|
+
threadId?: string;
|
|
11
|
+
resourceId?: string;
|
|
12
|
+
/** The user prompt the run answered. */
|
|
13
|
+
input: string;
|
|
14
|
+
/** The agent's final text answer. */
|
|
15
|
+
output: string;
|
|
16
|
+
/**
|
|
17
|
+
* The known-correct answer. Supplied by a scenario for a reference-based
|
|
18
|
+
* judge; never set on live traffic, which has no answer key.
|
|
19
|
+
*/
|
|
20
|
+
reference?: string;
|
|
21
|
+
toolCalls: {
|
|
22
|
+
name: string;
|
|
23
|
+
args: unknown;
|
|
24
|
+
result?: unknown;
|
|
25
|
+
error?: string;
|
|
26
|
+
}[];
|
|
27
|
+
usage: {
|
|
28
|
+
inputTokens: number;
|
|
29
|
+
outputTokens: number;
|
|
30
|
+
model?: string;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export interface ScorerOutput {
|
|
34
|
+
/** 0..1, so grades are comparable across scorers. */
|
|
35
|
+
score: number;
|
|
36
|
+
reason?: string;
|
|
37
|
+
metadata?: Record<string, unknown>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The queue a scorer's jobs go to. Two lanes exist only so a flood of slow
|
|
41
|
+
* LLM-judge jobs cannot starve the cheap heuristic ones; within a lane it is
|
|
42
|
+
* plain FIFO.
|
|
43
|
+
*/
|
|
44
|
+
export type ScorerLane = 'fast' | 'slow';
|
|
45
|
+
export declare const SCORER_LANE_QUEUES: Record<ScorerLane, string>;
|
|
46
|
+
/**
|
|
47
|
+
* A judge's model call. Present only on a scorer built with `pikkuAIJudge`;
|
|
48
|
+
* `score` is present only on one built with `pikkuAIScorer`. Exactly one of the
|
|
49
|
+
* two is set, which is what the two constructors exist to guarantee.
|
|
50
|
+
*/
|
|
51
|
+
export type ScorerJudgeConfig = {
|
|
52
|
+
model: string;
|
|
53
|
+
goal: string;
|
|
54
|
+
prompt?: (input: ScorerInput) => string;
|
|
55
|
+
};
|
|
56
|
+
export type PikkuAIScorer<Services = any> = {
|
|
57
|
+
name: string;
|
|
58
|
+
description: string;
|
|
59
|
+
lane: ScorerLane;
|
|
60
|
+
/** 0..1 fraction of live runs to grade. */
|
|
61
|
+
sampleRate: number;
|
|
62
|
+
/**
|
|
63
|
+
* Grades against a known-correct answer, so it is test-only: live traffic has
|
|
64
|
+
* no answer key and the runtime never samples it.
|
|
65
|
+
*/
|
|
66
|
+
requiresReference: boolean;
|
|
67
|
+
score?: (input: ScorerInput, services: Services) => ScorerOutput | Promise<ScorerOutput>;
|
|
68
|
+
judge?: ScorerJudgeConfig;
|
|
69
|
+
};
|
|
70
|
+
export type ScorerMeta = Record<string, {
|
|
71
|
+
name: string;
|
|
72
|
+
description: string;
|
|
73
|
+
lane: ScorerLane;
|
|
74
|
+
sampleRate: number;
|
|
75
|
+
requiresReference: boolean;
|
|
76
|
+
sourceFile?: string;
|
|
77
|
+
exportedName?: string;
|
|
78
|
+
}>;
|
|
79
|
+
/** A single scorer's job on a lane queue. */
|
|
80
|
+
export type ScoreJob = ScorerInput & {
|
|
81
|
+
scorerName: string;
|
|
82
|
+
};
|
|
83
|
+
export type AIRunScore = {
|
|
84
|
+
runId: string;
|
|
85
|
+
scorerName: string;
|
|
86
|
+
score: number;
|
|
87
|
+
reason?: string;
|
|
88
|
+
metadata?: Record<string, unknown>;
|
|
89
|
+
createdAt: Date;
|
|
90
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export { pikkuAIScorer, pikkuAIJudge } from './ai-scorer.js';
|
|
2
|
+
export { addAIScorer, getAIScorers, getAIScorersMeta, } from './ai-scorer-registry.js';
|
|
3
|
+
export { gradeRun } from './ai-scorer-grade.js';
|
|
4
|
+
export { enableScoreSnapshots, getScoreSnapshot, } from './ai-scorer-snapshots.js';
|
|
5
|
+
export { wireAIScorerQueueWorkers, } from './ai-scorer-worker.js';
|
|
6
|
+
export type { AIRunScore, PikkuAIScorer, ScoreJob, ScorerInput, ScorerJudgeConfig, ScorerLane, ScorerMeta, ScorerOutput, } from './ai-scorer.types.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { pikkuAIScorer, pikkuAIJudge } from './ai-scorer.js';
|
|
2
|
+
export { addAIScorer, getAIScorers, getAIScorersMeta, } from './ai-scorer-registry.js';
|
|
3
|
+
export { gradeRun } from './ai-scorer-grade.js';
|
|
4
|
+
export { enableScoreSnapshots, getScoreSnapshot, } from './ai-scorer-snapshots.js';
|
|
5
|
+
export { wireAIScorerQueueWorkers, } from './ai-scorer-worker.js';
|
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
export { wireChannel
|
|
2
|
-
export { addChannelMiddleware, combineChannelMiddleware,
|
|
1
|
+
export { wireChannel } from './channel-runner.js';
|
|
2
|
+
export { addChannelMiddleware, combineChannelMiddleware, } from './channel-middleware-runner.js';
|
|
3
3
|
export { logChannels } from './log-channels.js';
|
|
4
4
|
export { PikkuAbstractChannelHandler } from './pikku-abstract-channel-handler.js';
|
|
5
5
|
export type { EventHubService } from './eventhub-service.js';
|
|
6
6
|
export { ChannelStore } from './channel-store.js';
|
|
7
7
|
export type { Channel } from './channel-store.js';
|
|
8
8
|
export { EventHubStore } from './eventhub-store.js';
|
|
9
|
-
export type { BinaryData, ChannelsMeta, CoreChannel, CorePikkuChannelMiddleware, CorePikkuChannelMiddlewareFactory, ChannelMessageMeta, ChannelMeta,
|
|
9
|
+
export type { BinaryData, ChannelsMeta, CoreChannel, CorePikkuChannelMiddleware, CorePikkuChannelMiddlewareFactory, ChannelMessageMeta, ChannelMeta, PikkuChannel, PikkuChannelHandlerFactory, } from './channel.types.js';
|
|
10
10
|
export { defineChannelRoutes } from './define-channel-routes.js';
|
|
11
|
-
export {
|
|
12
|
-
export type { ApprovalPolicy, ApprovalRequester, Capabilities, Capability,
|
|
13
|
-
export { channelRemote, getChannelHostRPC, handleChannelRPCResponse, releaseChannelHostRPC, } from './channel-host-rpc.js';
|
|
11
|
+
export { ChannelDeploymentService, ChannelRPCError, ChannelRPCRegistry, unsupportedChannelRemote, } from './channel-rpc.js';
|
|
12
|
+
export type { ApprovalPolicy, ApprovalRequester, Capabilities, Capability, CapabilityHandler, ChannelRPCPending, ChannelRPCRequest, ChannelRPCResponse, ChannelRPCValidator, } from './channel-rpc.js';
|