@cognipeer/agent-sdk 0.8.7 → 0.8.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.cjs +30 -39
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.mjs +30 -39
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -2481,6 +2481,8 @@ declare class StructuredOutputManager<T> {
|
|
|
2481
2481
|
/**
|
|
2482
2482
|
* Build a nudge message for when the model didn't produce structured output.
|
|
2483
2483
|
* Used by the agent loop when it needs to retry.
|
|
2484
|
+
* User role is intentional: OpenAI-compatible chat templates such as Qwen
|
|
2485
|
+
* reject system messages anywhere except the beginning of the conversation.
|
|
2484
2486
|
*/
|
|
2485
2487
|
buildNudgeMessage(isLastAttempt: boolean): {
|
|
2486
2488
|
role: string;
|
package/dist/index.d.ts
CHANGED
|
@@ -2481,6 +2481,8 @@ declare class StructuredOutputManager<T> {
|
|
|
2481
2481
|
/**
|
|
2482
2482
|
* Build a nudge message for when the model didn't produce structured output.
|
|
2483
2483
|
* Used by the agent loop when it needs to retry.
|
|
2484
|
+
* User role is intentional: OpenAI-compatible chat templates such as Qwen
|
|
2485
|
+
* reject system messages anywhere except the beginning of the conversation.
|
|
2484
2486
|
*/
|
|
2485
2487
|
buildNudgeMessage(isLastAttempt: boolean): {
|
|
2486
2488
|
role: string;
|
package/dist/index.mjs
CHANGED
|
@@ -802,6 +802,28 @@ function normalizeUsage(raw) {
|
|
|
802
802
|
};
|
|
803
803
|
return normalized;
|
|
804
804
|
}
|
|
805
|
+
function recordUsage(state, modelName, normalized) {
|
|
806
|
+
const usageState = state.usage || { perRequest: [], totals: {} };
|
|
807
|
+
const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
808
|
+
const cachedInput = normalized.prompt_tokens_details?.cached_tokens;
|
|
809
|
+
usageState.perRequest.push({
|
|
810
|
+
id,
|
|
811
|
+
modelName,
|
|
812
|
+
usage: normalized,
|
|
813
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
814
|
+
turn: usageState.perRequest.length + 1,
|
|
815
|
+
cachedInput
|
|
816
|
+
});
|
|
817
|
+
const previous = usageState.totals[modelName] || { input: 0, output: 0, total: 0, cachedInput: 0 };
|
|
818
|
+
usageState.totals[modelName] = {
|
|
819
|
+
input: previous.input + (Number(normalized.prompt_tokens) || 0),
|
|
820
|
+
output: previous.output + (Number(normalized.completion_tokens) || 0),
|
|
821
|
+
total: previous.total + (Number(normalized.total_tokens) || 0),
|
|
822
|
+
cachedInput: previous.cachedInput + (Number(cachedInput) || 0)
|
|
823
|
+
};
|
|
824
|
+
state.usage = usageState;
|
|
825
|
+
return id;
|
|
826
|
+
}
|
|
805
827
|
function num(...vals) {
|
|
806
828
|
for (const v of vals) {
|
|
807
829
|
if (v === 0) return 0;
|
|
@@ -3171,24 +3193,7 @@ function createAgentCoreNode(opts) {
|
|
|
3171
3193
|
messageList: messagesWithResponse
|
|
3172
3194
|
});
|
|
3173
3195
|
if (normalized) {
|
|
3174
|
-
|
|
3175
|
-
const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
3176
|
-
const turn = usageState.perRequest.length + 1;
|
|
3177
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
3178
|
-
const cachedInputTok = normalized.prompt_tokens_details?.cached_tokens;
|
|
3179
|
-
usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
|
|
3180
|
-
const inputTok = normalized.prompt_tokens;
|
|
3181
|
-
const outputTok = normalized.completion_tokens;
|
|
3182
|
-
const totalTok = normalized.total_tokens;
|
|
3183
|
-
const key = modelName;
|
|
3184
|
-
const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
|
|
3185
|
-
usageState.totals[key] = {
|
|
3186
|
-
input: agg.input + (Number(inputTok) || 0),
|
|
3187
|
-
output: agg.output + (Number(outputTok) || 0),
|
|
3188
|
-
total: agg.total + (Number(totalTok) || 0),
|
|
3189
|
-
cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
|
|
3190
|
-
};
|
|
3191
|
-
state.usage = usageState;
|
|
3196
|
+
recordUsage(state, modelName, normalized);
|
|
3192
3197
|
}
|
|
3193
3198
|
return { messages: messagesWithResponse, usage: state.usage };
|
|
3194
3199
|
};
|
|
@@ -4870,11 +4875,13 @@ var StructuredOutputManager = class {
|
|
|
4870
4875
|
/**
|
|
4871
4876
|
* Build a nudge message for when the model didn't produce structured output.
|
|
4872
4877
|
* Used by the agent loop when it needs to retry.
|
|
4878
|
+
* User role is intentional: OpenAI-compatible chat templates such as Qwen
|
|
4879
|
+
* reject system messages anywhere except the beginning of the conversation.
|
|
4873
4880
|
*/
|
|
4874
4881
|
buildNudgeMessage(isLastAttempt) {
|
|
4875
4882
|
if (this.strategy.kind === "native") {
|
|
4876
4883
|
return {
|
|
4877
|
-
role: "
|
|
4884
|
+
role: "user",
|
|
4878
4885
|
content: [
|
|
4879
4886
|
"A structured output schema is active.",
|
|
4880
4887
|
"You MUST respond with valid JSON matching the schema.",
|
|
@@ -4884,7 +4891,7 @@ var StructuredOutputManager = class {
|
|
|
4884
4891
|
};
|
|
4885
4892
|
}
|
|
4886
4893
|
return {
|
|
4887
|
-
role: "
|
|
4894
|
+
role: "user",
|
|
4888
4895
|
content: [
|
|
4889
4896
|
"A structured output schema is active.",
|
|
4890
4897
|
"You MUST now call tool `response` with the final JSON object that matches the schema.",
|
|
@@ -4902,7 +4909,7 @@ var StructuredOutputManager = class {
|
|
|
4902
4909
|
const fieldIssues = error.fieldErrors.map((f) => `- "${f.path}": ${f.message} (received: ${JSON.stringify(f.received)})`).join("\n");
|
|
4903
4910
|
const action = this.strategy.kind === "native" ? "Please output corrected JSON matching the schema." : "Please call `response` again with corrected values matching the schema.";
|
|
4904
4911
|
return {
|
|
4905
|
-
role: "
|
|
4912
|
+
role: "user",
|
|
4906
4913
|
content: [
|
|
4907
4914
|
"Your previous structured output had validation errors:",
|
|
4908
4915
|
fieldIssues,
|
|
@@ -5881,24 +5888,7 @@ function createAgentNode(opts) {
|
|
|
5881
5888
|
messageList: messagesWithResponse
|
|
5882
5889
|
});
|
|
5883
5890
|
if (normalized) {
|
|
5884
|
-
|
|
5885
|
-
const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
5886
|
-
const turn = usageState.perRequest.length + 1;
|
|
5887
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
5888
|
-
const cachedInputTok = normalized.prompt_tokens_details.cached_tokens;
|
|
5889
|
-
usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
|
|
5890
|
-
const inputTok = normalized.prompt_tokens;
|
|
5891
|
-
const outputTok = normalized.completion_tokens;
|
|
5892
|
-
const totalTok = normalized.total_tokens;
|
|
5893
|
-
const key = modelName;
|
|
5894
|
-
const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
|
|
5895
|
-
usageState.totals[key] = {
|
|
5896
|
-
input: agg.input + (Number(inputTok) || 0),
|
|
5897
|
-
output: agg.output + (Number(outputTok) || 0),
|
|
5898
|
-
total: agg.total + (Number(totalTok) || 0),
|
|
5899
|
-
cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
|
|
5900
|
-
};
|
|
5901
|
-
state.usage = usageState;
|
|
5891
|
+
recordUsage(state, modelName, normalized);
|
|
5902
5892
|
}
|
|
5903
5893
|
return { messages: messagesWithResponse, usage: state.usage };
|
|
5904
5894
|
};
|
|
@@ -6348,6 +6338,7 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
|
|
|
6348
6338
|
outputTokensActual = normalized.completion_tokens;
|
|
6349
6339
|
totalTokensActual = normalized.total_tokens;
|
|
6350
6340
|
cachedInputTokens = normalized.prompt_tokens_details?.cached_tokens;
|
|
6341
|
+
recordUsage(state, getModelName(model) || "unknown_model", normalized);
|
|
6351
6342
|
}
|
|
6352
6343
|
} catch (err) {
|
|
6353
6344
|
durationMs = Date.now() - startTime;
|