Skip to content

Commit ac27764

Browse files
klopez4212Terp
andcommitted
fix(mobile): normalize linked agent instruction coordinates
Co-authored-by: Terp <5856f6900e8e0a4e0ea1f4f8d2ee33e4753d2119bc122f94ecafc660b46c0511@buzz.block.builderlab.xyz> Signed-off-by: kenny lopez <[email protected]>
1 parent 44f62d5 commit ac27764

2 files changed

Lines changed: 75 additions & 31 deletions

File tree

‎mobile/lib/features/agents/agent_instructions.dart‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ Future<String?> loadAgentInstructions(
118118
final registration = matching.first;
119119
final data = jsonDecode(registration.content);
120120
final personaId = data is Map ? data['persona_id'] : null;
121-
if (personaId is String &&
122-
RegExp(r'^[a-z0-9][a-z0-9_-]{0,63}$').hasMatch(personaId)) {
121+
if (personaId is String) {
122+
final slug = _personaDefinitionSlug(personaId);
123123
final definitions = await query(
124124
NostrFilter(
125125
kinds: const [30175],
126126
authors: [owner],
127127
tags: {
128-
'#d': [personaId],
128+
'#d': [slug],
129129
},
130130
),
131131
);
@@ -135,7 +135,7 @@ Future<String?> loadAgentInstructions(
135135
(event) =>
136136
event.kind == 30175 &&
137137
event.pubkey == owner &&
138-
event.getTagValue('d') == personaId,
138+
event.getTagValue('d') == slug,
139139
)
140140
.toList()
141141
..sort(
@@ -156,6 +156,28 @@ Future<String?> loadAgentInstructions(
156156
: null;
157157
}
158158

159+
// Keep in sync with desktop managed_agents/persona_events.rs normalize_d_tag.
160+
// Iterate Unicode scalars so non-ASCII characters each become one hyphen,
161+
// matching Rust's chars(), and lowercase only ASCII before slug validation.
162+
String _personaDefinitionSlug(String raw) {
163+
final slug = StringBuffer();
164+
for (var rune in raw.runes) {
165+
if (rune >= 65 && rune <= 90) rune += 32;
166+
final allowed =
167+
(rune >= 97 && rune <= 122) ||
168+
(rune >= 48 && rune <= 57) ||
169+
rune == 95 ||
170+
rune == 45;
171+
final character = allowed ? String.fromCharCode(rune) : '-';
172+
if (slug.isEmpty && (character == '_' || character == '-')) {
173+
slug.write('a');
174+
}
175+
slug.write(character);
176+
if (slug.length == 64) break;
177+
}
178+
return slug.isEmpty ? 'a' : slug.toString();
179+
}
180+
159181
/// Read-only instructions, with explicit unavailable and retry states.
160182
class AgentInstructions extends ConsumerWidget {
161183
const AgentInstructions({required this.pubkey, super.key});

‎mobile/test/features/agents/agent_instructions_test.dart‎

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,33 +126,55 @@ void main() {
126126
);
127127
expect(result, row.expected);
128128
}
129-
final linked = event(
130-
30177,
131-
[
132-
['d', agent],
133-
],
134-
{'system_prompt': 'Stale', 'persona_id': 'helper'},
135-
);
136-
expect(
137-
await loadAgentInstructions(
138-
agent,
139-
owner,
140-
(filter) async => switch (filter.kinds.single) {
141-
30177 => [linked],
142-
30175 => [
143-
event(
144-
30175,
145-
[
146-
['d', 'helper'],
147-
],
148-
{'system_prompt': 'Definition instructions'},
149-
),
150-
],
151-
_ => [],
152-
},
153-
),
154-
'Definition instructions',
155-
);
129+
// Coordinates match desktop persona_events::normalize_d_tag, including
130+
// built-in IDs and the mixed-case/leading punctuation of team-pack slugs.
131+
for (final (personaId, slug) in [
132+
('helper', 'helper'),
133+
('builtin:fizz', 'builtin-fizz'),
134+
('builtin:honey', 'builtin-honey'),
135+
('builtin:bumble', 'builtin-bumble'),
136+
('CodeReviewer', 'codereviewer'),
137+
('_Ops', 'a_ops'),
138+
('-Ops', 'a-ops'),
139+
('É😀Ops', 'a--ops'),
140+
('', 'a'),
141+
('A' * 65, 'a' * 64),
142+
]) {
143+
final linked = event(
144+
30177,
145+
[
146+
['d', agent],
147+
],
148+
{'system_prompt': 'Stale', 'persona_id': personaId},
149+
);
150+
for (final available in [true, false]) {
151+
expect(
152+
await loadAgentInstructions(
153+
agent,
154+
owner,
155+
(filter) async => switch (filter.kinds.single) {
156+
30177 => [linked],
157+
30175
158+
when available &&
159+
filter.authors?.single == owner &&
160+
filter.tags['#d']?.single == slug =>
161+
[
162+
event(
163+
30175,
164+
[
165+
['d', slug],
166+
],
167+
{'system_prompt': 'Definition instructions'},
168+
),
169+
],
170+
_ => [],
171+
},
172+
),
173+
available ? 'Definition instructions' : null,
174+
reason: 'persona_id: $personaId, definition available: $available',
175+
);
176+
}
177+
}
156178
await expectLater(
157179
loadAgentInstructions(
158180
agent,

0 commit comments

Comments
 (0)