doer-agent 0.5.8 → 0.6.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.
@@ -1,25 +1,15 @@
1
1
  import { readFile } from "node:fs/promises";
2
- import { createConnection as createMysqlConnection } from "mysql2/promise";
3
2
  import { StringCodec } from "nats";
4
- import { Client as PostgresClient } from "pg";
5
- import { normalizeAgentDatabaseConnection, normalizeAgentSettingsConfig, normalizeAgentSettingsPatch, readAgentSettingsConfig, resolveAgentDatabaseConnectionUrl, resolveAgentSettingsFilePath, toAgentSettingsPublic, writeAgentModelInstructions, writeAgentSettingsConfig, } from "./agent-settings.js";
3
+ import { normalizeAgentSettingsConfig, normalizeAgentSettingsPatch, readAgentSettingsConfig, resolveAgentSettingsFilePath, toAgentSettingsPublic, writeAgentModelInstructions, writeAgentSettingsConfig, } from "./agent-settings.js";
6
4
  const settingsRpcCodec = StringCodec();
7
5
  function normalizeSettingsRpcRequest(args) {
8
6
  const requestId = typeof args.request.requestId === "string" ? args.request.requestId.trim() : "";
9
7
  const responseSubject = typeof args.request.responseSubject === "string" ? args.request.responseSubject.trim() : "";
10
8
  const requestAgentId = typeof args.request.agentId === "string" ? args.request.agentId.trim() : "";
11
- const action = args.request.action === "update"
12
- ? "update"
13
- : args.request.action === "test_database_connection"
14
- ? "test_database_connection"
15
- : "get";
9
+ const action = args.request.action === "update" ? "update" : "get";
16
10
  if (!requestId || !responseSubject || !requestAgentId || requestAgentId !== args.agentId) {
17
11
  throw new Error("invalid settings rpc request");
18
12
  }
19
- const connection = args.request.connection === undefined ? null : normalizeAgentDatabaseConnection(args.request.connection);
20
- if (action === "test_database_connection" && !connection) {
21
- throw new Error("invalid database connection test payload");
22
- }
23
13
  return {
24
14
  requestId,
25
15
  responseSubject,
@@ -28,89 +18,20 @@ function normalizeSettingsRpcRequest(args) {
28
18
  defaults: args.request.defaults && typeof args.request.defaults === "object" && !Array.isArray(args.request.defaults)
29
19
  ? normalizeAgentSettingsConfig(args.request.defaults)
30
20
  : null,
31
- connection,
32
21
  };
33
22
  }
34
23
  function publishSettingsRpcResponse(args) {
35
24
  args.nc.publish(args.responseSubject, settingsRpcCodec.encode(JSON.stringify(args.payload)));
36
25
  }
37
- async function testDatabaseConnection(connection) {
38
- const connectionUrl = resolveAgentDatabaseConnectionUrl(connection);
39
- if (!connectionUrl) {
40
- if (connection.connection.mode === "env") {
41
- throw new Error(`Database URL env is missing: ${connection.connection.urlEnv}`);
42
- }
43
- throw new Error(`Database URL is missing for connection: ${connection.id}`);
44
- }
45
- const startedAt = Date.now();
46
- if (connection.provider === "mysql") {
47
- const client = await createMysqlConnection({
48
- uri: connectionUrl,
49
- connectTimeout: 5_000,
50
- });
51
- try {
52
- const [rows] = await client.query("SELECT DATABASE() AS database_name, VERSION() AS version");
53
- const firstRow = Array.isArray(rows)
54
- ? rows[0]
55
- : undefined;
56
- const latencyMs = Date.now() - startedAt;
57
- const database = firstRow?.database_name ?? null;
58
- const serverVersion = firstRow?.version ?? null;
59
- return {
60
- ok: true,
61
- provider: connection.provider,
62
- message: `Connected to MySQL${database ? ` (${database})` : ""} in ${latencyMs}ms`,
63
- latencyMs,
64
- database,
65
- serverVersion,
66
- };
67
- }
68
- finally {
69
- await client.end().catch(() => undefined);
70
- }
71
- }
72
- const client = new PostgresClient({
73
- connectionString: connectionUrl,
74
- connectionTimeoutMillis: 5_000,
75
- });
76
- await client.connect();
77
- try {
78
- const result = await client.query("SELECT current_database() AS database_name, version() AS version");
79
- const firstRow = result.rows[0] ?? { database_name: null, version: null };
80
- const latencyMs = Date.now() - startedAt;
81
- return {
82
- ok: true,
83
- provider: connection.provider,
84
- message: `Connected to PostgreSQL${firstRow.database_name ? ` (${firstRow.database_name})` : ""} in ${latencyMs}ms`,
85
- latencyMs,
86
- database: firstRow.database_name,
87
- serverVersion: firstRow.version,
88
- };
89
- }
90
- finally {
91
- await client.end().catch(() => undefined);
92
- }
93
- }
94
26
  export async function handleSettingsRpcMessage(args) {
95
27
  let requestId = "unknown";
96
28
  let responseSubject = "";
29
+ let didUpdateSettings = false;
97
30
  try {
98
31
  const payload = JSON.parse(settingsRpcCodec.decode(args.msg.data));
99
32
  const request = normalizeSettingsRpcRequest({ request: payload, agentId: args.agentId });
100
33
  requestId = request.requestId;
101
34
  responseSubject = request.responseSubject;
102
- if (request.action === "test_database_connection") {
103
- publishSettingsRpcResponse({
104
- nc: args.nc,
105
- responseSubject,
106
- payload: {
107
- requestId,
108
- ok: true,
109
- testResult: await testDatabaseConnection(request.connection),
110
- },
111
- });
112
- return;
113
- }
114
35
  const existing = await readAgentSettingsConfig({ workspaceRoot: args.workspaceRoot, defaults: request.defaults });
115
36
  const next = request.action === "update" ? normalizeAgentSettingsConfig(request.patch, existing) : existing;
116
37
  if (request.action === "update") {
@@ -123,6 +44,7 @@ export async function handleSettingsRpcMessage(args) {
123
44
  if (customInstructions !== undefined) {
124
45
  await writeAgentModelInstructions({ workspaceRoot: args.workspaceRoot, value: customInstructions });
125
46
  }
47
+ didUpdateSettings = true;
126
48
  }
127
49
  else if (request.defaults) {
128
50
  const filePath = resolveAgentSettingsFilePath(args.workspaceRoot);
@@ -140,6 +62,12 @@ export async function handleSettingsRpcMessage(args) {
140
62
  settings: await toAgentSettingsPublic({ workspaceRoot: args.workspaceRoot, config: next }),
141
63
  },
142
64
  });
65
+ if (didUpdateSettings) {
66
+ await Promise.resolve(args.onSettingsUpdated?.()).catch((error) => {
67
+ const message = error instanceof Error ? error.message : String(error);
68
+ args.onError(`settings update hook failed requestId=${requestId} error=${message}`);
69
+ });
70
+ }
143
71
  }
144
72
  catch (error) {
145
73
  const message = error instanceof Error ? error.message : String(error);
@@ -39,10 +39,6 @@ export function createDefaultAgentSettingsConfig() {
39
39
  env: {
40
40
  variables: [],
41
41
  },
42
- databases: {
43
- defaultConnectionId: null,
44
- connections: [],
45
- },
46
42
  };
47
43
  }
48
44
  function normalizeNullableString(value) {
@@ -58,16 +54,6 @@ function normalizeNullableString(value) {
58
54
  function normalizeCodexPersonality(value, fallback) {
59
55
  return value === "friendly" || value === "pragmatic" ? value : fallback;
60
56
  }
61
- function normalizeDatabaseConnectionId(value) {
62
- if (typeof value !== "string") {
63
- return null;
64
- }
65
- const trimmed = value.trim().toLowerCase();
66
- if (!trimmed || !/^[a-z0-9][a-z0-9._-]{0,63}$/.test(trimmed)) {
67
- return null;
68
- }
69
- return trimmed;
70
- }
71
57
  function normalizeEnvVarName(value) {
72
58
  if (typeof value !== "string") {
73
59
  return null;
@@ -78,71 +64,6 @@ function normalizeEnvVarName(value) {
78
64
  }
79
65
  return trimmed;
80
66
  }
81
- function normalizeAgentDatabaseProvider(value) {
82
- return value === "mysql" ? "mysql" : "postgres";
83
- }
84
- function normalizeAgentDatabaseConnectionSecret(value) {
85
- if (!value || typeof value !== "object" || Array.isArray(value)) {
86
- return null;
87
- }
88
- const raw = value;
89
- const mode = raw.mode === "env" ? "env" : raw.mode === "url" ? "url" : null;
90
- if (mode === "env") {
91
- const urlEnv = normalizeEnvVarName(raw.urlEnv);
92
- return urlEnv ? { mode, urlEnv } : null;
93
- }
94
- if (mode === "url") {
95
- const url = normalizeNullableString(raw.url);
96
- return url ? { mode, url } : null;
97
- }
98
- return null;
99
- }
100
- export function normalizeAgentDatabaseConnection(value) {
101
- if (!value || typeof value !== "object" || Array.isArray(value)) {
102
- return null;
103
- }
104
- const raw = value;
105
- const id = normalizeDatabaseConnectionId(raw.id);
106
- const connection = normalizeAgentDatabaseConnectionSecret(raw.connection);
107
- if (!id || !connection) {
108
- return null;
109
- }
110
- return {
111
- id,
112
- description: raw.description === null ? null : normalizeNullableString(raw.description),
113
- provider: normalizeAgentDatabaseProvider(raw.provider),
114
- enabled: typeof raw.enabled === "boolean" ? raw.enabled : true,
115
- readOnly: typeof raw.readOnly === "boolean" ? raw.readOnly : true,
116
- connection,
117
- };
118
- }
119
- function normalizeAgentDatabaseSettings(value, fallback) {
120
- if (!value || typeof value !== "object" || Array.isArray(value)) {
121
- return fallback;
122
- }
123
- const raw = value;
124
- const connectionsRaw = Array.isArray(raw.connections) ? raw.connections : [];
125
- const connections = [];
126
- const seenIds = new Set();
127
- for (const item of connectionsRaw) {
128
- const normalized = normalizeAgentDatabaseConnection(item);
129
- if (!normalized || seenIds.has(normalized.id)) {
130
- continue;
131
- }
132
- seenIds.add(normalized.id);
133
- connections.push(normalized);
134
- }
135
- const requestedDefault = raw.defaultConnectionId === null ? null : normalizeDatabaseConnectionId(raw.defaultConnectionId);
136
- const defaultConnectionId = requestedDefault && connections.some((connection) => connection.id === requestedDefault)
137
- ? requestedDefault
138
- : connections.some((connection) => connection.id === fallback.defaultConnectionId)
139
- ? fallback.defaultConnectionId
140
- : connections[0]?.id ?? null;
141
- return {
142
- defaultConnectionId,
143
- connections,
144
- };
145
- }
146
67
  function normalizeAgentEnvironmentVariable(value) {
147
68
  if (!value || typeof value !== "object" || Array.isArray(value)) {
148
69
  return null;
@@ -175,20 +96,6 @@ function normalizeAgentEnvironmentSettings(value, fallback) {
175
96
  }
176
97
  return { variables };
177
98
  }
178
- export function getAgentDatabaseConnectionById(config, connectionId) {
179
- const normalizedId = normalizeDatabaseConnectionId(connectionId);
180
- if (!normalizedId) {
181
- return null;
182
- }
183
- return config.databases.connections.find((connection) => connection.id === normalizedId) ?? null;
184
- }
185
- export function resolveAgentDatabaseConnectionUrl(connection) {
186
- if (connection.connection.mode === "url") {
187
- return connection.connection.url.trim() || null;
188
- }
189
- const envValue = process.env[connection.connection.urlEnv]?.trim();
190
- return envValue || null;
191
- }
192
99
  export function normalizeAgentSettingsConfig(value, fallback) {
193
100
  const base = fallback ?? createDefaultAgentSettingsConfig();
194
101
  const raw = value && typeof value === "object" && !Array.isArray(value) ? value : {};
@@ -197,14 +104,13 @@ export function normalizeAgentSettingsConfig(value, fallback) {
197
104
  const realtime = raw.realtime && typeof raw.realtime === "object" ? raw.realtime : {};
198
105
  const git = raw.git && typeof raw.git === "object" ? raw.git : {};
199
106
  const env = raw.env && typeof raw.env === "object" ? raw.env : null;
200
- const databases = raw.databases && typeof raw.databases === "object" ? raw.databases : null;
201
107
  return {
202
108
  general: {
203
109
  personality: normalizeCodexPersonality(general.personality, base.general.personality),
204
110
  },
205
111
  codex: {
206
112
  model: typeof codex.model === "string" && codex.model.trim() ? codex.model.trim() : base.codex.model,
207
- authMode: codex.authMode === "oauth" ? "oauth" : codex.authMode === "api_key" ? "api_key" : base.codex.authMode,
113
+ authMode: codex.authMode === "chatgpt" ? "chatgpt" : codex.authMode === "api_key" ? "api_key" : base.codex.authMode,
208
114
  computerUseEnabled: typeof codex.computerUseEnabled === "boolean" ? codex.computerUseEnabled : base.codex.computerUseEnabled,
209
115
  browserUseEnabled: typeof codex.browserUseEnabled === "boolean" ? codex.browserUseEnabled : base.codex.browserUseEnabled,
210
116
  },
@@ -225,7 +131,6 @@ export function normalizeAgentSettingsConfig(value, fallback) {
225
131
  oauthScope: git.oauthScope === null ? null : normalizeNullableString(git.oauthScope) ?? base.git.oauthScope,
226
132
  },
227
133
  env: normalizeAgentEnvironmentSettings(env, base.env),
228
- databases: normalizeAgentDatabaseSettings(databases, base.databases),
229
134
  };
230
135
  }
231
136
  export async function readAgentSettingsConfig(args) {
@@ -317,25 +222,6 @@ export async function toAgentSettingsPublic(args) {
317
222
  value: variable.value,
318
223
  })),
319
224
  },
320
- databases: {
321
- defaultConnectionId: args.config.databases.defaultConnectionId,
322
- connections: args.config.databases.connections.map((connection) => ({
323
- id: connection.id,
324
- description: connection.description,
325
- provider: connection.provider,
326
- enabled: connection.enabled,
327
- readOnly: connection.readOnly,
328
- connection: connection.connection.mode === "url"
329
- ? {
330
- mode: "url",
331
- url: connection.connection.url,
332
- }
333
- : {
334
- mode: "env",
335
- urlEnv: connection.connection.urlEnv,
336
- },
337
- })),
338
- },
339
225
  };
340
226
  }
341
227
  export function normalizeAgentSettingsPatch(value) {