@cognipeer/agent-sdk 0.5.2 → 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.cjs CHANGED
@@ -7124,23 +7124,79 @@ function convertContent(content) {
7124
7124
  function isZodSchema(obj) {
7125
7125
  return obj != null && typeof obj === "object" && typeof obj.safeParse === "function" && "_def" in obj;
7126
7126
  }
7127
+ function hasStrictUnsafeShape(node, inProperty = false) {
7128
+ if (!node || typeof node !== "object") return false;
7129
+ if (Array.isArray(node)) {
7130
+ return node.some((item) => hasStrictUnsafeShape(item, inProperty));
7131
+ }
7132
+ if (typeof node.$ref === "string") {
7133
+ return true;
7134
+ }
7135
+ const hasType = typeof node.type === "string" || Array.isArray(node.type) && node.type.length > 0;
7136
+ const hasProperties = !!node.properties && typeof node.properties === "object" && !Array.isArray(node.properties);
7137
+ const hasItems = node.items !== void 0;
7138
+ const hasComposite = ["anyOf", "oneOf", "allOf"].some((key) => Array.isArray(node[key]) && node[key].length > 0);
7139
+ if (inProperty && !hasType && !hasProperties && !hasItems) {
7140
+ return true;
7141
+ }
7142
+ if (inProperty && hasComposite && !hasType) {
7143
+ return true;
7144
+ }
7145
+ if (hasProperties) {
7146
+ for (const value of Object.values(node.properties)) {
7147
+ if (hasStrictUnsafeShape(value, true)) {
7148
+ return true;
7149
+ }
7150
+ }
7151
+ }
7152
+ if (node.items && hasStrictUnsafeShape(node.items, false)) {
7153
+ return true;
7154
+ }
7155
+ for (const key of ["anyOf", "oneOf", "allOf"]) {
7156
+ if (Array.isArray(node[key]) && hasStrictUnsafeShape(node[key], false)) {
7157
+ return true;
7158
+ }
7159
+ }
7160
+ for (const key of ["definitions", "$defs"]) {
7161
+ const defs = node[key];
7162
+ if (defs && typeof defs === "object") {
7163
+ for (const value of Object.values(defs)) {
7164
+ if (hasStrictUnsafeShape(value, false)) {
7165
+ return true;
7166
+ }
7167
+ }
7168
+ }
7169
+ }
7170
+ return false;
7171
+ }
7172
+ function canUseStrictToolSchema(schema) {
7173
+ if (!schema || typeof schema !== "object") {
7174
+ return true;
7175
+ }
7176
+ if (isZodSchema(schema)) {
7177
+ return true;
7178
+ }
7179
+ return !hasStrictUnsafeShape(schema);
7180
+ }
7127
7181
  function convertToJsonSchema(schema, strict = false) {
7128
7182
  if (!schema || typeof schema !== "object") {
7129
7183
  return { type: "object", properties: {}, required: [], additionalProperties: false };
7130
7184
  }
7131
7185
  if (isZodSchema(schema)) {
7132
- const converted = zodToJsonSchema.zodToJsonSchema(schema, {
7186
+ const converted2 = zodToJsonSchema.zodToJsonSchema(schema, {
7133
7187
  $refStrategy: "none",
7134
7188
  ...strict ? { openaiStrictMode: true } : {}
7135
7189
  });
7136
- delete converted["$schema"];
7137
- if (!converted.type) converted.type = "object";
7138
- return converted;
7190
+ delete converted2["$schema"];
7191
+ if (!converted2.type) converted2.type = "object";
7192
+ return converted2;
7139
7193
  }
7140
- if (!schema.type && schema.properties) {
7141
- return { type: "object", ...schema };
7194
+ const converted = { ...schema };
7195
+ delete converted["$schema"];
7196
+ if (!converted.type && converted.properties) {
7197
+ return { type: "object", ...converted };
7142
7198
  }
7143
- return schema;
7199
+ return converted;
7144
7200
  }
7145
7201
  function normalizeStrictSchema(schema) {
7146
7202
  if (!schema || typeof schema !== "object") return schema;
@@ -7189,20 +7245,25 @@ function toToolDefinition(tool, strict) {
7189
7245
  let name;
7190
7246
  let description;
7191
7247
  let parameters;
7248
+ let rawSchema;
7249
+ let useStrict = strict ?? false;
7192
7250
  if (tool.type === "function" && tool.function) {
7193
7251
  name = tool.function.name;
7194
7252
  description = tool.function.description ?? "";
7195
- parameters = tool.function.parameters ?? { type: "object", properties: {}, additionalProperties: false };
7253
+ rawSchema = tool.function.parameters ?? { type: "object", properties: {}, additionalProperties: false };
7196
7254
  } else {
7197
7255
  name = tool.name ?? "unknown";
7198
7256
  description = tool.description ?? "";
7199
- const rawSchema = tool.schema ?? tool.parameters;
7200
- parameters = convertToJsonSchema(rawSchema, strict);
7257
+ rawSchema = tool.schema ?? tool.parameters;
7258
+ }
7259
+ if (useStrict && !canUseStrictToolSchema(rawSchema)) {
7260
+ useStrict = false;
7201
7261
  }
7202
- if (strict) {
7262
+ parameters = convertToJsonSchema(rawSchema, useStrict);
7263
+ if (useStrict) {
7203
7264
  parameters = normalizeStrictSchema(parameters);
7204
7265
  }
7205
- return { name, description, parameters, ...strict ? { strict: true } : {} };
7266
+ return { name, description, parameters, ...useStrict ? { strict: true } : {} };
7206
7267
  }
7207
7268
  function toBaseChatMessage(response) {
7208
7269
  const msg = {