@cognipeer/agent-sdk 0.4.8 → 0.4.9

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 CHANGED
@@ -3157,7 +3157,23 @@ function createAgent(opts) {
3157
3157
  state = { ...state, ...await finalizeNode(state) };
3158
3158
  continue;
3159
3159
  }
3160
- if (toolCalls.length === 0) break;
3160
+ if (toolCalls.length === 0) {
3161
+ if (opts.outputSchema && !state.ctx?.__finalizedDueToStructuredOutput && !state.ctx?.__structuredOutputForceFinalize) {
3162
+ const forceMsg = {
3163
+ role: "system",
3164
+ content: [
3165
+ "A structured output schema is active.",
3166
+ "You MUST now call tool `response` with the final JSON object that matches the schema.",
3167
+ "Do not write the JSON in the assistant message.",
3168
+ "Call `response` exactly once, then stop."
3169
+ ].join("\n")
3170
+ };
3171
+ const ctx = { ...state.ctx || {}, __structuredOutputForceFinalize: true };
3172
+ state = { ...state, messages: [...state.messages, forceMsg], ctx };
3173
+ continue;
3174
+ }
3175
+ break;
3176
+ }
3161
3177
  onProgress?.({ stage: "tools", message: "Running tools" });
3162
3178
  if (cancelIfRequested("before_tools")) break;
3163
3179
  state = { ...state, ...await toolsNode(state) };
@@ -3170,31 +3186,31 @@ function createAgent(opts) {
3170
3186
  const last = state.messages[state.messages.length - 1];
3171
3187
  const lastHasToolCalls = Array.isArray(last?.tool_calls) && last.tool_calls.length > 0;
3172
3188
  if (!lastHasToolCalls) {
3173
- const forceMsg = {
3174
- role: "system",
3175
- content: [
3176
- "A structured output schema is active.",
3177
- "You MUST now call tool `response` with the final JSON object that matches the schema.",
3178
- "Do not write the JSON in the assistant message.",
3179
- "Call `response` exactly once, then stop."
3180
- ].join("\n")
3181
- };
3182
3189
  const alreadyForced = Boolean(state.ctx?.__structuredOutputForceFinalize);
3183
- if (!alreadyForced) {
3184
- const ctx = { ...state.ctx || {}, __structuredOutputForceFinalize: true };
3185
- state = { ...state, messages: [...state.messages, forceMsg], ctx };
3186
- try {
3187
- state = { ...state, ...await agentCore(state) };
3188
- const lastAfter = state.messages[state.messages.length - 1];
3189
- const toolCallsAfter = Array.isArray(lastAfter?.tool_calls) ? lastAfter.tool_calls : [];
3190
- if (toolCallsAfter.length > 0) {
3191
- state = { ...state, ...await toolsNode(state) };
3192
- }
3193
- } catch (err) {
3194
- const errMsg = err instanceof Error ? err.message : String(err);
3195
- emit?.({ type: "metadata", error: `Structured output force-finalize failed: ${errMsg}` });
3196
- throw err;
3190
+ if (alreadyForced) {
3191
+ const finalNudge = {
3192
+ role: "system",
3193
+ content: [
3194
+ "A structured output schema is active.",
3195
+ "You MUST now call tool `response` with the final JSON object that matches the schema.",
3196
+ "Do not write the JSON in the assistant message.",
3197
+ "Call `response` exactly once, then stop.",
3198
+ "Do NOT call any other tools. Produce the response NOW based on whatever information you have gathered so far."
3199
+ ].join("\n")
3200
+ };
3201
+ state = { ...state, messages: [...state.messages, finalNudge] };
3202
+ }
3203
+ try {
3204
+ state = { ...state, ...await agentCore(state) };
3205
+ const lastAfter = state.messages[state.messages.length - 1];
3206
+ const toolCallsAfter = Array.isArray(lastAfter?.tool_calls) ? lastAfter.tool_calls : [];
3207
+ if (toolCallsAfter.length > 0) {
3208
+ state = { ...state, ...await toolsNode(state) };
3197
3209
  }
3210
+ } catch (err) {
3211
+ const errMsg = err instanceof Error ? err.message : String(err);
3212
+ emit?.({ type: "metadata", error: `Structured output force-finalize failed: ${errMsg}` });
3213
+ throw err;
3198
3214
  }
3199
3215
  }
3200
3216
  }