@code-yeongyu/senpi 2026.7.14-3 → 2026.7.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/CHANGELOG.md +0 -38
- package/dist/core/agent-session.js +1 -1
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/compaction/branch-summarization.d.ts +1 -1
- package/dist/core/compaction/branch-summarization.d.ts.map +1 -1
- package/dist/core/compaction/branch-summarization.js.map +1 -1
- package/dist/core/extensions/builtin/prompt-preset/gpt-5.6.d.ts.map +1 -1
- package/dist/core/extensions/builtin/prompt-preset/gpt-5.6.js +5 -14
- package/dist/core/extensions/builtin/prompt-preset/gpt-5.6.js.map +1 -1
- package/dist/core/extensions/builtin/terminal/runtime-session.d.ts +1 -1
- package/dist/core/extensions/builtin/terminal/runtime-session.d.ts.map +1 -1
- package/dist/core/extensions/builtin/terminal/runtime-session.js +2 -3
- package/dist/core/extensions/builtin/terminal/runtime-session.js.map +1 -1
- package/dist/core/model-registry.d.ts.map +1 -1
- package/dist/core/model-registry.js +3 -18
- package/dist/core/model-registry.js.map +1 -1
- package/dist/core/model-resolver.d.ts.map +1 -1
- package/dist/core/model-resolver.js +0 -1
- package/dist/core/model-resolver.js.map +1 -1
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +2 -7
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/provider-display-names.d.ts.map +1 -1
- package/dist/core/provider-display-names.js +0 -1
- package/dist/core/provider-display-names.js.map +1 -1
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +11 -1
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +2 -2
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/docs/custom-provider.md +0 -2
- package/docs/models.md +0 -2
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/gondolin/package-lock.json +2 -2
- package/examples/extensions/gondolin/package.json +1 -1
- package/examples/extensions/sandbox/package-lock.json +2 -2
- package/examples/extensions/sandbox/package.json +1 -1
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/node_modules/@code-yeongyu/senpi-codemode/CHANGELOG.md +0 -16
- package/node_modules/@code-yeongyu/senpi-codemode/package.json +3 -3
- package/node_modules/@earendil-works/pi-agent-core/package.json +2 -2
- package/node_modules/@earendil-works/pi-ai/README.md +2 -3
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js +42 -23
- package/node_modules/@earendil-works/pi-ai/dist/api/anthropic-messages.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.js +6 -10
- package/node_modules/@earendil-works/pi-ai/dist/api/bedrock-converse-stream.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.d.ts +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.js +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-codex-responses.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js +3 -12
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-completions.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js +0 -23
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses-shared.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses.d.ts +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses.js +6 -22
- package/node_modules/@earendil-works/pi-ai/dist/api/openai-responses.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/compat.d.ts +0 -2
- package/node_modules/@earendil-works/pi-ai/dist/compat.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/compat.js +0 -3
- package/node_modules/@earendil-works/pi-ai/dist/compat.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.js +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/env-api-keys.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/index.d.ts +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/models.generated.d.ts +6 -58
- package/node_modules/@earendil-works/pi-ai/dist/models.generated.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/all.d.ts +5 -9
- package/node_modules/@earendil-works/pi-ai/dist/providers/all.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/all.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/github-copilot.models.d.ts +6 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/github-copilot.models.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/github-copilot.models.js +2 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/github-copilot.models.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/opencode.models.d.ts +0 -57
- package/node_modules/@earendil-works/pi-ai/dist/providers/opencode.models.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/opencode.models.js +0 -19
- package/node_modules/@earendil-works/pi-ai/dist/providers/opencode.models.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/openrouter.models.js +10 -10
- package/node_modules/@earendil-works/pi-ai/dist/providers/openrouter.models.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/vercel-ai-gateway.models.js +2 -2
- package/node_modules/@earendil-works/pi-ai/dist/providers/vercel-ai-gateway.models.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/context-transformer.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/context-transformer.js +0 -9
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/context-transformer.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.d.ts +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.js +2 -6
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/types.d.ts +1 -3
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/types.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/types.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/types.d.ts +5 -10
- package/node_modules/@earendil-works/pi-ai/dist/types.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/types.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/index.d.ts +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/index.js +0 -9
- package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/package.json +1 -1
- package/node_modules/@earendil-works/pi-pty/native/prebuilds/darwin-arm64/senpi_pty.darwin-arm64.node +0 -0
- package/node_modules/@earendil-works/pi-pty/package.json +1 -1
- package/node_modules/@earendil-works/pi-tui/package.json +1 -1
- package/package.json +6 -6
- package/dist/core/radius.d.ts +0 -21
- package/dist/core/radius.d.ts.map +0 -1
- package/dist/core/radius.js +0 -30
- package/dist/core/radius.js.map +0 -1
- package/examples/AGENTS.md +0 -34
- package/node_modules/@earendil-works/pi-ai/dist/api/pi-messages.d.ts +0 -97
- package/node_modules/@earendil-works/pi-ai/dist/api/pi-messages.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/pi-messages.js +0 -307
- package/node_modules/@earendil-works/pi-ai/dist/api/pi-messages.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/pi-messages.lazy.d.ts +0 -3
- package/node_modules/@earendil-works/pi-ai/dist/api/pi-messages.lazy.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/api/pi-messages.lazy.js +0 -3
- package/node_modules/@earendil-works/pi-ai/dist/api/pi-messages.lazy.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/coerce-parameters.d.ts +0 -8
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/coerce-parameters.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/coerce-parameters.js +0 -92
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/coerce-parameters.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/format.d.ts +0 -6
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/format.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/format.js +0 -54
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/format.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/index.d.ts +0 -4
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/index.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/index.js +0 -4
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/index.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-match.d.ts +0 -7
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-match.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-match.js +0 -16
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-match.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.d.ts +0 -19
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.js +0 -217
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/invoke-tag-scanner.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/parse.d.ts +0 -4
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/parse.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/parse.js +0 -46
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/parse.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.d.ts +0 -13
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.js +0 -144
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream-boundary.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.d.ts +0 -5
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.js +0 -208
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/stream.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/tool-resolver.d.ts +0 -4
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/tool-resolver.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/tool-resolver.js +0 -17
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/tool-resolver.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/xml-entities.d.ts +0 -5
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/xml-entities.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/xml-entities.js +0 -34
- package/node_modules/@earendil-works/pi-ai/dist/tool-call-middleware/protocols/anthropic-xml/xml-entities.js.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/radius.d.ts +0 -39
- package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/radius.d.ts.map +0 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/radius.js +0 -413
- package/node_modules/@earendil-works/pi-ai/dist/utils/oauth/radius.js.map +0 -1
|
Binary file
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@earendil-works/pi-tui",
|
|
3
3
|
"private": true,
|
|
4
|
-
"version": "2026.7.14
|
|
4
|
+
"version": "2026.7.14",
|
|
5
5
|
"description": "Terminal User Interface library with differential rendering for efficient text-based applications",
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "dist/index.js",
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@code-yeongyu/senpi",
|
|
3
|
-
"version": "2026.7.14
|
|
3
|
+
"version": "2026.7.14",
|
|
4
4
|
"description": "Coding agent CLI with read, bash, edit, write tools and session management",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"piConfig": {
|
|
@@ -43,11 +43,11 @@
|
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@anthropic-ai/sdk": "0.91.1",
|
|
45
45
|
"@aws-sdk/client-bedrock-runtime": "3.1048.0",
|
|
46
|
-
"@code-yeongyu/senpi-codemode": "^2026.7.14
|
|
47
|
-
"@earendil-works/pi-agent-core": "^2026.7.14
|
|
48
|
-
"@earendil-works/pi-ai": "^2026.7.14
|
|
49
|
-
"@earendil-works/pi-pty": "^2026.7.14
|
|
50
|
-
"@earendil-works/pi-tui": "^2026.7.14
|
|
46
|
+
"@code-yeongyu/senpi-codemode": "^2026.7.14",
|
|
47
|
+
"@earendil-works/pi-agent-core": "^2026.7.14",
|
|
48
|
+
"@earendil-works/pi-ai": "^2026.7.14",
|
|
49
|
+
"@earendil-works/pi-pty": "^2026.7.14",
|
|
50
|
+
"@earendil-works/pi-tui": "^2026.7.14",
|
|
51
51
|
"@mistralai/mistralai": "2.2.6",
|
|
52
52
|
"@modelcontextprotocol/sdk": "1.29.0",
|
|
53
53
|
"@mozilla/readability": "0.6.0",
|
package/dist/core/radius.d.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Radius (pi-messages gateway) provider wiring.
|
|
3
|
-
*
|
|
4
|
-
* The main Radius provider is a built-in OAuth provider in pi-ai; models are
|
|
5
|
-
* dynamic, cached on the stored OAuth credential (`gatewayConfig`) and
|
|
6
|
-
* injected via the OAuth provider's `modifyModels` hook, so startup, /reload,
|
|
7
|
-
* and registry refreshes work without network access. The catalog refreshes
|
|
8
|
-
* on login and on every token refresh.
|
|
9
|
-
*
|
|
10
|
-
* Additional gateways (e.g. a local dev gateway) can be declared in
|
|
11
|
-
* models.json with `"oauth": "radius"`; each entry is an independent Radius
|
|
12
|
-
* instance with its own credentials and catalog.
|
|
13
|
-
*/
|
|
14
|
-
export declare const RADIUS_PROVIDER_ID = "radius";
|
|
15
|
-
/**
|
|
16
|
-
* Register a Radius-style OAuth provider for a custom gateway declared in
|
|
17
|
-
* models.json (`"oauth": "radius"`). Runs on every models.json load so the
|
|
18
|
-
* registration survives `resetOAuthProviders()` during registry refreshes.
|
|
19
|
-
*/
|
|
20
|
-
export declare function registerCustomRadiusOAuthProvider(id: string, name: string | undefined, gateway: string): void;
|
|
21
|
-
//# sourceMappingURL=radius.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"radius.d.ts","sourceRoot":"","sources":["../../src/core/radius.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAIH,eAAO,MAAM,kBAAkB,WAAW,CAAC;AAE3C;;;;GAIG;AACH,wBAAgB,iCAAiC,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI,CAU7G"}
|
package/dist/core/radius.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Radius (pi-messages gateway) provider wiring.
|
|
3
|
-
*
|
|
4
|
-
* The main Radius provider is a built-in OAuth provider in pi-ai; models are
|
|
5
|
-
* dynamic, cached on the stored OAuth credential (`gatewayConfig`) and
|
|
6
|
-
* injected via the OAuth provider's `modifyModels` hook, so startup, /reload,
|
|
7
|
-
* and registry refreshes work without network access. The catalog refreshes
|
|
8
|
-
* on login and on every token refresh.
|
|
9
|
-
*
|
|
10
|
-
* Additional gateways (e.g. a local dev gateway) can be declared in
|
|
11
|
-
* models.json with `"oauth": "radius"`; each entry is an independent Radius
|
|
12
|
-
* instance with its own credentials and catalog.
|
|
13
|
-
*/
|
|
14
|
-
import { createRadiusOAuthProvider, registerOAuthProvider } from "@earendil-works/pi-ai/oauth";
|
|
15
|
-
export const RADIUS_PROVIDER_ID = "radius";
|
|
16
|
-
/**
|
|
17
|
-
* Register a Radius-style OAuth provider for a custom gateway declared in
|
|
18
|
-
* models.json (`"oauth": "radius"`). Runs on every models.json load so the
|
|
19
|
-
* registration survives `resetOAuthProviders()` during registry refreshes.
|
|
20
|
-
*/
|
|
21
|
-
export function registerCustomRadiusOAuthProvider(id, name, gateway) {
|
|
22
|
-
registerOAuthProvider(createRadiusOAuthProvider({
|
|
23
|
-
id,
|
|
24
|
-
name: name ?? id,
|
|
25
|
-
// Tolerate an API base URL: the gateway root is what the OAuth and
|
|
26
|
-
// config discovery endpoints hang off.
|
|
27
|
-
gateway: gateway.replace(/\/v1\/?$/u, ""),
|
|
28
|
-
}));
|
|
29
|
-
}
|
|
30
|
-
//# sourceMappingURL=radius.js.map
|
package/dist/core/radius.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"radius.js","sourceRoot":"","sources":["../../src/core/radius.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,yBAAyB,EAAE,qBAAqB,EAAE,MAAM,6BAA6B,CAAC;AAE/F,MAAM,CAAC,MAAM,kBAAkB,GAAG,QAAQ,CAAC;AAE3C;;;;GAIG;AACH,MAAM,UAAU,iCAAiC,CAAC,EAAU,EAAE,IAAwB,EAAE,OAAe;IACtG,qBAAqB,CACpB,yBAAyB,CAAC;QACzB,EAAE;QACF,IAAI,EAAE,IAAI,IAAI,EAAE;QAChB,mEAAmE;QACnE,uCAAuC;QACvC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,CAAC;KACzC,CAAC,CACF,CAAC;AACH,CAAC","sourcesContent":["/**\n * Radius (pi-messages gateway) provider wiring.\n *\n * The main Radius provider is a built-in OAuth provider in pi-ai; models are\n * dynamic, cached on the stored OAuth credential (`gatewayConfig`) and\n * injected via the OAuth provider's `modifyModels` hook, so startup, /reload,\n * and registry refreshes work without network access. The catalog refreshes\n * on login and on every token refresh.\n *\n * Additional gateways (e.g. a local dev gateway) can be declared in\n * models.json with `\"oauth\": \"radius\"`; each entry is an independent Radius\n * instance with its own credentials and catalog.\n */\n\nimport { createRadiusOAuthProvider, registerOAuthProvider } from \"@earendil-works/pi-ai/oauth\";\n\nexport const RADIUS_PROVIDER_ID = \"radius\";\n\n/**\n * Register a Radius-style OAuth provider for a custom gateway declared in\n * models.json (`\"oauth\": \"radius\"`). Runs on every models.json load so the\n * registration survives `resetOAuthProviders()` during registry refreshes.\n */\nexport function registerCustomRadiusOAuthProvider(id: string, name: string | undefined, gateway: string): void {\n\tregisterOAuthProvider(\n\t\tcreateRadiusOAuthProvider({\n\t\t\tid,\n\t\t\tname: name ?? id,\n\t\t\t// Tolerate an API base URL: the gateway root is what the OAuth and\n\t\t\t// config discovery endpoints hang off.\n\t\t\tgateway: gateway.replace(/\\/v1\\/?$/u, \"\"),\n\t\t}),\n\t);\n}\n"]}
|
package/examples/AGENTS.md
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
# packages/coding-agent/examples
|
|
2
|
-
|
|
3
|
-
Runnable examples for the public Senpi SDK and extension API. Examples are documentation-quality code and must reflect shipped APIs, not private core internals. Unqualified paths below are relative to this directory; `packages/...` paths are repository-relative.
|
|
4
|
-
|
|
5
|
-
## STRUCTURE
|
|
6
|
-
|
|
7
|
-
```text
|
|
8
|
-
extensions/ Tools, commands, UI, providers, hooks, resources
|
|
9
|
-
extensions/*/ Multi-file examples and nested private workspaces
|
|
10
|
-
sdk/ Programmatic SDK usage
|
|
11
|
-
rpc-extension-ui.ts RPC-compatible extension UI example
|
|
12
|
-
```
|
|
13
|
-
|
|
14
|
-
## CONVENTIONS
|
|
15
|
-
|
|
16
|
-
- Import public package surfaces such as `@code-yeongyu/senpi` and `@earendil-works/pi-ai`; do not reach into `packages/coding-agent/src/core/` internals.
|
|
17
|
-
- Keep examples small enough to teach one pattern, while preserving real error, cleanup, cancellation, and persistence behavior where relevant.
|
|
18
|
-
- Extension factories have no top-level runtime side effects. Register work through the public `pi.*` API and lifecycle events.
|
|
19
|
-
- New interactive examples should use configurable keybindings and themed TUI helpers. Existing demos may keep fixed controls when the control scheme is part of the example. Direct terminal writes belong only in examples explicitly teaching a terminal protocol; ordinary SDK examples may use normal stdout.
|
|
20
|
-
- Tool string enums use the shared `StringEnum` helper for provider compatibility.
|
|
21
|
-
- Stateful examples persist reconstructable state in session entries or tool-result details so fork/resume behavior remains valid.
|
|
22
|
-
- Nested example packages are private workspaces with exact-pinned dependencies. Treat their manifests and lock impact as production dependency changes.
|
|
23
|
-
|
|
24
|
-
## DOCUMENTATION CONTRACT
|
|
25
|
-
|
|
26
|
-
- Keep `extensions/README.md`, `packages/coding-agent/docs/extensions.md`, and `packages/coding-agent/docs/sdk.md` aligned with public API changes.
|
|
27
|
-
- New public extension capabilities should include a focused example when usage is not obvious from types alone.
|
|
28
|
-
- Do not present experimental or internal behavior as stable API.
|
|
29
|
-
|
|
30
|
-
## VALIDATION
|
|
31
|
-
|
|
32
|
-
- Run the focused tests for the public API demonstrated by the example.
|
|
33
|
-
- Typecheck examples through root `npm run check`.
|
|
34
|
-
- Interactive examples require real CLI or visual QA when their behavior changes.
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* pi-messages API implementation.
|
|
3
|
-
*
|
|
4
|
-
* Streams pi's own message protocol directly to a backend: the request is a
|
|
5
|
-
* single POST of `{ model, context, options }` to `<baseUrl>/messages`, the
|
|
6
|
-
* response is an SSE stream of serialized assistant-message events plus a
|
|
7
|
-
* terminal `done`/`error` event. This is the wire protocol spoken by the
|
|
8
|
-
* Radius gateway, but any backend implementing it can be used, e.g. via a
|
|
9
|
-
* models.json custom provider with `"api": "pi-messages"`.
|
|
10
|
-
*/
|
|
11
|
-
import type { AssistantMessage, SimpleStreamOptions, StreamFunction, StreamOptions, ThinkingLevel, ToolCall } from "../types.ts";
|
|
12
|
-
export interface PiMessagesOptions extends StreamOptions {
|
|
13
|
-
reasoning?: ThinkingLevel;
|
|
14
|
-
toolChoice?: "auto" | "none" | "required" | {
|
|
15
|
-
type: "function";
|
|
16
|
-
function: {
|
|
17
|
-
name: string;
|
|
18
|
-
};
|
|
19
|
-
};
|
|
20
|
-
/** Ask the backend for debug metadata (e.g. routing response headers). */
|
|
21
|
-
debug?: boolean;
|
|
22
|
-
}
|
|
23
|
-
type PiMessagesUsage = AssistantMessage["usage"];
|
|
24
|
-
type PiMessagesStopReason = AssistantMessage["stopReason"];
|
|
25
|
-
/** Impact summary of a server-side message rewrite (e.g. a gateway policy). */
|
|
26
|
-
export type PiMessagesRewriteImpact = {
|
|
27
|
-
policyId: string;
|
|
28
|
-
policyVersion: number;
|
|
29
|
-
changed: boolean;
|
|
30
|
-
tokenCountChange: number;
|
|
31
|
-
messageCountChange: number;
|
|
32
|
-
systemPromptChanged: boolean;
|
|
33
|
-
};
|
|
34
|
-
/** Serialized assistant-message event as sent by a pi-messages backend. */
|
|
35
|
-
export type PiMessagesEvent = {
|
|
36
|
-
type: "start";
|
|
37
|
-
} | {
|
|
38
|
-
type: "text_start";
|
|
39
|
-
contentIndex: number;
|
|
40
|
-
} | {
|
|
41
|
-
type: "text_delta";
|
|
42
|
-
contentIndex: number;
|
|
43
|
-
delta: string;
|
|
44
|
-
} | {
|
|
45
|
-
type: "text_end";
|
|
46
|
-
contentIndex: number;
|
|
47
|
-
content: string;
|
|
48
|
-
contentSignature?: string;
|
|
49
|
-
} | {
|
|
50
|
-
type: "thinking_start";
|
|
51
|
-
contentIndex: number;
|
|
52
|
-
} | {
|
|
53
|
-
type: "thinking_delta";
|
|
54
|
-
contentIndex: number;
|
|
55
|
-
delta: string;
|
|
56
|
-
} | {
|
|
57
|
-
type: "thinking_end";
|
|
58
|
-
contentIndex: number;
|
|
59
|
-
content: string;
|
|
60
|
-
contentSignature?: string;
|
|
61
|
-
redacted?: boolean;
|
|
62
|
-
} | {
|
|
63
|
-
type: "toolcall_start";
|
|
64
|
-
contentIndex: number;
|
|
65
|
-
id: string;
|
|
66
|
-
toolName: string;
|
|
67
|
-
} | {
|
|
68
|
-
type: "toolcall_delta";
|
|
69
|
-
contentIndex: number;
|
|
70
|
-
delta: string;
|
|
71
|
-
} | {
|
|
72
|
-
type: "toolcall_end";
|
|
73
|
-
contentIndex: number;
|
|
74
|
-
toolCall: ToolCall;
|
|
75
|
-
} | {
|
|
76
|
-
type: "done";
|
|
77
|
-
reason: Extract<PiMessagesStopReason, "stop" | "length" | "toolUse">;
|
|
78
|
-
usage: PiMessagesUsage;
|
|
79
|
-
responseId?: string;
|
|
80
|
-
rewrite?: PiMessagesRewriteImpact;
|
|
81
|
-
} | {
|
|
82
|
-
type: "error";
|
|
83
|
-
reason: Extract<PiMessagesStopReason, "aborted" | "error">;
|
|
84
|
-
usage: PiMessagesUsage;
|
|
85
|
-
errorMessage?: string;
|
|
86
|
-
responseId?: string;
|
|
87
|
-
rewrite?: PiMessagesRewriteImpact;
|
|
88
|
-
};
|
|
89
|
-
export declare class PiMessagesResponseError extends Error {
|
|
90
|
-
code?: string;
|
|
91
|
-
readonly diagnosticDetails: Record<string, unknown>;
|
|
92
|
-
constructor(message: string, code: string | undefined, diagnosticDetails: Record<string, unknown>);
|
|
93
|
-
}
|
|
94
|
-
export declare const stream: StreamFunction<"pi-messages", PiMessagesOptions>;
|
|
95
|
-
export declare const streamSimple: StreamFunction<"pi-messages", SimpleStreamOptions>;
|
|
96
|
-
export {};
|
|
97
|
-
//# sourceMappingURL=pi-messages.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pi-messages.d.ts","sourceRoot":"","sources":["../../src/api/pi-messages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,KAAK,EACX,gBAAgB,EAMhB,mBAAmB,EACnB,cAAc,EACd,aAAa,EACb,aAAa,EACb,QAAQ,EACR,MAAM,aAAa,CAAC;AAOrB,MAAM,WAAW,iBAAkB,SAAQ,aAAa;IACvD,SAAS,CAAC,EAAE,aAAa,CAAC;IAC1B,UAAU,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,UAAU,GAAG;QAAE,IAAI,EAAE,UAAU,CAAC;QAAC,QAAQ,EAAE;YAAE,IAAI,EAAE,MAAM,CAAA;SAAE,CAAA;KAAE,CAAC;IAC7F,0EAA0E;IAC1E,KAAK,CAAC,EAAE,OAAO,CAAC;CAChB;AAED,KAAK,eAAe,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;AACjD,KAAK,oBAAoB,GAAG,gBAAgB,CAAC,YAAY,CAAC,CAAC;AAE3D,+EAA+E;AAC/E,MAAM,MAAM,uBAAuB,GAAG;IACrC,QAAQ,EAAE,MAAM,CAAC;IACjB,aAAa,EAAE,MAAM,CAAC;IACtB,OAAO,EAAE,OAAO,CAAC;IACjB,gBAAgB,EAAE,MAAM,CAAC;IACzB,kBAAkB,EAAE,MAAM,CAAC;IAC3B,mBAAmB,EAAE,OAAO,CAAC;CAC7B,CAAC;AAEF,2EAA2E;AAC3E,MAAM,MAAM,eAAe,GACxB;IAAE,IAAI,EAAE,OAAO,CAAA;CAAE,GACjB;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,YAAY,EAAE,MAAM,CAAA;CAAE,GAC5C;IAAE,IAAI,EAAE,YAAY,CAAC;IAAC,YAAY,EAA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AAC;IAC3D,KAAK,EAAE,eAAe,CAAC;IACvB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,uBAAuB,CAAC;CACjC,CAAC;AAWL,qBAAa,uBAAwB,SAAQ,KAAK;IACjD,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,QAAQ,CAAC,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAEpD,YAAY,OAAO,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,SAAS,EAAE,iBAAiB,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAKhG;CACD;AAoPD,eAAO,MAAM,MAAM,EAAE,cAAc,CAAC,aAAa,EAAE,iBAAiB,CA0EnE,CAAC;AAEF,eAAO,MAAM,YAAY,EAAE,cAAc,CAAC,aAAa,EAAE,mBAAmB,CAY3E,CAAC"}
|
|
@@ -1,307 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* pi-messages API implementation.
|
|
3
|
-
*
|
|
4
|
-
* Streams pi's own message protocol directly to a backend: the request is a
|
|
5
|
-
* single POST of `{ model, context, options }` to `<baseUrl>/messages`, the
|
|
6
|
-
* response is an SSE stream of serialized assistant-message events plus a
|
|
7
|
-
* terminal `done`/`error` event. This is the wire protocol spoken by the
|
|
8
|
-
* Radius gateway, but any backend implementing it can be used, e.g. via a
|
|
9
|
-
* models.json custom provider with `"api": "pi-messages"`.
|
|
10
|
-
*/
|
|
11
|
-
import { appendAssistantMessageDiagnostic, createAssistantMessageDiagnostic } from "../utils/diagnostics.js";
|
|
12
|
-
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
13
|
-
import { headersToRecord, providerHeadersToRecord } from "../utils/headers.js";
|
|
14
|
-
import { parseStreamingJson } from "../utils/json-parse.js";
|
|
15
|
-
import { getProviderEnvValue } from "../utils/provider-env.js";
|
|
16
|
-
export class PiMessagesResponseError extends Error {
|
|
17
|
-
constructor(message, code, diagnosticDetails) {
|
|
18
|
-
super(message);
|
|
19
|
-
this.name = "PiMessagesResponseError";
|
|
20
|
-
this.code = code;
|
|
21
|
-
this.diagnosticDetails = diagnosticDetails;
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
function isRecord(value) {
|
|
25
|
-
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
26
|
-
}
|
|
27
|
-
function parsePiMessagesErrorBody(body) {
|
|
28
|
-
try {
|
|
29
|
-
const parsed = JSON.parse(body);
|
|
30
|
-
return isRecord(parsed) && isRecord(parsed.error) ? parsed : undefined;
|
|
31
|
-
}
|
|
32
|
-
catch {
|
|
33
|
-
return undefined;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
function truncateDiagnosticString(value) {
|
|
37
|
-
const maxLength = 8192;
|
|
38
|
-
return value.length > maxLength ? `${value.slice(0, maxLength)}…` : value;
|
|
39
|
-
}
|
|
40
|
-
function formatPiMessagesResponseError(response, body, errorBody) {
|
|
41
|
-
const message = typeof errorBody?.error?.message === "string" ? errorBody.error.message : undefined;
|
|
42
|
-
const code = typeof errorBody?.error?.code === "string" ? errorBody.error.code : undefined;
|
|
43
|
-
const suffix = message ?? body;
|
|
44
|
-
const codeSuffix = code ? ` (${code})` : "";
|
|
45
|
-
return `${response.status} ${response.statusText}: ${suffix}${codeSuffix}`;
|
|
46
|
-
}
|
|
47
|
-
function createPiMessagesResponseError(model, url, response, body) {
|
|
48
|
-
const errorBody = parsePiMessagesErrorBody(body);
|
|
49
|
-
const code = typeof errorBody?.error?.code === "string" ? errorBody.error.code : undefined;
|
|
50
|
-
return new PiMessagesResponseError(formatPiMessagesResponseError(response, body, errorBody), code, {
|
|
51
|
-
version: 1,
|
|
52
|
-
provider: model.provider,
|
|
53
|
-
model: model.id,
|
|
54
|
-
url: url.toString(),
|
|
55
|
-
status: response.status,
|
|
56
|
-
statusText: response.statusText,
|
|
57
|
-
error: errorBody?.error,
|
|
58
|
-
body: errorBody ? undefined : truncateDiagnosticString(body),
|
|
59
|
-
timestampMs: Date.now(),
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
function createEmptyUsage() {
|
|
63
|
-
return {
|
|
64
|
-
input: 0,
|
|
65
|
-
output: 0,
|
|
66
|
-
cacheRead: 0,
|
|
67
|
-
cacheWrite: 0,
|
|
68
|
-
totalTokens: 0,
|
|
69
|
-
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
function appendRewriteDiagnostic(message, rewrite) {
|
|
73
|
-
if (!rewrite) {
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
appendAssistantMessageDiagnostic(message, {
|
|
77
|
-
type: "pi_messages_rewrite",
|
|
78
|
-
timestamp: Date.now(),
|
|
79
|
-
details: { ...rewrite },
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
function createEventConverter(model) {
|
|
83
|
-
const partial = {
|
|
84
|
-
role: "assistant",
|
|
85
|
-
content: [],
|
|
86
|
-
api: model.api,
|
|
87
|
-
provider: model.provider,
|
|
88
|
-
model: model.id,
|
|
89
|
-
usage: createEmptyUsage(),
|
|
90
|
-
stopReason: "stop",
|
|
91
|
-
timestamp: Date.now(),
|
|
92
|
-
};
|
|
93
|
-
const toolJson = new Map();
|
|
94
|
-
return (event) => {
|
|
95
|
-
switch (event.type) {
|
|
96
|
-
case "done":
|
|
97
|
-
Object.assign(partial, {
|
|
98
|
-
stopReason: event.reason,
|
|
99
|
-
usage: event.usage,
|
|
100
|
-
responseId: event.responseId,
|
|
101
|
-
});
|
|
102
|
-
appendRewriteDiagnostic(partial, event.rewrite);
|
|
103
|
-
return { type: "done", reason: event.reason, message: partial };
|
|
104
|
-
case "error":
|
|
105
|
-
Object.assign(partial, {
|
|
106
|
-
stopReason: event.reason,
|
|
107
|
-
usage: event.usage,
|
|
108
|
-
errorMessage: event.errorMessage,
|
|
109
|
-
responseId: event.responseId,
|
|
110
|
-
});
|
|
111
|
-
appendRewriteDiagnostic(partial, event.rewrite);
|
|
112
|
-
return { type: "error", reason: event.reason, error: partial };
|
|
113
|
-
case "start":
|
|
114
|
-
break;
|
|
115
|
-
case "text_start":
|
|
116
|
-
partial.content[event.contentIndex] = { type: "text", text: "" };
|
|
117
|
-
break;
|
|
118
|
-
case "text_delta":
|
|
119
|
-
partial.content[event.contentIndex].text += event.delta;
|
|
120
|
-
break;
|
|
121
|
-
case "text_end":
|
|
122
|
-
Object.assign(partial.content[event.contentIndex], {
|
|
123
|
-
text: event.content,
|
|
124
|
-
textSignature: event.contentSignature,
|
|
125
|
-
});
|
|
126
|
-
break;
|
|
127
|
-
case "thinking_start":
|
|
128
|
-
partial.content[event.contentIndex] = { type: "thinking", thinking: "" };
|
|
129
|
-
break;
|
|
130
|
-
case "thinking_delta":
|
|
131
|
-
partial.content[event.contentIndex].thinking += event.delta;
|
|
132
|
-
break;
|
|
133
|
-
case "thinking_end":
|
|
134
|
-
Object.assign(partial.content[event.contentIndex], {
|
|
135
|
-
thinking: event.content,
|
|
136
|
-
thinkingSignature: event.contentSignature,
|
|
137
|
-
redacted: event.redacted,
|
|
138
|
-
});
|
|
139
|
-
break;
|
|
140
|
-
case "toolcall_start":
|
|
141
|
-
partial.content[event.contentIndex] = {
|
|
142
|
-
type: "toolCall",
|
|
143
|
-
id: event.id,
|
|
144
|
-
name: event.toolName,
|
|
145
|
-
arguments: {},
|
|
146
|
-
};
|
|
147
|
-
toolJson.set(event.contentIndex, "");
|
|
148
|
-
break;
|
|
149
|
-
case "toolcall_delta": {
|
|
150
|
-
const json = `${toolJson.get(event.contentIndex) ?? ""}${event.delta}`;
|
|
151
|
-
toolJson.set(event.contentIndex, json);
|
|
152
|
-
partial.content[event.contentIndex].arguments =
|
|
153
|
-
parseStreamingJson(json);
|
|
154
|
-
break;
|
|
155
|
-
}
|
|
156
|
-
case "toolcall_end":
|
|
157
|
-
Object.assign(partial.content[event.contentIndex], event.toolCall);
|
|
158
|
-
toolJson.delete(event.contentIndex);
|
|
159
|
-
return {
|
|
160
|
-
type: "toolcall_end",
|
|
161
|
-
contentIndex: event.contentIndex,
|
|
162
|
-
toolCall: partial.content[event.contentIndex],
|
|
163
|
-
partial,
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
return { ...event, partial };
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
async function* readPiMessagesEvents(stream) {
|
|
170
|
-
const decoder = new TextDecoder();
|
|
171
|
-
const reader = stream.getReader();
|
|
172
|
-
let buffer = "";
|
|
173
|
-
try {
|
|
174
|
-
while (true) {
|
|
175
|
-
const { done, value } = await reader.read();
|
|
176
|
-
buffer += done ? decoder.decode() : decoder.decode(value, { stream: true });
|
|
177
|
-
buffer = buffer.replace(/\r\n/g, "\n");
|
|
178
|
-
let split = buffer.indexOf("\n\n");
|
|
179
|
-
while (split !== -1) {
|
|
180
|
-
const event = parsePiMessagesEvent(buffer.slice(0, split));
|
|
181
|
-
if (event) {
|
|
182
|
-
yield event;
|
|
183
|
-
}
|
|
184
|
-
buffer = buffer.slice(split + 2);
|
|
185
|
-
split = buffer.indexOf("\n\n");
|
|
186
|
-
}
|
|
187
|
-
if (done) {
|
|
188
|
-
break;
|
|
189
|
-
}
|
|
190
|
-
}
|
|
191
|
-
if (buffer.trim()) {
|
|
192
|
-
const event = parsePiMessagesEvent(buffer);
|
|
193
|
-
if (event) {
|
|
194
|
-
yield event;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
finally {
|
|
199
|
-
reader.releaseLock();
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
function parsePiMessagesEvent(raw) {
|
|
203
|
-
const data = raw
|
|
204
|
-
.split("\n")
|
|
205
|
-
.find((line) => line.startsWith("data:"))
|
|
206
|
-
?.slice(5)
|
|
207
|
-
.trim();
|
|
208
|
-
return data && data !== "[DONE]" ? JSON.parse(data) : undefined;
|
|
209
|
-
}
|
|
210
|
-
function createErrorEvent(model, error, aborted) {
|
|
211
|
-
const reason = aborted ? "aborted" : "error";
|
|
212
|
-
const assistantMessage = {
|
|
213
|
-
role: "assistant",
|
|
214
|
-
content: [],
|
|
215
|
-
api: model.api,
|
|
216
|
-
provider: model.provider,
|
|
217
|
-
model: model.id,
|
|
218
|
-
usage: createEmptyUsage(),
|
|
219
|
-
stopReason: reason,
|
|
220
|
-
errorMessage: error instanceof Error ? error.message : String(error),
|
|
221
|
-
timestamp: Date.now(),
|
|
222
|
-
};
|
|
223
|
-
if (!aborted && error instanceof PiMessagesResponseError) {
|
|
224
|
-
appendAssistantMessageDiagnostic(assistantMessage, createAssistantMessageDiagnostic("pi_messages_response_failure", error, error.diagnosticDetails));
|
|
225
|
-
}
|
|
226
|
-
return { type: "error", reason, error: assistantMessage };
|
|
227
|
-
}
|
|
228
|
-
function resolveCacheRetention(cacheRetention, env) {
|
|
229
|
-
if (cacheRetention) {
|
|
230
|
-
return cacheRetention;
|
|
231
|
-
}
|
|
232
|
-
// Backend defaults apply when unset; only the legacy env opt-in is mapped.
|
|
233
|
-
return getProviderEnvValue("PI_CACHE_RETENTION", env) === "long" ? "long" : undefined;
|
|
234
|
-
}
|
|
235
|
-
export const stream = (model, context, options) => {
|
|
236
|
-
const eventStream = new AssistantMessageEventStream();
|
|
237
|
-
const convertEvent = createEventConverter(model);
|
|
238
|
-
void (async () => {
|
|
239
|
-
try {
|
|
240
|
-
const apiKey = options?.apiKey;
|
|
241
|
-
if (!apiKey) {
|
|
242
|
-
throw new Error(`No API key provided for provider "${model.provider}"`);
|
|
243
|
-
}
|
|
244
|
-
const url = new URL(`${model.baseUrl.replace(/\/+$/u, "")}/messages`);
|
|
245
|
-
if (options?.debug) {
|
|
246
|
-
url.searchParams.set("debug", "1");
|
|
247
|
-
}
|
|
248
|
-
let payload = {
|
|
249
|
-
model: model.id,
|
|
250
|
-
context,
|
|
251
|
-
options: {
|
|
252
|
-
temperature: options?.temperature,
|
|
253
|
-
maxTokens: options?.maxTokens,
|
|
254
|
-
reasoning: options?.reasoning,
|
|
255
|
-
cacheRetention: resolveCacheRetention(options?.cacheRetention, options?.env),
|
|
256
|
-
sessionId: options?.sessionId,
|
|
257
|
-
toolChoice: options?.toolChoice,
|
|
258
|
-
},
|
|
259
|
-
};
|
|
260
|
-
const nextPayload = await options?.onPayload?.(payload, model);
|
|
261
|
-
if (nextPayload !== undefined) {
|
|
262
|
-
payload = nextPayload;
|
|
263
|
-
}
|
|
264
|
-
const response = await fetch(url, {
|
|
265
|
-
method: "POST",
|
|
266
|
-
headers: {
|
|
267
|
-
authorization: `Bearer ${apiKey}`,
|
|
268
|
-
accept: "text/event-stream",
|
|
269
|
-
"content-type": "application/json",
|
|
270
|
-
...providerHeadersToRecord(options?.headers),
|
|
271
|
-
},
|
|
272
|
-
body: JSON.stringify(payload),
|
|
273
|
-
signal: options?.signal,
|
|
274
|
-
});
|
|
275
|
-
await options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);
|
|
276
|
-
if (!response.ok) {
|
|
277
|
-
const body = await response.text();
|
|
278
|
-
throw createPiMessagesResponseError(model, url, response, body);
|
|
279
|
-
}
|
|
280
|
-
if (!response.body) {
|
|
281
|
-
throw new Error(`${model.provider} response has no body`);
|
|
282
|
-
}
|
|
283
|
-
for await (const piEvent of readPiMessagesEvents(response.body)) {
|
|
284
|
-
const event = convertEvent(piEvent);
|
|
285
|
-
eventStream.push(event);
|
|
286
|
-
if (event.type === "done" || event.type === "error") {
|
|
287
|
-
return;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
throw new Error(`${model.provider} stream ended without a terminal event`);
|
|
291
|
-
}
|
|
292
|
-
catch (error) {
|
|
293
|
-
eventStream.push(createErrorEvent(model, error, options?.signal?.aborted ?? false));
|
|
294
|
-
}
|
|
295
|
-
})();
|
|
296
|
-
return eventStream;
|
|
297
|
-
};
|
|
298
|
-
export const streamSimple = (model, context, options) => {
|
|
299
|
-
const extra = options;
|
|
300
|
-
return stream(model, context, {
|
|
301
|
-
...options,
|
|
302
|
-
reasoning: options?.reasoning,
|
|
303
|
-
toolChoice: extra?.toolChoice,
|
|
304
|
-
debug: extra?.debug,
|
|
305
|
-
});
|
|
306
|
-
};
|
|
307
|
-
//# sourceMappingURL=pi-messages.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pi-messages.js","sourceRoot":"","sources":["../../src/api/pi-messages.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAeH,OAAO,EAAE,gCAAgC,EAAE,gCAAgC,EAAE,MAAM,yBAAyB,CAAC;AAC7G,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,eAAe,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAC/E,OAAO,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAC5D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAiE/D,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAIjD,YAAY,OAAe,EAAE,IAAwB,EAAE,iBAA0C;QAChG,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,yBAAyB,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,iBAAiB,GAAG,iBAAiB,CAAC;IAC5C,CAAC;CACD;AAED,SAAS,QAAQ,CAAC,KAAc;IAC/B,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC7E,CAAC;AAED,SAAS,wBAAwB,CAAC,IAAY;IAC7C,IAAI,CAAC;QACJ,MAAM,MAAM,GAAG,IAAI,CAAC,KAAK,CAAC,IAAI,CAAY,CAAC;QAC3C,OAAO,QAAQ,CAAC,MAAM,CAAC,IAAI,QAAQ,CAAC,M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,wCAAwC,CAAC,CAAC;QAC5E,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,WAAW,CAAC,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,IAAI,KAAK,CAAC,CAAC,CAAC;QACrF,CAAC;IACF,CAAC,CAAC,EAAE,CAAC;IAEL,OAAO,WAAW,CAAC;AACpB,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,YAAY,GAAuD,CAC/E,KAA2B,EAC3B,OAAgB,EAChB,OAA6B,EACC,EAAE;IAChC,MAAM,KAAK,GAAG,OAAwC,CAAC;IACvD,OAAO,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE;QAC7B,GAAG,OAAO;QACV,SAAS,EAAE,OAAO,EAAE,SAAS;QAC7B,UAAU,EAAE,KAAK,EAAE,UAAU;QAC7B,KAAK,EAAE,KAAK,EAAE,KAAK;KACnB,CAAC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["/**\n * pi-messages API implementation.\n *\n * Streams pi's own message protocol directly to a backend: the request is a\n * single POST of `{ model, context, options }` to `<baseUrl>/messages`, the\n * response is an SSE stream of serialized assistant-message events plus a\n * terminal `done`/`error` event. This is the wire protocol spoken by the\n * Radius gateway, but any backend implementing it can be used, e.g. via a\n * models.json custom provider with `\"api\": \"pi-messages\"`.\n */\n\nimport type {\n\tAssistantMessage,\n\tAssistantMessageEvent,\n\tCacheRetention,\n\tContext,\n\tModel,\n\tProviderEnv,\n\tSimpleStreamOptions,\n\tStreamFunction,\n\tStreamOptions,\n\tThinkingLevel,\n\tToolCall,\n} from \"../types.ts\";\nimport { appendAssistantMessageDiagnostic, createAssistantMessageDiagnostic } from \"../utils/diagnostics.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { headersToRecord, providerHeadersToRecord } from \"../utils/headers.ts\";\nimport { parseStreamingJson } from \"../utils/json-parse.ts\";\nimport { getProviderEnvValue } from \"../utils/provider-env.ts\";\n\nexport interface PiMessagesOptions extends StreamOptions {\n\treasoning?: ThinkingLevel;\n\ttoolChoice?: \"auto\" | \"none\" | \"required\" | { type: \"function\"; function: { name: string } };\n\t/** Ask the backend for debug metadata (e.g. routing response headers). */\n\tdebug?: boolean;\n}\n\ntype PiMessagesUsage = AssistantMessage[\"usage\"];\ntype PiMessagesStopReason = AssistantMessage[\"stopReason\"];\n\n/** Impact summary of a server-side message rewrite (e.g. a gateway policy). */\nexport type PiMessagesRewriteImpact = {\n\tpolicyId: string;\n\tpolicyVersion: number;\n\tchanged: boolean;\n\ttokenCountChange: number;\n\tmessageCountChange: number;\n\tsystemPromptChanged: boolean;\n};\n\n/** Serialized assistant-message event as sent by a pi-messages backend. */\nexport type PiMessagesEvent =\n\t| { type: \"start\" }\n\t| { type: \"text_start\"; contentIndex: number }\n\t| { type: \"text_delta\"; contentIndex: number; delta: string }\n\t| { type: \"text_end\"; contentIndex: number; content: string; contentSignature?: string }\n\t| { type: \"thinking_start\"; contentIndex: number }\n\t| { type: \"thinking_delta\"; contentIndex: number; delta: string }\n\t| {\n\t\t\ttype: \"thinking_end\";\n\t\t\tcontentIndex: number;\n\t\t\tcontent: string;\n\t\t\tcontentSignature?: string;\n\t\t\tredacted?: boolean;\n\t }\n\t| { type: \"toolcall_start\"; contentIndex: number; id: string; toolName: string }\n\t| { type: \"toolcall_delta\"; contentIndex: number; delta: string }\n\t| { type: \"toolcall_end\"; contentIndex: number; toolCall: ToolCall }\n\t| {\n\t\t\ttype: \"done\";\n\t\t\treason: Extract<PiMessagesStopReason, \"stop\" | \"length\" | \"toolUse\">;\n\t\t\tusage: PiMessagesUsage;\n\t\t\tresponseId?: string;\n\t\t\trewrite?: PiMessagesRewriteImpact;\n\t }\n\t| {\n\t\t\ttype: \"error\";\n\t\t\treason: Extract<PiMessagesStopReason, \"aborted\" | \"error\">;\n\t\t\tusage: PiMessagesUsage;\n\t\t\terrorMessage?: string;\n\t\t\tresponseId?: string;\n\t\t\trewrite?: PiMessagesRewriteImpact;\n\t };\n\ntype PiMessagesErrorBody = {\n\terror?: {\n\t\tmessage?: unknown;\n\t\tcode?: unknown;\n\t\tdetails?: unknown;\n\t\t[key: string]: unknown;\n\t};\n};\n\nexport class PiMessagesResponseError extends Error {\n\tcode?: string;\n\treadonly diagnosticDetails: Record<string, unknown>;\n\n\tconstructor(message: string, code: string | undefined, diagnosticDetails: Record<string, unknown>) {\n\t\tsuper(message);\n\t\tthis.name = \"PiMessagesResponseError\";\n\t\tthis.code = code;\n\t\tthis.diagnosticDetails = diagnosticDetails;\n\t}\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n\treturn typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\nfunction parsePiMessagesErrorBody(body: string): PiMessagesErrorBody | undefined {\n\ttry {\n\t\tconst parsed = JSON.parse(body) as unknown;\n\t\treturn isRecord(parsed) && isRecord(parsed.error) ? (parsed as PiMessagesErrorBody) : undefined;\n\t} catch {\n\t\treturn undefined;\n\t}\n}\n\nfunction truncateDiagnosticString(value: string): string {\n\tconst maxLength = 8192;\n\treturn value.length > maxLength ? `${value.slice(0, maxLength)}…` : value;\n}\n\nfunction formatPiMessagesResponseError(\n\tresponse: Response,\n\tbody: string,\n\terrorBody: PiMessagesErrorBody | undefined,\n): string {\n\tconst message = typeof errorBody?.error?.message === \"string\" ? errorBody.error.message : undefined;\n\tconst code = typeof errorBody?.error?.code === \"string\" ? errorBody.error.code : undefined;\n\tconst suffix = message ?? body;\n\tconst codeSuffix = code ? ` (${code})` : \"\";\n\treturn `${response.status} ${response.statusText}: ${suffix}${codeSuffix}`;\n}\n\nfunction createPiMessagesResponseError(\n\tmodel: Model<\"pi-messages\">,\n\turl: URL,\n\tresponse: Response,\n\tbody: string,\n): PiMessagesResponseError {\n\tconst errorBody = parsePiMessagesErrorBody(body);\n\tconst code = typeof errorBody?.error?.code === \"string\" ? errorBody.error.code : undefined;\n\treturn new PiMessagesResponseError(formatPiMessagesResponseError(response, body, errorBody), code, {\n\t\tversion: 1,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\turl: url.toString(),\n\t\tstatus: response.status,\n\t\tstatusText: response.statusText,\n\t\terror: errorBody?.error,\n\t\tbody: errorBody ? undefined : truncateDiagnosticString(body),\n\t\ttimestampMs: Date.now(),\n\t});\n}\n\nfunction createEmptyUsage(): PiMessagesUsage {\n\treturn {\n\t\tinput: 0,\n\t\toutput: 0,\n\t\tcacheRead: 0,\n\t\tcacheWrite: 0,\n\t\ttotalTokens: 0,\n\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t};\n}\n\nfunction appendRewriteDiagnostic(message: AssistantMessage, rewrite: PiMessagesRewriteImpact | undefined): void {\n\tif (!rewrite) {\n\t\treturn;\n\t}\n\tappendAssistantMessageDiagnostic(message, {\n\t\ttype: \"pi_messages_rewrite\",\n\t\ttimestamp: Date.now(),\n\t\tdetails: { ...rewrite },\n\t});\n}\n\nfunction createEventConverter(model: Model<\"pi-messages\">) {\n\tconst partial: AssistantMessage = {\n\t\trole: \"assistant\",\n\t\tcontent: [],\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\tusage: createEmptyUsage(),\n\t\tstopReason: \"stop\",\n\t\ttimestamp: Date.now(),\n\t};\n\tconst toolJson = new Map<number, string>();\n\n\treturn (event: PiMessagesEvent): AssistantMessageEvent => {\n\t\tswitch (event.type) {\n\t\t\tcase \"done\":\n\t\t\t\tObject.assign(partial, {\n\t\t\t\t\tstopReason: event.reason,\n\t\t\t\t\tusage: event.usage,\n\t\t\t\t\tresponseId: event.responseId,\n\t\t\t\t});\n\t\t\t\tappendRewriteDiagnostic(partial, event.rewrite);\n\t\t\t\treturn { type: \"done\", reason: event.reason, message: partial };\n\t\t\tcase \"error\":\n\t\t\t\tObject.assign(partial, {\n\t\t\t\t\tstopReason: event.reason,\n\t\t\t\t\tusage: event.usage,\n\t\t\t\t\terrorMessage: event.errorMessage,\n\t\t\t\t\tresponseId: event.responseId,\n\t\t\t\t});\n\t\t\t\tappendRewriteDiagnostic(partial, event.rewrite);\n\t\t\t\treturn { type: \"error\", reason: event.reason, error: partial };\n\t\t\tcase \"start\":\n\t\t\t\tbreak;\n\t\t\tcase \"text_start\":\n\t\t\t\tpartial.content[event.contentIndex] = { type: \"text\", text: \"\" };\n\t\t\t\tbreak;\n\t\t\tcase \"text_delta\":\n\t\t\t\t(partial.content[event.contentIndex] as { text: string }).text += event.delta;\n\t\t\t\tbreak;\n\t\t\tcase \"text_end\":\n\t\t\t\tObject.assign(partial.content[event.contentIndex]!, {\n\t\t\t\t\ttext: event.content,\n\t\t\t\t\ttextSignature: event.contentSignature,\n\t\t\t\t});\n\t\t\t\tbreak;\n\t\t\tcase \"thinking_start\":\n\t\t\t\tpartial.content[event.contentIndex] = { type: \"thinking\", thinking: \"\" };\n\t\t\t\tbreak;\n\t\t\tcase \"thinking_delta\":\n\t\t\t\t(partial.content[event.contentIndex] as { thinking: string }).thinking += event.delta;\n\t\t\t\tbreak;\n\t\t\tcase \"thinking_end\":\n\t\t\t\tObject.assign(partial.content[event.contentIndex]!, {\n\t\t\t\t\tthinking: event.content,\n\t\t\t\t\tthinkingSignature: event.contentSignature,\n\t\t\t\t\tredacted: event.redacted,\n\t\t\t\t});\n\t\t\t\tbreak;\n\t\t\tcase \"toolcall_start\":\n\t\t\t\tpartial.content[event.contentIndex] = {\n\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\tid: event.id,\n\t\t\t\t\tname: event.toolName,\n\t\t\t\t\targuments: {},\n\t\t\t\t};\n\t\t\t\ttoolJson.set(event.contentIndex, \"\");\n\t\t\t\tbreak;\n\t\t\tcase \"toolcall_delta\": {\n\t\t\t\tconst json = `${toolJson.get(event.contentIndex) ?? \"\"}${event.delta}`;\n\t\t\t\ttoolJson.set(event.contentIndex, json);\n\t\t\t\t(partial.content[event.contentIndex] as ToolCall).arguments =\n\t\t\t\t\tparseStreamingJson<ToolCall[\"arguments\"]>(json);\n\t\t\t\tbreak;\n\t\t\t}\n\t\t\tcase \"toolcall_end\":\n\t\t\t\tObject.assign(partial.content[event.contentIndex]!, event.toolCall);\n\t\t\t\ttoolJson.delete(event.contentIndex);\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"toolcall_end\",\n\t\t\t\t\tcontentIndex: event.contentIndex,\n\t\t\t\t\ttoolCall: partial.content[event.contentIndex] as ToolCall,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t}\n\n\t\treturn { ...event, partial } as AssistantMessageEvent;\n\t};\n}\n\nasync function* readPiMessagesEvents(stream: ReadableStream<Uint8Array>): AsyncGenerator<PiMessagesEvent> {\n\tconst decoder = new TextDecoder();\n\tconst reader = stream.getReader();\n\tlet buffer = \"\";\n\n\ttry {\n\t\twhile (true) {\n\t\t\tconst { done, value } = await reader.read();\n\t\t\tbuffer += done ? decoder.decode() : decoder.decode(value, { stream: true });\n\t\t\tbuffer = buffer.replace(/\\r\\n/g, \"\\n\");\n\n\t\t\tlet split = buffer.indexOf(\"\\n\\n\");\n\t\t\twhile (split !== -1) {\n\t\t\t\tconst event = parsePiMessagesEvent(buffer.slice(0, split));\n\t\t\t\tif (event) {\n\t\t\t\t\tyield event;\n\t\t\t\t}\n\t\t\t\tbuffer = buffer.slice(split + 2);\n\t\t\t\tsplit = buffer.indexOf(\"\\n\\n\");\n\t\t\t}\n\n\t\t\tif (done) {\n\t\t\t\tbreak;\n\t\t\t}\n\t\t}\n\n\t\tif (buffer.trim()) {\n\t\t\tconst event = parsePiMessagesEvent(buffer);\n\t\t\tif (event) {\n\t\t\t\tyield event;\n\t\t\t}\n\t\t}\n\t} finally {\n\t\treader.releaseLock();\n\t}\n}\n\nfunction parsePiMessagesEvent(raw: string): PiMessagesEvent | undefined {\n\tconst data = raw\n\t\t.split(\"\\n\")\n\t\t.find((line) => line.startsWith(\"data:\"))\n\t\t?.slice(5)\n\t\t.trim();\n\n\treturn data && data !== \"[DONE]\" ? (JSON.parse(data) as PiMessagesEvent) : undefined;\n}\n\nfunction createErrorEvent(model: Model<\"pi-messages\">, error: unknown, aborted: boolean): AssistantMessageEvent {\n\tconst reason = aborted ? \"aborted\" : \"error\";\n\tconst assistantMessage: AssistantMessage = {\n\t\trole: \"assistant\",\n\t\tcontent: [],\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\tusage: createEmptyUsage(),\n\t\tstopReason: reason,\n\t\terrorMessage: error instanceof Error ? error.message : String(error),\n\t\ttimestamp: Date.now(),\n\t};\n\n\tif (!aborted && error instanceof PiMessagesResponseError) {\n\t\tappendAssistantMessageDiagnostic(\n\t\t\tassistantMessage,\n\t\t\tcreateAssistantMessageDiagnostic(\"pi_messages_response_failure\", error, error.diagnosticDetails),\n\t\t);\n\t}\n\n\treturn { type: \"error\", reason, error: assistantMessage };\n}\n\nfunction resolveCacheRetention(cacheRetention?: CacheRetention, env?: ProviderEnv): CacheRetention | undefined {\n\tif (cacheRetention) {\n\t\treturn cacheRetention;\n\t}\n\t// Backend defaults apply when unset; only the legacy env opt-in is mapped.\n\treturn getProviderEnvValue(\"PI_CACHE_RETENTION\", env) === \"long\" ? \"long\" : undefined;\n}\n\nexport const stream: StreamFunction<\"pi-messages\", PiMessagesOptions> = (\n\tmodel: Model<\"pi-messages\">,\n\tcontext: Context,\n\toptions?: PiMessagesOptions,\n): AssistantMessageEventStream => {\n\tconst eventStream = new AssistantMessageEventStream();\n\tconst convertEvent = createEventConverter(model);\n\n\tvoid (async () => {\n\t\ttry {\n\t\t\tconst apiKey = options?.apiKey;\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error(`No API key provided for provider \"${model.provider}\"`);\n\t\t\t}\n\n\t\t\tconst url = new URL(`${model.baseUrl.replace(/\\/+$/u, \"\")}/messages`);\n\t\t\tif (options?.debug) {\n\t\t\t\turl.searchParams.set(\"debug\", \"1\");\n\t\t\t}\n\n\t\t\tlet payload: unknown = {\n\t\t\t\tmodel: model.id,\n\t\t\t\tcontext,\n\t\t\t\toptions: {\n\t\t\t\t\ttemperature: options?.temperature,\n\t\t\t\t\tmaxTokens: options?.maxTokens,\n\t\t\t\t\treasoning: options?.reasoning,\n\t\t\t\t\tcacheRetention: resolveCacheRetention(options?.cacheRetention, options?.env),\n\t\t\t\t\tsessionId: options?.sessionId,\n\t\t\t\t\ttoolChoice: options?.toolChoice,\n\t\t\t\t},\n\t\t\t};\n\t\t\tconst nextPayload = await options?.onPayload?.(payload, model);\n\t\t\tif (nextPayload !== undefined) {\n\t\t\t\tpayload = nextPayload;\n\t\t\t}\n\n\t\t\tconst response = await fetch(url, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\tauthorization: `Bearer ${apiKey}`,\n\t\t\t\t\taccept: \"text/event-stream\",\n\t\t\t\t\t\"content-type\": \"application/json\",\n\t\t\t\t\t...providerHeadersToRecord(options?.headers),\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify(payload),\n\t\t\t\tsignal: options?.signal,\n\t\t\t});\n\n\t\t\tawait options?.onResponse?.({ status: response.status, headers: headersToRecord(response.headers) }, model);\n\n\t\t\tif (!response.ok) {\n\t\t\t\tconst body = await response.text();\n\t\t\t\tthrow createPiMessagesResponseError(model, url, response, body);\n\t\t\t}\n\t\t\tif (!response.body) {\n\t\t\t\tthrow new Error(`${model.provider} response has no body`);\n\t\t\t}\n\n\t\t\tfor await (const piEvent of readPiMessagesEvents(response.body)) {\n\t\t\t\tconst event = convertEvent(piEvent);\n\t\t\t\teventStream.push(event);\n\t\t\t\tif (event.type === \"done\" || event.type === \"error\") {\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tthrow new Error(`${model.provider} stream ended without a terminal event`);\n\t\t} catch (error) {\n\t\t\teventStream.push(createErrorEvent(model, error, options?.signal?.aborted ?? false));\n\t\t}\n\t})();\n\n\treturn eventStream;\n};\n\nexport const streamSimple: StreamFunction<\"pi-messages\", SimpleStreamOptions> = (\n\tmodel: Model<\"pi-messages\">,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tconst extra = options as PiMessagesOptions | undefined;\n\treturn stream(model, context, {\n\t\t...options,\n\t\treasoning: options?.reasoning,\n\t\ttoolChoice: extra?.toolChoice,\n\t\tdebug: extra?.debug,\n\t});\n};\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pi-messages.lazy.d.ts","sourceRoot":"","sources":["../../src/api/pi-messages.lazy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGnD,eAAO,MAAM,aAAa,QAAO,eAA4D,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pi-messages.lazy.js","sourceRoot":"","sources":["../../src/api/pi-messages.lazy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,MAAM,CAAC,MAAM,aAAa,GAAG,GAAoB,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,kBAAkB,CAAC,CAAC,CAAC","sourcesContent":["import type { ProviderStreams } from \"../types.ts\";\nimport { lazyApi } from \"./lazy.ts\";\n\nexport const piMessagesApi = (): ProviderStreams => lazyApi(() => import(\"./pi-messages.ts\"));\n"]}
|