@morlay/session-mode 0.0.0-stage → 0.2.0-alpha.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.
Files changed (85) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +85 -2
  3. package/dist/endpoint.d.mts +2 -0
  4. package/dist/endpoint.mjs +3 -0
  5. package/dist/index.d.mts +691 -0
  6. package/dist/index.mjs +2350 -0
  7. package/dist/mode-config-BIYT66jI.mjs +22 -0
  8. package/dist/mode-config-rules-z4gdHLR1.mjs +52 -0
  9. package/dist/rolldown-runtime-3qf6kE1h.d.mts +1 -0
  10. package/dist/server.d.mts +2 -0
  11. package/dist/server.mjs +1 -0
  12. package/dist/session-mode-endpoint-CLpvSnzM.d.mts +120 -0
  13. package/dist/session-mode-endpoint-Dq-E_kHt.mjs +53 -0
  14. package/dist/session-mode-endpoint.service-Bn4EH6aB.d.mts +21 -0
  15. package/dist/session-mode-endpoint.service-MrqQipN6.mjs +79 -0
  16. package/dist/testing.d.mts +12 -0
  17. package/dist/testing.mjs +23 -0
  18. package/dist/ui.d.mts +125 -0
  19. package/dist/ui.mjs +1165 -0
  20. package/package.json +63 -3
  21. package/src/context-assembler.provide.ts +139 -0
  22. package/src/context-assembler.service.ts +67 -0
  23. package/src/contract/context-assembler.contract.ts +30 -0
  24. package/src/contract/index.ts +3 -0
  25. package/src/contract/session-mode.contract.ts +104 -0
  26. package/src/contract/session-scope.contract.ts +53 -0
  27. package/src/defaults.ts +39 -0
  28. package/src/endpoint/index.ts +6 -0
  29. package/src/endpoint/mode-config-rules.ts +59 -0
  30. package/src/endpoint/mode-config.ts +58 -0
  31. package/src/endpoint/session-mode-endpoint.ts +121 -0
  32. package/src/endpoint/session-mode-shapes.ts +71 -0
  33. package/src/endpoint/tool-group-ref.ts +11 -0
  34. package/src/events.ts +102 -0
  35. package/src/guidance/groups.ts +101 -0
  36. package/src/guidance/index.ts +4 -0
  37. package/src/guidance/packs/ask.ts +17 -0
  38. package/src/guidance/packs/code.ts +25 -0
  39. package/src/guidance/packs/fs.ts +60 -0
  40. package/src/guidance/packs/index.ts +52 -0
  41. package/src/guidance/packs/mcp.ts +25 -0
  42. package/src/guidance/packs/plan.ts +42 -0
  43. package/src/guidance/packs/shell.ts +45 -0
  44. package/src/guidance/packs/skill.ts +12 -0
  45. package/src/guidance/packs/swarm.ts +60 -0
  46. package/src/guidance/packs/web-access.ts +24 -0
  47. package/src/guidance/tool-groups.ts +72 -0
  48. package/src/guidance/types.ts +29 -0
  49. package/src/index.ts +63 -0
  50. package/src/mode-sources.ts +83 -0
  51. package/src/mode-store.ts +45 -0
  52. package/src/modes.ts +126 -0
  53. package/src/per-agent-rows.ts +62 -0
  54. package/src/per-agent.ts +36 -0
  55. package/src/persona.ts +30 -0
  56. package/src/ptc-describe.ts +136 -0
  57. package/src/ptc-sdk.ts +101 -0
  58. package/src/reminder.ts +89 -0
  59. package/src/rules.ts +8 -0
  60. package/src/server/index.ts +7 -0
  61. package/src/server/session-mode-endpoint.service.ts +117 -0
  62. package/src/session-mode-store.ts +218 -0
  63. package/src/session-mode.projection.ts +78 -0
  64. package/src/session-mode.provide.ts +38 -0
  65. package/src/session-mode.service.ts +670 -0
  66. package/src/session-scope.service.ts +515 -0
  67. package/src/subagent/delegation-context.ts +30 -0
  68. package/src/subagent/return-guidance.ts +56 -0
  69. package/src/subagent/subagent-guidance.service.ts +76 -0
  70. package/src/subagent.provide.ts +5 -0
  71. package/src/system-remainder.ts +43 -0
  72. package/src/testing/index.ts +34 -0
  73. package/src/tool-guidance.service.ts +265 -0
  74. package/src/ui/index.ts +12 -0
  75. package/src/ui/locales.ts +134 -0
  76. package/src/ui/session-mode-endpoint.client.ts +19 -0
  77. package/src/ui/session-mode-locales.contribution.ts +51 -0
  78. package/src/ui/session-mode-panel-tab.ts +39 -0
  79. package/src/ui/session-mode-panel.view.tsx +931 -0
  80. package/src/ui/session-mode-seat.view.tsx +174 -0
  81. package/src/ui/session-mode-slots.ts +13 -0
  82. package/src/ui/session-mode.provide.ts +24 -0
  83. package/src/ui/session-mode.store.ts +90 -0
  84. package/src/ui/use-roster.ts +24 -0
  85. package/src/ui/view-props.ts +19 -0
@@ -0,0 +1,174 @@
1
+ // composer 里的模式 chip:本包在会话里唯一的面(点开是模式清单)。
2
+ // 选择只在会话开始**之前**有效(那段历史属于某个模式的工具与提示词),所以 chip 有两个形态:空白期是
3
+ // 选择器,开过 turn 之后只读。判据是宿主的 `sessionModeEditable` 投影(与服务端拒绝切换读的是同一份事实)。
4
+ import { useState, type ReactNode } from "react";
5
+ import { inject } from "@morlay/zephyr";
6
+ import { SlotView } from "@morlay/zephyr-app/client";
7
+ import {
8
+ IconAgentPresetOutlineRegular,
9
+ IconChevronDownOutlineRegular,
10
+ Menu,
11
+ SeatButton,
12
+ Stack,
13
+ Text,
14
+ } from "@morlay/zephyr-ui";
15
+ import { zh, type SessionModeLocaleKey } from "./locales.ts";
16
+ import {
17
+ SessionModeLocales,
18
+ type SessionModeTranslate,
19
+ } from "./session-mode-locales.contribution.ts";
20
+ import { ConversationInputLeftSlot } from "./session-mode-slots.ts";
21
+ import { SessionModeStore } from "./session-mode.store.ts";
22
+ import { useRoster } from "./use-roster.ts";
23
+ import { viewPropsOf, type SessionModeViewProps } from "./view-props.ts";
24
+
25
+ // chip 与它下面那条错误:竖排、宽度跟着按钮走(composer 工具行里不撑开)。
26
+ const SEAT_STYLE = {
27
+ display: "flex",
28
+ flexDirection: "column",
29
+ gap: 4,
30
+ alignItems: "flex-start",
31
+ } as const;
32
+
33
+ // 纯展示组件在测试里也是直接渲染的(不经容器),所以翻译座缺省取中文那份字典。
34
+ const fallbackTranslate: SessionModeTranslate = (key: SessionModeLocaleKey) => zh[key];
35
+
36
+ // 没有会话状态读面时(测试、没装会话行的宿主):两路投影读不到,chip 按空白会话的缺省形态渲染。
37
+ const NO_SESSIONS: NonNullable<SessionModeViewProps["useSessions"]> = () => undefined;
38
+
39
+ // 从会话目录快照里读一条投影值(上游 `useSessions` 的 selector 入参)。
40
+ function projectionOf(
41
+ snapshot: { byId: Record<string, { projectionValues?: Record<string, unknown> }> },
42
+ sessionId: string | undefined,
43
+ key: string,
44
+ ): unknown {
45
+ return sessionId === undefined ? undefined : snapshot.byId[sessionId]?.projectionValues?.[key];
46
+ }
47
+
48
+ /** composer 里的模式选择器。 */
49
+ @SlotView()
50
+ export class SessionModeSeatView extends ConversationInputLeftSlot {
51
+ private readonly store = inject(SessionModeStore);
52
+
53
+ private readonly locales = inject(SessionModeLocales);
54
+
55
+ render(): ReactNode {
56
+ const props = viewPropsOf(this.viewProps);
57
+ return (
58
+ <SessionModeSeat
59
+ sessionId={props.sessionId}
60
+ store={this.store}
61
+ useSessions={props.useSessions}
62
+ t={this.locales.t()}
63
+ />
64
+ );
65
+ }
66
+ }
67
+
68
+ /** chip 的渲染本体(容器只负责注入,测试直接渲染它)。 */
69
+ export function SessionModeSeat({
70
+ sessionId,
71
+ store,
72
+ useSessions,
73
+ t = fallbackTranslate,
74
+ }: {
75
+ readonly sessionId: string | undefined;
76
+ readonly store: SessionModeStore;
77
+ readonly useSessions?: SessionModeViewProps["useSessions"];
78
+ readonly t?: SessionModeTranslate | undefined;
79
+ }): ReactNode {
80
+ const roster = useRoster(store);
81
+ const [open, setOpen] = useState(false);
82
+ const [busy, setBusy] = useState(false);
83
+ const [error, setError] = useState<string | null>(null);
84
+
85
+ // 两个会话事实都读投影(与服务端拒绝切换读同一份事实):现在选的是哪个模式、还能不能换。
86
+ const sessions = useSessions ?? NO_SESSIONS;
87
+ const selected = sessions((snapshot) => projectionOf(snapshot, sessionId, "sessionMode")) as
88
+ | string
89
+ | undefined;
90
+ const editable = sessions((snapshot) =>
91
+ projectionOf(snapshot, sessionId, "sessionModeEditable"),
92
+ ) as boolean | undefined;
93
+
94
+ if (roster === undefined) return null;
95
+
96
+ const current = selected ?? roster.default;
97
+ const chosen = roster.modes.find((mode) => mode.id === current);
98
+ const label = chosen?.name ?? current;
99
+
100
+ // 开过 turn 的会话只读:chip 只写当前模式、点不动,并把原因说出来(不然用户以为坏了)。
101
+ if (editable === false) {
102
+ return (
103
+ <div data-role="session-mode-seat" data-locked="true" style={SEAT_STYLE}>
104
+ <SeatButton
105
+ disabled
106
+ title={t("lockedHint")}
107
+ icon={<IconAgentPresetOutlineRegular />}
108
+ label={label}
109
+ />
110
+ </div>
111
+ );
112
+ }
113
+
114
+ return (
115
+ <div data-role="session-mode-seat" style={SEAT_STYLE}>
116
+ <Menu
117
+ open={open}
118
+ onClose={() => {
119
+ setOpen(false);
120
+ }}
121
+ items={roster.modes.map((mode) => ({
122
+ id: mode.id,
123
+ label: (
124
+ <Stack gap={2}>
125
+ <Text size="md">{mode.name}</Text>
126
+ <Text size="sm" tone="tertiary">
127
+ {mode.description ?? t("noDescription")}
128
+ </Text>
129
+ </Stack>
130
+ ),
131
+ }))}
132
+ selectedId={current}
133
+ onSelect={(id) => {
134
+ setOpen(false);
135
+ if (sessionId === undefined || id === current) return;
136
+ setBusy(true);
137
+ setError(null);
138
+ void store
139
+ .select(sessionId, id)
140
+ .catch((cause: unknown) => {
141
+ setError(cause instanceof Error ? cause.message : String(cause));
142
+ })
143
+ .finally(() => {
144
+ setBusy(false);
145
+ });
146
+ }}
147
+ align="start"
148
+ portal
149
+ anchor={
150
+ <SeatButton
151
+ aria-haspopup="menu"
152
+ aria-expanded={open}
153
+ title={error ?? t("seatHint")}
154
+ disabled={busy}
155
+ icon={<IconAgentPresetOutlineRegular />}
156
+ label={label}
157
+ chevron={<IconChevronDownOutlineRegular />}
158
+ onClick={() => {
159
+ setOpen((value) => !value);
160
+ }}
161
+ />
162
+ }
163
+ />
164
+ {/* 失败不能只藏在 tooltip 里:鼠标一移开就没人知道刚才那次点没生效。 */}
165
+ {error === null ? null : (
166
+ <div data-role="seat-error" role="alert">
167
+ <Text as="p" size="sm" tone="danger">
168
+ {`${t("seatFailed")}: ${error}`}
169
+ </Text>
170
+ </div>
171
+ )}
172
+ </div>
173
+ );
174
+ }
@@ -0,0 +1,13 @@
1
+ // 本包要占的两个位置:composer 工具行左侧(list + session scope)与右栏面板(keyed + session scope)。
2
+ //
3
+ // 位置名只写在这里——贡献处用 `extends` 关联类型,不重复写字符串。
4
+ import { SlotProvider, SlotViewBase } from "@morlay/zephyr-app/client";
5
+
6
+ // 右栏面板那一页的 id(keyed 位置的 key):包名是天然的唯一值。
7
+ export const PANEL_ID = "@morlay/session-mode";
8
+
9
+ @SlotProvider({ name: "conversation.input.left", multi: true, scope: "session" })
10
+ export abstract class ConversationInputLeftSlot extends SlotViewBase {}
11
+
12
+ @SlotProvider({ name: "sidebar.right.pane.tab", multi: true, scope: "session", key: PANEL_ID })
13
+ export abstract class SidebarRightPaneTabSlot extends SlotViewBase {}
@@ -0,0 +1,24 @@
1
+ // session-mode 的浏览器半装配:端点客户端(自己挂组)→ 读面 store → 两个视图(composer 的 chip、右栏面板)。
2
+ //
3
+ // 面板**不随会话自动打开**:右栏那一页只由用户自己从标签条的 guide 入口(`SessionModePanelTab`)打开。
4
+ // 会话切换只让面板正文重拉(视图自己的 effect),不动右栏选中的是哪个 tab。
5
+ import { provide } from "@morlay/zephyr";
6
+ import { SessionModeEndpointClient } from "./session-mode-endpoint.client.ts";
7
+ import { SessionModeStore } from "./session-mode.store.ts";
8
+ import { SessionModeLocales } from "./session-mode-locales.contribution.ts";
9
+ import { SessionModePanelTab } from "./session-mode-panel-tab.ts";
10
+ import { SessionModeSeatView } from "./session-mode-seat.view.tsx";
11
+ import { SessionModePanelView } from "./session-mode-panel.view.tsx";
12
+
13
+ // 顺序 = 依赖:客户端端点先(store 与视图都经它走 wire);视图在最后(它们的 `onInit` 要注册到位置上)。
14
+ export const provideSessionModeUi = provide([
15
+ SessionModeEndpointClient,
16
+ // `sessionMode` 那组 descriptor 由上面那一行的 `onInit` 挂上(没有单独的绑定行)。
17
+ SessionModeStore,
18
+ // 字典必须是第一条:tab 的标题/guide 文案都经它 bind。
19
+ SessionModeLocales,
20
+ // tab **类型**先注册:正文(PanelView)按同一个 key 挂到 `sidebar.right.pane.tab` 上。
21
+ SessionModePanelTab,
22
+ SessionModeSeatView,
23
+ SessionModePanelView,
24
+ ]);
@@ -0,0 +1,90 @@
1
+ // 模式清单与「某个会话当下生效的那一份」的读面:带缓存,失败不缓存(下一次读到就是重试)。
2
+ //
3
+ // 清单是装配事实(这条行的 config),页面生命周期里不会变;生效态随会话覆盖变,所以按会话缓存。
4
+ import { inject, Injectable } from "@morlay/zephyr";
5
+ import type { ModeConfig } from "../endpoint/mode-config.ts";
6
+ import type { SessionModeActiveState, SessionModeRoster } from "../endpoint/session-mode-shapes.ts";
7
+ import { SessionModeEndpointClient } from "./session-mode-endpoint.client.ts";
8
+
9
+ @Injectable()
10
+ export class SessionModeStore {
11
+ private readonly endpoint = inject(SessionModeEndpointClient);
12
+
13
+ private roster: Promise<SessionModeRoster> | undefined;
14
+
15
+ private readonly actives = new Map<string, Promise<SessionModeActiveState>>();
16
+
17
+ // 模式清单(只拉一次;失败作废,下次读就是重试)。
18
+ loadRoster(): Promise<SessionModeRoster> {
19
+ this.roster ??= this.endpoint.roster().catch((error: unknown) => {
20
+ this.roster = undefined;
21
+ throw error;
22
+ });
23
+ return this.roster;
24
+ }
25
+
26
+ // 某个会话当下生效的那一份(每次挂载都重拉:会话覆盖可能刚被改过)。
27
+ loadActive(sessionId: string): Promise<SessionModeActiveState> {
28
+ const pending = this.endpoint.active({ sessionId }).catch((error: unknown) => {
29
+ this.actives.delete(sessionId);
30
+ throw error;
31
+ });
32
+ this.actives.set(sessionId, pending);
33
+ return pending;
34
+ }
35
+
36
+ // 把某个空白会话切到某个模式。
37
+ async select(sessionId: string, mode: string): Promise<string> {
38
+ const result = await this.endpoint.select({ sessionId, mode });
39
+ this.actives.delete(sessionId);
40
+ return result.mode;
41
+ }
42
+
43
+ // 改这个会话的字段覆盖;返回新的生效态。
44
+ async patch(
45
+ sessionId: string,
46
+ patch: Readonly<Record<string, unknown>>,
47
+ ): Promise<SessionModeActiveState> {
48
+ return await this.cache(sessionId, this.endpoint.patch({ sessionId, patch }));
49
+ }
50
+
51
+ // 一次性应用整份会话覆盖。
52
+ async applyConfig(sessionId: string, config: ModeConfig): Promise<SessionModeActiveState> {
53
+ return await this.cache(sessionId, this.endpoint.applyConfig({ sessionId, config }));
54
+ }
55
+
56
+ // 把草稿写回模式配置(对新会话生效)。
57
+ async saveConfig(sessionId: string, config: ModeConfig): Promise<SessionModeActiveState> {
58
+ const state = await this.cache(sessionId, this.endpoint.saveConfig({ sessionId, config }));
59
+ this.roster = undefined;
60
+ return state;
61
+ }
62
+
63
+ // 以当前生效配置另存为新模式。
64
+ async saveAs(sessionId: string, code: string, name?: string): Promise<string> {
65
+ const result = await this.endpoint.saveAs({
66
+ sessionId,
67
+ code,
68
+ ...(name === undefined ? {} : { name }),
69
+ });
70
+ this.roster = undefined;
71
+ return result.mode;
72
+ }
73
+
74
+ // 删除当前模式;返回会话回到的那个模式。
75
+ async remove(sessionId: string): Promise<string> {
76
+ const result = await this.endpoint.removeMode({ sessionId });
77
+ this.roster = undefined;
78
+ this.actives.delete(sessionId);
79
+ return result.mode;
80
+ }
81
+
82
+ private async cache(
83
+ sessionId: string,
84
+ pending: Promise<SessionModeActiveState>,
85
+ ): Promise<SessionModeActiveState> {
86
+ const state = await pending;
87
+ this.actives.set(sessionId, Promise.resolve(state));
88
+ return state;
89
+ }
90
+ }
@@ -0,0 +1,24 @@
1
+ // 模式清单:装配事实(这条行的 config),页面生命周期里不会变,所以模块级只拉一次。
2
+ // 失败不缓存——下次挂载就是一次重试。
3
+ import { useEffect, useState } from "react";
4
+ import type { SessionModeRoster } from "../endpoint/session-mode-shapes.ts";
5
+ import type { SessionModeStore } from "./session-mode.store.ts";
6
+
7
+ export function useRoster(store: SessionModeStore): SessionModeRoster | undefined {
8
+ const [roster, setRoster] = useState<SessionModeRoster | undefined>(undefined);
9
+ useEffect(() => {
10
+ let alive = true;
11
+ store.loadRoster().then(
12
+ (value) => {
13
+ if (alive) setRoster(value);
14
+ },
15
+ () => {
16
+ // 读不到清单时 chip 不渲染(下一次挂载重试)。
17
+ },
18
+ );
19
+ return () => {
20
+ alive = false;
21
+ };
22
+ }, [store]);
23
+ return roster;
24
+ }
@@ -0,0 +1,19 @@
1
+ // 视图从位置渲染那里拿到的 props:会话身份与可选的会话状态读面。
2
+ //
3
+ // 只声明本包真正用到的那几个(位置渲染会带更多,用不到的不列)。
4
+ export interface SessionModeViewProps {
5
+ readonly sessionId?: string | undefined;
6
+ // 上游 `ui-conversation` 的会话状态读面(`useSessions(selector)`):面板用它跟模式变化。
7
+ readonly useSessions?:
8
+ | ((
9
+ selector: (state: {
10
+ byId: Record<string, { projectionValues?: Record<string, unknown> }>;
11
+ }) => unknown,
12
+ ) => unknown)
13
+ | undefined;
14
+ }
15
+
16
+ // 从 `this.viewProps`(`unknown`)里读出本包的 props。
17
+ export function viewPropsOf(value: unknown): SessionModeViewProps {
18
+ return (typeof value === "object" && value !== null ? value : {}) as SessionModeViewProps;
19
+ }