@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.cjs
CHANGED
|
@@ -812,6 +812,28 @@ function normalizeUsage(raw) {
|
|
|
812
812
|
};
|
|
813
813
|
return normalized;
|
|
814
814
|
}
|
|
815
|
+
function recordUsage(state, modelName, normalized) {
|
|
816
|
+
const usageState = state.usage || { perRequest: [], totals: {} };
|
|
817
|
+
const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
818
|
+
const cachedInput = normalized.prompt_tokens_details?.cached_tokens;
|
|
819
|
+
usageState.perRequest.push({
|
|
820
|
+
id,
|
|
821
|
+
modelName,
|
|
822
|
+
usage: normalized,
|
|
823
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
824
|
+
turn: usageState.perRequest.length + 1,
|
|
825
|
+
cachedInput
|
|
826
|
+
});
|
|
827
|
+
const previous = usageState.totals[modelName] || { input: 0, output: 0, total: 0, cachedInput: 0 };
|
|
828
|
+
usageState.totals[modelName] = {
|
|
829
|
+
input: previous.input + (Number(normalized.prompt_tokens) || 0),
|
|
830
|
+
output: previous.output + (Number(normalized.completion_tokens) || 0),
|
|
831
|
+
total: previous.total + (Number(normalized.total_tokens) || 0),
|
|
832
|
+
cachedInput: previous.cachedInput + (Number(cachedInput) || 0)
|
|
833
|
+
};
|
|
834
|
+
state.usage = usageState;
|
|
835
|
+
return id;
|
|
836
|
+
}
|
|
815
837
|
function num(...vals) {
|
|
816
838
|
for (const v of vals) {
|
|
817
839
|
if (v === 0) return 0;
|
|
@@ -3181,24 +3203,7 @@ function createAgentCoreNode(opts) {
|
|
|
3181
3203
|
messageList: messagesWithResponse
|
|
3182
3204
|
});
|
|
3183
3205
|
if (normalized) {
|
|
3184
|
-
|
|
3185
|
-
const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
3186
|
-
const turn = usageState.perRequest.length + 1;
|
|
3187
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
3188
|
-
const cachedInputTok = normalized.prompt_tokens_details?.cached_tokens;
|
|
3189
|
-
usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
|
|
3190
|
-
const inputTok = normalized.prompt_tokens;
|
|
3191
|
-
const outputTok = normalized.completion_tokens;
|
|
3192
|
-
const totalTok = normalized.total_tokens;
|
|
3193
|
-
const key = modelName;
|
|
3194
|
-
const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
|
|
3195
|
-
usageState.totals[key] = {
|
|
3196
|
-
input: agg.input + (Number(inputTok) || 0),
|
|
3197
|
-
output: agg.output + (Number(outputTok) || 0),
|
|
3198
|
-
total: agg.total + (Number(totalTok) || 0),
|
|
3199
|
-
cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
|
|
3200
|
-
};
|
|
3201
|
-
state.usage = usageState;
|
|
3206
|
+
recordUsage(state, modelName, normalized);
|
|
3202
3207
|
}
|
|
3203
3208
|
return { messages: messagesWithResponse, usage: state.usage };
|
|
3204
3209
|
};
|
|
@@ -4880,11 +4885,13 @@ var StructuredOutputManager = class {
|
|
|
4880
4885
|
/**
|
|
4881
4886
|
* Build a nudge message for when the model didn't produce structured output.
|
|
4882
4887
|
* Used by the agent loop when it needs to retry.
|
|
4888
|
+
* User role is intentional: OpenAI-compatible chat templates such as Qwen
|
|
4889
|
+
* reject system messages anywhere except the beginning of the conversation.
|
|
4883
4890
|
*/
|
|
4884
4891
|
buildNudgeMessage(isLastAttempt) {
|
|
4885
4892
|
if (this.strategy.kind === "native") {
|
|
4886
4893
|
return {
|
|
4887
|
-
role: "
|
|
4894
|
+
role: "user",
|
|
4888
4895
|
content: [
|
|
4889
4896
|
"A structured output schema is active.",
|
|
4890
4897
|
"You MUST respond with valid JSON matching the schema.",
|
|
@@ -4894,7 +4901,7 @@ var StructuredOutputManager = class {
|
|
|
4894
4901
|
};
|
|
4895
4902
|
}
|
|
4896
4903
|
return {
|
|
4897
|
-
role: "
|
|
4904
|
+
role: "user",
|
|
4898
4905
|
content: [
|
|
4899
4906
|
"A structured output schema is active.",
|
|
4900
4907
|
"You MUST now call tool `response` with the final JSON object that matches the schema.",
|
|
@@ -4912,7 +4919,7 @@ var StructuredOutputManager = class {
|
|
|
4912
4919
|
const fieldIssues = error.fieldErrors.map((f) => `- "${f.path}": ${f.message} (received: ${JSON.stringify(f.received)})`).join("\n");
|
|
4913
4920
|
const action = this.strategy.kind === "native" ? "Please output corrected JSON matching the schema." : "Please call `response` again with corrected values matching the schema.";
|
|
4914
4921
|
return {
|
|
4915
|
-
role: "
|
|
4922
|
+
role: "user",
|
|
4916
4923
|
content: [
|
|
4917
4924
|
"Your previous structured output had validation errors:",
|
|
4918
4925
|
fieldIssues,
|
|
@@ -5891,24 +5898,7 @@ function createAgentNode(opts) {
|
|
|
5891
5898
|
messageList: messagesWithResponse
|
|
5892
5899
|
});
|
|
5893
5900
|
if (normalized) {
|
|
5894
|
-
|
|
5895
|
-
const id = `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
5896
|
-
const turn = usageState.perRequest.length + 1;
|
|
5897
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
5898
|
-
const cachedInputTok = normalized.prompt_tokens_details.cached_tokens;
|
|
5899
|
-
usageState.perRequest.push({ id, modelName, usage: normalized, timestamp, turn, cachedInput: cachedInputTok });
|
|
5900
|
-
const inputTok = normalized.prompt_tokens;
|
|
5901
|
-
const outputTok = normalized.completion_tokens;
|
|
5902
|
-
const totalTok = normalized.total_tokens;
|
|
5903
|
-
const key = modelName;
|
|
5904
|
-
const agg = usageState.totals[key] || { input: 0, output: 0, total: 0, cachedInput: 0 };
|
|
5905
|
-
usageState.totals[key] = {
|
|
5906
|
-
input: agg.input + (Number(inputTok) || 0),
|
|
5907
|
-
output: agg.output + (Number(outputTok) || 0),
|
|
5908
|
-
total: agg.total + (Number(totalTok) || 0),
|
|
5909
|
-
cachedInput: agg.cachedInput + (Number(cachedInputTok) || 0)
|
|
5910
|
-
};
|
|
5911
|
-
state.usage = usageState;
|
|
5901
|
+
recordUsage(state, modelName, normalized);
|
|
5912
5902
|
}
|
|
5913
5903
|
return { messages: messagesWithResponse, usage: state.usage };
|
|
5914
5904
|
};
|
|
@@ -6358,6 +6348,7 @@ ${canonicalFacts.length > 0 ? canonicalFacts.map((fact) => `- ${fact.key}: ${fac
|
|
|
6358
6348
|
outputTokensActual = normalized.completion_tokens;
|
|
6359
6349
|
totalTokensActual = normalized.total_tokens;
|
|
6360
6350
|
cachedInputTokens = normalized.prompt_tokens_details?.cached_tokens;
|
|
6351
|
+
recordUsage(state, getModelName(model) || "unknown_model", normalized);
|
|
6361
6352
|
}
|
|
6362
6353
|
} catch (err) {
|
|
6363
6354
|
durationMs = Date.now() - startTime;
|