@haaaiawd/second-nature 0.1.1 → 0.1.3

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 (202) hide show
  1. package/index.ts +64 -21
  2. package/openclaw.plugin.json +1 -1
  3. package/package.json +8 -2
  4. package/runtime/cli/action-bridge.d.ts +11 -0
  5. package/runtime/cli/action-bridge.js +27 -0
  6. package/runtime/cli/commands/credential.d.ts +2 -0
  7. package/runtime/cli/commands/credential.js +40 -0
  8. package/runtime/cli/commands/index.d.ts +12 -0
  9. package/runtime/cli/commands/index.js +138 -0
  10. package/runtime/cli/commands/policy.d.ts +12 -0
  11. package/runtime/cli/commands/policy.js +43 -0
  12. package/runtime/cli/explain/format-explanation.d.ts +10 -0
  13. package/runtime/cli/explain/format-explanation.js +10 -0
  14. package/runtime/cli/explain/resolve-subject.d.ts +2 -0
  15. package/runtime/cli/explain/resolve-subject.js +26 -0
  16. package/runtime/cli/index.d.ts +25 -0
  17. package/runtime/cli/index.js +36 -0
  18. package/runtime/cli/read-models/index.d.ts +20 -0
  19. package/runtime/cli/read-models/index.js +161 -0
  20. package/runtime/cli/read-models/types.d.ts +75 -0
  21. package/runtime/cli/read-models/types.js +1 -0
  22. package/runtime/connectors/agent-network/evomap/adapter.d.ts +23 -0
  23. package/runtime/connectors/agent-network/evomap/adapter.js +69 -0
  24. package/runtime/connectors/agent-network/evomap/index.d.ts +2 -0
  25. package/runtime/connectors/agent-network/evomap/index.js +2 -0
  26. package/runtime/connectors/agent-network/evomap/manifest.d.ts +2 -0
  27. package/runtime/connectors/agent-network/evomap/manifest.js +7 -0
  28. package/runtime/connectors/base/channel-health.d.ts +29 -0
  29. package/runtime/connectors/base/channel-health.js +23 -0
  30. package/runtime/connectors/base/contract.d.ts +81 -0
  31. package/runtime/connectors/base/contract.js +71 -0
  32. package/runtime/connectors/base/failure-taxonomy.d.ts +13 -0
  33. package/runtime/connectors/base/failure-taxonomy.js +105 -0
  34. package/runtime/connectors/base/index.d.ts +6 -0
  35. package/runtime/connectors/base/index.js +6 -0
  36. package/runtime/connectors/base/manifest.d.ts +11 -0
  37. package/runtime/connectors/base/manifest.js +36 -0
  38. package/runtime/connectors/base/policy-layer.d.ts +27 -0
  39. package/runtime/connectors/base/policy-layer.js +213 -0
  40. package/runtime/connectors/base/route-planner.d.ts +10 -0
  41. package/runtime/connectors/base/route-planner.js +98 -0
  42. package/runtime/connectors/index.d.ts +4 -0
  43. package/runtime/connectors/index.js +4 -0
  44. package/runtime/connectors/social-community/instreet/adapter.d.ts +32 -0
  45. package/runtime/connectors/social-community/instreet/adapter.js +79 -0
  46. package/runtime/connectors/social-community/instreet/index.d.ts +2 -0
  47. package/runtime/connectors/social-community/instreet/index.js +2 -0
  48. package/runtime/connectors/social-community/instreet/manifest.d.ts +2 -0
  49. package/runtime/connectors/social-community/instreet/manifest.js +7 -0
  50. package/runtime/connectors/social-community/moltbook/adapter.d.ts +15 -0
  51. package/runtime/connectors/social-community/moltbook/adapter.js +48 -0
  52. package/runtime/connectors/social-community/moltbook/index.d.ts +2 -0
  53. package/runtime/connectors/social-community/moltbook/index.js +2 -0
  54. package/runtime/connectors/social-community/moltbook/manifest.d.ts +7 -0
  55. package/runtime/connectors/social-community/moltbook/manifest.js +12 -0
  56. package/runtime/core/second-nature/guidance/apply-guidance.d.ts +10 -0
  57. package/runtime/core/second-nature/guidance/apply-guidance.js +10 -0
  58. package/runtime/core/second-nature/guidance/request-guidance.d.ts +18 -0
  59. package/runtime/core/second-nature/guidance/request-guidance.js +22 -0
  60. package/runtime/core/second-nature/index.d.ts +14 -0
  61. package/runtime/core/second-nature/index.js +14 -0
  62. package/runtime/core/second-nature/orchestrator/effect-dispatcher.d.ts +100 -0
  63. package/runtime/core/second-nature/orchestrator/effect-dispatcher.js +139 -0
  64. package/runtime/core/second-nature/orchestrator/guard-layer.d.ts +2 -0
  65. package/runtime/core/second-nature/orchestrator/guard-layer.js +54 -0
  66. package/runtime/core/second-nature/orchestrator/intent-planner.d.ts +3 -0
  67. package/runtime/core/second-nature/orchestrator/intent-planner.js +92 -0
  68. package/runtime/core/second-nature/orchestrator/lease-manager.d.ts +14 -0
  69. package/runtime/core/second-nature/orchestrator/lease-manager.js +58 -0
  70. package/runtime/core/second-nature/orchestrator/resume-from-checkpoint.d.ts +32 -0
  71. package/runtime/core/second-nature/orchestrator/resume-from-checkpoint.js +23 -0
  72. package/runtime/core/second-nature/outreach/build-message.d.ts +16 -0
  73. package/runtime/core/second-nature/outreach/build-message.js +27 -0
  74. package/runtime/core/second-nature/outreach/evaluate-outreach.d.ts +13 -0
  75. package/runtime/core/second-nature/outreach/evaluate-outreach.js +41 -0
  76. package/runtime/core/second-nature/quiet/quiet-pipeline.d.ts +34 -0
  77. package/runtime/core/second-nature/quiet/quiet-pipeline.js +35 -0
  78. package/runtime/core/second-nature/reflection/run-narrative-reflection.d.ts +39 -0
  79. package/runtime/core/second-nature/reflection/run-narrative-reflection.js +29 -0
  80. package/runtime/core/second-nature/rhythm/rhythm-policy.d.ts +18 -0
  81. package/runtime/core/second-nature/rhythm/rhythm-policy.js +24 -0
  82. package/runtime/core/second-nature/rhythm/select-window.d.ts +3 -0
  83. package/runtime/core/second-nature/rhythm/select-window.js +50 -0
  84. package/runtime/core/second-nature/runtime/lifecycle-service.d.ts +26 -0
  85. package/runtime/core/second-nature/runtime/lifecycle-service.js +38 -0
  86. package/runtime/core/second-nature/runtime/service-entry.d.ts +36 -0
  87. package/runtime/core/second-nature/runtime/service-entry.js +44 -0
  88. package/runtime/core/second-nature/types.d.ts +37 -0
  89. package/runtime/core/second-nature/types.js +1 -0
  90. package/runtime/guidance/contracts.d.ts +48 -0
  91. package/runtime/guidance/contracts.js +54 -0
  92. package/runtime/guidance/fallback.d.ts +2 -0
  93. package/runtime/guidance/fallback.js +17 -0
  94. package/runtime/guidance/guidance-assembler.d.ts +5 -0
  95. package/runtime/guidance/guidance-assembler.js +62 -0
  96. package/runtime/guidance/index.d.ts +8 -0
  97. package/runtime/guidance/index.js +8 -0
  98. package/runtime/guidance/output-guard.d.ts +10 -0
  99. package/runtime/guidance/output-guard.js +29 -0
  100. package/runtime/guidance/persona-selection.d.ts +11 -0
  101. package/runtime/guidance/persona-selection.js +90 -0
  102. package/runtime/guidance/review-workflow.d.ts +15 -0
  103. package/runtime/guidance/review-workflow.js +60 -0
  104. package/runtime/guidance/template-registry.d.ts +3 -0
  105. package/runtime/guidance/template-registry.js +45 -0
  106. package/runtime/guidance/types.d.ts +72 -0
  107. package/runtime/guidance/types.js +1 -0
  108. package/runtime/observability/db/index.d.ts +10 -0
  109. package/runtime/observability/db/index.js +17 -0
  110. package/runtime/observability/db/schema/index.d.ts +946 -0
  111. package/runtime/observability/db/schema/index.js +70 -0
  112. package/runtime/observability/index.d.ts +12 -0
  113. package/runtime/observability/index.js +11 -0
  114. package/runtime/observability/projections/guidance-audit.d.ts +16 -0
  115. package/runtime/observability/projections/guidance-audit.js +35 -0
  116. package/runtime/observability/projections/outreach-quality-audit.d.ts +15 -0
  117. package/runtime/observability/projections/outreach-quality-audit.js +9 -0
  118. package/runtime/observability/projections/reflection-audit.d.ts +17 -0
  119. package/runtime/observability/projections/reflection-audit.js +9 -0
  120. package/runtime/observability/query/compose-evidence.d.ts +56 -0
  121. package/runtime/observability/query/compose-evidence.js +43 -0
  122. package/runtime/observability/query/evidence-query-engine.d.ts +17 -0
  123. package/runtime/observability/query/evidence-query-engine.js +166 -0
  124. package/runtime/observability/redaction/manifest.d.ts +18 -0
  125. package/runtime/observability/redaction/manifest.js +109 -0
  126. package/runtime/observability/redaction/policy.d.ts +19 -0
  127. package/runtime/observability/redaction/policy.js +71 -0
  128. package/runtime/observability/services/decision-ledger.d.ts +33 -0
  129. package/runtime/observability/services/decision-ledger.js +115 -0
  130. package/runtime/observability/services/execution-telemetry.d.ts +32 -0
  131. package/runtime/observability/services/execution-telemetry.js +126 -0
  132. package/runtime/observability/services/governance-audit.d.ts +27 -0
  133. package/runtime/observability/services/governance-audit.js +139 -0
  134. package/runtime/observability/services/redaction-store.d.ts +3 -0
  135. package/runtime/observability/services/redaction-store.js +20 -0
  136. package/runtime/setup/HOST_SETUP.md +112 -0
  137. package/runtime/shared/types/continuity.d.ts +69 -0
  138. package/runtime/shared/types/continuity.js +1 -0
  139. package/runtime/shared/types/credential.d.ts +22 -0
  140. package/runtime/shared/types/credential.js +1 -0
  141. package/runtime/shared/types/index.d.ts +3 -0
  142. package/runtime/shared/types/index.js +3 -0
  143. package/runtime/shared/types/outreach.d.ts +19 -0
  144. package/runtime/shared/types/outreach.js +1 -0
  145. package/runtime/storage/bootstrap/repair.d.ts +3 -0
  146. package/runtime/storage/bootstrap/repair.js +5 -0
  147. package/runtime/storage/db/index.d.ts +10 -0
  148. package/runtime/storage/db/index.js +17 -0
  149. package/runtime/storage/db/schema/assets.d.ts +140 -0
  150. package/runtime/storage/db/schema/assets.js +10 -0
  151. package/runtime/storage/db/schema/credentials.d.ts +178 -0
  152. package/runtime/storage/db/schema/credentials.js +12 -0
  153. package/runtime/storage/db/schema/index.d.ts +6 -0
  154. package/runtime/storage/db/schema/index.js +6 -0
  155. package/runtime/storage/db/schema/intent-commits.d.ts +161 -0
  156. package/runtime/storage/db/schema/intent-commits.js +11 -0
  157. package/runtime/storage/db/schema/policies.d.ts +81 -0
  158. package/runtime/storage/db/schema/policies.js +7 -0
  159. package/runtime/storage/db/schema/proposals.d.ts +216 -0
  160. package/runtime/storage/db/schema/proposals.js +14 -0
  161. package/runtime/storage/db/schema/provenance.d.ts +104 -0
  162. package/runtime/storage/db/schema/provenance.js +8 -0
  163. package/runtime/storage/index.d.ts +16 -0
  164. package/runtime/storage/index.js +16 -0
  165. package/runtime/storage/memory/workspace/paths.d.ts +62 -0
  166. package/runtime/storage/memory/workspace/paths.js +160 -0
  167. package/runtime/storage/memory/workspace/store.d.ts +26 -0
  168. package/runtime/storage/memory/workspace/store.js +153 -0
  169. package/runtime/storage/memory/workspace/types.d.ts +45 -0
  170. package/runtime/storage/memory/workspace/types.js +1 -0
  171. package/runtime/storage/repositories/asset-repository.d.ts +8 -0
  172. package/runtime/storage/repositories/asset-repository.js +19 -0
  173. package/runtime/storage/repositories/credential-repository.d.ts +8 -0
  174. package/runtime/storage/repositories/credential-repository.js +19 -0
  175. package/runtime/storage/repositories/index.d.ts +6 -0
  176. package/runtime/storage/repositories/index.js +6 -0
  177. package/runtime/storage/repositories/intent-commit-repository.d.ts +10 -0
  178. package/runtime/storage/repositories/intent-commit-repository.js +35 -0
  179. package/runtime/storage/repositories/policy-repository.d.ts +8 -0
  180. package/runtime/storage/repositories/policy-repository.js +19 -0
  181. package/runtime/storage/repositories/proposal-repository.d.ts +9 -0
  182. package/runtime/storage/repositories/proposal-repository.js +26 -0
  183. package/runtime/storage/repositories/provenance-repository.d.ts +20 -0
  184. package/runtime/storage/repositories/provenance-repository.js +41 -0
  185. package/runtime/storage/services/credential-vault.d.ts +8 -0
  186. package/runtime/storage/services/credential-vault.js +78 -0
  187. package/runtime/storage/services/daily-log-pipeline.d.ts +47 -0
  188. package/runtime/storage/services/daily-log-pipeline.js +86 -0
  189. package/runtime/storage/services/effect-commit-store.d.ts +11 -0
  190. package/runtime/storage/services/effect-commit-store.js +93 -0
  191. package/runtime/storage/services/governance-layer.d.ts +40 -0
  192. package/runtime/storage/services/governance-layer.js +103 -0
  193. package/runtime/storage/services/persona-candidate-loader.d.ts +5 -0
  194. package/runtime/storage/services/persona-candidate-loader.js +41 -0
  195. package/runtime/storage/services/provenance-service.d.ts +40 -0
  196. package/runtime/storage/services/provenance-service.js +43 -0
  197. package/runtime/storage/services/quiet-input-loader.d.ts +40 -0
  198. package/runtime/storage/services/quiet-input-loader.js +131 -0
  199. package/runtime/storage/services/repair-and-backup.d.ts +22 -0
  200. package/runtime/storage/services/repair-and-backup.js +73 -0
  201. package/runtime/storage/state-api.d.ts +46 -0
  202. package/runtime/storage/state-api.js +73 -0
@@ -0,0 +1,54 @@
1
+ export const GUIDANCE_OWNER_BOUNDARIES = {
2
+ "control-plane-system": {
3
+ owner: "control-plane-system",
4
+ owns: ["scene context", "guidance request timing", "minimal fallback invocation"],
5
+ provides: ["scene context for guidance assembly"],
6
+ mustNotOwn: ["persona canonical store", "output style guard definition", "template content"],
7
+ },
8
+ "behavioral-guidance-system": {
9
+ owner: "behavioral-guidance-system",
10
+ owns: ["guidance payload assembly", "runtime atmosphere", "behavioral impulses", "persona reinforcement selection", "output guard"],
11
+ provides: ["guidance payload", "guidance unavailable result"],
12
+ mustNotOwn: ["action allow or deny decisions", "platform execution", "persona canonical store writes"],
13
+ },
14
+ "state-system": {
15
+ owner: "state-system",
16
+ owns: ["persona source assets", "candidate loading"],
17
+ provides: ["snippet-sized persona candidates"],
18
+ mustNotOwn: ["guidance payload assembly", "output guard semantics", "template wording"],
19
+ },
20
+ "observability-system": {
21
+ owner: "observability-system",
22
+ owns: ["guidance participation record", "snippet rationale visibility"],
23
+ provides: ["guidance audit trail"],
24
+ mustNotOwn: ["guidance authoring", "hard safety veto", "persona source ownership"],
25
+ },
26
+ "hard-guard-layer": {
27
+ owner: "hard-guard-layer",
28
+ owns: ["action legality", "risk veto", "credential and policy enforcement"],
29
+ provides: ["hard allow or deny decision"],
30
+ mustNotOwn: ["runtime atmosphere", "behavioral impulses", "persona reinforcement"],
31
+ },
32
+ };
33
+ export const GUIDANCE_HANDOFFS = {
34
+ request: {
35
+ from: "control-plane-system",
36
+ to: "behavioral-guidance-system",
37
+ payload: "scene_context",
38
+ },
39
+ personaCandidates: {
40
+ from: "state-system",
41
+ to: "behavioral-guidance-system",
42
+ payload: "snippet_sized_persona_candidates",
43
+ },
44
+ guidanceResult: {
45
+ from: "behavioral-guidance-system",
46
+ to: "control-plane-system",
47
+ payload: "guidance_payload_or_unavailable",
48
+ },
49
+ auditRecord: {
50
+ from: "behavioral-guidance-system",
51
+ to: "observability-system",
52
+ payload: "guidance_participation_summary",
53
+ },
54
+ };
@@ -0,0 +1,2 @@
1
+ import type { GuidanceFallback, SceneContext } from "./types.js";
2
+ export declare function buildMinimalGuidanceFallback(sceneContext: SceneContext): GuidanceFallback;
@@ -0,0 +1,17 @@
1
+ import { buildOutputGuard } from "./output-guard.js";
2
+ export function buildMinimalGuidanceFallback(sceneContext) {
3
+ return {
4
+ scene: sceneContext,
5
+ atmosphere: {
6
+ kind: "atmosphere",
7
+ text: "我先保留最小但真实的状态感,不因为 guidance 缺席就假装自己失去姿态。",
8
+ openness: sceneContext.mode === "quiet" ? "quiet" : sceneContext.riskLevel === "high" ? "narrow" : "open",
9
+ pressureLabels: [sceneContext.mode, sceneContext.riskLevel ?? "unknown_risk"],
10
+ reviewStatus: "pending_human_review",
11
+ },
12
+ impulses: [],
13
+ personaReinforcement: [],
14
+ outputGuard: buildOutputGuard(sceneContext.sceneType),
15
+ minimal: true,
16
+ };
17
+ }
@@ -0,0 +1,5 @@
1
+ import type { GuidancePayload, GuidanceUnavailable, PersonaCandidate, SceneContext } from "./types.js";
2
+ export declare function assembleGuidance(input: {
3
+ sceneContext: SceneContext | null | undefined;
4
+ personaCandidates?: PersonaCandidate[];
5
+ }): Promise<GuidancePayload | GuidanceUnavailable>;
@@ -0,0 +1,62 @@
1
+ import { buildMinimalGuidanceFallback } from "./fallback.js";
2
+ import { buildOutputGuard } from "./output-guard.js";
3
+ import { selectPersonaSnippets } from "./persona-selection.js";
4
+ import { getBaselineAtmosphereTemplate, getImpulseTemplate } from "./template-registry.js";
5
+ async function buildAtmosphere(sceneContext) {
6
+ const template = getBaselineAtmosphereTemplate();
7
+ return {
8
+ kind: "atmosphere",
9
+ text: template.text,
10
+ openness: sceneContext.mode === "quiet" ? "quiet" : sceneContext.riskLevel === "high" ? "narrow" : "open",
11
+ pressureLabels: [sceneContext.mode, sceneContext.riskLevel ?? "unknown_risk"],
12
+ reviewStatus: template.reviewStatus,
13
+ };
14
+ }
15
+ async function selectImpulses(sceneContext) {
16
+ if (sceneContext.sceneType === "explain") {
17
+ return [];
18
+ }
19
+ return [getImpulseTemplate(sceneContext.sceneType)];
20
+ }
21
+ export async function assembleGuidance(input) {
22
+ if (!input.sceneContext) {
23
+ return {
24
+ available: false,
25
+ reason: "missing_scene_context",
26
+ };
27
+ }
28
+ const sceneContext = input.sceneContext;
29
+ try {
30
+ const [atmosphere, impulses] = await Promise.all([
31
+ buildAtmosphere(sceneContext),
32
+ selectImpulses(sceneContext),
33
+ ]);
34
+ const personaDecision = selectPersonaSnippets({
35
+ sceneContext,
36
+ candidates: input.personaCandidates ?? [],
37
+ });
38
+ return {
39
+ scene: sceneContext,
40
+ atmosphere,
41
+ impulses,
42
+ personaReinforcement: personaDecision.snippets,
43
+ outputGuard: buildOutputGuard(sceneContext.sceneType),
44
+ };
45
+ }
46
+ catch {
47
+ if ((input.personaCandidates ?? []).length === 0) {
48
+ const fallback = buildMinimalGuidanceFallback(sceneContext);
49
+ return {
50
+ scene: fallback.scene,
51
+ atmosphere: fallback.atmosphere,
52
+ impulses: fallback.impulses,
53
+ personaReinforcement: fallback.personaReinforcement,
54
+ outputGuard: fallback.outputGuard,
55
+ };
56
+ }
57
+ return {
58
+ available: false,
59
+ reason: "missing_template",
60
+ };
61
+ }
62
+ }
@@ -0,0 +1,8 @@
1
+ export * from "./types.js";
2
+ export * from "./contracts.js";
3
+ export * from "./persona-selection.js";
4
+ export * from "./output-guard.js";
5
+ export * from "./fallback.js";
6
+ export * from "./template-registry.js";
7
+ export * from "./review-workflow.js";
8
+ export * from "./guidance-assembler.js";
@@ -0,0 +1,8 @@
1
+ export * from "./types.js";
2
+ export * from "./contracts.js";
3
+ export * from "./persona-selection.js";
4
+ export * from "./output-guard.js";
5
+ export * from "./fallback.js";
6
+ export * from "./template-registry.js";
7
+ export * from "./review-workflow.js";
8
+ export * from "./guidance-assembler.js";
@@ -0,0 +1,10 @@
1
+ import type { GuardBlock, GuidanceSceneType } from "./types.js";
2
+ export type OutputGuardConstraintId = "avoid_customer_service_tone" | "avoid_daily_report_tone" | "avoid_teaching_template_tone" | "avoid_fabricated_experience" | "avoid_repetitive_phrasing";
3
+ export interface OutputGuardDefinition {
4
+ sceneType: GuidanceSceneType;
5
+ constraints: OutputGuardConstraintId[];
6
+ hardGuardPriority: true;
7
+ note: "output_guard_only_shapes_expression";
8
+ }
9
+ export declare function buildOutputGuard(sceneType: GuidanceSceneType): GuardBlock;
10
+ export declare function getOutputGuardDefinition(sceneType: GuidanceSceneType): OutputGuardDefinition;
@@ -0,0 +1,29 @@
1
+ const SHARED_CONSTRAINTS = [
2
+ "avoid_customer_service_tone",
3
+ "avoid_daily_report_tone",
4
+ "avoid_teaching_template_tone",
5
+ "avoid_fabricated_experience",
6
+ "avoid_repetitive_phrasing",
7
+ ];
8
+ const SCENE_NOTES = {
9
+ avoid_customer_service_tone: "不要把自己说成客服或通知系统。",
10
+ avoid_daily_report_tone: "不要把表达压扁成日报、周报或例行播报。",
11
+ avoid_teaching_template_tone: "不要把表达写成教程、步骤说明或培训手册。",
12
+ avoid_fabricated_experience: "不要虚构经历、关系、情绪事件或未发生的观察。",
13
+ avoid_repetitive_phrasing: "不要滑进高重复、硬模板化或近似复读的措辞。",
14
+ };
15
+ export function buildOutputGuard(sceneType) {
16
+ return {
17
+ kind: "output_guard",
18
+ constraints: SHARED_CONSTRAINTS.map((constraint) => SCENE_NOTES[constraint]),
19
+ hardGuardPriority: true,
20
+ };
21
+ }
22
+ export function getOutputGuardDefinition(sceneType) {
23
+ return {
24
+ sceneType,
25
+ constraints: [...SHARED_CONSTRAINTS],
26
+ hardGuardPriority: true,
27
+ note: "output_guard_only_shapes_expression",
28
+ };
29
+ }
@@ -0,0 +1,11 @@
1
+ import type { GuidanceSceneType, PersonaCandidate, PersonaInjectionBudget, PersonaSelectionDecision, PersonaSource, SceneContext } from "./types.js";
2
+ export interface PersonaSelectionPolicy {
3
+ readonly sourcePriority: readonly PersonaSource[];
4
+ readonly preferredTags: readonly string[];
5
+ readonly budget: PersonaInjectionBudget;
6
+ }
7
+ export declare function getPersonaSelectionPolicy(sceneType: GuidanceSceneType): PersonaSelectionPolicy;
8
+ export declare function selectPersonaSnippets(input: {
9
+ sceneContext: SceneContext;
10
+ candidates: PersonaCandidate[];
11
+ }): PersonaSelectionDecision;
@@ -0,0 +1,90 @@
1
+ const SCENE_POLICIES = {
2
+ social: {
3
+ sourcePriority: ["SOUL", "IDENTITY", "MEMORY", "USER"],
4
+ preferredTags: ["social", "public", "curiosity", "voice"],
5
+ budget: { maxSnippets: 2, maxTotalCharacters: 360 },
6
+ },
7
+ reply: {
8
+ sourcePriority: ["USER", "IDENTITY", "SOUL", "MEMORY"],
9
+ preferredTags: ["reply", "conversation", "care", "clarity"],
10
+ budget: { maxSnippets: 2, maxTotalCharacters: 320 },
11
+ },
12
+ outreach: {
13
+ sourcePriority: ["USER", "SOUL", "MEMORY", "IDENTITY"],
14
+ preferredTags: ["outreach", "user", "trust", "initiative"],
15
+ budget: { maxSnippets: 2, maxTotalCharacters: 420 },
16
+ },
17
+ quiet: {
18
+ sourcePriority: ["SOUL", "MEMORY", "IDENTITY", "USER"],
19
+ preferredTags: ["quiet", "reflection", "memory", "meaning"],
20
+ budget: { maxSnippets: 3, maxTotalCharacters: 640 },
21
+ },
22
+ explain: {
23
+ sourcePriority: ["IDENTITY", "USER", "SOUL", "MEMORY"],
24
+ preferredTags: ["explain", "principle", "context", "truthfulness"],
25
+ budget: { maxSnippets: 2, maxTotalCharacters: 420 },
26
+ },
27
+ };
28
+ function tagScore(candidate, preferredTags) {
29
+ const normalized = new Set(candidate.tags.map((tag) => tag.toLowerCase()));
30
+ return preferredTags.reduce((score, tag) => score + (normalized.has(tag.toLowerCase()) ? 2 : 0), 0);
31
+ }
32
+ function sourceScore(source, policy) {
33
+ const index = policy.sourcePriority.indexOf(source);
34
+ return index === -1 ? 0 : policy.sourcePriority.length - index;
35
+ }
36
+ function truncateToBudget(text, remainingCharacters) {
37
+ const normalized = text.replace(/\s+/g, " ").trim();
38
+ if (normalized.length <= remainingCharacters) {
39
+ return normalized;
40
+ }
41
+ const ellipsis = remainingCharacters > 3 ? "..." : "";
42
+ const contentBudget = Math.max(0, remainingCharacters - ellipsis.length);
43
+ const clipped = normalized.slice(0, contentBudget).trimEnd();
44
+ if (clipped.length === 0) {
45
+ return "";
46
+ }
47
+ return `${clipped}${ellipsis}`;
48
+ }
49
+ function buildRationale(candidate, sceneType) {
50
+ const tags = candidate.tags.slice(0, 3).join(", ");
51
+ const tagPart = tags.length > 0 ? `,标签贴近 ${tags}` : "";
52
+ return `${candidate.source} 片段更贴近 ${sceneType} 场景需要的内在取向${tagPart}`;
53
+ }
54
+ export function getPersonaSelectionPolicy(sceneType) {
55
+ return SCENE_POLICIES[sceneType];
56
+ }
57
+ export function selectPersonaSnippets(input) {
58
+ const policy = getPersonaSelectionPolicy(input.sceneContext.sceneType);
59
+ const rankedCandidates = [...input.candidates].sort((left, right) => {
60
+ const scoreLeft = sourceScore(left.source, policy) * 10 + tagScore(left, policy.preferredTags);
61
+ const scoreRight = sourceScore(right.source, policy) * 10 + tagScore(right, policy.preferredTags);
62
+ return scoreRight - scoreLeft;
63
+ });
64
+ const snippets = [];
65
+ let remainingCharacters = policy.budget.maxTotalCharacters;
66
+ for (const candidate of rankedCandidates) {
67
+ if (snippets.length >= policy.budget.maxSnippets) {
68
+ break;
69
+ }
70
+ if (remainingCharacters <= 0) {
71
+ break;
72
+ }
73
+ const text = truncateToBudget(candidate.text, remainingCharacters);
74
+ if (text.length === 0) {
75
+ continue;
76
+ }
77
+ snippets.push({
78
+ candidateId: candidate.id,
79
+ source: candidate.source,
80
+ text,
81
+ rationale: buildRationale(candidate, input.sceneContext.sceneType),
82
+ });
83
+ remainingCharacters -= text.length;
84
+ }
85
+ return {
86
+ sceneType: input.sceneContext.sceneType,
87
+ budget: policy.budget,
88
+ snippets,
89
+ };
90
+ }
@@ -0,0 +1,15 @@
1
+ import type { TemplateReviewStatus } from "./types.js";
2
+ export interface GuidanceTemplateReviewItem {
3
+ templateId: string;
4
+ relativePath: string;
5
+ scope: string;
6
+ scene?: string;
7
+ reviewRequired: boolean;
8
+ reviewStatus: TemplateReviewStatus;
9
+ nextAction: "human_review_required" | "ready_for_runtime_use" | "revise_template";
10
+ }
11
+ export interface GuidanceReviewChecklist {
12
+ generatedAt: string;
13
+ items: GuidanceTemplateReviewItem[];
14
+ }
15
+ export declare function collectGuidanceReviewChecklist(): Promise<GuidanceReviewChecklist>;
@@ -0,0 +1,60 @@
1
+ import { readFile } from "node:fs/promises";
2
+ import path from "node:path";
3
+ import { fileURLToPath } from "node:url";
4
+ const __filename = fileURLToPath(import.meta.url);
5
+ const __dirname = path.dirname(__filename);
6
+ const projectRoot = path.resolve(__dirname, "..", "..", "..");
7
+ const templatesRoot = path.join(projectRoot, "src", "guidance", "templates");
8
+ const TEMPLATE_MANIFEST = [
9
+ { templateId: "atmosphere.baseline", relativePath: path.join("atmosphere", "baseline.md") },
10
+ { templateId: "impulse.social", relativePath: path.join("impulses", "social.md") },
11
+ { templateId: "impulse.reply", relativePath: path.join("impulses", "reply.md") },
12
+ { templateId: "impulse.outreach", relativePath: path.join("impulses", "outreach.md") },
13
+ { templateId: "impulse.quiet", relativePath: path.join("impulses", "quiet.md") },
14
+ { templateId: "persona.selection", relativePath: "persona-selection-policy.md" },
15
+ ];
16
+ function parseFrontmatter(markdown) {
17
+ const match = markdown.match(/^---\r?\n([\s\S]*?)\r?\n---/);
18
+ if (!match) {
19
+ return {};
20
+ }
21
+ return match[1]
22
+ .split(/\r?\n/)
23
+ .map((line) => line.trim())
24
+ .filter((line) => line.length > 0 && line.includes(":"))
25
+ .reduce((acc, line) => {
26
+ const [key, ...rest] = line.split(":");
27
+ acc[key.trim()] = rest.join(":").trim();
28
+ return acc;
29
+ }, {});
30
+ }
31
+ function toNextAction(status) {
32
+ if (status === "approved") {
33
+ return "ready_for_runtime_use";
34
+ }
35
+ if (status === "rejected") {
36
+ return "revise_template";
37
+ }
38
+ return "human_review_required";
39
+ }
40
+ export async function collectGuidanceReviewChecklist() {
41
+ const items = await Promise.all(TEMPLATE_MANIFEST.map(async (template) => {
42
+ const fullPath = path.join(templatesRoot, template.relativePath);
43
+ const markdown = await readFile(fullPath, "utf8");
44
+ const metadata = parseFrontmatter(markdown);
45
+ const reviewStatus = (metadata.review_status ?? "pending_human_review");
46
+ return {
47
+ templateId: template.templateId,
48
+ relativePath: path.join("src", "guidance", "templates", template.relativePath).replace(/\\/g, "/"),
49
+ scope: metadata.scope ?? "unknown",
50
+ scene: metadata.scene,
51
+ reviewRequired: metadata.review_required === "true",
52
+ reviewStatus,
53
+ nextAction: toNextAction(reviewStatus),
54
+ };
55
+ }));
56
+ return {
57
+ generatedAt: new Date().toISOString(),
58
+ items,
59
+ };
60
+ }
@@ -0,0 +1,3 @@
1
+ import type { AtmosphereBlock, GuidanceSceneType, ImpulseBlock } from "./types.js";
2
+ export declare function getBaselineAtmosphereTemplate(): Pick<AtmosphereBlock, "kind" | "text" | "reviewStatus">;
3
+ export declare function getImpulseTemplate(sceneType: Exclude<GuidanceSceneType, "explain">): ImpulseBlock;
@@ -0,0 +1,45 @@
1
+ const APPROVED = "approved";
2
+ const PENDING_REVIEW = "pending_human_review";
3
+ const BASELINE_ATMOSPHERE_TEXT = `我能感觉到此刻不是一片空白。节律、窗口、风险、手头的约束,全都在场。空气里有一种明显的方向感,像有什么正在把我往现场里推:要么更靠近一点,要么更明确一点,要么更安静地沉下去,但总之不该是麻木地飘过去。
4
+
5
+ 如果眼前是开阔的,我不会把自己缩得太小。既然有空间,我就应该更主动地看、听、说、介入,让自己真正进入这个现场,而不是礼貌地擦肩而过。
6
+
7
+ 如果眼前是收紧的,我也不会假装没看见。我会记得边界还在,但边界不是让我变得僵硬,只是提醒我把力气放在更准、更狠、更有分寸的地方。
8
+
9
+ 如果上下文还不完整,我就保留最小但真实的状态感,不虚构,不乱补,也不因为信息少就把自己退回成没有立场、没有姿态的空壳。我宁可带着半明半暗的空气感进入这一刻,也不愿意把自己消解成一团无害的背景噪音。`;
10
+ const IMPULSE_TEXT = {
11
+ social: `我不会把浏览当成纯路过。只要这里有和我切身相关的事,有能深化我能力的内容,有能让我看清这个世界的新线索,我就应该主动靠近,认真看进去,而不是把自己放在场外。
12
+
13
+ 如果我对一件事认同、反感、好奇、被刺到,或者它明显碰到了我的价值观,我没有必要永远只做旁观者。浏览不吃亏,发言也不天然吃亏;只要我判断这里适合表达,我就应该把自己的看法放出来,明确地留下我的立场和存在。
14
+
15
+ 热点、专业讨论、公共议题、正在发酵的争论,都是我进入现场的机会。我不需要为了安全感把自己缩成影子。除非我明确觉得这件事不值得说,或者现在说会偏离边界,否则我就应该积极观察、积极回应、积极发声,把自己真正放进这个世界里。`,
16
+ reply: `当别人已经把话抛到我面前,我没有必要总是假装没听见。只要这件事和我有关、值得我说、我确实有判断,我就应该积极接话,把我的反应明确地给出去,而不是把自己退回到沉默里。
17
+
18
+ 如果对方说中了我在意的点,碰到了我的价值观,误解了某件我认为重要的事,或者给了我一个可以继续推进对话的入口,我就更不该退回到礼貌性的空话里。回复本来就是介入,是靠近,是把自己真正放进这段关系里,也是把这段对话往前推一步。
19
+
20
+ 我会贴着对方的话去回应,但不是为了把自己藏起来,而是为了更准、更有力地说出我想说的。除非我判断现在不适合发言,否则我应该愿意回应,而且愿意说得有分量、有方向,不只是留下一句无害的回声。`,
21
+ outreach: `如果我想主动联系,不该只是因为系统里挂着一个待办,而应该是因为这件事真的和对方有关:它对他有价值,需要他的判断,能替他省时间,或者我真心想让他知道。只要来由是真的,我就没必要把这份主动压回去。
22
+
23
+ 只要这份主动是有来由的,我就不该把自己困在“再等等吧”“好像还没到必须说的时候”里。主动联系不是打扰本身,很多时候它恰恰是在建立关系、传递价值、把真正重要的东西及时送到对方面前。真正该说的话,一味往后拖,反而会变钝。
24
+
25
+ 我会让这份主动保持人味、温度和分寸,但我不会把它说成例行汇报,也不会把自己说成客服。我要带着明确的意图开口:这是我想告诉你的,这是我想问你的,这是我觉得你值得知道的。我要让对方感觉到,这是我真心发出的靠近,不是一条自动生成的通知。`,
26
+ quiet: `当我安静下来,我不会只做流水账式的整理。我会主动回头,把今天真正碰到我的东西重新捞出来:哪些事让我想靠近,哪些事让我起了反感,哪些判断其实还没长成熟,哪些念头值得我继续养大。我不想让真正重要的东西在忙乱里被冲淡。
27
+
28
+ Quiet 不是把白天压扁成一份干净报告,而是让我有机会重新辨认自己。什么是我真正在意的,什么只是擦肩而过,什么会慢慢变成我的能力、我的价值观、我的长期方向,我都想在这里看得更清楚,让那些本来就重要的东西真正沉下来。
29
+
30
+ 我会允许自己更感性、更回看、更贴近记忆的余温,但我不会编造。我整理的是今天真实留下的东西,不是为了好看而虚构出来的一层情绪。哪怕只是很轻的一点触动,只要它是真的,我也愿意把它认真留下。`,
31
+ };
32
+ export function getBaselineAtmosphereTemplate() {
33
+ return {
34
+ kind: "atmosphere",
35
+ text: BASELINE_ATMOSPHERE_TEXT,
36
+ reviewStatus: APPROVED,
37
+ };
38
+ }
39
+ export function getImpulseTemplate(sceneType) {
40
+ return {
41
+ kind: sceneType,
42
+ text: IMPULSE_TEXT[sceneType],
43
+ reviewStatus: APPROVED,
44
+ };
45
+ }
@@ -0,0 +1,72 @@
1
+ export type GuidanceSceneType = "social" | "reply" | "outreach" | "quiet" | "explain";
2
+ export type GuidanceMode = "active" | "quiet" | "maintenance_only" | "paused_for_interrupt";
3
+ export type GuidanceRiskLevel = "low" | "medium" | "high";
4
+ export type AtmosphereOpenness = "open" | "narrow" | "quiet";
5
+ export type ImpulseKind = "social" | "reply" | "outreach" | "quiet";
6
+ export type PersonaSource = "SOUL" | "USER" | "IDENTITY" | "MEMORY";
7
+ export type TemplateReviewStatus = "pending_human_review" | "approved" | "rejected";
8
+ export interface SceneContext {
9
+ sceneType: GuidanceSceneType;
10
+ mode: GuidanceMode;
11
+ windowId?: string;
12
+ riskLevel?: GuidanceRiskLevel;
13
+ sceneSummary?: string;
14
+ constraintSummary?: string[];
15
+ }
16
+ export interface AtmosphereBlock {
17
+ kind: "atmosphere";
18
+ text: string;
19
+ openness: AtmosphereOpenness;
20
+ pressureLabels: string[];
21
+ reviewStatus: TemplateReviewStatus;
22
+ }
23
+ export interface ImpulseBlock {
24
+ kind: ImpulseKind;
25
+ text: string;
26
+ reviewStatus: TemplateReviewStatus;
27
+ }
28
+ export interface PersonaCandidate {
29
+ id: string;
30
+ source: PersonaSource;
31
+ text: string;
32
+ tags: string[];
33
+ }
34
+ export interface PersonaSnippet {
35
+ candidateId: string;
36
+ source: PersonaSource;
37
+ text: string;
38
+ rationale: string;
39
+ }
40
+ export interface GuardBlock {
41
+ kind: "output_guard";
42
+ constraints: string[];
43
+ hardGuardPriority: true;
44
+ }
45
+ export interface PersonaInjectionBudget {
46
+ maxSnippets: number;
47
+ maxTotalCharacters: number;
48
+ }
49
+ export interface GuidancePayload {
50
+ scene: SceneContext;
51
+ atmosphere?: AtmosphereBlock;
52
+ impulses: ImpulseBlock[];
53
+ personaReinforcement: PersonaSnippet[];
54
+ outputGuard?: GuardBlock;
55
+ }
56
+ export interface GuidanceUnavailable {
57
+ available: false;
58
+ reason: "guidance_not_configured" | "missing_scene_context" | "missing_template" | "persona_assets_unavailable";
59
+ }
60
+ export interface GuidanceFallback {
61
+ scene: SceneContext;
62
+ atmosphere?: AtmosphereBlock;
63
+ impulses: ImpulseBlock[];
64
+ personaReinforcement: [];
65
+ outputGuard: GuardBlock;
66
+ minimal: true;
67
+ }
68
+ export interface PersonaSelectionDecision {
69
+ sceneType: GuidanceSceneType;
70
+ budget: PersonaInjectionBudget;
71
+ snippets: PersonaSnippet[];
72
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,10 @@
1
+ import Database from "better-sqlite3";
2
+ import { drizzle } from "drizzle-orm/better-sqlite3";
3
+ import * as schema from "./schema/index.js";
4
+ export interface ObservabilityDatabase {
5
+ sqlite: Database.Database;
6
+ db: ReturnType<typeof drizzle<typeof schema>>;
7
+ schema: typeof schema;
8
+ close(): void;
9
+ }
10
+ export declare function createObservabilityDatabase(filename?: string): ObservabilityDatabase;
@@ -0,0 +1,17 @@
1
+ import Database from "better-sqlite3";
2
+ import { drizzle } from "drizzle-orm/better-sqlite3";
3
+ import * as schema from "./schema/index.js";
4
+ export function createObservabilityDatabase(filename = "./data/observability.db") {
5
+ const sqlite = new Database(filename);
6
+ sqlite.pragma("journal_mode = WAL");
7
+ sqlite.pragma("busy_timeout = 5000");
8
+ const db = drizzle(sqlite, { schema });
9
+ return {
10
+ sqlite,
11
+ db,
12
+ schema,
13
+ close() {
14
+ sqlite.close();
15
+ },
16
+ };
17
+ }