agents icon indicating copy to clipboard operation
agents copied to clipboard

TypeError: agent._url.replace is not a function

Open kchro3 opened this issue 9 months ago • 12 comments

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
    // }),
  });

kchro3 avatar Apr 05 '25 22:04 kchro3

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?

threepointone avatar Apr 05 '25 22:04 threepointone

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.

kchro3 avatar Apr 05 '25 22:04 kchro3

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.

threepointone avatar Apr 05 '25 22:04 threepointone

we'll write a guide for doing cross domain auth soon after dev week

threepointone avatar Apr 05 '25 22:04 threepointone

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

kchro3 avatar Apr 05 '25 22:04 kchro3

For now, I'll try the workaround with not using async to fetch the token.

kchro3 avatar Apr 05 '25 22:04 kchro3

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

kchro3 avatar Apr 06 '25 00:04 kchro3

hmm maybe ai sdk's use chat does polling

afk for the night, I'll have a look tomorrow

threepointone avatar Apr 06 '25 00:04 threepointone

Hey, any updates on this?

kchro3 avatar Apr 07 '25 16:04 kchro3

nothing specific yet, caught up with launch week

threepointone avatar Apr 07 '25 16:04 threepointone

Any updates on this btw? I saw your twitter thread

kchro3 avatar Apr 16 '25 16:04 kchro3

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

vpalam-dev avatar May 13 '25 14:05 vpalam-dev

Any update on this? Thanks!

bek91 avatar Jul 01 '25 06:07 bek91

@threepointone could you share an example on how to "abuse" the protocols array using useAgent?

Jensssen avatar Jul 30 '25 07:07 Jensssen

@threepointone never mind, I found a solution

Jensssen avatar Jul 30 '25 13:07 Jensssen

Any updates on this?

aurewill-gavel avatar Sep 04 '25 10:09 aurewill-gavel

you can find the guide on cross domain auth in docs folder in the repo, let me know if there are more questions, thanks

whoiskatrin avatar Sep 26 '25 13:09 whoiskatrin