File size: 9,266 Bytes
e81f4e4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | import { isRecord } from "../shared/types";
import type { UnknownRecord } from "../shared/types";
import {
GEMINI_NATIVE_HIDDEN_TOOLS_PROMPT,
buildToolCallInstructions,
buildToolPromptBlock,
} from "./prompt-format";
import {
extractToolMeta,
toolFunctionDeclarations,
toolItemsFromTools,
} from "./tool-meta";
import type { ToolMeta } from "./tool-meta";
type NameSet = Record<string, boolean>;
type BundlePolicy = {
mode?: unknown;
allowed?: NameSet | null;
hasAllowed?: boolean;
};
type ToolPromptDef = {
name?: unknown;
description?: unknown;
parameters?: unknown;
};
export type ToolSchemaIndex = Record<string, UnknownRecord>;
export type ToolPromptArtifact = {
readonly defs: readonly ToolPromptDef[];
readonly names: readonly string[];
toolCallInstructions: () => string;
inlinePromptBlock: (toolChoiceInstruction?: unknown) => string;
contextTranscript: (
toolChoiceInstruction?: unknown,
filename?: unknown,
) => string;
};
export type ToolBundle = {
readonly __toolBundle: true;
readonly source: unknown;
readonly items: UnknownRecord[];
readonly metas: ToolMeta[];
readonly defs: ToolMeta[];
readonly names: string[];
readonly nameSet: NameSet;
readonly openAIFunctionTools: UnknownRecord[];
readonly schemaIndex: ToolSchemaIndex | null;
readonly promptArtifact: ToolPromptArtifact;
};
export const emptyToolBundle: ToolBundle = {
__toolBundle: true,
source: null,
items: [],
metas: [],
defs: [],
names: [],
nameSet: {},
openAIFunctionTools: [],
schemaIndex: null,
promptArtifact: createToolPromptArtifact([], []),
};
export function isToolBundle(value: unknown): value is ToolBundle {
return !!(
value &&
typeof value === "object" &&
(value as { __toolBundle?: unknown }).__toolBundle === true
);
}
export function createToolBundle(tools: unknown): ToolBundle {
if (isToolBundle(tools)) return tools;
const items = toolItemsFromTools(tools);
if (!items.length) return emptyToolBundle;
const metas: ToolMeta[] = [];
const defs: ToolMeta[] = [];
const names: string[] = [];
const nameSet: NameSet = {};
const openAIFunctionTools: UnknownRecord[] = [];
const schemaIndex: ToolSchemaIndex = {};
const addMeta = (meta: ToolMeta | null) => {
if (!meta?.name) return;
metas.push(meta);
defs.push({
name: meta.name,
description: meta.description,
parameters: meta.parameters || {},
});
if (!nameSet[meta.name]) {
nameSet[meta.name] = true;
names.push(meta.name);
}
const fn: UnknownRecord = { name: meta.name };
if (meta.description) fn.description = meta.description;
if (meta.parameters != null) fn.parameters = meta.parameters;
openAIFunctionTools.push({ type: "function", function: fn });
if (isRecord(meta.parameters))
schemaIndex[meta.name.toLowerCase()] = meta.parameters;
};
for (const item of items) {
const declarations = toolFunctionDeclarations(item);
if (declarations.length) {
for (const declaration of declarations)
addMeta(extractToolMeta(declaration));
} else {
addMeta(extractToolMeta(item));
}
}
if (!metas.length) return { ...emptyToolBundle, source: tools, items };
return {
__toolBundle: true,
source: tools,
items,
metas,
defs,
names,
nameSet,
openAIFunctionTools,
schemaIndex: Object.keys(schemaIndex).length ? schemaIndex : null,
promptArtifact: createToolPromptArtifact(defs, names),
};
}
export function filterToolBundleByPolicy(
bundle: ToolBundle,
policy: BundlePolicy | null | undefined,
): ToolBundle {
if (
!bundle.openAIFunctionTools.length ||
(policy && policy.mode === "none")
) {
return { ...emptyToolBundle, source: bundle.source };
}
if (
!policy?.allowed ||
(!policy.hasAllowed && Object.keys(policy.allowed).length === 0)
)
return bundle;
const metas: ToolMeta[] = [];
const defs: ToolMeta[] = [];
const names: string[] = [];
const nameSet: NameSet = {};
const openAIFunctionTools: UnknownRecord[] = [];
const schemaIndex: ToolSchemaIndex = {};
for (let i = 0; i < bundle.metas.length; i++) {
const meta = bundle.metas[i];
if (!meta || !policy.allowed[String(meta.name || "").trim()]) continue;
metas.push(meta);
const def = bundle.defs[i] || {
name: meta.name,
description: meta.description,
parameters: meta.parameters || {},
};
defs.push(def);
if (!nameSet[meta.name]) {
nameSet[meta.name] = true;
names.push(meta.name);
}
const tool = bundle.openAIFunctionTools[i];
if (tool) openAIFunctionTools.push(tool);
if (isRecord(meta.parameters))
schemaIndex[meta.name.toLowerCase()] = meta.parameters;
}
if (!metas.length) return { ...emptyToolBundle, source: bundle.source };
return {
__toolBundle: true,
source: bundle.source,
items: bundle.items,
metas,
defs,
names,
nameSet,
openAIFunctionTools,
schemaIndex: Object.keys(schemaIndex).length ? schemaIndex : null,
promptArtifact: createToolPromptArtifact(defs, names),
};
}
export function nullableOpenAIFunctionTools(
bundle: ToolBundle | null | undefined,
): UnknownRecord[] | null {
return bundle?.openAIFunctionTools.length ? bundle.openAIFunctionTools : null;
}
export function toolNamesForPromptSource(
source: unknown,
fallbackDefs?: readonly ToolPromptDef[] | null,
): string[] {
if (isToolBundle(source)) return source.names;
const defs = toolPromptDefsFromSource(source, fallbackDefs);
const names: string[] = [];
const seen: NameSet = {};
for (const def of defs) {
const name = String(def?.name || "").trim();
if (!name || seen[name]) continue;
seen[name] = true;
names.push(name);
}
return names;
}
export function toolCallInstructionsFor(
source: unknown,
fallbackDefs?: readonly ToolPromptDef[] | null,
): string {
if (isToolBundle(source)) return source.promptArtifact.toolCallInstructions();
return buildToolCallInstructions(
toolNamesForPromptSource(source, fallbackDefs),
);
}
export function toolPromptBlockFor(
source: unknown,
toolChoiceInstruction?: unknown,
fallbackDefs?: readonly ToolPromptDef[] | null,
): string {
if (isToolBundle(source))
return source.promptArtifact.inlinePromptBlock(toolChoiceInstruction);
const defs = toolPromptDefsFromSource(source, fallbackDefs);
return defs.length
? buildToolPromptBlock([...defs], toolChoiceInstruction)
: "";
}
export function toolsContextTranscriptFor(
source: unknown,
toolChoiceInstruction?: unknown,
filename: unknown = "tools.txt",
fallbackDefs?: readonly ToolPromptDef[] | null,
): string {
if (isToolBundle(source))
return source.promptArtifact.contextTranscript(
toolChoiceInstruction,
filename,
);
const defs = toolPromptDefsFromSource(source, fallbackDefs);
return buildToolsContextTranscriptFromDefs(
defs,
toolChoiceInstruction,
filename,
);
}
function toolPromptDefsFromSource(
source: unknown,
fallbackDefs?: readonly ToolPromptDef[] | null,
): readonly ToolPromptDef[] {
if (Array.isArray(fallbackDefs)) return fallbackDefs;
return Array.isArray(source) ? (source as ToolPromptDef[]) : [];
}
function createToolPromptArtifact(
defs: readonly ToolMeta[],
names: readonly string[],
): ToolPromptArtifact {
const cachedDefs = defs.map((def) => ({
name: def.name,
description: def.description,
parameters: def.parameters || {},
}));
const cachedNames = [...names];
let instructions: string | null = null;
const promptBlocks = new Map<string, string>();
const transcripts = new Map<string, string>();
return {
defs: cachedDefs,
names: cachedNames,
toolCallInstructions() {
if (instructions == null)
instructions = buildToolCallInstructions(cachedNames);
return instructions;
},
inlinePromptBlock(toolChoiceInstruction?: unknown) {
const key = String(toolChoiceInstruction || "");
const cached = promptBlocks.get(key);
if (cached != null) return cached;
const text = cachedDefs.length
? buildToolPromptBlock([...cachedDefs], toolChoiceInstruction)
: "";
promptBlocks.set(key, text);
return text;
},
contextTranscript(
toolChoiceInstruction?: unknown,
filename: unknown = "tools.txt",
) {
const key = `${String(filename || "tools.txt")}\x00${String(toolChoiceInstruction || "")}`;
const cached = transcripts.get(key);
if (cached != null) return cached;
const text = buildToolsContextTranscriptFromDefs(
cachedDefs,
toolChoiceInstruction,
filename,
);
transcripts.set(key, text);
return text;
},
};
}
function buildToolsContextTranscriptFromDefs(
toolDefs: readonly ToolPromptDef[] | null | undefined,
choiceInstruction: unknown,
filename: unknown,
): string {
const defs = toolDefs || [];
const names = toolNamesForPromptSource(defs);
const sections = [`# ${filename || "tools.txt"}`];
if (defs.length) {
sections.push(
"Available tool descriptions, parameter schemas, and tool-use instructions.",
`Available tools:\n${JSON.stringify(defs, null, 2)}`,
`Tool call format instructions:\n${buildToolCallInstructions(names)}`,
);
} else {
sections.push("Tool-use instructions for this request.");
}
if (choiceInstruction)
sections.push(`Tool choice policy:\n${String(choiceInstruction).trim()}`);
sections.push(GEMINI_NATIVE_HIDDEN_TOOLS_PROMPT);
return `${sections.filter((section) => String(section || "").trim()).join("\n\n")}\n`;
}
|