@nodetool-ai/app-runtime 0.8.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.
Files changed (75) hide show
  1. package/README.md +104 -0
  2. package/dist/.nodetool-build-stamp +1 -0
  3. package/dist/actions.d.ts +85 -0
  4. package/dist/actions.d.ts.map +1 -0
  5. package/dist/actions.js +65 -0
  6. package/dist/actions.js.map +1 -0
  7. package/dist/bindings.d.ts +109 -0
  8. package/dist/bindings.d.ts.map +1 -0
  9. package/dist/bindings.js +257 -0
  10. package/dist/bindings.js.map +1 -0
  11. package/dist/bundle.d.ts +167 -0
  12. package/dist/bundle.d.ts.map +1 -0
  13. package/dist/bundle.js +289 -0
  14. package/dist/bundle.js.map +1 -0
  15. package/dist/chat.d.ts +33 -0
  16. package/dist/chat.d.ts.map +1 -0
  17. package/dist/chat.js +77 -0
  18. package/dist/chat.js.map +1 -0
  19. package/dist/conditions.d.ts +66 -0
  20. package/dist/conditions.d.ts.map +1 -0
  21. package/dist/conditions.js +221 -0
  22. package/dist/conditions.js.map +1 -0
  23. package/dist/doc-ops.d.ts +205 -0
  24. package/dist/doc-ops.d.ts.map +1 -0
  25. package/dist/doc-ops.js +304 -0
  26. package/dist/doc-ops.js.map +1 -0
  27. package/dist/document.d.ts +186 -0
  28. package/dist/document.d.ts.map +1 -0
  29. package/dist/document.js +326 -0
  30. package/dist/document.js.map +1 -0
  31. package/dist/documents.d.ts +38 -0
  32. package/dist/documents.d.ts.map +1 -0
  33. package/dist/documents.js +108 -0
  34. package/dist/documents.js.map +1 -0
  35. package/dist/fold.d.ts +50 -0
  36. package/dist/fold.d.ts.map +1 -0
  37. package/dist/fold.js +175 -0
  38. package/dist/fold.js.map +1 -0
  39. package/dist/index.d.ts +26 -0
  40. package/dist/index.d.ts.map +1 -0
  41. package/dist/index.js +26 -0
  42. package/dist/index.js.map +1 -0
  43. package/dist/input-validation.d.ts +4 -0
  44. package/dist/input-validation.d.ts.map +1 -0
  45. package/dist/input-validation.js +26 -0
  46. package/dist/input-validation.js.map +1 -0
  47. package/dist/mutable.d.ts +8 -0
  48. package/dist/mutable.d.ts.map +1 -0
  49. package/dist/mutable.js +2 -0
  50. package/dist/mutable.js.map +1 -0
  51. package/dist/operations.d.ts +51 -0
  52. package/dist/operations.d.ts.map +1 -0
  53. package/dist/operations.js +95 -0
  54. package/dist/operations.js.map +1 -0
  55. package/dist/policy.d.ts +29 -0
  56. package/dist/policy.d.ts.map +1 -0
  57. package/dist/policy.js +16 -0
  58. package/dist/policy.js.map +1 -0
  59. package/dist/predicates.d.ts +19 -0
  60. package/dist/predicates.d.ts.map +1 -0
  61. package/dist/predicates.js +19 -0
  62. package/dist/predicates.js.map +1 -0
  63. package/dist/script-run.d.ts +86 -0
  64. package/dist/script-run.d.ts.map +1 -0
  65. package/dist/script-run.js +113 -0
  66. package/dist/script-run.js.map +1 -0
  67. package/dist/state.d.ts +176 -0
  68. package/dist/state.d.ts.map +1 -0
  69. package/dist/state.js +357 -0
  70. package/dist/state.js.map +1 -0
  71. package/dist/widgets.d.ts +89 -0
  72. package/dist/widgets.d.ts.map +1 -0
  73. package/dist/widgets.js +667 -0
  74. package/dist/widgets.js.map +1 -0
  75. package/package.json +55 -0
@@ -0,0 +1,167 @@
1
+ import { type ApplicationDocument } from "./document.js";
2
+ /** Bumped whenever the bundle parser needs a new branch. */
3
+ export declare const APPLICATION_BUNDLE_SCHEMA_VERSION: 1;
4
+ /**
5
+ * A workflow graph, structural so this package stays dependency-free. The
6
+ * pinned Zod contract lives in `@nodetool-ai/protocol`.
7
+ */
8
+ export interface BundleGraph {
9
+ nodes: unknown[];
10
+ edges: unknown[];
11
+ }
12
+ /** One workflow carried by a bundle, addressed by its bundle-local `key`. */
13
+ export interface BundledWorkflow {
14
+ /** Bundle-local identifier that operations reference in place of an id. */
15
+ key: string;
16
+ name: string;
17
+ description?: string;
18
+ graph: BundleGraph;
19
+ /**
20
+ * A stable identity for this workflow across installs. An importer that
21
+ * already has the workflow this names reuses it rather than creating a
22
+ * duplicate row — which is how two shipped example apps can bind the same
23
+ * template and end up sharing one workflow. Absent on a hand-exported
24
+ * bundle, which always creates fresh workflows.
25
+ */
26
+ sourceId?: string;
27
+ /**
28
+ * What the release pinned for this workflow: the workflow version written
29
+ * at publish time and the hash of the graph frozen with it. Both null for a
30
+ * draft export, which pins nothing.
31
+ */
32
+ version?: number | null;
33
+ graphHash?: string | null;
34
+ }
35
+ /**
36
+ * A JS script document, structural for the same reason {@link BundleGraph} is:
37
+ * the pinned Zod contract lives in `@nodetool-ai/protocol`, which this package
38
+ * does not depend on. A consumer that needs the real document parses it there.
39
+ */
40
+ export interface BundleJsScriptDocument {
41
+ schemaVersion: number;
42
+ code: string;
43
+ inputs: Array<{
44
+ name: string;
45
+ type: string;
46
+ }>;
47
+ outputs: Array<{
48
+ name: string;
49
+ type: string;
50
+ }>;
51
+ }
52
+ /** One JS script carried by a bundle, addressed by its bundle-local `key`. */
53
+ export interface BundledJsScript {
54
+ /** Bundle-local identifier that script operations reference in place of an id. */
55
+ key: string;
56
+ name: string;
57
+ /** The document of the version the operations pin. */
58
+ document: BundleJsScriptDocument;
59
+ /** See {@link BundledWorkflow.sourceId}. */
60
+ sourceId?: string;
61
+ /** The version number the exporting install pinned; null when unpinned. */
62
+ version?: number | null;
63
+ }
64
+ export interface ApplicationBundle {
65
+ schemaVersion: number;
66
+ name: string;
67
+ description: string;
68
+ /**
69
+ * The application document; its operations reference `workflows[].key` and
70
+ * `scripts[].key` in place of real ids.
71
+ */
72
+ app: ApplicationDocument;
73
+ workflows: BundledWorkflow[];
74
+ scripts: BundledJsScript[];
75
+ }
76
+ /** The application side of an export — an application row, structurally. */
77
+ export interface BundleApplicationSource {
78
+ name: string;
79
+ description?: string;
80
+ document: ApplicationDocument;
81
+ }
82
+ /**
83
+ * One workflow to carry, keyed by the real id the document's operations use.
84
+ * Mirrors an entry of `application_versions.workflow_graphs` plus the
85
+ * workflow's name, so a released app exports exactly what it runs.
86
+ */
87
+ /** One script to carry, keyed by the real id the document's operations use. */
88
+ export interface BundleJsScriptSource {
89
+ scriptId: string;
90
+ name: string;
91
+ document: BundleJsScriptDocument;
92
+ /** See {@link BundledWorkflow.sourceId}. */
93
+ sourceId?: string;
94
+ version?: number | null;
95
+ }
96
+ export interface BundleWorkflowSource {
97
+ workflowId: string;
98
+ name: string;
99
+ description?: string;
100
+ /** See {@link BundledWorkflow.sourceId}. */
101
+ sourceId?: string;
102
+ graph: BundleGraph;
103
+ version?: number | null;
104
+ graphHash?: string | null;
105
+ }
106
+ /**
107
+ * Serialize an application and its workflows into a bundle: operations stop
108
+ * pointing at workflow ids and point at bundle-local keys instead.
109
+ *
110
+ * A workflow an operation binds but `workflows` does not carry keeps its raw
111
+ * id — the link is already broken, and dropping the operation would hide it.
112
+ */
113
+ export declare const bundleFromApplication: (app: BundleApplicationSource, workflows: readonly BundleWorkflowSource[], scripts?: readonly BundleJsScriptSource[]) => ApplicationBundle;
114
+ /** A workflow an importer must create, with the id it was assigned. */
115
+ export interface ResolvedBundleWorkflow extends BundledWorkflow {
116
+ /** The id the caller persists this workflow under. */
117
+ id: string;
118
+ }
119
+ export interface ApplyBundleResult {
120
+ app: {
121
+ name: string;
122
+ description: string;
123
+ document: ApplicationDocument;
124
+ };
125
+ workflows: ResolvedBundleWorkflow[];
126
+ scripts: ResolvedBundleJsScript[];
127
+ }
128
+ /** A script an importer must create, with the id it was assigned. */
129
+ export interface ResolvedBundleJsScript extends BundledJsScript {
130
+ /** The id the caller persists this script under. */
131
+ id: string;
132
+ }
133
+ export interface ApplyBundleOptions {
134
+ /**
135
+ * Mints the id each carried workflow is created under. Defaults to
136
+ * `crypto.randomUUID()`; pass one in when the caller owns id generation
137
+ * (the models layer's time-ordered uuid) or a test wants determinism.
138
+ */
139
+ newWorkflowId?: (workflow: BundledWorkflow, index: number) => string;
140
+ /** Mints the id each carried script is created under. Same default. */
141
+ newScriptId?: (script: BundledJsScript, index: number) => string;
142
+ }
143
+ /**
144
+ * Turn a bundle back into an application plus the workflows to create: every
145
+ * carried workflow gets a fresh id, and the document's operations are
146
+ * rewritten from bundle keys to those ids.
147
+ *
148
+ * Pure — the caller writes the rows, in whatever order its storage needs.
149
+ */
150
+ export declare const applyBundle: (bundle: ApplicationBundle, options?: ApplyBundleOptions) => ApplyBundleResult;
151
+ /**
152
+ * Re-pin every script operation to the version the importer actually created.
153
+ * A bundle carries one document per script; the row it lands in has its own
154
+ * version numbering, so the number the export pinned means nothing here.
155
+ */
156
+ export declare const pinScriptVersions: (document: ApplicationDocument, versionByScriptId: ReadonlyMap<string, number>) => ApplicationDocument;
157
+ /**
158
+ * Parse an unknown value into an {@link ApplicationBundle}, or null when it is
159
+ * not one. A future schema version is rejected rather than guessed at, and a
160
+ * workflow entry without a key or a graph is dropped. The document goes
161
+ * through `parseApplicationDocument`, so a bundle carrying a legacy
162
+ * `{ version, data }` document parses too.
163
+ */
164
+ export declare const parseApplicationBundle: (value: unknown) => ApplicationBundle | null;
165
+ /** The bundle as a file's contents. */
166
+ export declare const serializeApplicationBundle: (bundle: ApplicationBundle) => string;
167
+ //# sourceMappingURL=bundle.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bundle.d.ts","sourceRoot":"","sources":["../src/bundle.ts"],"names":[],"mappings":"AA0BA,OAAO,EAGL,KAAK,mBAAmB,EAEzB,MAAM,eAAe,CAAC;AAEvB,4DAA4D;AAC5D,eAAO,MAAM,iCAAiC,EAAG,CAAU,CAAC;AAE5D;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,KAAK,EAAE,OAAO,EAAE,CAAC;IACjB,KAAK,EAAE,OAAO,EAAE,CAAC;CAClB;AAED,6EAA6E;AAC7E,MAAM,WAAW,eAAe;IAC9B,2EAA2E;IAC3E,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,EAAE,WAAW,CAAC;IACnB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E,CAAC,MAAM,EAAE,eAAe,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;CAClE;AAYD;;;;;;GAMG;AACH,eAAO,MAAM,WAAW,WACd,iBAAiB,YAChB,kBAAkB,KAC1B,iBAsBF,CAAC;AAUF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,aAClB,mBAAmB,qBACV,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,KAC7C,mBAUD,CAAC;AA2DH;;;;;;GAMG;AACH,eAAO,MAAM,sBAAsB,UAC1B,OAAO,KACb,iBAAiB,GAAG,IA0BtB,CAAC;AAEF,uCAAuC;AACvC,eAAO,MAAM,0BAA0B,WAAY,iBAAiB,KAAG,MAC/B,CAAC"}
package/dist/bundle.js ADDED
@@ -0,0 +1,289 @@
1
+ /**
2
+ * The ApplicationBundle: one JSON document carrying an application together
3
+ * with the full graphs of every workflow its operations bind, so an app
4
+ * ships, exports, and imports as a single artifact.
5
+ *
6
+ * Inside a bundle an operation's `workflowId` holds a bundle-local **key**
7
+ * (`workflows[].key`), not a real workflow id. Import creates the workflows
8
+ * and rewrites the keys to the new ids — the same indirection `.nodetool`
9
+ * bundles use for `bundle://` asset refs. An asset-carrying bundle is this
10
+ * JSON riding inside the existing `.nodetool` zip alongside the asset entries,
11
+ * with the refs inside the graphs left as `bundle://`; nothing here needs to
12
+ * change for that path.
13
+ *
14
+ * Everything in this module is pure. Persistence — creating workflow rows,
15
+ * inserting the application, unpacking a zip — stays in the server and CLI
16
+ * callers.
17
+ */
18
+ import { isCallable, isNonEmptyString, isNumber, isRecord, isString } from "./predicates.js";
19
+ import { operationTarget, parseApplicationDocument } from "./document.js";
20
+ /** Bumped whenever the bundle parser needs a new branch. */
21
+ export const APPLICATION_BUNDLE_SCHEMA_VERSION = 1;
22
+ const isGraph = (value) => isRecord(value) && Array.isArray(value.nodes) && Array.isArray(value.edges);
23
+ const slugify = (name) => name
24
+ .toLowerCase()
25
+ .replace(/[^a-z0-9]+/g, "-")
26
+ // The collapse above leaves at most one dash at each end, so single-character
27
+ // anchors suffice — `/^-+|-+$/` backtracks polynomially on dash-heavy names.
28
+ .replace(/^-/, "")
29
+ .replace(/-$/, "");
30
+ /** A readable, collision-free key per id, stable for a given input order. */
31
+ const assignKeys = (items, fallback) => {
32
+ const keys = new Map();
33
+ const taken = new Set();
34
+ for (const item of items) {
35
+ if (keys.has(item.id))
36
+ continue;
37
+ const base = slugify(item.name) || fallback;
38
+ let key = base;
39
+ let suffix = 2;
40
+ while (taken.has(key)) {
41
+ key = `${base}-${suffix}`;
42
+ suffix += 1;
43
+ }
44
+ taken.add(key);
45
+ keys.set(item.id, key);
46
+ }
47
+ return keys;
48
+ };
49
+ /**
50
+ * Rewrite every operation's target id through the map for its kind, leaving
51
+ * misses alone: an id no carried entry covers is a link that is already broken,
52
+ * and dropping the operation would hide it.
53
+ */
54
+ const rewriteOperations = (document, workflowMap, scriptMap) => ({
55
+ ...document,
56
+ operations: document.operations.map((operation) => {
57
+ const target = operationTarget(operation);
58
+ if (target.kind === "script") {
59
+ const replacement = scriptMap.get(target.scriptId);
60
+ return replacement === undefined
61
+ ? operation
62
+ : {
63
+ ...operation,
64
+ target: { ...target, scriptId: replacement }
65
+ };
66
+ }
67
+ const replacement = workflowMap.get(operation.workflowId);
68
+ return replacement === undefined
69
+ ? operation
70
+ : { ...operation, workflowId: replacement };
71
+ })
72
+ });
73
+ /**
74
+ * Serialize an application and its workflows into a bundle: operations stop
75
+ * pointing at workflow ids and point at bundle-local keys instead.
76
+ *
77
+ * A workflow an operation binds but `workflows` does not carry keeps its raw
78
+ * id — the link is already broken, and dropping the operation would hide it.
79
+ */
80
+ export const bundleFromApplication = (app, workflows, scripts = []) => {
81
+ const keys = assignKeys(workflows.map((workflow) => ({
82
+ id: workflow.workflowId,
83
+ name: workflow.name
84
+ })), "workflow");
85
+ const scriptKeys = assignKeys(scripts.map((script) => ({ id: script.scriptId, name: script.name })), "script");
86
+ const seen = new Set();
87
+ const bundled = [];
88
+ for (const workflow of workflows) {
89
+ if (seen.has(workflow.workflowId))
90
+ continue;
91
+ seen.add(workflow.workflowId);
92
+ const key = keys.get(workflow.workflowId);
93
+ if (key === undefined)
94
+ continue;
95
+ const entry = {
96
+ key,
97
+ name: workflow.name,
98
+ graph: workflow.graph,
99
+ version: workflow.version ?? null,
100
+ graphHash: workflow.graphHash ?? null
101
+ };
102
+ if (workflow.description !== undefined) {
103
+ entry.description = workflow.description;
104
+ }
105
+ if (workflow.sourceId !== undefined) {
106
+ entry.sourceId = workflow.sourceId;
107
+ }
108
+ bundled.push(entry);
109
+ }
110
+ const seenScripts = new Set();
111
+ const bundledScripts = [];
112
+ for (const script of scripts) {
113
+ if (seenScripts.has(script.scriptId))
114
+ continue;
115
+ seenScripts.add(script.scriptId);
116
+ const key = scriptKeys.get(script.scriptId);
117
+ if (key === undefined)
118
+ continue;
119
+ const entry = {
120
+ key,
121
+ name: script.name,
122
+ document: script.document,
123
+ version: script.version ?? null
124
+ };
125
+ if (script.sourceId !== undefined) {
126
+ entry.sourceId = script.sourceId;
127
+ }
128
+ bundledScripts.push(entry);
129
+ }
130
+ return {
131
+ schemaVersion: APPLICATION_BUNDLE_SCHEMA_VERSION,
132
+ name: app.name,
133
+ description: app.description ?? "",
134
+ app: rewriteOperations(app.document, keys, scriptKeys),
135
+ workflows: bundled,
136
+ scripts: bundledScripts
137
+ };
138
+ };
139
+ const defaultMintedId = (option) => {
140
+ const uuid = globalThis.crypto?.randomUUID;
141
+ if (!isCallable(uuid)) {
142
+ throw new Error(`applyBundle needs a ${option} option: crypto.randomUUID is unavailable`);
143
+ }
144
+ return globalThis.crypto.randomUUID();
145
+ };
146
+ /**
147
+ * Turn a bundle back into an application plus the workflows to create: every
148
+ * carried workflow gets a fresh id, and the document's operations are
149
+ * rewritten from bundle keys to those ids.
150
+ *
151
+ * Pure — the caller writes the rows, in whatever order its storage needs.
152
+ */
153
+ export const applyBundle = (bundle, options = {}) => {
154
+ const mint = options.newWorkflowId ?? (() => defaultMintedId("newWorkflowId"));
155
+ const mintScript = options.newScriptId ?? (() => defaultMintedId("newScriptId"));
156
+ const workflows = bundle.workflows.map((workflow, index) => ({ ...workflow, id: mint(workflow, index) }));
157
+ const scripts = bundle.scripts.map((script, index) => ({ ...script, id: mintScript(script, index) }));
158
+ const keyToId = new Map(workflows.map((w) => [w.key, w.id]));
159
+ const scriptKeyToId = new Map(scripts.map((s) => [s.key, s.id]));
160
+ return {
161
+ app: {
162
+ name: bundle.name,
163
+ description: bundle.description,
164
+ document: rewriteOperations(bundle.app, keyToId, scriptKeyToId)
165
+ },
166
+ workflows,
167
+ scripts
168
+ };
169
+ };
170
+ const isPortList = (value) => Array.isArray(value) &&
171
+ value.every((port) => isRecord(port) && isString(port.name) && isString(port.type));
172
+ /**
173
+ * Re-pin every script operation to the version the importer actually created.
174
+ * A bundle carries one document per script; the row it lands in has its own
175
+ * version numbering, so the number the export pinned means nothing here.
176
+ */
177
+ export const pinScriptVersions = (document, versionByScriptId) => ({
178
+ ...document,
179
+ operations: document.operations.map((operation) => {
180
+ const target = operationTarget(operation);
181
+ if (target.kind !== "script")
182
+ return operation;
183
+ const version = versionByScriptId.get(target.scriptId);
184
+ return version === undefined
185
+ ? operation
186
+ : { ...operation, target: { ...target, scriptVersion: version } };
187
+ })
188
+ });
189
+ const parseScriptDocument = (value) => {
190
+ if (!isRecord(value))
191
+ return null;
192
+ if (!isString(value.code))
193
+ return null;
194
+ if (!isPortList(value.inputs) || !isPortList(value.outputs))
195
+ return null;
196
+ // Everything past the ports is carried through untouched — the pinned Zod
197
+ // contract in `@nodetool-ai/protocol` is what checks the rest, wherever the
198
+ // document is actually run.
199
+ return {
200
+ ...value,
201
+ schemaVersion: isNumber(value.schemaVersion) ? value.schemaVersion : 1,
202
+ code: value.code,
203
+ inputs: value.inputs,
204
+ outputs: value.outputs
205
+ };
206
+ };
207
+ const parseScript = (value) => {
208
+ if (!isRecord(value))
209
+ return null;
210
+ const { key, name } = value;
211
+ if (!isNonEmptyString(key))
212
+ return null;
213
+ const document = parseScriptDocument(value.document);
214
+ if (!document)
215
+ return null;
216
+ const parsed = {
217
+ key,
218
+ name: isString(name) ? name : key,
219
+ document,
220
+ version: isNumber(value.version) ? value.version : null
221
+ };
222
+ if (isNonEmptyString(value.sourceId)) {
223
+ parsed.sourceId = value.sourceId;
224
+ }
225
+ return parsed;
226
+ };
227
+ const parseWorkflow = (value) => {
228
+ if (!isRecord(value))
229
+ return null;
230
+ const { key, name, graph } = value;
231
+ if (!isNonEmptyString(key))
232
+ return null;
233
+ if (!isGraph(graph))
234
+ return null;
235
+ const parsed = {
236
+ key,
237
+ name: isString(name) ? name : key,
238
+ graph,
239
+ version: isNumber(value.version) ? value.version : null,
240
+ graphHash: isString(value.graphHash) ? value.graphHash : null
241
+ };
242
+ if (isString(value.description)) {
243
+ parsed.description = value.description;
244
+ }
245
+ if (isNonEmptyString(value.sourceId)) {
246
+ parsed.sourceId = value.sourceId;
247
+ }
248
+ return parsed;
249
+ };
250
+ /**
251
+ * Parse an unknown value into an {@link ApplicationBundle}, or null when it is
252
+ * not one. A future schema version is rejected rather than guessed at, and a
253
+ * workflow entry without a key or a graph is dropped. The document goes
254
+ * through `parseApplicationDocument`, so a bundle carrying a legacy
255
+ * `{ version, data }` document parses too.
256
+ */
257
+ export const parseApplicationBundle = (value) => {
258
+ if (!isRecord(value))
259
+ return null;
260
+ const schemaVersion = isNumber(value.schemaVersion)
261
+ ? value.schemaVersion
262
+ : APPLICATION_BUNDLE_SCHEMA_VERSION;
263
+ if (schemaVersion > APPLICATION_BUNDLE_SCHEMA_VERSION)
264
+ return null;
265
+ const app = parseApplicationDocument(value.app);
266
+ if (!app)
267
+ return null;
268
+ const workflows = Array.isArray(value.workflows)
269
+ ? value.workflows
270
+ .map(parseWorkflow)
271
+ .filter((w) => w !== null)
272
+ : [];
273
+ const scripts = Array.isArray(value.scripts)
274
+ ? value.scripts
275
+ .map(parseScript)
276
+ .filter((script) => script !== null)
277
+ : [];
278
+ return {
279
+ schemaVersion: APPLICATION_BUNDLE_SCHEMA_VERSION,
280
+ name: isString(value.name) ? value.name : "Untitled app",
281
+ description: isString(value.description) ? value.description : "",
282
+ app,
283
+ workflows,
284
+ scripts
285
+ };
286
+ };
287
+ /** The bundle as a file's contents. */
288
+ export const serializeApplicationBundle = (bundle) => `${JSON.stringify(bundle, null, 2)}\n`;
289
+ //# sourceMappingURL=bundle.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"bundle.js","sourceRoot":"","sources":["../src/bundle.ts"],"names":[],"mappings":"AACA;;;;;;;;;;;;;;;;GAgBG;AAEH,OAAO,EACL,UAAU,EACV,gBAAgB,EAChB,QAAQ,EACR,QAAQ,EACR,QAAQ,EACT,MAAM,iBAAiB,CAAC;AACzB,OAAO,EACL,eAAe,EACf,wBAAwB,EAGzB,MAAM,eAAe,CAAC;AAEvB,4DAA4D;AAC5D,MAAM,CAAC,MAAM,iCAAiC,GAAG,CAAU,CAAC;AA0G5D,MAAM,OAAO,GAAG,CAAC,KAAc,EAAwB,EAAE,CACvD,QAAQ,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;AAE9E,MAAM,OAAO,GAAG,CAAC,IAAY,EAAU,EAAE,CACvC,IAAI;KACD,WAAW,EAAE;KACb,OAAO,CAAC,aAAa,EAAE,GAAG,CAAC;IAC5B,8EAA8E;IAC9E,6EAA6E;KAC5E,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC;KACjB,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AAEvB,6EAA6E;AAC7E,MAAM,UAAU,GAAG,CACjB,KAAkD,EAClD,QAAgB,EACK,EAAE;IACvB,MAAM,IAAI,GAAG,IAAI,GAAG,EAAkB,CAAC;IACvC,MAAM,KAAK,GAAG,IAAI,GAAG,EAAU,CAAC;IAChC,KAAK,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AiC;QAChD,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,cAAc;QACxD,WAAW,EAAE,QAAQ,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC,CAAC,EAAE;QACjE,GAAG;QACH,SAAS;QACT,OAAO;KACR,CAAC;AACJ,CAAC,CAAC;AAEF,uCAAuC;AACvC,MAAM,CAAC,MAAM,0BAA0B,GAAG,CAAC,MAAyB,EAAU,EAAE,CAC9E,GAAG,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC,IAAI,CAAC"}
package/dist/chat.d.ts ADDED
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Conversation semantics for the chat widgets.
3
+ *
4
+ * A thread and a composer exist on three surfaces (web, mobile, the headless
5
+ * `app debug` harness), and all three have to agree on what a bound value means
6
+ * as a conversation — otherwise an app that reads correctly in the builder
7
+ * renders as one long assistant turn on a phone.
8
+ */
9
+ /** One turn of a conversation. `content` is text, or a list of content parts. */
10
+ export interface ChatMessage {
11
+ role: string;
12
+ content: unknown;
13
+ }
14
+ /** What a composer writes to its bound input when the user sends. */
15
+ export type ComposerValueFormat = "text" | "message" | "history";
16
+ /**
17
+ * Read a bound value as a conversation. A list of `{role, content}` objects is
18
+ * the canonical form; anything else — a streamed string, a media ref, a list of
19
+ * results — reads as one assistant turn, so a thread bound straight to an LLM
20
+ * output still renders.
21
+ */
22
+ export declare const messagesFrom: (value: unknown) => ChatMessage[];
23
+ /**
24
+ * The message a composer sends: plain text when there is nothing attached, and
25
+ * the protocol's content-part list when there is — the shape a MessageInput
26
+ * node and every provider already accept.
27
+ */
28
+ export declare const composeUserMessage: (text: string, imageUris?: ReadonlyArray<string>) => ChatMessage & {
29
+ type: "message";
30
+ };
31
+ /** The plain text of a message's content, ignoring media parts. */
32
+ export declare const messageText: (content: unknown) => string;
33
+ //# sourceMappingURL=chat.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat.d.ts","sourceRoot":"","sources":["../src/chat.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAIH,iFAAiF;AACjF,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,qEAAqE;AACrE,MAAM,MAAM,mBAAmB,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,CAAC;AAEjE;;;;;GAKG;AACH,eAAO,MAAM,YAAY,UAAW,OAAO,KAAG,WAAW,EAyBxD,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,kBAAkB,SACvB,MAAM,cACD,aAAa,CAAC,MAAM,CAAC,KAC/B,WAAW,GAAG;IAAE,IAAI,EAAE,SAAS,CAAA;CAejC,CAAC;AAEF,mEAAmE;AACnE,eAAO,MAAM,WAAW,YAAa,OAAO,KAAG,MAQ9C,CAAC"}
package/dist/chat.js ADDED
@@ -0,0 +1,77 @@
1
+ /**
2
+ * Conversation semantics for the chat widgets.
3
+ *
4
+ * A thread and a composer exist on three surfaces (web, mobile, the headless
5
+ * `app debug` harness), and all three have to agree on what a bound value means
6
+ * as a conversation — otherwise an app that reads correctly in the builder
7
+ * renders as one long assistant turn on a phone.
8
+ */
9
+ import { isRecord, isString } from "./predicates.js";
10
+ /**
11
+ * Read a bound value as a conversation. A list of `{role, content}` objects is
12
+ * the canonical form; anything else — a streamed string, a media ref, a list of
13
+ * results — reads as one assistant turn, so a thread bound straight to an LLM
14
+ * output still renders.
15
+ */
16
+ export const messagesFrom = (value) => {
17
+ if (value == null || value === "")
18
+ return [];
19
+ const items = Array.isArray(value) ? value : [value];
20
+ const messages = [];
21
+ // A streaming output accumulates one item per chunk. Those chunks are one
22
+ // reply, so consecutive text joins into the turn in progress rather than
23
+ // giving the thread a bubble per token.
24
+ let streaming = false;
25
+ for (const item of items) {
26
+ if (item == null || item === "")
27
+ continue;
28
+ if (isRecord(item) && isString(item.role)) {
29
+ messages.push({ role: item.role, content: item.content ?? "" });
30
+ streaming = false;
31
+ continue;
32
+ }
33
+ const chunk = isString(item) ? item : null;
34
+ const last = messages[messages.length - 1];
35
+ if (chunk !== null && streaming && last) {
36
+ last.content = `${last.content}${chunk}`;
37
+ continue;
38
+ }
39
+ messages.push({ role: "assistant", content: item });
40
+ streaming = chunk !== null;
41
+ }
42
+ return messages;
43
+ };
44
+ /**
45
+ * The message a composer sends: plain text when there is nothing attached, and
46
+ * the protocol's content-part list when there is — the shape a MessageInput
47
+ * node and every provider already accept.
48
+ */
49
+ export const composeUserMessage = (text, imageUris = []) => {
50
+ if (imageUris.length === 0) {
51
+ return { type: "message", role: "user", content: text };
52
+ }
53
+ return {
54
+ type: "message",
55
+ role: "user",
56
+ content: [
57
+ ...(text ? [{ type: "text", text }] : []),
58
+ ...imageUris.map((uri) => ({
59
+ type: "image_url",
60
+ image: { type: "image", uri }
61
+ }))
62
+ ]
63
+ };
64
+ };
65
+ /** The plain text of a message's content, ignoring media parts. */
66
+ export const messageText = (content) => {
67
+ if (isString(content))
68
+ return content;
69
+ if (!Array.isArray(content))
70
+ return content == null ? "" : String(content);
71
+ return content
72
+ .filter((part) => isRecord(part))
73
+ .filter((part) => part.type === "text" && isString(part.text))
74
+ .map((part) => part.text)
75
+ .join("\n");
76
+ };
77
+ //# sourceMappingURL=chat.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chat.js","sourceRoot":"","sources":["../src/chat.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAWrD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,KAAc,EAAiB,EAAE;IAC5D,IAAI,KAAK,IAAI,IAAI,IAAI,KAAK,KAAK,EAAE;QAAE,OAAO,EAAE,CAAC;IAC7C,MAAM,K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,IAAI,CAAC,CAAC;AAChB,CAAC,CAAC"}
@@ -0,0 +1,66 @@
1
+ /**
2
+ * The entire logic surface of the app layer: visibility, enablement, and
3
+ * formatting.
4
+ *
5
+ * This is a closed vocabulary on purpose. Derived values, validation rules,
6
+ * and data transformation belong in the graph, where they are testable,
7
+ * cacheable, and visible to the agent. If real apps outgrow what is here, the
8
+ * revision path is to adopt CEL or JSONLogic in the `Condition` and `format`
9
+ * slots — not to grow a language in this file.
10
+ */
11
+ import type { BindingRef, BindingScope, BindingMode, ExecutionField } from "./bindings.js";
12
+ import type { AppInstanceState } from "./state.js";
13
+ export type StateRef = {
14
+ source: "variable";
15
+ variableId: string;
16
+ } | {
17
+ source: "output";
18
+ operationId: string;
19
+ outputNodeId: string;
20
+ } | {
21
+ source: "input";
22
+ operationId: string;
23
+ inputNodeId: string;
24
+ } | {
25
+ source: "execution";
26
+ operationId: string;
27
+ field: ExecutionField;
28
+ };
29
+ export type ConditionOp = "eq" | "neq" | "gt" | "lt" | "gte" | "lte" | "contains" | "empty" | "notEmpty";
30
+ export interface Condition {
31
+ ref: StateRef;
32
+ op: ConditionOp;
33
+ value?: unknown;
34
+ }
35
+ /** How a condition is stored in Puck props: a binding token plus an operator. */
36
+ export interface ConditionProps {
37
+ binding?: string;
38
+ op?: string;
39
+ value?: string;
40
+ }
41
+ /** Convert a resolved binding into the {@link StateRef} a condition reads. */
42
+ export declare const refFromBinding: (ref: BindingRef) => StateRef | null;
43
+ /**
44
+ * Parse a Puck-stored condition. Returns null when the binding is unset or
45
+ * cannot be resolved — an unresolvable condition must not silently hide a
46
+ * widget, so callers treat null as "no condition".
47
+ */
48
+ export declare const parseCondition: (props: ConditionProps | null | undefined, scope: BindingScope, mode?: BindingMode) => Condition | null;
49
+ /**
50
+ * Read the current value a {@link StateRef} points at.
51
+ *
52
+ * HOLDOUT (anti-slop/no-unknown-returns): a variable, input or output slot
53
+ * holds a workflow value — the open domain `AppInstanceState` stores as
54
+ * `unknown` — so the read answers in that domain too.
55
+ */
56
+ export declare const readRef: (state: AppInstanceState, ref: StateRef) => unknown;
57
+ export declare const evaluateCondition: (state: AppInstanceState, condition: Condition) => boolean;
58
+ export declare const FORMAT_FILTERS: string[];
59
+ /**
60
+ * Interpolate a `format` template. `{binding}` inserts a value, optionally
61
+ * piped through one filter: `{op:main/out:n1|truncate:80}`. An unknown filter
62
+ * or unresolvable binding renders as the empty string rather than leaking the
63
+ * template into the UI.
64
+ */
65
+ export declare const formatTemplate: (template: string, state: AppInstanceState, scope: BindingScope) => string;
66
+ //# sourceMappingURL=conditions.d.ts.map