No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

1043 líneas
45KB

  1. <%
  2. ' ApiPortabilityController: canonical export and import (spec FR-018, NFR-007, NFR-009, AC-06, AC-14).
  3. '
  4. ' POST /api/v1/exports Export {"namespace_ids":[...]} (optional; default: every namespace the caller can read)
  5. ' POST /api/v1/imports Import the document returned by /exports, unchanged (max 8 MiB)
  6. '
  7. ' Export document (schema_version 1):
  8. ' {"format":"agent-memory-export","schema_version":1,"exported_at":"...","data":{...},"counts":{...},
  9. ' "integrity":{"algorithm":"sha256","digest":"<sha256 of the exact text of data>"}}
  10. ' data.namespaces [{path}], data.memories [{id, namespace, type, lifecycle_status, valid_from, valid_until, created_by,
  11. ' created_at, updated_at, current_revision, revisions[...], verifications[...]}], data.documents [{id, namespace,
  12. ' source_uri, format, title, current_revision, created_by, created_at, updated_at, revisions[{..., chunks[...]}]}],
  13. ' data.evidence_links [{id, owner_kind:"memory", owner_id, owner_revision, target_kind, target_id, target_revision,
  14. ' role, excerpt, created_by, created_at}].
  15. ' Canonical records keep their ids, revision numbers, timestamps and principal ids (provenance). Derived data
  16. ' (lexical and semantic indexes, content hashes) is rebuilt on import. Not exported in version 1: sessions,
  17. ' experiences, learning candidates, feedback, retrieval telemetry, audit events. Evidence whose target the caller
  18. ' cannot read is left out (counted in counts.evidence_links_omitted).
  19. '
  20. ' Import needs admin on every target namespace (matched by path in the caller's tenant; they must exist). Any id that
  21. ' already exists makes the whole import fail (409); any invalid item fails it with 422; nothing is written then.
  22. Const PORT_FORMAT = "agent-memory-export"
  23. Const PORT_SCHEMA = 1
  24. Const PORT_MAX_IMPORT = 8388608
  25. ' List of strings joined with a separator, on the native G3STRINGBUILDER (string concatenation in a loop is
  26. ' quadratic). The export runs all its queries before building, so no builder is held across G3DB calls.
  27. Class Port_Buf
  28. Private m_sb, m_n, m_sep
  29. Private Sub Class_Initialize()
  30. Set m_sb = Server.CreateObject("G3STRINGBUILDER")
  31. m_n = 0
  32. m_sep = ","
  33. End Sub
  34. Private Sub Class_Terminate()
  35. Set m_sb = Nothing
  36. End Sub
  37. ' Separator put between items (default ",")
  38. Public Property Let Sep(v)
  39. m_sep = v
  40. End Property
  41. Public Sub Add(s)
  42. If m_n > 0 Then
  43. If Len(m_sep) > 0 Then
  44. m_sb.Append m_sep
  45. End If
  46. End If
  47. m_sb.Append s
  48. m_n = m_n + 1
  49. End Sub
  50. Public Property Get Count()
  51. Count = m_n
  52. End Property
  53. Public Function Text()
  54. Text = m_sb.ToString()
  55. End Function
  56. End Class
  57. Class ApiPortabilityController_Class
  58. ' import state
  59. Private m_err, m_nsPaths(), m_nsIds(), m_nsN, m_types, m_memIds, m_cnt()
  60. Public Property Get useLayout()
  61. useLayout = False
  62. End Property
  63. Public Property Let useLayout(v)
  64. End Property
  65. Public Property Get Title()
  66. Title = "Portability API"
  67. End Property
  68. '---------------------------------------------------------------
  69. ' POST /api/v1/exports
  70. '---------------------------------------------------------------
  71. Public Sub Export()
  72. Dim body, input, allowed, requested, arr, i, ok, rs, nsRows, memRows, revRows, verRows, docRows, drRows, chRows, evRows
  73. Dim data, tq, nsList, m, p, v, d, dr, ch, item, sub1, sub2, nMem, nRev, nVer, nDoc, nDr, nCh, nEv, nEvAll, digest, out, ts
  74. If Not Api_Authenticate() Then Exit Sub
  75. body = Api_ReadBody()
  76. If IsEmpty(body) Then Exit Sub
  77. Set input = JsonLite_Parse(body)
  78. allowed = Api_AllowedNamespaceIds("read")
  79. If input.HasRaw("namespace_ids") Then
  80. arr = JsonLite_ParseStringArray(input.Raw("namespace_ids"))
  81. If IsNull(arr) Then
  82. Api_Invalid Array("namespace_ids must be an array of namespace ids.")
  83. Exit Sub
  84. End If
  85. requested = ""
  86. For i = 0 To UBound(arr)
  87. If Not Api_IsId(arr(i)) Then
  88. Api_Invalid Array("namespace_ids must be an array of namespace ids.")
  89. Exit Sub
  90. End If
  91. ' only ids the caller can read; others are silently left out (no existence disclosure)
  92. If InStr("," & allowed & ",", ",'" & LCase(arr(i)) & "',") > 0 Then
  93. If Len(requested) > 0 Then
  94. requested = requested & ","
  95. End If
  96. requested = requested & "'" & LCase(arr(i)) & "'"
  97. End If
  98. Next
  99. allowed = requested
  100. ElseIf input.Exists("namespace_ids") Then
  101. If Not IsNull(input.Item("namespace_ids")) Then
  102. Api_Invalid Array("namespace_ids must be an array of namespace ids.")
  103. Exit Sub
  104. End If
  105. End If
  106. ts = Api_Now()
  107. Set data = New Port_Buf
  108. data.Sep = "" ' items below carry their own commas
  109. nMem = 0 : nRev = 0 : nVer = 0 : nDoc = 0 : nDr = 0 : nCh = 0 : nEv = 0 : nEvAll = 0
  110. nsRows = Empty : memRows = Empty : revRows = Empty : verRows = Empty : docRows = Empty : drRows = Empty : chRows = Empty : evRows = Empty
  111. If Len(allowed) > 0 Then
  112. tq = "'" & Api_TenantId & "'" ' authenticated tenant id (a UUID), safe to inline
  113. nsRows = Rows("SELECT id, path FROM namespaces WHERE tenant_id = " & tq & " AND id IN (" & allowed & ") ORDER BY path")
  114. memRows = Rows("SELECT id, namespace_id, type, lifecycle_status, valid_from, valid_until, creator_principal_id, created_at, updated_at, current_revision " & _
  115. "FROM memories WHERE tenant_id = " & tq & " AND namespace_id IN (" & allowed & ") ORDER BY id")
  116. revRows = Rows("SELECT r.memory_id, r.id, r.revision, r.title, r.summary, r.content, r.attributes, r.tags, r.confidence, r.importance, r.change_reason, " & _
  117. "r.verification_status, r.created_by, r.created_at FROM memory_revisions r JOIN memories m ON m.id = r.memory_id " & _
  118. "WHERE m.tenant_id = " & tq & " AND m.namespace_id IN (" & allowed & ") ORDER BY r.memory_id, r.revision")
  119. verRows = Rows("SELECT v.memory_id, v.id, v.revision, v.method, v.result, v.notes, v.reviewer_principal_id, v.created_at FROM verifications v " & _
  120. "JOIN memories m ON m.id = v.memory_id WHERE m.tenant_id = " & tq & " AND m.namespace_id IN (" & allowed & ") ORDER BY v.memory_id, v.created_at, v.id")
  121. docRows = Rows("SELECT id, namespace_id, source_uri, format, title, current_revision, created_by, created_at, updated_at FROM documents " & _
  122. "WHERE tenant_id = " & tq & " AND namespace_id IN (" & allowed & ") ORDER BY id")
  123. drRows = Rows("SELECT r.document_id, r.id, r.revision, r.content, r.char_count, r.chunk_count, r.created_by, r.created_at FROM document_revisions r " & _
  124. "JOIN documents d ON d.id = r.document_id WHERE d.tenant_id = " & tq & " AND d.namespace_id IN (" & allowed & ") ORDER BY r.document_id, r.revision")
  125. chRows = Rows("SELECT c.document_id, c.document_revision, c.id, c.ordinal, c.content, c.heading_path, c.start_offset, c.end_offset, c.token_estimate, c.created_at " & _
  126. "FROM chunks c JOIN documents d ON d.id = c.document_id WHERE d.tenant_id = " & tq & " AND d.namespace_id IN (" & allowed & ") " & _
  127. "ORDER BY c.document_id, c.document_revision, c.ordinal")
  128. ' evidence of exported memories whose target the caller can read
  129. evRows = Rows("SELECT id, owner_id, owner_revision, target_kind, target_id, target_revision, role, excerpt, created_by, created_at FROM evidence_links e " & _
  130. "WHERE e.tenant_id = " & tq & " AND e.owner_kind = 'memory' AND e.owner_id IN (SELECT id FROM memories WHERE tenant_id = " & tq & " AND namespace_id IN (" & allowed & ")) AND (" & _
  131. "(e.target_kind = 'memory' AND e.target_id IN (SELECT id FROM memories WHERE tenant_id = " & tq & " AND namespace_id IN (" & allowed & "))) OR " & _
  132. "(e.target_kind = 'document' AND e.target_id IN (SELECT id FROM documents WHERE tenant_id = " & tq & " AND namespace_id IN (" & allowed & "))) OR " & _
  133. "(e.target_kind = 'chunk' AND e.target_id IN (SELECT c.id FROM chunks c JOIN documents d ON d.id = c.document_id WHERE d.tenant_id = " & tq & " AND d.namespace_id IN (" & allowed & "))) OR " & _
  134. "(e.target_kind = 'experience' AND e.target_id IN (SELECT id FROM experiences WHERE tenant_id = " & tq & " AND namespace_id IN (" & allowed & ")))) ORDER BY e.created_at, e.id")
  135. Set rs = DAL().Query("SELECT COUNT(*) FROM evidence_links WHERE tenant_id = " & tq & " AND owner_kind = 'memory' AND owner_id IN (SELECT id FROM memories WHERE tenant_id = " & tq & " AND namespace_id IN (" & allowed & "))", Empty)
  136. nEvAll = CLng(rs(0))
  137. rs.Close
  138. End If
  139. ' namespaces
  140. Set nsList = New Port_Buf
  141. If Not IsEmpty(nsRows) Then
  142. For i = 0 To UBound(nsRows, 2)
  143. nsList.Add "{""path"":" & JsonLite_Quote("" & nsRows(1, i)) & "}"
  144. Next
  145. End If
  146. data.Add "{""namespaces"":[" & nsList.Text() & "],""memories"":["
  147. ' memories with revisions and verifications (all three ordered by memory id: merge in one pass)
  148. p = 0
  149. v = 0
  150. If Not IsEmpty(memRows) Then
  151. For m = 0 To UBound(memRows, 2)
  152. Set sub1 = New Port_Buf
  153. Do While p <= RowsMax(revRows)
  154. If "" & revRows(0, p) <> "" & memRows(0, m) Then
  155. Exit Do
  156. End If
  157. sub1.Add "{""id"":" & JsonLite_Quote("" & revRows(1, p)) & ",""revision"":" & CLng(revRows(2, p)) & ",""title"":" & JsonLite_Quote("" & revRows(3, p)) & _
  158. ",""summary"":" & JStr(revRows(4, p)) & ",""content"":" & JsonLite_Quote("" & revRows(5, p)) & ",""attributes"":" & JRaw(revRows(6, p)) & _
  159. ",""tags"":" & JRaw(revRows(7, p)) & ",""confidence"":" & JNum(revRows(8, p)) & ",""importance"":" & JNum(revRows(9, p)) & _
  160. ",""change_reason"":" & JStr(revRows(10, p)) & ",""verification_status"":" & JsonLite_Quote("" & revRows(11, p)) & _
  161. ",""created_by"":" & JsonLite_Quote("" & revRows(12, p)) & ",""created_at"":" & JsonLite_Quote("" & revRows(13, p)) & "}"
  162. nRev = nRev + 1
  163. p = p + 1
  164. Loop
  165. Set sub2 = New Port_Buf
  166. Do While v <= RowsMax(verRows)
  167. If "" & verRows(0, v) <> "" & memRows(0, m) Then
  168. Exit Do
  169. End If
  170. sub2.Add "{""id"":" & JsonLite_Quote("" & verRows(1, v)) & ",""revision"":" & CLng(verRows(2, v)) & ",""method"":" & JsonLite_Quote("" & verRows(3, v)) & _
  171. ",""result"":" & JsonLite_Quote("" & verRows(4, v)) & ",""notes"":" & JStr(verRows(5, v)) & ",""reviewer"":" & JsonLite_Quote("" & verRows(6, v)) & _
  172. ",""created_at"":" & JsonLite_Quote("" & verRows(7, v)) & "}"
  173. nVer = nVer + 1
  174. v = v + 1
  175. Loop
  176. item = "{""id"":" & JsonLite_Quote("" & memRows(0, m)) & ",""namespace"":" & JsonLite_Quote(NsPath(nsRows, "" & memRows(1, m))) & _
  177. ",""type"":" & JsonLite_Quote("" & memRows(2, m)) & ",""lifecycle_status"":" & JsonLite_Quote("" & memRows(3, m)) & _
  178. ",""valid_from"":" & JStr(memRows(4, m)) & ",""valid_until"":" & JStr(memRows(5, m)) & ",""created_by"":" & JsonLite_Quote("" & memRows(6, m)) & _
  179. ",""created_at"":" & JsonLite_Quote("" & memRows(7, m)) & ",""updated_at"":" & JsonLite_Quote("" & memRows(8, m)) & _
  180. ",""current_revision"":" & CLng(memRows(9, m)) & ",""revisions"":[" & sub1.Text() & "],""verifications"":[" & sub2.Text() & "]}"
  181. If m > 0 Then
  182. item = "," & item
  183. End If
  184. data.Add item
  185. nMem = nMem + 1
  186. Next
  187. End If
  188. data.Add "],""documents"":["
  189. ' documents with revisions and their chunks
  190. dr = 0
  191. ch = 0
  192. If Not IsEmpty(docRows) Then
  193. For d = 0 To UBound(docRows, 2)
  194. Set sub1 = New Port_Buf
  195. Do While dr <= RowsMax(drRows)
  196. If "" & drRows(0, dr) <> "" & docRows(0, d) Then
  197. Exit Do
  198. End If
  199. Set sub2 = New Port_Buf
  200. Do While ch <= RowsMax(chRows)
  201. If "" & chRows(0, ch) <> "" & docRows(0, d) Then
  202. Exit Do
  203. End If
  204. If CLng(chRows(1, ch)) <> CLng(drRows(2, dr)) Then
  205. Exit Do
  206. End If
  207. sub2.Add "{""id"":" & JsonLite_Quote("" & chRows(2, ch)) & ",""ordinal"":" & CLng(chRows(3, ch)) & ",""content"":" & JsonLite_Quote("" & chRows(4, ch)) & _
  208. ",""heading_path"":" & JStr(chRows(5, ch)) & ",""start_offset"":" & CLng(chRows(6, ch)) & ",""end_offset"":" & CLng(chRows(7, ch)) & _
  209. ",""token_estimate"":" & CLng(chRows(8, ch)) & ",""created_at"":" & JsonLite_Quote("" & chRows(9, ch)) & "}"
  210. nCh = nCh + 1
  211. ch = ch + 1
  212. Loop
  213. sub1.Add "{""id"":" & JsonLite_Quote("" & drRows(1, dr)) & ",""revision"":" & CLng(drRows(2, dr)) & ",""content"":" & JsonLite_Quote("" & drRows(3, dr)) & _
  214. ",""created_by"":" & JsonLite_Quote("" & drRows(6, dr)) & ",""created_at"":" & JsonLite_Quote("" & drRows(7, dr)) & ",""chunks"":[" & sub2.Text() & "]}"
  215. nDr = nDr + 1
  216. dr = dr + 1
  217. Loop
  218. item = "{""id"":" & JsonLite_Quote("" & docRows(0, d)) & ",""namespace"":" & JsonLite_Quote(NsPath(nsRows, "" & docRows(1, d))) & _
  219. ",""source_uri"":" & JStr(docRows(2, d)) & ",""format"":" & JsonLite_Quote("" & docRows(3, d)) & ",""title"":" & JsonLite_Quote("" & docRows(4, d)) & _
  220. ",""current_revision"":" & CLng(docRows(5, d)) & ",""created_by"":" & JsonLite_Quote("" & docRows(6, d)) & ",""created_at"":" & JsonLite_Quote("" & docRows(7, d)) & _
  221. ",""updated_at"":" & JsonLite_Quote("" & docRows(8, d)) & ",""revisions"":[" & sub1.Text() & "]}"
  222. If d > 0 Then
  223. item = "," & item
  224. End If
  225. data.Add item
  226. nDoc = nDoc + 1
  227. Next
  228. End If
  229. data.Add "],""evidence_links"":["
  230. If Not IsEmpty(evRows) Then
  231. For i = 0 To UBound(evRows, 2)
  232. item = "{""id"":" & JsonLite_Quote("" & evRows(0, i)) & ",""owner_kind"":""memory"",""owner_id"":" & JsonLite_Quote("" & evRows(1, i)) & _
  233. ",""owner_revision"":" & JNum(evRows(2, i)) & ",""target_kind"":" & JsonLite_Quote("" & evRows(3, i)) & ",""target_id"":" & JsonLite_Quote("" & evRows(4, i)) & _
  234. ",""target_revision"":" & JNum(evRows(5, i)) & ",""role"":" & JsonLite_Quote("" & evRows(6, i)) & ",""excerpt"":" & JStr(evRows(7, i)) & _
  235. ",""created_by"":" & JsonLite_Quote("" & evRows(8, i)) & ",""created_at"":" & JsonLite_Quote("" & evRows(9, i)) & "}"
  236. If i > 0 Then
  237. item = "," & item
  238. End If
  239. data.Add item
  240. nEv = nEv + 1
  241. Next
  242. End If
  243. data.Add "]}"
  244. out = data.Text()
  245. digest = Api_Sha256(out)
  246. Api_Audit "export.create", "export", "", "ok"
  247. Response.AddHeader "Content-Disposition", "attachment; filename=""agent-memory-export.json"""
  248. Api_Send 200, "{""format"":""" & PORT_FORMAT & """,""schema_version"":" & PORT_SCHEMA & ",""exported_at"":" & JsonLite_Quote(ts) & _
  249. ",""data"":" & out & ",""counts"":{""namespaces"":" & (RowsMax(nsRows) + 1) & ",""memories"":" & nMem & ",""memory_revisions"":" & nRev & _
  250. ",""verifications"":" & nVer & ",""documents"":" & nDoc & ",""document_revisions"":" & nDr & ",""chunks"":" & nCh & ",""evidence_links"":" & nEv & _
  251. ",""evidence_links_omitted"":" & (nEvAll - nEv) & "},""integrity"":{""algorithm"":""sha256"",""digest"":""" & digest & """}}"
  252. End Sub
  253. '---------------------------------------------------------------
  254. ' POST /api/v1/imports
  255. '---------------------------------------------------------------
  256. Public Sub Import()
  257. Dim body, input, dataRaw, integ, data, items, i, k, rs
  258. If Not Api_Authenticate() Then Exit Sub
  259. body = Api_ReadBodyMax(PORT_MAX_IMPORT)
  260. If IsEmpty(body) Then Exit Sub
  261. Set input = JsonLite_Parse(body)
  262. ' envelope and integrity
  263. If "" & input.Item("format") <> PORT_FORMAT Then
  264. Api_Invalid Array("format must be """ & PORT_FORMAT & """ (a document produced by POST /exports).")
  265. Exit Sub
  266. End If
  267. If VarType(input.Item("schema_version")) <> vbDouble Then
  268. Api_Invalid Array("schema_version is required.")
  269. Exit Sub
  270. End If
  271. If input.Item("schema_version") <> PORT_SCHEMA Then
  272. Api_Error 422, "unsupported_schema_version", "schema_version " & input.Item("schema_version") & " is not supported; this server reads version " & PORT_SCHEMA & "."
  273. Exit Sub
  274. End If
  275. If Not input.HasRaw("data") Then
  276. Api_Invalid Array("data is required.")
  277. Exit Sub
  278. End If
  279. dataRaw = input.Raw("data")
  280. If Not input.HasRaw("integrity") Then
  281. Api_Invalid Array("integrity is required.")
  282. Exit Sub
  283. End If
  284. Set integ = JsonLite_Parse(input.Raw("integrity"))
  285. If integ Is Nothing Then
  286. Api_Invalid Array("integrity must be an object.")
  287. Exit Sub
  288. End If
  289. If LCase("" & integ.Item("algorithm")) <> "sha256" Then
  290. Api_Invalid Array("integrity.algorithm must be sha256.")
  291. Exit Sub
  292. End If
  293. If LCase("" & integ.Item("digest")) <> Api_Sha256(dataRaw) Then
  294. Api_Error 422, "integrity_mismatch", "The sha256 digest of data does not match integrity.digest. The export was changed or reformatted after it was produced."
  295. Exit Sub
  296. End If
  297. Set data = JsonLite_Parse(dataRaw)
  298. If data Is Nothing Then
  299. Api_Invalid Array("data must be an object.")
  300. Exit Sub
  301. End If
  302. ReDim m_cnt(7)
  303. For i = 0 To 7
  304. m_cnt(i) = 0
  305. Next
  306. m_err = ""
  307. m_memIds = ","
  308. m_nsN = 0
  309. ReDim m_nsPaths(15)
  310. ReDim m_nsIds(15)
  311. m_types = ","
  312. Set rs = DAL().Query("SELECT code FROM memory_types", Empty)
  313. Do While Not rs.EOF
  314. m_types = m_types & LCase("" & rs(0)) & ","
  315. rs.MoveNext
  316. Loop
  317. rs.Close
  318. ' namespaces: every path must exist in the caller's tenant with admin permission
  319. items = ArrayOf(data, "namespaces")
  320. If Len(m_err) = 0 Then
  321. For i = 0 To UBound(items)
  322. ResolveNamespace items(i)
  323. If Len(m_err) > 0 Then
  324. Exit For
  325. End If
  326. Next
  327. End If
  328. If Len(m_err) > 0 Then
  329. Api_Invalid Array(m_err)
  330. Exit Sub
  331. End If
  332. On Error Resume Next
  333. DAL().BeginTransaction
  334. If Err.Number <> 0 Then
  335. k = Err.Number : Api_NoteDbError
  336. Api_DbFailed k, ""
  337. Exit Sub
  338. End If
  339. On Error GoTo 0
  340. items = ArrayOf(data, "memories")
  341. If Len(m_err) = 0 Then
  342. For i = 0 To UBound(items)
  343. ImportMemory items(i), i
  344. If Len(m_err) > 0 Then
  345. Exit For
  346. End If
  347. Next
  348. End If
  349. If Len(m_err) = 0 Then
  350. items = ArrayOf(data, "documents")
  351. End If
  352. If Len(m_err) = 0 Then
  353. For i = 0 To UBound(items)
  354. ImportDocument items(i), i
  355. If Len(m_err) > 0 Then
  356. Exit For
  357. End If
  358. Next
  359. End If
  360. If Len(m_err) = 0 Then
  361. items = ArrayOf(data, "evidence_links")
  362. End If
  363. If Len(m_err) = 0 Then
  364. For i = 0 To UBound(items)
  365. ImportEvidence items(i), i
  366. If Len(m_err) > 0 Then
  367. Exit For
  368. End If
  369. Next
  370. End If
  371. If Len(m_err) > 0 Then
  372. On Error Resume Next
  373. DAL().RollbackTransaction
  374. On Error GoTo 0
  375. If Left(m_err, 9) = "conflict:" Then
  376. Api_Error 409, "import_conflict", Mid(m_err, 11) & " Nothing was imported."
  377. ElseIf Left(m_err, 3) = "db:" Then
  378. Api_Error 500, "internal_error", "Import failed while writing (" & Mid(m_err, 4) & "). Nothing was imported."
  379. Else
  380. Api_Invalid Array(m_err & " Nothing was imported.")
  381. End If
  382. Api_Audit "import.apply", "import", "", "failed"
  383. Exit Sub
  384. End If
  385. On Error Resume Next
  386. DAL().CommitTransaction
  387. If Err.Number <> 0 Then
  388. k = Err.Number : Api_NoteDbError
  389. Api_DbFailed k, ""
  390. Exit Sub
  391. End If
  392. On Error GoTo 0
  393. Emb_AfterWrite Api_TenantId, 8
  394. Api_Audit "import.apply", "import", "", "ok"
  395. Api_Send 201, "{""imported"":{""namespaces"":" & m_nsN & ",""memories"":" & m_cnt(0) & ",""memory_revisions"":" & m_cnt(1) & ",""verifications"":" & m_cnt(2) & _
  396. ",""documents"":" & m_cnt(3) & ",""document_revisions"":" & m_cnt(4) & ",""chunks"":" & m_cnt(5) & ",""evidence_links"":" & m_cnt(6) & "}" & _
  397. ",""indexing"":{""lexical"":""indexed"",""semantic"":""pending""}}"
  398. End Sub
  399. '===============================================================
  400. ' Import helpers. Each sets m_err ("" = fine; "conflict: ..." = 409; "db: ..." = 500; else 422) and returns.
  401. '===============================================================
  402. Private Sub ResolveNamespace(objText)
  403. Dim o, path, rs, id
  404. Set o = JsonLite_Parse(objText)
  405. path = ""
  406. If Not o Is Nothing Then
  407. path = Trim("" & o.Item("path"))
  408. End If
  409. If Len(path) = 0 Or Left(path, 1) <> "/" Then
  410. m_err = "data.namespaces: each entry needs a path starting with /."
  411. Exit Sub
  412. End If
  413. Set rs = DAL().Query("SELECT id FROM namespaces WHERE tenant_id = ? AND path = ?", Array(Api_TenantId, path))
  414. id = ""
  415. If Not rs.EOF Then
  416. id = "" & rs(0)
  417. End If
  418. rs.Close
  419. If Len(id) = 0 Then
  420. m_err = "namespace " & path & " does not exist or you lack admin permission on it (create it and grant admin first)."
  421. Exit Sub
  422. End If
  423. If Not Api_CanAccessNamespace(id, "admin") Then
  424. m_err = "namespace " & path & " does not exist or you lack admin permission on it (create it and grant admin first)."
  425. Exit Sub
  426. End If
  427. If m_nsN > UBound(m_nsPaths) Then
  428. ReDim Preserve m_nsPaths(m_nsN * 2)
  429. ReDim Preserve m_nsIds(m_nsN * 2)
  430. End If
  431. m_nsPaths(m_nsN) = path
  432. m_nsIds(m_nsN) = id
  433. m_nsN = m_nsN + 1
  434. End Sub
  435. Private Function NsIdOf(path)
  436. Dim i
  437. NsIdOf = ""
  438. For i = 0 To m_nsN - 1
  439. If m_nsPaths(i) = path Then
  440. NsIdOf = m_nsIds(i)
  441. Exit Function
  442. End If
  443. Next
  444. End Function
  445. Private Sub ImportMemory(objText, idx)
  446. Dim o, at, id, nsId, memType, life, curRev, revs, vers, i, r, rv, rid, title, content, tagsRaw, attrRaw, vs, curTitle, curContent, curTags, revSeen, ver
  447. at = "data.memories[" & idx & "]"
  448. Set o = JsonLite_Parse(objText)
  449. If o Is Nothing Then
  450. m_err = at & " must be an object."
  451. Exit Sub
  452. End If
  453. id = NeedId(o, "id", at)
  454. nsId = NeedNs(o, at)
  455. memType = LCase(NeedStr(o, "type", at, 40))
  456. life = NeedEnum(o, "lifecycle_status", at, ",active,retired,superseded,")
  457. curRev = NeedInt(o, "current_revision", at, 1, 100000)
  458. If Len(m_err) > 0 Then
  459. Exit Sub
  460. End If
  461. If InStr(m_types, "," & memType & ",") = 0 Then
  462. m_err = at & ".type '" & memType & "' is not a known memory type."
  463. Exit Sub
  464. End If
  465. If IdTaken("SELECT 1 FROM memories WHERE id = ?", id) Then
  466. m_err = "conflict: memory " & id & " already exists."
  467. Exit Sub
  468. End If
  469. revs = ArrayOf(o, "revisions")
  470. If Len(m_err) > 0 Then
  471. m_err = at & ": " & m_err
  472. Exit Sub
  473. End If
  474. If UBound(revs) + 1 <> curRev Then
  475. m_err = at & ".revisions must hold revisions 1.." & curRev & " (current_revision)."
  476. Exit Sub
  477. End If
  478. On Error Resume Next
  479. DAL().Execute "INSERT INTO memories (id, tenant_id, namespace_id, type, current_revision, lifecycle_status, creator_principal_id, created_at, updated_at) VALUES (?, ?, ?, ?, " & curRev & ", ?, ?, ?, ?)", _
  480. Array(id, Api_TenantId, nsId, memType, life, NeedId(o, "created_by", at), NeedTs(o, "created_at", at), NeedTs(o, "updated_at", at))
  481. DAL().Execute "UPDATE memories SET valid_from = ?, valid_until = ? WHERE id = ?", Array(OptStr(o, "valid_from", at, 40), OptStr(o, "valid_until", at, 40), id)
  482. If Err.Number <> 0 Then
  483. m_err = "db: memory " & id & ": " & Err.Description
  484. Err.Clear
  485. Exit Sub
  486. End If
  487. On Error GoTo 0
  488. If Len(m_err) > 0 Then
  489. Exit Sub
  490. End If
  491. m_cnt(0) = m_cnt(0) + 1
  492. m_memIds = m_memIds & id & ","
  493. revSeen = ","
  494. For i = 0 To UBound(revs)
  495. Set r = JsonLite_Parse(revs(i))
  496. rv = at & ".revisions[" & i & "]"
  497. If r Is Nothing Then
  498. m_err = rv & " must be an object."
  499. Exit Sub
  500. End If
  501. rid = NeedId(r, "id", rv)
  502. ver = NeedInt(r, "revision", rv, 1, curRev)
  503. title = NeedStr(r, "title", rv, 200)
  504. content = NeedStr(r, "content", rv, 100000)
  505. vs = NeedEnum(r, "verification_status", rv, ",unverified,verified,disputed,rejected,")
  506. tagsRaw = OptRaw(r, "tags", rv, "[")
  507. attrRaw = OptRaw(r, "attributes", rv, "{")
  508. If Len(m_err) > 0 Then
  509. Exit Sub
  510. End If
  511. If InStr(revSeen, "," & ver & ",") > 0 Then
  512. m_err = rv & ".revision " & ver & " appears twice."
  513. Exit Sub
  514. End If
  515. revSeen = revSeen & ver & ","
  516. If IdTaken("SELECT 1 FROM memory_revisions WHERE id = ?", rid) Then
  517. m_err = "conflict: memory revision " & rid & " already exists."
  518. Exit Sub
  519. End If
  520. On Error Resume Next
  521. DAL().Execute "INSERT INTO memory_revisions (id, memory_id, revision, title, content, content_hash, created_by, created_at) VALUES (?, ?, " & ver & ", ?, ?, ?, ?, ?)", _
  522. Array(rid, id, title, content, Mem_Hash(nsId, memType, title, content), NeedId(r, "created_by", rv), NeedTs(r, "created_at", rv))
  523. DAL().Execute "UPDATE memory_revisions SET summary = ?, attributes = ?, tags = ?, confidence = ?, importance = ?, change_reason = ?, verification_status = ? WHERE id = ?", _
  524. Array(OptStr(r, "summary", rv, 2000), attrRaw, tagsRaw, OptUnit(r, "confidence", rv), OptUnit(r, "importance", rv), OptStr(r, "change_reason", rv, 2000), vs, rid)
  525. If Err.Number <> 0 Then
  526. m_err = "db: memory revision " & rid & ": " & Err.Description
  527. Err.Clear
  528. Exit Sub
  529. End If
  530. On Error GoTo 0
  531. If Len(m_err) > 0 Then
  532. Exit Sub
  533. End If
  534. m_cnt(1) = m_cnt(1) + 1
  535. If ver = curRev Then
  536. curTitle = title
  537. curContent = content
  538. curTags = tagsRaw
  539. End If
  540. Next
  541. vers = ArrayOf(o, "verifications")
  542. If Len(m_err) > 0 Then
  543. m_err = at & ": " & m_err
  544. Exit Sub
  545. End If
  546. For i = 0 To UBound(vers)
  547. Set r = JsonLite_Parse(vers(i))
  548. rv = at & ".verifications[" & i & "]"
  549. If r Is Nothing Then
  550. m_err = rv & " must be an object."
  551. Exit Sub
  552. End If
  553. rid = NeedId(r, "id", rv)
  554. ver = NeedInt(r, "revision", rv, 1, curRev)
  555. vs = NeedEnum(r, "result", rv, ",verified,disputed,rejected,")
  556. If Len(m_err) > 0 Then
  557. Exit Sub
  558. End If
  559. If IdTaken("SELECT 1 FROM verifications WHERE id = ?", rid) Then
  560. m_err = "conflict: verification " & rid & " already exists."
  561. Exit Sub
  562. End If
  563. On Error Resume Next
  564. DAL().Execute "INSERT INTO verifications (id, memory_id, revision, method, result, notes, reviewer_principal_id, created_at) VALUES (?, ?, " & ver & ", ?, ?, ?, ?, ?)", _
  565. Array(rid, id, NeedStr(r, "method", rv, 100), vs, OptStr(r, "notes", rv, 2000), NeedId(r, "reviewer", rv), NeedTs(r, "created_at", rv))
  566. If Err.Number <> 0 Then
  567. m_err = "db: verification " & rid & ": " & Err.Description
  568. Err.Clear
  569. Exit Sub
  570. End If
  571. On Error GoTo 0
  572. If Len(m_err) > 0 Then
  573. Exit Sub
  574. End If
  575. m_cnt(2) = m_cnt(2) + 1
  576. Next
  577. ' derived data: lexical index now, semantic index after commit
  578. On Error Resume Next
  579. Search_IndexMemory id, curRev, Api_TenantId, nsId, curTitle, curContent, curTags
  580. Emb_MarkPending "memory", id, Api_TenantId, nsId, curRev
  581. If Err.Number <> 0 Then
  582. m_err = "db: indexing memory " & id & ": " & Err.Description
  583. Err.Clear
  584. End If
  585. On Error GoTo 0
  586. End Sub
  587. Private Sub ImportDocument(objText, idx)
  588. Dim o, at, id, nsId, fmt, curRev, revs, i, j, r, rv, rid, ver, content, chunks, c, cv, cid, revSeen, nCh
  589. at = "data.documents[" & idx & "]"
  590. Set o = JsonLite_Parse(objText)
  591. If o Is Nothing Then
  592. m_err = at & " must be an object."
  593. Exit Sub
  594. End If
  595. id = NeedId(o, "id", at)
  596. nsId = NeedNs(o, at)
  597. fmt = NeedEnum(o, "format", at, ",text,markdown,")
  598. curRev = NeedInt(o, "current_revision", at, 1, 100000)
  599. If Len(m_err) > 0 Then
  600. Exit Sub
  601. End If
  602. If IdTaken("SELECT 1 FROM documents WHERE id = ?", id) Then
  603. m_err = "conflict: document " & id & " already exists."
  604. Exit Sub
  605. End If
  606. revs = ArrayOf(o, "revisions")
  607. If Len(m_err) > 0 Then
  608. m_err = at & ": " & m_err
  609. Exit Sub
  610. End If
  611. If UBound(revs) + 1 <> curRev Then
  612. m_err = at & ".revisions must hold revisions 1.." & curRev & " (current_revision)."
  613. Exit Sub
  614. End If
  615. On Error Resume Next
  616. DAL().Execute "INSERT INTO documents (id, tenant_id, namespace_id, format, title, current_revision, created_by, created_at, updated_at) VALUES (?, ?, ?, ?, ?, " & curRev & ", ?, ?, ?)", _
  617. Array(id, Api_TenantId, nsId, fmt, NeedStr(o, "title", at, 500), NeedId(o, "created_by", at), NeedTs(o, "created_at", at), NeedTs(o, "updated_at", at))
  618. DAL().Execute "UPDATE documents SET source_uri = ? WHERE id = ?", Array(OptStr(o, "source_uri", at, 2000), id)
  619. If Err.Number <> 0 Then
  620. m_err = "db: document " & id & ": " & Err.Description
  621. Err.Clear
  622. Exit Sub
  623. End If
  624. On Error GoTo 0
  625. If Len(m_err) > 0 Then
  626. Exit Sub
  627. End If
  628. m_cnt(3) = m_cnt(3) + 1
  629. revSeen = ","
  630. For i = 0 To UBound(revs)
  631. Set r = JsonLite_Parse(revs(i))
  632. rv = at & ".revisions[" & i & "]"
  633. If r Is Nothing Then
  634. m_err = rv & " must be an object."
  635. Exit Sub
  636. End If
  637. rid = NeedId(r, "id", rv)
  638. ver = NeedInt(r, "revision", rv, 1, curRev)
  639. content = NeedStr(r, "content", rv, 1048576)
  640. chunks = ArrayOf(r, "chunks")
  641. If Len(m_err) > 0 Then
  642. Exit Sub
  643. End If
  644. If InStr(revSeen, "," & ver & ",") > 0 Then
  645. m_err = rv & ".revision " & ver & " appears twice."
  646. Exit Sub
  647. End If
  648. revSeen = revSeen & ver & ","
  649. If IdTaken("SELECT 1 FROM document_revisions WHERE id = ?", rid) Then
  650. m_err = "conflict: document revision " & rid & " already exists."
  651. Exit Sub
  652. End If
  653. nCh = UBound(chunks) + 1
  654. On Error Resume Next
  655. DAL().Execute "INSERT INTO document_revisions (id, document_id, revision, content_hash, content, char_count, chunk_count, created_by, created_at) VALUES (?, ?, " & ver & ", ?, ?, " & Len(content) & ", " & nCh & ", ?, ?)", _
  656. Array(rid, id, Api_Sha256(content), content, NeedId(r, "created_by", rv), NeedTs(r, "created_at", rv))
  657. If Err.Number <> 0 Then
  658. m_err = "db: document revision " & rid & ": " & Err.Description
  659. Err.Clear
  660. Exit Sub
  661. End If
  662. On Error GoTo 0
  663. If Len(m_err) > 0 Then
  664. Exit Sub
  665. End If
  666. m_cnt(4) = m_cnt(4) + 1
  667. For j = 0 To UBound(chunks)
  668. Set c = JsonLite_Parse(chunks(j))
  669. cv = rv & ".chunks[" & j & "]"
  670. If c Is Nothing Then
  671. m_err = cv & " must be an object."
  672. Exit Sub
  673. End If
  674. cid = NeedId(c, "id", cv)
  675. If Len(m_err) > 0 Then
  676. Exit Sub
  677. End If
  678. If IdTaken("SELECT 1 FROM chunks WHERE id = ?", cid) Then
  679. m_err = "conflict: chunk " & cid & " already exists."
  680. Exit Sub
  681. End If
  682. On Error Resume Next
  683. DAL().Execute "INSERT INTO chunks (id, document_id, document_revision, ordinal, content, heading_path, start_offset, end_offset, token_estimate, created_at) VALUES (?, ?, " & ver & ", " & _
  684. NeedInt(c, "ordinal", cv, 0, 1000000) & ", ?, ?, " & NeedInt(c, "start_offset", cv, 0, 100000000) & ", " & NeedInt(c, "end_offset", cv, 0, 100000000) & ", " & _
  685. NeedInt(c, "token_estimate", cv, 0, 100000000) & ", ?)", Array(cid, id, NeedStr(c, "content", cv, 1048576), OptStr(c, "heading_path", cv, 2000), NeedTs(c, "created_at", cv))
  686. If Err.Number <> 0 Then
  687. m_err = "db: chunk " & cid & ": " & Err.Description
  688. Err.Clear
  689. Exit Sub
  690. End If
  691. ' the current revision is searchable: lexical index now, semantic index after commit
  692. If ver = curRev Then
  693. DAL().Execute "INSERT INTO chunks_fts (content, chunk_id, tenant_id, namespace_id) VALUES (?, ?, ?, ?)", Array("" & c.Item("content"), cid, Api_TenantId, nsId)
  694. Emb_MarkPending "chunk", cid, Api_TenantId, nsId, ver
  695. End If
  696. If Err.Number <> 0 Then
  697. m_err = "db: indexing chunk " & cid & ": " & Err.Description
  698. Err.Clear
  699. Exit Sub
  700. End If
  701. On Error GoTo 0
  702. If Len(m_err) > 0 Then
  703. Exit Sub
  704. End If
  705. m_cnt(5) = m_cnt(5) + 1
  706. Next
  707. Next
  708. End Sub
  709. Private Sub ImportEvidence(objText, idx)
  710. Dim o, at, id, ownerId, kind
  711. at = "data.evidence_links[" & idx & "]"
  712. Set o = JsonLite_Parse(objText)
  713. If o Is Nothing Then
  714. m_err = at & " must be an object."
  715. Exit Sub
  716. End If
  717. id = NeedId(o, "id", at)
  718. ownerId = NeedId(o, "owner_id", at)
  719. kind = NeedEnum(o, "target_kind", at, ",experience,chunk,memory,document,")
  720. NeedEnum o, "owner_kind", at, ",memory,"
  721. If Len(m_err) > 0 Then
  722. Exit Sub
  723. End If
  724. If InStr(m_memIds, "," & ownerId & ",") = 0 Then
  725. m_err = at & ".owner_id must be a memory of this export."
  726. Exit Sub
  727. End If
  728. If IdTaken("SELECT 1 FROM evidence_links WHERE id = ?", id) Then
  729. m_err = "conflict: evidence link " & id & " already exists."
  730. Exit Sub
  731. End If
  732. On Error Resume Next
  733. DAL().Execute "INSERT INTO evidence_links (id, tenant_id, owner_kind, owner_id, target_kind, target_id, created_by, created_at) VALUES (?, ?, 'memory', ?, ?, ?, ?, ?)", _
  734. Array(id, Api_TenantId, ownerId, kind, NeedId(o, "target_id", at), NeedId(o, "created_by", at), NeedTs(o, "created_at", at))
  735. DAL().Execute "UPDATE evidence_links SET owner_revision = ?, target_revision = ?, role = ?, excerpt = ? WHERE id = ?", _
  736. Array(OptInt(o, "owner_revision", at), OptInt(o, "target_revision", at), NeedEnum(o, "role", at, ",supports,contradicts,context,"), OptStr(o, "excerpt", at, 2000), id)
  737. If Err.Number <> 0 Then
  738. m_err = "db: evidence link " & id & ": " & Err.Description
  739. Err.Clear
  740. Exit Sub
  741. End If
  742. On Error GoTo 0
  743. If Len(m_err) = 0 Then
  744. m_cnt(6) = m_cnt(6) + 1
  745. End If
  746. End Sub
  747. ' ---- field readers (first error wins; they return a harmless value after an error) ----
  748. Private Sub Fail(msg)
  749. If Len(m_err) = 0 Then
  750. m_err = msg
  751. End If
  752. End Sub
  753. Private Function ArrayOf(o, k)
  754. Dim a
  755. ArrayOf = Array()
  756. If Not o.HasRaw(k) Then
  757. If o.Exists(k) Then
  758. If Not IsNull(o.Item(k)) Then
  759. Fail k & " must be an array of objects."
  760. End If
  761. End If
  762. Exit Function
  763. End If
  764. a = JsonLite_SplitObjectArray(o.Raw(k))
  765. If IsNull(a) Then
  766. Fail k & " must be an array of objects."
  767. Exit Function
  768. End If
  769. ArrayOf = a
  770. End Function
  771. Private Function NeedStr(o, k, at, maxLen)
  772. Dim v
  773. NeedStr = ""
  774. v = o.Item(k)
  775. If VarType(v) <> vbString Then
  776. Fail at & "." & k & " is required (string)."
  777. Exit Function
  778. End If
  779. If Len(v) = 0 Or Len(v) > maxLen Then
  780. Fail at & "." & k & " must be 1 to " & maxLen & " characters."
  781. Exit Function
  782. End If
  783. NeedStr = v
  784. End Function
  785. Private Function OptStr(o, k, at, maxLen)
  786. Dim v
  787. OptStr = Null
  788. v = o.Item(k)
  789. If IsNull(v) Then
  790. Exit Function
  791. End If
  792. If VarType(v) <> vbString Then
  793. Fail at & "." & k & " must be a string or null."
  794. Exit Function
  795. End If
  796. If Len(v) > maxLen Then
  797. Fail at & "." & k & " must be at most " & maxLen & " characters."
  798. Exit Function
  799. End If
  800. OptStr = v
  801. End Function
  802. Private Function NeedId(o, k, at)
  803. Dim v
  804. v = NeedStr(o, k, at, 64)
  805. If Len(v) > 0 Then
  806. If Not Api_IsId(v) Then
  807. Fail at & "." & k & " must be a UUID."
  808. End If
  809. End If
  810. NeedId = LCase(v)
  811. End Function
  812. Private Function NeedTs(o, k, at)
  813. NeedTs = NeedStr(o, k, at, 40)
  814. End Function
  815. Private Function NeedEnum(o, k, at, allowedList)
  816. Dim v
  817. v = LCase(NeedStr(o, k, at, 40))
  818. If Len(v) > 0 Then
  819. If InStr(allowedList, "," & v & ",") = 0 Then
  820. Fail at & "." & k & " must be one of " & Mid(allowedList, 2, Len(allowedList) - 2) & "."
  821. End If
  822. End If
  823. NeedEnum = v
  824. End Function
  825. Private Function NeedInt(o, k, at, lo, hi)
  826. Dim v
  827. NeedInt = lo
  828. v = o.Item(k)
  829. If VarType(v) <> vbDouble Then
  830. Fail at & "." & k & " is required (integer)."
  831. Exit Function
  832. End If
  833. If v <> Int(v) Or v < lo Or v > hi Then
  834. Fail at & "." & k & " must be an integer between " & lo & " and " & hi & "."
  835. Exit Function
  836. End If
  837. NeedInt = CLng(v)
  838. End Function
  839. Private Function OptInt(o, k, at)
  840. Dim v
  841. OptInt = Null
  842. v = o.Item(k)
  843. If IsNull(v) Then
  844. Exit Function
  845. End If
  846. OptInt = NeedInt(o, k, at, 0, 100000000)
  847. End Function
  848. Private Function OptUnit(o, k, at)
  849. Dim v
  850. OptUnit = Null
  851. v = o.Item(k)
  852. If IsNull(v) Then
  853. Exit Function
  854. End If
  855. If VarType(v) <> vbDouble Then
  856. Fail at & "." & k & " must be a number between 0 and 1."
  857. Exit Function
  858. End If
  859. If v < 0 Or v > 1 Then
  860. Fail at & "." & k & " must be a number between 0 and 1."
  861. Exit Function
  862. End If
  863. OptUnit = v
  864. End Function
  865. ' Raw JSON array/object text (first character `first`), or Null
  866. Private Function OptRaw(o, k, at, first)
  867. Dim raw
  868. OptRaw = Null
  869. If Not o.HasRaw(k) Then
  870. If o.Exists(k) Then
  871. If Not IsNull(o.Item(k)) Then
  872. Fail at & "." & k & " has the wrong type."
  873. End If
  874. End If
  875. Exit Function
  876. End If
  877. raw = o.Raw(k)
  878. If Left(raw, 1) <> first Then
  879. Fail at & "." & k & " has the wrong type."
  880. Exit Function
  881. End If
  882. OptRaw = raw
  883. End Function
  884. Private Function NeedNs(o, at)
  885. Dim path, id
  886. path = NeedStr(o, "namespace", at, 500)
  887. NeedNs = ""
  888. If Len(path) = 0 Then
  889. Exit Function
  890. End If
  891. id = NsIdOf(path)
  892. If Len(id) = 0 Then
  893. Fail at & ".namespace " & path & " is not listed in data.namespaces."
  894. Exit Function
  895. End If
  896. NeedNs = id
  897. End Function
  898. Private Function IdTaken(sql, id)
  899. Dim rs
  900. Set rs = DAL().Query(sql, id)
  901. IdTaken = Not rs.EOF
  902. rs.Close
  903. End Function
  904. ' ---- export helpers ----
  905. Private Function Rows(sql)
  906. Dim rs
  907. Rows = Empty
  908. Set rs = DAL().Query(sql, Empty)
  909. If Not rs.EOF Then
  910. Rows = rs.GetRows()
  911. End If
  912. rs.Close
  913. End Function
  914. Private Function RowsMax(r)
  915. If IsEmpty(r) Then
  916. RowsMax = -1
  917. Else
  918. RowsMax = UBound(r, 2)
  919. End If
  920. End Function
  921. Private Function NsPath(nsRows, nsId)
  922. Dim i
  923. NsPath = ""
  924. For i = 0 To RowsMax(nsRows)
  925. If "" & nsRows(0, i) = nsId Then
  926. NsPath = "" & nsRows(1, i)
  927. Exit Function
  928. End If
  929. Next
  930. End Function
  931. Private Function JStr(v)
  932. If IsNull(v) Then
  933. JStr = "null"
  934. Else
  935. JStr = JsonLite_Quote("" & v)
  936. End If
  937. End Function
  938. Private Function JNum(v)
  939. If IsNull(v) Then
  940. JNum = "null"
  941. ElseIf Len("" & v) = 0 Then
  942. JNum = "null"
  943. Else
  944. JNum = JsonLite_Num(v)
  945. End If
  946. End Function
  947. Private Function JRaw(v)
  948. If IsNull(v) Then
  949. JRaw = "null"
  950. ElseIf Len("" & v) = 0 Then
  951. JRaw = "null"
  952. Else
  953. JRaw = "" & v
  954. End If
  955. End Function
  956. ' Static action dispatch (AxonASP mishandles runtime-built Eval/Execute calls and shared call sites)
  957. Public Sub Invoke(actionName, args)
  958. Select Case LCase(actionName)
  959. Case "export" : Export
  960. Case "import" : Import
  961. Case Else
  962. Err.Raise 438, "ApiPortabilityController_Class", "Action '" & actionName & "' not found"
  963. End Select
  964. End Sub
  965. End Class
  966. Dim ApiPortabilityController_Class__Singleton
  967. Function ApiPortabilityController()
  968. If IsEmpty(ApiPortabilityController_Class__Singleton) Then
  969. Set ApiPortabilityController_Class__Singleton = New ApiPortabilityController_Class
  970. End If
  971. Set ApiPortabilityController = ApiPortabilityController_Class__Singleton
  972. End Function
  973. ' Global entry point used by MVC_InvokeAction via GetRef (one call site per controller)
  974. Sub ApiPortabilityController_Invoke(actionName, args)
  975. ApiPortabilityController().Invoke actionName, args
  976. End Sub
  977. %>

Powered by TurnKey Linux.