@agentfold/shared 0.1.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/LICENSE +21 -0
- package/dist/index.d.ts +157 -0
- package/dist/index.js +71 -0
- package/dist/index.js.map +1 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 AgentFold contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentFold shared types — protocol-neutral, dependency-free.
|
|
3
|
+
*
|
|
4
|
+
* RAW HISTORY IS SOURCE OF TRUTH.
|
|
5
|
+
* Every folded piece of context must be recoverable from the immutable ledger.
|
|
6
|
+
*/
|
|
7
|
+
export type EventKind = "system" | "developer" | "user" | "assistant" | "tool_call" | "tool_result";
|
|
8
|
+
/**
|
|
9
|
+
* A single protocol-neutral event extracted from an agent conversation.
|
|
10
|
+
* Events are immutable once written. Updates to memory/task state never
|
|
11
|
+
* rewrite events.
|
|
12
|
+
*/
|
|
13
|
+
export interface AgentEvent {
|
|
14
|
+
id: string;
|
|
15
|
+
sessionId: string;
|
|
16
|
+
/** Position within the session conversation. Stable across requests. */
|
|
17
|
+
sequence: number;
|
|
18
|
+
/** ISO-8601 timestamp of first observation. */
|
|
19
|
+
timestamp: string;
|
|
20
|
+
kind: EventKind;
|
|
21
|
+
/** Tool name for tool_call / tool_result events. */
|
|
22
|
+
toolName?: string;
|
|
23
|
+
/** Protocol-level pairing id (OpenAI tool_call_id / Responses call_id). */
|
|
24
|
+
toolCallId?: string;
|
|
25
|
+
/** Normalized text content (redacted if redaction is enabled). */
|
|
26
|
+
content: string;
|
|
27
|
+
/** sha256 hex of `content` (post-redaction). */
|
|
28
|
+
contentSha256: string;
|
|
29
|
+
metadataJson: string;
|
|
30
|
+
tokenEstimate: number;
|
|
31
|
+
/** True if content contained secrets that were redacted or private tags. */
|
|
32
|
+
sensitive: boolean;
|
|
33
|
+
/** Whether the fold engine may ever fold this event's content. */
|
|
34
|
+
foldable: boolean;
|
|
35
|
+
source: string;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Deterministic record replacing a folded block in the outgoing context.
|
|
39
|
+
* Format is specified in docs/FOLD_FORMAT.md and must stay byte-stable
|
|
40
|
+
* for identical inputs.
|
|
41
|
+
*/
|
|
42
|
+
export interface FoldRecord {
|
|
43
|
+
/** 8-hex public id, printed in the digest as [AFOLD:<id>]. */
|
|
44
|
+
foldId: string;
|
|
45
|
+
sessionId: string;
|
|
46
|
+
/** Ledger event holding the original raw content. */
|
|
47
|
+
originalEventId: string;
|
|
48
|
+
/** sha256 hex of the raw (redacted) content — verified on recall. */
|
|
49
|
+
sha256: string;
|
|
50
|
+
toolName?: string;
|
|
51
|
+
resultKind?: "ok" | "failed" | "unknown";
|
|
52
|
+
originalTokens: number;
|
|
53
|
+
digestTokens: number;
|
|
54
|
+
lineCount: number;
|
|
55
|
+
errorCount: number;
|
|
56
|
+
/** Deterministically extracted identifiers, paths, numbers, errors... */
|
|
57
|
+
anchors: string[];
|
|
58
|
+
digest: string;
|
|
59
|
+
createdAt: string;
|
|
60
|
+
}
|
|
61
|
+
export interface AgentFoldConfig {
|
|
62
|
+
/** Fold when estimated live tokens exceed contextWindow * foldAt. */
|
|
63
|
+
foldAt: number;
|
|
64
|
+
/** Minimum relative savings (fraction of contextWindow) to trigger a fold pass. */
|
|
65
|
+
foldStep: number;
|
|
66
|
+
/** Post-fold live budget as fraction of usable window. */
|
|
67
|
+
targetLiveBudgetFraction: number;
|
|
68
|
+
/** Hard cap on live tokens regardless of window size. */
|
|
69
|
+
absoluteLiveCap: number;
|
|
70
|
+
/** Fraction of the window reserved for the model's output. */
|
|
71
|
+
outputReserve: number;
|
|
72
|
+
/** Fraction reserved for protocol overhead / safety margin. */
|
|
73
|
+
protocolReserve: number;
|
|
74
|
+
/** Protected tail tokens override (null = dynamic). */
|
|
75
|
+
protectedTailTokens: number | null;
|
|
76
|
+
/** Retrieval: max memory cards injected per request. */
|
|
77
|
+
retrievalTopK: number;
|
|
78
|
+
/** Max tokens of retrieved memory injected per request. */
|
|
79
|
+
retrievalBudgetTokens: number;
|
|
80
|
+
storage: {
|
|
81
|
+
/** Path to the SQLite database file. */
|
|
82
|
+
dbPath: string;
|
|
83
|
+
redactSecrets: boolean;
|
|
84
|
+
};
|
|
85
|
+
/** When true, internal errors fail the request instead of failing open. */
|
|
86
|
+
strictMode: boolean;
|
|
87
|
+
/** Default context window (tokens) for unknown models. */
|
|
88
|
+
defaultContextWindow: number;
|
|
89
|
+
/** Upstream base URL for proxy mode. */
|
|
90
|
+
upstreamBaseUrl: string;
|
|
91
|
+
/** Proxy listen port. */
|
|
92
|
+
port: number;
|
|
93
|
+
/** MCP recall result token cap. */
|
|
94
|
+
recallTokenCap: number;
|
|
95
|
+
}
|
|
96
|
+
export declare const DEFAULT_CONFIG: AgentFoldConfig;
|
|
97
|
+
/** Known model context windows (tokens). Extend as needed. */
|
|
98
|
+
export declare const CONTEXT_WINDOWS: Record<string, number>;
|
|
99
|
+
export declare function contextWindowFor(model: string, config: AgentFoldConfig): number;
|
|
100
|
+
export interface TokenCount {
|
|
101
|
+
tokens: number;
|
|
102
|
+
/** true when produced by the heuristic counter, not a model tokenizer. */
|
|
103
|
+
estimated: boolean;
|
|
104
|
+
}
|
|
105
|
+
export interface TokenCounter {
|
|
106
|
+
count(text: string): TokenCount;
|
|
107
|
+
}
|
|
108
|
+
export interface RequestMetrics {
|
|
109
|
+
sessionId: string;
|
|
110
|
+
model: string;
|
|
111
|
+
originalTokens: number;
|
|
112
|
+
compiledTokens: number;
|
|
113
|
+
savedTokens: number;
|
|
114
|
+
savingRatio: number;
|
|
115
|
+
foldedBlocks: number;
|
|
116
|
+
retrievedMemoryTokens: number;
|
|
117
|
+
protectedTailTokens: number;
|
|
118
|
+
pinnedTokens: number;
|
|
119
|
+
estimatedContextUsage: number;
|
|
120
|
+
compileLatencyMs: number;
|
|
121
|
+
proxyOverheadMs: number;
|
|
122
|
+
/** gross estimated savings before cache-refill penalty. */
|
|
123
|
+
grossTokensSaved: number;
|
|
124
|
+
/** estimated tokens to re-refill provider prefix cache after folding. */
|
|
125
|
+
estimatedReprefillCost: number;
|
|
126
|
+
netEstimatedSaving: number;
|
|
127
|
+
cacheMetricsAvailable: boolean;
|
|
128
|
+
warnings: string[];
|
|
129
|
+
diagnostics: string[];
|
|
130
|
+
}
|
|
131
|
+
export type MemoryStatus = "active" | "superseded" | "expired" | "invalid";
|
|
132
|
+
export interface MemoryCard {
|
|
133
|
+
id: string;
|
|
134
|
+
sessionId: string;
|
|
135
|
+
kind: string;
|
|
136
|
+
title: string;
|
|
137
|
+
timestamp: string;
|
|
138
|
+
evidence: string;
|
|
139
|
+
tokenCost: number;
|
|
140
|
+
confidence: number;
|
|
141
|
+
status: MemoryStatus;
|
|
142
|
+
supersededBy: string | null;
|
|
143
|
+
sourceEventId: string | null;
|
|
144
|
+
score: number;
|
|
145
|
+
scoreReasons: string[];
|
|
146
|
+
}
|
|
147
|
+
export interface Pin {
|
|
148
|
+
id: string;
|
|
149
|
+
sessionId: string | null;
|
|
150
|
+
text: string;
|
|
151
|
+
createdAt: string;
|
|
152
|
+
source: "user" | "agent" | "system";
|
|
153
|
+
}
|
|
154
|
+
export declare class AgentFoldError extends Error {
|
|
155
|
+
readonly code: "integrity_error" | "not_found" | "budget_exceeded" | "store_unavailable" | "invalid_input";
|
|
156
|
+
constructor(message: string, code: "integrity_error" | "not_found" | "budget_exceeded" | "store_unavailable" | "invalid_input");
|
|
157
|
+
}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentFold shared types — protocol-neutral, dependency-free.
|
|
3
|
+
*
|
|
4
|
+
* RAW HISTORY IS SOURCE OF TRUTH.
|
|
5
|
+
* Every folded piece of context must be recoverable from the immutable ledger.
|
|
6
|
+
*/
|
|
7
|
+
export const DEFAULT_CONFIG = {
|
|
8
|
+
foldAt: 0.45,
|
|
9
|
+
foldStep: 0.12,
|
|
10
|
+
targetLiveBudgetFraction: 0.7,
|
|
11
|
+
absoluteLiveCap: 200_000,
|
|
12
|
+
outputReserve: 0.2,
|
|
13
|
+
protocolReserve: 0.05,
|
|
14
|
+
protectedTailTokens: null,
|
|
15
|
+
retrievalTopK: 3,
|
|
16
|
+
retrievalBudgetTokens: 800,
|
|
17
|
+
storage: {
|
|
18
|
+
dbPath: ".agentfold/agentfold.db",
|
|
19
|
+
redactSecrets: true
|
|
20
|
+
},
|
|
21
|
+
strictMode: false,
|
|
22
|
+
defaultContextWindow: 128_000,
|
|
23
|
+
upstreamBaseUrl: "https://api.openai.com",
|
|
24
|
+
port: 3897,
|
|
25
|
+
recallTokenCap: 8_000
|
|
26
|
+
};
|
|
27
|
+
/** Known model context windows (tokens). Extend as needed. */
|
|
28
|
+
export const CONTEXT_WINDOWS = {
|
|
29
|
+
"gpt-4o": 128_000,
|
|
30
|
+
"gpt-4o-mini": 128_000,
|
|
31
|
+
"gpt-4.1": 1_000_000,
|
|
32
|
+
"gpt-4.1-mini": 1_000_000,
|
|
33
|
+
"gpt-4.1-nano": 1_000_000,
|
|
34
|
+
"gpt-5": 400_000,
|
|
35
|
+
"gpt-5-mini": 400_000,
|
|
36
|
+
o1: 200_000,
|
|
37
|
+
o3: 200_000,
|
|
38
|
+
"o4-mini": 200_000,
|
|
39
|
+
"claude-opus-4": 200_000,
|
|
40
|
+
"claude-sonnet-4": 200_000,
|
|
41
|
+
"claude-3-7-sonnet": 200_000,
|
|
42
|
+
"gemini-2.5-pro": 1_000_000,
|
|
43
|
+
"gemini-2.5-flash": 1_000_000,
|
|
44
|
+
"deepseek-chat": 128_000,
|
|
45
|
+
"deepseek-reasoner": 128_000,
|
|
46
|
+
"qwen-max": 128_000,
|
|
47
|
+
"glm-4.5": 128_000,
|
|
48
|
+
"kimi-k2": 256_000
|
|
49
|
+
};
|
|
50
|
+
export function contextWindowFor(model, config) {
|
|
51
|
+
if (CONTEXT_WINDOWS[model] != null)
|
|
52
|
+
return CONTEXT_WINDOWS[model];
|
|
53
|
+
// family prefix match, e.g. "gpt-4o-2024-08-13"
|
|
54
|
+
for (const [key, value] of Object.entries(CONTEXT_WINDOWS)) {
|
|
55
|
+
if (model.startsWith(key))
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
return config.defaultContextWindow;
|
|
59
|
+
}
|
|
60
|
+
// ---------------------------------------------------------------------------
|
|
61
|
+
// Errors
|
|
62
|
+
// ---------------------------------------------------------------------------
|
|
63
|
+
export class AgentFoldError extends Error {
|
|
64
|
+
code;
|
|
65
|
+
constructor(message, code) {
|
|
66
|
+
super(message);
|
|
67
|
+
this.code = code;
|
|
68
|
+
this.name = "AgentFoldError";
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAiHH,MAAM,CAAC,MAAM,cAAc,GAAoB;IAC7C,MAAM,EAAE,IAAI;IACZ,QAAQ,EAAE,IAAI;IACd,wBAAwB,EAAE,GAAG;IAC7B,eAAe,EAAE,OAAO;IACxB,aAAa,EAAE,GAAG;IAClB,eAAe,EAAE,IAAI;IACrB,mBAAmB,EAAE,IAAI;IACzB,aAAa,EAAE,CAAC;IAChB,qBAAqB,EAAE,GAAG;IAC1B,OAAO,EAAE;QACP,MAAM,EAAE,yBAAyB;QACjC,aAAa,EAAE,IAAI;KACpB;IACD,UAAU,EAAE,KAAK;IACjB,oBAAoB,EAAE,OAAO;IAC7B,eAAe,EAAE,wBAAwB;IACzC,IAAI,EAAE,IAAI;IACV,cAAc,EAAE,KAAK;CACtB,CAAC;AAEF,8DAA8D;AAC9D,MAAM,CAAC,MAAM,eAAe,GAA2B;IACrD,QAAQ,EAAE,OAAO;IACjB,aAAa,EAAE,OAAO;IACtB,SAAS,EAAE,SAAS;IACpB,cAAc,EAAE,SAAS;IACzB,cAAc,EAAE,SAAS;IACzB,OAAO,EAAE,OAAO;IAChB,YAAY,EAAE,OAAO;IACrB,EAAE,EAAE,OAAO;IACX,EAAE,EAAE,OAAO;IACX,SAAS,EAAE,OAAO;IAClB,eAAe,EAAE,OAAO;IACxB,iBAAiB,EAAE,OAAO;IAC1B,mBAAmB,EAAE,OAAO;IAC5B,gBAAgB,EAAE,SAAS;IAC3B,kBAAkB,EAAE,SAAS;IAC7B,eAAe,EAAE,OAAO;IACxB,mBAAmB,EAAE,OAAO;IAC5B,UAAU,EAAE,OAAO;IACnB,SAAS,EAAE,OAAO;IAClB,SAAS,EAAE,OAAO;CACnB,CAAC;AAEF,MAAM,UAAU,gBAAgB,CAAC,KAAa,EAAE,MAAuB;IACrE,IAAI,eAAe,CAAC,KAAK,CAAC,IAAI,IAAI;QAAE,OAAO,eAAe,CAAC,KAAK,CAAE,CAAC;IACnE,gDAAgD;IAChD,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,IAAI,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,EAAE,CAAC;QAC3D,IAAI,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC;YAAE,OAAO,KAAK,CAAC;IAC1C,CAAC;IACD,OAAO,MAAM,CAAC,oBAAoB,CAAC;AACrC,CAAC;AA8ED,8EAA8E;AAC9E,SAAS;AACT,8EAA8E;AAE9E,MAAM,OAAO,cAAe,SAAQ,KAAK;IAGrB;IAFlB,YACE,OAAe,EACC,IAKG;QAEnB,KAAK,CAAC,OAAO,CAAC,CAAC;QAPC,SAAI,GAAJ,IAAI,CAKD;QAGnB,IAAI,CAAC,IAAI,GAAG,gBAAgB,CAAC;IAC/B,CAAC;CACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@agentfold/shared",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"types": "./dist/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"devDependencies": {
|
|
14
|
+
"typescript": "^5.7.0"
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist"
|
|
18
|
+
],
|
|
19
|
+
"publishConfig": {
|
|
20
|
+
"access": "public"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/279458179/agentfold.git"
|
|
25
|
+
},
|
|
26
|
+
"keywords": [
|
|
27
|
+
"ai",
|
|
28
|
+
"agents",
|
|
29
|
+
"context",
|
|
30
|
+
"llm",
|
|
31
|
+
"memory",
|
|
32
|
+
"tokens",
|
|
33
|
+
"context-window",
|
|
34
|
+
"openai",
|
|
35
|
+
"mcp"
|
|
36
|
+
],
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"description": "AgentFold — virtual memory for AI agents: fold cold context, recall anything.",
|
|
39
|
+
"scripts": {
|
|
40
|
+
"build": "tsc -b",
|
|
41
|
+
"test": "vitest run"
|
|
42
|
+
}
|
|
43
|
+
}
|