Skip to content

Commit 34b69f9

Browse files
authored
Rename some things (#2)
* getMemories -> recall * Rename key to scope * better file name
1 parent 1b2da8f commit 34b69f9

File tree

8 files changed

+68
-37
lines changed

8 files changed

+68
-37
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 = 'Rename key column to scope in memory table';
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 RENAME COLUMN key TO scope;
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 RENAME COLUMN scope TO key;
27+
`);
28+
} finally {
29+
await client.end();
30+
}
31+
}

src/apis/forget.ts

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

66
const inputSchema = {
77
id: z.coerce
88
.string()
99
.min(1)
1010
.describe('The id of a specific memory to delete.'),
11-
key: zKey,
11+
scope: zScope,
1212
} as const;
1313

1414
const outputSchema = {
@@ -30,15 +30,15 @@ export const forgetFactory: ApiFactory<
3030
inputSchema,
3131
outputSchema,
3232
},
33-
fn: async ({ id, key }) => {
33+
fn: async ({ id, scope }) => {
3434
const result = await pgPool.query<{ id: string }>(
3535
/* sql */ `
3636
UPDATE ${schema}.memory
3737
SET deleted_at = NOW(), updated_at = NOW()
38-
WHERE id = $1 AND key = $2 AND deleted_at IS NULL
38+
WHERE id = $1 AND scope = $2 AND deleted_at IS NULL
3939
RETURNING id
4040
`,
41-
[id, key],
41+
[id, scope],
4242
);
4343

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

src/apis/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { forgetFactory } from './forget.js';
2-
import { getMemoriesFactory } from './getMemories.js';
2+
import { recallFactory } from './recall.js';
33
import { rememberFactory } from './remember.js';
44
import { updateFactory } from './update.js';
55

66
export const apiFactories = [
77
rememberFactory,
88
updateFactory,
99
forgetFactory,
10-
getMemoriesFactory,
10+
recallFactory,
1111
] as const;
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
import { z } from 'zod';
22
import { ApiFactory } from '../shared/boilerplate/src/types.js';
3-
import { Memory, ServerContext, zKey, zMemory } from '../types.js';
3+
import { Memory, ServerContext, zScope, zMemory } from '../types.js';
44

55
const inputSchema = {
6-
key: zKey,
6+
scope: zScope,
77
} as const;
88

99
const outputSchema = {
1010
memories: z.array(zMemory).describe('The list of memories found.'),
11-
key: zKey,
11+
scope: zScope,
1212
} as const;
1313

14-
export const getMemoriesFactory: ApiFactory<
14+
export const recallFactory: ApiFactory<
1515
ServerContext,
1616
typeof inputSchema,
1717
typeof outputSchema
1818
> = ({ pgPool, schema }) => ({
19-
name: 'getMemories',
19+
name: 'recall',
2020
method: 'get',
21-
route: ['/memory', '/memory/:key'],
21+
route: ['/memory', '/memory/:scope'],
2222
config: {
23-
title: 'Retrieve memories by key',
23+
title: 'Retrieve memories by scope',
2424
description:
25-
'This endpoint retrieves memories from the database, using the provided key as the scope.',
25+
'This endpoint retrieves memories from the database, using the provided scope.',
2626
inputSchema,
2727
outputSchema,
2828
},
29-
fn: async ({ key }) => {
29+
fn: async ({ scope }) => {
3030
const result = await pgPool.query<Memory>(
3131
/* sql */ `
3232
SELECT id, content, source, created_at, updated_at
3333
FROM ${schema}.memory
34-
WHERE key = $1 AND deleted_at IS NULL
34+
WHERE scope = $1 AND deleted_at IS NULL
3535
`,
36-
[key],
36+
[scope],
3737
);
3838

3939
return {
4040
memories: result.rows,
41-
key,
41+
scope,
4242
};
4343
},
4444
});

src/apis/remember.ts

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

55
const inputSchema = {
6-
key: zKey,
6+
scope: zScope,
77
content: z.string().min(1).describe('The content to remember.'),
88
source: zSource,
99
} as const;
@@ -23,18 +23,18 @@ export const rememberFactory: ApiFactory<
2323
config: {
2424
title: 'Store a new memory',
2525
description:
26-
'This endpoint stores a new memory in the database, using the provided key as the scope.',
26+
'This endpoint stores a new memory in the database, using the provided scope.',
2727
inputSchema,
2828
outputSchema,
2929
},
30-
fn: async ({ key, content, source }) => {
30+
fn: async ({ scope, content, source }) => {
3131
const result = await pgPool.query<{ id: string }>(
3232
/* sql */ `
33-
INSERT INTO ${schema}.memory (key, content, source)
33+
INSERT INTO ${schema}.memory (scope, content, source)
3434
VALUES ($1, $2, $3)
3535
RETURNING id
3636
`,
37-
[key, content, source || null],
37+
[scope, content, source || null],
3838
);
3939

4040
return {

src/apis/update.ts

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

66
const inputSchema = {
77
id: z.coerce
88
.string()
99
.min(1)
1010
.describe('The id of a specific memory to replace.'),
11-
key: zKey,
11+
scope: zScope,
1212
content: z.string().min(1).describe('The new content to remember.'),
1313
source: zSource,
1414
} as const;
@@ -32,15 +32,15 @@ export const updateFactory: ApiFactory<
3232
inputSchema,
3333
outputSchema,
3434
},
35-
fn: async ({ id, key, content, source }) => {
35+
fn: async ({ id, scope, content, source }) => {
3636
const result = await pgPool.query<{ id: string }>(
3737
/* sql */ `
3838
UPDATE ${schema}.memory
3939
SET content = $1, source = $2, updated_at = NOW()
40-
WHERE id = $3 AND key = $4 AND deleted_at IS NULL
40+
WHERE id = $3 AND scope = $4 AND deleted_at IS NULL
4141
RETURNING id
4242
`,
43-
[content, source || null, id, key],
43+
[content, source || null, id, scope],
4444
);
4545

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

src/resources.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,26 @@ export const additionalSetup = ({
1111
}: AdditionalSetupArgs<ServerContext>) => {
1212
server.registerResource(
1313
'memories',
14-
new ResourceTemplate('memory://{key}', { list: undefined }),
14+
new ResourceTemplate('memory://{scope}', { list: undefined }),
1515
{
1616
title: 'Memories',
1717
description: 'A collection of memories',
1818
},
19-
async (uri, { key }) => {
19+
async (uri, { scope }) => {
2020
const result = await pgPool.query<Memory>(
2121
/* sql */ `
2222
SELECT id, content, created_at, updated_at
2323
FROM ${schema}.memory
24-
WHERE key = $1 AND deleted_at IS NULL
24+
WHERE scope = $1 AND deleted_at IS NULL
2525
`,
26-
[key],
26+
[scope],
2727
);
2828

2929
return {
3030
contents: [
3131
{
3232
uri: uri.href,
33-
text: `Memories: key=\`${key}\`, count=${result.rows.length}
33+
text: `Memories: scope=\`${scope}\`, count=${result.rows.length}
3434
${result.rows.map((m) => `- (${m.id}) ${m.content}`).join('\n')}
3535
`,
3636
},

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ export const zMemory = z.object({
2828

2929
export type Memory = z.infer<typeof zMemory>;
3030

31-
export const zKey = z
31+
export const zScope = z
3232
.string()
3333
.min(1)
3434
.describe(
35-
'A unique identifier for the target set of memories. Can be any combination of user and application ids, as needed for scoping and personalization.',
35+
'A unique identifier for the target set of memories. Can be any combination of user, application, contextual ids, as needed for scoping and personalization.',
3636
);

0 commit comments

Comments
 (0)