import type * as plugins from './plugins.js'; /** An event's `type`, from its data. */ const eventType = (data: string): unknown => { try { return (JSON.parse(data) as { type?: unknown } | null)?.type; } catch { return undefined; } }; /** * Where a Messages stream ends: `message_stop` ends the answer, and an `error` event, which * Anthropic sends in place of the rest of a failed answer, ends it too (AGL-1130). */ export const anthropicMessagesTerminal: plugins.IModelStreamTerminal = { event: 'message_stop', ends: (data) => (data.includes('"message_stop"') || data.includes('"error"')) && ['message_stop', 'error'].includes(eventType(data) as string), };