Nex
Nex3mo ago

Caching with Convex

Hi, I am using the action-cache in my app, however there's a problem with cache.fetch. Suppose I have an action summarizePaperInternal that returns a string, and I want to cache it, so I follow the docs at https://github.com/get-convex/action-cache, and create an ActionCache. I would assume that after defining the action, the return of the ActionCache will be same as my action. However that doesn't appear to be the case and ActionCache.fetch returns any.
No description
No description
No description
20 Replies
Nex
NexOP3mo ago
Speaking of the docs, there seems to be a bug/error/typo in it at https://github.com/get-convex/action-cache. myFunction just calls cache.fetch but doesn't actually return any results of the fetched value from the cache.
No description
erquhart
erquhart3mo ago
Good catches here, thanks for that. PR up: https://github.com/get-convex/action-cache/pull/7
ian
ian3mo ago
released in v0.2.7
Nex
NexOP3mo ago
Ok but my main problem is still the return type of cache.fetch It's returning any, and I can't deploy my app because of it unless I type cast NVM, I see the PR fixes the return type too. Let me try, one sec. Looks like it's been fixed. Thanks guys, lightspeed. Unrelated to this, but I've pointed out a bug on the landing page two times now but no one seems to respond @erquhart @Ian
ian
ian3mo ago
yeah thanks for that! I already saw it posted by Wayne in our internal chat this morning
Nex
NexOP3mo ago
Cool Seems like I spoke too soon. This is a really weird bug.
Nex
NexOP3mo ago
When I only store the result in a variable and don't return it, the type is correctly inferred
No description
Nex
NexOP3mo ago
However
Nex
NexOP3mo ago
As soon as I return the result, the type changes to any. What the fuck?
No description
Nex
NexOP3mo ago
@erquhart Sorry for the ping, but I see you're online. Could you take a look at this? This entire caching bug has been blocking me for a good 2+ days now.
ian
ian3mo ago
This seems like a common issue related to circular types. If you add return types to all the related handler functions that hopefully fixes it essentially when the return type of one function internal.foo.one depends on the return type type of internal.foo.two, unfortunately the type of internal itself depends on both of them. So one's return value ends up depending on itself.
ian
ian3mo ago
Actions | Convex Developer Hub
Actions can call third party services to do things such as processing a payment
Nex
NexOP3mo ago
Interesting.
Nex
NexOP3mo ago
I guess you'll probably need to update the docs again, because this won't work out of the box then:
No description
ian
ian3mo ago
good call - it's a good practice to put out there too. Do you have time to cut a PR improving the docs?
Nex
NexOP3mo ago
I can do it, but there's a problem
Nex
NexOP3mo ago
In the example, the return type of generateLLMResponse isn't known, so inside myFunction, we can't explicitly write out the return type.
No description
ian
ian3mo ago
you can take some liberties - that's just as an illustrative example. e.g. { text: string, usage: Usage } or just string even fwiw the example.ts is correct, but b/c it was explicitly typing the return value
Nex
NexOP3mo ago
I think ReturnType<generateLLMResponse> might work I'll just do a generic pr with string.
Nex
NexOP3mo ago
pr is up https://github.com/get-convex/action-cache/pull/8 --- fyi: doing
result: MyReturnType = myCache.fetch()
return result
result: MyReturnType = myCache.fetch()
return result
won't work. runs into same inference issue. there's only one way to fix it:
handler: async (ctx, args): Promise<MyReturnType> => {
handler: async (ctx, args): Promise<MyReturnType> => {
which is typing the handler
GitHub
explicitly type result by NexWasTaken · Pull Request #8 · get-con...
Addresses issues raised in Discord: https://discord.com/channels/1019350475847499849/1377179000643915808 By submitting this pull request, I confirm that you can use, modify, copy, and redistribute...

Did you find this page helpful?