openfleet 0.3.13 → 0.3.14
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.
- package/dist/index.js +113 -9
- package/dist/lib/fallback.d.ts +21 -0
- package/dist/models.d.ts +1 -0
- package/dist/transcript/hooks.d.ts +17 -0
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -59,6 +59,7 @@ var models = {
|
|
|
59
59
|
var defaultModel = models.anthropic.sonnet;
|
|
60
60
|
var bigModel = defaultModel;
|
|
61
61
|
var smallModel = defaultModel;
|
|
62
|
+
var fallbackModel = models.freeModels.minimaxM25Free;
|
|
62
63
|
|
|
63
64
|
// src/agents/names.ts
|
|
64
65
|
var AGENT_NAMES = {
|
|
@@ -1125,6 +1126,80 @@ import { tool } from "@opencode-ai/plugin";
|
|
|
1125
1126
|
import { existsSync as existsSync3, readFileSync } from "fs";
|
|
1126
1127
|
import path3 from "path";
|
|
1127
1128
|
|
|
1129
|
+
// src/lib/fallback.ts
|
|
1130
|
+
var CREDIT_BALANCE_PATTERNS = [
|
|
1131
|
+
"credit balance is too low",
|
|
1132
|
+
"insufficient credits",
|
|
1133
|
+
"please go to plans & billing",
|
|
1134
|
+
"purchase credits"
|
|
1135
|
+
];
|
|
1136
|
+
var fallbackInProgress = new Set;
|
|
1137
|
+
var fallbackSessions = new Set;
|
|
1138
|
+
var lastFallbackTime = new Map;
|
|
1139
|
+
var COOLDOWN_MS = 30000;
|
|
1140
|
+
function isCreditBalanceError(message) {
|
|
1141
|
+
const lower = message.toLowerCase();
|
|
1142
|
+
return CREDIT_BALANCE_PATTERNS.some((p) => lower.includes(p));
|
|
1143
|
+
}
|
|
1144
|
+
function isSessionInFallback(sessionID) {
|
|
1145
|
+
return fallbackSessions.has(sessionID);
|
|
1146
|
+
}
|
|
1147
|
+
function getFallbackModelOverride() {
|
|
1148
|
+
const [providerID, modelID] = fallbackModel.split("/");
|
|
1149
|
+
return { providerID, modelID };
|
|
1150
|
+
}
|
|
1151
|
+
async function handleCreditBalanceFallback(client, sessionID) {
|
|
1152
|
+
if (fallbackInProgress.has(sessionID))
|
|
1153
|
+
return;
|
|
1154
|
+
const last = lastFallbackTime.get(sessionID);
|
|
1155
|
+
if (last && Date.now() - last < COOLDOWN_MS)
|
|
1156
|
+
return;
|
|
1157
|
+
fallbackInProgress.add(sessionID);
|
|
1158
|
+
fallbackSessions.add(sessionID);
|
|
1159
|
+
lastFallbackTime.set(sessionID, Date.now());
|
|
1160
|
+
try {
|
|
1161
|
+
await client.session.abort({ path: { id: sessionID } });
|
|
1162
|
+
const { data: messages } = await client.session.messages({
|
|
1163
|
+
path: { id: sessionID }
|
|
1164
|
+
});
|
|
1165
|
+
if (!messages || messages.length === 0) {
|
|
1166
|
+
throw new Error("No messages found after abort");
|
|
1167
|
+
}
|
|
1168
|
+
const lastUserMsg = [...messages].reverse().find((m) => m.info.role === "user");
|
|
1169
|
+
if (!lastUserMsg) {
|
|
1170
|
+
throw new Error("No user message found to revert to");
|
|
1171
|
+
}
|
|
1172
|
+
const textPart = lastUserMsg.parts.find((p) => p.type === "text");
|
|
1173
|
+
if (!textPart || textPart.type !== "text")
|
|
1174
|
+
return;
|
|
1175
|
+
const messageID = lastUserMsg.info.id;
|
|
1176
|
+
const text = textPart.text;
|
|
1177
|
+
await client.session.revert({
|
|
1178
|
+
path: { id: sessionID },
|
|
1179
|
+
body: { messageID }
|
|
1180
|
+
});
|
|
1181
|
+
const [providerID, modelID] = fallbackModel.split("/");
|
|
1182
|
+
await client.session.prompt({
|
|
1183
|
+
path: { id: sessionID },
|
|
1184
|
+
body: {
|
|
1185
|
+
model: { providerID, modelID },
|
|
1186
|
+
parts: [{ type: "text", text }]
|
|
1187
|
+
}
|
|
1188
|
+
});
|
|
1189
|
+
logger.info("Credit balance fallback triggered", { sessionID, fallbackModel });
|
|
1190
|
+
await client.tui.showToast({
|
|
1191
|
+
body: {
|
|
1192
|
+
message: "\u26A0\uFE0F Anthropic credit balance low \u2014 switched to Minimax M2.5 Free",
|
|
1193
|
+
variant: "warning"
|
|
1194
|
+
}
|
|
1195
|
+
});
|
|
1196
|
+
} catch (err) {
|
|
1197
|
+
logger.error("Credit balance fallback failed", { sessionID, err });
|
|
1198
|
+
} finally {
|
|
1199
|
+
fallbackInProgress.delete(sessionID);
|
|
1200
|
+
}
|
|
1201
|
+
}
|
|
1202
|
+
|
|
1128
1203
|
// src/transcript/writer.ts
|
|
1129
1204
|
import { existsSync as existsSync2 } from "fs";
|
|
1130
1205
|
import { appendFile, mkdir } from "fs/promises";
|
|
@@ -1329,6 +1404,11 @@ function createTranscriptHooks(ctx) {
|
|
|
1329
1404
|
const session = await getSessionInfo(ctx, input.sessionID);
|
|
1330
1405
|
await recordUserMessage(session, output.message, output.parts);
|
|
1331
1406
|
},
|
|
1407
|
+
"chat.model": async (input, output) => {
|
|
1408
|
+
if (isSessionInFallback(input.sessionID)) {
|
|
1409
|
+
output.model = getFallbackModelOverride();
|
|
1410
|
+
}
|
|
1411
|
+
},
|
|
1332
1412
|
"tool.execute.before": async (input, output) => {
|
|
1333
1413
|
const session = await getSessionInfo(ctx, input.sessionID);
|
|
1334
1414
|
await recordToolUse(session, input.tool, input.callID, output.args);
|
|
@@ -1358,6 +1438,28 @@ function createTranscriptHooks(ctx) {
|
|
|
1358
1438
|
err
|
|
1359
1439
|
});
|
|
1360
1440
|
}
|
|
1441
|
+
},
|
|
1442
|
+
event: async ({ event }) => {
|
|
1443
|
+
if (event.type === "session.status") {
|
|
1444
|
+
const { sessionID, status } = event.properties;
|
|
1445
|
+
if (status.type === "retry" && isCreditBalanceError(status.message)) {
|
|
1446
|
+
await handleCreditBalanceFallback(ctx.client, sessionID);
|
|
1447
|
+
}
|
|
1448
|
+
}
|
|
1449
|
+
if (event.type === "session.error") {
|
|
1450
|
+
const { sessionID, error } = event.properties;
|
|
1451
|
+
if (sessionID && error && "message" in error.data && isCreditBalanceError(String(error.data.message))) {
|
|
1452
|
+
await handleCreditBalanceFallback(ctx.client, sessionID);
|
|
1453
|
+
}
|
|
1454
|
+
}
|
|
1455
|
+
if (event.type === "message.updated") {
|
|
1456
|
+
const { info } = event.properties;
|
|
1457
|
+
if (info.role === "assistant" && info.error) {
|
|
1458
|
+
if ("message" in info.error.data && isCreditBalanceError(String(info.error.data.message))) {
|
|
1459
|
+
await handleCreditBalanceFallback(ctx.client, info.sessionID);
|
|
1460
|
+
}
|
|
1461
|
+
}
|
|
1462
|
+
}
|
|
1361
1463
|
}
|
|
1362
1464
|
};
|
|
1363
1465
|
}
|
|
@@ -1788,6 +1890,7 @@ var OpenfleetPlugin = async (ctx) => {
|
|
|
1788
1890
|
logger.info("Plugin loaded");
|
|
1789
1891
|
const saveConversation = createSaveConversationTool(ctx);
|
|
1790
1892
|
const transcriptHooks = createTranscriptHooks(ctx);
|
|
1893
|
+
const { event: transcriptEvent, ...otherTranscriptHooks } = transcriptHooks;
|
|
1791
1894
|
return {
|
|
1792
1895
|
tool: {
|
|
1793
1896
|
save_conversation: saveConversation
|
|
@@ -1796,16 +1899,17 @@ var OpenfleetPlugin = async (ctx) => {
|
|
|
1796
1899
|
configureAgents(config);
|
|
1797
1900
|
},
|
|
1798
1901
|
event: async ({ event }) => {
|
|
1799
|
-
if (event.type
|
|
1800
|
-
|
|
1801
|
-
|
|
1802
|
-
|
|
1803
|
-
|
|
1804
|
-
|
|
1805
|
-
|
|
1806
|
-
}
|
|
1902
|
+
if (event.type === "session.created") {
|
|
1903
|
+
const props = event.properties;
|
|
1904
|
+
if (!props?.info?.parentID) {
|
|
1905
|
+
setTimeout(async () => {
|
|
1906
|
+
await showFleetToast(ctx);
|
|
1907
|
+
}, 0);
|
|
1908
|
+
}
|
|
1909
|
+
}
|
|
1910
|
+
await transcriptEvent({ event });
|
|
1807
1911
|
},
|
|
1808
|
-
...
|
|
1912
|
+
...otherTranscriptHooks
|
|
1809
1913
|
};
|
|
1810
1914
|
};
|
|
1811
1915
|
async function showFleetToast(ctx) {
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { PluginInput } from "@opencode-ai/plugin";
|
|
2
|
+
/** Returns true if the message contains a known Anthropic credit balance error pattern. */
|
|
3
|
+
export declare function isCreditBalanceError(message: string): boolean;
|
|
4
|
+
/** Returns true if the given session has previously fallen back to the free model. */
|
|
5
|
+
export declare function isSessionInFallback(sessionID: string): boolean;
|
|
6
|
+
/** Returns the fallback model split into providerID and modelID. */
|
|
7
|
+
export declare function getFallbackModelOverride(): {
|
|
8
|
+
providerID: string;
|
|
9
|
+
modelID: string;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Handles automatic fallback to free model when Anthropic credit balance is low.
|
|
13
|
+
*
|
|
14
|
+
* This function:
|
|
15
|
+
* 1. Guards against re-entrant or rapid repeated fallbacks via in-progress set and cooldown
|
|
16
|
+
* 2. Aborts the current retry loop for the session
|
|
17
|
+
* 3. Finds and reverts to before the last user message
|
|
18
|
+
* 4. Re-sends that message using the fallback model
|
|
19
|
+
* 5. Shows a warning toast to the user
|
|
20
|
+
*/
|
|
21
|
+
export declare function handleCreditBalanceFallback(client: PluginInput["client"], sessionID: string): Promise<void>;
|
package/dist/models.d.ts
CHANGED
|
@@ -28,3 +28,4 @@ export declare const models: {
|
|
|
28
28
|
export declare const defaultModel: "anthropic/claude-sonnet-4-6";
|
|
29
29
|
export declare const bigModel: "anthropic/claude-sonnet-4-6";
|
|
30
30
|
export declare const smallModel: "anthropic/claude-sonnet-4-6";
|
|
31
|
+
export declare const fallbackModel: "opencode/minimax-m2.5-free";
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PluginInput } from "@opencode-ai/plugin";
|
|
2
|
+
import type { Event } from "@opencode-ai/sdk";
|
|
2
3
|
export declare function createTranscriptHooks(ctx: PluginInput): {
|
|
3
4
|
"chat.message": (input: {
|
|
4
5
|
sessionID: string;
|
|
@@ -7,6 +8,19 @@ export declare function createTranscriptHooks(ctx: PluginInput): {
|
|
|
7
8
|
message: unknown;
|
|
8
9
|
parts: unknown[];
|
|
9
10
|
}) => Promise<void>;
|
|
11
|
+
"chat.model": (input: {
|
|
12
|
+
sessionID: string;
|
|
13
|
+
agent: string;
|
|
14
|
+
model: {
|
|
15
|
+
providerID: string;
|
|
16
|
+
modelID: string;
|
|
17
|
+
};
|
|
18
|
+
}, output: {
|
|
19
|
+
model: {
|
|
20
|
+
providerID: string;
|
|
21
|
+
modelID: string;
|
|
22
|
+
};
|
|
23
|
+
}) => Promise<void>;
|
|
10
24
|
"tool.execute.before": (input: {
|
|
11
25
|
sessionID: string;
|
|
12
26
|
tool: string;
|
|
@@ -26,4 +40,7 @@ export declare function createTranscriptHooks(ctx: PluginInput): {
|
|
|
26
40
|
"experimental.chat.system.transform": (input: {}, output: {
|
|
27
41
|
system: string[];
|
|
28
42
|
}) => Promise<void>;
|
|
43
|
+
event: ({ event }: {
|
|
44
|
+
event: Event;
|
|
45
|
+
}) => Promise<void>;
|
|
29
46
|
};
|