copillm 0.2.3 → 0.2.4
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/README.md +1 -0
- package/dist/agentconfig/apply.js +4 -3
- package/dist/agentconfig/load.js +34 -1
- package/dist/agentconfig/schema.js +22 -0
- package/dist/agents/registry.js +80 -6
- package/dist/cli/auth/ensure.js +30 -0
- package/dist/cli/auth/runAuth.js +38 -0
- package/dist/cli/commands/agents/claude.js +47 -0
- package/dist/cli/commands/agents/codex.js +48 -0
- package/dist/cli/commands/agents/copilot.js +49 -0
- package/dist/cli/commands/agents/pi.js +47 -0
- package/dist/cli/commands/agents/shared.js +28 -0
- package/dist/cli/commands/auth.js +99 -0
- package/dist/cli/commands/daemon.js +357 -0
- package/dist/cli/commands/env.js +135 -0
- package/dist/cli/commands/models.js +80 -0
- package/dist/cli/configCommands.js +10 -0
- package/dist/cli/copillmFlags.js +101 -0
- package/dist/cli/daemon/ensureRunning.js +66 -0
- package/dist/cli/daemon/lifecycle.js +61 -0
- package/dist/cli/daemon/probes.js +68 -0
- package/dist/cli/daemon/runDaemon.js +102 -0
- package/dist/cli/daemon/spawnEnv.js +12 -0
- package/dist/cli/index.js +43 -0
- package/dist/cli/integrations/banner.js +51 -0
- package/dist/cli/integrations/claudeExport.js +14 -0
- package/dist/cli/integrations/refreshCodex.js +19 -0
- package/dist/cli/integrations/refreshPi.js +17 -0
- package/dist/cli/shared/backends.js +31 -0
- package/dist/cli/shared/debug.js +44 -0
- package/dist/cli/shared/deprecation.js +7 -0
- package/dist/cli/shared/exitCodes.js +9 -0
- package/dist/cli/shared/output.js +14 -0
- package/dist/cli/shared/parseAgent.js +6 -0
- package/dist/cli.js +1 -1355
- package/dist/server/errors.js +195 -0
- package/dist/server/proxy.js +50 -885
- package/dist/server/routes/debug.js +65 -0
- package/dist/server/routes/health.js +32 -0
- package/dist/server/routes/models.js +41 -0
- package/dist/server/routes/proxyForward.js +108 -0
- package/dist/server/routes/shared.js +161 -0
- package/dist/server/upstream/copilotClient.js +137 -0
- package/dist/server/upstream/streaming.js +146 -0
- package/package.json +7 -2
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { CopilotTokenExchangeError, CopilotTokenManagerError } from "../auth/copilotToken.js";
|
|
2
|
+
import { safeEnd, safeWrite } from "./requestLifecycle.js";
|
|
3
|
+
export class JsonRequestParseError extends Error {
|
|
4
|
+
constructor(message) {
|
|
5
|
+
super(message);
|
|
6
|
+
this.name = "JsonRequestParseError";
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
export class InvalidRequestShapeError extends Error {
|
|
10
|
+
constructor(message) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.name = "InvalidRequestShapeError";
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
export async function readUpstreamError(response) {
|
|
16
|
+
const contentType = response.headers.get("content-type");
|
|
17
|
+
let text;
|
|
18
|
+
try {
|
|
19
|
+
text = await response.text();
|
|
20
|
+
}
|
|
21
|
+
catch {
|
|
22
|
+
return { contentType, code: null, type: null, message: null, responseBytes: null };
|
|
23
|
+
}
|
|
24
|
+
const trimmed = text.trim();
|
|
25
|
+
const responseBytes = Buffer.byteLength(text, "utf8");
|
|
26
|
+
if (trimmed.length === 0) {
|
|
27
|
+
return { contentType, code: null, type: null, message: null, responseBytes };
|
|
28
|
+
}
|
|
29
|
+
try {
|
|
30
|
+
const parsed = JSON.parse(trimmed);
|
|
31
|
+
const extracted = extractErrorFields(parsed);
|
|
32
|
+
if (extracted.message || extracted.code || extracted.type) {
|
|
33
|
+
return { contentType, responseBytes, ...extracted };
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
// Fall through to a plain-text snippet below.
|
|
38
|
+
}
|
|
39
|
+
return {
|
|
40
|
+
contentType,
|
|
41
|
+
code: null,
|
|
42
|
+
type: null,
|
|
43
|
+
message: truncateForDiagnostics(trimmed),
|
|
44
|
+
responseBytes
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function extractErrorFields(payload) {
|
|
48
|
+
if (!payload || typeof payload !== "object") {
|
|
49
|
+
return { code: null, type: null, message: typeof payload === "string" ? truncateForDiagnostics(payload) : null };
|
|
50
|
+
}
|
|
51
|
+
const record = payload;
|
|
52
|
+
const nested = record.error;
|
|
53
|
+
if (typeof nested === "string") {
|
|
54
|
+
return {
|
|
55
|
+
code: readStringField(record, "code"),
|
|
56
|
+
type: readStringField(record, "type"),
|
|
57
|
+
message: truncateForDiagnostics(nested)
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
if (nested && typeof nested === "object") {
|
|
61
|
+
const errorRecord = nested;
|
|
62
|
+
return {
|
|
63
|
+
code: readStringField(errorRecord, "code") ?? readStringField(record, "code"),
|
|
64
|
+
type: readStringField(errorRecord, "type") ?? readStringField(record, "type"),
|
|
65
|
+
message: readTruncatedStringField(errorRecord, "message") ??
|
|
66
|
+
readTruncatedStringField(errorRecord, "detail") ??
|
|
67
|
+
readTruncatedStringField(record, "message") ??
|
|
68
|
+
readTruncatedStringField(record, "detail")
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
return {
|
|
72
|
+
code: readStringField(record, "code"),
|
|
73
|
+
type: readStringField(record, "type"),
|
|
74
|
+
message: readTruncatedStringField(record, "message") ?? readTruncatedStringField(record, "detail")
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export function buildUpstreamErrorPayload(category, statusCode, requestId, upstreamError, anthroShape) {
|
|
78
|
+
const code = upstreamError.code ?? category;
|
|
79
|
+
const type = upstreamError.type ?? category;
|
|
80
|
+
const message = formatUserFacingUpstreamErrorMessage(category, upstreamError);
|
|
81
|
+
if (anthroShape) {
|
|
82
|
+
return {
|
|
83
|
+
type: "error",
|
|
84
|
+
error: {
|
|
85
|
+
type,
|
|
86
|
+
message,
|
|
87
|
+
code,
|
|
88
|
+
upstream_status_code: statusCode,
|
|
89
|
+
request_id: requestId
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
error: {
|
|
95
|
+
type,
|
|
96
|
+
code,
|
|
97
|
+
message,
|
|
98
|
+
upstream_status_code: statusCode,
|
|
99
|
+
request_id: requestId
|
|
100
|
+
}
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
export function formatUpstreamErrorMessage(category, upstreamError) {
|
|
104
|
+
const parts = [upstreamError.code, upstreamError.type, upstreamError.message].filter((part) => typeof part === "string" && part.length > 0);
|
|
105
|
+
return parts.length > 0 ? `${category}: ${parts.join(": ")}` : category;
|
|
106
|
+
}
|
|
107
|
+
function formatUserFacingUpstreamErrorMessage(category, upstreamError) {
|
|
108
|
+
if (upstreamError.code && upstreamError.message) {
|
|
109
|
+
return `${upstreamError.code}: ${upstreamError.message}`;
|
|
110
|
+
}
|
|
111
|
+
if (upstreamError.message) {
|
|
112
|
+
return upstreamError.message;
|
|
113
|
+
}
|
|
114
|
+
return upstreamError.code ?? upstreamError.type ?? category;
|
|
115
|
+
}
|
|
116
|
+
function readStringField(record, key) {
|
|
117
|
+
const value = record[key];
|
|
118
|
+
return typeof value === "string" && value.length > 0 ? value : null;
|
|
119
|
+
}
|
|
120
|
+
function readTruncatedStringField(record, key) {
|
|
121
|
+
const value = readStringField(record, key);
|
|
122
|
+
return value ? truncateForDiagnostics(value) : null;
|
|
123
|
+
}
|
|
124
|
+
function truncateForDiagnostics(value) {
|
|
125
|
+
const maxChars = 500;
|
|
126
|
+
return value.length > maxChars ? `${value.slice(0, maxChars)}...` : value;
|
|
127
|
+
}
|
|
128
|
+
export function upstreamStatusCategory(status) {
|
|
129
|
+
if (status === 401 || status === 403) {
|
|
130
|
+
return "upstream_auth_error";
|
|
131
|
+
}
|
|
132
|
+
if (status === 429) {
|
|
133
|
+
return "upstream_rate_limited";
|
|
134
|
+
}
|
|
135
|
+
if (status >= 500) {
|
|
136
|
+
return "upstream_server_error";
|
|
137
|
+
}
|
|
138
|
+
if (status >= 400) {
|
|
139
|
+
return "upstream_request_error";
|
|
140
|
+
}
|
|
141
|
+
return "upstream_error";
|
|
142
|
+
}
|
|
143
|
+
export function healthFailure(error) {
|
|
144
|
+
if (error instanceof CopilotTokenExchangeError) {
|
|
145
|
+
if (error.statusCode === 401 || error.statusCode === 403) {
|
|
146
|
+
return {
|
|
147
|
+
httpStatus: 401,
|
|
148
|
+
payload: {
|
|
149
|
+
status: "unauthenticated",
|
|
150
|
+
error: "github_auth_invalid",
|
|
151
|
+
upstream_status_code: error.statusCode
|
|
152
|
+
}
|
|
153
|
+
};
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
httpStatus: 503,
|
|
157
|
+
payload: {
|
|
158
|
+
status: "upstream_unreachable",
|
|
159
|
+
error: "token_exchange_failed",
|
|
160
|
+
upstream_status_code: error.statusCode
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
if (error instanceof CopilotTokenManagerError) {
|
|
165
|
+
return {
|
|
166
|
+
httpStatus: 401,
|
|
167
|
+
payload: {
|
|
168
|
+
status: "unauthenticated",
|
|
169
|
+
error: "token_refresh_failed"
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
httpStatus: 503,
|
|
175
|
+
payload: {
|
|
176
|
+
status: "upstream_unreachable",
|
|
177
|
+
error: "token_refresh_unavailable"
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
export function writeAnthropicSseError(res, prelude, code) {
|
|
182
|
+
void prelude;
|
|
183
|
+
try {
|
|
184
|
+
safeWrite(res, `event: message_delta\ndata: ${JSON.stringify({
|
|
185
|
+
type: "message_delta",
|
|
186
|
+
delta: { stop_reason: "end_turn", stop_sequence: null },
|
|
187
|
+
usage: { input_tokens: 0, output_tokens: 0, cache_read_input_tokens: 0 }
|
|
188
|
+
})}\n\n`);
|
|
189
|
+
safeWrite(res, `event: error\ndata: ${JSON.stringify({ type: "error", error: { type: "api_error", message: code } })}\n\n`);
|
|
190
|
+
safeWrite(res, `event: message_stop\ndata: ${JSON.stringify({ type: "message_stop" })}\n\n`);
|
|
191
|
+
}
|
|
192
|
+
finally {
|
|
193
|
+
safeEnd(res);
|
|
194
|
+
}
|
|
195
|
+
}
|