@nanobpm/urban-pr-review-codefirst 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/README.md +141 -0
- package/components/review-round.json +69 -0
- package/db/migrations/001_init.sql +46 -0
- package/db/migrations/002_transcript.sql +7 -0
- package/db/migrations/003_open_escalation.sql +8 -0
- package/deno.json +22 -0
- package/deno.lock +77 -0
- package/main.ts +352 -0
- package/nano-generated/app-pages.ts +676 -0
- package/nano-generated/data-cli.ts +552 -0
- package/nano-generated/data-sdk.ts +536 -0
- package/nano-generated/domain-rows.d.ts +87 -0
- package/nano-generated/domain-types.ts +1418 -0
- package/nano-generated/domain.ts +36 -0
- package/nano-generated/llm-worker.ts +403 -0
- package/nano-generated/message-io.d.ts +17 -0
- package/nano-generated/messages.ts +26 -0
- package/nano-generated/meta.ts +18 -0
- package/nano-generated/node-deno-shim.mjs +250 -0
- package/nano-generated/node-loader.mjs +60 -0
- package/nano-generated/node-register.mjs +11 -0
- package/nano-generated/worker-io.d.ts +28 -0
- package/nano-generated/worker-sdk.ts +468 -0
- package/nano-generated/workers.ts +29 -0
- package/nano.app.json +17 -0
- package/nanobpm.project.json +22 -0
- package/package.json +28 -0
- package/pages/home.page.json +127 -0
- package/prompts/review-round.md +119 -0
- package/scripts/answer.ts +20 -0
- package/scripts/purge-db.ts +70 -0
- package/scripts/review-ready.ts +20 -0
- package/scripts/submit.ts +32 -0
- package/tsconfig.json +14 -0
- package/workflows/convergence-loop.ts +299 -0
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// AUTO-GENERATED by nanobpmn from the datasource schema (ADR 0029 §4.1/§6).
|
|
2
|
+
// The typed data-object layer: `openDomain()` → one `Table<T>` per table, so
|
|
3
|
+
// workers manipulate typed rows instead of hand-writing SQL. Do not edit —
|
|
4
|
+
// regenerated from the live tables. Erased to plain JS at `deno compile`.
|
|
5
|
+
// eslint-disable
|
|
6
|
+
|
|
7
|
+
import { openDataSource, type DataSource, type Table } from "./data-sdk.ts";
|
|
8
|
+
import type { Escalations, PullRequests, Rounds } from "./domain-rows.d.ts";
|
|
9
|
+
|
|
10
|
+
export interface Domain {
|
|
11
|
+
/** The underlying datasource handle — the raw-SQL escape hatch. */
|
|
12
|
+
readonly raw: DataSource;
|
|
13
|
+
readonly escalations: Table<Escalations>;
|
|
14
|
+
readonly pull_requests: Table<PullRequests>;
|
|
15
|
+
readonly rounds: Table<Rounds>;
|
|
16
|
+
/** Close the underlying connection. */
|
|
17
|
+
close(): void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Open the App's datasource as a typed domain: each table is
|
|
22
|
+
* a `Table<T>` gateway (`db.orders.insert(...)`, `db.orders.get(id)`), with the
|
|
23
|
+
* raw `DataSource` at `db.raw` for anything the gateway doesn't cover.
|
|
24
|
+
*/
|
|
25
|
+
export async function openDomain(source?: string): Promise<Domain> {
|
|
26
|
+
const raw = await openDataSource(source);
|
|
27
|
+
return {
|
|
28
|
+
raw,
|
|
29
|
+
escalations: raw.table<Escalations>("escalations", "id"),
|
|
30
|
+
pull_requests: raw.table<PullRequests>("pull_requests", "pr_key"),
|
|
31
|
+
rounds: raw.table<Rounds>("rounds", "id"),
|
|
32
|
+
close: () => raw.close(),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export type { Table };
|
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
// Urban LLM-as-job-worker runtime (ADR 0022 §E role 1).
|
|
2
|
+
//
|
|
3
|
+
// This file is materialised verbatim into <workspace>/nano-generated/llm-worker.ts by
|
|
4
|
+
// the console supervisor (like worker-sdk.ts / data-sdk.ts) and started from the
|
|
5
|
+
// App entrypoint via `startLlmWorkers()`. It is the *batteries-included*,
|
|
6
|
+
// offline-capable implementation of the manifest's `llm` seam: for every
|
|
7
|
+
// `workers[]` entry that carries an `llm` binding (instead of a `handler` file),
|
|
8
|
+
// it registers a Falcon job worker on that `taskType` whose handler is an LLM
|
|
9
|
+
// call — no external connector runtime required.
|
|
10
|
+
//
|
|
11
|
+
// A worker's contract (ADR 0022 §E):
|
|
12
|
+
// { "taskType": "classify", "llm": "classifier" } // workers[]
|
|
13
|
+
// "llm": { "classifier": { "provider": "env", // llm registry
|
|
14
|
+
// "model": "${NANO_APP_LLM_MODEL}",
|
|
15
|
+
// "output": { "decision": "risk" } } }
|
|
16
|
+
//
|
|
17
|
+
// The job's input variables drive the prompt (`prompt` string and/or `messages`
|
|
18
|
+
// array, optional `system`); the model's reply becomes the job's output
|
|
19
|
+
// variables. When the binding declares `output.decision`, the model's JSON is
|
|
20
|
+
// fed through that DMN decision (the "rails") and the decision's output is
|
|
21
|
+
// returned instead — the LLM proposes, the decision disposes.
|
|
22
|
+
//
|
|
23
|
+
// Provider is configuration, not code: `provider: "env"` resolves an
|
|
24
|
+
// OpenAI-compatible endpoint from the environment, so a fully-local model
|
|
25
|
+
// (Ollama, LM Studio, llama.cpp, vLLM, …) or a hosted API both work unchanged.
|
|
26
|
+
|
|
27
|
+
// This runtime reads only a small, stable slice of a worker job and writes only
|
|
28
|
+
// `{ type, handle }` back to the registrar, so it declares those shapes locally
|
|
29
|
+
// (structural mirrors of @nanobpm/worker's WorkerJob/WorkerOptions) and needs no
|
|
30
|
+
// sibling import — which keeps it a single self-contained file that type-checks
|
|
31
|
+
// identically in the source tree and once materialised into `nano-generated/`.
|
|
32
|
+
|
|
33
|
+
/** Job variables/headers default to untyped JSON. */
|
|
34
|
+
export type LlmVars = Record<string, unknown>;
|
|
35
|
+
|
|
36
|
+
/** The subset of a worker job this runtime reads (structural mirror of
|
|
37
|
+
* @nanobpm/worker's `WorkerJob`). */
|
|
38
|
+
export interface LlmJob {
|
|
39
|
+
readonly variables: LlmVars;
|
|
40
|
+
readonly customHeaders: Record<string, unknown>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** The subset of `defineWorker`'s options this runtime writes (structural mirror
|
|
44
|
+
* of @nanobpm/worker's `WorkerOptions`); the real registrar accepts a superset. */
|
|
45
|
+
export interface LlmWorkerOptions {
|
|
46
|
+
type: string;
|
|
47
|
+
handle: (job: LlmJob, ctx?: unknown) => Promise<unknown> | unknown;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Expand `${VAR}` / `${VAR:-default}` env templates (mirrors data-sdk's helper;
|
|
51
|
+
* inlined to keep this a self-contained, unit-testable module). */
|
|
52
|
+
function resolveEnvTemplate(
|
|
53
|
+
tpl: string,
|
|
54
|
+
env: (key: string) => string | undefined,
|
|
55
|
+
): string {
|
|
56
|
+
return tpl.replace(
|
|
57
|
+
/\$\{([A-Za-z_][A-Za-z0-9_]*)(?::-([^}]*))?\}/g,
|
|
58
|
+
(_m, name: string, dflt: string | undefined) => {
|
|
59
|
+
const v = env(name);
|
|
60
|
+
if (v !== undefined && v !== "") return v;
|
|
61
|
+
return dflt ?? "";
|
|
62
|
+
},
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// --- Manifest shapes (the subset this runtime reads) -----------------------
|
|
67
|
+
|
|
68
|
+
/** One `llm` registry entry (`nano.app.json` → `llm.<id>`). */
|
|
69
|
+
export interface LlmBinding {
|
|
70
|
+
/** Provider selector. `"env"` resolves the endpoint from NANO_APP_LLM_* env. */
|
|
71
|
+
provider: string;
|
|
72
|
+
/** Model id, typically an env template (e.g. `${NANO_APP_LLM_MODEL}`). */
|
|
73
|
+
model: string;
|
|
74
|
+
/** Constrains the structured output — currently a DMN decision id (the rails). */
|
|
75
|
+
output?: { decision?: string };
|
|
76
|
+
/** Action-API tools the agent may call (chat-agent role; unused by role 1). */
|
|
77
|
+
tools?: string[];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** One `workers[]` entry. Role 1 handles those with an `llm` binding. */
|
|
81
|
+
export interface WorkerDef {
|
|
82
|
+
taskType: string;
|
|
83
|
+
handler?: string;
|
|
84
|
+
llm?: string;
|
|
85
|
+
outputType?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** The manifest subset `startLlmWorkers` needs. */
|
|
89
|
+
export interface AppManifest {
|
|
90
|
+
workers?: WorkerDef[];
|
|
91
|
+
llm?: Record<string, LlmBinding>;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/** A resolved provider endpoint (OpenAI-compatible chat completions). */
|
|
95
|
+
export interface ProviderConfig {
|
|
96
|
+
baseUrl: string;
|
|
97
|
+
apiKey?: string;
|
|
98
|
+
model: string;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** One chat message on the wire. */
|
|
102
|
+
export interface ChatMessage {
|
|
103
|
+
role: "system" | "user" | "assistant";
|
|
104
|
+
content: string;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
type Env = (key: string) => string | undefined;
|
|
108
|
+
|
|
109
|
+
// Default environment reader: Deno first, Node fallback. Mirrors worker-sdk's RT
|
|
110
|
+
// adapter but only needs env reads here.
|
|
111
|
+
const defaultEnv: Env = (key) => {
|
|
112
|
+
const g = globalThis as unknown as {
|
|
113
|
+
Deno?: { env: { get(k: string): string | undefined } };
|
|
114
|
+
process?: { env: Record<string, string | undefined> };
|
|
115
|
+
};
|
|
116
|
+
if (g.Deno) return g.Deno.env.get(key);
|
|
117
|
+
if (g.process) return g.process.env[key];
|
|
118
|
+
return undefined;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
// --- Provider resolution ---------------------------------------------------
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Resolve an OpenAI-compatible endpoint for a binding. `provider: "env"` (the
|
|
125
|
+
* only built-in today) reads:
|
|
126
|
+
* - NANO_APP_LLM_BASE_URL (default http://localhost:11434/v1 — Ollama's
|
|
127
|
+
* OpenAI-compatible endpoint; override for any host)
|
|
128
|
+
* - NANO_APP_LLM_API_KEY (optional; sent as `Authorization: Bearer` if set)
|
|
129
|
+
* - NANO_APP_LLM_MODEL (fallback when the binding's `model` resolves empty)
|
|
130
|
+
* The binding's `model` is env-templated (`${VAR}` / `${VAR:-default}`).
|
|
131
|
+
*/
|
|
132
|
+
export function resolveProvider(
|
|
133
|
+
binding: LlmBinding,
|
|
134
|
+
env: Env = defaultEnv,
|
|
135
|
+
): ProviderConfig {
|
|
136
|
+
const baseUrl = (env("NANO_APP_LLM_BASE_URL") ?? "http://localhost:11434/v1")
|
|
137
|
+
.replace(
|
|
138
|
+
/\/+$/,
|
|
139
|
+
"",
|
|
140
|
+
);
|
|
141
|
+
const apiKey = env("NANO_APP_LLM_API_KEY") || undefined;
|
|
142
|
+
const model = resolveEnvTemplate(binding.model ?? "", env) ||
|
|
143
|
+
env("NANO_APP_LLM_MODEL") || "";
|
|
144
|
+
if (!model) {
|
|
145
|
+
throw new Error(
|
|
146
|
+
`llm worker: no model resolved for binding (model="${binding.model}"); set NANO_APP_LLM_MODEL or a literal model`,
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
return { baseUrl, apiKey, model };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// --- Prompt construction ---------------------------------------------------
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Build the chat messages for a job. Input contract (job variables):
|
|
156
|
+
* - `messages`: a ready `{role,content}[]` (used verbatim), or
|
|
157
|
+
* - `prompt`: a user-message string.
|
|
158
|
+
* An optional `system` string (variable or custom header) is prepended.
|
|
159
|
+
*/
|
|
160
|
+
export function buildMessages(job: LlmJob): ChatMessage[] {
|
|
161
|
+
const v = job.variables;
|
|
162
|
+
const system = typeof v.system === "string"
|
|
163
|
+
? v.system
|
|
164
|
+
: typeof job.customHeaders.system === "string"
|
|
165
|
+
? (job.customHeaders.system as string)
|
|
166
|
+
: undefined;
|
|
167
|
+
|
|
168
|
+
let msgs: ChatMessage[];
|
|
169
|
+
if (Array.isArray(v.messages)) {
|
|
170
|
+
msgs = (v.messages as unknown[]).map((m, i) => {
|
|
171
|
+
const o = m as { role?: unknown; content?: unknown };
|
|
172
|
+
if (
|
|
173
|
+
typeof o.content !== "string" ||
|
|
174
|
+
(o.role !== "user" && o.role !== "assistant" && o.role !== "system")
|
|
175
|
+
) {
|
|
176
|
+
throw new Error(
|
|
177
|
+
`llm worker: messages[${i}] must be { role: user|assistant|system, content: string }`,
|
|
178
|
+
);
|
|
179
|
+
}
|
|
180
|
+
return { role: o.role, content: o.content };
|
|
181
|
+
});
|
|
182
|
+
} else if (typeof v.prompt === "string") {
|
|
183
|
+
msgs = [{ role: "user", content: v.prompt }];
|
|
184
|
+
} else {
|
|
185
|
+
throw new Error(
|
|
186
|
+
"llm worker: job needs a 'prompt' string or a 'messages' array in its variables",
|
|
187
|
+
);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
return system ? [{ role: "system", content: system }, ...msgs] : msgs;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
// --- LLM + decision calls --------------------------------------------------
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Call the model's chat-completions endpoint. When `json` is set, requests a
|
|
197
|
+
* JSON object response (`response_format`) so structured output can be parsed.
|
|
198
|
+
* Returns the assistant message text.
|
|
199
|
+
*/
|
|
200
|
+
export async function callLlm(
|
|
201
|
+
provider: ProviderConfig,
|
|
202
|
+
messages: ChatMessage[],
|
|
203
|
+
opts: { json?: boolean; fetch?: typeof fetch } = {},
|
|
204
|
+
): Promise<string> {
|
|
205
|
+
const doFetch = opts.fetch ?? fetch;
|
|
206
|
+
const headers: Record<string, string> = {
|
|
207
|
+
"content-type": "application/json",
|
|
208
|
+
};
|
|
209
|
+
if (provider.apiKey) headers.authorization = `Bearer ${provider.apiKey}`;
|
|
210
|
+
const body: Record<string, unknown> = { model: provider.model, messages };
|
|
211
|
+
if (opts.json) body.response_format = { type: "json_object" };
|
|
212
|
+
|
|
213
|
+
const res = await doFetch(`${provider.baseUrl}/chat/completions`, {
|
|
214
|
+
method: "POST",
|
|
215
|
+
headers,
|
|
216
|
+
body: JSON.stringify(body),
|
|
217
|
+
});
|
|
218
|
+
if (!res.ok) {
|
|
219
|
+
const detail = await res.text().catch(() => "");
|
|
220
|
+
throw new Error(
|
|
221
|
+
`llm provider returned ${res.status}: ${detail.slice(0, 500)}`,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
const data = (await res.json()) as {
|
|
225
|
+
choices?: Array<{ message?: { content?: string } }>;
|
|
226
|
+
};
|
|
227
|
+
const content = data.choices?.[0]?.message?.content;
|
|
228
|
+
if (typeof content !== "string") {
|
|
229
|
+
throw new Error("llm provider returned no message content");
|
|
230
|
+
}
|
|
231
|
+
return content;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Evaluate a DMN decision on the engine (the output "rails"), returning its
|
|
236
|
+
* `output`. Uses the same engine base URL the workers connect to.
|
|
237
|
+
*/
|
|
238
|
+
export async function evaluateDecision(
|
|
239
|
+
engineBaseUrl: string,
|
|
240
|
+
decisionId: string,
|
|
241
|
+
variables: Record<string, unknown>,
|
|
242
|
+
doFetch: typeof fetch = fetch,
|
|
243
|
+
): Promise<unknown> {
|
|
244
|
+
const base = engineBaseUrl.replace(/\/+$/, "").replace(/\/v2$/, "");
|
|
245
|
+
const res = await doFetch(`${base}/v2/decision-definitions/evaluation`, {
|
|
246
|
+
method: "POST",
|
|
247
|
+
headers: { "content-type": "application/json" },
|
|
248
|
+
body: JSON.stringify({ decisionDefinitionId: decisionId, variables }),
|
|
249
|
+
});
|
|
250
|
+
if (!res.ok) {
|
|
251
|
+
const detail = await res.text().catch(() => "");
|
|
252
|
+
throw new Error(
|
|
253
|
+
`decision '${decisionId}' evaluation failed ${res.status}: ${
|
|
254
|
+
detail.slice(0, 500)
|
|
255
|
+
}`,
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
const data = (await res.json()) as { output?: unknown };
|
|
259
|
+
return data.output;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// --- Job handler -----------------------------------------------------------
|
|
263
|
+
|
|
264
|
+
export interface LlmWorkerRuntime {
|
|
265
|
+
engineBaseUrl: string;
|
|
266
|
+
env?: Env;
|
|
267
|
+
fetch?: typeof fetch;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Run one job through the LLM (and optional decision rails). Returns the output
|
|
272
|
+
* variables the job is completed with.
|
|
273
|
+
*
|
|
274
|
+
* - No `output` binding → returns `{ text }` (the raw completion).
|
|
275
|
+
* - `output` set → requests JSON, parses it; the parsed object is the output.
|
|
276
|
+
* - `output.decision` set → the parsed JSON is fed to that DMN decision and the
|
|
277
|
+
* decision's output is returned (object as-is, scalar wrapped as `{ result }`).
|
|
278
|
+
*/
|
|
279
|
+
export async function runLlmJob(
|
|
280
|
+
job: LlmJob,
|
|
281
|
+
binding: LlmBinding,
|
|
282
|
+
rt: LlmWorkerRuntime,
|
|
283
|
+
): Promise<Record<string, unknown>> {
|
|
284
|
+
const wantJson = !!binding.output;
|
|
285
|
+
const provider = resolveProvider(binding, rt.env ?? defaultEnv);
|
|
286
|
+
const messages = buildMessages(job);
|
|
287
|
+
const text = await callLlm(provider, messages, {
|
|
288
|
+
json: wantJson,
|
|
289
|
+
fetch: rt.fetch,
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
if (!wantJson) return { text };
|
|
293
|
+
|
|
294
|
+
let parsed: Record<string, unknown>;
|
|
295
|
+
try {
|
|
296
|
+
parsed = JSON.parse(text) as Record<string, unknown>;
|
|
297
|
+
} catch {
|
|
298
|
+
throw new Error(
|
|
299
|
+
`llm worker: expected JSON output but got: ${text.slice(0, 300)}`,
|
|
300
|
+
);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (binding.output?.decision) {
|
|
304
|
+
const out = await evaluateDecision(
|
|
305
|
+
rt.engineBaseUrl,
|
|
306
|
+
binding.output.decision,
|
|
307
|
+
parsed,
|
|
308
|
+
rt.fetch,
|
|
309
|
+
);
|
|
310
|
+
return out !== null && typeof out === "object"
|
|
311
|
+
? (out as Record<string, unknown>)
|
|
312
|
+
: { result: out };
|
|
313
|
+
}
|
|
314
|
+
return parsed;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// --- Registration ----------------------------------------------------------
|
|
318
|
+
|
|
319
|
+
/** Injectable worker registrar, so `startLlmWorkers` is unit-testable. Structural
|
|
320
|
+
* so this module needs no runtime import of the worker SDK. */
|
|
321
|
+
export type DefineWorkerFn = (opts: LlmWorkerOptions) => void;
|
|
322
|
+
|
|
323
|
+
/** Register one LLM worker on `taskType`. `define` is the worker registrar
|
|
324
|
+
* (`defineWorker` from the materialised worker SDK, or a test double). */
|
|
325
|
+
export function defineLlmWorker(
|
|
326
|
+
taskType: string,
|
|
327
|
+
binding: LlmBinding,
|
|
328
|
+
rt: LlmWorkerRuntime,
|
|
329
|
+
define: DefineWorkerFn,
|
|
330
|
+
): void {
|
|
331
|
+
define({
|
|
332
|
+
type: taskType,
|
|
333
|
+
handle: (job) => runLlmJob(job, binding, rt),
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
export interface StartLlmWorkersOptions {
|
|
338
|
+
/** The manifest object; when omitted, read from `manifestPath`. */
|
|
339
|
+
manifest?: AppManifest;
|
|
340
|
+
/** Path to `nano.app.json` (default `./nano.app.json`). */
|
|
341
|
+
manifestPath?: string;
|
|
342
|
+
/** Engine base URL (default env NANOBPMN_BASE_URL or http://127.0.0.1:8080). */
|
|
343
|
+
baseUrl?: string;
|
|
344
|
+
/** Env reader (default Deno/Node). */
|
|
345
|
+
env?: Env;
|
|
346
|
+
/** Fetch impl (default global). */
|
|
347
|
+
fetch?: typeof fetch;
|
|
348
|
+
/** Worker registrar (default defineWorker; injected in tests). */
|
|
349
|
+
define?: DefineWorkerFn;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* Start every LLM-as-worker declared in the App manifest. For each `workers[]`
|
|
354
|
+
* entry with an `llm` binding, resolves `llm.<id>` and registers a Falcon worker
|
|
355
|
+
* on its `taskType`. Returns the task types started. Missing bindings throw so a
|
|
356
|
+
* misconfigured manifest fails loudly at startup rather than at first job.
|
|
357
|
+
*/
|
|
358
|
+
export async function startLlmWorkers(
|
|
359
|
+
opts: StartLlmWorkersOptions = {},
|
|
360
|
+
): Promise<string[]> {
|
|
361
|
+
const env = opts.env ?? defaultEnv;
|
|
362
|
+
let manifest = opts.manifest;
|
|
363
|
+
if (!manifest) {
|
|
364
|
+
const path = opts.manifestPath ?? "./nano.app.json";
|
|
365
|
+
const text = await (globalThis as unknown as {
|
|
366
|
+
Deno?: { readTextFile(p: string): Promise<string> };
|
|
367
|
+
}).Deno?.readTextFile(path);
|
|
368
|
+
if (text === undefined) {
|
|
369
|
+
throw new Error(
|
|
370
|
+
"startLlmWorkers: no manifest provided and Deno.readTextFile unavailable",
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
manifest = JSON.parse(text) as AppManifest;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const engineBaseUrl = opts.baseUrl ?? env("NANOBPMN_BASE_URL") ??
|
|
377
|
+
"http://127.0.0.1:8080";
|
|
378
|
+
// Default registrar: load the materialised worker SDK lazily via a *variable*
|
|
379
|
+
// specifier so this static reference is not analysed in the source tree (where
|
|
380
|
+
// the hyphenated sibling does not exist). Tests inject `define` and never hit
|
|
381
|
+
// this path.
|
|
382
|
+
let define = opts.define;
|
|
383
|
+
if (!define) {
|
|
384
|
+
const spec = "./worker-sdk.ts";
|
|
385
|
+
const mod = (await import(spec)) as { defineWorker: DefineWorkerFn };
|
|
386
|
+
define = mod.defineWorker;
|
|
387
|
+
}
|
|
388
|
+
const rt: LlmWorkerRuntime = { engineBaseUrl, env, fetch: opts.fetch };
|
|
389
|
+
|
|
390
|
+
const started: string[] = [];
|
|
391
|
+
for (const w of manifest.workers ?? []) {
|
|
392
|
+
if (!w.llm) continue; // handler workers are started by startWorkers()
|
|
393
|
+
const binding = manifest.llm?.[w.llm];
|
|
394
|
+
if (!binding) {
|
|
395
|
+
throw new Error(
|
|
396
|
+
`worker '${w.taskType}' references unknown llm binding '${w.llm}'`,
|
|
397
|
+
);
|
|
398
|
+
}
|
|
399
|
+
defineLlmWorker(w.taskType, binding, rt, define);
|
|
400
|
+
started.push(w.taskType);
|
|
401
|
+
}
|
|
402
|
+
return started;
|
|
403
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// AUTO-GENERATED by nanobpmn from the process model (ADR 0040 slice 2).
|
|
2
|
+
// The bridge from a `bpmn:message`'s data envelope to the message type system:
|
|
3
|
+
// each named message maps to the TS type of its payload, so the typed
|
|
4
|
+
// `publishMessage` types a call by its message name. Do not edit — regenerated
|
|
5
|
+
// from the model. Erased to plain JS at compile.
|
|
6
|
+
// eslint-disable
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
/** Untyped fallback payload for a message that declares no envelope type. */
|
|
10
|
+
export type MessageVars = Record<string, unknown>;
|
|
11
|
+
|
|
12
|
+
/** Every declared message `name` (ADR 0040 slice 2): the model-derived set the
|
|
13
|
+
* typed `publishMessage` accepts, so `name` autocompletes and rejects unknown messages. */
|
|
14
|
+
export type MessageName = string;
|
|
15
|
+
|
|
16
|
+
/** Payload (`variables`) per declared message, keyed by `name`. */
|
|
17
|
+
export interface MessagePayloads {}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
// AUTO-GENERATED by nanobpmn (ADR 0040 slice 2): the typed `publishMessage`.
|
|
2
|
+
// Re-exports the worker SDK's `publishMessage` and overrides it with a
|
|
3
|
+
// messageName-keyed typed signature (variables typed from the message's declared
|
|
4
|
+
// envelope). Erased to a pass-through at runtime. Do not edit.
|
|
5
|
+
// eslint-disable
|
|
6
|
+
|
|
7
|
+
import { publishMessage as publishMessageRaw } from "./worker-sdk.ts";
|
|
8
|
+
import type { PublishMessageOptions, PublishMessageResult } from "./worker-sdk.ts";
|
|
9
|
+
import type { MessageName, MessagePayloads, MessageVars } from "./message-io.d.ts";
|
|
10
|
+
|
|
11
|
+
export type { PublishMessageOptions, PublishMessageResult } from "./worker-sdk.ts";
|
|
12
|
+
|
|
13
|
+
type PayloadFor<K extends MessageName> = K extends keyof MessagePayloads ? MessagePayloads[K] : MessageVars;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Typed `publishMessage`: `name` is constrained to the model's declared message
|
|
17
|
+
* names (`MessageName`, ADR 0040 slice 2) so it autocompletes and rejects unknown
|
|
18
|
+
* messages, and `variables` are typed from the message's declared envelope. A
|
|
19
|
+
* declared message with no domain type falls back to MessageVars.
|
|
20
|
+
*/
|
|
21
|
+
export function publishMessage<K extends MessageName>(
|
|
22
|
+
name: K,
|
|
23
|
+
opts?: PublishMessageOptions<PayloadFor<K> & object>,
|
|
24
|
+
): Promise<PublishMessageResult> {
|
|
25
|
+
return publishMessageRaw(name, opts as unknown as PublishMessageOptions);
|
|
26
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
// AUTO-GENERATED by nanobpmn from the process model (ADR 0040 §5): the typed
|
|
2
|
+
// model-metadata accessor. Each `nano:meta` key/value the models declare becomes
|
|
3
|
+
// a checked `AppMeta` property, so `meta(key)` is typed and app-wide queryable
|
|
4
|
+
// without a second store. Do not edit — regenerated from the model.
|
|
5
|
+
// eslint-disable
|
|
6
|
+
|
|
7
|
+
/** Every model-level metadata key the App's models declare (ADR 0040 §5). */
|
|
8
|
+
export interface AppMeta {}
|
|
9
|
+
|
|
10
|
+
export const appMeta: AppMeta = Object.create(null) as AppMeta;
|
|
11
|
+
|
|
12
|
+
/** Read a model-level metadata value. A declared key is typed; a dynamic key
|
|
13
|
+
* returns `string | undefined`. */
|
|
14
|
+
export function meta<K extends keyof AppMeta>(key: K): AppMeta[K];
|
|
15
|
+
export function meta(key: string): string | undefined;
|
|
16
|
+
export function meta(key: string): string | undefined {
|
|
17
|
+
return (appMeta as Record<string, string>)[key];
|
|
18
|
+
}
|