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