inspect-ai 0.3.70__py3-none-any.whl → 0.3.71__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- inspect_ai/_cli/eval.py +14 -8
- inspect_ai/_display/core/display.py +2 -0
- inspect_ai/_display/core/footer.py +13 -3
- inspect_ai/_display/plain/display.py +6 -2
- inspect_ai/_display/rich/display.py +19 -6
- inspect_ai/_display/textual/app.py +6 -1
- inspect_ai/_display/textual/display.py +4 -0
- inspect_ai/_display/textual/widgets/transcript.py +10 -6
- inspect_ai/_eval/task/run.py +5 -8
- inspect_ai/_util/content.py +20 -1
- inspect_ai/_util/transcript.py +10 -4
- inspect_ai/_util/working.py +4 -0
- inspect_ai/_view/www/App.css +6 -0
- inspect_ai/_view/www/dist/assets/index.css +115 -87
- inspect_ai/_view/www/dist/assets/index.js +5324 -2276
- inspect_ai/_view/www/eslint.config.mjs +24 -1
- inspect_ai/_view/www/log-schema.json +283 -20
- inspect_ai/_view/www/package.json +8 -3
- inspect_ai/_view/www/src/App.tsx +2 -2
- inspect_ai/_view/www/src/components/AnsiDisplay.tsx +4 -3
- inspect_ai/_view/www/src/components/Card.tsx +9 -8
- inspect_ai/_view/www/src/components/DownloadButton.tsx +2 -1
- inspect_ai/_view/www/src/components/EmptyPanel.tsx +2 -2
- inspect_ai/_view/www/src/components/ErrorPanel.tsx +4 -3
- inspect_ai/_view/www/src/components/ExpandablePanel.tsx +13 -5
- inspect_ai/_view/www/src/components/FindBand.tsx +3 -3
- inspect_ai/_view/www/src/components/HumanBaselineView.tsx +3 -3
- inspect_ai/_view/www/src/components/LabeledValue.tsx +5 -4
- inspect_ai/_view/www/src/components/LargeModal.tsx +18 -13
- inspect_ai/_view/www/src/components/{LightboxCarousel.css → LightboxCarousel.module.css} +22 -18
- inspect_ai/_view/www/src/components/LightboxCarousel.tsx +36 -27
- inspect_ai/_view/www/src/components/MessageBand.tsx +2 -1
- inspect_ai/_view/www/src/components/NavPills.tsx +9 -8
- inspect_ai/_view/www/src/components/ProgressBar.tsx +2 -1
- inspect_ai/_view/www/src/components/TabSet.tsx +21 -15
- inspect_ai/_view/www/src/index.tsx +2 -2
- inspect_ai/_view/www/src/metadata/MetaDataGrid.tsx +11 -9
- inspect_ai/_view/www/src/metadata/MetaDataView.tsx +3 -2
- inspect_ai/_view/www/src/metadata/MetadataGrid.module.css +1 -0
- inspect_ai/_view/www/src/metadata/RenderedContent.tsx +16 -0
- inspect_ai/_view/www/src/plan/DatasetDetailView.tsx +3 -2
- inspect_ai/_view/www/src/plan/DetailStep.tsx +2 -1
- inspect_ai/_view/www/src/plan/PlanCard.tsx +2 -5
- inspect_ai/_view/www/src/plan/PlanDetailView.tsx +6 -9
- inspect_ai/_view/www/src/plan/ScorerDetailView.tsx +2 -1
- inspect_ai/_view/www/src/plan/SolverDetailView.tsx +3 -3
- inspect_ai/_view/www/src/samples/InlineSampleDisplay.tsx +2 -2
- inspect_ai/_view/www/src/samples/SampleDialog.tsx +3 -3
- inspect_ai/_view/www/src/samples/SampleDisplay.tsx +2 -2
- inspect_ai/_view/www/src/samples/SampleSummaryView.tsx +2 -2
- inspect_ai/_view/www/src/samples/SamplesTools.tsx +2 -1
- inspect_ai/_view/www/src/samples/chat/ChatMessage.tsx +3 -19
- inspect_ai/_view/www/src/samples/chat/ChatMessageRenderer.tsx +2 -1
- inspect_ai/_view/www/src/samples/chat/ChatMessageRow.tsx +2 -1
- inspect_ai/_view/www/src/samples/chat/ChatView.tsx +2 -1
- inspect_ai/_view/www/src/samples/chat/ChatViewVirtualList.tsx +22 -7
- inspect_ai/_view/www/src/samples/chat/MessageContent.tsx +35 -6
- inspect_ai/_view/www/src/samples/chat/MessageContents.tsx +2 -2
- inspect_ai/_view/www/src/samples/chat/messages.ts +15 -2
- inspect_ai/_view/www/src/samples/chat/tools/ToolCallView.tsx +13 -4
- inspect_ai/_view/www/src/samples/chat/tools/ToolInput.module.css +2 -2
- inspect_ai/_view/www/src/samples/chat/tools/ToolInput.tsx +18 -19
- inspect_ai/_view/www/src/samples/chat/tools/ToolOutput.module.css +1 -1
- inspect_ai/_view/www/src/samples/chat/tools/ToolOutput.tsx +4 -3
- inspect_ai/_view/www/src/samples/chat/tools/ToolTitle.tsx +2 -2
- inspect_ai/_view/www/src/samples/error/FlatSampleErrorView.tsx +2 -3
- inspect_ai/_view/www/src/samples/error/SampleErrorView.tsx +3 -2
- inspect_ai/_view/www/src/samples/list/SampleFooter.tsx +2 -1
- inspect_ai/_view/www/src/samples/list/SampleHeader.tsx +2 -1
- inspect_ai/_view/www/src/samples/list/SampleList.tsx +57 -45
- inspect_ai/_view/www/src/samples/list/SampleRow.tsx +2 -1
- inspect_ai/_view/www/src/samples/list/SampleSeparator.tsx +2 -1
- inspect_ai/_view/www/src/samples/sample-tools/EpochFilter.tsx +2 -2
- inspect_ai/_view/www/src/samples/sample-tools/SelectScorer.tsx +4 -3
- inspect_ai/_view/www/src/samples/sample-tools/SortFilter.tsx +2 -5
- inspect_ai/_view/www/src/samples/sample-tools/sample-filter/SampleFilter.tsx +2 -2
- inspect_ai/_view/www/src/samples/scores/SampleScoreView.tsx +2 -1
- inspect_ai/_view/www/src/samples/scores/SampleScores.tsx +2 -2
- inspect_ai/_view/www/src/samples/transcript/ApprovalEventView.tsx +2 -1
- inspect_ai/_view/www/src/samples/transcript/ErrorEventView.tsx +2 -1
- inspect_ai/_view/www/src/samples/transcript/InfoEventView.tsx +2 -1
- inspect_ai/_view/www/src/samples/transcript/InputEventView.tsx +2 -1
- inspect_ai/_view/www/src/samples/transcript/LoggerEventView.module.css +4 -0
- inspect_ai/_view/www/src/samples/transcript/LoggerEventView.tsx +12 -2
- inspect_ai/_view/www/src/samples/transcript/ModelEventView.module.css +1 -1
- inspect_ai/_view/www/src/samples/transcript/ModelEventView.tsx +25 -28
- inspect_ai/_view/www/src/samples/transcript/SampleInitEventView.tsx +2 -1
- inspect_ai/_view/www/src/samples/transcript/SampleLimitEventView.tsx +5 -4
- inspect_ai/_view/www/src/samples/transcript/SampleTranscript.tsx +2 -2
- inspect_ai/_view/www/src/samples/transcript/SandboxEventView.tsx +8 -7
- inspect_ai/_view/www/src/samples/transcript/ScoreEventView.tsx +2 -2
- inspect_ai/_view/www/src/samples/transcript/StepEventView.tsx +3 -3
- inspect_ai/_view/www/src/samples/transcript/SubtaskEventView.tsx +18 -14
- inspect_ai/_view/www/src/samples/transcript/ToolEventView.tsx +5 -5
- inspect_ai/_view/www/src/samples/transcript/TranscriptView.tsx +34 -15
- inspect_ai/_view/www/src/samples/transcript/event/EventNav.tsx +2 -1
- inspect_ai/_view/www/src/samples/transcript/event/EventNavs.tsx +2 -1
- inspect_ai/_view/www/src/samples/transcript/event/EventRow.tsx +3 -2
- inspect_ai/_view/www/src/samples/transcript/event/EventSection.tsx +2 -2
- inspect_ai/_view/www/src/samples/transcript/event/EventTimingPanel.module.css +28 -0
- inspect_ai/_view/www/src/samples/transcript/event/EventTimingPanel.tsx +115 -0
- inspect_ai/_view/www/src/samples/transcript/event/utils.ts +29 -0
- inspect_ai/_view/www/src/samples/transcript/state/StateDiffView.tsx +2 -1
- inspect_ai/_view/www/src/samples/transcript/state/StateEventRenderers.tsx +3 -3
- inspect_ai/_view/www/src/samples/transcript/state/StateEventView.tsx +11 -8
- inspect_ai/_view/www/src/types/log.d.ts +129 -34
- inspect_ai/_view/www/src/usage/ModelTokenTable.tsx +6 -10
- inspect_ai/_view/www/src/usage/ModelUsagePanel.module.css +4 -0
- inspect_ai/_view/www/src/usage/ModelUsagePanel.tsx +32 -9
- inspect_ai/_view/www/src/usage/TokenTable.tsx +4 -6
- inspect_ai/_view/www/src/usage/UsageCard.tsx +2 -1
- inspect_ai/_view/www/src/utils/format.ts +1 -1
- inspect_ai/_view/www/src/utils/json.ts +24 -0
- inspect_ai/_view/www/src/workspace/WorkSpace.tsx +6 -5
- inspect_ai/_view/www/src/workspace/WorkSpaceView.tsx +9 -2
- inspect_ai/_view/www/src/workspace/error/TaskErrorPanel.tsx +2 -1
- inspect_ai/_view/www/src/workspace/navbar/Navbar.tsx +2 -1
- inspect_ai/_view/www/src/workspace/navbar/PrimaryBar.tsx +3 -3
- inspect_ai/_view/www/src/workspace/navbar/ResultsPanel.tsx +4 -3
- inspect_ai/_view/www/src/workspace/navbar/SecondaryBar.tsx +5 -4
- inspect_ai/_view/www/src/workspace/navbar/StatusPanel.tsx +5 -8
- inspect_ai/_view/www/src/workspace/sidebar/EvalStatus.tsx +5 -4
- inspect_ai/_view/www/src/workspace/sidebar/LogDirectoryTitleView.tsx +2 -1
- inspect_ai/_view/www/src/workspace/sidebar/Sidebar.tsx +2 -1
- inspect_ai/_view/www/src/workspace/sidebar/SidebarLogEntry.tsx +2 -2
- inspect_ai/_view/www/src/workspace/sidebar/SidebarScoreView.tsx +2 -1
- inspect_ai/_view/www/src/workspace/sidebar/SidebarScoresView.tsx +2 -2
- inspect_ai/_view/www/src/workspace/tabs/InfoTab.tsx +2 -2
- inspect_ai/_view/www/src/workspace/tabs/JsonTab.tsx +2 -5
- inspect_ai/_view/www/src/workspace/tabs/SamplesTab.tsx +12 -11
- inspect_ai/_view/www/yarn.lock +241 -5
- inspect_ai/log/_condense.py +3 -0
- inspect_ai/log/_recorders/eval.py +6 -1
- inspect_ai/log/_transcript.py +58 -1
- inspect_ai/model/__init__.py +2 -0
- inspect_ai/model/_call_tools.py +7 -0
- inspect_ai/model/_chat_message.py +22 -7
- inspect_ai/model/_conversation.py +10 -8
- inspect_ai/model/_generate_config.py +25 -4
- inspect_ai/model/_model.py +133 -57
- inspect_ai/model/_model_output.py +3 -0
- inspect_ai/model/_openai.py +106 -40
- inspect_ai/model/_providers/anthropic.py +134 -26
- inspect_ai/model/_providers/google.py +27 -8
- inspect_ai/model/_providers/groq.py +9 -4
- inspect_ai/model/_providers/openai.py +57 -4
- inspect_ai/model/_providers/openai_o1.py +10 -0
- inspect_ai/model/_providers/providers.py +1 -1
- inspect_ai/model/_reasoning.py +15 -2
- inspect_ai/scorer/_model.py +23 -19
- inspect_ai/solver/_human_agent/agent.py +14 -10
- inspect_ai/solver/_human_agent/commands/__init__.py +7 -3
- inspect_ai/solver/_human_agent/commands/submit.py +76 -30
- inspect_ai/tool/__init__.py +2 -0
- inspect_ai/tool/_tool.py +3 -1
- inspect_ai/tool/_tools/_computer/_resources/tool/_run.py +1 -1
- inspect_ai/tool/_tools/_web_browser/_resources/.pylintrc +8 -0
- inspect_ai/tool/_tools/_web_browser/_resources/.vscode/launch.json +24 -0
- inspect_ai/tool/_tools/_web_browser/_resources/.vscode/settings.json +25 -0
- inspect_ai/tool/_tools/_web_browser/_resources/Dockerfile +5 -6
- inspect_ai/tool/_tools/_web_browser/_resources/README.md +10 -11
- inspect_ai/tool/_tools/_web_browser/_resources/accessibility_tree.py +71 -0
- inspect_ai/tool/_tools/_web_browser/_resources/accessibility_tree_node.py +323 -0
- inspect_ai/tool/_tools/_web_browser/_resources/cdp/__init__.py +5 -0
- inspect_ai/tool/_tools/_web_browser/_resources/cdp/a11y.py +279 -0
- inspect_ai/tool/_tools/_web_browser/_resources/cdp/dom.py +9 -0
- inspect_ai/tool/_tools/_web_browser/_resources/cdp/dom_snapshot.py +293 -0
- inspect_ai/tool/_tools/_web_browser/_resources/cdp/page.py +94 -0
- inspect_ai/tool/_tools/_web_browser/_resources/constants.py +2 -0
- inspect_ai/tool/_tools/_web_browser/_resources/images/usage_diagram.svg +2 -0
- inspect_ai/tool/_tools/_web_browser/_resources/playwright_browser.py +50 -0
- inspect_ai/tool/_tools/_web_browser/_resources/playwright_crawler.py +31 -359
- inspect_ai/tool/_tools/_web_browser/_resources/playwright_page_crawler.py +280 -0
- inspect_ai/tool/_tools/_web_browser/_resources/pyproject.toml +65 -0
- inspect_ai/tool/_tools/_web_browser/_resources/rectangle.py +64 -0
- inspect_ai/tool/_tools/_web_browser/_resources/rpc_client_helpers.py +146 -0
- inspect_ai/tool/_tools/_web_browser/_resources/scale_factor.py +64 -0
- inspect_ai/tool/_tools/_web_browser/_resources/test_accessibility_tree_node.py +180 -0
- inspect_ai/tool/_tools/_web_browser/_resources/test_playwright_crawler.py +15 -9
- inspect_ai/tool/_tools/_web_browser/_resources/test_rectangle.py +15 -0
- inspect_ai/tool/_tools/_web_browser/_resources/test_web_client.py +44 -0
- inspect_ai/tool/_tools/_web_browser/_resources/web_browser_rpc_types.py +39 -0
- inspect_ai/tool/_tools/_web_browser/_resources/web_client.py +198 -48
- inspect_ai/tool/_tools/_web_browser/_resources/web_client_new_session.py +26 -25
- inspect_ai/tool/_tools/_web_browser/_resources/web_server.py +178 -39
- inspect_ai/tool/_tools/_web_browser/_web_browser.py +38 -19
- inspect_ai/util/__init__.py +2 -1
- inspect_ai/util/_display.py +12 -0
- inspect_ai/util/_sandbox/events.py +55 -21
- inspect_ai/util/_sandbox/self_check.py +131 -43
- inspect_ai/util/_subtask.py +11 -0
- {inspect_ai-0.3.70.dist-info → inspect_ai-0.3.71.dist-info}/METADATA +1 -1
- {inspect_ai-0.3.70.dist-info → inspect_ai-0.3.71.dist-info}/RECORD +197 -182
- {inspect_ai-0.3.70.dist-info → inspect_ai-0.3.71.dist-info}/WHEEL +1 -1
- inspect_ai/_view/www/node_modules/flatted/python/flatted.py +0 -149
- inspect_ai/_view/www/node_modules/flatted/python/test.py +0 -63
- inspect_ai/_view/www/src/components/VirtualList.module.css +0 -19
- inspect_ai/_view/www/src/components/VirtualList.tsx +0 -292
- inspect_ai/tool/_tools/_web_browser/_resources/accessibility_node.py +0 -312
- inspect_ai/tool/_tools/_web_browser/_resources/dm_env_servicer.py +0 -275
- inspect_ai/tool/_tools/_web_browser/_resources/images/usage_diagram.png +0 -0
- inspect_ai/tool/_tools/_web_browser/_resources/test_accessibility_node.py +0 -176
- inspect_ai/tool/_tools/_web_browser/_resources/test_dm_env_servicer.py +0 -135
- inspect_ai/tool/_tools/_web_browser/_resources/test_web_environment.py +0 -71
- inspect_ai/tool/_tools/_web_browser/_resources/web_environment.py +0 -184
- {inspect_ai-0.3.70.dist-info → inspect_ai-0.3.71.dist-info}/LICENSE +0 -0
- {inspect_ai-0.3.70.dist-info → inspect_ai-0.3.71.dist-info}/entry_points.txt +0 -0
- {inspect_ai-0.3.70.dist-info → inspect_ai-0.3.71.dist-info}/top_level.txt +0 -0
@@ -1,149 +0,0 @@
|
|
1
|
-
# ISC License
|
2
|
-
#
|
3
|
-
# Copyright (c) 2018-2021, Andrea Giammarchi, @WebReflection
|
4
|
-
#
|
5
|
-
# Permission to use, copy, modify, and/or distribute this software for any
|
6
|
-
# purpose with or without fee is hereby granted, provided that the above
|
7
|
-
# copyright notice and this permission notice appear in all copies.
|
8
|
-
#
|
9
|
-
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
10
|
-
# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
11
|
-
# AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
12
|
-
# INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
13
|
-
# LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
14
|
-
# OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
15
|
-
# PERFORMANCE OF THIS SOFTWARE.
|
16
|
-
|
17
|
-
import json as _json
|
18
|
-
|
19
|
-
class _Known:
|
20
|
-
def __init__(self):
|
21
|
-
self.key = []
|
22
|
-
self.value = []
|
23
|
-
|
24
|
-
class _String:
|
25
|
-
def __init__(self, value):
|
26
|
-
self.value = value
|
27
|
-
|
28
|
-
|
29
|
-
def _array_keys(value):
|
30
|
-
keys = []
|
31
|
-
i = 0
|
32
|
-
for _ in value:
|
33
|
-
keys.append(i)
|
34
|
-
i += 1
|
35
|
-
return keys
|
36
|
-
|
37
|
-
def _object_keys(value):
|
38
|
-
keys = []
|
39
|
-
for key in value:
|
40
|
-
keys.append(key)
|
41
|
-
return keys
|
42
|
-
|
43
|
-
def _is_array(value):
|
44
|
-
return isinstance(value, list) or isinstance(value, tuple)
|
45
|
-
|
46
|
-
def _is_object(value):
|
47
|
-
return isinstance(value, dict)
|
48
|
-
|
49
|
-
def _is_string(value):
|
50
|
-
return isinstance(value, str)
|
51
|
-
|
52
|
-
def _index(known, input, value):
|
53
|
-
input.append(value)
|
54
|
-
index = str(len(input) - 1)
|
55
|
-
known.key.append(value)
|
56
|
-
known.value.append(index)
|
57
|
-
return index
|
58
|
-
|
59
|
-
def _loop(keys, input, known, output):
|
60
|
-
for key in keys:
|
61
|
-
value = output[key]
|
62
|
-
if isinstance(value, _String):
|
63
|
-
_ref(key, input[int(value.value)], input, known, output)
|
64
|
-
|
65
|
-
return output
|
66
|
-
|
67
|
-
def _ref(key, value, input, known, output):
|
68
|
-
if _is_array(value) and not value in known:
|
69
|
-
known.append(value)
|
70
|
-
value = _loop(_array_keys(value), input, known, value)
|
71
|
-
elif _is_object(value) and not value in known:
|
72
|
-
known.append(value)
|
73
|
-
value = _loop(_object_keys(value), input, known, value)
|
74
|
-
|
75
|
-
output[key] = value
|
76
|
-
|
77
|
-
def _relate(known, input, value):
|
78
|
-
if _is_string(value) or _is_array(value) or _is_object(value):
|
79
|
-
try:
|
80
|
-
return known.value[known.key.index(value)]
|
81
|
-
except:
|
82
|
-
return _index(known, input, value)
|
83
|
-
|
84
|
-
return value
|
85
|
-
|
86
|
-
def _transform(known, input, value):
|
87
|
-
if _is_array(value):
|
88
|
-
output = []
|
89
|
-
for val in value:
|
90
|
-
output.append(_relate(known, input, val))
|
91
|
-
return output
|
92
|
-
|
93
|
-
if _is_object(value):
|
94
|
-
obj = {}
|
95
|
-
for key in value:
|
96
|
-
obj[key] = _relate(known, input, value[key])
|
97
|
-
return obj
|
98
|
-
|
99
|
-
return value
|
100
|
-
|
101
|
-
def _wrap(value):
|
102
|
-
if _is_string(value):
|
103
|
-
return _String(value)
|
104
|
-
|
105
|
-
if _is_array(value):
|
106
|
-
i = 0
|
107
|
-
for val in value:
|
108
|
-
value[i] = _wrap(val)
|
109
|
-
i += 1
|
110
|
-
|
111
|
-
elif _is_object(value):
|
112
|
-
for key in value:
|
113
|
-
value[key] = _wrap(value[key])
|
114
|
-
|
115
|
-
return value
|
116
|
-
|
117
|
-
def parse(value, *args, **kwargs):
|
118
|
-
json = _json.loads(value, *args, **kwargs)
|
119
|
-
wrapped = []
|
120
|
-
for value in json:
|
121
|
-
wrapped.append(_wrap(value))
|
122
|
-
|
123
|
-
input = []
|
124
|
-
for value in wrapped:
|
125
|
-
if isinstance(value, _String):
|
126
|
-
input.append(value.value)
|
127
|
-
else:
|
128
|
-
input.append(value)
|
129
|
-
|
130
|
-
value = input[0]
|
131
|
-
|
132
|
-
if _is_array(value):
|
133
|
-
return _loop(_array_keys(value), input, [value], value)
|
134
|
-
|
135
|
-
if _is_object(value):
|
136
|
-
return _loop(_object_keys(value), input, [value], value)
|
137
|
-
|
138
|
-
return value
|
139
|
-
|
140
|
-
|
141
|
-
def stringify(value, *args, **kwargs):
|
142
|
-
known = _Known()
|
143
|
-
input = []
|
144
|
-
output = []
|
145
|
-
i = int(_index(known, input, value))
|
146
|
-
while i < len(input):
|
147
|
-
output.append(_transform(known, input, input[i]))
|
148
|
-
i += 1
|
149
|
-
return _json.dumps(output, *args, **kwargs)
|
@@ -1,63 +0,0 @@
|
|
1
|
-
from flatted import stringify as _stringify, parse
|
2
|
-
|
3
|
-
def stringify(value):
|
4
|
-
return _stringify(value, separators=(',', ':'))
|
5
|
-
|
6
|
-
assert stringify([None, None]) == '[[null,null]]'
|
7
|
-
|
8
|
-
a = []
|
9
|
-
o = {}
|
10
|
-
|
11
|
-
assert stringify(a) == '[[]]'
|
12
|
-
assert stringify(o) == '[{}]'
|
13
|
-
|
14
|
-
a.append(a)
|
15
|
-
o['o'] = o
|
16
|
-
|
17
|
-
assert stringify(a) == '[["0"]]'
|
18
|
-
assert stringify(o) == '[{"o":"0"}]'
|
19
|
-
|
20
|
-
b = parse(stringify(a))
|
21
|
-
assert isinstance(b, list) and b[0] == b
|
22
|
-
|
23
|
-
a.append(1)
|
24
|
-
a.append('two')
|
25
|
-
a.append(True)
|
26
|
-
o['one'] = 1
|
27
|
-
o['two'] = 'two'
|
28
|
-
o['three'] = True
|
29
|
-
|
30
|
-
assert stringify(a) == '[["0",1,"1",true],"two"]'
|
31
|
-
assert stringify(o) == '[{"o":"0","one":1,"two":"1","three":true},"two"]'
|
32
|
-
|
33
|
-
a.append(o)
|
34
|
-
o['a'] = a
|
35
|
-
|
36
|
-
assert stringify(a) == '[["0",1,"1",true,"2"],"two",{"o":"2","one":1,"two":"1","three":true,"a":"0"}]'
|
37
|
-
assert stringify(o) == '[{"o":"0","one":1,"two":"1","three":true,"a":"2"},"two",["2",1,"1",true,"0"]]'
|
38
|
-
|
39
|
-
a.append({'test': 'OK'})
|
40
|
-
a.append([1, 2, 3])
|
41
|
-
|
42
|
-
o['test'] = {'test': 'OK'}
|
43
|
-
o['array'] = [1, 2, 3]
|
44
|
-
|
45
|
-
assert stringify(a) == '[["0",1,"1",true,"2","3","4"],"two",{"o":"2","one":1,"two":"1","three":true,"a":"0","test":"3","array":"4"},{"test":"5"},[1,2,3],"OK"]'
|
46
|
-
assert stringify(o) == '[{"o":"0","one":1,"two":"1","three":true,"a":"2","test":"3","array":"4"},"two",["2",1,"1",true,"0","3","4"],{"test":"5"},[1,2,3],"OK"]'
|
47
|
-
|
48
|
-
a2 = parse(stringify(a));
|
49
|
-
o2 = parse(stringify(o));
|
50
|
-
|
51
|
-
assert a2[0] == a2
|
52
|
-
assert o2['o'] == o2
|
53
|
-
|
54
|
-
assert a2[1] == 1 and a2[2] == 'two' and a2[3] == True and isinstance(a2[4], dict)
|
55
|
-
assert a2[4] == a2[4]['o'] and a2 == a2[4]['o']['a']
|
56
|
-
|
57
|
-
str = parse('[{"prop":"1","a":"2","b":"3"},{"value":123},["4","5"],{"e":"6","t":"7","p":4},{},{"b":"8"},"f",{"a":"9"},["10"],"sup",{"a":1,"d":2,"c":"7","z":"11","h":1},{"g":2,"a":"7","b":"12","f":6},{"r":4,"u":"7","c":5}]')
|
58
|
-
assert str['b']['t']['a'] == 'sup' and str['a'][1]['b'][0]['c'] == str['b']['t']
|
59
|
-
|
60
|
-
oo = parse('[{"a":"1","b":"0","c":"2"},{"aa":"3"},{"ca":"4","cb":"5","cc":"6","cd":"7","ce":"8","cf":"9"},{"aaa":"10"},{"caa":"4"},{"cba":"5"},{"cca":"2"},{"cda":"4"},"value2","value3","value1"]');
|
61
|
-
assert oo['a']['aa']['aaa'] == 'value1' and oo == oo['b'] and oo['c']['ca']['caa'] == oo['c']['ca']
|
62
|
-
|
63
|
-
print('OK')
|
@@ -1,19 +0,0 @@
|
|
1
|
-
.container {
|
2
|
-
position: relative;
|
3
|
-
width: 100%;
|
4
|
-
min-height: 100%;
|
5
|
-
overflow: visible;
|
6
|
-
}
|
7
|
-
|
8
|
-
.container.hidden {
|
9
|
-
overflow: hidden;
|
10
|
-
}
|
11
|
-
|
12
|
-
.content {
|
13
|
-
position: absolute;
|
14
|
-
top: 0;
|
15
|
-
left: 0;
|
16
|
-
height: 100%;
|
17
|
-
width: 100%;
|
18
|
-
overflow: visible;
|
19
|
-
}
|
@@ -1,292 +0,0 @@
|
|
1
|
-
import clsx from "clsx";
|
2
|
-
import React, {
|
3
|
-
forwardRef,
|
4
|
-
useEffect,
|
5
|
-
useImperativeHandle,
|
6
|
-
useMemo,
|
7
|
-
useRef,
|
8
|
-
useState,
|
9
|
-
} from "react";
|
10
|
-
import styles from "./VirtualList.module.css";
|
11
|
-
|
12
|
-
export interface VirtualListRef {
|
13
|
-
focus: () => void;
|
14
|
-
scrollToIndex: (index: number, direction?: "up" | "down") => void;
|
15
|
-
}
|
16
|
-
|
17
|
-
interface VirtualListProps<T> {
|
18
|
-
data: T[];
|
19
|
-
renderRow: (item: T, index: number) => React.ReactNode;
|
20
|
-
overscanCount?: number;
|
21
|
-
initialEstimatedRowHeight?: number;
|
22
|
-
sync?: boolean;
|
23
|
-
scrollRef?: React.RefObject<HTMLElement | null>;
|
24
|
-
className?: string;
|
25
|
-
style?: React.CSSProperties;
|
26
|
-
tabIndex?: number;
|
27
|
-
onKeyDown?: (event: React.KeyboardEvent<HTMLDivElement>) => void;
|
28
|
-
}
|
29
|
-
|
30
|
-
interface ListMetrics {
|
31
|
-
rowHeights: Map<number, number>;
|
32
|
-
totalHeight: number;
|
33
|
-
estimatedRowHeight: number;
|
34
|
-
}
|
35
|
-
|
36
|
-
export const VirtualList = forwardRef(function VirtualList<T>(
|
37
|
-
{
|
38
|
-
data,
|
39
|
-
renderRow,
|
40
|
-
overscanCount = 15,
|
41
|
-
initialEstimatedRowHeight = 50,
|
42
|
-
sync = false,
|
43
|
-
scrollRef,
|
44
|
-
onKeyDown,
|
45
|
-
...props
|
46
|
-
}: VirtualListProps<T>,
|
47
|
-
ref: React.Ref<VirtualListRef>,
|
48
|
-
) {
|
49
|
-
const [height, setHeight] = useState(0);
|
50
|
-
const [offset, setOffset] = useState(0);
|
51
|
-
const [listMetrics, setListMetrics] = useState<ListMetrics>({
|
52
|
-
rowHeights: new Map(),
|
53
|
-
totalHeight: data.length * initialEstimatedRowHeight,
|
54
|
-
estimatedRowHeight: initialEstimatedRowHeight,
|
55
|
-
});
|
56
|
-
|
57
|
-
const baseRef = useRef<HTMLDivElement>(null);
|
58
|
-
const containerRef = useRef<HTMLDivElement>(null);
|
59
|
-
const rowRefs = useRef<Map<number, HTMLElement>>(new Map());
|
60
|
-
|
61
|
-
const getRowHeight = (index: number): number => {
|
62
|
-
return listMetrics.rowHeights.get(index) || listMetrics.estimatedRowHeight;
|
63
|
-
};
|
64
|
-
|
65
|
-
// Calculate new estimated height based on measured rows
|
66
|
-
const calculateEstimatedHeight = (heights: Map<number, number>): number => {
|
67
|
-
if (heights.size === 0) return listMetrics.estimatedRowHeight;
|
68
|
-
|
69
|
-
// Calculate average of measured heights
|
70
|
-
let sum = 0;
|
71
|
-
heights.forEach((height) => {
|
72
|
-
sum += height;
|
73
|
-
});
|
74
|
-
|
75
|
-
// Use exponential moving average to smooth transitions
|
76
|
-
const alpha = 0.2; // Smoothing factor
|
77
|
-
const newEstimate = sum / heights.size;
|
78
|
-
return Math.round(
|
79
|
-
alpha * newEstimate + (1 - alpha) * listMetrics.estimatedRowHeight,
|
80
|
-
);
|
81
|
-
};
|
82
|
-
|
83
|
-
const rowPositions = useMemo(() => {
|
84
|
-
let currentPosition = 0;
|
85
|
-
const positions = new Map<number, number>();
|
86
|
-
|
87
|
-
for (let i = 0; i < data.length; i++) {
|
88
|
-
positions.set(i, currentPosition);
|
89
|
-
currentPosition += getRowHeight(i);
|
90
|
-
}
|
91
|
-
|
92
|
-
return positions;
|
93
|
-
}, [listMetrics.rowHeights, listMetrics.estimatedRowHeight, data.length]);
|
94
|
-
|
95
|
-
// Measure rendered rows and update heights if needed
|
96
|
-
const measureRows = () => {
|
97
|
-
let updates: [number, number][] = [];
|
98
|
-
|
99
|
-
rowRefs.current.forEach((element, index) => {
|
100
|
-
if (element) {
|
101
|
-
const measuredHeight = element.offsetHeight;
|
102
|
-
if (
|
103
|
-
measuredHeight &&
|
104
|
-
measuredHeight !== listMetrics.rowHeights.get(index)
|
105
|
-
) {
|
106
|
-
updates.push([index, measuredHeight]);
|
107
|
-
}
|
108
|
-
}
|
109
|
-
});
|
110
|
-
|
111
|
-
if (updates.length === 0) return;
|
112
|
-
|
113
|
-
const newHeights = new Map(listMetrics.rowHeights);
|
114
|
-
updates.forEach(([index, height]) => {
|
115
|
-
newHeights.set(index, height);
|
116
|
-
});
|
117
|
-
|
118
|
-
// Calculate new estimated height
|
119
|
-
const newEstimatedHeight = calculateEstimatedHeight(newHeights);
|
120
|
-
|
121
|
-
let newTotalHeight = 0;
|
122
|
-
for (let i = 0; i < data.length; i++) {
|
123
|
-
newTotalHeight += newHeights.get(i) || newEstimatedHeight;
|
124
|
-
}
|
125
|
-
|
126
|
-
setListMetrics({
|
127
|
-
rowHeights: newHeights,
|
128
|
-
totalHeight: newTotalHeight,
|
129
|
-
estimatedRowHeight: newEstimatedHeight,
|
130
|
-
});
|
131
|
-
};
|
132
|
-
|
133
|
-
useImperativeHandle(
|
134
|
-
ref,
|
135
|
-
() => ({
|
136
|
-
focus: () => {
|
137
|
-
baseRef.current?.focus();
|
138
|
-
},
|
139
|
-
scrollToIndex: (index: number, direction?: "up" | "down") => {
|
140
|
-
const scrollElement = scrollRef?.current || baseRef.current;
|
141
|
-
if (!scrollElement || index < 0 || index >= data.length) return;
|
142
|
-
|
143
|
-
const currentScrollTop = scrollElement.scrollTop;
|
144
|
-
const viewportHeight = scrollElement.offsetHeight;
|
145
|
-
|
146
|
-
const rowTop = rowPositions.get(index) || 0;
|
147
|
-
const rowHeight = getRowHeight(index);
|
148
|
-
const rowBottom = rowTop + rowHeight;
|
149
|
-
|
150
|
-
const isVisible =
|
151
|
-
rowTop >= currentScrollTop &&
|
152
|
-
rowBottom <= currentScrollTop + viewportHeight;
|
153
|
-
if (isVisible) return;
|
154
|
-
|
155
|
-
let newScrollTop: number;
|
156
|
-
if (direction === "up") {
|
157
|
-
newScrollTop = rowTop;
|
158
|
-
} else {
|
159
|
-
newScrollTop = rowBottom - viewportHeight;
|
160
|
-
}
|
161
|
-
|
162
|
-
newScrollTop = Math.max(
|
163
|
-
0,
|
164
|
-
Math.min(newScrollTop, listMetrics.totalHeight - viewportHeight),
|
165
|
-
);
|
166
|
-
scrollElement.scrollTop = newScrollTop;
|
167
|
-
},
|
168
|
-
}),
|
169
|
-
[rowPositions, data.length],
|
170
|
-
);
|
171
|
-
|
172
|
-
const resize = () => {
|
173
|
-
const scrollElement = scrollRef?.current || baseRef.current;
|
174
|
-
if (scrollElement && height !== scrollElement.offsetHeight) {
|
175
|
-
setHeight(scrollElement.offsetHeight);
|
176
|
-
}
|
177
|
-
};
|
178
|
-
|
179
|
-
const handleScroll = throttle(() => {
|
180
|
-
const scrollElement = scrollRef?.current || baseRef.current;
|
181
|
-
if (scrollElement) {
|
182
|
-
setOffset(scrollElement.scrollTop);
|
183
|
-
}
|
184
|
-
if (sync) {
|
185
|
-
setOffset((prev) => prev);
|
186
|
-
}
|
187
|
-
}, 100);
|
188
|
-
|
189
|
-
useEffect(() => {
|
190
|
-
resize();
|
191
|
-
const scrollElement = scrollRef?.current || baseRef.current;
|
192
|
-
|
193
|
-
if (scrollElement) {
|
194
|
-
scrollElement.addEventListener("scroll", handleScroll);
|
195
|
-
window.addEventListener("resize", resize);
|
196
|
-
|
197
|
-
return () => {
|
198
|
-
scrollElement.removeEventListener("scroll", handleScroll);
|
199
|
-
window.removeEventListener("resize", resize);
|
200
|
-
};
|
201
|
-
}
|
202
|
-
}, [scrollRef?.current]);
|
203
|
-
|
204
|
-
useEffect(() => {
|
205
|
-
measureRows();
|
206
|
-
});
|
207
|
-
|
208
|
-
const findRowAtOffset = (targetOffset: number): number => {
|
209
|
-
if (targetOffset <= 0) return 0;
|
210
|
-
if (targetOffset >= listMetrics.totalHeight) return data.length - 1;
|
211
|
-
|
212
|
-
let low = 0;
|
213
|
-
let high = data.length - 1;
|
214
|
-
let lastValid = 0;
|
215
|
-
|
216
|
-
while (low <= high) {
|
217
|
-
const mid = Math.floor((low + high) / 2);
|
218
|
-
const rowStart = rowPositions.get(mid) || 0;
|
219
|
-
|
220
|
-
if (rowStart <= targetOffset) {
|
221
|
-
lastValid = mid;
|
222
|
-
low = mid + 1;
|
223
|
-
} else {
|
224
|
-
high = mid - 1;
|
225
|
-
}
|
226
|
-
}
|
227
|
-
return lastValid;
|
228
|
-
};
|
229
|
-
|
230
|
-
const firstVisibleIdx = findRowAtOffset(offset);
|
231
|
-
const lastVisibleIdx = findRowAtOffset(offset + height);
|
232
|
-
|
233
|
-
const start = Math.max(0, firstVisibleIdx - overscanCount);
|
234
|
-
const end = Math.min(data.length, lastVisibleIdx + overscanCount);
|
235
|
-
|
236
|
-
const renderedRows = useMemo(() => {
|
237
|
-
const selection = data.slice(start, end);
|
238
|
-
return selection.map((item, index) => {
|
239
|
-
const actualIndex = start + index;
|
240
|
-
return (
|
241
|
-
<div
|
242
|
-
key={`list-item-${actualIndex}`}
|
243
|
-
ref={(el) => {
|
244
|
-
if (el) {
|
245
|
-
rowRefs.current.set(actualIndex, el);
|
246
|
-
} else {
|
247
|
-
rowRefs.current.delete(actualIndex);
|
248
|
-
}
|
249
|
-
}}
|
250
|
-
>
|
251
|
-
{renderRow(item, actualIndex)}
|
252
|
-
</div>
|
253
|
-
);
|
254
|
-
});
|
255
|
-
}, [data, start, end, renderRow]);
|
256
|
-
|
257
|
-
const top = rowPositions.get(start) || 0;
|
258
|
-
const scrollProps = scrollRef ? {} : { onScroll: handleScroll };
|
259
|
-
|
260
|
-
return (
|
261
|
-
<div ref={baseRef} {...props} {...scrollProps} onKeyDown={onKeyDown}>
|
262
|
-
<div
|
263
|
-
className={clsx(
|
264
|
-
styles.container,
|
265
|
-
!scrollRef?.current ? styles.hidden : undefined,
|
266
|
-
)}
|
267
|
-
style={{ height: `${listMetrics.totalHeight}px` }}
|
268
|
-
>
|
269
|
-
<div
|
270
|
-
className={styles.content}
|
271
|
-
style={{ transform: `translateY(${top}px)` }}
|
272
|
-
ref={containerRef}
|
273
|
-
>
|
274
|
-
{renderedRows}
|
275
|
-
</div>
|
276
|
-
</div>
|
277
|
-
</div>
|
278
|
-
);
|
279
|
-
}) as <T>(
|
280
|
-
props: VirtualListProps<T> & { ref?: React.Ref<VirtualListRef> },
|
281
|
-
) => React.ReactElement;
|
282
|
-
|
283
|
-
const throttle = (func: (...args: any[]) => void, limit: number) => {
|
284
|
-
let inThrottle: boolean;
|
285
|
-
return function (this: any, ...args: any[]) {
|
286
|
-
if (!inThrottle) {
|
287
|
-
func.apply(this, args);
|
288
|
-
inThrottle = true;
|
289
|
-
setTimeout(() => (inThrottle = false), limit);
|
290
|
-
}
|
291
|
-
};
|
292
|
-
};
|