cortico 0.1.0
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/LICENSE +21 -0
- package/README.md +47 -0
- package/package.json +43 -0
- package/src/boot.ts +46 -0
- package/src/bot.ts +1342 -0
- package/src/config-file.ts +41 -0
- package/src/core/README.md +92 -0
- package/src/core/billing.ts +26 -0
- package/src/core/blobs.ts +106 -0
- package/src/core/bus.ts +311 -0
- package/src/core/config-schema.ts +201 -0
- package/src/core/config.ts +262 -0
- package/src/core/core.ts +699 -0
- package/src/core/cost.ts +244 -0
- package/src/core/event-store.ts +304 -0
- package/src/core/fork.ts +113 -0
- package/src/core/generation.ts +118 -0
- package/src/core/instance-lock.ts +215 -0
- package/src/core/ipc-logger.ts +105 -0
- package/src/core/language.ts +62 -0
- package/src/core/log-context.ts +38 -0
- package/src/core/loop.ts +1858 -0
- package/src/core/markers.ts +48 -0
- package/src/core/prefix.ts +121 -0
- package/src/core/run.ts +95 -0
- package/src/core/secrets.ts +18 -0
- package/src/core/session.ts +56 -0
- package/src/core/sessions.ts +261 -0
- package/src/core/state.ts +104 -0
- package/src/core/template.ts +72 -0
- package/src/core/timers.ts +129 -0
- package/src/core/tool-log.ts +135 -0
- package/src/core/transcript.ts +92 -0
- package/src/core/truncate.ts +121 -0
- package/src/core/types.ts +1140 -0
- package/src/core/usage-log.ts +79 -0
- package/src/core/util.ts +386 -0
- package/src/deploy-listing.ts +160 -0
- package/src/deploy.ts +162 -0
- package/src/extensions/README.md +72 -0
- package/src/extensions/dry-mount.ts +390 -0
- package/src/extensions/manifest.ts +153 -0
- package/src/extensions/runtime.ts +42 -0
- package/src/extensions.ts +519 -0
- package/src/launcher.ts +285 -0
- package/src/paths.ts +155 -0
- package/src/protocol/open-responses/LICENSE +201 -0
- package/src/protocol/open-responses/README.md +14 -0
- package/src/protocol/open-responses/context-helpers.ts +27 -0
- package/src/protocol/open-responses/context-log.ts +59 -0
- package/src/protocol/open-responses/context.ts +89 -0
- package/src/protocol/open-responses/generated.ts +115 -0
- package/src/protocol/open-responses/index.ts +33 -0
- package/src/protocol/open-responses/openapi.json +4230 -0
- package/src/protocol/open-responses/stream.ts +146 -0
- package/src/protocol/open-responses/tokens.ts +31 -0
- package/src/providers/README.md +86 -0
- package/src/providers/base.ts +111 -0
- package/src/providers/configuration.ts +97 -0
- package/src/providers/console/config.ts +21 -0
- package/src/providers/console/settings.ts +443 -0
- package/src/providers/console/strings.ts +74 -0
- package/src/providers/console/types.ts +11 -0
- package/src/providers/llamacpp/archive.ts +148 -0
- package/src/providers/llamacpp/catalog.ts +247 -0
- package/src/providers/llamacpp/config.ts +36 -0
- package/src/providers/llamacpp/console/client.ts +6 -0
- package/src/providers/llamacpp/console/models-panel.ts +120 -0
- package/src/providers/llamacpp/console/runtime-panel.ts +173 -0
- package/src/providers/llamacpp/console/server.ts +153 -0
- package/src/providers/llamacpp/index.ts +95 -0
- package/src/providers/llamacpp/native.ts +51 -0
- package/src/providers/llamacpp/options.ts +117 -0
- package/src/providers/llamacpp/runtime-store.ts +139 -0
- package/src/providers/llamacpp/runtime.ts +214 -0
- package/src/providers/llamacpp/server.ts +286 -0
- package/src/providers/llamacpp/strings.ts +230 -0
- package/src/providers/openai-responses-compat/config.ts +20 -0
- package/src/providers/openai-responses-compat/index.ts +89 -0
- package/src/providers/openai-responses-compat/native.ts +127 -0
- package/src/providers/openai-responses-compat/strings.ts +19 -0
- package/src/providers/pricebook.ts +75 -0
- package/src/providers/registry.ts +137 -0
- package/src/providers/strings.ts +71 -0
- package/src/providers/transport/chat.ts +156 -0
- package/src/providers/transport/errors.ts +48 -0
- package/src/providers/transport/history.ts +68 -0
- package/src/providers/transport/native-input.ts +83 -0
- package/src/providers/transport/native-types.ts +13 -0
- package/src/providers/transport/response-assembly.ts +191 -0
- package/src/providers/transport/response-http.ts +196 -0
- package/src/providers/transport/response-meters.ts +27 -0
- package/src/providers/transport/responses-input.ts +56 -0
- package/src/web/README.md +84 -0
- package/src/web/client/console-pages/builtins/llm-settings/panel.ts +625 -0
- package/src/web/client/console-pages/builtins/llm-settings/pricing-panel.ts +168 -0
- package/src/web/client/console-pages/builtins/llm-settings/strings.ts +213 -0
- package/src/web/client/console-pages/builtins.ts +10 -0
- package/src/web/client/console-pages/context.ts +135 -0
- package/src/web/client/console-pages/host.ts +633 -0
- package/src/web/client/console-pages/loader.ts +132 -0
- package/src/web/client/console-pages/strings.ts +90 -0
- package/src/web/client/console-pages/tools/strings.ts +49 -0
- package/src/web/client/console-pages/tools/view.ts +215 -0
- package/src/web/client/core/api.ts +200 -0
- package/src/web/client/core/language.ts +56 -0
- package/src/web/client/core/lifecycle.ts +132 -0
- package/src/web/client/core/router.ts +199 -0
- package/src/web/client/core/stream.ts +157 -0
- package/src/web/client/core/websocket.ts +82 -0
- package/src/web/client/features/appearance/index.ts +333 -0
- package/src/web/client/features/appearance/strings.ts +87 -0
- package/src/web/client/features/config/index.ts +11 -0
- package/src/web/client/features/config/strings.ts +41 -0
- package/src/web/client/features/config/view.ts +428 -0
- package/src/web/client/features/core/events.ts +197 -0
- package/src/web/client/features/core/index.ts +254 -0
- package/src/web/client/features/core/run.ts +56 -0
- package/src/web/client/features/core/runlog.ts +214 -0
- package/src/web/client/features/core/sessions.ts +53 -0
- package/src/web/client/features/core/strings.ts +157 -0
- package/src/web/client/features/extensions/index.ts +422 -0
- package/src/web/client/features/extensions/strings.ts +164 -0
- package/src/web/client/features/feature.ts +93 -0
- package/src/web/client/features/live/context.ts +280 -0
- package/src/web/client/features/live/fork.ts +156 -0
- package/src/web/client/features/live/index.ts +392 -0
- package/src/web/client/features/live/onboarding.ts +113 -0
- package/src/web/client/features/live/protocol.ts +122 -0
- package/src/web/client/features/live/sessions.ts +71 -0
- package/src/web/client/features/live/status.ts +68 -0
- package/src/web/client/features/live/strings.ts +252 -0
- package/src/web/client/features/live/timeline.ts +590 -0
- package/src/web/client/features/prompts/editor.ts +292 -0
- package/src/web/client/features/prompts/index.ts +346 -0
- package/src/web/client/features/prompts/strings.ts +136 -0
- package/src/web/client/features/prompts/view.ts +256 -0
- package/src/web/client/features/providers/index.ts +100 -0
- package/src/web/client/features/providers/strings.ts +23 -0
- package/src/web/client/features/settings/general.ts +28 -0
- package/src/web/client/features/settings/index.ts +82 -0
- package/src/web/client/features/settings/strings.ts +33 -0
- package/src/web/client/features/storage/index.ts +1 -0
- package/src/web/client/features/storage/strings.ts +45 -0
- package/src/web/client/features/storage/view.ts +154 -0
- package/src/web/client/features/usage/chart.ts +486 -0
- package/src/web/client/features/usage/index.ts +407 -0
- package/src/web/client/features/usage/labels.ts +46 -0
- package/src/web/client/features/usage/range.ts +36 -0
- package/src/web/client/features/usage/state.ts +37 -0
- package/src/web/client/features/usage/strings.ts +191 -0
- package/src/web/client/features/usage/tooltip.ts +86 -0
- package/src/web/client/features/usage/types.ts +96 -0
- package/src/web/client/features/worlds/index.ts +424 -0
- package/src/web/client/features/worlds/strings.ts +121 -0
- package/src/web/client/main.ts +273 -0
- package/src/web/client/shell/avatar.ts +233 -0
- package/src/web/client/shell/index.ts +549 -0
- package/src/web/client/shell/strings.ts +89 -0
- package/src/web/client/theme/handoff.ts +43 -0
- package/src/web/client/theme/palette.ts +109 -0
- package/src/web/client/theme/registry.ts +130 -0
- package/src/web/client/theme/storage.ts +72 -0
- package/src/web/client/theme/strings.ts +123 -0
- package/src/web/client/theme/studio.ts +366 -0
- package/src/web/client/ui/actions.ts +109 -0
- package/src/web/client/ui/data.ts +185 -0
- package/src/web/client/ui/dom.ts +23 -0
- package/src/web/client/ui/fields.ts +191 -0
- package/src/web/client/ui/format.ts +75 -0
- package/src/web/client/ui/icons.ts +183 -0
- package/src/web/client/ui/images.ts +91 -0
- package/src/web/client/ui/index.ts +89 -0
- package/src/web/client/ui/lamp.ts +125 -0
- package/src/web/client/ui/log.ts +98 -0
- package/src/web/client/ui/overlay.ts +184 -0
- package/src/web/client/ui/page.ts +8 -0
- package/src/web/client/ui/prompt-input.tsx +311 -0
- package/src/web/client/ui/sheet.ts +131 -0
- package/src/web/client/ui/strings.ts +71 -0
- package/src/web/console-pages.ts +367 -0
- package/src/web/files.ts +107 -0
- package/src/web/path-picker.ts +296 -0
- package/src/web/public/index.html +17 -0
- package/src/web/public/styles.css +1715 -0
- package/src/web/server.ts +1883 -0
- package/src/web/shared/client-panel.ts +603 -0
- package/src/web/shared/console-protocol.ts +560 -0
- package/src/web/shared/css.d.ts +1 -0
- package/src/web/shared/path-picker.ts +23 -0
- package/src/web/shared/theme.ts +256 -0
- package/src/web/theme-store.ts +37 -0
- package/src/world.ts +449 -0
- package/src/worlds/bilibili/ENV_PROMPT.md +5 -0
- package/src/worlds/bilibili/README.md +176 -0
- package/src/worlds/bilibili/audience-admission.ts +995 -0
- package/src/worlds/bilibili/client.ts +528 -0
- package/src/worlds/bilibili/coalescing-buffer.ts +125 -0
- package/src/worlds/bilibili/config.ts +185 -0
- package/src/worlds/bilibili/console/client.ts +202 -0
- package/src/worlds/bilibili/definition.ts +35 -0
- package/src/worlds/bilibili/gift-frame.ts +131 -0
- package/src/worlds/bilibili/normalize.ts +452 -0
- package/src/worlds/bilibili/overlay/announcement.ts +76 -0
- package/src/worlds/bilibili/overlay/assets.ts +82 -0
- package/src/worlds/bilibili/overlay/model.ts +494 -0
- package/src/worlds/bilibili/overlay/project.ts +202 -0
- package/src/worlds/bilibili/overlay/server.ts +286 -0
- package/src/worlds/bilibili/overlay/types.ts +217 -0
- package/src/worlds/bilibili/overlay/web/app.js +690 -0
- package/src/worlds/bilibili/overlay/web/editor.css +297 -0
- package/src/worlds/bilibili/overlay/web/editor.html +102 -0
- package/src/worlds/bilibili/overlay/web/editor.js +2116 -0
- package/src/worlds/bilibili/overlay/web/overlay.html +13 -0
- package/src/worlds/bilibili/overlay/web/styles.css +258 -0
- package/src/worlds/bilibili/protobuf.ts +123 -0
- package/src/worlds/bilibili/wire.ts +94 -0
- package/src/worlds/bilibili/world.ts +1389 -0
- package/src/worlds/console-fixture/ENV_PROMPT.md +1 -0
- package/src/worlds/console-fixture/console/client.ts +72 -0
- package/src/worlds/console-fixture/world.ts +73 -0
- package/src/worlds/index.ts +15 -0
- package/src/worlds/minecraft/ENV_PROMPT.md +32 -0
- package/src/worlds/minecraft/ENV_PROMPT_CAMERA.md +2 -0
- package/src/worlds/minecraft/LICENSE-mineflayer-pathfinder.txt +21 -0
- package/src/worlds/minecraft/README.md +917 -0
- package/src/worlds/minecraft/blueprint-plan.ts +915 -0
- package/src/worlds/minecraft/blueprint-registry.ts +566 -0
- package/src/worlds/minecraft/blueprint-repair.ts +236 -0
- package/src/worlds/minecraft/blueprint-resource.ts +272 -0
- package/src/worlds/minecraft/blueprint.ts +492 -0
- package/src/worlds/minecraft/body-lease.ts +340 -0
- package/src/worlds/minecraft/bridge.ts +749 -0
- package/src/worlds/minecraft/check.ts +651 -0
- package/src/worlds/minecraft/chests.ts +292 -0
- package/src/worlds/minecraft/client-launch.ts +272 -0
- package/src/worlds/minecraft/client-options.ts +85 -0
- package/src/worlds/minecraft/client-skins.ts +164 -0
- package/src/worlds/minecraft/client-window.ps1 +91 -0
- package/src/worlds/minecraft/client.ts +423 -0
- package/src/worlds/minecraft/combat-context.ts +109 -0
- package/src/worlds/minecraft/combat.ts +1552 -0
- package/src/worlds/minecraft/config.ts +499 -0
- package/src/worlds/minecraft/console/access.ts +218 -0
- package/src/worlds/minecraft/console/client.ts +244 -0
- package/src/worlds/minecraft/console/log.ts +110 -0
- package/src/worlds/minecraft/console/mount.ts +420 -0
- package/src/worlds/minecraft/console/skin.ts +257 -0
- package/src/worlds/minecraft/console/style.css +154 -0
- package/src/worlds/minecraft/console/world.ts +449 -0
- package/src/worlds/minecraft/deaths.ts +135 -0
- package/src/worlds/minecraft/definition.ts +18 -0
- package/src/worlds/minecraft/engine-child.ts +274 -0
- package/src/worlds/minecraft/engine-ipc.ts +98 -0
- package/src/worlds/minecraft/entity-facts.ts +132 -0
- package/src/worlds/minecraft/escape.ts +407 -0
- package/src/worlds/minecraft/executor.ts +14494 -0
- package/src/worlds/minecraft/explored.ts +156 -0
- package/src/worlds/minecraft/geometry.ts +205 -0
- package/src/worlds/minecraft/goal-plan.ts +647 -0
- package/src/worlds/minecraft/item-break.ts +126 -0
- package/src/worlds/minecraft/item-facts.ts +146 -0
- package/src/worlds/minecraft/item-pick.ts +67 -0
- package/src/worlds/minecraft/level-dat.ts +176 -0
- package/src/worlds/minecraft/log.ts +132 -0
- package/src/worlds/minecraft/mineflayer-fixes.ts +1180 -0
- package/src/worlds/minecraft/names.ts +377 -0
- package/src/worlds/minecraft/pathfinder-lib.d.ts +13 -0
- package/src/worlds/minecraft/pathfinder-perf.ts +796 -0
- package/src/worlds/minecraft/piglin.ts +49 -0
- package/src/worlds/minecraft/policy.ts +547 -0
- package/src/worlds/minecraft/precheck.ts +655 -0
- package/src/worlds/minecraft/proxy.ts +445 -0
- package/src/worlds/minecraft/ranged.ts +635 -0
- package/src/worlds/minecraft/readouts.ts +102 -0
- package/src/worlds/minecraft/round.ts +26 -0
- package/src/worlds/minecraft/search-observation.ts +92 -0
- package/src/worlds/minecraft/server-config.ts +467 -0
- package/src/worlds/minecraft/server.ts +658 -0
- package/src/worlds/minecraft/show.ts +84 -0
- package/src/worlds/minecraft/skills.ts +1763 -0
- package/src/worlds/minecraft/terrain.ts +1735 -0
- package/src/worlds/minecraft/window.ts +96 -0
- package/src/worlds/minecraft/works.ts +179 -0
- package/src/worlds/minecraft/world.ts +6438 -0
- package/src/worlds/qq/ENV_PROMPT.md +5 -0
- package/src/worlds/qq/config.ts +108 -0
- package/src/worlds/qq/console/client.ts +126 -0
- package/src/worlds/qq/console/events.ts +155 -0
- package/src/worlds/qq/console/gate.ts +232 -0
- package/src/worlds/qq/console/roster.ts +203 -0
- package/src/worlds/qq/console/style.css +65 -0
- package/src/worlds/qq/conversation.ts +28 -0
- package/src/worlds/qq/definition.ts +62 -0
- package/src/worlds/qq/driver.ts +375 -0
- package/src/worlds/qq/history-tools.ts +209 -0
- package/src/worlds/qq/normalize.ts +323 -0
- package/src/worlds/qq/qface-map.ts +282 -0
- package/src/worlds/qq/vision-prompt.ts +27 -0
- package/src/worlds/qq/vision.ts +642 -0
- package/src/worlds/qq/vlm.ts +142 -0
- package/src/worlds/qq/world.ts +1503 -0
- package/src/worlds/terminal/ENV_PROMPT.md +11 -0
- package/src/worlds/terminal/config.ts +9 -0
- package/src/worlds/terminal/definition.ts +14 -0
- package/src/worlds/terminal/world.ts +700 -0
- package/src/worlds/websearch/ENV_PROMPT.md +1 -0
- package/src/worlds/websearch/brave-client.ts +261 -0
- package/src/worlds/websearch/config.ts +75 -0
- package/src/worlds/websearch/definition.ts +20 -0
- package/src/worlds/websearch/world.ts +171 -0
|
@@ -0,0 +1,1140 @@
|
|
|
1
|
+
import type { PriceDefinition } from '../providers/pricebook.ts';
|
|
2
|
+
import type { ContextRecord, Item } from '../protocol/open-responses/context.ts';
|
|
3
|
+
import type { StreamEvent } from '../protocol/open-responses/index.ts';
|
|
4
|
+
import type { ResponseClient, ProviderAttempt } from './generation.ts';
|
|
5
|
+
import type { ConfigGroup } from './config-schema.ts';
|
|
6
|
+
import type { Language } from './language.ts';
|
|
7
|
+
|
|
8
|
+
export type { ConfigGroup, ConfigProperty, ConfigValues } from './config-schema.ts';
|
|
9
|
+
|
|
10
|
+
/** Core、Persona 与 World 的共享契约;session id 对 Core 不透明。 */
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 事件的上下文分区。internal 正文进入 user 消息;external 正文按 SessionDecl.eventDelivery 投递。
|
|
14
|
+
* 两类事件共用总线、批次、投递闸门和事件库。
|
|
15
|
+
*/
|
|
16
|
+
export type EventOrigin = 'external' | 'internal';
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 事件分类标签,由消费方决定保留方式。
|
|
20
|
+
* - snapshot:当前状态;交接记录按 source/type 仅保留最后一条。
|
|
21
|
+
* - speak:对外发言的播放结果;交接记录仅保留最近一段。
|
|
22
|
+
* 标签不改变事件的 type;状态与结果需要不同的保留方式时应使用不同 type。
|
|
23
|
+
*/
|
|
24
|
+
export type EventTag = 'snapshot' | 'speak';
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* 持久化事件记录。cursor 是落库时分配的全局递增序号。
|
|
28
|
+
* CoreApi 不暴露事件库;Persona 通过 World 历史工具查询外部事件。
|
|
29
|
+
*/
|
|
30
|
+
export interface EventEnvelope {
|
|
31
|
+
cursor: number;
|
|
32
|
+
/** 事件所属的 run 分片;cursor 跨 run 递增。 */
|
|
33
|
+
run?: string;
|
|
34
|
+
/** 产生方定义的事件类型。 */
|
|
35
|
+
type: string;
|
|
36
|
+
/** ISO 8601 带时区偏移 */
|
|
37
|
+
ts: string;
|
|
38
|
+
/** 产生方 id:World id、core 或 persona。 */
|
|
39
|
+
source: string;
|
|
40
|
+
/** World 未指定时由 Core 填入 external。 */
|
|
41
|
+
origin: EventOrigin;
|
|
42
|
+
tags?: readonly EventTag[];
|
|
43
|
+
/**
|
|
44
|
+
* 是否可进入模型上下文;缺省为 deliver。
|
|
45
|
+
* archive-only 由候选处理结果承载投递;水位之后且未被结果引用的外部原始事件可在重启时补投。
|
|
46
|
+
*/
|
|
47
|
+
contextDelivery?: 'deliver' | 'archive-only';
|
|
48
|
+
/**
|
|
49
|
+
* 产生方生成的正文,直接进入上下文。
|
|
50
|
+
* Core 不改写正文;事件库 cursor 不属于正文,平台消息 id 由 World 自行标注。
|
|
51
|
+
*/
|
|
52
|
+
text: string;
|
|
53
|
+
/** 产生方定义的稳定过滤键。 */
|
|
54
|
+
senderKey?: string;
|
|
55
|
+
/** 产生方私有数据,不渲染进模型上下文。 */
|
|
56
|
+
meta?: Record<string, unknown>;
|
|
57
|
+
/**
|
|
58
|
+
* 随事件保存的附件;BlobInput 落库时转为 BlobRef,fallbackText 附在正文后。
|
|
59
|
+
* 投递时按模型接受的 MIME 类型附加内容。
|
|
60
|
+
*/
|
|
61
|
+
blobs?: BlobRef[];
|
|
62
|
+
/**
|
|
63
|
+
* 在下一批投递时从 session 移除;事件库记录保留。
|
|
64
|
+
* 仅在整批内部项均为 ephemeral 且无外部正文时生效。
|
|
65
|
+
*/
|
|
66
|
+
ephemeral?: true;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* 延迟到投递时渲染的事件。render 读取现有状态,不执行长时间采样。
|
|
71
|
+
* 返回 null、抛错或超过投递期限时,不落库、不投递。
|
|
72
|
+
* 游标和时间戳在投递时分配;排队项不持久化,不参与重启补投。
|
|
73
|
+
*/
|
|
74
|
+
export interface DeferredEventSpec {
|
|
75
|
+
type: string;
|
|
76
|
+
source: string;
|
|
77
|
+
origin: EventOrigin;
|
|
78
|
+
senderKey?: string;
|
|
79
|
+
meta?: Record<string, unknown>;
|
|
80
|
+
tags?: readonly EventTag[];
|
|
81
|
+
render: () => DeferredRendered | null | Promise<DeferredRendered | null>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export type DeferredRendered = string | { text: string; blobs?: BlobInput[] };
|
|
85
|
+
|
|
86
|
+
/** 候选的原始事件;宿主补全来源、游标并以 archive-only 保存。 */
|
|
87
|
+
export type CandidateSourceEvent = Omit<
|
|
88
|
+
EventEnvelope,
|
|
89
|
+
'cursor' | 'source' | 'origin' | 'contextDelivery'
|
|
90
|
+
>;
|
|
91
|
+
|
|
92
|
+
/** 候选处理生成的事件;时间戳和游标在投递时分配。 */
|
|
93
|
+
export type CandidateProjectionEvent = Omit<CandidateSourceEvent, 'ts'>;
|
|
94
|
+
|
|
95
|
+
/** 一次候选处理的输出;candidateIndexes 指向输入候选数组中被此事件引用的项。 */
|
|
96
|
+
export interface CandidateProjection {
|
|
97
|
+
candidateIndexes: readonly number[];
|
|
98
|
+
event: CandidateProjectionEvent;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export interface CandidateEventSpec {
|
|
102
|
+
source: string;
|
|
103
|
+
origin: EventOrigin;
|
|
104
|
+
/** 该候选引用的原始归档,按到达顺序排列。 */
|
|
105
|
+
sourceEvents: readonly EventEnvelope[];
|
|
106
|
+
/** 仅供投递闸门做字面关键词匹配,不渲染进上下文。 */
|
|
107
|
+
gateText: string;
|
|
108
|
+
/** World 的候选数据;Core 不解释其内容。 */
|
|
109
|
+
value: unknown;
|
|
110
|
+
project: CandidateProjector;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** 每批按来源和 project 函数分组调用一次,选择或合并候选。 */
|
|
114
|
+
export type CandidateProjector = (
|
|
115
|
+
candidates: readonly CandidateEventSpec[],
|
|
116
|
+
) => readonly CandidateProjection[];
|
|
117
|
+
|
|
118
|
+
export interface CandidatePushSpec {
|
|
119
|
+
/** 每条源事件单独归档。 */
|
|
120
|
+
sourceEvents: readonly CandidateSourceEvent[];
|
|
121
|
+
gateText: string;
|
|
122
|
+
value: unknown;
|
|
123
|
+
project: CandidateProjector;
|
|
124
|
+
origin?: EventOrigin;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** 总线项包括已落库事件、待渲染事件和候选。事件生成方式与 TriggerMode 相互独立。 */
|
|
128
|
+
export type WakeItem =
|
|
129
|
+
| { event: EventEnvelope; deferred?: undefined; candidate?: undefined }
|
|
130
|
+
| { event?: undefined; deferred: DeferredEventSpec; candidate?: undefined }
|
|
131
|
+
| { event?: undefined; deferred?: undefined; candidate: CandidateEventSpec };
|
|
132
|
+
|
|
133
|
+
export interface EventRangeQuery {
|
|
134
|
+
/** 游标区间(含端点) */
|
|
135
|
+
fromCursor?: number;
|
|
136
|
+
toCursor?: number;
|
|
137
|
+
/** ISO时间区间(含端点) */
|
|
138
|
+
fromTs?: string;
|
|
139
|
+
toTs?: string;
|
|
140
|
+
senderKey?: string;
|
|
141
|
+
source?: string;
|
|
142
|
+
origin?: EventOrigin;
|
|
143
|
+
/** 从区间末尾取至多 limit 条事件。 */
|
|
144
|
+
limit?: number;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
export interface EventGrepQuery {
|
|
148
|
+
/** 不区分大小写的纯文本包含匹配。 */
|
|
149
|
+
keyword: string;
|
|
150
|
+
/** 每个命中附带前后各 context 条事件。 */
|
|
151
|
+
context: number;
|
|
152
|
+
senderKey?: string;
|
|
153
|
+
source?: string;
|
|
154
|
+
origin?: EventOrigin;
|
|
155
|
+
fromTs?: string;
|
|
156
|
+
toTs?: string;
|
|
157
|
+
/** 最多返回的命中组数。 */
|
|
158
|
+
limit?: number;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export interface EventGrepHit {
|
|
162
|
+
hitCursor: number;
|
|
163
|
+
/** 命中事件与相邻上下文,按游标升序。 */
|
|
164
|
+
events: EventEnvelope[];
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export interface EventStoreReader {
|
|
168
|
+
get(cursor: number): EventEnvelope | undefined;
|
|
169
|
+
latestCursor(): number;
|
|
170
|
+
range(q: EventRangeQuery): EventEnvelope[];
|
|
171
|
+
/** 返回前 before 条、自身及后 after 条事件。 */
|
|
172
|
+
around(cursor: number, before: number, after: number): EventEnvelope[];
|
|
173
|
+
grep(q: EventGrepQuery): EventGrepHit[];
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export interface EventStore extends EventStoreReader {
|
|
177
|
+
/** 保存事件并分配游标;不触发投递。 */
|
|
178
|
+
append(e: Omit<EventEnvelope, 'cursor'>): EventEnvelope;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/** 发给模型 API 的函数工具定义。 */
|
|
182
|
+
export interface ToolSchema {
|
|
183
|
+
name: string;
|
|
184
|
+
description: string;
|
|
185
|
+
/** JSON Schema 对象 */
|
|
186
|
+
parameters: Record<string, unknown>;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 工具分类标签,供装配和上下文处理使用,不发送给模型。
|
|
191
|
+
* - read:不改变外部状态或持久状态;不保证无成本、无需排队或可并发执行。
|
|
192
|
+
* - write:修改 Memory 或 World 的内部记录。
|
|
193
|
+
* - speak:对外发言及对自身输出通道的控制。
|
|
194
|
+
* - act:改变外部状态,但不发言。
|
|
195
|
+
* - flow:控制 session、调度或认知流程。
|
|
196
|
+
* - snapshot:回执描述当前状态,可与其他标签并用;交接记录按工具名仅保留最后一次。
|
|
197
|
+
* 执行屏障和结束行为由 ToolDef 的字段声明。
|
|
198
|
+
*/
|
|
199
|
+
export type ToolTag = 'read' | 'write' | 'speak' | 'act' | 'flow' | 'snapshot';
|
|
200
|
+
|
|
201
|
+
export interface ToolCallContext {
|
|
202
|
+
/** Persona 定义的 session id;Core 仅用于查询、用量统计和日志关联。 */
|
|
203
|
+
role: string;
|
|
204
|
+
log: Logger;
|
|
205
|
+
/**
|
|
206
|
+
* 将工具执行期间取得的外部事件交给主循环,在本轮工具结果之后投递。
|
|
207
|
+
* 仅主循环提供;其他调用方可省略。
|
|
208
|
+
*/
|
|
209
|
+
queueExternalEvents?: (events: EventEnvelope[]) => void;
|
|
210
|
+
/** session 内的 tool_call id,用于关联输出流和后续结果事件。直接调用工具时可省略。 */
|
|
211
|
+
callId?: string;
|
|
212
|
+
/**
|
|
213
|
+
* 主循环轮次,进程内递增;同轮工具调用共用此值。
|
|
214
|
+
* 仅主循环提供;缺省时消费方不能施加基于轮次的限制。
|
|
215
|
+
*/
|
|
216
|
+
round?: number;
|
|
217
|
+
/**
|
|
218
|
+
* 宿主放弃本次调用时触发,跨进程代理超时也会触发。
|
|
219
|
+
* 耗时工具必须在提交外部副作用前检查此信号。
|
|
220
|
+
*/
|
|
221
|
+
signal?: AbortSignal;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* 工具结果;字符串返回值等价于 { text }。
|
|
226
|
+
* 附件随回执保存,句柄由 Core 分配,文本说明附在正文后。
|
|
227
|
+
*/
|
|
228
|
+
export interface ToolOutcome {
|
|
229
|
+
text: string;
|
|
230
|
+
blobs?: BlobInput[];
|
|
231
|
+
/** 执行失败;工具调用记录据此标记 failed。 */
|
|
232
|
+
failed?: true;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/** handler 异常由 Core 转为失败回执。 */
|
|
236
|
+
export interface ToolDef extends ToolSchema {
|
|
237
|
+
/** 装配层按标签筛选。空数组表示不分类,组装主工具表时会告警。 */
|
|
238
|
+
tags: readonly ToolTag[];
|
|
239
|
+
/**
|
|
240
|
+
* 模型读取本工具结果后才可继续执行动作。
|
|
241
|
+
* 同一条 assistant 输出中排在它后面的工具调用会被跳过。
|
|
242
|
+
*/
|
|
243
|
+
barrierAfter?: boolean;
|
|
244
|
+
/**
|
|
245
|
+
* 工具完成后结束本次唤醒,不再调用下一轮模型;期间到达的事件退回总线。
|
|
246
|
+
* 通常同时设置 barrierAfter,使同一条输出中的后续调用得到未执行回执。
|
|
247
|
+
*/
|
|
248
|
+
endsTurn?: boolean;
|
|
249
|
+
handler: (args: Record<string, unknown>, ctx: ToolCallContext) => Promise<string | ToolOutcome>;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** 供 Persona 绑定 handler 的工具声明。 */
|
|
253
|
+
export type ToolSpec = ToolSchema & { usage?: string; tags: readonly ToolTag[] };
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Persona 提供的 Memory 二进制存储。句柄以 mem: 开头,其余格式由后端定义。
|
|
257
|
+
* nameHint 仅供参考,最终句柄由后端决定。
|
|
258
|
+
*/
|
|
259
|
+
export interface BlobStore {
|
|
260
|
+
put(nameHint: string, bytes: Uint8Array, mime: string): string;
|
|
261
|
+
get(handle: string): { bytes: Uint8Array; mime: string } | null;
|
|
262
|
+
list(prefix?: string): Array<{ handle: string; mime: string; size: number }>;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* 已保存附件的引用。log: 由 Core 按内容分配,mem: 由 Persona 的 BlobStore 解析。
|
|
267
|
+
* 模型接受该 MIME 类型时附加内容,否则只发送 fallbackText。
|
|
268
|
+
*/
|
|
269
|
+
export interface BlobRef {
|
|
270
|
+
handle: string;
|
|
271
|
+
mime: string;
|
|
272
|
+
name?: string;
|
|
273
|
+
/** 附件在 session 中的文本说明。 */
|
|
274
|
+
fallbackText: string;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* 待保存的附件;可提供新字节或已有的 log:/mem: 句柄。
|
|
279
|
+
* 两种形式均须提供 fallbackText,供上下文使用。
|
|
280
|
+
*/
|
|
281
|
+
export type BlobInput =
|
|
282
|
+
| { bytes: Uint8Array; mime: string; name?: string; fallbackText: string }
|
|
283
|
+
| { handle: string; fallbackText: string };
|
|
284
|
+
|
|
285
|
+
/** 上下文记录中的事件索引;正文位置为 content.slice(start, start + chars)。 */
|
|
286
|
+
export interface FrameEventRef {
|
|
287
|
+
cursor: number;
|
|
288
|
+
ts: string;
|
|
289
|
+
type: string;
|
|
290
|
+
source: string;
|
|
291
|
+
tags?: readonly EventTag[];
|
|
292
|
+
start: number;
|
|
293
|
+
chars: number;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
export interface ModelSpec {
|
|
297
|
+
model: string;
|
|
298
|
+
/** 是否请求推理;启用时强度由 reasoningEffort 指定。具体请求字段由 provider 映射。 */
|
|
299
|
+
thinking: boolean;
|
|
300
|
+
/**
|
|
301
|
+
* 仅 thinking=true 时有效;缺省使用端点默认值。
|
|
302
|
+
* provider 声明 reasoningTiers 时须使用表内值,开放模块接受任意非空字符串。
|
|
303
|
+
*/
|
|
304
|
+
reasoningEffort?: string;
|
|
305
|
+
/** 缺省时不发送温度参数,使用端点默认值。 */
|
|
306
|
+
temperature?: number;
|
|
307
|
+
maxTokens?: number;
|
|
308
|
+
/**
|
|
309
|
+
* 手动设置的上下文窗口上限(token);与 provider 探测值取较小者。
|
|
310
|
+
* 两者均未知时,Core 不按窗口大小裁剪上下文。
|
|
311
|
+
*/
|
|
312
|
+
contextWindow?: number;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* provider 的推理选项,将 thinking 和 reasoningEffort 合并为一次选择。
|
|
317
|
+
* 空选项表允许操作者填写任意非空 effort;provider 可提供建议值。
|
|
318
|
+
*/
|
|
319
|
+
export interface ReasoningTier {
|
|
320
|
+
/** provider 内唯一的选项 id。 */
|
|
321
|
+
id: string;
|
|
322
|
+
label: string;
|
|
323
|
+
thinking: boolean;
|
|
324
|
+
effort?: string;
|
|
325
|
+
/** 该选项的使用限制或说明。 */
|
|
326
|
+
note?: string;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
/** provider 支持的 service_tier 选项。 */
|
|
330
|
+
export interface ServiceTier {
|
|
331
|
+
/** 发送给端点的 service_tier 值。 */
|
|
332
|
+
id: string;
|
|
333
|
+
label: string;
|
|
334
|
+
/** 该选项的使用限制或说明。 */
|
|
335
|
+
note?: string;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
export interface LLMUsage {
|
|
339
|
+
promptTokens: number;
|
|
340
|
+
completionTokens: number;
|
|
341
|
+
cacheHitTokens: number;
|
|
342
|
+
cacheMissTokens: number;
|
|
343
|
+
reasoningTokens?: number;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** 单次模型调用的持久记录,保存于 data/usage.jsonl。 */
|
|
347
|
+
export interface UsageRecord {
|
|
348
|
+
recordId?: string;
|
|
349
|
+
version?: 2;
|
|
350
|
+
run?: string;
|
|
351
|
+
/** 主循环轮次;fork 和 World 自报用量不提供。 */
|
|
352
|
+
round?: number;
|
|
353
|
+
attempt?: ProviderAttempt;
|
|
354
|
+
/** World 自报费用,不归属于当前 provider。 */
|
|
355
|
+
charges?: import('./generation.ts').Charge[];
|
|
356
|
+
/** 部署时区的 ISO 时间戳,格式由 nowIso 定义。 */
|
|
357
|
+
ts: string;
|
|
358
|
+
/** 常驻 session 使用声明 id;fork 实例 id 由声明 id 和序号派生。 */
|
|
359
|
+
sessionId: string;
|
|
360
|
+
/** Persona 定义的 session 声明 id,用于分类统计。 */
|
|
361
|
+
role: string;
|
|
362
|
+
label: string;
|
|
363
|
+
model: string;
|
|
364
|
+
promptTokens: number;
|
|
365
|
+
completionTokens: number;
|
|
366
|
+
cacheHitTokens: number;
|
|
367
|
+
cacheMissTokens: number;
|
|
368
|
+
reasoningTokens: number;
|
|
369
|
+
/**
|
|
370
|
+
* 缺省表示成功。failed 表示无可用结果;discarded 表示已返回结果因关机或运行代次变更而丢弃。
|
|
371
|
+
* 两者的实际用量均计入总消耗,并在 UsageAggregate.failed 中单列。
|
|
372
|
+
*/
|
|
373
|
+
outcome?: 'failed' | 'discarded';
|
|
374
|
+
/** 请求前缀 SHA 指纹的前 12 位,用于判断前缀是否变化。 */
|
|
375
|
+
prefixHash?: string;
|
|
376
|
+
/** 失败调用从开流到失败的耗时(ms)。 */
|
|
377
|
+
failedAfterMs?: number;
|
|
378
|
+
/** 失败调用的上游请求 id。 */
|
|
379
|
+
requestId?: string;
|
|
380
|
+
/** 失败调用的 LLMError.status;0 表示流内失败。 */
|
|
381
|
+
status?: number;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/** 单个 session 的轮数上限:soft 追加提示,hard 结束循环。 */
|
|
385
|
+
export interface RoundCaps {
|
|
386
|
+
soft: number;
|
|
387
|
+
hard: number;
|
|
388
|
+
/** 第 soft 轮最后一条工具回执末尾的提示,由 Persona 提供;缺省或返回 null 时不追加。 */
|
|
389
|
+
softHint?: () => string | null;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Persona 注册的 session 声明。id 对 Core 不透明。
|
|
394
|
+
* rounds 和 tools 在使用时读取,支持配置热改;模型配置来自当前 provider。
|
|
395
|
+
*/
|
|
396
|
+
export interface SessionDecl {
|
|
397
|
+
id: string;
|
|
398
|
+
/** 控制台与日志中的显示名。 */
|
|
399
|
+
label: string;
|
|
400
|
+
rounds: () => RoundCaps;
|
|
401
|
+
/** 是否持久化;false 用于临时 fork。 */
|
|
402
|
+
persistent: boolean;
|
|
403
|
+
receivesEvents: boolean;
|
|
404
|
+
/**
|
|
405
|
+
* 外部事件在上下文中的位置,运行中不变。
|
|
406
|
+
* - tool(默认):进入保留的 external_event_frame 调用/回执对;模型生成的同名调用会被丢弃。
|
|
407
|
+
* - user:进入 user 消息,排在内部文本之后。
|
|
408
|
+
* 内部文本由 Persona.onDelivery 注入;Core 不添加说明。
|
|
409
|
+
*/
|
|
410
|
+
eventDelivery?: 'tool' | 'user';
|
|
411
|
+
/** 生成期间向此接收器转发输出增量;运行中不变。 */
|
|
412
|
+
outputTap?: OutputTap;
|
|
413
|
+
tools: () => ToolDef[];
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/** 接收模型输出流;Core 不解释增量中的语义。 */
|
|
417
|
+
export interface OutputTap {
|
|
418
|
+
onEvent(event: StreamEvent): void;
|
|
419
|
+
/** 返回 true 表示增量已产生外部输出,此后主循环不再允许抢占该轮。 */
|
|
420
|
+
externalizes?(event: StreamEvent): boolean;
|
|
421
|
+
onRoundEnd?(): void;
|
|
422
|
+
onAbort?(reason: string): void;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
export interface ForkOptions {
|
|
426
|
+
/** 已声明的 session id;默认工具和轮数上限来自该声明,模型来自当前 provider。 */
|
|
427
|
+
id: string;
|
|
428
|
+
/** 完整的初始上下文,由 Persona 构造。 */
|
|
429
|
+
messages: ContextRecord[];
|
|
430
|
+
tools?: ToolDef[];
|
|
431
|
+
/** 每次工具执行后检查;返回 true 时结束循环。 */
|
|
432
|
+
stopWhen?: () => boolean;
|
|
433
|
+
/** 追加到收尾轮工具结果后的提示;轮次按 rounds().soft 设置,限制在 1 至 max(1, rounds().hard - 1)。 */
|
|
434
|
+
wrapUpHint?: string;
|
|
435
|
+
/** 循环达到硬上限时,追加到返回正文末尾的说明。 */
|
|
436
|
+
capNote?: string;
|
|
437
|
+
/**
|
|
438
|
+
* 模型输出未满足要求时的补充提示 fallback。
|
|
439
|
+
* 循环结束后,若 stopWhen 未满足且 when(最后正文) 为真,追加 user 提示并再运行一轮。
|
|
440
|
+
*/
|
|
441
|
+
nudge?: { when: (lastContent: string) => boolean; message: string };
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/** Persona 可查询的 session 状态。 */
|
|
445
|
+
export interface SessionInfo {
|
|
446
|
+
id: string;
|
|
447
|
+
/** 此声明当前运行的实例数。 */
|
|
448
|
+
running: number;
|
|
449
|
+
/** 接收事件的常驻 session 的请求上下文快照;其他 session 返回 null。 */
|
|
450
|
+
snapshot: ContextRecord[] | null;
|
|
451
|
+
/**
|
|
452
|
+
* 下一次请求的输入 token 估计:最近的上游计数加新增内容估算;无上游计数时估算全部内容。
|
|
453
|
+
* 仅接收事件的常驻 session 提供;其他 session 为 null。
|
|
454
|
+
*/
|
|
455
|
+
estTokens: number | null;
|
|
456
|
+
/**
|
|
457
|
+
* 输入 token 上限:模型上下文窗口减生成上限,最低为 0;超过时 Core 强制交接。
|
|
458
|
+
* 窗口未知或非接收事件的常驻 session 时为 null。
|
|
459
|
+
*/
|
|
460
|
+
hardTokens: number | null;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
/** Persona 返回的上下文交接结果。 */
|
|
464
|
+
export interface ContextHandoffResult {
|
|
465
|
+
/** 不含 system 前缀的保留上下文;null 表示由 Core 从当前上下文选择。 */
|
|
466
|
+
tail: ContextRecord[] | null;
|
|
467
|
+
/**
|
|
468
|
+
* 声明返回内容可由 Core 按剩余 token 预算裁剪,预算扣除 system 前缀与合成开头。
|
|
469
|
+
* 未设置此项时,超限内容仍会被裁剪,但同时记录告警。
|
|
470
|
+
*/
|
|
471
|
+
trim?: boolean;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
/** Persona 访问 Core 的接口。 */
|
|
475
|
+
export interface CoreApi {
|
|
476
|
+
/** 向接收事件的 session 注入内部事件;立即落库,正文由 Persona 提供。 */
|
|
477
|
+
injectInternal(text: string, kind?: string): void;
|
|
478
|
+
/** 注入内部事件,在投递时调用 render;渲染与持久化规则见 DeferredEventSpec。 */
|
|
479
|
+
injectDeferred(kind: string, render: () => string | null | Promise<string | null>): void;
|
|
480
|
+
/**
|
|
481
|
+
* 将 Persona 提供的正文作为外部事件投递,source=persona、origin=external。
|
|
482
|
+
* 正文位置由 SessionDecl.eventDelivery 决定。
|
|
483
|
+
*/
|
|
484
|
+
injectExternal(text: string, kind?: string): void;
|
|
485
|
+
/** 在安全轮次边界请求上下文交接;已有请求待处理时返回 false。 */
|
|
486
|
+
requestContextHandoff(): boolean;
|
|
487
|
+
/** 运行临时 session 的工具循环,返回最后一段正文。 */
|
|
488
|
+
spawnFork(opts: ForkOptions): Promise<string>;
|
|
489
|
+
sessionInfo(id: string): SessionInfo;
|
|
490
|
+
/** 记录用量并支持重试的模型客户端。 */
|
|
491
|
+
llm: ResponseClient;
|
|
492
|
+
/** 跨重启恢复的定时器;载荷对 Core 不透明。 */
|
|
493
|
+
timers: TimersApi;
|
|
494
|
+
/** 按 FIFO 顺序扣留和放行唤醒项;安装、解除与到期策略由 Persona 定义。 */
|
|
495
|
+
deliveryGate: DeliveryGateApi;
|
|
496
|
+
/**
|
|
497
|
+
* 返回共享的 Persona 状态对象;修改后须调用 savePersonaState 原子保存。
|
|
498
|
+
* Core 不解释其内容。
|
|
499
|
+
*/
|
|
500
|
+
personaState(): Record<string, unknown>;
|
|
501
|
+
savePersonaState(): void;
|
|
502
|
+
/** 当前主 session 工具表中带有指定标签的工具名。 */
|
|
503
|
+
toolsTagged(tag: ToolTag): ReadonlySet<string>;
|
|
504
|
+
/** 解析 log:/mem: 句柄;规则与 WorldHost.blob 相同。 */
|
|
505
|
+
blob(handle: string): { bytes: Uint8Array; mime: string } | null;
|
|
506
|
+
log: Logger;
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/** 持久定时器条目;payload 对 Core 不透明。 */
|
|
510
|
+
export interface TimerEntry {
|
|
511
|
+
id: string;
|
|
512
|
+
/** 到期时刻;保留设定方提供的 ISO 字符串。 */
|
|
513
|
+
atIso: string;
|
|
514
|
+
payload: Record<string, unknown>;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
/** 跨重启恢复的定时器。 */
|
|
518
|
+
export interface TimersApi {
|
|
519
|
+
set(atIso: string, payload?: Record<string, unknown>): { ok: true; id: string } | { ok: false; error: string };
|
|
520
|
+
cancel(id: string): boolean;
|
|
521
|
+
list(): ReadonlyArray<TimerEntry>;
|
|
522
|
+
/** 清除全部条目,返回删除数量。 */
|
|
523
|
+
clearAll(): number;
|
|
524
|
+
/** 注册唯一的到期处理器;调用时该条目已从表中移除。 */
|
|
525
|
+
onDue(handler: (entry: TimerEntry) => void): void;
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
/**
|
|
529
|
+
* 扣留全部唤醒项;解除、关键词命中或积压溢出时,可按 FIFO 顺序放行整批。
|
|
530
|
+
* 关键词是否放行由回调决定;溢出时先授予整批放行许可,再调用回调。操作者暂停的优先级更高。
|
|
531
|
+
*/
|
|
532
|
+
export interface DeliveryGate {
|
|
533
|
+
id: string;
|
|
534
|
+
/** 区分大小写的字面子串;匹配外部事件正文或候选 gateText。 */
|
|
535
|
+
keyword?: string;
|
|
536
|
+
/** 被扣留的外部即时事件和候选数超过此值时,通知回调并临时放行一批;不计 piggyback 项。 */
|
|
537
|
+
overflowLimit: number;
|
|
538
|
+
onKeyword: () => void;
|
|
539
|
+
onOverflow: () => void;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
export interface DeliveryGateApi {
|
|
543
|
+
/** 安装或替换闸门;此前无闸门时,已积压内容获准放行一次。 */
|
|
544
|
+
set(gate: DeliveryGate): void;
|
|
545
|
+
/** 只解除匹配 id 的闸门;deliverQueued=false 时不立即投递积压。 */
|
|
546
|
+
clear(id: string, deliverQueued?: boolean): boolean;
|
|
547
|
+
isBlocked(): boolean;
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
/** 运行时查询当前模型的名称、MIME 支持和上下文窗口;配置热改后读取新值。 */
|
|
551
|
+
export interface ModelFacts {
|
|
552
|
+
model(): string;
|
|
553
|
+
accepts(mime: string): boolean;
|
|
554
|
+
/** provider 探测值与手动配置取较小者,单位 token;两者均未知时返回 undefined。 */
|
|
555
|
+
contextWindow(): number | undefined;
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/** 事件的投递触发方式,由产生方选择;与事件的渲染时机独立。 */
|
|
559
|
+
export type TriggerMode =
|
|
560
|
+
/** 立即投递整批,并请求取消尚未产生外部输出的在途模型轮;是否可取消由主循环判断。 */
|
|
561
|
+
| 'preempt'
|
|
562
|
+
/** 立即投递整批积压。 */
|
|
563
|
+
| 'flush'
|
|
564
|
+
/** 按 quietGapMs、minBatchAgeMs、maxBatchAgeMs 和 maxBatchSize 合批。 */
|
|
565
|
+
| 'debounce'
|
|
566
|
+
/** 只入队,不触发投递或刷新计时器,随下一批唤醒一起投递。 */
|
|
567
|
+
| 'piggyback';
|
|
568
|
+
|
|
569
|
+
export interface PushOptions {
|
|
570
|
+
/** false 表示仅落库,不投递;默认 true。 */
|
|
571
|
+
deliver?: boolean;
|
|
572
|
+
/** 缺省时外部事件使用 debounce,内部事件使用 flush。 */
|
|
573
|
+
trigger?: TriggerMode;
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* World 向 Persona 请求认知任务。World 提供任务说明和本 World 的工具名;
|
|
578
|
+
* Persona 决定上下文、轮数、token 预算、超时及是否受理,模型配置来自当前 provider。
|
|
579
|
+
* Core 校验请求工具、转交请求方身份并统计并发数。World 自带模型的调用不经过此接口。
|
|
580
|
+
*/
|
|
581
|
+
export interface CognitionRequest {
|
|
582
|
+
/** 任务说明;不指定身份、模型或执行预算。 */
|
|
583
|
+
brief: string;
|
|
584
|
+
/**
|
|
585
|
+
* 仅可引用请求方 World.tools() 中的工具;越界请求以 error 返回,不交给 Persona。
|
|
586
|
+
* 缺省为空;Persona 仍可自行提供 Memory 等自有工具。
|
|
587
|
+
*/
|
|
588
|
+
tools?: string[];
|
|
589
|
+
/** 供 Persona 参考的任务规模;不强制执行。 */
|
|
590
|
+
hint?: {
|
|
591
|
+
/** 建议的工具循环轮数。 */
|
|
592
|
+
rounds?: number;
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
/** 认知结果或失败原因;World 应将结果如实报告给模型。 */
|
|
597
|
+
export type CognitionResult = { text: string } | { error: string };
|
|
598
|
+
|
|
599
|
+
/** World 可用的认知接口;Persona 未实现或关闭此能力时不可用。 */
|
|
600
|
+
export interface CognitionHost {
|
|
601
|
+
/** 所有失败均通过 error 返回,包括校验失败和 Persona 实现抛错。 */
|
|
602
|
+
request(req: CognitionRequest): Promise<CognitionResult>;
|
|
603
|
+
}
|
|
604
|
+
|
|
605
|
+
/** Core 提供的请求方身份、已校验工具和并发数。 */
|
|
606
|
+
export interface CognitionContext {
|
|
607
|
+
worldId: string;
|
|
608
|
+
/** req.tools 对应的已校验定义;未指定工具时为空。 */
|
|
609
|
+
tools: ToolDef[];
|
|
610
|
+
/** 该 World 的在途请求数,包含本次请求;并发限制由 Persona 决定。 */
|
|
611
|
+
running: number;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/** Persona 的认知实现;未提供时 WorldHost 不提供 cognition。 */
|
|
615
|
+
export interface PersonaCognition {
|
|
616
|
+
/** 每次读取 WorldHost.cognition 时查询;false 时返回 undefined,缺省为启用。 */
|
|
617
|
+
enabled?(): boolean;
|
|
618
|
+
/**
|
|
619
|
+
* 处理请求;上下文、预算和引导语由 Persona 定义。
|
|
620
|
+
* Core 将抛出的异常转为 error 结果。
|
|
621
|
+
*/
|
|
622
|
+
request(req: CognitionRequest, ctx: CognitionContext): Promise<CognitionResult>;
|
|
623
|
+
}
|
|
624
|
+
|
|
625
|
+
/** Core 提供给 World 的宿主接口;事件写入、队列消费和认知请求支持跨进程异步实现。 */
|
|
626
|
+
export interface WorldHost {
|
|
627
|
+
/**
|
|
628
|
+
* 保存事件并分配游标,按 opts 投递;返回已保存记录。origin 缺省为 external。
|
|
629
|
+
* internal 仅用于内部机制或已认证操作员通道,不得用于来源未经认证的外部内容。
|
|
630
|
+
*/
|
|
631
|
+
pushEvent(
|
|
632
|
+
e: Omit<EventEnvelope, 'cursor' | 'origin' | 'contextDelivery' | 'blobs'> & { origin?: EventOrigin; blobs?: BlobInput[] },
|
|
633
|
+
opts?: PushOptions,
|
|
634
|
+
): Promise<EventEnvelope>;
|
|
635
|
+
/**
|
|
636
|
+
* 渲染与持久化规则见 DeferredEventSpec;origin 缺省为 external。
|
|
637
|
+
* 隐藏 World 或已失效宿主的项直接丢弃;其他项按 trigger 入队。
|
|
638
|
+
*/
|
|
639
|
+
pushDeferred(
|
|
640
|
+
e: Pick<DeferredEventSpec, 'type' | 'senderKey' | 'meta' | 'tags' | 'render'> & { origin?: EventOrigin },
|
|
641
|
+
opts?: { trigger?: TriggerMode },
|
|
642
|
+
): void;
|
|
643
|
+
/**
|
|
644
|
+
* 立即归档原始事件,将候选加入总线。project 在投递时处理同来源、同函数的整批候选。
|
|
645
|
+
* 生成的事件在当前批的即时事件之后保存并投递。
|
|
646
|
+
*/
|
|
647
|
+
pushCandidate?(
|
|
648
|
+
spec: CandidatePushSpec,
|
|
649
|
+
opts?: { trigger?: TriggerMode },
|
|
650
|
+
): Promise<readonly EventEnvelope[]>;
|
|
651
|
+
store: EventStoreReader;
|
|
652
|
+
/**
|
|
653
|
+
* 按 filter 取走尚未投递的外部即时事件,保持原序且不等待。
|
|
654
|
+
* 取走后不会在后续批次重复投递。
|
|
655
|
+
*/
|
|
656
|
+
drainPendingEvents(filter: (e: EventEnvelope) => boolean): Promise<EventEnvelope[]>;
|
|
657
|
+
/** 当前模型的能力;渲染层据 MIME 支持决定是否附加二进制内容。 */
|
|
658
|
+
modelFacts: ModelFacts;
|
|
659
|
+
/**
|
|
660
|
+
* 解析 log:/mem: 句柄;无效或内容不存在时返回 null。
|
|
661
|
+
* 保存新附件须通过事件或工具回执的 blobs 字段。
|
|
662
|
+
*/
|
|
663
|
+
blob(handle: string): { bytes: Uint8Array; mime: string } | null;
|
|
664
|
+
/**
|
|
665
|
+
* 记录 World 自有模型的用量,写入 session 统计和 usage.jsonl。
|
|
666
|
+
* 仅记录主动上报的用量;Core 不管理 World 自有模型的运行时。
|
|
667
|
+
*/
|
|
668
|
+
reportUsage(usage: LLMUsage, opts?: { model?: string; label?: string; charges?: import('./generation.ts').Charge[] }): void;
|
|
669
|
+
/**
|
|
670
|
+
* 最近 withinMs 毫秒内 Core 模型调用失败或流中断的次数。
|
|
671
|
+
* 未接入此查询的宿主可省略。
|
|
672
|
+
*/
|
|
673
|
+
llmStalls?(withinMs: number): Promise<number>;
|
|
674
|
+
/** Persona 未实现、全局关闭或宿主未接入时为 undefined。 */
|
|
675
|
+
cognition?: CognitionHost;
|
|
676
|
+
log: Logger;
|
|
677
|
+
}
|
|
678
|
+
|
|
679
|
+
/** 可清除的磁盘或内存存储单元。 */
|
|
680
|
+
export interface StoragePart {
|
|
681
|
+
key: string;
|
|
682
|
+
label: string;
|
|
683
|
+
kind: 'disk' | 'memory';
|
|
684
|
+
/** 磁盘位置说明;内存存储省略。 */
|
|
685
|
+
location?: string;
|
|
686
|
+
/** 清除不可恢复的记录时设置;控制台要求额外确认。 */
|
|
687
|
+
danger?: boolean;
|
|
688
|
+
/** 清除后果。 */
|
|
689
|
+
note?: string;
|
|
690
|
+
/** 批量清除时的升序执行顺序,默认 0;session 使用 10,清除后重建前缀。 */
|
|
691
|
+
order?: number;
|
|
692
|
+
/** 当前存储规模(条数或大小)的实时说明。 */
|
|
693
|
+
stat(): string;
|
|
694
|
+
/** 返回清除结果;抛错表示失败。 */
|
|
695
|
+
clear(): Promise<string> | string;
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/** 存储项的归属。装配层按声明来源盖章,控制台按它把项分到 Core、Persona、Memory 与各 World 的页面。 */
|
|
699
|
+
export type StorageOwner = 'core' | 'persona' | 'memory' | `world:${string}`;
|
|
700
|
+
|
|
701
|
+
export interface OwnedStoragePart extends StoragePart {
|
|
702
|
+
owner: StorageOwner;
|
|
703
|
+
}
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* 须与 src/web/shared/console-protocol.ts 的 ConsolePanelDecl 保持结构和字段语义一致。
|
|
707
|
+
* 类型分别定义以保持 Core 的依赖方向。id 在页面内唯一。
|
|
708
|
+
*/
|
|
709
|
+
export interface WorldPanelDecl {
|
|
710
|
+
/** 本页内唯一,使用 [a-z0-9-],不带 World 前缀。 */
|
|
711
|
+
id: string;
|
|
712
|
+
title: string;
|
|
713
|
+
description?: string;
|
|
714
|
+
/** 允许经 HTTP GET 调用的方法;省略时本面板只接受 POST。 */
|
|
715
|
+
getMethods?: readonly string[];
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
/** 提示词模板占位符;控制台展示声明及当前展开值。 */
|
|
719
|
+
export interface PromptVarDecl {
|
|
720
|
+
/** 包含命名空间的占位符名称。 */
|
|
721
|
+
name: string;
|
|
722
|
+
/** 占位符对应的运行时内容。 */
|
|
723
|
+
description: string;
|
|
724
|
+
/**
|
|
725
|
+
* 值是否可能换行;控制台据此建议独占一行。
|
|
726
|
+
* 模板渲染不对续行追加缩进或列表符号。
|
|
727
|
+
*/
|
|
728
|
+
multiline?: boolean;
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
/**
|
|
732
|
+
* 可编辑的提示词模板;框架负责读取、revision 校验和原子保存。
|
|
733
|
+
* 绝对路径不发送到浏览器。
|
|
734
|
+
*/
|
|
735
|
+
export interface PromptDocDecl {
|
|
736
|
+
/** 全局唯一,惯例为 worlds.<World>.<名> 或 persona.<名>。 */
|
|
737
|
+
key: string;
|
|
738
|
+
title: string;
|
|
739
|
+
description: string;
|
|
740
|
+
/** 读取路径;文件不存在时读作空,首次保存时创建。 */
|
|
741
|
+
path: string;
|
|
742
|
+
/**
|
|
743
|
+
* 部署覆盖文件的写入路径;声明方通过 path 指定当前读取的模板。
|
|
744
|
+
* 缺省时读写均使用 path。
|
|
745
|
+
*/
|
|
746
|
+
deploymentPath?: string;
|
|
747
|
+
/**
|
|
748
|
+
* envPrompt 为 World 环境模板,prefix 为 Persona 总装模板。
|
|
749
|
+
* 未指定时仅作为可编辑文件。
|
|
750
|
+
*/
|
|
751
|
+
role?: 'envPrompt' | 'prefix';
|
|
752
|
+
/** 模板支持的占位符,须与运行时提供的变量一致。 */
|
|
753
|
+
vars?: PromptVarDecl[];
|
|
754
|
+
}
|
|
755
|
+
|
|
756
|
+
/**
|
|
757
|
+
* World 自报的组件可用状态。
|
|
758
|
+
* - online:功能可用,包括当前空闲。
|
|
759
|
+
* - loading:正在连接、启动或预热。
|
|
760
|
+
* - error:已启用但无法工作。
|
|
761
|
+
* - offline:未启用或缺少配置。
|
|
762
|
+
* 控制台按 state 显示,不从其他状态标签推断。
|
|
763
|
+
*/
|
|
764
|
+
export interface WorldLamp {
|
|
765
|
+
/** 组件名称,用于悬停说明。 */
|
|
766
|
+
label: string;
|
|
767
|
+
state: 'online' | 'loading' | 'error' | 'offline';
|
|
768
|
+
/** 悬停时显示的状态详情。 */
|
|
769
|
+
hint?: string;
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/** 每个 World 在导航中最多显示的状态灯数量,超出部分截断。 */
|
|
773
|
+
export const MODULE_LAMP_MAX = 7;
|
|
774
|
+
|
|
775
|
+
/** World 提供的控制台页面内容,按声明的面板 id 路由。 */
|
|
776
|
+
export interface WorldConsoleDecl {
|
|
777
|
+
/**
|
|
778
|
+
* 按 console(language) 生成的显示名;缺省使用 WorldDefinition.label。
|
|
779
|
+
* 装配层状态和操作结果仍使用定义中的 label。
|
|
780
|
+
*/
|
|
781
|
+
label?: string;
|
|
782
|
+
/** 按声明顺序显示,至多 MODULE_LAMP_MAX 个;缺省时不显示。 */
|
|
783
|
+
lamps?: WorldLamp[];
|
|
784
|
+
badges?: Array<{ label: string; value: string | number; tone?: 'on' | 'off' | 'plain' }>;
|
|
785
|
+
panels?: WorldPanelDecl[];
|
|
786
|
+
/**
|
|
787
|
+
* 处理本页的 panel/method/args 请求,默认返回 JSON。
|
|
788
|
+
* 返回 { $binary: { mime, base64 } } 时响应为二进制;未实现时返回 503。
|
|
789
|
+
*/
|
|
790
|
+
invoke?(panel: string, method: string, args: unknown[]): Promise<unknown>;
|
|
791
|
+
/**
|
|
792
|
+
* 每条推送连接调用一次;多连接广播由 World 实现。
|
|
793
|
+
* 未实现时拒绝对应的 WebSocket 握手。
|
|
794
|
+
*/
|
|
795
|
+
stream?(panel: string, socket: WorldStreamSocket): void;
|
|
796
|
+
/** World 的提示词模板;role=envPrompt 的模板用 envPromptVars() 渲染。 */
|
|
797
|
+
promptDocs?: PromptDocDecl[];
|
|
798
|
+
/** World 的可清除存储,列在本 World 页的数据页签。 */
|
|
799
|
+
storage?: StoragePart[];
|
|
800
|
+
/**
|
|
801
|
+
* 独立页面链接;inheritTheme 表示打开时附带当前主题快照。
|
|
802
|
+
* 控制台不解释 href 的路径语义。
|
|
803
|
+
*/
|
|
804
|
+
links?: Array<{ label: string; href: string; inheritTheme?: boolean }>;
|
|
805
|
+
config?: ConfigGroup[];
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
export type ShutdownVerificationStatus = 'verified-ended' | 'still-live' | 'unknown';
|
|
809
|
+
|
|
810
|
+
/** World 在 stop() 内完成并缓存的外部状态检查;汇总层不重新访问外部系统。 */
|
|
811
|
+
export interface ShutdownExternalCheck {
|
|
812
|
+
key: string;
|
|
813
|
+
label: string;
|
|
814
|
+
status: ShutdownVerificationStatus;
|
|
815
|
+
detail: string;
|
|
816
|
+
/** 未验证结束时由操作者执行的动作。 */
|
|
817
|
+
manualAction: string;
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
/** World 的环境描述、事件和工具契约。 */
|
|
821
|
+
export interface World {
|
|
822
|
+
id: string;
|
|
823
|
+
/**
|
|
824
|
+
* 环境模板的当前变量值;文本来自 role=envPrompt 的 promptDocs 模板。
|
|
825
|
+
* 返回 null 时省略该 World 的环境段;返回空对象时保留无变量的模板全文。
|
|
826
|
+
* 每次前缀重建(包括上下文交接)时重新调用。
|
|
827
|
+
*/
|
|
828
|
+
envPromptVars(): Record<string, string> | null | Promise<Record<string, string> | null>;
|
|
829
|
+
/** 工具定义和用法由 World 提供;使用时机与跨工具指导写入环境提示词。 */
|
|
830
|
+
tools(): ToolDef[];
|
|
831
|
+
/**
|
|
832
|
+
* 每次控制台请求按浏览器 language 生成声明和响应文案,省略时为中文。
|
|
833
|
+
* 语言不缓存,不影响发给模型的文本;未实现时仅显示通用信息。
|
|
834
|
+
*/
|
|
835
|
+
console?(language?: Language): WorldConsoleDecl;
|
|
836
|
+
/** 主 session 的输出流接收器;装配层合并已挂载 World 的接收器,随挂载变化更新。 */
|
|
837
|
+
outputTap?(): OutputTap;
|
|
838
|
+
/** 挂载时接收宿主并开始连接平台、推送事件。 */
|
|
839
|
+
start(host: WorldHost): Promise<void>;
|
|
840
|
+
stop(): Promise<void>;
|
|
841
|
+
/**
|
|
842
|
+
* 新前缀和保留上下文已装入 session,总线尚未恢复投递时调用。
|
|
843
|
+
* 此时推送的事件进入新 session 的第一批;隐藏 World 不接收通知。
|
|
844
|
+
*/
|
|
845
|
+
onHandoffEnded?(): void;
|
|
846
|
+
/**
|
|
847
|
+
* assistant 自然结束、LLM 失败或轮数达到硬上限时调用,用于清理本轮暂态。
|
|
848
|
+
* 隐藏 World 不接收通知。
|
|
849
|
+
*/
|
|
850
|
+
onTurnEnded?(): void;
|
|
851
|
+
/**
|
|
852
|
+
* stop() 完成后返回外部状态检查的同步只读快照。
|
|
853
|
+
* 网络检查须在 stop() 的既有期限内完成并缓存。
|
|
854
|
+
*/
|
|
855
|
+
shutdownVerification?(): readonly ShutdownExternalCheck[];
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
/** text 按序拼入 system 前缀;title 仅用于显示。 */
|
|
859
|
+
export interface PrefixSegment {
|
|
860
|
+
title: string;
|
|
861
|
+
text: string;
|
|
862
|
+
/** 来源模板的 promptDoc key;缺省时控制台只读。 */
|
|
863
|
+
sourceKey?: string;
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
export interface WorldPrefixContext {
|
|
867
|
+
id: string;
|
|
868
|
+
/** 框架使用模板和变量渲染的环境描述。 */
|
|
869
|
+
envPrompt: string;
|
|
870
|
+
/** 来源模板 key,传递至 PrefixSegment.sourceKey。 */
|
|
871
|
+
sourceKey?: string;
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
/** Core 在构造 system 前缀时提供的上下文。 */
|
|
875
|
+
export interface SystemPrefixContext {
|
|
876
|
+
now: Date;
|
|
877
|
+
timezone: string;
|
|
878
|
+
/** 按 World id 排序 */
|
|
879
|
+
worlds: WorldPrefixContext[];
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
export type SessionOpeningReason = 'new' | 'restarted' | 'cleared';
|
|
883
|
+
|
|
884
|
+
export interface MemoryAssemblyContext {
|
|
885
|
+
now: Date;
|
|
886
|
+
timezone: string;
|
|
887
|
+
}
|
|
888
|
+
|
|
889
|
+
/** 一个 World 的挂载或可见状态被装配层改变。`label` 是控制台显示名。 */
|
|
890
|
+
export type WorldLifecycleEvent =
|
|
891
|
+
| { kind: 'mounted' | 'unmounted' | 'restarted'; id: string; label: string }
|
|
892
|
+
| { kind: 'visibility'; id: string; label: string; visible: boolean };
|
|
893
|
+
|
|
894
|
+
/**
|
|
895
|
+
* Persona 定义上下文内容、session 和 Memory 操作。Core 在生命周期边界调用可选钩子。
|
|
896
|
+
* 文本通过 CoreApi 的注入方法或有文本返回值的钩子提供;Core 不生成语义内容。
|
|
897
|
+
*/
|
|
898
|
+
export interface Persona {
|
|
899
|
+
/** system 前缀的有序段;Core 按序拼接 text。 */
|
|
900
|
+
systemSegments(ctx: SystemPrefixContext): Promise<PrefixSegment[]>;
|
|
901
|
+
/** 控制台预览使用的模板变量当前值;未实现时仅显示占位符声明。 */
|
|
902
|
+
promptVarValues?(ctx: { now: Date; timezone: string }):
|
|
903
|
+
| Record<string, string>
|
|
904
|
+
| Promise<Record<string, string>>;
|
|
905
|
+
/** session 新建、重启恢复或清空后调用;可通过 injectInternal 注入开场文本。 */
|
|
906
|
+
onOpening?(ctx: { reason: SessionOpeningReason }): void;
|
|
907
|
+
/**
|
|
908
|
+
* 一批事件已渲染并分配游标、尚未进入上下文时调用;events 按投递序包含内部和外部事件。
|
|
909
|
+
* injectInternal 在此钩子内注入的内容加入当前批,排在已有内部行之后、外部正文之前;钩子阻塞投递。
|
|
910
|
+
*/
|
|
911
|
+
onDelivery?(ctx: { events: EventEnvelope[] }): void;
|
|
912
|
+
/**
|
|
913
|
+
* 一批事件处理结束时调用;Persona 可查询 sessionInfo 并决定是否请求交接。
|
|
914
|
+
* Core 在超过 hardTokens 时强制交接。
|
|
915
|
+
*/
|
|
916
|
+
onBatchEnd?(): void;
|
|
917
|
+
/** assistant 自然结束、LLM 失败或轮数达到硬上限时调用。 */
|
|
918
|
+
onTurnEnded?(): void;
|
|
919
|
+
/** 一批处理结束且总线没有非 piggyback 的即时事件或候选时调用。 */
|
|
920
|
+
onIdle?(): void | Promise<void>;
|
|
921
|
+
/** 连续模型调用失败后恢复时调用;返回待注入文本,null 表示不注入。 */
|
|
922
|
+
onStallsRecovered?(info: { count: number; quietMs: number }): string | null;
|
|
923
|
+
/** World 被激活、停用、重启或可见性改变时调用;启动期初始挂载不通知。 */
|
|
924
|
+
onWorldLifecycle?(event: WorldLifecycleEvent): void;
|
|
925
|
+
/**
|
|
926
|
+
* Persona 的工具名,用于拒绝重名 World。
|
|
927
|
+
* 未提供时,重名仅在组装工具表时告警并保留先加入的工具。
|
|
928
|
+
*/
|
|
929
|
+
ownToolNames?(): string[];
|
|
930
|
+
/**
|
|
931
|
+
* session 的合成开头:每次请求置于 system 前缀之后、持久历史之前,不写入 session,交接时
|
|
932
|
+
* 不进保留内容。Core 每次出请求前调用一次,丢弃 system 与 developer 项,补齐工具配对,
|
|
933
|
+
* 计入 hardTokens。内容由 Persona 的当前输入决定。
|
|
934
|
+
*/
|
|
935
|
+
sessionHead?(): Item[];
|
|
936
|
+
/** Memory 实例。Core 不读它的内容;bot 没给 memoryName 时控制台以它的类名作 Memory 页标题。 */
|
|
937
|
+
memory?: object;
|
|
938
|
+
/** Memory 目录的绝对路径。 */
|
|
939
|
+
memoryDir: string;
|
|
940
|
+
/** mem: 句柄的后端,由 Persona 解释和保存二进制内容。 */
|
|
941
|
+
blobs: BlobStore;
|
|
942
|
+
/** 在 declareSessions 之前接收 CoreApi。 */
|
|
943
|
+
attach(core: CoreApi): void;
|
|
944
|
+
/** 必须恰有一个 receivesEvents=true 且 persistent=true 的声明。 */
|
|
945
|
+
declareSessions(): SessionDecl[];
|
|
946
|
+
/**
|
|
947
|
+
* 在稳定轮次边界、投递已暂停且快照已取时调用;返回新上下文,tail=null 时由 Core 选择。
|
|
948
|
+
* Core 校验工具配对,并将前缀与保留上下文限制在 hardTokens 内;保留比例由 Persona 决定。
|
|
949
|
+
* 钩子内注入的事件在重建完成后投递。
|
|
950
|
+
*/
|
|
951
|
+
onHandoff?(snapshot: ContextRecord[], ctx: { hardTokens: number | null }): Promise<ContextHandoffResult>;
|
|
952
|
+
/**
|
|
953
|
+
* 认知任务实现;上下文和预算归 Persona,模型配置归当前 provider。
|
|
954
|
+
* 未实现时,WorldHost 不提供 cognition。
|
|
955
|
+
*/
|
|
956
|
+
cognition?: PersonaCognition;
|
|
957
|
+
/**
|
|
958
|
+
* Persona 的控制台页面声明;Memory 和认知操作归此接口,跨组件部署操作归装配层。
|
|
959
|
+
* 面板 id 在页内唯一、不带前缀;page id 由装配层生成。language 与 World.console 相同。
|
|
960
|
+
*/
|
|
961
|
+
console?(language?: Language): PersonaConsoleDecl;
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
/**
|
|
965
|
+
* 与 src/web/shared/console-protocol.ts 的 ConsoleStream 保持结构和语义一致。
|
|
966
|
+
* 类型分别定义以保持 Core 的依赖方向,修改时须同步。
|
|
967
|
+
*/
|
|
968
|
+
export interface WorldStreamSocket {
|
|
969
|
+
/** 连接已关闭时忽略发送,不抛错。 */
|
|
970
|
+
send(data: string): void;
|
|
971
|
+
/** 关闭连接;reason 原样发送给对端。 */
|
|
972
|
+
close(reason?: string): void;
|
|
973
|
+
onMessage(cb: (text: string) => void): void;
|
|
974
|
+
onClose(cb: () => void): void;
|
|
975
|
+
readonly open: boolean;
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
export interface PersonaConsoleDecl {
|
|
979
|
+
badges?: Array<{ label: string; value: string | number; tone?: 'on' | 'off' | 'plain' }>;
|
|
980
|
+
panels?: WorldPanelDecl[];
|
|
981
|
+
invoke?(panel: string, method: string, args: unknown[]): Promise<unknown>;
|
|
982
|
+
promptDocs?: PromptDocDecl[];
|
|
983
|
+
storage?: StoragePart[];
|
|
984
|
+
config?: ConfigGroup[];
|
|
985
|
+
/**
|
|
986
|
+
* Memory 页的声明:面板、模板与存储项归 Memory 而不是 Persona。面板 id 不得与本页的重复,
|
|
987
|
+
* invoke 共用;缺省或三项皆空时没有 Memory 页。
|
|
988
|
+
*/
|
|
989
|
+
memory?: { panels?: WorldPanelDecl[]; promptDocs?: PromptDocDecl[]; storage?: StoragePart[] };
|
|
990
|
+
}
|
|
991
|
+
|
|
992
|
+
/**
|
|
993
|
+
* 日志级别:
|
|
994
|
+
* - error:功能不可用或需要人工处理;附带 err 或 data,并生成事故记录。
|
|
995
|
+
* - warn:异常已通过重试、降级等方式处理,或观测结果与预期不符。
|
|
996
|
+
* - info:生命周期和任务状态变化。
|
|
997
|
+
* - debug:诊断所需的中间状态;数值写入 data。
|
|
998
|
+
* - trace:高频状态变化,默认不写入日志。
|
|
999
|
+
*/
|
|
1000
|
+
export type LogLevel = 'trace' | 'debug' | 'info' | 'warn' | 'error';
|
|
1001
|
+
|
|
1002
|
+
export const LOG_LEVEL_RANK: Record<LogLevel, number> = { trace: 0, debug: 1, info: 2, warn: 3, error: 4 };
|
|
1003
|
+
|
|
1004
|
+
export interface LogError {
|
|
1005
|
+
name: string;
|
|
1006
|
+
message: string;
|
|
1007
|
+
stack?: string;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/** 日志关联字段;显式值覆盖异步上下文中的值。 */
|
|
1011
|
+
export interface LogAnchorFields {
|
|
1012
|
+
sess?: string;
|
|
1013
|
+
round?: number;
|
|
1014
|
+
resp?: string;
|
|
1015
|
+
call?: string;
|
|
1016
|
+
ev?: number;
|
|
1017
|
+
task?: number;
|
|
1018
|
+
}
|
|
1019
|
+
|
|
1020
|
+
/** `data/runs/<run>/log.jsonl` 一行。 */
|
|
1021
|
+
export interface LogRecord extends LogAnchorFields {
|
|
1022
|
+
/** 部署时区的 ISO 时间戳,精度为毫秒。 */
|
|
1023
|
+
ts: string;
|
|
1024
|
+
run: string;
|
|
1025
|
+
/** 当前日志流内递增的序号。 */
|
|
1026
|
+
seq: number;
|
|
1027
|
+
level: LogLevel;
|
|
1028
|
+
/** 子系统路径。 */
|
|
1029
|
+
area: string;
|
|
1030
|
+
/** 区域内的事件类型标识。 */
|
|
1031
|
+
event?: string;
|
|
1032
|
+
/** 可独立理解的中文说明。 */
|
|
1033
|
+
msg: string;
|
|
1034
|
+
durMs?: number;
|
|
1035
|
+
/** 折叠窗口内相同 area 和 event/msg 的重复次数,仅出现在汇总行。 */
|
|
1036
|
+
repeat?: number;
|
|
1037
|
+
data?: unknown;
|
|
1038
|
+
err?: LogError;
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
/** 写入日志的条目;sink 分配 ts/run/seq,子进程可提供自身时间戳和关联字段。 */
|
|
1042
|
+
export interface LogInput extends LogAnchorFields {
|
|
1043
|
+
level: LogLevel;
|
|
1044
|
+
area: string;
|
|
1045
|
+
msg: string;
|
|
1046
|
+
event?: string;
|
|
1047
|
+
durMs?: number;
|
|
1048
|
+
data?: unknown;
|
|
1049
|
+
err?: unknown;
|
|
1050
|
+
ts?: string;
|
|
1051
|
+
}
|
|
1052
|
+
|
|
1053
|
+
export interface LogEmitOptions extends LogAnchorFields {
|
|
1054
|
+
event?: string;
|
|
1055
|
+
durMs?: number;
|
|
1056
|
+
data?: unknown;
|
|
1057
|
+
err?: unknown;
|
|
1058
|
+
ts?: string;
|
|
1059
|
+
}
|
|
1060
|
+
|
|
1061
|
+
export interface Logger {
|
|
1062
|
+
trace(msg: string, data?: unknown): void;
|
|
1063
|
+
debug(msg: string, data?: unknown): void;
|
|
1064
|
+
info(msg: string, data?: unknown): void;
|
|
1065
|
+
warn(msg: string, data?: unknown): void;
|
|
1066
|
+
/** data 为 Error 或包含 err/error 时,将栈写入 err 字段。 */
|
|
1067
|
+
error(msg: string, data?: unknown): void;
|
|
1068
|
+
emit(level: LogLevel, msg: string, opts?: LogEmitOptions): void;
|
|
1069
|
+
child(area: string): Logger;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
/** 模型服务端点配置;由 activeProvider 选择当前端点。 */
|
|
1073
|
+
export interface LLMProviderEntry {
|
|
1074
|
+
options?: Record<string, unknown>;
|
|
1075
|
+
/** 端点的模型配置。缺省表示尚未选择模型,不能启用或执行调用。 */
|
|
1076
|
+
spec?: ModelSpec;
|
|
1077
|
+
serviceTier?: string;
|
|
1078
|
+
pricing?: PriceDefinition[];
|
|
1079
|
+
/** 已注册的 provider 模块 id。 */
|
|
1080
|
+
kind: string;
|
|
1081
|
+
baseUrl: string;
|
|
1082
|
+
/** 密钥环境变量名;未指定时不要求鉴权。 */
|
|
1083
|
+
secret?: string;
|
|
1084
|
+
/** 图像输入的手动开关;provider 可通过 accepts 提供具体的 MIME 支持判断。 */
|
|
1085
|
+
multimodal?: boolean;
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
/**
|
|
1089
|
+
* Core 的运行配置;部署层合并各组件的配置段。
|
|
1090
|
+
* 模型设置归 provider,session 轮数及上下文阶段预算归 Persona。
|
|
1091
|
+
*/
|
|
1092
|
+
export interface CoreConfig {
|
|
1093
|
+
/** 控制台和终端使用的显示名。 */
|
|
1094
|
+
displayName: string;
|
|
1095
|
+
timezone: string;
|
|
1096
|
+
/**
|
|
1097
|
+
* 进程启动时确定的控制台默认语言;缺省按环境变量或系统区域选择。
|
|
1098
|
+
* 浏览器可单独保存语言选择,不影响模型文本。
|
|
1099
|
+
*/
|
|
1100
|
+
language?: 'zh' | 'en';
|
|
1101
|
+
/** 以端点名为键的共享模型服务配置。 */
|
|
1102
|
+
providers: Record<string, LLMProviderEntry>;
|
|
1103
|
+
/** providers 中的端点名;新模型调用读取当前值。 */
|
|
1104
|
+
activeProvider: string;
|
|
1105
|
+
/** provider 配置格式版本,保存配置时写入。 */
|
|
1106
|
+
providerSchemaVersion?: number;
|
|
1107
|
+
context: {
|
|
1108
|
+
/** 是否在请求中保留 provider 支持回传的历史推理内容;不改变已保存的 session。 */
|
|
1109
|
+
keepPastThinking: boolean;
|
|
1110
|
+
};
|
|
1111
|
+
batching: {
|
|
1112
|
+
/** debounce 批距末次事件到达的等待时间。 */
|
|
1113
|
+
quietGapMs: number;
|
|
1114
|
+
/** debounce 批距首次事件到达的最短等待时间。 */
|
|
1115
|
+
minBatchAgeMs: number;
|
|
1116
|
+
/** debounce 批距首次事件到达的最长等待时间。 */
|
|
1117
|
+
maxBatchAgeMs: number;
|
|
1118
|
+
/** 积压外部即时事件和候选达到此数时立即投递;不计 piggyback 项。 */
|
|
1119
|
+
maxBatchSize: number;
|
|
1120
|
+
};
|
|
1121
|
+
web: {
|
|
1122
|
+
port: number;
|
|
1123
|
+
/** 控制台配色方案 id;浏览器没有保存过选择时用它,认不出的 id 落到框架默认方案。 */
|
|
1124
|
+
theme: string;
|
|
1125
|
+
};
|
|
1126
|
+
paths: {
|
|
1127
|
+
/** 相对部署目录或绝对路径;目录名由 Persona 指定。 */
|
|
1128
|
+
memory: string;
|
|
1129
|
+
|
|
1130
|
+
data: string;
|
|
1131
|
+
};
|
|
1132
|
+
logging: {
|
|
1133
|
+
/** 低于此级别的记录不写入 log.jsonl。 */
|
|
1134
|
+
file: LogLevel;
|
|
1135
|
+
/** stdout 日志的最低级别。 */
|
|
1136
|
+
console: LogLevel;
|
|
1137
|
+
/** 逗号分隔的 area=level;区域可带 * 后缀,按最长前缀匹配。 */
|
|
1138
|
+
areas: string;
|
|
1139
|
+
};
|
|
1140
|
+
}
|