evolcore 0.0.5 → 0.0.6
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 +43 -0
- package/README.md +4 -12
- package/dist/agents/baseagent.js +39 -0
- package/dist/agents/claude-runner.js +10 -2
- package/dist/agents/codex-runner.js +37 -8
- package/dist/agents/ecagent-runner.js +1159 -0
- package/dist/agents/gemini-runner.js +4 -1
- package/dist/agents/runner-types.js +20 -4
- package/dist/aun/msg/history.js +32 -9
- package/dist/aun/outbox.js +4 -9
- package/dist/channels/aun.js +32 -6
- package/dist/channels/daemon.js +7 -4
- package/dist/channels/dingtalk.js +3 -5
- package/dist/channels/feishu.js +16 -108
- package/dist/channels/wechat.js +4 -4
- package/dist/channels/wecom.js +4 -6
- package/dist/cli/agent-command.js +1 -1
- package/dist/cli/agent.js +42 -11
- package/dist/cli/aun-commands.js +50 -3
- package/dist/cli/daemon-commands.js +81 -6
- package/dist/cli/data-command.js +132 -0
- package/dist/cli/index.js +11 -3
- package/dist/cli/init.js +76 -17
- package/dist/cli/net-check.js +9 -20
- package/dist/cli/restart-monitor.js +11 -2
- package/dist/cli/trigger-command.js +1 -1
- package/dist/cli/watch-msg.js +12 -25
- package/dist/config/builtin-roles.js +3 -1
- package/dist/config/config-field-policy.js +1 -1
- package/dist/config/gateway-config.js +21 -4
- package/dist/config/resolved-config-op.js +10 -0
- package/dist/core/command/command-handler.js +2 -2
- package/dist/core/command/menu-handler.js +11 -9
- package/dist/core/command/role-menu.js +11 -0
- package/dist/core/command/slash-handler.js +18 -14
- package/dist/core/data-migration.js +1434 -0
- package/dist/core/handoff/store.js +14 -4
- package/dist/core/inference/text-inference.js +42 -1
- package/dist/core/message/pending-hints.js +1 -1
- package/dist/core/message/response-engine.js +406 -167
- package/dist/core/message/retry-scheduler.js +71 -0
- package/dist/core/model/model-catalog.js +7 -2
- package/dist/core/model/model-fallback.js +81 -0
- package/dist/core/protected-paths.js +41 -8
- package/dist/core/session/adapters/ecagent-session-file-adapter.js +112 -0
- package/dist/core/session/session-fs-store.js +283 -59
- package/dist/core/session/session-manager.js +19 -21
- package/dist/core/session/session-title.js +1 -0
- package/dist/core/session/session-turn-coordinator.js +8 -1
- package/dist/core/system-channels.js +1 -0
- package/dist/index.js +10 -9
- package/dist/paths.js +35 -27
- package/dist/trigger/feedback.js +16 -0
- package/dist/trigger/legacy-session-history.js +19 -0
- package/dist/trigger/manager.js +12 -1
- package/dist/trigger/parser.js +4 -2
- package/dist/trigger/scheduler.js +185 -15
- package/dist/trigger/session-lock.js +62 -0
- package/dist/trigger/state.js +64 -0
- package/dist/trigger/validation.js +32 -2
- package/dist/utils/aid-bind.js +1 -1
- package/dist/utils/error-utils.js +9 -0
- package/dist/utils/welcome.js +2 -3
- package/ecagent/LICENSE +21 -0
- package/ecagent/NOTICE +11 -0
- package/ecagent/README.md +61 -0
- package/ecagent/dist/agent-loop.d.ts +24 -0
- package/ecagent/dist/agent-loop.d.ts.map +1 -0
- package/ecagent/dist/agent-loop.js +547 -0
- package/ecagent/dist/agent-loop.js.map +1 -0
- package/ecagent/dist/agent.d.ts +127 -0
- package/ecagent/dist/agent.d.ts.map +1 -0
- package/ecagent/dist/agent.js +386 -0
- package/ecagent/dist/agent.js.map +1 -0
- package/ecagent/dist/harness/agent-harness.d.ts +95 -0
- package/ecagent/dist/harness/agent-harness.d.ts.map +1 -0
- package/ecagent/dist/harness/agent-harness.js +926 -0
- package/ecagent/dist/harness/agent-harness.js.map +1 -0
- package/ecagent/dist/harness/compaction/branch-summarization.d.ts +51 -0
- package/ecagent/dist/harness/compaction/branch-summarization.d.ts.map +1 -0
- package/ecagent/dist/harness/compaction/branch-summarization.js +174 -0
- package/ecagent/dist/harness/compaction/branch-summarization.js.map +1 -0
- package/ecagent/dist/harness/compaction/compaction.d.ts +95 -0
- package/ecagent/dist/harness/compaction/compaction.d.ts.map +1 -0
- package/ecagent/dist/harness/compaction/compaction.js +528 -0
- package/ecagent/dist/harness/compaction/compaction.js.map +1 -0
- package/ecagent/dist/harness/compaction/utils.d.ts +25 -0
- package/ecagent/dist/harness/compaction/utils.d.ts.map +1 -0
- package/ecagent/dist/harness/compaction/utils.js +131 -0
- package/ecagent/dist/harness/compaction/utils.js.map +1 -0
- package/ecagent/dist/harness/env/nodejs.d.ts +51 -0
- package/ecagent/dist/harness/env/nodejs.d.ts.map +1 -0
- package/ecagent/dist/harness/env/nodejs.js +513 -0
- package/ecagent/dist/harness/env/nodejs.js.map +1 -0
- package/ecagent/dist/harness/messages.d.ts +51 -0
- package/ecagent/dist/harness/messages.d.ts.map +1 -0
- package/ecagent/dist/harness/messages.js +102 -0
- package/ecagent/dist/harness/messages.js.map +1 -0
- package/ecagent/dist/harness/prompt-templates.d.ts +48 -0
- package/ecagent/dist/harness/prompt-templates.d.ts.map +1 -0
- package/ecagent/dist/harness/prompt-templates.js +230 -0
- package/ecagent/dist/harness/prompt-templates.js.map +1 -0
- package/ecagent/dist/harness/session/jsonl-repo.d.ts +26 -0
- package/ecagent/dist/harness/session/jsonl-repo.d.ts.map +1 -0
- package/ecagent/dist/harness/session/jsonl-repo.js +100 -0
- package/ecagent/dist/harness/session/jsonl-repo.js.map +1 -0
- package/ecagent/dist/harness/session/jsonl-storage.d.ts +34 -0
- package/ecagent/dist/harness/session/jsonl-storage.d.ts.map +1 -0
- package/ecagent/dist/harness/session/jsonl-storage.js +234 -0
- package/ecagent/dist/harness/session/jsonl-storage.js.map +1 -0
- package/ecagent/dist/harness/session/memory-repo.d.ts +18 -0
- package/ecagent/dist/harness/session/memory-repo.d.ts.map +1 -0
- package/ecagent/dist/harness/session/memory-repo.js +44 -0
- package/ecagent/dist/harness/session/memory-repo.js.map +1 -0
- package/ecagent/dist/harness/session/memory-storage.d.ts +25 -0
- package/ecagent/dist/harness/session/memory-storage.d.ts.map +1 -0
- package/ecagent/dist/harness/session/memory-storage.js +111 -0
- package/ecagent/dist/harness/session/memory-storage.js.map +1 -0
- package/ecagent/dist/harness/session/repo-utils.d.ts +11 -0
- package/ecagent/dist/harness/session/repo-utils.d.ts.map +1 -0
- package/ecagent/dist/harness/session/repo-utils.js +39 -0
- package/ecagent/dist/harness/session/repo-utils.js.map +1 -0
- package/ecagent/dist/harness/session/session.d.ts +47 -0
- package/ecagent/dist/harness/session/session.d.ts.map +1 -0
- package/ecagent/dist/harness/session/session.js +245 -0
- package/ecagent/dist/harness/session/session.js.map +1 -0
- package/ecagent/dist/harness/session/uuid.d.ts +2 -0
- package/ecagent/dist/harness/session/uuid.d.ts.map +1 -0
- package/ecagent/dist/harness/session/uuid.js +50 -0
- package/ecagent/dist/harness/session/uuid.js.map +1 -0
- package/ecagent/dist/harness/skills.d.ts +44 -0
- package/ecagent/dist/harness/skills.d.ts.map +1 -0
- package/ecagent/dist/harness/skills.js +311 -0
- package/ecagent/dist/harness/skills.js.map +1 -0
- package/ecagent/dist/harness/system-prompt.d.ts +3 -0
- package/ecagent/dist/harness/system-prompt.d.ts.map +1 -0
- package/ecagent/dist/harness/system-prompt.js +30 -0
- package/ecagent/dist/harness/system-prompt.js.map +1 -0
- package/ecagent/dist/harness/types.d.ts +619 -0
- package/ecagent/dist/harness/types.d.ts.map +1 -0
- package/ecagent/dist/harness/types.js +81 -0
- package/ecagent/dist/harness/types.js.map +1 -0
- package/ecagent/dist/harness/utils/shell-output.d.ts +14 -0
- package/ecagent/dist/harness/utils/shell-output.d.ts.map +1 -0
- package/ecagent/dist/harness/utils/shell-output.js +126 -0
- package/ecagent/dist/harness/utils/shell-output.js.map +1 -0
- package/ecagent/dist/harness/utils/truncate.d.ts +70 -0
- package/ecagent/dist/harness/utils/truncate.d.ts.map +1 -0
- package/ecagent/dist/harness/utils/truncate.js +290 -0
- package/ecagent/dist/harness/utils/truncate.js.map +1 -0
- package/ecagent/dist/index.d.ts +22 -0
- package/ecagent/dist/index.d.ts.map +1 -0
- package/ecagent/dist/index.js +27 -0
- package/ecagent/dist/index.js.map +1 -0
- package/ecagent/dist/node.d.ts +3 -0
- package/ecagent/dist/node.d.ts.map +1 -0
- package/ecagent/dist/node.js +3 -0
- package/ecagent/dist/node.js.map +1 -0
- package/ecagent/dist/proxy.d.ts +69 -0
- package/ecagent/dist/proxy.d.ts.map +1 -0
- package/ecagent/dist/proxy.js +278 -0
- package/ecagent/dist/proxy.js.map +1 -0
- package/ecagent/dist/runtime/api/lazy.d.ts +15 -0
- package/ecagent/dist/runtime/api/lazy.d.ts.map +1 -0
- package/ecagent/dist/runtime/api/lazy.js +59 -0
- package/ecagent/dist/runtime/api/lazy.js.map +1 -0
- package/ecagent/dist/runtime/api/pi-messages.d.ts +97 -0
- package/ecagent/dist/runtime/api/pi-messages.d.ts.map +1 -0
- package/ecagent/dist/runtime/api/pi-messages.js +307 -0
- package/ecagent/dist/runtime/api/pi-messages.js.map +1 -0
- package/ecagent/dist/runtime/api/pi-messages.lazy.d.ts +3 -0
- package/ecagent/dist/runtime/api/pi-messages.lazy.d.ts.map +1 -0
- package/ecagent/dist/runtime/api/pi-messages.lazy.js +3 -0
- package/ecagent/dist/runtime/api/pi-messages.lazy.js.map +1 -0
- package/ecagent/dist/runtime/auth/context.d.ts +7 -0
- package/ecagent/dist/runtime/auth/context.d.ts.map +1 -0
- package/ecagent/dist/runtime/auth/context.js +42 -0
- package/ecagent/dist/runtime/auth/context.js.map +1 -0
- package/ecagent/dist/runtime/auth/credential-store.d.ts +16 -0
- package/ecagent/dist/runtime/auth/credential-store.d.ts.map +1 -0
- package/ecagent/dist/runtime/auth/credential-store.js +39 -0
- package/ecagent/dist/runtime/auth/credential-store.js.map +1 -0
- package/ecagent/dist/runtime/auth/helpers.d.ts +20 -0
- package/ecagent/dist/runtime/auth/helpers.d.ts.map +1 -0
- package/ecagent/dist/runtime/auth/helpers.js +46 -0
- package/ecagent/dist/runtime/auth/helpers.js.map +1 -0
- package/ecagent/dist/runtime/auth/resolve.d.ts +26 -0
- package/ecagent/dist/runtime/auth/resolve.d.ts.map +1 -0
- package/ecagent/dist/runtime/auth/resolve.js +100 -0
- package/ecagent/dist/runtime/auth/resolve.js.map +1 -0
- package/ecagent/dist/runtime/auth/types.d.ts +180 -0
- package/ecagent/dist/runtime/auth/types.d.ts.map +1 -0
- package/ecagent/dist/runtime/auth/types.js +2 -0
- package/ecagent/dist/runtime/auth/types.js.map +1 -0
- package/ecagent/dist/runtime/compat.d.ts +28 -0
- package/ecagent/dist/runtime/compat.d.ts.map +1 -0
- package/ecagent/dist/runtime/compat.js +86 -0
- package/ecagent/dist/runtime/compat.js.map +1 -0
- package/ecagent/dist/runtime/index.d.ts +20 -0
- package/ecagent/dist/runtime/index.d.ts.map +1 -0
- package/ecagent/dist/runtime/index.js +17 -0
- package/ecagent/dist/runtime/index.js.map +1 -0
- package/ecagent/dist/runtime/models.d.ts +142 -0
- package/ecagent/dist/runtime/models.d.ts.map +1 -0
- package/ecagent/dist/runtime/models.js +245 -0
- package/ecagent/dist/runtime/models.js.map +1 -0
- package/ecagent/dist/runtime/oauth.d.ts +2 -0
- package/ecagent/dist/runtime/oauth.d.ts.map +1 -0
- package/ecagent/dist/runtime/oauth.js +2 -0
- package/ecagent/dist/runtime/oauth.js.map +1 -0
- package/ecagent/dist/runtime/providers/faux.d.ts +97 -0
- package/ecagent/dist/runtime/providers/faux.d.ts.map +1 -0
- package/ecagent/dist/runtime/providers/faux.js +395 -0
- package/ecagent/dist/runtime/providers/faux.js.map +1 -0
- package/ecagent/dist/runtime/types.d.ts +607 -0
- package/ecagent/dist/runtime/types.d.ts.map +1 -0
- package/ecagent/dist/runtime/types.js +2 -0
- package/ecagent/dist/runtime/types.js.map +1 -0
- package/ecagent/dist/runtime/utils/diagnostics.d.ts +19 -0
- package/ecagent/dist/runtime/utils/diagnostics.d.ts.map +1 -0
- package/ecagent/dist/runtime/utils/diagnostics.js +25 -0
- package/ecagent/dist/runtime/utils/diagnostics.js.map +1 -0
- package/ecagent/dist/runtime/utils/event-stream.d.ts +21 -0
- package/ecagent/dist/runtime/utils/event-stream.d.ts.map +1 -0
- package/ecagent/dist/runtime/utils/event-stream.js +77 -0
- package/ecagent/dist/runtime/utils/event-stream.js.map +1 -0
- package/ecagent/dist/runtime/utils/headers.d.ts +4 -0
- package/ecagent/dist/runtime/utils/headers.d.ts.map +1 -0
- package/ecagent/dist/runtime/utils/headers.js +18 -0
- package/ecagent/dist/runtime/utils/headers.js.map +1 -0
- package/ecagent/dist/runtime/utils/json-parse.d.ts +16 -0
- package/ecagent/dist/runtime/utils/json-parse.d.ts.map +1 -0
- package/ecagent/dist/runtime/utils/json-parse.js +113 -0
- package/ecagent/dist/runtime/utils/json-parse.js.map +1 -0
- package/ecagent/dist/runtime/utils/oauth/types.d.ts +64 -0
- package/ecagent/dist/runtime/utils/oauth/types.d.ts.map +1 -0
- package/ecagent/dist/runtime/utils/oauth/types.js +2 -0
- package/ecagent/dist/runtime/utils/oauth/types.js.map +1 -0
- package/ecagent/dist/runtime/utils/overflow.d.ts +58 -0
- package/ecagent/dist/runtime/utils/overflow.d.ts.map +1 -0
- package/ecagent/dist/runtime/utils/overflow.js +158 -0
- package/ecagent/dist/runtime/utils/overflow.js.map +1 -0
- package/ecagent/dist/runtime/utils/provider-env.d.ts +7 -0
- package/ecagent/dist/runtime/utils/provider-env.d.ts.map +1 -0
- package/ecagent/dist/runtime/utils/provider-env.js +44 -0
- package/ecagent/dist/runtime/utils/provider-env.js.map +1 -0
- package/ecagent/dist/runtime/utils/retry.d.ts +12 -0
- package/ecagent/dist/runtime/utils/retry.d.ts.map +1 -0
- package/ecagent/dist/runtime/utils/retry.js +90 -0
- package/ecagent/dist/runtime/utils/retry.js.map +1 -0
- package/ecagent/dist/runtime/utils/typebox-helpers.d.ts +17 -0
- package/ecagent/dist/runtime/utils/typebox-helpers.d.ts.map +1 -0
- package/ecagent/dist/runtime/utils/typebox-helpers.js +21 -0
- package/ecagent/dist/runtime/utils/typebox-helpers.js.map +1 -0
- package/ecagent/dist/runtime/utils/validation.d.ts +18 -0
- package/ecagent/dist/runtime/utils/validation.d.ts.map +1 -0
- package/ecagent/dist/runtime/utils/validation.js +269 -0
- package/ecagent/dist/runtime/utils/validation.js.map +1 -0
- package/ecagent/dist/types.d.ts +401 -0
- package/ecagent/dist/types.d.ts.map +1 -0
- package/ecagent/dist/types.js +2 -0
- package/ecagent/dist/types.js.map +1 -0
- package/ecagent/package.json +93 -0
- package/ecagent/src/agent-loop.ts +792 -0
- package/ecagent/src/agent.ts +575 -0
- package/ecagent/src/harness/agent-harness.ts +1029 -0
- package/ecagent/src/harness/compaction/branch-summarization.ts +261 -0
- package/ecagent/src/harness/compaction/compaction.ts +753 -0
- package/ecagent/src/harness/compaction/utils.ts +144 -0
- package/ecagent/src/harness/env/nodejs.ts +569 -0
- package/ecagent/src/harness/messages.ts +164 -0
- package/ecagent/src/harness/prompt-templates.ts +267 -0
- package/ecagent/src/harness/session/jsonl-repo.ts +179 -0
- package/ecagent/src/harness/session/jsonl-storage.ts +314 -0
- package/ecagent/src/harness/session/memory-repo.ts +50 -0
- package/ecagent/src/harness/session/memory-storage.ts +133 -0
- package/ecagent/src/harness/session/repo-utils.ts +51 -0
- package/ecagent/src/harness/session/session.ts +338 -0
- package/ecagent/src/harness/session/uuid.ts +54 -0
- package/ecagent/src/harness/skills.ts +375 -0
- package/ecagent/src/harness/system-prompt.ts +34 -0
- package/ecagent/src/harness/types.ts +838 -0
- package/ecagent/src/harness/utils/shell-output.ts +135 -0
- package/ecagent/src/harness/utils/truncate.ts +344 -0
- package/ecagent/src/index.ts +46 -0
- package/ecagent/src/node.ts +2 -0
- package/ecagent/src/proxy.ts +367 -0
- package/ecagent/src/runtime/api/lazy.ts +70 -0
- package/ecagent/src/runtime/api/pi-messages.lazy.ts +4 -0
- package/ecagent/src/runtime/api/pi-messages.ts +436 -0
- package/ecagent/src/runtime/auth/context.ts +45 -0
- package/ecagent/src/runtime/auth/credential-store.ts +47 -0
- package/ecagent/src/runtime/auth/helpers.ts +46 -0
- package/ecagent/src/runtime/auth/resolve.ts +141 -0
- package/ecagent/src/runtime/auth/types.ts +182 -0
- package/ecagent/src/runtime/compat.ts +158 -0
- package/ecagent/src/runtime/index.ts +31 -0
- package/ecagent/src/runtime/models.ts +452 -0
- package/ecagent/src/runtime/oauth.ts +1 -0
- package/ecagent/src/runtime/providers/faux.ts +538 -0
- package/ecagent/src/runtime/types.ts +718 -0
- package/ecagent/src/runtime/utils/diagnostics.ts +45 -0
- package/ecagent/src/runtime/utils/event-stream.ts +88 -0
- package/ecagent/src/runtime/utils/headers.ts +18 -0
- package/ecagent/src/runtime/utils/json-parse.ts +124 -0
- package/ecagent/src/runtime/utils/oauth/types.ts +79 -0
- package/ecagent/src/runtime/utils/overflow.ts +165 -0
- package/ecagent/src/runtime/utils/provider-env.ts +52 -0
- package/ecagent/src/runtime/utils/retry.ts +101 -0
- package/ecagent/src/runtime/utils/typebox-helpers.ts +24 -0
- package/ecagent/src/runtime/utils/validation.ts +310 -0
- package/ecagent/src/types.ts +430 -0
- package/ecagent/tsconfig.build.json +32 -0
- package/kits/schemas/_meta.json +7 -4
- package/kits/schemas/agent-config.schema.10.json +370 -0
- package/kits/schemas/defaults.schema.4.json +93 -0
- package/kits/schemas/relation-config.schema.8.json +69 -0
- package/kits/templates/roles/admin.json +1 -1
- package/package.json +22 -6
- package/skills/eclink/SKILL.md +70 -0
- package/skills/eclink/agents/openai.yaml +4 -0
- package/assets/.env.template +0 -4
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Proxy stream function for apps that route LLM calls through a server.
|
|
3
|
+
* The server manages auth and proxies requests to LLM providers.
|
|
4
|
+
*/
|
|
5
|
+
// Internal import for JSON parsing utility
|
|
6
|
+
import { EventStream, parseStreamingJson, } from "ecagent/runtime";
|
|
7
|
+
// Create stream class matching ProxyMessageEventStream
|
|
8
|
+
class ProxyMessageEventStream extends EventStream {
|
|
9
|
+
constructor() {
|
|
10
|
+
super((event) => event.type === "done" || event.type === "error", (event) => {
|
|
11
|
+
if (event.type === "done")
|
|
12
|
+
return event.message;
|
|
13
|
+
if (event.type === "error")
|
|
14
|
+
return event.error;
|
|
15
|
+
throw new Error("Unexpected event type");
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Stream function that proxies through a server instead of calling LLM providers directly.
|
|
21
|
+
* The server strips the partial field from delta events to reduce bandwidth.
|
|
22
|
+
* We reconstruct the partial message client-side.
|
|
23
|
+
*
|
|
24
|
+
* Use this as the `streamFn` option when creating an Agent that needs to go through a proxy.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```typescript
|
|
28
|
+
* const agent = new Agent({
|
|
29
|
+
* streamFn: (model, context, options) =>
|
|
30
|
+
* streamProxy(model, context, {
|
|
31
|
+
* ...options,
|
|
32
|
+
* authToken: await getAuthToken(),
|
|
33
|
+
* proxyUrl: "https://genai.example.com",
|
|
34
|
+
* }),
|
|
35
|
+
* });
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
function buildProxyRequestOptions(options) {
|
|
39
|
+
return {
|
|
40
|
+
temperature: options.temperature,
|
|
41
|
+
maxTokens: options.maxTokens,
|
|
42
|
+
reasoning: options.reasoning,
|
|
43
|
+
cacheRetention: options.cacheRetention,
|
|
44
|
+
sessionId: options.sessionId,
|
|
45
|
+
headers: options.headers,
|
|
46
|
+
metadata: options.metadata,
|
|
47
|
+
transport: options.transport,
|
|
48
|
+
thinkingBudgets: options.thinkingBudgets,
|
|
49
|
+
maxRetryDelayMs: options.maxRetryDelayMs,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
export function streamProxy(model, context, options) {
|
|
53
|
+
const stream = new ProxyMessageEventStream();
|
|
54
|
+
(async () => {
|
|
55
|
+
// Initialize the partial message that we'll build up from events
|
|
56
|
+
const partial = {
|
|
57
|
+
role: "assistant",
|
|
58
|
+
stopReason: "stop",
|
|
59
|
+
content: [],
|
|
60
|
+
api: model.api,
|
|
61
|
+
provider: model.provider,
|
|
62
|
+
model: model.id,
|
|
63
|
+
usage: {
|
|
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
|
+
timestamp: Date.now(),
|
|
72
|
+
};
|
|
73
|
+
let reader;
|
|
74
|
+
const abortHandler = () => {
|
|
75
|
+
if (reader) {
|
|
76
|
+
reader.cancel("Request aborted by user").catch(() => { });
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
if (options.signal) {
|
|
80
|
+
options.signal.addEventListener("abort", abortHandler);
|
|
81
|
+
}
|
|
82
|
+
try {
|
|
83
|
+
const response = await fetch(`${options.proxyUrl}/api/stream`, {
|
|
84
|
+
method: "POST",
|
|
85
|
+
headers: {
|
|
86
|
+
Authorization: `Bearer ${options.authToken}`,
|
|
87
|
+
"Content-Type": "application/json",
|
|
88
|
+
},
|
|
89
|
+
body: JSON.stringify({
|
|
90
|
+
model,
|
|
91
|
+
context,
|
|
92
|
+
options: buildProxyRequestOptions(options),
|
|
93
|
+
}),
|
|
94
|
+
signal: options.signal,
|
|
95
|
+
});
|
|
96
|
+
if (!response.ok) {
|
|
97
|
+
let errorMessage = `Proxy error: ${response.status} ${response.statusText}`;
|
|
98
|
+
try {
|
|
99
|
+
const errorData = (await response.json());
|
|
100
|
+
if (errorData.error) {
|
|
101
|
+
errorMessage = `Proxy error: ${errorData.error}`;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
// Couldn't parse error response
|
|
106
|
+
}
|
|
107
|
+
throw new Error(errorMessage);
|
|
108
|
+
}
|
|
109
|
+
reader = response.body.getReader();
|
|
110
|
+
const decoder = new TextDecoder();
|
|
111
|
+
let buffer = "";
|
|
112
|
+
while (true) {
|
|
113
|
+
const { done, value } = await reader.read();
|
|
114
|
+
if (done)
|
|
115
|
+
break;
|
|
116
|
+
if (options.signal?.aborted) {
|
|
117
|
+
throw new Error("Request aborted by user");
|
|
118
|
+
}
|
|
119
|
+
buffer += decoder.decode(value, { stream: true });
|
|
120
|
+
const lines = buffer.split("\n");
|
|
121
|
+
buffer = lines.pop() || "";
|
|
122
|
+
for (const line of lines) {
|
|
123
|
+
if (line.startsWith("data: ")) {
|
|
124
|
+
const data = line.slice(6).trim();
|
|
125
|
+
if (data) {
|
|
126
|
+
const proxyEvent = JSON.parse(data);
|
|
127
|
+
const event = processProxyEvent(proxyEvent, partial);
|
|
128
|
+
if (event) {
|
|
129
|
+
stream.push(event);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (options.signal?.aborted) {
|
|
136
|
+
throw new Error("Request aborted by user");
|
|
137
|
+
}
|
|
138
|
+
stream.end();
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
142
|
+
const reason = options.signal?.aborted ? "aborted" : "error";
|
|
143
|
+
partial.stopReason = reason;
|
|
144
|
+
partial.errorMessage = errorMessage;
|
|
145
|
+
stream.push({
|
|
146
|
+
type: "error",
|
|
147
|
+
reason,
|
|
148
|
+
error: partial,
|
|
149
|
+
});
|
|
150
|
+
stream.end();
|
|
151
|
+
}
|
|
152
|
+
finally {
|
|
153
|
+
if (options.signal) {
|
|
154
|
+
options.signal.removeEventListener("abort", abortHandler);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
})();
|
|
158
|
+
return stream;
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Process a proxy event and update the partial message.
|
|
162
|
+
*/
|
|
163
|
+
function processProxyEvent(proxyEvent, partial) {
|
|
164
|
+
switch (proxyEvent.type) {
|
|
165
|
+
case "start":
|
|
166
|
+
return { type: "start", partial };
|
|
167
|
+
case "text_start":
|
|
168
|
+
partial.content[proxyEvent.contentIndex] = { type: "text", text: "" };
|
|
169
|
+
return { type: "text_start", contentIndex: proxyEvent.contentIndex, partial };
|
|
170
|
+
case "text_delta": {
|
|
171
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
172
|
+
if (content?.type === "text") {
|
|
173
|
+
content.text += proxyEvent.delta;
|
|
174
|
+
return {
|
|
175
|
+
type: "text_delta",
|
|
176
|
+
contentIndex: proxyEvent.contentIndex,
|
|
177
|
+
delta: proxyEvent.delta,
|
|
178
|
+
partial,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
throw new Error("Received text_delta for non-text content");
|
|
182
|
+
}
|
|
183
|
+
case "text_end": {
|
|
184
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
185
|
+
if (content?.type === "text") {
|
|
186
|
+
content.textSignature = proxyEvent.contentSignature;
|
|
187
|
+
return {
|
|
188
|
+
type: "text_end",
|
|
189
|
+
contentIndex: proxyEvent.contentIndex,
|
|
190
|
+
content: content.text,
|
|
191
|
+
partial,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
throw new Error("Received text_end for non-text content");
|
|
195
|
+
}
|
|
196
|
+
case "thinking_start":
|
|
197
|
+
partial.content[proxyEvent.contentIndex] = { type: "thinking", thinking: "" };
|
|
198
|
+
return { type: "thinking_start", contentIndex: proxyEvent.contentIndex, partial };
|
|
199
|
+
case "thinking_delta": {
|
|
200
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
201
|
+
if (content?.type === "thinking") {
|
|
202
|
+
content.thinking += proxyEvent.delta;
|
|
203
|
+
return {
|
|
204
|
+
type: "thinking_delta",
|
|
205
|
+
contentIndex: proxyEvent.contentIndex,
|
|
206
|
+
delta: proxyEvent.delta,
|
|
207
|
+
partial,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
throw new Error("Received thinking_delta for non-thinking content");
|
|
211
|
+
}
|
|
212
|
+
case "thinking_end": {
|
|
213
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
214
|
+
if (content?.type === "thinking") {
|
|
215
|
+
content.thinkingSignature = proxyEvent.contentSignature;
|
|
216
|
+
return {
|
|
217
|
+
type: "thinking_end",
|
|
218
|
+
contentIndex: proxyEvent.contentIndex,
|
|
219
|
+
content: content.thinking,
|
|
220
|
+
partial,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
throw new Error("Received thinking_end for non-thinking content");
|
|
224
|
+
}
|
|
225
|
+
case "toolcall_start":
|
|
226
|
+
partial.content[proxyEvent.contentIndex] = {
|
|
227
|
+
type: "toolCall",
|
|
228
|
+
id: proxyEvent.id,
|
|
229
|
+
name: proxyEvent.toolName,
|
|
230
|
+
arguments: {},
|
|
231
|
+
partialJson: "",
|
|
232
|
+
};
|
|
233
|
+
return { type: "toolcall_start", contentIndex: proxyEvent.contentIndex, partial };
|
|
234
|
+
case "toolcall_delta": {
|
|
235
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
236
|
+
if (content?.type === "toolCall") {
|
|
237
|
+
content.partialJson += proxyEvent.delta;
|
|
238
|
+
content.arguments = parseStreamingJson(content.partialJson) || {};
|
|
239
|
+
partial.content[proxyEvent.contentIndex] = { ...content }; // Trigger reactivity
|
|
240
|
+
return {
|
|
241
|
+
type: "toolcall_delta",
|
|
242
|
+
contentIndex: proxyEvent.contentIndex,
|
|
243
|
+
delta: proxyEvent.delta,
|
|
244
|
+
partial,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
throw new Error("Received toolcall_delta for non-toolCall content");
|
|
248
|
+
}
|
|
249
|
+
case "toolcall_end": {
|
|
250
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
251
|
+
if (content?.type === "toolCall") {
|
|
252
|
+
delete content.partialJson;
|
|
253
|
+
return {
|
|
254
|
+
type: "toolcall_end",
|
|
255
|
+
contentIndex: proxyEvent.contentIndex,
|
|
256
|
+
toolCall: content,
|
|
257
|
+
partial,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
return undefined;
|
|
261
|
+
}
|
|
262
|
+
case "done":
|
|
263
|
+
partial.stopReason = proxyEvent.reason;
|
|
264
|
+
partial.usage = proxyEvent.usage;
|
|
265
|
+
return { type: "done", reason: proxyEvent.reason, message: partial };
|
|
266
|
+
case "error":
|
|
267
|
+
partial.stopReason = proxyEvent.reason;
|
|
268
|
+
partial.errorMessage = proxyEvent.errorMessage;
|
|
269
|
+
partial.usage = proxyEvent.usage;
|
|
270
|
+
return { type: "error", reason: proxyEvent.reason, error: partial };
|
|
271
|
+
default: {
|
|
272
|
+
const _exhaustiveCheck = proxyEvent;
|
|
273
|
+
console.warn(`Unhandled proxy event type: ${proxyEvent.type}`);
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
//# sourceMappingURL=proxy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"proxy.js","sourceRoot":"","sources":["../src/proxy.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,2CAA2C;AAC3C,OAAO,EAIN,WAAW,EAEX,kBAAkB,GAIlB,MAAM,iBAAiB,CAAC;AAEzB,uDAAuD;AACvD,MAAM,uBAAwB,SAAQ,WAAoD;IACzF;QACC,KAAK,CACJ,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,MAAM,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO,EAC1D,CAAC,KAAK,EAAE,EAAE;YACT,IAAI,KAAK,CAAC,IAAI,KAAK,MAAM;gBAAE,OAAO,KAAK,CAAC,OAAO,CAAC;YAChD,IAAI,KAAK,CAAC,IAAI,KAAK,OAAO;gBAAE,OAAO,KAAK,CAAC,KAAK,CAAC;YAC/C,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;QAC1C,CAAC,CACD,CAAC;IACH,CAAC;CACD;AAmDD;;;;;;;;;;;;;;;;;;GAkBG;AACH,SAAS,wBAAwB,CAAC,OAA2B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AAM,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC;QAErE,OAAO,CAAC,CAAC,CAAC;YACT,MAAM,gBAAgB,GAAU,UAAU,CAAC;YAC3C,OAAO,CAAC,IAAI,CAAC,+BAAgC,UAAkB,CAAC,IAAI,EAAE,CAAC,CAAC;YACxE,OAAO,SAAS,CAAC;QAClB,CAAC;IACF,CAAC;AACF,CAAC","sourcesContent":["/**\n * Proxy stream function for apps that route LLM calls through a server.\n * The server manages auth and proxies requests to LLM providers.\n */\n\n// Internal import for JSON parsing utility\nimport {\n\ttype AssistantMessage,\n\ttype AssistantMessageEvent,\n\ttype Context,\n\tEventStream,\n\ttype Model,\n\tparseStreamingJson,\n\ttype SimpleStreamOptions,\n\ttype StopReason,\n\ttype ToolCall,\n} from \"ecagent/runtime\";\n\n// Create stream class matching ProxyMessageEventStream\nclass ProxyMessageEventStream extends EventStream<AssistantMessageEvent, AssistantMessage> {\n\tconstructor() {\n\t\tsuper(\n\t\t\t(event) => event.type === \"done\" || event.type === \"error\",\n\t\t\t(event) => {\n\t\t\t\tif (event.type === \"done\") return event.message;\n\t\t\t\tif (event.type === \"error\") return event.error;\n\t\t\t\tthrow new Error(\"Unexpected event type\");\n\t\t\t},\n\t\t);\n\t}\n}\n\n/**\n * Proxy event types - server sends these with partial field stripped to reduce bandwidth.\n */\nexport type ProxyAssistantMessageEvent =\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; contentSignature?: string }\n\t| { type: \"thinking_start\"; contentIndex: number }\n\t| { type: \"thinking_delta\"; contentIndex: number; delta: string }\n\t| { type: \"thinking_end\"; contentIndex: number; contentSignature?: string }\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 }\n\t| {\n\t\t\ttype: \"done\";\n\t\t\treason: Extract<StopReason, \"stop\" | \"length\" | \"toolUse\">;\n\t\t\tusage: AssistantMessage[\"usage\"];\n\t }\n\t| {\n\t\t\ttype: \"error\";\n\t\t\treason: Extract<StopReason, \"aborted\" | \"error\">;\n\t\t\terrorMessage?: string;\n\t\t\tusage: AssistantMessage[\"usage\"];\n\t };\n\ntype ProxySerializableStreamOptions = Pick<\n\tSimpleStreamOptions,\n\t| \"temperature\"\n\t| \"maxTokens\"\n\t| \"reasoning\"\n\t| \"cacheRetention\"\n\t| \"sessionId\"\n\t| \"headers\"\n\t| \"metadata\"\n\t| \"transport\"\n\t| \"thinkingBudgets\"\n\t| \"maxRetryDelayMs\"\n>;\n\nexport interface ProxyStreamOptions extends ProxySerializableStreamOptions {\n\t/** Local abort signal for the proxy request */\n\tsignal?: AbortSignal;\n\t/** Auth token for the proxy server */\n\tauthToken: string;\n\t/** Proxy server URL (e.g., \"https://genai.example.com\") */\n\tproxyUrl: string;\n}\n\n/**\n * Stream function that proxies through a server instead of calling LLM providers directly.\n * The server strips the partial field from delta events to reduce bandwidth.\n * We reconstruct the partial message client-side.\n *\n * Use this as the `streamFn` option when creating an Agent that needs to go through a proxy.\n *\n * @example\n * ```typescript\n * const agent = new Agent({\n * streamFn: (model, context, options) =>\n * streamProxy(model, context, {\n * ...options,\n * authToken: await getAuthToken(),\n * proxyUrl: \"https://genai.example.com\",\n * }),\n * });\n * ```\n */\nfunction buildProxyRequestOptions(options: ProxyStreamOptions): ProxySerializableStreamOptions {\n\treturn {\n\t\ttemperature: options.temperature,\n\t\tmaxTokens: options.maxTokens,\n\t\treasoning: options.reasoning,\n\t\tcacheRetention: options.cacheRetention,\n\t\tsessionId: options.sessionId,\n\t\theaders: options.headers,\n\t\tmetadata: options.metadata,\n\t\ttransport: options.transport,\n\t\tthinkingBudgets: options.thinkingBudgets,\n\t\tmaxRetryDelayMs: options.maxRetryDelayMs,\n\t};\n}\n\nexport function streamProxy(model: Model<any>, context: Context, options: ProxyStreamOptions): ProxyMessageEventStream {\n\tconst stream = new ProxyMessageEventStream();\n\n\t(async () => {\n\t\t// Initialize the partial message that we'll build up from events\n\t\tconst partial: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tstopReason: \"stop\",\n\t\t\tcontent: [],\n\t\t\tapi: model.api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\tlet reader: ReadableStreamDefaultReader<Uint8Array> | undefined;\n\n\t\tconst abortHandler = () => {\n\t\t\tif (reader) {\n\t\t\t\treader.cancel(\"Request aborted by user\").catch(() => {});\n\t\t\t}\n\t\t};\n\n\t\tif (options.signal) {\n\t\t\toptions.signal.addEventListener(\"abort\", abortHandler);\n\t\t}\n\n\t\ttry {\n\t\t\tconst response = await fetch(`${options.proxyUrl}/api/stream`, {\n\t\t\t\tmethod: \"POST\",\n\t\t\t\theaders: {\n\t\t\t\t\tAuthorization: `Bearer ${options.authToken}`,\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t},\n\t\t\t\tbody: JSON.stringify({\n\t\t\t\t\tmodel,\n\t\t\t\t\tcontext,\n\t\t\t\t\toptions: buildProxyRequestOptions(options),\n\t\t\t\t}),\n\t\t\t\tsignal: options.signal,\n\t\t\t});\n\n\t\t\tif (!response.ok) {\n\t\t\t\tlet errorMessage = `Proxy error: ${response.status} ${response.statusText}`;\n\t\t\t\ttry {\n\t\t\t\t\tconst errorData = (await response.json()) as { error?: string };\n\t\t\t\t\tif (errorData.error) {\n\t\t\t\t\t\terrorMessage = `Proxy error: ${errorData.error}`;\n\t\t\t\t\t}\n\t\t\t\t} catch {\n\t\t\t\t\t// Couldn't parse error response\n\t\t\t\t}\n\t\t\t\tthrow new Error(errorMessage);\n\t\t\t}\n\n\t\t\treader = response.body!.getReader();\n\t\t\tconst decoder = new TextDecoder();\n\t\t\tlet buffer = \"\";\n\n\t\t\twhile (true) {\n\t\t\t\tconst { done, value } = await reader.read();\n\t\t\t\tif (done) break;\n\n\t\t\t\tif (options.signal?.aborted) {\n\t\t\t\t\tthrow new Error(\"Request aborted by user\");\n\t\t\t\t}\n\n\t\t\t\tbuffer += decoder.decode(value, { stream: true });\n\t\t\t\tconst lines = buffer.split(\"\\n\");\n\t\t\t\tbuffer = lines.pop() || \"\";\n\n\t\t\t\tfor (const line of lines) {\n\t\t\t\t\tif (line.startsWith(\"data: \")) {\n\t\t\t\t\t\tconst data = line.slice(6).trim();\n\t\t\t\t\t\tif (data) {\n\t\t\t\t\t\t\tconst proxyEvent = JSON.parse(data) as ProxyAssistantMessageEvent;\n\t\t\t\t\t\t\tconst event = processProxyEvent(proxyEvent, partial);\n\t\t\t\t\t\t\tif (event) {\n\t\t\t\t\t\t\t\tstream.push(event);\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (options.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request aborted by user\");\n\t\t\t}\n\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\tconst errorMessage = error instanceof Error ? error.message : String(error);\n\t\t\tconst reason = options.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\tpartial.stopReason = reason;\n\t\t\tpartial.errorMessage = errorMessage;\n\t\t\tstream.push({\n\t\t\t\ttype: \"error\",\n\t\t\t\treason,\n\t\t\t\terror: partial,\n\t\t\t});\n\t\t\tstream.end();\n\t\t} finally {\n\t\t\tif (options.signal) {\n\t\t\t\toptions.signal.removeEventListener(\"abort\", abortHandler);\n\t\t\t}\n\t\t}\n\t})();\n\n\treturn stream;\n}\n\n/**\n * Process a proxy event and update the partial message.\n */\nfunction processProxyEvent(\n\tproxyEvent: ProxyAssistantMessageEvent,\n\tpartial: AssistantMessage,\n): AssistantMessageEvent | undefined {\n\tswitch (proxyEvent.type) {\n\t\tcase \"start\":\n\t\t\treturn { type: \"start\", partial };\n\n\t\tcase \"text_start\":\n\t\t\tpartial.content[proxyEvent.contentIndex] = { type: \"text\", text: \"\" };\n\t\t\treturn { type: \"text_start\", contentIndex: proxyEvent.contentIndex, partial };\n\n\t\tcase \"text_delta\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"text\") {\n\t\t\t\tcontent.text += proxyEvent.delta;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tdelta: proxyEvent.delta,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received text_delta for non-text content\");\n\t\t}\n\n\t\tcase \"text_end\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"text\") {\n\t\t\t\tcontent.textSignature = proxyEvent.contentSignature;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tcontent: content.text,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received text_end for non-text content\");\n\t\t}\n\n\t\tcase \"thinking_start\":\n\t\t\tpartial.content[proxyEvent.contentIndex] = { type: \"thinking\", thinking: \"\" };\n\t\t\treturn { type: \"thinking_start\", contentIndex: proxyEvent.contentIndex, partial };\n\n\t\tcase \"thinking_delta\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"thinking\") {\n\t\t\t\tcontent.thinking += proxyEvent.delta;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tdelta: proxyEvent.delta,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received thinking_delta for non-thinking content\");\n\t\t}\n\n\t\tcase \"thinking_end\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"thinking\") {\n\t\t\t\tcontent.thinkingSignature = proxyEvent.contentSignature;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tcontent: content.thinking,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received thinking_end for non-thinking content\");\n\t\t}\n\n\t\tcase \"toolcall_start\":\n\t\t\tpartial.content[proxyEvent.contentIndex] = {\n\t\t\t\ttype: \"toolCall\",\n\t\t\t\tid: proxyEvent.id,\n\t\t\t\tname: proxyEvent.toolName,\n\t\t\t\targuments: {},\n\t\t\t\tpartialJson: \"\",\n\t\t\t} satisfies ToolCall & { partialJson: string } as ToolCall;\n\t\t\treturn { type: \"toolcall_start\", contentIndex: proxyEvent.contentIndex, partial };\n\n\t\tcase \"toolcall_delta\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"toolCall\") {\n\t\t\t\t(content as any).partialJson += proxyEvent.delta;\n\t\t\t\tcontent.arguments = parseStreamingJson((content as any).partialJson) || {};\n\t\t\t\tpartial.content[proxyEvent.contentIndex] = { ...content }; // Trigger reactivity\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"toolcall_delta\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\tdelta: proxyEvent.delta,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\tthrow new Error(\"Received toolcall_delta for non-toolCall content\");\n\t\t}\n\n\t\tcase \"toolcall_end\": {\n\t\t\tconst content = partial.content[proxyEvent.contentIndex];\n\t\t\tif (content?.type === \"toolCall\") {\n\t\t\t\tdelete (content as any).partialJson;\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"toolcall_end\",\n\t\t\t\t\tcontentIndex: proxyEvent.contentIndex,\n\t\t\t\t\ttoolCall: content,\n\t\t\t\t\tpartial,\n\t\t\t\t};\n\t\t\t}\n\t\t\treturn undefined;\n\t\t}\n\n\t\tcase \"done\":\n\t\t\tpartial.stopReason = proxyEvent.reason;\n\t\t\tpartial.usage = proxyEvent.usage;\n\t\t\treturn { type: \"done\", reason: proxyEvent.reason, message: partial };\n\n\t\tcase \"error\":\n\t\t\tpartial.stopReason = proxyEvent.reason;\n\t\t\tpartial.errorMessage = proxyEvent.errorMessage;\n\t\t\tpartial.usage = proxyEvent.usage;\n\t\t\treturn { type: \"error\", reason: proxyEvent.reason, error: partial };\n\n\t\tdefault: {\n\t\t\tconst _exhaustiveCheck: never = proxyEvent;\n\t\t\tconsole.warn(`Unhandled proxy event type: ${(proxyEvent as any).type}`);\n\t\t\treturn undefined;\n\t\t}\n\t}\n}\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Api, AssistantMessageEvent, Model, ProviderStreams } from "../types.ts";
|
|
2
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Returns a stream synchronously while running async setup (auth resolution,
|
|
5
|
+
* lazy module loading) behind it. Setup failures terminate the stream with an
|
|
6
|
+
* error event.
|
|
7
|
+
*/
|
|
8
|
+
export declare function lazyStream(model: Model<Api>, setup: () => Promise<AsyncIterable<AssistantMessageEvent>>): AssistantMessageEventStream;
|
|
9
|
+
/**
|
|
10
|
+
* Wraps a dynamically imported API implementation module as `ProviderStreams`.
|
|
11
|
+
* The module loads on first stream call; the host's import cache deduplicates
|
|
12
|
+
* loads. Load failures terminate the returned stream with an error event.
|
|
13
|
+
*/
|
|
14
|
+
export declare function lazyApi(load: () => Promise<ProviderStreams>): ProviderStreams;
|
|
15
|
+
//# sourceMappingURL=lazy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../../../src/runtime/api/lazy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAoB,qBAAqB,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACxG,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AAgCvE;;;;GAIG;AACH,wBAAgB,UAAU,CACzB,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,KAAK,EAAE,MAAM,OAAO,CAAC,aAAa,CAAC,qBAAqB,CAAC,CAAC,GACxD,2BAA2B,CAc7B;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,eAAe,CAAC,GAAG,eAAe,CAO7E"}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
2
|
+
function createSetupErrorMessage(model, error) {
|
|
3
|
+
return {
|
|
4
|
+
role: "assistant",
|
|
5
|
+
content: [],
|
|
6
|
+
api: model.api,
|
|
7
|
+
provider: model.provider,
|
|
8
|
+
model: model.id,
|
|
9
|
+
usage: {
|
|
10
|
+
input: 0,
|
|
11
|
+
output: 0,
|
|
12
|
+
cacheRead: 0,
|
|
13
|
+
cacheWrite: 0,
|
|
14
|
+
totalTokens: 0,
|
|
15
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
16
|
+
},
|
|
17
|
+
stopReason: "error",
|
|
18
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
19
|
+
timestamp: Date.now(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function forwardStream(target, source) {
|
|
23
|
+
(async () => {
|
|
24
|
+
for await (const event of source) {
|
|
25
|
+
target.push(event);
|
|
26
|
+
}
|
|
27
|
+
target.end();
|
|
28
|
+
})();
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Returns a stream synchronously while running async setup (auth resolution,
|
|
32
|
+
* lazy module loading) behind it. Setup failures terminate the stream with an
|
|
33
|
+
* error event.
|
|
34
|
+
*/
|
|
35
|
+
export function lazyStream(model, setup) {
|
|
36
|
+
const outer = new AssistantMessageEventStream();
|
|
37
|
+
setup()
|
|
38
|
+
.then((inner) => {
|
|
39
|
+
forwardStream(outer, inner);
|
|
40
|
+
})
|
|
41
|
+
.catch((error) => {
|
|
42
|
+
const message = createSetupErrorMessage(model, error);
|
|
43
|
+
outer.push({ type: "error", reason: "error", error: message });
|
|
44
|
+
outer.end(message);
|
|
45
|
+
});
|
|
46
|
+
return outer;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Wraps a dynamically imported API implementation module as `ProviderStreams`.
|
|
50
|
+
* The module loads on first stream call; the host's import cache deduplicates
|
|
51
|
+
* loads. Load failures terminate the returned stream with an error event.
|
|
52
|
+
*/
|
|
53
|
+
export function lazyApi(load) {
|
|
54
|
+
return {
|
|
55
|
+
stream: (model, context, options) => lazyStream(model, async () => (await load()).stream(model, context, options)),
|
|
56
|
+
streamSimple: (model, context, options) => lazyStream(model, async () => (await load()).streamSimple(model, context, options)),
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
//# sourceMappingURL=lazy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazy.js","sourceRoot":"","sources":["../../../src/runtime/api/lazy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AAEvE,SAAS,uBAAuB,CAAC,KAAiB,EAAE,KAAc;IACjE,OAAO;QACN,IAAI,EAAE,WAAW;QACjB,OAAO,EAAE,EAAE;QACX,GAAG,EAAE,KAAK,CAAC,GAAG;QACd,QAAQ,EAAE,KAAK,CAAC,QAAQ;QACxB,KAAK,EAAE,KAAK,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C;QAC/D,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACpB,CAAC,CAAC,CAAC;IAEJ,OAAO,KAAK,CAAC;AACd,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAAC,IAAoC;IAC3D,OAAO;QACN,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,CACnC,UAAU,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QAC9E,YAAY,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,CACzC,UAAU,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;KACpF,CAAC;AACH,CAAC","sourcesContent":["import type { Api, AssistantMessage, AssistantMessageEvent, Model, ProviderStreams } from \"../types.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\n\nfunction createSetupErrorMessage(model: Model<Api>, error: unknown): AssistantMessage {\n\treturn {\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: {\n\t\t\tinput: 0,\n\t\t\toutput: 0,\n\t\t\tcacheRead: 0,\n\t\t\tcacheWrite: 0,\n\t\t\ttotalTokens: 0,\n\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t},\n\t\tstopReason: \"error\",\n\t\terrorMessage: error instanceof Error ? error.message : String(error),\n\t\ttimestamp: Date.now(),\n\t};\n}\n\nfunction forwardStream(target: AssistantMessageEventStream, source: AsyncIterable<AssistantMessageEvent>): void {\n\t(async () => {\n\t\tfor await (const event of source) {\n\t\t\ttarget.push(event);\n\t\t}\n\t\ttarget.end();\n\t})();\n}\n\n/**\n * Returns a stream synchronously while running async setup (auth resolution,\n * lazy module loading) behind it. Setup failures terminate the stream with an\n * error event.\n */\nexport function lazyStream(\n\tmodel: Model<Api>,\n\tsetup: () => Promise<AsyncIterable<AssistantMessageEvent>>,\n): AssistantMessageEventStream {\n\tconst outer = new AssistantMessageEventStream();\n\n\tsetup()\n\t\t.then((inner) => {\n\t\t\tforwardStream(outer, inner);\n\t\t})\n\t\t.catch((error) => {\n\t\t\tconst message = createSetupErrorMessage(model, error);\n\t\t\touter.push({ type: \"error\", reason: \"error\", error: message });\n\t\t\touter.end(message);\n\t\t});\n\n\treturn outer;\n}\n\n/**\n * Wraps a dynamically imported API implementation module as `ProviderStreams`.\n * The module loads on first stream call; the host's import cache deduplicates\n * loads. Load failures terminate the returned stream with an error event.\n */\nexport function lazyApi(load: () => Promise<ProviderStreams>): ProviderStreams {\n\treturn {\n\t\tstream: (model, context, options) =>\n\t\t\tlazyStream(model, async () => (await load()).stream(model, context, options)),\n\t\tstreamSimple: (model, context, options) =>\n\t\t\tlazyStream(model, async () => (await load()).streamSimple(model, context, options)),\n\t};\n}\n"]}
|
|
@@ -0,0 +1,97 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pi-messages.d.ts","sourceRoot":"","sources":["../../../src/runtime/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,CA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