From 3efed6e9d2dc9920db0a8f194b288e27adfd6a33 Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Sat, 6 Dec 2025 13:47:00 +0100 Subject: [PATCH] Fix service reconnection: return available offer from pool MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Modified /services/:uuid endpoint to return an available (unanswered) offer from the service's offer pool instead of always returning the initial offer. This fixes reconnection failures where clients would try to answer already-consumed offers. Changes: - Query all offers from the service's peer ID - Return first unanswered offer - Return 503 if no offers available Fixes: "Offer already answered" errors on reconnection attempts 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 --- src/app.ts | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/app.ts b/src/app.ts index b2c881c..6023ecd 100644 --- a/src/app.ts +++ b/src/app.ts @@ -291,6 +291,7 @@ export function createApp(storage: Storage, config: Config) { /** * GET /services/:uuid * Get service details by index UUID + * Returns an available (unanswered) offer from the service's pool */ app.get('/services/:uuid', async (c) => { try { @@ -302,19 +303,32 @@ export function createApp(storage: Storage, config: Config) { return c.json({ error: 'Service not found' }, 404); } - // Get associated offer - const offer = await storage.getOfferById(service.offerId); + // Get the initial offer to find the peer ID + const initialOffer = await storage.getOfferById(service.offerId); - if (!offer) { + if (!initialOffer) { return c.json({ error: 'Associated offer not found' }, 404); } + // Get all offers from this peer + const peerOffers = await storage.getOffersByPeerId(initialOffer.peerId); + + // Find an unanswered offer + const availableOffer = peerOffers.find(offer => !offer.answererPeerId); + + if (!availableOffer) { + return c.json({ + error: 'No available offers', + message: 'All offers from this service are currently in use. Please try again later.' + }, 503); + } + return c.json({ serviceId: service.id, username: service.username, serviceFqn: service.serviceFqn, - offerId: service.offerId, - sdp: offer.sdp, + offerId: availableOffer.id, + sdp: availableOffer.sdp, isPublic: service.isPublic, metadata: service.metadata ? JSON.parse(service.metadata) : undefined, createdAt: service.createdAt,