Skip to content

Instantly share code, notes, and snippets.

View andyjessop's full-sized avatar

Andy Jessop andyjessop

View GitHub Profile
@andyjessop
andyjessop / MCPAgent.ts
Created March 21, 2025 10:45
MCP client-server agent
/**
* This interface describes an MCPAgent that extends the Agent class to function
* as both an MCP client and an MCP server. It can consume and produce MCP
* requests/notifications in adherence to the MCP specification.
*
* By handling both Client and Server message forms, this interface enables
* both connecting to MCP clients, and receiving connections from other MCP servers.
* Methods with a `handle...` prefix receive incoming requests/notifications, while methods with
* a `send...` prefix dispatch messages to a peer.
*
@andyjessop
andyjessop / index.ts
Last active February 21, 2025 13:42
Workflow + structured outputs
import { WorkflowEntrypoint, WorkflowEvent, WorkflowStep } from 'cloudflare:workers';
import { createWorkersAi } from 'workers-ai-provider';
import { generateObject } from 'ai';
import z from 'zod';
type Env = {
AI: Ai;
MY_WORKFLOW: Workflow;
};
@andyjessop
andyjessop / RenderRoute.tsx
Created February 17, 2025 06:14
Router.ts
import {
type ComponentType,
type LazyExoticComponent,
type ReactElement,
Suspense,
} from "react";
interface ComponentRoute {
component: LazyExoticComponent<ComponentType<any>>;
parent?: string;
@andyjessop
andyjessop / fsm.test.ts
Last active February 3, 2025 16:11
Simplified state machine
import { expect, test } from "bun:test";
import { generateMachine } from "./fsm";
test("happy path: simple transitions", async () => {
const config = {
init: "idle",
transitions: [
{ action: "start", from: "idle", to: "running" },
{ action: "finish", from: "running", to: "completed" },
],
@andyjessop
andyjessop / AsyncMapLevel.ts
Last active January 6, 2025 13:55
PouchDB-CloudflareKV adapter
import { AbstractLevelDOWN } from "abstract-leveldown";
import type { AsyncMap } from "../../../../packages/async-map/src/types.ts";
type Callback<T = void> = (error?: Error | null, result?: T) => void;
/**
* A utility for enabling hook up between various "AsyncMap" storage mechanisms to PouchDB via the Level adapter
*/
export class AsyncMapLevel extends AbstractLevelDOWN<string, string> {
#storage: AsyncMap<string, string>;
@andyjessop
andyjessop / workers.config.jsonc
Last active October 18, 2024 12:30
Declarative Worker Definition
{
"compatibility_date": "2024-01-01",
"compatbility_flags": [
"nodejs_compat"
],
"global": {
"TEST_AVATARS_CACHE": {
"type": "kv",
},
"GAMES": {
@andyjessop
andyjessop / gist:b6f4a643c423ef8d66a06a7c93ad7b01
Created October 16, 2024 11:27
Conditionally apply formatters for TS projects using conform.nvim
return {
"stevearc/conform.nvim",
event = { "BufReadPre", "BufNewFile", "BufEnter" },
config = function()
local conform = require("conform")
-- Global variable to store the current formatter
vim.g.current_formatter = "None"
local formatters_by_ft = {
@andyjessop
andyjessop / WorkerRegistry.ts
Created October 10, 2024 09:06
Server-based Worker Registry
// WorkerRegistry.ts
import { spawn, ChildProcess } from 'child_process';
import axios from 'axios';
import { v4 as uuidv4 } from 'uuid';
export class WorkerRegistry {
private serverProcess: ChildProcess | null = null;
private serverUrl: string = 'http://localhost:3000';
private heartbeatInterval: NodeJS.Timeout | null = null;
private instanceId: string = uuidv4();
/**
* This Fetch API interface represents the response to a request.
*
* [MDN Reference](https://developer.mozilla.org/docs/Web/API/Response)
*/
declare var Response: {
prototype: Response;
new (body?: BodyInit | null, init?: ResponseInit): Response;
redirect(url: string, status?: number): Response;
json(any: any, maybeInit?: ResponseInit | Response): Response;
@andyjessop
andyjessop / versions.test.ts
Last active September 30, 2024 14:31
Ideas for a modular Wrangler
// /packages/wrangler/versions/versions.test.ts
import { versions } from './versions';
it("should output things", async () => {
const { completed, dir, logs, mock, seed, stop } = versions({
fixture: "./tests/fixtures/",
env: {
/* extra environment variables */
},