@littlepress/harness-integrations 0.0.0-stage → 1.0.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/dist/dsh.d.ts ADDED
@@ -0,0 +1,32 @@
1
+ import { type Footprint, type GenerateRequest, type GenerationEvent, type Generator, type HostActions, type RenderTarget, type RootChrome, type SurfaceArtifact, type SurfaceState } from "@littlepress/ui-foundation-contracts";
2
+ import { type ActionInvocation, HostBridge } from "./host.js";
3
+ export type DshPresentationInput = {
4
+ requestId: string;
5
+ question: string;
6
+ answer?: string | null;
7
+ data?: GenerateRequest["data"];
8
+ instructions?: string | null;
9
+ actions: HostActions;
10
+ locale: string;
11
+ footprint: Footprint;
12
+ state: SurfaceState;
13
+ chrome: RootChrome;
14
+ renderTarget: RenderTarget;
15
+ };
16
+ export type DshRendererWiring = {
17
+ onAction(input: ActionInvocation): Promise<unknown>;
18
+ };
19
+ export interface DshRendererSlot {
20
+ render(artifact: SurfaceArtifact, wiring: DshRendererWiring): void | Promise<void>;
21
+ }
22
+ export type DshAdapterOptions = {
23
+ generator: Generator;
24
+ host: HostBridge;
25
+ renderer: DshRendererSlot;
26
+ };
27
+ export declare function createDshGenerateRequest(input: DshPresentationInput): GenerateRequest;
28
+ export declare class DshAdapter {
29
+ #private;
30
+ constructor(options: DshAdapterOptions);
31
+ present(input: DshPresentationInput, signal?: AbortSignal): AsyncIterable<GenerationEvent>;
32
+ }
package/dist/dsh.js ADDED
@@ -0,0 +1,54 @@
1
+ import { CONTRACT_VERSION, validateContract, } from "@littlepress/ui-foundation-contracts";
2
+ export function createDshGenerateRequest(input) {
3
+ const request = {
4
+ contract_version: CONTRACT_VERSION,
5
+ request_id: input.requestId,
6
+ question: input.question,
7
+ ...(input.answer === undefined ? {} : { answer: input.answer }),
8
+ ...(input.data === undefined ? {} : { data: input.data }),
9
+ ...(input.instructions === undefined ? {} : { instructions: input.instructions }),
10
+ actions: input.actions,
11
+ locale: input.locale,
12
+ footprint: input.footprint,
13
+ state: input.state,
14
+ chrome: input.chrome,
15
+ render_target: input.renderTarget,
16
+ };
17
+ const issues = validateContract("GenerateRequest", request);
18
+ if (issues.length > 0) {
19
+ const first = issues[0];
20
+ throw new Error(`DSH materials cannot form GenerateRequest at ${first?.path}: ${first?.message}`);
21
+ }
22
+ return request;
23
+ }
24
+ export class DshAdapter {
25
+ #generator;
26
+ #host;
27
+ #renderer;
28
+ constructor(options) {
29
+ this.#generator = options.generator;
30
+ this.#host = options.host;
31
+ this.#renderer = options.renderer;
32
+ }
33
+ present(input, signal) {
34
+ return this.#present(input, signal);
35
+ }
36
+ async *#present(input, signal) {
37
+ const request = createDshGenerateRequest(input);
38
+ const rendered = new Set();
39
+ for await (const event of this.#generator.generate(request, signal)) {
40
+ const artifact = event.artifact;
41
+ if (artifact) {
42
+ const renderKey = `${artifact.artifact_id}:${artifact.revision}`;
43
+ if (!rendered.has(renderKey)) {
44
+ rendered.add(renderKey);
45
+ const surface = this.#host.attach(request, artifact);
46
+ await this.#renderer.render(artifact, {
47
+ onAction: (action) => surface.dispatch(action),
48
+ });
49
+ }
50
+ }
51
+ yield event;
52
+ }
53
+ }
54
+ }
package/dist/host.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ import { type ActionEvent, type GenerateRequest, type JsonObject, type SurfaceArtifact } from "@littlepress/ui-foundation-contracts";
2
+ export type PartnerActionHandler = (event: ActionEvent) => unknown | Promise<unknown>;
3
+ export type ActionInvocation = {
4
+ actionName: string;
5
+ payload: JsonObject;
6
+ invocationId?: string;
7
+ itemId?: string | null;
8
+ };
9
+ export declare class HostActionError extends Error {
10
+ readonly name: string;
11
+ }
12
+ export declare class IdempotencyConflictError extends HostActionError {
13
+ readonly name: string;
14
+ }
15
+ export declare class HostBridge {
16
+ #private;
17
+ constructor(options?: {
18
+ createInvocationId?: () => string;
19
+ });
20
+ register(actionName: string, handler: PartnerActionHandler): () => void;
21
+ attach(request: GenerateRequest, artifact: SurfaceArtifact): SurfaceHostBridge;
22
+ /**
23
+ * Bind the Widget that a whole answer shows after its text. The Widget's actions keep
24
+ * their own identity: the original request, its real run, its own surface and revision,
25
+ * and the actions that request allowed. The answer artifact must hold exactly that
26
+ * Widget's components under the `w.` prefix, or the attach is refused.
27
+ */
28
+ attachAnswer(answer: SurfaceArtifact, widget: {
29
+ request: GenerateRequest;
30
+ artifact: SurfaceArtifact;
31
+ }): SurfaceHostBridge;
32
+ dispose(): void;
33
+ }
34
+ export interface SurfaceHostBridge {
35
+ createActionEvent(input: ActionInvocation): ActionEvent;
36
+ dispatch(input: ActionInvocation): Promise<unknown>;
37
+ }
package/dist/host.js ADDED
@@ -0,0 +1,174 @@
1
+ import { CONTRACT_VERSION, validateActionEvent, validateContract, } from "@littlepress/ui-foundation-contracts";
2
+ export class HostActionError extends Error {
3
+ name = "HostActionError";
4
+ }
5
+ export class IdempotencyConflictError extends HostActionError {
6
+ name = "IdempotencyConflictError";
7
+ }
8
+ function formatIssues(label, issues) {
9
+ const first = issues[0];
10
+ return `${label} is invalid at ${first?.path ?? "<root>"}: ${first?.message ?? "unknown error"}`;
11
+ }
12
+ function invocationId() {
13
+ return `invoke_${globalThis.crypto.randomUUID()}`;
14
+ }
15
+ function canonicalize(value) {
16
+ if (Array.isArray(value))
17
+ return value.map(canonicalize);
18
+ if (typeof value !== "object" || value === null)
19
+ return value;
20
+ return Object.fromEntries(Object.entries(value)
21
+ .sort(([left], [right]) => left.localeCompare(right))
22
+ .map(([key, entry]) => [key, canonicalize(entry)]));
23
+ }
24
+ export class HostBridge {
25
+ #handlers = new Map();
26
+ #invocations = new Map();
27
+ #createInvocationId;
28
+ constructor(options = {}) {
29
+ this.#createInvocationId = options.createInvocationId ?? invocationId;
30
+ }
31
+ register(actionName, handler) {
32
+ if (this.#handlers.has(actionName))
33
+ throw new HostActionError(`Handler ${actionName} is already registered.`);
34
+ this.#handlers.set(actionName, handler);
35
+ return () => {
36
+ if (this.#handlers.get(actionName) === handler)
37
+ this.#handlers.delete(actionName);
38
+ };
39
+ }
40
+ attach(request, artifact) {
41
+ const requestIssues = validateContract("GenerateRequest", request);
42
+ if (requestIssues.length > 0)
43
+ throw new HostActionError(formatIssues("GenerateRequest", requestIssues));
44
+ const artifactIssues = validateContract("SurfaceArtifact", artifact);
45
+ if (artifactIssues.length > 0)
46
+ throw new HostActionError(formatIssues("SurfaceArtifact", artifactIssues));
47
+ if (artifact.request_id !== request.request_id) {
48
+ throw new HostActionError("SurfaceArtifact request_id does not match the GenerateRequest.");
49
+ }
50
+ if (artifact.catalog.id !== request.render_target.catalog.id
51
+ || artifact.catalog.version !== request.render_target.catalog.version) {
52
+ throw new HostActionError("SurfaceArtifact catalog does not match the requested render target.");
53
+ }
54
+ return new BoundSurfaceHostBridge(request, artifact, this.#handlers, this.#invocations, this.#createInvocationId);
55
+ }
56
+ /**
57
+ * Bind the Widget that a whole answer shows after its text. The Widget's actions keep
58
+ * their own identity: the original request, its real run, its own surface and revision,
59
+ * and the actions that request allowed. The answer artifact must hold exactly that
60
+ * Widget's components under the `w.` prefix, or the attach is refused.
61
+ */
62
+ attachAnswer(answer, widget) {
63
+ const answerIssues = validateContract("SurfaceArtifact", answer);
64
+ if (answerIssues.length > 0)
65
+ throw new HostActionError(formatIssues("SurfaceArtifact", answerIssues));
66
+ const bridge = this.attach(widget.request, widget.artifact);
67
+ // The compiler joins the Widget as its messages leave it: later updateComponents replace
68
+ // components by id or add new ones, so compare with that final set, not the first message.
69
+ const components = (artifact) => {
70
+ const table = new Map();
71
+ for (const message of artifact.messages) {
72
+ const body = (message.createSurface ?? message.updateComponents);
73
+ for (const component of body?.components ?? [])
74
+ table.set(String(component.id), component);
75
+ }
76
+ return [...table.values()];
77
+ };
78
+ const inAnswer = new Map(answer.messages[0]?.createSurface?.components
79
+ ?.filter((component) => typeof component.id === "string" && component.id.startsWith("w."))
80
+ .map((component) => [String(component.id).slice(2), component]) ?? []);
81
+ const fromWidget = components(widget.artifact);
82
+ const matches = fromWidget.length === inAnswer.size && fromWidget.every((component) => {
83
+ const moved = inAnswer.get(String(component.id));
84
+ return moved !== undefined && sameWidgetComponent(component, moved);
85
+ });
86
+ if (!matches)
87
+ throw new HostActionError("The answer does not hold this Widget's components under w.");
88
+ return bridge;
89
+ }
90
+ dispose() {
91
+ this.#invocations.clear();
92
+ }
93
+ }
94
+ /**
95
+ * The answer moves a Widget's ids and child references under `w.`: the component's `id`, its
96
+ * `children` ids and a child template's `componentId`. Nothing else changes, so an `id` inside
97
+ * its data or an action's context is compared as it is.
98
+ */
99
+ function sameWidgetComponent(original, moved) {
100
+ if (typeof moved !== "object" || moved === null || Array.isArray(moved))
101
+ return false;
102
+ // Not under `w.` is not one of the Widget's components: it can never equal the original.
103
+ const restore = (id) => (typeof id === "string" && id.startsWith("w.") ? id.slice(2) : { unmoved: id });
104
+ const component = moved;
105
+ const restored = { ...component, id: restore(component.id) };
106
+ const children = component.children;
107
+ if (Array.isArray(children))
108
+ restored.children = children.map(restore);
109
+ else if (typeof children === "object" && children !== null && "componentId" in children) {
110
+ restored.children = { ...children, componentId: restore(children.componentId) };
111
+ }
112
+ return JSON.stringify(canonicalize(restored)) === JSON.stringify(canonicalize(original));
113
+ }
114
+ class BoundSurfaceHostBridge {
115
+ #actions;
116
+ #handlers;
117
+ #artifact;
118
+ #request;
119
+ #createInvocationId;
120
+ #invocations;
121
+ constructor(request, artifact, handlers, invocations, createInvocationId) {
122
+ this.#request = request;
123
+ this.#artifact = artifact;
124
+ this.#handlers = handlers;
125
+ this.#invocations = invocations;
126
+ this.#createInvocationId = createInvocationId;
127
+ this.#actions = new Map(request.actions.allowed.map((action) => [action.name, action]));
128
+ }
129
+ createActionEvent(input) {
130
+ const action = this.#actions.get(input.actionName);
131
+ if (!action)
132
+ throw new HostActionError(`Action ${input.actionName} is not allowed for this surface.`);
133
+ const itemBindings = Object.entries(action.payload_bindings).filter(([, binding]) => binding.source === "item");
134
+ if (itemBindings.length > 0 && !input.itemId) {
135
+ throw new HostActionError(`Item-bound action ${input.actionName} requires a stable itemId.`);
136
+ }
137
+ const mismatchedId = itemBindings.find(([field, binding]) => binding.path === "/id" && input.payload[field] !== input.itemId);
138
+ if (mismatchedId) {
139
+ throw new HostActionError(`Action payload field ${mismatchedId[0]} must match the renderer itemId.`);
140
+ }
141
+ const event = {
142
+ contract_version: CONTRACT_VERSION,
143
+ invocation_id: input.invocationId ?? this.#createInvocationId(),
144
+ action_name: input.actionName,
145
+ request_id: this.#request.request_id,
146
+ run_id: this.#artifact.run_id,
147
+ surface_id: this.#artifact.surface_id,
148
+ revision: this.#artifact.revision,
149
+ payload: input.payload,
150
+ ...(input.itemId === undefined ? {} : { item_id: input.itemId }),
151
+ };
152
+ const issues = validateActionEvent(action, event);
153
+ if (issues.length > 0)
154
+ throw new HostActionError(formatIssues("ActionEvent", issues));
155
+ return event;
156
+ }
157
+ dispatch(input) {
158
+ const event = this.createActionEvent(input);
159
+ const fingerprint = JSON.stringify(canonicalize(event));
160
+ const previous = this.#invocations.get(event.invocation_id);
161
+ if (previous) {
162
+ if (previous.fingerprint !== fingerprint) {
163
+ throw new IdempotencyConflictError(`Invocation ID ${event.invocation_id} was already used for different action content.`);
164
+ }
165
+ return previous.result;
166
+ }
167
+ const handler = this.#handlers.get(event.action_name);
168
+ if (!handler)
169
+ throw new HostActionError(`No Partner handler is registered for ${event.action_name}.`);
170
+ const result = Promise.resolve().then(() => handler(event));
171
+ this.#invocations.set(event.invocation_id, { fingerprint, result });
172
+ return result;
173
+ }
174
+ }
@@ -0,0 +1,4 @@
1
+ export { AnswerStream, DEFAULT_ANSWER_INTERVAL_MS, answerHostSection, answerSystemPrompt, type AnswerPromptOptions, type AnswerStreamOptions, } from "./answers.js";
2
+ export * from "./cloud.js";
3
+ export * from "./dsh.js";
4
+ export * from "./host.js";
package/dist/index.js ADDED
@@ -0,0 +1,5 @@
1
+ // The diff behind AnswerStream stays inside the SDK: Hosts stream answers through AnswerStream.
2
+ export { AnswerStream, DEFAULT_ANSWER_INTERVAL_MS, answerHostSection, answerSystemPrompt, } from "./answers.js";
3
+ export * from "./cloud.js";
4
+ export * from "./dsh.js";
5
+ export * from "./host.js";
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@littlepress/harness-integrations",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "1.0.0",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "git+https://github.com/BinaryTape/LittlePress.git",
7
+ "directory": "integrations/typescript"
8
+ },
9
+ "publishConfig": {
10
+ "access": "public"
11
+ },
12
+ "license": "Apache-2.0",
13
+ "homepage": "https://littlepress.ai",
14
+ "type": "module",
15
+ "files": [
16
+ "dist",
17
+ "LICENSE",
18
+ "NOTICE"
19
+ ],
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "import": "./dist/index.js"
24
+ },
25
+ "./cloud": {
26
+ "types": "./dist/cloud.d.ts",
27
+ "import": "./dist/cloud.js"
28
+ },
29
+ "./dsh": {
30
+ "types": "./dist/dsh.d.ts",
31
+ "import": "./dist/dsh.js"
32
+ },
33
+ "./host": {
34
+ "types": "./dist/host.d.ts",
35
+ "import": "./dist/host.js"
36
+ }
37
+ },
38
+ "dependencies": {
39
+ "@littlepress/ui-foundation-contracts": "1.0.0"
40
+ },
41
+ "devDependencies": {
42
+ "@littlepress/renderer-web": "1.0.0",
43
+ "@types/node": "24.3.0",
44
+ "esbuild": "0.25.12",
45
+ "playwright": "1.62.0",
46
+ "react": "18.3.1",
47
+ "react-dom": "18.3.1",
48
+ "tsx": "4.20.5",
49
+ "typescript": "5.8.3"
50
+ },
51
+ "scripts": {
52
+ "build": "rm -rf dist && tsc -p tsconfig.json",
53
+ "typecheck": "tsc -p tsconfig.json --noEmit",
54
+ "test": "tsx --test test/*.test.ts",
55
+ "check": "pnpm run typecheck && pnpm run test && pnpm run build"
56
+ }
6
57
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.