@sellable/install 0.1.794 → 0.1.795-wip.runtime-ux.20261004.1

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.
@@ -0,0 +1,98 @@
1
+ // Shared by the control plane and the runtime. A custom Runs API is only a
2
+ // requested destination here; the runtime must also match a trusted connection.
3
+ export const ENDPOINT_MODEL_SELECTION_ALLOWLIST = Object.freeze({
4
+ openrouter: Object.freeze([
5
+ // Retained for existing saved selections; the UI shortlist excludes Opus.
6
+ "anthropic/claude-opus-5.5",
7
+ "deepseek/deepseek-v4-flash-0731",
8
+ "openai/gpt-6-luna",
9
+ "anthropic/claude-sonnet-5.5",
10
+ ]),
11
+ });
12
+ export const ENDPOINT_MODEL_REASONING_EFFORTS = Object.freeze([
13
+ "none",
14
+ "low",
15
+ "medium",
16
+ "high",
17
+ "xhigh",
18
+ ]);
19
+
20
+ export function normalizeRunsApiBaseUrl(value) {
21
+ if (
22
+ typeof value !== "string" ||
23
+ value.length > 1_024 ||
24
+ value !== value.trim()
25
+ )
26
+ return null;
27
+ try {
28
+ const url = new URL(value);
29
+ if (
30
+ url.username ||
31
+ url.password ||
32
+ url.search ||
33
+ url.hash ||
34
+ /[\\\s]/.test(value) ||
35
+ (url.protocol !== "https:" &&
36
+ !(
37
+ url.protocol === "http:" &&
38
+ ["127.0.0.1", "[::1]"].includes(url.hostname)
39
+ ))
40
+ )
41
+ return null;
42
+ return `${url.origin}${url.pathname.replace(/\/$/, "")}`;
43
+ } catch {
44
+ return null;
45
+ }
46
+ }
47
+
48
+ export function compileEndpointModelSelection(value) {
49
+ if (value === undefined || value === null) return null;
50
+ if (typeof value !== "object" || Array.isArray(value)) return "invalid";
51
+ const custom = Object.hasOwn(value, "runsApiBaseUrl");
52
+ const hasFallbackPolicy = Object.hasOwn(value, "fallbackPolicy");
53
+ if (
54
+ Object.keys(value)
55
+ .filter((key) => key !== "fallbackPolicy")
56
+ .sort()
57
+ .join(",") !==
58
+ (custom
59
+ ? "model,provider,reasoningEffort,runsApiBaseUrl"
60
+ : "model,provider,reasoningEffort")
61
+ )
62
+ return "invalid";
63
+ if (
64
+ hasFallbackPolicy &&
65
+ !["STOP", "PRODUCT_DEFAULT"].includes(value.fallbackPolicy)
66
+ )
67
+ return "invalid";
68
+ const { provider, model, reasoningEffort } = value;
69
+ if (
70
+ typeof provider !== "string" ||
71
+ typeof model !== "string" ||
72
+ !ENDPOINT_MODEL_REASONING_EFFORTS.includes(reasoningEffort)
73
+ )
74
+ return "invalid";
75
+ if (
76
+ !custom &&
77
+ (!Object.hasOwn(ENDPOINT_MODEL_SELECTION_ALLOWLIST, provider) ||
78
+ !ENDPOINT_MODEL_SELECTION_ALLOWLIST[provider].includes(model))
79
+ )
80
+ return "invalid";
81
+ const runsApiBaseUrl = custom
82
+ ? normalizeRunsApiBaseUrl(value.runsApiBaseUrl)
83
+ : null;
84
+ if (
85
+ custom &&
86
+ (!runsApiBaseUrl ||
87
+ !/^[a-z][a-z0-9_-]{0,63}$/.test(provider) ||
88
+ !/^[A-Za-z0-9][A-Za-z0-9._:/-]{0,199}$/.test(model))
89
+ )
90
+ return "invalid";
91
+ return Object.freeze({
92
+ provider,
93
+ model,
94
+ reasoningEffort,
95
+ ...(custom ? { runsApiBaseUrl } : {}),
96
+ ...(hasFallbackPolicy ? { fallbackPolicy: value.fallbackPolicy } : {}),
97
+ });
98
+ }
@@ -7,6 +7,11 @@ import {
7
7
  writeFileSync,
8
8
  } from "node:fs";
9
9
  import { dirname, isAbsolute } from "node:path";
10
+ import {
11
+ compileEndpointModelSelection,
12
+ normalizeRunsApiBaseUrl,
13
+ } from "./endpoint-model-selection.mjs";
14
+ export { ENDPOINT_MODEL_SELECTION_ALLOWLIST } from "./endpoint-model-selection.mjs";
10
15
 
11
16
  const LOOPBACK_ORIGIN = "http://127.0.0.1:8642";
12
17
  const RUN_ID = /^run_[a-f0-9]{32}$/;
@@ -206,37 +211,12 @@ function canonicalHistory(value) {
206
211
  return result;
207
212
  }
208
213
 
209
- // The models a caller may name for one endpoint run. The server checks the
210
- // same list before it hands the run over; the runtime checks it again here so
211
- // a run can never reach Hermes with a model that was not reviewed.
212
- export const ENDPOINT_MODEL_SELECTION_ALLOWLIST = Object.freeze({
213
- openrouter: Object.freeze([
214
- "anthropic/claude-opus-5.5",
215
- "anthropic/claude-sonnet-5.5",
216
- ]),
217
- });
218
- const ENDPOINT_MODEL_REASONING_EFFORTS = Object.freeze([
219
- "low",
220
- "medium",
221
- "high",
222
- "xhigh",
223
- ]);
224
-
225
214
  export function validateEndpointModelSelection(value) {
226
- if (
227
- !exactObject(value) ||
228
- Object.keys(value).sort().join(",") !== "model,provider,reasoningEffort" ||
229
- !Object.hasOwn(ENDPOINT_MODEL_SELECTION_ALLOWLIST, value.provider) ||
230
- !ENDPOINT_MODEL_SELECTION_ALLOWLIST[value.provider].includes(value.model) ||
231
- !ENDPOINT_MODEL_REASONING_EFFORTS.includes(value.reasoningEffort)
232
- ) {
215
+ const compiled = compileEndpointModelSelection(value);
216
+ if (!compiled || compiled === "invalid") {
233
217
  throw new Error("hermes_runs_model_selection_rejected");
234
218
  }
235
- return Object.freeze({
236
- provider: value.provider,
237
- model: value.model,
238
- reasoningEffort: value.reasoningEffort,
239
- });
219
+ return compiled;
240
220
  }
241
221
 
242
222
  export function validateOpenerOutputContract(value) {
@@ -264,7 +244,7 @@ export function validateOpenerOutputContract(value) {
264
244
  return JSON.parse(stableJson(value));
265
245
  }
266
246
 
267
- function compileCreateBody(input, openerStage = false) {
247
+ function compileCreateBody(input, openerStage = false, verification = false) {
268
248
  if (!exactObject(input)) throw new Error("hermes_runs_submit_rejected");
269
249
  const allowed = [
270
250
  "conversationHistory",
@@ -272,7 +252,7 @@ function compileCreateBody(input, openerStage = false) {
272
252
  "instructions",
273
253
  "correlation",
274
254
  "outputContract",
275
- ...(openerStage ? [] : ["modelSelection"]),
255
+ ...(openerStage && !verification ? [] : ["modelSelection"]),
276
256
  ];
277
257
  if (Object.keys(input).some((key) => !allowed.includes(key))) {
278
258
  throw new Error("hermes_runs_submit_rejected");
@@ -606,12 +586,19 @@ function submitNotAdmitted(response, value) {
606
586
  export function createHermesRunsClient(options = {}) {
607
587
  const {
608
588
  apiKeyFile,
589
+ baseUrl = LOOPBACK_ORIGIN,
590
+ scopeHeaders = {},
609
591
  fixture,
610
592
  fetchImpl = fetch,
611
593
  timeoutMs = 20_000,
612
594
  } = options;
613
595
  assertFixture(fixture);
596
+ const origin = normalizeRunsApiBaseUrl(baseUrl);
614
597
  if (
598
+ !origin ||
599
+ !exactObject(scopeHeaders) ||
600
+ Object.keys(scopeHeaders).some(key => !["x-sellable-workspace-id", "x-sellable-agent-id"].includes(key)) ||
601
+ Object.values(scopeHeaders).some(value => typeof value !== "string" || !/^[A-Za-z0-9_-]{1,128}$/.test(value)) ||
615
602
  typeof fetchImpl !== "function" ||
616
603
  !Number.isSafeInteger(timeoutMs) ||
617
604
  timeoutMs < 100 ||
@@ -624,9 +611,11 @@ export function createHermesRunsClient(options = {}) {
624
611
  if (!path.startsWith("/v1/runs")) {
625
612
  throw new Error("hermes_runs_path_rejected");
626
613
  }
627
- return fetchImpl(`${LOOPBACK_ORIGIN}${path}`, {
614
+ return fetchImpl(`${origin}${path}`, {
628
615
  ...init,
616
+ redirect: "error",
629
617
  headers: {
618
+ ...scopeHeaders,
630
619
  authorization: `Bearer ${apiKey}`,
631
620
  ...(init.body === undefined
632
621
  ? {}
@@ -635,8 +624,8 @@ export function createHermesRunsClient(options = {}) {
635
624
  signal: init.signal ?? AbortSignal.timeout(timeoutMs),
636
625
  });
637
626
  };
638
- const submit = async (compiled, { signal } = {}, openerStage = false) => {
639
- const body = compileCreateBody(compiled, openerStage);
627
+ const submit = async (compiled, { signal } = {}, openerStage = false, verification = false) => {
628
+ const body = compileCreateBody(compiled, openerStage, verification);
640
629
  let response;
641
630
  try {
642
631
  response = await request("/v1/runs", {
@@ -678,6 +667,8 @@ export function createHermesRunsClient(options = {}) {
678
667
  return Object.freeze({
679
668
  fixtureIdentity: HERMES_RUNS_FIXTURE_IDENTITY,
680
669
  submit: (compiled, options) => submit(compiled, options),
670
+ // Fixed operator verification uses the existing tool-free session policy.
671
+ submitVerification: (compiled, options) => submit(compiled, options, true, true),
681
672
  // Only this reserved session prefix receives the pinned tool-free bridge policy.
682
673
  submitOpenerStage: (compiled, options) => submit(compiled, options, true),
683
674
  async status(runId, { signal } = {}) {
@@ -0,0 +1,7 @@
1
+ import type { RunsApiConnection } from "./runs-api-connections.mjs";
2
+ export function createOpenAiRunsClient(options: {
3
+ connection: RunsApiConnection;
4
+ responseSchema?: unknown;
5
+ modelSelection?: { provider: string; model: string; reasoningEffort: string };
6
+ fetchImpl?: typeof fetch;
7
+ }): any;
@@ -0,0 +1,271 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { createHermesRunsClient } from "./hermes-runs-client.mjs";
3
+
4
+ async function boundedJson(response) {
5
+ const reader = response.body?.getReader();
6
+ if (!reader) throw new Error("empty_response");
7
+ let size = 0;
8
+ const chunks = [];
9
+ try {
10
+ while (true) {
11
+ const value = await reader.read();
12
+ if (value.done) break;
13
+ size += value.value.byteLength;
14
+ if (size > 512 * 1024) {
15
+ await reader.cancel();
16
+ throw new Error("response_too_large");
17
+ }
18
+ chunks.push(value.value);
19
+ }
20
+ } finally {
21
+ reader.releaseLock();
22
+ }
23
+ return JSON.parse(Buffer.concat(chunks).toString("utf8"));
24
+ }
25
+
26
+ /** Text/structured-output adapter. All calls remain on the approved origin;
27
+ * tools, retries and provider/model fallback are deliberately not synthesized.
28
+ * The existing native client still validates the normalized run lifecycle. */
29
+ export function createOpenAiRunsClient({
30
+ connection,
31
+ responseSchema,
32
+ modelSelection,
33
+ fetchImpl = fetch,
34
+ }) {
35
+ const runs = new Map();
36
+ const json = (value, status = 200) =>
37
+ new Response(JSON.stringify(value), {
38
+ status,
39
+ headers: { "content-type": "application/json" },
40
+ });
41
+ const bridge = async (url, init = {}) => {
42
+ const path = String(url).slice(connection.baseUrl.length);
43
+ if (path === "/v1/runs" && init.method === "POST") {
44
+ const request = JSON.parse(init.body);
45
+ if (
46
+ !request.model &&
47
+ modelSelection &&
48
+ request.session_id?.startsWith("sellable_opener_stage_")
49
+ ) {
50
+ request.provider = modelSelection.provider;
51
+ request.model = modelSelection.model;
52
+ request.model_options = {
53
+ ...request.model_options,
54
+ reasoning_effort: modelSelection.reasoningEffort,
55
+ };
56
+ }
57
+ const model = connection.models.find(
58
+ (item) =>
59
+ item.provider === request.provider &&
60
+ item.model === request.model &&
61
+ item.reasoningEfforts.includes(
62
+ request.model_options?.reasoning_effort
63
+ )
64
+ );
65
+ if (!model) return json({ error: "model_not_allowed" }, 403);
66
+ const run = {
67
+ run_id: `run_${randomBytes(16).toString("hex")}`,
68
+ status: "running",
69
+ controller: new AbortController(),
70
+ };
71
+ const schema =
72
+ request.model_options?.sellable_opener_output_contract?.schema ??
73
+ responseSchema;
74
+ const structured = schema && connection.structuredOutputs !== false;
75
+ const instructions =
76
+ structured || !schema
77
+ ? request.instructions
78
+ : `${request.instructions}\nReturn only JSON conforming to this schema:\n${JSON.stringify(schema)}`;
79
+ const history = (request.conversation_history ?? []).map(
80
+ ({ role, content }) => ({ role, content })
81
+ );
82
+ const effort = request.model_options?.reasoning_effort;
83
+ const responses = connection.protocol === "openai-responses";
84
+ const body = responses
85
+ ? {
86
+ model: request.model,
87
+ instructions,
88
+ input: [...history, { role: "user", content: request.input }],
89
+ stream: false,
90
+ store: false,
91
+ max_output_tokens: 4096,
92
+ ...(effort && effort !== "none" ? { reasoning: { effort } } : {}),
93
+ ...(structured
94
+ ? {
95
+ text: {
96
+ format: {
97
+ type: "json_schema",
98
+ name: "endpoint_output",
99
+ strict: true,
100
+ schema,
101
+ },
102
+ },
103
+ }
104
+ : {}),
105
+ }
106
+ : {
107
+ model: request.model,
108
+ messages: [
109
+ { role: "system", content: instructions },
110
+ ...history,
111
+ { role: "user", content: request.input },
112
+ ],
113
+ stream: false,
114
+ max_tokens: 4096,
115
+ ...(effort && effort !== "none"
116
+ ? { reasoning_effort: effort }
117
+ : {}),
118
+ ...(structured
119
+ ? {
120
+ response_format: {
121
+ type: "json_schema",
122
+ json_schema: {
123
+ name: "endpoint_output",
124
+ strict: true,
125
+ schema,
126
+ },
127
+ },
128
+ }
129
+ : {}),
130
+ };
131
+ runs.set(run.run_id, run);
132
+ run.promise = (async () => {
133
+ try {
134
+ const response = await fetchImpl(
135
+ `${connection.baseUrl}/${responses ? "responses" : "chat/completions"}`,
136
+ {
137
+ method: "POST",
138
+ headers: {
139
+ authorization: init.headers.authorization,
140
+ "content-type": "application/json",
141
+ },
142
+ body: JSON.stringify(body),
143
+ redirect: "error",
144
+ signal: AbortSignal.any([
145
+ run.controller.signal,
146
+ AbortSignal.timeout(750_000),
147
+ ]),
148
+ }
149
+ );
150
+ if (!response.ok)
151
+ throw new Error(
152
+ response.status === 401 || response.status === 403
153
+ ? "access_denied"
154
+ : "provider_unavailable"
155
+ );
156
+ const value = await boundedJson(response);
157
+ const output = responses
158
+ ? (value.output ?? [])
159
+ .filter((item) => item.type === "message")
160
+ .flatMap((item) => item.content ?? [])
161
+ .filter((item) => item.type === "output_text")
162
+ .map((item) => item.text)
163
+ .join("")
164
+ : value.choices?.[0]?.message?.content;
165
+ if (
166
+ typeof output !== "string" ||
167
+ !output.trim() ||
168
+ Buffer.byteLength(output) > 48 * 1024 ||
169
+ (responses
170
+ ? value.status !== "completed"
171
+ : value.choices?.[0]?.finish_reason !== "stop") ||
172
+ typeof value.model !== "string" ||
173
+ !/^[A-Za-z0-9][A-Za-z0-9._:/-]{0,127}$/.test(value.model) ||
174
+ (value.model !== request.model &&
175
+ !new RegExp(
176
+ `^${request.model.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}-[0-9]{4}-?[0-9]{2}-?[0-9]{2}$`
177
+ ).test(value.model))
178
+ )
179
+ throw new Error("output_rejected");
180
+ Object.assign(run, {
181
+ status: "completed",
182
+ output,
183
+ runtime: { provider: request.provider, model: value.model },
184
+ usage: {
185
+ input_tokens:
186
+ value.usage?.input_tokens ?? value.usage?.prompt_tokens ?? 0,
187
+ output_tokens:
188
+ value.usage?.output_tokens ??
189
+ value.usage?.completion_tokens ??
190
+ 0,
191
+ },
192
+ });
193
+ } catch (error) {
194
+ Object.assign(run, {
195
+ status: "failed",
196
+ error:
197
+ error.message === "access_denied"
198
+ ? "connection_access_denied"
199
+ : "connection_generation_failed",
200
+ });
201
+ }
202
+ })();
203
+ return json({ run_id: run.run_id, status: "started" }, 202);
204
+ }
205
+ const match = /^\/v1\/runs\/(run_[a-f0-9]{32})(?:\/(events|stop))?$/.exec(
206
+ path
207
+ );
208
+ const run = match && runs.get(match[1]);
209
+ if (!run) return json({ error: "run_missing" }, 404);
210
+ const snapshot = () => ({
211
+ run_id: run.run_id,
212
+ status: run.status,
213
+ ...(run.status === "completed"
214
+ ? { output: run.output, runtime: run.runtime, usage: run.usage }
215
+ : run.status === "failed"
216
+ ? { error: run.error }
217
+ : {}),
218
+ });
219
+ if (match[2] === "stop") {
220
+ run.controller.abort();
221
+ // Aborting HTTP cannot prove cancellation at a remote inference server.
222
+ return json({ error: "stop_uncertain" }, 409);
223
+ }
224
+ if (match[2] === "events") {
225
+ if (init.signal?.aborted) throw new Error("stream_aborted");
226
+ let closed = false;
227
+ let abort;
228
+ const cleanup = () => {
229
+ closed = true;
230
+ init.signal?.removeEventListener("abort", abort);
231
+ };
232
+ return new Response(
233
+ new ReadableStream({
234
+ start(controller) {
235
+ abort = () => {
236
+ if (!closed) {
237
+ cleanup();
238
+ controller.error(new Error("stream_aborted"));
239
+ }
240
+ };
241
+ init.signal?.addEventListener("abort", abort, { once: true });
242
+ if (init.signal?.aborted) {
243
+ abort();
244
+ return;
245
+ }
246
+ void run.promise.then(() => {
247
+ if (closed) return;
248
+ cleanup();
249
+ controller.enqueue(
250
+ new TextEncoder().encode(
251
+ `data: ${JSON.stringify({ event: run.status === "completed" ? "run.completed" : "run.failed", timestamp: new Date().toISOString(), ...snapshot() })}\n\n`
252
+ )
253
+ );
254
+ controller.close();
255
+ });
256
+ },
257
+ cancel() {
258
+ cleanup();
259
+ },
260
+ }),
261
+ { headers: { "content-type": "text/event-stream" } }
262
+ );
263
+ }
264
+ return json(snapshot());
265
+ };
266
+ return createHermesRunsClient({
267
+ baseUrl: connection.baseUrl,
268
+ apiKeyFile: connection.apiKeyFile,
269
+ fetchImpl: bridge,
270
+ });
271
+ }
@@ -0,0 +1,30 @@
1
+ export const RUNS_API_CONNECTIONS_ENV: "SELLABLE_AGENT_RUNS_API_CONNECTIONS_JSON";
2
+ export type RunsApiConnection = Readonly<{
3
+ runtimeId: string;
4
+ workspaceId: string;
5
+ agentId: string;
6
+ baseUrl: string;
7
+ apiKeyFile: string;
8
+ endpointIds: string[];
9
+ workKinds: Array<"STANDARD" | "OPENER" | "REPLY" | "FOLLOW_UP">;
10
+ models: Array<{
11
+ provider: string;
12
+ model: string;
13
+ reasoningEfforts: string[];
14
+ }>;
15
+ localRoot?: string;
16
+ name?: string;
17
+ protocol?: "runs-api" | "openai-chat" | "openai-responses";
18
+ structuredOutputs?: boolean;
19
+ }>;
20
+ export function runsApiConnectionId(connection: RunsApiConnection): string;
21
+ export function parseRunsApiConnections(raw: unknown): RunsApiConnection[];
22
+ export function runsApiConnectionsForRuntime(
23
+ raw: unknown,
24
+ runtime: {
25
+ runtimeId: string;
26
+ workspaceId: string;
27
+ agentId: string;
28
+ }
29
+ ): RunsApiConnection[];
30
+ export function createConfiguredRunsClient(options: any): any;