@cognipeer/agent-sdk 0.6.1 → 0.6.3
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/README.md +58 -8
- package/dist/index.cjs +244 -155
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +16 -6
- package/dist/index.d.ts +16 -6
- package/dist/index.mjs +242 -156
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -627,6 +627,7 @@ function buildSystemPrompt(extra, planning, name = "Agent", todoListPrompt) {
|
|
|
627
627
|
"- Keep privacy and safety: do not reveal secrets or sensitive data.",
|
|
628
628
|
"- If inputs are ambiguous or missing, ask one concise clarifying question.",
|
|
629
629
|
"- Reuse prior tool results already present in the conversation when sufficient.",
|
|
630
|
+
'- Use recovery tools such as "get_tool_response" only when the visible transcript explicitly shows a reduced tool-response marker (for example ARCHIVED_TOOL_RESPONSE, STRUCTURED_TOOL_RESPONSE, SUMMARIZED_TOOL_RESPONSE, DROPPED_TOOL_RESPONSE, or a truncation note with an executionId). Never use a recovery tool to re-fetch a normal tool result already visible in context.',
|
|
630
631
|
planningBlock,
|
|
631
632
|
extraTrimmed ? `Extra instructions: ${extraTrimmed}` : ""
|
|
632
633
|
].filter(Boolean).join("\n");
|
|
@@ -2109,8 +2110,6 @@ var BASE_DEFAULTS = {
|
|
|
2109
2110
|
policy: "hybrid",
|
|
2110
2111
|
lastTurnsToKeep: 8,
|
|
2111
2112
|
toolResponsePolicy: "summarize_archive",
|
|
2112
|
-
archiveLargeToolResponses: true,
|
|
2113
|
-
retrieveArchivedToolResponseOnDemand: true,
|
|
2114
2113
|
budget: {
|
|
2115
2114
|
systemReserveTokens: 1200,
|
|
2116
2115
|
goalsReserveTokens: 1200,
|
|
@@ -2143,8 +2142,7 @@ var BASE_DEFAULTS = {
|
|
|
2143
2142
|
defaultPolicy: "summarize_archive",
|
|
2144
2143
|
toolResponseRetentionByTool: {},
|
|
2145
2144
|
criticalTools: [...DEFAULT_CRITICAL_TOOLS],
|
|
2146
|
-
schemaValidation: "strict"
|
|
2147
|
-
retryOnSchemaError: true
|
|
2145
|
+
schemaValidation: "strict"
|
|
2148
2146
|
}
|
|
2149
2147
|
};
|
|
2150
2148
|
function buildProfile(overrides) {
|
|
@@ -2555,6 +2553,7 @@ function createToolsNode(initialTools, opts) {
|
|
|
2555
2553
|
anyTool._stateRef.toolHistory = state.toolHistory;
|
|
2556
2554
|
anyTool._stateRef.toolHistoryArchived = state.toolHistoryArchived;
|
|
2557
2555
|
anyTool._stateRef.pendingApprovals = pendingApprovals;
|
|
2556
|
+
anyTool._stateRef.messages = state.messages;
|
|
2558
2557
|
anyTool._stateRef.ctx = state.ctx || (state.ctx = {});
|
|
2559
2558
|
anyTool._stateRef.__onEvent = onEvent;
|
|
2560
2559
|
}
|
|
@@ -4023,8 +4022,10 @@ function createAgent(opts) {
|
|
|
4023
4022
|
agentRuntime: res.agent || runtime,
|
|
4024
4023
|
status: "success"
|
|
4025
4024
|
});
|
|
4026
|
-
const
|
|
4027
|
-
const
|
|
4025
|
+
const isTransientSummarizationExit = !!res.ctx?.__needsSummarization;
|
|
4026
|
+
const finalAssistantMsg = isTransientSummarizationExit ? void 0 : getLastAssistantMessage(res.messages);
|
|
4027
|
+
const finalIsSyntheticSummary = !isTransientSummarizationExit && finalAssistantMsg && isSyntheticSummaryMessage(finalAssistantMsg);
|
|
4028
|
+
const content = isTransientSummarizationExit || finalIsSyntheticSummary ? "" : extractMessageText2(finalAssistantMsg);
|
|
4028
4029
|
let parsed = void 0;
|
|
4029
4030
|
let outputError = void 0;
|
|
4030
4031
|
if (soManager) {
|
|
@@ -4037,17 +4038,19 @@ function createAgent(opts) {
|
|
|
4037
4038
|
} else {
|
|
4038
4039
|
outputError = fallbackResult.error;
|
|
4039
4040
|
}
|
|
4040
|
-
} else {
|
|
4041
|
+
} else if (!isTransientSummarizationExit) {
|
|
4041
4042
|
const noResult = soManager.noOutputResult(1);
|
|
4042
4043
|
if (!noResult.success) {
|
|
4043
4044
|
outputError = noResult.error;
|
|
4044
4045
|
}
|
|
4045
4046
|
}
|
|
4046
4047
|
}
|
|
4047
|
-
|
|
4048
|
-
|
|
4049
|
-
|
|
4050
|
-
|
|
4048
|
+
if (!isTransientSummarizationExit && !finalIsSyntheticSummary) {
|
|
4049
|
+
emit({ type: "finalAnswer", content });
|
|
4050
|
+
if (streamEnabled && content) {
|
|
4051
|
+
onStream?.({ text: content, isFinal: true });
|
|
4052
|
+
emit({ type: "stream", text: content, isFinal: true });
|
|
4053
|
+
}
|
|
4051
4054
|
}
|
|
4052
4055
|
return {
|
|
4053
4056
|
content,
|
|
@@ -4845,6 +4848,13 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
|
|
|
4845
4848
|
};
|
|
4846
4849
|
};
|
|
4847
4850
|
}
|
|
4851
|
+
var TOOL_RESPONSE_RECOVERY_MARKERS = [
|
|
4852
|
+
/ARCHIVED_TOOL_RESPONSE\s*\[/,
|
|
4853
|
+
/STRUCTURED_TOOL_RESPONSE\s*\[/,
|
|
4854
|
+
/SUMMARIZED_TOOL_RESPONSE\b/,
|
|
4855
|
+
/DROPPED_TOOL_RESPONSE\s*\[/,
|
|
4856
|
+
/Use get_tool_response with executionId/i
|
|
4857
|
+
];
|
|
4848
4858
|
var todoStatusSchema = zod.z.enum(["not-started", "in-progress", "completed", "blocked"]);
|
|
4849
4859
|
var todoWriteItemSchema = zod.z.object({
|
|
4850
4860
|
id: zod.z.number().int().min(1).describe("Sequential id starting from 1"),
|
|
@@ -4913,180 +4923,223 @@ function formatTodoListSummary(todoList) {
|
|
|
4913
4923
|
}
|
|
4914
4924
|
return todoList.map((item) => `${item.id}. ${item.title || item.step || item.description || `Step ${item.id}`} [${item.status}]`).join("\n");
|
|
4915
4925
|
}
|
|
4916
|
-
function
|
|
4917
|
-
|
|
4918
|
-
|
|
4919
|
-
|
|
4920
|
-
|
|
4921
|
-
|
|
4922
|
-
|
|
4923
|
-
|
|
4924
|
-
|
|
4925
|
-
|
|
4926
|
-
|
|
4927
|
-
|
|
4928
|
-
|
|
4929
|
-
|
|
4930
|
-
|
|
4931
|
-
|
|
4932
|
-
|
|
4933
|
-
|
|
4934
|
-
|
|
4926
|
+
function flattenRecoveryContent(content) {
|
|
4927
|
+
if (typeof content === "string") {
|
|
4928
|
+
return content;
|
|
4929
|
+
}
|
|
4930
|
+
if (Array.isArray(content)) {
|
|
4931
|
+
return content.map((part) => {
|
|
4932
|
+
if (typeof part === "string") {
|
|
4933
|
+
return part;
|
|
4934
|
+
}
|
|
4935
|
+
if (part && typeof part === "object") {
|
|
4936
|
+
if (typeof part.text === "string") {
|
|
4937
|
+
return part.text;
|
|
4938
|
+
}
|
|
4939
|
+
if (typeof part.content === "string") {
|
|
4940
|
+
return part.content;
|
|
4941
|
+
}
|
|
4942
|
+
}
|
|
4943
|
+
return "";
|
|
4944
|
+
}).filter(Boolean).join("\n");
|
|
4945
|
+
}
|
|
4946
|
+
return "";
|
|
4947
|
+
}
|
|
4948
|
+
function hasToolResponseRecoveryReference(messages, executionId) {
|
|
4949
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
4950
|
+
return false;
|
|
4951
|
+
}
|
|
4952
|
+
return messages.some((message) => {
|
|
4953
|
+
if (message?.role === "system") {
|
|
4954
|
+
return false;
|
|
4955
|
+
}
|
|
4956
|
+
const content = flattenRecoveryContent(message?.content);
|
|
4957
|
+
if (!content) {
|
|
4958
|
+
return false;
|
|
4959
|
+
}
|
|
4960
|
+
const hasRecoveryMarker = TOOL_RESPONSE_RECOVERY_MARKERS.some((pattern) => pattern.test(content));
|
|
4961
|
+
if (!hasRecoveryMarker) {
|
|
4962
|
+
return false;
|
|
4963
|
+
}
|
|
4964
|
+
return executionId ? content.includes(executionId) : true;
|
|
4965
|
+
});
|
|
4966
|
+
}
|
|
4967
|
+
function createManageTodoTool(stateRef) {
|
|
4968
|
+
const manageTodo = createTool({
|
|
4969
|
+
name: "manage_todo_list",
|
|
4970
|
+
description: "Manage a structured todo list to track progress and plan tasks throughout your coding session.\n\nOperations:\n- read: return the current plan\n- write: replace the full plan with a complete ordered todoList\n- update: patch existing todo items by id without resending the whole plan\n\nRules:\n- Use write only when creating or fully rewriting the plan\n- After a plan exists, prefer update for status, evidence, or owner changes\n- Update payloads should contain only the changed items\n- When using update, pass expectedVersion to avoid overwriting a newer plan\n- Keep ids unique; write operations must keep ids sequential starting from 1\n- Keep at most ONE item in-progress at a time\n- If update fails due to version mismatch, read the latest plan and retry",
|
|
4971
|
+
schema: zod.z.object({
|
|
4972
|
+
operation: zod.z.enum(["write", "read", "update"]),
|
|
4973
|
+
expectedVersion: zod.z.number().int().min(0).optional(),
|
|
4974
|
+
todoList: zod.z.array(zod.z.union([todoWriteItemSchema, todoUpdateItemSchema])).optional()
|
|
4975
|
+
}),
|
|
4976
|
+
func: async ({ operation, expectedVersion, todoList }) => {
|
|
4977
|
+
const toolStateRef = manageTodo._stateRef;
|
|
4978
|
+
const onEvent = toolStateRef?.__onEvent;
|
|
4979
|
+
const emitPlanEvent = (list) => {
|
|
4980
|
+
const version = stateRef.planVersion || 1;
|
|
4981
|
+
const adherenceScore = stateRef.adherenceScore || 0;
|
|
4982
|
+
const planData = {
|
|
4983
|
+
source: "manage_todo_list",
|
|
4984
|
+
operation,
|
|
4985
|
+
version,
|
|
4986
|
+
adherenceScore,
|
|
4987
|
+
count: Array.isArray(list) ? list.length : 0
|
|
4988
|
+
};
|
|
4989
|
+
onEvent?.({ type: "plan", todoList: list, ...planData });
|
|
4990
|
+
const traceSession = toolStateRef?.ctx?.__traceSession;
|
|
4991
|
+
recordTraceEvent(traceSession, {
|
|
4992
|
+
type: "plan",
|
|
4993
|
+
label: `Plan ${operation}`,
|
|
4994
|
+
actor: { scope: "agent", name: "manage_todo_list", role: "planner" },
|
|
4995
|
+
sections: [
|
|
4996
|
+
{
|
|
4997
|
+
kind: "summary",
|
|
4998
|
+
label: "Todo List",
|
|
4999
|
+
content: formatTodoListSummary(list)
|
|
5000
|
+
},
|
|
5001
|
+
{
|
|
5002
|
+
kind: "metadata",
|
|
5003
|
+
label: "Plan Metadata",
|
|
5004
|
+
data: planData
|
|
5005
|
+
}
|
|
5006
|
+
],
|
|
5007
|
+
debug: planData
|
|
5008
|
+
});
|
|
5009
|
+
};
|
|
5010
|
+
const currentVersion = stateRef.planVersion || 0;
|
|
5011
|
+
if (operation === "read") {
|
|
5012
|
+
const list = stateRef.todoList || [];
|
|
5013
|
+
emitPlanEvent(list);
|
|
5014
|
+
return list;
|
|
5015
|
+
}
|
|
5016
|
+
if (typeof expectedVersion === "number" && expectedVersion !== currentVersion) {
|
|
5017
|
+
return {
|
|
5018
|
+
status: "error",
|
|
5019
|
+
operation,
|
|
5020
|
+
error: "Plan version mismatch. Read the latest plan and retry.",
|
|
5021
|
+
version: currentVersion,
|
|
5022
|
+
adherenceScore: stateRef.adherenceScore || 0
|
|
5023
|
+
};
|
|
5024
|
+
}
|
|
5025
|
+
if (!Array.isArray(todoList)) {
|
|
5026
|
+
return {
|
|
5027
|
+
status: "error",
|
|
5028
|
+
operation,
|
|
5029
|
+
error: "todoList is required for write and update operations.",
|
|
5030
|
+
version: currentVersion,
|
|
5031
|
+
adherenceScore: stateRef.adherenceScore || 0
|
|
5032
|
+
};
|
|
5033
|
+
}
|
|
5034
|
+
if (operation === "write") {
|
|
5035
|
+
const parsed = zod.z.array(todoWriteItemSchema).safeParse(todoList);
|
|
5036
|
+
if (!parsed.success) {
|
|
5037
|
+
return {
|
|
5038
|
+
status: "error",
|
|
4935
5039
|
operation,
|
|
4936
|
-
|
|
4937
|
-
|
|
4938
|
-
|
|
5040
|
+
error: parsed.error.issues[0]?.message || "Invalid todoList for write.",
|
|
5041
|
+
version: currentVersion,
|
|
5042
|
+
adherenceScore: stateRef.adherenceScore || 0
|
|
4939
5043
|
};
|
|
4940
|
-
onEvent?.({ type: "plan", todoList: list, ...planData });
|
|
4941
|
-
const traceSession = toolStateRef?.ctx?.__traceSession;
|
|
4942
|
-
recordTraceEvent(traceSession, {
|
|
4943
|
-
type: "plan",
|
|
4944
|
-
label: `Plan ${operation}`,
|
|
4945
|
-
actor: { scope: "agent", name: "manage_todo_list", role: "planner" },
|
|
4946
|
-
sections: [
|
|
4947
|
-
{
|
|
4948
|
-
kind: "summary",
|
|
4949
|
-
label: "Todo List",
|
|
4950
|
-
content: formatTodoListSummary(list)
|
|
4951
|
-
},
|
|
4952
|
-
{
|
|
4953
|
-
kind: "metadata",
|
|
4954
|
-
label: "Plan Metadata",
|
|
4955
|
-
data: planData
|
|
4956
|
-
}
|
|
4957
|
-
],
|
|
4958
|
-
debug: planData
|
|
4959
|
-
});
|
|
4960
|
-
};
|
|
4961
|
-
const currentVersion = stateRef.planVersion || 0;
|
|
4962
|
-
if (operation === "read") {
|
|
4963
|
-
const list = stateRef.todoList || [];
|
|
4964
|
-
emitPlanEvent(list);
|
|
4965
|
-
return list;
|
|
4966
5044
|
}
|
|
4967
|
-
|
|
5045
|
+
const normalizedList = parsed.data.map((item) => normalizeTodoItem(item));
|
|
5046
|
+
const validation = validatePlanInvariants(normalizedList, true);
|
|
5047
|
+
if (!validation.ok) {
|
|
4968
5048
|
return {
|
|
4969
5049
|
status: "error",
|
|
4970
5050
|
operation,
|
|
4971
|
-
error:
|
|
5051
|
+
error: validation.error,
|
|
4972
5052
|
version: currentVersion,
|
|
4973
5053
|
adherenceScore: stateRef.adherenceScore || 0
|
|
4974
5054
|
};
|
|
4975
5055
|
}
|
|
4976
|
-
|
|
5056
|
+
stateRef.todoList = normalizedList;
|
|
5057
|
+
} else if (operation === "update") {
|
|
5058
|
+
const parsed = zod.z.array(todoUpdateItemSchema).safeParse(todoList);
|
|
5059
|
+
if (!parsed.success) {
|
|
4977
5060
|
return {
|
|
4978
5061
|
status: "error",
|
|
4979
5062
|
operation,
|
|
4980
|
-
error: "todoList
|
|
5063
|
+
error: parsed.error.issues[0]?.message || "Invalid todoList for update.",
|
|
4981
5064
|
version: currentVersion,
|
|
4982
5065
|
adherenceScore: stateRef.adherenceScore || 0
|
|
4983
5066
|
};
|
|
4984
5067
|
}
|
|
4985
|
-
|
|
4986
|
-
|
|
4987
|
-
|
|
4988
|
-
|
|
4989
|
-
|
|
4990
|
-
|
|
4991
|
-
|
|
4992
|
-
|
|
4993
|
-
|
|
4994
|
-
|
|
4995
|
-
|
|
4996
|
-
|
|
4997
|
-
|
|
4998
|
-
if (
|
|
4999
|
-
return {
|
|
5000
|
-
status: "error",
|
|
5001
|
-
operation,
|
|
5002
|
-
error: validation.error,
|
|
5003
|
-
version: currentVersion,
|
|
5004
|
-
adherenceScore: stateRef.adherenceScore || 0
|
|
5005
|
-
};
|
|
5006
|
-
}
|
|
5007
|
-
stateRef.todoList = normalizedList;
|
|
5008
|
-
} else if (operation === "update") {
|
|
5009
|
-
const parsed = zod.z.array(todoUpdateItemSchema).safeParse(todoList);
|
|
5010
|
-
if (!parsed.success) {
|
|
5011
|
-
return {
|
|
5012
|
-
status: "error",
|
|
5013
|
-
operation,
|
|
5014
|
-
error: parsed.error.issues[0]?.message || "Invalid todoList for update.",
|
|
5015
|
-
version: currentVersion,
|
|
5016
|
-
adherenceScore: stateRef.adherenceScore || 0
|
|
5017
|
-
};
|
|
5018
|
-
}
|
|
5019
|
-
const currentList = Array.isArray(stateRef.todoList) ? stateRef.todoList : [];
|
|
5020
|
-
if (currentList.length === 0) {
|
|
5068
|
+
const currentList = Array.isArray(stateRef.todoList) ? stateRef.todoList : [];
|
|
5069
|
+
if (currentList.length === 0) {
|
|
5070
|
+
return {
|
|
5071
|
+
status: "error",
|
|
5072
|
+
operation,
|
|
5073
|
+
error: "No existing plan to update. Use write first.",
|
|
5074
|
+
version: currentVersion,
|
|
5075
|
+
adherenceScore: stateRef.adherenceScore || 0
|
|
5076
|
+
};
|
|
5077
|
+
}
|
|
5078
|
+
const currentMap = new Map(currentList.map((item) => [item.id, item]));
|
|
5079
|
+
const patchMap = /* @__PURE__ */ new Map();
|
|
5080
|
+
for (const item of parsed.data) {
|
|
5081
|
+
if (patchMap.has(item.id)) {
|
|
5021
5082
|
return {
|
|
5022
5083
|
status: "error",
|
|
5023
5084
|
operation,
|
|
5024
|
-
error: "
|
|
5085
|
+
error: "Update payload contains duplicate todo ids.",
|
|
5025
5086
|
version: currentVersion,
|
|
5026
5087
|
adherenceScore: stateRef.adherenceScore || 0
|
|
5027
5088
|
};
|
|
5028
5089
|
}
|
|
5029
|
-
|
|
5030
|
-
|
|
5031
|
-
for (const item of parsed.data) {
|
|
5032
|
-
if (patchMap.has(item.id)) {
|
|
5033
|
-
return {
|
|
5034
|
-
status: "error",
|
|
5035
|
-
operation,
|
|
5036
|
-
error: "Update payload contains duplicate todo ids.",
|
|
5037
|
-
version: currentVersion,
|
|
5038
|
-
adherenceScore: stateRef.adherenceScore || 0
|
|
5039
|
-
};
|
|
5040
|
-
}
|
|
5041
|
-
patchMap.set(item.id, item);
|
|
5042
|
-
if (!currentMap.has(item.id)) {
|
|
5043
|
-
return {
|
|
5044
|
-
status: "error",
|
|
5045
|
-
operation,
|
|
5046
|
-
error: `Cannot update missing todo id ${item.id}. Use write to replace the plan.`,
|
|
5047
|
-
version: currentVersion,
|
|
5048
|
-
adherenceScore: stateRef.adherenceScore || 0
|
|
5049
|
-
};
|
|
5050
|
-
}
|
|
5051
|
-
}
|
|
5052
|
-
const mergedList = currentList.map((item) => {
|
|
5053
|
-
const patch = patchMap.get(item.id);
|
|
5054
|
-
return patch ? normalizeTodoItem(patch, item) : item;
|
|
5055
|
-
});
|
|
5056
|
-
const validation = validatePlanInvariants(mergedList, false);
|
|
5057
|
-
if (!validation.ok) {
|
|
5090
|
+
patchMap.set(item.id, item);
|
|
5091
|
+
if (!currentMap.has(item.id)) {
|
|
5058
5092
|
return {
|
|
5059
5093
|
status: "error",
|
|
5060
5094
|
operation,
|
|
5061
|
-
error:
|
|
5095
|
+
error: `Cannot update missing todo id ${item.id}. Use write to replace the plan.`,
|
|
5062
5096
|
version: currentVersion,
|
|
5063
5097
|
adherenceScore: stateRef.adherenceScore || 0
|
|
5064
5098
|
};
|
|
5065
5099
|
}
|
|
5066
|
-
stateRef.todoList = mergedList;
|
|
5067
5100
|
}
|
|
5068
|
-
|
|
5069
|
-
|
|
5070
|
-
|
|
5071
|
-
|
|
5072
|
-
|
|
5073
|
-
|
|
5074
|
-
|
|
5075
|
-
|
|
5076
|
-
|
|
5077
|
-
|
|
5078
|
-
|
|
5079
|
-
|
|
5080
|
-
|
|
5081
|
-
|
|
5082
|
-
|
|
5083
|
-
|
|
5101
|
+
const mergedList = currentList.map((item) => {
|
|
5102
|
+
const patch = patchMap.get(item.id);
|
|
5103
|
+
return patch ? normalizeTodoItem(patch, item) : item;
|
|
5104
|
+
});
|
|
5105
|
+
const validation = validatePlanInvariants(mergedList, false);
|
|
5106
|
+
if (!validation.ok) {
|
|
5107
|
+
return {
|
|
5108
|
+
status: "error",
|
|
5109
|
+
operation,
|
|
5110
|
+
error: validation.error,
|
|
5111
|
+
version: currentVersion,
|
|
5112
|
+
adherenceScore: stateRef.adherenceScore || 0
|
|
5113
|
+
};
|
|
5114
|
+
}
|
|
5115
|
+
stateRef.todoList = mergedList;
|
|
5116
|
+
}
|
|
5117
|
+
stateRef.planVersion = currentVersion + 1;
|
|
5118
|
+
stateRef.adherenceScore = calculateAdherenceScore(stateRef.todoList || []);
|
|
5119
|
+
const payload = {
|
|
5120
|
+
status: "ok",
|
|
5121
|
+
operation,
|
|
5122
|
+
count: Array.isArray(todoList) ? todoList.length : void 0,
|
|
5123
|
+
version: stateRef.planVersion || 1,
|
|
5124
|
+
adherenceScore: stateRef.adherenceScore || 0
|
|
5125
|
+
};
|
|
5126
|
+
emitPlanEvent(stateRef.todoList || []);
|
|
5127
|
+
return payload;
|
|
5128
|
+
}
|
|
5129
|
+
});
|
|
5130
|
+
manageTodo._stateRef = stateRef;
|
|
5131
|
+
return manageTodo;
|
|
5132
|
+
}
|
|
5133
|
+
function createGetToolResponseTool(stateRef) {
|
|
5084
5134
|
const getTool = createTool({
|
|
5085
5135
|
name: "get_tool_response",
|
|
5086
|
-
description: "RETRIEVE the full output of a tool execution whose response was archived, dropped, or later compacted in conversation history.
|
|
5136
|
+
description: "RETRIEVE the full output of a tool execution whose response was archived, dropped, truncated, or later compacted in conversation history. This tool is only useful when the visible transcript already shows a reduced tool-response marker for the referenced executionId or tool_call_id. Use it only when a marker such as 'ARCHIVED_TOOL_RESPONSE', 'STRUCTURED_TOOL_RESPONSE', 'SUMMARIZED_TOOL_RESPONSE', 'DROPPED_TOOL_RESPONSE', or an explicit truncation note points you to a missing field you still need. Do not call it to re-fetch a normal tool result that is already present in context.",
|
|
5087
5137
|
schema: zod.z.object({ executionId: zod.z.string().describe("Tool execution id or original tool_call_id") }),
|
|
5088
5138
|
maxExecutionsPerRun: 8,
|
|
5089
5139
|
func: async ({ executionId }) => {
|
|
5140
|
+
if (!hasToolResponseRecoveryReference(stateRef.messages, executionId)) {
|
|
5141
|
+
return "Execution not recoverable from the visible transcript. Use this only when the conversation already shows a reduced tool-response marker for that executionId.";
|
|
5142
|
+
}
|
|
5090
5143
|
const matchesExecution = (t) => t?.executionId === executionId || t?.tool_call_id === executionId;
|
|
5091
5144
|
let execution = stateRef.toolHistory?.find((t) => matchesExecution(t));
|
|
5092
5145
|
if (!execution) {
|
|
@@ -5099,7 +5152,16 @@ function createContextTools(stateRef, opts) {
|
|
|
5099
5152
|
}
|
|
5100
5153
|
});
|
|
5101
5154
|
getTool._stateRef = stateRef;
|
|
5102
|
-
|
|
5155
|
+
return getTool;
|
|
5156
|
+
}
|
|
5157
|
+
function createContextTools(stateRef, opts) {
|
|
5158
|
+
const tools = [];
|
|
5159
|
+
if (opts?.planningEnabled) {
|
|
5160
|
+
tools.push(createManageTodoTool(stateRef));
|
|
5161
|
+
}
|
|
5162
|
+
if (opts?.includeGetToolResponse ?? true) {
|
|
5163
|
+
tools.push(createGetToolResponseTool(stateRef));
|
|
5164
|
+
}
|
|
5103
5165
|
return tools;
|
|
5104
5166
|
}
|
|
5105
5167
|
|
|
@@ -5267,9 +5329,11 @@ function createSmartAgent(opts) {
|
|
|
5267
5329
|
useTodoList: planningEnabled
|
|
5268
5330
|
};
|
|
5269
5331
|
const stateRef = { toolHistory: void 0, toolHistoryArchived: void 0, todoList: void 0, planVersion: 0, adherenceScore: 0 };
|
|
5270
|
-
const contextTools = createContextTools(stateRef, { planningEnabled });
|
|
5271
|
-
const
|
|
5272
|
-
const
|
|
5332
|
+
const contextTools = createContextTools(stateRef, { planningEnabled, includeGetToolResponse: false });
|
|
5333
|
+
const getToolResponseTool = createGetToolResponseTool(stateRef);
|
|
5334
|
+
const toolsWithoutRecovery = [...opts.tools ?? [], ...contextTools];
|
|
5335
|
+
const toolsWithRecovery = [...toolsWithoutRecovery, getToolResponseTool];
|
|
5336
|
+
const base = createAgent({ ...runtimeOpts, tools: toolsWithoutRecovery });
|
|
5273
5337
|
base.__runtime.runtimeProfile = resolved.runtimeProfile;
|
|
5274
5338
|
base.__runtime.smart = resolved;
|
|
5275
5339
|
const summarizationEnabled = resolved.summarization.enable !== false;
|
|
@@ -5327,6 +5391,27 @@ function createSmartAgent(opts) {
|
|
|
5327
5391
|
);
|
|
5328
5392
|
return { role: "system", content: sys };
|
|
5329
5393
|
}
|
|
5394
|
+
function syncRuntimeTools(currentState, currentMessages) {
|
|
5395
|
+
const needsRecoveryTool = hasToolResponseRecoveryReference(currentMessages);
|
|
5396
|
+
const nextTools = needsRecoveryTool ? toolsWithRecovery : toolsWithoutRecovery;
|
|
5397
|
+
const currentRuntime = currentState.agent || base.__runtime;
|
|
5398
|
+
if (currentRuntime.tools === nextTools) {
|
|
5399
|
+
return currentState;
|
|
5400
|
+
}
|
|
5401
|
+
return {
|
|
5402
|
+
...currentState,
|
|
5403
|
+
agent: {
|
|
5404
|
+
...currentRuntime,
|
|
5405
|
+
tools: nextTools
|
|
5406
|
+
}
|
|
5407
|
+
};
|
|
5408
|
+
}
|
|
5409
|
+
function producedTerminalAssistantTurn(messages) {
|
|
5410
|
+
const lastAssistant = [...messages].reverse().find((message) => message?.role === "assistant");
|
|
5411
|
+
if (!lastAssistant) return false;
|
|
5412
|
+
const toolCalls = Array.isArray(lastAssistant.tool_calls) ? lastAssistant.tool_calls : [];
|
|
5413
|
+
return toolCalls.length === 0;
|
|
5414
|
+
}
|
|
5330
5415
|
async function trySummarize(currentState, currentRawMessages) {
|
|
5331
5416
|
if (!summarizer) return { state: currentState, rawMessages: currentRawMessages, compressed: false };
|
|
5332
5417
|
const delta = await summarizer(currentState);
|
|
@@ -5348,13 +5433,13 @@ function createSmartAgent(opts) {
|
|
|
5348
5433
|
stateRef.adherenceScore = input.plan?.adherenceScore || 0;
|
|
5349
5434
|
const alreadyHasSystem = Array.isArray(input.messages) && input.messages[0]?.role === "system";
|
|
5350
5435
|
const seedMessages = alreadyHasSystem ? [...input.messages || []] : [systemMessage2(), ...input.messages || []];
|
|
5351
|
-
let state = await syncMemory({ ...input, messages: seedMessages });
|
|
5436
|
+
let state = syncRuntimeTools(await syncMemory({ ...input, messages: seedMessages }), seedMessages);
|
|
5352
5437
|
let lastResult = null;
|
|
5353
5438
|
let rawMessages = [...seedMessages];
|
|
5354
5439
|
const effectiveMaxToolCalls = config?.limits?.maxToolCalls ?? resolved.limits.maxToolCalls ?? 10;
|
|
5355
5440
|
const iterationLimit = Math.max(effectiveMaxToolCalls * 3 + 5, 30);
|
|
5356
5441
|
for (let i = 0; i < iterationLimit; i++) {
|
|
5357
|
-
state = { ...state, messages: rawMessages };
|
|
5442
|
+
state = syncRuntimeTools({ ...state, messages: rawMessages }, rawMessages);
|
|
5358
5443
|
const next = summarizationEnabled ? decideBefore(state) : "agent";
|
|
5359
5444
|
if (next === "contextSummarize" && summarizer) {
|
|
5360
5445
|
const result = await trySummarize(state, rawMessages);
|
|
@@ -5381,7 +5466,7 @@ function createSmartAgent(opts) {
|
|
|
5381
5466
|
if (currentPlan && !state.plan) {
|
|
5382
5467
|
state = { ...state, plan: currentPlan };
|
|
5383
5468
|
}
|
|
5384
|
-
state = syncPlanState(await syncMemory(state));
|
|
5469
|
+
state = syncRuntimeTools(syncPlanState(await syncMemory(state)), rawMessages);
|
|
5385
5470
|
stateRef.toolHistory = state.toolHistory;
|
|
5386
5471
|
stateRef.toolHistoryArchived = state.toolHistoryArchived;
|
|
5387
5472
|
if (state.ctx?.__needsSummarization && summarizer) {
|
|
@@ -5395,6 +5480,7 @@ function createSmartAgent(opts) {
|
|
|
5395
5480
|
}
|
|
5396
5481
|
if (state.ctx?.__finalizedDueToStructuredOutput) break;
|
|
5397
5482
|
if (opts.outputSchema && lastResult?.output != null) break;
|
|
5483
|
+
if (producedTerminalAssistantTurn(appendedMessages)) break;
|
|
5398
5484
|
if (summarizationEnabled) {
|
|
5399
5485
|
const after = decideAfter(state);
|
|
5400
5486
|
if (after === "contextSummarize" && summarizer) {
|
|
@@ -7694,8 +7780,10 @@ exports.createAgentNode = createAgentNode;
|
|
|
7694
7780
|
exports.createCodeGuardrail = createCodeGuardrail;
|
|
7695
7781
|
exports.createContextSummarizeNode = createContextSummarizeNode;
|
|
7696
7782
|
exports.createContextTools = createContextTools;
|
|
7783
|
+
exports.createGetToolResponseTool = createGetToolResponseTool;
|
|
7697
7784
|
exports.createGuardrail = createGuardrail;
|
|
7698
7785
|
exports.createJsonGuardrail = createJsonGuardrail;
|
|
7786
|
+
exports.createManageTodoTool = createManageTodoTool;
|
|
7699
7787
|
exports.createProvider = createProvider;
|
|
7700
7788
|
exports.createRegexGuardrail = createRegexGuardrail;
|
|
7701
7789
|
exports.createResolverNode = createResolverNode;
|
|
@@ -7717,6 +7805,7 @@ exports.generateTraceId = generateTraceId;
|
|
|
7717
7805
|
exports.getModelCapabilities = getModelCapabilities;
|
|
7718
7806
|
exports.getNestedValue = getNestedValue;
|
|
7719
7807
|
exports.getResolvedSmartConfig = getResolvedSmartConfig;
|
|
7808
|
+
exports.hasToolResponseRecoveryReference = hasToolResponseRecoveryReference;
|
|
7720
7809
|
exports.httpSink = httpSink;
|
|
7721
7810
|
exports.isSmartModel = isSmartModel;
|
|
7722
7811
|
exports.jsonSchemaRule = jsonSchemaRule;
|