evolcore 0.0.4 → 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 +65 -3
- 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 +127 -21
- package/dist/cli/data-command.js +132 -0
- package/dist/cli/index.js +11 -3
- package/dist/cli/init.js +116 -37
- 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/access-policy-domain.js +0 -20
- package/dist/config/access-policy.js +5 -29
- 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/auth/operation-catalog.js +1 -1
- package/dist/core/command/command-handler.js +2 -2
- package/dist/core/command/connect-menu.js +20 -42
- package/dist/core/command/menu-handler.js +11 -9
- package/dist/core/command/role-menu.js +27 -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/inbound-admission.js +23 -13
- 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 +300 -61
- package/dist/core/session/session-manager.js +225 -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/cross-platform.js +8 -2
- package/dist/utils/error-utils.js +9 -0
- package/dist/utils/process-introspect.js +19 -3
- 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/docs/evolcore/config.md +4 -4
- package/kits/docs/evolcore/contact.md +2 -1
- package/kits/schemas/_meta.json +7 -4
- package/kits/schemas/agent-config.schema.10.json +370 -0
- package/kits/schemas/agent-config.schema.9.json +2 -12
- 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,367 @@
|
|
|
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
|
+
|
|
6
|
+
// Internal import for JSON parsing utility
|
|
7
|
+
import {
|
|
8
|
+
type AssistantMessage,
|
|
9
|
+
type AssistantMessageEvent,
|
|
10
|
+
type Context,
|
|
11
|
+
EventStream,
|
|
12
|
+
type Model,
|
|
13
|
+
parseStreamingJson,
|
|
14
|
+
type SimpleStreamOptions,
|
|
15
|
+
type StopReason,
|
|
16
|
+
type ToolCall,
|
|
17
|
+
} from "ecagent/runtime";
|
|
18
|
+
|
|
19
|
+
// Create stream class matching ProxyMessageEventStream
|
|
20
|
+
class ProxyMessageEventStream extends EventStream<AssistantMessageEvent, AssistantMessage> {
|
|
21
|
+
constructor() {
|
|
22
|
+
super(
|
|
23
|
+
(event) => event.type === "done" || event.type === "error",
|
|
24
|
+
(event) => {
|
|
25
|
+
if (event.type === "done") return event.message;
|
|
26
|
+
if (event.type === "error") return event.error;
|
|
27
|
+
throw new Error("Unexpected event type");
|
|
28
|
+
},
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Proxy event types - server sends these with partial field stripped to reduce bandwidth.
|
|
35
|
+
*/
|
|
36
|
+
export type ProxyAssistantMessageEvent =
|
|
37
|
+
| { type: "start" }
|
|
38
|
+
| { type: "text_start"; contentIndex: number }
|
|
39
|
+
| { type: "text_delta"; contentIndex: number; delta: string }
|
|
40
|
+
| { type: "text_end"; contentIndex: number; contentSignature?: string }
|
|
41
|
+
| { type: "thinking_start"; contentIndex: number }
|
|
42
|
+
| { type: "thinking_delta"; contentIndex: number; delta: string }
|
|
43
|
+
| { type: "thinking_end"; contentIndex: number; contentSignature?: string }
|
|
44
|
+
| { type: "toolcall_start"; contentIndex: number; id: string; toolName: string }
|
|
45
|
+
| { type: "toolcall_delta"; contentIndex: number; delta: string }
|
|
46
|
+
| { type: "toolcall_end"; contentIndex: number }
|
|
47
|
+
| {
|
|
48
|
+
type: "done";
|
|
49
|
+
reason: Extract<StopReason, "stop" | "length" | "toolUse">;
|
|
50
|
+
usage: AssistantMessage["usage"];
|
|
51
|
+
}
|
|
52
|
+
| {
|
|
53
|
+
type: "error";
|
|
54
|
+
reason: Extract<StopReason, "aborted" | "error">;
|
|
55
|
+
errorMessage?: string;
|
|
56
|
+
usage: AssistantMessage["usage"];
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
type ProxySerializableStreamOptions = Pick<
|
|
60
|
+
SimpleStreamOptions,
|
|
61
|
+
| "temperature"
|
|
62
|
+
| "maxTokens"
|
|
63
|
+
| "reasoning"
|
|
64
|
+
| "cacheRetention"
|
|
65
|
+
| "sessionId"
|
|
66
|
+
| "headers"
|
|
67
|
+
| "metadata"
|
|
68
|
+
| "transport"
|
|
69
|
+
| "thinkingBudgets"
|
|
70
|
+
| "maxRetryDelayMs"
|
|
71
|
+
>;
|
|
72
|
+
|
|
73
|
+
export interface ProxyStreamOptions extends ProxySerializableStreamOptions {
|
|
74
|
+
/** Local abort signal for the proxy request */
|
|
75
|
+
signal?: AbortSignal;
|
|
76
|
+
/** Auth token for the proxy server */
|
|
77
|
+
authToken: string;
|
|
78
|
+
/** Proxy server URL (e.g., "https://genai.example.com") */
|
|
79
|
+
proxyUrl: string;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Stream function that proxies through a server instead of calling LLM providers directly.
|
|
84
|
+
* The server strips the partial field from delta events to reduce bandwidth.
|
|
85
|
+
* We reconstruct the partial message client-side.
|
|
86
|
+
*
|
|
87
|
+
* Use this as the `streamFn` option when creating an Agent that needs to go through a proxy.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* const agent = new Agent({
|
|
92
|
+
* streamFn: (model, context, options) =>
|
|
93
|
+
* streamProxy(model, context, {
|
|
94
|
+
* ...options,
|
|
95
|
+
* authToken: await getAuthToken(),
|
|
96
|
+
* proxyUrl: "https://genai.example.com",
|
|
97
|
+
* }),
|
|
98
|
+
* });
|
|
99
|
+
* ```
|
|
100
|
+
*/
|
|
101
|
+
function buildProxyRequestOptions(options: ProxyStreamOptions): ProxySerializableStreamOptions {
|
|
102
|
+
return {
|
|
103
|
+
temperature: options.temperature,
|
|
104
|
+
maxTokens: options.maxTokens,
|
|
105
|
+
reasoning: options.reasoning,
|
|
106
|
+
cacheRetention: options.cacheRetention,
|
|
107
|
+
sessionId: options.sessionId,
|
|
108
|
+
headers: options.headers,
|
|
109
|
+
metadata: options.metadata,
|
|
110
|
+
transport: options.transport,
|
|
111
|
+
thinkingBudgets: options.thinkingBudgets,
|
|
112
|
+
maxRetryDelayMs: options.maxRetryDelayMs,
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export function streamProxy(model: Model<any>, context: Context, options: ProxyStreamOptions): ProxyMessageEventStream {
|
|
117
|
+
const stream = new ProxyMessageEventStream();
|
|
118
|
+
|
|
119
|
+
(async () => {
|
|
120
|
+
// Initialize the partial message that we'll build up from events
|
|
121
|
+
const partial: AssistantMessage = {
|
|
122
|
+
role: "assistant",
|
|
123
|
+
stopReason: "stop",
|
|
124
|
+
content: [],
|
|
125
|
+
api: model.api,
|
|
126
|
+
provider: model.provider,
|
|
127
|
+
model: model.id,
|
|
128
|
+
usage: {
|
|
129
|
+
input: 0,
|
|
130
|
+
output: 0,
|
|
131
|
+
cacheRead: 0,
|
|
132
|
+
cacheWrite: 0,
|
|
133
|
+
totalTokens: 0,
|
|
134
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
135
|
+
},
|
|
136
|
+
timestamp: Date.now(),
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
let reader: ReadableStreamDefaultReader<Uint8Array> | undefined;
|
|
140
|
+
|
|
141
|
+
const abortHandler = () => {
|
|
142
|
+
if (reader) {
|
|
143
|
+
reader.cancel("Request aborted by user").catch(() => {});
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
if (options.signal) {
|
|
148
|
+
options.signal.addEventListener("abort", abortHandler);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
const response = await fetch(`${options.proxyUrl}/api/stream`, {
|
|
153
|
+
method: "POST",
|
|
154
|
+
headers: {
|
|
155
|
+
Authorization: `Bearer ${options.authToken}`,
|
|
156
|
+
"Content-Type": "application/json",
|
|
157
|
+
},
|
|
158
|
+
body: JSON.stringify({
|
|
159
|
+
model,
|
|
160
|
+
context,
|
|
161
|
+
options: buildProxyRequestOptions(options),
|
|
162
|
+
}),
|
|
163
|
+
signal: options.signal,
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
if (!response.ok) {
|
|
167
|
+
let errorMessage = `Proxy error: ${response.status} ${response.statusText}`;
|
|
168
|
+
try {
|
|
169
|
+
const errorData = (await response.json()) as { error?: string };
|
|
170
|
+
if (errorData.error) {
|
|
171
|
+
errorMessage = `Proxy error: ${errorData.error}`;
|
|
172
|
+
}
|
|
173
|
+
} catch {
|
|
174
|
+
// Couldn't parse error response
|
|
175
|
+
}
|
|
176
|
+
throw new Error(errorMessage);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
reader = response.body!.getReader();
|
|
180
|
+
const decoder = new TextDecoder();
|
|
181
|
+
let buffer = "";
|
|
182
|
+
|
|
183
|
+
while (true) {
|
|
184
|
+
const { done, value } = await reader.read();
|
|
185
|
+
if (done) break;
|
|
186
|
+
|
|
187
|
+
if (options.signal?.aborted) {
|
|
188
|
+
throw new Error("Request aborted by user");
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
buffer += decoder.decode(value, { stream: true });
|
|
192
|
+
const lines = buffer.split("\n");
|
|
193
|
+
buffer = lines.pop() || "";
|
|
194
|
+
|
|
195
|
+
for (const line of lines) {
|
|
196
|
+
if (line.startsWith("data: ")) {
|
|
197
|
+
const data = line.slice(6).trim();
|
|
198
|
+
if (data) {
|
|
199
|
+
const proxyEvent = JSON.parse(data) as ProxyAssistantMessageEvent;
|
|
200
|
+
const event = processProxyEvent(proxyEvent, partial);
|
|
201
|
+
if (event) {
|
|
202
|
+
stream.push(event);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (options.signal?.aborted) {
|
|
210
|
+
throw new Error("Request aborted by user");
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
stream.end();
|
|
214
|
+
} catch (error) {
|
|
215
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
216
|
+
const reason = options.signal?.aborted ? "aborted" : "error";
|
|
217
|
+
partial.stopReason = reason;
|
|
218
|
+
partial.errorMessage = errorMessage;
|
|
219
|
+
stream.push({
|
|
220
|
+
type: "error",
|
|
221
|
+
reason,
|
|
222
|
+
error: partial,
|
|
223
|
+
});
|
|
224
|
+
stream.end();
|
|
225
|
+
} finally {
|
|
226
|
+
if (options.signal) {
|
|
227
|
+
options.signal.removeEventListener("abort", abortHandler);
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
})();
|
|
231
|
+
|
|
232
|
+
return stream;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Process a proxy event and update the partial message.
|
|
237
|
+
*/
|
|
238
|
+
function processProxyEvent(
|
|
239
|
+
proxyEvent: ProxyAssistantMessageEvent,
|
|
240
|
+
partial: AssistantMessage,
|
|
241
|
+
): AssistantMessageEvent | undefined {
|
|
242
|
+
switch (proxyEvent.type) {
|
|
243
|
+
case "start":
|
|
244
|
+
return { type: "start", partial };
|
|
245
|
+
|
|
246
|
+
case "text_start":
|
|
247
|
+
partial.content[proxyEvent.contentIndex] = { type: "text", text: "" };
|
|
248
|
+
return { type: "text_start", contentIndex: proxyEvent.contentIndex, partial };
|
|
249
|
+
|
|
250
|
+
case "text_delta": {
|
|
251
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
252
|
+
if (content?.type === "text") {
|
|
253
|
+
content.text += proxyEvent.delta;
|
|
254
|
+
return {
|
|
255
|
+
type: "text_delta",
|
|
256
|
+
contentIndex: proxyEvent.contentIndex,
|
|
257
|
+
delta: proxyEvent.delta,
|
|
258
|
+
partial,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
throw new Error("Received text_delta for non-text content");
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
case "text_end": {
|
|
265
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
266
|
+
if (content?.type === "text") {
|
|
267
|
+
content.textSignature = proxyEvent.contentSignature;
|
|
268
|
+
return {
|
|
269
|
+
type: "text_end",
|
|
270
|
+
contentIndex: proxyEvent.contentIndex,
|
|
271
|
+
content: content.text,
|
|
272
|
+
partial,
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
throw new Error("Received text_end for non-text content");
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
case "thinking_start":
|
|
279
|
+
partial.content[proxyEvent.contentIndex] = { type: "thinking", thinking: "" };
|
|
280
|
+
return { type: "thinking_start", contentIndex: proxyEvent.contentIndex, partial };
|
|
281
|
+
|
|
282
|
+
case "thinking_delta": {
|
|
283
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
284
|
+
if (content?.type === "thinking") {
|
|
285
|
+
content.thinking += proxyEvent.delta;
|
|
286
|
+
return {
|
|
287
|
+
type: "thinking_delta",
|
|
288
|
+
contentIndex: proxyEvent.contentIndex,
|
|
289
|
+
delta: proxyEvent.delta,
|
|
290
|
+
partial,
|
|
291
|
+
};
|
|
292
|
+
}
|
|
293
|
+
throw new Error("Received thinking_delta for non-thinking content");
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
case "thinking_end": {
|
|
297
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
298
|
+
if (content?.type === "thinking") {
|
|
299
|
+
content.thinkingSignature = proxyEvent.contentSignature;
|
|
300
|
+
return {
|
|
301
|
+
type: "thinking_end",
|
|
302
|
+
contentIndex: proxyEvent.contentIndex,
|
|
303
|
+
content: content.thinking,
|
|
304
|
+
partial,
|
|
305
|
+
};
|
|
306
|
+
}
|
|
307
|
+
throw new Error("Received thinking_end for non-thinking content");
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
case "toolcall_start":
|
|
311
|
+
partial.content[proxyEvent.contentIndex] = {
|
|
312
|
+
type: "toolCall",
|
|
313
|
+
id: proxyEvent.id,
|
|
314
|
+
name: proxyEvent.toolName,
|
|
315
|
+
arguments: {},
|
|
316
|
+
partialJson: "",
|
|
317
|
+
} satisfies ToolCall & { partialJson: string } as ToolCall;
|
|
318
|
+
return { type: "toolcall_start", contentIndex: proxyEvent.contentIndex, partial };
|
|
319
|
+
|
|
320
|
+
case "toolcall_delta": {
|
|
321
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
322
|
+
if (content?.type === "toolCall") {
|
|
323
|
+
(content as any).partialJson += proxyEvent.delta;
|
|
324
|
+
content.arguments = parseStreamingJson((content as any).partialJson) || {};
|
|
325
|
+
partial.content[proxyEvent.contentIndex] = { ...content }; // Trigger reactivity
|
|
326
|
+
return {
|
|
327
|
+
type: "toolcall_delta",
|
|
328
|
+
contentIndex: proxyEvent.contentIndex,
|
|
329
|
+
delta: proxyEvent.delta,
|
|
330
|
+
partial,
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
throw new Error("Received toolcall_delta for non-toolCall content");
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
case "toolcall_end": {
|
|
337
|
+
const content = partial.content[proxyEvent.contentIndex];
|
|
338
|
+
if (content?.type === "toolCall") {
|
|
339
|
+
delete (content as any).partialJson;
|
|
340
|
+
return {
|
|
341
|
+
type: "toolcall_end",
|
|
342
|
+
contentIndex: proxyEvent.contentIndex,
|
|
343
|
+
toolCall: content,
|
|
344
|
+
partial,
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
return undefined;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
case "done":
|
|
351
|
+
partial.stopReason = proxyEvent.reason;
|
|
352
|
+
partial.usage = proxyEvent.usage;
|
|
353
|
+
return { type: "done", reason: proxyEvent.reason, message: partial };
|
|
354
|
+
|
|
355
|
+
case "error":
|
|
356
|
+
partial.stopReason = proxyEvent.reason;
|
|
357
|
+
partial.errorMessage = proxyEvent.errorMessage;
|
|
358
|
+
partial.usage = proxyEvent.usage;
|
|
359
|
+
return { type: "error", reason: proxyEvent.reason, error: partial };
|
|
360
|
+
|
|
361
|
+
default: {
|
|
362
|
+
const _exhaustiveCheck: never = proxyEvent;
|
|
363
|
+
console.warn(`Unhandled proxy event type: ${(proxyEvent as any).type}`);
|
|
364
|
+
return undefined;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { Api, AssistantMessage, AssistantMessageEvent, Model, ProviderStreams } from "../types.ts";
|
|
2
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.ts";
|
|
3
|
+
|
|
4
|
+
function createSetupErrorMessage(model: Model<Api>, error: unknown): AssistantMessage {
|
|
5
|
+
return {
|
|
6
|
+
role: "assistant",
|
|
7
|
+
content: [],
|
|
8
|
+
api: model.api,
|
|
9
|
+
provider: model.provider,
|
|
10
|
+
model: model.id,
|
|
11
|
+
usage: {
|
|
12
|
+
input: 0,
|
|
13
|
+
output: 0,
|
|
14
|
+
cacheRead: 0,
|
|
15
|
+
cacheWrite: 0,
|
|
16
|
+
totalTokens: 0,
|
|
17
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
18
|
+
},
|
|
19
|
+
stopReason: "error",
|
|
20
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
21
|
+
timestamp: Date.now(),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function forwardStream(target: AssistantMessageEventStream, source: AsyncIterable<AssistantMessageEvent>): void {
|
|
26
|
+
(async () => {
|
|
27
|
+
for await (const event of source) {
|
|
28
|
+
target.push(event);
|
|
29
|
+
}
|
|
30
|
+
target.end();
|
|
31
|
+
})();
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Returns a stream synchronously while running async setup (auth resolution,
|
|
36
|
+
* lazy module loading) behind it. Setup failures terminate the stream with an
|
|
37
|
+
* error event.
|
|
38
|
+
*/
|
|
39
|
+
export function lazyStream(
|
|
40
|
+
model: Model<Api>,
|
|
41
|
+
setup: () => Promise<AsyncIterable<AssistantMessageEvent>>,
|
|
42
|
+
): AssistantMessageEventStream {
|
|
43
|
+
const outer = new AssistantMessageEventStream();
|
|
44
|
+
|
|
45
|
+
setup()
|
|
46
|
+
.then((inner) => {
|
|
47
|
+
forwardStream(outer, inner);
|
|
48
|
+
})
|
|
49
|
+
.catch((error) => {
|
|
50
|
+
const message = createSetupErrorMessage(model, error);
|
|
51
|
+
outer.push({ type: "error", reason: "error", error: message });
|
|
52
|
+
outer.end(message);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return outer;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Wraps a dynamically imported API implementation module as `ProviderStreams`.
|
|
60
|
+
* The module loads on first stream call; the host's import cache deduplicates
|
|
61
|
+
* loads. Load failures terminate the returned stream with an error event.
|
|
62
|
+
*/
|
|
63
|
+
export function lazyApi(load: () => Promise<ProviderStreams>): ProviderStreams {
|
|
64
|
+
return {
|
|
65
|
+
stream: (model, context, options) =>
|
|
66
|
+
lazyStream(model, async () => (await load()).stream(model, context, options)),
|
|
67
|
+
streamSimple: (model, context, options) =>
|
|
68
|
+
lazyStream(model, async () => (await load()).streamSimple(model, context, options)),
|
|
69
|
+
};
|
|
70
|
+
}
|