summaryrefslogtreecommitdiffhomepage
path: root/js/src
diff options
context:
space:
mode:
authorDax Raad <[email protected]>2025-05-29 14:16:15 -0400
committerDax Raad <[email protected]>2025-05-29 14:16:15 -0400
commitd398001f96fd1a7438ac2ef07b4b87bb13766b27 (patch)
treeb01c93fd0a6b7000c3f6198693bacb85fc00250d /js/src
parente68747a64aa577a3efb77eda0bfb67f16e341906 (diff)
downloadopencode-d398001f96fd1a7438ac2ef07b4b87bb13766b27.tar.gz
opencode-d398001f96fd1a7438ac2ef07b4b87bb13766b27.zip
Remove React/Ink dependencies and add context window management
- Remove unused React and Ink CLI dependencies to simplify package - Update provider schema to use maxOutputTokens for clarity - Add automatic summarization when approaching context window limits - Simplify message event handling and add cost/token metadata display 🤖 Generated with opencode Co-Authored-By: opencode <[email protected]>
Diffstat (limited to 'js/src')
-rw-r--r--js/src/index.ts50
-rw-r--r--js/src/provider/provider.ts2
-rw-r--r--js/src/session/session.ts22
3 files changed, 29 insertions, 45 deletions
diff --git a/js/src/index.ts b/js/src/index.ts
index 1f6c7ae26..7bd4e01b2 100644
--- a/js/src/index.ts
+++ b/js/src/index.ts
@@ -9,6 +9,7 @@ import cac from "cac";
import { Share } from "./share/share";
import { Storage } from "./storage/storage";
import { LLM } from "./llm/llm";
+import { Message } from "./session/message";
const cli = cac("opencode");
@@ -41,49 +42,8 @@ cli
`Share ID: ${Share.URL.replace("api.", "")}/share?id=${session.id}`,
);
- let index = 0;
- Bus.subscribe(Storage.Event.Write, async (payload) => {
- const [root, , type, messageID] = payload.properties.key.split("/");
- if (root !== "session" && type !== "message") return;
- const message = await Session.messages(session.id).then((x) =>
- x.find((x) => x.id === messageID),
- );
- if (!message) return;
-
- for (; index < message.parts.length; index++) {
- const part = message.parts[index];
- if (part.type === "text") continue;
- if (part.type === "step-start") continue;
- if (
- part.type === "tool-invocation" &&
- part.toolInvocation.state !== "result"
- )
- break;
-
- if (part.type === "tool-invocation") {
- console.log(`🔧 ${part.toolInvocation.toolName}`);
- if (
- part.toolInvocation.state === "result" &&
- "result" in part.toolInvocation
- ) {
- const result = part.toolInvocation.result;
- if (typeof result === "string") {
- const lines = result.split("\n");
- const truncated = lines.slice(0, 4);
- if (lines.length > 4) truncated.push("...");
- console.log(truncated.join("\n"));
- } else if (result && typeof result === "object") {
- const jsonStr = JSON.stringify(result, null, 2);
- const lines = jsonStr.split("\n");
- const truncated = lines.slice(0, 4);
- if (lines.length > 4) truncated.push("...");
- console.log(truncated.join("\n"));
- }
- }
- continue;
- }
- console.log(part);
- }
+ Bus.subscribe(Message.Event.Updated, async (message) => {
+ console.log("Thinking...");
});
const providers = await LLM.providers();
@@ -107,6 +67,10 @@ cli
console.log("opencode:", part.text);
}
}
+ console.log({
+ cost: result.metadata.assistant?.cost,
+ tokens: result.metadata.assistant?.tokens,
+ });
});
});
diff --git a/js/src/provider/provider.ts b/js/src/provider/provider.ts
index 44482b6db..a35645e69 100644
--- a/js/src/provider/provider.ts
+++ b/js/src/provider/provider.ts
@@ -11,7 +11,7 @@ export namespace Provider {
outputCached: z.number(),
}),
contextWindow: z.number(),
- maxTokens: z.number().optional(),
+ maxOutputTokens: z.number().optional(),
attachment: z.boolean(),
reasoning: z.boolean().optional(),
})
diff --git a/js/src/session/session.ts b/js/src/session/session.ts
index e773ed297..0de559dbc 100644
--- a/js/src/session/session.ts
+++ b/js/src/session/session.ts
@@ -149,11 +149,31 @@ export namespace Session {
modelID: string;
parts: Message.Part[];
}) {
- using abort = lock(input.sessionID);
const l = log.clone().tag("session", input.sessionID);
l.info("chatting");
const model = await LLM.findModel(input.providerID, input.modelID);
let msgs = await messages(input.sessionID);
+ const previous = msgs.at(-1);
+ if (previous?.metadata.assistant) {
+ const tokens =
+ previous.metadata.assistant.tokens.input +
+ previous.metadata.assistant.tokens.output;
+ console.log(tokens);
+ if (
+ tokens >
+ (model.info.contextWindow - (model.info.maxOutputTokens ?? 0)) * 0.9
+ ) {
+ await summarize({
+ sessionID: input.sessionID,
+ providerID: input.providerID,
+ modelID: input.modelID,
+ });
+ return chat(input);
+ }
+ }
+
+ using abort = lock(input.sessionID);
+
const lastSummary = msgs.findLast(
(msg) => msg.metadata.assistant?.summary === true,
);