@rakshazi/nodion 0.0.0-stage → 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 +304 -0
- package/README.md +62 -2
- package/catalog.ts +192 -0
- package/index.ts +125 -0
- package/options.ts +35 -0
- package/package.json +24 -5
- package/panel.ts +84 -0
- package/plan-state.ts +155 -0
- package/plan.ts +452 -0
package/index.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
// Nodion provider registration: /login nodion or NODION_AI_KEY, live catalog, optional coding plan.
|
|
2
|
+
import { createProvider, envApiKeyAuth, type Model } from "@earendil-works/pi-ai";
|
|
3
|
+
import { openAICompletionsApi } from "@earendil-works/pi-ai/compat";
|
|
4
|
+
import {
|
|
5
|
+
type ExtensionAPI,
|
|
6
|
+
type ExtensionContext,
|
|
7
|
+
getAgentDir,
|
|
8
|
+
SettingsManager,
|
|
9
|
+
} from "@earendil-works/pi-coding-agent";
|
|
10
|
+
import { API_KEY_ENV, BASE_URL, fetchCatalog, onlyCodingPlanModels, PROVIDER_ID } from "./catalog.ts";
|
|
11
|
+
import { resolveOptions } from "./options.ts";
|
|
12
|
+
import { PlanPanel } from "./panel.ts";
|
|
13
|
+
import { fetchPlan, NO_KEY_MESSAGE, renderFooter, type PlanView } from "./plan.ts";
|
|
14
|
+
import { createPlanState } from "./plan-state.ts";
|
|
15
|
+
|
|
16
|
+
const STATUS_KEY = "nodion-plan";
|
|
17
|
+
const POLL_INTERVAL_MS = 300_000;
|
|
18
|
+
|
|
19
|
+
let activeCtx: ExtensionContext | undefined;
|
|
20
|
+
let disposed = false;
|
|
21
|
+
|
|
22
|
+
/** The base URL Pi stamped on a Nodion model, so a models.json gateway override reaches the plan call. */
|
|
23
|
+
function planBaseUrl(ctx: ExtensionContext): string | undefined {
|
|
24
|
+
if (ctx.model?.provider === PROVIDER_ID) return ctx.model.baseUrl;
|
|
25
|
+
return ctx.modelRegistry.getAll().find((model) => model.provider === PROVIDER_ID)?.baseUrl;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function paint(ctx: ExtensionContext, provider = ctx.model?.provider): void {
|
|
29
|
+
if (disposed || !ctx.hasUI) return;
|
|
30
|
+
const { plan } = state.current();
|
|
31
|
+
if (!plan || provider !== PROVIDER_ID) {
|
|
32
|
+
ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
ctx.ui.setStatus(STATUS_KEY, renderFooter(plan, { fg: (token, text) => ctx.ui.theme.fg(token, text) }));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const state = createPlanState({
|
|
39
|
+
getApiKey: async () => activeCtx?.modelRegistry.getApiKeyForProvider(PROVIDER_ID),
|
|
40
|
+
baseUrl: () => (activeCtx ? planBaseUrl(activeCtx) : undefined),
|
|
41
|
+
fetchPlan: (request) => fetchPlan(request),
|
|
42
|
+
onPublish: () => {
|
|
43
|
+
if (activeCtx) paint(activeCtx);
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export default function nodion(pi: ExtensionAPI): void {
|
|
48
|
+
const loaded = resolveOptions(SettingsManager.create(process.cwd(), getAgentDir()).getGlobalSettings());
|
|
49
|
+
const codingPlanOnly = loaded.options.codingPlanOnly;
|
|
50
|
+
|
|
51
|
+
// The factory also runs without a UI, so a broken settings.json is reported at session start.
|
|
52
|
+
if (loaded.warning) pi.on("session_start", (_event, ctx) => ctx.ui.notify(`nodion: ${loaded.warning}`, "warning"));
|
|
53
|
+
|
|
54
|
+
pi.registerProvider(
|
|
55
|
+
createProvider({
|
|
56
|
+
id: PROVIDER_ID,
|
|
57
|
+
name: "Nodion",
|
|
58
|
+
baseUrl: BASE_URL,
|
|
59
|
+
auth: { apiKey: envApiKeyAuth("Nodion API key", [API_KEY_ENV]) },
|
|
60
|
+
// Empty on purpose: createProvider restores and persists the fetched catalog per provider.
|
|
61
|
+
models: [],
|
|
62
|
+
fetchModels: (context): Promise<Model<"openai-completions">[]> =>
|
|
63
|
+
fetchCatalog({ baseUrl: context.baseUrl, credential: context.credential, signal: context.signal }),
|
|
64
|
+
filterModels: (models) => (codingPlanOnly ? onlyCodingPlanModels(models) : models),
|
|
65
|
+
api: openAICompletionsApi(),
|
|
66
|
+
}),
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
pi.on("session_start", (_event, ctx) => {
|
|
70
|
+
disposed = false;
|
|
71
|
+
activeCtx = ctx;
|
|
72
|
+
state.reset();
|
|
73
|
+
if (!ctx.hasUI) return;
|
|
74
|
+
paint(ctx);
|
|
75
|
+
if (ctx.model?.provider !== PROVIDER_ID) return;
|
|
76
|
+
state.startPolling(POLL_INTERVAL_MS);
|
|
77
|
+
void state.load(true);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
pi.on("model_select", (event, ctx) => {
|
|
81
|
+
activeCtx = ctx;
|
|
82
|
+
if (!ctx.hasUI) return;
|
|
83
|
+
paint(ctx, event.model.provider);
|
|
84
|
+
if (event.model.provider !== PROVIDER_ID) {
|
|
85
|
+
state.stopPolling();
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
state.startPolling(POLL_INTERVAL_MS);
|
|
89
|
+
void state.load(false);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
pi.on("agent_settled", (_event, ctx) => {
|
|
93
|
+
activeCtx = ctx;
|
|
94
|
+
if (!ctx.hasUI || ctx.model?.provider !== PROVIDER_ID) return;
|
|
95
|
+
void state.load(false);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
pi.on("session_shutdown", (_event, ctx) => {
|
|
99
|
+
activeCtx = ctx;
|
|
100
|
+
disposed = true;
|
|
101
|
+
state.dispose();
|
|
102
|
+
if (ctx.hasUI) ctx.ui.setStatus(STATUS_KEY, undefined);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
pi.registerCommand("nodion", {
|
|
106
|
+
description: "Show Nodion coding plan usage",
|
|
107
|
+
handler: async (_args, ctx) => {
|
|
108
|
+
if (ctx.mode !== "tui") {
|
|
109
|
+
ctx.ui.notify("/nodion requires interactive mode", "error");
|
|
110
|
+
return;
|
|
111
|
+
}
|
|
112
|
+
const apiKey = await ctx.modelRegistry.getApiKeyForProvider(PROVIDER_ID);
|
|
113
|
+
if (!apiKey) {
|
|
114
|
+
ctx.ui.notify(NO_KEY_MESSAGE, "warning");
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
activeCtx = ctx;
|
|
118
|
+
const initial: PlanView = { ...state.current(), loading: true };
|
|
119
|
+
await ctx.ui.custom<void>(
|
|
120
|
+
(tui, theme, _keybindings, done) => new PlanPanel(tui, theme, initial, () => state.load(true), () => done()),
|
|
121
|
+
{ overlay: true, overlayOptions: { width: 56, margin: 1, anchor: "center" } },
|
|
122
|
+
);
|
|
123
|
+
},
|
|
124
|
+
});
|
|
125
|
+
}
|
package/options.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// The `nodion` section of the global settings.json, read once when the extension loads.
|
|
2
|
+
export interface NodionOptions {
|
|
3
|
+
codingPlanOnly: boolean;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export interface ResolvedNodionOptions {
|
|
7
|
+
options: NodionOptions;
|
|
8
|
+
warning?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
const DEFAULT_OPTIONS: NodionOptions = { codingPlanOnly: false };
|
|
12
|
+
|
|
13
|
+
/** The coding-plan switch, defaulting to every model and naming a broken value instead of guessing. */
|
|
14
|
+
export function resolveOptions(settings: unknown): ResolvedNodionOptions {
|
|
15
|
+
if (typeof settings !== "object" || settings === null || Array.isArray(settings)) {
|
|
16
|
+
return { options: { ...DEFAULT_OPTIONS } };
|
|
17
|
+
}
|
|
18
|
+
const section = "nodion" in settings ? settings.nodion : undefined;
|
|
19
|
+
if (section === undefined) return { options: { ...DEFAULT_OPTIONS } };
|
|
20
|
+
if (typeof section !== "object" || section === null || Array.isArray(section)) {
|
|
21
|
+
return {
|
|
22
|
+
options: { ...DEFAULT_OPTIONS },
|
|
23
|
+
warning: 'settings.json: "nodion" must be an object; showing all Nodion models.',
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
const value = "coding_plan_only" in section ? section.coding_plan_only : undefined;
|
|
27
|
+
if (value === undefined) return { options: { ...DEFAULT_OPTIONS } };
|
|
28
|
+
if (typeof value !== "boolean") {
|
|
29
|
+
return {
|
|
30
|
+
options: { ...DEFAULT_OPTIONS },
|
|
31
|
+
warning: 'settings.json: "nodion.coding_plan_only" must be a boolean; showing all Nodion models.',
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
return { options: { codingPlanOnly: value } };
|
|
35
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@rakshazi/nodion",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Register Nodion as a Pi provider with a live model catalog",
|
|
5
|
+
"license": "LGPL-3.0-or-later",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/rakshazi/pi-extensions.git",
|
|
9
|
+
"directory": "extensions/nodion"
|
|
10
|
+
},
|
|
11
|
+
"keywords": ["pi-package"],
|
|
12
|
+
"type": "module",
|
|
13
|
+
"pi": {
|
|
14
|
+
"extensions": ["./index.ts"]
|
|
15
|
+
},
|
|
16
|
+
"files": ["index.ts", "catalog.ts", "options.ts", "panel.ts", "plan-state.ts", "plan.ts", "README.md", "LICENSE"],
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@earendil-works/pi-ai": "*",
|
|
19
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
20
|
+
"@earendil-works/pi-tui": "*"
|
|
21
|
+
},
|
|
22
|
+
"publishConfig": {
|
|
23
|
+
"access": "public"
|
|
24
|
+
}
|
|
25
|
+
}
|
package/panel.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
// The /nodion overlay: a static informational card with a 30-second redraw for countdowns.
|
|
2
|
+
import {
|
|
3
|
+
matchesKey,
|
|
4
|
+
truncateToWidth,
|
|
5
|
+
visibleWidth,
|
|
6
|
+
type Component,
|
|
7
|
+
type TUI,
|
|
8
|
+
} from "@earendil-works/pi-tui";
|
|
9
|
+
import type { Theme } from "@earendil-works/pi-coding-agent";
|
|
10
|
+
import { renderPlanPanel, type PlanView } from "./plan.ts";
|
|
11
|
+
|
|
12
|
+
export class PlanPanel implements Component {
|
|
13
|
+
private view: PlanView;
|
|
14
|
+
private refreshing = false;
|
|
15
|
+
private closed = false;
|
|
16
|
+
private readonly tui: TUI;
|
|
17
|
+
private readonly theme: Theme;
|
|
18
|
+
private readonly refresh: () => Promise<PlanView>;
|
|
19
|
+
private readonly done: () => void;
|
|
20
|
+
private timer: NodeJS.Timeout | undefined;
|
|
21
|
+
|
|
22
|
+
constructor(
|
|
23
|
+
tui: TUI,
|
|
24
|
+
theme: Theme,
|
|
25
|
+
initial: PlanView,
|
|
26
|
+
refresh: () => Promise<PlanView>,
|
|
27
|
+
done: () => void,
|
|
28
|
+
) {
|
|
29
|
+
this.tui = tui;
|
|
30
|
+
this.theme = theme;
|
|
31
|
+
this.view = initial;
|
|
32
|
+
this.refresh = refresh;
|
|
33
|
+
this.done = done;
|
|
34
|
+
this.timer = setInterval(() => {
|
|
35
|
+
if (!this.closed) this.tui.requestRender();
|
|
36
|
+
}, 30_000);
|
|
37
|
+
void this.reload();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
render(width: number): string[] {
|
|
41
|
+
return renderPlanPanel(this.view, width, new Date(), {
|
|
42
|
+
fg: (token, text) => this.theme.fg(token, text),
|
|
43
|
+
bold: (text) => this.theme.bold(text),
|
|
44
|
+
width: (text) => visibleWidth(text),
|
|
45
|
+
}).map((line) => truncateToWidth(line, width, "…"));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
handleInput(data: string): void {
|
|
49
|
+
if (matchesKey(data, "escape") || matchesKey(data, "ctrl+c")) {
|
|
50
|
+
this.dispose();
|
|
51
|
+
this.done();
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
if (matchesKey(data, "r") && !this.refreshing) void this.reload();
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
invalidate(): void {}
|
|
58
|
+
|
|
59
|
+
dispose(): void {
|
|
60
|
+
this.closed = true;
|
|
61
|
+
if (this.timer !== undefined) {
|
|
62
|
+
clearInterval(this.timer);
|
|
63
|
+
this.timer = undefined;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private async reload(): Promise<void> {
|
|
68
|
+
if (this.refreshing || this.closed) return;
|
|
69
|
+
this.refreshing = true;
|
|
70
|
+
this.view = { ...this.view, error: undefined, loading: true };
|
|
71
|
+
this.tui.requestRender();
|
|
72
|
+
try {
|
|
73
|
+
const view = await this.refresh();
|
|
74
|
+
if (!this.closed) this.view = view;
|
|
75
|
+
} catch (error) {
|
|
76
|
+
if (!this.closed) {
|
|
77
|
+
this.view = { ...this.view, error: error instanceof Error ? error.message : String(error), loading: false };
|
|
78
|
+
}
|
|
79
|
+
} finally {
|
|
80
|
+
this.refreshing = false;
|
|
81
|
+
if (!this.closed) this.tui.requestRender();
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
package/plan-state.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// Owns the plan refresh throttle, in-flight request, polling and generation guard; no Pi imports.
|
|
2
|
+
import { NO_KEY_MESSAGE, type CodingPlan, type PlanFetch, type PlanRequest, type PlanView } from "./plan.ts";
|
|
3
|
+
|
|
4
|
+
export const MIN_FETCH_GAP_MS = 30_000;
|
|
5
|
+
|
|
6
|
+
export interface PlanStateDeps {
|
|
7
|
+
getApiKey: () => Promise<string | undefined>;
|
|
8
|
+
baseUrl: () => string | undefined;
|
|
9
|
+
fetchPlan: (request: PlanRequest) => Promise<PlanFetch>;
|
|
10
|
+
onPublish: (plan: CodingPlan | undefined, fetchedAt: Date | undefined) => void;
|
|
11
|
+
now?: () => number;
|
|
12
|
+
setInterval?: (fn: () => void, ms: number) => unknown;
|
|
13
|
+
clearInterval?: (handle: unknown) => void;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export interface PlanState {
|
|
17
|
+
load(force: boolean): Promise<PlanView>;
|
|
18
|
+
current(): PlanView;
|
|
19
|
+
startPolling(intervalMs: number): void;
|
|
20
|
+
stopPolling(): void;
|
|
21
|
+
reset(): void;
|
|
22
|
+
dispose(): void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function createPlanState(deps: PlanStateDeps): PlanState {
|
|
26
|
+
const now = deps.now ?? Date.now;
|
|
27
|
+
const schedule = deps.setInterval ?? setInterval;
|
|
28
|
+
const cancel = deps.clearInterval ?? clearInterval;
|
|
29
|
+
|
|
30
|
+
let plan: CodingPlan | undefined;
|
|
31
|
+
let fetchedAt: Date | undefined;
|
|
32
|
+
let lastAttemptAt: number | undefined;
|
|
33
|
+
let inFlight: Promise<PlanView> | undefined;
|
|
34
|
+
let abort: AbortController | undefined;
|
|
35
|
+
let poll: unknown;
|
|
36
|
+
let closed = false;
|
|
37
|
+
let generation = 0;
|
|
38
|
+
|
|
39
|
+
function viewOf(loading: boolean): PlanView {
|
|
40
|
+
return {
|
|
41
|
+
...(plan ? { plan } : {}),
|
|
42
|
+
...(fetchedAt ? { updatedAt: fetchedAt } : {}),
|
|
43
|
+
loading,
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function publish(): void {
|
|
48
|
+
deps.onPublish(plan, fetchedAt);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async function run(gen: number): Promise<PlanView> {
|
|
52
|
+
const apiKey = await deps.getApiKey();
|
|
53
|
+
if (gen !== generation || closed) return viewOf(false);
|
|
54
|
+
|
|
55
|
+
if (!apiKey) {
|
|
56
|
+
plan = undefined;
|
|
57
|
+
fetchedAt = undefined;
|
|
58
|
+
publish();
|
|
59
|
+
return { error: NO_KEY_MESSAGE, loading: false };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
const controller = new AbortController();
|
|
63
|
+
abort = controller;
|
|
64
|
+
let result: PlanFetch;
|
|
65
|
+
try {
|
|
66
|
+
result = await deps.fetchPlan({ apiKey, baseUrl: deps.baseUrl(), signal: controller.signal });
|
|
67
|
+
} catch (error) {
|
|
68
|
+
result = {
|
|
69
|
+
ok: false,
|
|
70
|
+
reason: "failed",
|
|
71
|
+
message: `Nodion plan request failed: ${error instanceof Error ? error.message : String(error)}`,
|
|
72
|
+
};
|
|
73
|
+
} finally {
|
|
74
|
+
if (abort === controller) abort = undefined;
|
|
75
|
+
}
|
|
76
|
+
if (gen !== generation || closed) return viewOf(false);
|
|
77
|
+
|
|
78
|
+
if (result.ok) {
|
|
79
|
+
plan = result.plan;
|
|
80
|
+
fetchedAt = new Date(now());
|
|
81
|
+
publish();
|
|
82
|
+
return { plan, updatedAt: fetchedAt, loading: false };
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
if (result.reason === "failed") {
|
|
86
|
+
publish();
|
|
87
|
+
return { ...viewOf(false), error: result.message };
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// no_plan, ended and unauthorized all mean the cached plan is no longer valid.
|
|
91
|
+
plan = undefined;
|
|
92
|
+
fetchedAt = undefined;
|
|
93
|
+
publish();
|
|
94
|
+
return { error: result.message, loading: false };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async function load(force: boolean): Promise<PlanView> {
|
|
98
|
+
if (closed) return viewOf(false);
|
|
99
|
+
if (inFlight) return inFlight;
|
|
100
|
+
if (!force && lastAttemptAt !== undefined && now() - lastAttemptAt < MIN_FETCH_GAP_MS) return viewOf(false);
|
|
101
|
+
|
|
102
|
+
lastAttemptAt = now();
|
|
103
|
+
const gen = generation;
|
|
104
|
+
const promise = run(gen);
|
|
105
|
+
inFlight = promise;
|
|
106
|
+
void promise.finally(() => {
|
|
107
|
+
if (inFlight === promise) inFlight = undefined;
|
|
108
|
+
});
|
|
109
|
+
return promise;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
function startPolling(intervalMs: number): void {
|
|
113
|
+
if (poll !== undefined) return;
|
|
114
|
+
poll = schedule(() => {
|
|
115
|
+
if (!closed) void load(true);
|
|
116
|
+
}, intervalMs);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function stopPolling(): void {
|
|
120
|
+
if (poll !== undefined) {
|
|
121
|
+
cancel(poll);
|
|
122
|
+
poll = undefined;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
function reset(): void {
|
|
127
|
+
generation++;
|
|
128
|
+
closed = false;
|
|
129
|
+
plan = undefined;
|
|
130
|
+
fetchedAt = undefined;
|
|
131
|
+
lastAttemptAt = undefined;
|
|
132
|
+
inFlight = undefined;
|
|
133
|
+
abort?.abort();
|
|
134
|
+
abort = undefined;
|
|
135
|
+
stopPolling();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function dispose(): void {
|
|
139
|
+
generation++;
|
|
140
|
+
closed = true;
|
|
141
|
+
abort?.abort();
|
|
142
|
+
abort = undefined;
|
|
143
|
+
stopPolling();
|
|
144
|
+
inFlight = undefined;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
load,
|
|
149
|
+
current: () => viewOf(false),
|
|
150
|
+
startPolling,
|
|
151
|
+
stopPolling,
|
|
152
|
+
reset,
|
|
153
|
+
dispose,
|
|
154
|
+
};
|
|
155
|
+
}
|