Repository navigation
Expand file tree
/
Copy pathindex.html
More file actions
204 lines (190 loc) · 5.4 KB
/
Copy pathindex.html
File metadata and controls
204 lines (190 loc) · 5.4 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Droid SDK browser examples</title>
<style>
body {
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
max-width: 64rem;
margin: 2rem;
line-height: 1.5;
}
form {
display: grid;
gap: 1rem;
margin-block: 2rem;
}
label {
display: grid;
gap: 0.25rem;
}
input {
box-sizing: border-box;
font: inherit;
max-width: 48rem;
padding: 0.5rem;
width: 100%;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.75rem;
}
button {
font: inherit;
padding: 0.5rem 0.75rem;
}
pre {
background: #f4f4f5;
padding: 0.75rem;
overflow-x: auto;
white-space: pre-wrap;
}
#error:empty {
display: none;
}
#error {
background: #fee2e2;
padding: 0.75rem;
white-space: pre-wrap;
}
#output:empty::before {
color: #71717a;
content: 'Choose an example to run.';
}
</style>
</head>
<body>
<h1>Droid SDK browser examples</h1>
<p>
Start the Droid daemon separately, enter your local connection details,
then choose an example. Values stay in this page's memory and are not
stored or sent to the example server.
</p>
<form id="launcher">
<label>
Daemon WebSocket URL
<input
id="daemon-url"
name="daemonUrl"
type="url"
value="ws://127.0.0.1:37643"
required
/>
</label>
<label>
Factory API key
<input
id="api-key"
name="apiKey"
type="password"
autocomplete="off"
spellcheck="false"
placeholder="fk-..."
required
/>
</label>
<label>
Session working directory
<input
id="cwd"
name="cwd"
type="text"
autocomplete="off"
spellcheck="false"
placeholder="/absolute/path/to/a/repo"
/>
</label>
<div class="actions">
<button type="button" data-example="daemon-session" data-needs-cwd>
Run daemon session
</button>
<button
type="button"
data-example="daemon-multi-session"
data-needs-cwd
>
Run two sessions
</button>
<button type="button" data-example="daemon-lifecycle" data-needs-cwd>
Run lifecycle operations
</button>
<button type="button" data-example="list-sessions">
List sessions
</button>
</div>
</form>
<div id="error"></div>
<h2>Output</h2>
<div id="output"></div>
<script type="module">
const form = document.querySelector('#launcher');
const daemonUrlInput = document.querySelector('#daemon-url');
const apiKeyInput = document.querySelector('#api-key');
const cwdInput = document.querySelector('#cwd');
const errorBox = document.querySelector('#error');
const output = document.querySelector('#output');
const buttons = [...form.querySelectorAll('[data-example]')];
const showError = (err) => {
errorBox.textContent = String(
(err && (err.stack || err.message)) || err
);
};
window.addEventListener('error', (e) => showError(e.error ?? e.message));
window.addEventListener('unhandledrejection', (e) => showError(e.reason));
const setRunning = (running) => {
for (const button of buttons) button.disabled = running;
};
const renderRunning = (example) => {
const entry = document.createElement('pre');
entry.textContent = `Running ${example}…`;
output.replaceChildren(entry);
};
const isWebSocketUrl = (value) => {
try {
return ['ws:', 'wss:'].includes(new URL(value).protocol);
} catch {
return false;
}
};
const isAbsolutePath = (value) =>
value.startsWith('/') ||
/^[A-Za-z]:[\\/]/.test(value) ||
value.startsWith('\\\\');
const runExample = async (button) => {
errorBox.textContent = '';
const daemonUrl = daemonUrlInput.value.trim();
const apiKey = apiKeyInput.value;
const cwd = cwdInput.value.trim();
const needsCwd = button.hasAttribute('data-needs-cwd');
daemonUrlInput.setCustomValidity(
isWebSocketUrl(daemonUrl) ? '' : 'Enter a ws:// or wss:// daemon URL.'
);
cwdInput.setCustomValidity(
!needsCwd || isAbsolutePath(cwd)
? ''
: 'Enter an absolute working directory for this example.'
);
if (!form.reportValidity()) {
return;
}
const example = button.dataset.example;
globalThis.__DROID_EXAMPLE_CONFIG__ = { daemonUrl, apiKey, cwd };
renderRunning(example);
setRunning(true);
try {
await import(`./${example}.js?run=${Date.now()}`);
} catch (error) {
showError(error);
} finally {
delete globalThis.__DROID_EXAMPLE_CONFIG__;
setRunning(false);
}
};
for (const button of buttons) {
button.addEventListener('click', () => void runExample(button));
}
</script>
</body>
</html>