unbrowse 3.1.0-experiments.995f8bb → 3.1.0-experiments.9cbcb13

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 (105) hide show
  1. package/dist/cli.js +79 -33
  2. package/dist/index.js +2 -6
  3. package/dist/mcp.js +73 -22
  4. package/dist/server.js +25994 -0
  5. package/package.json +1 -2
  6. package/vendor/kuri/manifest.json +1 -1
  7. package/runtime-src/agent-outcome.ts +0 -166
  8. package/runtime-src/analytics-session.ts +0 -55
  9. package/runtime-src/api/browse-index.ts +0 -343
  10. package/runtime-src/api/browse-session.ts +0 -572
  11. package/runtime-src/api/browse-submit-prereqs.ts +0 -48
  12. package/runtime-src/api/browse-submit.ts +0 -1184
  13. package/runtime-src/api/routes.ts +0 -1833
  14. package/runtime-src/auth/browser-cookies.ts +0 -423
  15. package/runtime-src/auth/index.ts +0 -535
  16. package/runtime-src/auth/runtime.ts +0 -116
  17. package/runtime-src/browser/index.ts +0 -659
  18. package/runtime-src/browser/types.ts +0 -41
  19. package/runtime-src/build-info.generated.ts +0 -6
  20. package/runtime-src/capture/index.ts +0 -1929
  21. package/runtime-src/capture/prefetch.ts +0 -95
  22. package/runtime-src/capture/rsc.ts +0 -45
  23. package/runtime-src/cli/shortcuts.ts +0 -273
  24. package/runtime-src/cli.ts +0 -1734
  25. package/runtime-src/client/graph-client.ts +0 -100
  26. package/runtime-src/client/index.ts +0 -1425
  27. package/runtime-src/debug-trace.ts +0 -18
  28. package/runtime-src/domain.ts +0 -38
  29. package/runtime-src/execution/index.ts +0 -3407
  30. package/runtime-src/execution/retry.ts +0 -46
  31. package/runtime-src/execution/robots.ts +0 -167
  32. package/runtime-src/execution/search-forms.ts +0 -188
  33. package/runtime-src/execution/token-resolver.ts +0 -135
  34. package/runtime-src/extraction/index.ts +0 -1507
  35. package/runtime-src/foundry/publish-bundle.ts +0 -392
  36. package/runtime-src/graph/agent-augment.ts +0 -315
  37. package/runtime-src/graph/index.ts +0 -1532
  38. package/runtime-src/graph/local-fixtures.ts +0 -393
  39. package/runtime-src/graph/local-harness.ts +0 -646
  40. package/runtime-src/graph/planner.ts +0 -411
  41. package/runtime-src/graph/session.ts +0 -294
  42. package/runtime-src/graph/trace-store.ts +0 -136
  43. package/runtime-src/impact-log.ts +0 -227
  44. package/runtime-src/index.ts +0 -24
  45. package/runtime-src/indexer/index.ts +0 -465
  46. package/runtime-src/intent-match.ts +0 -1515
  47. package/runtime-src/kuri/client.ts +0 -1839
  48. package/runtime-src/logger.ts +0 -30
  49. package/runtime-src/marketplace/index.ts +0 -111
  50. package/runtime-src/mcp.ts +0 -1911
  51. package/runtime-src/orchestrator/browser-agent.ts +0 -374
  52. package/runtime-src/orchestrator/dag-advisor.ts +0 -59
  53. package/runtime-src/orchestrator/dag-feedback.ts +0 -257
  54. package/runtime-src/orchestrator/first-pass-action.ts +0 -403
  55. package/runtime-src/orchestrator/index.ts +0 -4480
  56. package/runtime-src/orchestrator/passive-publish.ts +0 -187
  57. package/runtime-src/orchestrator/timing-economics.ts +0 -80
  58. package/runtime-src/payments/cascade.ts +0 -137
  59. package/runtime-src/payments/index.ts +0 -270
  60. package/runtime-src/payments/lobster-pay.ts +0 -182
  61. package/runtime-src/payments/wallet.ts +0 -98
  62. package/runtime-src/publish/review-context.ts +0 -93
  63. package/runtime-src/publish/sanitize.ts +0 -197
  64. package/runtime-src/publish/schema-review.ts +0 -192
  65. package/runtime-src/publish-admission.ts +0 -388
  66. package/runtime-src/ratelimit/index.ts +0 -23
  67. package/runtime-src/reverse-engineer/bundle-scanner.ts +0 -127
  68. package/runtime-src/reverse-engineer/description-prompt.ts +0 -213
  69. package/runtime-src/reverse-engineer/index.ts +0 -1551
  70. package/runtime-src/reverse-engineer/token-sources.ts +0 -379
  71. package/runtime-src/router.ts +0 -17
  72. package/runtime-src/routing-telemetry.ts +0 -395
  73. package/runtime-src/runtime/browser-access.ts +0 -11
  74. package/runtime-src/runtime/browser-auth.ts +0 -12
  75. package/runtime-src/runtime/browser-host.ts +0 -48
  76. package/runtime-src/runtime/lifecycle.ts +0 -17
  77. package/runtime-src/runtime/local-server.ts +0 -311
  78. package/runtime-src/runtime/paths.ts +0 -99
  79. package/runtime-src/runtime/setup.ts +0 -251
  80. package/runtime-src/runtime/supervisor.ts +0 -69
  81. package/runtime-src/runtime/update-hints.ts +0 -351
  82. package/runtime-src/server.ts +0 -100
  83. package/runtime-src/session-logs.ts +0 -142
  84. package/runtime-src/settings.ts +0 -221
  85. package/runtime-src/single-binary.ts +0 -143
  86. package/runtime-src/site-policy.ts +0 -54
  87. package/runtime-src/stale-cleanup-runner.ts +0 -144
  88. package/runtime-src/stale-cleanup.ts +0 -133
  89. package/runtime-src/telemetry-attribution.ts +0 -120
  90. package/runtime-src/telemetry.ts +0 -253
  91. package/runtime-src/template-params.ts +0 -141
  92. package/runtime-src/transform/drift.ts +0 -60
  93. package/runtime-src/transform/index.ts +0 -277
  94. package/runtime-src/types/index.ts +0 -1
  95. package/runtime-src/types/skill.ts +0 -931
  96. package/runtime-src/vault/index.ts +0 -196
  97. package/runtime-src/verification/auth-gate.ts +0 -8
  98. package/runtime-src/verification/candidates.ts +0 -27
  99. package/runtime-src/verification/index.ts +0 -120
  100. package/runtime-src/verification/matrix.ts +0 -30
  101. package/runtime-src/version.ts +0 -148
  102. package/runtime-src/workflow/artifact.ts +0 -161
  103. package/runtime-src/workflow/compile.ts +0 -808
  104. package/runtime-src/workflow/publish.ts +0 -225
  105. package/runtime-src/workflow/runtime.ts +0 -213
@@ -1,411 +0,0 @@
1
- /**
2
- * DAG execution planner — advisory only.
3
- *
4
- * Provides topological ordering, prerequisite analysis, and session-level
5
- * feedback tracking for the operation graph. Nothing here auto-executes;
6
- * callers use the plan to boost/penalize ranked endpoints and to suggest
7
- * next steps to the agent.
8
- */
9
-
10
- import type {
11
- SkillManifest,
12
- SkillOperationGraph,
13
- SkillOperationNode,
14
- SkillOperationEdge,
15
- } from "../types/index.js";
16
- import { ensureSkillOperationGraph } from "./index.js";
17
- import { deriveAuthDependencies } from "../auth/runtime.js";
18
-
19
- // ---------------------------------------------------------------------------
20
- // Types
21
- // ---------------------------------------------------------------------------
22
-
23
- export interface ExecutionStep {
24
- operation_id: string;
25
- endpoint_id: string;
26
- method: string;
27
- produces_bindings: string[];
28
- requires_confirmation: boolean;
29
- }
30
-
31
- export interface ExecutionPlan {
32
- steps: ExecutionStep[];
33
- /** True when all prerequisite bindings for the target are already known. */
34
- chain_ready: boolean;
35
- /** Operations that were unreachable and therefore excluded. */
36
- unreachable: string[];
37
- }
38
-
39
- export interface DagAdvisoryPlan {
40
- /** All prerequisite bindings are satisfied by known bindings. */
41
- chain_ready: boolean;
42
- /** Operations that become runnable after the target completes. */
43
- predicted_next: string[];
44
- /** Ordered prerequisite operations leading to the target. */
45
- prerequisite_order: string[];
46
- /** Operations in the graph that are not needed for the target. */
47
- skippable: string[];
48
- /** Auth dependencies detected from endpoint semantics. */
49
- auth_dependencies: import("../auth/runtime.js").AuthDependency[];
50
- }
51
-
52
- // ---------------------------------------------------------------------------
53
- // Mutable-method detection
54
- // ---------------------------------------------------------------------------
55
-
56
- const MUTABLE_METHODS = new Set(["POST", "PUT", "DELETE", "PATCH"]);
57
-
58
- function isMutable(method: string): boolean {
59
- return MUTABLE_METHODS.has(method.toUpperCase());
60
- }
61
-
62
- // ---------------------------------------------------------------------------
63
- // 1. buildExecutionPlan
64
- // ---------------------------------------------------------------------------
65
-
66
- /**
67
- * Topological sort from entry points to `targetOperationId`, producing
68
- * ordered steps. Each step carries the bindings it will produce that
69
- * downstream steps need. Unreachable nodes are excluded. Mutable
70
- * operations (POST/PUT/DELETE/PATCH) are flagged `requires_confirmation`.
71
- */
72
- export function buildExecutionPlan(
73
- graph: SkillOperationGraph,
74
- targetOperationId: string,
75
- knownBindings: Set<string>,
76
- ): ExecutionPlan {
77
- const opMap = new Map<string, SkillOperationNode>();
78
- for (const op of graph.operations) {
79
- opMap.set(op.operation_id, op);
80
- }
81
-
82
- // Build adjacency lists from the edge list.
83
- // predecessors: operation_id -> set of operation_ids that must run first
84
- const predecessors = new Map<string, Set<string>>();
85
- for (const op of graph.operations) {
86
- predecessors.set(op.operation_id, new Set());
87
- }
88
- for (const edge of graph.edges) {
89
- if (edge.kind !== "dependency" && edge.kind !== "parent_child" && edge.kind !== "auth") continue;
90
- const deps = predecessors.get(edge.to_operation_id);
91
- if (deps) deps.add(edge.from_operation_id);
92
- }
93
-
94
- // Walk backwards from target to find all ancestors (required ops).
95
- const needed = new Set<string>();
96
- const queue: string[] = [targetOperationId];
97
- while (queue.length > 0) {
98
- const current = queue.pop()!;
99
- if (needed.has(current)) continue;
100
- needed.add(current);
101
- const deps = predecessors.get(current);
102
- if (deps) {
103
- for (const dep of deps) {
104
- queue.push(dep);
105
- }
106
- }
107
- }
108
-
109
- // Filter to only check if we can skip a needed node because its
110
- // required bindings are already known.
111
- const satisfied = new Set<string>(knownBindings);
112
-
113
- // Kahn's algorithm over the needed subgraph.
114
- const inDegree = new Map<string, number>();
115
- for (const id of needed) {
116
- inDegree.set(id, 0);
117
- }
118
- for (const edge of graph.edges) {
119
- if (edge.kind !== "dependency" && edge.kind !== "parent_child" && edge.kind !== "auth") continue;
120
- if (!needed.has(edge.from_operation_id) || !needed.has(edge.to_operation_id)) continue;
121
- inDegree.set(edge.to_operation_id, (inDegree.get(edge.to_operation_id) ?? 0) + 1);
122
- }
123
-
124
- const ready: string[] = [];
125
- for (const [id, deg] of inDegree) {
126
- if (deg === 0) ready.push(id);
127
- }
128
-
129
- const steps: ExecutionStep[] = [];
130
- const visited = new Set<string>();
131
-
132
- while (ready.length > 0) {
133
- // Sort for determinism: prefer lower operation_id.
134
- ready.sort();
135
- const current = ready.shift()!;
136
- if (visited.has(current)) continue;
137
- visited.add(current);
138
-
139
- const op = opMap.get(current);
140
- if (!op) continue;
141
-
142
- const producesBindings = op.provides.map((b) => b.key);
143
- steps.push({
144
- operation_id: op.operation_id,
145
- endpoint_id: op.endpoint_id,
146
- method: op.method,
147
- produces_bindings: producesBindings,
148
- requires_confirmation: isMutable(op.method),
149
- });
150
-
151
- // Mark produced bindings as satisfied.
152
- for (const key of producesBindings) {
153
- satisfied.add(key);
154
- }
155
-
156
- // Decrement in-degree for successors.
157
- for (const edge of graph.edges) {
158
- if (edge.kind !== "dependency" && edge.kind !== "parent_child" && edge.kind !== "auth") continue;
159
- if (edge.from_operation_id !== current) continue;
160
- if (!needed.has(edge.to_operation_id)) continue;
161
- const deg = (inDegree.get(edge.to_operation_id) ?? 1) - 1;
162
- inDegree.set(edge.to_operation_id, deg);
163
- if (deg === 0) ready.push(edge.to_operation_id);
164
- }
165
- }
166
-
167
- // Determine chain_ready: check whether the target's required bindings are
168
- // all either in the original known set or produced by steps before it.
169
- const targetOp = opMap.get(targetOperationId);
170
- const chainReady = targetOp
171
- ? targetOp.requires.every(
172
- (b) => !b.required || satisfied.has(b.key) || knownBindings.has(b.key),
173
- )
174
- : false;
175
-
176
- // Unreachable = operations in the graph that are not in the needed set.
177
- const unreachable = graph.operations
178
- .filter((op) => !needed.has(op.operation_id))
179
- .map((op) => op.operation_id);
180
-
181
- return { steps, chain_ready: chainReady, unreachable };
182
- }
183
-
184
- // ---------------------------------------------------------------------------
185
- // 2. fetchDagAdvisoryPlan
186
- // ---------------------------------------------------------------------------
187
-
188
- /**
189
- * Uses the local graph to build an advisory execution plan.
190
- *
191
- * @param skill The skill manifest (used to build/get the graph).
192
- * @param targetEndpointId The endpoint we want to execute.
193
- * @param knownBindingKeys Binding keys already available from context/params.
194
- */
195
- export function fetchDagAdvisoryPlan(
196
- skill: SkillManifest,
197
- targetEndpointId: string,
198
- knownBindingKeys: string[],
199
- ): DagAdvisoryPlan {
200
- const graph = ensureSkillOperationGraph(skill);
201
- const knownSet = new Set(knownBindingKeys);
202
-
203
- // Derive auth dependencies from the target endpoint's semantics.
204
- const authDeps = deriveAuthDependencies(skill, targetEndpointId);
205
-
206
- // Find the target operation by endpoint_id.
207
- const targetOp = graph.operations.find(
208
- (op) => op.endpoint_id === targetEndpointId,
209
- );
210
- if (!targetOp) {
211
- return {
212
- chain_ready: true,
213
- predicted_next: [],
214
- prerequisite_order: [],
215
- skippable: [],
216
- auth_dependencies: authDeps,
217
- };
218
- }
219
-
220
- const plan = buildExecutionPlan(graph, targetOp.operation_id, knownSet);
221
-
222
- // prerequisite_order: steps before the target (excluding target itself).
223
- const prerequisiteOrder = plan.steps
224
- .filter((s) => s.operation_id !== targetOp.operation_id)
225
- .map((s) => s.endpoint_id);
226
-
227
- // predicted_next: operations that list the target's provides as a
228
- // required binding and are not in the plan themselves.
229
- const planOpIds = new Set(plan.steps.map((s) => s.operation_id));
230
- const targetProvides = new Set(targetOp.provides.map((b) => b.key));
231
- const predictedNext: string[] = [];
232
- for (const op of graph.operations) {
233
- if (planOpIds.has(op.operation_id)) continue;
234
- const needsTargetOutput = op.requires.some(
235
- (b) => b.required && targetProvides.has(b.key),
236
- );
237
- if (needsTargetOutput) predictedNext.push(op.endpoint_id);
238
- }
239
-
240
- // skippable: unreachable operations (not needed for this target).
241
- const skippable = plan.unreachable.map((opId) => {
242
- const op = graph.operations.find((o) => o.operation_id === opId);
243
- return op?.endpoint_id ?? opId;
244
- });
245
-
246
- return {
247
- chain_ready: plan.chain_ready,
248
- predicted_next: predictedNext,
249
- prerequisite_order: prerequisiteOrder,
250
- skippable,
251
- auth_dependencies: authDeps,
252
- };
253
- }
254
-
255
- // ---------------------------------------------------------------------------
256
- // 3. applyDagAdvisoryBoosts
257
- // ---------------------------------------------------------------------------
258
-
259
- /**
260
- * Boost/penalize ranked endpoints based on the DAG advisory plan.
261
- *
262
- * - Endpoints on the critical path (`prerequisite_order`) get a boost.
263
- * - Endpoints in `skippable` get a penalty.
264
- * - Endpoints in `predicted_next` get a small boost (good follow-ups).
265
- *
266
- * Returns a new array, sorted by adjusted score descending.
267
- */
268
- export function applyDagAdvisoryBoosts<
269
- T extends { endpoint_id: string; score: number },
270
- >(
271
- ranked: T[],
272
- dagPlan: DagAdvisoryPlan,
273
- ): T[] {
274
- const prerequisiteSet = new Set(dagPlan.prerequisite_order);
275
- const skippableSet = new Set(dagPlan.skippable);
276
- const predictedNextSet = new Set(dagPlan.predicted_next);
277
-
278
- // Check session negatives: don't boost endpoints that have failed.
279
- const negativeEndpoints = new Set<string>();
280
- for (const [, negatives] of sessionNegatives) {
281
- for (const epId of negatives) {
282
- negativeEndpoints.add(epId);
283
- }
284
- }
285
-
286
- const boosted = ranked.map((item) => {
287
- let adjustment = 0;
288
-
289
- // Don't boost endpoints that have failed in this session.
290
- if (negativeEndpoints.has(item.endpoint_id)) {
291
- return { ...item, score: item.score - 0.1 };
292
- }
293
-
294
- if (prerequisiteSet.has(item.endpoint_id)) {
295
- adjustment += 0.15; // Critical path boost
296
- }
297
- if (predictedNextSet.has(item.endpoint_id)) {
298
- adjustment += 0.05; // Follow-up boost
299
- }
300
- if (skippableSet.has(item.endpoint_id)) {
301
- adjustment -= 0.1; // Not needed penalty
302
- }
303
-
304
- return { ...item, score: item.score + adjustment };
305
- });
306
-
307
- return boosted.sort((a, b) => b.score - a.score);
308
- }
309
-
310
- // ---------------------------------------------------------------------------
311
- // 4. recordDagNegative
312
- // ---------------------------------------------------------------------------
313
-
314
- /** Session-level store of failed endpoints, keyed by skill_id. */
315
- const sessionNegatives = new Map<string, Set<string>>();
316
-
317
- /**
318
- * Record that this endpoint failed for this session. Prevents the planner
319
- * from re-boosting it in `applyDagAdvisoryBoosts`.
320
- */
321
- export function recordDagNegative(
322
- skill: { skill_id: string },
323
- endpointId: string,
324
- ): void {
325
- const existing = sessionNegatives.get(skill.skill_id);
326
- if (existing) {
327
- existing.add(endpointId);
328
- } else {
329
- sessionNegatives.set(skill.skill_id, new Set([endpointId]));
330
- }
331
- }
332
-
333
- /**
334
- * Check whether an endpoint has been recorded as failed for a skill.
335
- * Exposed for testing.
336
- */
337
- export function isDagNegative(
338
- skill: { skill_id: string },
339
- endpointId: string,
340
- ): boolean {
341
- return sessionNegatives.get(skill.skill_id)?.has(endpointId) ?? false;
342
- }
343
-
344
- /**
345
- * Clear all session negatives. Exposed for testing.
346
- */
347
- export function clearDagSessionState(): void {
348
- sessionNegatives.clear();
349
- sessionTraces.clear();
350
- }
351
-
352
- // ---------------------------------------------------------------------------
353
- // 5. recordDagSessionAction
354
- // ---------------------------------------------------------------------------
355
-
356
- /** Session-level store of execution traces, keyed by skill_id. */
357
- const sessionTraces = new Map<
358
- string,
359
- Array<{ endpoint_id: string; success: boolean; timestamp: number }>
360
- >();
361
-
362
- /**
363
- * Record that this endpoint was executed in this session. Accumulates an
364
- * ordered trace of operations per skill.
365
- */
366
- export function recordDagSessionAction(
367
- skill: { skill_id: string },
368
- endpointId: string,
369
- success: boolean,
370
- ): void {
371
- const existing = sessionTraces.get(skill.skill_id);
372
- const entry = { endpoint_id: endpointId, success, timestamp: Date.now() };
373
- if (existing) {
374
- existing.push(entry);
375
- } else {
376
- sessionTraces.set(skill.skill_id, [entry]);
377
- }
378
- }
379
-
380
- /**
381
- * Retrieve the session trace for a skill. Exposed for testing.
382
- */
383
- export function getDagSessionTrace(
384
- skill: { skill_id: string },
385
- ): Array<{ endpoint_id: string; success: boolean; timestamp: number }> {
386
- return sessionTraces.get(skill.skill_id) ?? [];
387
- }
388
-
389
- // ---------------------------------------------------------------------------
390
- // 6. upsertDagEdgesFromOperationGraph
391
- // ---------------------------------------------------------------------------
392
-
393
- /**
394
- * Merge new edges from a freshly-built graph into the skill manifest's
395
- * persisted operation_graph. If the skill has no graph yet, assign the
396
- * provided one directly. Existing edges are preserved (dedup by edge_id),
397
- * and the generated_at timestamp is updated.
398
- */
399
- export function upsertDagEdgesFromOperationGraph(
400
- skill: SkillManifest,
401
- graph: SkillOperationGraph,
402
- ): void {
403
- if (!skill.operation_graph) { skill.operation_graph = graph; return; }
404
- const existingEdgeIds = new Set(skill.operation_graph.edges.map(e => e.edge_id));
405
- for (const edge of graph.edges) {
406
- if (!existingEdgeIds.has(edge.edge_id)) {
407
- skill.operation_graph.edges.push(edge);
408
- }
409
- }
410
- skill.operation_graph.generated_at = graph.generated_at;
411
- }
@@ -1,294 +0,0 @@
1
- import type {
2
- SkillOperationGraph,
3
- SkillOperationNode,
4
- OperationBinding,
5
- } from "../types/index.js";
6
-
7
- /** Result of matching a captured request against the operation graph. */
8
- export interface IndexedOperation {
9
- operation_id: string;
10
- method: string;
11
- url_template: string;
12
- /** Binding values extracted from the matched response body. */
13
- extracted_bindings: Record<string, unknown>;
14
- }
15
-
16
- /** A single entry in the session execution trace. */
17
- export interface TraceEntry {
18
- operationId: string;
19
- success: boolean;
20
- timestamp: string;
21
- }
22
-
23
- /**
24
- * Build a regex from a url_template by replacing `{param}` with a capture-all segment pattern.
25
- * E.g. "https://api.example.com/users/{user_id}/posts" -> /^https:\/\/api\.example\.com\/users\/[^\/]+\/posts$/
26
- */
27
- function templateToRegex(urlTemplate: string): RegExp {
28
- // Escape regex-special chars, then replace escaped \{...\} placeholders with [^/]+
29
- const escaped = urlTemplate.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
30
- const pattern = escaped.replace(/\\\{[^}]+\\\}/g, "[^/]+");
31
- // Allow optional trailing query string
32
- return new RegExp(`^${pattern}(\\?.*)?$`);
33
- }
34
-
35
- /**
36
- * Check whether an operation is runnable given a set of known bindings.
37
- * Re-implemented locally to avoid depending on a non-exported function in graph/index.ts.
38
- */
39
- function isRunnable(
40
- operation: SkillOperationNode,
41
- bindings: Record<string, unknown>,
42
- ): boolean {
43
- return operation.requires.every((binding) => {
44
- if (!binding.required) return true;
45
- const value = bindings[binding.key];
46
- return value != null && value !== "";
47
- });
48
- }
49
-
50
- /**
51
- * Try to extract binding values from a JSON response body for a given set of `provides` bindings.
52
- * Walks top-level and one-level-nested keys looking for matches against binding keys.
53
- */
54
- function extractBindingsFromJson(
55
- responseBody: string | undefined,
56
- provides: OperationBinding[],
57
- ): Record<string, unknown> {
58
- if (!responseBody) return {};
59
- const extracted: Record<string, unknown> = {};
60
- let parsed: unknown;
61
- try {
62
- parsed = JSON.parse(responseBody);
63
- } catch {
64
- return extracted;
65
- }
66
- if (typeof parsed !== "object" || parsed === null) return extracted;
67
-
68
- const provideKeys = new Set(provides.map((b) => b.key));
69
-
70
- function walkObject(obj: Record<string, unknown>, depth: number): void {
71
- for (const [key, value] of Object.entries(obj)) {
72
- if (provideKeys.has(key) && value != null && value !== "") {
73
- // For arrays/objects, store the value directly; for scalars, store as-is
74
- extracted[key] = value;
75
- }
76
- // Walk one level deep into nested objects (not arrays)
77
- if (
78
- depth === 0 &&
79
- typeof value === "object" &&
80
- value !== null &&
81
- !Array.isArray(value)
82
- ) {
83
- walkObject(value as Record<string, unknown>, depth + 1);
84
- }
85
- // For arrays, check first element
86
- if (depth === 0 && Array.isArray(value) && value.length > 0) {
87
- const first = value[0];
88
- if (typeof first === "object" && first !== null) {
89
- // Check if array items have keys matching provides
90
- for (const itemKey of Object.keys(first as Record<string, unknown>)) {
91
- if (provideKeys.has(itemKey)) {
92
- const itemValue = (first as Record<string, unknown>)[itemKey];
93
- if (itemValue != null && itemValue !== "") {
94
- extracted[itemKey] = itemValue;
95
- }
96
- }
97
- }
98
- }
99
- }
100
- }
101
- }
102
-
103
- walkObject(parsed as Record<string, unknown>, 0);
104
- return extracted;
105
- }
106
-
107
- /**
108
- * Session-scoped graph index that tracks observed requests and binding state.
109
- * Ephemeral — lives for the duration of one action sequence, not persisted.
110
- */
111
- export class GraphSession {
112
- private graph: SkillOperationGraph;
113
- private observedOperations = new Map<
114
- string,
115
- { timestamp: string; extracted: Record<string, unknown> }
116
- >();
117
- private executedOperations = new Map<
118
- string,
119
- { success: boolean; timestamp: string; responseFields?: string[] }
120
- >();
121
- private knownBindings: Record<string, unknown> = {};
122
- private trace: TraceEntry[] = [];
123
- private templateCache: Array<{
124
- operation: SkillOperationNode;
125
- regex: RegExp;
126
- }>;
127
-
128
- constructor(graph: SkillOperationGraph) {
129
- this.graph = graph;
130
- // Pre-compile URL template regexes for fast matching
131
- this.templateCache = graph.operations.map((op) => ({
132
- operation: op,
133
- regex: templateToRegex(op.url_template),
134
- }));
135
- }
136
-
137
- /**
138
- * Index a captured request against the graph — match it to an operation.
139
- * Returns the matched operation info, or null if no match.
140
- */
141
- indexRequest(request: {
142
- url: string;
143
- method: string;
144
- response_body?: string;
145
- }): IndexedOperation | null {
146
- for (const { operation, regex } of this.templateCache) {
147
- if (
148
- operation.method === request.method &&
149
- regex.test(request.url)
150
- ) {
151
- // Extract bindings from the response body
152
- const extracted = extractBindingsFromJson(
153
- request.response_body,
154
- operation.provides,
155
- );
156
-
157
- // Mark as observed
158
- this.observedOperations.set(operation.operation_id, {
159
- timestamp: new Date().toISOString(),
160
- extracted,
161
- });
162
-
163
- // Merge extracted bindings into known state
164
- for (const [key, value] of Object.entries(extracted)) {
165
- if (value != null && value !== "") {
166
- this.knownBindings[key] = value;
167
- }
168
- }
169
-
170
- return {
171
- operation_id: operation.operation_id,
172
- method: operation.method,
173
- url_template: operation.url_template,
174
- extracted_bindings: extracted,
175
- };
176
- }
177
- }
178
- return null;
179
- }
180
-
181
- /** Get bindings that are now known from indexed responses. */
182
- getKnownBindings(): Record<string, unknown> {
183
- return { ...this.knownBindings };
184
- }
185
-
186
- /** Get operations that are now runnable given known bindings. */
187
- getRunnableOperations(): SkillOperationNode[] {
188
- return this.graph.operations.filter((op) =>
189
- isRunnable(op, this.knownBindings),
190
- );
191
- }
192
-
193
- /**
194
- * Get the next suggested operations (unblocked by current state).
195
- * Excludes already-executed and already-observed operations.
196
- * If targetOperationId is provided, includes ops that would become runnable
197
- * after the target completes (considering its provides).
198
- */
199
- getSuggestedNext(targetOperationId?: string): SkillOperationNode[] {
200
- const seen = new Set([
201
- ...this.observedOperations.keys(),
202
- ...this.executedOperations.keys(),
203
- ]);
204
-
205
- // Build effective bindings: current known + target's provides if specified
206
- let effectiveBindings = { ...this.knownBindings };
207
- if (targetOperationId) {
208
- const targetOp = this.graph.operations.find(
209
- (op) => op.operation_id === targetOperationId,
210
- );
211
- if (targetOp) {
212
- for (const binding of targetOp.provides) {
213
- if (effectiveBindings[binding.key] == null) {
214
- // Mark as "will be available" with a placeholder
215
- effectiveBindings[binding.key] = binding.example_value ?? "__pending__";
216
- }
217
- }
218
- }
219
- }
220
-
221
- // Find reachable operations from observed/executed nodes via edges
222
- const reachableFromObserved = new Set<string>();
223
- for (const edge of this.graph.edges) {
224
- if (
225
- this.observedOperations.has(edge.from_operation_id) ||
226
- this.executedOperations.has(edge.from_operation_id) ||
227
- edge.from_operation_id === targetOperationId
228
- ) {
229
- reachableFromObserved.add(edge.to_operation_id);
230
- }
231
- }
232
- // Entry operations are always reachable
233
- for (const entryId of this.graph.entry_operation_ids) {
234
- reachableFromObserved.add(entryId);
235
- }
236
-
237
- return this.graph.operations.filter((op) => {
238
- if (seen.has(op.operation_id)) return false;
239
- if (op.operation_id === targetOperationId) return false;
240
- // Must be reachable from an observed node or be an entry
241
- if (!reachableFromObserved.has(op.operation_id)) return false;
242
- return isRunnable(op, effectiveBindings);
243
- });
244
- }
245
-
246
- /** Record that an operation was explicitly executed. */
247
- recordExecution(
248
- operationId: string,
249
- success: boolean,
250
- responseFields?: string[],
251
- ): void {
252
- const timestamp = new Date().toISOString();
253
- this.executedOperations.set(operationId, {
254
- success,
255
- timestamp,
256
- responseFields,
257
- });
258
- this.trace.push({ operationId, success, timestamp });
259
-
260
- // If successful and responseFields provided, mark those bindings as known
261
- if (success && responseFields) {
262
- const op = this.graph.operations.find(
263
- (o) => o.operation_id === operationId,
264
- );
265
- if (op) {
266
- for (const field of responseFields) {
267
- const binding = op.provides.find((b) => b.key === field);
268
- if (binding) {
269
- this.knownBindings[field] =
270
- binding.example_value ?? `__from_${operationId}__`;
271
- }
272
- }
273
- }
274
- }
275
- }
276
-
277
- /** Get session trace (ordered list of executed operations). */
278
- getTrace(): TraceEntry[] {
279
- return [...this.trace];
280
- }
281
-
282
- /** Serialize session state for inclusion in CaptureResult. */
283
- toSnapshot(): {
284
- observed_operations: string[];
285
- known_bindings: Record<string, unknown>;
286
- suggested_next: string[];
287
- } {
288
- return {
289
- observed_operations: [...this.observedOperations.keys()],
290
- known_bindings: this.getKnownBindings(),
291
- suggested_next: this.getSuggestedNext().map((op) => op.operation_id),
292
- };
293
- }
294
- }