@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.mjs CHANGED
@@ -2887,6 +2887,21 @@ function isSyntheticSummaryMessage(message) {
2887
2887
  }
2888
2888
  return false;
2889
2889
  }
2890
+ function getMessageText(message) {
2891
+ if (typeof message?.content === "string") return message.content;
2892
+ if (Array.isArray(message?.content)) {
2893
+ return message.content.map((chunk) => typeof chunk === "string" ? chunk : chunk?.text ?? chunk?.content ?? "").join("");
2894
+ }
2895
+ return "";
2896
+ }
2897
+ function getLastAssistantMessage(messages) {
2898
+ for (let index = messages.length - 1; index >= 0; index -= 1) {
2899
+ if (messages[index]?.role === "assistant") {
2900
+ return messages[index];
2901
+ }
2902
+ }
2903
+ return void 0;
2904
+ }
2890
2905
  function createAgent(opts) {
2891
2906
  const resolver = createResolverNode();
2892
2907
  const agentCore = createAgentCoreNode(opts);
@@ -3190,10 +3205,25 @@ function createAgent(opts) {
3190
3205
  } catch (err) {
3191
3206
  const errMsg = err instanceof Error ? err.message : String(err);
3192
3207
  emit?.({ type: "metadata", error: `Structured output force-finalize failed: ${errMsg}` });
3208
+ throw err;
3193
3209
  }
3194
3210
  }
3195
3211
  }
3196
3212
  }
3213
+ if (state.ctx?.__needsSummarization && !opts?.summarization) {
3214
+ throw new Error(
3215
+ "Agent context exceeded the available budget before a final assistant response could be generated. Reduce tool response size or use createSmartAgent with summarization enabled."
3216
+ );
3217
+ }
3218
+ const lastAfterLoop = state.messages[state.messages.length - 1];
3219
+ if (lastAfterLoop?.role === "tool" && !pausedStage) {
3220
+ const isExpectedExit = state.ctx?.__awaitingApproval || state.ctx?.__cancelled || state.ctx?.__finalizedDueToStructuredOutput || state.ctx?.__finalizedDueToToolLimit || state.ctx?.__needsSummarization;
3221
+ if (!isExpectedExit) {
3222
+ throw new Error(
3223
+ "Agent loop terminated with a pending tool response but no subsequent model invocation. This usually indicates a model provider error or exhausted iteration budget."
3224
+ );
3225
+ }
3226
+ }
3197
3227
  if (!pausedStage && typeof onStateChange === "function" && onStateChange(state)) {
3198
3228
  checkpointIfRequested("after_loop");
3199
3229
  }
@@ -3263,10 +3293,8 @@ function createAgent(opts) {
3263
3293
  agentRuntime: res.agent || runtime,
3264
3294
  status: "success"
3265
3295
  });
3266
- const finalMsg = res.messages[res.messages.length - 1];
3267
- let content = "";
3268
- if (typeof finalMsg?.content === "string") content = finalMsg.content;
3269
- else if (Array.isArray(finalMsg?.content)) content = finalMsg.content.map((c) => typeof c === "string" ? c : c?.text ?? c?.content ?? "").join("");
3296
+ const finalAssistantMsg = getLastAssistantMessage(res.messages);
3297
+ const content = getMessageText(finalAssistantMsg);
3270
3298
  let parsed = void 0;
3271
3299
  const schema = opts.outputSchema;
3272
3300
  if (schema && res.ctx?.__structuredOutputParsed) {
@@ -3287,7 +3315,7 @@ function createAgent(opts) {
3287
3315
  } catch {
3288
3316
  }
3289
3317
  }
3290
- emit({ type: "finalAnswer", content: typeof finalMsg?.content === "string" ? finalMsg.content : content });
3318
+ emit({ type: "finalAnswer", content });
3291
3319
  if (streamEnabled && content) {
3292
3320
  onStream?.({ text: content, isFinal: true });
3293
3321
  emit({ type: "stream", text: content, isFinal: true });