forked from osaurus-ai/osaurus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDatabaseTools.swift
More file actions
1178 lines (1075 loc) · 46.7 KB
/
Copy pathDatabaseTools.swift
File metadata and controls
1178 lines (1075 loc) · 46.7 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// DatabaseTools.swift
// osaurus
//
// The `db_*` tools the agent calls when `Agent.settings.dbEnabled == true`
// (spec §6.1, §6.2). One tool per typed operation plus the raw `db_execute`
// escape hatch. All tools delegate to `LocalAgentBridge.shared` so per-agent
// serialisation, `_changelog` stamping, and migration-file generation happen
// in one place.
//
// The agent ID is resolved from `ChatExecutionContext.currentAgentId`. Tool
// calls outside a chat session (no agent id in context) return an
// `unavailable` envelope — these tools are gated to opt-in agents only.
//
// Naming: tool names use underscores (`db_schema`, `db_create_table`) to
// conform to `ToolRegistry.sanitizeToolName` (which strips `.`). The
// onboarding prompt block (`OnboardingPrompt.block`) keeps the names in
// sync with what's registered here.
//
import Foundation
// MARK: - Shared helpers
/// Common helpers for `db_*` tools. Resolves the active agent, parses
/// `AgentSQLValue` arg payloads, and turns thrown `AgentDatabaseError`
/// values into structured failure envelopes.
enum DatabaseToolHelpers {
/// Resolve the agent id we should operate on, falling back to a
/// failure envelope when there's no active agent context.
static func requireAgentId(tool: String) -> ArgumentRequirement<UUID> {
guard let id = ChatExecutionContext.currentAgentId else {
return .failure(
ToolEnvelope.failure(
kind: .unavailable,
message:
"Agent DB tools require an active agent context. "
+ "This usually means the agent hasn't enabled the database "
+ "feature, or the call originated outside a chat session.",
tool: tool,
retryable: false
)
)
}
return .value(id)
}
/// Coerce an arbitrary JSON object dict into a `[column: AgentSQLValue]`
/// map. Strings, numbers, booleans, nil all round-trip. `NSNumber` is
/// classified using `objCType` so a JSON integer doesn't become a
/// double accidentally.
static func toSQLValues(_ raw: [String: Any]) -> [String: AgentSQLValue] {
var out: [String: AgentSQLValue] = [:]
out.reserveCapacity(raw.count)
for (k, v) in raw {
out[k] = toSQLValue(v)
}
return out
}
static func toSQLValue(_ value: Any) -> AgentSQLValue {
if value is NSNull { return .null }
if let n = value as? NSNumber {
// CFBoolean is also NSNumber — disambiguate by ObjC type.
let type = String(cString: n.objCType)
if type == "c" || type == "B" { return .bool(n.boolValue) }
if Double(n.int64Value) == n.doubleValue {
return .integer(n.int64Value)
}
return .double(n.doubleValue)
}
if let s = value as? String { return .text(s) }
if let b = value as? Bool { return .bool(b) }
if let i = value as? Int { return .integer(Int64(i)) }
if let d = value as? Double { return .double(d) }
if let data = value as? Data { return .blob(data) }
return .text(String(describing: value))
}
static func toSQLValueArray(_ value: Any) -> [AgentSQLValue] {
if let arr = value as? [Any] { return arr.map { toSQLValue($0) } }
return []
}
/// Render an `AgentSQLValue` back into a JSON-serialisable `Any` so
/// query results survive `ToolEnvelope.success(result:)`.
static func toJSONAny(_ value: AgentSQLValue) -> Any {
switch value {
case .null: return NSNull()
case .integer(let n): return NSNumber(value: n)
case .double(let d): return NSNumber(value: d)
case .text(let s): return s
case .bool(let b): return NSNumber(value: b)
case .blob(let data): return data.base64EncodedString()
}
}
/// Map a thrown `AgentDatabaseError` (or anything else) into a
/// structured failure envelope. `forbidden` / `invalidArgument` /
/// `tableExists` / `tableNotFound` carry their own envelopes so the
/// model can self-correct without parsing prose.
static func envelope(for error: Error, tool: String) -> String {
if let dbErr = error as? AgentDatabaseError {
switch dbErr {
case .forbidden(let m):
return ToolEnvelope.failure(
kind: .rejected,
message: m,
tool: tool,
retryable: false
)
case .invalidArgument(let m):
return ToolEnvelope.failure(
kind: .invalidArgs,
message: m,
tool: tool
)
case .tableExists(let name):
return ToolEnvelope.failure(
kind: .invalidArgs,
message:
"Table `\(name)` already exists. Call `db_schema` first to "
+ "decide whether to evolve it via `db_alter_table` instead.",
tool: tool,
retryable: false
)
case .tableNotFound(let name):
return ToolEnvelope.failure(
kind: .invalidArgs,
message: "Table `\(name)` does not exist.",
tool: tool,
retryable: false
)
case .notOpen:
return ToolEnvelope.failure(
kind: .unavailable,
message: "Agent database is not open.",
tool: tool,
retryable: true
)
default:
return ToolEnvelope.failure(
kind: .executionError,
message: dbErr.localizedDescription,
tool: tool
)
}
}
return ToolEnvelope.failure(
kind: .executionError,
message: error.localizedDescription,
tool: tool
)
}
}
// MARK: - db_schema
final class DBSchemaTool: OsaurusTool, @unchecked Sendable {
let name = "db_schema"
let description =
"Return the current schema (tables, columns, indexes, views) of your "
+ "private database. The schema snapshot is also auto-injected into "
+ "your system prompt at run-start; call this when you need to "
+ "confirm schema changes you just made."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([:]),
])
func execute(argumentsJSON: String) async throws -> String {
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
do {
let schema = try LocalAgentBridge.shared.schema(agentId: agentId)
// Canonical encoder: the schema becomes the tool-result content
// the model sees on its next turn, so byte-stable output
// protects prompt-prefix caches (see `JSONDeterminism.swift`).
let data = try JSONEncoder.osaurusCanonical().encode(schema)
// `ToolEnvelope.success(result:)` accepts `Any` and re-encodes,
// so route the Codable schema through a plain Foundation object.
let json = try JSONSerialization.jsonObject(with: data)
return ToolEnvelope.success(tool: name, result: json)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_create_table
final class DBCreateTableTool: OsaurusTool, @unchecked Sendable {
let name = "db_create_table"
let description =
"Create a new table in your private database. `purpose` is required "
+ "and surfaced to the user — make it a clear, single-sentence "
+ "description of what the table is for. Host-managed columns "
+ "(`id`, `_created_at`, `_updated_at`, `_deleted_at`) are added "
+ "automatically; do not redeclare them. Call `db_schema` first to "
+ "confirm there isn't already a table with this name."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"name": .object([
"type": .string("string"),
"description": .string("Table name. ASCII letters/digits/_, ≤ 128 chars."),
]),
"purpose": .object([
"type": .string("string"),
"description": .string(
"Why this table exists, in one sentence. Surfaced to the user."
),
]),
"columns": .object([
"type": .string("array"),
"description": .string(
"Column definitions. Each item: `{name, type, nullable?, "
+ "default?, primary_key?}`."
),
"items": .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"name": .object(["type": .string("string")]),
"type": .object([
"type": .string("string"),
"description": .string(
"Column type affinity. One of TEXT, INTEGER, REAL, BLOB, NUMERIC."
),
]),
"nullable": .object([
"type": .string("boolean"),
"description": .string("Default true."),
]),
"default": .object([
"type": .string("string"),
"description": .string(
"SQL literal used as the default, e.g. `0`, `''`, `current_timestamp`."
),
]),
"primary_key": .object([
"type": .string("boolean"),
"description": .string("Default false."),
]),
]),
"required": .array([.string("name"), .string("type")]),
]),
]),
"indexes": .object([
"type": .string("array"),
"description": .string("Optional. Indexes to create alongside the table."),
"items": .object([
"type": .string("object"),
"properties": .object([
"name": .object(["type": .string("string")]),
"columns": .object([
"type": .string("array"),
"items": .object(["type": .string("string")]),
]),
"unique": .object(["type": .string("boolean")]),
]),
"required": .array([.string("name"), .string("columns")]),
]),
]),
]),
"required": .array([.string("name"), .string("purpose"), .string("columns")]),
])
func execute(argumentsJSON: String) async throws -> String {
let argsReq = requireArgumentsDictionary(argumentsJSON, tool: name)
guard case .value(let args) = argsReq else { return argsReq.failureEnvelope ?? "" }
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
let nameReq = requireString(args, "name", expected: "table name", tool: name)
guard case .value(let tableName) = nameReq else { return nameReq.failureEnvelope ?? "" }
let purposeReq = requireString(
args,
"purpose",
expected: "single-sentence description of what this table is for",
tool: name
)
guard case .value(let purpose) = purposeReq else { return purposeReq.failureEnvelope ?? "" }
guard let columnsRaw = args["columns"] as? [[String: Any]] else {
return ToolEnvelope.failure(
kind: .invalidArgs,
message: "Missing required argument `columns` (array of column specs).",
field: "columns",
expected: "array of {name, type, nullable?, default?, primary_key?}",
tool: name
)
}
var columns: [AgentColumnSpec] = []
for col in columnsRaw {
guard let cName = col["name"] as? String, let cType = col["type"] as? String else {
return ToolEnvelope.failure(
kind: .invalidArgs,
message: "Each column needs `name` and `type`.",
tool: name
)
}
columns.append(
AgentColumnSpec(
name: cName,
type: cType,
nullable: (col["nullable"] as? Bool) ?? true,
defaultValue: col["default"] as? String,
primaryKey: (col["primary_key"] as? Bool) ?? false
)
)
}
var indexes: [AgentIndexSpec] = []
if let idxRaw = args["indexes"] as? [[String: Any]] {
for idx in idxRaw {
guard let iName = idx["name"] as? String,
let iCols = idx["columns"] as? [String]
else { continue }
indexes.append(
AgentIndexSpec(
name: iName,
columns: iCols,
unique: (idx["unique"] as? Bool) ?? false
)
)
}
}
do {
let result = try LocalAgentBridge.shared.createTable(
agentId: agentId,
name: tableName,
purpose: purpose,
columns: columns,
indexes: indexes
)
return ToolEnvelope.success(
tool: name,
result: [
"success": true,
"migration_id": result.migrationIndex,
"applied_sql": result.appliedSQL,
]
)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_alter_table
final class DBAlterTableTool: OsaurusTool, @unchecked Sendable {
let name = "db_alter_table"
let description =
"Evolve an existing table. Phase 1 supports `add_column` only — to "
+ "rename or drop columns, use `db_migrate` with the explicit "
+ "rebuild SQL. Every change writes a reversible migration to the "
+ "agent's migrations directory."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"name": .object([
"type": .string("string"),
"description": .string("Existing table name."),
]),
"add_columns": .object([
"type": .string("array"),
"description": .string("Columns to add. Same shape as `db_create_table.columns`."),
"items": .object([
"type": .string("object"),
"properties": .object([
"name": .object(["type": .string("string")]),
"type": .object(["type": .string("string")]),
"nullable": .object(["type": .string("boolean")]),
"default": .object(["type": .string("string")]),
]),
"required": .array([.string("name"), .string("type")]),
]),
]),
]),
"required": .array([.string("name"), .string("add_columns")]),
])
func execute(argumentsJSON: String) async throws -> String {
let argsReq = requireArgumentsDictionary(argumentsJSON, tool: name)
guard case .value(let args) = argsReq else { return argsReq.failureEnvelope ?? "" }
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
let nameReq = requireString(args, "name", expected: "existing table name", tool: name)
guard case .value(let tableName) = nameReq else { return nameReq.failureEnvelope ?? "" }
guard let addRaw = args["add_columns"] as? [[String: Any]], !addRaw.isEmpty else {
return ToolEnvelope.failure(
kind: .invalidArgs,
message: "`add_columns` must be a non-empty array.",
tool: name
)
}
var additions: [AgentColumnSpec] = []
for col in addRaw {
guard let cName = col["name"] as? String, let cType = col["type"] as? String else {
return ToolEnvelope.failure(
kind: .invalidArgs,
message: "Each add_column entry needs `name` and `type`.",
tool: name
)
}
additions.append(
AgentColumnSpec(
name: cName,
type: cType,
nullable: (col["nullable"] as? Bool) ?? true,
defaultValue: col["default"] as? String,
primaryKey: false
)
)
}
do {
let result = try LocalAgentBridge.shared.alterTableAddColumns(
agentId: agentId,
name: tableName,
additions: additions
)
return ToolEnvelope.success(
tool: name,
result: [
"success": true,
"migration_id": result.migrationIndex,
"applied_sql": result.appliedSQL,
]
)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_migrate
final class DBMigrateTool: OsaurusTool, @unchecked Sendable {
let name = "db_migrate"
let description =
"Run a raw SQL migration as a reversible pair. Use only for cases "
+ "the typed surface (`db_create_table`, `db_alter_table`) can't "
+ "express, like composite indexes, triggers, or virtual tables. "
+ "Provide both `up_sql` and `down_sql`; both run inside a "
+ "transaction (down is NOT executed here — it's persisted for a "
+ "future rollback). DROP TABLE, TRUNCATE, and unconstrained "
+ "DELETE are rejected."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"up_sql": .object([
"type": .string("string"),
"description": .string("SQL to apply now."),
]),
"down_sql": .object([
"type": .string("string"),
"description": .string("SQL that reverses `up_sql`. Use `-- no-op` if not invertible."),
]),
"description": .object([
"type": .string("string"),
"description": .string("Short description used for the migration filename."),
]),
]),
"required": .array([.string("up_sql"), .string("down_sql"), .string("description")]),
])
func execute(argumentsJSON: String) async throws -> String {
let argsReq = requireArgumentsDictionary(argumentsJSON, tool: name)
guard case .value(let args) = argsReq else { return argsReq.failureEnvelope ?? "" }
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
let upReq = requireString(args, "up_sql", expected: "SQL to apply", tool: name)
guard case .value(let upSQL) = upReq else { return upReq.failureEnvelope ?? "" }
let downReq = requireString(args, "down_sql", expected: "SQL that reverses up_sql", tool: name)
guard case .value(let downSQL) = downReq else { return downReq.failureEnvelope ?? "" }
let descReq = requireString(args, "description", expected: "short description", tool: name)
guard case .value(let desc) = descReq else { return descReq.failureEnvelope ?? "" }
do {
let result = try LocalAgentBridge.shared.runMigration(
agentId: agentId,
upSQL: upSQL,
downSQL: downSQL,
description: desc
)
return ToolEnvelope.success(
tool: name,
result: ["success": true, "migration_id": result.migrationIndex]
)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_insert
final class DBInsertTool: OsaurusTool, @unchecked Sendable {
let name = "db_insert"
let description =
"Insert one row into a table. The host-managed columns (`id`, "
+ "`_created_at`, `_updated_at`, `_deleted_at`) are filled in "
+ "automatically — do not include them in `row`."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"table": .object(["type": .string("string")]),
"row": .object([
"type": .string("object"),
"description": .string(
"Column → value map. Strings / numbers / booleans / null only."
),
"additionalProperties": .bool(true),
]),
]),
"required": .array([.string("table"), .string("row")]),
])
func execute(argumentsJSON: String) async throws -> String {
let argsReq = requireArgumentsDictionary(argumentsJSON, tool: name)
guard case .value(let args) = argsReq else { return argsReq.failureEnvelope ?? "" }
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
let tableReq = requireString(args, "table", expected: "table name", tool: name)
guard case .value(let table) = tableReq else { return tableReq.failureEnvelope ?? "" }
guard let rowRaw = args["row"] as? [String: Any] else {
return ToolEnvelope.failure(
kind: .invalidArgs,
message: "`row` must be a JSON object.",
tool: name
)
}
do {
let result = try LocalAgentBridge.shared.insert(
agentId: agentId,
table: table,
row: DatabaseToolHelpers.toSQLValues(rowRaw)
)
return ToolEnvelope.success(
tool: name,
result: ["id": NSNumber(value: result.rowID)]
)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_upsert
final class DBUpsertTool: OsaurusTool, @unchecked Sendable {
let name = "db_upsert"
let description =
"Insert a row, or update the existing row if one conflicts on "
+ "`key_columns`. The conflict columns must have a UNIQUE or "
+ "PRIMARY KEY constraint."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"table": .object(["type": .string("string")]),
"key_columns": .object([
"type": .string("array"),
"items": .object(["type": .string("string")]),
"description": .string("Columns to conflict on. Must be UNIQUE or PRIMARY KEY."),
]),
"row": .object([
"type": .string("object"),
"additionalProperties": .bool(true),
]),
]),
"required": .array([.string("table"), .string("key_columns"), .string("row")]),
])
func execute(argumentsJSON: String) async throws -> String {
let argsReq = requireArgumentsDictionary(argumentsJSON, tool: name)
guard case .value(let args) = argsReq else { return argsReq.failureEnvelope ?? "" }
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
let tableReq = requireString(args, "table", expected: "table name", tool: name)
guard case .value(let table) = tableReq else { return tableReq.failureEnvelope ?? "" }
let keyReq = requireStringArray(
args,
"key_columns",
expected: "non-empty array of column names",
tool: name
)
guard case .value(let keyColumns) = keyReq else { return keyReq.failureEnvelope ?? "" }
guard let rowRaw = args["row"] as? [String: Any] else {
return ToolEnvelope.failure(
kind: .invalidArgs,
message: "`row` must be a JSON object.",
tool: name
)
}
do {
let result = try LocalAgentBridge.shared.upsert(
agentId: agentId,
table: table,
keyColumns: keyColumns,
row: DatabaseToolHelpers.toSQLValues(rowRaw)
)
return ToolEnvelope.success(
tool: name,
result: ["id": NSNumber(value: result.rowID)]
)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_update
final class DBUpdateTool: OsaurusTool, @unchecked Sendable {
let name = "db_update"
let description =
"Update rows matched by `where`. `_updated_at` is refreshed "
+ "automatically. By default soft-deleted rows are skipped — pass "
+ "`include_deleted=true` to update them too (rare)."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"table": .object(["type": .string("string")]),
"set": .object([
"type": .string("object"),
"description": .string("Column → new value map."),
"additionalProperties": .bool(true),
]),
"where": .object([
"type": .string("object"),
"description": .string("Column → value equality predicate. ANDed together."),
"additionalProperties": .bool(true),
]),
"include_deleted": .object([
"type": .string("boolean"),
"description": .string("Default false."),
]),
]),
"required": .array([.string("table"), .string("set"), .string("where")]),
])
func execute(argumentsJSON: String) async throws -> String {
let argsReq = requireArgumentsDictionary(argumentsJSON, tool: name)
guard case .value(let args) = argsReq else { return argsReq.failureEnvelope ?? "" }
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
let tableReq = requireString(args, "table", expected: "table name", tool: name)
guard case .value(let table) = tableReq else { return tableReq.failureEnvelope ?? "" }
guard let setRaw = args["set"] as? [String: Any], !setRaw.isEmpty else {
return ToolEnvelope.failure(
kind: .invalidArgs,
message: "`set` must be a non-empty JSON object.",
tool: name
)
}
guard let whereRaw = args["where"] as? [String: Any] else {
return ToolEnvelope.failure(
kind: .invalidArgs,
message: "`where` must be a JSON object.",
tool: name
)
}
let includeDeleted = (args["include_deleted"] as? Bool) ?? false
do {
let result = try LocalAgentBridge.shared.update(
agentId: agentId,
table: table,
set: DatabaseToolHelpers.toSQLValues(setRaw),
whereClause: DatabaseToolHelpers.toSQLValues(whereRaw),
includeDeleted: includeDeleted
)
return ToolEnvelope.success(
tool: name,
result: ["rows_affected": result.rowsAffected]
)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_delete (soft)
final class DBDeleteTool: OsaurusTool, @unchecked Sendable {
let name = "db_delete"
let description =
"Soft-delete rows matched by `where`. Sets `_deleted_at` to now. "
+ "Recoverable via `db_restore`. Do not hard-delete from agent "
+ "code — if the user explicitly asks to wipe data, use "
+ "`db_execute` so the audit trail flags it."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"table": .object(["type": .string("string")]),
"where": .object([
"type": .string("object"),
"additionalProperties": .bool(true),
]),
]),
"required": .array([.string("table"), .string("where")]),
])
func execute(argumentsJSON: String) async throws -> String {
let argsReq = requireArgumentsDictionary(argumentsJSON, tool: name)
guard case .value(let args) = argsReq else { return argsReq.failureEnvelope ?? "" }
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
let tableReq = requireString(args, "table", expected: "table name", tool: name)
guard case .value(let table) = tableReq else { return tableReq.failureEnvelope ?? "" }
guard let whereRaw = args["where"] as? [String: Any], !whereRaw.isEmpty else {
return ToolEnvelope.failure(
kind: .invalidArgs,
message:
"`where` must be a non-empty JSON object. Soft-deleting every "
+ "row of a table is rarely intended — pass an explicit predicate.",
tool: name
)
}
do {
let result = try LocalAgentBridge.shared.softDelete(
agentId: agentId,
table: table,
whereClause: DatabaseToolHelpers.toSQLValues(whereRaw)
)
return ToolEnvelope.success(
tool: name,
result: ["rows_affected": result.rowsAffected]
)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_restore
final class DBRestoreTool: OsaurusTool, @unchecked Sendable {
let name = "db_restore"
let description =
"Un-soft-delete rows matched by `where` (sets `_deleted_at = NULL`)."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"table": .object(["type": .string("string")]),
"where": .object([
"type": .string("object"),
"additionalProperties": .bool(true),
]),
]),
"required": .array([.string("table"), .string("where")]),
])
func execute(argumentsJSON: String) async throws -> String {
let argsReq = requireArgumentsDictionary(argumentsJSON, tool: name)
guard case .value(let args) = argsReq else { return argsReq.failureEnvelope ?? "" }
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
let tableReq = requireString(args, "table", expected: "table name", tool: name)
guard case .value(let table) = tableReq else { return tableReq.failureEnvelope ?? "" }
guard let whereRaw = args["where"] as? [String: Any], !whereRaw.isEmpty else {
return ToolEnvelope.failure(
kind: .invalidArgs,
message: "`where` must be a non-empty JSON object.",
tool: name
)
}
do {
let result = try LocalAgentBridge.shared.restore(
agentId: agentId,
table: table,
whereClause: DatabaseToolHelpers.toSQLValues(whereRaw)
)
return ToolEnvelope.success(
tool: name,
result: ["rows_affected": result.rowsAffected]
)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_query
final class DBQueryTool: OsaurusTool, @unchecked Sendable {
let name = "db_query"
let description =
"Run a read-only SQL query. Returns at most 1000 rows; `truncated` "
+ "is true when there were more. Queries auto-filter "
+ "`_deleted_at IS NULL` on user tables unless you pass "
+ "`include_deleted=true` explicitly in the WHERE clause."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"sql": .object([
"type": .string("string"),
"description": .string(
"SELECT statement. May reference `?1`, `?2`, … bound from `params`."
),
]),
"params": .object([
"type": .string("array"),
"description": .string("Positional bind parameters (optional)."),
"items": .object(["type": .string("string")]),
]),
]),
"required": .array([.string("sql")]),
])
func execute(argumentsJSON: String) async throws -> String {
let argsReq = requireArgumentsDictionary(argumentsJSON, tool: name)
guard case .value(let args) = argsReq else { return argsReq.failureEnvelope ?? "" }
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
let sqlReq = requireString(args, "sql", expected: "SELECT statement", tool: name)
guard case .value(let sql) = sqlReq else { return sqlReq.failureEnvelope ?? "" }
let params: [AgentSQLValue]
if let raw = args["params"] {
params = DatabaseToolHelpers.toSQLValueArray(raw)
} else {
params = []
}
do {
let result = try LocalAgentBridge.shared.query(
agentId: agentId,
sql: sql,
params: params
)
let rows: [[Any]] = result.rows.map { row in
row.map { DatabaseToolHelpers.toJSONAny($0) }
}
return ToolEnvelope.success(
tool: name,
result: [
"columns": result.columns,
"rows": rows,
"truncated": result.truncated,
]
)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_execute
final class DBExecuteTool: OsaurusTool, @unchecked Sendable {
let name = "db_execute"
let description =
"Raw SQL escape hatch. Use only when the typed surface "
+ "(`db_insert`/`db_update`/`db_query`/etc.) can't express what "
+ "you need. Logged distinctly in the activity log with "
+ "`op='raw'`. DROP TABLE, TRUNCATE, DROP DATABASE, and "
+ "unconstrained DELETE are rejected."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"sql": .object([
"type": .string("string"),
"description": .string("Statement to execute."),
]),
"params": .object([
"type": .string("array"),
"items": .object(["type": .string("string")]),
]),
]),
"required": .array([.string("sql")]),
])
func execute(argumentsJSON: String) async throws -> String {
let argsReq = requireArgumentsDictionary(argumentsJSON, tool: name)
guard case .value(let args) = argsReq else { return argsReq.failureEnvelope ?? "" }
let agentReq = DatabaseToolHelpers.requireAgentId(tool: name)
guard case .value(let agentId) = agentReq else { return agentReq.failureEnvelope ?? "" }
let sqlReq = requireString(args, "sql", expected: "SQL statement", tool: name)
guard case .value(let sql) = sqlReq else { return sqlReq.failureEnvelope ?? "" }
let params: [AgentSQLValue]
if let raw = args["params"] {
params = DatabaseToolHelpers.toSQLValueArray(raw)
} else {
params = []
}
do {
let result = try LocalAgentBridge.shared.execute(
agentId: agentId,
sql: sql,
params: params
)
var resultDict: [String: Any] = ["rows_affected": result.rowsAffected]
if let warning = result.warning {
resultDict["warning"] = warning
}
return ToolEnvelope.success(
tool: name,
result: resultDict,
warnings: result.warning.map { [$0] }
)
} catch {
return DatabaseToolHelpers.envelope(for: error, tool: name)
}
}
}
// MARK: - db_define_view
/// Phase 2 saved-view tools (spec §6.3). A saved view is a named
/// SELECT/CTE the agent reuses across runs and the UI surfaces on
/// the Views and Home tabs. Definitions go through
/// `LocalAgentBridge.defineView` so the `_changelog` audit entry is
/// stamped the same way as any other write.
final class DBDefineViewTool: OsaurusTool, @unchecked Sendable {
let name = "db_define_view"
let description =
"Save (or redefine) a named SQL view the user and you can re-run "
+ "later. View bodies must be SELECT or WITH ... SELECT only — "
+ "if you need to write data, use `db_insert` / `db_update`. "
+ "`render_hint` controls how the UI plots the result; use one "
+ "of `table`, `bar`, `line`, `pie`, `number`."
let parameters: JSONValue? = .object([
"type": .string("object"),
"additionalProperties": .bool(false),
"properties": .object([
"name": .object([
"type": .string("string"),
"description": .string("View name. ASCII letters/digits/_."),
]),
"sql": .object([
"type": .string("string"),
"description": .string(
"SELECT or WITH-CTE statement. Re-runnable; bind params "
+ "are not supported on views (Phase 2 limitation)."
),
]),
"render_hint": .object([
"type": .string("string"),
"description": .string(
"Suggested visualization: `table`, `bar`, `line`, `pie`, `number`."
),
]),
"refresh": .object([
"type": .string("string"),
"description": .string(
"Refresh policy: `manual`, `on_run`, or a duration like `5m`."
),
]),
"description": .object([
"type": .string("string"),
"description": .string("Optional one-liner describing what the view shows."),
]),