@aidenhq/sdk 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/judge.d.ts +31 -0
- package/dist/src/judge.js +89 -0
- package/dist/src/vendor/proxy.d.ts +4 -0
- package/dist/src/vendor/proxy.js +2 -1
- package/package.json +3 -2
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export declare const LUNA = "~openai/gpt-luna-latest";
|
|
2
|
+
export declare const DEEPSEEK = "~deepseek/deepseek-flash-latest";
|
|
3
|
+
export declare const GLM = "~z-ai/glm-flash-latest";
|
|
4
|
+
/** Each judge model's reasoning effort. */
|
|
5
|
+
export declare const EFFORT: Record<string, string>;
|
|
6
|
+
/** Output cap, reasoning included: room for high-effort reasoning; Luna allows 128,000. */
|
|
7
|
+
export declare const MAX_TOKENS = 100000;
|
|
8
|
+
/** The largest diff a judge reads; the reviewer sends a larger one to the owner as too large. */
|
|
9
|
+
export declare const MAX_DIFF = 150000;
|
|
10
|
+
export declare const judgeModels: (authorModel?: string) => string[];
|
|
11
|
+
export declare const secondModels: (authorModel?: string, first?: string) => string[];
|
|
12
|
+
/** Retries for a caller that reaches OpenRouter directly (an eval); through the model gateway, which retries, pass none. */
|
|
13
|
+
export declare const RETRY_DELAYS_MS: number[];
|
|
14
|
+
/**
|
|
15
|
+
* OpenRouter chat completions with a strict JSON schema, ZDR providers only, no tools. Resolves { content, refusal, usage }.
|
|
16
|
+
* The services pass the model gateway's url and the token the kernel minted for the call; an eval passes OpenRouter's key.
|
|
17
|
+
*/
|
|
18
|
+
export declare function openRouterAsk(key: string, { timeoutMs, fetchFn, schema, url, retryDelaysMs }: {
|
|
19
|
+
timeoutMs?: number;
|
|
20
|
+
fetchFn?: typeof fetch;
|
|
21
|
+
schema: unknown;
|
|
22
|
+
url?: string;
|
|
23
|
+
retryDelaysMs?: number[];
|
|
24
|
+
}): (model: string, { system, user }: {
|
|
25
|
+
system: string;
|
|
26
|
+
user: string;
|
|
27
|
+
}) => Promise<{
|
|
28
|
+
content: any;
|
|
29
|
+
refusal: any;
|
|
30
|
+
usage: any;
|
|
31
|
+
}>;
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
// The judges' shared pieces (design §4.4 "Judge"): the AI reviewer and the grader keep their own contracts, prompts and
|
|
2
|
+
// credentials, and share the models they may use, the cross-family choice and the one schema-bound, tool-less model call.
|
|
3
|
+
//
|
|
4
|
+
// Canonical copy: kernel/sdk/src/judge.ts. The reviewer and grading services hold byte-identical copies at
|
|
5
|
+
// src/aidenhq-sdk/judge.ts: edit this one, then run `node scripts/sync-shared.mjs`.
|
|
6
|
+
// GPT Luna at medium, then DeepSeek Flash at high (the owner's pick, 2026-11-13; the reviewer eval scores it). GLM Flash at high
|
|
7
|
+
// is the owner's checker model (the reviewer's second opinion).
|
|
8
|
+
export const LUNA = "~openai/gpt-luna-latest";
|
|
9
|
+
export const DEEPSEEK = "~deepseek/deepseek-flash-latest";
|
|
10
|
+
export const GLM = "~z-ai/glm-flash-latest";
|
|
11
|
+
/** Each judge model's reasoning effort. */
|
|
12
|
+
export const EFFORT = { [LUNA]: "medium", [DEEPSEEK]: "high", [GLM]: "high" };
|
|
13
|
+
/** Output cap, reasoning included: room for high-effort reasoning; Luna allows 128,000. */
|
|
14
|
+
export const MAX_TOKENS = 100_000;
|
|
15
|
+
/** The largest diff a judge reads; the reviewer sends a larger one to the owner as too large. */
|
|
16
|
+
export const MAX_DIFF = 150_000;
|
|
17
|
+
/**
|
|
18
|
+
* The models to ask, in order: GPT Luna, then DeepSeek Flash, minus the author's own model family (a DeepSeek author, most of
|
|
19
|
+
* the fleet, gets Luna only; an OpenAI author gets DeepSeek only; never its own family, not even as the fallback).
|
|
20
|
+
*/
|
|
21
|
+
const FAMILIES = [
|
|
22
|
+
[LUNA, /openai|gpt/i],
|
|
23
|
+
[DEEPSEEK, /deepseek/i],
|
|
24
|
+
];
|
|
25
|
+
export const judgeModels = (authorModel = "") => FAMILIES.filter(([, re]) => !re.test(String(authorModel))).map(([m]) => m);
|
|
26
|
+
/**
|
|
27
|
+
* A second opinion's models, in order: never the author's family nor the first judge's (GPT Sol, the advisor, is OpenAI like
|
|
28
|
+
* Luna, so it can't be the second opinion on Luna's verdict).
|
|
29
|
+
*/
|
|
30
|
+
const SECOND = [
|
|
31
|
+
[GLM, /z-ai|glm/i],
|
|
32
|
+
[DEEPSEEK, /deepseek/i],
|
|
33
|
+
[LUNA, /openai|gpt/i],
|
|
34
|
+
];
|
|
35
|
+
export const secondModels = (authorModel = "", first = "") => SECOND.filter(([, re]) => !re.test(String(authorModel)) && !re.test(String(first))).map(([m]) => m);
|
|
36
|
+
const RETRY_STATUS = new Set([429, 500, 502, 503, 504, 524]);
|
|
37
|
+
/** Retries for a caller that reaches OpenRouter directly (an eval); through the model gateway, which retries, pass none. */
|
|
38
|
+
export const RETRY_DELAYS_MS = [1000, 3000, 9000, 20000];
|
|
39
|
+
/**
|
|
40
|
+
* fetch that retries a transient OpenRouter failure (429/5xx "high demand", a reset or timeout) with jittered backoff,
|
|
41
|
+
* honouring Retry-After (capped at 20s); a 4xx validation or auth error is returned at once.
|
|
42
|
+
*/
|
|
43
|
+
async function fetchRetrying(fetchFn, delays, url, init, timeoutMs) {
|
|
44
|
+
for (let n = 0;; n++) {
|
|
45
|
+
let res, err;
|
|
46
|
+
try {
|
|
47
|
+
res = await fetchFn(url, { ...init, signal: AbortSignal.timeout(timeoutMs) });
|
|
48
|
+
}
|
|
49
|
+
catch (e) {
|
|
50
|
+
err = e;
|
|
51
|
+
}
|
|
52
|
+
if (!(err ? true : RETRY_STATUS.has(res.status)) || n >= delays.length) {
|
|
53
|
+
if (err)
|
|
54
|
+
throw err;
|
|
55
|
+
return res;
|
|
56
|
+
}
|
|
57
|
+
const after = Number(res?.headers?.get("retry-after"));
|
|
58
|
+
const wait = Math.min(after > 0 ? after * 1000 : delays[n] * (0.75 + Math.random() / 2), 20_000);
|
|
59
|
+
console.error(`openrouter retry ${n + 1}/${delays.length} (${res?.status ?? String(err.message).slice(0, 80)}) in ${Math.round(wait)}ms`);
|
|
60
|
+
await res?.body?.cancel().catch(() => { });
|
|
61
|
+
await new Promise((r) => setTimeout(r, wait));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* OpenRouter chat completions with a strict JSON schema, ZDR providers only, no tools. Resolves { content, refusal, usage }.
|
|
66
|
+
* The services pass the model gateway's url and the token the kernel minted for the call; an eval passes OpenRouter's key.
|
|
67
|
+
*/
|
|
68
|
+
export function openRouterAsk(key, { timeoutMs = 600_000, fetchFn = fetch, schema, url = "https://openrouter.ai/api/v1/chat/completions", retryDelaysMs = [] }) {
|
|
69
|
+
return async (model, { system, user }) => {
|
|
70
|
+
const res = await fetchRetrying(fetchFn, retryDelaysMs, url, {
|
|
71
|
+
method: "POST",
|
|
72
|
+
headers: { authorization: `Bearer ${key}`, "content-type": "application/json" },
|
|
73
|
+
body: JSON.stringify({
|
|
74
|
+
model,
|
|
75
|
+
messages: [{ role: "system", content: system }, { role: "user", content: user }],
|
|
76
|
+
response_format: { type: "json_schema", json_schema: { name: "verdict", strict: true, schema } },
|
|
77
|
+
provider: { zdr: true },
|
|
78
|
+
reasoning: { effort: EFFORT[model] ?? "high" },
|
|
79
|
+
max_tokens: MAX_TOKENS,
|
|
80
|
+
temperature: 0,
|
|
81
|
+
}),
|
|
82
|
+
}, timeoutMs);
|
|
83
|
+
if (!res.ok)
|
|
84
|
+
throw new Error(`OpenRouter ${res.status}: ${(await res.text().catch(() => "")).slice(0, 200)}`);
|
|
85
|
+
const json = await res.json();
|
|
86
|
+
const msg = json?.choices?.[0]?.message ?? {};
|
|
87
|
+
return { content: msg.content ?? "", refusal: msg.refusal ?? null, usage: json?.usage };
|
|
88
|
+
};
|
|
89
|
+
}
|
|
@@ -16,6 +16,8 @@ export interface Hooks {
|
|
|
16
16
|
args?(name: string, args: Record<string, unknown>, tool: Tool): Record<string, unknown>;
|
|
17
17
|
/** Tools never listed or called (a real security rule; most connections have none). */
|
|
18
18
|
deny?: RegExp;
|
|
19
|
+
/** A tool's answer as it goes back (ClickHouse: cut to a size). */
|
|
20
|
+
result?(result: CallToolResult): CallToolResult;
|
|
19
21
|
/** Whether the upstream says a tool only reads. Default: it marks it readOnlyHint. */
|
|
20
22
|
isRead?(tool: Tool): boolean;
|
|
21
23
|
/** A problem that makes the connection unusable though signed in (Jira: the site isn't reachable). */
|
|
@@ -40,6 +42,8 @@ export interface ProxyOptions {
|
|
|
40
42
|
caller(req: IncomingMessage): Promise<unknown>;
|
|
41
43
|
/** The MCP server's instructions to agents. */
|
|
42
44
|
instructions: string;
|
|
45
|
+
/** How long one tool call may take (ClickHouse's queries run up to two minutes); default: the MCP SDK's 60 seconds. */
|
|
46
|
+
callTimeoutMs?: number;
|
|
43
47
|
/** Remembers the tool list here (tools.json), for stand-ins while not connected. */
|
|
44
48
|
stateDir?: string;
|
|
45
49
|
hooks?: Hooks;
|
package/dist/src/vendor/proxy.js
CHANGED
|
@@ -117,7 +117,8 @@ export function createVendorProxy(o) {
|
|
|
117
117
|
if (!tool)
|
|
118
118
|
throw new Error(`${o.name} doesn't offer ${name}`);
|
|
119
119
|
const sent = hooks.args ? hooks.args(name, args, tool) : args;
|
|
120
|
-
|
|
120
|
+
const result = (await u.client.callTool({ name, arguments: sent }, undefined, o.callTimeoutMs ? { timeout: o.callTimeoutMs } : undefined));
|
|
121
|
+
return hooks.result ? hooks.result(result) : result;
|
|
121
122
|
});
|
|
122
123
|
}
|
|
123
124
|
function buildServer(readOnly) {
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aidenhq/sdk",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "The shared code of AidenHQ modules: contract types, engine IO, the caller check and fence, write executors, the reviewer side, the source writer and the vendor MCP pass-through.",
|
|
3
|
+
"version": "0.3.0",
|
|
4
|
+
"description": "The shared code of AidenHQ modules: contract types, engine IO, the caller check and fence, write executors, the reviewer side, the judges' models and call, the source writer and the vendor MCP pass-through.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
7
7
|
"type": "git",
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
"./caller": "./dist/src/caller.js",
|
|
24
24
|
"./fence": "./dist/src/fence.js",
|
|
25
25
|
"./reviewer": "./dist/src/reviewer.js",
|
|
26
|
+
"./judge": "./dist/src/judge.js",
|
|
26
27
|
"./progress": "./dist/src/progress.js",
|
|
27
28
|
"./source-store": "./dist/src/source-store.js",
|
|
28
29
|
"./vendor/proxy": "./dist/src/vendor/proxy.js",
|