@cognipeer/agent-sdk 0.4.5 → 0.4.6
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/index.cjs +33 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +33 -5
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2897,6 +2897,21 @@ function isSyntheticSummaryMessage(message) {
|
|
|
2897
2897
|
}
|
|
2898
2898
|
return false;
|
|
2899
2899
|
}
|
|
2900
|
+
function getMessageText(message) {
|
|
2901
|
+
if (typeof message?.content === "string") return message.content;
|
|
2902
|
+
if (Array.isArray(message?.content)) {
|
|
2903
|
+
return message.content.map((chunk) => typeof chunk === "string" ? chunk : chunk?.text ?? chunk?.content ?? "").join("");
|
|
2904
|
+
}
|
|
2905
|
+
return "";
|
|
2906
|
+
}
|
|
2907
|
+
function getLastAssistantMessage(messages) {
|
|
2908
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
2909
|
+
if (messages[index]?.role === "assistant") {
|
|
2910
|
+
return messages[index];
|
|
2911
|
+
}
|
|
2912
|
+
}
|
|
2913
|
+
return void 0;
|
|
2914
|
+
}
|
|
2900
2915
|
function createAgent(opts) {
|
|
2901
2916
|
const resolver = createResolverNode();
|
|
2902
2917
|
const agentCore = createAgentCoreNode(opts);
|
|
@@ -3200,10 +3215,25 @@ function createAgent(opts) {
|
|
|
3200
3215
|
} catch (err) {
|
|
3201
3216
|
const errMsg = err instanceof Error ? err.message : String(err);
|
|
3202
3217
|
emit?.({ type: "metadata", error: `Structured output force-finalize failed: ${errMsg}` });
|
|
3218
|
+
throw err;
|
|
3203
3219
|
}
|
|
3204
3220
|
}
|
|
3205
3221
|
}
|
|
3206
3222
|
}
|
|
3223
|
+
if (state.ctx?.__needsSummarization && !opts?.summarization) {
|
|
3224
|
+
throw new Error(
|
|
3225
|
+
"Agent context exceeded the available budget before a final assistant response could be generated. Reduce tool response size or use createSmartAgent with summarization enabled."
|
|
3226
|
+
);
|
|
3227
|
+
}
|
|
3228
|
+
const lastAfterLoop = state.messages[state.messages.length - 1];
|
|
3229
|
+
if (lastAfterLoop?.role === "tool" && !pausedStage) {
|
|
3230
|
+
const isExpectedExit = state.ctx?.__awaitingApproval || state.ctx?.__cancelled || state.ctx?.__finalizedDueToStructuredOutput || state.ctx?.__finalizedDueToToolLimit || state.ctx?.__needsSummarization;
|
|
3231
|
+
if (!isExpectedExit) {
|
|
3232
|
+
throw new Error(
|
|
3233
|
+
"Agent loop terminated with a pending tool response but no subsequent model invocation. This usually indicates a model provider error or exhausted iteration budget."
|
|
3234
|
+
);
|
|
3235
|
+
}
|
|
3236
|
+
}
|
|
3207
3237
|
if (!pausedStage && typeof onStateChange === "function" && onStateChange(state)) {
|
|
3208
3238
|
checkpointIfRequested("after_loop");
|
|
3209
3239
|
}
|
|
@@ -3273,10 +3303,8 @@ function createAgent(opts) {
|
|
|
3273
3303
|
agentRuntime: res.agent || runtime,
|
|
3274
3304
|
status: "success"
|
|
3275
3305
|
});
|
|
3276
|
-
const
|
|
3277
|
-
|
|
3278
|
-
if (typeof finalMsg?.content === "string") content = finalMsg.content;
|
|
3279
|
-
else if (Array.isArray(finalMsg?.content)) content = finalMsg.content.map((c) => typeof c === "string" ? c : c?.text ?? c?.content ?? "").join("");
|
|
3306
|
+
const finalAssistantMsg = getLastAssistantMessage(res.messages);
|
|
3307
|
+
const content = getMessageText(finalAssistantMsg);
|
|
3280
3308
|
let parsed = void 0;
|
|
3281
3309
|
const schema = opts.outputSchema;
|
|
3282
3310
|
if (schema && res.ctx?.__structuredOutputParsed) {
|
|
@@ -3297,7 +3325,7 @@ function createAgent(opts) {
|
|
|
3297
3325
|
} catch {
|
|
3298
3326
|
}
|
|
3299
3327
|
}
|
|
3300
|
-
emit({ type: "finalAnswer", content
|
|
3328
|
+
emit({ type: "finalAnswer", content });
|
|
3301
3329
|
if (streamEnabled && content) {
|
|
3302
3330
|
onStream?.({ text: content, isFinal: true });
|
|
3303
3331
|
emit({ type: "stream", text: content, isFinal: true });
|