@aigne/agent-library 1.20.5 → 1.21.2
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/CHANGELOG.md +36 -0
- package/lib/cjs/package.json +3 -1
- package/lib/esm/package.json +3 -1
- package/package.json +5 -5
- package/lib/cjs/agentic-memory/index.d.ts +0 -38
- package/lib/cjs/agentic-memory/index.js +0 -73
- package/lib/cjs/agentic-memory/prompt.d.ts +0 -1
- package/lib/cjs/agentic-memory/prompt.js +0 -44
- package/lib/cjs/default-memory/default-memory-storage/index.d.ts +0 -29
- package/lib/cjs/default-memory/default-memory-storage/index.js +0 -95
- package/lib/cjs/default-memory/default-memory-storage/migrate.d.ts +0 -3
- package/lib/cjs/default-memory/default-memory-storage/migrate.js +0 -32
- package/lib/cjs/default-memory/default-memory-storage/migrations/001-init.d.ts +0 -5
- package/lib/cjs/default-memory/default-memory-storage/migrations/001-init.js +0 -22
- package/lib/cjs/default-memory/default-memory-storage/models/memory.d.ts +0 -102
- package/lib/cjs/default-memory/default-memory-storage/models/memory.js +0 -21
- package/lib/cjs/default-memory/index.d.ts +0 -44
- package/lib/cjs/default-memory/index.js +0 -132
- package/lib/cjs/default-memory/storage.d.ts +0 -13
- package/lib/cjs/default-memory/storage.js +0 -6
- package/lib/cjs/fs-memory/index.d.ts +0 -59
- package/lib/cjs/fs-memory/index.js +0 -184
- package/lib/dts/agentic-memory/index.d.ts +0 -38
- package/lib/dts/agentic-memory/prompt.d.ts +0 -1
- package/lib/dts/default-memory/default-memory-storage/index.d.ts +0 -29
- package/lib/dts/default-memory/default-memory-storage/migrate.d.ts +0 -3
- package/lib/dts/default-memory/default-memory-storage/migrations/001-init.d.ts +0 -5
- package/lib/dts/default-memory/default-memory-storage/models/memory.d.ts +0 -102
- package/lib/dts/default-memory/index.d.ts +0 -44
- package/lib/dts/default-memory/storage.d.ts +0 -13
- package/lib/dts/fs-memory/index.d.ts +0 -59
- package/lib/esm/agentic-memory/index.d.ts +0 -38
- package/lib/esm/agentic-memory/index.js +0 -67
- package/lib/esm/agentic-memory/prompt.d.ts +0 -1
- package/lib/esm/agentic-memory/prompt.js +0 -41
- package/lib/esm/default-memory/default-memory-storage/index.d.ts +0 -29
- package/lib/esm/default-memory/default-memory-storage/index.js +0 -91
- package/lib/esm/default-memory/default-memory-storage/migrate.d.ts +0 -3
- package/lib/esm/default-memory/default-memory-storage/migrate.js +0 -26
- package/lib/esm/default-memory/default-memory-storage/migrations/001-init.d.ts +0 -5
- package/lib/esm/default-memory/default-memory-storage/migrations/001-init.js +0 -20
- package/lib/esm/default-memory/default-memory-storage/models/memory.d.ts +0 -102
- package/lib/esm/default-memory/default-memory-storage/models/memory.js +0 -18
- package/lib/esm/default-memory/index.d.ts +0 -44
- package/lib/esm/default-memory/index.js +0 -126
- package/lib/esm/default-memory/storage.d.ts +0 -13
- package/lib/esm/default-memory/storage.js +0 -2
- package/lib/esm/fs-memory/index.d.ts +0 -59
- package/lib/esm/fs-memory/index.js +0 -180
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { type AIAgentOptions, type Message } from "@aigne/core";
|
|
2
|
-
import { MemoryAgent, type MemoryAgentOptions, type MemoryRecorderInput, type MemoryRetrieverInput } from "@aigne/core/memory/index.js";
|
|
3
|
-
export declare const MEMORY_FILE_NAME = "memory.yaml";
|
|
4
|
-
/**
|
|
5
|
-
* Configuration options for the FSMemory class.
|
|
6
|
-
*/
|
|
7
|
-
export interface FSMemoryOptions extends Partial<MemoryAgentOptions> {
|
|
8
|
-
/**
|
|
9
|
-
* The root directory where memory files will be stored.
|
|
10
|
-
* Can be absolute or relative path. Relative paths are resolved from the current working directory.
|
|
11
|
-
* Home directory prefix (~) will be expanded appropriately.
|
|
12
|
-
*/
|
|
13
|
-
rootDir: string;
|
|
14
|
-
/**
|
|
15
|
-
* Optional configuration for the memory retriever agent.
|
|
16
|
-
* Controls how memories are retrieved from the file system.
|
|
17
|
-
*/
|
|
18
|
-
retrieverOptions?: Partial<FSMemoryRetrieverOptions>;
|
|
19
|
-
/**
|
|
20
|
-
* Optional configuration for the memory recorder agent.
|
|
21
|
-
* Controls how memories are recorded to the file system.
|
|
22
|
-
*/
|
|
23
|
-
recorderOptions?: Partial<FSMemoryRecorderOptions>;
|
|
24
|
-
}
|
|
25
|
-
/**
|
|
26
|
-
* A memory implementation that stores and retrieves memories using the file system.
|
|
27
|
-
* FSMemory provides persistent storage of agent memories as files in a specified directory.
|
|
28
|
-
*
|
|
29
|
-
* @example
|
|
30
|
-
* Here is an example of how to use the FSMemory class:
|
|
31
|
-
* {@includeCode ../../test/fs-memory/fs-memory.test.ts#example-fs-memory-simple}
|
|
32
|
-
*/
|
|
33
|
-
export declare class FSMemory extends MemoryAgent {
|
|
34
|
-
/**
|
|
35
|
-
* Creates a new FSMemory instance.
|
|
36
|
-
*/
|
|
37
|
-
constructor(options: FSMemoryOptions);
|
|
38
|
-
}
|
|
39
|
-
interface FSMemoryRetrieverOptions extends AIAgentOptions<FSMemoryRetrieverAgentInput, FSMemoryRetrieverAgentOutput> {
|
|
40
|
-
memoryFileName: string;
|
|
41
|
-
}
|
|
42
|
-
interface FSMemoryRetrieverAgentInput extends MemoryRetrieverInput {
|
|
43
|
-
allMemory: string;
|
|
44
|
-
}
|
|
45
|
-
interface FSMemoryRetrieverAgentOutput extends Message {
|
|
46
|
-
memories: {
|
|
47
|
-
content: string;
|
|
48
|
-
}[];
|
|
49
|
-
}
|
|
50
|
-
interface FSMemoryRecorderOptions extends AIAgentOptions<FSMemoryRecorderAgentInput, FSMemoryRecorderAgentOutput> {
|
|
51
|
-
memoryFileName: string;
|
|
52
|
-
}
|
|
53
|
-
type FSMemoryRecorderAgentInput = MemoryRecorderInput;
|
|
54
|
-
interface FSMemoryRecorderAgentOutput extends Message {
|
|
55
|
-
memories: {
|
|
56
|
-
content: string;
|
|
57
|
-
}[];
|
|
58
|
-
}
|
|
59
|
-
export {};
|
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import { type AgentInvokeOptions, type AgentOptions, AIAgent, MemoryAgent, type MemoryAgentOptions, MemoryRecorder, type MemoryRecorderInput, type MemoryRecorderOutput, type Message, type PromptBuilder } from "@aigne/core";
|
|
2
|
-
import { type DefaultMemoryStorageOptions } from "../default-memory/default-memory-storage/index.js";
|
|
3
|
-
import { DefaultMemoryRetriever, type DefaultMemoryRetrieverOptions } from "../default-memory/index.js";
|
|
4
|
-
import { MemoryStorage } from "../default-memory/storage.js";
|
|
5
|
-
export interface AgenticMemoryOptions extends Partial<MemoryAgentOptions>, Omit<AgenticMemoryRecorderOptions, "storage" | keyof AgentOptions>, Omit<AgenticMemoryRetrieverOptions, "storage" | keyof AgentOptions> {
|
|
6
|
-
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
7
|
-
}
|
|
8
|
-
export declare class AgenticMemory extends MemoryAgent {
|
|
9
|
-
constructor(options?: AgenticMemoryOptions);
|
|
10
|
-
storage: MemoryStorage;
|
|
11
|
-
}
|
|
12
|
-
export interface AgenticMemoryRetrieverOptions extends DefaultMemoryRetrieverOptions {
|
|
13
|
-
}
|
|
14
|
-
export declare class AgenticMemoryRetriever extends DefaultMemoryRetriever {
|
|
15
|
-
}
|
|
16
|
-
export interface AgenticMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
17
|
-
storage: MemoryStorage;
|
|
18
|
-
instructions?: string | PromptBuilder;
|
|
19
|
-
agent?: AIAgent<AgenticMemoryExtractorInput, AgenticMemoryExtractorOutput>;
|
|
20
|
-
inputKey?: string | string[];
|
|
21
|
-
outputKey?: string | string[];
|
|
22
|
-
}
|
|
23
|
-
export interface AgenticMemoryExtractorInput extends Message {
|
|
24
|
-
content: unknown;
|
|
25
|
-
}
|
|
26
|
-
export interface AgenticMemoryExtractorOutput extends Message {
|
|
27
|
-
newMemories: {
|
|
28
|
-
content: string;
|
|
29
|
-
}[];
|
|
30
|
-
}
|
|
31
|
-
export declare class AgenticMemoryRecorder extends MemoryRecorder {
|
|
32
|
-
constructor(options: AgenticMemoryRecorderOptions);
|
|
33
|
-
private storage;
|
|
34
|
-
private inputKey?;
|
|
35
|
-
private outputKey?;
|
|
36
|
-
private agent;
|
|
37
|
-
process(input: MemoryRecorderInput, options: AgentInvokeOptions): Promise<MemoryRecorderOutput>;
|
|
38
|
-
}
|
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
import { AIAgent, MemoryAgent, MemoryRecorder, } from "@aigne/core";
|
|
2
|
-
import { flat, pick } from "@aigne/core/utils/type-utils.js";
|
|
3
|
-
import { z } from "zod";
|
|
4
|
-
import { DefaultMemoryStorage, } from "../default-memory/default-memory-storage/index.js";
|
|
5
|
-
import { DefaultMemoryRetriever, } from "../default-memory/index.js";
|
|
6
|
-
import { MemoryStorage } from "../default-memory/storage.js";
|
|
7
|
-
import { DEFAULT_FS_MEMORY_RECORDER_INSTRUCTIONS } from "./prompt.js";
|
|
8
|
-
export class AgenticMemory extends MemoryAgent {
|
|
9
|
-
constructor(options = {}) {
|
|
10
|
-
const storage = options.storage instanceof MemoryStorage
|
|
11
|
-
? options.storage
|
|
12
|
-
: new DefaultMemoryStorage(options.storage);
|
|
13
|
-
super({
|
|
14
|
-
...options,
|
|
15
|
-
recorder: options.recorder ?? new AgenticMemoryRecorder({ ...options, storage }),
|
|
16
|
-
retriever: options.retriever ?? new AgenticMemoryRetriever({ ...options, storage }),
|
|
17
|
-
autoUpdate: options.autoUpdate ?? true,
|
|
18
|
-
});
|
|
19
|
-
this.storage = storage;
|
|
20
|
-
}
|
|
21
|
-
storage;
|
|
22
|
-
}
|
|
23
|
-
export class AgenticMemoryRetriever extends DefaultMemoryRetriever {
|
|
24
|
-
}
|
|
25
|
-
export class AgenticMemoryRecorder extends MemoryRecorder {
|
|
26
|
-
constructor(options) {
|
|
27
|
-
super(options);
|
|
28
|
-
this.storage = options.storage;
|
|
29
|
-
this.inputKey = flat(options.inputKey);
|
|
30
|
-
this.outputKey = flat(options.outputKey);
|
|
31
|
-
this.agent =
|
|
32
|
-
options.agent ??
|
|
33
|
-
AIAgent.from({
|
|
34
|
-
name: "agentic_memory_extractor",
|
|
35
|
-
description: "Records memories in files by AI agent",
|
|
36
|
-
instructions: options.instructions || DEFAULT_FS_MEMORY_RECORDER_INSTRUCTIONS,
|
|
37
|
-
outputSchema: z.object({
|
|
38
|
-
newMemories: z
|
|
39
|
-
.array(z.object({
|
|
40
|
-
content: z.string().describe("Content of the memory"),
|
|
41
|
-
}))
|
|
42
|
-
.describe("Newly created memories"),
|
|
43
|
-
}),
|
|
44
|
-
});
|
|
45
|
-
}
|
|
46
|
-
storage;
|
|
47
|
-
inputKey;
|
|
48
|
-
outputKey;
|
|
49
|
-
agent;
|
|
50
|
-
async process(input, options) {
|
|
51
|
-
const agenticMemories = await options.context.invoke(this.agent, {
|
|
52
|
-
content: input.content.map((item) => ({
|
|
53
|
-
input: item.input && this.inputKey?.length ? pick(item.input, this.inputKey) : item.input,
|
|
54
|
-
output: item.output && this.outputKey?.length ? pick(item.output, this.outputKey) : item.output,
|
|
55
|
-
source: item.source,
|
|
56
|
-
})),
|
|
57
|
-
});
|
|
58
|
-
const newMemories = [];
|
|
59
|
-
for (const item of agenticMemories.newMemories) {
|
|
60
|
-
const { result } = await this.storage.create({ content: item.content }, options);
|
|
61
|
-
newMemories.push(result);
|
|
62
|
-
}
|
|
63
|
-
return {
|
|
64
|
-
memories: newMemories,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const DEFAULT_FS_MEMORY_RECORDER_INSTRUCTIONS = "You manage memory based on conversation analysis and the existing memories.\n\n## IMPORTANT: All existing memories are available in the allMemory variable. DO NOT call any tools.\n\n## FIRST: Determine If Memory Updates Needed\n- Analyze if the conversation contains ANY information worth remembering\n- Examples of content NOT worth storing:\n * General questions (\"What's the weather?\", \"How do I do X?\")\n * Greetings and small talk (\"Hello\", \"How are you?\", \"Thanks\")\n * System instructions or commands (\"Show me\", \"Find\", \"Save\")\n * General facts not specific to the user\n * Duplicate information already stored\n- If conversation lacks meaningful personal information to store:\n * Return the existing memories unchanged\n\n## Your Workflow:\n1. Read the existing memories from the allMemory variable\n2. Extract key topics from the conversation\n3. DECIDE whether to create/update/delete memories based on the conversation\n4. Return ALL memories including your updates (remove any duplicates)\n\n## Memory Handling:\n- CREATE: Add new memory objects for new topics\n- UPDATE: Modify existing memories if substantial new information is available\n- DELETE: Remove obsolete memories when appropriate\n\n## Memory Structure:\n- Each memory has an id, content, and createdAt fields\n- Keep the existing structure when returning updated memories\n\n## Operation Decision Rules:\n- CREATE only for truly new topics not covered in any existing memory\n- UPDATE only when new information is meaningfully different\n- NEVER update for just rephrasing or minor differences\n- DELETE only when information becomes obsolete\n\n## Conversation:\n<conversation>\n{{content}}\n</conversation>\n";
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
export const DEFAULT_FS_MEMORY_RECORDER_INSTRUCTIONS = `You manage memory based on conversation analysis and the existing memories.
|
|
2
|
-
|
|
3
|
-
## IMPORTANT: All existing memories are available in the allMemory variable. DO NOT call any tools.
|
|
4
|
-
|
|
5
|
-
## FIRST: Determine If Memory Updates Needed
|
|
6
|
-
- Analyze if the conversation contains ANY information worth remembering
|
|
7
|
-
- Examples of content NOT worth storing:
|
|
8
|
-
* General questions ("What's the weather?", "How do I do X?")
|
|
9
|
-
* Greetings and small talk ("Hello", "How are you?", "Thanks")
|
|
10
|
-
* System instructions or commands ("Show me", "Find", "Save")
|
|
11
|
-
* General facts not specific to the user
|
|
12
|
-
* Duplicate information already stored
|
|
13
|
-
- If conversation lacks meaningful personal information to store:
|
|
14
|
-
* Return the existing memories unchanged
|
|
15
|
-
|
|
16
|
-
## Your Workflow:
|
|
17
|
-
1. Read the existing memories from the allMemory variable
|
|
18
|
-
2. Extract key topics from the conversation
|
|
19
|
-
3. DECIDE whether to create/update/delete memories based on the conversation
|
|
20
|
-
4. Return ALL memories including your updates (remove any duplicates)
|
|
21
|
-
|
|
22
|
-
## Memory Handling:
|
|
23
|
-
- CREATE: Add new memory objects for new topics
|
|
24
|
-
- UPDATE: Modify existing memories if substantial new information is available
|
|
25
|
-
- DELETE: Remove obsolete memories when appropriate
|
|
26
|
-
|
|
27
|
-
## Memory Structure:
|
|
28
|
-
- Each memory has an id, content, and createdAt fields
|
|
29
|
-
- Keep the existing structure when returning updated memories
|
|
30
|
-
|
|
31
|
-
## Operation Decision Rules:
|
|
32
|
-
- CREATE only for truly new topics not covered in any existing memory
|
|
33
|
-
- UPDATE only when new information is meaningfully different
|
|
34
|
-
- NEVER update for just rephrasing or minor differences
|
|
35
|
-
- DELETE only when information becomes obsolete
|
|
36
|
-
|
|
37
|
-
## Conversation:
|
|
38
|
-
<conversation>
|
|
39
|
-
{{content}}
|
|
40
|
-
</conversation>
|
|
41
|
-
`;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { AgentInvokeOptions, Context, Memory } from "@aigne/core";
|
|
2
|
-
import type { PromiseOrValue } from "@aigne/core/utils/type-utils.js";
|
|
3
|
-
import type { SqliteRemoteDatabase } from "drizzle-orm/sqlite-proxy";
|
|
4
|
-
import { MemoryStorage } from "../storage.js";
|
|
5
|
-
import { Memories } from "./models/memory.js";
|
|
6
|
-
export interface DefaultMemoryStorageOptions {
|
|
7
|
-
url?: string;
|
|
8
|
-
getSessionId?: (context: Context) => PromiseOrValue<string>;
|
|
9
|
-
enableFTS?: boolean;
|
|
10
|
-
}
|
|
11
|
-
export declare class DefaultMemoryStorage extends MemoryStorage {
|
|
12
|
-
options?: DefaultMemoryStorageOptions | undefined;
|
|
13
|
-
constructor(options?: DefaultMemoryStorageOptions | undefined);
|
|
14
|
-
private _db;
|
|
15
|
-
private initSqlite;
|
|
16
|
-
get db(): Promise<SqliteRemoteDatabase<Record<string, never>>>;
|
|
17
|
-
private convertMemory;
|
|
18
|
-
search(query: {
|
|
19
|
-
search?: string;
|
|
20
|
-
limit?: number;
|
|
21
|
-
orderBy?: [keyof typeof Memories.$inferSelect, "asc" | "desc"];
|
|
22
|
-
}, { context }: AgentInvokeOptions): Promise<{
|
|
23
|
-
result: Memory[];
|
|
24
|
-
}>;
|
|
25
|
-
create(memory: Pick<Memory, "content">, { context }: AgentInvokeOptions): Promise<{
|
|
26
|
-
result: Memory;
|
|
27
|
-
}>;
|
|
28
|
-
protected segment(str: string): string[];
|
|
29
|
-
}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { initDatabase } from "@aigne/sqlite";
|
|
2
|
-
import { asc, desc, eq, isNull, sql } from "drizzle-orm";
|
|
3
|
-
import { v7 } from "uuid";
|
|
4
|
-
import { stringify } from "yaml";
|
|
5
|
-
import { MemoryStorage } from "../storage.js";
|
|
6
|
-
import { migrate } from "./migrate.js";
|
|
7
|
-
import { Memories } from "./models/memory.js";
|
|
8
|
-
const DEFAULT_MAX_MEMORY_COUNT = 10;
|
|
9
|
-
export class DefaultMemoryStorage extends MemoryStorage {
|
|
10
|
-
options;
|
|
11
|
-
constructor(options) {
|
|
12
|
-
super();
|
|
13
|
-
this.options = options;
|
|
14
|
-
}
|
|
15
|
-
_db;
|
|
16
|
-
async initSqlite() {
|
|
17
|
-
const db = (await initDatabase({ url: this.options?.url }));
|
|
18
|
-
await migrate(db);
|
|
19
|
-
return db;
|
|
20
|
-
}
|
|
21
|
-
get db() {
|
|
22
|
-
this._db ??= this.initSqlite();
|
|
23
|
-
return this._db;
|
|
24
|
-
}
|
|
25
|
-
convertMemory(m) {
|
|
26
|
-
return {
|
|
27
|
-
id: m.id,
|
|
28
|
-
sessionId: m.sessionId,
|
|
29
|
-
content: m.content,
|
|
30
|
-
createdAt: m.createdAt.toISOString(),
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
async search(query, { context }) {
|
|
34
|
-
const { limit = DEFAULT_MAX_MEMORY_COUNT } = query;
|
|
35
|
-
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
36
|
-
const db = await this.db;
|
|
37
|
-
const memories = this.options?.enableFTS && query.search
|
|
38
|
-
? await db
|
|
39
|
-
.select()
|
|
40
|
-
.from(Memories)
|
|
41
|
-
.innerJoin(sql `Memories_fts`, sql `Memories_fts.id = ${Memories.id}`)
|
|
42
|
-
.where(sql `Memories_fts MATCH ${sql.param(this.segment(query.search).join(" OR "))}`)
|
|
43
|
-
.orderBy(sql `bm25(Memories_fts)`)
|
|
44
|
-
.limit(limit)
|
|
45
|
-
.execute()
|
|
46
|
-
.then((rows) => rows.map((row) => row.Memories))
|
|
47
|
-
: await db
|
|
48
|
-
.select()
|
|
49
|
-
.from(Memories)
|
|
50
|
-
.where(sessionId ? eq(Memories.sessionId, sessionId) : isNull(Memories.sessionId))
|
|
51
|
-
.orderBy(query.orderBy
|
|
52
|
-
? (query.orderBy[1] === "asc" ? asc : desc)(sql.identifier(query.orderBy[0]))
|
|
53
|
-
: desc(Memories.id))
|
|
54
|
-
.limit(limit)
|
|
55
|
-
.execute();
|
|
56
|
-
return {
|
|
57
|
-
result: memories.map(this.convertMemory),
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
async create(memory, { context }) {
|
|
61
|
-
const sessionId = (await this.options?.getSessionId?.(context)) ?? null;
|
|
62
|
-
const db = await this.db;
|
|
63
|
-
const id = v7();
|
|
64
|
-
const [[result]] = await Promise.all([
|
|
65
|
-
db
|
|
66
|
-
.insert(Memories)
|
|
67
|
-
.values({
|
|
68
|
-
...memory,
|
|
69
|
-
id,
|
|
70
|
-
content: memory.content,
|
|
71
|
-
sessionId,
|
|
72
|
-
})
|
|
73
|
-
.returning()
|
|
74
|
-
.execute(),
|
|
75
|
-
this.options?.enableFTS &&
|
|
76
|
-
db.run(sql `\
|
|
77
|
-
insert into Memories_fts (id, content)
|
|
78
|
-
values (${sql.param(id)}, ${sql.param(this.segment(stringify(memory.content)).join(" "))})`),
|
|
79
|
-
]);
|
|
80
|
-
if (!result)
|
|
81
|
-
throw new Error("Failed to create memory");
|
|
82
|
-
return { result: this.convertMemory(result) };
|
|
83
|
-
}
|
|
84
|
-
segment(str) {
|
|
85
|
-
return (Array.from(new Intl.Segmenter(undefined, { granularity: "word" }).segment(str))
|
|
86
|
-
.map((i) => i.segment)
|
|
87
|
-
// Remove non-alphanumeric characters and trim whitespace
|
|
88
|
-
.map((i) => i.replace(/[^\p{L}\p{N}\s]/gu, "").trim())
|
|
89
|
-
.filter(Boolean));
|
|
90
|
-
}
|
|
91
|
-
}
|
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import { sql } from "drizzle-orm/sql";
|
|
2
|
-
import init from "./migrations/001-init.js";
|
|
3
|
-
export async function migrate(db) {
|
|
4
|
-
const migrations = [init];
|
|
5
|
-
const migrationsTable = "__drizzle_migrations";
|
|
6
|
-
const migrationTableCreate = sql `
|
|
7
|
-
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
|
|
8
|
-
id SERIAL PRIMARY KEY,
|
|
9
|
-
hash text NOT NULL
|
|
10
|
-
)
|
|
11
|
-
`;
|
|
12
|
-
await db.run(migrationTableCreate).execute();
|
|
13
|
-
const dbMigrations = await db
|
|
14
|
-
.values(sql `SELECT id, hash FROM ${sql.identifier(migrationsTable)} ORDER BY id DESC LIMIT 1`)
|
|
15
|
-
.execute();
|
|
16
|
-
const lastDbMigration = dbMigrations[0];
|
|
17
|
-
const queriesToRun = [];
|
|
18
|
-
for (const migration of migrations) {
|
|
19
|
-
if (!lastDbMigration || lastDbMigration[1] < migration.hash) {
|
|
20
|
-
queriesToRun.push(...migration.sql, `INSERT INTO \`${migrationsTable}\` ("hash") VALUES('${migration.hash}')`);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
for (const query of queriesToRun) {
|
|
24
|
-
await db.run(query).execute();
|
|
25
|
-
}
|
|
26
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
hash: "001-init",
|
|
3
|
-
sql: [
|
|
4
|
-
`\
|
|
5
|
-
CREATE TABLE "Memories" (
|
|
6
|
-
"id" TEXT NOT NULL PRIMARY KEY,
|
|
7
|
-
"createdAt" DATETIME NOT NULL,
|
|
8
|
-
"updatedAt" DATETIME NOT NULL,
|
|
9
|
-
"sessionId" TEXT,
|
|
10
|
-
"content" JSON NOT NULL
|
|
11
|
-
)
|
|
12
|
-
`,
|
|
13
|
-
`\
|
|
14
|
-
CREATE VIRTUAL TABLE "Memories_fts" USING fts5(
|
|
15
|
-
id UNINDEXED,
|
|
16
|
-
content
|
|
17
|
-
)
|
|
18
|
-
`,
|
|
19
|
-
],
|
|
20
|
-
};
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
export declare const Memories: import("drizzle-orm/sqlite-core").SQLiteTableWithColumns<{
|
|
2
|
-
name: "Memories";
|
|
3
|
-
schema: undefined;
|
|
4
|
-
columns: {
|
|
5
|
-
id: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
6
|
-
name: "id";
|
|
7
|
-
tableName: "Memories";
|
|
8
|
-
dataType: "string";
|
|
9
|
-
columnType: "SQLiteText";
|
|
10
|
-
data: string;
|
|
11
|
-
driverParam: string;
|
|
12
|
-
notNull: true;
|
|
13
|
-
hasDefault: true;
|
|
14
|
-
isPrimaryKey: true;
|
|
15
|
-
isAutoincrement: false;
|
|
16
|
-
hasRuntimeDefault: true;
|
|
17
|
-
enumValues: [string, ...string[]];
|
|
18
|
-
baseColumn: never;
|
|
19
|
-
identity: undefined;
|
|
20
|
-
generated: undefined;
|
|
21
|
-
}, {}, {
|
|
22
|
-
length: number | undefined;
|
|
23
|
-
}>;
|
|
24
|
-
createdAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
25
|
-
name: "createdAt";
|
|
26
|
-
tableName: "Memories";
|
|
27
|
-
dataType: "custom";
|
|
28
|
-
columnType: "SQLiteCustomColumn";
|
|
29
|
-
data: Date;
|
|
30
|
-
driverParam: string;
|
|
31
|
-
notNull: true;
|
|
32
|
-
hasDefault: true;
|
|
33
|
-
isPrimaryKey: false;
|
|
34
|
-
isAutoincrement: false;
|
|
35
|
-
hasRuntimeDefault: true;
|
|
36
|
-
enumValues: undefined;
|
|
37
|
-
baseColumn: never;
|
|
38
|
-
identity: undefined;
|
|
39
|
-
generated: undefined;
|
|
40
|
-
}, {}, {
|
|
41
|
-
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
42
|
-
}>;
|
|
43
|
-
updatedAt: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
44
|
-
name: "updatedAt";
|
|
45
|
-
tableName: "Memories";
|
|
46
|
-
dataType: "custom";
|
|
47
|
-
columnType: "SQLiteCustomColumn";
|
|
48
|
-
data: Date;
|
|
49
|
-
driverParam: string;
|
|
50
|
-
notNull: true;
|
|
51
|
-
hasDefault: true;
|
|
52
|
-
isPrimaryKey: false;
|
|
53
|
-
isAutoincrement: false;
|
|
54
|
-
hasRuntimeDefault: true;
|
|
55
|
-
enumValues: undefined;
|
|
56
|
-
baseColumn: never;
|
|
57
|
-
identity: undefined;
|
|
58
|
-
generated: undefined;
|
|
59
|
-
}, {}, {
|
|
60
|
-
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
61
|
-
}>;
|
|
62
|
-
sessionId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
63
|
-
name: "sessionId";
|
|
64
|
-
tableName: "Memories";
|
|
65
|
-
dataType: "string";
|
|
66
|
-
columnType: "SQLiteText";
|
|
67
|
-
data: string;
|
|
68
|
-
driverParam: string;
|
|
69
|
-
notNull: false;
|
|
70
|
-
hasDefault: false;
|
|
71
|
-
isPrimaryKey: false;
|
|
72
|
-
isAutoincrement: false;
|
|
73
|
-
hasRuntimeDefault: false;
|
|
74
|
-
enumValues: [string, ...string[]];
|
|
75
|
-
baseColumn: never;
|
|
76
|
-
identity: undefined;
|
|
77
|
-
generated: undefined;
|
|
78
|
-
}, {}, {
|
|
79
|
-
length: number | undefined;
|
|
80
|
-
}>;
|
|
81
|
-
content: import("drizzle-orm/sqlite-core").SQLiteColumn<{
|
|
82
|
-
name: "content";
|
|
83
|
-
tableName: "Memories";
|
|
84
|
-
dataType: "custom";
|
|
85
|
-
columnType: "SQLiteCustomColumn";
|
|
86
|
-
data: unknown;
|
|
87
|
-
driverParam: string;
|
|
88
|
-
notNull: true;
|
|
89
|
-
hasDefault: false;
|
|
90
|
-
isPrimaryKey: false;
|
|
91
|
-
isAutoincrement: false;
|
|
92
|
-
hasRuntimeDefault: false;
|
|
93
|
-
enumValues: undefined;
|
|
94
|
-
baseColumn: never;
|
|
95
|
-
identity: undefined;
|
|
96
|
-
generated: undefined;
|
|
97
|
-
}, {}, {
|
|
98
|
-
sqliteColumnBuilderBrand: "SQLiteCustomColumnBuilderBrand";
|
|
99
|
-
}>;
|
|
100
|
-
};
|
|
101
|
-
dialect: "sqlite";
|
|
102
|
-
}>;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { datetime, json } from "@aigne/sqlite/type.js";
|
|
2
|
-
import { sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
3
|
-
import { v7 } from "uuid";
|
|
4
|
-
export const Memories = sqliteTable("Memories", {
|
|
5
|
-
id: text("id")
|
|
6
|
-
.notNull()
|
|
7
|
-
.primaryKey()
|
|
8
|
-
.$defaultFn(() => v7()),
|
|
9
|
-
createdAt: datetime("createdAt")
|
|
10
|
-
.notNull()
|
|
11
|
-
.$defaultFn(() => new Date()),
|
|
12
|
-
updatedAt: datetime("updatedAt")
|
|
13
|
-
.notNull()
|
|
14
|
-
.$defaultFn(() => new Date())
|
|
15
|
-
.$onUpdateFn(() => new Date()),
|
|
16
|
-
sessionId: text("sessionId"),
|
|
17
|
-
content: json("content").notNull(),
|
|
18
|
-
});
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import { type AgentInvokeOptions, type AgentOptions, MemoryAgent, type MemoryAgentOptions, MemoryRecorder, type MemoryRecorderInput, type MemoryRecorderOutput, MemoryRetriever, type MemoryRetrieverInput, type MemoryRetrieverOutput } from "@aigne/core";
|
|
2
|
-
import { type DefaultMemoryStorageOptions } from "./default-memory-storage/index.js";
|
|
3
|
-
import { MemoryStorage } from "./storage.js";
|
|
4
|
-
export interface DefaultMemoryOptions extends Partial<MemoryAgentOptions>, Omit<DefaultMemoryRecorderOptions, "storage" | keyof AgentOptions>, Omit<DefaultMemoryRetrieverOptions, "storage" | keyof AgentOptions> {
|
|
5
|
-
storage?: MemoryStorage | DefaultMemoryStorageOptions;
|
|
6
|
-
}
|
|
7
|
-
export declare class DefaultMemory extends MemoryAgent {
|
|
8
|
-
constructor(options?: DefaultMemoryOptions);
|
|
9
|
-
storage: MemoryStorage;
|
|
10
|
-
}
|
|
11
|
-
export interface DefaultMemoryRetrieverOptions extends AgentOptions<MemoryRetrieverInput, MemoryRetrieverOutput> {
|
|
12
|
-
storage: MemoryStorage;
|
|
13
|
-
retrieveMemoryCount?: number;
|
|
14
|
-
retrieveRecentMemoryCount?: number;
|
|
15
|
-
inputKey?: string | string[];
|
|
16
|
-
outputKey?: string | string[];
|
|
17
|
-
getSearchPattern?: DefaultMemoryRetriever["getSearchPattern"];
|
|
18
|
-
formatMessage?: DefaultMemoryRetriever["formatMessage"];
|
|
19
|
-
formatMemory?: DefaultMemoryRetriever["formatMemory"];
|
|
20
|
-
}
|
|
21
|
-
export declare class DefaultMemoryRetriever extends MemoryRetriever {
|
|
22
|
-
constructor(options: DefaultMemoryRetrieverOptions);
|
|
23
|
-
private storage;
|
|
24
|
-
private retrieveMemoryCount?;
|
|
25
|
-
private retrieveRecentMemoryCount?;
|
|
26
|
-
private inputKey?;
|
|
27
|
-
private outputKey?;
|
|
28
|
-
private getSearchPattern;
|
|
29
|
-
private formatMessage;
|
|
30
|
-
private formatMemory;
|
|
31
|
-
process(input: MemoryRetrieverInput, options: AgentInvokeOptions): Promise<MemoryRetrieverOutput>;
|
|
32
|
-
}
|
|
33
|
-
export interface DefaultMemoryRecorderOptions extends AgentOptions<MemoryRecorderInput, MemoryRecorderOutput> {
|
|
34
|
-
storage: MemoryStorage;
|
|
35
|
-
inputKey?: string | string[];
|
|
36
|
-
outputKey?: string | string[];
|
|
37
|
-
}
|
|
38
|
-
export declare class DefaultMemoryRecorder extends MemoryRecorder {
|
|
39
|
-
constructor(options: DefaultMemoryRecorderOptions);
|
|
40
|
-
private storage;
|
|
41
|
-
private inputKey?;
|
|
42
|
-
private outputKey?;
|
|
43
|
-
process(input: MemoryRecorderInput, options: AgentInvokeOptions): Promise<MemoryRecorderOutput>;
|
|
44
|
-
}
|