TypeError: agent._url.replace is not a function
I get this error when I pass in the query field in the useAgent. I'm on the latest agents SDK.
For whatever it's worth, I'm running my backend locally on localhost:8787 & I am trying to use the "query" field in the useAgents hook so that I can pass in a JWT.
Something like:
// works if i uncomment below
const agent = useAgent({
agent: "test-agent",
host: "http://localhost:8787",
// query: async () => ({
// token: await getToken(), << calls clerk's getToken function
// }),
});
oh hmm, this is tricky. we use that field to send the initial get-messages call, which requires it to be ready synchronously, but when the query is an async url provider (like yours) it won't be available. I can fix the error being thrown (PR incoming) but it might not pass the query string when so. is there anyway you can fetch the token (say, with something like use()) and pass it as a regular query object to useAgent?
How are you supposed to authenticate requests in general?
It feels like a pretty big compromise to have to send the token through as a query parameter in the first place.
well, if your agents on the same domain as your app, you could use cookies, and it will just work.
are you trying to send a jwt because your app and agents are on different domains? unfortunately, the websockets standard hasn't been updated in years, and it's a PITA to do auth. options are using query params, or the protocols array.
we'll write a guide for doing cross domain auth soon after dev week
They are on different origins/subdomains, so I don't think it would work. One is a CF worker, one is a CF page.
A doc/guide with best practices on this would be great. I think that auth is going to be a blocker for any enterprise / production app
For now, I'll try the workaround with not using async to fetch the token.
Hi again, I believe that I'm still having problems:
It looks like it's polling GET on http://localhost:8787/agents/<agent>/<agent_id>?_pk=<ID> every 10s or so, and I have middleware that checks for a token, the token is missing.
I don't see this happening for a different agent that isn't a chat agent, so I assume that this is only happening for chat agents
hmm maybe ai sdk's use chat does polling
afk for the night, I'll have a look tomorrow
Hey, any updates on this?
nothing specific yet, caught up with launch week
Any updates on this btw? I saw your twitter thread
I'm also experiencing the same issue, my frontend is an Expo mobile app, so it'd be really great to have an auth guide
Any update on this? Thanks!
@threepointone could you share an example on how to "abuse" the protocols array using useAgent?
@threepointone never mind, I found a solution
Any updates on this?
you can find the guide on cross domain auth in docs folder in the repo, let me know if there are more questions, thanks