@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 +73 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +73 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -7114,23 +7114,79 @@ function convertContent(content) {
|
|
|
7114
7114
|
function isZodSchema(obj) {
|
|
7115
7115
|
return obj != null && typeof obj === "object" && typeof obj.safeParse === "function" && "_def" in obj;
|
|
7116
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
|
+
}
|
|
7117
7171
|
function convertToJsonSchema(schema, strict = false) {
|
|
7118
7172
|
if (!schema || typeof schema !== "object") {
|
|
7119
7173
|
return { type: "object", properties: {}, required: [], additionalProperties: false };
|
|
7120
7174
|
}
|
|
7121
7175
|
if (isZodSchema(schema)) {
|
|
7122
|
-
const
|
|
7176
|
+
const converted2 = zodToJsonSchema(schema, {
|
|
7123
7177
|
$refStrategy: "none",
|
|
7124
7178
|
...strict ? { openaiStrictMode: true } : {}
|
|
7125
7179
|
});
|
|
7126
|
-
delete
|
|
7127
|
-
if (!
|
|
7128
|
-
return
|
|
7180
|
+
delete converted2["$schema"];
|
|
7181
|
+
if (!converted2.type) converted2.type = "object";
|
|
7182
|
+
return converted2;
|
|
7129
7183
|
}
|
|
7130
|
-
|
|
7131
|
-
|
|
7184
|
+
const converted = { ...schema };
|
|
7185
|
+
delete converted["$schema"];
|
|
7186
|
+
if (!converted.type && converted.properties) {
|
|
7187
|
+
return { type: "object", ...converted };
|
|
7132
7188
|
}
|
|
7133
|
-
return
|
|
7189
|
+
return converted;
|
|
7134
7190
|
}
|
|
7135
7191
|
function normalizeStrictSchema(schema) {
|
|
7136
7192
|
if (!schema || typeof schema !== "object") return schema;
|
|
@@ -7179,20 +7235,25 @@ function toToolDefinition(tool, strict) {
|
|
|
7179
7235
|
let name;
|
|
7180
7236
|
let description;
|
|
7181
7237
|
let parameters;
|
|
7238
|
+
let rawSchema;
|
|
7239
|
+
let useStrict = strict ?? false;
|
|
7182
7240
|
if (tool.type === "function" && tool.function) {
|
|
7183
7241
|
name = tool.function.name;
|
|
7184
7242
|
description = tool.function.description ?? "";
|
|
7185
|
-
|
|
7243
|
+
rawSchema = tool.function.parameters ?? { type: "object", properties: {}, additionalProperties: false };
|
|
7186
7244
|
} else {
|
|
7187
7245
|
name = tool.name ?? "unknown";
|
|
7188
7246
|
description = tool.description ?? "";
|
|
7189
|
-
|
|
7190
|
-
|
|
7247
|
+
rawSchema = tool.schema ?? tool.parameters;
|
|
7248
|
+
}
|
|
7249
|
+
if (useStrict && !canUseStrictToolSchema(rawSchema)) {
|
|
7250
|
+
useStrict = false;
|
|
7191
7251
|
}
|
|
7192
|
-
|
|
7252
|
+
parameters = convertToJsonSchema(rawSchema, useStrict);
|
|
7253
|
+
if (useStrict) {
|
|
7193
7254
|
parameters = normalizeStrictSchema(parameters);
|
|
7194
7255
|
}
|
|
7195
|
-
return { name, description, parameters, ...
|
|
7256
|
+
return { name, description, parameters, ...useStrict ? { strict: true } : {} };
|
|
7196
7257
|
}
|
|
7197
7258
|
function toBaseChatMessage(response) {
|
|
7198
7259
|
const msg = {
|