-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtypes.ts
More file actions
191 lines (167 loc) · 5.36 KB
/
types.ts
File metadata and controls
191 lines (167 loc) · 5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
* Supermemory Plugin Types
*
* Configuration schema and types for the Supermemory integration.
* API docs: https://supermemory.ai/docs/api-reference
*/
import { Type, type Static } from "@sinclair/typebox";
/**
* Memory operation mode:
* - tandem: Both built-in memory and Supermemory active. Registers supermemory_search tool.
* - primary: Supermemory only. Replaces memory_search tool. Requires slot config.
* - off: Plugin disabled.
*/
export const MemoryMode = Type.Union([
Type.Literal("tandem"),
Type.Literal("primary"),
Type.Literal("off"),
]);
export type MemoryMode = Static<typeof MemoryMode>;
/**
* Container scope for memory isolation:
* - agent: Memories shared across all sessions for this agent (personal assistant use case)
* - session: Memories isolated per session/conversation (multi-group privacy)
*/
export const ContainerScope = Type.Union([Type.Literal("agent"), Type.Literal("session")]);
export type ContainerScope = Static<typeof ContainerScope>;
/**
* Supermemory plugin configuration schema.
*/
export const supermemoryConfigSchema = Type.Object({
/** Supermemory API key (required) */
apiKey: Type.String({ description: "Supermemory API key" }),
/** Memory operation mode (default: tandem) */
mode: Type.Optional(
Type.Union([Type.Literal("tandem"), Type.Literal("primary"), Type.Literal("off")], {
default: "tandem",
description: "Memory mode: tandem (both active), primary (replace built-in), or off",
}),
),
/** Optional container/namespace tag for organizing memories */
containerTag: Type.Optional(
Type.String({ description: "Container tag for organizing memories by namespace" }),
),
/** Memory isolation scope (default: agent) */
containerScope: Type.Optional(
Type.Union([Type.Literal("agent"), Type.Literal("session")], {
default: "agent",
description:
"Memory isolation scope. 'agent' shares memories across all sessions (personal assistant). " +
"'session' isolates memories per conversation (multi-group privacy).",
}),
),
/** Auto-inject relevant memories on agent start (default: true) */
autoRecall: Type.Optional(
Type.Boolean({
default: true,
description: "Automatically inject relevant memories into agent context",
}),
),
/** Chunk relevance threshold for search 0.0-1.0 (default: 0.5) */
threshold: Type.Optional(
Type.Number({
minimum: 0,
maximum: 1,
default: 0.5,
description: "Chunk threshold for search relevance (0.2=broad, 0.8=precise)",
}),
),
/** Base URL for Supermemory API (default: https://api.supermemory.ai) */
baseUrl: Type.Optional(
Type.String({
default: "https://api.supermemory.ai",
description: "Supermemory API base URL",
}),
),
});
export type SupermemoryConfig = Static<typeof supermemoryConfigSchema>;
/**
* Supermemory v3 API response types.
*/
/** Chunk within a document search result */
export type SupermemoryChunk = {
content: string;
score: number;
isRelevant?: boolean;
};
/** Document result from /v3/search */
export type SupermemoryDocumentResult = {
documentId: string;
title?: string;
type?: string;
score: number;
chunks: SupermemoryChunk[];
metadata?: Record<string, unknown>;
createdAt?: string;
updatedAt?: string;
};
/** Response from POST /v3/search */
export type SupermemorySearchResponse = {
results: SupermemoryDocumentResult[];
total?: number;
timing?: Record<string, number>;
};
/** Flattened search result for easier consumption */
export type SupermemorySearchResult = {
id: string;
content: string;
score: number;
metadata?: Record<string, unknown>;
};
/** Response from POST /v3/documents */
export type SupermemoryAddResponse = {
id: string;
status: string;
};
/**
* Parsed configuration with defaults applied.
*/
export function parseConfig(raw: unknown): SupermemoryConfig {
const cfg = raw as Partial<SupermemoryConfig>;
return {
apiKey: cfg.apiKey ?? "",
mode: cfg.mode ?? "tandem",
containerTag: cfg.containerTag,
containerScope: cfg.containerScope ?? "agent",
autoRecall: cfg.autoRecall ?? true,
threshold: cfg.threshold ?? 0.5,
baseUrl: cfg.baseUrl ?? "https://api.supermemory.ai",
};
}
/**
* Resolve the container tag based on scope configuration.
* This determines how memories are isolated/shared.
*
* @param params - Resolution parameters
* @returns Container tag string for Supermemory API
*/
export function resolveContainerTag(params: {
config: SupermemoryConfig;
agentId?: string;
sessionKey?: string;
}): string | undefined {
const { config, agentId, sessionKey } = params;
// If explicit containerTag is set, use it as a prefix
const prefix = config.containerTag ? `${config.containerTag}:` : "";
switch (config.containerScope) {
case "session":
// Session scope: isolate memories per conversation
// e.g. 'agent:main:telegram:group:123' or just the sessionKey
if (sessionKey) {
return `${prefix}${sessionKey}`;
}
// Fallback to agentId if no sessionKey
if (agentId) {
return `${prefix}${agentId}`;
}
return config.containerTag;
case "agent":
default:
// Agent scope: share memories across all sessions
// e.g. 'main' (agentId) or just the containerTag
if (agentId) {
return `${prefix}${agentId}`;
}
return config.containerTag;
}
}