@valuya/channel-access-core 0.2.0-beta.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Valuya
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,8 @@
1
+ # @valuya/channel-access-core
2
+
3
+ Shared contracts and runtime helpers for channel-access packages such as:
4
+
5
+ - `@valuya/whatsapp-channel-access`
6
+ - `@valuya/telegram-channel-access`
7
+
8
+ This package is intentionally channel-agnostic. Channel-specific resource building, linking, and access resolution stay in the transport packages.
@@ -0,0 +1,195 @@
1
+ export type LogFn = (event: string, fields: Record<string, unknown>) => void;
2
+ export type ChannelAccessState = "paid_active" | "trial_active" | "expired_payment_required" | "inactive";
3
+ export type ChannelMode = {
4
+ kind: "human";
5
+ } | {
6
+ kind: "agent";
7
+ soulId: string;
8
+ };
9
+ export type SoulConfig = {
10
+ id: number | string;
11
+ slug: string;
12
+ name: string;
13
+ version: number | string;
14
+ capabilities?: Record<string, unknown> | null;
15
+ tool_policy?: Record<string, unknown> | null;
16
+ memory_policy?: Record<string, unknown> | null;
17
+ compiled_prompt_artifacts?: Record<string, unknown> | null;
18
+ };
19
+ export type ChannelRuntimeConfig = {
20
+ mode: "human" | "agent";
21
+ channel: string;
22
+ channel_kind: string;
23
+ provider: string | null;
24
+ channel_app_id?: string | null;
25
+ visit_url: string | null;
26
+ human_routing?: Record<string, unknown> | null;
27
+ agent_routing?: Record<string, unknown> | null;
28
+ fallback?: {
29
+ allowed: boolean;
30
+ mode: "human";
31
+ } | null;
32
+ soul: SoulConfig | null;
33
+ };
34
+ export type ChannelAccessResolveRequest = {
35
+ resource: string;
36
+ plan: string;
37
+ channel?: {
38
+ kind?: string | null;
39
+ provider?: string | null;
40
+ channel_identifier?: string | null;
41
+ phone_number?: string | null;
42
+ bot_name?: string | null;
43
+ chat_id?: string | null;
44
+ } | null;
45
+ };
46
+ export type ChannelAccessResolveResponse = {
47
+ ok: boolean;
48
+ state: ChannelAccessState;
49
+ resource: string;
50
+ anchor_resource: string;
51
+ plan: string;
52
+ expires_at: string | null;
53
+ payment_url: string | null;
54
+ reason: string | null;
55
+ runtime_config: ChannelRuntimeConfig | null;
56
+ capabilities: {
57
+ channel_access_version: string;
58
+ [key: string]: unknown;
59
+ };
60
+ };
61
+ export type LegacyEntitlementResponse = {
62
+ active?: boolean;
63
+ reason?: string;
64
+ evaluated_plan?: string;
65
+ expires_at?: string | null;
66
+ payment_url?: string | null;
67
+ state?: string;
68
+ required?: Record<string, unknown> | null;
69
+ };
70
+ export type SoulDefinition = {
71
+ id: string;
72
+ name: string;
73
+ systemPrompt: string;
74
+ locale?: string;
75
+ memoryPolicy?: {
76
+ keepRecentTurns: number;
77
+ summarizeAfterTurns: number;
78
+ };
79
+ tools?: string[];
80
+ };
81
+ export type SoulMemoryTurn = {
82
+ role: "user" | "assistant";
83
+ content: string;
84
+ createdAt: string;
85
+ };
86
+ export type SoulMemory = {
87
+ recentTurns: SoulMemoryTurn[];
88
+ summaries: string[];
89
+ userProfile?: Record<string, unknown>;
90
+ updatedAt: string;
91
+ };
92
+ export type SoulResponse = {
93
+ reply: string;
94
+ memory?: SoulMemory;
95
+ metadata?: Record<string, unknown>;
96
+ };
97
+ export interface MemoryStore {
98
+ load(args: {
99
+ userId: string;
100
+ soulId: string;
101
+ }): Promise<SoulMemory>;
102
+ save(args: {
103
+ userId: string;
104
+ soulId: string;
105
+ memory: SoulMemory;
106
+ }): Promise<void>;
107
+ }
108
+ export interface SoulRuntime {
109
+ run(args: {
110
+ soul: SoulDefinition;
111
+ message: string;
112
+ memory: SoulMemory;
113
+ protocolSubjectHeader: string;
114
+ locale?: string;
115
+ }): Promise<SoulResponse>;
116
+ }
117
+ export interface GuardToolClient {
118
+ getChannelAccessState(args: {
119
+ protocolSubjectHeader: string;
120
+ resource: string;
121
+ plan: string;
122
+ }): Promise<Record<string, unknown>>;
123
+ getEntitlements(args: {
124
+ protocolSubjectHeader: string;
125
+ resource: string;
126
+ plan: string;
127
+ }): Promise<Record<string, unknown>>;
128
+ getRecentOrders?(args: {
129
+ protocolSubjectHeader: string;
130
+ }): Promise<Record<string, unknown>>;
131
+ getRecentPayments?(args: {
132
+ protocolSubjectHeader: string;
133
+ }): Promise<Record<string, unknown>>;
134
+ }
135
+ export declare class InMemoryMemoryStore implements MemoryStore {
136
+ private readonly store;
137
+ load(args: {
138
+ userId: string;
139
+ soulId: string;
140
+ }): Promise<SoulMemory>;
141
+ save(args: {
142
+ userId: string;
143
+ soulId: string;
144
+ memory: SoulMemory;
145
+ }): Promise<void>;
146
+ }
147
+ export declare class OpenAISoulRuntimeAdapter implements SoulRuntime {
148
+ private readonly args;
149
+ constructor(args: {
150
+ runCompletion: (args: {
151
+ system: string;
152
+ user: string;
153
+ locale?: string;
154
+ soul: SoulDefinition;
155
+ }) => Promise<SoulResponse>;
156
+ });
157
+ run(args: {
158
+ soul: SoulDefinition;
159
+ message: string;
160
+ memory: SoulMemory;
161
+ protocolSubjectHeader: string;
162
+ locale?: string;
163
+ }): Promise<SoulResponse>;
164
+ }
165
+ export declare class StaticSoulRuntime implements SoulRuntime {
166
+ private readonly reply;
167
+ constructor(reply: string);
168
+ run(): Promise<SoulResponse>;
169
+ }
170
+ export declare function createGuardReadTools(client: GuardToolClient): {
171
+ getChannelAccessState(args: {
172
+ protocolSubjectHeader: string;
173
+ resource: string;
174
+ plan: string;
175
+ }): Promise<Record<string, unknown>>;
176
+ getEntitlements(args: {
177
+ protocolSubjectHeader: string;
178
+ resource: string;
179
+ plan: string;
180
+ }): Promise<Record<string, unknown>>;
181
+ getRecentOrders(args: {
182
+ protocolSubjectHeader: string;
183
+ }): Promise<{}>;
184
+ getRecentPayments(args: {
185
+ protocolSubjectHeader: string;
186
+ }): Promise<{}>;
187
+ };
188
+ export declare function appendMemory(memory: SoulMemory, userMessage: string, assistantReply: string): SoulMemory;
189
+ export declare function buildAllowedAccessReply(args: {
190
+ visitUrl?: string | null;
191
+ state: "paid_active" | "trial_active";
192
+ expiresAt?: string;
193
+ language?: "de" | "en";
194
+ }): string;
195
+ export declare function buildRuntimeErrorReply(error: "runtime_missing" | "agent_misconfigured", language?: "de" | "en"): string;
package/dist/index.js ADDED
@@ -0,0 +1,108 @@
1
+ export class InMemoryMemoryStore {
2
+ store = new Map();
3
+ async load(args) {
4
+ return this.store.get(memoryKey(args.userId, args.soulId)) || emptyMemory();
5
+ }
6
+ async save(args) {
7
+ this.store.set(memoryKey(args.userId, args.soulId), args.memory);
8
+ }
9
+ }
10
+ export class OpenAISoulRuntimeAdapter {
11
+ args;
12
+ constructor(args) {
13
+ this.args = args;
14
+ }
15
+ async run(args) {
16
+ const memorySummary = args.memory.summaries.join("\n") || "(no summary)";
17
+ const recentTurns = args.memory.recentTurns.map((turn) => `${turn.role}: ${turn.content}`).join("\n") || "(no history)";
18
+ return this.args.runCompletion({
19
+ system: args.soul.systemPrompt,
20
+ user: [
21
+ `Protocol subject: ${args.protocolSubjectHeader}`,
22
+ `Memory summary:\n${memorySummary}`,
23
+ `Recent turns:\n${recentTurns}`,
24
+ `Current message:\n${args.message}`,
25
+ ].join("\n\n"),
26
+ locale: args.locale,
27
+ soul: args.soul,
28
+ });
29
+ }
30
+ }
31
+ export class StaticSoulRuntime {
32
+ reply;
33
+ constructor(reply) {
34
+ this.reply = reply;
35
+ }
36
+ async run() {
37
+ return { reply: this.reply };
38
+ }
39
+ }
40
+ export function createGuardReadTools(client) {
41
+ return {
42
+ async getChannelAccessState(args) {
43
+ return client.getChannelAccessState(args);
44
+ },
45
+ async getEntitlements(args) {
46
+ return client.getEntitlements(args);
47
+ },
48
+ async getRecentOrders(args) {
49
+ return client.getRecentOrders?.(args) || {};
50
+ },
51
+ async getRecentPayments(args) {
52
+ return client.getRecentPayments?.(args) || {};
53
+ },
54
+ };
55
+ }
56
+ export function appendMemory(memory, userMessage, assistantReply) {
57
+ const nextTurns = [
58
+ ...memory.recentTurns,
59
+ { role: "user", content: userMessage, createdAt: new Date().toISOString() },
60
+ { role: "assistant", content: assistantReply, createdAt: new Date().toISOString() },
61
+ ].slice(-12);
62
+ return {
63
+ ...memory,
64
+ recentTurns: nextTurns,
65
+ updatedAt: new Date().toISOString(),
66
+ };
67
+ }
68
+ export function buildAllowedAccessReply(args) {
69
+ const language = args.language || "en";
70
+ if (language === "de") {
71
+ return [
72
+ args.state === "trial_active"
73
+ ? "Dein Zugang ist aktiv. Dein kostenloser Zugang laeuft aktuell."
74
+ : "Dein Zugang ist aktiv.",
75
+ args.expiresAt ? `Gueltig bis: ${args.expiresAt}.` : null,
76
+ args.visitUrl ? `Direktlink: ${args.visitUrl}` : null,
77
+ ].filter(Boolean).join("\n");
78
+ }
79
+ return [
80
+ args.state === "trial_active"
81
+ ? "Your access is active. Your free access is currently running."
82
+ : "Your access is active.",
83
+ args.expiresAt ? `Valid until: ${args.expiresAt}.` : null,
84
+ args.visitUrl ? `Direct link: ${args.visitUrl}` : null,
85
+ ].filter(Boolean).join("\n");
86
+ }
87
+ export function buildRuntimeErrorReply(error, language = "en") {
88
+ if (language === "de") {
89
+ if (error === "agent_misconfigured") {
90
+ return "Dein Zugang ist aktiv, aber der Agent ist gerade nicht korrekt konfiguriert. Bitte spaeter erneut versuchen.";
91
+ }
92
+ return "Dein Zugang ist aktiv, aber fuer diesen Kanal ist noch keine Laufzeit konfiguriert.";
93
+ }
94
+ if (error === "agent_misconfigured") {
95
+ return "Your access is active, but the agent is currently misconfigured. Please try again later.";
96
+ }
97
+ return "Your access is active, but no runtime is configured for this channel yet.";
98
+ }
99
+ function memoryKey(userId, soulId) {
100
+ return `${userId}::${soulId}`;
101
+ }
102
+ function emptyMemory() {
103
+ return {
104
+ recentTurns: [],
105
+ summaries: [],
106
+ updatedAt: new Date(0).toISOString(),
107
+ };
108
+ }
package/package.json ADDED
@@ -0,0 +1,31 @@
1
+ {
2
+ "name": "@valuya/channel-access-core",
3
+ "version": "0.2.0-beta.1",
4
+ "description": "Shared channel-access contracts and runtime helpers for Valuya Guard channel packages",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "default": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist/index.js",
16
+ "dist/index.d.ts",
17
+ "README.md"
18
+ ],
19
+ "devDependencies": {
20
+ "@types/node": "^25.0.10"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public",
24
+ "tag": "next"
25
+ },
26
+ "license": "MIT",
27
+ "scripts": {
28
+ "build": "tsc -p tsconfig.json",
29
+ "test": "pnpm run build && node --test dist/*.test.js"
30
+ }
31
+ }