instant_record 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +58 -0
- data/README.md +267 -61
- data/app/controllers/instant_record/bootstraps_controller.rb +39 -12
- data/app/controllers/instant_record/events_controller.rb +58 -6
- data/app/controllers/instant_record/files_controller.rb +116 -0
- data/app/controllers/instant_record/mutations_controller.rb +84 -5
- data/app/controllers/instant_record/native_screens_controller.rb +60 -0
- data/app/controllers/instant_record/records_controller.rb +86 -8
- data/app/controllers/instant_record/shells_controller.rb +64 -0
- data/app/controllers/instant_record/sync_notices_controller.rb +81 -0
- data/app/helpers/instant_record/sync_notices_helper.rb +41 -0
- data/app/jobs/instant_record/shell_build_job.rb +16 -0
- data/app/models/instant_record/change.rb +36 -3
- data/app/models/instant_record/mutation_applier.rb +633 -50
- data/app/views/instant_record/sync_notices/_notices.html.erb +63 -0
- data/compiled/README.md +272 -0
- data/compiled/pipeline/browser_migrations.rb +18 -0
- data/compiled/pipeline/build.rb +13 -0
- data/compiled/pipeline/build_native.rb +14 -0
- data/compiled/pipeline/native/host.c +78 -0
- data/compiled/pipeline/native/instant_record_native.h +57 -0
- data/compiled/pipeline/native_shim.c +149 -0
- data/compiled/pipeline/reactor_shim.c +66 -0
- data/compiled/pipeline/sqlite/rh_shim.c +5 -0
- data/compiled/pipeline/sqlite_release.rb +43 -0
- data/compiled/toolchain.env.example +22 -0
- data/compiled/web/index.html +20 -0
- data/compiled/web/package-lock.json +884 -0
- data/compiled/web/package.json +17 -0
- data/compiled/web/src/app-host-core.js +556 -0
- data/compiled/web/src/app-host.js +16 -0
- data/compiled/web/src/branch-manager.js +140 -0
- data/compiled/web/src/branch-preview.js +397 -0
- data/compiled/web/src/changes.js +191 -0
- data/compiled/web/src/faults.js +172 -0
- data/compiled/web/src/flight-recorder.js +347 -0
- data/compiled/web/src/history-fill.js +68 -0
- data/compiled/web/src/http-headers.js +100 -0
- data/compiled/web/src/leader-election.js +705 -0
- data/compiled/web/src/main.js +608 -0
- data/compiled/web/src/manifest.js +112 -0
- data/compiled/web/src/opfs-wasi-core.js +109 -0
- data/compiled/web/src/opfs-wasi-file.js +8 -0
- data/compiled/web/src/reactor-runtime.js +1692 -0
- data/compiled/web/src/redactor.js +518 -0
- data/compiled/web/src/release-updates.js +48 -0
- data/compiled/web/src/replay.js +338 -0
- data/compiled/web/src/schema-recovery.js +204 -0
- data/compiled/web/src/server-request.js +68 -0
- data/compiled/web/src/sha256.js +98 -0
- data/compiled/web/src/shell.js +52 -0
- data/compiled/web/src/snapshot-store.js +38 -0
- data/compiled/web/src/sqlite-redactor.js +111 -0
- data/compiled/web/src/storage-backend.js +159 -0
- data/compiled/web/src/stream-leader.js +675 -0
- data/compiled/web/src/sync-context.js +91 -0
- data/compiled/web/src/sync-drain.js +104 -0
- data/compiled/web/src/sync-schedule.js +9 -0
- data/compiled/web/src/wasi-minimal.js +275 -0
- data/compiled/web/src/worker.js +39 -0
- data/compiled/web/vite.config.js +27 -0
- data/config/routes.rb +25 -0
- data/db/migrate/20261119000201_add_scope_to_instant_record_changes.rb +7 -0
- data/db/migrate/20261121000001_create_instant_record_sync_notices.rb +21 -0
- data/db/migrate/20261121000101_add_partition_to_instant_record_changes.rb +8 -0
- data/db/migrate/20261121230300_add_delay_line_to_instant_record_outbox.rb +10 -0
- data/exe/instant_record_mcp +6 -0
- data/frontend/LiveList.js +67 -0
- data/frontend/chord.js +79 -0
- data/frontend/file.js +65 -0
- data/frontend/files.js +738 -0
- data/frontend/focus.js +10 -0
- data/frontend/held.js +192 -0
- data/frontend/http.js +76 -0
- data/frontend/index.js +53 -0
- data/frontend/keyboard.js +56 -0
- data/frontend/live.js +298 -0
- data/frontend/notices.js +118 -0
- data/frontend/package.json +15 -0
- data/frontend/range.js +10 -0
- data/frontend/selection.js +47 -0
- data/frontend/shortcuts.js +25 -0
- data/frontend/sync-notices.js +130 -0
- data/frontend/undo.js +48 -0
- data/frontend/window.js +181 -0
- data/lib/generators/instant_record/install/install_generator.rb +18 -30
- data/lib/generators/instant_record/install/templates/instant_record.rb +34 -0
- data/lib/generators/instant_record/ios/ios_generator.rb +71 -0
- data/lib/generators/instant_record/ios/templates/App.swift.tt +35 -0
- data/lib/generators/instant_record/ios/templates/Info.plist +11 -0
- data/lib/generators/instant_record/ios/templates/LibraryEngine.swift +30 -0
- data/lib/instant_record/branch/sqlite_store.rb +142 -0
- data/lib/instant_record/branch.rb +388 -0
- data/lib/instant_record/client/notifier.rb +8 -12
- data/lib/instant_record/client/rebaser.rb +565 -0
- data/lib/instant_record/client/transport.rb +64 -42
- data/lib/instant_record/client.rb +1600 -169
- data/lib/instant_record/compiled_site.rb +85 -0
- data/lib/instant_record/compiler/app_stage.rb +212 -0
- data/lib/instant_record/compiler/ast_lowerer.rb +659 -0
- data/lib/instant_record/compiler/browser_migrations.rb +161 -0
- data/lib/instant_record/compiler/entry_builder.rb +832 -0
- data/lib/instant_record/compiler/fts5_smoke.rb +205 -0
- data/lib/instant_record/compiler/gem_vendor.rb +194 -0
- data/lib/instant_record/compiler/host_adapters.rb +506 -0
- data/lib/instant_record/compiler/kotlin_codegen.rb +209 -0
- data/lib/instant_record/compiler/manifest.rb +178 -0
- data/lib/instant_record/compiler/native_build.rb +586 -0
- data/lib/instant_record/compiler/native_contracts.rb +338 -0
- data/lib/instant_record/compiler/path_scrubber.rb +31 -0
- data/lib/instant_record/compiler/reachability_tracer.rb +1188 -0
- data/lib/instant_record/compiler/route_analyzer.rb +623 -0
- data/lib/instant_record/compiler/runner.rb +42 -0
- data/lib/instant_record/compiler/schema_lock.rb +606 -0
- data/lib/instant_record/compiler/source_pages.rb +77 -0
- data/lib/instant_record/compiler/static_linter.rb +770 -0
- data/lib/instant_record/compiler/swift_codegen.rb +341 -0
- data/lib/instant_record/compiler/toolchain.rb +219 -0
- data/lib/instant_record/compiler/wasm_build.rb +704 -0
- data/lib/instant_record/compiler/wasm_tree.rb +518 -0
- data/lib/instant_record/compiler.rb +277 -0
- data/lib/instant_record/configuration.rb +104 -0
- data/lib/instant_record/engine.rb +68 -18
- data/lib/instant_record/file_store.rb +162 -0
- data/lib/instant_record/files.rb +92 -0
- data/lib/instant_record/hlc.rb +387 -0
- data/lib/instant_record/host.rb +66 -0
- data/lib/instant_record/inertia/renderer.rb +52 -0
- data/lib/instant_record/inertia.rb +222 -0
- data/lib/instant_record/mcp/cli.rb +319 -0
- data/lib/instant_record/mcp/host_process.rb +229 -0
- data/lib/instant_record/mcp/http_exchange.rb +233 -0
- data/lib/instant_record/mcp/replica.rb +556 -0
- data/lib/instant_record/mcp/runtime.rb +406 -0
- data/lib/instant_record/mcp/server.rb +429 -0
- data/lib/instant_record/mcp.rb +47 -0
- data/lib/instant_record/native/contract.rb +367 -0
- data/lib/instant_record/native/node.rb +125 -0
- data/lib/instant_record/native/screen.rb +366 -0
- data/lib/instant_record/native/xcode_project.rb +344 -0
- data/lib/instant_record/native.rb +41 -0
- data/lib/instant_record/outbox_mutation.rb +81 -9
- data/lib/instant_record/outbox_upcaster.rb +161 -0
- data/lib/instant_record/read_tracker.rb +610 -0
- data/lib/instant_record/routing.rb +33 -0
- data/lib/instant_record/runtime_scoped.rb +4 -0
- data/lib/instant_record/shell.rb +486 -0
- data/lib/instant_record/sync_actions.rb +800 -0
- data/lib/instant_record/sync_metadata.rb +17 -2
- data/lib/instant_record/sync_notice.rb +234 -0
- data/lib/instant_record/sync_partition.rb +63 -0
- data/lib/instant_record/sync_scope.rb +300 -0
- data/lib/instant_record/sync_shape.rb +102 -0
- data/lib/instant_record/sync_window.rb +92 -15
- data/lib/instant_record/syncable.rb +313 -17
- data/lib/instant_record/testing/authoritative_server.rb +547 -0
- data/lib/instant_record/testing/chaos_simulator.rb +429 -0
- data/lib/instant_record/testing/cruby_host_bridge.rb +370 -0
- data/lib/instant_record/testing/fault_network.rb +140 -0
- data/lib/instant_record/testing/parity_suite.rb +435 -0
- data/lib/instant_record/testing/runtimes/base.rb +184 -0
- data/lib/instant_record/testing/runtimes/cruby_runtime.rb +255 -0
- data/lib/instant_record/testing/runtimes/native_runtime.rb +346 -0
- data/lib/instant_record/testing/runtimes/wasm_runtime.rb +371 -0
- data/lib/instant_record/testing/wasm_host_bridge.mjs +240 -0
- data/lib/instant_record/testing.rb +333 -0
- data/lib/instant_record/trace/redactor.rb +278 -0
- data/lib/instant_record/trace/replayer.rb +126 -0
- data/lib/instant_record/trace/schema_extractor.rb +37 -0
- data/lib/instant_record/trace.rb +22 -0
- data/lib/instant_record/version.rb +1 -1
- data/lib/instant_record.rb +107 -37
- data/lib/tasks/instant_record.rake +299 -47
- data/native/ios/InstantRecordNative/Package.swift +12 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/AppHost.swift +706 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Engine.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/FileStore.swift +1048 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/HTTPEngine.swift +36 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Icons.swift +19 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Node.swift +64 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/ScreenView.swift +481 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Screens.swift +94 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/StreamClient.swift +74 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncNotice.swift +282 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/SyncSchedule.swift +153 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Theme.swift +140 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/Transport.swift +108 -0
- data/native/ios/InstantRecordNative/Sources/InstantRecordNative/TypedContracts.swift +237 -0
- data/native/ios/README.md +341 -0
- data/sig/instant_record/inertia.rbs +47 -0
- data/sig/instant_record/native.rbs +38 -0
- metadata +194 -18
- data/lib/generators/instant_record/install/templates/database.js +0 -32
- data/lib/generators/instant_record/install/templates/index.html +0 -69
- data/lib/generators/instant_record/install/templates/rails.sw.js +0 -750
- data/lib/instant_record/local_schema.rb +0 -75
- data/lib/instant_record/pglite_compat.rb +0 -40
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
module InstantRecord
|
|
2
|
+
# InstantRecord for an Inertia page, shared by Rails and the compiled WASI/Spinel runtime.
|
|
3
|
+
#
|
|
4
|
+
# page = InstantRecord::Inertia::Page.for_request("Email/Inbox", request)
|
|
5
|
+
# page.string("member", @member)
|
|
6
|
+
# page.strings("inboxes", @inboxes)
|
|
7
|
+
# page.bool("composing", params[:compose] == "1")
|
|
8
|
+
# page.live("mail", "MailEntry") { JSON.generate(mail_props) }
|
|
9
|
+
# page.live("thread", "MailEntry") { JSON.generate(thread_props) }
|
|
10
|
+
#
|
|
11
|
+
# Props are serialized as JSON text into parallel String arrays rather than a
|
|
12
|
+
# polymorphic Ruby Hash, and lazy/live props use Ruby blocks rather than Procs
|
|
13
|
+
# inside a Hash, so Roundhouse and Spinel keep strict concrete types.
|
|
14
|
+
module Inertia
|
|
15
|
+
CHANNEL_NAME = "instant_record".freeze
|
|
16
|
+
CHANNEL_TYPE = "local".freeze
|
|
17
|
+
# A burst of changes becomes one reload. Reads are local, so this is short.
|
|
18
|
+
THROTTLE_MS = 150
|
|
19
|
+
|
|
20
|
+
class Page
|
|
21
|
+
HEX = "0123456789abcdef".freeze
|
|
22
|
+
|
|
23
|
+
attr_reader :component, :url, :version
|
|
24
|
+
|
|
25
|
+
def self.for_request(component, request, version: "")
|
|
26
|
+
if request.nil?
|
|
27
|
+
return new(component, "/", false, "", "", "", version)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
req_headers = request.headers
|
|
31
|
+
new(
|
|
32
|
+
component,
|
|
33
|
+
request.fullpath.to_s,
|
|
34
|
+
!req_headers["X-Inertia"].to_s.empty?,
|
|
35
|
+
req_headers["X-Inertia-Partial-Component"].to_s,
|
|
36
|
+
req_headers["X-Inertia-Partial-Data"].to_s,
|
|
37
|
+
req_headers["X-Inertia-Partial-Except"].to_s,
|
|
38
|
+
version
|
|
39
|
+
)
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def self.header_list(value)
|
|
43
|
+
out = []
|
|
44
|
+
parts = value.split(",")
|
|
45
|
+
i = 0
|
|
46
|
+
while i < parts.length
|
|
47
|
+
part = parts[i].strip
|
|
48
|
+
out << part unless part.empty?
|
|
49
|
+
i += 1
|
|
50
|
+
end
|
|
51
|
+
out
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def self.quote(value)
|
|
55
|
+
out = +"\""
|
|
56
|
+
value.each_char do |char|
|
|
57
|
+
code = char.ord
|
|
58
|
+
if char == "\""
|
|
59
|
+
out << "\\\""
|
|
60
|
+
elsif char == "\\"
|
|
61
|
+
out << "\\\\"
|
|
62
|
+
elsif char == "\n"
|
|
63
|
+
out << "\\n"
|
|
64
|
+
elsif char == "\r"
|
|
65
|
+
out << "\\r"
|
|
66
|
+
elsif char == "\t"
|
|
67
|
+
out << "\\t"
|
|
68
|
+
elsif code < 32
|
|
69
|
+
out << "\\u00" << HEX[code / 16] << HEX[code % 16]
|
|
70
|
+
else
|
|
71
|
+
out << char
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
out << "\""
|
|
75
|
+
out
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def self.json_strings(values)
|
|
79
|
+
out = +"["
|
|
80
|
+
i = 0
|
|
81
|
+
while i < values.length
|
|
82
|
+
out << "," if i > 0
|
|
83
|
+
out << quote(values[i])
|
|
84
|
+
i += 1
|
|
85
|
+
end
|
|
86
|
+
out << "]"
|
|
87
|
+
out
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def initialize(component, url, inertia, partial_component, partial_data, partial_except, version = "")
|
|
91
|
+
@component = component
|
|
92
|
+
@url = url
|
|
93
|
+
@version = version
|
|
94
|
+
@inertia = inertia
|
|
95
|
+
@only = Page.header_list(partial_data)
|
|
96
|
+
@except = Page.header_list(partial_except)
|
|
97
|
+
@partial = inertia && partial_component == component
|
|
98
|
+
@prop_keys = ["errors"]
|
|
99
|
+
@prop_values = ["{}"]
|
|
100
|
+
@live_keys = []
|
|
101
|
+
@live_values = []
|
|
102
|
+
end
|
|
103
|
+
|
|
104
|
+
def inertia?
|
|
105
|
+
@inertia
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def partial?
|
|
109
|
+
@partial
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def want?(name)
|
|
113
|
+
!@partial || ((@only.empty? || @only.include?(name)) && !@except.include?(name))
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def string(name, value)
|
|
117
|
+
put_json(name, Page.quote(value)) if want?(name)
|
|
118
|
+
nil
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def int(name, value)
|
|
122
|
+
put_json(name, value.to_s) if want?(name)
|
|
123
|
+
nil
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
def bool(name, value)
|
|
127
|
+
put_json(name, value ? "true" : "false") if want?(name)
|
|
128
|
+
nil
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def strings(name, values)
|
|
132
|
+
put_json(name, Page.json_strings(values)) if want?(name)
|
|
133
|
+
nil
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def raw(name, encoded)
|
|
137
|
+
put_json(name, encoded) if want?(name)
|
|
138
|
+
nil
|
|
139
|
+
end
|
|
140
|
+
|
|
141
|
+
def always_raw(name, encoded)
|
|
142
|
+
put_json(name, encoded)
|
|
143
|
+
nil
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def lazy(name)
|
|
147
|
+
put_json(name, yield) if want?(name)
|
|
148
|
+
nil
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def live(name, event)
|
|
152
|
+
register_live(name, [event])
|
|
153
|
+
put_json(name, yield) if want?(name)
|
|
154
|
+
nil
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
def live_many(name, events)
|
|
158
|
+
register_live(name, events)
|
|
159
|
+
put_json(name, yield) if want?(name)
|
|
160
|
+
nil
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Written by hand so CRuby and the compiled runtime emit identical JSON.
|
|
164
|
+
def json
|
|
165
|
+
out = +"{\"component\":"
|
|
166
|
+
out << Page.quote(@component)
|
|
167
|
+
out << ",\"props\":{"
|
|
168
|
+
i = 0
|
|
169
|
+
while i < @prop_keys.length
|
|
170
|
+
out << "," if i > 0
|
|
171
|
+
out << Page.quote(@prop_keys[i]) << ":" << @prop_values[i]
|
|
172
|
+
i += 1
|
|
173
|
+
end
|
|
174
|
+
out << "},\"url\":" << Page.quote(@url)
|
|
175
|
+
out << ",\"version\":" << Page.quote(@version)
|
|
176
|
+
unless @live_keys.empty?
|
|
177
|
+
out << ",\"liveProps\":{"
|
|
178
|
+
i = 0
|
|
179
|
+
while i < @live_keys.length
|
|
180
|
+
out << "," if i > 0
|
|
181
|
+
out << Page.quote(@live_keys[i]) << ":" << @live_values[i]
|
|
182
|
+
i += 1
|
|
183
|
+
end
|
|
184
|
+
out << "}"
|
|
185
|
+
end
|
|
186
|
+
out << "}"
|
|
187
|
+
out
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
private
|
|
191
|
+
|
|
192
|
+
def put_json(name, encoded)
|
|
193
|
+
i = @prop_keys.index(name)
|
|
194
|
+
if i.nil?
|
|
195
|
+
@prop_keys << name
|
|
196
|
+
@prop_values << encoded
|
|
197
|
+
else
|
|
198
|
+
@prop_values[i] = encoded
|
|
199
|
+
end
|
|
200
|
+
nil
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
def register_live(name, events)
|
|
204
|
+
entry = +"{\"listeners\":[{\"channel\":{\"name\":"
|
|
205
|
+
entry << Page.quote(CHANNEL_NAME)
|
|
206
|
+
entry << ",\"type\":" << Page.quote(CHANNEL_TYPE)
|
|
207
|
+
entry << "},\"events\":" << Page.json_strings(events)
|
|
208
|
+
entry << "}],\"throttle\":" << THROTTLE_MS.to_s << "}"
|
|
209
|
+
i = @live_keys.index(name)
|
|
210
|
+
if i.nil?
|
|
211
|
+
@live_keys << name
|
|
212
|
+
@live_values << entry
|
|
213
|
+
else
|
|
214
|
+
@live_values[i] = entry
|
|
215
|
+
end
|
|
216
|
+
nil
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
end
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
InstantRecord::Host.load_inertia_renderer
|
|
@@ -0,0 +1,319 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "optparse"
|
|
3
|
+
|
|
4
|
+
module InstantRecord
|
|
5
|
+
module Mcp
|
|
6
|
+
# Offline CLI + drop-in MCP stdio server launcher around the compiled native
|
|
7
|
+
# `host` binary. Invoked both by `bin/rails instant_record:mcp` and by the
|
|
8
|
+
# standalone `instant_record_mcp` executable.
|
|
9
|
+
class Cli
|
|
10
|
+
SUBCOMMANDS = %w[serve sync status models query get search outbox render action request history].freeze
|
|
11
|
+
|
|
12
|
+
def self.run(argv = ARGV, env: ENV, stdin: $stdin, stdout: $stdout, stderr: $stderr, defaults: {}, runtime_factory: nil)
|
|
13
|
+
new(env: env, stdin: stdin, stdout: stdout, stderr: stderr, defaults: defaults, runtime_factory: runtime_factory).run(argv)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def initialize(env: ENV, stdin: $stdin, stdout: $stdout, stderr: $stderr, defaults: {}, runtime_factory: nil)
|
|
17
|
+
@env = env
|
|
18
|
+
@stdin = stdin
|
|
19
|
+
@stdout = stdout
|
|
20
|
+
@stderr = stderr
|
|
21
|
+
@defaults = defaults
|
|
22
|
+
@runtime_factory = runtime_factory
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def run(argv)
|
|
26
|
+
args = argv.dup
|
|
27
|
+
options = default_options
|
|
28
|
+
|
|
29
|
+
parser = build_global_parser(options)
|
|
30
|
+
parser.order!(args)
|
|
31
|
+
|
|
32
|
+
if options[:help]
|
|
33
|
+
@stdout.puts(usage_text(parser))
|
|
34
|
+
return 0
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
command = args.first && !args.first.start_with?("-") ? args.shift : "serve"
|
|
38
|
+
unless SUBCOMMANDS.include?(command)
|
|
39
|
+
@stderr.puts("Unknown command: #{command}. Available: #{SUBCOMMANDS.join(', ')}")
|
|
40
|
+
return 2
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
cmd_options = {}
|
|
44
|
+
cmd_parser = build_command_parser(command, cmd_options, options)
|
|
45
|
+
cmd_parser.parse!(args)
|
|
46
|
+
|
|
47
|
+
if options[:help]
|
|
48
|
+
@stdout.puts(cmd_parser.to_s)
|
|
49
|
+
return 0
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
runtime = build_runtime(options)
|
|
53
|
+
begin
|
|
54
|
+
if options[:sync_on_start] && command != "sync"
|
|
55
|
+
sync_res = runtime.sync
|
|
56
|
+
@stderr.puts("[instant_record:mcp] initial sync: #{sync_res['error'] || sync_res['summary']}")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
execute_command(command, args, cmd_options, options, runtime)
|
|
60
|
+
ensure
|
|
61
|
+
runtime.close
|
|
62
|
+
end
|
|
63
|
+
rescue OptionParser::ParseError, ArgumentError => e
|
|
64
|
+
@stderr.puts("Error: #{e.message}")
|
|
65
|
+
2
|
|
66
|
+
rescue Error => e
|
|
67
|
+
@stderr.puts("Error: #{e.message}")
|
|
68
|
+
1
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
private
|
|
72
|
+
|
|
73
|
+
def default_options
|
|
74
|
+
env_headers =
|
|
75
|
+
if @env["INSTANT_RECORD_HEADERS"] && !@env["INSTANT_RECORD_HEADERS"].empty?
|
|
76
|
+
JSON.parse(@env["INSTANT_RECORD_HEADERS"])
|
|
77
|
+
else
|
|
78
|
+
{}
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
scope = @env["INSTANT_RECORD_SCOPE"] || @defaults[:scope]
|
|
82
|
+
root = @defaults[:root] || Dir.pwd
|
|
83
|
+
|
|
84
|
+
{
|
|
85
|
+
host: @env["INSTANT_RECORD_HOST"] || @defaults[:host] || Mcp.default_host_path(root),
|
|
86
|
+
db: @env["INSTANT_RECORD_DB"] || @defaults[:db] || Mcp.default_database_path(root, scope: scope),
|
|
87
|
+
endpoint: @env["INSTANT_RECORD_ENDPOINT"] || @defaults[:endpoint],
|
|
88
|
+
token: @env["INSTANT_RECORD_TOKEN"] || @defaults[:token],
|
|
89
|
+
cookie: @env["INSTANT_RECORD_COOKIE"] || @defaults[:cookie],
|
|
90
|
+
scope: scope,
|
|
91
|
+
headers: (@defaults[:headers] || {}).merge(env_headers),
|
|
92
|
+
offline: truthy?(@env["INSTANT_RECORD_OFFLINE"]) || !!@defaults[:offline],
|
|
93
|
+
sync_on_start: truthy?(@env["INSTANT_RECORD_SYNC_ON_START"]) || !!@defaults[:sync_on_start],
|
|
94
|
+
build: truthy?(@env["INSTANT_RECORD_BUILD"]) || !!@defaults[:build],
|
|
95
|
+
timeout: (@env["INSTANT_RECORD_TIMEOUT"] || @defaults[:timeout] || HttpExchange::DEFAULT_TIMEOUT).to_f,
|
|
96
|
+
models: @defaults[:models],
|
|
97
|
+
screens: @defaults[:screens] || [],
|
|
98
|
+
pretty: false,
|
|
99
|
+
help: false
|
|
100
|
+
}
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def build_global_parser(options)
|
|
104
|
+
OptionParser.new do |opts|
|
|
105
|
+
opts.banner = "Usage: instant_record_mcp [global options] [command] [command options]"
|
|
106
|
+
opts.on("--host PATH", "Path to compiled native host binary") { |v| options[:host] = v }
|
|
107
|
+
opts.on("--db PATH", "Path to local SQLite replica database") { |v| options[:db] = v }
|
|
108
|
+
opts.on("--endpoint URL", "Rails sync endpoint URL") { |v| options[:endpoint] = v }
|
|
109
|
+
opts.on("--token TOKEN", "Bearer auth token") { |v| options[:token] = v }
|
|
110
|
+
opts.on("--cookie COOKIE", "Session Cookie header value") { |v| options[:cookie] = v }
|
|
111
|
+
opts.on("--scope SCOPE", "Sync scope query parameter") { |v| options[:scope] = v }
|
|
112
|
+
opts.on("--header HEADER", "Extra HTTP header ('Name: Value'), repeatable") do |v|
|
|
113
|
+
name, value = v.split(":", 2)
|
|
114
|
+
raise ArgumentError, "Header must be 'Name: Value'" if value.nil?
|
|
115
|
+
|
|
116
|
+
options[:headers][name.strip] = value.strip
|
|
117
|
+
end
|
|
118
|
+
opts.on("--offline", "Disable network sync requests") { options[:offline] = true }
|
|
119
|
+
opts.on("--sync-on-start", "Run a sync pass before executing or serving") { options[:sync_on_start] = true }
|
|
120
|
+
opts.on("--timeout SECONDS", Float, "HTTP sync timeout in seconds") { |v| options[:timeout] = v }
|
|
121
|
+
opts.on("--build", "Compile native host if missing (when build callback is available)") { options[:build] = true }
|
|
122
|
+
opts.on("--pretty", "Pretty-print JSON output") { options[:pretty] = true }
|
|
123
|
+
opts.on("-h", "--help", "Show help") { options[:help] = true }
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def build_command_parser(command, cmd_options, global_options)
|
|
128
|
+
OptionParser.new do |opts|
|
|
129
|
+
opts.banner = "Usage: instant_record_mcp #{command} [options]"
|
|
130
|
+
case command
|
|
131
|
+
when "query"
|
|
132
|
+
cmd_options[:where] = {}
|
|
133
|
+
opts.on("-w", "--where KEY=VALUE", "Filter condition (repeatable)") { |v| assign_kv(cmd_options[:where], v) }
|
|
134
|
+
opts.on("--where-json JSON", "Filter object as JSON") { |v| cmd_options[:where].merge!(JSON.parse(v)) }
|
|
135
|
+
opts.on("--order SPEC", "Sort order (e.g. 'created_at desc')") { |v| cmd_options[:order] = v }
|
|
136
|
+
opts.on("--columns LIST", "Comma-separated column names") { |v| cmd_options[:columns] = v.split(",").map(&:strip) }
|
|
137
|
+
opts.on("--limit N", Integer, "Max rows to return") { |v| cmd_options[:limit] = v }
|
|
138
|
+
opts.on("--offset N", Integer, "Row offset") { |v| cmd_options[:offset] = v }
|
|
139
|
+
when "search"
|
|
140
|
+
cmd_options[:models] = []
|
|
141
|
+
opts.on("-m", "--model NAME", "Restrict search to model (repeatable)") { |v| cmd_options[:models] << v }
|
|
142
|
+
opts.on("--columns LIST", "Comma-separated columns to search") { |v| cmd_options[:columns] = v.split(",").map(&:strip) }
|
|
143
|
+
opts.on("--limit N", Integer, "Max matches to return") { |v| cmd_options[:limit] = v }
|
|
144
|
+
when "outbox"
|
|
145
|
+
opts.on("--limit N", Integer, "Max outbox mutations to return") { |v| cmd_options[:limit] = v }
|
|
146
|
+
when "render"
|
|
147
|
+
cmd_options[:params] = {}
|
|
148
|
+
opts.on("-p", "--param KEY=VALUE", "Screen param (repeatable)") { |v| assign_kv(cmd_options[:params], v) }
|
|
149
|
+
when "action"
|
|
150
|
+
cmd_options[:params] = {}
|
|
151
|
+
cmd_options[:args] = {}
|
|
152
|
+
cmd_options[:values] = {}
|
|
153
|
+
opts.on("-p", "--param KEY=VALUE", "Screen param (repeatable)") { |v| assign_kv(cmd_options[:params], v) }
|
|
154
|
+
opts.on("-a", "--arg KEY=VALUE", "Action arg (repeatable)") { |v| assign_kv(cmd_options[:args], v) }
|
|
155
|
+
opts.on("-v", "--value KEY=VALUE", "Form field value (repeatable)") { |v| assign_kv(cmd_options[:values], v) }
|
|
156
|
+
opts.on("--render", "Re-render the screen after performing the action") { cmd_options[:render] = true }
|
|
157
|
+
opts.on("--sync", "Sync with the server after performing the action") { cmd_options[:sync] = true }
|
|
158
|
+
when "request"
|
|
159
|
+
cmd_options[:headers] = {}
|
|
160
|
+
opts.on("-d", "--data BODY", "Raw request body") { |v| cmd_options[:body] = v }
|
|
161
|
+
opts.on("--json JSON", "JSON object body (sets Content-Type: application/json)") do |v|
|
|
162
|
+
cmd_options[:body] = JSON.generate(JSON.parse(v))
|
|
163
|
+
cmd_options[:content_type] = "application/json"
|
|
164
|
+
end
|
|
165
|
+
opts.on("--content-type TYPE", "Content-Type header") { |v| cmd_options[:content_type] = v }
|
|
166
|
+
opts.on("-H", "--header HEADER", "HTTP header ('Name: Value')") do |v|
|
|
167
|
+
name, value = v.split(":", 2)
|
|
168
|
+
raise ArgumentError, "Header must be 'Name: Value'" if value.nil?
|
|
169
|
+
|
|
170
|
+
cmd_options[:headers][name.strip] = value.strip
|
|
171
|
+
end
|
|
172
|
+
opts.on("--sync", "Sync with the server after a successful request") { cmd_options[:sync] = true }
|
|
173
|
+
when "history"
|
|
174
|
+
opts.on("--query QUERY", "/records query string to fetch from the server") { |v| cmd_options[:query] = v }
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
opts.on("--pretty", "Pretty-print JSON output") { global_options[:pretty] = true }
|
|
178
|
+
opts.on("-h", "--help", "Show help") { global_options[:help] = true }
|
|
179
|
+
end
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def build_runtime(options)
|
|
183
|
+
return @runtime_factory.call(options) if @runtime_factory
|
|
184
|
+
|
|
185
|
+
if !File.executable?(options[:host].to_s) && options[:build] && @defaults[:builder]
|
|
186
|
+
built_dir = @defaults[:builder].call
|
|
187
|
+
options[:host] = File.join(built_dir.to_s, "host")
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
Runtime.new(
|
|
191
|
+
host_binary: options[:host],
|
|
192
|
+
database_path: options[:db],
|
|
193
|
+
endpoint: options[:endpoint],
|
|
194
|
+
token: options[:token],
|
|
195
|
+
cookie: options[:cookie],
|
|
196
|
+
headers: options[:headers],
|
|
197
|
+
scope: options[:scope],
|
|
198
|
+
offline: options[:offline],
|
|
199
|
+
timeout: options[:timeout],
|
|
200
|
+
models: options[:models],
|
|
201
|
+
screens: options[:screens],
|
|
202
|
+
err: @stderr
|
|
203
|
+
)
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def execute_command(command, args, cmd_options, options, runtime)
|
|
207
|
+
if command == "serve"
|
|
208
|
+
Server.new(runtime, input: @stdin, output: @stdout, err: @stderr).run
|
|
209
|
+
return 0
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
result =
|
|
213
|
+
case command
|
|
214
|
+
when "status"
|
|
215
|
+
runtime.status
|
|
216
|
+
when "sync"
|
|
217
|
+
runtime.sync(endpoint: args.first)
|
|
218
|
+
when "models"
|
|
219
|
+
runtime.list_models
|
|
220
|
+
when "query"
|
|
221
|
+
model = args.shift or raise ArgumentError, "Usage: query <Model> [options]"
|
|
222
|
+
runtime.query_records(
|
|
223
|
+
model: model,
|
|
224
|
+
where: cmd_options[:where] || {},
|
|
225
|
+
order: cmd_options[:order],
|
|
226
|
+
columns: cmd_options[:columns],
|
|
227
|
+
limit: cmd_options[:limit] || Replica::DEFAULT_LIMIT,
|
|
228
|
+
offset: cmd_options[:offset] || 0
|
|
229
|
+
)
|
|
230
|
+
when "get"
|
|
231
|
+
model = args.shift
|
|
232
|
+
id = args.shift
|
|
233
|
+
raise ArgumentError, "Usage: get <Model> <id>" unless model && id
|
|
234
|
+
|
|
235
|
+
runtime.get_record(model: model, id: id)
|
|
236
|
+
when "search"
|
|
237
|
+
query = args.join(" ").strip
|
|
238
|
+
raise ArgumentError, "Usage: search <query> [options]" if query.empty?
|
|
239
|
+
|
|
240
|
+
runtime.search_records(
|
|
241
|
+
query: query,
|
|
242
|
+
models: cmd_options[:models]&.empty? ? nil : cmd_options[:models],
|
|
243
|
+
columns: cmd_options[:columns],
|
|
244
|
+
limit: cmd_options[:limit] || Replica::DEFAULT_LIMIT
|
|
245
|
+
)
|
|
246
|
+
when "outbox"
|
|
247
|
+
runtime.outbox(limit: cmd_options[:limit] || 100)
|
|
248
|
+
when "render"
|
|
249
|
+
screen = args.shift or raise ArgumentError, "Usage: render <screen> [--param k=v]"
|
|
250
|
+
runtime.render_screen(screen: screen, params: cmd_options[:params] || {})
|
|
251
|
+
when "action"
|
|
252
|
+
screen = args.shift
|
|
253
|
+
action = args.shift
|
|
254
|
+
raise ArgumentError, "Usage: action <screen> <action> [--param k=v] [--arg k=v] [--value k=v]" unless screen && action
|
|
255
|
+
|
|
256
|
+
runtime.perform_action(
|
|
257
|
+
screen: screen,
|
|
258
|
+
action: action,
|
|
259
|
+
params: cmd_options[:params] || {},
|
|
260
|
+
args: cmd_options[:args] || {},
|
|
261
|
+
values: cmd_options[:values] || {},
|
|
262
|
+
render: !!cmd_options[:render],
|
|
263
|
+
sync: !!cmd_options[:sync]
|
|
264
|
+
)
|
|
265
|
+
when "request"
|
|
266
|
+
method = args.shift
|
|
267
|
+
path = args.shift
|
|
268
|
+
raise ArgumentError, "Usage: request <METHOD> <path> [--data ...]" unless method && path
|
|
269
|
+
|
|
270
|
+
runtime.app_request(
|
|
271
|
+
method: method,
|
|
272
|
+
path: path,
|
|
273
|
+
body: cmd_options[:body],
|
|
274
|
+
content_type: cmd_options[:content_type],
|
|
275
|
+
headers: cmd_options[:headers] || {},
|
|
276
|
+
sync: !!cmd_options[:sync]
|
|
277
|
+
)
|
|
278
|
+
when "history"
|
|
279
|
+
key = args.shift or raise ArgumentError, "Usage: history <key> [--query ...]"
|
|
280
|
+
runtime.fetch_history(key: key, query: cmd_options[:query])
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
json = options[:pretty] ? JSON.pretty_generate(result) : JSON.generate(result)
|
|
284
|
+
@stdout.puts(json)
|
|
285
|
+
result.is_a?(Hash) && result["ok"] == false ? 1 : 0
|
|
286
|
+
end
|
|
287
|
+
|
|
288
|
+
def assign_kv(hash, pair)
|
|
289
|
+
key, value = pair.split("=", 2)
|
|
290
|
+
raise ArgumentError, "Expected KEY=VALUE, got #{pair.inspect}" if value.nil? || key.empty?
|
|
291
|
+
|
|
292
|
+
hash[key] = value
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
def truthy?(value)
|
|
296
|
+
%w[1 true yes on].include?(value.to_s.strip.downcase)
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
def usage_text(parser)
|
|
300
|
+
<<~TEXT
|
|
301
|
+
#{parser}
|
|
302
|
+
Commands (default: serve):
|
|
303
|
+
serve Start MCP JSON-RPC 2.0 server over stdio
|
|
304
|
+
sync [endpoint] Push local outbox mutations and pull latest changes
|
|
305
|
+
status Show sync cursor, pending count, models, and screens
|
|
306
|
+
models List synced models and local row counts
|
|
307
|
+
query <Model> Query synced records from the local SQLite replica
|
|
308
|
+
get <Model> <id> Fetch a single synced record by ID
|
|
309
|
+
search <query> Search synced records across models
|
|
310
|
+
outbox Inspect pending local offline mutations
|
|
311
|
+
render <screen> Render a compiled native screen as JSON
|
|
312
|
+
action <screen> <action> Perform a validated local screen action offline
|
|
313
|
+
request <METHOD> <path> Dispatch a validated local HTTP request offline
|
|
314
|
+
history <key> Inspect or fetch a windowed history page
|
|
315
|
+
TEXT
|
|
316
|
+
end
|
|
317
|
+
end
|
|
318
|
+
end
|
|
319
|
+
end
|