Skip to content

Commit 10cedd8

Browse files
committed
wip
1 parent 995e751 commit 10cedd8

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export class EpicMeMCP extends McpAgent<Env, State, Props> {
3838
tools: {},
3939
resources: {},
4040
completions: {},
41+
sampling: {},
4142
},
4243
instructions: `
4344
EpicMe is a journaling app that allows users to write about and review their experiences, thoughts, and reflections.

src/tools.ts

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,81 @@ Please ask them explicitely for their email address and don't just guess.
163163
})
164164
}
165165
}
166-
return createReply(
167-
`Entry "${createdEntry.title}" created successfully with ID "${createdEntry.id}"`,
166+
167+
// At this point, we're technically done, but we're going to ask their
168+
// LLM to suggest tags to be added to the entry.
169+
// TODO: make this happen after the response is sent to the user.
170+
171+
const existingTags = await agent.db.getTags(user.id)
172+
const existingTagNames = existingTags.map((t) => t.name)
173+
174+
const result = await agent.server.server.createMessage({
175+
messages: [
176+
{
177+
role: 'user',
178+
content: {
179+
type: 'text',
180+
mimeType: 'text/plain',
181+
text: `
182+
Based on this journal entry, suggest relevant tags. Consider the title, content, mood, location, and weather. Only suggest tags that don't already exist. Here's the entry:
183+
Title: ${createdEntry.title}
184+
Content: ${createdEntry.content}
185+
Mood: ${createdEntry.mood || 'not specified'}
186+
Location: ${createdEntry.location || 'not specified'}
187+
Weather: ${createdEntry.weather || 'not specified'}
188+
189+
Existing tags: ${existingTagNames.join(', ')}
190+
191+
Respond with a JSON array of tag names only.
192+
`.trim(),
193+
},
194+
},
195+
],
196+
systemPrompt:
197+
'You are a helpful assistant that suggests relevant tags for journal entries. Only suggest tags that would be useful for categorizing and finding entries later. Respond with a JSON array of tag names only.',
198+
maxTokens: 1000,
199+
})
200+
201+
const responseSchema = z.object({
202+
content: z.object({
203+
type: z.literal('text'),
204+
text: z
205+
.string()
206+
.transform((text) =>
207+
z.array(z.string()).parse(JSON.parse(text)),
208+
),
209+
}),
210+
})
211+
const parsedResult = responseSchema.parse(result)
212+
const suggestedTags = parsedResult.content.text
213+
214+
const newTags = suggestedTags.filter(
215+
(tag: string) => !existingTagNames.includes(tag),
168216
)
217+
218+
if (newTags.length > 0) {
219+
for (const tagName of newTags) {
220+
await agent.db.createTag(user.id, { name: tagName })
221+
}
222+
223+
const createdTags = await agent.db.getTags(user.id)
224+
225+
for (const tag of createdTags) {
226+
await agent.db.addTagToEntry(user.id, {
227+
entryId: createdEntry.id,
228+
tagId: tag.id,
229+
})
230+
}
231+
}
232+
233+
return createReply({
234+
entry: `Entry "${createdEntry.title}" created successfully with ID "${createdEntry.id}"`,
235+
suggestedTags: newTags,
236+
message:
237+
newTags.length > 0
238+
? `Here are some suggested tags for your entry: ${newTags.join(', ')}`
239+
: 'No new tags to suggest for this entry.',
240+
})
169241
} catch (error) {
170242
return createErrorReply(error)
171243
}

0 commit comments

Comments
 (0)