Skip to content

Commit ad61ef9

Browse files
committed
docs: add Go SDK examples
1 parent feb6ed6 commit ad61ef9

38 files changed

Lines changed: 3481 additions & 31 deletions

.docs/code-example-guide.md

Lines changed: 117 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ This guide defines the standards for code examples across the Kernel documentati
44

55
## General Principles
66

7-
1. **Complete and runnable**: Every code example should be complete enough to run as-is
7+
1. **Complete and runnable**: Every code example must be complete enough to run as-is
88
2. **Consistent naming**: Use standardized variable names across all examples
99
3. **No hardcoded credentials**: Never include API keys or credentials in examples
10-
4. **Multi-language support**: When applicable, show both TypeScript/JavaScript and Python examples
10+
4. **Multi-language support**: When applicable, show TypeScript/JavaScript, Python, and Go examples
1111

1212
## Variable Naming Conventions
1313

@@ -27,6 +27,14 @@ This guide defines the standards for code examples across the Kernel documentati
2727
- Context: `context`
2828
- Page: `page`
2929

30+
### Go
31+
- SDK client: `client`
32+
- Browser instance: `kernelBrowser`
33+
- Additional browsers: `kernelBrowser2`, etc.
34+
- Context: `ctx`
35+
- Session ID: `sessionID`
36+
- Invocation ID: `invocationID`
37+
3038
## Code Example Structure
3139

3240
### Minimal Example (Browser Creation)
@@ -55,6 +63,29 @@ kernel = Kernel()
5563
kernel_browser = kernel.browsers.create()
5664
print(kernel_browser.session_id)
5765
```
66+
67+
```go Go
68+
package main
69+
70+
import (
71+
"context"
72+
"fmt"
73+
74+
"github.com/kernel/kernel-go-sdk"
75+
)
76+
77+
func main() {
78+
ctx := context.Background()
79+
client := kernel.NewClient()
80+
81+
kernelBrowser, err := client.Browsers.New(ctx, kernel.BrowserNewParams{})
82+
if err != nil {
83+
panic(err)
84+
}
85+
86+
fmt.Println(kernelBrowser.SessionID)
87+
}
88+
```
5889
</CodeGroup>
5990

6091
### Full Example (With Browser Automation)
@@ -129,6 +160,16 @@ const kernel = new Kernel();
129160
kernel = Kernel()
130161
```
131162

163+
```go
164+
package main
165+
166+
import "github.com/kernel/kernel-go-sdk"
167+
168+
func main() {
169+
_ = kernel.NewClient()
170+
}
171+
```
172+
132173
The SDK automatically reads the API key from the `KERNEL_API_KEY` environment variable.
133174

134175
### ❌ Incorrect - Hardcoded credentials
@@ -145,6 +186,22 @@ kernel = Kernel(api_key="your-api-key")
145186
kernel = Kernel(api_key=os.getenv("KERNEL_API_KEY"))
146187
```
147188

189+
```go
190+
// DON'T DO THIS
191+
package main
192+
193+
import (
194+
"github.com/kernel/kernel-go-sdk"
195+
"github.com/kernel/kernel-go-sdk/option"
196+
)
197+
198+
func main() {
199+
_ = kernel.NewClient(
200+
option.WithAPIKey("your-api-key"),
201+
)
202+
}
203+
```
204+
148205
## Feature-Specific Examples
149206

150207
### Simple Feature Toggle
@@ -171,6 +228,28 @@ kernel_browser = kernel.browsers.create(
171228
stealth=True,
172229
)
173230
```
231+
232+
```go Go
233+
package main
234+
235+
import (
236+
"context"
237+
238+
"github.com/kernel/kernel-go-sdk"
239+
)
240+
241+
func main() {
242+
ctx := context.Background()
243+
client := kernel.NewClient()
244+
245+
_, err := client.Browsers.New(ctx, kernel.BrowserNewParams{
246+
Stealth: kernel.Bool(true),
247+
})
248+
if err != nil {
249+
panic(err)
250+
}
251+
}
252+
```
174253
</CodeGroup>
175254

176255
### Feature with Configuration
@@ -197,10 +276,36 @@ kernel_browser = kernel.browsers.create(
197276
stealth=True
198277
)
199278
```
279+
280+
```go Go
281+
package main
282+
283+
import (
284+
"context"
285+
286+
"github.com/kernel/kernel-go-sdk"
287+
)
288+
289+
func main() {
290+
ctx := context.Background()
291+
client := kernel.NewClient()
292+
293+
kernelBrowser, err := client.Browsers.New(ctx, kernel.BrowserNewParams{
294+
Stealth: kernel.Bool(true),
295+
})
296+
if err != nil {
297+
panic(err)
298+
}
299+
300+
_ = kernelBrowser
301+
}
302+
```
200303
</CodeGroup>
201304

202305
## App Development Examples
203306

307+
Kernel app examples currently use TypeScript/JavaScript and Python. Add a Go version only after the Go SDK has documented app framework support and the snippet has been tested against that SDK.
308+
204309
For Kernel app examples, follow this pattern:
205310

206311
<CodeGroup>
@@ -299,17 +404,19 @@ finally:
299404
### Indentation
300405
- TypeScript/JavaScript: 2 spaces
301406
- Python: 4 spaces
407+
- Go: tabs from `gofmt`
302408

303409
### String Quotes
304410
- TypeScript/JavaScript: Single quotes `'` (except for avoiding escaping)
305411
- Python: Double quotes `"`
412+
- Go: Double quotes `"`
306413

307414
### Line Length
308415
- Keep lines under 100 characters when possible
309416
- Break long parameter lists across multiple lines
310417

311418
### Comments
312-
- Use comments sparingly - code should be self-explanatory
419+
- Use comments sparingly; keep code self-explanatory
313420
- Add comments only for non-obvious logic or important context
314421
- Never add comments like "NEW CODE:" or similar meta-comments
315422

@@ -340,6 +447,10 @@ Always use `<CodeGroup>` with proper language labels:
340447
```python Python
341448
# Python code here
342449
```
450+
451+
```go Go
452+
// Go code here
453+
```
343454
</CodeGroup>
344455
````
345456

@@ -354,7 +465,9 @@ Before publishing a code example, verify:
354465
- [ ] Includes error handling (for full examples)
355466
- [ ] Includes cleanup code (for full examples)
356467
- [ ] Uses proper indentation and formatting
357-
- [ ] Both TypeScript and Python versions are provided (when applicable)
468+
- [ ] TypeScript/JavaScript, Python, and Go versions are provided (when applicable)
469+
- [ ] Go examples are formatted with `gofmt`
470+
- [ ] Go examples are tested against the actual Go SDK before publishing
358471
- [ ] Code has been tested or follows proven patterns
359472

360473
## Reference
@@ -363,4 +476,3 @@ See these files for examples:
363476
- `introduction/create.mdx` - Standard browser creation pattern
364477
- `apps/develop.mdx` - App development pattern
365478
- `browsers/file-io.mdx` - Complex automation example
366-

README.md

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,13 @@ This is the documentation for the Kernel platform. It's connected to [onkernel.c
1111

1212
## Code Snippets
1313

14-
Code samples in the docs are generated from our OpenAPI spec so the examples stay in sync with the API. There are two ways the generator is invoked:
14+
Code samples in the docs are authored inline in MDX. Keep SDK examples aligned with the SDK repos, and follow the standards in `.docs/code-example-guide.md` when adding or editing examples.
1515

16-
- Custom MDX tag: use `<OpenAPICodeGroup>get /api/v1/users</OpenAPICodeGroup>` (or omit the verb to use the default behavior).
16+
When you add Go examples:
1717

18-
How the generator works (current behavior):
19-
20-
- The script is `.github/scripts/generate_code_samples.ts` and is executed with Bun. It fetches the OpenAPI spec from the URL configured at the top of that script.
21-
- It reads `x-codeSamples` entries for each operation and extracts samples for TypeScript/JavaScript and Python. Samples are normalized and may be transformed by simple "overrides" (see the script for the override parsing and injection heuristics).
22-
- It writes snippet files under `snippets/openapi/` as MDX files containing a `<CodeGroup>` with the generated code fences. It also updates any MDX files under `apps/` and `browsers/` that contain the inline or tag forms by replacing them with the generated `<CodeGroup>` blocks.
23-
- The generator can produce a base snippet and additional variant snippets controlled by `.github/scripts/code_samples.config.json` (variants are keyed by `"method /path"`).
24-
- The script also removes stale snippet files matching the `-.mdx` suffix pattern.
25-
26-
How it affects the repository:
27-
28-
- New or updated files are created under `snippets/openapi/*.mdx`.
29-
- MDX pages in `apps/` and `browsers/` may be modified in-place to replace `<OpenAPICodeGroup>`/mustache tags with generated `<CodeGroup>` blocks.
30-
- A GitHub Action (`.github/workflows/generate_code_snippets.yaml`) runs the script on push (except to `main`), commits any changes, and pushes them to the `gh_action_generated_docs` branch so Mintlify can deploy the generated docs.
31-
32-
Notes and gotchas:
33-
34-
- The generator runs remotely against the OpenAPI URL defined in the script, so it needs network access and the spec to include `x-codeSamples` for useful output.
35-
- The override parsing and code injection are heuristic. Complex sample sources might not be transformed exactly as intended. See the script for details on how keys/`log` overrides are applied.
36-
- The GitHub Action installs Bun and runs the script; if you run it locally, install Bun and run `bun run .github/scripts/generate_code_samples.ts` from the repo root.
37-
38-
Example: to add a snippet placeholder to a page, add `<OpenAPICodeGroup>get /api/v1/users</OpenAPICodeGroup>` and let the generator fill `snippets/openapi` and update the page during the next run.
18+
- Test the snippet against the actual Go SDK, usually from a sibling `../kernel-go-sdk` checkout.
19+
- Run `gofmt` on the extracted Go code before publishing.
20+
- Note the validation you ran in the pull request description.
3921

4022
## Local Development
4123

apps/invoke.mdx

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,33 @@ invocation = kernel.invocations.create(
3434
)
3535
print(invocation.id)
3636
```
37+
38+
```go Go
39+
package main
40+
41+
import (
42+
"context"
43+
"fmt"
44+
45+
"github.com/kernel/kernel-go-sdk"
46+
)
47+
48+
func main() {
49+
ctx := context.Background()
50+
client := kernel.NewClient()
51+
52+
invocation, err := client.Invocations.New(ctx, kernel.InvocationNewParams{
53+
ActionName: "analyze",
54+
AppName: "my-app",
55+
Version: "1.0.0",
56+
})
57+
if err != nil {
58+
panic(err)
59+
}
60+
61+
fmt.Println(invocation.ID)
62+
}
63+
```
3764
</CodeGroup>
3865

3966
### Asynchronous invocations
@@ -70,6 +97,34 @@ invocation = kernel.invocations.create(
7097
)
7198
print(invocation.id)
7299
```
100+
101+
```go Go
102+
package main
103+
104+
import (
105+
"context"
106+
"fmt"
107+
108+
"github.com/kernel/kernel-go-sdk"
109+
)
110+
111+
func main() {
112+
ctx := context.Background()
113+
client := kernel.NewClient()
114+
115+
invocation, err := client.Invocations.New(ctx, kernel.InvocationNewParams{
116+
Async: kernel.Bool(true),
117+
ActionName: "analyze",
118+
AppName: "my-app",
119+
Version: "1.0.0",
120+
})
121+
if err != nil {
122+
panic(err)
123+
}
124+
125+
fmt.Println(invocation.ID)
126+
}
127+
```
73128
</CodeGroup>
74129

75130
## Via CLI

apps/logs.mdx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ from kernel import Kernel
2121
kernel = Kernel()
2222
logs = kernel.invocations.follow(invocation_id)
2323
```
24+
25+
```go Go
26+
package main
27+
28+
import (
29+
"context"
30+
"fmt"
31+
32+
"github.com/kernel/kernel-go-sdk"
33+
)
34+
35+
func main() {
36+
ctx := context.Background()
37+
client := kernel.NewClient()
38+
39+
logs := client.Invocations.FollowStreaming(ctx, "inv_123", kernel.InvocationFollowParams{})
40+
defer logs.Close()
41+
42+
for logs.Next() {
43+
event := logs.Current()
44+
if event.Event == "log" {
45+
fmt.Println(event.Message)
46+
}
47+
}
48+
if err := logs.Err(); err != nil {
49+
panic(err)
50+
}
51+
}
52+
```
2453
</CodeGroup>
2554

2655
<Info>Log lines will be truncated to 64KiB. For large payloads write data to external storage and log a reference instead.</Info>

0 commit comments

Comments
 (0)