@otium-team/negotium 0.23.15 → 0.24.1
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/agent-helpers.js +969 -245
- package/dist/agent-helpers.js.map +13 -10
- package/dist/{chunk-ng13grfj.js → chunk-fzfzskjq.js} +18 -2
- package/dist/{chunk-ng13grfj.js.map → chunk-fzfzskjq.js.map} +4 -4
- package/dist/{chunk-m1ngkjdd.js → chunk-hn2cqdd0.js} +1 -1
- package/dist/{chunk-3hf8y6bd.js → chunk-kfckfff2.js} +3 -3
- package/dist/{chunk-3hf8y6bd.js.map → chunk-kfckfff2.js.map} +2 -2
- package/dist/hosted-agent.js +19 -3
- package/dist/hosted-agent.js.map +5 -5
- package/dist/main.js +3744 -2928
- package/dist/main.js.map +18 -15
- package/dist/mcp-factories.js +959 -229
- package/dist/mcp-factories.js.map +14 -11
- package/dist/prompts.js +221 -1
- package/dist/prompts.js.map +7 -5
- package/dist/registry.js +3 -3
- package/dist/rollout.js +2 -2
- package/dist/runtime/src/agents/mcp-tools/ask-user.ts +56 -1
- package/dist/runtime/src/application/submit-runtime-gateway-turn.ts +71 -0
- package/dist/runtime/src/application/turn-follow-ups.ts +7 -0
- package/dist/runtime/src/mcp/wiki-server.ts +8 -0
- package/dist/runtime/src/platform/mcp-config.ts +26 -0
- package/dist/runtime/src/prompts/builders.ts +5 -0
- package/dist/runtime/src/runtime/turn-runner.ts +65 -1
- package/dist/runtime/src/storage/api-topics.ts +29 -0
- package/dist/runtime/src/topics/agent-plugins/mcp.schema.json +120 -0
- package/dist/runtime/src/topics/agent-plugins/plugin.schema.json +65 -0
- package/dist/runtime/src/topics/plugin-server-instructions.ts +201 -0
- package/dist/runtime/src/topics/plugin-turn.ts +37 -0
- package/dist/runtime/src/topics/plugins.ts +445 -0
- package/dist/runtime/src/topics/update.ts +34 -0
- package/dist/runtime/src/types/api.ts +5 -0
- package/dist/runtime/src/version.ts +1 -1
- package/dist/storage.js +23 -2
- package/dist/storage.js.map +3 -3
- package/dist/types/packages/core/src/agents/mcp-tools/ask-user.d.ts +14 -0
- package/dist/types/packages/core/src/platform/mcp-config.d.ts +6 -0
- package/dist/types/packages/core/src/prompts/builders.d.ts +3 -0
- package/dist/types/packages/core/src/runtime/turn-runner.d.ts +14 -1
- package/dist/types/packages/core/src/storage/api-topics.d.ts +3 -0
- package/dist/types/packages/core/src/topics/plugin-server-instructions.d.ts +55 -0
- package/dist/types/packages/core/src/topics/plugin-turn.d.ts +17 -0
- package/dist/types/packages/core/src/topics/plugins.d.ts +66 -0
- package/dist/types/packages/core/src/types/api.d.ts +5 -0
- package/dist/types/packages/core/src/version.d.ts +1 -1
- package/package.json +1 -1
- /package/dist/{chunk-m1ngkjdd.js.map → chunk-hn2cqdd0.js.map} +0 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json",
|
|
4
|
+
"title": "Agent Plugins Manifest",
|
|
5
|
+
"description": "Machine-readable schema for plugin.json in Agent Plugins 1.0.0. The Agent Plugins specification defines additional semantic and operational requirements.",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"properties": {
|
|
8
|
+
"$schema": {
|
|
9
|
+
"const": "https://agent-plugins.org/schemas/1.0.0/plugin.schema.json",
|
|
10
|
+
"description": "Canonical identifier of the plugin manifest schema for the Agent Plugins version targeted by this document."
|
|
11
|
+
},
|
|
12
|
+
"name": {
|
|
13
|
+
"type": "string",
|
|
14
|
+
"minLength": 1,
|
|
15
|
+
"maxLength": 64,
|
|
16
|
+
"pattern": "^(?!.*(?:--|\\.\\.))[a-z0-9](?:[a-z0-9.-]*[a-z0-9])?$",
|
|
17
|
+
"description": "Human-readable plugin name."
|
|
18
|
+
},
|
|
19
|
+
"version": {
|
|
20
|
+
"type": "string"
|
|
21
|
+
},
|
|
22
|
+
"description": {
|
|
23
|
+
"type": "string"
|
|
24
|
+
},
|
|
25
|
+
"author": {
|
|
26
|
+
"type": "object",
|
|
27
|
+
"properties": {
|
|
28
|
+
"name": {
|
|
29
|
+
"type": "string"
|
|
30
|
+
},
|
|
31
|
+
"email": {
|
|
32
|
+
"type": "string"
|
|
33
|
+
},
|
|
34
|
+
"url": {
|
|
35
|
+
"type": "string"
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"additionalProperties": false
|
|
39
|
+
},
|
|
40
|
+
"homepage": {
|
|
41
|
+
"type": "string"
|
|
42
|
+
},
|
|
43
|
+
"repository": {
|
|
44
|
+
"type": "string"
|
|
45
|
+
},
|
|
46
|
+
"license": {
|
|
47
|
+
"type": "string"
|
|
48
|
+
},
|
|
49
|
+
"keywords": {
|
|
50
|
+
"type": "array",
|
|
51
|
+
"items": {
|
|
52
|
+
"type": "string"
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
"extensions": {
|
|
56
|
+
"type": "object",
|
|
57
|
+
"description": "Client-specific manifest data keyed by reverse-domain extension namespace. Agent Plugins assigns no semantics to namespace object contents.",
|
|
58
|
+
"additionalProperties": {
|
|
59
|
+
"type": "object"
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
"required": ["$schema", "name"],
|
|
64
|
+
"additionalProperties": false
|
|
65
|
+
}
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `instructions` a room's plugin MCP servers return from `initialize`,
|
|
3
|
+
* shown to the model the same way on Claude, Codex and Maestro.
|
|
4
|
+
*
|
|
5
|
+
* The engines do not agree on their own. Claude Code puts each connected
|
|
6
|
+
* server's instructions in a reminder of its own, cut at 2048 characters.
|
|
7
|
+
* Codex shows them as tool metadata only, and only for some models. Maestro
|
|
8
|
+
* never reads them. So the node reads them itself and puts the same text in
|
|
9
|
+
* every engine's system prompt; a Claude turn may see a server's text twice.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
13
|
+
import { SSEClientTransport } from "@modelcontextprotocol/sdk/client/sse.js";
|
|
14
|
+
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
|
|
15
|
+
import type { HostMcpServerSpec } from "#runtime-gateway";
|
|
16
|
+
|
|
17
|
+
/** How long one server gets to answer `initialize`. */
|
|
18
|
+
export const SERVER_INSTRUCTIONS_TIMEOUT_MS = 3000;
|
|
19
|
+
/** How long a server's answer is reused before it is read again. */
|
|
20
|
+
export const SERVER_INSTRUCTIONS_TTL_MS = 5 * 60 * 1000;
|
|
21
|
+
/** The most UTF-8 bytes of one server's text the prompt carries. */
|
|
22
|
+
export const SERVER_INSTRUCTIONS_MAX_BYTES = 8 * 1024;
|
|
23
|
+
/** The most UTF-8 bytes the whole section carries. */
|
|
24
|
+
export const INSTRUCTIONS_SECTION_MAX_BYTES = 32 * 1024;
|
|
25
|
+
|
|
26
|
+
export interface McpServerInstructions {
|
|
27
|
+
server: string;
|
|
28
|
+
/** As the server sent it. */
|
|
29
|
+
text: string;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface FetchedServerInstructions {
|
|
33
|
+
/** Servers that returned non-empty instructions, in the order given. */
|
|
34
|
+
instructions: McpServerInstructions[];
|
|
35
|
+
/** Servers that could not be read in time, each with why. */
|
|
36
|
+
problems: string[];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export interface FetchInstructionsOptions {
|
|
40
|
+
timeoutMs?: number;
|
|
41
|
+
/** The turn's stop: every read in flight ends when it fires. */
|
|
42
|
+
signal?: AbortSignal;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const cache = new Map<string, { text: string; expires: number }>();
|
|
46
|
+
|
|
47
|
+
/** A server's answer is reused only for the same name, url and headers. */
|
|
48
|
+
function cacheKey(server: string, spec: HostMcpServerSpec): string {
|
|
49
|
+
const headers = Object.entries(spec.headers ?? {})
|
|
50
|
+
.map(([name, value]) => [name.toLowerCase(), value])
|
|
51
|
+
.sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0));
|
|
52
|
+
return JSON.stringify([server, spec.type, spec.url, headers]);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** Forget every cached answer. */
|
|
56
|
+
export function clearMcpServerInstructionsCache(): void {
|
|
57
|
+
cache.clear();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function transportFor(spec: HostMcpServerSpec, signal: AbortSignal) {
|
|
61
|
+
const url = new URL(spec.url);
|
|
62
|
+
const requestInit = spec.headers ? { headers: spec.headers } : undefined;
|
|
63
|
+
// A redirect would carry the server's headers, often a credential, to
|
|
64
|
+
// another url, so it fails the read instead.
|
|
65
|
+
const noRedirect = (input: string | URL, init?: RequestInit) =>
|
|
66
|
+
fetch(input, {
|
|
67
|
+
...init,
|
|
68
|
+
redirect: "error",
|
|
69
|
+
signal: init?.signal ? AbortSignal.any([init.signal, signal]) : signal,
|
|
70
|
+
});
|
|
71
|
+
return spec.type === "sse"
|
|
72
|
+
? new SSEClientTransport(url, { requestInit, fetch: noRedirect })
|
|
73
|
+
: new StreamableHTTPClientTransport(url, { requestInit, fetch: noRedirect });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
async function readInstructions(
|
|
77
|
+
spec: HostMcpServerSpec,
|
|
78
|
+
timeoutMs: number,
|
|
79
|
+
stop: AbortSignal | undefined,
|
|
80
|
+
): Promise<string> {
|
|
81
|
+
const ended = new AbortController();
|
|
82
|
+
const timer = setTimeout(() => ended.abort(new Error(`no answer in ${timeoutMs} ms`)), timeoutMs);
|
|
83
|
+
timer.unref?.();
|
|
84
|
+
const onStop = () => ended.abort(new Error("the turn was stopped"));
|
|
85
|
+
if (stop?.aborted) onStop();
|
|
86
|
+
else stop?.addEventListener("abort", onStop, { once: true });
|
|
87
|
+
const client = new Client({ name: "negotium-plugin-instructions", version: "1.0.0" });
|
|
88
|
+
const transport = transportFor(spec, ended.signal);
|
|
89
|
+
try {
|
|
90
|
+
return await Promise.race([
|
|
91
|
+
client.connect(transport).then(() => client.getInstructions() ?? ""),
|
|
92
|
+
new Promise<never>((_, reject) => {
|
|
93
|
+
if (ended.signal.aborted) reject(ended.signal.reason);
|
|
94
|
+
ended.signal.addEventListener("abort", () => reject(ended.signal.reason), { once: true });
|
|
95
|
+
}),
|
|
96
|
+
]);
|
|
97
|
+
} finally {
|
|
98
|
+
clearTimeout(timer);
|
|
99
|
+
stop?.removeEventListener("abort", onStop);
|
|
100
|
+
// End the server's session when it opened one; the turn does not wait.
|
|
101
|
+
const ending =
|
|
102
|
+
!ended.signal.aborted && transport instanceof StreamableHTTPClientTransport
|
|
103
|
+
? transport.terminateSession()
|
|
104
|
+
: Promise.resolve();
|
|
105
|
+
void ending.catch(() => {}).finally(() => client.close().catch(() => {}));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Read every server's `instructions` at once, reusing an answer younger than
|
|
111
|
+
* `SERVER_INSTRUCTIONS_TTL_MS`; never throws. A failed read is not cached.
|
|
112
|
+
*/
|
|
113
|
+
export async function fetchMcpServerInstructions(
|
|
114
|
+
servers: Readonly<Record<string, HostMcpServerSpec>>,
|
|
115
|
+
options: FetchInstructionsOptions = {},
|
|
116
|
+
): Promise<FetchedServerInstructions> {
|
|
117
|
+
const timeoutMs = options.timeoutMs ?? SERVER_INSTRUCTIONS_TIMEOUT_MS;
|
|
118
|
+
const entries = Object.entries(servers);
|
|
119
|
+
const results = await Promise.allSettled(
|
|
120
|
+
entries.map(async ([server, spec]) => {
|
|
121
|
+
const key = cacheKey(server, spec);
|
|
122
|
+
const hit = cache.get(key);
|
|
123
|
+
if (hit && hit.expires > Date.now()) return hit.text;
|
|
124
|
+
const text = await readInstructions(spec, timeoutMs, options.signal);
|
|
125
|
+
cache.set(key, { text, expires: Date.now() + SERVER_INSTRUCTIONS_TTL_MS });
|
|
126
|
+
return text;
|
|
127
|
+
}),
|
|
128
|
+
);
|
|
129
|
+
const fetched: FetchedServerInstructions = { instructions: [], problems: [] };
|
|
130
|
+
results.forEach((result, index) => {
|
|
131
|
+
const server = entries[index][0];
|
|
132
|
+
if (result.status === "rejected") {
|
|
133
|
+
const reason = result.reason instanceof Error ? result.reason.message : String(result.reason);
|
|
134
|
+
fetched.problems.push(`plugin server "${server}": instructions not read (${reason})`);
|
|
135
|
+
} else if (result.value.trim()) {
|
|
136
|
+
fetched.instructions.push({ server, text: result.value });
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return fetched;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const encoder = new TextEncoder();
|
|
143
|
+
const byteLength = (text: string) => encoder.encode(text).length;
|
|
144
|
+
|
|
145
|
+
/** The longest start of `text` within `maxBytes` UTF-8 bytes, never splitting a character. */
|
|
146
|
+
function cutToBytes(text: string, maxBytes: number): string {
|
|
147
|
+
const encoded = encoder.encode(text);
|
|
148
|
+
let end = Math.max(0, maxBytes);
|
|
149
|
+
while (end > 0 && (encoded[end] & 0xc0) === 0x80) end--;
|
|
150
|
+
return new TextDecoder().decode(encoded.subarray(0, end));
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
const cutMarker = (server: string, sent: number) =>
|
|
154
|
+
`\n\n[negotium cut the instructions of "${server}" here; the server sent ${sent} bytes]`;
|
|
155
|
+
|
|
156
|
+
export interface RenderedServerInstructions {
|
|
157
|
+
/** The system prompt section; empty when no server's text is shown. */
|
|
158
|
+
section: string;
|
|
159
|
+
/** Servers whose text was cut or left out for size. */
|
|
160
|
+
problems: string[];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* The system prompt section for the servers' instructions, the same text for
|
|
165
|
+
* every engine. A server's text is cut at `SERVER_INSTRUCTIONS_MAX_BYTES` and
|
|
166
|
+
* the section at `INSTRUCTIONS_SECTION_MAX_BYTES`, each cut marked in the
|
|
167
|
+
* text; servers past the section's limit are left out.
|
|
168
|
+
*/
|
|
169
|
+
export function renderMcpServerInstructions(
|
|
170
|
+
instructions: readonly McpServerInstructions[],
|
|
171
|
+
): RenderedServerInstructions {
|
|
172
|
+
const head = [
|
|
173
|
+
"## MCP Server Instructions",
|
|
174
|
+
"",
|
|
175
|
+
"The following MCP servers have provided instructions for how to use their tools and resources:",
|
|
176
|
+
].join("\n");
|
|
177
|
+
const blocks: string[] = [];
|
|
178
|
+
const problems: string[] = [];
|
|
179
|
+
let left = INSTRUCTIONS_SECTION_MAX_BYTES - byteLength(head);
|
|
180
|
+
for (const { server, text: sent } of instructions) {
|
|
181
|
+
const heading = `\n\n### ${server}\n\n`;
|
|
182
|
+
const text = sent.trim();
|
|
183
|
+
const size = byteLength(text);
|
|
184
|
+
const room = Math.min(SERVER_INSTRUCTIONS_MAX_BYTES, left - byteLength(heading));
|
|
185
|
+
if (size <= room) {
|
|
186
|
+
blocks.push(`${heading}${text}`);
|
|
187
|
+
left -= byteLength(heading) + size;
|
|
188
|
+
continue;
|
|
189
|
+
}
|
|
190
|
+
const marker = cutMarker(server, size);
|
|
191
|
+
if (room <= byteLength(marker)) {
|
|
192
|
+
problems.push(`plugin server "${server}": instructions left out (the section is full)`);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
const shown = `${cutToBytes(text, room - byteLength(marker))}${marker}`;
|
|
196
|
+
blocks.push(`${heading}${shown}`);
|
|
197
|
+
left -= byteLength(heading) + byteLength(shown);
|
|
198
|
+
problems.push(`plugin server "${server}": instructions cut from ${size} bytes`);
|
|
199
|
+
}
|
|
200
|
+
return { section: blocks.length > 0 ? `${head}${blocks.join("")}` : "", problems };
|
|
201
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a room's plugins give one turn: the plugins whose skills the system
|
|
3
|
+
* prompt carries, and their root `mcp.json` servers. Turns fail closed: what
|
|
4
|
+
* does not load gives nothing, and servers whose names clash are left out.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { pluginMcpRefusal } from "#platform/mcp-config";
|
|
8
|
+
import type { HostMcpServerSpec } from "#runtime-gateway";
|
|
9
|
+
import { loadTopicPlugins, type TopicPlugin } from "#topics/plugins";
|
|
10
|
+
import type { TopicDto } from "#types/api";
|
|
11
|
+
|
|
12
|
+
export interface TopicPluginTurn {
|
|
13
|
+
plugins: readonly TopicPlugin[];
|
|
14
|
+
mcpServers: Record<string, HostMcpServerSpec>;
|
|
15
|
+
/** What the plugin folders reported while loading. */
|
|
16
|
+
loadProblems: readonly string[];
|
|
17
|
+
/** Why the plugins' servers were left out: a name that clashes. */
|
|
18
|
+
serverProblems: readonly string[];
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const NONE: TopicPluginTurn = { plugins: [], mcpServers: {}, loadProblems: [], serverProblems: [] };
|
|
22
|
+
|
|
23
|
+
export function resolveTopicPluginTurn(
|
|
24
|
+
topic: Pick<TopicDto, "kind" | "isSubagent" | "plugins">,
|
|
25
|
+
hostServerNames: readonly string[],
|
|
26
|
+
): TopicPluginTurn {
|
|
27
|
+
if (!topic.plugins?.length) return NONE;
|
|
28
|
+
const { plugins, problems: loadProblems } = loadTopicPlugins(topic.plugins);
|
|
29
|
+
const servers = Object.assign({}, ...plugins.map((plugin) => plugin.mcpServers));
|
|
30
|
+
const refusal = pluginMcpRefusal(topic, Object.keys(servers), hostServerNames);
|
|
31
|
+
return {
|
|
32
|
+
plugins,
|
|
33
|
+
mcpServers: refusal ? {} : servers,
|
|
34
|
+
loadProblems,
|
|
35
|
+
serverProblems: refusal ? [refusal] : [],
|
|
36
|
+
};
|
|
37
|
+
}
|