@rhizomatics/signalk-einklabel-plugin 1.0.0 → 1.2.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/CHANGELOG.md +11 -0
- package/README.md +55 -5
- package/dist/cli/index.d.ts +2 -0
- package/dist/cli/index.js +46 -11
- package/dist/devices/gicisky/compression.d.ts +16 -0
- package/dist/devices/gicisky/compression.js +34 -0
- package/dist/devices/gicisky/encode.d.ts +10 -0
- package/dist/devices/gicisky/encode.js +130 -0
- package/dist/devices/gicisky/index.d.ts +11 -0
- package/dist/devices/gicisky/index.js +164 -0
- package/dist/devices/gicisky/layout.d.ts +35 -0
- package/dist/devices/gicisky/layout.js +35 -0
- package/dist/devices/gicisky/metadata.d.ts +16 -0
- package/dist/devices/gicisky/metadata.js +127 -0
- package/dist/devices/gicisky/protocol.d.ts +53 -0
- package/dist/devices/gicisky/protocol.js +88 -0
- package/dist/devices/types.d.ts +3 -0
- package/dist/devices/zhsunyco/index.js +3 -1
- package/dist/plugin.js +2 -0
- package/dist/render/binding.d.ts +9 -0
- package/dist/render/binding.js +21 -0
- package/dist/render/reframe.d.ts +12 -0
- package/dist/render/reframe.js +36 -0
- package/package.json +12 -10
- package/dist/render/llmPrompt.d.ts +0 -86
- package/dist/render/llmPrompt.js +0 -199
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.reframeBitmap = reframeBitmap;
|
|
4
|
+
/** Nearest-neighbour, not bilinear: the vendor colour quantisers threshold each pixel independently into a small fixed palette (see `nearestColour`/`classifyColour`), so blended edge pixels from interpolation would just be re-thresholded arbitrarily rather than improving the result. */
|
|
5
|
+
function scaleBitmap(bitmap, targetWidth, targetHeight) {
|
|
6
|
+
const data = new Uint8Array(targetWidth * targetHeight * 4);
|
|
7
|
+
for (let y = 0; y < targetHeight; y++) {
|
|
8
|
+
const srcY = Math.min(bitmap.height - 1, Math.floor((y * bitmap.height) / targetHeight));
|
|
9
|
+
for (let x = 0; x < targetWidth; x++) {
|
|
10
|
+
const srcX = Math.min(bitmap.width - 1, Math.floor((x * bitmap.width) / targetWidth));
|
|
11
|
+
const srcOffset = (srcY * bitmap.width + srcX) * 4;
|
|
12
|
+
const dstOffset = (y * targetWidth + x) * 4;
|
|
13
|
+
data.set(bitmap.data.subarray(srcOffset, srcOffset + 4), dstOffset);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return { width: targetWidth, height: targetHeight, data };
|
|
17
|
+
}
|
|
18
|
+
/** Opaque white (matches an untouched label's own background) rather than transparent black, since the vendor colour quantisers treat a fully-transparent pixel the same as an opaque black one. */
|
|
19
|
+
function cropBitmap(bitmap, targetWidth, targetHeight) {
|
|
20
|
+
const data = new Uint8Array(targetWidth * targetHeight * 4).fill(255);
|
|
21
|
+
const copyWidth = Math.min(bitmap.width, targetWidth);
|
|
22
|
+
const copyHeight = Math.min(bitmap.height, targetHeight);
|
|
23
|
+
for (let y = 0; y < copyHeight; y++) {
|
|
24
|
+
const srcOffset = y * bitmap.width * 4;
|
|
25
|
+
const dstOffset = y * targetWidth * 4;
|
|
26
|
+
data.set(bitmap.data.subarray(srcOffset, srcOffset + copyWidth * 4), dstOffset);
|
|
27
|
+
}
|
|
28
|
+
return { width: targetWidth, height: targetHeight, data };
|
|
29
|
+
}
|
|
30
|
+
/** Applies `mode` to fit `bitmap` onto a `targetWidth`x`targetHeight` panel - a no-op whenever the size already matches, regardless of mode. */
|
|
31
|
+
function reframeBitmap(bitmap, targetWidth, targetHeight, mode) {
|
|
32
|
+
if (mode === "fixed" || (bitmap.width === targetWidth && bitmap.height === targetHeight)) {
|
|
33
|
+
return bitmap;
|
|
34
|
+
}
|
|
35
|
+
return mode === "scale" ? scaleBitmap(bitmap, targetWidth, targetHeight) : cropBitmap(bitmap, targetWidth, targetHeight);
|
|
36
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rhizomatics/signalk-einklabel-plugin",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"description": "Display SignalK data on eInk Electronic Shelf Labels, includes working examples for tide clock and watch schedule.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"ble",
|
|
@@ -9,13 +9,15 @@
|
|
|
9
9
|
"esl",
|
|
10
10
|
"instrument",
|
|
11
11
|
"signalk",
|
|
12
|
+
"zhsunyco",
|
|
13
|
+
"gicisky",
|
|
12
14
|
"signalk-category-hardware",
|
|
13
15
|
"signalk-category-instruments",
|
|
14
16
|
"signalk-node-server-plugin",
|
|
15
17
|
"tides",
|
|
16
18
|
"watch-schedule"
|
|
17
19
|
],
|
|
18
|
-
"homepage": "https://rhizomatics.
|
|
20
|
+
"homepage": "https://signalk-einklabel.rhizomatics.org.uk/",
|
|
19
21
|
"bugs": {
|
|
20
22
|
"url": "https://github.com/rhizomatics/signalk-einklabel-plugin/issues"
|
|
21
23
|
},
|
|
@@ -49,26 +51,26 @@
|
|
|
49
51
|
"lint": "oxlint"
|
|
50
52
|
},
|
|
51
53
|
"dependencies": {
|
|
52
|
-
"@fontsource/roboto": "^5.
|
|
53
|
-
"@fontsource/roboto-mono": "^5.
|
|
54
|
-
"@fontsource/roboto-serif": "^5.
|
|
54
|
+
"@fontsource/roboto": "^5.3.0",
|
|
55
|
+
"@fontsource/roboto-mono": "^5.3.0",
|
|
56
|
+
"@fontsource/roboto-serif": "^5.3.0",
|
|
55
57
|
"@naugehyde/node-ble": "^1.13.5",
|
|
56
58
|
"@resvg/resvg-wasm": "^2.6.2",
|
|
57
59
|
"@xmldom/xmldom": "^0.9.10",
|
|
58
|
-
"commander": "^
|
|
60
|
+
"commander": "^15.0.0",
|
|
59
61
|
"form-data": "^4.0.6",
|
|
60
62
|
"luxon": "^3.7.2",
|
|
61
63
|
"mathjs": "^15.2.0",
|
|
62
64
|
"pngjs": "^7.0.0"
|
|
63
65
|
},
|
|
64
66
|
"devDependencies": {
|
|
65
|
-
"@signalk/server-api": "^2.
|
|
67
|
+
"@signalk/server-api": "^2.31.0",
|
|
66
68
|
"@types/luxon": "^3.7.1",
|
|
67
69
|
"@types/node": "^20.14.0",
|
|
68
70
|
"@types/pngjs": "^6.0.5",
|
|
69
|
-
"oxfmt": "^0.
|
|
70
|
-
"oxlint": "^1.
|
|
71
|
-
"oxlint-tsgolint": "^7.0.
|
|
71
|
+
"oxfmt": "^0.66.0",
|
|
72
|
+
"oxlint": "^1.81.0",
|
|
73
|
+
"oxlint-tsgolint": "^7.0.2001",
|
|
72
74
|
"ts-node": "^10.9.2",
|
|
73
75
|
"typescript": "^5.5.0"
|
|
74
76
|
},
|
|
@@ -1,86 +0,0 @@
|
|
|
1
|
-
import { PluginConfig } from "../config";
|
|
2
|
-
import { Colour } from "../devices/types";
|
|
3
|
-
import { Binding } from "./binding";
|
|
4
|
-
import { TemplateContext } from "./types";
|
|
5
|
-
/** Facts about one physical label a prompt's `source=label,path=...` placeholders can reference - see `buildLabelContext`. */
|
|
6
|
-
export interface LabelMeta {
|
|
7
|
-
manufacturer: string;
|
|
8
|
-
/** The physical panel's own size label, e.g. `'3.7"'` - `DeviceMetadata.label` verbatim, see `../devices/types.ts`. */
|
|
9
|
-
label: string;
|
|
10
|
-
width: number;
|
|
11
|
-
height: number;
|
|
12
|
-
colours: Colour[];
|
|
13
|
-
description?: string;
|
|
14
|
-
position?: {
|
|
15
|
-
latitude: number;
|
|
16
|
-
longitude: number;
|
|
17
|
-
};
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Builds the `context.label` object a prompt/target-guidance fragment addresses via
|
|
21
|
-
* `source=label,path=...` bindings (e.g. `{source=label,path=width}`, or the bare-path shorthand
|
|
22
|
-
* `{width}` is *not* supported here deliberately - see `./binding.ts`'s `parseBinding`, every `{...}`
|
|
23
|
-
* placeholder is a real binding, so a `label` path always needs the explicit `source=label,path=`
|
|
24
|
-
* form to disambiguate it from a `signalk` self path). `colours`/`fonts` are left as arrays (each
|
|
25
|
-
* colour entry pre-annotated with its hex code, e.g. `"black (#000000)"`) rather than joined into a
|
|
26
|
-
* single string, so a prompt author can either use them bare (renders as JSON, e.g. for a model that
|
|
27
|
-
* parses structured hints) or add `format=csv` (see `./formatters.ts`) for a plain comma-separated list.
|
|
28
|
-
*/
|
|
29
|
-
export declare function buildLabelContext(meta: LabelMeta): Record<string, unknown>;
|
|
30
|
-
/**
|
|
31
|
-
* Every binding referenced across one or more prompt fragments - every `{...}` placeholder, deduplicated
|
|
32
|
-
* across all of `texts` combined, parsed exactly the way a template's `<desc>` binding is
|
|
33
|
-
* (`parseBinding`, see `./binding.ts`), so a bare path (`{design.length}`, `source=signalk,context=self`
|
|
34
|
-
* shorthand), the full binding grammar (`{source=signalk,path=navigation.position,format=position}`),
|
|
35
|
-
* and a `source=label,path=...` binding all work uniformly. Pass the result to `assembleRawContext`
|
|
36
|
-
* (repaintScheduler.ts) exactly as a template's own bindings are - it fetches the `signalk`/
|
|
37
|
-
* `resources`-sourced ones and silently ignores `label`/`einklabel`-sourced ones, which resolve directly
|
|
38
|
-
* against `context.label`/`context.meta` instead (built by the caller, not fetched).
|
|
39
|
-
*
|
|
40
|
-
* A placeholder that isn't valid binding grammar (e.g. a typo like `{source=taheight}`) is silently
|
|
41
|
-
* skipped here rather than thrown - the same per-field isolation `SvgRenderer` gives a bad `<desc>`
|
|
42
|
-
* binding, so one malformed placeholder doesn't take down the whole prompt. `substitutePlaceholders`
|
|
43
|
-
* hits the identical parse error at substitution time and turns it into "???" for just that field.
|
|
44
|
-
*/
|
|
45
|
-
export declare function findPromptBindings(...texts: string[]): Binding[];
|
|
46
|
-
/**
|
|
47
|
-
* Substitutes every `{...}` placeholder in `text` with its resolved binding value against `context`
|
|
48
|
-
* (built by `assembleRawContext` plus `context.label` from `buildLabelContext` - see
|
|
49
|
-
* `findPromptBindings`). Mirrors `renderBinding`'s per-field isolation, but substitutes "???" for
|
|
50
|
-
* anything that resolves to no value at all (missing path, invalid binding grammar) rather than "" -
|
|
51
|
-
* prose with a silently-blank word reads as a fact ("for the sailor of a m vessel"), not as a gap the
|
|
52
|
-
* reader would notice. Two things override that "???": a binding that resolves successfully to a
|
|
53
|
-
* legitimately empty string (e.g. an unset `{source=label,path=description}`) is left as empty, since
|
|
54
|
-
* that's a real answer, not a miss; and a binding with an explicit `default=` (see `./binding.ts`) uses
|
|
55
|
-
* that default instead, since the prompt author has already said what a missing value should read as.
|
|
56
|
-
*/
|
|
57
|
-
export declare function substitutePlaceholders(text: string, context: TemplateContext): string;
|
|
58
|
-
/**
|
|
59
|
-
* Strips everything outside the first `<svg`...last `</svg>` span - a chat model asked for "only raw
|
|
60
|
-
* SVG markup" still often wraps it in a markdown code fence or adds a sentence of commentary either
|
|
61
|
-
* side, despite the target-guidance fragment telling it not to.
|
|
62
|
-
*/
|
|
63
|
-
export declare function extractSvg(raw: string): string;
|
|
64
|
-
export type LlmSettings = Pick<PluginConfig, "llmProvider" | "llmApiKey" | "llmModel" | "llmBaseUrl"> & {
|
|
65
|
-
llmTimeoutSeconds?: number;
|
|
66
|
-
};
|
|
67
|
-
/**
|
|
68
|
-
* `ai` and every `@ai-sdk/*` provider package are `optionalDependencies` (see package.json), not plain
|
|
69
|
-
* `dependencies` - most installs (every device using `renderMode: "svg-template"` only) never touch an
|
|
70
|
-
* LLM at all, so they shouldn't have to pull in this whole gateway stack, and an install that fails to
|
|
71
|
-
* fetch one of these (network hiccup, `npm install --omit=optional`, an unsupported platform) must not
|
|
72
|
-
* break BLE painting/templates, which have nothing to do with it. That means every reference to one of
|
|
73
|
-
* these packages has to be a `require()` reached only when a `renderMode: "llm-prompt"` device actually
|
|
74
|
-
* calls `callLlm` - a top-level `import` would be resolved eagerly the moment this module loads (which
|
|
75
|
-
* is every plugin start, via `repaintScheduler.ts`), throwing before any config is even read. Types are
|
|
76
|
-
* still fully checked via `typeof import(...)` below, which - unlike a value `import` - is erased
|
|
77
|
-
* entirely at compile time and leaves no runtime trace for `tsc` to eagerly require.
|
|
78
|
-
*/
|
|
79
|
-
export declare function loadOptional<M>(moduleName: string): M;
|
|
80
|
-
/**
|
|
81
|
-
* Calls the configured LLM gateway with `prompt`, returning its raw text response - not yet extracted/
|
|
82
|
-
* validated as SVG, see `extractSvg`. This is the first network call in the codebase that needs its own
|
|
83
|
-
* timeout (`fetchJson` in `../httpJson.ts` has none - every existing call is to the local SignalK
|
|
84
|
-
* server's own REST API).
|
|
85
|
-
*/
|
|
86
|
-
export declare function callLlm(settings: LlmSettings, prompt: string): Promise<string>;
|
package/dist/render/llmPrompt.js
DELETED
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildLabelContext = buildLabelContext;
|
|
4
|
-
exports.findPromptBindings = findPromptBindings;
|
|
5
|
-
exports.substitutePlaceholders = substitutePlaceholders;
|
|
6
|
-
exports.extractSvg = extractSvg;
|
|
7
|
-
exports.loadOptional = loadOptional;
|
|
8
|
-
exports.callLlm = callLlm;
|
|
9
|
-
const binding_1 = require("./binding");
|
|
10
|
-
const formatters_1 = require("./formatters");
|
|
11
|
-
const COLOUR_HEX = { black: "#000000", white: "#FFFFFF", red: "#FF0000", yellow: "#FFFF00" };
|
|
12
|
-
/** The only `font-family` values `SvgRenderer` is guaranteed to render - see `expandGenericFontFamilies` in `./svgRenderer.ts`. */
|
|
13
|
-
const SAFE_FONT_FAMILIES = ["serif", "sans-serif", "monospace"];
|
|
14
|
-
/**
|
|
15
|
-
* Builds the `context.label` object a prompt/target-guidance fragment addresses via
|
|
16
|
-
* `source=label,path=...` bindings (e.g. `{source=label,path=width}`, or the bare-path shorthand
|
|
17
|
-
* `{width}` is *not* supported here deliberately - see `./binding.ts`'s `parseBinding`, every `{...}`
|
|
18
|
-
* placeholder is a real binding, so a `label` path always needs the explicit `source=label,path=`
|
|
19
|
-
* form to disambiguate it from a `signalk` self path). `colours`/`fonts` are left as arrays (each
|
|
20
|
-
* colour entry pre-annotated with its hex code, e.g. `"black (#000000)"`) rather than joined into a
|
|
21
|
-
* single string, so a prompt author can either use them bare (renders as JSON, e.g. for a model that
|
|
22
|
-
* parses structured hints) or add `format=csv` (see `./formatters.ts`) for a plain comma-separated list.
|
|
23
|
-
*/
|
|
24
|
-
function buildLabelContext(meta) {
|
|
25
|
-
return {
|
|
26
|
-
manufacturer: meta.manufacturer,
|
|
27
|
-
label: meta.label,
|
|
28
|
-
width: meta.width,
|
|
29
|
-
height: meta.height,
|
|
30
|
-
colours: meta.colours.map((colour) => `${colour} (${COLOUR_HEX[colour]})`),
|
|
31
|
-
fonts: SAFE_FONT_FAMILIES,
|
|
32
|
-
description: meta.description ?? "",
|
|
33
|
-
position: meta.position ? (0, formatters_1.applyFormat)("position", meta.position, {}, 3) : undefined,
|
|
34
|
-
};
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Every binding referenced across one or more prompt fragments - every `{...}` placeholder, deduplicated
|
|
38
|
-
* across all of `texts` combined, parsed exactly the way a template's `<desc>` binding is
|
|
39
|
-
* (`parseBinding`, see `./binding.ts`), so a bare path (`{design.length}`, `source=signalk,context=self`
|
|
40
|
-
* shorthand), the full binding grammar (`{source=signalk,path=navigation.position,format=position}`),
|
|
41
|
-
* and a `source=label,path=...` binding all work uniformly. Pass the result to `assembleRawContext`
|
|
42
|
-
* (repaintScheduler.ts) exactly as a template's own bindings are - it fetches the `signalk`/
|
|
43
|
-
* `resources`-sourced ones and silently ignores `label`/`einklabel`-sourced ones, which resolve directly
|
|
44
|
-
* against `context.label`/`context.meta` instead (built by the caller, not fetched).
|
|
45
|
-
*
|
|
46
|
-
* A placeholder that isn't valid binding grammar (e.g. a typo like `{source=taheight}`) is silently
|
|
47
|
-
* skipped here rather than thrown - the same per-field isolation `SvgRenderer` gives a bad `<desc>`
|
|
48
|
-
* binding, so one malformed placeholder doesn't take down the whole prompt. `substitutePlaceholders`
|
|
49
|
-
* hits the identical parse error at substitution time and turns it into "???" for just that field.
|
|
50
|
-
*/
|
|
51
|
-
function findPromptBindings(...texts) {
|
|
52
|
-
const seen = new Set();
|
|
53
|
-
const bindings = [];
|
|
54
|
-
for (const text of texts) {
|
|
55
|
-
for (const match of text.matchAll(/\{([^{}]+)\}/g)) {
|
|
56
|
-
const key = match[1].trim();
|
|
57
|
-
if (seen.has(key))
|
|
58
|
-
continue;
|
|
59
|
-
seen.add(key);
|
|
60
|
-
try {
|
|
61
|
-
bindings.push((0, binding_1.parseBinding)(key));
|
|
62
|
-
}
|
|
63
|
-
catch {
|
|
64
|
-
// see doc comment above - left for `substitutePlaceholders` to turn into "???"
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
return bindings;
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Substitutes every `{...}` placeholder in `text` with its resolved binding value against `context`
|
|
72
|
-
* (built by `assembleRawContext` plus `context.label` from `buildLabelContext` - see
|
|
73
|
-
* `findPromptBindings`). Mirrors `renderBinding`'s per-field isolation, but substitutes "???" for
|
|
74
|
-
* anything that resolves to no value at all (missing path, invalid binding grammar) rather than "" -
|
|
75
|
-
* prose with a silently-blank word reads as a fact ("for the sailor of a m vessel"), not as a gap the
|
|
76
|
-
* reader would notice. Two things override that "???": a binding that resolves successfully to a
|
|
77
|
-
* legitimately empty string (e.g. an unset `{source=label,path=description}`) is left as empty, since
|
|
78
|
-
* that's a real answer, not a miss; and a binding with an explicit `default=` (see `./binding.ts`) uses
|
|
79
|
-
* that default instead, since the prompt author has already said what a missing value should read as.
|
|
80
|
-
*/
|
|
81
|
-
function substitutePlaceholders(text, context) {
|
|
82
|
-
return text.replace(/\{([^{}]+)\}/g, (_match, raw) => {
|
|
83
|
-
const key = raw.trim();
|
|
84
|
-
try {
|
|
85
|
-
const binding = (0, binding_1.parseBinding)(key);
|
|
86
|
-
const value = (0, binding_1.resolveBinding)(binding, context);
|
|
87
|
-
if ((value === undefined || value === null) && binding.default === undefined)
|
|
88
|
-
return "???";
|
|
89
|
-
return (0, binding_1.renderBinding)(binding, context);
|
|
90
|
-
}
|
|
91
|
-
catch {
|
|
92
|
-
return "???";
|
|
93
|
-
}
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Strips everything outside the first `<svg`...last `</svg>` span - a chat model asked for "only raw
|
|
98
|
-
* SVG markup" still often wraps it in a markdown code fence or adds a sentence of commentary either
|
|
99
|
-
* side, despite the target-guidance fragment telling it not to.
|
|
100
|
-
*/
|
|
101
|
-
function extractSvg(raw) {
|
|
102
|
-
const start = raw.indexOf("<svg");
|
|
103
|
-
const end = raw.lastIndexOf("</svg>");
|
|
104
|
-
if (start === -1 || end === -1 || end < start) {
|
|
105
|
-
throw new Error('LLM response did not contain a "<svg>...</svg>" document');
|
|
106
|
-
}
|
|
107
|
-
return raw.slice(start, end + "</svg>".length);
|
|
108
|
-
}
|
|
109
|
-
/**
|
|
110
|
-
* `ai` and every `@ai-sdk/*` provider package are `optionalDependencies` (see package.json), not plain
|
|
111
|
-
* `dependencies` - most installs (every device using `renderMode: "svg-template"` only) never touch an
|
|
112
|
-
* LLM at all, so they shouldn't have to pull in this whole gateway stack, and an install that fails to
|
|
113
|
-
* fetch one of these (network hiccup, `npm install --omit=optional`, an unsupported platform) must not
|
|
114
|
-
* break BLE painting/templates, which have nothing to do with it. That means every reference to one of
|
|
115
|
-
* these packages has to be a `require()` reached only when a `renderMode: "llm-prompt"` device actually
|
|
116
|
-
* calls `callLlm` - a top-level `import` would be resolved eagerly the moment this module loads (which
|
|
117
|
-
* is every plugin start, via `repaintScheduler.ts`), throwing before any config is even read. Types are
|
|
118
|
-
* still fully checked via `typeof import(...)` below, which - unlike a value `import` - is erased
|
|
119
|
-
* entirely at compile time and leaves no runtime trace for `tsc` to eagerly require.
|
|
120
|
-
*/
|
|
121
|
-
function loadOptional(moduleName) {
|
|
122
|
-
try {
|
|
123
|
-
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
124
|
-
return require(moduleName);
|
|
125
|
-
}
|
|
126
|
-
catch (err) {
|
|
127
|
-
throw new Error(`LLM provider support needs the optional dependency "${moduleName}", which isn't installed - run "npm install ${moduleName}" (${err.message})`);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
/** Ollama's own default local listen address - see the `"ollama"` case in `resolveModel` below. */
|
|
131
|
-
const DEFAULT_OLLAMA_BASE_URL = "http://localhost:11434/v1";
|
|
132
|
-
/**
|
|
133
|
-
* Resolves `settings` to a Vercel AI SDK model handle. `"ollama"` and `"local"` both go through the
|
|
134
|
-
* generic `@ai-sdk/openai-compatible` provider (Ollama/LM Studio/vLLM all expose an OpenAI-compatible
|
|
135
|
-
* endpoint, and none of them has - or needs - its own dedicated `@ai-sdk/*` package): `"ollama"`
|
|
136
|
-
* defaults `llmBaseUrl` to Ollama's own standard local address so it works with no further config
|
|
137
|
-
* (override it only if Ollama is running elsewhere, e.g. on the SignalK server's own host reached over
|
|
138
|
-
* the network); `"local"` is for anything else OpenAI-compatible, where there's no sensible universal
|
|
139
|
-
* default, so `llmBaseUrl` is required.
|
|
140
|
-
*/
|
|
141
|
-
function resolveModel(settings) {
|
|
142
|
-
const model = settings.llmModel;
|
|
143
|
-
if (!model) {
|
|
144
|
-
throw new Error("no LLM model configured (PluginConfig.llmModel)");
|
|
145
|
-
}
|
|
146
|
-
switch (settings.llmProvider ?? "openai") {
|
|
147
|
-
case "anthropic": {
|
|
148
|
-
const { createAnthropic } = loadOptional("@ai-sdk/anthropic");
|
|
149
|
-
return createAnthropic({ apiKey: settings.llmApiKey })(model);
|
|
150
|
-
}
|
|
151
|
-
case "google": {
|
|
152
|
-
const { createGoogleGenerativeAI } = loadOptional("@ai-sdk/google");
|
|
153
|
-
return createGoogleGenerativeAI({ apiKey: settings.llmApiKey })(model);
|
|
154
|
-
}
|
|
155
|
-
case "xai": {
|
|
156
|
-
const { createXai } = loadOptional("@ai-sdk/xai");
|
|
157
|
-
return createXai({ apiKey: settings.llmApiKey })(model);
|
|
158
|
-
}
|
|
159
|
-
case "ollama": {
|
|
160
|
-
const { createOpenAICompatible } = loadOptional("@ai-sdk/openai-compatible");
|
|
161
|
-
return createOpenAICompatible({
|
|
162
|
-
name: "ollama",
|
|
163
|
-
baseURL: settings.llmBaseUrl || DEFAULT_OLLAMA_BASE_URL,
|
|
164
|
-
apiKey: settings.llmApiKey,
|
|
165
|
-
})(model);
|
|
166
|
-
}
|
|
167
|
-
case "local": {
|
|
168
|
-
if (!settings.llmBaseUrl) {
|
|
169
|
-
throw new Error('llmBaseUrl is required when llmProvider is "local"');
|
|
170
|
-
}
|
|
171
|
-
const { createOpenAICompatible } = loadOptional("@ai-sdk/openai-compatible");
|
|
172
|
-
return createOpenAICompatible({ name: "local", baseURL: settings.llmBaseUrl, apiKey: settings.llmApiKey })(model);
|
|
173
|
-
}
|
|
174
|
-
case "openai":
|
|
175
|
-
default: {
|
|
176
|
-
const { createOpenAI } = loadOptional("@ai-sdk/openai");
|
|
177
|
-
return createOpenAI({ apiKey: settings.llmApiKey })(model);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
/**
|
|
182
|
-
* Calls the configured LLM gateway with `prompt`, returning its raw text response - not yet extracted/
|
|
183
|
-
* validated as SVG, see `extractSvg`. This is the first network call in the codebase that needs its own
|
|
184
|
-
* timeout (`fetchJson` in `../httpJson.ts` has none - every existing call is to the local SignalK
|
|
185
|
-
* server's own REST API).
|
|
186
|
-
*/
|
|
187
|
-
async function callLlm(settings, prompt) {
|
|
188
|
-
const model = resolveModel(settings);
|
|
189
|
-
const { generateText } = loadOptional("ai");
|
|
190
|
-
const controller = new AbortController();
|
|
191
|
-
const timer = setTimeout(() => controller.abort(), (settings.llmTimeoutSeconds ?? 30) * 1000);
|
|
192
|
-
try {
|
|
193
|
-
const { text } = await generateText({ model, prompt, abortSignal: controller.signal });
|
|
194
|
-
return text;
|
|
195
|
-
}
|
|
196
|
-
finally {
|
|
197
|
-
clearTimeout(timer);
|
|
198
|
-
}
|
|
199
|
-
}
|