@wheatley-ai/cli 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +15 -0
- package/bin/wheatley.js +2 -0
- package/dist/approvals/gate.js +278 -0
- package/dist/approvals/module.js +21 -0
- package/dist/approvals/policy.js +30 -0
- package/dist/approvals/preview.js +17 -0
- package/dist/approvals/prompt.js +98 -0
- package/dist/approvals/rpc.js +16 -0
- package/dist/approvals/store.js +159 -0
- package/dist/cli/ask.js +94 -0
- package/dist/cli/chat.js +180 -0
- package/dist/cli/client.js +104 -0
- package/dist/cli/daemon.js +35 -0
- package/dist/cli/init.js +72 -0
- package/dist/cli/io.js +45 -0
- package/dist/cli/main.js +204 -0
- package/dist/cli/session.js +87 -0
- package/dist/cli/status.js +46 -0
- package/dist/contracts/approval-gate.js +1 -0
- package/dist/contracts/config-store.js +1 -0
- package/dist/contracts/delivery-outbox.js +1 -0
- package/dist/contracts/errors.js +28 -0
- package/dist/contracts/extension-catalog.js +1 -0
- package/dist/contracts/maintenance-runner.js +1 -0
- package/dist/contracts/memory-store.js +1 -0
- package/dist/contracts/module.js +7 -0
- package/dist/contracts/tool-names.js +43 -0
- package/dist/contracts/trigger-runtime.js +1 -0
- package/dist/contracts/webhook-ingress.js +2 -0
- package/dist/core/agent/agent-store.js +300 -0
- package/dist/core/agent/agent.js +1020 -0
- package/dist/core/agent/gated-models.js +89 -0
- package/dist/core/agent/pi-adapter.js +401 -0
- package/dist/core/agent/policy-gate.js +44 -0
- package/dist/core/builtin-catalog.js +20 -0
- package/dist/core/concurrency-gate.js +87 -0
- package/dist/core/config/atomic-file.js +61 -0
- package/dist/core/config/config-store.js +138 -0
- package/dist/core/config/pending-restart.js +53 -0
- package/dist/core/config/secret-references.js +69 -0
- package/dist/core/config/secret-store.js +147 -0
- package/dist/core/config/validate.js +55 -0
- package/dist/core/config/yaml-patch.js +57 -0
- package/dist/core/daemon.js +88 -0
- package/dist/core/deployment.js +347 -0
- package/dist/core/error-message.js +4 -0
- package/dist/core/events.js +25 -0
- package/dist/core/logger.js +81 -0
- package/dist/core/models.js +27 -0
- package/dist/core/module.js +42 -0
- package/dist/core/rpc/agent-handlers.js +82 -0
- package/dist/core/rpc/deployment-handlers.js +63 -0
- package/dist/core/rpc/router.js +95 -0
- package/dist/core/server/connection.js +170 -0
- package/dist/core/server/control-server.js +152 -0
- package/dist/core/server/static-files.js +79 -0
- package/dist/core/supervisor.js +206 -0
- package/dist/core/tool-routing.js +41 -0
- package/dist/dashboard/assets/index-3COz_MZq.css +2 -0
- package/dist/dashboard/assets/index-RnxedkKh.js +11 -0
- package/dist/dashboard/assets/sheet-presentation-U1UUymXc.js +1 -0
- package/dist/dashboard/index.html +13 -0
- package/dist/db/database.js +16 -0
- package/dist/db/migrations.js +184 -0
- package/dist/frontends/discord/approvals.js +199 -0
- package/dist/frontends/discord/attachments.js +115 -0
- package/dist/frontends/discord/binding.js +489 -0
- package/dist/frontends/discord/config.js +107 -0
- package/dist/frontends/discord/discord-runtime.js +52 -0
- package/dist/frontends/discord/errors.js +61 -0
- package/dist/frontends/discord/live-runs.js +189 -0
- package/dist/frontends/discord/module.js +39 -0
- package/dist/frontends/discord/recent-ids.js +20 -0
- package/dist/frontends/slack/approval-blocks.js +98 -0
- package/dist/frontends/slack/attachments.js +102 -0
- package/dist/frontends/slack/background-tasks.js +20 -0
- package/dist/frontends/slack/binding.js +405 -0
- package/dist/frontends/slack/bounded.js +9 -0
- package/dist/frontends/slack/chat-id.js +17 -0
- package/dist/frontends/slack/config.js +80 -0
- package/dist/frontends/slack/delivery-errors.js +81 -0
- package/dist/frontends/slack/inbound.js +76 -0
- package/dist/frontends/slack/live-progress.js +190 -0
- package/dist/frontends/slack/manifest.js +66 -0
- package/dist/frontends/slack/outbound.js +160 -0
- package/dist/frontends/slack/schemas.js +99 -0
- package/dist/frontends/slack/signature.js +30 -0
- package/dist/frontends/slack/slack-client.js +60 -0
- package/dist/frontends/slack/slack-frontend.js +38 -0
- package/dist/frontends/slack/state-store.js +112 -0
- package/dist/frontends/telegram/approvals.js +134 -0
- package/dist/frontends/telegram/attachments.js +126 -0
- package/dist/frontends/telegram/binding.js +144 -0
- package/dist/frontends/telegram/chat-key.js +43 -0
- package/dist/frontends/telegram/client.js +88 -0
- package/dist/frontends/telegram/config.js +93 -0
- package/dist/frontends/telegram/delivery.js +215 -0
- package/dist/frontends/telegram/frontend.js +42 -0
- package/dist/frontends/telegram/grammy-client.js +122 -0
- package/dist/frontends/telegram/http-status.js +6 -0
- package/dist/frontends/telegram/inbound.js +149 -0
- package/dist/frontends/telegram/live.js +182 -0
- package/dist/frontends/telegram/markdown.js +103 -0
- package/dist/frontends/telegram/recent-set.js +18 -0
- package/dist/frontends/telegram/serial-queue.js +23 -0
- package/dist/index.js +7 -0
- package/dist/mcp/content.js +61 -0
- package/dist/mcp/mcp-module.js +35 -0
- package/dist/mcp/mcp-tool-provider.js +93 -0
- package/dist/mcp/server-connection.js +297 -0
- package/dist/memory/consolidation.js +260 -0
- package/dist/memory/format.js +5 -0
- package/dist/memory/migrations.js +76 -0
- package/dist/memory/module.js +69 -0
- package/dist/memory/names.js +20 -0
- package/dist/memory/rows.js +22 -0
- package/dist/memory/rpc.js +49 -0
- package/dist/memory/snapshots.js +12 -0
- package/dist/memory/store.js +281 -0
- package/dist/memory/tools.js +158 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/config.d.ts +252 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/config.js +110 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/domain.d.ts +512 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/domain.js +441 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/duration.d.ts +8 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/duration.js +28 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/events.d.ts +234 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/events.js +124 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/index.d.ts +11 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/index.js +11 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json-rpc.d.ts +81 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json-rpc.js +157 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json.d.ts +14 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/json.js +23 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/limits.d.ts +17 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/limits.js +17 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/methods.d.ts +1107 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/methods.js +201 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/rpc-error.d.ts +45 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/rpc-error.js +44 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/validation.d.ts +20 -0
- package/dist/node_modules/@wheatley-ai/protocol/dist/validation.js +11 -0
- package/dist/node_modules/@wheatley-ai/protocol/package.json +14 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/chunk.d.ts +11 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/chunk.js +162 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/commands.d.ts +48 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/commands.js +39 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/delivery-error.d.ts +29 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/delivery-error.js +27 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/frontend.d.ts +236 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/frontend.js +2 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/index.d.ts +10 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/index.js +7 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/json.d.ts +9 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/json.js +2 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/logger.d.ts +18 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/logger.js +2 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/plugin.d.ts +48 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/plugin.js +5 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/schema.d.ts +69 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/schema.js +3 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/tool.d.ts +69 -0
- package/dist/node_modules/@wheatley-ai/sdk/dist/tool.js +3 -0
- package/dist/node_modules/@wheatley-ai/sdk/package.json +11 -0
- package/dist/ops/backup.js +185 -0
- package/dist/ops/cli.js +195 -0
- package/dist/ops/command-runner.js +34 -0
- package/dist/ops/dashboard-assets.js +37 -0
- package/dist/ops/migrate.js +63 -0
- package/dist/ops/module.js +47 -0
- package/dist/ops/paths.js +55 -0
- package/dist/ops/service.js +376 -0
- package/dist/ops/update.js +145 -0
- package/dist/plugins/contributions.js +89 -0
- package/dist/plugins/guards.js +111 -0
- package/dist/plugins/load.js +76 -0
- package/dist/plugins/module.js +31 -0
- package/dist/plugins/provider.js +349 -0
- package/dist/plugins/schema.js +69 -0
- package/dist/skills/disclosure.js +142 -0
- package/dist/skills/discovery.js +120 -0
- package/dist/skills/fs-errors.js +8 -0
- package/dist/skills/read-skill-tool.js +3 -0
- package/dist/skills/skill-file.js +90 -0
- package/dist/skills/skills.js +140 -0
- package/dist/tools/builtin-tools.js +61 -0
- package/dist/tools/input.js +45 -0
- package/dist/tools/paths.js +6 -0
- package/dist/tools/read-file.js +65 -0
- package/dist/tools/shell.js +156 -0
- package/dist/tools/write-file.js +58 -0
- package/dist/triggers/delivery-codec.js +99 -0
- package/dist/triggers/delivery-outbox.js +313 -0
- package/dist/triggers/due-timer.js +14 -0
- package/dist/triggers/errors.js +2 -0
- package/dist/triggers/migrations.js +91 -0
- package/dist/triggers/module.js +104 -0
- package/dist/triggers/rpc.js +35 -0
- package/dist/triggers/schedule.js +52 -0
- package/dist/triggers/sql-row.js +59 -0
- package/dist/triggers/trigger-runtime.js +445 -0
- package/dist/triggers/trigger-tools.js +207 -0
- package/dist/triggers/webhook-ingress.js +216 -0
- package/dist/wiring/agent-services.js +144 -0
- package/dist/wiring/catalog.js +81 -0
- package/dist/wiring/daemon.js +83 -0
- package/dist/wiring/frontend-host.js +268 -0
- package/package.json +50 -3
- package/README.md +0 -3
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import { RunOriginSchema, RunTimelineItemSchema, decode, isJsonValue, } from "@wheatley-ai/protocol";
|
|
2
|
+
/** The migration namespace of the core Agent tables in `agent.sqlite`. */
|
|
3
|
+
export const CORE_NAMESPACE = "core";
|
|
4
|
+
/** The core tables: Chats and their current Conversation, Runs, Chat messages, and Run timelines. */
|
|
5
|
+
export const coreMigrations = [
|
|
6
|
+
{
|
|
7
|
+
name: "create chats, runs, messages, and timeline",
|
|
8
|
+
up: (db) => {
|
|
9
|
+
db.exec(`
|
|
10
|
+
CREATE TABLE core_chats (
|
|
11
|
+
binding_id TEXT NOT NULL,
|
|
12
|
+
chat_id TEXT NOT NULL,
|
|
13
|
+
conversation_id TEXT NOT NULL,
|
|
14
|
+
PRIMARY KEY (binding_id, chat_id)
|
|
15
|
+
) STRICT;
|
|
16
|
+
CREATE TABLE core_trigger_conversations (
|
|
17
|
+
trigger_id TEXT PRIMARY KEY,
|
|
18
|
+
conversation_id TEXT NOT NULL
|
|
19
|
+
) STRICT;
|
|
20
|
+
CREATE TABLE core_runs (
|
|
21
|
+
seq INTEGER PRIMARY KEY,
|
|
22
|
+
run_id TEXT NOT NULL UNIQUE,
|
|
23
|
+
conversation_id TEXT NOT NULL,
|
|
24
|
+
origin TEXT NOT NULL,
|
|
25
|
+
interactive INTEGER NOT NULL,
|
|
26
|
+
delivery TEXT,
|
|
27
|
+
status TEXT NOT NULL,
|
|
28
|
+
submission_id TEXT,
|
|
29
|
+
started_at TEXT NOT NULL,
|
|
30
|
+
ended_at TEXT,
|
|
31
|
+
error TEXT,
|
|
32
|
+
model TEXT,
|
|
33
|
+
input_tokens INTEGER,
|
|
34
|
+
output_tokens INTEGER,
|
|
35
|
+
cost_usd REAL
|
|
36
|
+
) STRICT;
|
|
37
|
+
CREATE INDEX core_runs_status ON core_runs (status);
|
|
38
|
+
CREATE TABLE core_messages (
|
|
39
|
+
seq INTEGER PRIMARY KEY,
|
|
40
|
+
message_id TEXT NOT NULL UNIQUE,
|
|
41
|
+
conversation_id TEXT NOT NULL,
|
|
42
|
+
binding_id TEXT,
|
|
43
|
+
chat_id TEXT,
|
|
44
|
+
role TEXT NOT NULL,
|
|
45
|
+
text TEXT NOT NULL,
|
|
46
|
+
run_id TEXT,
|
|
47
|
+
sender TEXT,
|
|
48
|
+
source TEXT,
|
|
49
|
+
at TEXT NOT NULL
|
|
50
|
+
) STRICT;
|
|
51
|
+
CREATE INDEX core_messages_chat ON core_messages (binding_id, chat_id, seq);
|
|
52
|
+
CREATE TABLE core_timeline (
|
|
53
|
+
seq INTEGER PRIMARY KEY,
|
|
54
|
+
run_id TEXT NOT NULL,
|
|
55
|
+
item_key TEXT NOT NULL UNIQUE,
|
|
56
|
+
item TEXT NOT NULL
|
|
57
|
+
) STRICT;
|
|
58
|
+
CREATE INDEX core_timeline_run ON core_timeline (run_id, seq);
|
|
59
|
+
`);
|
|
60
|
+
},
|
|
61
|
+
version: 1,
|
|
62
|
+
},
|
|
63
|
+
];
|
|
64
|
+
const isText = (value) => typeof value === "string";
|
|
65
|
+
const isNumber = (value) => typeof value === "number";
|
|
66
|
+
const textOf = (row, column) => {
|
|
67
|
+
const value = row[column];
|
|
68
|
+
if (!isText(value)) {
|
|
69
|
+
throw new Error(`Column ${column} is not text`);
|
|
70
|
+
}
|
|
71
|
+
return value;
|
|
72
|
+
};
|
|
73
|
+
const optionalText = (row, column) => {
|
|
74
|
+
const value = row[column];
|
|
75
|
+
return isText(value) ? value : undefined;
|
|
76
|
+
};
|
|
77
|
+
const optionalNumber = (row, column) => {
|
|
78
|
+
const value = row[column];
|
|
79
|
+
return isNumber(value) ? value : undefined;
|
|
80
|
+
};
|
|
81
|
+
const isRunStatus = (value) => value === "running" ||
|
|
82
|
+
value === "waiting_approval" ||
|
|
83
|
+
value === "completed" ||
|
|
84
|
+
value === "failed" ||
|
|
85
|
+
value === "stopped";
|
|
86
|
+
const isRole = (value) => value === "user" || value === "assistant";
|
|
87
|
+
const isSource = (value) => value === "reply" || value === "command" || value === "trigger";
|
|
88
|
+
const timelineItemRow = (row) => {
|
|
89
|
+
const value = JSON.parse(textOf(row, "item"));
|
|
90
|
+
const decoded = isJsonValue(value)
|
|
91
|
+
? decode(RunTimelineItemSchema, value)
|
|
92
|
+
: undefined;
|
|
93
|
+
if (decoded === undefined || !decoded.ok) {
|
|
94
|
+
throw new Error("A stored timeline item is invalid");
|
|
95
|
+
}
|
|
96
|
+
return decoded.value;
|
|
97
|
+
};
|
|
98
|
+
const messageRow = (row) => {
|
|
99
|
+
const role = textOf(row, "role");
|
|
100
|
+
const source = optionalText(row, "source");
|
|
101
|
+
if (!isRole(role)) {
|
|
102
|
+
throw new Error(`A stored message has the role ${role}`);
|
|
103
|
+
}
|
|
104
|
+
const message = {
|
|
105
|
+
at: textOf(row, "at"),
|
|
106
|
+
messageId: textOf(row, "message_id"),
|
|
107
|
+
role,
|
|
108
|
+
text: textOf(row, "text"),
|
|
109
|
+
};
|
|
110
|
+
const runId = optionalText(row, "run_id");
|
|
111
|
+
const sender = optionalText(row, "sender");
|
|
112
|
+
if (runId !== undefined) {
|
|
113
|
+
message.runId = runId;
|
|
114
|
+
}
|
|
115
|
+
if (sender !== undefined) {
|
|
116
|
+
message.senderDisplayName = sender;
|
|
117
|
+
}
|
|
118
|
+
if (source !== undefined && isSource(source)) {
|
|
119
|
+
message.source = source;
|
|
120
|
+
}
|
|
121
|
+
return message;
|
|
122
|
+
};
|
|
123
|
+
const isRunDelivery = (value) => typeof value === "object" &&
|
|
124
|
+
value !== null &&
|
|
125
|
+
"bindingId" in value &&
|
|
126
|
+
typeof value.bindingId === "string" &&
|
|
127
|
+
"chatId" in value &&
|
|
128
|
+
typeof value.chatId === "string" &&
|
|
129
|
+
"source" in value &&
|
|
130
|
+
(value.source === "reply" || value.source === "trigger");
|
|
131
|
+
/** Reads and writes the core tables of one `agent.sqlite`. Every method runs synchronously in one statement. */
|
|
132
|
+
export class AgentStore {
|
|
133
|
+
#db;
|
|
134
|
+
#agent;
|
|
135
|
+
constructor(db, agent) {
|
|
136
|
+
this.#db = db;
|
|
137
|
+
this.#agent = agent;
|
|
138
|
+
}
|
|
139
|
+
/** The current Conversation of a Chat, or `undefined` before its first message. */
|
|
140
|
+
chatConversation = (bindingId, chatId) => {
|
|
141
|
+
const row = this.#db
|
|
142
|
+
.prepare("SELECT conversation_id FROM core_chats WHERE binding_id = ? AND chat_id = ?")
|
|
143
|
+
.get(bindingId, chatId);
|
|
144
|
+
return row === undefined ? undefined : textOf(row, "conversation_id");
|
|
145
|
+
};
|
|
146
|
+
/** Points a Chat at a new current Conversation. */
|
|
147
|
+
setChatConversation = (bindingId, chatId, conversationId) => {
|
|
148
|
+
this.#db
|
|
149
|
+
.prepare(`INSERT INTO core_chats (binding_id, chat_id, conversation_id) VALUES (?, ?, ?)
|
|
150
|
+
ON CONFLICT (binding_id, chat_id) DO UPDATE SET conversation_id = excluded.conversation_id`)
|
|
151
|
+
.run(bindingId, chatId, conversationId);
|
|
152
|
+
};
|
|
153
|
+
/** The shared Conversation of a Trigger, or `undefined` before its first `shared` firing. */
|
|
154
|
+
triggerConversation = (triggerId) => {
|
|
155
|
+
const row = this.#db
|
|
156
|
+
.prepare("SELECT conversation_id FROM core_trigger_conversations WHERE trigger_id = ?")
|
|
157
|
+
.get(triggerId);
|
|
158
|
+
return row === undefined ? undefined : textOf(row, "conversation_id");
|
|
159
|
+
};
|
|
160
|
+
setTriggerConversation = (triggerId, conversationId) => {
|
|
161
|
+
this.#db
|
|
162
|
+
.prepare(`INSERT INTO core_trigger_conversations (trigger_id, conversation_id) VALUES (?, ?)
|
|
163
|
+
ON CONFLICT (trigger_id) DO UPDATE SET conversation_id = excluded.conversation_id`)
|
|
164
|
+
.run(triggerId, conversationId);
|
|
165
|
+
};
|
|
166
|
+
insertRun = (run) => {
|
|
167
|
+
this.#db
|
|
168
|
+
.prepare(`INSERT INTO core_runs (run_id, conversation_id, origin, interactive, delivery, status, started_at, model)
|
|
169
|
+
VALUES (?, ?, ?, ?, ?, 'running', ?, ?)`)
|
|
170
|
+
.run(run.runId, run.conversationId, JSON.stringify(run.origin), run.interactive ? 1 : 0, run.delivery === null ? null : JSON.stringify(run.delivery), run.startedAt, run.model ?? null);
|
|
171
|
+
};
|
|
172
|
+
setSubmission = (runId, submissionId) => {
|
|
173
|
+
this.#db
|
|
174
|
+
.prepare("UPDATE core_runs SET submission_id = ? WHERE run_id = ?")
|
|
175
|
+
.run(submissionId, runId);
|
|
176
|
+
};
|
|
177
|
+
/** Moves an active Run between `running` and `waiting_approval`. Resolves `false` if the Run already ended. */
|
|
178
|
+
setActiveStatus = (runId, status) => this.#db
|
|
179
|
+
.prepare("UPDATE core_runs SET status = ? WHERE run_id = ? AND status IN ('running', 'waiting_approval') AND status <> ?")
|
|
180
|
+
.run(status, runId, status).changes > 0;
|
|
181
|
+
/** Ends an active Run. Resolves `false` if it already ended. */
|
|
182
|
+
endRun = (runId, end) => this.#db
|
|
183
|
+
.prepare(`UPDATE core_runs SET status = ?, ended_at = ?, error = ?, input_tokens = ?, output_tokens = ?, cost_usd = ?
|
|
184
|
+
WHERE run_id = ? AND status IN ('running', 'waiting_approval')`)
|
|
185
|
+
.run(end.status, end.endedAt, end.error ?? null, end.usage?.inputTokens ?? null, end.usage?.outputTokens ?? null, end.usage?.costUsd ?? null, runId).changes > 0;
|
|
186
|
+
getRun = (runId) => {
|
|
187
|
+
const row = this.#db
|
|
188
|
+
.prepare("SELECT * FROM core_runs WHERE run_id = ?")
|
|
189
|
+
.get(runId);
|
|
190
|
+
return row === undefined ? undefined : this.#storedRun(row);
|
|
191
|
+
};
|
|
192
|
+
/** Runs that are `running` or `waiting_approval`, oldest first. */
|
|
193
|
+
activeRuns = () => this.#db
|
|
194
|
+
.prepare("SELECT * FROM core_runs WHERE status IN ('running', 'waiting_approval') ORDER BY seq")
|
|
195
|
+
.all()
|
|
196
|
+
.map((row) => this.#storedRun(row));
|
|
197
|
+
/** Runs newest first, after the Run `before` if given. Fetches `limit + 1` rows so callers can set `hasMore`. */
|
|
198
|
+
listRuns = (options) => {
|
|
199
|
+
const beforeSeq = options.before === undefined
|
|
200
|
+
? undefined
|
|
201
|
+
: optionalNumber(this.#db
|
|
202
|
+
.prepare("SELECT seq FROM core_runs WHERE run_id = ?")
|
|
203
|
+
.get(options.before) ?? {}, "seq");
|
|
204
|
+
const status = options.activeOnly
|
|
205
|
+
? "AND status IN ('running', 'waiting_approval')"
|
|
206
|
+
: "";
|
|
207
|
+
return this.#db
|
|
208
|
+
.prepare(`SELECT * FROM core_runs WHERE seq < ? ${status} ORDER BY seq DESC LIMIT ?`)
|
|
209
|
+
.all(beforeSeq ?? Number.MAX_SAFE_INTEGER, options.limit + 1)
|
|
210
|
+
.map((row) => this.#storedRun(row).summary);
|
|
211
|
+
};
|
|
212
|
+
insertMessage = (insert) => {
|
|
213
|
+
const { message } = insert;
|
|
214
|
+
this.#db
|
|
215
|
+
.prepare(`INSERT INTO core_messages (message_id, conversation_id, binding_id, chat_id, role, text, run_id, sender, source, at)
|
|
216
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`)
|
|
217
|
+
.run(message.messageId, insert.conversationId, insert.chat?.bindingId ?? null, insert.chat?.chatId ?? null, message.role, message.text, message.runId ?? null, message.senderDisplayName ?? null, message.source ?? null, message.at);
|
|
218
|
+
};
|
|
219
|
+
/** Messages of a Conversation in a Chat, oldest first, before the message `before`. Fetches `limit + 1`. */
|
|
220
|
+
chatMessages = (conversationId, before, limit) => {
|
|
221
|
+
const beforeSeq = before === undefined
|
|
222
|
+
? undefined
|
|
223
|
+
: optionalNumber(this.#db
|
|
224
|
+
.prepare("SELECT seq FROM core_messages WHERE message_id = ?")
|
|
225
|
+
.get(before) ?? {}, "seq");
|
|
226
|
+
return this.#db
|
|
227
|
+
.prepare("SELECT * FROM core_messages WHERE conversation_id = ? AND seq < ? ORDER BY seq DESC LIMIT ?")
|
|
228
|
+
.all(conversationId, beforeSeq ?? Number.MAX_SAFE_INTEGER, limit + 1)
|
|
229
|
+
.map((row) => messageRow(row))
|
|
230
|
+
.toReversed();
|
|
231
|
+
};
|
|
232
|
+
/** The last assistant message text of a Run, or an empty string. */
|
|
233
|
+
runReply = (runId) => {
|
|
234
|
+
const row = this.#db
|
|
235
|
+
.prepare("SELECT text FROM core_messages WHERE run_id = ? AND role = 'assistant' ORDER BY seq DESC LIMIT 1")
|
|
236
|
+
.get(runId);
|
|
237
|
+
return row === undefined ? "" : textOf(row, "text");
|
|
238
|
+
};
|
|
239
|
+
/** Adds a timeline item, or replaces the item with the same key, for example a Tool call that finished. */
|
|
240
|
+
putTimelineItem = (runId, key, item) => {
|
|
241
|
+
this.#db
|
|
242
|
+
.prepare(`INSERT INTO core_timeline (run_id, item_key, item) VALUES (?, ?, ?)
|
|
243
|
+
ON CONFLICT (item_key) DO UPDATE SET item = excluded.item`)
|
|
244
|
+
.run(runId, key, JSON.stringify(item));
|
|
245
|
+
};
|
|
246
|
+
timelineItem = (key) => {
|
|
247
|
+
const row = this.#db
|
|
248
|
+
.prepare("SELECT item FROM core_timeline WHERE item_key = ?")
|
|
249
|
+
.get(key);
|
|
250
|
+
return row === undefined ? undefined : timelineItemRow(row);
|
|
251
|
+
};
|
|
252
|
+
timeline = (runId) => this.#db
|
|
253
|
+
.prepare("SELECT item FROM core_timeline WHERE run_id = ? ORDER BY seq")
|
|
254
|
+
.all(runId)
|
|
255
|
+
.map((row) => timelineItemRow(row));
|
|
256
|
+
#storedRun = (row) => {
|
|
257
|
+
const status = textOf(row, "status");
|
|
258
|
+
const originValue = JSON.parse(textOf(row, "origin"));
|
|
259
|
+
const origin = isJsonValue(originValue)
|
|
260
|
+
? decode(RunOriginSchema, originValue)
|
|
261
|
+
: undefined;
|
|
262
|
+
if (!isRunStatus(status) || origin === undefined || !origin.ok) {
|
|
263
|
+
throw new Error("A stored Run is invalid");
|
|
264
|
+
}
|
|
265
|
+
const summary = {
|
|
266
|
+
agent: this.#agent,
|
|
267
|
+
conversationId: textOf(row, "conversation_id"),
|
|
268
|
+
endedAt: optionalText(row, "ended_at") ?? null,
|
|
269
|
+
origin: origin.value,
|
|
270
|
+
runId: textOf(row, "run_id"),
|
|
271
|
+
startedAt: textOf(row, "started_at"),
|
|
272
|
+
status,
|
|
273
|
+
};
|
|
274
|
+
const error = optionalText(row, "error");
|
|
275
|
+
const model = optionalText(row, "model");
|
|
276
|
+
const inputTokens = optionalNumber(row, "input_tokens");
|
|
277
|
+
const outputTokens = optionalNumber(row, "output_tokens");
|
|
278
|
+
const costUsd = optionalNumber(row, "cost_usd");
|
|
279
|
+
if (error !== undefined) {
|
|
280
|
+
summary.error = error;
|
|
281
|
+
}
|
|
282
|
+
if (model !== undefined) {
|
|
283
|
+
summary.model = model;
|
|
284
|
+
}
|
|
285
|
+
if (inputTokens !== undefined && outputTokens !== undefined) {
|
|
286
|
+
summary.usage =
|
|
287
|
+
costUsd === undefined
|
|
288
|
+
? { inputTokens, outputTokens }
|
|
289
|
+
: { costUsd, inputTokens, outputTokens };
|
|
290
|
+
}
|
|
291
|
+
const deliveryText = optionalText(row, "delivery");
|
|
292
|
+
const delivery = deliveryText === undefined ? null : JSON.parse(deliveryText);
|
|
293
|
+
return {
|
|
294
|
+
delivery: isRunDelivery(delivery) ? delivery : null,
|
|
295
|
+
interactive: optionalNumber(row, "interactive") === 1,
|
|
296
|
+
submissionId: optionalText(row, "submission_id"),
|
|
297
|
+
summary,
|
|
298
|
+
};
|
|
299
|
+
};
|
|
300
|
+
}
|