@blastlauncher/runtime 3.0.0-alpha.1 → 3.0.0-alpha.3

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.
@@ -26,6 +26,7 @@ export declare const AI: {
26
26
  readonly "OpenAI_GPT-6_Astra": "openai-gpt-6-astra";
27
27
  readonly "OpenAI_GPT-6_Sol": "openai-gpt-6-sol";
28
28
  readonly "OpenAI_GPT-6.1_Sol": "openai-gpt-6.1-sol";
29
+ readonly "OpenAI_GPT-6.1_Terra": "openai-gpt-6.1-terra";
29
30
  readonly "OpenAI_GPT-6_Luna": "openai-gpt-6-luna";
30
31
  readonly "Groq_GPT-OSS_20b": "groq-openai/gpt-oss-20b";
31
32
  readonly "Groq_GPT-OSS_120b": "groq-openai/gpt-oss-120b";
@@ -55,6 +56,7 @@ export declare const AI: {
55
56
  readonly "Vercel_GLM-5.3_Flash": "gateway-zai/glm-5.3-flash";
56
57
  readonly "Vercel_Kimi_K2.7_Code": "gateway-moonshotai/kimi-k2.7-code";
57
58
  readonly Vercel_Kimi_K3: "gateway-moonshotai/kimi-k3";
59
+ readonly "Vercel_Kimi_K3.1_Pro": "gateway-moonshotai/kimi-k3.1-pro";
58
60
  readonly Vercel_Gemma_4_31B: "gateway-google/gemma-4-31b-it";
59
61
  readonly Vercel_Inkling: "gateway-thinkingmachines/inkling";
60
62
  readonly Vercel_Inkling_Small: "gateway-thinkingmachines/inkling-small";
@@ -214,6 +216,7 @@ export declare const unstable_AI: {
214
216
  readonly "OpenAI_GPT-6_Astra": "openai-gpt-6-astra";
215
217
  readonly "OpenAI_GPT-6_Sol": "openai-gpt-6-sol";
216
218
  readonly "OpenAI_GPT-6.1_Sol": "openai-gpt-6.1-sol";
219
+ readonly "OpenAI_GPT-6.1_Terra": "openai-gpt-6.1-terra";
217
220
  readonly "OpenAI_GPT-6_Luna": "openai-gpt-6-luna";
218
221
  readonly "Groq_GPT-OSS_20b": "groq-openai/gpt-oss-20b";
219
222
  readonly "Groq_GPT-OSS_120b": "groq-openai/gpt-oss-120b";
@@ -243,6 +246,7 @@ export declare const unstable_AI: {
243
246
  readonly "Vercel_GLM-5.3_Flash": "gateway-zai/glm-5.3-flash";
244
247
  readonly "Vercel_Kimi_K2.7_Code": "gateway-moonshotai/kimi-k2.7-code";
245
248
  readonly Vercel_Kimi_K3: "gateway-moonshotai/kimi-k3";
249
+ readonly "Vercel_Kimi_K3.1_Pro": "gateway-moonshotai/kimi-k3.1-pro";
246
250
  readonly Vercel_Gemma_4_31B: "gateway-google/gemma-4-31b-it";
247
251
  readonly Vercel_Inkling: "gateway-thinkingmachines/inkling";
248
252
  readonly Vercel_Inkling_Small: "gateway-thinkingmachines/inkling-small";
@@ -2,7 +2,17 @@
2
2
  // reports both to the Developer Hub; Blast has no network and writes to the
3
3
  // runtime's log channel instead, which the host already collects per session.
4
4
  // The sink is installed by the runtime entry, never by extension code.
5
- import { format } from "node:util";
5
+ function formatValue(value) {
6
+ if (value instanceof Error)
7
+ return `${value.name}: ${value.message}`;
8
+ if (typeof value === "string")
9
+ return value;
10
+ if (value && typeof value === "object") {
11
+ const entries = Object.entries(value).map(([key, entry]) => `${key}: ${formatValue(entry)}`);
12
+ return `{ ${entries.join(", ")} }`;
13
+ }
14
+ return String(value);
15
+ }
6
16
  let sink;
7
17
  /** Installs (or, with `undefined`, removes) the runtime log sink. */
8
18
  export function setRuntimeLogSink(next) {
@@ -13,7 +23,7 @@ export function setRuntimeLogSink(next) {
13
23
  * a no-op: reporting must never be the reason a command fails.
14
24
  */
15
25
  export function captureException(exception) {
16
- sink?.("error", `captureException: ${format(exception)}`);
26
+ sink?.("error", `captureException: ${formatValue(exception)}`);
17
27
  }
18
28
  /**
19
29
  * Records heap statistics with a label. Raycast only records when memory
@@ -21,6 +31,6 @@ export function captureException(exception) {
21
31
  * to keep, and nothing is returned to the extension.
22
32
  */
23
33
  export function captureMemorySnapshot(label) {
24
- const { heapUsed, heapTotal } = process.memoryUsage();
25
- sink?.("debug", `captureMemorySnapshot: ${label} heapUsed=${heapUsed} heapTotal=${heapTotal}`);
34
+ const memory = globalThis.process?.memoryUsage?.();
35
+ sink?.("debug", `captureMemorySnapshot: ${label} heapUsed=${memory?.heapUsed ?? 0} heapTotal=${memory?.heapTotal ?? 0}`);
26
36
  }
@@ -13,6 +13,7 @@ export declare const AI: {
13
13
  readonly "OpenAI_GPT-6_Astra": "openai-gpt-6-astra";
14
14
  readonly "OpenAI_GPT-6_Sol": "openai-gpt-6-sol";
15
15
  readonly "OpenAI_GPT-6.1_Sol": "openai-gpt-6.1-sol";
16
+ readonly "OpenAI_GPT-6.1_Terra": "openai-gpt-6.1-terra";
16
17
  readonly "OpenAI_GPT-6_Luna": "openai-gpt-6-luna";
17
18
  readonly "Groq_GPT-OSS_20b": "groq-openai/gpt-oss-20b";
18
19
  readonly "Groq_GPT-OSS_120b": "groq-openai/gpt-oss-120b";
@@ -42,6 +43,7 @@ export declare const AI: {
42
43
  readonly "Vercel_GLM-5.3_Flash": "gateway-zai/glm-5.3-flash";
43
44
  readonly "Vercel_Kimi_K2.7_Code": "gateway-moonshotai/kimi-k2.7-code";
44
45
  readonly Vercel_Kimi_K3: "gateway-moonshotai/kimi-k3";
46
+ readonly "Vercel_Kimi_K3.1_Pro": "gateway-moonshotai/kimi-k3.1-pro";
45
47
  readonly Vercel_Gemma_4_31B: "gateway-google/gemma-4-31b-it";
46
48
  readonly Vercel_Inkling: "gateway-thinkingmachines/inkling";
47
49
  readonly Vercel_Inkling_Small: "gateway-thinkingmachines/inkling-small";
@@ -11,6 +11,7 @@ export const AI = {
11
11
  "OpenAI_GPT-6_Astra": "openai-gpt-6-astra",
12
12
  "OpenAI_GPT-6_Sol": "openai-gpt-6-sol",
13
13
  "OpenAI_GPT-6.1_Sol": "openai-gpt-6.1-sol",
14
+ "OpenAI_GPT-6.1_Terra": "openai-gpt-6.1-terra",
14
15
  "OpenAI_GPT-6_Luna": "openai-gpt-6-luna",
15
16
  "Groq_GPT-OSS_20b": "groq-openai/gpt-oss-20b",
16
17
  "Groq_GPT-OSS_120b": "groq-openai/gpt-oss-120b",
@@ -40,6 +41,7 @@ export const AI = {
40
41
  "Vercel_GLM-5.3_Flash": "gateway-zai/glm-5.3-flash",
41
42
  "Vercel_Kimi_K2.7_Code": "gateway-moonshotai/kimi-k2.7-code",
42
43
  Vercel_Kimi_K3: "gateway-moonshotai/kimi-k3",
44
+ "Vercel_Kimi_K3.1_Pro": "gateway-moonshotai/kimi-k3.1-pro",
43
45
  Vercel_Gemma_4_31B: "gateway-google/gemma-4-31b-it",
44
46
  Vercel_Inkling: "gateway-thinkingmachines/inkling",
45
47
  Vercel_Inkling_Small: "gateway-thinkingmachines/inkling-small",
@@ -40,6 +40,7 @@ export declare const AI: {
40
40
  readonly "OpenAI_GPT-6_Astra": "openai-gpt-6-astra";
41
41
  readonly "OpenAI_GPT-6_Sol": "openai-gpt-6-sol";
42
42
  readonly "OpenAI_GPT-6.1_Sol": "openai-gpt-6.1-sol";
43
+ readonly "OpenAI_GPT-6.1_Terra": "openai-gpt-6.1-terra";
43
44
  readonly "OpenAI_GPT-6_Luna": "openai-gpt-6-luna";
44
45
  readonly "Groq_GPT-OSS_20b": "groq-openai/gpt-oss-20b";
45
46
  readonly "Groq_GPT-OSS_120b": "groq-openai/gpt-oss-120b";
@@ -69,6 +70,7 @@ export declare const AI: {
69
70
  readonly "Vercel_GLM-5.3_Flash": "gateway-zai/glm-5.3-flash";
70
71
  readonly "Vercel_Kimi_K2.7_Code": "gateway-moonshotai/kimi-k2.7-code";
71
72
  readonly Vercel_Kimi_K3: "gateway-moonshotai/kimi-k3";
73
+ readonly "Vercel_Kimi_K3.1_Pro": "gateway-moonshotai/kimi-k3.1-pro";
72
74
  readonly Vercel_Gemma_4_31B: "gateway-google/gemma-4-31b-it";
73
75
  readonly Vercel_Inkling: "gateway-thinkingmachines/inkling";
74
76
  readonly Vercel_Inkling_Small: "gateway-thinkingmachines/inkling-small";
@@ -409,6 +411,7 @@ export declare const unstable_AI: {
409
411
  readonly "OpenAI_GPT-6_Astra": "openai-gpt-6-astra";
410
412
  readonly "OpenAI_GPT-6_Sol": "openai-gpt-6-sol";
411
413
  readonly "OpenAI_GPT-6.1_Sol": "openai-gpt-6.1-sol";
414
+ readonly "OpenAI_GPT-6.1_Terra": "openai-gpt-6.1-terra";
412
415
  readonly "OpenAI_GPT-6_Luna": "openai-gpt-6-luna";
413
416
  readonly "Groq_GPT-OSS_20b": "groq-openai/gpt-oss-20b";
414
417
  readonly "Groq_GPT-OSS_120b": "groq-openai/gpt-oss-120b";
@@ -438,6 +441,7 @@ export declare const unstable_AI: {
438
441
  readonly "Vercel_GLM-5.3_Flash": "gateway-zai/glm-5.3-flash";
439
442
  readonly "Vercel_Kimi_K2.7_Code": "gateway-moonshotai/kimi-k2.7-code";
440
443
  readonly Vercel_Kimi_K3: "gateway-moonshotai/kimi-k3";
444
+ readonly "Vercel_Kimi_K3.1_Pro": "gateway-moonshotai/kimi-k3.1-pro";
441
445
  readonly Vercel_Gemma_4_31B: "gateway-google/gemma-4-31b-it";
442
446
  readonly Vercel_Inkling: "gateway-thinkingmachines/inkling";
443
447
  readonly Vercel_Inkling_Small: "gateway-thinkingmachines/inkling-small";
@@ -50,7 +50,7 @@ export function NavigationProvider({ children }) {
50
50
  const popToRootStack = useMemo(() => (options) => {
51
51
  setPopToRootOptions(options);
52
52
  // The root view is the provider's own children, not an entry (decision
53
- // 0003), so nothing above it is kept; onPops run topmost first, like
53
+ // 0001), so nothing above it is kept; onPops run topmost first, like
54
54
  // successive `pop()` calls.
55
55
  if (entries.current.length === 0)
56
56
  return;
@@ -11,6 +11,7 @@ export declare const AI: {
11
11
  readonly "OpenAI_GPT-6_Astra": "openai-gpt-6-astra";
12
12
  readonly "OpenAI_GPT-6_Sol": "openai-gpt-6-sol";
13
13
  readonly "OpenAI_GPT-6.1_Sol": "openai-gpt-6.1-sol";
14
+ readonly "OpenAI_GPT-6.1_Terra": "openai-gpt-6.1-terra";
14
15
  readonly "OpenAI_GPT-6_Luna": "openai-gpt-6-luna";
15
16
  readonly "Groq_GPT-OSS_20b": "groq-openai/gpt-oss-20b";
16
17
  readonly "Groq_GPT-OSS_120b": "groq-openai/gpt-oss-120b";
@@ -40,6 +41,7 @@ export declare const AI: {
40
41
  readonly "Vercel_GLM-5.3_Flash": "gateway-zai/glm-5.3-flash";
41
42
  readonly "Vercel_Kimi_K2.7_Code": "gateway-moonshotai/kimi-k2.7-code";
42
43
  readonly Vercel_Kimi_K3: "gateway-moonshotai/kimi-k3";
44
+ readonly "Vercel_Kimi_K3.1_Pro": "gateway-moonshotai/kimi-k3.1-pro";
43
45
  readonly Vercel_Gemma_4_31B: "gateway-google/gemma-4-31b-it";
44
46
  readonly Vercel_Inkling: "gateway-thinkingmachines/inkling";
45
47
  readonly Vercel_Inkling_Small: "gateway-thinkingmachines/inkling-small";
@@ -1,6 +1,6 @@
1
1
  // `closeMainWindow` from the declarations. Closing the window is a client
2
2
  // action, so the runtime records the request on the root `Navigation` scene
3
- // node (decision 0001, like HUD in decision 0004) and resolves. A no-view
3
+ // node (decision 0001, like HUD in decision 0001) and resolves. A no-view
4
4
  // command mounts its scene on the first request, exactly like feedback.
5
5
  import { PopToRootType } from "./enums.generated.js";
6
6
  import { mountScene } from "./feedback.js";
@@ -0,0 +1,19 @@
1
+ import type { InitCommand } from "@blastlauncher/protocol";
2
+ import { type CommandModule } from "./core.js";
3
+ export type BrowserMessagePort = {
4
+ postMessage(message: unknown): void;
5
+ addEventListener(type: "message", listener: (event: any) => void): void;
6
+ removeEventListener?(type: "message", listener: (event: any) => void): void;
7
+ start?(): void;
8
+ };
9
+ export type BrowserRuntimeOptions = {
10
+ port: BrowserMessagePort;
11
+ /** A pre-evaluated command module, or a loader for the init command. */
12
+ command?: CommandModule;
13
+ loadCommand?: (command: InitCommand) => Promise<CommandModule> | CommandModule;
14
+ };
15
+ export type BrowserExitMessage = {
16
+ type: "exit";
17
+ code: number;
18
+ };
19
+ export declare function startBrowserRuntime(options: BrowserRuntimeOptions): () => void;
@@ -0,0 +1,32 @@
1
+ import { startRuntime } from "./core.js";
2
+ /* oxlint-disable unicorn/consistent-function-scoping */
3
+ export function startBrowserRuntime(options) {
4
+ if (!options.command && !options.loadCommand)
5
+ throw new Error("Browser runtime requires command or loadCommand");
6
+ const loadCommand = options.loadCommand ?? (() => options.command);
7
+ let removeListener;
8
+ let receive = () => { };
9
+ const listener = (event) => receive(event.data);
10
+ const runtime = startRuntime({
11
+ send(message) {
12
+ // MessagePort has no target-origin argument; this is already scoped to the port.
13
+ // oxlint-disable-next-line unicorn/require-post-message-target-origin
14
+ options.port.postMessage(message);
15
+ },
16
+ onMessage(onMessage) {
17
+ receive = onMessage;
18
+ options.port.addEventListener("message", listener);
19
+ removeListener = () => options.port.removeEventListener?.("message", listener);
20
+ options.port.start?.();
21
+ return () => removeListener?.();
22
+ },
23
+ exit(code) {
24
+ // MessagePort has no target-origin argument; this is already scoped to the port.
25
+ // oxlint-disable-next-line unicorn/require-post-message-target-origin
26
+ options.port.postMessage({ type: "exit", code });
27
+ removeListener?.();
28
+ },
29
+ loadCommand,
30
+ });
31
+ return () => runtime();
32
+ }
package/dist/core.d.ts ADDED
@@ -0,0 +1,18 @@
1
+ import { type InitCommand, type LogLevel, type RuntimeMessage } from "@blastlauncher/protocol";
2
+ import { type LaunchProps } from "./api/launch.js";
3
+ export type CommandModule = {
4
+ default?: (props: LaunchProps) => unknown;
5
+ getModels?: unknown;
6
+ streamCompletion?: unknown;
7
+ };
8
+ export type RuntimeOptions = {
9
+ send(message: RuntimeMessage): void;
10
+ onMessage(listener: (value: unknown) => void): () => void;
11
+ exit(code: number): void;
12
+ loadCommand(command: InitCommand): Promise<CommandModule> | CommandModule;
13
+ log?(level: LogLevel, args: unknown[]): void;
14
+ };
15
+ export type RuntimeController = (() => void) & {
16
+ fatal(error: unknown, code?: string): void;
17
+ };
18
+ export declare function startRuntime(options: RuntimeOptions): RuntimeController;
package/dist/core.js ADDED
@@ -0,0 +1,260 @@
1
+ import { parseHostMessage, PROTOCOL_VERSION, } from "@blastlauncher/protocol";
2
+ import { createElement, Suspense } from "react";
3
+ import { createModelProvider } from "./provider.js";
4
+ import { setCapabilityRequester } from "./api/capability.js";
5
+ import { setRuntimeLogSink } from "./api/diagnostics.js";
6
+ import { setEnvironment } from "./api/environment.js";
7
+ import { setFeedbackMount } from "./api/feedback.js";
8
+ import { decodeFormValues, flushFormPersistence, setFormDraftValues } from "./api/form.js";
9
+ import { LaunchType } from "./api/launch.js";
10
+ import { NavigationProvider } from "./api/navigation.js";
11
+ import { setPreferenceValues } from "./api/preferences.js";
12
+ import { setRenderMount } from "./api/legacy.js";
13
+ import { UnsupportedApiError } from "./api/unsupported.js";
14
+ import { createSceneRoot } from "./reconciler/index.js";
15
+ /* oxlint-disable unicorn/consistent-function-scoping */
16
+ export function startRuntime(options) {
17
+ let provider;
18
+ let providerLoad;
19
+ let stopping = false;
20
+ let initialized = false;
21
+ let loaded = false;
22
+ let nextRequest = 1;
23
+ const pending = new Map();
24
+ const send = (message) => {
25
+ if (!stopping)
26
+ options.send(message);
27
+ };
28
+ const errorMessage = (error) => error instanceof Error ? error.message : String(error);
29
+ const log = (level, args) => {
30
+ if (options.log)
31
+ options.log(level, args);
32
+ else
33
+ send({ type: "log", level, message: args.map(String).join(" ") });
34
+ };
35
+ const finish = (code) => {
36
+ if (stopping)
37
+ return;
38
+ stopping = true;
39
+ setCapabilityRequester(undefined);
40
+ pending.clear();
41
+ options.exit(code);
42
+ };
43
+ const fatal = (error, code = "RUNTIME_ERROR") => {
44
+ send({
45
+ type: "error",
46
+ code: error instanceof UnsupportedApiError ? error.code : code,
47
+ message: errorMessage(error),
48
+ fatal: true,
49
+ });
50
+ finish(1);
51
+ };
52
+ const root = createSceneRoot({
53
+ publish: (scene) => send({ type: "scene", ...scene }),
54
+ onWarning: (message) => log("warn", [message]),
55
+ onError: fatal,
56
+ });
57
+ setCapabilityRequester((name, params, requestOptions = {}) => {
58
+ if (requestOptions.signal?.aborted)
59
+ return Promise.reject(new DOMException("The operation was aborted", "AbortError"));
60
+ const id = String(nextRequest++);
61
+ return new Promise((resolve, reject) => {
62
+ const abort = () => {
63
+ reject(new DOMException("The operation was aborted", "AbortError"));
64
+ send({ type: "capability.cancel", id });
65
+ };
66
+ pending.set(id, {
67
+ resolve,
68
+ reject,
69
+ progress: (value) => {
70
+ if (!requestOptions.signal?.aborted)
71
+ requestOptions.onProgress?.(value);
72
+ },
73
+ cleanup: () => requestOptions.signal?.removeEventListener("abort", abort),
74
+ });
75
+ requestOptions.signal?.addEventListener("abort", abort, { once: true });
76
+ send({ type: "capability.request", id, name, params });
77
+ });
78
+ });
79
+ setRuntimeLogSink((level, message) => log(level, [message]));
80
+ function loadingFallback(mode) {
81
+ return mode === "menu-bar"
82
+ ? createElement("MenuBarExtra", { isLoading: true })
83
+ : createElement("List", { isLoading: true });
84
+ }
85
+ function withSuspense(element, mode) {
86
+ return createElement(Suspense, { fallback: loadingFallback(mode) }, element);
87
+ }
88
+ function launchProps(command) {
89
+ return {
90
+ arguments: command.arguments ?? {},
91
+ launchType: LaunchType.UserInitiated,
92
+ ...(command.draftValues === undefined
93
+ ? {}
94
+ : { draftValues: decodeFormValues(command.draftValues) }),
95
+ ...(command.fallbackText === undefined ? {} : { fallbackText: command.fallbackText }),
96
+ ...(command.launchContext === undefined ? {} : { launchContext: command.launchContext }),
97
+ };
98
+ }
99
+ async function load(message) {
100
+ setEnvironment(message.environment, message.command);
101
+ setPreferenceValues(message.preferences);
102
+ const props = launchProps(message.command);
103
+ setFormDraftValues(props.draftValues);
104
+ let rendered = false;
105
+ setRenderMount((element) => {
106
+ if (rendered)
107
+ return;
108
+ rendered = true;
109
+ root.render(createElement(NavigationProvider, {
110
+ children: withSuspense(element, message.command.mode),
111
+ }));
112
+ loaded = true;
113
+ });
114
+ const command = await options.loadCommand(message.command);
115
+ setRenderMount(undefined);
116
+ if (stopping)
117
+ return;
118
+ if (rendered)
119
+ return;
120
+ if (message.command.mode === "model-provider") {
121
+ provider = createModelProvider(command, send);
122
+ return;
123
+ }
124
+ if (message.command.mode === "no-view") {
125
+ setFeedbackMount(() => {
126
+ if (!loaded) {
127
+ loaded = true;
128
+ root.render(createElement(NavigationProvider, { children: null }));
129
+ }
130
+ });
131
+ await command.default(props);
132
+ if (stopping)
133
+ return;
134
+ finish(0);
135
+ return;
136
+ }
137
+ let running = false;
138
+ function Command() {
139
+ if (running)
140
+ return null;
141
+ const result = command.default?.(props);
142
+ if (result !== null &&
143
+ (typeof result === "object" || typeof result === "function") &&
144
+ "then" in result &&
145
+ typeof result.then === "function") {
146
+ running = true;
147
+ void Promise.resolve(result).then(() => finish(0), fatal);
148
+ return null;
149
+ }
150
+ return result;
151
+ }
152
+ const wrap = message.command.mode === "view" &&
153
+ typeof command.default === "function" &&
154
+ !command.default.prototype?.isReactComponent;
155
+ const element = createElement(wrap ? Command : command.default, props);
156
+ const suspended = withSuspense(element, message.command.mode);
157
+ // Every loaded command gets the provider, menu-bar included: it is the
158
+ // only scene home for feedback (HUD, toast, alert) and window requests.
159
+ root.render(createElement(NavigationProvider, { children: suspended }));
160
+ loaded = true;
161
+ }
162
+ function shutdown() {
163
+ if (stopping)
164
+ return;
165
+ try {
166
+ root.unmount();
167
+ void flushFormPersistence().then(() => finish(0), fatal);
168
+ }
169
+ catch (error) {
170
+ fatal(error);
171
+ }
172
+ }
173
+ function handle(value) {
174
+ if (stopping)
175
+ return;
176
+ const parsed = parseHostMessage(value);
177
+ if (!parsed.ok)
178
+ return fatal(parsed.error, "PROTOCOL_ERROR");
179
+ const message = parsed.message;
180
+ if (!initialized) {
181
+ if (message.type !== "init" || message.protocol !== PROTOCOL_VERSION)
182
+ return fatal(`Expected init with protocol ${PROTOCOL_VERSION}`, "PROTOCOL_ERROR");
183
+ initialized = true;
184
+ send({ type: "ready", protocol: PROTOCOL_VERSION });
185
+ providerLoad = load(message);
186
+ void providerLoad.catch(fatal);
187
+ return;
188
+ }
189
+ switch (message.type) {
190
+ case "provider.getModels":
191
+ case "provider.stream":
192
+ case "provider.abort":
193
+ void providerLoad
194
+ ?.then(() => {
195
+ if (!provider)
196
+ return fatal("Provider request in command session", "PROTOCOL_ERROR");
197
+ provider.handle(message);
198
+ })
199
+ .catch(fatal);
200
+ return;
201
+ case "init":
202
+ return fatal("Duplicate init", "PROTOCOL_ERROR");
203
+ case "shutdown":
204
+ return shutdown();
205
+ case "capability.progress": {
206
+ const request = pending.get(message.id);
207
+ if (!request)
208
+ return fatal(`Unknown capability id: ${message.id}`, "PROTOCOL_ERROR");
209
+ request.progress?.(message.value);
210
+ return;
211
+ }
212
+ case "capability.result": {
213
+ const request = pending.get(message.id);
214
+ if (!request)
215
+ return fatal(`Unknown capability id: ${message.id}`, "PROTOCOL_ERROR");
216
+ pending.delete(message.id);
217
+ request.cleanup();
218
+ if (message.ok)
219
+ request.resolve(message.value);
220
+ else
221
+ request.reject(Object.assign(new Error(message.error.message), { code: message.error.code }));
222
+ return;
223
+ }
224
+ case "event": {
225
+ if (!loaded)
226
+ return fatal("Event before command loaded", "PROTOCOL_ERROR");
227
+ const failed = (error) => {
228
+ if (error instanceof UnsupportedApiError)
229
+ fatal(error);
230
+ else
231
+ send({
232
+ type: "error",
233
+ code: "EVENT_ERROR",
234
+ message: errorMessage(error),
235
+ fatal: false,
236
+ });
237
+ };
238
+ try {
239
+ void Promise.resolve(root.dispatch(message.node, message.handler, message.args)).catch(failed);
240
+ }
241
+ catch (error) {
242
+ failed(error);
243
+ }
244
+ try {
245
+ root.flush();
246
+ }
247
+ catch (error) {
248
+ fatal(error);
249
+ }
250
+ }
251
+ }
252
+ }
253
+ const remove = options.onMessage(handle);
254
+ const controller = (() => {
255
+ remove();
256
+ shutdown();
257
+ });
258
+ controller.fatal = fatal;
259
+ return controller;
260
+ }
package/dist/main.js CHANGED
@@ -6,77 +6,18 @@ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExte
6
6
  }
7
7
  return path;
8
8
  };
9
- import { createLineDecoder, encodeMessage, parseHostMessage, PROTOCOL_VERSION, } from "@blastlauncher/protocol";
10
- import { createModelProvider } from "./provider.js";
9
+ import { createLineDecoder, encodeMessage } from "@blastlauncher/protocol";
11
10
  import { Console } from "node:console";
12
11
  import { registerHooks } from "node:module";
13
12
  import { Writable } from "node:stream";
14
13
  import { pathToFileURL } from "node:url";
15
14
  import { format } from "node:util";
16
- import { createElement, Suspense } from "react";
17
- import { setCapabilityRequester } from "./api/capability.js";
18
- import { setRuntimeLogSink } from "./api/diagnostics.js";
19
- import { setEnvironment } from "./api/environment.js";
20
- import { setFeedbackMount } from "./api/feedback.js";
21
- import { decodeFormValues, flushFormPersistence, setFormDraftValues } from "./api/form.js";
22
- import { LaunchType } from "./api/launch.js";
23
- import { NavigationProvider } from "./api/navigation.js";
24
- import { setPreferenceValues } from "./api/preferences.js";
25
- import { setRenderMount } from "./api/legacy.js";
26
- import { UnsupportedApiError } from "./api/unsupported.js";
27
- import { createSceneRoot } from "./reconciler/index.js";
28
- let provider;
29
- let providerLoad;
15
+ import { startRuntime } from "./core.js";
30
16
  let stopping = false;
31
- let initialized = false;
32
- let loaded = false;
33
- let nextRequest = 1;
34
- const pending = new Map();
35
- // Protocol output owns the real stdout stream. Extension code that writes to
36
- // `process.stdout` (or a library doing it for them) must not corrupt the NDJSON
37
- // framing, so those writes become log messages instead. The protocol keeps its
38
- // own reference to the original writer.
39
- process.stdout.on("error", (error) => {
40
- if (error.code === "EPIPE")
41
- process.exit(0);
42
- throw error;
43
- });
44
- const writeToStdout = process.stdout.write.bind(process.stdout);
45
- process.stdout.write = ((chunk, encoding, callback) => {
46
- const done = typeof encoding === "function" ? encoding : callback;
47
- log("info", [String(chunk).replace(/\n$/, "")]);
48
- if (typeof done === "function")
49
- queueMicrotask(done);
50
- return true;
51
- });
52
- function send(message) {
53
- if (!stopping)
54
- writeToStdout(encodeMessage(message));
55
- }
56
- function errorMessage(error) {
57
- return error instanceof Error ? error.message : String(error);
58
- }
59
- function finish(code) {
60
- if (stopping)
61
- return;
62
- stopping = true;
63
- process.stdin.pause();
64
- setCapabilityRequester(undefined);
65
- pending.clear();
66
- // Explicit exit also stops extension timers; wait for protocol output to drain.
67
- process.stdout.end(() => process.exit(code));
68
- }
69
- function fatal(error, code = "RUNTIME_ERROR") {
70
- send({
71
- type: "error",
72
- code: error instanceof UnsupportedApiError ? error.code : code,
73
- message: errorMessage(error),
74
- fatal: true,
75
- });
76
- finish(1);
77
- }
17
+ const protocolWrite = process.stdout.write.bind(process.stdout);
78
18
  function log(level, args) {
79
- send({ type: "log", level, message: format(...args) });
19
+ if (!stopping)
20
+ protocolWrite(encodeMessage({ type: "log", level, message: format(...args) }));
80
21
  }
81
22
  function logStream(level) {
82
23
  return new Writable({
@@ -86,7 +27,14 @@ function logStream(level) {
86
27
  },
87
28
  });
88
29
  }
89
- // Console's remaining methods (trace, table, time, assert, …) also stay off stdout.
30
+ const writeToStdout = protocolWrite;
31
+ process.stdout.write = ((chunk, encoding, callback) => {
32
+ const done = typeof encoding === "function" ? encoding : callback;
33
+ log("info", [String(chunk).replace(/\n$/, "")]);
34
+ if (typeof done === "function")
35
+ queueMicrotask(done);
36
+ return true;
37
+ });
90
38
  Object.assign(console, new Console({ stdout: logStream("info"), stderr: logStream("error") }));
91
39
  console.timeStamp = () => { };
92
40
  for (const [method, level] of [
@@ -98,10 +46,6 @@ for (const [method, level] of [
98
46
  ]) {
99
47
  console[method] = (...args) => log(level, args);
100
48
  }
101
- process.on("uncaughtException", (error) => fatal(error));
102
- process.on("unhandledRejection", (error) => fatal(error));
103
- process.stdout.on("error", () => process.exit(1));
104
- process.stdin.on("error", fatal);
105
49
  const modules = new Map([
106
50
  ["@raycast/api", import.meta.resolve("@blastlauncher/runtime/api")],
107
51
  ...["react", "react/jsx-runtime", "react/jsx-dev-runtime"].map((name) => [
@@ -115,225 +59,40 @@ registerHooks({
115
59
  return url ? { url, shortCircuit: true } : nextResolve(specifier, context);
116
60
  },
117
61
  });
118
- const root = createSceneRoot({
119
- publish: (scene) => send({ type: "scene", ...scene }),
120
- onWarning: (message) => log("warn", [message]),
121
- onError: fatal,
122
- });
123
- setCapabilityRequester((name, params, options = {}) => {
124
- if (options.signal?.aborted)
125
- return Promise.reject(new DOMException("The operation was aborted", "AbortError"));
126
- const id = String(nextRequest++);
127
- return new Promise((resolve, reject) => {
128
- const abort = () => {
129
- reject(new DOMException("The operation was aborted", "AbortError"));
130
- send({ type: "capability.cancel", id });
131
- };
132
- pending.set(id, {
133
- resolve,
134
- reject,
135
- progress: (value) => {
136
- if (!options.signal?.aborted)
137
- options.onProgress?.(value);
138
- },
139
- cleanup: () => options.signal?.removeEventListener("abort", abort),
62
+ // `runtime` is only read from stdin callbacks, after this assignment.
63
+ const runtime = startRuntime({
64
+ send(message) {
65
+ writeToStdout(encodeMessage(message));
66
+ },
67
+ onMessage(listener) {
68
+ // The protocol decoder enforces the 8 MiB line cap (0001), strict UTF-8 and
69
+ // the malformed/unterminated-line rules; do not hand-roll framing here.
70
+ const decoder = createLineDecoder({
71
+ onLine: listener,
72
+ onError: (error) => runtime.fatal(error, "PROTOCOL_ERROR"),
140
73
  });
141
- options.signal?.addEventListener("abort", abort, { once: true });
142
- send({ type: "capability.request", id, name, params });
143
- });
144
- });
145
- // `captureException` and `captureMemorySnapshot` report diagnostics through the
146
- // protocol's log channel; the runtime owns no other reporter.
147
- setRuntimeLogSink((level, message) => log(level, [message]));
148
- function loadingFallback(mode) {
149
- return mode === "menu-bar"
150
- ? createElement("MenuBarExtra", { isLoading: true })
151
- : createElement("List", { isLoading: true });
152
- }
153
- function withSuspense(element, mode) {
154
- return createElement(Suspense, { fallback: loadingFallback(mode) }, element);
155
- }
156
- /**
157
- * Launch props for this command: the manifest arguments the host resolved and,
158
- * when another command started it, the `launchCommand` context. Blast only
159
- * launches user initiated commands (background launch is out of scope).
160
- */
161
- function launchProps(command) {
162
- return {
163
- arguments: command.arguments ?? {},
164
- launchType: LaunchType.UserInitiated,
165
- ...(command.draftValues === undefined
166
- ? {}
167
- : { draftValues: decodeFormValues(command.draftValues) }),
168
- ...(command.fallbackText === undefined ? {} : { fallbackText: command.fallbackText }),
169
- ...(command.launchContext === undefined ? {} : { launchContext: command.launchContext }),
170
- };
171
- }
172
- async function load(message) {
173
- setEnvironment(message.environment, message.command);
174
- setPreferenceValues(message.preferences);
175
- const props = launchProps(message.command);
176
- setFormDraftValues(props.draftValues);
177
- // Legacy commands call `render(element)` while the module is being imported;
178
- // that element becomes the command root instead of a default export.
179
- let rendered = false;
180
- setRenderMount((element) => {
181
- if (rendered)
182
- return;
183
- rendered = true;
184
- root.render(message.command.mode === "menu-bar"
185
- ? withSuspense(element, message.command.mode)
186
- : createElement(NavigationProvider, {
187
- children: withSuspense(element, message.command.mode),
188
- }));
189
- loaded = true;
190
- });
191
- const command = await import(__rewriteRelativeImportExtension(pathToFileURL(message.command.entry).href));
192
- setRenderMount(undefined);
193
- if (stopping)
194
- return;
195
- if (rendered)
196
- return;
197
- if (message.command.mode === "model-provider") {
198
- provider = createModelProvider(command, send);
199
- return;
200
- }
201
- if (message.command.mode === "no-view") {
202
- setFeedbackMount(() => {
203
- if (!loaded) {
204
- loaded = true;
205
- root.render(createElement(NavigationProvider, { children: null }));
206
- }
74
+ const onData = (chunk) => decoder.push(chunk);
75
+ process.stdin.on("data", onData);
76
+ process.stdin.on("end", () => {
77
+ decoder.end();
78
+ runtime();
207
79
  });
208
- // A no-view command runs until it settles, with a scene only for feedback.
209
- // A rejection is a fatal error through the caller's `.catch`.
210
- await command.default(props);
211
- if (stopping)
212
- return;
213
- finish(0);
214
- return;
215
- }
216
- // Call inside React so ordinary view commands retain hooks and navigation.
217
- // A thenable is a command body, never a suspending React component (0004).
218
- let running = false;
219
- function Command() {
220
- if (running)
221
- return null;
222
- const result = command.default(props);
223
- if (result !== null &&
224
- (typeof result === "object" || typeof result === "function") &&
225
- "then" in result &&
226
- typeof result.then === "function") {
227
- running = true;
228
- void Promise.resolve(result).then(() => finish(0), fatal);
229
- return null;
230
- }
231
- return result;
232
- }
233
- const wrap = message.command.mode === "view" &&
234
- typeof command.default === "function" &&
235
- !command.default.prototype?.isReactComponent;
236
- const element = createElement(wrap ? Command : command.default, props);
237
- const suspended = withSuspense(element, message.command.mode);
238
- root.render(message.command.mode === "menu-bar"
239
- ? suspended
240
- : createElement(NavigationProvider, { children: suspended }));
241
- loaded = true;
242
- }
243
- function shutdown() {
244
- if (stopping)
245
- return;
246
- try {
247
- root.unmount();
248
- void flushFormPersistence().then(() => finish(0), fatal);
249
- }
250
- catch (error) {
251
- fatal(error);
252
- }
253
- }
254
- function handle(value) {
255
- if (stopping)
256
- return;
257
- const parsed = parseHostMessage(value);
258
- if (!parsed.ok)
259
- return fatal(parsed.error, "PROTOCOL_ERROR");
260
- const message = parsed.message;
261
- if (!initialized) {
262
- if (message.type !== "init" || message.protocol !== PROTOCOL_VERSION) {
263
- return fatal(`Expected init with protocol ${PROTOCOL_VERSION}`, "PROTOCOL_ERROR");
264
- }
265
- initialized = true;
266
- send({ type: "ready", protocol: PROTOCOL_VERSION });
267
- providerLoad = load(message);
268
- void providerLoad.catch(fatal);
269
- return;
270
- }
271
- switch (message.type) {
272
- case "provider.getModels":
273
- case "provider.stream":
274
- case "provider.abort":
275
- void providerLoad
276
- ?.then(() => {
277
- if (!provider)
278
- return fatal("Provider request in command session", "PROTOCOL_ERROR");
279
- provider.handle(message);
280
- })
281
- .catch(fatal);
282
- return;
283
- case "init":
284
- return fatal("Duplicate init", "PROTOCOL_ERROR");
285
- case "shutdown":
286
- return shutdown();
287
- case "capability.progress": {
288
- const request = pending.get(message.id);
289
- if (!request)
290
- return fatal(`Unknown capability id: ${message.id}`, "PROTOCOL_ERROR");
291
- request.progress?.(message.value);
292
- return;
293
- }
294
- case "capability.result": {
295
- const request = pending.get(message.id);
296
- if (!request)
297
- return fatal(`Unknown capability id: ${message.id}`, "PROTOCOL_ERROR");
298
- pending.delete(message.id);
299
- request.cleanup();
300
- if (message.ok)
301
- request.resolve(message.value);
302
- else
303
- request.reject(Object.assign(new Error(message.error.message), { code: message.error.code }));
304
- return;
305
- }
306
- case "event": {
307
- if (!loaded)
308
- return fatal("Event before command loaded", "PROTOCOL_ERROR");
309
- const failed = (error) => {
310
- if (error instanceof UnsupportedApiError)
311
- fatal(error);
312
- else
313
- send({ type: "error", code: "EVENT_ERROR", message: errorMessage(error), fatal: false });
314
- };
315
- try {
316
- // Do not await: a handler can be waiting for a capability.result on stdin.
317
- void Promise.resolve(root.dispatch(message.node, message.handler, message.args)).catch(failed);
318
- }
319
- catch (error) {
320
- failed(error);
321
- }
322
- try {
323
- root.flush();
324
- }
325
- catch (error) {
326
- fatal(error);
327
- }
328
- }
329
- }
330
- }
331
- const decoder = createLineDecoder({
332
- onLine: handle,
333
- onError: (error) => fatal(error, "PROTOCOL_ERROR"),
80
+ return () => process.stdin.off("data", onData);
81
+ },
82
+ exit(code) {
83
+ stopping = true;
84
+ process.stdin.pause();
85
+ process.stdout.end(() => process.exit(code));
86
+ },
87
+ log,
88
+ loadCommand: (command) => import(__rewriteRelativeImportExtension(pathToFileURL(command.entry).href)),
334
89
  });
335
- process.stdin.on("data", (chunk) => decoder.push(chunk));
336
- process.stdin.on("end", () => {
337
- decoder.end();
338
- shutdown();
90
+ process.on("uncaughtException", (error) => runtime.fatal(error));
91
+ process.on("unhandledRejection", (error) => runtime.fatal(error));
92
+ process.stdin.on("error", (error) => runtime.fatal(error));
93
+ // EPIPE means the host closed our stdout: a normal shutdown, not a crash.
94
+ process.stdout.on("error", (error) => {
95
+ if (error.code === "EPIPE")
96
+ process.exit(0);
97
+ process.exit(1);
339
98
  });
package/package.json CHANGED
@@ -16,14 +16,19 @@
16
16
  "types": "./dist/main.d.ts",
17
17
  "import": "./dist/main.js",
18
18
  "default": "./dist/main.js"
19
+ },
20
+ "./browser": {
21
+ "types": "./dist/browser.d.ts",
22
+ "import": "./dist/browser.js",
23
+ "default": "./dist/browser.js"
19
24
  }
20
25
  },
21
26
  "dependencies": {
22
- "@blastlauncher/protocol": "3.0.0-alpha.1",
27
+ "@blastlauncher/protocol": "3.0.0-alpha.3",
23
28
  "react": "19.3.1",
24
29
  "react-reconciler": "0.34.0"
25
30
  },
26
- "version": "3.0.0-alpha.1",
31
+ "version": "3.0.0-alpha.3",
27
32
  "license": "MIT",
28
33
  "repository": {
29
34
  "type": "git",