kernel

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2026 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Overview

Package kernel implements an HTTP + WebSocket client for the Jupyter Server REST API. It covers kernel lifecycle (list/start/restart/interrupt/stop) and provides a Channel type for executing code via the Jupyter messaging protocol.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatOutputs

func FormatOutputs(outputs []Output) string

FormatOutputs converts a slice of Outputs into a human-readable string suitable for returning to the model. Images are represented as a placeholder referencing their MIME type.

Types

type Channel

type Channel struct {
	// contains filtered or unexported fields
}

Channel is a multiplexed WebSocket connection to a Jupyter kernel. It implements the Jupyter Messaging Protocol v5 over a single WebSocket.

func (*Channel) Close

func (ch *Channel) Close()

Close closes the WebSocket connection.

func (*Channel) Execute

func (ch *Channel) Execute(ctx context.Context, code string, timeout time.Duration) ([]Output, error)

Execute sends an execute_request for code and collects outputs until the kernel becomes idle or timeout elapses. Returns parsed outputs.

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is a minimal Jupyter Server REST client.

func New

func New(cfg Config) *Client

New creates a Jupyter kernel REST client.

func (*Client) GetKernel

func (c *Client) GetKernel(ctx context.Context, id string) (*KernelInfo, error)

GetKernel returns info about a specific kernel.

func (*Client) InterruptKernel

func (c *Client) InterruptKernel(ctx context.Context, id string) error

InterruptKernel sends an interrupt signal to a running kernel.

func (*Client) ListKernels

func (c *Client) ListKernels(ctx context.Context) ([]KernelInfo, error)

ListKernels returns all running kernels.

func (*Client) OpenChannel

func (c *Client) OpenChannel(ctx context.Context, kernelID string) (*Channel, error)

OpenChannel opens a WebSocket channel to a kernel for code execution.

func (*Client) RestartKernel

func (c *Client) RestartKernel(ctx context.Context, id string) error

RestartKernel restarts a kernel, clearing all state.

func (*Client) StartKernel

func (c *Client) StartKernel(ctx context.Context, name string) (*KernelInfo, error)

StartKernel starts a new kernel with the given name (e.g. "python3").

func (*Client) StopKernel

func (c *Client) StopKernel(ctx context.Context, id string) error

StopKernel shuts down a kernel.

type Config

type Config struct {
	ServerURL string // e.g. "http://localhost:8888"
	Token     string // Jupyter token
}

Config holds the connection parameters for a Jupyter server.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns a Config populated from environment variables.

func MergeConfig

func MergeConfig(base Config, serverURL, token string) Config

MergeConfig merges explicit values (from tool input) onto a base config, keeping env-var defaults when the explicit value is empty.

type KernelInfo

type KernelInfo struct {
	ID             string `json:"id"`
	Name           string `json:"name"`
	LastActivity   string `json:"last_activity"`
	ExecutionState string `json:"execution_state"`
	Connections    int    `json:"connections"`
}

KernelInfo represents a running Jupyter kernel.

type Output

type Output struct {
	Type      string         // "stream", "execute_result", "display_data", "error"
	Name      string         // for stream: "stdout" or "stderr"
	Text      string         // text representation
	ImagePNG  string         // base64-encoded PNG (display_data)
	ImageJPEG string         // base64-encoded JPEG (display_data)
	Data      map[string]any // raw data dict from the message
}

Output is a parsed result from a single Jupyter kernel message.

func (Output) ImageData

func (o Output) ImageData() string

ImageData returns the base64 image data, or empty string.

func (Output) ImageMIME

func (o Output) ImageMIME() string

ImageMIME returns the MIME type of the image, or empty string.

func (Output) IsImage

func (o Output) IsImage() bool

IsImage reports whether the output contains an image.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL