@deepagents/context 0.12.1 → 0.13.0
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.js
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
// packages/context/src/lib/engine.ts
|
|
2
|
+
import { mergeWith } from "lodash-es";
|
|
3
|
+
|
|
1
4
|
// packages/context/src/lib/estimate.ts
|
|
2
5
|
import { encode } from "gpt-tokenizer";
|
|
3
6
|
var defaultTokenizer = {
|
|
@@ -1054,6 +1057,8 @@ var ContextEngine = class {
|
|
|
1054
1057
|
#branch = null;
|
|
1055
1058
|
#chatData = null;
|
|
1056
1059
|
#initialized = false;
|
|
1060
|
+
/** Initial metadata to merge on first initialization */
|
|
1061
|
+
#initialMetadata;
|
|
1057
1062
|
constructor(options) {
|
|
1058
1063
|
if (!options.chatId) {
|
|
1059
1064
|
throw new Error("chatId is required");
|
|
@@ -1065,6 +1070,7 @@ var ContextEngine = class {
|
|
|
1065
1070
|
this.#chatId = options.chatId;
|
|
1066
1071
|
this.#userId = options.userId;
|
|
1067
1072
|
this.#branchName = "main";
|
|
1073
|
+
this.#initialMetadata = options.metadata;
|
|
1068
1074
|
}
|
|
1069
1075
|
/**
|
|
1070
1076
|
* Initialize the chat and branch if they don't exist.
|
|
@@ -1077,6 +1083,15 @@ var ContextEngine = class {
|
|
|
1077
1083
|
id: this.#chatId,
|
|
1078
1084
|
userId: this.#userId
|
|
1079
1085
|
});
|
|
1086
|
+
if (this.#initialMetadata) {
|
|
1087
|
+
this.#chatData = await this.#store.updateChat(this.#chatId, {
|
|
1088
|
+
metadata: {
|
|
1089
|
+
...this.#chatData.metadata,
|
|
1090
|
+
...this.#initialMetadata
|
|
1091
|
+
}
|
|
1092
|
+
});
|
|
1093
|
+
this.#initialMetadata = void 0;
|
|
1094
|
+
}
|
|
1080
1095
|
this.#branch = await this.#store.getActiveBranch(this.#chatId);
|
|
1081
1096
|
this.#initialized = true;
|
|
1082
1097
|
}
|
|
@@ -1550,6 +1565,36 @@ var ContextEngine = class {
|
|
|
1550
1565
|
}
|
|
1551
1566
|
this.#chatData = await this.#store.updateChat(this.#chatId, storeUpdates);
|
|
1552
1567
|
}
|
|
1568
|
+
/**
|
|
1569
|
+
* Track token usage for the current chat.
|
|
1570
|
+
* Accumulates usage metrics in chat.metadata.usage.
|
|
1571
|
+
*
|
|
1572
|
+
* @param usage - Token usage from AI SDK (LanguageModelUsage)
|
|
1573
|
+
*
|
|
1574
|
+
* @example
|
|
1575
|
+
* ```ts
|
|
1576
|
+
* // In onFinish callback
|
|
1577
|
+
* const usage = await result.totalUsage;
|
|
1578
|
+
* await context.trackUsage(usage);
|
|
1579
|
+
* ```
|
|
1580
|
+
*/
|
|
1581
|
+
async trackUsage(usage) {
|
|
1582
|
+
await this.#ensureInitialized();
|
|
1583
|
+
const freshChatData = await this.#store.getChat(this.#chatId);
|
|
1584
|
+
const currentUsage = freshChatData?.metadata?.usage ?? {};
|
|
1585
|
+
const updatedUsage = mergeWith(
|
|
1586
|
+
{},
|
|
1587
|
+
currentUsage,
|
|
1588
|
+
usage,
|
|
1589
|
+
(a, b) => typeof a === "number" || typeof b === "number" ? (a ?? 0) + (b ?? 0) : void 0
|
|
1590
|
+
);
|
|
1591
|
+
this.#chatData = await this.#store.updateChat(this.#chatId, {
|
|
1592
|
+
metadata: {
|
|
1593
|
+
...freshChatData?.metadata,
|
|
1594
|
+
usage: updatedUsage
|
|
1595
|
+
}
|
|
1596
|
+
});
|
|
1597
|
+
}
|
|
1553
1598
|
/**
|
|
1554
1599
|
* Consolidate context fragments (no-op for now).
|
|
1555
1600
|
*
|
|
@@ -1898,6 +1943,7 @@ var errorRecoveryGuardrail = {
|
|
|
1898
1943
|
"My response format was invalid. Let me try again with a properly formatted response."
|
|
1899
1944
|
);
|
|
1900
1945
|
}
|
|
1946
|
+
console.dir({ part }, { depth: null });
|
|
1901
1947
|
return logAndFail(
|
|
1902
1948
|
"Unknown error",
|
|
1903
1949
|
`An error occurred: ${errorText}. Let me try a different approach.`
|
|
@@ -2974,12 +3020,20 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
2974
3020
|
}
|
|
2975
3021
|
async listChats(options) {
|
|
2976
3022
|
const params = [];
|
|
2977
|
-
|
|
3023
|
+
const whereClauses = [];
|
|
2978
3024
|
let limitClause = "";
|
|
2979
3025
|
if (options?.userId) {
|
|
2980
|
-
|
|
3026
|
+
whereClauses.push("c.userId = ?");
|
|
2981
3027
|
params.push(options.userId);
|
|
2982
3028
|
}
|
|
3029
|
+
if (options?.metadata) {
|
|
3030
|
+
whereClauses.push(`json_extract(c.metadata, '$.' || ?) = ?`);
|
|
3031
|
+
params.push(options.metadata.key);
|
|
3032
|
+
params.push(
|
|
3033
|
+
typeof options.metadata.value === "boolean" ? options.metadata.value ? 1 : 0 : options.metadata.value
|
|
3034
|
+
);
|
|
3035
|
+
}
|
|
3036
|
+
const whereClause = whereClauses.length > 0 ? `WHERE ${whereClauses.join(" AND ")}` : "";
|
|
2983
3037
|
if (options?.limit !== void 0) {
|
|
2984
3038
|
limitClause = " LIMIT ?";
|
|
2985
3039
|
params.push(options.limit);
|
|
@@ -2993,6 +3047,7 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
2993
3047
|
c.id,
|
|
2994
3048
|
c.userId,
|
|
2995
3049
|
c.title,
|
|
3050
|
+
c.metadata,
|
|
2996
3051
|
c.createdAt,
|
|
2997
3052
|
c.updatedAt,
|
|
2998
3053
|
COUNT(DISTINCT m.id) as messageCount,
|
|
@@ -3008,6 +3063,7 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3008
3063
|
id: row.id,
|
|
3009
3064
|
userId: row.userId,
|
|
3010
3065
|
title: row.title ?? void 0,
|
|
3066
|
+
metadata: row.metadata ? JSON.parse(row.metadata) : void 0,
|
|
3011
3067
|
messageCount: row.messageCount,
|
|
3012
3068
|
branchCount: row.branchCount,
|
|
3013
3069
|
createdAt: row.createdAt,
|
|
@@ -3037,11 +3093,17 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3037
3093
|
// Message Operations (Graph Nodes)
|
|
3038
3094
|
// ==========================================================================
|
|
3039
3095
|
async addMessage(message2) {
|
|
3040
|
-
const existingParent = message2.parentId === message2.id ? this.#db.prepare("SELECT parentId FROM messages WHERE id = ?").get(message2.id) : void 0;
|
|
3041
|
-
const parentId = message2.parentId === message2.id ? existingParent?.parentId ?? null : message2.parentId;
|
|
3042
3096
|
this.#db.prepare(
|
|
3043
3097
|
`INSERT INTO messages (id, chatId, parentId, name, type, data, createdAt)
|
|
3044
|
-
VALUES (
|
|
3098
|
+
VALUES (
|
|
3099
|
+
?1,
|
|
3100
|
+
?2,
|
|
3101
|
+
CASE WHEN ?3 = ?1 THEN (SELECT parentId FROM messages WHERE id = ?1) ELSE ?3 END,
|
|
3102
|
+
?4,
|
|
3103
|
+
?5,
|
|
3104
|
+
?6,
|
|
3105
|
+
?7
|
|
3106
|
+
)
|
|
3045
3107
|
ON CONFLICT(id) DO UPDATE SET
|
|
3046
3108
|
name = excluded.name,
|
|
3047
3109
|
type = excluded.type,
|
|
@@ -3049,7 +3111,7 @@ var SqliteContextStore = class extends ContextStore {
|
|
|
3049
3111
|
).run(
|
|
3050
3112
|
message2.id,
|
|
3051
3113
|
message2.chatId,
|
|
3052
|
-
parentId,
|
|
3114
|
+
message2.parentId,
|
|
3053
3115
|
message2.name,
|
|
3054
3116
|
message2.type ?? null,
|
|
3055
3117
|
JSON.stringify(message2.data),
|