opencode-gateway 0.2.3 → 0.2.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 (79) hide show
  1. package/dist/cli.js +0 -0
  2. package/dist/index.js +36910 -56
  3. package/dist/runtime/delay.d.ts +1 -0
  4. package/dist/store/database.d.ts +22 -0
  5. package/dist/store/migrations.d.ts +2 -2
  6. package/dist/store/sqlite.d.ts +2 -2
  7. package/package.json +9 -3
  8. package/dist/binding/execution.js +0 -1
  9. package/dist/binding/gateway.js +0 -1
  10. package/dist/binding/index.js +0 -4
  11. package/dist/binding/opencode.js +0 -1
  12. package/dist/cli/args.js +0 -53
  13. package/dist/cli/doctor.js +0 -49
  14. package/dist/cli/init.js +0 -40
  15. package/dist/cli/opencode-config-file.js +0 -18
  16. package/dist/cli/opencode-config.js +0 -194
  17. package/dist/cli/paths.js +0 -22
  18. package/dist/cli/templates.js +0 -41
  19. package/dist/config/cron.js +0 -52
  20. package/dist/config/gateway.js +0 -148
  21. package/dist/config/memory.js +0 -105
  22. package/dist/config/paths.js +0 -39
  23. package/dist/config/telegram.js +0 -91
  24. package/dist/cron/runtime.js +0 -402
  25. package/dist/delivery/telegram.js +0 -75
  26. package/dist/delivery/text.js +0 -175
  27. package/dist/gateway.js +0 -117
  28. package/dist/host/file-sender.js +0 -59
  29. package/dist/host/logger.js +0 -53
  30. package/dist/host/transport.js +0 -35
  31. package/dist/mailbox/router.js +0 -16
  32. package/dist/media/mime.js +0 -45
  33. package/dist/memory/prompt.js +0 -122
  34. package/dist/opencode/adapter.js +0 -340
  35. package/dist/opencode/driver-hub.js +0 -82
  36. package/dist/opencode/event-normalize.js +0 -48
  37. package/dist/opencode/event-stream.js +0 -65
  38. package/dist/opencode/events.js +0 -1
  39. package/dist/questions/client.js +0 -36
  40. package/dist/questions/format.js +0 -36
  41. package/dist/questions/normalize.js +0 -45
  42. package/dist/questions/parser.js +0 -96
  43. package/dist/questions/runtime.js +0 -195
  44. package/dist/questions/types.js +0 -1
  45. package/dist/runtime/attachments.js +0 -12
  46. package/dist/runtime/conversation-coordinator.js +0 -22
  47. package/dist/runtime/executor.js +0 -407
  48. package/dist/runtime/mailbox.js +0 -112
  49. package/dist/runtime/opencode-runner.js +0 -79
  50. package/dist/runtime/runtime-singleton.js +0 -28
  51. package/dist/session/context.js +0 -23
  52. package/dist/session/conversation-key.js +0 -3
  53. package/dist/session/switcher.js +0 -59
  54. package/dist/session/system-prompt.js +0 -52
  55. package/dist/store/migrations.js +0 -197
  56. package/dist/store/sqlite.js +0 -777
  57. package/dist/telegram/client.js +0 -180
  58. package/dist/telegram/media.js +0 -65
  59. package/dist/telegram/normalize.js +0 -119
  60. package/dist/telegram/poller.js +0 -166
  61. package/dist/telegram/runtime.js +0 -157
  62. package/dist/telegram/state.js +0 -149
  63. package/dist/telegram/types.js +0 -1
  64. package/dist/tools/channel-new-session.js +0 -27
  65. package/dist/tools/channel-send-file.js +0 -27
  66. package/dist/tools/channel-target.js +0 -34
  67. package/dist/tools/cron-run.js +0 -20
  68. package/dist/tools/cron-upsert.js +0 -51
  69. package/dist/tools/gateway-dispatch-cron.js +0 -33
  70. package/dist/tools/gateway-status.js +0 -25
  71. package/dist/tools/schedule-cancel.js +0 -12
  72. package/dist/tools/schedule-format.js +0 -48
  73. package/dist/tools/schedule-list.js +0 -17
  74. package/dist/tools/schedule-once.js +0 -43
  75. package/dist/tools/schedule-status.js +0 -23
  76. package/dist/tools/telegram-send-test.js +0 -26
  77. package/dist/tools/telegram-status.js +0 -49
  78. package/dist/tools/time.js +0 -25
  79. package/dist/utils/error.js +0 -57
@@ -1,59 +0,0 @@
1
- import { resolveConversationKeyForTarget } from "./conversation-key";
2
- export class ChannelSessionSwitcher {
3
- store;
4
- sessions;
5
- router;
6
- contract;
7
- opencode;
8
- telegramEnabled;
9
- constructor(store, sessions, router, contract, opencode, telegramEnabled) {
10
- this.store = store;
11
- this.sessions = sessions;
12
- this.router = router;
13
- this.contract = contract;
14
- this.opencode = opencode;
15
- this.telegramEnabled = telegramEnabled;
16
- }
17
- hasEnabledChannel() {
18
- return this.telegramEnabled;
19
- }
20
- async createAndSwitchSession(target, title) {
21
- assertSupportedTarget(target, this.telegramEnabled);
22
- const conversationKey = resolveConversationKeyForTarget(target, this.router, this.contract);
23
- const previousSessionId = this.store.getSessionBinding(conversationKey);
24
- const recordedAtMs = Date.now();
25
- const newSessionId = await this.opencode.createFreshSession(defaultSessionTitle(title, target));
26
- if (previousSessionId !== null) {
27
- this.store.deletePendingQuestionsForSession(previousSessionId);
28
- this.store.clearSessionReplyTargets(previousSessionId);
29
- }
30
- this.store.putSessionBinding(conversationKey, newSessionId, recordedAtMs);
31
- this.sessions.replaceReplyTargets(newSessionId, conversationKey, [target], recordedAtMs);
32
- return {
33
- channel: target.channel,
34
- target: target.target,
35
- topic: target.topic,
36
- conversationKey,
37
- previousSessionId,
38
- newSessionId,
39
- effectiveOn: "next_message",
40
- };
41
- }
42
- }
43
- function assertSupportedTarget(target, telegramEnabled) {
44
- if (target.channel !== "telegram") {
45
- throw new Error(`unsupported channel for session switching: ${target.channel}`);
46
- }
47
- if (!telegramEnabled) {
48
- throw new Error("telegram is not enabled");
49
- }
50
- }
51
- function defaultSessionTitle(title, target) {
52
- const normalized = title?.trim() ?? "";
53
- if (normalized.length > 0) {
54
- return normalized;
55
- }
56
- return target.topic === null
57
- ? `Gateway ${target.channel}:${target.target}`
58
- : `Gateway ${target.channel}:${target.target} topic ${target.topic}`;
59
- }
@@ -1,52 +0,0 @@
1
- export class GatewaySystemPromptBuilder {
2
- sessions;
3
- memory;
4
- constructor(sessions, memory) {
5
- this.sessions = sessions;
6
- this.memory = memory;
7
- }
8
- async buildPrompts(sessionId) {
9
- if (!this.sessions.isGatewaySession(sessionId)) {
10
- return [];
11
- }
12
- const prompts = [];
13
- const gatewayPrompt = buildGatewayContextPrompt(this.sessions.listReplyTargets(sessionId));
14
- if (gatewayPrompt !== null) {
15
- prompts.push(gatewayPrompt);
16
- }
17
- const memoryPrompt = await this.memory.buildPrompt();
18
- if (memoryPrompt !== null) {
19
- prompts.push(memoryPrompt);
20
- }
21
- return prompts;
22
- }
23
- }
24
- function buildGatewayContextPrompt(targets) {
25
- if (targets.length === 0) {
26
- return null;
27
- }
28
- if (targets.length === 1) {
29
- const target = targets[0];
30
- return [
31
- "Gateway context:",
32
- `- Current message source channel: ${target.channel}`,
33
- `- Current reply target id: ${target.target}`,
34
- `- Current reply topic: ${target.topic ?? "none"}`,
35
- "- Unless the user explicitly asks otherwise, channel-aware actions should default to this target.",
36
- "- If the user asks to start a fresh channel session, use channel_new_session.",
37
- "- If the user asks for a one-shot reminder or relative-time follow-up, prefer schedule_once.",
38
- "- If the user asks for a recurring schedule, prefer cron_upsert.",
39
- "- Use schedule_list and schedule_status to inspect existing scheduled jobs and recent run results.",
40
- "- Scheduled results delivered to this channel are automatically appended to this session as context.",
41
- ].join("\n");
42
- }
43
- return [
44
- "Gateway context:",
45
- `- This session currently fans out to ${targets.length} reply targets.`,
46
- ...targets.map((target, index) => `- Target ${index + 1}: channel=${target.channel}, id=${target.target}, topic=${target.topic ?? "none"}`),
47
- "- If a tool needs a single explicit target, do not guess; ask the user or use explicit tool arguments.",
48
- "- If the user asks to start a fresh channel session for this route, use channel_new_session.",
49
- "- Prefer schedule_once for one-shot reminders and cron_upsert for recurring schedules.",
50
- "- Use schedule_list and schedule_status to inspect scheduled jobs and recent run results.",
51
- ].join("\n");
52
- }
@@ -1,197 +0,0 @@
1
- const LATEST_SCHEMA_VERSION = 7;
2
- export function migrateGatewayDatabase(db) {
3
- db.exec("PRAGMA journal_mode = WAL;");
4
- db.exec("PRAGMA foreign_keys = ON;");
5
- let currentVersion = readUserVersion(db);
6
- if (currentVersion > LATEST_SCHEMA_VERSION) {
7
- throw new Error(`unsupported gateway database schema version: ${currentVersion}`);
8
- }
9
- if (currentVersion === 0) {
10
- migrateToV1(db);
11
- currentVersion = 1;
12
- }
13
- if (currentVersion === 1) {
14
- migrateToV2(db);
15
- currentVersion = 2;
16
- }
17
- if (currentVersion === 2) {
18
- migrateToV3(db);
19
- currentVersion = 3;
20
- }
21
- if (currentVersion === 3) {
22
- migrateToV4(db);
23
- currentVersion = 4;
24
- }
25
- if (currentVersion === 4) {
26
- migrateToV5(db);
27
- currentVersion = 5;
28
- }
29
- if (currentVersion === 5) {
30
- migrateToV6(db);
31
- currentVersion = 6;
32
- }
33
- if (currentVersion === 6) {
34
- migrateToV7(db);
35
- }
36
- }
37
- function readUserVersion(db) {
38
- const row = db.query("PRAGMA user_version;").get();
39
- return row?.user_version ?? 0;
40
- }
41
- function migrateToV1(db) {
42
- db.exec(`
43
- CREATE TABLE session_bindings (
44
- conversation_key TEXT PRIMARY KEY NOT NULL,
45
- session_id TEXT NOT NULL,
46
- updated_at_ms INTEGER NOT NULL
47
- );
48
-
49
- CREATE TABLE runtime_journal (
50
- id INTEGER PRIMARY KEY AUTOINCREMENT,
51
- kind TEXT NOT NULL,
52
- recorded_at_ms INTEGER NOT NULL,
53
- conversation_key TEXT,
54
- payload_json TEXT NOT NULL
55
- );
56
-
57
- CREATE INDEX runtime_journal_kind_recorded_at_ms_idx
58
- ON runtime_journal (kind, recorded_at_ms);
59
- `);
60
- db.exec("PRAGMA user_version = 1;");
61
- }
62
- function migrateToV2(db) {
63
- db.exec(`
64
- CREATE TABLE kv_state (
65
- key TEXT PRIMARY KEY NOT NULL,
66
- value TEXT NOT NULL,
67
- updated_at_ms INTEGER NOT NULL
68
- );
69
- `);
70
- db.exec("PRAGMA user_version = 2;");
71
- }
72
- function migrateToV3(db) {
73
- db.exec(`
74
- CREATE TABLE cron_jobs (
75
- id TEXT PRIMARY KEY NOT NULL,
76
- schedule TEXT NOT NULL,
77
- prompt TEXT NOT NULL,
78
- delivery_channel TEXT,
79
- delivery_target TEXT,
80
- delivery_topic TEXT,
81
- enabled INTEGER NOT NULL,
82
- next_run_at_ms INTEGER NOT NULL,
83
- created_at_ms INTEGER NOT NULL,
84
- updated_at_ms INTEGER NOT NULL
85
- );
86
-
87
- CREATE INDEX cron_jobs_enabled_next_run_at_ms_idx
88
- ON cron_jobs (enabled, next_run_at_ms);
89
-
90
- CREATE TABLE cron_runs (
91
- id INTEGER PRIMARY KEY AUTOINCREMENT,
92
- job_id TEXT NOT NULL,
93
- scheduled_for_ms INTEGER NOT NULL,
94
- started_at_ms INTEGER NOT NULL,
95
- finished_at_ms INTEGER,
96
- status TEXT NOT NULL,
97
- response_text TEXT,
98
- error_message TEXT
99
- );
100
-
101
- CREATE INDEX cron_runs_job_id_started_at_ms_idx
102
- ON cron_runs (job_id, started_at_ms DESC);
103
-
104
- CREATE INDEX cron_runs_status_started_at_ms_idx
105
- ON cron_runs (status, started_at_ms DESC);
106
- `);
107
- db.exec("PRAGMA user_version = 3;");
108
- }
109
- function migrateToV4(db) {
110
- db.exec(`
111
- CREATE TABLE mailbox_entries (
112
- id INTEGER PRIMARY KEY AUTOINCREMENT,
113
- mailbox_key TEXT NOT NULL,
114
- source_kind TEXT NOT NULL,
115
- external_id TEXT NOT NULL,
116
- sender TEXT NOT NULL,
117
- body TEXT NOT NULL,
118
- reply_channel TEXT,
119
- reply_target TEXT,
120
- reply_topic TEXT,
121
- created_at_ms INTEGER NOT NULL
122
- );
123
-
124
- CREATE UNIQUE INDEX mailbox_entries_source_kind_external_id_idx
125
- ON mailbox_entries (source_kind, external_id);
126
-
127
- CREATE INDEX mailbox_entries_mailbox_key_id_idx
128
- ON mailbox_entries (mailbox_key, id);
129
- `);
130
- db.exec("PRAGMA user_version = 4;");
131
- }
132
- function migrateToV5(db) {
133
- db.exec(`
134
- CREATE TABLE mailbox_entry_attachments (
135
- mailbox_entry_id INTEGER NOT NULL REFERENCES mailbox_entries(id) ON DELETE CASCADE,
136
- ordinal INTEGER NOT NULL,
137
- kind TEXT NOT NULL,
138
- mime_type TEXT NOT NULL,
139
- file_name TEXT,
140
- local_path TEXT NOT NULL,
141
- PRIMARY KEY (mailbox_entry_id, ordinal)
142
- );
143
-
144
- CREATE INDEX mailbox_entry_attachments_entry_id_ordinal_idx
145
- ON mailbox_entry_attachments (mailbox_entry_id, ordinal);
146
- `);
147
- db.exec("PRAGMA user_version = 5;");
148
- }
149
- function migrateToV6(db) {
150
- db.exec(`
151
- CREATE TABLE session_reply_targets (
152
- session_id TEXT NOT NULL,
153
- ordinal INTEGER NOT NULL,
154
- conversation_key TEXT NOT NULL,
155
- delivery_channel TEXT NOT NULL,
156
- delivery_target TEXT NOT NULL,
157
- delivery_topic TEXT NOT NULL,
158
- updated_at_ms INTEGER NOT NULL,
159
- PRIMARY KEY (session_id, ordinal)
160
- );
161
-
162
- CREATE INDEX session_reply_targets_conversation_key_updated_at_ms_idx
163
- ON session_reply_targets (conversation_key, updated_at_ms DESC);
164
-
165
- CREATE TABLE pending_questions (
166
- id INTEGER PRIMARY KEY AUTOINCREMENT,
167
- request_id TEXT NOT NULL,
168
- session_id TEXT NOT NULL,
169
- delivery_channel TEXT NOT NULL,
170
- delivery_target TEXT NOT NULL,
171
- delivery_topic TEXT NOT NULL,
172
- question_json TEXT NOT NULL,
173
- telegram_message_id INTEGER,
174
- created_at_ms INTEGER NOT NULL
175
- );
176
-
177
- CREATE UNIQUE INDEX pending_questions_request_target_topic_idx
178
- ON pending_questions (request_id, delivery_channel, delivery_target, delivery_topic);
179
-
180
- CREATE INDEX pending_questions_target_topic_created_at_ms_idx
181
- ON pending_questions (delivery_channel, delivery_target, delivery_topic, created_at_ms);
182
-
183
- CREATE INDEX pending_questions_session_id_created_at_ms_idx
184
- ON pending_questions (session_id, created_at_ms);
185
- `);
186
- db.exec("PRAGMA user_version = 6;");
187
- }
188
- function migrateToV7(db) {
189
- db.exec(`
190
- ALTER TABLE cron_jobs
191
- ADD COLUMN kind TEXT NOT NULL DEFAULT 'cron';
192
-
193
- ALTER TABLE cron_jobs
194
- ADD COLUMN run_at_ms INTEGER;
195
- `);
196
- db.exec(`PRAGMA user_version = ${LATEST_SCHEMA_VERSION};`);
197
- }