@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.cjs +108 -12
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +108 -12
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -3625,6 +3625,21 @@ function createAgent(opts) {
|
|
|
3625
3625
|
}
|
|
3626
3626
|
if (toolCalls.length === 0) {
|
|
3627
3627
|
if (soManager && !state.ctx?.__finalizedDueToStructuredOutput && !state.ctx?.__structuredOutputForceFinalize) {
|
|
3628
|
+
if (soManager.strategy.kind === "native") {
|
|
3629
|
+
const assistantText = extractMessageText2(lastMsg);
|
|
3630
|
+
if (assistantText) {
|
|
3631
|
+
const parsed = soManager.parseFromContent(assistantText);
|
|
3632
|
+
if (parsed.success) {
|
|
3633
|
+
const ctx2 = {
|
|
3634
|
+
...state.ctx || {},
|
|
3635
|
+
__finalizedDueToStructuredOutput: true,
|
|
3636
|
+
__structuredOutputParsed: parsed.data
|
|
3637
|
+
};
|
|
3638
|
+
state = { ...state, ctx: ctx2 };
|
|
3639
|
+
}
|
|
3640
|
+
}
|
|
3641
|
+
break;
|
|
3642
|
+
}
|
|
3628
3643
|
const nudge = soManager.buildNudgeMessage(false);
|
|
3629
3644
|
const ctx = { ...state.ctx || {}, __structuredOutputForceFinalize: true };
|
|
3630
3645
|
state = { ...state, messages: [...state.messages, nudge], ctx };
|
|
@@ -3641,6 +3656,23 @@ function createAgent(opts) {
|
|
|
3641
3656
|
if (state.ctx?.__finalizedDueToStructuredOutput) break;
|
|
3642
3657
|
}
|
|
3643
3658
|
if (soManager && !state.ctx?.__finalizedDueToStructuredOutput) {
|
|
3659
|
+
const lastForParse = state.messages[state.messages.length - 1];
|
|
3660
|
+
if (lastForParse?.role === "assistant") {
|
|
3661
|
+
const assistantText = extractMessageText2(lastForParse);
|
|
3662
|
+
if (assistantText) {
|
|
3663
|
+
const parsed = soManager.parseFromContent(assistantText);
|
|
3664
|
+
if (parsed.success) {
|
|
3665
|
+
const ctx = {
|
|
3666
|
+
...state.ctx || {},
|
|
3667
|
+
__finalizedDueToStructuredOutput: true,
|
|
3668
|
+
__structuredOutputParsed: parsed.data
|
|
3669
|
+
};
|
|
3670
|
+
state = { ...state, ctx };
|
|
3671
|
+
}
|
|
3672
|
+
}
|
|
3673
|
+
}
|
|
3674
|
+
}
|
|
3675
|
+
if (soManager && soManager.strategy.kind === "tool_based" && !state.ctx?.__finalizedDueToStructuredOutput) {
|
|
3644
3676
|
const maxPostLoopRetries = soManager.maxRetries;
|
|
3645
3677
|
for (let postRetry = 0; postRetry < maxPostLoopRetries; postRetry++) {
|
|
3646
3678
|
if (state.ctx?.__finalizedDueToStructuredOutput) break;
|
|
@@ -7092,27 +7124,86 @@ function convertContent(content) {
|
|
|
7092
7124
|
function isZodSchema(obj) {
|
|
7093
7125
|
return obj != null && typeof obj === "object" && typeof obj.safeParse === "function" && "_def" in obj;
|
|
7094
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
|
+
}
|
|
7095
7181
|
function convertToJsonSchema(schema, strict = false) {
|
|
7096
7182
|
if (!schema || typeof schema !== "object") {
|
|
7097
7183
|
return { type: "object", properties: {}, required: [], additionalProperties: false };
|
|
7098
7184
|
}
|
|
7099
7185
|
if (isZodSchema(schema)) {
|
|
7100
|
-
const
|
|
7186
|
+
const converted2 = zodToJsonSchema.zodToJsonSchema(schema, {
|
|
7101
7187
|
$refStrategy: "none",
|
|
7102
7188
|
...strict ? { openaiStrictMode: true } : {}
|
|
7103
7189
|
});
|
|
7104
|
-
delete
|
|
7105
|
-
if (!
|
|
7106
|
-
return
|
|
7190
|
+
delete converted2["$schema"];
|
|
7191
|
+
if (!converted2.type) converted2.type = "object";
|
|
7192
|
+
return converted2;
|
|
7107
7193
|
}
|
|
7108
|
-
|
|
7109
|
-
|
|
7194
|
+
const converted = { ...schema };
|
|
7195
|
+
delete converted["$schema"];
|
|
7196
|
+
if (!converted.type && converted.properties) {
|
|
7197
|
+
return { type: "object", ...converted };
|
|
7110
7198
|
}
|
|
7111
|
-
return
|
|
7199
|
+
return converted;
|
|
7112
7200
|
}
|
|
7113
7201
|
function normalizeStrictSchema(schema) {
|
|
7114
7202
|
if (!schema || typeof schema !== "object") return schema;
|
|
7115
7203
|
const clone2 = { ...schema };
|
|
7204
|
+
if (typeof clone2.format === "string") {
|
|
7205
|
+
delete clone2.format;
|
|
7206
|
+
}
|
|
7116
7207
|
if (clone2.type === "object" || clone2.properties) {
|
|
7117
7208
|
if (!clone2.type) clone2.type = "object";
|
|
7118
7209
|
if (clone2.properties && typeof clone2.properties === "object") {
|
|
@@ -7154,20 +7245,25 @@ function toToolDefinition(tool, strict) {
|
|
|
7154
7245
|
let name;
|
|
7155
7246
|
let description;
|
|
7156
7247
|
let parameters;
|
|
7248
|
+
let rawSchema;
|
|
7249
|
+
let useStrict = strict ?? false;
|
|
7157
7250
|
if (tool.type === "function" && tool.function) {
|
|
7158
7251
|
name = tool.function.name;
|
|
7159
7252
|
description = tool.function.description ?? "";
|
|
7160
|
-
|
|
7253
|
+
rawSchema = tool.function.parameters ?? { type: "object", properties: {}, additionalProperties: false };
|
|
7161
7254
|
} else {
|
|
7162
7255
|
name = tool.name ?? "unknown";
|
|
7163
7256
|
description = tool.description ?? "";
|
|
7164
|
-
|
|
7165
|
-
|
|
7257
|
+
rawSchema = tool.schema ?? tool.parameters;
|
|
7258
|
+
}
|
|
7259
|
+
if (useStrict && !canUseStrictToolSchema(rawSchema)) {
|
|
7260
|
+
useStrict = false;
|
|
7166
7261
|
}
|
|
7167
|
-
|
|
7262
|
+
parameters = convertToJsonSchema(rawSchema, useStrict);
|
|
7263
|
+
if (useStrict) {
|
|
7168
7264
|
parameters = normalizeStrictSchema(parameters);
|
|
7169
7265
|
}
|
|
7170
|
-
return { name, description, parameters, ...
|
|
7266
|
+
return { name, description, parameters, ...useStrict ? { strict: true } : {} };
|
|
7171
7267
|
}
|
|
7172
7268
|
function toBaseChatMessage(response) {
|
|
7173
7269
|
const msg = {
|