-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathindex.ts
More file actions
61 lines (56 loc) · 1.44 KB
/
index.ts
File metadata and controls
61 lines (56 loc) · 1.44 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
import { z } from 'zod';
import { renderTemplate } from './renderTemplate';
export const InputType = z.object({
baseUrl: z.preprocess(
(v) => (typeof v === 'string' && v.trim() === '' ? undefined : v),
z.string().default('https://api.memmachine.ai/v2')
),
apiKey: z.string().nonempty(),
orgId: z.string().optional(),
projectId: z.string().optional(),
types: z.array(z.string()).default(['episodic', 'semantic']),
query: z.string().nonempty(),
limit: z.number().default(10),
filter: z.string().default(''),
contextTemplate: z.string().default('')
});
export const OutputType = z.object({
memoryContext: z.string()
});
export async function tool({
baseUrl,
apiKey,
orgId,
projectId,
types,
query,
limit,
filter,
contextTemplate
}: z.infer<typeof InputType>): Promise<z.infer<typeof OutputType>> {
// 请求数据
const response = await fetch(`${baseUrl}/memories/search`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
authorization: `Bearer ${apiKey}`
},
body: JSON.stringify({
org_id: orgId,
project_id: projectId,
types,
top_k: limit,
query,
filter
})
});
if (!response.ok) {
return Promise.reject({
error: `MemMachine API Error: ${response.status} ${response.statusText}`
});
}
const data = await response.json();
return {
memoryContext: renderTemplate(contextTemplate, data?.content || {})
};
}