@vellumai/assistant 0.10.1-dev.202606240206.7c2bca6 → 0.10.1-dev.202606240317.ea25efe
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/package.json +1 -1
- package/src/__tests__/disk-pressure-guard.test.ts +41 -0
- package/src/__tests__/provider-usage-tracking.test.ts +39 -0
- package/src/__tests__/registry.test.ts +3 -0
- package/src/__tests__/workspace-tool-loader.test.ts +3 -0
- package/src/agent/loop-exclusive-tool.test.ts +150 -0
- package/src/agent/loop.ts +56 -0
- package/src/daemon/conversation.ts +4 -1
- package/src/daemon/disk-pressure-guard.ts +12 -2
- package/src/plugins/defaults/advisor/__tests__/consult.test.ts +44 -0
- package/src/plugins/defaults/advisor/__tests__/context-pack-gating.test.ts +106 -0
- package/src/plugins/defaults/advisor/__tests__/context-pack.test.ts +60 -0
- package/src/plugins/defaults/advisor/consult.ts +48 -6
- package/src/plugins/defaults/advisor/context-pack.ts +288 -0
- package/src/plugins/defaults/advisor/steering.ts +14 -2
- package/src/plugins/defaults/advisor/tools/advisor.ts +28 -5
- package/src/providers/anthropic/client.ts +5 -0
- package/src/providers/call-site-routing.ts +4 -0
- package/src/providers/openai/responses-provider.ts +5 -0
- package/src/providers/openrouter/client.ts +5 -0
- package/src/providers/provider-send-message.ts +4 -0
- package/src/providers/ratelimit.ts +4 -0
- package/src/providers/retry.ts +4 -0
- package/src/providers/types.ts +9 -0
- package/src/providers/usage-tracking.ts +4 -0
- package/src/tools/tool-defaults.ts +2 -0
- package/src/tools/types.ts +17 -2
|
@@ -16,7 +16,11 @@ import {
|
|
|
16
16
|
getConfiguredProvider,
|
|
17
17
|
userMessage,
|
|
18
18
|
} from "../../../providers/provider-send-message.js";
|
|
19
|
-
import type {
|
|
19
|
+
import type {
|
|
20
|
+
Message,
|
|
21
|
+
ProviderEvent,
|
|
22
|
+
ToolDefinition,
|
|
23
|
+
} from "../../../providers/types.js";
|
|
20
24
|
import { ADVISOR_CONFIG } from "./config.js";
|
|
21
25
|
import { advisorRequestText, buildAdvisorSystem } from "./steering.js";
|
|
22
26
|
import { toAdvisorMessages } from "./transcript.js";
|
|
@@ -26,6 +30,26 @@ import { toAdvisorMessages } from "./transcript.js";
|
|
|
26
30
|
// on via `llm.advisorProfile`, which we float above the call-site layers.
|
|
27
31
|
const ADVISOR_CALL_SITE: LLMCallSite = "advisor";
|
|
28
32
|
|
|
33
|
+
/**
|
|
34
|
+
* The single tool the consult may attach: a `web_search`-named tool that
|
|
35
|
+
* provider-native search (Anthropic/OpenAI) substitutes for its server-side
|
|
36
|
+
* web tool. Only passed when `provider.supportsNativeWebSearch` is true, so the
|
|
37
|
+
* provider runs the search itself and returns results inline — no agent loop,
|
|
38
|
+
* which keeps the consult a one-shot completion.
|
|
39
|
+
*/
|
|
40
|
+
const ADVISOR_WEB_SEARCH_TOOL: ToolDefinition = {
|
|
41
|
+
name: "web_search",
|
|
42
|
+
description:
|
|
43
|
+
"Search the web for current information to ground your guidance.",
|
|
44
|
+
input_schema: {
|
|
45
|
+
type: "object",
|
|
46
|
+
properties: {
|
|
47
|
+
query: { type: "string", description: "The search query." },
|
|
48
|
+
},
|
|
49
|
+
required: ["query"],
|
|
50
|
+
},
|
|
51
|
+
};
|
|
52
|
+
|
|
29
53
|
/**
|
|
30
54
|
* Resolve the routing override for the advisor consult. When the workspace has
|
|
31
55
|
* set `llm.advisorProfile`, force it above the call-site layers so it is
|
|
@@ -45,6 +69,13 @@ function advisorOverride(): {
|
|
|
45
69
|
export interface ConsultParams {
|
|
46
70
|
systemPrompt: string | null;
|
|
47
71
|
messages: ReadonlyArray<Message>;
|
|
72
|
+
/**
|
|
73
|
+
* The agent's runtime context (available tools and skills, workspace/project
|
|
74
|
+
* context, recalled memory), gathered by the tool from its `ToolContext`.
|
|
75
|
+
* Embedded in the advisor's system prompt so its advice is grounded in what
|
|
76
|
+
* the agent can actually do. Omitted/null when nothing could be gathered.
|
|
77
|
+
*/
|
|
78
|
+
runtimeContext?: string | null;
|
|
48
79
|
signal?: AbortSignal;
|
|
49
80
|
/**
|
|
50
81
|
* Optional sink for the advisor's generated text as it streams. Each call
|
|
@@ -81,14 +112,25 @@ export async function consultAdvisor(params: ConsultParams): Promise<string> {
|
|
|
81
112
|
}
|
|
82
113
|
|
|
83
114
|
// Append the consult instruction as the final user turn, then run a
|
|
84
|
-
//
|
|
85
|
-
//
|
|
86
|
-
//
|
|
115
|
+
// completion through the resolved provider. No `max_tokens` is set, so the
|
|
116
|
+
// resolver applies the profile's normal output budget rather than an
|
|
117
|
+
// advisor-specific cap.
|
|
87
118
|
const messages: Message[] = [...history, userMessage(advisorRequestText())];
|
|
88
119
|
|
|
120
|
+
// Give the advisor live web access when — and only when — the resolved
|
|
121
|
+
// provider runs search server-side (provider-native). Passing a `web_search`
|
|
122
|
+
// tool to a non-native provider would surface a client tool call this
|
|
123
|
+
// one-shot consult cannot execute, so we gate strictly on the capability and
|
|
124
|
+
// otherwise keep the consult tool-less.
|
|
125
|
+
const webEnabled = provider.supportsNativeWebSearch === true;
|
|
126
|
+
|
|
89
127
|
const { onText } = params;
|
|
90
128
|
const response = await provider.sendMessage(messages, {
|
|
91
|
-
systemPrompt: buildAdvisorSystem(
|
|
129
|
+
systemPrompt: buildAdvisorSystem(
|
|
130
|
+
params.systemPrompt,
|
|
131
|
+
params.runtimeContext,
|
|
132
|
+
),
|
|
133
|
+
...(webEnabled ? { tools: [ADVISOR_WEB_SEARCH_TOOL] } : {}),
|
|
92
134
|
// Forward the model's visible text deltas to the caller's sink so the
|
|
93
135
|
// guidance streams live. Other event types (thinking, usage) ride their
|
|
94
136
|
// own channels and are intentionally not surfaced as tool output.
|
|
@@ -100,7 +142,7 @@ export async function consultAdvisor(params: ConsultParams): Promise<string> {
|
|
|
100
142
|
config: {
|
|
101
143
|
callSite: ADVISOR_CALL_SITE,
|
|
102
144
|
...override,
|
|
103
|
-
tool_choice: { type: "none" },
|
|
145
|
+
tool_choice: webEnabled ? { type: "auto" } : { type: "none" },
|
|
104
146
|
},
|
|
105
147
|
signal: withTimeout(params.signal, ADVISOR_CONFIG.timeoutMs),
|
|
106
148
|
});
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Assemble the runtime context the advisor needs to make grounded
|
|
3
|
+
* recommendations — the same situational awareness the executing agent has:
|
|
4
|
+
* - the tools available to it this turn,
|
|
5
|
+
* - the skills it can load,
|
|
6
|
+
* - the loaded workspace / project context, NOW.md, PKB, and open documents,
|
|
7
|
+
* - and relevant memory pulled through the recall search.
|
|
8
|
+
*
|
|
9
|
+
* The advisor already receives the agent's transcript and system prompt; this
|
|
10
|
+
* adds the situational context that lives *outside* the prompt (tools and
|
|
11
|
+
* skills are passed to the model as a separate catalog, not inlined) plus a
|
|
12
|
+
* fresh, task-focused memory recall.
|
|
13
|
+
*
|
|
14
|
+
* Personal-memory surfaces are gated to the same policy the main agent's
|
|
15
|
+
* memory injectors apply: the recall search honors `canAccessMemory` (like the
|
|
16
|
+
* `recall` tool), and NOW.md / PKB honor `isPersonalMemoryAllowed` (plus the
|
|
17
|
+
* scratchpad-injection toggle for NOW.md). The advisor tool is low-risk and can
|
|
18
|
+
* run on remote/trusted-contact turns, so without these gates it could forward
|
|
19
|
+
* private content the main agent itself would not receive.
|
|
20
|
+
*
|
|
21
|
+
* Every section is best-effort: each source is wrapped so a failure or empty
|
|
22
|
+
* result drops just that section, never the consult. Daemon- and memory-side
|
|
23
|
+
* modules are pulled in via dynamic `import()` so this plugin module — loaded
|
|
24
|
+
* at bootstrap through `defaults/index.ts` — never forms a static import cycle
|
|
25
|
+
* with them. The result is a single string injected into the advisor's system
|
|
26
|
+
* prompt (see `buildAdvisorSystem`), or `null` when nothing could be gathered.
|
|
27
|
+
*/
|
|
28
|
+
|
|
29
|
+
import type { ChannelId } from "../../../channels/types.js";
|
|
30
|
+
import type { TrustContext } from "../../../daemon/trust-context.js";
|
|
31
|
+
import type { Message } from "../../../providers/types.js";
|
|
32
|
+
import type { TrustClass } from "../../../runtime/actor-trust-resolver.js";
|
|
33
|
+
|
|
34
|
+
export interface AdvisorContextSources {
|
|
35
|
+
conversationId: string;
|
|
36
|
+
workingDir: string;
|
|
37
|
+
/** The live tool set the executor sees this turn (`ToolContext.allowedToolNames`). */
|
|
38
|
+
allowedToolNames?: ReadonlySet<string>;
|
|
39
|
+
/**
|
|
40
|
+
* Trust class of the turn's actor, from the per-turn `ToolContext.trustClass`
|
|
41
|
+
* snapshot. Gates the memory recall and (with {@link sourceChannel}) the
|
|
42
|
+
* personal-memory surfaces.
|
|
43
|
+
*/
|
|
44
|
+
trustClass: TrustClass;
|
|
45
|
+
/**
|
|
46
|
+
* Channel the turn originates on, from the per-turn `ToolContext.executionChannel`
|
|
47
|
+
* snapshot. Combined with {@link trustClass} to evaluate personal-memory
|
|
48
|
+
* access exactly as the injectors do, off the same per-turn snapshot rather
|
|
49
|
+
* than the mutable live conversation trust.
|
|
50
|
+
*/
|
|
51
|
+
sourceChannel?: string;
|
|
52
|
+
/** The captured transcript, used to derive the recall query. */
|
|
53
|
+
transcript: ReadonlyArray<Message>;
|
|
54
|
+
signal?: AbortSignal;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Cap a block so the assembled context never balloons the consult prompt. */
|
|
58
|
+
function truncate(text: string, max: number): string {
|
|
59
|
+
const trimmed = text.trim();
|
|
60
|
+
return trimmed.length <= max ? trimmed : `${trimmed.slice(0, max)}…`;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/** First sentence (or a capped prefix) of a tool/skill description. */
|
|
64
|
+
function summarize(description: string | undefined, max = 160): string {
|
|
65
|
+
if (!description) return "";
|
|
66
|
+
const firstSentence = description.split(/(?<=[.!?])\s/)[0] ?? description;
|
|
67
|
+
return truncate(firstSentence, max);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** Pull the most recent user-authored text to seed the memory recall query. */
|
|
71
|
+
export function deriveRecallQuery(
|
|
72
|
+
transcript: ReadonlyArray<Message>,
|
|
73
|
+
): string | null {
|
|
74
|
+
for (let i = transcript.length - 1; i >= 0; i--) {
|
|
75
|
+
const message = transcript[i];
|
|
76
|
+
if (message.role !== "user") continue;
|
|
77
|
+
const text = message.content
|
|
78
|
+
.map((block) => (block.type === "text" ? block.text : ""))
|
|
79
|
+
.join(" ")
|
|
80
|
+
.trim();
|
|
81
|
+
if (text.length > 0) return truncate(text, 500);
|
|
82
|
+
}
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** `## Available tools` — the live tool set the agent can act with this turn. */
|
|
87
|
+
async function buildToolsSection(
|
|
88
|
+
allowedToolNames: ReadonlySet<string> | undefined,
|
|
89
|
+
): Promise<string | null> {
|
|
90
|
+
if (!allowedToolNames || allowedToolNames.size === 0) return null;
|
|
91
|
+
try {
|
|
92
|
+
const { getTool } = await import("../../../tools/registry.js");
|
|
93
|
+
const lines: string[] = [];
|
|
94
|
+
for (const name of [...allowedToolNames].sort()) {
|
|
95
|
+
// The advisor advises; it never recommends consulting itself.
|
|
96
|
+
if (name === "advisor") continue;
|
|
97
|
+
const summary = summarize(getTool(name)?.description);
|
|
98
|
+
lines.push(summary ? `- ${name} — ${summary}` : `- ${name}`);
|
|
99
|
+
}
|
|
100
|
+
if (lines.length === 0) return null;
|
|
101
|
+
return `## Available tools (what the agent can do)\n${lines.join("\n")}`;
|
|
102
|
+
} catch {
|
|
103
|
+
return null;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** `## Available skills` — the skills the agent can load via `skill_load`. */
|
|
108
|
+
async function buildSkillsSection(): Promise<string | null> {
|
|
109
|
+
try {
|
|
110
|
+
const { loadSkillCatalog } = await import("../../../config/skills.js");
|
|
111
|
+
const catalog = loadSkillCatalog();
|
|
112
|
+
if (catalog.length === 0) return null;
|
|
113
|
+
const lines = catalog.slice(0, 60).map((skill) => {
|
|
114
|
+
const summary = summarize(skill.description);
|
|
115
|
+
const when = skill.activationHints?.length
|
|
116
|
+
? ` (use when: ${truncate(skill.activationHints.join("; "), 120)})`
|
|
117
|
+
: "";
|
|
118
|
+
const label = skill.displayName || skill.name || skill.id;
|
|
119
|
+
return `- ${label} (${skill.id})${summary ? ` — ${summary}` : ""}${when}`;
|
|
120
|
+
});
|
|
121
|
+
const more =
|
|
122
|
+
catalog.length > 60 ? `\n- …and ${catalog.length - 60} more` : "";
|
|
123
|
+
return `## Available skills (load with skill_load)\n${lines.join("\n")}${more}`;
|
|
124
|
+
} catch {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Whether personal-memory surfaces (NOW.md, PKB) may be exposed to the advisor
|
|
131
|
+
* — the same `isPersonalMemoryAllowed` gate the runtime memory injectors apply.
|
|
132
|
+
*
|
|
133
|
+
* Derived from the per-turn trust snapshot (`ToolContext.trustClass` /
|
|
134
|
+
* `executionChannel`, threaded in via {@link AdvisorContextSources}), NOT the
|
|
135
|
+
* live `findConversation().trustContext`: that conversation state is mutable
|
|
136
|
+
* and a concurrent guardian/meta command could flip it to guardian mid-flight,
|
|
137
|
+
* granting a remote/non-guardian turn access its own snapshot was never given.
|
|
138
|
+
* Fail-closed: if the gate can't be resolved, returns false.
|
|
139
|
+
*/
|
|
140
|
+
async function personalMemoryAllowedForAdvisor(
|
|
141
|
+
trustClass: TrustClass,
|
|
142
|
+
sourceChannel: string | undefined,
|
|
143
|
+
): Promise<boolean> {
|
|
144
|
+
try {
|
|
145
|
+
const { isPersonalMemoryAllowed } =
|
|
146
|
+
await import("../../../daemon/trust-context.js");
|
|
147
|
+
// `isPersonalMemoryAllowed` reads only `sourceChannel` + `trustClass`; build
|
|
148
|
+
// a minimal trust context from the per-turn snapshot. The channel may be
|
|
149
|
+
// absent (local/internal turns), which the gate treats as non-remote.
|
|
150
|
+
const snapshot = {
|
|
151
|
+
sourceChannel: sourceChannel as ChannelId | undefined,
|
|
152
|
+
trustClass,
|
|
153
|
+
} as TrustContext;
|
|
154
|
+
return isPersonalMemoryAllowed(snapshot);
|
|
155
|
+
} catch {
|
|
156
|
+
return false;
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** `## Workspace & project context` — the loaded environment around the agent. */
|
|
161
|
+
async function buildWorkspaceSection(
|
|
162
|
+
sources: AdvisorContextSources,
|
|
163
|
+
): Promise<string | null> {
|
|
164
|
+
const { conversationId } = sources;
|
|
165
|
+
const parts: string[] = [];
|
|
166
|
+
|
|
167
|
+
// The `<workspace>` directory listing is not personal memory — the agent's
|
|
168
|
+
// own file tools already operate in this cwd — so it is surfaced ungated, the
|
|
169
|
+
// same way the workspace-context injector does.
|
|
170
|
+
try {
|
|
171
|
+
const { resolveWorkspaceTopLevelContext } =
|
|
172
|
+
await import("../../../daemon/conversation-workspace.js");
|
|
173
|
+
const workspace = resolveWorkspaceTopLevelContext(conversationId);
|
|
174
|
+
if (workspace) parts.push(truncate(workspace, 2500));
|
|
175
|
+
} catch {
|
|
176
|
+
/* best-effort */
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// NOW.md and PKB are personal-memory surfaces. Gate them behind the same
|
|
180
|
+
// `isPersonalMemoryAllowed` policy (and, for NOW.md, the scratchpad-injection
|
|
181
|
+
// toggle) the runtime injectors use, evaluated off the per-turn trust
|
|
182
|
+
// snapshot, so a low-risk advisor consult cannot forward private content the
|
|
183
|
+
// main agent would never receive.
|
|
184
|
+
if (
|
|
185
|
+
await personalMemoryAllowedForAdvisor(
|
|
186
|
+
sources.trustClass,
|
|
187
|
+
sources.sourceChannel,
|
|
188
|
+
)
|
|
189
|
+
) {
|
|
190
|
+
try {
|
|
191
|
+
const [{ readNowScratchpad }, { getConfig }] = await Promise.all([
|
|
192
|
+
import("../../../daemon/now-scratchpad.js"),
|
|
193
|
+
import("../../../config/loader.js"),
|
|
194
|
+
]);
|
|
195
|
+
if (getConfig().memory.retrieval.scratchpadInjection.enabled) {
|
|
196
|
+
const now = readNowScratchpad();
|
|
197
|
+
if (now) parts.push(`NOW.md scratchpad:\n${truncate(now, 1500)}`);
|
|
198
|
+
}
|
|
199
|
+
} catch {
|
|
200
|
+
/* best-effort */
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
try {
|
|
204
|
+
const { readPkbContext } = await import("../../../memory/pkb/context.js");
|
|
205
|
+
const pkb = readPkbContext();
|
|
206
|
+
if (pkb) parts.push(truncate(pkb, 1500));
|
|
207
|
+
} catch {
|
|
208
|
+
/* best-effort */
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
const { buildActiveDocuments } =
|
|
214
|
+
await import("../../../daemon/conversation-runtime-assembly.js");
|
|
215
|
+
const docs = buildActiveDocuments(conversationId);
|
|
216
|
+
if (docs && docs.length > 0) {
|
|
217
|
+
const titles = docs
|
|
218
|
+
.slice(0, 20)
|
|
219
|
+
.map((doc) => `- ${doc.title} (${doc.wordCount} words)`)
|
|
220
|
+
.join("\n");
|
|
221
|
+
parts.push(`Open documents:\n${titles}`);
|
|
222
|
+
}
|
|
223
|
+
} catch {
|
|
224
|
+
/* best-effort */
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
if (parts.length === 0) return null;
|
|
228
|
+
return `## Workspace & project context\n${parts.join("\n\n")}`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/** `## Relevant memory (recall)` — a fresh, task-focused recall search. */
|
|
232
|
+
async function buildMemorySection(
|
|
233
|
+
sources: AdvisorContextSources,
|
|
234
|
+
): Promise<string | null> {
|
|
235
|
+
try {
|
|
236
|
+
const { resolveCapabilities } =
|
|
237
|
+
await import("../../../runtime/capabilities.js");
|
|
238
|
+
// Recall reads sensitive local context; honor the same trust gate the
|
|
239
|
+
// `recall` tool applies. Non-guardian turns get no fresh recall here.
|
|
240
|
+
if (!resolveCapabilities(sources.trustClass).canAccessMemory) return null;
|
|
241
|
+
|
|
242
|
+
const query = deriveRecallQuery(sources.transcript);
|
|
243
|
+
if (!query) return null;
|
|
244
|
+
|
|
245
|
+
const [{ runDeterministicRecallSearch }, { getConfig }] = await Promise.all(
|
|
246
|
+
[
|
|
247
|
+
import("../../../memory/context-search/search.js"),
|
|
248
|
+
import("../../../config/loader.js"),
|
|
249
|
+
],
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const { evidence } = await runDeterministicRecallSearch(
|
|
253
|
+
{ query, max_results: 8 },
|
|
254
|
+
{
|
|
255
|
+
workingDir: sources.workingDir,
|
|
256
|
+
conversationId: sources.conversationId,
|
|
257
|
+
config: getConfig(),
|
|
258
|
+
signal: sources.signal,
|
|
259
|
+
},
|
|
260
|
+
);
|
|
261
|
+
if (evidence.length === 0) return null;
|
|
262
|
+
|
|
263
|
+
const lines = evidence.slice(0, 8).map((item) => {
|
|
264
|
+
const excerpt = truncate(item.excerpt, 220);
|
|
265
|
+
return `- [${item.source}] ${item.title} (${item.locator}): ${excerpt}`;
|
|
266
|
+
});
|
|
267
|
+
return `## Relevant memory (recall: "${truncate(query, 120)}")\n${lines.join("\n")}`;
|
|
268
|
+
} catch {
|
|
269
|
+
return null;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Gather the advisor's runtime context block, or `null` if nothing is
|
|
275
|
+
* available. Sections run concurrently; each is independently best-effort.
|
|
276
|
+
*/
|
|
277
|
+
export async function buildAdvisorContext(
|
|
278
|
+
sources: AdvisorContextSources,
|
|
279
|
+
): Promise<string | null> {
|
|
280
|
+
const sections = await Promise.all([
|
|
281
|
+
buildToolsSection(sources.allowedToolNames),
|
|
282
|
+
buildSkillsSection(),
|
|
283
|
+
buildWorkspaceSection(sources),
|
|
284
|
+
buildMemorySection(sources),
|
|
285
|
+
]);
|
|
286
|
+
const present = sections.filter((s): s is string => s !== null);
|
|
287
|
+
return present.length > 0 ? present.join("\n\n") : null;
|
|
288
|
+
}
|
|
@@ -33,9 +33,15 @@ export function stripSteering(systemPrompt: string | null): string | null {
|
|
|
33
33
|
* System prompt for the advisor sub-call. Frames the advisor's role and, for
|
|
34
34
|
* context, quotes the executor's own system prompt (as the advisor tool does —
|
|
35
35
|
* the advisor sees the system prompt as context about the executor's task).
|
|
36
|
+
*
|
|
37
|
+
* `runtimeContext`, when present, carries the agent's situational context that
|
|
38
|
+
* lives outside its system prompt — available tools and skills, workspace /
|
|
39
|
+
* project context, and recalled memory (see `buildAdvisorContext`) — so the
|
|
40
|
+
* advisor can ground its recommendations in what the agent can actually do.
|
|
36
41
|
*/
|
|
37
42
|
export function buildAdvisorSystem(
|
|
38
43
|
originalSystemPrompt: string | null,
|
|
44
|
+
runtimeContext?: string | null,
|
|
39
45
|
): string {
|
|
40
46
|
const base = `You are a senior advisor consulted by another AI agent working on a task — most often at the planning stage, before it starts building, but sometimes partway through. The entire conversation above is the agent's working context: its task or goal, every tool call it has made, and every result it has seen. The agent has paused to consult you because you bring a second, independent perspective it cannot get from inside its own reasoning loop. Your job is to maximize its odds of completing the task correctly and efficiently.
|
|
41
47
|
|
|
@@ -54,8 +60,14 @@ How to advise:
|
|
|
54
60
|
- Stay in your lane. Advise the agent; do not role-play as it, write its final deliverable, or take its next action for it. If the agent is already on the right track, confirm it and sharpen the plan rather than manufacturing objections.
|
|
55
61
|
|
|
56
62
|
Write as much as the guidance genuinely needs, and no more.`;
|
|
57
|
-
|
|
58
|
-
|
|
63
|
+
let prompt = base;
|
|
64
|
+
if (originalSystemPrompt) {
|
|
65
|
+
prompt += `\n\nFor context, the agent is operating under this system prompt:\n<agent_system_prompt>\n${originalSystemPrompt}\n</agent_system_prompt>`;
|
|
66
|
+
}
|
|
67
|
+
if (runtimeContext) {
|
|
68
|
+
prompt += `\n\nThe agent's runtime context — the tools and skills available to it, the loaded workspace/project context, and relevant memory — follows. Ground your recommendations in what the agent can actually do and what is around it; reference specific tools, skills, files, or memory where relevant.\n<agent_runtime_context>\n${runtimeContext}\n</agent_runtime_context>`;
|
|
69
|
+
}
|
|
70
|
+
return prompt;
|
|
59
71
|
}
|
|
60
72
|
|
|
61
73
|
/**
|
|
@@ -19,19 +19,26 @@ import { RiskLevel } from "@vellumai/plugin-api";
|
|
|
19
19
|
import { advisorEnabledForProfile } from "../advisor-gate.js";
|
|
20
20
|
import { getCapture } from "../advisor-state-store.js";
|
|
21
21
|
import { consultAdvisor } from "../consult.js";
|
|
22
|
+
import { buildAdvisorContext } from "../context-pack.js";
|
|
22
23
|
|
|
23
24
|
const advisorTool: ToolDefinition = {
|
|
24
25
|
name: "advisor",
|
|
25
26
|
description:
|
|
26
27
|
"Consult a stronger advisor model to shape your plan and get strategic guidance. " +
|
|
27
28
|
"Takes NO parameters — your full conversation (the task, every tool call, and every " +
|
|
28
|
-
"result) is forwarded automatically
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
29
|
+
"result) is forwarded automatically, along with your available tools and skills, the " +
|
|
30
|
+
"workspace/project context, and relevant memory. Call it BEFORE you start building: it " +
|
|
31
|
+
"can lay out a plan when you don't have one yet, or review and sharpen the plan you've " +
|
|
32
|
+
"already drafted. Also call it when you're stuck, when weighing a change in approach, and " +
|
|
33
|
+
"once before declaring a task complete. It runs on its own — if you call it alongside " +
|
|
34
|
+
"other tools, those are held back until you've seen its guidance. Give its guidance " +
|
|
35
|
+
"serious weight.",
|
|
32
36
|
input_schema: { type: "object", properties: {}, additionalProperties: false },
|
|
33
37
|
// Read-only advice; low risk so the consult isn't gated behind a prompt.
|
|
34
38
|
defaultRiskLevel: RiskLevel.Low,
|
|
39
|
+
// Runs alone in its turn: the loop defers any sibling tool calls so the model
|
|
40
|
+
// incorporates the advisor's guidance before acting on anything else.
|
|
41
|
+
exclusive: true,
|
|
35
42
|
async execute(
|
|
36
43
|
_input: Record<string, unknown>,
|
|
37
44
|
ctx: ToolContext,
|
|
@@ -48,9 +55,25 @@ const advisorTool: ToolDefinition = {
|
|
|
48
55
|
}
|
|
49
56
|
try {
|
|
50
57
|
const capture = getCapture(ctx.conversationId);
|
|
58
|
+
const messages = capture?.messages ?? [];
|
|
59
|
+
// Gather the agent's situational context (tools, skills, workspace,
|
|
60
|
+
// memory) so the advisor reasons with the same awareness the agent has.
|
|
61
|
+
// Best-effort: a failure here must not block the consult.
|
|
62
|
+
const runtimeContext = await buildAdvisorContext({
|
|
63
|
+
conversationId: ctx.conversationId,
|
|
64
|
+
workingDir: ctx.workingDir,
|
|
65
|
+
allowedToolNames: ctx.allowedToolNames,
|
|
66
|
+
// Per-turn trust snapshot — gates personal-memory surfaces off the same
|
|
67
|
+
// values the executor captured for this invocation, not live state.
|
|
68
|
+
trustClass: ctx.trustClass,
|
|
69
|
+
sourceChannel: ctx.executionChannel,
|
|
70
|
+
transcript: messages,
|
|
71
|
+
signal: ctx.signal,
|
|
72
|
+
}).catch(() => null);
|
|
51
73
|
const advice = await consultAdvisor({
|
|
52
74
|
systemPrompt: capture?.systemPrompt ?? null,
|
|
53
|
-
messages
|
|
75
|
+
messages,
|
|
76
|
+
runtimeContext,
|
|
54
77
|
signal: ctx.signal,
|
|
55
78
|
// Stream the advisor's guidance live as it generates: each delta is
|
|
56
79
|
// surfaced as a `tool_output_chunk` for this tool call and rendered in
|
|
@@ -786,6 +786,11 @@ export class AnthropicProvider implements Provider {
|
|
|
786
786
|
this.useNativeWebSearch = options.useNativeWebSearch ?? false;
|
|
787
787
|
}
|
|
788
788
|
|
|
789
|
+
/** See {@link Provider.supportsNativeWebSearch}. */
|
|
790
|
+
get supportsNativeWebSearch(): boolean {
|
|
791
|
+
return this.useNativeWebSearch;
|
|
792
|
+
}
|
|
793
|
+
|
|
789
794
|
async sendMessage(
|
|
790
795
|
messages: Message[],
|
|
791
796
|
options?: SendMessageOptions,
|
|
@@ -43,6 +43,9 @@ import type {
|
|
|
43
43
|
|
|
44
44
|
export class CallSiteRoutingProvider implements Provider {
|
|
45
45
|
public readonly tokenEstimationProvider?: string;
|
|
46
|
+
// Forward native web-search capability so it survives the wrapper chain
|
|
47
|
+
// (callers like the advisor consult gate on it). Fixed at construction.
|
|
48
|
+
public readonly supportsNativeWebSearch?: boolean;
|
|
46
49
|
|
|
47
50
|
// Per-call async context that tracks which provider is currently executing.
|
|
48
51
|
// Using AsyncLocalStorage instead of a plain instance field means concurrent
|
|
@@ -94,6 +97,7 @@ export class CallSiteRoutingProvider implements Provider {
|
|
|
94
97
|
) => Promise<Provider | null>,
|
|
95
98
|
) {
|
|
96
99
|
this.tokenEstimationProvider = defaultProvider.tokenEstimationProvider;
|
|
100
|
+
this.supportsNativeWebSearch = defaultProvider.supportsNativeWebSearch;
|
|
97
101
|
if (defaultProvider.countInputTokens) {
|
|
98
102
|
this.countInputTokens =
|
|
99
103
|
defaultProvider.countInputTokens.bind(defaultProvider);
|
|
@@ -194,6 +194,11 @@ export class OpenAIResponsesProvider implements Provider {
|
|
|
194
194
|
this.useNativeWebSearch = options.useNativeWebSearch ?? false;
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
+
/** See {@link Provider.supportsNativeWebSearch}. */
|
|
198
|
+
get supportsNativeWebSearch(): boolean {
|
|
199
|
+
return this.useNativeWebSearch;
|
|
200
|
+
}
|
|
201
|
+
|
|
197
202
|
async sendMessage(
|
|
198
203
|
messages: Message[],
|
|
199
204
|
options?: SendMessageOptions,
|
|
@@ -140,6 +140,11 @@ export class OpenRouterProvider extends OpenAIChatCompletionsProvider {
|
|
|
140
140
|
return isAnthropicModel(this.defaultModel) ? "anthropic" : this.name;
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
+
/** See {@link Provider.supportsNativeWebSearch}. Set per model at construction. */
|
|
144
|
+
get supportsNativeWebSearch(): boolean {
|
|
145
|
+
return this.useNativeWebSearch;
|
|
146
|
+
}
|
|
147
|
+
|
|
143
148
|
override async sendMessage(
|
|
144
149
|
messages: Message[],
|
|
145
150
|
options?: SendMessageOptions,
|
|
@@ -43,6 +43,9 @@ let lazyInitPromise: Promise<void> | null = null;
|
|
|
43
43
|
export class CallSiteConfiguredProvider implements Provider {
|
|
44
44
|
public readonly name: string;
|
|
45
45
|
public readonly tokenEstimationProvider?: string;
|
|
46
|
+
// Forward native web-search capability so it survives the wrapper chain
|
|
47
|
+
// (callers like the advisor consult gate on it). Fixed at construction.
|
|
48
|
+
public readonly supportsNativeWebSearch?: boolean;
|
|
46
49
|
|
|
47
50
|
constructor(
|
|
48
51
|
private readonly inner: Provider,
|
|
@@ -52,6 +55,7 @@ export class CallSiteConfiguredProvider implements Provider {
|
|
|
52
55
|
) {
|
|
53
56
|
this.name = inner.name;
|
|
54
57
|
this.tokenEstimationProvider = inner.tokenEstimationProvider;
|
|
58
|
+
this.supportsNativeWebSearch = inner.supportsNativeWebSearch;
|
|
55
59
|
}
|
|
56
60
|
|
|
57
61
|
sendMessage(
|
|
@@ -23,6 +23,10 @@ export class RateLimitProvider implements Provider {
|
|
|
23
23
|
return this.inner.tokenEstimationProvider;
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
get supportsNativeWebSearch(): boolean | undefined {
|
|
27
|
+
return this.inner.supportsNativeWebSearch;
|
|
28
|
+
}
|
|
29
|
+
|
|
26
30
|
private requestTimestamps: number[];
|
|
27
31
|
|
|
28
32
|
// Forward the optional token-counting endpoint so the capability survives
|
package/src/providers/retry.ts
CHANGED
|
@@ -618,6 +618,10 @@ export class RetryProvider implements Provider {
|
|
|
618
618
|
return this.inner.tokenEstimationProvider;
|
|
619
619
|
}
|
|
620
620
|
|
|
621
|
+
get supportsNativeWebSearch(): boolean | undefined {
|
|
622
|
+
return this.inner.supportsNativeWebSearch;
|
|
623
|
+
}
|
|
624
|
+
|
|
621
625
|
// Forward the optional token-counting endpoint so the capability survives
|
|
622
626
|
// the wrapper chain (callers gate on its presence). Bound straight to the
|
|
623
627
|
// inner provider — count_tokens is a cheap separate endpoint and its caller
|
package/src/providers/types.ts
CHANGED
|
@@ -265,6 +265,15 @@ export interface Provider {
|
|
|
265
265
|
* Falls back to `name` when unset.
|
|
266
266
|
*/
|
|
267
267
|
tokenEstimationProvider?: string;
|
|
268
|
+
/**
|
|
269
|
+
* True when this provider instance was constructed to run web search
|
|
270
|
+
* server-side (provider-native). The native search only activates when a
|
|
271
|
+
* `web_search`-named tool is passed in the request, so callers that want to
|
|
272
|
+
* enable web search on a one-shot completion (e.g. the advisor consult) check
|
|
273
|
+
* this first — passing the tool to a non-native instance would surface an
|
|
274
|
+
* unexecutable client tool call. Absent/false on providers without it.
|
|
275
|
+
*/
|
|
276
|
+
supportsNativeWebSearch?: boolean;
|
|
268
277
|
sendMessage(
|
|
269
278
|
messages: Message[],
|
|
270
279
|
options?: SendMessageOptions,
|
|
@@ -18,6 +18,9 @@ const log = getLogger("provider-usage-tracking");
|
|
|
18
18
|
export class UsageTrackingProvider implements Provider {
|
|
19
19
|
public readonly name: string;
|
|
20
20
|
public readonly tokenEstimationProvider?: string;
|
|
21
|
+
// Forward native web-search capability so it survives the wrapper chain
|
|
22
|
+
// (callers like the advisor consult gate on it). Fixed at construction.
|
|
23
|
+
public readonly supportsNativeWebSearch?: boolean;
|
|
21
24
|
// Forward the optional token-counting endpoint so the capability survives
|
|
22
25
|
// the wrapper chain. Bound straight to the inner provider — count_tokens is
|
|
23
26
|
// not billed, so there's no usage to track.
|
|
@@ -26,6 +29,7 @@ export class UsageTrackingProvider implements Provider {
|
|
|
26
29
|
constructor(private readonly inner: Provider) {
|
|
27
30
|
this.name = inner.name;
|
|
28
31
|
this.tokenEstimationProvider = inner.tokenEstimationProvider;
|
|
32
|
+
this.supportsNativeWebSearch = inner.supportsNativeWebSearch;
|
|
29
33
|
if (inner.countInputTokens) {
|
|
30
34
|
this.countInputTokens = inner.countInputTokens.bind(inner);
|
|
31
35
|
}
|
|
@@ -92,6 +92,7 @@ export function finalizeTool(tool: ToolDefinition, defaultName = ""): Tool {
|
|
|
92
92
|
});
|
|
93
93
|
const executionTarget = tool.executionTarget ?? TOOL_DEFAULTS.executionTarget;
|
|
94
94
|
const category = tool.category ?? TOOL_DEFAULTS.category;
|
|
95
|
+
const exclusive = tool.exclusive ?? false;
|
|
95
96
|
return {
|
|
96
97
|
...tool,
|
|
97
98
|
name,
|
|
@@ -101,5 +102,6 @@ export function finalizeTool(tool: ToolDefinition, defaultName = ""): Tool {
|
|
|
101
102
|
executionTarget,
|
|
102
103
|
execute,
|
|
103
104
|
category,
|
|
105
|
+
exclusive,
|
|
104
106
|
};
|
|
105
107
|
}
|
package/src/tools/types.ts
CHANGED
|
@@ -449,6 +449,15 @@ export const ToolDefinitionSchema = z.object({
|
|
|
449
449
|
) => Promise<ToolExecutionResult>
|
|
450
450
|
>()
|
|
451
451
|
.optional(),
|
|
452
|
+
/**
|
|
453
|
+
* When true, this tool runs alone in its turn. If the model emits it
|
|
454
|
+
* alongside other tool calls, the agent loop executes only this one and
|
|
455
|
+
* defers the siblings — returning them un-run with a benign notice — so the
|
|
456
|
+
* model incorporates this tool's output before acting on anything else. The
|
|
457
|
+
* `advisor` tool sets this so its guidance lands before the agent commits to
|
|
458
|
+
* a path. Default false (the loop runs sibling calls concurrently as usual).
|
|
459
|
+
*/
|
|
460
|
+
exclusive: z.boolean().optional(),
|
|
452
461
|
});
|
|
453
462
|
|
|
454
463
|
/**
|
|
@@ -460,8 +469,14 @@ export const ToolDefinitionSchema = z.object({
|
|
|
460
469
|
*/
|
|
461
470
|
export type ToolDefinition = z.infer<typeof ToolDefinitionSchema>;
|
|
462
471
|
|
|
463
|
-
/**
|
|
464
|
-
|
|
472
|
+
/**
|
|
473
|
+
* Tool after the loader has derived its name and filled defaults. Every field
|
|
474
|
+
* is required except `exclusive`, which stays optional — most tools never set
|
|
475
|
+
* it, and the agent loop reads it as `?.exclusive === true`, so forcing every
|
|
476
|
+
* hand-built `Tool` (MCP/meet/test fixtures) to carry it would be noise.
|
|
477
|
+
*/
|
|
478
|
+
export type Tool = Required<Omit<ToolDefinition, "exclusive">> &
|
|
479
|
+
Pick<ToolDefinition, "exclusive">;
|
|
465
480
|
|
|
466
481
|
/** The kind of extension that owns a tool. Core tools have no owner. */
|
|
467
482
|
export type OwnerKind = "skill" | "mcp" | "plugin" | "workspace";
|