Skip to content

Commit c7147e6

Browse files
committed
docs(snippets): update code block labels and imports
1 parent 67afb37 commit c7147e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+228
-138
lines changed

.github/scripts/code_samples.config.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
"variants": {
33
"post /invocations": [
44
{ "name": "async", "overrides": { "async": true } }
5+
],
6+
"post /browsers": [
7+
{ "name": "timeout", "overrides": { "timeout_seconds": 300 } },
8+
{ "name": "live-view", "overrides": { "log": "browser.browser_live_view_url" } }
59
]
610
}
711
}

.github/scripts/generate_code_samples.ts

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ function normalizeLang(input: string): SupportedLanguage {
2424
}
2525

2626
function renderFenceInfo(lang: SupportedLanguage, rawLang: string): string {
27-
if (lang === "typescript" || lang === "javascript") return "typescript index.ts";
28-
if (lang === "python") return "python main.py";
27+
if (lang === "typescript" || lang === "javascript") return "typescript Typescript/Javascript";
28+
if (lang === "python") return "python Python";
2929
return `${lang} ${toTitleCase(rawLang)}`;
3030
}
3131

@@ -92,6 +92,10 @@ function injectParamsIntoObjectLiteral(
9292
}
9393
}
9494
}
95+
// For JS/TS single-line objects, remove any trailing comma before the closing brace
96+
if (lang !== "python") {
97+
updated = updated.replace(/,\s*}/, " }");
98+
}
9599
return updated;
96100
}
97101

@@ -136,29 +140,67 @@ function applyOverridesToSource(
136140
overrides?: Record<string, unknown>
137141
): string {
138142
if (!overrides || Object.keys(overrides).length === 0) return src;
139-
// Heuristic: find the first object literal in a create(...) call and inject params
140-
const createCall = /(invocations\.(create|new)\()([\s\S]*?)(\))/m;
141-
const match = src.match(createCall);
142-
if (!match) return src;
143-
const before = src.slice(0, match.index ?? 0);
144-
const after = src.slice((match.index ?? 0) + match[0].length);
145-
const inside = match[3] ?? "";
146-
const objectMatch = inside.match(/\{[\s\S]*?\}/m);
147-
if (objectMatch) {
148-
const objBefore = inside.slice(0, objectMatch.index!);
149-
const obj = objectMatch[0];
150-
const objAfter = inside.slice((objectMatch.index || 0) + objectMatch[0].length);
151-
const injected = injectParamsIntoObjectLiteral(lang, obj, overrides);
152-
const newInside = objBefore + injected + objAfter;
153-
return before + match[1] + newInside + match[4] + after;
143+
let transformed = src;
144+
145+
// Heuristic: find the first object literal in a X.create(...) call and inject params (excluding special keys like 'log')
146+
const { log: _logOverride, ...injectionOverrides } = overrides as Record<string, unknown>;
147+
if (Object.keys(injectionOverrides).length > 0) {
148+
const createCall = /(\b\w+\.(create|new)\()([\s\S]*?)(\))/m;
149+
const match = transformed.match(createCall);
150+
if (match) {
151+
const before = transformed.slice(0, match.index ?? 0);
152+
const after = transformed.slice((match.index ?? 0) + match[0].length);
153+
const inside = match[3] ?? "";
154+
const objectMatch = inside.match(/\{[\s\S]*?\}/m);
155+
if (objectMatch) {
156+
const objBefore = inside.slice(0, objectMatch.index!);
157+
const obj = objectMatch[0];
158+
const objAfter = inside.slice((objectMatch.index || 0) + objectMatch[0].length);
159+
const injected = injectParamsIntoObjectLiteral(lang, obj, injectionOverrides);
160+
const newInside = objBefore + injected + objAfter;
161+
transformed = before + match[1] + newInside + match[4] + after;
162+
} else if (lang === "python") {
163+
const injectedArgs = injectParamsIntoPythonArgs(inside, injectionOverrides);
164+
const needsNewlineBeforeParen = /\n/.test(injectedArgs) && !/\n\s*$/.test(injectedArgs);
165+
const joiner = needsNewlineBeforeParen ? "\n" : "";
166+
transformed = before + match[1] + injectedArgs + joiner + match[4] + after;
167+
} else {
168+
// JS/TS with no object literal: insert one and inject
169+
const injectedObj = injectParamsIntoObjectLiteral(lang, "{}", injectionOverrides);
170+
transformed = before + match[1] + injectedObj + match[4] + after;
171+
}
172+
}
154173
}
155-
if (lang === "python") {
156-
const injectedArgs = injectParamsIntoPythonArgs(inside, overrides);
157-
const needsNewlineBeforeParen = /\n/.test(injectedArgs) && !/\n\s*$/.test(injectedArgs);
158-
const joiner = needsNewlineBeforeParen ? "\n" : "";
159-
return before + match[1] + injectedArgs + joiner + match[4] + after;
174+
175+
// Apply log override if present: replace argument of the last console.log/print call
176+
const logExpr = (overrides as Record<string, unknown>)?.log;
177+
if (typeof logExpr === "string" && logExpr.trim().length > 0) {
178+
if (lang === "python") {
179+
const regex = /print\(([^\)]*)\)/g;
180+
let lastMatch: RegExpExecArray | null = null;
181+
let m: RegExpExecArray | null;
182+
while ((m = regex.exec(transformed)) !== null) lastMatch = m;
183+
if (lastMatch) {
184+
const start = lastMatch.index;
185+
const end = start + lastMatch[0].length;
186+
transformed =
187+
transformed.slice(0, start) + `print(${logExpr})` + transformed.slice(end);
188+
}
189+
} else {
190+
const regex = /console\.log\(([^\)]*)\)/g;
191+
let lastMatch: RegExpExecArray | null = null;
192+
let m: RegExpExecArray | null;
193+
while ((m = regex.exec(transformed)) !== null) lastMatch = m;
194+
if (lastMatch) {
195+
const start = lastMatch.index;
196+
const end = start + lastMatch[0].length;
197+
transformed =
198+
transformed.slice(0, start) + `console.log(${logExpr})` + transformed.slice(end);
199+
}
200+
}
160201
}
161-
return src;
202+
203+
return transformed;
162204
}
163205

164206
function parseOverridesString(raw: string): Record<string, unknown> | undefined {
@@ -318,7 +360,7 @@ function slugifyEndpoint(method: string, endpoint: string): string {
318360
async function writeSnippetFile(filePath: string, blocks: string) {
319361
const dir = path.dirname(filePath);
320362
await mkdir(dir, { recursive: true });
321-
const content = `<CodeGroup dropdown>\n${blocks.trim()}\n</CodeGroup>\n`;
363+
const content = `<CodeGroup>\n${blocks.trim()}\n</CodeGroup>\n`;
322364
await writeFile(filePath, content, "utf8");
323365
}
324366

@@ -403,7 +445,7 @@ async function processFile(file: string, spec: any) {
403445
if (inline) overrides = { ...(overrides || {}), ...inline };
404446
}
405447
const blocks = extractCodeSamples(spec, endpoint, method, overrides).trim();
406-
return `<CodeGroup dropdown>\n${blocks}\n</CodeGroup>`;
448+
return `<CodeGroup>\n${blocks}\n</CodeGroup>`;
407449
});
408450

409451
if (changed) {

apps/status.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
title: "Status"
33
---
44

5-
Once you've [deployed](/apps/deploy) an app and invoked it, you can check its status.
6-
75
import GetInvocationStatus from '/snippets/openapi/get-invocations-id.mdx';
86

7+
Once you've [deployed](/apps/deploy) an app and invoked it, you can check its status.
8+
99
<GetInvocationStatus />
1010

1111
<Info>

apps/stop.mdx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
title: "Stopping"
33
---
44

5+
import StopInvocation from '/snippets/openapi/patch-invocations-id.mdx';
6+
57
You can terminate an invocation that's running. You can use this to stop automations or agents stuck in an infinite loop.
68

79
<Info>
@@ -11,10 +13,7 @@ Terminating an invocation also destroys any browsers associated with it.
1113
## Via API
1214
You can also do this via the API:
1315

14-
import StopInvocation from '/snippets/openapi/patch-invocations-id.mdx';
15-
1616
<StopInvocation />
1717

18-
1918
## Via CLI
2019
Use `ctrl-c` in the terminal tab where you launched the invocation.

browsers/create-a-browser.mdx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
title: "Create a Browser"
33
---
44

5+
import CreateBrowserSnippet from '/snippets/openapi/post-browsers.mdx';
6+
import DeleteBrowserSnippet from '/snippets/openapi/delete-browsers-id.mdx';
7+
58
Kernel browsers were designed to be lightweight, fast, and efficient for cloud-based browser automations at scale. They can be used as part of the Kernel [app platform](/apps/develop) or connected to from another service with the Chrome DevTools Protocol.
69

710
## 1. Create a Kernel browser
811

912
Use our SDK to create a browser:
1013

11-
import CreateBrowserSnippet from '/snippets/openapi/post-browsers.mdx';
12-
1314
<CreateBrowserSnippet />
1415

1516
## 2. Connect to the browser with the Chrome DevTools Protocol
@@ -39,8 +40,6 @@ browser = playwright.chromium.connect_over_cdp(kernel_browser.cdp_ws_url)
3940

4041
When you're finished with the browser, you can delete it:
4142

42-
import DeleteBrowserSnippet from '/snippets/openapi/delete-browsers-id.mdx';
43-
4443
<DeleteBrowserSnippet />
4544

4645
You can also delete the browser by [specifiying a timeout](/browsers/termination#timeouts-for-non-persisted-browsers) when you create the browser.

browsers/live-view.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
title: "Live View"
33
---
44

5+
import CreateBrowserForLiveView from '/snippets/openapi/post-browsers-live-view.mdx';
6+
57
Humans-in-the-loop can access the live view of Kernel browsers in real-time to resolve errors or take unscripted actions.
68

79
To access the live view, visit the `browser_live_view_url` provided when you create a Kernel browser:
810

9-
import CreateBrowserForLiveView from '/snippets/openapi/post-browsers.mdx';
10-
1111
<CreateBrowserForLiveView />
1212

1313
## Query parameters

browsers/termination.mdx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22
title: "Termination & Timeouts"
33
---
44

5+
import DeleteBrowser from '/snippets/openapi/delete-browsers-id.mdx';
6+
import CreateBrowserTimeout from '/snippets/openapi/post-browsers-timeout.mdx';
7+
58
Kernel browsers should be terminated after you're done with them.
69

710
## Via API
811

912
You can delete a browser by making a `DELETE` request to Kernel's API:
1013

11-
import DeleteBrowser from '/snippets/openapi/delete-browsers-id.mdx';
12-
1314
<DeleteBrowser />
1415

1516
## Timeouts for non-persisted browsers
@@ -18,6 +19,4 @@ If you don't manually delete a `non-persisted` browser, deletion will happen aut
1819

1920
You can set a custom timeout of up to 24 hours via the API:
2021

21-
import CreateBrowserTimeout from '/snippets/openapi/post-browsers.mdx';
22-
2322
<CreateBrowserTimeout />

snippets/openapi/delete-browsers-id-fs-watch-watch_id.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<CodeGroup dropdown>
2-
```typescript index.ts
1+
<CodeGroup>
2+
```typescript Typescript/Javascript
33
import Kernel from '@onkernel/sdk';
44

55
const client = new Kernel({
@@ -10,7 +10,7 @@ await client.browsers.fs.watch.stop('watch_id', { id: 'id' });
1010
```
1111

1212

13-
```python main.py
13+
```python Python
1414
from kernel import Kernel
1515

1616
client = Kernel(

snippets/openapi/delete-browsers-id.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<CodeGroup dropdown>
2-
```typescript index.ts
1+
<CodeGroup>
2+
```typescript Typescript/Javascript
33
import Kernel from '@onkernel/sdk';
44

55
const client = new Kernel({
@@ -10,7 +10,7 @@ await client.browsers.deleteByID('htzv5orfit78e1m2biiifpbv');
1010
```
1111

1212

13-
```python main.py
13+
```python Python
1414
from kernel import Kernel
1515

1616
client = Kernel(

snippets/openapi/delete-browsers.mdx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<CodeGroup dropdown>
2-
```typescript index.ts
1+
<CodeGroup>
2+
```typescript Typescript/Javascript
33
import Kernel from '@onkernel/sdk';
44

55
const client = new Kernel({
@@ -10,7 +10,7 @@ await client.browsers.delete({ persistent_id: 'persistent_id' });
1010
```
1111

1212

13-
```python main.py
13+
```python Python
1414
from kernel import Kernel
1515

1616
client = Kernel(

0 commit comments

Comments
 (0)