@wowok/agent-mcp 2.3.13 → 2.3.14
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/harness/checkpoint.d.ts +8 -0
- package/dist/harness/checkpoint.js +129 -0
- package/dist/harness/index.d.ts +33 -0
- package/dist/harness/index.js +75 -0
- package/dist/harness/plan.d.ts +18 -0
- package/dist/harness/plan.js +252 -0
- package/dist/harness/recover.d.ts +17 -0
- package/dist/harness/recover.js +139 -0
- package/dist/harness/types.d.ts +137 -0
- package/dist/harness/types.js +1 -0
- package/dist/harness/verify.d.ts +42 -0
- package/dist/harness/verify.js +237 -0
- package/dist/index.js +134 -52
- package/dist/loop-engineering/aggregate.d.ts +50 -0
- package/dist/loop-engineering/aggregate.js +132 -0
- package/dist/loop-engineering/diagnose.d.ts +21 -0
- package/dist/loop-engineering/diagnose.js +179 -0
- package/dist/loop-engineering/improve.d.ts +26 -0
- package/dist/loop-engineering/improve.js +178 -0
- package/dist/loop-engineering/index.d.ts +4 -0
- package/dist/loop-engineering/index.js +4 -0
- package/dist/loop-engineering/pipeline.d.ts +17 -0
- package/dist/loop-engineering/pipeline.js +56 -0
- package/dist/mode-market/index.d.ts +3 -0
- package/dist/mode-market/index.js +3 -0
- package/dist/mode-market/registry.d.ts +53 -0
- package/dist/mode-market/registry.js +124 -0
- package/dist/mode-market/review.d.ts +27 -0
- package/dist/mode-market/review.js +214 -0
- package/dist/mode-market/submission.d.ts +25 -0
- package/dist/mode-market/submission.js +85 -0
- package/dist/schema/call/allocation.d.ts +40 -40
- package/dist/schema/call/arbitration.d.ts +90 -90
- package/dist/schema/call/base.d.ts +1054 -0
- package/dist/schema/call/base.js +100 -22
- package/dist/schema/call/bridge-handler.js +36 -18
- package/dist/schema/call/bridge.d.ts +115 -115
- package/dist/schema/call/handler.d.ts +2 -1
- package/dist/schema/call/handler.js +94 -3
- package/dist/schema/call/index.d.ts +1 -0
- package/dist/schema/call/index.js +1 -0
- package/dist/schema/call/machine.d.ts +144 -144
- package/dist/schema/call/order.d.ts +12 -12
- package/dist/schema/call/progress.d.ts +6 -6
- package/dist/schema/call/reward.d.ts +6 -6
- package/dist/schema/call/semantic.d.ts +23 -0
- package/dist/schema/call/semantic.js +764 -0
- package/dist/schema/call/service.d.ts +142 -142
- package/dist/schema/call/treasury.d.ts +204 -204
- package/dist/schema/local/index.d.ts +113 -2
- package/dist/schema/local/index.js +11 -1
- package/dist/schema/messenger/index.d.ts +416 -92
- package/dist/schema/messenger/index.js +9 -0
- package/dist/schema/operations.d.ts +988 -495
- package/dist/schema/operations.js +14 -3
- package/dist/schema/query/index.d.ts +346 -342
- package/dist/schema/query/index.js +4 -1
- package/dist/schemas/account_operation.output.json +3 -0
- package/dist/schemas/account_operation.schema.json +1 -1
- package/dist/schemas/index.json +1 -1
- package/dist/schemas/local_info_operation.output.json +3 -0
- package/dist/schemas/local_mark_operation.output.json +3 -0
- package/dist/schemas/messenger_operation.output.json +21 -0
- package/dist/schemas/messenger_operation.schema.json +21 -0
- package/dist/schemas/onchain_events.output.json +3 -0
- package/dist/schemas/onchain_operations.output.json +464 -21
- package/dist/schemas/onchain_operations.schema.json +21 -17
- package/dist/schemas/onchain_operations_allocation.schema.json +11 -9
- package/dist/schemas/onchain_operations_arbitration.schema.json +11 -9
- package/dist/schemas/onchain_operations_contact.schema.json +11 -9
- package/dist/schemas/onchain_operations_demand.schema.json +11 -9
- package/dist/schemas/onchain_operations_gen_passport.schema.json +21 -17
- package/dist/schemas/onchain_operations_guard.schema.json +11 -9
- package/dist/schemas/onchain_operations_machine.schema.json +11 -9
- package/dist/schemas/onchain_operations_order.schema.json +11 -9
- package/dist/schemas/onchain_operations_payment.schema.json +11 -9
- package/dist/schemas/onchain_operations_permission.schema.json +11 -9
- package/dist/schemas/onchain_operations_personal.schema.json +11 -9
- package/dist/schemas/onchain_operations_progress.schema.json +11 -9
- package/dist/schemas/onchain_operations_repository.schema.json +11 -9
- package/dist/schemas/onchain_operations_reward.schema.json +11 -9
- package/dist/schemas/onchain_operations_service.schema.json +11 -9
- package/dist/schemas/onchain_operations_treasury.schema.json +11 -9
- package/dist/schemas/onchain_table_data.output.json +3 -0
- package/dist/schemas/query_toolkit.schema.json +5 -1
- package/dist/telemetry/index.d.ts +19 -0
- package/dist/telemetry/index.js +112 -0
- package/dist/telemetry/redact.d.ts +2 -0
- package/dist/telemetry/redact.js +23 -0
- package/dist/telemetry/storage.d.ts +8 -0
- package/dist/telemetry/storage.js +80 -0
- package/package.json +6 -3
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Checkpoint } from "./types.js";
|
|
2
|
+
export declare function saveCheckpoint(cp: Checkpoint): void;
|
|
3
|
+
export declare function loadCheckpoint(taskId: string, checkpointId: string): Checkpoint | null;
|
|
4
|
+
export declare function loadLatestCheckpoint(taskId: string): Checkpoint | null;
|
|
5
|
+
export declare function listCheckpoints(taskId: string): Checkpoint[];
|
|
6
|
+
export declare function listTasks(): string[];
|
|
7
|
+
export declare function deleteCheckpoint(taskId: string, checkpointId: string): void;
|
|
8
|
+
export declare function createCheckpoint(taskId: string, phase: string, phaseStatus: Checkpoint["phase_status"], odgSnapshot: Checkpoint["odg_snapshot"], userDecisions: Record<string, unknown>, operationLog: Checkpoint["operation_log"], nextPhase: string, semanticSummary: Checkpoint["semantic_summary"]): Checkpoint;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, writeFileSync, readFileSync, readdirSync, unlinkSync, openSync, closeSync } from "fs";
|
|
2
|
+
import { join } from "path";
|
|
3
|
+
import { homedir } from "os";
|
|
4
|
+
const CHECKPOINT_ROOT = join(homedir(), ".wowok", "checkpoints");
|
|
5
|
+
function getTaskDir(taskId) {
|
|
6
|
+
return join(CHECKPOINT_ROOT, taskId);
|
|
7
|
+
}
|
|
8
|
+
function getCheckpointPath(taskId, checkpointId) {
|
|
9
|
+
return join(getTaskDir(taskId), `${checkpointId}.json`);
|
|
10
|
+
}
|
|
11
|
+
function getLatestPath(taskId) {
|
|
12
|
+
return join(getTaskDir(taskId), "latest.json");
|
|
13
|
+
}
|
|
14
|
+
function acquireLock(filePath) {
|
|
15
|
+
const lockPath = filePath + ".lock";
|
|
16
|
+
try {
|
|
17
|
+
const fd = openSync(lockPath, "wx");
|
|
18
|
+
closeSync(fd);
|
|
19
|
+
return true;
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
function releaseLock(filePath) {
|
|
26
|
+
const lockPath = filePath + ".lock";
|
|
27
|
+
try {
|
|
28
|
+
unlinkSync(lockPath);
|
|
29
|
+
}
|
|
30
|
+
catch {
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
export function saveCheckpoint(cp) {
|
|
34
|
+
const taskDir = getTaskDir(cp.task_id);
|
|
35
|
+
if (!existsSync(taskDir)) {
|
|
36
|
+
mkdirSync(taskDir, { recursive: true });
|
|
37
|
+
}
|
|
38
|
+
const cpPath = getCheckpointPath(cp.task_id, cp.checkpoint_id);
|
|
39
|
+
const latestPath = getLatestPath(cp.task_id);
|
|
40
|
+
if (!acquireLock(cpPath)) {
|
|
41
|
+
throw new Error(`Cannot acquire lock for checkpoint ${cp.checkpoint_id} — another process may be writing`);
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
writeFileSync(cpPath, JSON.stringify(cp, null, 2), "utf8");
|
|
45
|
+
writeFileSync(latestPath, JSON.stringify(cp, null, 2), "utf8");
|
|
46
|
+
}
|
|
47
|
+
finally {
|
|
48
|
+
releaseLock(cpPath);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
export function loadCheckpoint(taskId, checkpointId) {
|
|
52
|
+
const cpPath = getCheckpointPath(taskId, checkpointId);
|
|
53
|
+
if (!existsSync(cpPath))
|
|
54
|
+
return null;
|
|
55
|
+
try {
|
|
56
|
+
const raw = readFileSync(cpPath, "utf8");
|
|
57
|
+
return JSON.parse(raw);
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
export function loadLatestCheckpoint(taskId) {
|
|
64
|
+
const latestPath = getLatestPath(taskId);
|
|
65
|
+
if (!existsSync(latestPath))
|
|
66
|
+
return null;
|
|
67
|
+
try {
|
|
68
|
+
const raw = readFileSync(latestPath, "utf8");
|
|
69
|
+
return JSON.parse(raw);
|
|
70
|
+
}
|
|
71
|
+
catch {
|
|
72
|
+
return null;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
export function listCheckpoints(taskId) {
|
|
76
|
+
const taskDir = getTaskDir(taskId);
|
|
77
|
+
if (!existsSync(taskDir))
|
|
78
|
+
return [];
|
|
79
|
+
const files = readdirSync(taskDir)
|
|
80
|
+
.filter((f) => f.endsWith(".json") && f !== "latest.json")
|
|
81
|
+
.sort();
|
|
82
|
+
return files
|
|
83
|
+
.map((f) => {
|
|
84
|
+
try {
|
|
85
|
+
const raw = readFileSync(join(taskDir, f), "utf8");
|
|
86
|
+
return JSON.parse(raw);
|
|
87
|
+
}
|
|
88
|
+
catch {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
.filter((cp) => cp !== null);
|
|
93
|
+
}
|
|
94
|
+
export function listTasks() {
|
|
95
|
+
if (!existsSync(CHECKPOINT_ROOT))
|
|
96
|
+
return [];
|
|
97
|
+
return readdirSync(CHECKPOINT_ROOT, { withFileTypes: true })
|
|
98
|
+
.filter((d) => d.isDirectory())
|
|
99
|
+
.map((d) => d.name);
|
|
100
|
+
}
|
|
101
|
+
export function deleteCheckpoint(taskId, checkpointId) {
|
|
102
|
+
const cpPath = getCheckpointPath(taskId, checkpointId);
|
|
103
|
+
if (existsSync(cpPath)) {
|
|
104
|
+
if (!acquireLock(cpPath)) {
|
|
105
|
+
throw new Error(`Cannot acquire lock for deletion of ${checkpointId}`);
|
|
106
|
+
}
|
|
107
|
+
try {
|
|
108
|
+
unlinkSync(cpPath);
|
|
109
|
+
}
|
|
110
|
+
finally {
|
|
111
|
+
releaseLock(cpPath);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
export function createCheckpoint(taskId, phase, phaseStatus, odgSnapshot, userDecisions, operationLog, nextPhase, semanticSummary) {
|
|
116
|
+
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
|
|
117
|
+
return {
|
|
118
|
+
checkpoint_id: `CP-${phase.replace(/\s+/g, "")}-${phaseStatus}-${timestamp}`,
|
|
119
|
+
task_id: taskId,
|
|
120
|
+
timestamp: new Date().toISOString(),
|
|
121
|
+
phase,
|
|
122
|
+
phase_status: phaseStatus,
|
|
123
|
+
odg_snapshot: odgSnapshot,
|
|
124
|
+
user_decisions: userDecisions,
|
|
125
|
+
operation_log: operationLog,
|
|
126
|
+
next_phase: nextPhase,
|
|
127
|
+
semantic_summary: semanticSummary,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export * from "./types.js";
|
|
2
|
+
export * from "./plan.js";
|
|
3
|
+
export * from "./verify.js";
|
|
4
|
+
export * from "./recover.js";
|
|
5
|
+
export * from "./checkpoint.js";
|
|
6
|
+
import type { DecisionPoint } from "./plan.js";
|
|
7
|
+
import type { VerifyInput } from "./verify.js";
|
|
8
|
+
import type { ODG, ExpectedResult, VerifyReport, RecoveryAction, Checkpoint } from "./types.js";
|
|
9
|
+
export declare class Harness {
|
|
10
|
+
private context;
|
|
11
|
+
plan(userInput: string, taskId?: string): ODG;
|
|
12
|
+
classifyIntent(userInput: string): import("./plan.js").IntentClassification;
|
|
13
|
+
getDecisions(odg: ODG): DecisionPoint[];
|
|
14
|
+
detectCycles(odg: ODG): string[] | null;
|
|
15
|
+
getScenarioRegistry(): import("./types.js").ScenarioTemplate[];
|
|
16
|
+
expect(operation: string, intent: string, operationType: string, data?: Record<string, unknown>): ExpectedResult;
|
|
17
|
+
verify(input: VerifyInput): VerifyReport;
|
|
18
|
+
recover(errorCode: string, operationId: string, context?: {
|
|
19
|
+
operation_type?: string;
|
|
20
|
+
data?: Record<string, unknown>;
|
|
21
|
+
}): RecoveryAction;
|
|
22
|
+
resetRecovery(operationId: string): void;
|
|
23
|
+
getBackoffDelay(attempt: number): number;
|
|
24
|
+
getRecoveryRules(): Record<string, import("./recover.js").RecoveryRule>;
|
|
25
|
+
checkpoint(taskId: string, phase: string, phaseStatus: Checkpoint["phase_status"], odgSnapshot: Checkpoint["odg_snapshot"], userDecisions: Record<string, unknown>, operationLog: Checkpoint["operation_log"], nextPhase: string, semanticSummary: Checkpoint["semantic_summary"]): Checkpoint;
|
|
26
|
+
loadCheckpoint(taskId: string): Checkpoint | null;
|
|
27
|
+
listCheckpoints(taskId: string): Checkpoint[];
|
|
28
|
+
listTasks(): string[];
|
|
29
|
+
setContext(key: string, value: unknown): void;
|
|
30
|
+
getContext<T>(key: string): T | undefined;
|
|
31
|
+
}
|
|
32
|
+
export declare function getHarness(): Harness;
|
|
33
|
+
export declare function setHarness(h: Harness): void;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export * from "./types.js";
|
|
2
|
+
export * from "./plan.js";
|
|
3
|
+
export * from "./verify.js";
|
|
4
|
+
export * from "./recover.js";
|
|
5
|
+
export * from "./checkpoint.js";
|
|
6
|
+
import { classifyIntent, generateODG, identifyDecisionPoints, detectCycles, SCENARIO_REGISTRY } from "./plan.js";
|
|
7
|
+
import { verify, generateExpected } from "./verify.js";
|
|
8
|
+
import { recover, resetAttempts, getBackoffDelay, RECOVERY_RULES } from "./recover.js";
|
|
9
|
+
import { saveCheckpoint, loadLatestCheckpoint, listCheckpoints, listTasks, createCheckpoint } from "./checkpoint.js";
|
|
10
|
+
export class Harness {
|
|
11
|
+
context = new Map();
|
|
12
|
+
plan(userInput, taskId) {
|
|
13
|
+
return generateODG(userInput, taskId);
|
|
14
|
+
}
|
|
15
|
+
classifyIntent(userInput) {
|
|
16
|
+
return classifyIntent(userInput);
|
|
17
|
+
}
|
|
18
|
+
getDecisions(odg) {
|
|
19
|
+
return identifyDecisionPoints(odg);
|
|
20
|
+
}
|
|
21
|
+
detectCycles(odg) {
|
|
22
|
+
return detectCycles(odg);
|
|
23
|
+
}
|
|
24
|
+
getScenarioRegistry() {
|
|
25
|
+
return SCENARIO_REGISTRY;
|
|
26
|
+
}
|
|
27
|
+
expect(operation, intent, operationType, data) {
|
|
28
|
+
return generateExpected(operation, intent, operationType, data);
|
|
29
|
+
}
|
|
30
|
+
verify(input) {
|
|
31
|
+
return verify(input);
|
|
32
|
+
}
|
|
33
|
+
recover(errorCode, operationId, context) {
|
|
34
|
+
return recover(errorCode, operationId, context);
|
|
35
|
+
}
|
|
36
|
+
resetRecovery(operationId) {
|
|
37
|
+
resetAttempts(operationId);
|
|
38
|
+
}
|
|
39
|
+
getBackoffDelay(attempt) {
|
|
40
|
+
return getBackoffDelay(attempt);
|
|
41
|
+
}
|
|
42
|
+
getRecoveryRules() {
|
|
43
|
+
return RECOVERY_RULES;
|
|
44
|
+
}
|
|
45
|
+
checkpoint(taskId, phase, phaseStatus, odgSnapshot, userDecisions, operationLog, nextPhase, semanticSummary) {
|
|
46
|
+
const cp = createCheckpoint(taskId, phase, phaseStatus, odgSnapshot, userDecisions, operationLog, nextPhase, semanticSummary);
|
|
47
|
+
saveCheckpoint(cp);
|
|
48
|
+
return cp;
|
|
49
|
+
}
|
|
50
|
+
loadCheckpoint(taskId) {
|
|
51
|
+
return loadLatestCheckpoint(taskId);
|
|
52
|
+
}
|
|
53
|
+
listCheckpoints(taskId) {
|
|
54
|
+
return listCheckpoints(taskId);
|
|
55
|
+
}
|
|
56
|
+
listTasks() {
|
|
57
|
+
return listTasks();
|
|
58
|
+
}
|
|
59
|
+
setContext(key, value) {
|
|
60
|
+
this.context.set(key, value);
|
|
61
|
+
}
|
|
62
|
+
getContext(key) {
|
|
63
|
+
return this.context.get(key);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
let singleton = null;
|
|
67
|
+
export function getHarness() {
|
|
68
|
+
if (!singleton) {
|
|
69
|
+
singleton = new Harness();
|
|
70
|
+
}
|
|
71
|
+
return singleton;
|
|
72
|
+
}
|
|
73
|
+
export function setHarness(h) {
|
|
74
|
+
singleton = h;
|
|
75
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { ODG, ScenarioTemplate } from "./types.js";
|
|
2
|
+
export declare const SCENARIO_REGISTRY: ScenarioTemplate[];
|
|
3
|
+
export interface IntentClassification {
|
|
4
|
+
scenario: string;
|
|
5
|
+
matched_keywords: string[];
|
|
6
|
+
confidence: number;
|
|
7
|
+
fallback: boolean;
|
|
8
|
+
}
|
|
9
|
+
export declare function classifyIntent(userInput: string): IntentClassification;
|
|
10
|
+
export declare function generateODG(userInput: string, taskId?: string): ODG;
|
|
11
|
+
export interface DecisionPoint {
|
|
12
|
+
round: string;
|
|
13
|
+
question: string;
|
|
14
|
+
options: string[];
|
|
15
|
+
default?: string;
|
|
16
|
+
}
|
|
17
|
+
export declare function identifyDecisionPoints(odg: ODG): DecisionPoint[];
|
|
18
|
+
export declare function detectCycles(odg: ODG): string[] | null;
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
export const SCENARIO_REGISTRY = [
|
|
2
|
+
{
|
|
3
|
+
name: "freelance",
|
|
4
|
+
keywords: ["freelance", "service", "deliverable", "client", "gig", "consult", "design", "develop"],
|
|
5
|
+
description: "Service delivery with escrow: publish → order → deliver → accept → withdraw",
|
|
6
|
+
default_permissions: { provider: 1000, customer: "namedOperator", arbiter: 1500 },
|
|
7
|
+
objects: [
|
|
8
|
+
{ type: "Permission", reversible: true, dependencies: [], user_decisions: ["reuse existing or create new?"], estimated_time: "2min" },
|
|
9
|
+
{ type: "Service", reversible: true, dependencies: ["Permission"], user_decisions: ["name", "description", "token_type"], estimated_time: "3min" },
|
|
10
|
+
{ type: "Machine", reversible: true, dependencies: ["Service"], user_decisions: ["state graph", "operators"], estimated_time: "10min" },
|
|
11
|
+
{ type: "Guard", reversible: false, dependencies: ["Machine"], user_decisions: ["guard table", "submission fields"], risk_flags: ["immutable_after_creation"], estimated_time: "5min" },
|
|
12
|
+
{ type: "Guard", reversible: false, dependencies: ["Machine"], user_decisions: ["refund guard"], risk_flags: ["immutable_after_creation"], estimated_time: "5min" },
|
|
13
|
+
{ type: "Allocation", reversible: true, dependencies: ["Service", "Guard"], user_decisions: ["sharing ratios", "allocators"], estimated_time: "5min" },
|
|
14
|
+
],
|
|
15
|
+
phases: [
|
|
16
|
+
{ name: "Foundation", object_types: ["Permission", "Service"] },
|
|
17
|
+
{ name: "Business Logic", object_types: ["Machine"] },
|
|
18
|
+
{ name: "Trust Layer", object_types: ["Guard"] },
|
|
19
|
+
{ name: "Revenue", object_types: ["Allocation"] },
|
|
20
|
+
{ name: "Publish", object_types: ["Service"] },
|
|
21
|
+
],
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
name: "rental",
|
|
25
|
+
keywords: ["rent", "rental", "lease", "property", "item", "deposit", "tenant", "landlord"],
|
|
26
|
+
description: "Item rental with deposit: publish → rent → inspect → return/refund",
|
|
27
|
+
default_permissions: { provider: 1000, customer: "namedOperator", arbiter: 1500 },
|
|
28
|
+
objects: [
|
|
29
|
+
{ type: "Permission", reversible: true, dependencies: [], user_decisions: ["reuse existing or create new?"], estimated_time: "2min" },
|
|
30
|
+
{ type: "Service", reversible: true, dependencies: ["Permission"], user_decisions: ["name", "deposit amount", "rental terms"], estimated_time: "3min" },
|
|
31
|
+
{ type: "Machine", reversible: true, dependencies: ["Service"], user_decisions: ["rental states", "return/inspect flow"], estimated_time: "10min" },
|
|
32
|
+
{ type: "Guard", reversible: false, dependencies: ["Machine"], user_decisions: ["return guard", "damage guard"], risk_flags: ["immutable_after_creation"], estimated_time: "5min" },
|
|
33
|
+
{ type: "Guard", reversible: false, dependencies: ["Machine"], user_decisions: ["refund guard"], risk_flags: ["immutable_after_creation"], estimated_time: "5min" },
|
|
34
|
+
{ type: "Allocation", reversible: true, dependencies: ["Service", "Guard"], user_decisions: ["deposit/refund allocation", "damage compensation"], estimated_time: "5min" },
|
|
35
|
+
],
|
|
36
|
+
phases: [
|
|
37
|
+
{ name: "Foundation", object_types: ["Permission", "Service"] },
|
|
38
|
+
{ name: "Business Logic", object_types: ["Machine"] },
|
|
39
|
+
{ name: "Trust Layer", object_types: ["Guard"] },
|
|
40
|
+
{ name: "Revenue", object_types: ["Allocation"] },
|
|
41
|
+
{ name: "Publish", object_types: ["Service"] },
|
|
42
|
+
],
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: "digital_goods",
|
|
46
|
+
keywords: ["digital", "download", "ebook", "software", "license", "instant", "content"],
|
|
47
|
+
description: "Instant digital delivery: publish → buy → instant delivery",
|
|
48
|
+
default_permissions: { provider: 1000, customer: "namedOperator" },
|
|
49
|
+
objects: [
|
|
50
|
+
{ type: "Permission", reversible: true, dependencies: [], estimated_time: "2min" },
|
|
51
|
+
{ type: "Service", reversible: true, dependencies: ["Permission"], user_decisions: ["name", "price", "delivery method"], estimated_time: "3min" },
|
|
52
|
+
{ type: "Machine", reversible: true, dependencies: ["Service"], user_decisions: ["instant delivery states"], estimated_time: "5min" },
|
|
53
|
+
{ type: "Allocation", reversible: true, dependencies: ["Service"], user_decisions: ["revenue split"], estimated_time: "3min" },
|
|
54
|
+
],
|
|
55
|
+
phases: [
|
|
56
|
+
{ name: "Foundation", object_types: ["Permission", "Service"] },
|
|
57
|
+
{ name: "Business Logic", object_types: ["Machine"] },
|
|
58
|
+
{ name: "Revenue", object_types: ["Allocation"] },
|
|
59
|
+
{ name: "Publish", object_types: ["Service"] },
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: "travel_package",
|
|
64
|
+
keywords: ["travel", "tour", "trip", "package", "booking", "itinerary", "vacation", "hotel"],
|
|
65
|
+
description: "Multi-stage service with milestones: publish → book → itinerary → complete",
|
|
66
|
+
default_permissions: { provider: 1000, customer: "namedOperator", arbiter: 1500 },
|
|
67
|
+
objects: [
|
|
68
|
+
{ type: "Permission", reversible: true, dependencies: [], estimated_time: "2min" },
|
|
69
|
+
{ type: "Service", reversible: true, dependencies: ["Permission"], user_decisions: ["name", "package details", "pricing tiers"], estimated_time: "5min" },
|
|
70
|
+
{ type: "Machine", reversible: true, dependencies: ["Service"], user_decisions: ["booking states", "milestone flow", "cancellation"], estimated_time: "15min" },
|
|
71
|
+
{ type: "Guard", reversible: false, dependencies: ["Machine"], user_decisions: ["booking guard", "cancellation guard"], risk_flags: ["immutable_after_creation"], estimated_time: "5min" },
|
|
72
|
+
{ type: "Allocation", reversible: true, dependencies: ["Service", "Guard"], user_decisions: ["milestone payments", "refund policy"], estimated_time: "5min" },
|
|
73
|
+
],
|
|
74
|
+
phases: [
|
|
75
|
+
{ name: "Foundation", object_types: ["Permission", "Service"] },
|
|
76
|
+
{ name: "Business Logic", object_types: ["Machine"] },
|
|
77
|
+
{ name: "Trust Layer", object_types: ["Guard"] },
|
|
78
|
+
{ name: "Revenue", object_types: ["Allocation"] },
|
|
79
|
+
{ name: "Publish", object_types: ["Service"] },
|
|
80
|
+
],
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: "general",
|
|
84
|
+
keywords: [],
|
|
85
|
+
description: "Fallback for unmatched scenarios — generic service template",
|
|
86
|
+
default_permissions: { provider: 1000, customer: "namedOperator" },
|
|
87
|
+
objects: [
|
|
88
|
+
{ type: "Permission", reversible: true, dependencies: [], estimated_time: "2min" },
|
|
89
|
+
{ type: "Service", reversible: true, dependencies: ["Permission"], user_decisions: ["name", "description", "token_type"], estimated_time: "3min" },
|
|
90
|
+
{ type: "Machine", reversible: true, dependencies: ["Service"], user_decisions: ["state graph"], estimated_time: "10min" },
|
|
91
|
+
{ type: "Guard", reversible: false, dependencies: ["Machine"], user_decisions: ["guard requirements"], risk_flags: ["immutable_after_creation"], estimated_time: "5min" },
|
|
92
|
+
{ type: "Allocation", reversible: true, dependencies: ["Service"], user_decisions: ["sharing"], estimated_time: "5min" },
|
|
93
|
+
],
|
|
94
|
+
phases: [
|
|
95
|
+
{ name: "Foundation", object_types: ["Permission", "Service"] },
|
|
96
|
+
{ name: "Business Logic", object_types: ["Machine"] },
|
|
97
|
+
{ name: "Trust Layer", object_types: ["Guard"] },
|
|
98
|
+
{ name: "Revenue", object_types: ["Allocation"] },
|
|
99
|
+
{ name: "Publish", object_types: ["Service"] },
|
|
100
|
+
],
|
|
101
|
+
},
|
|
102
|
+
];
|
|
103
|
+
export function classifyIntent(userInput) {
|
|
104
|
+
const lower = userInput.toLowerCase();
|
|
105
|
+
let bestMatch = null;
|
|
106
|
+
let bestScore = 0;
|
|
107
|
+
let matchedKeywords = [];
|
|
108
|
+
for (const scenario of SCENARIO_REGISTRY) {
|
|
109
|
+
if (scenario.keywords.length === 0)
|
|
110
|
+
continue;
|
|
111
|
+
const hits = scenario.keywords.filter((kw) => lower.includes(kw));
|
|
112
|
+
if (hits.length > bestScore) {
|
|
113
|
+
bestScore = hits.length;
|
|
114
|
+
bestMatch = scenario;
|
|
115
|
+
matchedKeywords = hits;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
if (bestMatch && bestScore > 0) {
|
|
119
|
+
const confidence = Math.min(bestScore / 3, 1.0);
|
|
120
|
+
return {
|
|
121
|
+
scenario: bestMatch.name,
|
|
122
|
+
matched_keywords: matchedKeywords,
|
|
123
|
+
confidence,
|
|
124
|
+
fallback: false,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
scenario: "general",
|
|
129
|
+
matched_keywords: [],
|
|
130
|
+
confidence: 0.3,
|
|
131
|
+
fallback: true,
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
let nodeIdCounter = 0;
|
|
135
|
+
function nextNodeId(type) {
|
|
136
|
+
nodeIdCounter++;
|
|
137
|
+
return `${type.toLowerCase()}_${nodeIdCounter}`;
|
|
138
|
+
}
|
|
139
|
+
export function generateODG(userInput, taskId) {
|
|
140
|
+
const classification = classifyIntent(userInput);
|
|
141
|
+
const template = SCENARIO_REGISTRY.find((s) => s.name === classification.scenario) ?? SCENARIO_REGISTRY[SCENARIO_REGISTRY.length - 1];
|
|
142
|
+
const objects = template.objects.map((obj) => ({
|
|
143
|
+
id: nextNodeId(obj.type),
|
|
144
|
+
type: obj.type,
|
|
145
|
+
status: "not_created",
|
|
146
|
+
reversible: obj.reversible,
|
|
147
|
+
dependencies: [],
|
|
148
|
+
user_decisions: obj.user_decisions,
|
|
149
|
+
risk_flags: obj.risk_flags,
|
|
150
|
+
estimated_time: obj.estimated_time,
|
|
151
|
+
}));
|
|
152
|
+
const typeToLatestId = new Map();
|
|
153
|
+
for (const obj of objects) {
|
|
154
|
+
const depTypes = template.objects.find((t) => t.type === obj.type)?.dependencies ?? [];
|
|
155
|
+
obj.dependencies = depTypes
|
|
156
|
+
.map((depType) => typeToLatestId.get(depType))
|
|
157
|
+
.filter((id) => id !== undefined);
|
|
158
|
+
typeToLatestId.set(obj.type, obj.id);
|
|
159
|
+
}
|
|
160
|
+
const phases = template.phases.map((phase) => {
|
|
161
|
+
const phaseObjectIds = objects
|
|
162
|
+
.filter((obj) => phase.object_types.includes(obj.type))
|
|
163
|
+
.map((obj) => obj.id);
|
|
164
|
+
return { name: phase.name, objects: phaseObjectIds };
|
|
165
|
+
});
|
|
166
|
+
return {
|
|
167
|
+
task_id: taskId ?? `task_${Date.now()}`,
|
|
168
|
+
scenario: classification.scenario,
|
|
169
|
+
objects,
|
|
170
|
+
phases,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
export function identifyDecisionPoints(odg) {
|
|
174
|
+
const decisions = [];
|
|
175
|
+
const scenario = SCENARIO_REGISTRY.find((s) => s.name === odg.scenario);
|
|
176
|
+
decisions.push({
|
|
177
|
+
round: "R1",
|
|
178
|
+
question: `I've classified your intent as "${odg.scenario}". Is this correct?`,
|
|
179
|
+
options: ["Yes, correct", "No, let me clarify"],
|
|
180
|
+
});
|
|
181
|
+
decisions.push({
|
|
182
|
+
round: "R2",
|
|
183
|
+
question: "Do you have existing Permission objects to reuse, or should I create a new one?",
|
|
184
|
+
options: ["Create new", "Reuse existing", "Show me what I have"],
|
|
185
|
+
});
|
|
186
|
+
decisions.push({
|
|
187
|
+
round: "R3",
|
|
188
|
+
question: "What is the service name, description, and pricing token?",
|
|
189
|
+
options: ["WOW (default)", "USDT", "USDC", "Custom token"],
|
|
190
|
+
default: "WOW (default)",
|
|
191
|
+
});
|
|
192
|
+
if (scenario?.default_permissions.arbiter) {
|
|
193
|
+
decisions.push({
|
|
194
|
+
round: "R4",
|
|
195
|
+
question: "Permission model: provider=1000, customer uses namedOperator, arbiter=1500. Adjust?",
|
|
196
|
+
options: ["Use defaults", "Customize"],
|
|
197
|
+
default: "Use defaults",
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
decisions.push({
|
|
201
|
+
round: "R5",
|
|
202
|
+
question: "What are the service states and transitions? (entry → states → terminals)",
|
|
203
|
+
options: ["Use scenario template", "Custom design"],
|
|
204
|
+
});
|
|
205
|
+
const guardNodes = odg.objects.filter((o) => o.type === "Guard");
|
|
206
|
+
if (guardNodes.length > 0) {
|
|
207
|
+
decisions.push({
|
|
208
|
+
round: "R6",
|
|
209
|
+
question: `${guardNodes.length} Guard(s) needed. What should each Guard verify?`,
|
|
210
|
+
options: ["Withdrawal guard", "Refund guard", "Delivery guard", "Custom"],
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
decisions.push({
|
|
214
|
+
round: "R7",
|
|
215
|
+
question: "How should revenue be split? (e.g., merchant 80%, platform 20%)",
|
|
216
|
+
options: ["100% to merchant", "80/20 split", "Custom split"],
|
|
217
|
+
default: "100% to merchant",
|
|
218
|
+
});
|
|
219
|
+
return decisions;
|
|
220
|
+
}
|
|
221
|
+
export function detectCycles(odg) {
|
|
222
|
+
const visited = new Set();
|
|
223
|
+
const stack = new Set();
|
|
224
|
+
const cycleNodes = [];
|
|
225
|
+
function dfs(nodeId, path) {
|
|
226
|
+
if (stack.has(nodeId)) {
|
|
227
|
+
const cycleStart = path.indexOf(nodeId);
|
|
228
|
+
cycleNodes.push(...path.slice(cycleStart), nodeId);
|
|
229
|
+
return true;
|
|
230
|
+
}
|
|
231
|
+
if (visited.has(nodeId))
|
|
232
|
+
return false;
|
|
233
|
+
visited.add(nodeId);
|
|
234
|
+
stack.add(nodeId);
|
|
235
|
+
const node = odg.objects.find((o) => o.id === nodeId);
|
|
236
|
+
if (node) {
|
|
237
|
+
for (const dep of node.dependencies) {
|
|
238
|
+
if (dfs(dep, [...path, nodeId]))
|
|
239
|
+
return true;
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
stack.delete(nodeId);
|
|
243
|
+
return false;
|
|
244
|
+
}
|
|
245
|
+
for (const obj of odg.objects) {
|
|
246
|
+
if (!visited.has(obj.id)) {
|
|
247
|
+
if (dfs(obj.id, []))
|
|
248
|
+
return cycleNodes;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { RecoveryAction, RecoveryStrategy } from "./types.js";
|
|
2
|
+
export interface RecoveryRule {
|
|
3
|
+
error_code: string;
|
|
4
|
+
strategy: RecoveryStrategy;
|
|
5
|
+
max_attempts: number;
|
|
6
|
+
automation: "full" | "semi" | "manual";
|
|
7
|
+
detail: string;
|
|
8
|
+
user_prompt?: string;
|
|
9
|
+
adjusted_params?: Record<string, unknown>;
|
|
10
|
+
}
|
|
11
|
+
export declare const RECOVERY_RULES: Record<string, RecoveryRule>;
|
|
12
|
+
export declare function resetAttempts(operationId: string): void;
|
|
13
|
+
export declare function recover(errorCode: string, operationId: string, context?: {
|
|
14
|
+
operation_type?: string;
|
|
15
|
+
data?: Record<string, unknown>;
|
|
16
|
+
}): RecoveryAction;
|
|
17
|
+
export declare function getBackoffDelay(attempt: number): number;
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
export const RECOVERY_RULES = {
|
|
2
|
+
insufficient_balance: {
|
|
3
|
+
error_code: "insufficient_balance",
|
|
4
|
+
strategy: "claim_faucet",
|
|
5
|
+
max_attempts: 3,
|
|
6
|
+
automation: "semi",
|
|
7
|
+
detail: "1. Claim faucet tokens (testnet) 2. Prompt user to top up (mainnet) 3. Retry original operation",
|
|
8
|
+
user_prompt: "Insufficient balance for gas or operation. On testnet, claim faucet tokens. On mainnet, please top up your wallet and retry.",
|
|
9
|
+
},
|
|
10
|
+
network_error: {
|
|
11
|
+
error_code: "network_error",
|
|
12
|
+
strategy: "retry",
|
|
13
|
+
max_attempts: 5,
|
|
14
|
+
automation: "full",
|
|
15
|
+
detail: "Exponential backoff retry: 1s / 2s / 4s / 8s / 16s. Switch RPC node if available.",
|
|
16
|
+
},
|
|
17
|
+
guard_rejected: {
|
|
18
|
+
error_code: "guard_rejected",
|
|
19
|
+
strategy: "fill_submission",
|
|
20
|
+
max_attempts: 2,
|
|
21
|
+
automation: "semi",
|
|
22
|
+
detail: "1. Parse Guard table structure 2. Prompt user to fill submission fields 3. Resubmit with call_with_submission",
|
|
23
|
+
user_prompt: "Guard verification rejected. Please fill in the required submission data and resubmit.",
|
|
24
|
+
},
|
|
25
|
+
immutable_violation: {
|
|
26
|
+
error_code: "immutable_violation",
|
|
27
|
+
strategy: "recreate",
|
|
28
|
+
max_attempts: 1,
|
|
29
|
+
automation: "semi",
|
|
30
|
+
detail: "1. Stop modification attempt 2. Evaluate creating a new object as replacement 3. Re-plan affected phases",
|
|
31
|
+
user_prompt: "Cannot modify an immutable object. A new object must be created as a replacement. Confirm to proceed with recreation.",
|
|
32
|
+
},
|
|
33
|
+
permission_denied: {
|
|
34
|
+
error_code: "permission_denied",
|
|
35
|
+
strategy: "switch_account",
|
|
36
|
+
max_attempts: 2,
|
|
37
|
+
automation: "semi",
|
|
38
|
+
detail: "1. Query object's permission_guard 2. List accounts with permission 3. Switch account or prompt user to add permission",
|
|
39
|
+
user_prompt: "Current account lacks permission for this operation. Switch to an authorized account or update the Permission object.",
|
|
40
|
+
},
|
|
41
|
+
object_not_found: {
|
|
42
|
+
error_code: "object_not_found",
|
|
43
|
+
strategy: "query_and_retry",
|
|
44
|
+
max_attempts: 3,
|
|
45
|
+
automation: "semi",
|
|
46
|
+
detail: "1. Re-query the object (may be sync delay) 2. If truly absent, search for similar objects 3. Prompt user to reselect",
|
|
47
|
+
user_prompt: "Referenced object not found. It may be a synchronization delay. Retrying... If the issue persists, please select a valid object.",
|
|
48
|
+
},
|
|
49
|
+
state_conflict: {
|
|
50
|
+
error_code: "state_conflict",
|
|
51
|
+
strategy: "adjust_params",
|
|
52
|
+
max_attempts: 2,
|
|
53
|
+
automation: "full",
|
|
54
|
+
detail: "1. Query object's current true state 2. Update ODG status annotations 3. Adjust subsequent operations (e.g., skip completed steps)",
|
|
55
|
+
},
|
|
56
|
+
invalid_parameter: {
|
|
57
|
+
error_code: "invalid_parameter",
|
|
58
|
+
strategy: "adjust_params",
|
|
59
|
+
max_attempts: 2,
|
|
60
|
+
automation: "semi",
|
|
61
|
+
detail: "1. Query the relevant schema 2. Auto-fix obvious errors (e.g., BigInt vs string) 3. If unfixable, prompt user",
|
|
62
|
+
user_prompt: "Parameter validation failed. Please check the parameter types and values against the schema and retry.",
|
|
63
|
+
},
|
|
64
|
+
unknown: {
|
|
65
|
+
error_code: "unknown",
|
|
66
|
+
strategy: "escalate_human",
|
|
67
|
+
max_attempts: 1,
|
|
68
|
+
automation: "manual",
|
|
69
|
+
detail: "1. Report full error context to telemetry 2. Escalate to human intervention 3. Flag for Loop Engineering rule addition",
|
|
70
|
+
user_prompt: "An unclassified error occurred. Please review the error details and contact support if needed. This error has been logged for tool improvement.",
|
|
71
|
+
},
|
|
72
|
+
};
|
|
73
|
+
const operationAttempts = new Map();
|
|
74
|
+
function getAttemptCount(operationId, errorCode) {
|
|
75
|
+
const opMap = operationAttempts.get(operationId);
|
|
76
|
+
if (!opMap)
|
|
77
|
+
return 0;
|
|
78
|
+
return opMap.get(errorCode) ?? 0;
|
|
79
|
+
}
|
|
80
|
+
function incrementAttempt(operationId, errorCode) {
|
|
81
|
+
if (!operationAttempts.has(operationId)) {
|
|
82
|
+
operationAttempts.set(operationId, new Map());
|
|
83
|
+
}
|
|
84
|
+
const opMap = operationAttempts.get(operationId);
|
|
85
|
+
const current = (opMap.get(errorCode) ?? 0) + 1;
|
|
86
|
+
opMap.set(errorCode, current);
|
|
87
|
+
return current;
|
|
88
|
+
}
|
|
89
|
+
function getTotalAttempts(operationId) {
|
|
90
|
+
const opMap = operationAttempts.get(operationId);
|
|
91
|
+
if (!opMap)
|
|
92
|
+
return 0;
|
|
93
|
+
let total = 0;
|
|
94
|
+
for (const count of opMap.values())
|
|
95
|
+
total += count;
|
|
96
|
+
return total;
|
|
97
|
+
}
|
|
98
|
+
export function resetAttempts(operationId) {
|
|
99
|
+
operationAttempts.delete(operationId);
|
|
100
|
+
}
|
|
101
|
+
export function recover(errorCode, operationId, context) {
|
|
102
|
+
const rule = RECOVERY_RULES[errorCode] ?? RECOVERY_RULES.unknown;
|
|
103
|
+
const TOTAL_RETRY_LIMIT = 5;
|
|
104
|
+
const totalAttempts = getTotalAttempts(operationId);
|
|
105
|
+
const typeAttempts = getAttemptCount(operationId, errorCode);
|
|
106
|
+
if (totalAttempts >= TOTAL_RETRY_LIMIT) {
|
|
107
|
+
return {
|
|
108
|
+
strategy: "stop",
|
|
109
|
+
should_retry: false,
|
|
110
|
+
max_attempts: TOTAL_RETRY_LIMIT,
|
|
111
|
+
current_attempt: totalAttempts,
|
|
112
|
+
detail: `Total retry limit (${TOTAL_RETRY_LIMIT}) reached for operation ${operationId}. Escalating to human intervention.`,
|
|
113
|
+
user_prompt: "Maximum retry attempts reached. Manual intervention required.",
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
if (typeAttempts >= rule.max_attempts) {
|
|
117
|
+
return {
|
|
118
|
+
strategy: "escalate_human",
|
|
119
|
+
should_retry: false,
|
|
120
|
+
max_attempts: rule.max_attempts,
|
|
121
|
+
current_attempt: typeAttempts,
|
|
122
|
+
detail: `Per-error-code retry limit (${rule.max_attempts}) reached for ${errorCode}. Escalating to human.`,
|
|
123
|
+
user_prompt: `Recovery attempts for ${errorCode} exhausted. Manual intervention required.`,
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
const currentAttempt = incrementAttempt(operationId, errorCode);
|
|
127
|
+
return {
|
|
128
|
+
strategy: rule.strategy,
|
|
129
|
+
should_retry: rule.automation === "full" || currentAttempt < rule.max_attempts,
|
|
130
|
+
max_attempts: rule.max_attempts,
|
|
131
|
+
current_attempt: currentAttempt,
|
|
132
|
+
adjusted_params: rule.adjusted_params,
|
|
133
|
+
user_prompt: rule.user_prompt,
|
|
134
|
+
detail: rule.detail,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
export function getBackoffDelay(attempt) {
|
|
138
|
+
return Math.pow(2, attempt - 1) * 1000;
|
|
139
|
+
}
|