@saltcorn/copilot 0.5.0 → 0.5.1

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 (2) hide show
  1. package/package.json +1 -1
  2. package/user-copilot.js +53 -55
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@saltcorn/copilot",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "AI assistant for building Saltcorn applications",
5
5
  "main": "index.js",
6
6
  "dependencies": {
package/user-copilot.js CHANGED
@@ -466,39 +466,42 @@ const run = async (table_id, viewname, config, state, { res, req }) => {
466
466
  const getCompletionArguments = async (config) => {
467
467
  let tools = [];
468
468
  const sysPrompts = [];
469
- let properties = {};
470
469
 
471
- const tableNames = (config?.tables || []).map((t) => t.table_name);
472
- properties.table_name = {
473
- type: "string",
474
- enum: tableNames,
475
- description: `Which table is this query from. Every query has to select rows from one table, even if it is based on joins from different tables`,
476
- };
477
- properties.sql_id_query = {
478
- type: "string",
479
- description: `An SQL query for this table's primary keys. This must select only the primary keys (even if the user wants a count), for example SELECT ${
480
- tableNames[0][0]
481
- }."${Table.findOne(tableNames[0]).pk_name}" from "${tableNames[0]}" ${
482
- tableNames[0][0]
483
- } JOIN ... where... Use this to join other tables in the database.`,
484
- };
485
- properties.is_count = {
486
- type: "boolean",
487
- description: `Is the only desired output a count? Make this true if the user wants a count of rows`,
488
- };
470
+ if (config.tables.length) {
471
+ let properties = {};
472
+
473
+ const tableNames = (config?.tables || []).map((t) => t.table_name);
474
+ properties.table_name = {
475
+ type: "string",
476
+ enum: tableNames,
477
+ description: `Which table is this query from. Every query has to select rows from one table, even if it is based on joins from different tables`,
478
+ };
479
+ properties.sql_id_query = {
480
+ type: "string",
481
+ description: `An SQL query for this table's primary keys. This must select only the primary keys (even if the user wants a count), for example SELECT ${
482
+ tableNames[0][0]
483
+ }."${Table.findOne(tableNames[0]).pk_name}" from "${tableNames[0]}" ${
484
+ tableNames[0][0]
485
+ } JOIN ... where... Use this to join other tables in the database.`,
486
+ };
487
+ properties.is_count = {
488
+ type: "boolean",
489
+ description: `Is the only desired output a count? Make this true if the user wants a count of rows`,
490
+ };
489
491
 
490
- tools.push({
491
- type: "function",
492
- function: {
493
- name: "TableQuery",
494
- description: `Query a table and show the results to the user in a grid format`,
495
- parameters: {
496
- type: "object",
497
- required: ["table_name", "sql_id_query", "is_count"],
498
- properties,
492
+ tools.push({
493
+ type: "function",
494
+ function: {
495
+ name: "TableQuery",
496
+ description: `Query a table and show the results to the user in a grid format`,
497
+ parameters: {
498
+ type: "object",
499
+ required: ["table_name", "sql_id_query", "is_count"],
500
+ properties,
501
+ },
499
502
  },
500
- },
501
- });
503
+ });
504
+ }
502
505
 
503
506
  for (const action of config?.actions || []) {
504
507
  let properties = {};
@@ -536,18 +539,19 @@ const getCompletionArguments = async (config) => {
536
539
  const systemPrompt =
537
540
  "You are helping users retrieve information and perform actions on a relational database" +
538
541
  config.sys_prompt +
539
- `
540
- If you are generating SQL, Your database the following tables in PostgreSQL:
542
+ (config.tables.length
543
+ ? `
544
+ If you are generating SQL, Your database has the following tables in PostgreSQL:
541
545
 
542
546
  ` +
543
- tables
544
- .map(
545
- (t) => `CREATE TABLE "${t.name}" (${
546
- t.description
547
- ? `
547
+ tables
548
+ .map(
549
+ (t) => `CREATE TABLE "${t.name}" (${
550
+ t.description
551
+ ? `
548
552
  /* ${t.description} */`
549
- : ""
550
- }
553
+ : ""
554
+ }
551
555
  ${t.fields
552
556
  .map(
553
557
  (f) =>
@@ -559,15 +563,16 @@ ${t.fields
559
563
  )
560
564
  .join(",\n")}
561
565
  )`
562
- )
563
- .join(";\n\n") +
564
- `
566
+ )
567
+ .join(";\n\n") +
568
+ `
565
569
 
566
570
  Use the TableQuery tool if the user asks to see, find or count or otherwise access rows from a table that matches what the user is looking for, or if
567
571
  the user is asking for a summary or inference from such rows. The TableQuery query is parametrised by a SQL SELECT query which
568
572
  selects primary key values from the specified table. You can join other tables or use complex logic in the WHERE clause, but you must
569
573
  always return porimary key values from the specified table.
570
- `;
574
+ `
575
+ : "");
571
576
  //console.log("sysprompt", systemPrompt);
572
577
 
573
578
  if (tools.length === 0) tools = undefined;
@@ -600,7 +605,7 @@ const interact = async (table_id, viewname, config, body, { req, res }) => {
600
605
  interactions: [{ role: "user", content: userinput }],
601
606
  });
602
607
  }
603
- return await process_interaction(run, userinput, config, req);
608
+ return await process_interaction(run, config, req);
604
609
  };
605
610
 
606
611
  const renderQueryInteraction = async (table, result, config, req) => {
@@ -653,20 +658,13 @@ const renderQueryInteraction = async (table, result, config, req) => {
653
658
  );
654
659
  };
655
660
 
656
- const process_interaction = async (
657
- run,
658
- input,
659
- config,
660
- req,
661
- prevResponses = []
662
- ) => {
661
+ const process_interaction = async (run, config, req, prevResponses = []) => {
663
662
  const complArgs = await getCompletionArguments(config);
664
663
  complArgs.chat = run.context.interactions;
665
664
  //complArgs.debugResult = true;
666
- //console.log(complArgs);
667
- console.log("complArgs", JSON.stringify(complArgs, null, 2));
665
+ //console.log("complArgs", JSON.stringify(complArgs, null, 2));
668
666
 
669
- const answer = await getState().functions.llm_generate.run(input, complArgs);
667
+ const answer = await getState().functions.llm_generate.run("", complArgs);
670
668
  console.log("answer", answer);
671
669
  await addToContext(run, {
672
670
  interactions:
@@ -820,7 +818,7 @@ const process_interaction = async (
820
818
  }
821
819
  }
822
820
  if (hasResult)
823
- return await process_interaction(run, "", config, req, [
821
+ return await process_interaction(run, config, req, [
824
822
  ...prevResponses,
825
823
  ...responses,
826
824
  ]);