refactor: return complete tool results inline
This commit is contained in:
@@ -4,13 +4,8 @@ import type { AnalysisOperationPort } from "../application/AnalysisProvider.js";
|
||||
import type { BinarySessionPort } from "../application/BinarySession.js";
|
||||
import type { PromptCompletionKind } from "../contracts/promptContracts.js";
|
||||
import { artifactInventoryResultSchema } from "../domain/artifactGraph.js";
|
||||
import { parseAddressedPage } from "../domain/hopperValues.js";
|
||||
import { processCaptureSchema } from "../domain/processCapture.js";
|
||||
|
||||
const COMPLETION_SCAN_LIMIT = 10_000;
|
||||
const PROCEDURE_PAGE_SIZE = 500;
|
||||
const MAX_COMPLETION_VALUE_LENGTH = 4_096;
|
||||
|
||||
const documentListSchema = z.array(z.string().min(1));
|
||||
const providerStatusSchema = z.object({
|
||||
providers: z.array(z.object({ id: z.string().min(1) })),
|
||||
@@ -32,13 +27,12 @@ export interface PromptCompletionSource {
|
||||
): Promise<readonly string[]>;
|
||||
}
|
||||
|
||||
/** Build bounded live completion over the active provider and session ledger. */
|
||||
/** Build live completion over the active provider and session ledger. */
|
||||
export const createPromptCompletionSource = (
|
||||
analysis: AnalysisOperationPort,
|
||||
session?: BinarySessionPort,
|
||||
): PromptCompletionSource => ({
|
||||
async complete(kind, value, context) {
|
||||
if (value.length > MAX_COMPLETION_VALUE_LENGTH) return [];
|
||||
const candidates = await completionCandidates(
|
||||
kind,
|
||||
analysis,
|
||||
@@ -46,9 +40,9 @@ export const createPromptCompletionSource = (
|
||||
context,
|
||||
);
|
||||
const prefix = normalize(value);
|
||||
return uniqueSorted(candidates)
|
||||
.filter((candidate) => normalize(candidate).startsWith(prefix))
|
||||
.slice(0, COMPLETION_SCAN_LIMIT);
|
||||
return uniqueSorted(candidates).filter((candidate) =>
|
||||
normalize(candidate).startsWith(prefix),
|
||||
);
|
||||
},
|
||||
});
|
||||
|
||||
@@ -81,38 +75,25 @@ const documentCandidates = async (
|
||||
const result = await analysis.execute("list_documents", {});
|
||||
if (!result.ok) return [];
|
||||
const parsed = documentListSchema.safeParse(result.value.result);
|
||||
return parsed.success ? parsed.data.slice(0, COMPLETION_SCAN_LIMIT) : [];
|
||||
return parsed.success ? parsed.data : [];
|
||||
};
|
||||
|
||||
const procedureCandidates = async (
|
||||
analysis: AnalysisOperationPort,
|
||||
document: string | undefined,
|
||||
): Promise<readonly string[]> => {
|
||||
if (document !== undefined && document.length > MAX_COMPLETION_VALUE_LENGTH)
|
||||
return [];
|
||||
const byAddress = new Map<string, string>();
|
||||
let offset = 0;
|
||||
let exhaustive = false;
|
||||
while (byAddress.size < COMPLETION_SCAN_LIMIT / 2) {
|
||||
const result = await analysis.execute("list_procedures", {
|
||||
offset,
|
||||
limit: PROCEDURE_PAGE_SIZE,
|
||||
...(document === undefined || document.length === 0 ? {} : { document }),
|
||||
});
|
||||
if (!result.ok) break;
|
||||
const parsed = parseAddressedPage(result.value.result);
|
||||
if (!parsed.ok) break;
|
||||
for (const item of parsed.value.items) {
|
||||
if (byAddress.size >= COMPLETION_SCAN_LIMIT / 2) break;
|
||||
if (!byAddress.has(item.address)) byAddress.set(item.address, item.name);
|
||||
}
|
||||
const nextOffset = parsed.value.nextOffset;
|
||||
if (!parsed.value.hasMore) {
|
||||
exhaustive = true;
|
||||
break;
|
||||
}
|
||||
if (nextOffset === null || nextOffset <= offset) break;
|
||||
offset = nextOffset;
|
||||
const input =
|
||||
document === undefined || document.length === 0 ? {} : { document };
|
||||
const result = await analysis.execute("list_procedures", input);
|
||||
if (result.ok) {
|
||||
const parsed = z
|
||||
.array(z.object({ address: z.string(), value: z.string() }))
|
||||
.safeParse(result.value.result);
|
||||
if (parsed.success)
|
||||
for (const item of parsed.data)
|
||||
if (!byAddress.has(item.address))
|
||||
byAddress.set(item.address, item.value);
|
||||
}
|
||||
|
||||
const addressesByName = new Map<string, Set<string>>();
|
||||
@@ -122,9 +103,8 @@ const procedureCandidates = async (
|
||||
addressesByName.set(name, addresses);
|
||||
}
|
||||
const candidates = [...byAddress.keys()];
|
||||
if (exhaustive)
|
||||
for (const [name, addresses] of addressesByName)
|
||||
if (addresses.size === 1 && name.length > 0) candidates.push(name);
|
||||
for (const [name, addresses] of addressesByName)
|
||||
if (addresses.size === 1 && name.length > 0) candidates.push(name);
|
||||
return candidates;
|
||||
};
|
||||
|
||||
@@ -140,7 +120,7 @@ const providerCandidates = (
|
||||
({ provider }) => provider.id,
|
||||
) ?? [])
|
||||
: parsed.data.providers.map(({ id }) => id);
|
||||
return ["auto", ...candidates].slice(0, COMPLETION_SCAN_LIMIT);
|
||||
return ["auto", ...candidates];
|
||||
};
|
||||
|
||||
const evidenceCandidates = (
|
||||
@@ -149,11 +129,8 @@ const evidenceCandidates = (
|
||||
): readonly string[] => {
|
||||
if (session === undefined) return [];
|
||||
const candidates: string[] = [];
|
||||
for (const evidence of session.exportEvidenceBundle().records) {
|
||||
const remaining = COMPLETION_SCAN_LIMIT - candidates.length;
|
||||
candidates.push(...evidenceValues(kind, evidence).slice(0, remaining));
|
||||
if (candidates.length >= COMPLETION_SCAN_LIMIT) break;
|
||||
}
|
||||
for (const evidence of session.exportEvidenceBundle().records)
|
||||
candidates.push(...evidenceValues(kind, evidence));
|
||||
return candidates;
|
||||
};
|
||||
|
||||
@@ -191,18 +168,10 @@ const unknownCandidates = (
|
||||
: session
|
||||
.listUnknowns()
|
||||
.filter(({ status }) => status !== "resolved")
|
||||
.map(({ unknown_id: id }) => id)
|
||||
.slice(0, COMPLETION_SCAN_LIMIT);
|
||||
.map(({ unknown_id: id }) => id);
|
||||
|
||||
const uniqueSorted = (values: readonly string[]): string[] =>
|
||||
[
|
||||
...new Set(
|
||||
values.filter(
|
||||
(value) =>
|
||||
value.length > 0 && value.length <= MAX_COMPLETION_VALUE_LENGTH,
|
||||
),
|
||||
),
|
||||
].sort(compareText);
|
||||
[...new Set(values.filter((value) => value.length > 0))].sort(compareText);
|
||||
|
||||
const compareText = (left: string, right: string): number =>
|
||||
left < right ? -1 : left > right ? 1 : 0;
|
||||
|
||||
Reference in New Issue
Block a user