use openai-adapters

This commit is contained in:
Nate 2025-07-15 00:07:30 -07:00
parent 371f75fb09
commit 7667374bfb
8 changed files with 194 additions and 264 deletions

254
package-lock.json generated
View file

@ -1,15 +1,16 @@
{
"name": "@continuedev/cli",
"version": "1.2.0",
"version": "1.3.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "@continuedev/cli",
"version": "1.2.0",
"version": "1.3.0",
"license": "Apache-2.0",
"dependencies": {
"@continuedev/openai-adapters": "^1.0.18",
"@continuedev/config-yaml": "^1.0.96",
"@continuedev/openai-adapters": "^1.0.46-beta",
"@continuedev/sdk": "^0.0.12",
"@modelcontextprotocol/sdk": "^1.9.0",
"@types/react": "^19.1.8",
@ -23,6 +24,7 @@
"ink": "^6.0.1",
"lowlight": "^3.3.0",
"minimatch": "^10.0.3",
"node-fetch": "^3.3.2",
"open": "^10.1.0",
"react": "^19.1.0",
"readline-sync": "^1.4.10",
@ -55,6 +57,32 @@
"tsx": "^4.20.3"
}
},
"../continue/packages/openai-adapters": {
"name": "@continuedev/openai-adapters",
"version": "1.0.46-beta",
"license": "Apache-2.0",
"dependencies": {
"@aws-sdk/client-bedrock-runtime": "^3.842.0",
"@aws-sdk/credential-providers": "^3.840.0",
"@continuedev/config-types": "^1.0.5",
"@continuedev/config-yaml": "^1.0.51",
"@continuedev/fetch": "^1.0.15",
"dotenv": "^16.5.0",
"json-schema": "^0.4.0",
"node-fetch": "^3.3.2",
"openai": "^4.104.0",
"uuid": "^11.1.0",
"zod": "^3.24.2"
},
"devDependencies": {
"@types/follow-redirects": "^1.14.4",
"@types/jest": "^29.5.14",
"@types/json-schema": "^7.0.15",
"cross-env": "^7.0.3",
"ts-jest": "^29.2.3",
"ts-node": "^10.9.2"
}
},
"node_modules/@alcalzone/ansi-tokenize": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/@alcalzone/ansi-tokenize/-/ansi-tokenize-0.1.3.tgz",
@ -103,137 +131,6 @@
"node": ">=6.0.0"
}
},
"node_modules/@azure-rest/core-client": {
"version": "1.4.0",
"resolved": "https://registry.npmjs.org/@azure-rest/core-client/-/core-client-1.4.0.tgz",
"integrity": "sha512-ozTDPBVUDR5eOnMIwhggbnVmOrka4fXCs8n8mvUo4WLLc38kki6bAOByDoVZZPz/pZy2jMt2kwfpvy/UjALj6w==",
"license": "MIT",
"dependencies": {
"@azure/abort-controller": "^2.0.0",
"@azure/core-auth": "^1.3.0",
"@azure/core-rest-pipeline": "^1.5.0",
"@azure/core-tracing": "^1.0.1",
"@azure/core-util": "^1.0.0",
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@azure/abort-controller": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/@azure/abort-controller/-/abort-controller-2.1.2.tgz",
"integrity": "sha512-nBrLsEWm4J2u5LpAPjxADTlq3trDgVZZXHNKabeXZtpq3d3AbN/KGO82R87rdDz5/lYB024rtEf10/q0urNgsA==",
"license": "MIT",
"dependencies": {
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@azure/core-auth": {
"version": "1.9.0",
"resolved": "https://registry.npmjs.org/@azure/core-auth/-/core-auth-1.9.0.tgz",
"integrity": "sha512-FPwHpZywuyasDSLMqJ6fhbOK3TqUdviZNF8OqRGA4W5Ewib2lEEZ+pBsYcBa88B2NGO/SEnYPGhyBqNlE8ilSw==",
"license": "MIT",
"dependencies": {
"@azure/abort-controller": "^2.0.0",
"@azure/core-util": "^1.11.0",
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@azure/core-rest-pipeline": {
"version": "1.20.0",
"resolved": "https://registry.npmjs.org/@azure/core-rest-pipeline/-/core-rest-pipeline-1.20.0.tgz",
"integrity": "sha512-ASoP8uqZBS3H/8N8at/XwFr6vYrRP3syTK0EUjDXQy0Y1/AUS+QeIRThKmTNJO2RggvBBxaXDPM7YoIwDGeA0g==",
"license": "MIT",
"dependencies": {
"@azure/abort-controller": "^2.0.0",
"@azure/core-auth": "^1.8.0",
"@azure/core-tracing": "^1.0.1",
"@azure/core-util": "^1.11.0",
"@azure/logger": "^1.0.0",
"@typespec/ts-http-runtime": "^0.2.2",
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@azure/core-sse": {
"version": "2.2.0",
"resolved": "https://registry.npmjs.org/@azure/core-sse/-/core-sse-2.2.0.tgz",
"integrity": "sha512-6Xg/CeW0jRyMoWt+puw2x6Qqkml3tr76Cn/oA9goIcUXtsi3ngmTwCVbwqkUWfhsOfo4F+78LGgiswSxTHN0sg==",
"license": "MIT",
"dependencies": {
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@azure/core-tracing": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@azure/core-tracing/-/core-tracing-1.2.0.tgz",
"integrity": "sha512-UKTiEJPkWcESPYJz3X5uKRYyOcJD+4nYph+KpfdPRnQJVrZfk0KJgdnaAWKfhsBBtAf/D58Az4AvCJEmWgIBAg==",
"license": "MIT",
"dependencies": {
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@azure/core-util": {
"version": "1.12.0",
"resolved": "https://registry.npmjs.org/@azure/core-util/-/core-util-1.12.0.tgz",
"integrity": "sha512-13IyjTQgABPARvG90+N2dXpC+hwp466XCdQXPCRlbWHgd3SJd5Q1VvaBGv6k1BIa4MQm6hAF1UBU1m8QUxV8sQ==",
"license": "MIT",
"dependencies": {
"@azure/abort-controller": "^2.0.0",
"@typespec/ts-http-runtime": "^0.2.2",
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@azure/logger": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/@azure/logger/-/logger-1.2.0.tgz",
"integrity": "sha512-0hKEzLhpw+ZTAfNJyRrn6s+V0nDWzXk9OjBr2TiGIu0OfMr5s2V4FpKLTAK3Ca5r5OKLbf4hkOGDPyiRjie/jA==",
"license": "MIT",
"dependencies": {
"@typespec/ts-http-runtime": "^0.2.2",
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@azure/openai": {
"version": "1.0.0-beta.13",
"resolved": "https://registry.npmjs.org/@azure/openai/-/openai-1.0.0-beta.13.tgz",
"integrity": "sha512-oHE5ScnPTXALmyEBgqokZlYVT7F76EfrKjMWF+YcFJdUxk9Adhvht2iL5v+QpmlAIMdkih1q8DkTs/tApDjBpw==",
"deprecated": "The Azure OpenAI client library for JavaScript beta has been retired. Please migrate to the stable OpenAI SDK for JavaScript using the migration guide: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/openai/openai/MIGRATION.md.",
"license": "MIT",
"dependencies": {
"@azure-rest/core-client": "^1.1.7",
"@azure/core-auth": "^1.4.0",
"@azure/core-rest-pipeline": "^1.13.0",
"@azure/core-sse": "^2.0.0",
"@azure/core-util": "^1.4.0",
"@azure/logger": "^1.0.3",
"tslib": "^2.4.0"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@babel/code-frame": {
"version": "7.27.1",
"resolved": "https://registry.npmjs.org/@babel/code-frame/-/code-frame-7.27.1.tgz",
@ -735,16 +632,14 @@
"version": "1.0.14",
"resolved": "https://registry.npmjs.org/@continuedev/config-types/-/config-types-1.0.14.tgz",
"integrity": "sha512-PVHyHPyRXd2QsaNgnCpiKYU3uHFTlyuQSkqE8OwrBmQqO6/TXUVIr/2EGtyIZGrml4Y+rGMSH40WU4/0t4SGpQ==",
"license": "Apache-2.0",
"dependencies": {
"zod": "^3.23.8"
}
},
"node_modules/@continuedev/config-yaml": {
"version": "1.0.86",
"resolved": "https://registry.npmjs.org/@continuedev/config-yaml/-/config-yaml-1.0.86.tgz",
"integrity": "sha512-X0H1iPunFcqxgTcoS87EvbqNHjfkfidWt8rQ2CU5A8iBzPSDI/jdkok0Bm0MjgzgZuXbR6GxvwMcZcOTiFw1nA==",
"license": "Apache-2.0",
"version": "1.0.96",
"resolved": "https://registry.npmjs.org/@continuedev/config-yaml/-/config-yaml-1.0.96.tgz",
"integrity": "sha512-zEdSI1k2SSEj1qRuOpyERebqMzCEynJ2ce2+q2/5VjR1/2uSi2PpiZr5saofs2VKSQtIU4Pj4xwQ7Utd0hQEBA==",
"dependencies": {
"@continuedev/config-types": "^1.0.14",
"yaml": "^2.6.1",
@ -754,49 +649,9 @@
"config-yaml": "dist/cli.js"
}
},
"node_modules/@continuedev/fetch": {
"version": "1.0.6",
"resolved": "https://registry.npmjs.org/@continuedev/fetch/-/fetch-1.0.6.tgz",
"integrity": "sha512-QpB9cHgEw5avoouFR0qm7/XTo44SrVfkGAmzbDTui2P9djj+DYvj+0yaJ8e/Ubyc5PUjFTUS2YnRar7NfYpHIg==",
"license": "Apache-2.0",
"dependencies": {
"@continuedev/config-types": "^1.0.5",
"follow-redirects": "^1.15.6",
"http-proxy-agent": "^7.0.2",
"https-proxy-agent": "^7.0.5",
"node-fetch": "^3.3.2"
}
},
"node_modules/@continuedev/openai-adapters": {
"version": "1.0.21",
"resolved": "https://registry.npmjs.org/@continuedev/openai-adapters/-/openai-adapters-1.0.21.tgz",
"integrity": "sha512-8sfBHm9LNplT5SB/CqHd/FKz5dszc3wme9zBhMw28CbO+WBup+dd/+d1701uuf5Z/12xWA9T8+VZr3y7CHfp9Q==",
"license": "Apache-2.0",
"dependencies": {
"@azure/openai": "^1.0.0-beta.12",
"@continuedev/config-types": "^1.0.5",
"@continuedev/config-yaml": "^1.0.51",
"@continuedev/fetch": "^1.0.3",
"dotenv": "^16.5.0",
"json-schema": "^0.4.0",
"node-fetch": "^3.3.2",
"openai": "^4.76.0",
"uuid": "^11.1.0",
"zod": "^3.24.2"
}
},
"node_modules/@continuedev/openai-adapters/node_modules/uuid": {
"version": "11.1.0",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-11.1.0.tgz",
"integrity": "sha512-0/A9rDy9P7cJ+8w1c9WD9V//9Wj15Ce2MPz8Ri6032usz+NfePxx5AcN3bN+r6ZL6jEo066/yNYB3tn4pQEx+A==",
"funding": [
"https://github.com/sponsors/broofa",
"https://github.com/sponsors/ctavan"
],
"license": "MIT",
"bin": {
"uuid": "dist/esm/bin/uuid"
}
"resolved": "../continue/packages/openai-adapters",
"link": true
},
"node_modules/@continuedev/sdk": {
"version": "0.0.12",
@ -3149,20 +3004,6 @@
"dev": true,
"license": "MIT"
},
"node_modules/@typespec/ts-http-runtime": {
"version": "0.2.2",
"resolved": "https://registry.npmjs.org/@typespec/ts-http-runtime/-/ts-http-runtime-0.2.2.tgz",
"integrity": "sha512-Gz/Sm64+Sq/vklJu1tt9t+4R2lvnud8NbTD/ZfpZtMiUX7YeVpCA8j6NSW8ptwcoLL+NmYANwqP8DV0q/bwl2w==",
"license": "MIT",
"dependencies": {
"http-proxy-agent": "^7.0.0",
"https-proxy-agent": "^7.0.0",
"tslib": "^2.6.2"
},
"engines": {
"node": ">=18.0.0"
}
},
"node_modules/@workos-inc/node": {
"version": "7.49.0",
"resolved": "https://registry.npmjs.org/@workos-inc/node/-/node-7.49.0.tgz",
@ -3233,6 +3074,7 @@
"version": "7.1.3",
"resolved": "https://registry.npmjs.org/agent-base/-/agent-base-7.1.3.tgz",
"integrity": "sha512-jRR5wdylq8CkOe6hei19GGZnxM6rBGwFl3Bg0YItGDimvjGtAvdZk4Pu6Cl4u4Igsws4a1fd1Vq3ezrhn4KmFw==",
"dev": true,
"license": "MIT",
"engines": {
"node": ">= 14"
@ -4569,7 +4411,6 @@
"version": "4.0.1",
"resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz",
"integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==",
"license": "MIT",
"engines": {
"node": ">= 12"
}
@ -5405,7 +5246,6 @@
"url": "https://paypal.me/jimmywarting"
}
],
"license": "MIT",
"dependencies": {
"node-domexception": "^1.0.0",
"web-streams-polyfill": "^3.0.3"
@ -5653,7 +5493,6 @@
"version": "4.0.10",
"resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz",
"integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==",
"license": "MIT",
"dependencies": {
"fetch-blob": "^3.1.2"
},
@ -6120,6 +5959,7 @@
"version": "7.0.2",
"resolved": "https://registry.npmjs.org/http-proxy-agent/-/http-proxy-agent-7.0.2.tgz",
"integrity": "sha512-T1gkAiYYDWYx3V5Bmyu7HcfcvL7mUrTWiM6yOfa3PIphViJ/gFPbvidQ+veqSOHci/PxBcDabeUNCzpOODJZig==",
"dev": true,
"license": "MIT",
"dependencies": {
"agent-base": "^7.1.0",
@ -6133,6 +5973,7 @@
"version": "7.0.6",
"resolved": "https://registry.npmjs.org/https-proxy-agent/-/https-proxy-agent-7.0.6.tgz",
"integrity": "sha512-vK9P5/iUfdl95AI+JVyUuIcVtd4ofvtrOr3HNtM2yxC9bnMbEdp3x01OhQNnjb8IJYi38VlTE3mBXwcfvywuSw==",
"dev": true,
"license": "MIT",
"dependencies": {
"agent-base": "^7.1.2",
@ -8195,12 +8036,6 @@
"dev": true,
"license": "MIT"
},
"node_modules/json-schema": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/json-schema/-/json-schema-0.4.0.tgz",
"integrity": "sha512-es94M3nTIfsEPisRafak+HDLfHXnKBhV3vU5eqPcS3flIWqcxJWgXHXiey3YrpaNsanY5ei1VoYEbOzijuq9BA==",
"license": "(AFL-2.1 OR BSD-3-Clause)"
},
"node_modules/json5": {
"version": "2.2.3",
"resolved": "https://registry.npmjs.org/json5/-/json5-2.2.3.tgz",
@ -8763,7 +8598,6 @@
"version": "3.3.2",
"resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz",
"integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==",
"license": "MIT",
"dependencies": {
"data-uri-to-buffer": "^4.0.0",
"fetch-blob": "^3.1.4",
@ -14235,7 +14069,6 @@
"version": "3.3.3",
"resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.3.3.tgz",
"integrity": "sha512-d2JWLCivmZYTSIoge9MsgFCZrt571BikcWGYkjC1khllbTeDlGqZ2D8vD8E/lJa8WGWbb7Plm8/XJYV7IJHZZw==",
"license": "MIT",
"engines": {
"node": ">= 8"
}
@ -14486,15 +14319,14 @@
"license": "ISC"
},
"node_modules/yaml": {
"version": "2.7.1",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.7.1.tgz",
"integrity": "sha512-10ULxpnOCQXxJvBgxsn9ptjq6uviG/htZKk9veJGhlqn3w/DxQ631zFF+nlQXLwmImeS5amR2dl2U8sg6U9jsQ==",
"license": "ISC",
"version": "2.8.0",
"resolved": "https://registry.npmjs.org/yaml/-/yaml-2.8.0.tgz",
"integrity": "sha512-4lLa/EcQCB0cJkyts+FpIRx5G/llPxfP6VQU5KByHEhLxY3IJCH0f0Hy1MHI8sClTvsIb8qwRJ6R/ZdlDJ/leQ==",
"bin": {
"yaml": "bin.mjs"
},
"engines": {
"node": ">= 14"
"node": ">= 14.6"
}
},
"node_modules/yargs": {

View file

@ -32,7 +32,8 @@
},
"homepage": "https://continue.dev",
"dependencies": {
"@continuedev/openai-adapters": "^1.0.18",
"@continuedev/config-yaml": "^1.0.96",
"@continuedev/openai-adapters": "^1.0.46-beta",
"@continuedev/sdk": "^0.0.12",
"@modelcontextprotocol/sdk": "^1.9.0",
"@types/react": "^19.1.8",
@ -46,6 +47,7 @@
"ink": "^6.0.1",
"lowlight": "^3.3.0",
"minimatch": "^10.0.3",
"node-fetch": "^3.3.2",
"open": "^10.1.0",
"react": "^19.1.0",
"readline-sync": "^1.4.10",

View file

@ -11,7 +11,7 @@ import { env } from "../env.js";
// Config file path
const AUTH_CONFIG_PATH = path.join(os.homedir(), ".continue", "auth.json");
interface AuthConfig {
export interface AuthConfig {
userId?: string;
userEmail?: string;
accessToken?: string;

View file

@ -1,21 +1,26 @@
#!/usr/bin/env node
import { ContinueClient } from "@continuedev/sdk";
import {
BaseLlmApi,
constructLlmApi,
LLMConfig,
} from "@continuedev/openai-adapters";
import { Assistant, ContinueClient } from "@continuedev/sdk";
import chalk from "chalk";
import { ChatCompletionMessageParam } from "openai/resources.mjs";
import * as readlineSync from "readline-sync";
import { parseArgs } from "./args.js";
import { ensureAuthenticated } from "./auth/ensureAuth.js";
import { loadAuthConfig } from "./auth/workos.js";
import { AuthConfig, loadAuthConfig } from "./auth/workos.js";
import { initializeContinueSDK } from "./continueSDK.js";
import { introMessage } from "./intro.js";
import { configureLogger } from "./logger.js";
import { MCPService } from "./mcp.js";
import { loadSession, saveSession } from "./session.js";
import { handleSlashCommands } from "./slashCommands.js";
import { streamChatResponse } from "./streamChatResponse.js";
import { constructSystemMessage } from "./systemMessage.js";
import { startTUIChat } from "./ui/index.js";
import { loadSession, saveSession, hasSession } from "./session.js";
// Parse command line arguments
const args = parseArgs();
@ -23,6 +28,53 @@ const args = parseArgs();
// Configure logger based on headless mode
configureLogger(args.isHeadless);
function getLlmApi(
assistant: Assistant,
authConfig: AuthConfig
): [BaseLlmApi, string] {
const model = assistant.config.models?.find((model) =>
model?.roles?.includes("chat")
);
if (!model) {
throw new Error(
"No models with the chat role found in the configured assistant"
);
}
const config: LLMConfig =
model.provider === "continue-proxy"
? {
provider: model.provider,
requestOptions: model.requestOptions,
apiBase: model.apiBase,
apiKey: authConfig.accessToken,
env: {
apiKeyLocation: (model as any).apiKeyLocation,
// envSecretLocations: model.env,
orgScopeId: null, // TODO
proxyUrl: undefined, // TODO
},
}
: {
provider: model.provider as any,
apiKey: model.apiKey,
apiBase: model.apiBase,
requestOptions: model.requestOptions,
env: model.env,
};
const llmApi = constructLlmApi(config);
if (!llmApi) {
throw new Error(
"Failed to initialized LLM. Please check your configuration."
);
}
return [llmApi, model.model];
}
async function chat() {
const isAuthenticated = await ensureAuthenticated(true);
@ -63,7 +115,8 @@ async function chat() {
// Initialize ContinueSDK and MCPService once
let assistant: ContinueClient["assistant"];
let client: ContinueClient["client"];
let model: string;
let llmApi: BaseLlmApi;
let mcpService: MCPService;
try {
@ -73,7 +126,8 @@ async function chat() {
);
assistant = continueSdk.assistant;
client = continueSdk.client;
[llmApi, model] = getLlmApi(assistant, authConfig);
mcpService = await MCPService.create(assistant.config);
} catch (error) {
console.error(
@ -85,7 +139,14 @@ async function chat() {
// If not in headless mode, start the TUI chat (default)
if (!args.isHeadless) {
await startTUIChat(assistant, client, mcpService, args.prompt, args.resume);
await startTUIChat(
assistant,
llmApi,
model,
mcpService,
args.prompt,
args.resume
);
return;
}
@ -94,7 +155,7 @@ async function chat() {
// Rules
let chatHistory: ChatCompletionMessageParam[] = [];
// Load previous session if --resume flag is used
if (args.resume) {
const savedHistory = loadSession();
@ -105,7 +166,7 @@ async function chat() {
console.log(chalk.yellow("No previous session found, starting fresh..."));
}
}
// If no session loaded or not resuming, initialize with system message
if (chatHistory.length === 0) {
const rulesSystemMessage = assistant.systemMessage;
@ -156,13 +217,19 @@ async function chat() {
}
try {
const finalResponse = await streamChatResponse(chatHistory, assistant, client);
const abortController = new AbortController();
const finalResponse = await streamChatResponse(
chatHistory,
model,
llmApi,
abortController
);
// In headless mode, only print the final response
if (args.isHeadless && finalResponse.trim()) {
console.log(finalResponse);
}
// Save session after each successful response
saveSession(chatHistory);
} catch (e: any) {

View file

@ -1,4 +1,4 @@
import { ContinueClient } from "@continuedev/sdk";
import { BaseLlmApi } from "@continuedev/openai-adapters";
import chalk from "chalk";
import * as dotenv from "dotenv";
import type {
@ -62,10 +62,10 @@ export interface StreamCallbacks {
// Define a function to handle streaming responses with tool calling
export async function streamChatResponse(
chatHistory: ChatCompletionMessageParam[],
assistant: ContinueClient["assistant"],
client: ContinueClient["client"],
callbacks?: StreamCallbacks,
abortController?: AbortController
model: string,
llmApi: BaseLlmApi,
abortController: AbortController,
callbacks?: StreamCallbacks
) {
const args = parseArgs();
const isHeadless = args.isHeadless;
@ -84,14 +84,15 @@ export async function streamChatResponse(
// "chat.log",
// "---\n\n" + JSON.stringify(chatHistory, null, 2) + "\n\n"
// );
stream = await client.chat.completions.create({
model: assistant.getModel(),
messages: chatHistory,
stream: true,
tools: toolsForRequest,
}, {
signal: abortController?.signal,
});
stream = llmApi.chatCompletionStream(
{
model,
messages: chatHistory,
stream: true,
tools: toolsForRequest,
},
abortController.signal
);
} catch (error: any) {
console.error(
chalk.red("Error in streamChatResponse:"),
@ -165,7 +166,7 @@ export async function streamChatResponse(
// Try to parse complete JSON
const parsed = JSON.parse(toolArguments);
toolCall.arguments = parsed;
// Notify start if we haven't already and have both name and args
if (toolCall.name && !toolCall.startNotified) {
toolCall.startNotified = true;
@ -193,7 +194,11 @@ export async function streamChatResponse(
}
// Notify that content is complete if we have content and are about to process tool calls
if (aiResponse.trim() && currentToolCalls.length > 0 && callbacks?.onContentComplete) {
if (
aiResponse.trim() &&
currentToolCalls.length > 0 &&
callbacks?.onContentComplete
) {
callbacks.onContentComplete(aiResponse);
}
@ -258,9 +263,9 @@ export async function streamChatResponse(
callbacks.onToolError(errorMessage, toolCall.name);
} else if (!isHeadless) {
console.info(
`${chalk.red("[Tool error:")} ${chalk.red(errorMessage)}${chalk.red(
")"
)}`
`${chalk.red("[Tool error:")} ${chalk.red(
errorMessage
)}${chalk.red(")")}`
);
}
}

View file

@ -1,3 +1,4 @@
import { BaseLlmApi } from "@continuedev/openai-adapters";
import { ContinueClient } from "@continuedev/sdk";
import { Box, Text, useApp } from "ink";
import { ChatCompletionMessageParam } from "openai/resources.mjs";
@ -15,7 +16,8 @@ import UserInput from "./UserInput.js";
interface TUIChatProps {
assistant: ContinueClient["assistant"];
client: ContinueClient["client"];
model: string;
llmApi: BaseLlmApi;
initialPrompt?: string;
resume?: boolean;
}
@ -31,7 +33,8 @@ interface DisplayMessage {
const TUIChat: React.FC<TUIChatProps> = ({
assistant,
client,
model,
llmApi,
initialPrompt,
resume,
}) => {
@ -275,10 +278,10 @@ const TUIChat: React.FC<TUIChatProps> = ({
await streamChatResponse(
newHistory,
assistant,
client,
streamCallbacks,
controller
model,
llmApi,
controller,
streamCallbacks
);
// Finalize any remaining streaming message

View file

@ -1,10 +1,10 @@
import { type AssistantConfig } from "@continuedev/sdk";
import { Box, Text, useApp, useInput } from "ink";
import React, { useState } from "react";
import SlashCommandUI from "./SlashCommandUI.js";
import FileSearchUI from "./FileSearchUI.js";
import { TextBuffer } from "./TextBuffer.js";
import { InputHistory } from "../util/inputHistory.js";
import FileSearchUI from "./FileSearchUI.js";
import SlashCommandUI from "./SlashCommandUI.js";
import { TextBuffer } from "./TextBuffer.js";
interface UserInputProps {
onSubmit: (message: string) => void;
@ -33,7 +33,9 @@ const UserInput: React.FC<UserInputProps> = ({
const [showFileSearch, setShowFileSearch] = useState(false);
const [fileSearchFilter, setFileSearchFilter] = useState("");
const [selectedFileIndex, setSelectedFileIndex] = useState(0);
const [currentFiles, setCurrentFiles] = useState<Array<{path: string; displayName: string}>>([]);
const [currentFiles, setCurrentFiles] = useState<
Array<{ path: string; displayName: string }>
>([]);
const { exit } = useApp();
// Get all available slash commands
@ -114,8 +116,12 @@ const UserInput: React.FC<UserInputProps> = ({
cmd.name.toLowerCase().includes(slashCommandFilter.toLowerCase())
)
.sort((a, b) => {
const aStartsWith = a.name.toLowerCase().startsWith(slashCommandFilter.toLowerCase());
const bStartsWith = b.name.toLowerCase().startsWith(slashCommandFilter.toLowerCase());
const aStartsWith = a.name
.toLowerCase()
.startsWith(slashCommandFilter.toLowerCase());
const bStartsWith = b.name
.toLowerCase()
.startsWith(slashCommandFilter.toLowerCase());
if (aStartsWith && !bStartsWith) return -1;
if (!aStartsWith && bStartsWith) return 1;
@ -155,7 +161,7 @@ const UserInput: React.FC<UserInputProps> = ({
const afterCursor = inputText.slice(cursorPosition);
// Replace the partial file reference with the full file name
const fileName = filePath.split('/').pop() || filePath;
const fileName = filePath.split("/").pop() || filePath;
const newText = beforeAt + "@" + fileName + " " + afterCursor;
const newCursorPos = lastAtIndex + 1 + fileName.length + 1;
@ -247,24 +253,36 @@ const UserInput: React.FC<UserInputProps> = ({
// Handle file search navigation
if (showFileSearch) {
if (key.upArrow) {
setSelectedFileIndex((prev) => (prev > 0 ? prev - 1 : Math.max(0, Math.min(currentFiles.length - 1, 9))));
setSelectedFileIndex((prev) =>
prev > 0
? prev - 1
: Math.max(0, Math.min(currentFiles.length - 1, 9))
);
return;
}
if (key.downArrow) {
setSelectedFileIndex((prev) => (prev < Math.min(currentFiles.length - 1, 9) ? prev + 1 : 0));
setSelectedFileIndex((prev) =>
prev < Math.min(currentFiles.length - 1, 9) ? prev + 1 : 0
);
return;
}
if (key.return && !key.shift) {
if (currentFiles.length > 0 && selectedFileIndex < currentFiles.length) {
if (
currentFiles.length > 0 &&
selectedFileIndex < currentFiles.length
) {
selectFile(currentFiles[selectedFileIndex].path);
}
return;
}
if (key.tab) {
if (currentFiles.length > 0 && selectedFileIndex < currentFiles.length) {
if (
currentFiles.length > 0 &&
selectedFileIndex < currentFiles.length
) {
selectFile(currentFiles[selectedFileIndex].path);
}
return;
@ -336,7 +354,7 @@ const UserInput: React.FC<UserInputProps> = ({
setCursorPosition(newCursor);
updateSlashCommandState(newText, newCursor);
updateFileSearchState(newText, newCursor);
// Reset history navigation when user starts typing
inputHistory.resetNavigation();
}
@ -448,4 +466,4 @@ const UserInput: React.FC<UserInputProps> = ({
);
};
export default UserInput;
export default UserInput;

View file

@ -1,3 +1,4 @@
import { BaseLlmApi } from "@continuedev/openai-adapters";
import { ContinueClient } from "@continuedev/sdk";
import { render } from "ink";
import React from "react";
@ -9,7 +10,8 @@ export { default as MarkdownRenderer } from "./MarkdownRenderer.js";
export async function startTUIChat(
assistant: ContinueClient["assistant"],
client: ContinueClient["client"],
llmApi: BaseLlmApi,
model: string,
mcpService: MCPService,
initialPrompt?: string,
resume?: boolean
@ -21,7 +23,8 @@ export async function startTUIChat(
const { unmount } = render(
React.createElement(TUIChat, {
assistant,
client,
model,
llmApi,
initialPrompt,
resume,
})
@ -34,4 +37,4 @@ export async function startTUIChat(
});
return { unmount };
}
}