@moderniser/plugin 0.1.5 → 0.1.6
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/dist/client.d.ts +4 -0
- package/dist/index.js +55 -10
- package/package.json +1 -1
package/dist/client.d.ts
CHANGED
|
@@ -2,3 +2,7 @@ export declare const API: () => string;
|
|
|
2
2
|
export declare function setStoredKey(key: string): void;
|
|
3
3
|
export declare function storedKey(): string | undefined;
|
|
4
4
|
export declare function resolveKey(): string | undefined;
|
|
5
|
+
export declare const COMPANY_HEADER = "x-moderniser-company";
|
|
6
|
+
export declare function setStoredCompany(companyId: string): void;
|
|
7
|
+
export declare function resolveCompany(): string | undefined;
|
|
8
|
+
export declare function companyHeaders(): Record<string, string>;
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import { createRequire } from "node:module";
|
|
|
2
2
|
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
3
3
|
|
|
4
4
|
// packages/plugin/src/client.ts
|
|
5
|
-
import { readFileSync } from "node:fs";
|
|
5
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from "node:fs";
|
|
6
6
|
import { homedir } from "node:os";
|
|
7
7
|
import { join } from "node:path";
|
|
8
8
|
var API = () => (process.env.MODERNISER_API_URL ?? "https://v2.moderniser.ai/api").replace(/\/+$/, "");
|
|
@@ -34,6 +34,44 @@ function fromAuthFile() {
|
|
|
34
34
|
function resolveKey() {
|
|
35
35
|
return process.env.MODERNISER_API_KEY ?? stored ?? fromAuthFile();
|
|
36
36
|
}
|
|
37
|
+
var COMPANY_HEADER = "x-moderniser-company";
|
|
38
|
+
function companyFilePath() {
|
|
39
|
+
const data = process.env.XDG_DATA_HOME || join(homedir(), ".local", "share");
|
|
40
|
+
return join(data, "moderniser", "company.json");
|
|
41
|
+
}
|
|
42
|
+
var storedCompany;
|
|
43
|
+
function setStoredCompany(companyId) {
|
|
44
|
+
storedCompany = companyId;
|
|
45
|
+
fileCompany = companyId;
|
|
46
|
+
try {
|
|
47
|
+
const path = companyFilePath();
|
|
48
|
+
mkdirSync(join(path, ".."), { recursive: true });
|
|
49
|
+
const temp = `${path}.${process.pid}.tmp`;
|
|
50
|
+
writeFileSync(temp, JSON.stringify({ companyId }, null, 2) + `
|
|
51
|
+
`, { mode: 384 });
|
|
52
|
+
renameSync(temp, path);
|
|
53
|
+
} catch {}
|
|
54
|
+
}
|
|
55
|
+
var fileCompany;
|
|
56
|
+
function companyFromFile() {
|
|
57
|
+
if (fileCompany !== undefined)
|
|
58
|
+
return fileCompany ?? undefined;
|
|
59
|
+
fileCompany = null;
|
|
60
|
+
try {
|
|
61
|
+
const parsed = JSON.parse(readFileSync(companyFilePath(), "utf8"));
|
|
62
|
+
if (typeof parsed?.companyId === "string" && parsed.companyId.length > 0) {
|
|
63
|
+
fileCompany = parsed.companyId;
|
|
64
|
+
}
|
|
65
|
+
} catch {}
|
|
66
|
+
return fileCompany ?? undefined;
|
|
67
|
+
}
|
|
68
|
+
function resolveCompany() {
|
|
69
|
+
return process.env.MODERNISER_COMPANY ?? storedCompany ?? companyFromFile();
|
|
70
|
+
}
|
|
71
|
+
function companyHeaders() {
|
|
72
|
+
const company = resolveCompany();
|
|
73
|
+
return company ? { [COMPANY_HEADER]: company } : {};
|
|
74
|
+
}
|
|
37
75
|
|
|
38
76
|
// packages/plugin/src/auth.ts
|
|
39
77
|
async function start() {
|
|
@@ -67,7 +105,7 @@ async function poll(deviceCode, expiresIn, intervalSeconds) {
|
|
|
67
105
|
continue;
|
|
68
106
|
}
|
|
69
107
|
if (body.token)
|
|
70
|
-
return body.token;
|
|
108
|
+
return { token: body.token, companyId: body.companyId };
|
|
71
109
|
if (body.error === "authorization_pending")
|
|
72
110
|
continue;
|
|
73
111
|
if (body.error === "slow_down") {
|
|
@@ -92,11 +130,13 @@ function moderniserAuth() {
|
|
|
92
130
|
instructions: `Check that your browser shows this code: ${started.userCode}. After signing in for the first time, restart to pick up the Moderniser tools.`,
|
|
93
131
|
method: "auto",
|
|
94
132
|
async callback() {
|
|
95
|
-
const
|
|
96
|
-
if (!
|
|
133
|
+
const collected = await poll(started.deviceCode, started.expiresIn, started.interval);
|
|
134
|
+
if (!collected)
|
|
97
135
|
return { type: "failed" };
|
|
98
|
-
setStoredKey(token);
|
|
99
|
-
|
|
136
|
+
setStoredKey(collected.token);
|
|
137
|
+
if (collected.companyId)
|
|
138
|
+
setStoredCompany(collected.companyId);
|
|
139
|
+
return { type: "success", key: collected.token };
|
|
100
140
|
}
|
|
101
141
|
};
|
|
102
142
|
}
|
|
@@ -158,7 +198,7 @@ async function call(path, body) {
|
|
|
158
198
|
try {
|
|
159
199
|
return await fetch(`${API2()}${path}`, {
|
|
160
200
|
method: "POST",
|
|
161
|
-
headers: { "Content-Type": "application/json", Authorization: `Bearer ${key}
|
|
201
|
+
headers: { "Content-Type": "application/json", Authorization: `Bearer ${key}`, ...companyHeaders() },
|
|
162
202
|
body: JSON.stringify(body),
|
|
163
203
|
signal: AbortSignal.timeout(1e4)
|
|
164
204
|
});
|
|
@@ -175,7 +215,7 @@ async function gatewayProduct() {
|
|
|
175
215
|
return {};
|
|
176
216
|
try {
|
|
177
217
|
const res = await fetch(`${GATEWAY()}/models`, {
|
|
178
|
-
headers: { Authorization: `Bearer ${key}
|
|
218
|
+
headers: { Authorization: `Bearer ${key}`, ...companyHeaders() },
|
|
179
219
|
signal: AbortSignal.timeout(5000)
|
|
180
220
|
});
|
|
181
221
|
if (!res.ok)
|
|
@@ -212,11 +252,16 @@ var ModerniserPlugin = async () => {
|
|
|
212
252
|
}
|
|
213
253
|
};
|
|
214
254
|
const key = KEY();
|
|
255
|
+
const company = companyHeaders();
|
|
215
256
|
config.provider ??= {};
|
|
216
257
|
config.provider[PROVIDER_ID] ??= {
|
|
217
258
|
name: "Moderniser",
|
|
218
259
|
npm: "@ai-sdk/openai-compatible",
|
|
219
|
-
options: {
|
|
260
|
+
options: {
|
|
261
|
+
baseURL: GATEWAY(),
|
|
262
|
+
...key ? { apiKey: key } : {},
|
|
263
|
+
...Object.keys(company).length ? { headers: company } : {}
|
|
264
|
+
},
|
|
220
265
|
models
|
|
221
266
|
};
|
|
222
267
|
config.enabled_providers ??= [PROVIDER_ID];
|
|
@@ -231,7 +276,7 @@ var ModerniserPlugin = async () => {
|
|
|
231
276
|
config.mcp[PROVIDER_ID] ??= {
|
|
232
277
|
type: "remote",
|
|
233
278
|
url: `${API2()}/mcp`,
|
|
234
|
-
headers: { Authorization: `Bearer ${mcpKey}
|
|
279
|
+
headers: { Authorization: `Bearer ${mcpKey}`, ...companyHeaders() },
|
|
235
280
|
oauth: false
|
|
236
281
|
};
|
|
237
282
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@moderniser/plugin",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.6",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Moderniser plugin for opencode: points inference at the gateway, declares the Moderniser remote MCP server, and reports session telemetry",
|
|
6
6
|
"license": "MIT",
|