|
| 1 | +import { a2aPeerId } from '../core/a2a-receipt.js'; |
| 2 | +import { Fault } from '../core/util.js'; |
| 3 | +import { bounded, record } from './shared.js'; |
| 4 | + |
| 5 | +type Json = Record<string, unknown>; |
| 6 | + |
| 7 | +export const invalidA2AReply = (reason: string) => new Fault('invalid_protocol', reason, 502); |
| 8 | + |
| 9 | +export function matchingA2AReply(raw: unknown, requestId: string): Json { |
| 10 | + const reply = record(raw); |
| 11 | + if (reply.jsonrpc !== '2.0' || reply.id !== requestId) |
| 12 | + throw invalidA2AReply('A2A reply did not match the original JSON-RPC request.'); |
| 13 | + if (Object.hasOwn(reply, 'error') === Object.hasOwn(reply, 'result')) |
| 14 | + throw invalidA2AReply('A2A reply must contain either a result or an error.'); |
| 15 | + return reply; |
| 16 | +} |
| 17 | + |
| 18 | +export function exactA2APeerId(value: unknown): string | undefined { |
| 19 | + if (value === undefined) return undefined; |
| 20 | + const id = a2aPeerId.safeParse(value); |
| 21 | + if (!id.success) throw invalidA2AReply('A2A task or context identifier is invalid.'); |
| 22 | + return id.data; |
| 23 | +} |
| 24 | + |
| 25 | +/** `TASK_STATE_INPUT_REQUIRED` (1.0) and `input-required` (0.3) both become `input-required`. */ |
| 26 | +export const stateName = (value: unknown): string | null => { |
| 27 | + const raw = bounded(value, 60); |
| 28 | + if (!raw) return null; |
| 29 | + return raw.startsWith('TASK_STATE_') |
| 30 | + ? raw.slice('TASK_STATE_'.length).toLowerCase().replaceAll('_', '-') |
| 31 | + : raw.toLowerCase(); |
| 32 | +}; |
| 33 | + |
| 34 | +/** 1.0 text parts have no discriminator; 0.3 parts are tagged `kind: "text"`. */ |
| 35 | +export function partsText(parts: unknown): string { |
| 36 | + if (!Array.isArray(parts)) return ''; |
| 37 | + return parts |
| 38 | + .map((part) => { |
| 39 | + const p = record(part); |
| 40 | + const tag = p.kind ?? p.type; |
| 41 | + return (tag === undefined || tag === 'text') && typeof p.text === 'string' ? p.text : ''; |
| 42 | + }) |
| 43 | + .join(''); |
| 44 | +} |
| 45 | + |
| 46 | +type A2AEvent = |
| 47 | + | { type: 'message'; value: Json } |
| 48 | + | { type: 'task'; value: Json } |
| 49 | + | { type: 'status'; value: Json; final: boolean } |
| 50 | + | { type: 'artifact'; value: Json }; |
| 51 | +const wrappedEvents = ['statusUpdate', 'artifactUpdate', 'task', 'message'] as const; |
| 52 | + |
| 53 | +/** A 1.0 result names its only payload member; a 0.3 result has a `kind` discriminator. */ |
| 54 | +export function eventOf(result: Json): A2AEvent | undefined { |
| 55 | + const wrapped = wrappedEvents.filter((key) => Object.hasOwn(result, key)); |
| 56 | + if (wrapped.length > 1) return undefined; |
| 57 | + const [tag] = wrapped; |
| 58 | + if (tag === 'statusUpdate') return { type: 'status', value: record(result[tag]), final: false }; |
| 59 | + if (tag === 'artifactUpdate') return { type: 'artifact', value: record(result[tag]) }; |
| 60 | + if (tag) return { type: tag, value: record(result[tag]) }; |
| 61 | + if (result.kind === 'status-update') |
| 62 | + return { type: 'status', value: result, final: result.final === true }; |
| 63 | + if (result.kind === 'artifact-update') return { type: 'artifact', value: result }; |
| 64 | + if (result.kind === 'task' || result.kind === 'message') |
| 65 | + return { type: result.kind, value: result }; |
| 66 | + if (typeof result.id === 'string' && result.status) return { type: 'task', value: result }; |
| 67 | + return undefined; |
| 68 | +} |
0 commit comments