Skip to content

Commit e342ecf

Browse files
authored
Add attribute typings for agent and transcription (#811)
Introduces a new file `attribute-typings.dart` defining Dart classes and enums for agent and transcription attributes, including JSON serialization helpers. Also exports this new typings file from the main library entry point.
1 parent 51b796c commit e342ecf

File tree

2 files changed

+111
-2
lines changed

2 files changed

+111
-2
lines changed

lib/livekit_client.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ export 'src/track/local/local.dart';
4444
export 'src/track/local/video.dart';
4545
export 'src/track/options.dart';
4646
export 'src/track/processor.dart';
47-
export 'src/track/processor_native.dart'
48-
if (dart.library.js_interop) 'src/track/processor_web.dart';
47+
export 'src/track/processor_native.dart' if (dart.library.js_interop) 'src/track/processor_web.dart';
4948
export 'src/track/remote/audio.dart';
5049
export 'src/track/remote/remote.dart';
5150
export 'src/track/remote/video.dart';
@@ -60,3 +59,4 @@ export 'src/types/video_encoding.dart';
6059
export 'src/types/video_parameters.dart';
6160
export 'src/widgets/screen_select_dialog.dart';
6261
export 'src/widgets/video_track_renderer.dart';
62+
export 'src/types/attribute-typings.dart';
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// To parse this JSON data, do
2+
//
3+
// final agentAttributes = agentAttributesFromJson(jsonString);
4+
// final transcriptionAttributes = transcriptionAttributesFromJson(jsonString);
5+
6+
import 'dart:convert';
7+
8+
AgentAttributes agentAttributesFromJson(String str) => AgentAttributes.fromJson(json.decode(str));
9+
10+
String agentAttributesToJson(AgentAttributes data) => json.encode(data.toJson());
11+
12+
TranscriptionAttributes transcriptionAttributesFromJson(String str) =>
13+
TranscriptionAttributes.fromJson(json.decode(str));
14+
15+
String transcriptionAttributesToJson(TranscriptionAttributes data) => json.encode(data.toJson());
16+
17+
class AgentAttributes {
18+
List<AgentInput>? lkAgentInputs;
19+
List<AgentOutput>? lkAgentOutputs;
20+
AgentState? lkAgentState;
21+
String? lkPublishOnBehalf;
22+
23+
AgentAttributes({
24+
this.lkAgentInputs,
25+
this.lkAgentOutputs,
26+
this.lkAgentState,
27+
this.lkPublishOnBehalf,
28+
});
29+
30+
factory AgentAttributes.fromJson(Map<String, dynamic> json) => AgentAttributes(
31+
lkAgentInputs: json['lk.agent.inputs'] == null
32+
? []
33+
: List<AgentInput>.from(json['lk.agent.inputs']!.map((x) => agentInputValues.map[x]!)),
34+
lkAgentOutputs: json['lk.agent.outputs'] == null
35+
? []
36+
: List<AgentOutput>.from(json['lk.agent.outputs']!.map((x) => agentOutputValues.map[x]!)),
37+
lkAgentState: agentStateValues.map[json['lk.agent.state']]!,
38+
lkPublishOnBehalf: json['lk.publish_on_behalf'],
39+
);
40+
41+
Map<String, dynamic> toJson() => {
42+
'lk.agent.inputs':
43+
lkAgentInputs == null ? [] : List<dynamic>.from(lkAgentInputs!.map((x) => agentInputValues.reverse[x])),
44+
'lk.agent.outputs':
45+
lkAgentOutputs == null ? [] : List<dynamic>.from(lkAgentOutputs!.map((x) => agentOutputValues.reverse[x])),
46+
'lk.agent.state': agentStateValues.reverse[lkAgentState],
47+
'lk.publish_on_behalf': lkPublishOnBehalf,
48+
};
49+
}
50+
51+
enum AgentInput { AUDIO, TEXT, VIDEO }
52+
53+
final agentInputValues = EnumValues({'audio': AgentInput.AUDIO, 'text': AgentInput.TEXT, 'video': AgentInput.VIDEO});
54+
55+
enum AgentOutput { AUDIO, TRANSCRIPTION }
56+
57+
final agentOutputValues = EnumValues({'audio': AgentOutput.AUDIO, 'transcription': AgentOutput.TRANSCRIPTION});
58+
59+
enum AgentState { IDLE, INITIALIZING, LISTENING, SPEAKING, THINKING }
60+
61+
final agentStateValues = EnumValues({
62+
'idle': AgentState.IDLE,
63+
'initializing': AgentState.INITIALIZING,
64+
'listening': AgentState.LISTENING,
65+
'speaking': AgentState.SPEAKING,
66+
'thinking': AgentState.THINKING
67+
});
68+
69+
///Schema for transcription-related attributes
70+
class TranscriptionAttributes {
71+
///The segment id of the transcription
72+
String? lkSegmentId;
73+
74+
///The associated track id of the transcription
75+
String? lkTranscribedTrackId;
76+
77+
///Whether the transcription is final
78+
bool? lkTranscriptionFinal;
79+
80+
TranscriptionAttributes({
81+
this.lkSegmentId,
82+
this.lkTranscribedTrackId,
83+
this.lkTranscriptionFinal,
84+
});
85+
86+
factory TranscriptionAttributes.fromJson(Map<String, dynamic> json) => TranscriptionAttributes(
87+
lkSegmentId: json['lk.segment_id'],
88+
lkTranscribedTrackId: json['lk.transcribed_track_id'],
89+
lkTranscriptionFinal: json['lk.transcription_final'],
90+
);
91+
92+
Map<String, dynamic> toJson() => {
93+
'lk.segment_id': lkSegmentId,
94+
'lk.transcribed_track_id': lkTranscribedTrackId,
95+
'lk.transcription_final': lkTranscriptionFinal,
96+
};
97+
}
98+
99+
class EnumValues<T> {
100+
Map<String, T> map;
101+
late Map<T, String> reverseMap;
102+
103+
EnumValues(this.map);
104+
105+
Map<T, String> get reverse {
106+
reverseMap = map.map((k, v) => MapEntry(v, k));
107+
return reverseMap;
108+
}
109+
}

0 commit comments

Comments
 (0)