1- import 'dart:async' ;
21import 'dart:convert' ;
3- import 'dart:typed_data' ;
42
53import 'package:hooks_riverpod/hooks_riverpod.dart' ;
64import 'package:http/http.dart' as http;
75
86import '../../shared/deeplink/deep_link.dart' ;
97import '../../shared/hosted/hosted_environment.dart' ;
8+ import '../../shared/utils/bounded_fetch.dart' ;
109import 'agent_avatar_picker.dart' ;
1110
1211/// Creates the HTTP client that fetches a card's `agent.json` ; overridden in
@@ -16,9 +15,7 @@ final agentCardClientProvider = Provider<http.Client Function()>(
1615);
1716
1817/// Largest `agent.json` accepted, far above any real card.
19- const maxAgentCardBytes = 256 * 1024 ;
20-
21- const _fetchTimeout = Duration (seconds: 15 );
18+ const maxAgentCardBytes = 2 * 1024 * 1024 ;
2219
2320/// Why a card's draft could not be loaded, worded for the user.
2421class AgentCardException implements Exception {
@@ -61,76 +58,64 @@ class AgentCreationDraft {
6158 http.Client client,
6259 ) async {
6360 final cdn = HostedEnvironment .avatarsCdnOrigin;
64- final url = cdn. replace (path : '/${ link . cardId }/agent.json' );
65- final http. StreamedResponse response ;
66- final List < int > body ;
61+ // The id is opaque; pathSegments percent-encodes anything path-unsafe.
62+ final url = cdn. replace (pathSegments : [link.cardId, 'agent.json' ]) ;
63+ final BoundedResponse response ;
6764 try {
68- response = await client
69- .send (
70- http.Request ('GET' , url)
71- ..headers['accept' ] = 'application/json'
72- // A redirect could lead off the CDN; treat it as unavailable.
73- ..followRedirects = false ,
74- )
75- .timeout (_fetchTimeout);
76- if (response.statusCode == 403 || response.statusCode == 404 ) {
77- throw const AgentCardException ('This agent card isn’t available.' );
78- }
79- if (response.statusCode != 200 ) {
80- throw AgentCardException (
81- 'Couldn’t load this agent card (HTTP ${response .statusCode }).' ,
82- );
83- }
84- if ((response.contentLength ?? 0 ) > maxAgentCardBytes) {
85- throw const AgentCardException ('This agent card is too large.' );
86- }
87- body = await _readBounded (response.stream).timeout (_fetchTimeout);
88- } on AgentCardException {
89- rethrow ;
65+ response = await boundedGet (
66+ client,
67+ url,
68+ maxBytes: maxAgentCardBytes,
69+ headers: const {'accept' : 'application/json' },
70+ // A redirect could lead off the CDN; treat it as unavailable.
71+ followRedirects: false ,
72+ );
73+ } on ResponseTooLargeException {
74+ throw const AgentCardException ('This agent card is too large.' );
9075 } on Exception {
9176 throw const AgentCardException (
9277 'Couldn’t load this agent card. Check your connection and reopen the '
9378 'link.' ,
9479 );
9580 }
81+ if (response.statusCode == 403 || response.statusCode == 404 ) {
82+ throw const AgentCardException ('This agent card isn’t available.' );
83+ }
84+ if (response.statusCode != 200 ) {
85+ throw AgentCardException (
86+ 'Couldn’t load this agent card (HTTP ${response .statusCode }).' ,
87+ );
88+ }
9689 final Object ? json;
9790 try {
98- json = jsonDecode (utf8.decode (body ));
91+ json = jsonDecode (utf8.decode (response.bytes ));
9992 } on FormatException {
10093 throw const AgentCardException ('This agent card is invalid.' );
10194 }
10295 if (json is ! Map <String , dynamic >) {
10396 throw const AgentCardException ('This agent card is invalid.' );
10497 }
105- return parseAgentCard (json, link.cardId, cdn );
98+ return parseAgentCard (json, link.cardId);
10699 }
107100
108- /// Reads a card's draft from its `agent.json` , validated against the card
109- /// schema (v1 or v2). `v` , `id` (matching [cardId] ), `name` and `avatarUrl`
110- /// are required; `instructions` , `model` and `reasoningEffort` are optional
111- /// and left for the user when absent. A required field that is missing, or
112- /// any known field of the wrong type, makes the card invalid. Other keys are
113- /// ignored. The avatar must be an https URL on [cdn] , the card's own host.
101+ /// Reads a card's draft from its `agent.json` . `id` (matching [cardId] ),
102+ /// `name` and `avatarUrl` (an https URL) are required; `instructions` ,
103+ /// `model` and `reasoningEffort` are optional and left for the user when
104+ /// absent or blank. A missing required field, or a known field of the wrong
105+ /// type, makes the card invalid. Other keys, including `v` , are ignored.
114106 static AgentCreationDraft parseAgentCard (
115107 Map <String , dynamic > json,
116108 String cardId,
117- Uri cdn,
118109 ) {
119110 const invalid = AgentCardException ('This agent card is invalid.' );
120- final version = json['v' ];
121- if (version is ! int || (version != 1 && version != 2 )) throw invalid;
122111 if (json['id' ] != cardId) throw invalid;
123112 final name = json['name' ];
124113 final avatarUrl = json['avatarUrl' ];
125114 if (name is ! String || name.trim ().isEmpty || avatarUrl is ! String ) {
126115 throw invalid;
127116 }
128117 final avatar = Uri .tryParse (avatarUrl);
129- if (avatar == null ||
130- avatar.scheme != 'https' ||
131- avatar.host != cdn.host ||
132- avatar.hasPort ||
133- avatar.userInfo.isNotEmpty) {
118+ if (avatar == null || avatar.scheme != 'https' || avatar.host.isEmpty) {
134119 throw invalid;
135120 }
136121 String ? optional (String key) {
@@ -140,26 +125,12 @@ class AgentCreationDraft {
140125 return text == null || text.isEmpty ? null : text;
141126 }
142127
143- final modelId = optional ('model' );
144- final reasoningEffort = optional ('reasoningEffort' );
145- if (reasoningEffort != null && modelId == null ) throw invalid;
146128 return AgentCreationDraft (
147129 name: name.trim (),
148130 instructions: optional ('instructions' ) ?? '' ,
149131 avatar: AgentAvatarSelection .url (avatarUrl),
150- modelId: modelId ,
151- reasoningEffort: reasoningEffort,
132+ modelId: optional ( 'model' ) ,
133+ reasoningEffort: optional ( ' reasoningEffort' ) ,
152134 );
153135 }
154-
155- static Future <List <int >> _readBounded (Stream <List <int >> stream) async {
156- final bytes = BytesBuilder (copy: false );
157- await for (final chunk in stream) {
158- if (bytes.length + chunk.length > maxAgentCardBytes) {
159- throw const AgentCardException ('This agent card is too large.' );
160- }
161- bytes.add (chunk);
162- }
163- return bytes.takeBytes ();
164- }
165136}
0 commit comments