@ikenga/contract 0.7.0 → 0.9.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.
- package/README.md +56 -11
- package/dist/canvas/Canvas.d.ts +7 -0
- package/dist/canvas/Canvas.d.ts.map +1 -0
- package/dist/canvas/Canvas.js +115 -0
- package/dist/canvas/Canvas.js.map +1 -0
- package/dist/canvas/canvas.css +579 -0
- package/dist/canvas/index.d.ts +7 -0
- package/dist/canvas/index.d.ts.map +1 -0
- package/dist/canvas/index.js +4 -0
- package/dist/canvas/index.js.map +1 -0
- package/dist/canvas/types.d.ts +45 -0
- package/dist/canvas/types.d.ts.map +1 -0
- package/dist/canvas/types.js +2 -0
- package/dist/canvas/types.js.map +1 -0
- package/dist/canvas/use-drag-snap.d.ts +33 -0
- package/dist/canvas/use-drag-snap.d.ts.map +1 -0
- package/dist/canvas/use-drag-snap.js +73 -0
- package/dist/canvas/use-drag-snap.js.map +1 -0
- package/dist/canvas/use-pan-zoom.d.ts +32 -0
- package/dist/canvas/use-pan-zoom.d.ts.map +1 -0
- package/dist/canvas/use-pan-zoom.js +161 -0
- package/dist/canvas/use-pan-zoom.js.map +1 -0
- package/dist/host-verbs.d.ts +194 -0
- package/dist/host-verbs.d.ts.map +1 -0
- package/dist/host-verbs.js +15 -0
- package/dist/host-verbs.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/manifest.d.ts +376 -19
- package/dist/manifest.d.ts.map +1 -1
- package/dist/manifest.js +95 -4
- package/dist/manifest.js.map +1 -1
- package/dist/registry.d.ts +364 -36
- package/dist/registry.d.ts.map +1 -1
- package/dist/registry.js +9 -0
- package/dist/registry.js.map +1 -1
- package/dist/scopes.js +1 -1
- package/dist/scopes.js.map +1 -1
- package/package.json +24 -2
- package/schemas/registry/index-v1.json +11 -0
- package/src/canvas/Canvas.tsx +161 -0
- package/src/canvas/canvas.css +579 -0
- package/src/canvas/index.ts +14 -0
- package/src/canvas/types.ts +48 -0
- package/src/canvas/use-drag-snap.ts +107 -0
- package/src/canvas/use-pan-zoom.ts +211 -0
- package/src/host-verbs.ts +207 -0
- package/src/index.ts +1 -0
- package/src/manifest.test.ts +97 -0
- package/src/manifest.ts +109 -4
- package/src/registry.ts +9 -0
- package/src/scopes.ts +1 -1
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// Scale-aware grid-snap drag for the <Canvas> primitive.
|
|
2
|
+
//
|
|
3
|
+
// Owns the in-flight drag gesture (which item, where it started) and the
|
|
4
|
+
// pointer math that moves it: cursor delta is divided by the current viewport
|
|
5
|
+
// scale (so a 10px screen move at scale 0.5 becomes a 20px canvas move, and at
|
|
6
|
+
// scale 2.0 a 5px canvas move), then snapped to the `gridSnap` lattice.
|
|
7
|
+
//
|
|
8
|
+
// nx = round((startPos.x + dx / scale) / gridSnap) * gridSnap
|
|
9
|
+
//
|
|
10
|
+
// Does NOT own `layout` — it reads the current placement to seed the gesture
|
|
11
|
+
// and emits the moved layout through `onLayoutChange`. The parent stays the
|
|
12
|
+
// source of truth.
|
|
13
|
+
//
|
|
14
|
+
// Extracted verbatim (behavior-preserving) from shell home.tsx's canvas block.
|
|
15
|
+
|
|
16
|
+
import { useCallback, useEffect, useRef } from 'react';
|
|
17
|
+
import type { ItemId, Placement } from './types.js';
|
|
18
|
+
|
|
19
|
+
export interface DragState {
|
|
20
|
+
id: ItemId;
|
|
21
|
+
startMouse: { x: number; y: number };
|
|
22
|
+
startPos: { x: number; y: number };
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface UseDragSnapArgs {
|
|
26
|
+
/** Controlled layout (id → placement). Read at gesture start; never mutated. */
|
|
27
|
+
layout: Record<ItemId, Placement>;
|
|
28
|
+
/** Snap lattice in canvas units. Home passes 12, studio passes 24. */
|
|
29
|
+
gridSnap: number;
|
|
30
|
+
/** Current viewport scale — drag delta is divided by this. */
|
|
31
|
+
scale: number;
|
|
32
|
+
/** Emits the moved layout so the parent can persist / re-render. */
|
|
33
|
+
onLayoutChange?: (layout: Record<ItemId, Placement>) => void;
|
|
34
|
+
/** Stage element whose `.is-dragging` class suppresses the pan transition. */
|
|
35
|
+
stageRef: React.RefObject<HTMLDivElement | null>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface UseDragSnap {
|
|
39
|
+
dragState: React.MutableRefObject<DragState | null>;
|
|
40
|
+
/** Begin dragging `id` from a mousedown at screen (clientX, clientY). */
|
|
41
|
+
beginDrag: (id: ItemId, clientX: number, clientY: number) => void;
|
|
42
|
+
/** Is an item drag in flight? */
|
|
43
|
+
isDragging: () => boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function useDragSnap(args: UseDragSnapArgs): UseDragSnap {
|
|
47
|
+
const { layout, gridSnap, scale, onLayoutChange, stageRef } = args;
|
|
48
|
+
const dragState = useRef<DragState | null>(null);
|
|
49
|
+
|
|
50
|
+
// Keep the freshest layout/scale/snap reachable from the global listener
|
|
51
|
+
// without re-subscribing it on every render.
|
|
52
|
+
const layoutRef = useRef(layout);
|
|
53
|
+
layoutRef.current = layout;
|
|
54
|
+
const scaleRef = useRef(scale);
|
|
55
|
+
scaleRef.current = scale;
|
|
56
|
+
const gridSnapRef = useRef(gridSnap);
|
|
57
|
+
gridSnapRef.current = gridSnap;
|
|
58
|
+
const onLayoutChangeRef = useRef(onLayoutChange);
|
|
59
|
+
onLayoutChangeRef.current = onLayoutChange;
|
|
60
|
+
|
|
61
|
+
const beginDrag = useCallback(
|
|
62
|
+
(id: ItemId, clientX: number, clientY: number) => {
|
|
63
|
+
const w = layoutRef.current[id];
|
|
64
|
+
if (!w) return;
|
|
65
|
+
dragState.current = {
|
|
66
|
+
id,
|
|
67
|
+
startMouse: { x: clientX, y: clientY },
|
|
68
|
+
startPos: { x: w.x, y: w.y },
|
|
69
|
+
};
|
|
70
|
+
stageRef.current?.classList.add('is-dragging');
|
|
71
|
+
},
|
|
72
|
+
[stageRef]
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const isDragging = useCallback(() => dragState.current != null, []);
|
|
76
|
+
|
|
77
|
+
useEffect(() => {
|
|
78
|
+
const onMove = (e: MouseEvent) => {
|
|
79
|
+
if (!dragState.current) return;
|
|
80
|
+
const s = scaleRef.current || 1;
|
|
81
|
+
const snap = gridSnapRef.current;
|
|
82
|
+
const dx = e.clientX - dragState.current.startMouse.x;
|
|
83
|
+
const dy = e.clientY - dragState.current.startMouse.y;
|
|
84
|
+
const nx = Math.round((dragState.current.startPos.x + dx / s) / snap) * snap;
|
|
85
|
+
const ny = Math.round((dragState.current.startPos.y + dy / s) / snap) * snap;
|
|
86
|
+
const id = dragState.current.id;
|
|
87
|
+
const cur = layoutRef.current;
|
|
88
|
+
const prev = cur[id];
|
|
89
|
+
if (!prev || (prev.x === nx && prev.y === ny)) return;
|
|
90
|
+
onLayoutChangeRef.current?.({ ...cur, [id]: { ...prev, x: nx, y: ny } });
|
|
91
|
+
};
|
|
92
|
+
const onUp = () => {
|
|
93
|
+
if (dragState.current) {
|
|
94
|
+
dragState.current = null;
|
|
95
|
+
stageRef.current?.classList.remove('is-dragging');
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
window.addEventListener('mousemove', onMove);
|
|
99
|
+
window.addEventListener('mouseup', onUp);
|
|
100
|
+
return () => {
|
|
101
|
+
window.removeEventListener('mousemove', onMove);
|
|
102
|
+
window.removeEventListener('mouseup', onUp);
|
|
103
|
+
};
|
|
104
|
+
}, [stageRef]);
|
|
105
|
+
|
|
106
|
+
return { dragState, beginDrag, isDragging };
|
|
107
|
+
}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
// Pan / zoom / auto-fit for the <Canvas> primitive.
|
|
2
|
+
//
|
|
3
|
+
// Owns the viewport pan offset + scale, the imperative auto-fit math, the
|
|
4
|
+
// pan gesture (Space-drag / middle-mouse / empty-canvas drag), and the
|
|
5
|
+
// Space/Escape keyboard affordances + window-resize re-fit. It deliberately
|
|
6
|
+
// does NOT own `layout`, `selectedId`, or `editMode` — those stay parent-held
|
|
7
|
+
// and are threaded in as values so the auto-fit closure can read them.
|
|
8
|
+
//
|
|
9
|
+
// Extracted verbatim (behavior-preserving) from shell home.tsx's canvas block.
|
|
10
|
+
|
|
11
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
12
|
+
import type { ItemId, Placement, Viewport } from './types.js';
|
|
13
|
+
|
|
14
|
+
export interface UsePanZoomArgs {
|
|
15
|
+
/** Controlled layout (id → placement) the auto-fit bounding box reads. */
|
|
16
|
+
layout: Record<ItemId, Placement>;
|
|
17
|
+
/** Controlled edit-mode flag — auto-fit reserves palette width when true. */
|
|
18
|
+
editMode: boolean;
|
|
19
|
+
/** Re-fit on window resize. Default true. */
|
|
20
|
+
autoFitOnResize?: boolean;
|
|
21
|
+
/** Notified whenever the viewport pan/scale changes (controlled mirror). */
|
|
22
|
+
onViewportChange?: (viewport: Viewport) => void;
|
|
23
|
+
/** Escape exits edit mode → parent owns the state flip. */
|
|
24
|
+
onEditModeChange?: (editMode: boolean) => void;
|
|
25
|
+
/** Escape clears selection. */
|
|
26
|
+
onSelectionChange?: (selectedId: ItemId | null) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface UsePanZoom {
|
|
30
|
+
/** Live viewport (x/y/scale). Mirrors `pan` in the original home code. */
|
|
31
|
+
pan: Viewport;
|
|
32
|
+
setPan: React.Dispatch<React.SetStateAction<Viewport>>;
|
|
33
|
+
/** Imperative re-fit. `animate=false` snaps without the transition. */
|
|
34
|
+
autoFit: (animate?: boolean) => void;
|
|
35
|
+
canvasRef: React.RefObject<HTMLDivElement | null>;
|
|
36
|
+
stageRef: React.RefObject<HTMLDivElement | null>;
|
|
37
|
+
/** True while Space is held — drag/pan branch reads this. */
|
|
38
|
+
spaceDown: React.MutableRefObject<boolean>;
|
|
39
|
+
/** Begin a pan gesture from a mousedown at screen (clientX, clientY). */
|
|
40
|
+
beginPan: (clientX: number, clientY: number) => void;
|
|
41
|
+
/** Is a pan gesture in flight? */
|
|
42
|
+
isPanning: () => boolean;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const RESERVED_PALETTE_WIDTH = 320;
|
|
46
|
+
|
|
47
|
+
export function usePanZoom(args: UsePanZoomArgs): UsePanZoom {
|
|
48
|
+
const {
|
|
49
|
+
layout,
|
|
50
|
+
editMode,
|
|
51
|
+
autoFitOnResize = true,
|
|
52
|
+
onViewportChange,
|
|
53
|
+
onEditModeChange,
|
|
54
|
+
onSelectionChange,
|
|
55
|
+
} = args;
|
|
56
|
+
|
|
57
|
+
const [pan, setPan] = useState<Viewport>({ x: 0, y: 0, scale: 1 });
|
|
58
|
+
|
|
59
|
+
const canvasRef = useRef<HTMLDivElement>(null);
|
|
60
|
+
const stageRef = useRef<HTMLDivElement>(null);
|
|
61
|
+
const spaceDown = useRef(false);
|
|
62
|
+
const panStart = useRef<{ x: number; y: number } | null>(null);
|
|
63
|
+
// Latest pan reachable synchronously (for beginPan's offset seed) without
|
|
64
|
+
// adding `pan` to the gesture callbacks' deps.
|
|
65
|
+
const panRef = useRef(pan);
|
|
66
|
+
panRef.current = pan;
|
|
67
|
+
|
|
68
|
+
// Mirror viewport changes to the controlled parent in an effect (never
|
|
69
|
+
// inside a setState updater — that would setState-during-render the parent
|
|
70
|
+
// and trip React's "Cannot update a component while rendering a different
|
|
71
|
+
// component" warning). The ref skips the initial mount so the parent isn't
|
|
72
|
+
// notified of the default {0,0,1} before any real fit.
|
|
73
|
+
const viewportCbRef = useRef(onViewportChange);
|
|
74
|
+
viewportCbRef.current = onViewportChange;
|
|
75
|
+
const mountedViewport = useRef(false);
|
|
76
|
+
useEffect(() => {
|
|
77
|
+
if (!mountedViewport.current) {
|
|
78
|
+
mountedViewport.current = true;
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
viewportCbRef.current?.(pan);
|
|
82
|
+
}, [pan]);
|
|
83
|
+
|
|
84
|
+
// Auto-fit: scale the bounding box of all items into the visible area,
|
|
85
|
+
// scaling down only (never up). Reserves the palette gutter in edit mode.
|
|
86
|
+
// Threads `layout` + `editMode` as deps so the closure never goes stale.
|
|
87
|
+
const autoFit = useCallback(
|
|
88
|
+
(animate = true) => {
|
|
89
|
+
const canvas = canvasRef.current;
|
|
90
|
+
const items = Object.values(layout);
|
|
91
|
+
if (!canvas || !items.length) return;
|
|
92
|
+
let minX = Infinity,
|
|
93
|
+
minY = Infinity,
|
|
94
|
+
maxX = -Infinity,
|
|
95
|
+
maxY = -Infinity;
|
|
96
|
+
for (const w of items) {
|
|
97
|
+
if (w.x < minX) minX = w.x;
|
|
98
|
+
if (w.y < minY) minY = w.y;
|
|
99
|
+
if (w.x + w.w > maxX) maxX = w.x + w.w;
|
|
100
|
+
if (w.y + w.h > maxY) maxY = w.y + w.h;
|
|
101
|
+
}
|
|
102
|
+
const cw = canvas.clientWidth - (editMode ? RESERVED_PALETTE_WIDTH : 0) - 40;
|
|
103
|
+
const ch = canvas.clientHeight - 44 - 32;
|
|
104
|
+
const bw = maxX - minX;
|
|
105
|
+
const bh = maxY - minY;
|
|
106
|
+
const sx = bw > 0 ? cw / bw : 1;
|
|
107
|
+
const sy = bh > 0 ? ch / bh : 1;
|
|
108
|
+
const scale = Math.min(1, sx, sy);
|
|
109
|
+
const offX = Math.round((cw + 40 - bw * scale) / 2 - minX * scale);
|
|
110
|
+
const offY = Math.round((ch + 32 - bh * scale) / 2 - minY * scale + 22);
|
|
111
|
+
setPan({ x: offX, y: offY, scale });
|
|
112
|
+
if (stageRef.current) stageRef.current.classList.toggle('is-dragging', !animate);
|
|
113
|
+
},
|
|
114
|
+
[layout, editMode, setPan]
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
// Initial fit + window-resize re-fit. Empty dep array on purpose — autoFit
|
|
118
|
+
// is read through the ref-stable callback, matching the original effect.
|
|
119
|
+
useEffect(() => {
|
|
120
|
+
requestAnimationFrame(() => autoFit(false));
|
|
121
|
+
if (!autoFitOnResize) return;
|
|
122
|
+
const onResize = () => autoFit(true);
|
|
123
|
+
window.addEventListener('resize', onResize);
|
|
124
|
+
return () => window.removeEventListener('resize', onResize);
|
|
125
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
126
|
+
}, []);
|
|
127
|
+
|
|
128
|
+
// Re-fit whenever edit mode toggles (palette gutter changes the fit box).
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
autoFit(true);
|
|
131
|
+
}, [editMode, autoFit]);
|
|
132
|
+
|
|
133
|
+
// Keyboard — Escape exits edit, Space arms the pan-grab cursor.
|
|
134
|
+
useEffect(() => {
|
|
135
|
+
const down = (e: KeyboardEvent) => {
|
|
136
|
+
if (e.key === 'Escape' && editMode) {
|
|
137
|
+
onEditModeChange?.(false);
|
|
138
|
+
onSelectionChange?.(null);
|
|
139
|
+
}
|
|
140
|
+
if (
|
|
141
|
+
e.code === 'Space' &&
|
|
142
|
+
!e.repeat &&
|
|
143
|
+
document.activeElement === document.body &&
|
|
144
|
+
canvasRef.current
|
|
145
|
+
) {
|
|
146
|
+
spaceDown.current = true;
|
|
147
|
+
canvasRef.current.style.cursor = 'grab';
|
|
148
|
+
e.preventDefault();
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
const up = (e: KeyboardEvent) => {
|
|
152
|
+
if (e.code === 'Space') {
|
|
153
|
+
spaceDown.current = false;
|
|
154
|
+
if (canvasRef.current) canvasRef.current.style.cursor = '';
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
window.addEventListener('keydown', down);
|
|
158
|
+
window.addEventListener('keyup', up);
|
|
159
|
+
return () => {
|
|
160
|
+
window.removeEventListener('keydown', down);
|
|
161
|
+
window.removeEventListener('keyup', up);
|
|
162
|
+
};
|
|
163
|
+
}, [editMode, onEditModeChange, onSelectionChange]);
|
|
164
|
+
|
|
165
|
+
const beginPan = useCallback((clientX: number, clientY: number) => {
|
|
166
|
+
const p = panRef.current;
|
|
167
|
+
panStart.current = { x: clientX - p.x, y: clientY - p.y };
|
|
168
|
+
stageRef.current?.classList.add('is-dragging');
|
|
169
|
+
if (canvasRef.current) canvasRef.current.style.cursor = 'grabbing';
|
|
170
|
+
}, []);
|
|
171
|
+
|
|
172
|
+
const isPanning = useCallback(() => panStart.current != null, []);
|
|
173
|
+
|
|
174
|
+
// Global pan move/up. Drag (item) move/up lives in use-drag-snap so this
|
|
175
|
+
// effect only owns the pan branch + its own cleanup.
|
|
176
|
+
useEffect(() => {
|
|
177
|
+
const onMove = (e: MouseEvent) => {
|
|
178
|
+
if (panStart.current) {
|
|
179
|
+
setPan((p) => ({
|
|
180
|
+
...p,
|
|
181
|
+
x: e.clientX - panStart.current!.x,
|
|
182
|
+
y: e.clientY - panStart.current!.y,
|
|
183
|
+
}));
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
const onUp = () => {
|
|
187
|
+
if (panStart.current) {
|
|
188
|
+
panStart.current = null;
|
|
189
|
+
stageRef.current?.classList.remove('is-dragging');
|
|
190
|
+
if (canvasRef.current) canvasRef.current.style.cursor = spaceDown.current ? 'grab' : '';
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
window.addEventListener('mousemove', onMove);
|
|
194
|
+
window.addEventListener('mouseup', onUp);
|
|
195
|
+
return () => {
|
|
196
|
+
window.removeEventListener('mousemove', onMove);
|
|
197
|
+
window.removeEventListener('mouseup', onUp);
|
|
198
|
+
};
|
|
199
|
+
}, [setPan]);
|
|
200
|
+
|
|
201
|
+
return {
|
|
202
|
+
pan,
|
|
203
|
+
setPan,
|
|
204
|
+
autoFit,
|
|
205
|
+
canvasRef,
|
|
206
|
+
stageRef,
|
|
207
|
+
spaceDown,
|
|
208
|
+
beginPan,
|
|
209
|
+
isPanning,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Host-bridge verb shapes for the agent-ops pkg — the **G-TRIGGER** freeze gate.
|
|
3
|
+
*
|
|
4
|
+
* Unlike the kernel RPC methods in `rpc.ts`, `host.*` verbs are dispatched
|
|
5
|
+
* FE-side in the iframe host (`shell/src/components/pkg/pkg-iframe-host.tsx`)
|
|
6
|
+
* and invoked from the iframe via `app.callServerTool({ name, arguments })`,
|
|
7
|
+
* returning their payload on `res.structuredContent`. These types are the
|
|
8
|
+
* shared contract the shell (WP-09) produces and the agent-ops pkg
|
|
9
|
+
* (WP-08 reads, WP-12 writes) consumes. Frozen so changing them after both
|
|
10
|
+
* sides exist forces a cross-repo re-sync.
|
|
11
|
+
*
|
|
12
|
+
* Gated by `capabilities.agentOps` (see `AgentOpsCapabilitySchema`).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
/** Typed failure codes a host.agentOps verb returns on the `{ ok: false }`
|
|
16
|
+
* branch, so the pkg UI can render a specific state rather than a raw string.
|
|
17
|
+
* - `daemon_down` — `~/.agent-ops/daemon.lock` missing or daemon not alive
|
|
18
|
+
* - `unauthorized` — the daemon rejected the token (401) — should not happen
|
|
19
|
+
* in normal operation (the shell reads the live lock)
|
|
20
|
+
* - `not_found` — no job with that id (daemon 404 / config miss)
|
|
21
|
+
* - `disabled` — job is disabled, cannot run-now (daemon 409)
|
|
22
|
+
* - `forbidden` — daemon rejected host/origin/method (403/405)
|
|
23
|
+
* - `io_error` — lock/config file unreadable or unwritable
|
|
24
|
+
* - `error` — any other failure */
|
|
25
|
+
export type AgentOpsErrorCode =
|
|
26
|
+
| 'daemon_down'
|
|
27
|
+
| 'unauthorized'
|
|
28
|
+
| 'not_found'
|
|
29
|
+
| 'disabled'
|
|
30
|
+
| 'forbidden'
|
|
31
|
+
| 'io_error'
|
|
32
|
+
| 'error';
|
|
33
|
+
|
|
34
|
+
export interface AgentOpsErrorResult {
|
|
35
|
+
ok: false;
|
|
36
|
+
code: AgentOpsErrorCode;
|
|
37
|
+
/** HTTP status from the daemon when applicable, else null. */
|
|
38
|
+
status: number | null;
|
|
39
|
+
error: string;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** `host.agentOps.runNow({ jobId })` — fire an out-of-schedule run via the
|
|
43
|
+
* daemon's localhost trigger endpoint. The shell reads the 0600
|
|
44
|
+
* `~/.agent-ops/daemon.lock` for `{ port, secret }` and POSTs
|
|
45
|
+
* `127.0.0.1:<port>/jobs/<jobId>/trigger` with BOTH required headers
|
|
46
|
+
* (`x-agent-ops-token: <secret>` + `x-agent-ops-trigger: 1`). */
|
|
47
|
+
export interface AgentOpsRunNowArgs {
|
|
48
|
+
jobId: string;
|
|
49
|
+
}
|
|
50
|
+
export type AgentOpsRunNowResult =
|
|
51
|
+
| { ok: true; status: number; message: string }
|
|
52
|
+
| AgentOpsErrorResult;
|
|
53
|
+
|
|
54
|
+
/** `host.agentOps.setEnabled({ jobId, enabled })` — flip a job's `enabled`
|
|
55
|
+
* flag in the project-scoped config (`~/.atelier/skill-agent-ops/jobs.json`).
|
|
56
|
+
* The daemon honors it on next config load. Does NOT touch the executor. */
|
|
57
|
+
export interface AgentOpsSetEnabledArgs {
|
|
58
|
+
jobId: string;
|
|
59
|
+
enabled: boolean;
|
|
60
|
+
}
|
|
61
|
+
export type AgentOpsSetEnabledResult =
|
|
62
|
+
| { ok: true; jobId: string; enabled: boolean }
|
|
63
|
+
| AgentOpsErrorResult;
|
|
64
|
+
|
|
65
|
+
/** The editable job fields the CRUD form sends to `host.agentOps.upsertJob`.
|
|
66
|
+
* The host fills JobDefinition defaults (schedule_dialect, timeout_ms, retries,
|
|
67
|
+
* backoff, concurrency_policy) for any omitted field; the daemon's Zod loader
|
|
68
|
+
* is the final validation backstop on next config load. */
|
|
69
|
+
export interface AgentOpsJobInput {
|
|
70
|
+
id: string;
|
|
71
|
+
label: string;
|
|
72
|
+
schedule: string;
|
|
73
|
+
timezone?: string;
|
|
74
|
+
enabled?: boolean;
|
|
75
|
+
mode?: 'agent' | 'script';
|
|
76
|
+
command: string;
|
|
77
|
+
model?: string | null;
|
|
78
|
+
agent?: string | null;
|
|
79
|
+
schedule_dialect?: '5f' | '6f';
|
|
80
|
+
timeout_ms?: number;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** `host.agentOps.upsertJob({ job })` — create-or-update a job in the
|
|
84
|
+
* project-scoped config (atomic rewrite; replace by id if present, else
|
|
85
|
+
* append). The daemon honors it on next config load. Validating-only write —
|
|
86
|
+
* the shell does NOT run the job, never becomes the executor. */
|
|
87
|
+
export interface AgentOpsUpsertJobArgs {
|
|
88
|
+
job: AgentOpsJobInput;
|
|
89
|
+
}
|
|
90
|
+
export type AgentOpsUpsertJobResult =
|
|
91
|
+
| { ok: true; jobId: string; created: boolean }
|
|
92
|
+
| AgentOpsErrorResult;
|
|
93
|
+
|
|
94
|
+
/** `host.agentOps.deleteJob({ jobId })` — remove a job from the project-scoped
|
|
95
|
+
* config (atomic rewrite). The daemon stops scheduling it on next load. */
|
|
96
|
+
export interface AgentOpsDeleteJobArgs {
|
|
97
|
+
jobId: string;
|
|
98
|
+
}
|
|
99
|
+
export type AgentOpsDeleteJobResult =
|
|
100
|
+
| { ok: true; jobId: string }
|
|
101
|
+
| AgentOpsErrorResult;
|
|
102
|
+
|
|
103
|
+
/** `host.agentOps.tailRun({ jobId, offset? })` — read the live (or last-completed)
|
|
104
|
+
* run output for a job by byte-range, for the pkg's Live-output view.
|
|
105
|
+
*
|
|
106
|
+
* Unlike the other agent-ops verbs (which reach the daemon's localhost endpoint
|
|
107
|
+
* or rewrite its config), tailRun is a pure **filesystem read on the shell's own
|
|
108
|
+
* event loop**: it opens the per-job marker (`~/.agent-ops/runs/<slug>.marker.json`)
|
|
109
|
+
* to learn the current run's `tailPath` + `status` + `pid`, then reads that tail
|
|
110
|
+
* file from `offset` forward (capped per call). Because it never touches the
|
|
111
|
+
* daemon, the daemon being blocked mid-run (synchronously executing a job) is
|
|
112
|
+
* irrelevant — the shell still streams whatever the child has teed to disk so far.
|
|
113
|
+
*
|
|
114
|
+
* Mechanism is **script-mode only**: the daemon tees a script job's combined
|
|
115
|
+
* stdout/stderr to the tail file as it runs. Agent jobs (`claude -p`) stay
|
|
116
|
+
* byte-for-byte unchanged and have no tail file, so tailRun returns an empty
|
|
117
|
+
* chunk with `mode:'agent'` (the pkg renders 'live output not available for
|
|
118
|
+
* agent jobs' + a spinner driven off the marker's `status`).
|
|
119
|
+
*
|
|
120
|
+
* Polling loop: call with `offset:0` first, then feed the returned `nextOffset`
|
|
121
|
+
* back on each poll. `eof` true means the reader caught up to the current end of
|
|
122
|
+
* file; keep polling while `running` is true. After a run completes the marker
|
|
123
|
+
* still points at the final tail, so `running:false` STILL returns the final
|
|
124
|
+
* chunk for scrollback — the view shows the completed output, not an empty pane.
|
|
125
|
+
*
|
|
126
|
+
* - `running` — `status === 'running'` AND the marker's `pid` is alive.
|
|
127
|
+
* - `status` — the marker's `status` (`'running' | 'done'`), or `null` when
|
|
128
|
+
* no marker exists yet (job has never produced a run).
|
|
129
|
+
* - `startedAtMs` — the run's start epoch-ms from the marker, else `null`.
|
|
130
|
+
* - `mode` — `'script'` (has a tail) / `'agent'` (no tail) / `null` (no marker).
|
|
131
|
+
* - `chunk` — lossy-utf8 decode of the bytes read this call (may be empty).
|
|
132
|
+
* - `nextOffset` — `offset + bytesRead`; pass back on the next poll.
|
|
133
|
+
* - `eof` — reached the current end of the tail file.
|
|
134
|
+
*
|
|
135
|
+
* Best-effort + non-throwing on the shell side: a missing marker / missing tail /
|
|
136
|
+
* unreadable file resolves to `{ ok:true, chunk:'', eof:true, ... }` rather than
|
|
137
|
+
* an error, so the view degrades to 'no output yet'. Only a path-escape attempt
|
|
138
|
+
* (marker.tailPath pointing outside `~/.agent-ops/runs/`) maps to an
|
|
139
|
+
* `{ ok:false, code:'io_error' }`. */
|
|
140
|
+
export interface AgentOpsTailRunArgs {
|
|
141
|
+
jobId: string;
|
|
142
|
+
/** Byte offset into the tail file to read from. Defaults to 0. */
|
|
143
|
+
offset?: number;
|
|
144
|
+
}
|
|
145
|
+
export type AgentOpsTailRunResult =
|
|
146
|
+
| {
|
|
147
|
+
ok: true;
|
|
148
|
+
running: boolean;
|
|
149
|
+
status: 'running' | 'done' | null;
|
|
150
|
+
startedAtMs: number | null;
|
|
151
|
+
mode: 'agent' | 'script' | null;
|
|
152
|
+
chunk: string;
|
|
153
|
+
nextOffset: number;
|
|
154
|
+
eof: boolean;
|
|
155
|
+
}
|
|
156
|
+
| AgentOpsErrorResult;
|
|
157
|
+
|
|
158
|
+
/** A merged config+state row as returned by `host.agentOps.listJobs`. Config
|
|
159
|
+
* fields come from `~/.atelier/skill-agent-ops/jobs.json` (a JobDefinition);
|
|
160
|
+
* `state` is that job's entry in `.company/cron/jobs-state.json` (or null if
|
|
161
|
+
* it has never run). Field casing matches the on-disk files (camelCase state);
|
|
162
|
+
* the pkg's data layer (WP-08) maps this into the snake_case G-VIEW. */
|
|
163
|
+
export interface AgentOpsRawJobState {
|
|
164
|
+
nextRunAtMs: number | null;
|
|
165
|
+
lastRunAtMs: number | null;
|
|
166
|
+
lastStatus: string | null;
|
|
167
|
+
consecutiveErrors: number;
|
|
168
|
+
lastDurationMs: number | null;
|
|
169
|
+
totalCostUsd: number | null;
|
|
170
|
+
totalRuns: number | null;
|
|
171
|
+
lastUsage: {
|
|
172
|
+
costUsd: number | null;
|
|
173
|
+
numTurns: number | null;
|
|
174
|
+
inputTokens: number | null;
|
|
175
|
+
outputTokens: number | null;
|
|
176
|
+
cacheReadTokens: number | null;
|
|
177
|
+
sessionId: string | null;
|
|
178
|
+
} | null;
|
|
179
|
+
}
|
|
180
|
+
export interface AgentOpsRawJob {
|
|
181
|
+
id: string;
|
|
182
|
+
label: string;
|
|
183
|
+
schedule: string;
|
|
184
|
+
schedule_dialect: '5f' | '6f';
|
|
185
|
+
timezone: string;
|
|
186
|
+
enabled: boolean;
|
|
187
|
+
command: string;
|
|
188
|
+
mode: 'agent' | 'script';
|
|
189
|
+
model: string | null;
|
|
190
|
+
agent: string | null;
|
|
191
|
+
_disabledReason: string | null;
|
|
192
|
+
state: AgentOpsRawJobState | null;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** `host.agentOps.listJobs({})` — read the project-scoped job config + the
|
|
196
|
+
* daemon's runtime state file and return both, merged per job, plus daemon
|
|
197
|
+
* liveness. Run history (cron_job_runs / agent_runs) is NOT included here —
|
|
198
|
+
* the pkg reads that directly via `host.dbQuery`. */
|
|
199
|
+
export type AgentOpsListJobsArgs = Record<string, never>;
|
|
200
|
+
export type AgentOpsListJobsResult =
|
|
201
|
+
| {
|
|
202
|
+
ok: true;
|
|
203
|
+
daemon_up: boolean;
|
|
204
|
+
daemon_pid: number | null;
|
|
205
|
+
jobs: AgentOpsRawJob[];
|
|
206
|
+
}
|
|
207
|
+
| AgentOpsErrorResult;
|
package/src/index.ts
CHANGED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { test } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
ManifestSchema,
|
|
6
|
+
RequiresEntrySchema,
|
|
7
|
+
RequireSourceSchema,
|
|
8
|
+
} from './manifest.js';
|
|
9
|
+
|
|
10
|
+
// ─── WP-11 — `requires` field (ADR-015 §3) ──────────────────────────────────
|
|
11
|
+
|
|
12
|
+
const BASE = {
|
|
13
|
+
id: 'com.ikenga.studio',
|
|
14
|
+
name: 'Studio',
|
|
15
|
+
version: '0.1.0',
|
|
16
|
+
ikenga_api: '1',
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
test('RequiresEntry: full shape parses', () => {
|
|
20
|
+
const e = RequiresEntrySchema.parse({
|
|
21
|
+
kind: 'skill',
|
|
22
|
+
name: '@ikenga/studio-beat-detect',
|
|
23
|
+
source: 'npx',
|
|
24
|
+
ref: 'v1.2.0',
|
|
25
|
+
});
|
|
26
|
+
assert.equal(e.kind, 'skill');
|
|
27
|
+
assert.equal(e.source, 'npx');
|
|
28
|
+
assert.equal(e.ref, 'v1.2.0');
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
test('RequiresEntry: source + ref optional', () => {
|
|
32
|
+
const e = RequiresEntrySchema.parse({ kind: 'skill', name: 'skill-core' });
|
|
33
|
+
assert.equal(e.source, undefined);
|
|
34
|
+
assert.equal(e.ref, undefined);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
test('RequiresEntry: rejects unknown field (.strict mirrors Rust deny_unknown_fields)', () => {
|
|
38
|
+
assert.throws(() =>
|
|
39
|
+
RequiresEntrySchema.parse({ kind: 'skill', name: 'skill-core', bogus: true }),
|
|
40
|
+
);
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('RequiresEntry: kind bundle is accepted (WP-18 G-BUNDLE)', () => {
|
|
44
|
+
// WP-18 locked design decision 4: `RequiresEntrySchema.kind` is `z.string()`
|
|
45
|
+
// (a free string, not a closed enum), so a `requires` entry may reference a
|
|
46
|
+
// bundle — `{kind:"bundle", name}` parses and carries the kind through. This
|
|
47
|
+
// is the contract-side half of the G-BUNDLE "requires kind:bundle parses" DoD.
|
|
48
|
+
const e = RequiresEntrySchema.parse({ kind: 'bundle', name: 'studio-archetypes' });
|
|
49
|
+
assert.equal(e.kind, 'bundle');
|
|
50
|
+
assert.equal(e.name, 'studio-archetypes');
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
test('RequiresEntry: rejects an out-of-set source', () => {
|
|
54
|
+
assert.throws(() =>
|
|
55
|
+
RequiresEntrySchema.parse({ kind: 'skill', name: 'x', source: 'ftp' }),
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test('RequireSource: only git|npx|catalog|local', () => {
|
|
60
|
+
for (const s of ['git', 'npx', 'catalog', 'local']) {
|
|
61
|
+
assert.equal(RequireSourceSchema.parse(s), s);
|
|
62
|
+
}
|
|
63
|
+
assert.throws(() => RequireSourceSchema.parse('http'));
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
test('Manifest: requires parses and round-trips', () => {
|
|
67
|
+
const m = ManifestSchema.parse({
|
|
68
|
+
...BASE,
|
|
69
|
+
requires: [
|
|
70
|
+
{ kind: 'skill', name: '@ikenga/studio-archetypes', source: 'npx' },
|
|
71
|
+
{ kind: 'skill', name: 'skill-core', source: 'git', ref: 'v1.0.0' },
|
|
72
|
+
{ kind: 'skill', name: '@ikenga/studio-doctor' },
|
|
73
|
+
],
|
|
74
|
+
});
|
|
75
|
+
assert.equal(m.requires.length, 3);
|
|
76
|
+
assert.equal(m.requires[0].name, '@ikenga/studio-archetypes');
|
|
77
|
+
assert.equal(m.requires[2].source, undefined);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
test('Manifest: requires defaults to [] when absent (pre-Phase-4 manifest)', () => {
|
|
81
|
+
const m = ManifestSchema.parse({ ...BASE });
|
|
82
|
+
assert.deepEqual(m.requires, []);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('Manifest: retired bundling fields are no longer part of the type (WP-17)', () => {
|
|
86
|
+
// ADR-015 decision 4: `skills`/`commands`/`agents` were hard-retired from the
|
|
87
|
+
// schema (lockstep with the Rust `deny_unknown_fields` Manifest, which REJECTS
|
|
88
|
+
// them — the authoritative loader). ManifestSchema is non-strict, so a stray
|
|
89
|
+
// legacy key is stripped rather than rejected here; assert it does not survive
|
|
90
|
+
// onto the parsed object.
|
|
91
|
+
const m = ManifestSchema.parse({ ...BASE, skills: 'skills', commands: 'commands' }) as Record<
|
|
92
|
+
string,
|
|
93
|
+
unknown
|
|
94
|
+
>;
|
|
95
|
+
assert.equal(m.skills, undefined);
|
|
96
|
+
assert.equal(m.commands, undefined);
|
|
97
|
+
});
|