forked from github/copilot-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_events.py
More file actions
2867 lines (2222 loc) · 117 KB
/
session_events.py
File metadata and controls
2867 lines (2222 loc) · 117 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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
AUTO-GENERATED FILE - DO NOT EDIT
Generated from: session-events.schema.json
"""
from enum import Enum
from dataclasses import dataclass
from typing import Any, TypeVar, cast
from collections.abc import Callable
from datetime import datetime
from uuid import UUID
import dateutil.parser
T = TypeVar("T")
EnumT = TypeVar("EnumT", bound=Enum)
def from_float(x: Any) -> float:
assert isinstance(x, (float, int)) and not isinstance(x, bool)
return float(x)
def to_float(x: Any) -> float:
assert isinstance(x, (int, float))
return x
def to_class(c: type[T], x: Any) -> dict:
assert isinstance(x, c)
return cast(Any, x).to_dict()
def from_str(x: Any) -> str:
assert isinstance(x, str)
return x
def from_none(x: Any) -> Any:
assert x is None
return x
def from_union(fs, x):
for f in fs:
try:
return f(x)
except Exception:
pass
assert False
def to_enum(c: type[EnumT], x: Any) -> EnumT:
assert isinstance(x, c)
return x.value
def from_list(f: Callable[[Any], T], x: Any) -> list[T]:
assert isinstance(x, list)
return [f(y) for y in x]
def from_dict(f: Callable[[Any], T], x: Any) -> dict[str, T]:
assert isinstance(x, dict)
return { k: f(v) for (k, v) in x.items() }
def from_bool(x: Any) -> bool:
assert isinstance(x, bool)
return x
def from_datetime(x: Any) -> datetime:
return dateutil.parser.parse(x)
def from_int(x: Any) -> int:
assert isinstance(x, int) and not isinstance(x, bool)
return x
class AgentMode(Enum):
"""The agent mode that was active when this message was sent"""
AUTOPILOT = "autopilot"
INTERACTIVE = "interactive"
PLAN = "plan"
SHELL = "shell"
@dataclass
class LineRange:
"""Optional line range to scope the attachment to a specific section of the file"""
end: float
"""End line number (1-based, inclusive)"""
start: float
"""Start line number (1-based)"""
@staticmethod
def from_dict(obj: Any) -> 'LineRange':
assert isinstance(obj, dict)
end = from_float(obj.get("end"))
start = from_float(obj.get("start"))
return LineRange(end, start)
def to_dict(self) -> dict:
result: dict = {}
result["end"] = to_float(self.end)
result["start"] = to_float(self.start)
return result
class ReferenceType(Enum):
"""Type of GitHub reference"""
DISCUSSION = "discussion"
ISSUE = "issue"
PR = "pr"
@dataclass
class End:
"""End position of the selection"""
character: float
"""End character offset within the line (0-based)"""
line: float
"""End line number (0-based)"""
@staticmethod
def from_dict(obj: Any) -> 'End':
assert isinstance(obj, dict)
character = from_float(obj.get("character"))
line = from_float(obj.get("line"))
return End(character, line)
def to_dict(self) -> dict:
result: dict = {}
result["character"] = to_float(self.character)
result["line"] = to_float(self.line)
return result
@dataclass
class Start:
"""Start position of the selection"""
character: float
"""Start character offset within the line (0-based)"""
line: float
"""Start line number (0-based)"""
@staticmethod
def from_dict(obj: Any) -> 'Start':
assert isinstance(obj, dict)
character = from_float(obj.get("character"))
line = from_float(obj.get("line"))
return Start(character, line)
def to_dict(self) -> dict:
result: dict = {}
result["character"] = to_float(self.character)
result["line"] = to_float(self.line)
return result
@dataclass
class Selection:
"""Position range of the selection within the file"""
end: End
"""End position of the selection"""
start: Start
"""Start position of the selection"""
@staticmethod
def from_dict(obj: Any) -> 'Selection':
assert isinstance(obj, dict)
end = End.from_dict(obj.get("end"))
start = Start.from_dict(obj.get("start"))
return Selection(end, start)
def to_dict(self) -> dict:
result: dict = {}
result["end"] = to_class(End, self.end)
result["start"] = to_class(Start, self.start)
return result
class AttachmentType(Enum):
BLOB = "blob"
DIRECTORY = "directory"
FILE = "file"
GITHUB_REFERENCE = "github_reference"
SELECTION = "selection"
@dataclass
class Attachment:
"""A user message attachment — a file, directory, code selection, blob, or GitHub reference
File attachment
Directory attachment
Code selection attachment from an editor
GitHub issue, pull request, or discussion reference
Blob attachment with inline base64-encoded data
"""
type: AttachmentType
"""Attachment type discriminator"""
display_name: str | None = None
"""User-facing display name for the attachment
User-facing display name for the selection
"""
line_range: LineRange | None = None
"""Optional line range to scope the attachment to a specific section of the file"""
path: str | None = None
"""Absolute file path
Absolute directory path
"""
file_path: str | None = None
"""Absolute path to the file containing the selection"""
selection: Selection | None = None
"""Position range of the selection within the file"""
text: str | None = None
"""The selected text content"""
number: float | None = None
"""Issue, pull request, or discussion number"""
reference_type: ReferenceType | None = None
"""Type of GitHub reference"""
state: str | None = None
"""Current state of the referenced item (e.g., open, closed, merged)"""
title: str | None = None
"""Title of the referenced item"""
url: str | None = None
"""URL to the referenced item on GitHub"""
data: str | None = None
"""Base64-encoded content"""
mime_type: str | None = None
"""MIME type of the inline data"""
@staticmethod
def from_dict(obj: Any) -> 'Attachment':
assert isinstance(obj, dict)
type = AttachmentType(obj.get("type"))
display_name = from_union([from_str, from_none], obj.get("displayName"))
line_range = from_union([LineRange.from_dict, from_none], obj.get("lineRange"))
path = from_union([from_str, from_none], obj.get("path"))
file_path = from_union([from_str, from_none], obj.get("filePath"))
selection = from_union([Selection.from_dict, from_none], obj.get("selection"))
text = from_union([from_str, from_none], obj.get("text"))
number = from_union([from_float, from_none], obj.get("number"))
reference_type = from_union([ReferenceType, from_none], obj.get("referenceType"))
state = from_union([from_str, from_none], obj.get("state"))
title = from_union([from_str, from_none], obj.get("title"))
url = from_union([from_str, from_none], obj.get("url"))
data = from_union([from_str, from_none], obj.get("data"))
mime_type = from_union([from_str, from_none], obj.get("mimeType"))
return Attachment(type, display_name, line_range, path, file_path, selection, text, number, reference_type, state, title, url, data, mime_type)
def to_dict(self) -> dict:
result: dict = {}
result["type"] = to_enum(AttachmentType, self.type)
if self.display_name is not None:
result["displayName"] = from_union([from_str, from_none], self.display_name)
if self.line_range is not None:
result["lineRange"] = from_union([lambda x: to_class(LineRange, x), from_none], self.line_range)
if self.path is not None:
result["path"] = from_union([from_str, from_none], self.path)
if self.file_path is not None:
result["filePath"] = from_union([from_str, from_none], self.file_path)
if self.selection is not None:
result["selection"] = from_union([lambda x: to_class(Selection, x), from_none], self.selection)
if self.text is not None:
result["text"] = from_union([from_str, from_none], self.text)
if self.number is not None:
result["number"] = from_union([to_float, from_none], self.number)
if self.reference_type is not None:
result["referenceType"] = from_union([lambda x: to_enum(ReferenceType, x), from_none], self.reference_type)
if self.state is not None:
result["state"] = from_union([from_str, from_none], self.state)
if self.title is not None:
result["title"] = from_union([from_str, from_none], self.title)
if self.url is not None:
result["url"] = from_union([from_str, from_none], self.url)
if self.data is not None:
result["data"] = from_union([from_str, from_none], self.data)
if self.mime_type is not None:
result["mimeType"] = from_union([from_str, from_none], self.mime_type)
return result
@dataclass
class Agent:
"""A background agent task"""
agent_id: str
"""Unique identifier of the background agent"""
agent_type: str
"""Type of the background agent"""
description: str | None = None
"""Human-readable description of the agent task"""
@staticmethod
def from_dict(obj: Any) -> 'Agent':
assert isinstance(obj, dict)
agent_id = from_str(obj.get("agentId"))
agent_type = from_str(obj.get("agentType"))
description = from_union([from_str, from_none], obj.get("description"))
return Agent(agent_id, agent_type, description)
def to_dict(self) -> dict:
result: dict = {}
result["agentId"] = from_str(self.agent_id)
result["agentType"] = from_str(self.agent_type)
if self.description is not None:
result["description"] = from_union([from_str, from_none], self.description)
return result
@dataclass
class Shell:
"""A background shell command"""
shell_id: str
"""Unique identifier of the background shell"""
description: str | None = None
"""Human-readable description of the shell command"""
@staticmethod
def from_dict(obj: Any) -> 'Shell':
assert isinstance(obj, dict)
shell_id = from_str(obj.get("shellId"))
description = from_union([from_str, from_none], obj.get("description"))
return Shell(shell_id, description)
def to_dict(self) -> dict:
result: dict = {}
result["shellId"] = from_str(self.shell_id)
if self.description is not None:
result["description"] = from_union([from_str, from_none], self.description)
return result
@dataclass
class BackgroundTasks:
"""Background tasks still running when the agent became idle"""
agents: list[Agent]
"""Currently running background agents"""
shells: list[Shell]
"""Currently running background shell commands"""
@staticmethod
def from_dict(obj: Any) -> 'BackgroundTasks':
assert isinstance(obj, dict)
agents = from_list(Agent.from_dict, obj.get("agents"))
shells = from_list(Shell.from_dict, obj.get("shells"))
return BackgroundTasks(agents, shells)
def to_dict(self) -> dict:
result: dict = {}
result["agents"] = from_list(lambda x: to_class(Agent, x), self.agents)
result["shells"] = from_list(lambda x: to_class(Shell, x), self.shells)
return result
@dataclass
class CodeChanges:
"""Aggregate code change metrics for the session"""
files_modified: list[str]
"""List of file paths that were modified during the session"""
lines_added: float
"""Total number of lines added during the session"""
lines_removed: float
"""Total number of lines removed during the session"""
@staticmethod
def from_dict(obj: Any) -> 'CodeChanges':
assert isinstance(obj, dict)
files_modified = from_list(from_str, obj.get("filesModified"))
lines_added = from_float(obj.get("linesAdded"))
lines_removed = from_float(obj.get("linesRemoved"))
return CodeChanges(files_modified, lines_added, lines_removed)
def to_dict(self) -> dict:
result: dict = {}
result["filesModified"] = from_list(from_str, self.files_modified)
result["linesAdded"] = to_float(self.lines_added)
result["linesRemoved"] = to_float(self.lines_removed)
return result
@dataclass
class CompactionTokensUsed:
"""Token usage breakdown for the compaction LLM call"""
cached_input: float
"""Cached input tokens reused in the compaction LLM call"""
input: float
"""Input tokens consumed by the compaction LLM call"""
output: float
"""Output tokens produced by the compaction LLM call"""
@staticmethod
def from_dict(obj: Any) -> 'CompactionTokensUsed':
assert isinstance(obj, dict)
cached_input = from_float(obj.get("cachedInput"))
input = from_float(obj.get("input"))
output = from_float(obj.get("output"))
return CompactionTokensUsed(cached_input, input, output)
def to_dict(self) -> dict:
result: dict = {}
result["cachedInput"] = to_float(self.cached_input)
result["input"] = to_float(self.input)
result["output"] = to_float(self.output)
return result
class HostType(Enum):
"""Hosting platform type of the repository (github or ado)"""
ADO = "ado"
GITHUB = "github"
@dataclass
class ContextClass:
"""Working directory and git context at session start
Updated working directory and git context at resume time
"""
cwd: str
"""Current working directory path"""
base_commit: str | None = None
"""Base commit of current git branch at session start time"""
branch: str | None = None
"""Current git branch name"""
git_root: str | None = None
"""Root directory of the git repository, resolved via git rev-parse"""
head_commit: str | None = None
"""Head commit of current git branch at session start time"""
host_type: HostType | None = None
"""Hosting platform type of the repository (github or ado)"""
repository: str | None = None
"""Repository identifier derived from the git remote URL ("owner/name" for GitHub,
"org/project/repo" for Azure DevOps)
"""
@staticmethod
def from_dict(obj: Any) -> 'ContextClass':
assert isinstance(obj, dict)
cwd = from_str(obj.get("cwd"))
base_commit = from_union([from_str, from_none], obj.get("baseCommit"))
branch = from_union([from_str, from_none], obj.get("branch"))
git_root = from_union([from_str, from_none], obj.get("gitRoot"))
head_commit = from_union([from_str, from_none], obj.get("headCommit"))
host_type = from_union([HostType, from_none], obj.get("hostType"))
repository = from_union([from_str, from_none], obj.get("repository"))
return ContextClass(cwd, base_commit, branch, git_root, head_commit, host_type, repository)
def to_dict(self) -> dict:
result: dict = {}
result["cwd"] = from_str(self.cwd)
if self.base_commit is not None:
result["baseCommit"] = from_union([from_str, from_none], self.base_commit)
if self.branch is not None:
result["branch"] = from_union([from_str, from_none], self.branch)
if self.git_root is not None:
result["gitRoot"] = from_union([from_str, from_none], self.git_root)
if self.head_commit is not None:
result["headCommit"] = from_union([from_str, from_none], self.head_commit)
if self.host_type is not None:
result["hostType"] = from_union([lambda x: to_enum(HostType, x), from_none], self.host_type)
if self.repository is not None:
result["repository"] = from_union([from_str, from_none], self.repository)
return result
@dataclass
class TokenDetail:
"""Token usage detail for a single billing category"""
batch_size: float
"""Number of tokens in this billing batch"""
cost_per_batch: float
"""Cost per batch of tokens"""
token_count: float
"""Total token count for this entry"""
token_type: str
"""Token category (e.g., "input", "output")"""
@staticmethod
def from_dict(obj: Any) -> 'TokenDetail':
assert isinstance(obj, dict)
batch_size = from_float(obj.get("batchSize"))
cost_per_batch = from_float(obj.get("costPerBatch"))
token_count = from_float(obj.get("tokenCount"))
token_type = from_str(obj.get("tokenType"))
return TokenDetail(batch_size, cost_per_batch, token_count, token_type)
def to_dict(self) -> dict:
result: dict = {}
result["batchSize"] = to_float(self.batch_size)
result["costPerBatch"] = to_float(self.cost_per_batch)
result["tokenCount"] = to_float(self.token_count)
result["tokenType"] = from_str(self.token_type)
return result
@dataclass
class CopilotUsage:
"""Per-request cost and usage data from the CAPI copilot_usage response field"""
token_details: list[TokenDetail]
"""Itemized token usage breakdown"""
total_nano_aiu: float
"""Total cost in nano-AIU (AI Units) for this request"""
@staticmethod
def from_dict(obj: Any) -> 'CopilotUsage':
assert isinstance(obj, dict)
token_details = from_list(TokenDetail.from_dict, obj.get("tokenDetails"))
total_nano_aiu = from_float(obj.get("totalNanoAiu"))
return CopilotUsage(token_details, total_nano_aiu)
def to_dict(self) -> dict:
result: dict = {}
result["tokenDetails"] = from_list(lambda x: to_class(TokenDetail, x), self.token_details)
result["totalNanoAiu"] = to_float(self.total_nano_aiu)
return result
@dataclass
class ErrorClass:
"""Error details when the tool execution failed
Error details when the hook failed
"""
message: str
"""Human-readable error message"""
code: str | None = None
"""Machine-readable error code"""
stack: str | None = None
"""Error stack trace, when available"""
@staticmethod
def from_dict(obj: Any) -> 'ErrorClass':
assert isinstance(obj, dict)
message = from_str(obj.get("message"))
code = from_union([from_str, from_none], obj.get("code"))
stack = from_union([from_str, from_none], obj.get("stack"))
return ErrorClass(message, code, stack)
def to_dict(self) -> dict:
result: dict = {}
result["message"] = from_str(self.message)
if self.code is not None:
result["code"] = from_union([from_str, from_none], self.code)
if self.stack is not None:
result["stack"] = from_union([from_str, from_none], self.stack)
return result
class Status(Enum):
"""Whether the agent completed successfully or failed"""
COMPLETED = "completed"
FAILED = "failed"
class KindType(Enum):
AGENT_COMPLETED = "agent_completed"
SHELL_COMPLETED = "shell_completed"
SHELL_DETACHED_COMPLETED = "shell_detached_completed"
@dataclass
class KindClass:
"""Structured metadata identifying what triggered this notification"""
type: KindType
agent_id: str | None = None
"""Unique identifier of the background agent"""
agent_type: str | None = None
"""Type of the agent (e.g., explore, task, general-purpose)"""
description: str | None = None
"""Human-readable description of the agent task
Human-readable description of the command
"""
prompt: str | None = None
"""The full prompt given to the background agent"""
status: Status | None = None
"""Whether the agent completed successfully or failed"""
exit_code: float | None = None
"""Exit code of the shell command, if available"""
shell_id: str | None = None
"""Unique identifier of the shell session
Unique identifier of the detached shell session
"""
@staticmethod
def from_dict(obj: Any) -> 'KindClass':
assert isinstance(obj, dict)
type = KindType(obj.get("type"))
agent_id = from_union([from_str, from_none], obj.get("agentId"))
agent_type = from_union([from_str, from_none], obj.get("agentType"))
description = from_union([from_str, from_none], obj.get("description"))
prompt = from_union([from_str, from_none], obj.get("prompt"))
status = from_union([Status, from_none], obj.get("status"))
exit_code = from_union([from_float, from_none], obj.get("exitCode"))
shell_id = from_union([from_str, from_none], obj.get("shellId"))
return KindClass(type, agent_id, agent_type, description, prompt, status, exit_code, shell_id)
def to_dict(self) -> dict:
result: dict = {}
result["type"] = to_enum(KindType, self.type)
if self.agent_id is not None:
result["agentId"] = from_union([from_str, from_none], self.agent_id)
if self.agent_type is not None:
result["agentType"] = from_union([from_str, from_none], self.agent_type)
if self.description is not None:
result["description"] = from_union([from_str, from_none], self.description)
if self.prompt is not None:
result["prompt"] = from_union([from_str, from_none], self.prompt)
if self.status is not None:
result["status"] = from_union([lambda x: to_enum(Status, x), from_none], self.status)
if self.exit_code is not None:
result["exitCode"] = from_union([to_float, from_none], self.exit_code)
if self.shell_id is not None:
result["shellId"] = from_union([from_str, from_none], self.shell_id)
return result
@dataclass
class Metadata:
"""Metadata about the prompt template and its construction"""
prompt_version: str | None = None
"""Version identifier of the prompt template used"""
variables: dict[str, Any] | None = None
"""Template variables used when constructing the prompt"""
@staticmethod
def from_dict(obj: Any) -> 'Metadata':
assert isinstance(obj, dict)
prompt_version = from_union([from_str, from_none], obj.get("promptVersion"))
variables = from_union([lambda x: from_dict(lambda x: x, x), from_none], obj.get("variables"))
return Metadata(prompt_version, variables)
def to_dict(self) -> dict:
result: dict = {}
if self.prompt_version is not None:
result["promptVersion"] = from_union([from_str, from_none], self.prompt_version)
if self.variables is not None:
result["variables"] = from_union([lambda x: from_dict(lambda x: x, x), from_none], self.variables)
return result
class Mode(Enum):
FORM = "form"
@dataclass
class Requests:
"""Request count and cost metrics"""
cost: float
"""Cumulative cost multiplier for requests to this model"""
count: float
"""Total number of API requests made to this model"""
@staticmethod
def from_dict(obj: Any) -> 'Requests':
assert isinstance(obj, dict)
cost = from_float(obj.get("cost"))
count = from_float(obj.get("count"))
return Requests(cost, count)
def to_dict(self) -> dict:
result: dict = {}
result["cost"] = to_float(self.cost)
result["count"] = to_float(self.count)
return result
@dataclass
class Usage:
"""Token usage breakdown"""
cache_read_tokens: float
"""Total tokens read from prompt cache across all requests"""
cache_write_tokens: float
"""Total tokens written to prompt cache across all requests"""
input_tokens: float
"""Total input tokens consumed across all requests to this model"""
output_tokens: float
"""Total output tokens produced across all requests to this model"""
@staticmethod
def from_dict(obj: Any) -> 'Usage':
assert isinstance(obj, dict)
cache_read_tokens = from_float(obj.get("cacheReadTokens"))
cache_write_tokens = from_float(obj.get("cacheWriteTokens"))
input_tokens = from_float(obj.get("inputTokens"))
output_tokens = from_float(obj.get("outputTokens"))
return Usage(cache_read_tokens, cache_write_tokens, input_tokens, output_tokens)
def to_dict(self) -> dict:
result: dict = {}
result["cacheReadTokens"] = to_float(self.cache_read_tokens)
result["cacheWriteTokens"] = to_float(self.cache_write_tokens)
result["inputTokens"] = to_float(self.input_tokens)
result["outputTokens"] = to_float(self.output_tokens)
return result
@dataclass
class ModelMetric:
requests: Requests
"""Request count and cost metrics"""
usage: Usage
"""Token usage breakdown"""
@staticmethod
def from_dict(obj: Any) -> 'ModelMetric':
assert isinstance(obj, dict)
requests = Requests.from_dict(obj.get("requests"))
usage = Usage.from_dict(obj.get("usage"))
return ModelMetric(requests, usage)
def to_dict(self) -> dict:
result: dict = {}
result["requests"] = to_class(Requests, self.requests)
result["usage"] = to_class(Usage, self.usage)
return result
class Operation(Enum):
"""The type of operation performed on the plan file
Whether the file was newly created or updated
"""
CREATE = "create"
DELETE = "delete"
UPDATE = "update"
@dataclass
class Command:
identifier: str
"""Command identifier (e.g., executable name)"""
read_only: bool
"""Whether this command is read-only (no side effects)"""
@staticmethod
def from_dict(obj: Any) -> 'Command':
assert isinstance(obj, dict)
identifier = from_str(obj.get("identifier"))
read_only = from_bool(obj.get("readOnly"))
return Command(identifier, read_only)
def to_dict(self) -> dict:
result: dict = {}
result["identifier"] = from_str(self.identifier)
result["readOnly"] = from_bool(self.read_only)
return result
class PermissionRequestKind(Enum):
CUSTOM_TOOL = "custom-tool"
HOOK = "hook"
MCP = "mcp"
MEMORY = "memory"
READ = "read"
SHELL = "shell"
URL = "url"
WRITE = "write"
@dataclass
class PossibleURL:
url: str
"""URL that may be accessed by the command"""
@staticmethod
def from_dict(obj: Any) -> 'PossibleURL':
assert isinstance(obj, dict)
url = from_str(obj.get("url"))
return PossibleURL(url)
def to_dict(self) -> dict:
result: dict = {}
result["url"] = from_str(self.url)
return result
@dataclass
class PermissionRequest:
"""Details of the permission being requested
Shell command permission request
File write permission request
File or directory read permission request
MCP tool invocation permission request
URL access permission request
Memory storage permission request
Custom tool invocation permission request
Hook confirmation permission request
"""
kind: PermissionRequestKind
"""Permission kind discriminator"""
can_offer_session_approval: bool | None = None
"""Whether the UI can offer session-wide approval for this command pattern"""
commands: list[Command] | None = None
"""Parsed command identifiers found in the command text"""
full_command_text: str | None = None
"""The complete shell command text to be executed"""
has_write_file_redirection: bool | None = None
"""Whether the command includes a file write redirection (e.g., > or >>)"""
intention: str | None = None
"""Human-readable description of what the command intends to do
Human-readable description of the intended file change
Human-readable description of why the file is being read
Human-readable description of why the URL is being accessed
"""
possible_paths: list[str] | None = None
"""File paths that may be read or written by the command"""
possible_urls: list[PossibleURL] | None = None
"""URLs that may be accessed by the command"""
tool_call_id: str | None = None
"""Tool call ID that triggered this permission request"""
warning: str | None = None
"""Optional warning message about risks of running this command"""
diff: str | None = None
"""Unified diff showing the proposed changes"""
file_name: str | None = None
"""Path of the file being written to"""
new_file_contents: str | None = None
"""Complete new file contents for newly created files"""
path: str | None = None
"""Path of the file or directory being read"""
args: Any = None
"""Arguments to pass to the MCP tool
Arguments to pass to the custom tool
"""
read_only: bool | None = None
"""Whether this MCP tool is read-only (no side effects)"""
server_name: str | None = None
"""Name of the MCP server providing the tool"""
tool_name: str | None = None
"""Internal name of the MCP tool
Name of the custom tool
Name of the tool the hook is gating
"""
tool_title: str | None = None
"""Human-readable title of the MCP tool"""
url: str | None = None
"""URL to be fetched"""
citations: str | None = None
"""Source references for the stored fact"""
fact: str | None = None
"""The fact or convention being stored"""
subject: str | None = None
"""Topic or subject of the memory being stored"""
tool_description: str | None = None
"""Description of what the custom tool does"""
hook_message: str | None = None
"""Optional message from the hook explaining why confirmation is needed"""
tool_args: Any = None
"""Arguments of the tool call being gated"""
@staticmethod
def from_dict(obj: Any) -> 'PermissionRequest':
assert isinstance(obj, dict)
kind = PermissionRequestKind(obj.get("kind"))
can_offer_session_approval = from_union([from_bool, from_none], obj.get("canOfferSessionApproval"))
commands = from_union([lambda x: from_list(Command.from_dict, x), from_none], obj.get("commands"))
full_command_text = from_union([from_str, from_none], obj.get("fullCommandText"))
has_write_file_redirection = from_union([from_bool, from_none], obj.get("hasWriteFileRedirection"))
intention = from_union([from_str, from_none], obj.get("intention"))
possible_paths = from_union([lambda x: from_list(from_str, x), from_none], obj.get("possiblePaths"))
possible_urls = from_union([lambda x: from_list(PossibleURL.from_dict, x), from_none], obj.get("possibleUrls"))
tool_call_id = from_union([from_str, from_none], obj.get("toolCallId"))
warning = from_union([from_str, from_none], obj.get("warning"))
diff = from_union([from_str, from_none], obj.get("diff"))
file_name = from_union([from_str, from_none], obj.get("fileName"))
new_file_contents = from_union([from_str, from_none], obj.get("newFileContents"))
path = from_union([from_str, from_none], obj.get("path"))
args = obj.get("args")
read_only = from_union([from_bool, from_none], obj.get("readOnly"))
server_name = from_union([from_str, from_none], obj.get("serverName"))
tool_name = from_union([from_str, from_none], obj.get("toolName"))
tool_title = from_union([from_str, from_none], obj.get("toolTitle"))
url = from_union([from_str, from_none], obj.get("url"))
citations = from_union([from_str, from_none], obj.get("citations"))
fact = from_union([from_str, from_none], obj.get("fact"))
subject = from_union([from_str, from_none], obj.get("subject"))
tool_description = from_union([from_str, from_none], obj.get("toolDescription"))
hook_message = from_union([from_str, from_none], obj.get("hookMessage"))
tool_args = obj.get("toolArgs")
return PermissionRequest(kind, can_offer_session_approval, commands, full_command_text, has_write_file_redirection, intention, possible_paths, possible_urls, tool_call_id, warning, diff, file_name, new_file_contents, path, args, read_only, server_name, tool_name, tool_title, url, citations, fact, subject, tool_description, hook_message, tool_args)
def to_dict(self) -> dict:
result: dict = {}
result["kind"] = to_enum(PermissionRequestKind, self.kind)
if self.can_offer_session_approval is not None:
result["canOfferSessionApproval"] = from_union([from_bool, from_none], self.can_offer_session_approval)