pi-openmodel-provider 0.2.19 → 0.2.20
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/CHANGELOG.md +8 -0
- package/package.json +1 -1
- package/src/errors.ts +31 -21
package/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,14 @@ All notable changes to this project will be documented in this file.
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.2.20] - 2026-06-28
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
- `src/errors.ts` — replaced all `as any` casts with proper `isRecord()` type guard for type-safe body parsing
|
|
12
|
+
|
|
13
|
+
### Fixed
|
|
14
|
+
- Added missing `isRecord()` utility type guard to eliminate `any` usage in error parsing (adheres to project type safety policy)
|
|
15
|
+
|
|
8
16
|
## [0.2.19] - 2026-06-27
|
|
9
17
|
|
|
10
18
|
### Added
|
package/package.json
CHANGED
package/src/errors.ts
CHANGED
|
@@ -7,39 +7,49 @@
|
|
|
7
7
|
* Proxy endpoints return errors in provider-specific formats (Anthropic, OpenAI, Gemini).
|
|
8
8
|
*/
|
|
9
9
|
|
|
10
|
+
/** Check if a value is a non-null object (Record) */
|
|
11
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
12
|
+
return typeof value === "object" && value !== null
|
|
13
|
+
}
|
|
14
|
+
|
|
10
15
|
/** Parse an OpenModel Web API error response body */
|
|
11
16
|
export function parseWebError(body: unknown): { code: string; message: string; detail?: string } {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
code:
|
|
16
|
-
|
|
17
|
+
if (isRecord(body) && isRecord(body.error)) {
|
|
18
|
+
const err = body.error
|
|
19
|
+
if (typeof err.code === "string" && typeof err.msg === "string") {
|
|
20
|
+
const result: { code: string; message: string; detail?: string } = {
|
|
21
|
+
code: err.code,
|
|
22
|
+
message: err.msg,
|
|
23
|
+
}
|
|
24
|
+
if (typeof err.detail === "string") {
|
|
25
|
+
result.detail = err.detail
|
|
26
|
+
}
|
|
27
|
+
return result
|
|
17
28
|
}
|
|
18
|
-
if (err.detail) {
|
|
19
|
-
result.detail = String(err.detail)
|
|
20
|
-
}
|
|
21
|
-
return result
|
|
22
29
|
}
|
|
23
30
|
return { code: "UNKNOWN", message: "An unknown error occurred" }
|
|
24
31
|
}
|
|
25
32
|
|
|
26
33
|
/** Parse an OpenModel proxy API error body (any format) */
|
|
27
34
|
export function parseProxyError(body: unknown): { code: string; message: string } {
|
|
28
|
-
|
|
35
|
+
if (isRecord(body) && isRecord(body.error)) {
|
|
36
|
+
const err = body.error
|
|
29
37
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
38
|
+
// Anthropic format: { type: "error", error: { type, message } }
|
|
39
|
+
if (body.type === "error" && typeof err.message === "string") {
|
|
40
|
+
return { code: typeof err.type === "string" ? err.type : "UNKNOWN", message: err.message }
|
|
41
|
+
}
|
|
34
42
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
43
|
+
// OpenAI format: { error: { message, type, code } }
|
|
44
|
+
if (typeof err.message === "string") {
|
|
45
|
+
const code = typeof err.code === "string" ? err.code : typeof err.type === "string" ? err.type : "UNKNOWN"
|
|
46
|
+
return { code, message: err.message }
|
|
47
|
+
}
|
|
39
48
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
49
|
+
// Gemini format: { error: { code, message, status } }
|
|
50
|
+
if (typeof err.status === "string") {
|
|
51
|
+
return { code: err.status, message: typeof err.message === "string" ? err.message : "Unknown error" }
|
|
52
|
+
}
|
|
43
53
|
}
|
|
44
54
|
|
|
45
55
|
return { code: "UNKNOWN", message: "An unknown error occurred" }
|