@nexface/agent 0.1.21-alpha.3 → 0.1.21-alpha.5
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.js +236 -7
- package/dist/models/k.js +1 -0
- package/dist/request.d.ts +1 -0
- package/dist/request.js +2 -0
- package/dist/shared/text-only-reply.d.ts +6 -0
- package/dist/shared/text-only-reply.js +10 -0
- package/dist/shared/tool-presentation.d.ts +18 -0
- package/dist/shared/tool-presentation.js +5 -0
- package/dist/shared/tool-result-markdown.d.ts +15 -0
- package/dist/shared/tool-result-markdown.js +55 -0
- package/dist/shared/tool-result-reference.d.ts +2 -0
- package/dist/shared/tool-result-reference.js +5 -0
- package/package.json +4 -3
package/dist/agent.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import { getToolPresentationController } from "./shared/tool-presentation.js";
|
|
1
2
|
import { decodeToolProxy } from "./shared/tool-proxy.js";
|
|
3
|
+
import { filterToolResultBlocks } from "./shared/tool-result-markdown.js";
|
|
4
|
+
import { requestsTextOnlyReply } from "./shared/text-only-reply.js";
|
|
5
|
+
import { isToolResultReferenceId, toolResultReferenceLanguage, } from "./shared/tool-result-reference.js";
|
|
2
6
|
import { pageDecisionSymbol } from "./shared/runtime-execution.js";
|
|
3
7
|
import { recordPageDiagnostic } from "./page-diagnostics.js";
|
|
4
8
|
import { isNavigationLoadingPage, isPendingPage, navigationLoadingWaits, pageFingerprint, } from "./page-observation.js";
|
|
@@ -246,6 +250,23 @@ function parseArguments(call, createError = (message) => new AgentToolError("INV
|
|
|
246
250
|
}
|
|
247
251
|
return input;
|
|
248
252
|
}
|
|
253
|
+
function summarizeToolInput(input) {
|
|
254
|
+
const scope = Object.create(null);
|
|
255
|
+
for (const [key, value] of Object.entries(input)) {
|
|
256
|
+
if (key.length > 64)
|
|
257
|
+
continue;
|
|
258
|
+
if (typeof value === "string") {
|
|
259
|
+
const text = value.trim().replace(/\s+/g, " ");
|
|
260
|
+
scope[key] = text.length > 80 ? text.slice(0, 79) + "…" : text;
|
|
261
|
+
}
|
|
262
|
+
else if (value === null || typeof value === "number" || typeof value === "boolean") {
|
|
263
|
+
scope[key] = value;
|
|
264
|
+
}
|
|
265
|
+
if (Object.keys(scope).length >= 6)
|
|
266
|
+
break;
|
|
267
|
+
}
|
|
268
|
+
return Object.keys(scope).length ? scope : undefined;
|
|
269
|
+
}
|
|
249
270
|
function parseEventInput(call) {
|
|
250
271
|
try {
|
|
251
272
|
return { input: JSON.parse(call.arguments || "{}") };
|
|
@@ -879,6 +900,10 @@ class Nexface {
|
|
|
879
900
|
get model() {
|
|
880
901
|
return { ...this.#modelInfo };
|
|
881
902
|
}
|
|
903
|
+
#hasToolRenderer(name) {
|
|
904
|
+
return (this.#request?.hasRenderer(name) === true ||
|
|
905
|
+
Boolean(this.#toolRuntime && getToolPresentationController(this.#toolRuntime)?.get(name)));
|
|
906
|
+
}
|
|
882
907
|
async #getTools(signal) {
|
|
883
908
|
signal.throwIfAborted();
|
|
884
909
|
const runtimeTools = this.#toolRuntime?.getTools() ?? [];
|
|
@@ -1732,7 +1757,190 @@ class Nexface {
|
|
|
1732
1757
|
content,
|
|
1733
1758
|
});
|
|
1734
1759
|
}
|
|
1735
|
-
|
|
1760
|
+
#currentTurnStart() {
|
|
1761
|
+
let start = this.#messages.length;
|
|
1762
|
+
while (start > 0 && this.#messages[start - 1]?.role !== "user")
|
|
1763
|
+
start -= 1;
|
|
1764
|
+
return start;
|
|
1765
|
+
}
|
|
1766
|
+
#toolResultCandidates(definitions) {
|
|
1767
|
+
const start = this.#currentTurnStart();
|
|
1768
|
+
const calls = new Map();
|
|
1769
|
+
const receipts = new Set();
|
|
1770
|
+
const pendingInputs = new Map();
|
|
1771
|
+
const results = new Map();
|
|
1772
|
+
for (const message of this.#messages.slice(start)) {
|
|
1773
|
+
if (message.role === "assistant") {
|
|
1774
|
+
for (const call of message.toolCalls) {
|
|
1775
|
+
if (!isToolResultReferenceId(call.id))
|
|
1776
|
+
continue;
|
|
1777
|
+
if (calls.has(call.id)) {
|
|
1778
|
+
calls.set(call.id, undefined);
|
|
1779
|
+
results.delete(call.id);
|
|
1780
|
+
continue;
|
|
1781
|
+
}
|
|
1782
|
+
try {
|
|
1783
|
+
const args = parseArguments(call);
|
|
1784
|
+
const decoded = call.name === callApplicationTool.name
|
|
1785
|
+
? decodeToolProxy(args)
|
|
1786
|
+
: { name: call.name, arguments: args };
|
|
1787
|
+
calls.set(call.id, { name: decoded.name, input: decoded.arguments });
|
|
1788
|
+
}
|
|
1789
|
+
catch {
|
|
1790
|
+
calls.set(call.id, undefined);
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
}
|
|
1794
|
+
else if (message.role === "tool") {
|
|
1795
|
+
const id = message.toolCallId;
|
|
1796
|
+
if (receipts.has(id)) {
|
|
1797
|
+
results.delete(id);
|
|
1798
|
+
continue;
|
|
1799
|
+
}
|
|
1800
|
+
receipts.add(id);
|
|
1801
|
+
const call = calls.get(id);
|
|
1802
|
+
if (!call || !this.#hasToolRenderer(call.name))
|
|
1803
|
+
continue;
|
|
1804
|
+
const definition = definitions.get(call.name);
|
|
1805
|
+
if (!definition)
|
|
1806
|
+
continue;
|
|
1807
|
+
let result;
|
|
1808
|
+
try {
|
|
1809
|
+
const payload = JSON.parse(message.content);
|
|
1810
|
+
if (!payload || !Object.hasOwn(payload, "result") || Object.hasOwn(payload, "error"))
|
|
1811
|
+
continue;
|
|
1812
|
+
result = payload.result;
|
|
1813
|
+
}
|
|
1814
|
+
catch {
|
|
1815
|
+
continue;
|
|
1816
|
+
}
|
|
1817
|
+
const properties = definition.inputSchema.properties;
|
|
1818
|
+
const confirmation = properties?.confirmation;
|
|
1819
|
+
const businessSchema = properties?.input?.type === "object" &&
|
|
1820
|
+
confirmation?.type === "object" &&
|
|
1821
|
+
confirmation.properties?.requestId?.type === "string" &&
|
|
1822
|
+
confirmation.properties?.approved?.type === "boolean"
|
|
1823
|
+
? properties.input
|
|
1824
|
+
: undefined;
|
|
1825
|
+
const wireInput = call.input.input;
|
|
1826
|
+
if (result && typeof result === "object" && "status" in result) {
|
|
1827
|
+
if (result.status === "confirmation_required" &&
|
|
1828
|
+
"requestId" in result &&
|
|
1829
|
+
typeof result.requestId === "string" &&
|
|
1830
|
+
"message" in result &&
|
|
1831
|
+
typeof result.message === "string" &&
|
|
1832
|
+
"nextAction" in result &&
|
|
1833
|
+
typeof result.nextAction === "string" &&
|
|
1834
|
+
Object.keys(result).length === 4) {
|
|
1835
|
+
if (businessSchema && wireInput && !validateToolInput(businessSchema, wireInput).length)
|
|
1836
|
+
pendingInputs.set(JSON.stringify([call.name, result.requestId]), {
|
|
1837
|
+
id,
|
|
1838
|
+
input: wireInput,
|
|
1839
|
+
});
|
|
1840
|
+
continue;
|
|
1841
|
+
}
|
|
1842
|
+
if (businessSchema &&
|
|
1843
|
+
result.status === "rejected" &&
|
|
1844
|
+
"toolExecuted" in result &&
|
|
1845
|
+
result.toolExecuted === false &&
|
|
1846
|
+
Object.keys(result).every((key) => ["status", "requestId", "toolExecuted"].includes(key)))
|
|
1847
|
+
continue;
|
|
1848
|
+
}
|
|
1849
|
+
let inputScope;
|
|
1850
|
+
if (!validateToolInput(definition.inputSchema, call.input).length) {
|
|
1851
|
+
if (businessSchema) {
|
|
1852
|
+
const answer = call.input.confirmation;
|
|
1853
|
+
const pending = pendingInputs.get(JSON.stringify([call.name, answer?.requestId]));
|
|
1854
|
+
const businessInput = answer
|
|
1855
|
+
? answer.approved && pending && calls.get(pending.id)
|
|
1856
|
+
? pending.input
|
|
1857
|
+
: undefined
|
|
1858
|
+
: wireInput;
|
|
1859
|
+
if (businessInput && !validateToolInput(businessSchema, businessInput).length)
|
|
1860
|
+
inputScope = summarizeToolInput(businessInput);
|
|
1861
|
+
}
|
|
1862
|
+
else {
|
|
1863
|
+
inputScope = summarizeToolInput(call.input);
|
|
1864
|
+
}
|
|
1865
|
+
}
|
|
1866
|
+
results.set(id, {
|
|
1867
|
+
toolCallId: id,
|
|
1868
|
+
toolName: call.name,
|
|
1869
|
+
...(inputScope ? { inputScope } : {}),
|
|
1870
|
+
});
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1873
|
+
return results;
|
|
1874
|
+
}
|
|
1875
|
+
#toolResultInstructions(definitions) {
|
|
1876
|
+
const results = this.#toolResultCandidates(definitions);
|
|
1877
|
+
const planning = "\n\nWhen the user asks for separate results by scope or in a specific order, use tool inputs that obtain the requested results at that granularity.";
|
|
1878
|
+
if (!results.size)
|
|
1879
|
+
return [...definitions.keys()].some((name) => this.#hasToolRenderer(name)) ? planning : "";
|
|
1880
|
+
const descriptions = [
|
|
1881
|
+
...new Set([...results.values()].map(({ toolName }) => toolName)),
|
|
1882
|
+
].flatMap((toolName) => {
|
|
1883
|
+
const description = definitions.get(toolName)?.description;
|
|
1884
|
+
return description ? [{ toolName, description }] : [];
|
|
1885
|
+
});
|
|
1886
|
+
return [
|
|
1887
|
+
planning,
|
|
1888
|
+
"\n<reply_rules>",
|
|
1889
|
+
"<selection_and_placement>",
|
|
1890
|
+
"Tool result references for the final reply:",
|
|
1891
|
+
"A result reference displays the tool's user-facing component at that position in the reply.",
|
|
1892
|
+
"Use the tool results to answer each part of the request. When the user asks to see a list, compare options, or inspect a result, include a reference if an available component directly presents the requested information, even if the user did not ask for a component. Follow the requested scope and order, and skip results used only to prepare another step.",
|
|
1893
|
+
"Use the tool description to tell which information the component actually shows. If the user asks only for details the component omits, answer those details in prose and omit the reference, even when the tool result contains the answer. Results that directly answer the request remain eligible without a description of their display.",
|
|
1894
|
+
"Write the reference as you answer the part it supports. For a reply with separate sections, start each relevant section with its heading or a short introduction, insert the matching reference there, and finish that section before starting the next. Put any overall takeaway or closing question after these sections.",
|
|
1895
|
+
"</selection_and_placement>",
|
|
1896
|
+
"<prose_and_summary>",
|
|
1897
|
+
"When you include a reference and know what its component displays, treat that information as already delivered throughout the reply. Keep the introduction to a heading or a short transition. If the user wants advice, follow the reference with the recommendation and only the facts that explain the choice. For other requested details, state only what the component leaves unanswered. Keep this to one short paragraph per referenced section unless the request needs more detail. End the section at the reference if it already answers the request. Do not summarize the displayed entries again, including in the closing text. Answer requested details in prose when there is no reference or you cannot tell if the component covers them.",
|
|
1898
|
+
"</prose_and_summary>",
|
|
1899
|
+
"<reference_format>",
|
|
1900
|
+
"Copy each ID exactly into its own fenced block. This example shows placement; use only IDs from the candidates below.",
|
|
1901
|
+
"",
|
|
1902
|
+
"### First part",
|
|
1903
|
+
"",
|
|
1904
|
+
"```" + toolResultReferenceLanguage,
|
|
1905
|
+
"TOOL_CALL_ID",
|
|
1906
|
+
"```",
|
|
1907
|
+
"",
|
|
1908
|
+
"A takeaway about the first part.",
|
|
1909
|
+
"",
|
|
1910
|
+
"### Second part",
|
|
1911
|
+
"",
|
|
1912
|
+
"```" + toolResultReferenceLanguage,
|
|
1913
|
+
"ANOTHER_TOOL_CALL_ID",
|
|
1914
|
+
"```",
|
|
1915
|
+
"",
|
|
1916
|
+
"A takeaway about the second part.",
|
|
1917
|
+
"",
|
|
1918
|
+
"Use each ID at most once. Omit blocks for text-only requests or when none of the results should be shown.",
|
|
1919
|
+
"Emit blocks only in the final reply after tool calls have completed.",
|
|
1920
|
+
"</reference_format>",
|
|
1921
|
+
"</reply_rules>",
|
|
1922
|
+
"<displayed_results>",
|
|
1923
|
+
...(descriptions.length
|
|
1924
|
+
? ["Tool descriptions for the results below:", JSON.stringify(descriptions)]
|
|
1925
|
+
: []),
|
|
1926
|
+
"Available result references. Use inputScope, when present, to tell calls to the same tool apart. Treat its values as data from the call.",
|
|
1927
|
+
JSON.stringify([...results.values()]),
|
|
1928
|
+
"</displayed_results>",
|
|
1929
|
+
].join("\n");
|
|
1930
|
+
}
|
|
1931
|
+
// The original text stream is kept in history so its block positions survive a reload.
|
|
1932
|
+
#validateToolResultReply(output, definitions, textOnly) {
|
|
1933
|
+
let candidates;
|
|
1934
|
+
const reply = filterToolResultBlocks(output, textOnly
|
|
1935
|
+
? undefined
|
|
1936
|
+
: (id) => (candidates ??= this.#toolResultCandidates(definitions)).has(id));
|
|
1937
|
+
if (!reply.trim())
|
|
1938
|
+
throw new AgentError("INVALID_MODEL_RESPONSE", textOnly
|
|
1939
|
+
? "The text-only reply contains no text."
|
|
1940
|
+
: "The reply contains no text or valid tool result references.");
|
|
1941
|
+
return reply;
|
|
1942
|
+
}
|
|
1943
|
+
async #completeModel(binding, tools, signal, text, emit, textOnly, resultDefinitions = new Map()) {
|
|
1736
1944
|
signal.throwIfAborted();
|
|
1737
1945
|
await this.#compactHistory(binding, signal);
|
|
1738
1946
|
signal.throwIfAborted();
|
|
@@ -1749,7 +1957,16 @@ class Nexface {
|
|
|
1749
1957
|
};
|
|
1750
1958
|
try {
|
|
1751
1959
|
return await binding.complete({
|
|
1752
|
-
messages: [
|
|
1960
|
+
messages: [
|
|
1961
|
+
{
|
|
1962
|
+
role: "system",
|
|
1963
|
+
content: this.#systemPrompt +
|
|
1964
|
+
(textOnly
|
|
1965
|
+
? "\n\nThe user requested a text-only reply for this turn. Answer in text without component references."
|
|
1966
|
+
: this.#toolResultInstructions(resultDefinitions)),
|
|
1967
|
+
},
|
|
1968
|
+
...this.#messages,
|
|
1969
|
+
],
|
|
1753
1970
|
...(this.#historySummary ? { historySummary: this.#historySummary } : {}),
|
|
1754
1971
|
...(this.#pageDecision ? { pageDelegationHistory: true } : {}),
|
|
1755
1972
|
tools,
|
|
@@ -1930,15 +2147,19 @@ class Nexface {
|
|
|
1930
2147
|
let recoverableFailures = state?.recoverableFailures ?? 0;
|
|
1931
2148
|
const unresolvedToolFailures = state?.unresolvedToolFailures ?? new Set();
|
|
1932
2149
|
const startStep = state?.step ?? 0;
|
|
2150
|
+
const userMessage = this.#messages[this.#currentTurnStart() - 1];
|
|
2151
|
+
const textOnly = userMessage?.role === "user" && requestsTextOnlyReply(userMessage.content);
|
|
1933
2152
|
if (startStep >= this.#maxSteps) {
|
|
2153
|
+
const definitions = new Map(initialDefinitions.map((definition) => [definition.name, definition]));
|
|
1934
2154
|
const text = createTextEmitter(emit);
|
|
1935
|
-
const response = await this.#completeModel(binding, [], signal, text, emit);
|
|
2155
|
+
const response = await this.#completeModel(binding, [], signal, text, emit, textOnly, definitions);
|
|
1936
2156
|
signal.throwIfAborted();
|
|
1937
2157
|
validateToolCallIds(response.toolCalls);
|
|
1938
2158
|
const output = response.content?.trim() ?? "";
|
|
1939
2159
|
if (response.toolCalls.length || !output) {
|
|
1940
2160
|
throw new AgentError("AGENT_RUN_LIMIT_REACHED", "Agent step limit reached before completion.");
|
|
1941
2161
|
}
|
|
2162
|
+
const finalOutput = this.#validateToolResultReply(output, definitions, textOnly);
|
|
1942
2163
|
this.#messages.push({
|
|
1943
2164
|
id: text.id,
|
|
1944
2165
|
role: "assistant",
|
|
@@ -1947,7 +2168,7 @@ class Nexface {
|
|
|
1947
2168
|
});
|
|
1948
2169
|
response.accept?.();
|
|
1949
2170
|
text.finish(response.content);
|
|
1950
|
-
return { outcome: { type: "success" }, output };
|
|
2171
|
+
return { outcome: { type: "success" }, output: finalOutput };
|
|
1951
2172
|
}
|
|
1952
2173
|
for (let step = startStep; step < this.#maxSteps; step += 1) {
|
|
1953
2174
|
signal.throwIfAborted();
|
|
@@ -2017,7 +2238,8 @@ class Nexface {
|
|
|
2017
2238
|
pageResponse = await this.#decidePage(binding, signal, emit);
|
|
2018
2239
|
response = resumed
|
|
2019
2240
|
? { content: null, toolCalls: resumed.calls }
|
|
2020
|
-
: (pageResponse ??
|
|
2241
|
+
: (pageResponse ??
|
|
2242
|
+
(await this.#completeModel(binding, tools, signal, text, emit, textOnly, applicationDefinitionsByName)));
|
|
2021
2243
|
if (this.#pageDecision &&
|
|
2022
2244
|
response.toolCalls.some((call) => call.name === "nxf_executeAction")) {
|
|
2023
2245
|
throw new AgentError("UNKNOWN_MODEL_TOOL", "Code execution is unavailable when pageDecision is enabled.");
|
|
@@ -2081,6 +2303,7 @@ class Nexface {
|
|
|
2081
2303
|
if (!output) {
|
|
2082
2304
|
throw new AgentError("INVALID_MODEL_RESPONSE", "nxf_finish needs non-empty output.");
|
|
2083
2305
|
}
|
|
2306
|
+
const finalOutput = this.#validateToolResultReply(output, applicationDefinitionsByName, textOnly);
|
|
2084
2307
|
text.finish(response.content || output);
|
|
2085
2308
|
this.#messages.push({
|
|
2086
2309
|
id: text.id,
|
|
@@ -2097,7 +2320,10 @@ class Nexface {
|
|
|
2097
2320
|
result: { status: finishInput.status, output },
|
|
2098
2321
|
}),
|
|
2099
2322
|
});
|
|
2100
|
-
return {
|
|
2323
|
+
return {
|
|
2324
|
+
outcome: { type: "success" },
|
|
2325
|
+
output: finalOutput,
|
|
2326
|
+
};
|
|
2101
2327
|
}
|
|
2102
2328
|
text.finish(response.content);
|
|
2103
2329
|
const call = response.toolCalls[0];
|
|
@@ -2107,6 +2333,9 @@ class Nexface {
|
|
|
2107
2333
|
if (!call && !output) {
|
|
2108
2334
|
throw new AgentError("INVALID_MODEL_RESPONSE", "Model returned neither a message nor a tool call.");
|
|
2109
2335
|
}
|
|
2336
|
+
const finalOutput = !call
|
|
2337
|
+
? this.#validateToolResultReply(output, applicationDefinitionsByName, textOnly)
|
|
2338
|
+
: output;
|
|
2110
2339
|
if (!resumed) {
|
|
2111
2340
|
this.#messages.push({
|
|
2112
2341
|
id: text.id,
|
|
@@ -2137,7 +2366,7 @@ class Nexface {
|
|
|
2137
2366
|
if (!call) {
|
|
2138
2367
|
return {
|
|
2139
2368
|
outcome: { type: "success" },
|
|
2140
|
-
output,
|
|
2369
|
+
output: finalOutput,
|
|
2141
2370
|
};
|
|
2142
2371
|
}
|
|
2143
2372
|
const hasMixedFinish = preparedCalls.some(({ kind }) => kind === "finish");
|
package/dist/models/k.js
CHANGED
|
@@ -633,6 +633,7 @@ async function consumeStream(stream, onTextDelta, onReasoning) {
|
|
|
633
633
|
}
|
|
634
634
|
}
|
|
635
635
|
if (!result) throw new AgentModelError("INVALID_MODEL_RESPONSE", "Model stream ended without a final message.");
|
|
636
|
+
if ("length" === result.stopReason) throw new AgentModelError("INVALID_MODEL_RESPONSE", "The model reached its output token limit before completing the response.");
|
|
636
637
|
return result;
|
|
637
638
|
}
|
|
638
639
|
async function rejectsStrictTools(response) {
|
package/dist/request.d.ts
CHANGED
|
@@ -33,6 +33,7 @@ export interface InternalRequestInput {
|
|
|
33
33
|
readonly resume?: AgentResumeEntry;
|
|
34
34
|
readonly toolResult?: BrowserToolResult;
|
|
35
35
|
readonly receipts: readonly Message[];
|
|
36
|
+
readonly hasRenderer: (toolName: string) => boolean;
|
|
36
37
|
}
|
|
37
38
|
export interface RequestContinuation {
|
|
38
39
|
version: 3;
|
package/dist/request.js
CHANGED
|
@@ -256,6 +256,8 @@ export function decodeRequest(value) {
|
|
|
256
256
|
inputSchema: tool.parameters,
|
|
257
257
|
effect: extension?.toolMetadata?.[tool.name]?.effect ?? "action",
|
|
258
258
|
})),
|
|
259
|
+
hasRenderer: (name) => Boolean(extension?.toolMetadata && Object.hasOwn(extension.toolMetadata, name)) &&
|
|
260
|
+
extension?.toolMetadata?.[name]?.hasRenderer === true,
|
|
259
261
|
contexts: value.context.map((context, index) => {
|
|
260
262
|
let data = context.value;
|
|
261
263
|
try {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Generated from shared/text-only-reply.ts. Do not edit manually.
|
|
2
|
+
const chineseTextOnly = /^(?:请[ \t]*)?(?:(?:只用|仅用)(?:纯)?|(?:用|以)纯)(?:文字|文本)(?:[\s,。;:,:]|(?:简要|简单|详细|直接)?(?:回答|回复|介绍|解释|说明|描述|总结|列出)|$)/u;
|
|
3
|
+
const englishTextOnly = /^(?:please\s+)?(?:(?:answer|reply|respond)\s+(?:using|in|with)\s+|use\s+)(?:plain\s+text(?:\s+only)?|text\s+only)(?=\s*(?:[.,:;!]|$)|\s+(?:to|and|about)\b)/i;
|
|
4
|
+
export function requestsTextOnlyReply(content) {
|
|
5
|
+
const text = typeof content === "string"
|
|
6
|
+
? content
|
|
7
|
+
: content?.find((part) => part.type === "text" && part.text?.trim())?.text;
|
|
8
|
+
const opening = text?.trimStart() ?? "";
|
|
9
|
+
return chineseTextOnly.test(opening) || englishTextOnly.test(opening);
|
|
10
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ToolRuntime } from "@nexface/tools";
|
|
2
|
+
export interface ToolRenderContext<TInput = unknown, TResult = unknown> {
|
|
3
|
+
input: TInput;
|
|
4
|
+
status: "running" | "success" | "error" | "cancelled";
|
|
5
|
+
result?: TResult;
|
|
6
|
+
}
|
|
7
|
+
export interface ToolPresentation {
|
|
8
|
+
definition?: ReturnType<ToolRuntime["getTools"]>[number]["definition"];
|
|
9
|
+
confirmationProtocol?: true;
|
|
10
|
+
render(context: ToolRenderContext): unknown;
|
|
11
|
+
}
|
|
12
|
+
export interface ToolPresentationController {
|
|
13
|
+
get(name: string): ToolPresentation | undefined;
|
|
14
|
+
names(): readonly string[];
|
|
15
|
+
subscribe(listener: () => void): () => void;
|
|
16
|
+
}
|
|
17
|
+
export declare const toolPresentationSymbol: unique symbol;
|
|
18
|
+
export declare function getToolPresentationController(runtime: ToolRuntime): ToolPresentationController | undefined;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
2
|
+
export type MarkdownDocument = ReturnType<typeof fromMarkdown>;
|
|
3
|
+
export declare const toolResultReferenceLanguages: string[];
|
|
4
|
+
export declare function toolResultBlock(node: MarkdownDocument["children"][number], source: string): {
|
|
5
|
+
start: number;
|
|
6
|
+
end: number;
|
|
7
|
+
id: string | undefined;
|
|
8
|
+
} | undefined;
|
|
9
|
+
export declare function toolResultBlocks(text: string): {
|
|
10
|
+
start: number;
|
|
11
|
+
end: number;
|
|
12
|
+
id: string | undefined;
|
|
13
|
+
}[];
|
|
14
|
+
export declare function filterToolResultBlocks(text: string, keep?: (toolCallId: string) => boolean): string;
|
|
15
|
+
export declare function stripToolResultBlocks(text: string): string;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// Generated from shared/tool-result-markdown.ts. Do not edit manually.
|
|
2
|
+
import { fromMarkdown } from "mdast-util-from-markdown";
|
|
3
|
+
import { isToolResultReferenceId, toolResultReferenceLanguage } from "./tool-result-reference.js";
|
|
4
|
+
export const toolResultReferenceLanguages = [toolResultReferenceLanguage, "nexface-result"];
|
|
5
|
+
export function toolResultBlock(node, source) {
|
|
6
|
+
if (node.type !== "code")
|
|
7
|
+
return;
|
|
8
|
+
const start = node.position?.start.offset;
|
|
9
|
+
const end = node.position?.end.offset;
|
|
10
|
+
if (start === undefined || end === undefined)
|
|
11
|
+
return;
|
|
12
|
+
const [opening, ...lines] = source.slice(start, end).split(/\r\n?|\n/);
|
|
13
|
+
const fence = opening?.match(/^(`{3,}|~{3,})/)?.[1];
|
|
14
|
+
const language = node.lang;
|
|
15
|
+
if (!fence || !language)
|
|
16
|
+
return;
|
|
17
|
+
if (!toolResultReferenceLanguages.includes(language)) {
|
|
18
|
+
// A streaming language token may be split across chunks.
|
|
19
|
+
if (!lines.length &&
|
|
20
|
+
end === source.length &&
|
|
21
|
+
toolResultReferenceLanguages.some((referenceLanguage) => referenceLanguage.startsWith(language)))
|
|
22
|
+
return { start, end, id: undefined };
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const id = node.value.trim();
|
|
26
|
+
const closing = lines.at(-1)?.match(/^ {0,3}(`{3,}|~{3,})[ \t]*$/)?.[1];
|
|
27
|
+
const closed = closing && closing[0] === fence[0] && closing.length >= fence.length;
|
|
28
|
+
return { start, end, id: !node.meta && closed && isToolResultReferenceId(id) ? id : undefined };
|
|
29
|
+
}
|
|
30
|
+
export function toolResultBlocks(text) {
|
|
31
|
+
if (!text.includes("```") && !text.includes("~~~"))
|
|
32
|
+
return [];
|
|
33
|
+
return fromMarkdown(text).children.flatMap((node) => toolResultBlock(node, text) ?? []);
|
|
34
|
+
}
|
|
35
|
+
export function filterToolResultBlocks(text, keep) {
|
|
36
|
+
const blocks = toolResultBlocks(text);
|
|
37
|
+
if (!blocks.length)
|
|
38
|
+
return text;
|
|
39
|
+
let output = "";
|
|
40
|
+
let offset = 0;
|
|
41
|
+
const seen = new Set();
|
|
42
|
+
for (const { start, end, id } of blocks) {
|
|
43
|
+
if (id && keep?.(id) && !seen.has(id)) {
|
|
44
|
+
seen.add(id);
|
|
45
|
+
continue;
|
|
46
|
+
}
|
|
47
|
+
output += text.slice(offset, start);
|
|
48
|
+
offset = end;
|
|
49
|
+
}
|
|
50
|
+
output += text.slice(offset);
|
|
51
|
+
return output.trim() ? output : "";
|
|
52
|
+
}
|
|
53
|
+
export function stripToolResultBlocks(text) {
|
|
54
|
+
return filterToolResultBlocks(text);
|
|
55
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nexface/agent",
|
|
3
|
-
"version": "0.1.21-alpha.
|
|
3
|
+
"version": "0.1.21-alpha.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"engines": {
|
|
@@ -45,7 +45,8 @@
|
|
|
45
45
|
"THIRD_PARTY_LICENSES.txt"
|
|
46
46
|
],
|
|
47
47
|
"dependencies": {
|
|
48
|
-
"@ag-ui/core": "0.0.59"
|
|
48
|
+
"@ag-ui/core": "0.0.59",
|
|
49
|
+
"mdast-util-from-markdown": "2.0.3"
|
|
49
50
|
},
|
|
50
51
|
"typesVersions": {
|
|
51
52
|
"*": {
|
|
@@ -73,7 +74,7 @@
|
|
|
73
74
|
}
|
|
74
75
|
},
|
|
75
76
|
"peerDependencies": {
|
|
76
|
-
"@nexface/tools": "0.1.21-alpha.
|
|
77
|
+
"@nexface/tools": "0.1.21-alpha.5"
|
|
77
78
|
},
|
|
78
79
|
"repository": {
|
|
79
80
|
"type": "git",
|