stub-auth 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +60 -0
- package/dist/handler.js +54 -0
- package/dist/index.js +174 -0
- package/package.json +41 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 intisy
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# stub-auth
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/stub-auth)
|
|
4
|
+
[](https://www.npmjs.com/package/stub-auth)
|
|
5
|
+
[](https://github.com/intisy/stub-auth/actions/workflows/publish.yml)
|
|
6
|
+
|
|
7
|
+
A stub AI-provider driver for [`core-auth`](https://github.com/intisy/core-auth). It returns canned,
|
|
8
|
+
valid Anthropic Messages API responses (JSON or SSE) so the auth pipeline — discovery, routing, and
|
|
9
|
+
the per-app adapters in Claude Code and OpenCode — can be validated end to end without contacting any
|
|
10
|
+
real provider. It is also the reference **example** for building new provider plugins: define
|
|
11
|
+
`{ id, label, models, handle }`, let core-auth do the rest.
|
|
12
|
+
|
|
13
|
+
## Under-the-Hood Architecture
|
|
14
|
+
|
|
15
|
+
```mermaid
|
|
16
|
+
flowchart LR
|
|
17
|
+
A[cc / oc chat] --> B[core-auth / loader proxy]
|
|
18
|
+
B --> C{active provider}
|
|
19
|
+
C -->|stub| D[driver.handle]
|
|
20
|
+
D -->|stream?| E[canned SSE]
|
|
21
|
+
D -->|else| F[canned JSON]
|
|
22
|
+
E --> A
|
|
23
|
+
F --> A
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## Structure
|
|
27
|
+
|
|
28
|
+
- `src/driver.ts` — the provider: `id`/`label`/`models` + `handle()` returning the canned response.
|
|
29
|
+
- `src/index.ts` — OpenCode entry (`defineProvider(driver).opencode`).
|
|
30
|
+
- `src/handler.ts` — Claude entry (the named `handle` the loader proxy calls).
|
|
31
|
+
- `dist/` — esbuild bundles core-auth in, producing self-contained `index.js` + `handler.js`.
|
|
32
|
+
|
|
33
|
+
## Installation
|
|
34
|
+
|
|
35
|
+
### Via plugin-updater (primary)
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
npx -y plugin-updater@latest add https://github.com/intisy/stub-auth
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Then pick **Stub** in the loader's Providers tab (`cc auth`) / `oc auth login`.
|
|
42
|
+
|
|
43
|
+
### Via npm
|
|
44
|
+
|
|
45
|
+
```bash
|
|
46
|
+
npm install stub-auth
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Configuration
|
|
50
|
+
|
|
51
|
+
`stub-auth` has no settings of its own. The active provider is stored by the loader; OpenCode selects
|
|
52
|
+
it via `oc auth login` + a `stub/...` model.
|
|
53
|
+
|
|
54
|
+
## Logging
|
|
55
|
+
|
|
56
|
+
Request routing is logged by the loader/core-auth under `<configDir>/logs/YYYY-MM-DD/`.
|
|
57
|
+
|
|
58
|
+
## License
|
|
59
|
+
|
|
60
|
+
MIT
|
package/dist/handler.js
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
// src/driver.ts
|
|
2
|
+
var STUB_TEXT = "Hello from stub-auth \u2014 the core-auth pipeline works end to end.";
|
|
3
|
+
function stubText(model) {
|
|
4
|
+
return STUB_TEXT + " (served by " + model + ")";
|
|
5
|
+
}
|
|
6
|
+
function jsonBody(model) {
|
|
7
|
+
return {
|
|
8
|
+
id: "msg_stub_0001",
|
|
9
|
+
type: "message",
|
|
10
|
+
role: "assistant",
|
|
11
|
+
model,
|
|
12
|
+
content: [{ type: "text", text: stubText(model) }],
|
|
13
|
+
stop_reason: "end_turn",
|
|
14
|
+
stop_sequence: null,
|
|
15
|
+
usage: { input_tokens: 1, output_tokens: 12 }
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
function sse(event, data) {
|
|
19
|
+
return "event: " + event + "\ndata: " + JSON.stringify(data) + "\n\n";
|
|
20
|
+
}
|
|
21
|
+
function streamBody(model) {
|
|
22
|
+
const msg = { id: "msg_stub_0001", type: "message", role: "assistant", model, content: [], stop_reason: null, stop_sequence: null, usage: { input_tokens: 1, output_tokens: 0 } };
|
|
23
|
+
return sse("message_start", { type: "message_start", message: msg }) + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }) + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: stubText(model) } }) + sse("content_block_stop", { type: "content_block_stop", index: 0 }) + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 12 } }) + sse("message_stop", { type: "message_stop" });
|
|
24
|
+
}
|
|
25
|
+
var driver = {
|
|
26
|
+
id: "stub",
|
|
27
|
+
label: "Stub",
|
|
28
|
+
opencodeProvider: "stub",
|
|
29
|
+
opencodeNpm: "@ai-sdk/anthropic",
|
|
30
|
+
// a few models so the Claude model-mapping is demonstrable
|
|
31
|
+
models: {
|
|
32
|
+
"stub-model": { name: "Stub Default" },
|
|
33
|
+
"stub-pro": { name: "Stub Pro" },
|
|
34
|
+
"stub-fast": { name: "Stub Fast" }
|
|
35
|
+
},
|
|
36
|
+
async handle(request, ctx) {
|
|
37
|
+
let body = {};
|
|
38
|
+
try {
|
|
39
|
+
body = await request.clone().json();
|
|
40
|
+
} catch {
|
|
41
|
+
}
|
|
42
|
+
const model = ctx && ctx.model || body.model || "stub-model";
|
|
43
|
+
if (body.stream) {
|
|
44
|
+
return new Response(streamBody(model), { status: 200, headers: { "content-type": "text/event-stream" } });
|
|
45
|
+
}
|
|
46
|
+
return new Response(JSON.stringify(jsonBody(model)), { status: 200, headers: { "content-type": "application/json" } });
|
|
47
|
+
}
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// src/handler.ts
|
|
51
|
+
var handle = driver.handle;
|
|
52
|
+
export {
|
|
53
|
+
handle
|
|
54
|
+
};
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
// core-auth/dist/opencode.js
|
|
2
|
+
import { existsSync as existsSync4, readFileSync as readFileSync2, writeFileSync as writeFileSync2, mkdirSync as mkdirSync3 } from "fs";
|
|
3
|
+
import { join as join4, dirname, resolve } from "path";
|
|
4
|
+
import { homedir as homedir2 } from "os";
|
|
5
|
+
|
|
6
|
+
// core-auth/dist/env.js
|
|
7
|
+
import { existsSync } from "fs";
|
|
8
|
+
import { join } from "path";
|
|
9
|
+
import { homedir } from "os";
|
|
10
|
+
function getConfigDir() {
|
|
11
|
+
return process.env.HUB_CONFIG_DIR || (existsSync(join(homedir(), ".claude")) ? join(homedir(), ".claude") : join(homedir(), ".config", "opencode"));
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// core-auth/dist/log.js
|
|
15
|
+
import { existsSync as existsSync3, mkdirSync as mkdirSync2, appendFileSync } from "fs";
|
|
16
|
+
import { join as join3 } from "path";
|
|
17
|
+
|
|
18
|
+
// core-auth/dist/config.js
|
|
19
|
+
import { existsSync as existsSync2, readFileSync, writeFileSync, mkdirSync } from "fs";
|
|
20
|
+
import { join as join2 } from "path";
|
|
21
|
+
function paths() {
|
|
22
|
+
const dir = getConfigDir();
|
|
23
|
+
return { preferred: join2(dir, "config", "core-auth.json"), fallback: join2(dir, "core-auth.json") };
|
|
24
|
+
}
|
|
25
|
+
function readConfig() {
|
|
26
|
+
const { preferred, fallback } = paths();
|
|
27
|
+
const p = existsSync2(preferred) ? preferred : existsSync2(fallback) ? fallback : null;
|
|
28
|
+
try {
|
|
29
|
+
return p ? JSON.parse(readFileSync(p, "utf8")) : {};
|
|
30
|
+
} catch {
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// core-auth/dist/log.js
|
|
36
|
+
var START_TIME = (/* @__PURE__ */ new Date()).toISOString().replace(/:/g, "-").split(".")[0];
|
|
37
|
+
function log(message) {
|
|
38
|
+
if (readConfig().logging === false)
|
|
39
|
+
return;
|
|
40
|
+
try {
|
|
41
|
+
const dateStr = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
42
|
+
const dir = join3(getConfigDir(), "logs", dateStr);
|
|
43
|
+
if (!existsSync3(dir))
|
|
44
|
+
mkdirSync2(dir, { recursive: true });
|
|
45
|
+
appendFileSync(join3(dir, "core-auth-" + START_TIME + ".log"), "[" + (/* @__PURE__ */ new Date()).toISOString() + "] " + message + "\n");
|
|
46
|
+
} catch {
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// core-auth/dist/opencode.js
|
|
51
|
+
function opencodeConfigPath() {
|
|
52
|
+
const override = (process.env.OPENCODE_CONFIG || "").trim();
|
|
53
|
+
if (override)
|
|
54
|
+
return resolve(override);
|
|
55
|
+
const base = process.env.XDG_CONFIG_HOME || join4(homedir2(), ".config");
|
|
56
|
+
const dir = join4(base, "opencode");
|
|
57
|
+
const jsonc = join4(dir, "opencode.jsonc");
|
|
58
|
+
const json = join4(dir, "opencode.json");
|
|
59
|
+
return existsSync4(jsonc) ? jsonc : json;
|
|
60
|
+
}
|
|
61
|
+
function stripJsonc(text) {
|
|
62
|
+
return text.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (match, group) => group ? "" : match).replace(/,(\s*[}\]])/g, "$1");
|
|
63
|
+
}
|
|
64
|
+
function mergeModels(opencodeProvider, models, npm) {
|
|
65
|
+
const path = opencodeConfigPath();
|
|
66
|
+
let config = {};
|
|
67
|
+
try {
|
|
68
|
+
if (existsSync4(path))
|
|
69
|
+
config = JSON.parse(stripJsonc(readFileSync2(path, "utf8")));
|
|
70
|
+
} catch {
|
|
71
|
+
}
|
|
72
|
+
if (!config.$schema)
|
|
73
|
+
config.$schema = "https://opencode.ai/config.json";
|
|
74
|
+
config.provider = config.provider || {};
|
|
75
|
+
config.provider[opencodeProvider] = config.provider[opencodeProvider] || {};
|
|
76
|
+
if (npm)
|
|
77
|
+
config.provider[opencodeProvider].npm = npm;
|
|
78
|
+
const existing = config.provider[opencodeProvider].models || {};
|
|
79
|
+
config.provider[opencodeProvider].models = { ...existing, ...models };
|
|
80
|
+
try {
|
|
81
|
+
if (!existsSync4(dirname(path)))
|
|
82
|
+
mkdirSync3(dirname(path), { recursive: true });
|
|
83
|
+
writeFileSync2(path, JSON.stringify(config, null, 2), "utf8");
|
|
84
|
+
} catch (e) {
|
|
85
|
+
log("opencode model merge failed: " + (e && e.message));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
function createOpencodePlugin(def) {
|
|
89
|
+
const opencodeProvider = def.opencodeProvider || "anthropic";
|
|
90
|
+
return async function() {
|
|
91
|
+
try {
|
|
92
|
+
mergeModels(opencodeProvider, def.models || {}, def.opencodeNpm);
|
|
93
|
+
} catch {
|
|
94
|
+
}
|
|
95
|
+
return {
|
|
96
|
+
auth: {
|
|
97
|
+
provider: opencodeProvider,
|
|
98
|
+
methods: [{ label: def.label + " (via core-auth)", type: "api" }],
|
|
99
|
+
loader: async function() {
|
|
100
|
+
return {
|
|
101
|
+
apiKey: def.id,
|
|
102
|
+
fetch: function(input, init) {
|
|
103
|
+
return def.handle(new Request(input, init), { configDir: getConfigDir(), log });
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
// core-auth/dist/provider.js
|
|
113
|
+
function defineProvider(def) {
|
|
114
|
+
return {
|
|
115
|
+
def,
|
|
116
|
+
handle: (request, ctx) => def.handle(request, ctx || { configDir: getConfigDir(), log }),
|
|
117
|
+
opencode: createOpencodePlugin(def)
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// src/driver.ts
|
|
122
|
+
var STUB_TEXT = "Hello from stub-auth \u2014 the core-auth pipeline works end to end.";
|
|
123
|
+
function stubText(model) {
|
|
124
|
+
return STUB_TEXT + " (served by " + model + ")";
|
|
125
|
+
}
|
|
126
|
+
function jsonBody(model) {
|
|
127
|
+
return {
|
|
128
|
+
id: "msg_stub_0001",
|
|
129
|
+
type: "message",
|
|
130
|
+
role: "assistant",
|
|
131
|
+
model,
|
|
132
|
+
content: [{ type: "text", text: stubText(model) }],
|
|
133
|
+
stop_reason: "end_turn",
|
|
134
|
+
stop_sequence: null,
|
|
135
|
+
usage: { input_tokens: 1, output_tokens: 12 }
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function sse(event, data) {
|
|
139
|
+
return "event: " + event + "\ndata: " + JSON.stringify(data) + "\n\n";
|
|
140
|
+
}
|
|
141
|
+
function streamBody(model) {
|
|
142
|
+
const msg = { id: "msg_stub_0001", type: "message", role: "assistant", model, content: [], stop_reason: null, stop_sequence: null, usage: { input_tokens: 1, output_tokens: 0 } };
|
|
143
|
+
return sse("message_start", { type: "message_start", message: msg }) + sse("content_block_start", { type: "content_block_start", index: 0, content_block: { type: "text", text: "" } }) + sse("content_block_delta", { type: "content_block_delta", index: 0, delta: { type: "text_delta", text: stubText(model) } }) + sse("content_block_stop", { type: "content_block_stop", index: 0 }) + sse("message_delta", { type: "message_delta", delta: { stop_reason: "end_turn", stop_sequence: null }, usage: { output_tokens: 12 } }) + sse("message_stop", { type: "message_stop" });
|
|
144
|
+
}
|
|
145
|
+
var driver = {
|
|
146
|
+
id: "stub",
|
|
147
|
+
label: "Stub",
|
|
148
|
+
opencodeProvider: "stub",
|
|
149
|
+
opencodeNpm: "@ai-sdk/anthropic",
|
|
150
|
+
// a few models so the Claude model-mapping is demonstrable
|
|
151
|
+
models: {
|
|
152
|
+
"stub-model": { name: "Stub Default" },
|
|
153
|
+
"stub-pro": { name: "Stub Pro" },
|
|
154
|
+
"stub-fast": { name: "Stub Fast" }
|
|
155
|
+
},
|
|
156
|
+
async handle(request, ctx) {
|
|
157
|
+
let body = {};
|
|
158
|
+
try {
|
|
159
|
+
body = await request.clone().json();
|
|
160
|
+
} catch {
|
|
161
|
+
}
|
|
162
|
+
const model = ctx && ctx.model || body.model || "stub-model";
|
|
163
|
+
if (body.stream) {
|
|
164
|
+
return new Response(streamBody(model), { status: 200, headers: { "content-type": "text/event-stream" } });
|
|
165
|
+
}
|
|
166
|
+
return new Response(JSON.stringify(jsonBody(model)), { status: 200, headers: { "content-type": "application/json" } });
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
// src/index.ts
|
|
171
|
+
var StubProvider = defineProvider(driver).opencode;
|
|
172
|
+
export {
|
|
173
|
+
StubProvider
|
|
174
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "stub-auth",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Stub AI provider driver for core-auth — canned responses to validate the auth pipeline; the reference example for building provider plugins.",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"author": "intisy",
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/intisy/stub-auth.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": ["core-auth", "provider", "stub", "auth", "example"],
|
|
14
|
+
"files": ["dist/", "README.md", "LICENSE"],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=20.0.0"
|
|
17
|
+
},
|
|
18
|
+
"claudeHub": {
|
|
19
|
+
"authProviders": [
|
|
20
|
+
{
|
|
21
|
+
"name": "stub",
|
|
22
|
+
"handler": "dist/handler.js",
|
|
23
|
+
"models": [
|
|
24
|
+
{ "id": "stub-model", "name": "Stub Default" },
|
|
25
|
+
{ "id": "stub-pro", "name": "Stub Pro" },
|
|
26
|
+
{ "id": "stub-fast", "name": "Stub Fast" }
|
|
27
|
+
]
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
"scripts": {
|
|
32
|
+
"build": "npx tsc --project core-auth/tsconfig.json && npx esbuild src/index.ts --bundle --platform=node --format=esm --outfile=dist/index.js && npx esbuild src/handler.ts --bundle --platform=node --format=esm --outfile=dist/handler.js",
|
|
33
|
+
"prepublishOnly": "npm run build"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/bun": "^1.3.14",
|
|
37
|
+
"@types/node": "^25.9.1",
|
|
38
|
+
"esbuild": "^0.25.0",
|
|
39
|
+
"typescript": "^6.0.3"
|
|
40
|
+
}
|
|
41
|
+
}
|