Skip to content

Commit

Permalink
fix: mirror some types from vscode.d.ts (#36)
Browse files Browse the repository at this point in the history
* fix: use a handwritten `index.d.ts`

* Simplify further
  • Loading branch information
joyceerhl authored May 24, 2024
1 parent cf2f8cc commit 21e1bc8
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 19,262 deletions.
15 changes: 9 additions & 6 deletions src/base/vscodeTypes.ts → build/postcompile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
* Copyright (c) Microsoft Corporation and GitHub. All rights reserved.
*--------------------------------------------------------------------------------------------*/

import type { Uri, Range } from 'vscode';
export type { ChatResponsePart } from 'vscode';
import * as path from 'path';
import { copyStaticAssets } from './postinstall';

export interface ChatDocumentContext {
uri: Uri;
version: number;
ranges: Range[];
async function main() {
// Ship the vscodeTypes.d.ts file in the dist bundle
await copyStaticAssets([
'src/base/vscodeTypes.d.ts',
], 'dist/base/');
}

main();
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@vscode/prompt-tsx",
"version": "0.1.9-alpha",
"version": "0.1.10-alpha",
"description": "Declare LLM prompts with TSX",
"main": "./dist/base/index.js",
"types": "./dist/base/index.d.ts",
"scripts": {
"build": "node build/esbuild.js",
"compile": "tsc -p tsconfig.json",
"compile": "tsc -p tsconfig.json && tsx ./build/postcompile.ts",
"watch": "tsc --watch --sourceMap",
"test": "vscode-test",
"prettier": "prettier --list-different --write --cache .",
Expand All @@ -25,6 +25,7 @@
"@types/node": "^20.11.30",
"@vscode/test-cli": "^0.0.9",
"@vscode/test-electron": "^2.3.10",
"@types/vscode": "^1.89.0",
"esbuild": "0.20.2",
"mocha": "^10.2.0",
"npm-dts": "^1.3.12",
Expand Down
4 changes: 2 additions & 2 deletions src/base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
* Copyright (c) Microsoft Corporation and GitHub. All rights reserved.
*--------------------------------------------------------------------------------------------*/

import type { CancellationToken, LanguageModelChatMessage, Progress } from "vscode";
import type { CancellationToken, Progress } from "vscode";
import { ChatMessage, ChatRole } from "./openai";
import { MetadataMap, PromptRenderer } from "./promptRenderer";
import { PromptReference } from "./results";
import { ITokenizer } from "./tokenizer/tokenizer";
import { BasePromptElementProps, IChatEndpointInfo, PromptElementCtor } from "./types";
import { ChatDocumentContext, ChatResponsePart } from "./vscodeTypes";
import { ChatDocumentContext, ChatResponsePart, LanguageModelChatMessage } from "./vscodeTypes.d";

export { ChatMessage, ChatRole } from './openai';
export * from './results';
Expand Down
228 changes: 228 additions & 0 deletions src/base/vscodeTypes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation and GitHub. All rights reserved.
*--------------------------------------------------------------------------------------------*/

import type { Command, MarkdownString, Range, ThemeIcon, Uri } from 'vscode';

/**
* Represents a part of a chat response that is formatted as Markdown.
*/
export class ChatResponseMarkdownPart {
/**
* A markdown string or a string that should be interpreted as markdown.
*/
value: MarkdownString;

/**
* Create a new ChatResponseMarkdownPart.
*
* @param value A markdown string or a string that should be interpreted as markdown. The boolean form of {@link MarkdownString.isTrusted} is NOT supported.
*/
constructor(value: string | MarkdownString);
}

/**
* Represents a file tree structure in a chat response.
*/
export interface ChatResponseFileTree {
/**
* The name of the file or directory.
*/
name: string;

/**
* An array of child file trees, if the current file tree is a directory.
*/
children?: ChatResponseFileTree[];
}

/**
* Represents a part of a chat response that is a file tree.
*/
export class ChatResponseFileTreePart {
/**
* File tree data.
*/
value: ChatResponseFileTree[];

/**
* The base uri to which this file tree is relative
*/
baseUri: Uri;

/**
* Create a new ChatResponseFileTreePart.
* @param value File tree data.
* @param baseUri The base uri to which this file tree is relative.
*/
constructor(value: ChatResponseFileTree[], baseUri: Uri);
}

/**
* Represents a part of a chat response that is an anchor, that is rendered as a link to a target.
*/
export class ChatResponseAnchorPart {
/**
* The target of this anchor.
*/
value: Uri | Location;

/**
* An optional title that is rendered with value.
*/
title?: string;

/**
* Create a new ChatResponseAnchorPart.
* @param value A uri or location.
* @param title An optional title that is rendered with value.
*/
constructor(value: Uri | Location, title?: string);
}

/**
* Represents a part of a chat response that is a progress message.
*/
export class ChatResponseProgressPart {
/**
* The progress message
*/
value: string;

/**
* Create a new ChatResponseProgressPart.
* @param value A progress message
*/
constructor(value: string);
}

/**
* Represents a part of a chat response that is a reference, rendered separately from the content.
*/
export class ChatResponseReferencePart {
/**
* The reference target.
*/
value: Uri | Location;

/**
* The icon for the reference.
*/
iconPath?: Uri | ThemeIcon | {
/**
* The icon path for the light theme.
*/
light: Uri;
/**
* The icon path for the dark theme.
*/
dark: Uri;
};

/**
* Create a new ChatResponseReferencePart.
* @param value A uri or location
* @param iconPath Icon for the reference shown in UI
*/
constructor(value: Uri | Location, iconPath?: Uri | ThemeIcon | {
/**
* The icon path for the light theme.
*/
light: Uri;
/**
* The icon path for the dark theme.
*/
dark: Uri;
});
}

/**
* Represents a part of a chat response that is a button that executes a command.
*/
export class ChatResponseCommandButtonPart {
/**
* The command that will be executed when the button is clicked.
*/
value: Command;

/**
* Create a new ChatResponseCommandButtonPart.
* @param value A Command that will be executed when the button is clicked.
*/
constructor(value: Command);
}

/**
* Represents the different chat response types.
*/
export type ChatResponsePart = ChatResponseMarkdownPart | ChatResponseFileTreePart | ChatResponseAnchorPart
| ChatResponseProgressPart | ChatResponseReferencePart | ChatResponseCommandButtonPart;


export interface ChatDocumentContext {
uri: Uri;
version: number;
ranges: Range[];
}


/**
* Represents the role of a chat message. This is either the user or the assistant.
*/
export enum LanguageModelChatMessageRole {
/**
* The user role, e.g the human interacting with a language model.
*/
User = 1,

/**
* The assistant role, e.g. the language model generating responses.
*/
Assistant = 2
}

/**
* Represents a message in a chat. Can assume different roles, like user or assistant.
*/
export class LanguageModelChatMessage {

/**
* Utility to create a new user message.
*
* @param content The content of the message.
* @param name The optional name of a user for the message.
*/
static User(content: string, name?: string): LanguageModelChatMessage;

/**
* Utility to create a new assistant message.
*
* @param content The content of the message.
* @param name The optional name of a user for the message.
*/
static Assistant(content: string, name?: string): LanguageModelChatMessage;

/**
* The role of this message.
*/
role: LanguageModelChatMessageRole;

/**
* The content of this message.
*/
content: string;

/**
* The optional name of a user for this message.
*/
name: string | undefined;

/**
* Create a new user message.
*
* @param role The role of the message.
* @param content The content of the message.
* @param name The optional name of a user for the message.
*/
constructor(role: LanguageModelChatMessageRole, content: string, name?: string);
}
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"exclude": ["build"],
"include": ["src", "vscode.d.ts"],
"include": ["src"],
"compilerOptions": {
"module": "commonjs",
"target": "es2020",
Expand Down
Loading

0 comments on commit 21e1bc8

Please sign in to comment.