Skip to content

Commit b5da960

Browse files
committed
dev
1 parent 0c6d7e7 commit b5da960

28 files changed

+1648
-2
lines changed

.github/dependabot.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# To get started with Dependabot version updates, you'll need to specify which
2+
# package ecosystems to update and where the package manifests are located.
3+
# Please see the documentation for all configuration options:
4+
# https://docs.github.com/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file
5+
6+
version: 2
7+
updates:
8+
- package-ecosystem: "bun"
9+
directory: "/"
10+
schedule:
11+
interval: "weekly"

.github/workflows/bun.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
name: Bun
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
call-ci:
11+
uses: qntx/workflows/.github/workflows/bun.yml@main

.github/workflows/bun_publish.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name: Publish to NPM
2+
3+
on:
4+
push:
5+
tags:
6+
- "v*.*.*"
7+
8+
jobs:
9+
call-publish:
10+
uses: qntx/workflows/.github/workflows/bun-publish.yml@main
11+
secrets:
12+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/stale.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name: Close Stale Issues and PRs
2+
3+
on:
4+
schedule:
5+
- cron: "30 1 * * *"
6+
workflow_dispatch:
7+
8+
jobs:
9+
close-stale:
10+
uses: qntx/workflows/.github/workflows/stale.yml@main

.gitignore

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# dependencies (bun install)
2+
node_modules
3+
4+
# output
5+
out
6+
dist
7+
*.tgz
8+
9+
# code coverage
10+
coverage
11+
*.lcov
12+
13+
# logs
14+
logs
15+
_.log
16+
report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json
17+
18+
# dotenv environment variable files
19+
.env
20+
.env.development.local
21+
.env.test.local
22+
.env.production.local
23+
.env.local
24+
25+
# caches
26+
.eslintcache
27+
.cache
28+
*.tsbuildinfo
29+
30+
# IntelliJ based IDEs
31+
.idea
32+
33+
# Finder (MacOS) folder config
34+
.DS_Store
35+
36+
/3rdparty

Makefile

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.DEFAULT_GOAL := all
2+
3+
.PHONY: help install lint format typecheck test check clean all
4+
5+
help: ## Show this help
6+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | \
7+
awk 'BEGIN {FS = ":.*?## "}; {printf " \033[36m%-12s\033[0m %s\n", $$1, $$2}'
8+
9+
install: ## Install all dependencies
10+
bun install
11+
12+
lint: ## Run Biome linter
13+
bunx @biomejs/biome check src/ tests/ examples/
14+
15+
format: ## Run Biome formatter (auto-fix)
16+
bunx @biomejs/biome check --write src/ tests/ examples/
17+
18+
typecheck: ## Run TypeScript type checker
19+
bun run typecheck
20+
21+
test: ## Run bun tests
22+
bun test
23+
24+
check: lint typecheck ## Run all static checks (lint + typecheck)
25+
26+
clean: ## Remove build artifacts and caches
27+
rm -rf dist/ node_modules/.cache
28+
29+
all: check test ## Run all checks + tests

README.md

Lines changed: 182 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,182 @@
1-
# x402-openai-typescript
2-
Drop-in OpenAI Typescript client with transparent x402 payment support.
1+
# x402-openai
2+
3+
> Drop-in OpenAI TypeScript client with transparent x402 payment support — **EVM, Solana, and beyond**.
4+
5+
[![TypeScript](https://img.shields.io/badge/typescript-5.0%2B-blue)](https://typescriptlang.org)
6+
[![License](https://img.shields.io/badge/license-MIT-green)](LICENSE)
7+
8+
## Install
9+
10+
```bash
11+
# EVM only
12+
bun add x402-openai openai @x402/fetch @x402/evm viem
13+
14+
# Solana (SVM) only
15+
bun add x402-openai openai @x402/fetch @x402/svm @solana/kit @scure/base
16+
17+
# Both chains
18+
bun add x402-openai openai @x402/fetch @x402/evm @x402/svm viem @solana/kit @scure/base
19+
```
20+
21+
## Quick Start
22+
23+
### EVM (Ethereum / Base / …)
24+
25+
```ts
26+
import { X402OpenAI } from "x402-openai";
27+
import { EvmWallet } from "x402-openai/wallets";
28+
29+
const client = new X402OpenAI({
30+
wallet: new EvmWallet({ privateKey: "0x…" }),
31+
});
32+
33+
// Use exactly like openai.OpenAI — everything just works!
34+
const completion = await client.chat.completions.create({
35+
model: "gpt-4o-mini",
36+
messages: [{ role: "user", content: "Hello!" }],
37+
});
38+
console.log(completion.choices[0]?.message.content);
39+
```
40+
41+
### SVM (Solana)
42+
43+
```ts
44+
import { X402OpenAI } from "x402-openai";
45+
import { SvmWallet } from "x402-openai/wallets";
46+
47+
const client = new X402OpenAI({
48+
wallet: new SvmWallet({ privateKey: "base58…" }),
49+
});
50+
```
51+
52+
### Multi-chain
53+
54+
Register multiple wallets — the x402 protocol automatically selects the right chain based on the server's payment requirements.
55+
56+
```ts
57+
import { X402OpenAI } from "x402-openai";
58+
import { EvmWallet, SvmWallet } from "x402-openai/wallets";
59+
60+
const client = new X402OpenAI({
61+
wallets: [
62+
new EvmWallet({ privateKey: "0x…" }),
63+
new SvmWallet({ privateKey: "base58…" }),
64+
],
65+
});
66+
```
67+
68+
### EVM mnemonic phrase
69+
70+
```ts
71+
import { EvmWallet } from "x402-openai/wallets";
72+
73+
const wallet = new EvmWallet({ mnemonic: "word1 word2 … word12" });
74+
75+
// BIP-44 account #2
76+
const wallet2 = new EvmWallet({ mnemonic: "word1 word2 …", accountIndex: 2 });
77+
78+
// Custom derivation path
79+
const wallet3 = new EvmWallet({
80+
mnemonic: "word1 word2 …",
81+
derivationPath: "m/44'/60'/2'/0/0",
82+
});
83+
```
84+
85+
### Streaming
86+
87+
```ts
88+
const stream = await client.chat.completions.create({
89+
model: "gpt-4o-mini",
90+
messages: [{ role: "user", content: "Explain x402" }],
91+
stream: true,
92+
});
93+
94+
for await (const chunk of stream) {
95+
const content = chunk.choices[0]?.delta?.content;
96+
if (content) process.stdout.write(content);
97+
}
98+
```
99+
100+
### Advanced: pre-built x402 client
101+
102+
```ts
103+
import { x402Client, wrapFetchWithPayment } from "@x402/fetch";
104+
import { X402OpenAI } from "x402-openai";
105+
106+
const x402 = new x402Client();
107+
// ... register custom payment schemes ...
108+
109+
const client = new X402OpenAI({ x402Client: x402 });
110+
```
111+
112+
## API
113+
114+
### `new X402OpenAI(options)`
115+
116+
Drop-in replacement for `openai.OpenAI`. Provide **exactly one** credential source:
117+
118+
| Parameter | Description |
119+
| ------------ | --------------------------------------------------- |
120+
| `wallet` | A single `Wallet` adapter (e.g. `EvmWallet`) |
121+
| `wallets` | List of `Wallet` adapters for multi-chain support |
122+
| `x402Client` | Pre-configured `x402Client` |
123+
124+
Default `baseURL` is `https://llm.qntx.fun/v1`. All standard OpenAI constructor options (`baseURL`, `timeout`, `maxRetries`, …) are forwarded transparently.
125+
126+
### Wallet adapters
127+
128+
| Class | Chain | Install extras |
129+
| -------------------------------- | ------------------------ | ----------------------------------------- |
130+
| `EvmWallet({ privateKey: … })` | EVM (Ethereum, Base, …) | `@x402/evm viem` |
131+
| `EvmWallet({ mnemonic: … })` | EVM (BIP-39) | `@x402/evm viem` |
132+
| `SvmWallet({ privateKey: … })` | Solana | `@x402/svm @solana/kit @scure/base` |
133+
134+
All wallets implement the `Wallet` interface. See [`src/wallets/base.ts`](src/wallets/base.ts) to add support for a new chain.
135+
136+
## Examples
137+
138+
```bash
139+
# EVM — private key
140+
EVM_PRIVATE_KEY="0x..." bun examples/chat-evm.ts
141+
142+
# EVM — mnemonic phrase
143+
MNEMONIC="word1 word2 ..." bun examples/chat-evm-mnemonic.ts
144+
145+
# SVM (Solana) — private key
146+
SOLANA_PRIVATE_KEY="base58..." bun examples/chat-svm.ts
147+
148+
# Streaming — EVM
149+
EVM_PRIVATE_KEY="0x..." bun examples/streaming-evm.ts
150+
151+
# Streaming — EVM mnemonic
152+
MNEMONIC="word1 word2 ..." bun examples/streaming-evm-mnemonic.ts
153+
154+
# Streaming — SVM
155+
SOLANA_PRIVATE_KEY="base58..." bun examples/streaming-svm.ts
156+
157+
# Multi-chain (EVM + SVM)
158+
EVM_PRIVATE_KEY="0x..." SOLANA_PRIVATE_KEY="base58..." bun examples/multichain.ts
159+
160+
# List models
161+
EVM_PRIVATE_KEY="0x..." bun examples/models.ts
162+
163+
# List models — mnemonic
164+
MNEMONIC="word1 word2 ..." bun examples/models-mnemonic.ts
165+
```
166+
167+
## Development
168+
169+
```bash
170+
# Install dependencies
171+
bun install
172+
173+
# Run tests
174+
bun test
175+
176+
# Type check
177+
bun run typecheck
178+
```
179+
180+
## License
181+
182+
This project is licensed under the [MIT License](LICENSE).

0 commit comments

Comments
 (0)