@alexsun-top/opencode-chat-room 0.8.3 → 0.8.5

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.
Files changed (156) hide show
  1. package/CHANGELOG.md +7 -1
  2. package/README.md +5 -5
  3. package/README.zh-CN.md +5 -5
  4. package/cli.ts +1 -1
  5. package/dist/cli.js +35 -16
  6. package/package.json +15 -9
  7. package/packages/chat-room-channeld/README.md +3 -2
  8. package/packages/chat-room-channeld/src/inbound.ts +3 -6
  9. package/packages/chat-room-channeld/src/pg/sql.mjs +157 -142
  10. package/packages/chat-room-channeld/src/pg/sql.mts +175 -0
  11. package/packages/chat-room-channeld/src/pg/worker.mjs +107 -99
  12. package/packages/chat-room-channeld/src/pg/worker.mts +166 -0
  13. package/plugin.ts +1 -1
  14. package/server/assets/chat/admin-badge.js +1 -1
  15. package/server/assets/chat/admin.js +16 -10
  16. package/server/assets/chat/announcements.js +12 -2
  17. package/server/assets/chat/attachment-store.js +15 -6
  18. package/server/assets/chat/attachments.js +48 -33
  19. package/server/assets/chat/chat-background.js +8 -14
  20. package/server/assets/chat/clipboard.js +2 -1
  21. package/server/assets/chat/confirm-dialog.js +3 -3
  22. package/server/assets/chat/copy-action.js +1 -2
  23. package/server/assets/chat/diagram-viewer.js +7 -9
  24. package/server/assets/chat/doc-preview.js +19 -12
  25. package/server/assets/chat/editor.js +87 -61
  26. package/server/assets/chat/events.js +49 -34
  27. package/server/assets/chat/files.js +45 -13
  28. package/server/assets/chat/history-loader.js +5 -3
  29. package/server/assets/chat/image-zoom.js +21 -17
  30. package/server/assets/chat/install-prompt.js +10 -9
  31. package/server/assets/chat/integration-account-schema.js +5 -3
  32. package/server/assets/chat/integration-editor.js +2 -2
  33. package/server/assets/chat/integration-values.js +17 -11
  34. package/server/assets/chat/integrations.js +138 -81
  35. package/server/assets/chat/join-requests.js +20 -13
  36. package/server/assets/chat/markdown-footnotes.js +8 -8
  37. package/server/assets/chat/markdown.js +48 -38
  38. package/server/assets/chat/members.js +48 -25
  39. package/server/assets/chat/mentions.js +39 -12
  40. package/server/assets/chat/message-cache.js +2 -2
  41. package/server/assets/chat/message-polling.js +40 -21
  42. package/server/assets/chat/message-window.js +10 -6
  43. package/server/assets/chat/messages.js +70 -46
  44. package/server/assets/chat/office-preview.js +3 -3
  45. package/server/assets/chat/palette.js +45 -29
  46. package/server/assets/chat/panels.js +19 -1
  47. package/server/assets/chat/pwa.js +5 -4
  48. package/server/assets/chat/questions.js +25 -22
  49. package/server/assets/chat/recall.js +39 -21
  50. package/server/assets/chat/receipts.js +9 -7
  51. package/server/assets/chat/reminders.js +25 -8
  52. package/server/assets/chat/replies.js +34 -15
  53. package/server/assets/chat/resource-format.js +2 -2
  54. package/server/assets/chat/room-list.js +2 -2
  55. package/server/assets/chat/room-watcher.js +8 -4
  56. package/server/assets/chat/rooms.js +51 -40
  57. package/server/assets/chat/search.js +8 -6
  58. package/server/assets/chat/settings.js +16 -14
  59. package/server/assets/chat/shiki.js +1 -1
  60. package/server/assets/chat/shortcuts.js +3 -3
  61. package/server/assets/chat/side-pane.js +4 -3
  62. package/server/assets/chat/state.js +65 -37
  63. package/server/assets/chat/system-events.js +6 -5
  64. package/server/assets/chat/system-messages.js +15 -15
  65. package/server/assets/chat/task-form.js +5 -5
  66. package/server/assets/chat/task-row.js +1 -1
  67. package/server/assets/chat/tasks.js +41 -28
  68. package/server/assets/chat/window-controls.js +3 -2
  69. package/server/assets/conversation.css +14 -2
  70. package/server/assets/doc-preview.css +6 -0
  71. package/server/assets/office/THIRD-PARTY-NOTICES.txt +23 -1
  72. package/server/assets/office/licenses/docx-preview-HEADER.txt +6 -0
  73. package/server/assets/office/licenses/echarts-LICENSE-d3.txt +27 -0
  74. package/server/assets/office/licenses/echarts-NOTICE.txt +5 -0
  75. package/server/assets/office/licenses/immediate-LICENSE.txt +20 -0
  76. package/server/assets/office/licenses/lie-LICENSE.txt +7 -0
  77. package/server/assets/office/licenses/pako-LICENSE.txt +21 -0
  78. package/server/assets/office/licenses/pako-ZLIB.txt +18 -0
  79. package/server/assets/office/licenses/setimmediate-LICENSE.txt +20 -0
  80. package/server/chat.html +1 -0
  81. package/server/sw.js +3 -1
  82. package/skills/chatroom/SKILL.md +9 -0
  83. package/src/README.md +1 -1
  84. package/src/agent/agent-receipts.ts +12 -4
  85. package/src/center/center-client.ts +1 -1
  86. package/src/state/pg-sql.mjs +157 -142
  87. package/src/state/pg-sql.mts +175 -0
  88. package/src/state/pg-worker.mjs +107 -99
  89. package/src/state/pg-worker.mts +166 -0
  90. package/src/state/store.ts +21 -7
  91. package/src/tui/app-types.mjs +1 -0
  92. package/src/tui/app-types.mts +175 -0
  93. package/src/tui/app.mjs +426 -423
  94. package/src/tui/app.mts +500 -0
  95. package/src/tui/config.mjs +166 -158
  96. package/src/tui/config.mts +247 -0
  97. package/src/tui/http.mjs +239 -206
  98. package/src/tui/http.mts +412 -0
  99. package/src/tui/i18n.mjs +17 -20
  100. package/src/tui/i18n.mts +42 -0
  101. package/src/tui/info-app.mjs +431 -427
  102. package/src/tui/info-app.mts +500 -0
  103. package/src/tui/info-content.mjs +205 -199
  104. package/src/tui/info-content.mts +341 -0
  105. package/src/tui/join-app.mjs +75 -64
  106. package/src/tui/join-app.mts +99 -0
  107. package/src/tui/locales/en.mjs +302 -320
  108. package/src/tui/locales/en.mts +349 -0
  109. package/src/tui/locales/zh-CN.mjs +302 -320
  110. package/src/tui/locales/zh-CN.mts +349 -0
  111. package/src/tui/main.mjs +219 -213
  112. package/src/tui/main.mts +255 -0
  113. package/src/tui/markdown.mjs +128 -272
  114. package/src/tui/markdown.mts +152 -0
  115. package/src/tui/mentions.mjs +181 -184
  116. package/src/tui/mentions.mts +251 -0
  117. package/src/tui/message-app.mjs +181 -173
  118. package/src/tui/message-app.mts +213 -0
  119. package/src/tui/message-view.mjs +90 -111
  120. package/src/tui/message-view.mts +151 -0
  121. package/src/tui/messages.mjs +55 -49
  122. package/src/tui/messages.mts +106 -0
  123. package/src/tui/palette-app.mjs +364 -344
  124. package/src/tui/palette-app.mts +388 -0
  125. package/src/tui/palette.mjs +100 -98
  126. package/src/tui/palette.mts +176 -0
  127. package/src/tui/prefs.mjs +56 -63
  128. package/src/tui/prefs.mts +126 -0
  129. package/src/tui/reminder-app.mjs +191 -181
  130. package/src/tui/reminder-app.mts +228 -0
  131. package/src/tui/room-settings-app.mjs +257 -242
  132. package/src/tui/room-settings-app.mts +301 -0
  133. package/src/tui/session.mjs +30 -29
  134. package/src/tui/session.mts +51 -0
  135. package/src/tui/settings-app.mjs +151 -138
  136. package/src/tui/settings-app.mts +179 -0
  137. package/src/tui/ui-content.mjs +426 -421
  138. package/src/tui/ui-content.mts +499 -0
  139. package/src/tui/ui-help.mjs +58 -67
  140. package/src/tui/ui-help.mts +87 -0
  141. package/src/tui/ui-mentions.mjs +34 -36
  142. package/src/tui/ui-mentions.mts +60 -0
  143. package/src/tui/ui-overlay-view.mjs +259 -267
  144. package/src/tui/ui-overlay-view.mts +322 -0
  145. package/src/tui/ui-suggest-view.mjs +54 -57
  146. package/src/tui/ui-suggest-view.mts +78 -0
  147. package/src/tui/ui-suggest.mjs +164 -167
  148. package/src/tui/ui-suggest.mts +248 -0
  149. package/src/tui/ui-theme.mjs +52 -57
  150. package/src/tui/ui-theme.mts +98 -0
  151. package/src/tui/ui-view.mjs +267 -275
  152. package/src/tui/ui-view.mts +308 -0
  153. package/src/tui/ui.mjs +372 -359
  154. package/src/tui/ui.mts +440 -0
  155. package/src/tui/wire.mjs +215 -0
  156. package/src/tui/wire.mts +383 -0
@@ -4,172 +4,187 @@
4
4
  //
5
5
  // This is intentionally not a general SQLite translator. Keep the dialect
6
6
  // narrow and add a test when the state layer starts using a new construct.
7
-
8
7
  const PRAGMA_TABLE_INFO = /PRAGMA\s+table_info\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)/gi;
9
-
10
8
  function pragmaTableInfo(table) {
11
- const tableName = table.replace(/'/g, "''");
12
- return `SELECT c.column_name AS name,c.data_type AS type,` +
13
- `CASE WHEN c.is_nullable='NO' THEN 1 ELSE 0 END AS "notnull",` +
14
- `c.column_default AS dflt_value,` +
15
- `CASE WHEN EXISTS (SELECT 1 FROM pg_index i JOIN pg_attribute a ON a.attrelid=i.indrelid AND a.attnum=ANY(i.indkey) ` +
16
- `WHERE i.indrelid=(quote_ident(c.table_schema)||'.'||quote_ident(c.table_name))::regclass AND i.indisprimary AND a.attname=c.column_name) ` +
17
- `THEN 1 ELSE 0 END AS pk ` +
18
- `FROM information_schema.columns c WHERE c.table_schema=current_schema() AND c.table_name='${tableName}' ORDER BY c.ordinal_position`;
9
+ const tableName = table.replace(/'/g, "''");
10
+ return `SELECT c.column_name AS name,c.data_type AS type,` +
11
+ `CASE WHEN c.is_nullable='NO' THEN 1 ELSE 0 END AS "notnull",` +
12
+ `c.column_default AS dflt_value,` +
13
+ `CASE WHEN EXISTS (SELECT 1 FROM pg_index i JOIN pg_attribute a ON a.attrelid=i.indrelid AND a.attnum=ANY(i.indkey) ` +
14
+ `WHERE i.indrelid=(quote_ident(c.table_schema)||'.'||quote_ident(c.table_name))::regclass AND i.indisprimary AND a.attname=c.column_name) ` +
15
+ `THEN 1 ELSE 0 END AS pk ` +
16
+ `FROM information_schema.columns c WHERE c.table_schema=current_schema() AND c.table_name='${tableName}' ORDER BY c.ordinal_position`;
19
17
  }
20
-
21
18
  const isIdentStart = (ch) => !!ch && /[A-Za-z_]/.test(ch);
22
19
  const isIdentChar = (ch) => !!ch && /[A-Za-z0-9_$]/.test(ch);
23
-
24
20
  function skipQuoted(sql, index) {
25
- const quote = sql[index];
26
- let i = index + 1;
27
- while (i < sql.length) {
28
- if (sql[i] === quote) {
29
- if (sql[i + 1] === quote) { i += 2; continue; }
30
- return i + 1;
21
+ const quote = sql[index];
22
+ let i = index + 1;
23
+ while (i < sql.length) {
24
+ if (sql[i] === quote) {
25
+ if (sql[i + 1] === quote) {
26
+ i += 2;
27
+ continue;
28
+ }
29
+ return i + 1;
30
+ }
31
+ i++;
31
32
  }
32
- i++;
33
- }
34
- return i;
33
+ return i;
35
34
  }
36
-
37
35
  function skipComment(sql, index) {
38
- if (sql[index + 1] === "-") {
39
- const end = sql.indexOf("\n", index + 2);
40
- return end < 0 ? sql.length : end;
41
- }
42
- const end = sql.indexOf("*/", index + 2);
43
- return end < 0 ? sql.length : end + 2;
36
+ if (sql[index + 1] === "-") {
37
+ const end = sql.indexOf("\n", index + 2);
38
+ return end < 0 ? sql.length : end;
39
+ }
40
+ const end = sql.indexOf("*/", index + 2);
41
+ return end < 0 ? sql.length : end + 2;
44
42
  }
45
-
46
43
  function hasTopLevelComma(sql, openIndex) {
47
- let depth = 0;
48
- for (let i = openIndex; i < sql.length; i++) {
49
- const ch = sql[i];
50
- if (ch === "'" || ch === '"') { i = skipQuoted(sql, i) - 1; continue; }
51
- if (ch === "-" && sql[i + 1] === "-") { i = skipComment(sql, i) - 1; continue; }
52
- if (ch === "/" && sql[i + 1] === "*") { i = skipComment(sql, i) - 1; continue; }
53
- if (ch === "(") depth++;
54
- else if (ch === ")") { depth--; if (depth === 0) return false; }
55
- else if (ch === "," && depth === 1) return true;
56
- }
57
- return false;
44
+ let depth = 0;
45
+ for (let i = openIndex; i < sql.length; i++) {
46
+ const ch = sql[i];
47
+ if (ch === "'" || ch === '"') {
48
+ i = skipQuoted(sql, i) - 1;
49
+ continue;
50
+ }
51
+ if (ch === "-" && sql[i + 1] === "-") {
52
+ i = skipComment(sql, i) - 1;
53
+ continue;
54
+ }
55
+ if (ch === "/" && sql[i + 1] === "*") {
56
+ i = skipComment(sql, i) - 1;
57
+ continue;
58
+ }
59
+ if (ch === "(")
60
+ depth++;
61
+ else if (ch === ")") {
62
+ depth--;
63
+ if (depth === 0)
64
+ return false;
65
+ }
66
+ else if (ch === "," && depth === 1)
67
+ return true;
68
+ }
69
+ return false;
58
70
  }
59
-
60
71
  function wordBefore(sql, index) {
61
- let i = index - 1;
62
- while (i >= 0 && /\s/.test(sql[i])) i--;
63
- const end = i + 1;
64
- while (i >= 0 && isIdentChar(sql[i])) i--;
65
- return sql.slice(i + 1, end).toLowerCase();
72
+ let i = index - 1;
73
+ while (i >= 0 && /\s/.test(sql.charAt(i)))
74
+ i--;
75
+ const end = i + 1;
76
+ while (i >= 0 && isIdentChar(sql[i]))
77
+ i--;
78
+ return sql.slice(i + 1, end).toLowerCase();
66
79
  }
67
-
68
80
  function wordAfter(sql, index) {
69
- let i = index + 1;
70
- while (i < sql.length && /\s/.test(sql[i])) i++;
71
- const start = i;
72
- while (i < sql.length && isIdentChar(sql[i])) i++;
73
- return sql.slice(start, i).toLowerCase();
81
+ let i = index + 1;
82
+ while (i < sql.length && /\s/.test(sql.charAt(i)))
83
+ i++;
84
+ const start = i;
85
+ while (i < sql.length && isIdentChar(sql[i]))
86
+ i++;
87
+ return sql.slice(start, i).toLowerCase();
74
88
  }
75
-
76
89
  function scan(sql) {
77
- let out = "";
78
- let index = 0;
79
- let parameter = 0;
80
- while (index < sql.length) {
81
- const ch = sql[index];
82
- if (ch === "'" || ch === '"') {
83
- const end = skipQuoted(sql, index);
84
- out += sql.slice(index, end);
85
- index = end;
86
- continue;
87
- }
88
- if (ch === "-" && sql[index + 1] === "-") {
89
- const end = skipComment(sql, index);
90
- out += sql.slice(index, end);
91
- index = end;
92
- continue;
93
- }
94
- if (ch === "/" && sql[index + 1] === "*") {
95
- const end = skipComment(sql, index);
96
- out += sql.slice(index, end);
97
- index = end;
98
- continue;
99
- }
100
- if (ch === "?") {
101
- // CASE WHEN ? needs a boolean; a bare `? IS NULL` has no type to infer.
102
- const nextWord = wordAfter(sql, index);
103
- const cast = (wordBefore(sql, index) === "when" && nextWord === "then") || nextWord === "is";
104
- out += `$${++parameter}${cast ? (nextWord === "is" ? "::text" : "::boolean") : ""}`;
105
- index++;
106
- continue;
107
- }
108
- if (isIdentStart(ch)) {
109
- let end = index + 1;
110
- while (isIdentChar(sql[end])) end++;
111
- const word = sql.slice(index, end);
112
- const lower = word.toLowerCase();
113
- // SQLite's scalar max(a,b)/min(a,b); the one-argument aggregate forms stay.
114
- if (lower === "max" || lower === "min") {
115
- let next = end;
116
- while (next < sql.length && /\s/.test(sql[next])) next++;
117
- if (sql[next] === "(" && hasTopLevelComma(sql, next)) {
118
- out += lower === "max" ? "GREATEST" : "LEAST";
119
- index = end;
120
- continue;
90
+ let out = "";
91
+ let index = 0;
92
+ let parameter = 0;
93
+ while (index < sql.length) {
94
+ const ch = sql.charAt(index);
95
+ if (ch === "'" || ch === '"') {
96
+ const end = skipQuoted(sql, index);
97
+ out += sql.slice(index, end);
98
+ index = end;
99
+ continue;
121
100
  }
122
- }
123
- // Preserve camelCase result aliases: PostgreSQL folds unquoted identifiers.
124
- if (lower === "as") {
125
- const previous = wordBefore(sql, index);
126
- let next = end;
127
- while (next < sql.length && /\s/.test(sql[next])) next++;
128
- if (isIdentStart(sql[next])) {
129
- let aliasEnd = next + 1;
130
- while (isIdentChar(sql[aliasEnd])) aliasEnd++;
131
- const alias = sql.slice(next, aliasEnd);
132
- if (/[A-Z]/.test(alias) && previous !== "default" && alias.toLowerCase() !== "identity") {
133
- out += `AS "${alias}"`;
134
- index = aliasEnd;
101
+ if (ch === "-" && sql[index + 1] === "-") {
102
+ const end = skipComment(sql, index);
103
+ out += sql.slice(index, end);
104
+ index = end;
135
105
  continue;
136
- }
137
106
  }
138
- }
139
- out += word;
140
- index = end;
141
- continue;
107
+ if (ch === "/" && sql[index + 1] === "*") {
108
+ const end = skipComment(sql, index);
109
+ out += sql.slice(index, end);
110
+ index = end;
111
+ continue;
112
+ }
113
+ if (ch === "?") {
114
+ // CASE WHEN ? needs a boolean; a bare `? IS NULL` has no type to infer.
115
+ const nextWord = wordAfter(sql, index);
116
+ const cast = (wordBefore(sql, index) === "when" && nextWord === "then") || nextWord === "is";
117
+ out += `$${++parameter}${cast ? (nextWord === "is" ? "::text" : "::boolean") : ""}`;
118
+ index++;
119
+ continue;
120
+ }
121
+ if (isIdentStart(ch)) {
122
+ let end = index + 1;
123
+ while (isIdentChar(sql[end]))
124
+ end++;
125
+ const word = sql.slice(index, end);
126
+ const lower = word.toLowerCase();
127
+ // SQLite's scalar max(a,b)/min(a,b); the one-argument aggregate forms stay.
128
+ if (lower === "max" || lower === "min") {
129
+ let next = end;
130
+ while (next < sql.length && /\s/.test(sql.charAt(next)))
131
+ next++;
132
+ if (sql[next] === "(" && hasTopLevelComma(sql, next)) {
133
+ out += lower === "max" ? "GREATEST" : "LEAST";
134
+ index = end;
135
+ continue;
136
+ }
137
+ }
138
+ // Preserve camelCase result aliases: PostgreSQL folds unquoted identifiers.
139
+ if (lower === "as") {
140
+ const previous = wordBefore(sql, index);
141
+ let next = end;
142
+ while (next < sql.length && /\s/.test(sql.charAt(next)))
143
+ next++;
144
+ if (isIdentStart(sql[next])) {
145
+ let aliasEnd = next + 1;
146
+ while (isIdentChar(sql[aliasEnd]))
147
+ aliasEnd++;
148
+ const alias = sql.slice(next, aliasEnd);
149
+ if (/[A-Z]/.test(alias) && previous !== "default" && alias.toLowerCase() !== "identity") {
150
+ out += `AS "${alias}"`;
151
+ index = aliasEnd;
152
+ continue;
153
+ }
154
+ }
155
+ }
156
+ out += word;
157
+ index = end;
158
+ continue;
159
+ }
160
+ out += ch;
161
+ index++;
142
162
  }
143
- out += ch;
144
- index++;
145
- }
146
- return out;
163
+ return out;
147
164
  }
148
-
149
165
  function appendOnConflictDoNothing(sql) {
150
- return `${sql.replace(/[\s;]+$/, "")} ON CONFLICT DO NOTHING`;
166
+ return `${sql.replace(/[\s;]+$/, "")} ON CONFLICT DO NOTHING`;
151
167
  }
152
-
153
168
  export function translate(sql) {
154
- let text = String(sql);
155
- // Connection tuning pragmas have no PostgreSQL equivalent; foreign keys are
156
- // always enforced and concurrency is handled by advisory locks.
157
- text = text.replace(/PRAGMA\s+(?:busy_timeout|journal_mode|foreign_keys)\s*(?:=\s*[^;]+)?\s*;?/gi, "");
158
- text = text.replace(PRAGMA_TABLE_INFO, (_, table) => pragmaTableInfo(table));
159
- // SQLite INTEGER is 64-bit; PostgreSQL INTEGER is 32-bit and epoch
160
- // milliseconds overflow it.
161
- text = text.replace(/\bINTEGER\b/gi, "BIGINT");
162
- text = text.replace(/\bBIGINT\s+PRIMARY\s+KEY\s+AUTOINCREMENT\b/gi, "BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY");
163
- // Manual `BEGIN IMMEDIATE` appears inside receiver-state's schema script.
164
- text = text.replace(/\bBEGIN\s+(?:DEFERRED|IMMEDIATE|EXCLUSIVE)\b/gi, "BEGIN");
165
- if (/\bINSERT\s+OR\s+IGNORE\b/i.test(text)) {
166
- text = text.replace(/\bINSERT\s+OR\s+IGNORE\b/gi, "INSERT");
167
- if (!/\bON\s+CONFLICT\b/i.test(text)) text = appendOnConflictDoNothing(text);
168
- }
169
- // SQLite treats 1/0 as booleans; PostgreSQL needs an explicit cast.
170
- return scan(text).trim();
169
+ let text = String(sql);
170
+ // Connection tuning pragmas have no PostgreSQL equivalent; foreign keys are
171
+ // always enforced and concurrency is handled by advisory locks.
172
+ text = text.replace(/PRAGMA\s+(?:busy_timeout|journal_mode|foreign_keys)\s*(?:=\s*[^;]+)?\s*;?/gi, "");
173
+ text = text.replace(PRAGMA_TABLE_INFO, (_, table) => pragmaTableInfo(table));
174
+ // SQLite INTEGER is 64-bit; PostgreSQL INTEGER is 32-bit and epoch
175
+ // milliseconds overflow it.
176
+ text = text.replace(/\bINTEGER\b/gi, "BIGINT");
177
+ text = text.replace(/\bBIGINT\s+PRIMARY\s+KEY\s+AUTOINCREMENT\b/gi, "BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY");
178
+ // Manual `BEGIN IMMEDIATE` appears inside receiver-state's schema script.
179
+ text = text.replace(/\bBEGIN\s+(?:DEFERRED|IMMEDIATE|EXCLUSIVE)\b/gi, "BEGIN");
180
+ if (/\bINSERT\s+OR\s+IGNORE\b/i.test(text)) {
181
+ text = text.replace(/\bINSERT\s+OR\s+IGNORE\b/gi, "INSERT");
182
+ if (!/\bON\s+CONFLICT\b/i.test(text))
183
+ text = appendOnConflictDoNothing(text);
184
+ }
185
+ // SQLite treats 1/0 as booleans; PostgreSQL needs an explicit cast.
186
+ return scan(text).trim();
171
187
  }
172
-
173
188
  export function hasSql(text) {
174
- return text.replace(/[;\s]/g, "").length > 0;
175
- }
189
+ return text.replace(/[;\s]/g, "").length > 0;
190
+ }
@@ -0,0 +1,175 @@
1
+ // SQLite -> PostgreSQL translation for the chat-room state SQL. The app's SQL
2
+ // uses ? placeholders, SQLite DDL, a few pragmas and a handful of scalar
3
+ // functions; everything else is deliberately passed through unchanged.
4
+ //
5
+ // This is intentionally not a general SQLite translator. Keep the dialect
6
+ // narrow and add a test when the state layer starts using a new construct.
7
+
8
+ const PRAGMA_TABLE_INFO = /PRAGMA\s+table_info\s*\(\s*([A-Za-z_][A-Za-z0-9_]*)\s*\)/gi;
9
+
10
+ function pragmaTableInfo(table: string): string {
11
+ const tableName = table.replace(/'/g, "''");
12
+ return `SELECT c.column_name AS name,c.data_type AS type,` +
13
+ `CASE WHEN c.is_nullable='NO' THEN 1 ELSE 0 END AS "notnull",` +
14
+ `c.column_default AS dflt_value,` +
15
+ `CASE WHEN EXISTS (SELECT 1 FROM pg_index i JOIN pg_attribute a ON a.attrelid=i.indrelid AND a.attnum=ANY(i.indkey) ` +
16
+ `WHERE i.indrelid=(quote_ident(c.table_schema)||'.'||quote_ident(c.table_name))::regclass AND i.indisprimary AND a.attname=c.column_name) ` +
17
+ `THEN 1 ELSE 0 END AS pk ` +
18
+ `FROM information_schema.columns c WHERE c.table_schema=current_schema() AND c.table_name='${tableName}' ORDER BY c.ordinal_position`;
19
+ }
20
+
21
+ const isIdentStart = (ch: string | undefined): boolean => !!ch && /[A-Za-z_]/.test(ch);
22
+ const isIdentChar = (ch: string | undefined): boolean => !!ch && /[A-Za-z0-9_$]/.test(ch);
23
+
24
+ function skipQuoted(sql: string, index: number): number {
25
+ const quote = sql[index];
26
+ let i = index + 1;
27
+ while (i < sql.length) {
28
+ if (sql[i] === quote) {
29
+ if (sql[i + 1] === quote) { i += 2; continue; }
30
+ return i + 1;
31
+ }
32
+ i++;
33
+ }
34
+ return i;
35
+ }
36
+
37
+ function skipComment(sql: string, index: number): number {
38
+ if (sql[index + 1] === "-") {
39
+ const end = sql.indexOf("\n", index + 2);
40
+ return end < 0 ? sql.length : end;
41
+ }
42
+ const end = sql.indexOf("*/", index + 2);
43
+ return end < 0 ? sql.length : end + 2;
44
+ }
45
+
46
+ function hasTopLevelComma(sql: string, openIndex: number): boolean {
47
+ let depth = 0;
48
+ for (let i = openIndex; i < sql.length; i++) {
49
+ const ch = sql[i];
50
+ if (ch === "'" || ch === '"') { i = skipQuoted(sql, i) - 1; continue; }
51
+ if (ch === "-" && sql[i + 1] === "-") { i = skipComment(sql, i) - 1; continue; }
52
+ if (ch === "/" && sql[i + 1] === "*") { i = skipComment(sql, i) - 1; continue; }
53
+ if (ch === "(") depth++;
54
+ else if (ch === ")") { depth--; if (depth === 0) return false; }
55
+ else if (ch === "," && depth === 1) return true;
56
+ }
57
+ return false;
58
+ }
59
+
60
+ function wordBefore(sql: string, index: number): string {
61
+ let i = index - 1;
62
+ while (i >= 0 && /\s/.test(sql.charAt(i))) i--;
63
+ const end = i + 1;
64
+ while (i >= 0 && isIdentChar(sql[i])) i--;
65
+ return sql.slice(i + 1, end).toLowerCase();
66
+ }
67
+
68
+ function wordAfter(sql: string, index: number): string {
69
+ let i = index + 1;
70
+ while (i < sql.length && /\s/.test(sql.charAt(i))) i++;
71
+ const start = i;
72
+ while (i < sql.length && isIdentChar(sql[i])) i++;
73
+ return sql.slice(start, i).toLowerCase();
74
+ }
75
+
76
+ function scan(sql: string): string {
77
+ let out = "";
78
+ let index = 0;
79
+ let parameter = 0;
80
+ while (index < sql.length) {
81
+ const ch = sql.charAt(index);
82
+ if (ch === "'" || ch === '"') {
83
+ const end = skipQuoted(sql, index);
84
+ out += sql.slice(index, end);
85
+ index = end;
86
+ continue;
87
+ }
88
+ if (ch === "-" && sql[index + 1] === "-") {
89
+ const end = skipComment(sql, index);
90
+ out += sql.slice(index, end);
91
+ index = end;
92
+ continue;
93
+ }
94
+ if (ch === "/" && sql[index + 1] === "*") {
95
+ const end = skipComment(sql, index);
96
+ out += sql.slice(index, end);
97
+ index = end;
98
+ continue;
99
+ }
100
+ if (ch === "?") {
101
+ // CASE WHEN ? needs a boolean; a bare `? IS NULL` has no type to infer.
102
+ const nextWord = wordAfter(sql, index);
103
+ const cast = (wordBefore(sql, index) === "when" && nextWord === "then") || nextWord === "is";
104
+ out += `$${++parameter}${cast ? (nextWord === "is" ? "::text" : "::boolean") : ""}`;
105
+ index++;
106
+ continue;
107
+ }
108
+ if (isIdentStart(ch)) {
109
+ let end = index + 1;
110
+ while (isIdentChar(sql[end])) end++;
111
+ const word = sql.slice(index, end);
112
+ const lower = word.toLowerCase();
113
+ // SQLite's scalar max(a,b)/min(a,b); the one-argument aggregate forms stay.
114
+ if (lower === "max" || lower === "min") {
115
+ let next = end;
116
+ while (next < sql.length && /\s/.test(sql.charAt(next))) next++;
117
+ if (sql[next] === "(" && hasTopLevelComma(sql, next)) {
118
+ out += lower === "max" ? "GREATEST" : "LEAST";
119
+ index = end;
120
+ continue;
121
+ }
122
+ }
123
+ // Preserve camelCase result aliases: PostgreSQL folds unquoted identifiers.
124
+ if (lower === "as") {
125
+ const previous = wordBefore(sql, index);
126
+ let next = end;
127
+ while (next < sql.length && /\s/.test(sql.charAt(next))) next++;
128
+ if (isIdentStart(sql[next])) {
129
+ let aliasEnd = next + 1;
130
+ while (isIdentChar(sql[aliasEnd])) aliasEnd++;
131
+ const alias = sql.slice(next, aliasEnd);
132
+ if (/[A-Z]/.test(alias) && previous !== "default" && alias.toLowerCase() !== "identity") {
133
+ out += `AS "${alias}"`;
134
+ index = aliasEnd;
135
+ continue;
136
+ }
137
+ }
138
+ }
139
+ out += word;
140
+ index = end;
141
+ continue;
142
+ }
143
+ out += ch;
144
+ index++;
145
+ }
146
+ return out;
147
+ }
148
+
149
+ function appendOnConflictDoNothing(sql: string): string {
150
+ return `${sql.replace(/[\s;]+$/, "")} ON CONFLICT DO NOTHING`;
151
+ }
152
+
153
+ export function translate(sql: string): string {
154
+ let text = String(sql);
155
+ // Connection tuning pragmas have no PostgreSQL equivalent; foreign keys are
156
+ // always enforced and concurrency is handled by advisory locks.
157
+ text = text.replace(/PRAGMA\s+(?:busy_timeout|journal_mode|foreign_keys)\s*(?:=\s*[^;]+)?\s*;?/gi, "");
158
+ text = text.replace(PRAGMA_TABLE_INFO, (_, table) => pragmaTableInfo(table));
159
+ // SQLite INTEGER is 64-bit; PostgreSQL INTEGER is 32-bit and epoch
160
+ // milliseconds overflow it.
161
+ text = text.replace(/\bINTEGER\b/gi, "BIGINT");
162
+ text = text.replace(/\bBIGINT\s+PRIMARY\s+KEY\s+AUTOINCREMENT\b/gi, "BIGINT GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY");
163
+ // Manual `BEGIN IMMEDIATE` appears inside receiver-state's schema script.
164
+ text = text.replace(/\bBEGIN\s+(?:DEFERRED|IMMEDIATE|EXCLUSIVE)\b/gi, "BEGIN");
165
+ if (/\bINSERT\s+OR\s+IGNORE\b/i.test(text)) {
166
+ text = text.replace(/\bINSERT\s+OR\s+IGNORE\b/gi, "INSERT");
167
+ if (!/\bON\s+CONFLICT\b/i.test(text)) text = appendOnConflictDoNothing(text);
168
+ }
169
+ // SQLite treats 1/0 as booleans; PostgreSQL needs an explicit cast.
170
+ return scan(text).trim();
171
+ }
172
+
173
+ export function hasSql(text: string): boolean {
174
+ return text.replace(/[;\s]/g, "").length > 0;
175
+ }