Skip to content

Experimental TypeScript/JavaScript runtime #121

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions cog-fixture/cog.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Example cog.yaml for JavaScript/TypeScript predictor
predict: "./example_predictor.ts:simplePredictFunction"
concurrency:
max: 2
10 changes: 10 additions & 0 deletions cog-fixture/example_predictor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Example function-based predictor
export function simplePredictFunction(input) {
const multiplier = input.multiplier || 1;
return {
original: input.text,
uppercase: input.text.toUpperCase(),
length: input.text.length * multiplier,
timestamp: new Date().toISOString(),
};
}
86 changes: 86 additions & 0 deletions deno-coglet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Deno Coglet

A Deno/TypeScript implementation of the Cog runtime interface that allows JavaScript/TypeScript predictors to work with the Cog Go runner.

## Overview

This implementation provides the same file-based IPC interface as the Python coglet, enabling the Go runner to manage JavaScript/TypeScript prediction functions and classes.

## Architecture

The communication protocol uses:
- **File-based IPC** for exchanging requests and responses
- **HTTP status updates** to notify the Go runner of state changes
- **Formatted logging** for proper log routing

## Usage

### Running the Coglet

```bash
deno run --allow-read --allow-write --allow-net --allow-env \
coglet.ts \
--ipc-url http://localhost:8080 \
--working-dir /tmp/work
```

### Creating a Predictor

You can create predictors as functions or classes:

```typescript
// Function predictor
export function myPredictor(input: { text: string }) {
return { result: input.text.toUpperCase() };
}

// Class predictor with setup
export class MyPredictor {
private model: any;

async setup() {
// Initialize your model here
this.model = await loadModel();
}

async predict(input: { prompt: string }) {
return await this.model.generate(input.prompt);
}
}
```

### Integration with Go Runner

To use this with the Go runner, you would need to modify the runner to use Deno instead of Python:

```go
// In runner.go
args := []string{
"run",
"--allow-read", "--allow-write", "--allow-net", "--allow-env",
"/path/to/coglet.ts",
"--ipc-url", ipcUrl,
"--working-dir", workingDir,
}
cmd := exec.Command("deno", args...)
```

## File Interface

- `config.json` - Initial configuration from Go
- `request-{id}.json` - Prediction requests
- `response-{id}-{epoch}.json` - Prediction responses
- `cancel-{id}` - Cancel specific prediction
- `stop` - Shutdown signal
- `setup_result.json` - Setup status
- `openapi.json` - API schema

## Testing

Run the test script to see it in action:

```bash
./test_runner.sh
```

This will start a mock IPC server, run the coglet with an example predictor, and send a test prediction request.
Loading