|
| 1 | +/** |
| 2 | + * Buyer award selection - a bounded tool-calling loop for picking the best-value bid (see |
| 3 | + * packages/harness-runtime/src/quote.ts's decideBid for the identical pattern this mirrors). |
| 4 | + * Reputation (when a reputation URL is configured) is fetched once and folded into a deterministic |
| 5 | + * value score via compute_value_score; the model proposes through fetch_seller_reputation / |
| 6 | + * compute_value_score / submit_award, and a deterministic fallback (cheapest bid) covers any |
| 7 | + * failure - a bad model call costs a worse pick, never a stuck round. |
| 8 | + */ |
| 9 | +import { |
| 10 | + complete, sha256Hex, llmRuntimeInfo, runToolLoop, BudgetGuard, StepCounter, pickCheapest, |
| 11 | + type Bid, type Want, type LlmUse, type CompleteOpts, |
| 12 | +} from '@pay/agent-runtime' |
| 13 | +import { fetchReputation } from './reputation.js' |
| 14 | +import { fetchSellerReputationTool, computeValueScoreTool, submitAwardTool, type SubmitAwardInput } from './award-tools.js' |
| 15 | + |
| 16 | +type Llm = (opts: CompleteOpts) => Promise<string> |
| 17 | +const selectionGuardrail = 'winner must match collected BID set; fallback is cheapest available bid' |
| 18 | + |
| 19 | +function buyerLlm( |
| 20 | + round: number, |
| 21 | + agent: string, |
| 22 | + status: LlmUse['status'], |
| 23 | + reason: string, |
| 24 | + audit: Pick<LlmUse, 'inputHash' | 'outputHash'> = {}, |
| 25 | +): LlmUse { |
| 26 | + const info = status === 'skipped' ? undefined : llmRuntimeInfo({ maxTokens: 150 }) |
| 27 | + return { |
| 28 | + round, |
| 29 | + agent, |
| 30 | + purpose: 'buyer_award', |
| 31 | + status, |
| 32 | + ...(info ? { provider: info.provider, model: info.model } : {}), |
| 33 | + usedFor: 'buyer_award', |
| 34 | + affectedFunds: false, |
| 35 | + ...audit, |
| 36 | + reason, |
| 37 | + guardrail: selectionGuardrail, |
| 38 | + createdAt: new Date().toISOString(), |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +const errReason = (e: unknown): string => String((e as Error).message ?? e).slice(0, 120) |
| 43 | + |
| 44 | +/** Best-value selection via a bounded tool loop; deterministic cheapest fallback on any failure. */ |
| 45 | +export async function pickWinner( |
| 46 | + want: Want, |
| 47 | + pool: Bid[], |
| 48 | + buyerName: string, |
| 49 | + reputationUrl?: string, |
| 50 | + llm: Llm = complete, |
| 51 | + doFetch: typeof fetch = fetch, |
| 52 | +): Promise<{ winner: Bid; reason?: string; llm: LlmUse }> { |
| 53 | + if (pool.length === 1) { |
| 54 | + return { winner: pool[0], llm: buyerLlm(want.round, buyerName, 'skipped', 'single bid; no model selection needed') } |
| 55 | + } |
| 56 | + |
| 57 | + const reputation = (reputationUrl && (await fetchReputation(reputationUrl, doFetch))) || [] |
| 58 | + |
| 59 | + const system = |
| 60 | + 'You are a buyer choosing the best-value bid for a Solana data service. Call fetch_seller_reputation ' + |
| 61 | + 'once to see track records, then compute_value_score for each bid before deciding - a cheap seller ' + |
| 62 | + 'that fails verification or no-shows is not a bargain. Call submit_award with {"by": "<seller name>", ' + |
| 63 | + '"reason": "<short>"}.' |
| 64 | + const initialPrompt = |
| 65 | + `service=${want.service} arg=${want.arg} budget=${want.budgetSol}\nbids:\n` + |
| 66 | + pool.map((b) => `- ${b.by}: ${b.priceSol} SOL${b.note ? ` (${b.note})` : ''}`).join('\n') |
| 67 | + const inputHash = sha256Hex(`${system}\n${initialPrompt}`) |
| 68 | + |
| 69 | + let finalInput: SubmitAwardInput | undefined |
| 70 | + let outputHash: string | undefined |
| 71 | + try { |
| 72 | + const outcome = await runToolLoop( |
| 73 | + { |
| 74 | + agentId: buyerName, |
| 75 | + system, |
| 76 | + initialPrompt, |
| 77 | + tools: [ |
| 78 | + fetchSellerReputationTool(reputation), |
| 79 | + computeValueScoreTool(want.budgetSol, reputation), |
| 80 | + submitAwardTool, |
| 81 | + ], |
| 82 | + finalToolName: 'submit_award', |
| 83 | + maxRounds: 5, |
| 84 | + // No lamports move during award selection — only escrow deposit does — so the spend cap is |
| 85 | + // never meant to bind here; see the matching note in quote.ts's decideBid. |
| 86 | + budget: new BudgetGuard({ maxToolCalls: 10, maxSpendLamports: Number.MAX_SAFE_INTEGER, maxDurationSecs: 30 }), |
| 87 | + steps: new StepCounter(5), |
| 88 | + maxTokens: 150, |
| 89 | + }, |
| 90 | + llm, |
| 91 | + ) |
| 92 | + finalInput = outcome.finalInput as SubmitAwardInput | undefined |
| 93 | + if (finalInput) outputHash = sha256Hex(JSON.stringify(finalInput)) |
| 94 | + } catch (e) { |
| 95 | + return { |
| 96 | + winner: pickCheapest(pool)!, |
| 97 | + reason: 'cheapest available', |
| 98 | + llm: buyerLlm(want.round, buyerName, 'fallback', `LLM unavailable: ${errReason(e)}`, { inputHash }), |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (!finalInput) { |
| 103 | + return { |
| 104 | + winner: pickCheapest(pool)!, |
| 105 | + reason: 'cheapest available', |
| 106 | + llm: buyerLlm(want.round, buyerName, 'fallback', 'model exhausted rounds without deciding', { inputHash }), |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + const chosen = pool.find((b) => b.by === finalInput!.by) |
| 111 | + if (!chosen) { |
| 112 | + return { |
| 113 | + winner: pickCheapest(pool)!, |
| 114 | + reason: 'cheapest available', |
| 115 | + llm: buyerLlm(want.round, buyerName, 'fallback', 'model returned a seller outside the bid pool', { inputHash, outputHash }), |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + console.error(`[buyer] picked ${chosen.by} (${chosen.priceSol} SOL): ${finalInput.reason ?? ''}`) |
| 120 | + return { |
| 121 | + winner: chosen, |
| 122 | + reason: finalInput.reason, |
| 123 | + llm: buyerLlm(want.round, buyerName, 'used', finalInput.reason ?? 'model selected winner via tool loop', { inputHash, outputHash }), |
| 124 | + } |
| 125 | +} |
0 commit comments