forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructured.dart
More file actions
267 lines (229 loc) · 7.91 KB
/
Copy pathstructured.dart
File metadata and controls
267 lines (229 loc) · 7.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import 'dart:convert';
import 'dart:math';
import 'package:omi/backend/schema/gen/conversation_wire.g.dart' as wire;
// Phase 4.1 — Structured, ActionItem, AppResponse, and Event are kept as deliberate
// adapters, not typedefs:
// - Structured: client-only `id`, getEmoji() behavior (utf8 decode + random pick),
// fromJson that accepts String action items, and toJson that serializes actionItems
// as description strings (generated emits objects).
// - ActionItem: client-only `id`/`deleted` fields absent from GeneratedActionItem.
// - AppResponse: client-only `id` and toJson key 'appId' (generated emits 'app_id').
// - Event: client-only `id`, field name `startsAt` (generated `start`), and fromJson
// epoch-int -> DateTime conversion.
// Section has no client-only fields or behavior, so it stays a typedef over the wire type.
typedef Section = wire.GeneratedSection;
class Structured {
int id = 0;
String title;
String overview;
String emoji;
String category;
List<Section> sections = [];
List<ActionItem> actionItems = [];
List<Event> events = [];
Structured(this.title, this.overview, {this.id = 0, this.emoji = '', this.category = 'other'});
getEmoji() {
try {
if (emoji.isNotEmpty) return utf8.decode(emoji.toString().codeUnits);
return ['🧠', '😎', '🧑💻', '🚀'][Random().nextInt(4)];
} catch (e) {
// return ['🧠', '😎', '🧑💻', '🚀'][Random().nextInt(4)];
return emoji; // should return random?
}
}
static Structured fromJson(Map<String, dynamic> json) {
var structured = Structured(
json['title'] ?? '',
json['overview'] ?? '',
emoji: json['emoji'] ?? '🧠',
category: json['category'] ?? 'other',
);
final sections = json['sections'];
if (sections is List) {
for (final section in sections) {
final Map<String, dynamic>? sectionJson = section is Map<String, dynamic>
? section
: section is Map
? Map<String, dynamic>.from(section)
: null;
if (sectionJson == null) continue;
// Section.fromJson throws a FormatException on a missing or mistyped
// `heading` / `body_markdown`. Skip the bad entry the way the
// actionItems and events loops below do: one malformed section used to
// throw out of here and take the whole conversation decode with it.
try {
structured.sections.add(Section.fromJson(sectionJson));
} on FormatException {
continue;
}
}
}
final aItems = json['actionItems'] ?? json['action_items'];
if (aItems is List) {
for (final item in aItems) {
if (item is String) {
if (item.isEmpty) continue;
structured.actionItems.add(ActionItem(item));
} else if (item is Map<String, dynamic>) {
structured.actionItems.add(ActionItem.fromJson(item));
} else if (item is Map) {
structured.actionItems.add(ActionItem.fromJson(Map<String, dynamic>.from(item)));
}
}
}
final events = json['events'];
if (events is List) {
for (final event in events) {
if (event is Map && event.isEmpty) continue;
if (event is Map<String, dynamic>) {
structured.events.add(Event.fromJson(event));
} else if (event is Map) {
structured.events.add(Event.fromJson(Map<String, dynamic>.from(event)));
}
}
}
return structured;
}
factory Structured.fromGenerated(wire.GeneratedStructured generated) {
var structured = Structured(
generated.title,
generated.overview,
emoji: generated.emoji,
category: generated.category,
);
structured.sections = generated.sections?.toList() ?? [];
structured.actionItems = generated.actionItems?.map(ActionItem.fromGenerated).toList() ?? [];
structured.events = generated.events?.map(Event.fromGenerated).toList() ?? [];
return structured;
}
@override
String toString() {
var str = '';
str += '${getEmoji()} $title\n\n$overview\n\n'; // ($category)
for (var section in sections) {
str += '${section.heading}\n${section.bodyMarkdown}\n\n';
}
if (actionItems.isNotEmpty) {
str += 'Action Items:\n';
for (var item in actionItems) {
str += '- ${item.description}\n';
}
}
if (events.isNotEmpty) {
str += 'Events:\n';
for (var event in events) {
str += '- ${event.title} (${event.startsAt.toLocal()} for ${event.duration} minutes)\n';
}
}
return str.trim();
}
toJson() {
return {
'title': title,
'overview': overview,
'emoji': emoji,
'category': category,
'sections': sections.map((section) => section.toJson()).toList(),
'actionItems': actionItems.map((item) => item.description).toList(),
'events': events.map((event) => event.toJson()).toList(),
};
}
wire.GeneratedStructured toGenerated() {
return wire.GeneratedStructured(
title: title,
overview: overview,
emoji: emoji,
category: category,
sections: sections.toList(),
actionItems: actionItems.map((item) => item.toGenerated()).toList(),
events: events.map((event) => event.toGenerated()).toList(),
);
}
}
class ActionItem {
int id = 0;
String description;
bool completed = false;
bool deleted = false;
ActionItem(this.description, {this.id = 0, this.completed = false, this.deleted = false});
factory ActionItem.fromGenerated(wire.GeneratedActionItem generated) {
return ActionItem(generated.description, completed: generated.completed);
}
static fromJson(Map<String, dynamic> json) {
final generated = wire.GeneratedActionItem.fromJson(json);
return ActionItem(
generated.description,
completed: generated.completed,
deleted: json['deleted'] ?? false,
);
}
wire.GeneratedActionItem toGenerated() {
return wire.GeneratedActionItem(description: description, completed: completed);
}
toJson() => {...toGenerated().toJson(), 'deleted': deleted};
}
class AppResponse {
int id = 0;
String? appId;
String content;
AppResponse(this.content, {this.id = 0, this.appId});
factory AppResponse.fromGenerated(wire.GeneratedAppResult generated) {
return AppResponse(generated.content, appId: generated.appId);
}
wire.GeneratedAppResult toGenerated() {
return wire.GeneratedAppResult(appId: appId, content: content);
}
toJson() => {'appId': appId, 'content': content};
factory AppResponse.fromJson(Map<String, dynamic> json) {
return AppResponse.fromGenerated(wire.GeneratedAppResult.fromJson(json));
}
}
class Event {
int id = 0;
String title;
DateTime startsAt;
int duration;
String description;
bool created = false;
Event(this.title, this.startsAt, this.duration, {this.description = '', this.created = false, this.id = 0});
factory Event.fromGenerated(wire.GeneratedEvent generated) {
return Event(
generated.title,
generated.start,
generated.duration,
description: generated.description,
created: generated.created,
);
}
factory Event.fromJson(Map<String, dynamic> json) {
final rawStart = json['startsAt'] ?? json['start'];
if (rawStart is int) {
return Event(
json['title'] ?? '',
DateTime.fromMillisecondsSinceEpoch(rawStart * 1000).toLocal(),
json['duration'] ?? 30,
description: json['description'] ?? '',
created: json['created'] ?? false,
);
}
return Event.fromGenerated(wire.GeneratedEvent.fromJson(json));
}
wire.GeneratedEvent toGenerated() {
return wire.GeneratedEvent(
title: title,
start: startsAt,
duration: duration,
description: description,
created: created,
);
}
toJson() {
return {
'title': title,
'startsAt': startsAt.toUtc().toIso8601String(),
'duration': duration,
'description': description,
'created': created,
};
}
}