File size: 5,913 Bytes
76289e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import type { Result } from "@openclaw/normalization-core/result";
import type { SqliteWorkerCommand } from "../infra/sqlite-worker-contract.js";
import type {
  PluginStateComparisonLimits,
  PluginStatePreparedComparison,
} from "./plugin-state-store.comparison.js";
import type { PluginStateSequencedJournalParams } from "./plugin-state-store.journal.js";
import type { PluginStateRegisterEntryParams } from "./plugin-state-store.kernel.js";
import type { PluginStateKeyRangeParams } from "./plugin-state-store.reads.js";
import type {
  PluginStateCompareResult,
  PluginStateEntry,
  PluginStateObservation,
  PluginStateStoreErrorCode,
  PluginStateStoreOperation,
} from "./plugin-state-store.types.js";
import type { PluginStateWorkerFailure } from "./plugin-state-worker-errors.js";

type Namespace = { pluginId: string; namespace: string };
type Key = Namespace & { key: string };
type Register = Omit<PluginStateRegisterEntryParams, "createdAtMs"> & { maxPluginEntries: number };

export type PluginStateWorkerOperations = {
  "pluginState.appendJournal": {
    input: PluginStateSequencedJournalParams;
    output: Result<number, PluginStateWorkerFailure>;
  };
  "pluginState.entriesInKeyRange": {
    input: PluginStateKeyRangeParams;
    output: Result<PluginStateEntry<unknown>[], PluginStateWorkerFailure>;
  };
  "pluginState.observe": {
    input: Key;
    output: Result<PluginStateObservation<unknown>, PluginStateWorkerFailure>;
  };
  "pluginState.compareUpdate": {
    input: PluginStatePreparedComparison & PluginStateComparisonLimits & { operation: "update" };
    output: Result<PluginStateCompareResult<unknown>, PluginStateWorkerFailure>;
  };
  "pluginState.compareDelete": {
    input: PluginStatePreparedComparison & PluginStateComparisonLimits & { operation: "delete" };
    output: Result<PluginStateCompareResult<unknown>, PluginStateWorkerFailure>;
  };
  "pluginState.register": { input: Register; output: Result<void, PluginStateWorkerFailure> };
  "pluginState.registerIfAbsent": {
    input: Register;
    output: Result<boolean, PluginStateWorkerFailure>;
  };
  "pluginState.deleteIfEqual": {
    input: Key & { expected: string | number | boolean | null };
    output: Result<boolean, PluginStateWorkerFailure>;
  };
  "pluginState.lookup": { input: Key; output: Result<unknown, PluginStateWorkerFailure> };
  "pluginState.lookupMany": {
    input: Namespace & { keys: readonly string[] };
    output: Result<Array<Result<unknown, PluginStateWorkerFailure>>, PluginStateWorkerFailure>;
  };
  "pluginState.consume": { input: Key; output: Result<unknown, PluginStateWorkerFailure> };
  "pluginState.delete": { input: Key; output: Result<boolean, PluginStateWorkerFailure> };
  "pluginState.entries": {
    input: Namespace;
    output: Result<PluginStateEntry<unknown>[], PluginStateWorkerFailure>;
  };
  "pluginState.count": { input: Namespace; output: Result<number, PluginStateWorkerFailure> };
  "pluginState.clear": { input: Namespace; output: Result<void, PluginStateWorkerFailure> };
};

export const pluginStateWorkerOperations = {
  "pluginState.appendJournal": {
    operation: "register",
    code: "PLUGIN_STATE_WRITE_FAILED",
    message: "Failed to register sequenced plugin state journal entry.",
  },
  "pluginState.entriesInKeyRange": {
    operation: "entries",
    code: "PLUGIN_STATE_READ_FAILED",
    message: "Failed to list plugin state entries by key range.",
  },
  "pluginState.observe": {
    operation: "lookup",
    code: "PLUGIN_STATE_READ_FAILED",
    message: "Failed to observe plugin state entry.",
  },
  "pluginState.compareUpdate": {
    operation: "register",
    code: "PLUGIN_STATE_WRITE_FAILED",
    message: "Failed to update plugin state entry.",
  },
  "pluginState.compareDelete": {
    operation: "delete",
    code: "PLUGIN_STATE_WRITE_FAILED",
    message: "Failed to conditionally delete plugin state entry.",
  },
  "pluginState.register": {
    operation: "register",
    code: "PLUGIN_STATE_WRITE_FAILED",
    message: "Failed to register plugin state entry.",
  },
  "pluginState.registerIfAbsent": {
    operation: "register",
    code: "PLUGIN_STATE_WRITE_FAILED",
    message: "Failed to register plugin state entry.",
  },
  "pluginState.deleteIfEqual": {
    operation: "delete",
    code: "PLUGIN_STATE_WRITE_FAILED",
    message: "Failed to conditionally delete plugin state entry.",
  },
  "pluginState.lookup": {
    operation: "lookup",
    code: "PLUGIN_STATE_READ_FAILED",
    message: "Failed to read plugin state entry.",
  },
  "pluginState.lookupMany": {
    operation: "lookup",
    code: "PLUGIN_STATE_READ_FAILED",
    message: "Failed to read plugin state entries.",
  },
  "pluginState.consume": {
    operation: "consume",
    code: "PLUGIN_STATE_READ_FAILED",
    message: "Failed to consume plugin state entry.",
  },
  "pluginState.delete": {
    operation: "delete",
    code: "PLUGIN_STATE_WRITE_FAILED",
    message: "Failed to delete plugin state entry.",
  },
  "pluginState.entries": {
    operation: "entries",
    code: "PLUGIN_STATE_READ_FAILED",
    message: "Failed to list plugin state entries.",
  },
  "pluginState.count": {
    operation: "count",
    code: "PLUGIN_STATE_READ_FAILED",
    message: "Failed to count plugin state entries.",
  },
  "pluginState.clear": {
    operation: "clear",
    code: "PLUGIN_STATE_WRITE_FAILED",
    message: "Failed to clear plugin state namespace.",
  },
} as const satisfies Record<
  keyof PluginStateWorkerOperations,
  {
    operation: PluginStateStoreOperation;
    code: PluginStateStoreErrorCode;
    message: string;
  }
>;

/** Selects the plugin-state branch of the shared actor's typed command union. */
export function isPluginStateWorkerCommand(command: {
  type: string;
  input: unknown;
}): command is SqliteWorkerCommand<PluginStateWorkerOperations> {
  return Object.hasOwn(pluginStateWorkerOperations, command.type);
}