Merge branch 'pzaback-feat/enhanced-share' into preview

This commit is contained in:
Nate Sesti 2024-04-24 00:11:47 -07:00
commit 5d9cbf963c
10 changed files with 125 additions and 24 deletions

View file

@ -1,4 +1,5 @@
{
"tabWidth": 2,
"useTabs": false
"useTabs": false,
"trailingComma": "all"
}

View file

@ -1,24 +1,98 @@
import path from "path";
import * as fs from "fs";
import { homedir } from "os";
import { SlashCommand } from "../..";
import { languageForFilepath } from "../../autocomplete/constructPrompt";
import { stripImages } from "../../llm/countTokens";
// If useful elsewhere, helper funcs should move to core/util/index.ts or similar
function getOffsetDatetime(date: Date): Date {
const offset = date.getTimezoneOffset();
const offsetHours = Math.floor(offset / 60);
const offsetMinutes = offset % 60;
date.setHours(date.getHours() - offsetHours);
date.setMinutes(date.getMinutes() - offsetMinutes);
return date;
}
function asBasicISOString(date: Date): string {
const isoString = date.toISOString();
return isoString.replace(/[-:]|(\.\d+Z)/g, "");
}
function reformatCodeBlocks(msgText: string): string {
const codeBlockFenceRegex = /```((.*?\.(\w+))\s*.*)\n/g;
msgText = msgText.replace(codeBlockFenceRegex,
(match, metadata, filename, extension) => {
const lang = languageForFilepath(filename);
return `\`\`\`${extension}\n${lang.comment} ${metadata}\n`;
},
);
// Appease the markdown linter
return msgText.replace(/```\n```/g, '```\n\n```');
}
const ShareSlashCommand: SlashCommand = {
name: "share",
description: "Download and share this session",
run: async function* ({ ide, history }) {
let content = `This is a session transcript from [Continue](https://continue.dev) on ${new Date().toLocaleString()}.`;
description: "Export the current chat session to markdown",
run: async function* ({ ide, history, params }) {
const now = new Date();
for (const msg of history) {
content += `\n\n## ${
msg.role === "user" ? "User" : "Continue"
}\n\n${stripImages(msg.content)}`;
let content = `### [Continue](https://continue.dev) session transcript\n Exported: ${now.toLocaleString()}`;
// As currently implemented, the /share command is by definition the last
// message in the chat history, this will omit it
for (const msg of history.slice(0, history.length - 1)) {
let msgText = msg.content;
msgText = stripImages(msg.content);
if (msg.role === "user" && msgText.search("```") > -1) {
msgText = reformatCodeBlocks(msgText);
}
// format messages as blockquotes
msgText = msgText.replace(/^/gm, "> ");
content += `\n\n#### ${
msg.role === "user" ? "_User_" : "_Assistant_"
}\n\n${msgText}`;
}
const continueDir = await ide.getContinueDir();
const path = `${continueDir}/session.md`;
await ide.writeFile(path, content);
await ide.openFile(path);
let outputDir: string = params?.outputDir;
if (!outputDir) {
outputDir = await ide.getContinueDir();
}
yield `The session transcript has been saved to a markdown file at \`${path}\`.`;
if (outputDir.startsWith("~")) {
outputDir = outputDir.replace(/^~/, homedir);
} else if (
outputDir.startsWith("./") ||
outputDir.startsWith(`.\\`) ||
outputDir === "."
) {
const workspaceDirs = await ide.getWorkspaceDirs();
// Although the most common situation is to have one directory open in a
// workspace it's also possible to have just a file open without an
// associated directory or to use multi-root workspaces in which multiple
// folders are included. We default to using the first item in the list, if
// it exists.
const workspaceDirectory = workspaceDirs?.[0] || "";
outputDir = outputDir.replace(/^./, workspaceDirectory);
}
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const dtString = asBasicISOString(getOffsetDatetime(now));
const outPath = path.join(outputDir, `${dtString}_session.md`); //TODO: more flexible naming?
await ide.writeFile(outPath, content);
await ide.openFile(outPath);
yield `The session transcript has been saved to a markdown file at \`${outPath}\`.`;
},
};

View file

@ -39,7 +39,7 @@ export const defaultConfig: SerializedContinueConfig = {
},
{
name: "share",
description: "Export this session as markdown",
description: "Export the current chat session to markdown",
},
{
name: "cmd",
@ -110,7 +110,7 @@ export const defaultConfigJetBrains: SerializedContinueConfig = {
},
{
name: "share",
description: "Export this session as markdown",
description: "Export the current chat session to markdown",
},
],
customCommands: [

View file

@ -339,10 +339,10 @@ function finalToBrowserConfig(
})),
systemMessage: final.systemMessage,
completionOptions: final.completionOptions,
slashCommands: final.slashCommands?.map((m) => ({
name: m.name,
description: m.description,
options: m.params,
slashCommands: final.slashCommands?.map((s) => ({
name: s.name,
description: s.description,
params: s.params, //PZTODO: is this why params aren't referenced properly by slash commands?
})),
contextProviders: final.contextProviders?.map((c) => c.description),
disableIndexing: final.disableIndexing,

View file

@ -131,7 +131,7 @@ After the "Full example" these examples will only show the relevant portion of t
},
{
"name": "share",
"description": "Download and share this session",
"description": "Export the current chat session to markdown",
"step": "ShareSessionStep"
},
{

View file

@ -43,10 +43,13 @@ Type "/share" to generate a shareable markdown transcript of your current chat h
```json
{
"name": "share",
"description": "Download and share this session"
"description": "Export the current chat session to markdown",
"params": { "ouputDir": "~/.continue/session-transcripts" }
}
```
Use the `ouputDir` parameter to specify where you want to the markdown file to be saved.
### `/cmd`
Generate a shell command from natural language and (only in VS Code) automatically paste it into the terminal.

View file

@ -130,7 +130,7 @@ After the "Full example" these examples will only show the relevant portion of t
},
{
"name": "share",
"description": "Download and share this session",
"description": "Export the current chat session to markdown",
"step": "ShareSessionStep"
},
{

View file

@ -41,7 +41,7 @@ const val DEFAULT_CONFIG = """
},
{
"name": "share",
"description": "Download and share this session",
"description": "Export the current chat session to markdown",
"step": "ShareSessionStep"
},
{

View file

@ -1127,6 +1127,29 @@
}
}
}
},
{
"if": {
"properties": {
"name": {
"enum": [
"share"
]
}
}
},
"then": {
"properties": {
"params": {
"properties": {
"outputDir": {
"type": "string",
"markdownDescription": "If outputDir is set to `.` or begins with `./` or `.\\`, file will be saved to the current workspace or a subdirectory thereof, respectively. `~` can similarly be used to specify the user's home directory."
}
}
}
}
}
}
],
"required": ["name", "description"]

View file

@ -108,7 +108,7 @@ const initialState: State = {
},
{
name: "share",
description: "Download and share this session",
description: "Export the current chat session to markdown",
},
{
name: "cmd",