Part 2

How my chat turns links into project cards

The project cards in my chat used to depend on a tool call the model sometimes forgot. Now a link in the answer is enough. Follow one from the streamed text to the React card.

Phillipp Bertram7 min read
  • React
  • AI agents
  • TypeScript
A Markdown link to the Pricewerk project, connected by a copper line to the project card it turns into.
Contents6 sections

For about two weeks, a project card in this chat depended on the model remembering to ask for one. When an answer named a project, the model was also supposed to call a tool, showProjects. Naming the project was never the problem. The tool call was. The model did the hard part, choosing a project that fits the question, and then skipped the paperwork. When it had to note down a visitor's project in the same step, it often dropped the cards altogether. The comment about that is still in the agent's code; you may have spotted it in part 1.

For a visitor, nothing looks broken when that happens. The answer says that I use AI agents at Pricewerk, and they are left to go and find Pricewerk. My fixes were the usual ones: let the model make both tool calls in one step, add another sentence to the prompt. But a prompt is a request, not a guarantee, and I was still asking the model to remember two things.

The better question was why it had to ask at all. A good answer already says which project it means. If it says so with a link, the client can do the rest, and since 12 November that is how the cards work. The example below plays back an answer in chunks, the way the model streams it. The Pricewerk card appears with the chunk that completes the link. Switch to Markdown to see how little the answer needs to carry.

Chat example
Phillipp · Agent

How do you use AI agents in your own projects?

At Pricewerk, I use AI agents to summarise market news for my research.

2026 - present · Founder & Product Engineer

Pricewerk: Analysis tools and AI agents for traders

With Pricewerk, I build analysis tools and indicators for TradingView and MetaTrader. AI agents help with internal research, summarise news and analyse market information.

  • AI agents
  • TradingView
  • MetaTrader
  • Python
View project

One link is enough

The important part of the answer is the destination of the link. Every project in my portfolio has an ID, and the English and German pages use the same one. That gives me a stable connection between a sentence the model writes and a project I have actually published.

Assistant message
md
At [Pricewerk](/en#projekt-pricewerk), I use AI agents to summarise market news for my research.

The model makes the one decision only it can make: which project helps answer this visitor's question. Everything after that is ordinary code, and ordinary code doesn't forget.

What counts as a link

The client reads the assistant's text and looks for project links. Two short functions do that:

chat-links.ts
ts
const PROJECT_ANCHOR = /^\/#projekt-([\w-]+)$/;
const INTERNAL_LINK_PATTERN = /\[[^\]]*\]\((\/[^)\s]*)\)/g;

/** Only complete Markdown links count; code examples and unfinished streamed links do not. */
export function getInternalLinks(text: string): string[] {
  const prose = text.replace(/```[\s\S]*?(?:```|$)|~~~[\s\S]*?(?:~~~|$)|`[^`]*`/g, '');
  return [...new Set([...prose.matchAll(INTERNAL_LINK_PATTERN)].map((match) => match[1]))];
}

/** Both language versions use the same public portfolio IDs. */
export function projectIdFromHref(href: string, projects: ProjectIdentities): string | null {
  const id = PROJECT_ANCHOR.exec(withoutLocale(href))?.[1];
  return id && projects.some((project) => project.id === id) ? id : null;
}

Only complete links. The pattern needs the closing parenthesis. Halfway through a streamed answer, the text ends in [Pricewerk](/en#projekt-price, and that matches nothing. The resolver runs again with every update, and the card appears with the chunk that completes the link.

Not in code. The highlighted line removes fenced blocks and inline code before it looks for links. The |$ covers a code block that is still open, which is the normal case while an answer streams. So when you ask the agent about this article and it quotes one of the examples, you get the example and not another Pricewerk card.

Only my projects. projectIdFromHref strips the language prefix, accepts nothing but a project anchor on the homepage and looks the ID up in the portfolio. A link to another website is still a link. The model can also invent a project. It just doesn't get a card for it.

Let React own the details

Once the client has a list of IDs, the rendering step is small. These are the two relevant lines from the message component, shown together:

chat-message.tsx
tsx
const projectIds = getMessageProjectIds(message, portfolioItems);

projectIds.map((projectId) => <ChatProjectCard key={projectId} projectId={projectId} />);

The card looks up the title, role, image and technologies in the portfolio for the current language. If I correct a project description, the card picks up that correction the next time someone opens the site. The model never has to spell out an image URL or assemble a React component, both of which it would attempt with great confidence.

There is a small but useful consequence for saved conversations. The browser can resolve the links again from the stored answer. If a project has since been removed, its old ID no longer matches the catalogue. The original sentence is still there, and the page can render the rest of the conversation.

Where the tool still helps

showProjects still exists, with a smaller job. Sometimes an answer explains how I work without linking a specific project, and a card would still help. Then the agent passes the portfolio ID to the tool:

showProjects input
json
{
  "projectIds": ["pricewerk"]
}

The client accepts the requested IDs once the tool has finished successfully. A tool call and a link can refer to the same project in one answer, so the last step of the resolver puts the explicit selections first and fills the remaining places from the links:

chat-links.ts
ts
return [...new Set([...requestedIds, ...linkedIds])]
  .filter((id) => projects.some((project) => project.id === id))
  .slice(0, CHAT_MAX_PROJECT_CARDS);

The order matters. Filtering before taking the first three ensures an obsolete ID cannot use up a visible slot. The set handles duplicates while preserving the order, so a second mention of Pricewerk still gives you one card.

A tool call also costs a second trip to the model: after the tool has run, the model is called again with the result before it can finish its answer. When I compared the old rules with the new ones on 71 test questions, the average answer went from 1.27 model calls to 1.14. Other prompt rules changed in the same comparison, so the links don't get all the credit.

What I check

InputVisible result
A complete Pricewerk portfolio linkThe Pricewerk card
The same link and a completed tool call for the same IDOne card
A project name in plain text, or a link inside a code sampleNo card
An unfinished tool call alongside a valid project linkThe card from the link

These cases are small enough to exercise with fixed messages. This is one of eight tests, the one for the story at the top: two linked projects, no tool call, two cards. The second assertion sends the message through JSON first, which is what a saved conversation looks like when it comes back.

project-cards.test.ts
ts
test('shows the projects linked in an answer even without a showProjects call', () => {
  const message = answer(
    '[Pricewerk](/#projekt-pricewerk) and [Goose](/en#projekt-aerosys-copilot)',
  );
  assert.deepEqual(getMessageProjectIds(message), ['pricewerk', 'aerosys-copilot']);
  assert.deepEqual(getMessageProjectIds(JSON.parse(JSON.stringify(message))), [
    'pricewerk',
    'aerosys-copilot',
  ]);
});

All eight run in well under a second, without an API key, a network connection or a model that is having a creative day:

Terminal
txt
$ pnpm exec tsx --test tests/chat/project-cards.test.ts
…
ℹ tests 8
ℹ pass 8
ℹ fail 0
ℹ duration_ms 155.017834

What these tests cannot tell me is whether the model picks a project that fits the question. That is a job for evals. You can try the question from the example in the website chat.

An experiment for later

Today the client decides that a link deserves a card. Two young protocols would let the agent ask for it in a standard way. AG-UI defines the events between an agent and a frontend as an open format: a run starts, text streams in, a tool is called, state changes. A2UI goes one step further. The agent describes the interface itself as JSON, using components from a catalogue the client knows, and the client renders them with its own widgets. My React app would still render ChatProjectCard, so the design and the portfolio data stay on my side.

I haven't used either in this chat. The stream I have already does the job, so this would be an experiment rather than a migration. And yes, it would mean the model asks for the card again. The link would stay as the safety net.

In short

  • A card that depends on a tool call is one more thing the model can forget. A link is part of the sentence it is writing anyway.
  • The model chooses the project. The client checks the ID against the portfolio, and React renders the card from my own data.
  • Only complete links outside code count, so the same resolver works while an answer is still streaming.
  • The tool remains for references the text doesn't link. Explicit selections go first, and three cards are the limit.
  • Everything after the model's choice is plain code, covered by eight tests that need no API key.

Series

2 parts published

Building my website agent

  1. 01How my website chat became an agent
  2. 02How my chat turns links into project cardsYou are here

Written by

Phillipp Bertram

Software development and AI from Osnabrück

Ask my agent about this article

The article goes along as context. The answer opens in the chat.