melchizedek-agents 1.14.0 → 1.15.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/.env.example +9 -1
- package/CHANGELOG.md +74 -0
- package/DOCUMENTATION.md +22 -0
- package/config/agents/syndicate.schema.json +66 -44
- package/config/agents/syndicateSchema.yaml +11 -6
- package/dist/lib/a2a/app.d.ts +8 -0
- package/dist/lib/a2a/app.js +2 -2
- package/dist/lib/compile.d.ts +4 -4
- package/dist/lib/compileNative.js +4 -1
- package/dist/lib/envSyndicate.d.ts +35 -0
- package/dist/lib/envSyndicate.js +70 -0
- package/dist/lib/loadSyndicate.d.ts +19 -7
- package/dist/lib/loadSyndicate.js +14 -5
- package/dist/lib/models/capabilities.d.ts +2 -0
- package/dist/lib/models/capabilities.js +2 -0
- package/dist/lib/models/chatCompletionsAdapter.js +13 -0
- package/dist/lib/models/claudeAdapter.js +15 -0
- package/dist/lib/models/contextWindow.d.ts +32 -0
- package/dist/lib/models/contextWindow.js +73 -0
- package/dist/lib/models/contract.d.ts +15 -1
- package/dist/lib/models/errorResponse.d.ts +4 -0
- package/dist/lib/models/errorResponse.js +25 -0
- package/dist/lib/models/geminiAdapter.js +9 -0
- package/dist/lib/models/genaiMapping.js +10 -1
- package/dist/lib/models/gptAdapter.d.ts +1 -1
- package/dist/lib/models/gptAdapter.js +3 -0
- package/dist/lib/models/jevAdapter.js +2 -0
- package/dist/lib/registry.d.ts +23 -9
- package/dist/lib/registry.js +57 -3
- package/dist/lib/runtime/native/agentLoop.js +25 -3
- package/dist/lib/runtime/native/compaction.d.ts +91 -42
- package/dist/lib/runtime/native/compaction.js +188 -86
- package/dist/lib/runtime/native/history.d.ts +52 -1
- package/dist/lib/runtime/native/history.js +80 -6
- package/dist/lib/runtime/native/recall.d.ts +9 -0
- package/dist/lib/runtime/native/recall.js +42 -0
- package/dist/lib/runtime/native/request.d.ts +8 -2
- package/dist/lib/runtime/native/request.js +30 -9
- package/dist/lib/storage/postgres/index.d.ts +3 -0
- package/dist/lib/storage/postgres/index.js +2 -0
- package/dist/lib/syndicateSchema.d.ts +16 -12
- package/dist/lib/syndicateSchema.js +10 -12
- package/dist/lib/tools/memoryTools.js +3 -0
- package/dist/lib/tools/tool.d.ts +9 -2
- package/dist/lib/tools/toolContract.d.ts +1 -1
- package/dist/lib/tools/toolContract.js +4 -2
- package/dist/lib/tools/webExtractTool.js +4 -0
- package/dist/scripts/a2a_server.js +28 -4
- package/dist/scripts/registry.d.ts +7 -4
- package/dist/scripts/registry.js +17 -5
- package/package.json +2 -1
|
@@ -351,6 +351,15 @@ export class ClaudeAdapter {
|
|
|
351
351
|
if (blocks.length > 0)
|
|
352
352
|
messages.push({ role, content: blocks });
|
|
353
353
|
});
|
|
354
|
+
// This step's notes, after the history: on the last user message (tool
|
|
355
|
+
// results are user content here too), so the cached prefix before it holds.
|
|
356
|
+
if (request.turnNotes) {
|
|
357
|
+
const last = messages[messages.length - 1];
|
|
358
|
+
if (last?.role === 'user')
|
|
359
|
+
last.content.push({ type: 'text', text: request.turnNotes });
|
|
360
|
+
else
|
|
361
|
+
messages.push({ role: 'user', content: [{ type: 'text', text: request.turnNotes }] });
|
|
362
|
+
}
|
|
354
363
|
if (droppedImages.size > 0) {
|
|
355
364
|
setLlmSpanAttribute('llm.image.dropped', [...droppedImages].join(','));
|
|
356
365
|
for (const what of droppedImages) {
|
|
@@ -484,6 +493,12 @@ export class ClaudeAdapter {
|
|
|
484
493
|
...(thinking ? { thinking } : {}),
|
|
485
494
|
...(Object.keys(outputConfig).length > 0 ? { output_config: outputConfig } : {}),
|
|
486
495
|
...(plan?.dropBlock ? { betas: [THINKING_BINDING_BETA] } : {}),
|
|
496
|
+
// Automatic prompt caching: the API puts the breakpoint on the last
|
|
497
|
+
// cacheable block and moves it forward as the history grows, so each
|
|
498
|
+
// step reads the previous step's prefix at the cached price. The loop
|
|
499
|
+
// keeps the prefix stable for it (ModelRequest.turnNotes). Anthropic's
|
|
500
|
+
// own API only until Bedrock and Vertex are confirmed to take the field.
|
|
501
|
+
...(endpoint.platform === 'direct' ? { cache_control: { type: 'ephemeral' } } : {}),
|
|
487
502
|
};
|
|
488
503
|
return { body, beta: plan?.dropBlock === true, structuredTool: structuredTool !== undefined };
|
|
489
504
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/models/contextWindow.ts — how many tokens a model's context holds,
|
|
3
|
+
* read from the model id (ADR 0154).
|
|
4
|
+
*
|
|
5
|
+
* WHY this file exists:
|
|
6
|
+
* Compaction on by default needs a threshold per model, and nothing in the
|
|
7
|
+
* registry said how big a window is. The numbers here are each provider's
|
|
8
|
+
* published context length for the id families the engine routes
|
|
9
|
+
* (lib/models/providerMap.ts), newest row first; an id a family does not
|
|
10
|
+
* name gets the family's newest row, as the Claude generation table does.
|
|
11
|
+
* Ollama's is whatever the server was started with: OLLAMA_CONTEXT_LENGTH
|
|
12
|
+
* when set, else Ollama's 4,096 default (lib/models/ollamaAdapter.ts), the
|
|
13
|
+
* one provider that cannot report an overflow, which is why its threshold
|
|
14
|
+
* must be right.
|
|
15
|
+
*
|
|
16
|
+
* `compactAfterTokens(model)` is the default `compact_after_tokens`: 60%
|
|
17
|
+
* of the window, so a step's own output, the summary and a tool result
|
|
18
|
+
* or two still fit after the threshold is passed. A window the table does
|
|
19
|
+
* not know gets UNKNOWN_WINDOW.
|
|
20
|
+
*/
|
|
21
|
+
/** Assumed for a provider the table does not know. */
|
|
22
|
+
export declare const UNKNOWN_WINDOW = 100000;
|
|
23
|
+
/** The share of the window past which an agent compacts by default. */
|
|
24
|
+
export declare const COMPACT_AT_SHARE = 0.6;
|
|
25
|
+
/** Ollama's context length unless the server says otherwise. */
|
|
26
|
+
export declare const OLLAMA_DEFAULT_CONTEXT = 4096;
|
|
27
|
+
/** The Ollama server's context length: OLLAMA_CONTEXT_LENGTH, else Ollama's default. */
|
|
28
|
+
export declare function ollamaContextLength(env?: NodeJS.ProcessEnv): number;
|
|
29
|
+
/** The tokens `model`'s context holds, or undefined for a provider the table does not know (Jev, a gateway-only id). */
|
|
30
|
+
export declare function contextWindowOf(model: string, env?: NodeJS.ProcessEnv): number | undefined;
|
|
31
|
+
/** The default `compact_after_tokens` for `model`: COMPACT_AT_SHARE of its window. */
|
|
32
|
+
export declare function compactAfterTokens(model: string, env?: NodeJS.ProcessEnv): number;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* lib/models/contextWindow.ts — how many tokens a model's context holds,
|
|
3
|
+
* read from the model id (ADR 0154).
|
|
4
|
+
*
|
|
5
|
+
* WHY this file exists:
|
|
6
|
+
* Compaction on by default needs a threshold per model, and nothing in the
|
|
7
|
+
* registry said how big a window is. The numbers here are each provider's
|
|
8
|
+
* published context length for the id families the engine routes
|
|
9
|
+
* (lib/models/providerMap.ts), newest row first; an id a family does not
|
|
10
|
+
* name gets the family's newest row, as the Claude generation table does.
|
|
11
|
+
* Ollama's is whatever the server was started with: OLLAMA_CONTEXT_LENGTH
|
|
12
|
+
* when set, else Ollama's 4,096 default (lib/models/ollamaAdapter.ts), the
|
|
13
|
+
* one provider that cannot report an overflow, which is why its threshold
|
|
14
|
+
* must be right.
|
|
15
|
+
*
|
|
16
|
+
* `compactAfterTokens(model)` is the default `compact_after_tokens`: 60%
|
|
17
|
+
* of the window, so a step's own output, the summary and a tool result
|
|
18
|
+
* or two still fit after the threshold is passed. A window the table does
|
|
19
|
+
* not know gets UNKNOWN_WINDOW.
|
|
20
|
+
*/
|
|
21
|
+
import { providerForModel } from './providerMap.js';
|
|
22
|
+
/** Assumed for a provider the table does not know. */
|
|
23
|
+
export const UNKNOWN_WINDOW = 100_000;
|
|
24
|
+
/** The share of the window past which an agent compacts by default. */
|
|
25
|
+
export const COMPACT_AT_SHARE = 0.6;
|
|
26
|
+
/** Ollama's context length unless the server says otherwise. */
|
|
27
|
+
export const OLLAMA_DEFAULT_CONTEXT = 4_096;
|
|
28
|
+
const CLAUDE = [[/^claude-/, 200_000]];
|
|
29
|
+
const OPENAI = [
|
|
30
|
+
[/^gpt-4o/, 128_000],
|
|
31
|
+
[/^gpt-4\.1/, 1_047_576],
|
|
32
|
+
[/^o[0-9]/, 200_000],
|
|
33
|
+
[/^gpt-5/, 400_000],
|
|
34
|
+
];
|
|
35
|
+
const GEMINI = [[/^gemini-/, 1_048_576]];
|
|
36
|
+
const XAI = [
|
|
37
|
+
[/^grok-3/, 131_072],
|
|
38
|
+
[/^grok-4/, 256_000],
|
|
39
|
+
];
|
|
40
|
+
const MOONSHOT = [
|
|
41
|
+
[/^kimi-k2/, 262_144],
|
|
42
|
+
[/^kimi-k3/, 1_000_000],
|
|
43
|
+
];
|
|
44
|
+
const DEEPSEEK = [[/^deepseek-/, 1_000_000]];
|
|
45
|
+
const FAMILIES = {
|
|
46
|
+
anthropic: CLAUDE,
|
|
47
|
+
openai: OPENAI,
|
|
48
|
+
gemini: GEMINI,
|
|
49
|
+
xai: XAI,
|
|
50
|
+
moonshot: MOONSHOT,
|
|
51
|
+
deepseek: DEEPSEEK,
|
|
52
|
+
};
|
|
53
|
+
/** The Ollama server's context length: OLLAMA_CONTEXT_LENGTH, else Ollama's default. */
|
|
54
|
+
export function ollamaContextLength(env = process.env) {
|
|
55
|
+
const set = Number(env.OLLAMA_CONTEXT_LENGTH);
|
|
56
|
+
return Number.isFinite(set) && set > 0 ? Math.floor(set) : OLLAMA_DEFAULT_CONTEXT;
|
|
57
|
+
}
|
|
58
|
+
/** The tokens `model`'s context holds, or undefined for a provider the table does not know (Jev, a gateway-only id). */
|
|
59
|
+
export function contextWindowOf(model, env = process.env) {
|
|
60
|
+
const provider = providerForModel(model);
|
|
61
|
+
if (provider === 'ollama')
|
|
62
|
+
return ollamaContextLength(env);
|
|
63
|
+
const rows = FAMILIES[provider];
|
|
64
|
+
if (!rows || rows.length === 0)
|
|
65
|
+
return undefined;
|
|
66
|
+
const bare = model.replace(/^[a-z-]+\//, '');
|
|
67
|
+
const hit = rows.find(([pattern]) => pattern.test(bare));
|
|
68
|
+
return (hit ?? rows[rows.length - 1])[1];
|
|
69
|
+
}
|
|
70
|
+
/** The default `compact_after_tokens` for `model`: COMPACT_AT_SHARE of its window. */
|
|
71
|
+
export function compactAfterTokens(model, env = process.env) {
|
|
72
|
+
return Math.round((contextWindowOf(model, env) ?? UNKNOWN_WINDOW) * COMPACT_AT_SHARE);
|
|
73
|
+
}
|
|
@@ -207,10 +207,24 @@ export interface ModelRequest {
|
|
|
207
207
|
* request on.
|
|
208
208
|
*/
|
|
209
209
|
model: string;
|
|
210
|
-
/**
|
|
210
|
+
/**
|
|
211
|
+
* The system prompt. Only text that is the same from one request of the
|
|
212
|
+
* agent to the next belongs here: every provider caches the prompt as a
|
|
213
|
+
* prefix (tools, then system, then the history), so a word that changes
|
|
214
|
+
* here misses the whole history behind it. Text for this step alone goes
|
|
215
|
+
* in `turnNotes`.
|
|
216
|
+
*/
|
|
211
217
|
system?: string;
|
|
212
218
|
/** The conversation, oldest first. */
|
|
213
219
|
messages: Message[];
|
|
220
|
+
/**
|
|
221
|
+
* Text the model reads this step that is not part of the durable
|
|
222
|
+
* instruction: recalled facts, a note for this turn. Each adapter places
|
|
223
|
+
* it after the history, where it leaves the cached prefix alone: as text
|
|
224
|
+
* at the end of the last user message, or as a user message of its own
|
|
225
|
+
* when the history ends on tool results. Absent means none.
|
|
226
|
+
*/
|
|
227
|
+
turnNotes?: string;
|
|
214
228
|
/** Client-side tools. */
|
|
215
229
|
tools?: ToolDeclaration[];
|
|
216
230
|
/** Provider-side tools, by name. */
|
|
@@ -33,6 +33,10 @@ export declare const ERROR_STATUS_KEY = "error.status";
|
|
|
33
33
|
export declare function errorText(err: unknown): string;
|
|
34
34
|
/** What a caught error says about trying again. A cancellation never is retryable. */
|
|
35
35
|
export declare function errorDecision(err: unknown): RetryDecision;
|
|
36
|
+
export declare function isContextOverflow(error: {
|
|
37
|
+
message?: string;
|
|
38
|
+
status?: number;
|
|
39
|
+
} | undefined): boolean;
|
|
36
40
|
/** What a non-2xx HTTP status says about trying again. */
|
|
37
41
|
export declare function statusDecision(status: number): RetryDecision;
|
|
38
42
|
/**
|
|
@@ -14,6 +14,31 @@ export function errorDecision(err) {
|
|
|
14
14
|
const decision = classifyError(err);
|
|
15
15
|
return err?.name === 'AbortError' ? { ...decision, retryable: false } : decision;
|
|
16
16
|
}
|
|
17
|
+
/**
|
|
18
|
+
* The provider refused the request because the prompt no longer fits the
|
|
19
|
+
* model's window (ADR 0154): a 400 (or 413, or a status it did not give)
|
|
20
|
+
* whose message says so in one of the providers' spellings. The loop
|
|
21
|
+
* answers it with a forced compaction and one retry; the fallback never
|
|
22
|
+
* redirects it (it is the request's fault, not the provider's).
|
|
23
|
+
*
|
|
24
|
+
* Anthropic "prompt is too long: 213463 tokens > 200000 maximum"
|
|
25
|
+
* OpenAI "This model's maximum context length is 128000 tokens …" /
|
|
26
|
+
* "Your input exceeds the context window of this model" (context_length_exceeded)
|
|
27
|
+
* Gemini "The input token count (…) exceeds the maximum number of tokens allowed (…)"
|
|
28
|
+
* xAI "This model's maximum prompt length is 131072 …"
|
|
29
|
+
* Moonshot "Your request exceeded model token limit: 262144"
|
|
30
|
+
* DeepSeek "This model's maximum context length is 1000000 tokens …"
|
|
31
|
+
* llama.cpp "the request exceeds the available context size"
|
|
32
|
+
* Ollama reports nothing: it truncates the prompt instead.
|
|
33
|
+
*/
|
|
34
|
+
const OVERFLOW_MESSAGE = /prompt is too long|context[_ ]length|context window|maximum (?:prompt|context) length|input token count .*exceeds|exceeds the (?:available )?context|exceeded model token limit|request_too_large|input is too long|too many tokens/i;
|
|
35
|
+
export function isContextOverflow(error) {
|
|
36
|
+
if (!error?.message)
|
|
37
|
+
return false;
|
|
38
|
+
if (error.status !== undefined && error.status !== 400 && error.status !== 413)
|
|
39
|
+
return false;
|
|
40
|
+
return OVERFLOW_MESSAGE.test(error.message);
|
|
41
|
+
}
|
|
17
42
|
/** What a non-2xx HTTP status says about trying again. */
|
|
18
43
|
export function statusDecision(status) {
|
|
19
44
|
return { retryable: isRetryableStatus(status), status };
|
|
@@ -249,6 +249,15 @@ function buildRequest(request, options) {
|
|
|
249
249
|
if (parts.length)
|
|
250
250
|
contents.push({ role: message.role === 'assistant' ? 'model' : 'user', parts });
|
|
251
251
|
});
|
|
252
|
+
// This step's notes, after the history, on the last user content (function
|
|
253
|
+
// responses are user content too), so the cached prefix before it holds.
|
|
254
|
+
if (request.turnNotes) {
|
|
255
|
+
const last = contents[contents.length - 1];
|
|
256
|
+
if (last?.role === 'user')
|
|
257
|
+
last.parts.push({ text: request.turnNotes });
|
|
258
|
+
else
|
|
259
|
+
contents.push({ role: 'user', parts: [{ text: request.turnNotes }] });
|
|
260
|
+
}
|
|
252
261
|
const config = {};
|
|
253
262
|
if (system.length)
|
|
254
263
|
config.systemInstruction = { parts: system.map((text) => ({ text })) };
|
|
@@ -858,9 +858,18 @@ export function modelRequestToLlmRequest(request) {
|
|
|
858
858
|
config.stopSequences = [...sampling.stop];
|
|
859
859
|
if (request.signal)
|
|
860
860
|
config.abortSignal = request.signal;
|
|
861
|
+
// This step's notes, after the history, as the Gemini adapter places them.
|
|
862
|
+
const contents = messagesToContents({ messages: request.messages }).contents;
|
|
863
|
+
if (request.turnNotes) {
|
|
864
|
+
const last = contents[contents.length - 1];
|
|
865
|
+
if (last?.role === 'user')
|
|
866
|
+
(last.parts ??= []).push({ text: request.turnNotes });
|
|
867
|
+
else
|
|
868
|
+
contents.push({ role: 'user', parts: [{ text: request.turnNotes }] });
|
|
869
|
+
}
|
|
861
870
|
return {
|
|
862
871
|
model: request.model,
|
|
863
|
-
contents
|
|
872
|
+
contents,
|
|
864
873
|
config,
|
|
865
874
|
liveConnectConfig: {},
|
|
866
875
|
toolsDict: Object.fromEntries(declarations.map((d) => [d.name, declaredTool(d)])),
|
|
@@ -73,7 +73,7 @@ export interface ResponsesInput {
|
|
|
73
73
|
* followed by the item it preceded; one that does not puts its calls before
|
|
74
74
|
* its text, as ADK always built it.
|
|
75
75
|
*/
|
|
76
|
-
export declare function responsesInput(request: Pick<ModelRequest, 'system' | 'messages'>, replay?: ReasoningReplay): ResponsesInput;
|
|
76
|
+
export declare function responsesInput(request: Pick<ModelRequest, 'system' | 'messages' | 'turnNotes'>, replay?: ReasoningReplay): ResponsesInput;
|
|
77
77
|
/** The request's client-side tools as Responses function tools; a strict one sends the strict form of its schema. */
|
|
78
78
|
export declare function responsesFunctionTools(request: Pick<ModelRequest, 'tools'>): WireItem[];
|
|
79
79
|
/** The native tools a request sends, the ones it drops, and the span flags they set. */
|
|
@@ -157,6 +157,9 @@ export function responsesInput(request, replay) {
|
|
|
157
157
|
}
|
|
158
158
|
flush();
|
|
159
159
|
}
|
|
160
|
+
// This step's notes, after the history, as one more user item, so the cached prefix before them holds.
|
|
161
|
+
if (request.turnNotes)
|
|
162
|
+
input.push({ role: 'user', content: [{ type: 'input_text', text: request.turnNotes }] });
|
|
160
163
|
return { ...(system.length > 0 ? { instructions: system.join('\n\n') } : {}), input, droppedBlobs };
|
|
161
164
|
}
|
|
162
165
|
/** The request's client-side tools as Responses function tools; a strict one sends the strict form of its schema. */
|
|
@@ -112,6 +112,8 @@ export function stateOf(request) {
|
|
|
112
112
|
texts.push(request.system.trim());
|
|
113
113
|
for (const m of request.messages)
|
|
114
114
|
texts.push(...messageTexts(m));
|
|
115
|
+
if (request.turnNotes?.trim())
|
|
116
|
+
texts.push(request.turnNotes.trim());
|
|
115
117
|
return texts.length === 1 ? texts[0] : texts;
|
|
116
118
|
}
|
|
117
119
|
function messageTexts(m) {
|
package/dist/lib/registry.d.ts
CHANGED
|
@@ -10,10 +10,14 @@
|
|
|
10
10
|
* validation before anything is written, an author and a note on each
|
|
11
11
|
* version, and rollback by version number.
|
|
12
12
|
*
|
|
13
|
-
* Every function takes a client with supabase-js's `rpc` and `from
|
|
14
|
-
*
|
|
15
|
-
*
|
|
13
|
+
* Every function takes either a client with supabase-js's `rpc` and `from`
|
|
14
|
+
* (the service-role client) or a direct Postgres connection: anything with
|
|
15
|
+
* pg's `query` (a `pg.Pool`, postgresStorage's `pool`, PGlite). Both run the
|
|
16
|
+
* same SQL functions from the migration, so a version is recorded the same
|
|
17
|
+
* way. `postgresRegistryReader` is what the server loads rows through on
|
|
18
|
+
* DATABASE_URL. Nothing here prints a credential.
|
|
16
19
|
*/
|
|
20
|
+
import type { RegistryReader } from './loadSyndicate.ts';
|
|
17
21
|
/** The part of a supabase-js client this module uses. */
|
|
18
22
|
export interface RegistryClient {
|
|
19
23
|
rpc(fn: string, args: Record<string, unknown>): PromiseLike<{
|
|
@@ -25,6 +29,16 @@ export interface RegistryClient {
|
|
|
25
29
|
}>;
|
|
26
30
|
from(table: string): any;
|
|
27
31
|
}
|
|
32
|
+
/** The part of a direct Postgres connection this module uses: pg's `query` (a `pg.Pool`, PGlite). */
|
|
33
|
+
export interface RegistryPool {
|
|
34
|
+
query(text: string, values?: unknown[]): Promise<{
|
|
35
|
+
rows: any[];
|
|
36
|
+
}>;
|
|
37
|
+
}
|
|
38
|
+
/** Where the registry is: the Supabase API, or a direct connection. */
|
|
39
|
+
export type RegistryStore = RegistryClient | RegistryPool;
|
|
40
|
+
/** The registry's active and stored definitions over a direct connection, for `loadSyndicateFromRegistry`. */
|
|
41
|
+
export declare function postgresRegistryReader(pool: RegistryPool): RegistryReader;
|
|
28
42
|
export interface RegistryVersion {
|
|
29
43
|
id: string;
|
|
30
44
|
version: number;
|
|
@@ -71,17 +85,17 @@ export declare function parseRegistryRef(ref: string): {
|
|
|
71
85
|
* version now active: a new number, or an existing one when the same
|
|
72
86
|
* definition was published before (publishing it again re-activates it).
|
|
73
87
|
*/
|
|
74
|
-
export declare function publishAgent(client:
|
|
88
|
+
export declare function publishAgent(client: RegistryStore, id: string, config: unknown, options?: PublishAgentOptions): Promise<number>;
|
|
75
89
|
/** Make a stored version active again (a rollback, or a roll forward). */
|
|
76
|
-
export declare function activateVersion(client:
|
|
90
|
+
export declare function activateVersion(client: RegistryStore, id: string, version: number, options?: PublishOptions): Promise<number>;
|
|
77
91
|
/** One stored version's definition. */
|
|
78
|
-
export declare function getVersion(client:
|
|
92
|
+
export declare function getVersion(client: RegistryStore, id: string, version: number): Promise<unknown>;
|
|
79
93
|
/** Every version of `id`, newest first, with the active one marked. */
|
|
80
|
-
export declare function listVersions(client:
|
|
94
|
+
export declare function listVersions(client: RegistryStore, id: string): Promise<RegistryVersion[]>;
|
|
81
95
|
/** The active entry of every published id. */
|
|
82
|
-
export declare function listAgents(client:
|
|
96
|
+
export declare function listAgents(client: RegistryStore): Promise<RegistryEntry[]>;
|
|
83
97
|
/**
|
|
84
98
|
* Stop serving `id`: its active row goes, its versions stay. Publishing or
|
|
85
99
|
* activating it again brings it back. Returns false when it was not active.
|
|
86
100
|
*/
|
|
87
|
-
export declare function retireAgent(client:
|
|
101
|
+
export declare function retireAgent(client: RegistryStore, id: string): Promise<boolean>;
|
package/dist/lib/registry.js
CHANGED
|
@@ -10,13 +10,41 @@
|
|
|
10
10
|
* validation before anything is written, an author and a note on each
|
|
11
11
|
* version, and rollback by version number.
|
|
12
12
|
*
|
|
13
|
-
* Every function takes a client with supabase-js's `rpc` and `from
|
|
14
|
-
*
|
|
15
|
-
*
|
|
13
|
+
* Every function takes either a client with supabase-js's `rpc` and `from`
|
|
14
|
+
* (the service-role client) or a direct Postgres connection: anything with
|
|
15
|
+
* pg's `query` (a `pg.Pool`, postgresStorage's `pool`, PGlite). Both run the
|
|
16
|
+
* same SQL functions from the migration, so a version is recorded the same
|
|
17
|
+
* way. `postgresRegistryReader` is what the server loads rows through on
|
|
18
|
+
* DATABASE_URL. Nothing here prints a credential.
|
|
16
19
|
*/
|
|
17
20
|
import { bundleReferences } from './loadSyndicate.js';
|
|
18
21
|
import { validateSyndicateConfig } from './syndicateSchema.js';
|
|
19
22
|
const isObject = (v) => v !== null && typeof v === 'object' && !Array.isArray(v);
|
|
23
|
+
const isPool = (c) => typeof c.from !== 'function' && typeof c.query === 'function';
|
|
24
|
+
/** One statement on a direct connection; a database error goes through `fail` like the API's. */
|
|
25
|
+
async function sql(pool, action, text, values) {
|
|
26
|
+
try {
|
|
27
|
+
return (await pool.query(text, values)).rows;
|
|
28
|
+
}
|
|
29
|
+
catch (err) {
|
|
30
|
+
const e = err;
|
|
31
|
+
fail(action, { message: e.message ?? String(err), ...(e.code ? { code: e.code } : {}) });
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const iso = (v) => (v instanceof Date ? v.toISOString() : String(v ?? ''));
|
|
35
|
+
/** jsonb comes back parsed from pg and PGlite; a driver that returns text is parsed here. */
|
|
36
|
+
const json = (v) => (typeof v === 'string' ? JSON.parse(v) : v);
|
|
37
|
+
/** The registry's active and stored definitions over a direct connection, for `loadSyndicateFromRegistry`. */
|
|
38
|
+
export function postgresRegistryReader(pool) {
|
|
39
|
+
return {
|
|
40
|
+
async definition(id, version) {
|
|
41
|
+
const rows = version === undefined
|
|
42
|
+
? await sql(pool, `Reading ${id}`, 'SELECT yaml_content FROM adk_agent_registry WHERE id = $1', [id])
|
|
43
|
+
: await sql(pool, `Reading ${id}@${version}`, 'SELECT yaml_content FROM adk_agent_registry_versions WHERE id = $1 AND version = $2', [id, version]);
|
|
44
|
+
return rows[0] ? json(rows[0].yaml_content) : undefined;
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
20
48
|
/** Registry ids the database accepts (the same rule as melchizedek_registry_publish). */
|
|
21
49
|
export const REGISTRY_ID = /^[A-Za-z0-9_.-]{1,120}$/;
|
|
22
50
|
/** `<id>@<version>` names one stored version; a bare id names the active one. */
|
|
@@ -50,6 +78,10 @@ export async function publishAgent(client, id, config, options = {}) {
|
|
|
50
78
|
// The server validates every registry row at load (ADR 0018): an invalid
|
|
51
79
|
// definition would publish fine and fail on its first request.
|
|
52
80
|
validateSyndicateConfig(structuredClone(config), `registry:${id}`);
|
|
81
|
+
if (isPool(client)) {
|
|
82
|
+
const rows = await sql(client, `Publishing ${id}`, 'SELECT melchizedek_registry_publish($1, $2::jsonb, $3, $4) AS version', [id, JSON.stringify(config), options.author ?? null, options.note ?? null]);
|
|
83
|
+
return Number(rows[0]?.version);
|
|
84
|
+
}
|
|
53
85
|
const { data, error } = await client.rpc('melchizedek_registry_publish', {
|
|
54
86
|
p_id: id,
|
|
55
87
|
p_config: config,
|
|
@@ -68,6 +100,10 @@ export async function activateVersion(client, id, version, options = {}) {
|
|
|
68
100
|
// A stored version passed validation when it was published, but the schema
|
|
69
101
|
// may have moved since; refuse to activate what the server would refuse.
|
|
70
102
|
validateSyndicateConfig(structuredClone(await getVersion(client, id, version)), `registry:${id}@${version}`);
|
|
103
|
+
if (isPool(client)) {
|
|
104
|
+
const rows = await sql(client, `Activating ${id}@${version}`, 'SELECT melchizedek_registry_activate($1, $2, $3, $4) AS version', [id, version, options.author ?? null, options.note ?? null]);
|
|
105
|
+
return Number(rows[0]?.version);
|
|
106
|
+
}
|
|
71
107
|
const { data, error } = await client.rpc('melchizedek_registry_activate', {
|
|
72
108
|
p_id: id,
|
|
73
109
|
p_version: version,
|
|
@@ -80,6 +116,12 @@ export async function activateVersion(client, id, version, options = {}) {
|
|
|
80
116
|
}
|
|
81
117
|
/** One stored version's definition. */
|
|
82
118
|
export async function getVersion(client, id, version) {
|
|
119
|
+
if (isPool(client)) {
|
|
120
|
+
const definition = await postgresRegistryReader(client).definition(id, version);
|
|
121
|
+
if (definition === undefined)
|
|
122
|
+
throw new Error(`Registry id ${id} has no version ${version}.`);
|
|
123
|
+
return definition;
|
|
124
|
+
}
|
|
83
125
|
const { data, error } = await client
|
|
84
126
|
.from('adk_agent_registry_versions')
|
|
85
127
|
.select('yaml_content')
|
|
@@ -94,6 +136,12 @@ export async function getVersion(client, id, version) {
|
|
|
94
136
|
}
|
|
95
137
|
/** Every version of `id`, newest first, with the active one marked. */
|
|
96
138
|
export async function listVersions(client, id) {
|
|
139
|
+
if (isPool(client)) {
|
|
140
|
+
const rows = await sql(client, `Listing versions of ${id}`, `SELECT v.id, v.version, v.config_hash, v.published_by, v.published_at, v.note, (a.version = v.version) AS active
|
|
141
|
+
FROM adk_agent_registry_versions v LEFT JOIN adk_agent_registry a ON a.id = v.id
|
|
142
|
+
WHERE v.id = $1 ORDER BY v.version DESC`, [id]);
|
|
143
|
+
return rows.map((r) => ({ id: r.id, version: Number(r.version), configHash: r.config_hash, publishedBy: r.published_by, publishedAt: iso(r.published_at), note: r.note, active: r.active === true }));
|
|
144
|
+
}
|
|
97
145
|
const [versions, active] = await Promise.all([
|
|
98
146
|
client
|
|
99
147
|
.from('adk_agent_registry_versions')
|
|
@@ -119,6 +167,10 @@ export async function listVersions(client, id) {
|
|
|
119
167
|
}
|
|
120
168
|
/** The active entry of every published id. */
|
|
121
169
|
export async function listAgents(client) {
|
|
170
|
+
if (isPool(client)) {
|
|
171
|
+
const rows = await sql(client, 'Listing the registry', 'SELECT id, version, config_hash, published_by, published_at FROM adk_agent_registry ORDER BY id', []);
|
|
172
|
+
return rows.map((r) => ({ id: r.id, version: r.version ?? null, configHash: r.config_hash ?? null, publishedBy: r.published_by ?? null, publishedAt: r.published_at ? iso(r.published_at) : null }));
|
|
173
|
+
}
|
|
122
174
|
const { data, error } = await client
|
|
123
175
|
.from('adk_agent_registry')
|
|
124
176
|
.select('id, version, config_hash, published_by, published_at')
|
|
@@ -139,6 +191,8 @@ export async function listAgents(client) {
|
|
|
139
191
|
*/
|
|
140
192
|
export async function retireAgent(client, id) {
|
|
141
193
|
assertId(id);
|
|
194
|
+
if (isPool(client))
|
|
195
|
+
return (await sql(client, `Retiring ${id}`, 'DELETE FROM adk_agent_registry WHERE id = $1 RETURNING id', [id])).length > 0;
|
|
142
196
|
const { data, error } = await client.from('adk_agent_registry').delete().eq('id', id).select('id');
|
|
143
197
|
if (error)
|
|
144
198
|
fail(`Retiring ${id}`, error);
|
|
@@ -127,6 +127,7 @@
|
|
|
127
127
|
*/
|
|
128
128
|
import { randomUUID } from 'node:crypto';
|
|
129
129
|
import { circuitOpen, recordFailure, recordSuccess } from '../../models/circuitBreaker.js';
|
|
130
|
+
import { isContextOverflow } from '../../models/errorResponse.js';
|
|
130
131
|
import { providerForModel } from '../../models/providerMap.js';
|
|
131
132
|
import { resolveAdapter } from '../../models/registry.js';
|
|
132
133
|
import { toContractJsonSchema } from '../../models/schemaNormalize.js';
|
|
@@ -611,11 +612,13 @@ const hasParts = (event) => (event.content?.parts?.length ?? 0) > 0;
|
|
|
611
612
|
* the fallback by ADR 0044's rules, each a leaf adapter
|
|
612
613
|
* called by its own step (ADR 0053).
|
|
613
614
|
*/
|
|
614
|
-
async function* modelStep(agent, ctx, stepOptions) {
|
|
615
|
+
async function* modelStep(agent, ctx, stepOptions, overflowRetry = false) {
|
|
615
616
|
const adapterFor = ctx.adapterFor ?? resolveAdapter;
|
|
616
617
|
const fallbackId = agent.fallbackModel;
|
|
618
|
+
// ADR 0154: a prompt the provider refused as too long is handed back unstored once, for the loop to compact and retry.
|
|
619
|
+
const overflow = (final) => overflowRetry && isContextOverflow(final.error);
|
|
617
620
|
if (!fallbackId) {
|
|
618
|
-
return yield* withPartials((onPartial) => runModelStep({ ...stepOptions, adapter: adapterFor(agent.model), onPartial }));
|
|
621
|
+
return yield* withPartials((onPartial) => runModelStep({ ...stepOptions, adapter: adapterFor(agent.model), onPartial, redirect: (final) => overflow(final) }));
|
|
619
622
|
}
|
|
620
623
|
const log = ctx.log ?? ((m) => console.warn(m));
|
|
621
624
|
const onFallback = (why) => {
|
|
@@ -636,12 +639,17 @@ async function* modelStep(agent, ctx, stepOptions) {
|
|
|
636
639
|
},
|
|
637
640
|
// ADR 0044: a retryable failure counts against the provider, and redirects when nothing was produced.
|
|
638
641
|
redirect: (final, produced) => {
|
|
642
|
+
if (overflow(final))
|
|
643
|
+
return true;
|
|
639
644
|
if (!final.error?.retryable)
|
|
640
645
|
return false;
|
|
641
646
|
recordFailure(provider);
|
|
642
647
|
return !produced && final.parts.length === 0;
|
|
643
648
|
},
|
|
644
649
|
}));
|
|
650
|
+
// An overflow is the request's fault, not the provider's: never the fallback's to answer.
|
|
651
|
+
if (first.redirected && isContextOverflow(first.redirected))
|
|
652
|
+
return first;
|
|
645
653
|
if (first.redirected) {
|
|
646
654
|
return yield* onFallback(`${agent.model} failed (${first.redirected.code}: ${first.redirected.message.slice(0, 120)})`);
|
|
647
655
|
}
|
|
@@ -687,6 +695,9 @@ async function* agentLoop(agent, ctx) {
|
|
|
687
695
|
return sessions.append(session, event);
|
|
688
696
|
};
|
|
689
697
|
let steps = 0;
|
|
698
|
+
// A context overflow (ADR 0154) forces one compaction and one retry of the step; a second overflow ends the run on its error.
|
|
699
|
+
let overflowRetried = false;
|
|
700
|
+
let forceCompaction = false;
|
|
690
701
|
let lastEvent;
|
|
691
702
|
const selfCorrection = ctx.selfCorrection ?? new SelfCorrection();
|
|
692
703
|
const correction = selfCorrection.forModel(agent.name, ctx.invocationId);
|
|
@@ -737,7 +748,10 @@ async function* agentLoop(agent, ctx) {
|
|
|
737
748
|
...(ctx.branch !== undefined ? { branch: ctx.branch } : {}),
|
|
738
749
|
...(ctx.isolationScope !== undefined ? { isolationScope: ctx.isolationScope } : {}),
|
|
739
750
|
...(currentTurnSignal() ? { signal: currentTurnSignal() } : {}),
|
|
751
|
+
...(ctx.log ? { log: ctx.log } : {}),
|
|
752
|
+
...(forceCompaction ? { force: true } : {}),
|
|
740
753
|
});
|
|
754
|
+
forceCompaction = false;
|
|
741
755
|
if (compacted) {
|
|
742
756
|
// A turn that stopped during the summary stores nothing and makes no step (as ADK's Runner recorded it).
|
|
743
757
|
if (ctx.signal?.aborted || currentTurnSignal()?.aborted)
|
|
@@ -752,9 +766,17 @@ async function* agentLoop(agent, ctx) {
|
|
|
752
766
|
}
|
|
753
767
|
steps += 1;
|
|
754
768
|
// Telemetry hook: each step is a model.call span.
|
|
755
|
-
const step = yield* traceModelCall(agent, ctx, (traced) => modelStep(agent, traced, { ...traced, agent, beforeAppend: beforeStore, stateOverlay: runTemp.values(), ...(correction ? { correction } : {}) }));
|
|
769
|
+
const step = yield* traceModelCall(agent, ctx, (traced) => modelStep(agent, traced, { ...traced, agent, beforeAppend: beforeStore, stateOverlay: runTemp.values(), ...(correction ? { correction } : {}) }, !overflowRetried));
|
|
756
770
|
if (step.stopped)
|
|
757
771
|
return { reason: 'stopped', steps, lastEvent, stop: step.stopped };
|
|
772
|
+
// The prompt no longer fits the window (ADR 0154): the refused call was handed back unstored; the next
|
|
773
|
+
// pass compacts whatever the threshold says, with half the tail, and the step runs once more.
|
|
774
|
+
if (step.redirected && isContextOverflow(step.redirected)) {
|
|
775
|
+
overflowRetried = true;
|
|
776
|
+
forceCompaction = true;
|
|
777
|
+
ctx.log?.(`[compaction] ${agent.name}: the prompt overflowed the window (${step.redirected.message.slice(0, 120)}); compacting and retrying the step once`);
|
|
778
|
+
continue;
|
|
779
|
+
}
|
|
758
780
|
const modelEvent = step.event;
|
|
759
781
|
if (!modelEvent)
|
|
760
782
|
return { reason: 'empty', steps, lastEvent };
|