Skip to content

Commit 77bec81

Browse files
committed
Add optional source col for memories
1 parent 7003493 commit 77bec81

File tree

5 files changed

+53
-11
lines changed

5 files changed

+53
-11
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { Client } from 'pg';
2+
3+
const schema = process.env.DB_SCHEMA || 'tiger_memory';
4+
5+
export const description = 'Add a source column for memories';
6+
7+
export async function up() {
8+
const client = new Client();
9+
10+
try {
11+
await client.connect();
12+
await client.query(/* sql */ `
13+
ALTER TABLE ${schema}.memory ADD COLUMN source TEXT DEFAULT NULL;
14+
`);
15+
} finally {
16+
await client.end();
17+
}
18+
}
19+
20+
export async function down() {
21+
const client = new Client();
22+
23+
try {
24+
await client.connect();
25+
await client.query(/* sql */ `
26+
ALTER TABLE ${schema}.memory DROP COLUMN source;
27+
`);
28+
} finally {
29+
await client.end();
30+
}
31+
}

src/apis/getMemories.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export const getMemoriesFactory: ApiFactory<
2929
fn: async ({ key }) => {
3030
const result = await pgPool.query<Memory>(
3131
/* sql */ `
32-
SELECT id, content, created_at, updated_at
32+
SELECT id, content, source, created_at, updated_at
3333
FROM ${schema}.memory
3434
WHERE key = $1 AND deleted_at IS NULL
3535
`,

src/apis/remember.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import { z } from 'zod';
22
import { ApiFactory } from '../shared/boilerplate/src/types.js';
3-
import { ServerContext, zKey } from '../types.js';
3+
import { ServerContext, zKey, zSource } from '../types.js';
44

55
const inputSchema = {
66
key: zKey,
77
content: z.string().min(1).describe('The content to remember.'),
8+
source: zSource,
89
} as const;
910

1011
const outputSchema = {
@@ -26,14 +27,14 @@ export const rememberFactory: ApiFactory<
2627
inputSchema,
2728
outputSchema,
2829
},
29-
fn: async ({ key, content }) => {
30+
fn: async ({ key, content, source }) => {
3031
const result = await pgPool.query<{ id: string }>(
3132
/* sql */ `
32-
INSERT INTO ${schema}.memory (key, content)
33-
VALUES ($1, $2)
33+
INSERT INTO ${schema}.memory (key, content, source)
34+
VALUES ($1, $2, $3)
3435
RETURNING id
3536
`,
36-
[key, content],
37+
[key, content, source || null],
3738
);
3839

3940
return {

src/apis/update.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { z } from 'zod';
22
import { ApiFactory } from '../shared/boilerplate/src/types.js';
3-
import { ServerContext, zKey } from '../types.js';
3+
import { ServerContext, zKey, zSource } from '../types.js';
44
import { StatusError } from '../shared/boilerplate/src/StatusError.js';
55

66
const inputSchema = {
@@ -10,6 +10,7 @@ const inputSchema = {
1010
.describe('The id of a specific memory to replace.'),
1111
key: zKey,
1212
content: z.string().min(1).describe('The new content to remember.'),
13+
source: zSource,
1314
} as const;
1415

1516
const outputSchema = {
@@ -31,15 +32,15 @@ export const updateFactory: ApiFactory<
3132
inputSchema,
3233
outputSchema,
3334
},
34-
fn: async ({ id, key, content }) => {
35+
fn: async ({ id, key, content, source }) => {
3536
const result = await pgPool.query<{ id: string }>(
3637
/* sql */ `
3738
UPDATE ${schema}.memory
38-
SET content = $1, updated_at = NOW()
39-
WHERE id = $2 AND key = $3 AND deleted_at IS NULL
39+
SET content = $1, source = $2, updated_at = NOW()
40+
WHERE id = $3 AND key = $4 AND deleted_at IS NULL
4041
RETURNING id
4142
`,
42-
[content, id, key],
43+
[content, source || null, id, key],
4344
);
4445

4546
if (result.rows[0]?.id == null) {

src/types.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,18 @@ export interface ServerContext extends Record<string, unknown> {
66
schema: string;
77
}
88

9+
export const zSource = z
10+
.string()
11+
.min(0)
12+
.nullable()
13+
.describe(
14+
'The source or origin of this memory. A deep URI to the origin of the fact is preferred (e.g., a specific URL, file path, or reference).',
15+
);
16+
917
export const zMemory = z.object({
1018
id: z.string().describe('The unique identifier of this memory.'),
1119
content: z.string().describe('The content of this memory.'),
20+
source: zSource,
1221
created_at: z
1322
.date()
1423
.describe('The date and time when this memory was created.'),

0 commit comments

Comments
 (0)