@koneeric/onetool 0.1.0 → 0.3.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.
@@ -19,9 +19,12 @@ import {
19
19
  jobStarted,
20
20
  jobState,
21
21
  nextState,
22
+ nextStreak,
22
23
  noJobMessage,
24
+ oneProgramNote,
23
25
  runRequest,
24
26
  searchTools,
27
+ signInMessage,
25
28
  shellOffMessage,
26
29
  toolValue,
27
30
  withTrail,
@@ -29,11 +32,13 @@ import {
29
32
  import { LibraryError, checkSave, isProgramName } from "../../library/src/index.js";
30
33
  import { runProgram } from "../../sandbox/src/index.js";
31
34
  import { ShellError, editFile, readFile, realPlace, runCommand, writeFile } from "../../shell/src/index.js";
32
- import { isServerError } from "./connect.js";
35
+ import { isServerError, isSignInFailure } from "./connect.js";
33
36
  import { toolsWith } from "./descriptions.js";
34
37
  import { makeLibrary } from "./library.js";
35
38
  import { makeRecords, refused } from "./record.js";
36
39
 
40
+ const say = (line) => process.stderr.write(`${line}\n`);
41
+
37
42
  const answer = (settings, text, isError = false) => ({
38
43
  content: [{ type: "text", text: cutAnswer(text, settings.answerLimit) }],
39
44
  ...(isError ? { isError: true } : {}),
@@ -42,6 +47,9 @@ const answer = (settings, text, isError = false) => ({
42
47
  // the decision of a call by the way a question ended
43
48
  const ASKED = { decline: "no", cancel: "no answer", unsupported: "cannot ask", failed: "failed" };
44
49
 
50
+ // the decisions of a record that say a question reached the host
51
+ const QUESTIONED = new Set(["asked: yes", "asked: no", "asked: no answer", "asked: failed"]);
52
+
45
53
  // the ended jobs kept, the lines of `progress` kept and how much of one
46
54
  const ENDED_KEPT = 20;
47
55
  const PROGRESS_KEPT = 10;
@@ -53,8 +61,9 @@ const isPlainObject = (value) => typeof value === "object" && value !== null &&
53
61
  * `list()` (async) is what tools/list answers, `call(name, args)` answers tools/call. `ready` is set by the caller: a promise
54
62
  * that is settled once every connection is connected or given up. `server` is the `Server` that speaks to the host:
55
63
  * it is asked through it, once its capabilities are known. `host` (host.js) holds the folders and rules of the host.
64
+ * `settingsPath` is the full path of the settings file, for the message of a server that needs a sign-in.
56
65
  */
57
- export function makeTools(settings, connections, server, host) {
66
+ export function makeTools(settings, connections, server, host, settingsPath) {
58
67
  const records = makeRecords(settings);
59
68
  const library = makeLibrary(settings, host, records);
60
69
  const self = {
@@ -63,13 +72,17 @@ export function makeTools(settings, connections, server, host) {
63
72
  };
64
73
  // the kept state: one plain object for the life of the process
65
74
  let kept = {};
75
+ // the numbers of the session, and the streak of programs in the foreground that each made one call
76
+ const numbers = { programs: 0, calls: 0 };
77
+ let streak = 0;
66
78
 
67
79
  const catalog = () => [
68
- ...connections.map((connection) =>
69
- connection.error === null
80
+ ...connections.map((connection) => {
81
+ if (connection.needed) return { server: connection.name, tools: [], signIn: true };
82
+ return connection.error === null
70
83
  ? { server: connection.name, tools: connection.tools }
71
- : { server: connection.name, tools: [], error: connection.error },
72
- ),
84
+ : { server: connection.name, tools: [], error: connection.error };
85
+ }),
73
86
  ...settings.bad.map(({ name, reason }) => ({ server: name, tools: [], error: `bad entry: ${reason}` })),
74
87
  ...(settings.shell === null
75
88
  ? []
@@ -90,7 +103,10 @@ export function makeTools(settings, connections, server, host) {
90
103
  await self.ready;
91
104
  await host.idle();
92
105
  const { programs } = await library.read();
93
- return answer(settings, searchTools(visible(), words, { limit: settings.searchLimit, programs }));
106
+ return answer(
107
+ settings,
108
+ searchTools(visible(), words, { limit: settings.searchLimit, programs, session: { programs: numbers.programs, calls: numbers.calls } }),
109
+ );
94
110
  }
95
111
 
96
112
  // one question open at a time, for all the programs: each call takes its turn after the one before it
@@ -257,7 +273,62 @@ export function makeTools(settings, connections, server, host) {
257
273
  }
258
274
  }
259
275
 
260
- async function callTool(serverName, tool, args, signal, record, ms) {
276
+ const signInServers = connections.filter((connection) => connection.signIn);
277
+
278
+ async function tellHost() {
279
+ try {
280
+ await server.sendToolListChanged();
281
+ } catch {
282
+ // a failure of the sending is ignored
283
+ }
284
+ }
285
+
286
+ /** A call met a sign-in failure: the server is sign in needed, the line is written, and the host is told once for `told`. */
287
+ async function signInFailed(connection, told) {
288
+ if (connection.needed || connection.tools === null) return;
289
+ const closing = connection.goNeeded(connection.stamp());
290
+ say(`onetool: server ${connection.name}: sign in needed`);
291
+ if (!told.sent) {
292
+ told.sent = true;
293
+ await tellHost();
294
+ }
295
+ await closing;
296
+ }
297
+
298
+ /** What is done at the beginning of a call for one sign-in server; gives whether its state changed. */
299
+ async function pickUpOne(connection) {
300
+ const stamp = connection.stamp();
301
+ if (connection.needed) {
302
+ if (stamp === null || stamp === connection.stampTried) return false;
303
+ await connection.connect();
304
+ if (connection.needed) return false;
305
+ if (connection.error !== null) say(`onetool: server ${connection.name}: not connected (${connection.error})`);
306
+ return true;
307
+ }
308
+ if (connection.withSignIn && connection.tools !== null && stamp === null) {
309
+ const closing = connection.goNeeded(null);
310
+ say(`onetool: server ${connection.name}: sign in needed`);
311
+ await closing;
312
+ return true;
313
+ }
314
+ return false;
315
+ }
316
+
317
+ // the looks at the sign-in servers are made one pass after the other: a call that arrives while one is made waits for it
318
+ let looking = Promise.resolve();
319
+
320
+ async function lookAtSignIns() {
321
+ const changes = await Promise.all(signInServers.map((connection) => pickUpOne(connection).catch(() => false)));
322
+ if (changes.includes(true)) await tellHost();
323
+ }
324
+
325
+ const pickUp = () => {
326
+ if (signInServers.length === 0) return Promise.resolve();
327
+ looking = looking.then(lookAtSignIns, lookAtSignIns);
328
+ return looking;
329
+ };
330
+
331
+ async function callTool(serverName, tool, args, signal, record, ms, told) {
261
332
  if (serverName === "library") return callLibrary(tool, args, signal, record, ms);
262
333
  if (settings.shell !== null && serverName === "shell") return callShell(tool, args, signal, record, ms);
263
334
  const id = `${serverName}.${tool}`;
@@ -274,6 +345,10 @@ export function makeTools(settings, connections, server, host) {
274
345
  if (call !== null) byHost = hostSays(id, call, record);
275
346
  }
276
347
  const connection = connections.find((candidate) => candidate.name === serverName);
348
+ if (connection?.needed) {
349
+ if (byHost !== null) record.decision = "asked: not asked";
350
+ throw new Error(`${id} failed: ${signInMessage(connection.name, settingsPath)}`);
351
+ }
277
352
  if (!connection || !connection.connected) {
278
353
  if (byHost !== null) record.decision = "asked: not asked";
279
354
  throw new Error(`${id} failed: not connected`);
@@ -287,6 +362,10 @@ export function makeTools(settings, connections, server, host) {
287
362
  try {
288
363
  result = await connection.client.callTool({ name: tool, arguments: args }, undefined, { timeout: ms, signal });
289
364
  } catch (err) {
365
+ if (connection.signIn && isSignInFailure(err)) {
366
+ await signInFailed(connection, told);
367
+ throw new Error(`${id} failed: ${signInMessage(connection.name, settingsPath)}`);
368
+ }
290
369
  // an address has no process to end: what the server did not say itself is told in one fixed way
291
370
  if (connection.remote && !isServerError(err)) throw new Error(`${id} failed: no answer from the server`);
292
371
  throw new Error(`${id} failed: ${err?.message ?? String(err)}`);
@@ -313,18 +392,20 @@ export function makeTools(settings, connections, server, host) {
313
392
 
314
393
  /**
315
394
  * One program, from the host's files to the kept state: what a program in the foreground and a job both do. `seconds` is
316
- * its limit; `program` its records; `onProgress` and `signal` are the ones handed to `runProgram`. Gives the result, in
317
- * which a state that was too large has made an error of an `ok` result.
395
+ * its limit; `program` its records; `onProgress` and `signal` are the ones handed to `runProgram`; `foreground` says that the
396
+ * program counts for the streak. Gives the result, in which a state that was too large has made an error of an `ok` result.
318
397
  */
319
- async function execute(code, { seconds, program, onProgress, signal }) {
398
+ async function execute(code, { seconds, program, onProgress, signal, foreground = false }) {
320
399
  await host.refresh();
321
400
  // the program's library: read once, as it is when the program starts
322
401
  const saved = await library.read();
323
402
  const uses = [];
324
403
  const names = {};
325
404
  for (const connection of connections) {
326
- if (connection.error === null) names[connection.name] = connection.tools.map((tool) => tool.name);
405
+ if (connection.needed) names[connection.name] = true;
406
+ else if (connection.error === null) names[connection.name] = connection.tools.map((tool) => tool.name);
327
407
  }
408
+ const told = { sent: false };
328
409
  const withShell = host.hasFolders();
329
410
  if (withShell) names.shell = SHELL_TOOLS.map((tool) => tool.name);
330
411
  // stops the commands and the open question of this program, once it has ended
@@ -336,7 +417,7 @@ export function makeTools(settings, connections, server, host) {
336
417
  callTool: async (serverName, tool, callArgs) => {
337
418
  const record = program.begin(serverName, tool, callArgs);
338
419
  try {
339
- const value = await callTool(serverName, tool, callArgs, controller.signal, record, seconds * 1000 + 1000);
420
+ const value = await callTool(serverName, tool, callArgs, controller.signal, record, seconds * 1000 + 1000, told);
340
421
  program.resolved(record, value);
341
422
  return value;
342
423
  } catch (err) {
@@ -361,6 +442,15 @@ export function makeTools(settings, connections, server, host) {
361
442
  const final =
362
443
  next.problem !== null && result.ok ? { ok: false, error: { kind: "error", message: next.problem }, logs: result.logs } : result;
363
444
  program.notes.push(...(await library.finish({ library: saved, uses, code, ok: final.ok })));
445
+ // no wait from here to the end: the programs are counted one after the other, in the order their results were there
446
+ numbers.programs += 1;
447
+ numbers.calls += program.calls.length;
448
+ if (foreground) {
449
+ const asked = program.calls.some((record) => QUESTIONED.has(record.decision));
450
+ const next = nextStreak(streak, { ok: final.ok, calls: program.calls.length, asked, lib: uses.length });
451
+ streak = next.streak;
452
+ if (next.say) program.notes.push(oneProgramNote(3));
453
+ }
364
454
  return final;
365
455
  } finally {
366
456
  await program.close();
@@ -381,7 +471,7 @@ export function makeTools(settings, connections, server, host) {
381
471
  await self.ready;
382
472
  await host.idle();
383
473
  const program = records.program();
384
- const result = await execute(code, { seconds: settings.timeoutSeconds, program, onProgress: () => {} });
474
+ const result = await execute(code, { seconds: settings.timeoutSeconds, program, onProgress: () => {}, foreground: true });
385
475
  return reply(formatRun(result, { timeoutSeconds: settings.timeoutSeconds }), !result.ok, program);
386
476
  }
387
477
 
@@ -453,9 +543,11 @@ export function makeTools(settings, connections, server, host) {
453
543
 
454
544
  self.call = async (name, args) => {
455
545
  host.start();
456
- if (name === "search") return search(args ?? {});
457
- if (name === "run") return run(args ?? {});
458
- throw new McpError(ErrorCode.InvalidParams, `unknown tool: ${name}`);
546
+ if (name !== "search" && name !== "run") throw new McpError(ErrorCode.InvalidParams, `unknown tool: ${name}`);
547
+ // a sign-in made or forgotten since the last call is picked up before anything else of the call
548
+ await self.ready;
549
+ await pickUp();
550
+ return name === "search" ? search(args ?? {}) : run(args ?? {});
459
551
  };
460
552
  return self;
461
553
  }