@cognipeer/agent-sdk 0.5.1 → 0.5.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/dist/index.mjs CHANGED
@@ -3615,6 +3615,21 @@ function createAgent(opts) {
3615
3615
  }
3616
3616
  if (toolCalls.length === 0) {
3617
3617
  if (soManager && !state.ctx?.__finalizedDueToStructuredOutput && !state.ctx?.__structuredOutputForceFinalize) {
3618
+ if (soManager.strategy.kind === "native") {
3619
+ const assistantText = extractMessageText2(lastMsg);
3620
+ if (assistantText) {
3621
+ const parsed = soManager.parseFromContent(assistantText);
3622
+ if (parsed.success) {
3623
+ const ctx2 = {
3624
+ ...state.ctx || {},
3625
+ __finalizedDueToStructuredOutput: true,
3626
+ __structuredOutputParsed: parsed.data
3627
+ };
3628
+ state = { ...state, ctx: ctx2 };
3629
+ }
3630
+ }
3631
+ break;
3632
+ }
3618
3633
  const nudge = soManager.buildNudgeMessage(false);
3619
3634
  const ctx = { ...state.ctx || {}, __structuredOutputForceFinalize: true };
3620
3635
  state = { ...state, messages: [...state.messages, nudge], ctx };
@@ -3631,6 +3646,23 @@ function createAgent(opts) {
3631
3646
  if (state.ctx?.__finalizedDueToStructuredOutput) break;
3632
3647
  }
3633
3648
  if (soManager && !state.ctx?.__finalizedDueToStructuredOutput) {
3649
+ const lastForParse = state.messages[state.messages.length - 1];
3650
+ if (lastForParse?.role === "assistant") {
3651
+ const assistantText = extractMessageText2(lastForParse);
3652
+ if (assistantText) {
3653
+ const parsed = soManager.parseFromContent(assistantText);
3654
+ if (parsed.success) {
3655
+ const ctx = {
3656
+ ...state.ctx || {},
3657
+ __finalizedDueToStructuredOutput: true,
3658
+ __structuredOutputParsed: parsed.data
3659
+ };
3660
+ state = { ...state, ctx };
3661
+ }
3662
+ }
3663
+ }
3664
+ }
3665
+ if (soManager && soManager.strategy.kind === "tool_based" && !state.ctx?.__finalizedDueToStructuredOutput) {
3634
3666
  const maxPostLoopRetries = soManager.maxRetries;
3635
3667
  for (let postRetry = 0; postRetry < maxPostLoopRetries; postRetry++) {
3636
3668
  if (state.ctx?.__finalizedDueToStructuredOutput) break;
@@ -7082,27 +7114,86 @@ function convertContent(content) {
7082
7114
  function isZodSchema(obj) {
7083
7115
  return obj != null && typeof obj === "object" && typeof obj.safeParse === "function" && "_def" in obj;
7084
7116
  }
7117
+ function hasStrictUnsafeShape(node, inProperty = false) {
7118
+ if (!node || typeof node !== "object") return false;
7119
+ if (Array.isArray(node)) {
7120
+ return node.some((item) => hasStrictUnsafeShape(item, inProperty));
7121
+ }
7122
+ if (typeof node.$ref === "string") {
7123
+ return true;
7124
+ }
7125
+ const hasType = typeof node.type === "string" || Array.isArray(node.type) && node.type.length > 0;
7126
+ const hasProperties = !!node.properties && typeof node.properties === "object" && !Array.isArray(node.properties);
7127
+ const hasItems = node.items !== void 0;
7128
+ const hasComposite = ["anyOf", "oneOf", "allOf"].some((key) => Array.isArray(node[key]) && node[key].length > 0);
7129
+ if (inProperty && !hasType && !hasProperties && !hasItems) {
7130
+ return true;
7131
+ }
7132
+ if (inProperty && hasComposite && !hasType) {
7133
+ return true;
7134
+ }
7135
+ if (hasProperties) {
7136
+ for (const value of Object.values(node.properties)) {
7137
+ if (hasStrictUnsafeShape(value, true)) {
7138
+ return true;
7139
+ }
7140
+ }
7141
+ }
7142
+ if (node.items && hasStrictUnsafeShape(node.items, false)) {
7143
+ return true;
7144
+ }
7145
+ for (const key of ["anyOf", "oneOf", "allOf"]) {
7146
+ if (Array.isArray(node[key]) && hasStrictUnsafeShape(node[key], false)) {
7147
+ return true;
7148
+ }
7149
+ }
7150
+ for (const key of ["definitions", "$defs"]) {
7151
+ const defs = node[key];
7152
+ if (defs && typeof defs === "object") {
7153
+ for (const value of Object.values(defs)) {
7154
+ if (hasStrictUnsafeShape(value, false)) {
7155
+ return true;
7156
+ }
7157
+ }
7158
+ }
7159
+ }
7160
+ return false;
7161
+ }
7162
+ function canUseStrictToolSchema(schema) {
7163
+ if (!schema || typeof schema !== "object") {
7164
+ return true;
7165
+ }
7166
+ if (isZodSchema(schema)) {
7167
+ return true;
7168
+ }
7169
+ return !hasStrictUnsafeShape(schema);
7170
+ }
7085
7171
  function convertToJsonSchema(schema, strict = false) {
7086
7172
  if (!schema || typeof schema !== "object") {
7087
7173
  return { type: "object", properties: {}, required: [], additionalProperties: false };
7088
7174
  }
7089
7175
  if (isZodSchema(schema)) {
7090
- const converted = zodToJsonSchema(schema, {
7176
+ const converted2 = zodToJsonSchema(schema, {
7091
7177
  $refStrategy: "none",
7092
7178
  ...strict ? { openaiStrictMode: true } : {}
7093
7179
  });
7094
- delete converted["$schema"];
7095
- if (!converted.type) converted.type = "object";
7096
- return converted;
7180
+ delete converted2["$schema"];
7181
+ if (!converted2.type) converted2.type = "object";
7182
+ return converted2;
7097
7183
  }
7098
- if (!schema.type && schema.properties) {
7099
- return { type: "object", ...schema };
7184
+ const converted = { ...schema };
7185
+ delete converted["$schema"];
7186
+ if (!converted.type && converted.properties) {
7187
+ return { type: "object", ...converted };
7100
7188
  }
7101
- return schema;
7189
+ return converted;
7102
7190
  }
7103
7191
  function normalizeStrictSchema(schema) {
7104
7192
  if (!schema || typeof schema !== "object") return schema;
7105
7193
  const clone2 = { ...schema };
7194
+ if (typeof clone2.format === "string") {
7195
+ delete clone2.format;
7196
+ }
7106
7197
  if (clone2.type === "object" || clone2.properties) {
7107
7198
  if (!clone2.type) clone2.type = "object";
7108
7199
  if (clone2.properties && typeof clone2.properties === "object") {
@@ -7144,20 +7235,25 @@ function toToolDefinition(tool, strict) {
7144
7235
  let name;
7145
7236
  let description;
7146
7237
  let parameters;
7238
+ let rawSchema;
7239
+ let useStrict = strict ?? false;
7147
7240
  if (tool.type === "function" && tool.function) {
7148
7241
  name = tool.function.name;
7149
7242
  description = tool.function.description ?? "";
7150
- parameters = tool.function.parameters ?? { type: "object", properties: {}, additionalProperties: false };
7243
+ rawSchema = tool.function.parameters ?? { type: "object", properties: {}, additionalProperties: false };
7151
7244
  } else {
7152
7245
  name = tool.name ?? "unknown";
7153
7246
  description = tool.description ?? "";
7154
- const rawSchema = tool.schema ?? tool.parameters;
7155
- parameters = convertToJsonSchema(rawSchema, strict);
7247
+ rawSchema = tool.schema ?? tool.parameters;
7248
+ }
7249
+ if (useStrict && !canUseStrictToolSchema(rawSchema)) {
7250
+ useStrict = false;
7156
7251
  }
7157
- if (strict) {
7252
+ parameters = convertToJsonSchema(rawSchema, useStrict);
7253
+ if (useStrict) {
7158
7254
  parameters = normalizeStrictSchema(parameters);
7159
7255
  }
7160
- return { name, description, parameters, ...strict ? { strict: true } : {} };
7256
+ return { name, description, parameters, ...useStrict ? { strict: true } : {} };
7161
7257
  }
7162
7258
  function toBaseChatMessage(response) {
7163
7259
  const msg = {