-
Notifications
You must be signed in to change notification settings - Fork 7
Add Tzafon Northstar computer use template #134
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
Open
mttias
wants to merge
2
commits into
kernel:main
Choose a base branch
from
mttias:tzafon-computer-use
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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 @@ | ||
| TZAFON_API_KEY=your-tzafon-api-key |
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,60 @@ | ||
| # Kernel Python Sample App - Tzafon Northstar Computer Use | ||
|
|
||
| This is a Kernel application that implements a CUA (computer use agent) loop using Tzafon's Northstar CUA Fast model with Kernel's Computer Controls API. The model is accessed via Tzafon's [Lightcone](https://docs.lightcone.ai) API platform. | ||
|
|
||
| [Northstar CUA Fast](https://docs.lightcone.ai) is a 4B-parameter vision model trained with reinforcement learning for computer use tasks. | ||
|
|
||
| ## Setup | ||
|
|
||
| 1. Get your API keys: | ||
| - **Kernel**: [dashboard.onkernel.com](https://dashboard.onkernel.com) | ||
| - **Tzafon**: [tzafon.ai](https://www.tzafon.ai) | ||
|
|
||
| 2. Deploy the app: | ||
| ```bash | ||
| kernel login | ||
| cp .env.example .env # Add your TZAFON_API_KEY | ||
| kernel deploy main.py --env-file .env | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| kernel invoke python-tzafon-cua cua-task --payload '{"query": "Go to wikipedia.org and search for Alan Turing"}' | ||
| ``` | ||
|
|
||
| ## Recording Replays | ||
|
|
||
| > **Note:** Replay recording is only available to Kernel users on paid plans. | ||
|
|
||
| Add `"record_replay": true` to your payload to capture a video of the browser session: | ||
|
|
||
| ```bash | ||
| kernel invoke python-tzafon-cua cua-task --payload '{"query": "Navigate to https://example.com", "record_replay": true}' | ||
| ``` | ||
|
|
||
| When enabled, the response will include a `replay_url` field with a link to view the recorded session. | ||
|
|
||
| ## Viewport Configuration | ||
|
|
||
| Northstar CUA Fast works well with a **1280x800** viewport, which is the default. | ||
|
|
||
| ## Supported Actions | ||
|
|
||
| | Action | Description | | ||
| |--------|-------------| | ||
| | `click` | Left mouse click at coordinates | | ||
| | `double_click` | Double-click at coordinates | | ||
| | `triple_click` | Triple-click at coordinates | | ||
| | `right_click` | Right mouse click at coordinates | | ||
| | `scroll` | Scroll page vertically or horizontally | | ||
| | `type` | Type text into focused element | | ||
| | `key` / `keypress` | Send keyboard input | | ||
| | `navigate` | Navigate to a URL | | ||
| | `drag` | Click-and-drag operation | | ||
| | `wait` | Pause for UI to update | | ||
|
|
||
| ## Resources | ||
|
|
||
| - [Lightcone API Documentation](https://docs.lightcone.ai) | ||
| - [Kernel Documentation](https://www.kernel.sh/docs/quickstart) |
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,7 @@ | ||
| __pycache__/ | ||
| *.py[cod] | ||
| *$py.class | ||
| .env | ||
| *.log | ||
| .venv/ | ||
| venv/ |
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,124 @@ | ||
| """ | ||
| Tzafon Northstar Sampling Loop | ||
|
|
||
| Implements the agent loop for Tzafon's Northstar CUA Fast model. | ||
| Northstar uses the Lightcone SDK with a responses API: | ||
| - Actions are returned via computer_call outputs | ||
| - Tool results use computer_call_output with screenshot images | ||
| - The model stops when no computer_call is in the output or the action is terminal | ||
| - Continuation uses previous_response_id for multi-turn context | ||
|
|
||
| @see https://docs.lightcone.ai | ||
| """ | ||
|
|
||
| import asyncio | ||
| from typing import Any, Optional | ||
|
|
||
| from kernel import Kernel | ||
| from tzafon import Lightcone | ||
|
|
||
| from tools import ComputerTool | ||
|
|
||
| MODEL = "tzafon.northstar-cua-fast" | ||
|
|
||
| TOOL = { | ||
| "type": "computer_use", | ||
| "display_width": 1280, | ||
| "display_height": 800, | ||
| "environment": "browser", | ||
| } | ||
|
|
||
| TERMINAL_ACTIONS = {"terminate", "done", "answer"} | ||
|
|
||
|
|
||
| async def sampling_loop( | ||
| *, | ||
| task: str, | ||
| api_key: str, | ||
| kernel: Kernel, | ||
| session_id: str, | ||
| model: str = MODEL, | ||
| max_steps: int = 50, | ||
| viewport_width: int = 1280, | ||
| viewport_height: int = 800, | ||
| ) -> dict[str, Any]: | ||
| """Run the Northstar CUA loop until the model terminates or max steps are reached.""" | ||
| tzafon = Lightcone(api_key=api_key) | ||
| computer = ComputerTool(kernel, session_id, viewport_width, viewport_height) | ||
|
|
||
| tool = { | ||
| **TOOL, | ||
| "display_width": viewport_width, | ||
| "display_height": viewport_height, | ||
| } | ||
|
|
||
| screenshot_url = computer.capture_screenshot() | ||
|
|
||
| response = tzafon.responses.create( | ||
| model=model, | ||
| tools=[tool], | ||
| input=[ | ||
| { | ||
| "role": "user", | ||
| "content": [ | ||
| {"type": "input_text", "text": task}, | ||
| {"type": "input_image", "image_url": screenshot_url}, | ||
| ], | ||
| } | ||
| ], | ||
| ) | ||
|
|
||
| final_result: Optional[str] = None | ||
|
|
||
| for step in range(max_steps): | ||
| computer_call = next( | ||
| (o for o in (response.output or []) if o.type == "computer_call"), | ||
| None, | ||
| ) | ||
| if not computer_call: | ||
| break | ||
|
|
||
| action = computer_call.action | ||
| label = action.type | ||
| if action.x is not None: | ||
| label += f" @ ({action.x}, {action.y})" | ||
| if action.text: | ||
| label += f" '{action.text}'" | ||
| print(f"[{step + 1}] {label}") | ||
|
|
||
| if action.type in TERMINAL_ACTIONS: | ||
| final_result = action.result or action.text or action.status | ||
| print(f"Result: {final_result}") | ||
| break | ||
|
|
||
| await computer.execute(action) | ||
| await asyncio.sleep(1) | ||
|
|
||
| screenshot_url = computer.capture_screenshot() | ||
| response = tzafon.responses.create( | ||
| model=model, | ||
| previous_response_id=response.id, | ||
| tools=[tool], | ||
| input=[ | ||
| { | ||
| "type": "computer_call_output", | ||
| "call_id": computer_call.call_id, | ||
| "output": { | ||
| "type": "input_image", | ||
| "image_url": screenshot_url, | ||
| }, | ||
| } | ||
| ], | ||
| ) | ||
|
|
||
| messages: list[str] = [] | ||
| for item in response.output or []: | ||
| if item.type == "message": | ||
| for block in item.content or []: | ||
| if block.text: | ||
| messages.append(block.text) | ||
|
|
||
| return { | ||
| "messages": messages, | ||
| "final_result": final_result, | ||
| } |
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,65 @@ | ||
| import os | ||
| from typing import Optional, TypedDict | ||
|
|
||
| import kernel | ||
| from loop import sampling_loop | ||
| from session import KernelBrowserSession | ||
|
|
||
|
|
||
| class QueryInput(TypedDict): | ||
| query: str | ||
| record_replay: Optional[bool] | ||
|
|
||
|
|
||
| class QueryOutput(TypedDict): | ||
| result: str | ||
| replay_url: Optional[str] | ||
|
|
||
|
|
||
| api_key = os.getenv("TZAFON_API_KEY") | ||
| if not api_key: | ||
| raise ValueError("TZAFON_API_KEY is not set") | ||
|
|
||
| app = kernel.App("python-tzafon-cua") | ||
|
|
||
|
|
||
| @app.action("cua-task") | ||
| async def cua_task( | ||
| ctx: kernel.KernelContext, | ||
| payload: QueryInput, | ||
| ) -> QueryOutput: | ||
| if not payload or not payload.get("query"): | ||
| raise ValueError("Query is required") | ||
|
|
||
| record_replay = payload.get("record_replay", False) | ||
|
|
||
| async with KernelBrowserSession( | ||
| invocation_id=ctx.invocation_id, | ||
| stealth=True, | ||
| record_replay=record_replay, | ||
| ) as session: | ||
| print("Kernel browser live view url:", session.live_view_url) | ||
|
|
||
| loop_result = await sampling_loop( | ||
| task=payload["query"], | ||
| api_key=str(api_key), | ||
| kernel=session.kernel, | ||
| session_id=str(session.session_id), | ||
| viewport_width=session.viewport_width, | ||
| viewport_height=session.viewport_height, | ||
| ) | ||
|
|
||
| final_result = loop_result.get("final_result") | ||
| messages = loop_result.get("messages", []) | ||
|
|
||
| if final_result: | ||
| result = final_result | ||
| elif messages: | ||
| result = messages[-1] | ||
| else: | ||
| result = "Task completed" | ||
|
|
||
| return { | ||
| "result": result, | ||
| "replay_url": session.replay_view_url, | ||
| } |
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,9 @@ | ||
| [project] | ||
| name = "python-tzafon-cua" | ||
| version = "0.1.0" | ||
| description = "Kernel reference app for Tzafon Northstar Computer Use" | ||
| requires-python = ">=3.11" | ||
| dependencies = [ | ||
| "kernel>=0.35.0", | ||
| "tzafon", | ||
| ] | ||
Oops, something went wrong.
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.
is there any version pinning required for
tzafon?