-
Notifications
You must be signed in to change notification settings - Fork 3
Magnitude Template #57
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dprevoznik
merged 1 commit into
main
from
danny/kernel-370-create-kernel-app-add-magnitude-sample-app
Sep 26, 2025
+212
−2
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Kernel TypeScript Magnitude.run Template | ||
|
|
||
| This template demonstrates integrating Magnitude.run with a Kernel app. | ||
|
|
||
| It defines a single action that: | ||
| - Navigates to a given URL (via Magnitude `agent.act()`) | ||
| - Extracts up to 5 absolute URLs on the page (via Magnitude `agent.extract()`) | ||
|
|
||
| ## Quickstart | ||
|
|
||
| - Deploy: | ||
| kernel login # or: export KERNEL_API_KEY=<your_api_key> | ||
| kernel deploy index.ts --env ANTHROPIC_API_KEY=XXX | ||
|
|
||
| - Invoke: | ||
| kernel invoke ts-magnitude mag-url-extract --payload '{"url": "https://fandom.com"}' | ||
|
|
||
| ## Notes | ||
| - Uses Anthropic as the model provider with model: `anthropic/claude-sonnet-4`. | ||
| - Requires `ANTHROPIC_API_KEY` in the deployment environment. | ||
| - The agent connects to the Kernel-managed browser via CDP for live view & observability. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # Dependencies | ||
| node_modules/ | ||
| package-lock.json | ||
|
|
||
| # TypeScript | ||
| *.tsbuildinfo | ||
| dist/ | ||
| build/ | ||
|
|
||
| # Environment | ||
| .env | ||
| .env.local | ||
| .env.*.local | ||
|
|
||
| # IDE | ||
| .vscode/ | ||
| .idea/ | ||
| *.swp | ||
| *.swo | ||
|
|
||
| # OS | ||
| .DS_Store | ||
| Thumbs.db | ||
|
|
||
| # Logs | ||
| logs/ | ||
| *.log | ||
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
|
|
||
| # Testing | ||
| coverage/ | ||
| .nyc_output/ | ||
|
|
||
| # Misc | ||
| .cache/ | ||
| .temp/ | ||
| .tmp/ | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { Kernel, type KernelContext } from "@onkernel/sdk"; | ||
| import { startBrowserAgent } from "magnitude-core"; | ||
| import { z } from "zod"; | ||
|
|
||
| const kernel = new Kernel(); | ||
|
|
||
| const app = kernel.app("ts-magnitude"); | ||
|
|
||
| interface UrlInput { | ||
| url: string; | ||
| } | ||
|
|
||
| interface UrlsOutput { | ||
| urls: string[]; | ||
| } | ||
|
|
||
| // Use env var + inline model directly in the agent call. | ||
|
|
||
| app.action<UrlInput, UrlsOutput>( | ||
| "mag-url-extract", | ||
| async (ctx: KernelContext, payload?: UrlInput): Promise<UrlsOutput> => { | ||
| if (!payload?.url) { | ||
| throw new Error("URL is required"); | ||
| } | ||
|
|
||
| let target = payload.url; | ||
| if (!target.startsWith("http://") && !target.startsWith("https://")) { | ||
| target = `https://${target}`; | ||
| } | ||
| try { | ||
| // Validate URL | ||
| new URL(target); | ||
| } catch { | ||
| throw new Error(`Invalid URL: ${target}`); | ||
| } | ||
|
|
||
| // Create a Kernel browser so we get a live view and observability | ||
| const kernelBrowser = await kernel.browsers.create({ | ||
| invocation_id: ctx.invocation_id, | ||
| stealth: true, | ||
| }); | ||
|
|
||
| console.log( | ||
| "Kernel browser live view url:", | ||
| kernelBrowser.browser_live_view_url | ||
| ); | ||
|
|
||
| // Start a Magnitude BrowserAgent connected to the Kernel browser via CDP | ||
| const agent = await startBrowserAgent({ | ||
| narrate: true, | ||
| url: target, | ||
| llm: { | ||
| provider: "anthropic", | ||
| options: { | ||
| model: "claude-sonnet-4-20250514", | ||
| apiKey: process.env.ANTHROPIC_API_KEY as string, | ||
| }, | ||
| }, | ||
| browser: { | ||
| cdp: kernelBrowser.cdp_ws_url, | ||
| contextOptions: { | ||
| viewport: { width: 1024, height: 768 } | ||
| } | ||
| }, | ||
| virtualScreenDimensions: { width: 1024, height: 768 } | ||
| }); | ||
|
|
||
| try { | ||
| ////////////////////////////////////// | ||
| // Navigate and extract via Magnitude | ||
| ////////////////////////////////////// | ||
| await agent.act( | ||
| `Go to ${target}. Explore the page by scrolling down the page twice. Narrate key steps.` | ||
| ); | ||
|
|
||
| const urls = await agent.extract( | ||
| "Extract up to 5 absolute URLs on the current page", | ||
| z.array(z.string().url()).describe("List of absolute URLs") | ||
| ); | ||
|
|
||
| return { urls }; | ||
| } finally { | ||
| try { | ||
| await agent.stop(); | ||
| } catch (e) { | ||
| console.warn("Warning: failed to stop agent", e); | ||
| } | ||
| try { | ||
| await kernel.invocations.deleteBrowsers(ctx.invocation_id); | ||
| console.log(`Browsers for invocation ${ctx.invocation_id} cleaned up successfully`); | ||
| } catch (e) { | ||
| console.warn( | ||
| `Warning: failed to clean up browsers for ${ctx.invocation_id}`, | ||
| e | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| ); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| { | ||
| "name": "ts-magnitude", | ||
| "module": "index.ts", | ||
| "type": "module", | ||
| "private": true, | ||
| "peerDependencies": { | ||
| "typescript": "^5" | ||
| }, | ||
| "dependencies": { | ||
| "@onkernel/sdk": ">=0.11.0", | ||
| "magnitude-core": "latest", | ||
| "zod": "^3.25.0" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "lib": ["ESNext", "DOM"], | ||
| "target": "ESNext", | ||
| "module": "ESNext", | ||
| "moduleDetection": "force", | ||
| "jsx": "react-jsx", | ||
| "allowJs": true, | ||
| "moduleResolution": "bundler", | ||
| "allowImportingTsExtensions": true, | ||
| "verbatimModuleSyntax": true, | ||
| "noEmit": true, | ||
| "strict": true, | ||
| "skipLibCheck": true, | ||
| "noFallthroughCasesInSwitch": true, | ||
| "noUncheckedIndexedAccess": true, | ||
| "noUnusedLocals": false, | ||
| "noUnusedParameters": false, | ||
| "noPropertyAccessFromIndexSignature": false | ||
| }, | ||
| "include": ["./**/*.ts", "./**/*.tsx"], | ||
| "exclude": ["node_modules", "dist"] | ||
| } | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this should be in the payload since that is most likely how people will use web agents? This could be a more common patter, not entirely sure though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I set it up this way so someone can quickly see Magnitude’s benefits without needing a specific task that highlights each feature (e.g. Extract URLs, scroll, etc.). I agree this isn’t the most common pattern or likely a workflow users would have, but it shows off the benefits well IMO and it’s easy to overwrite if needed.