@m6d/cortex-server 1.6.0 → 1.7.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.
@@ -10,9 +10,9 @@ export type KnowledgeConfig = {
10
10
  };
11
11
  domains?: Record<string, DomainDef>;
12
12
  };
13
- export type PromptContext = {
14
- session: Record<string, unknown> | null;
15
- requestContext: Record<string, unknown>;
13
+ export type PromptContext<TSession extends Record<string, unknown> = Record<string, unknown>, TRequestContext extends Record<string, unknown> = Record<string, unknown>> = {
14
+ session: TSession | null;
15
+ requestContext: TRequestContext;
16
16
  };
17
17
  export type DatabaseConfig = {
18
18
  type: "mssql";
@@ -26,8 +26,8 @@ export type StorageConfig = {
26
26
  secretKey: string;
27
27
  bucketName?: string;
28
28
  };
29
- export type CortexAgentDefinition = {
30
- systemPrompt: string | ((context: PromptContext) => string | Promise<string>);
29
+ export type CortexAgentDefinition<TSession extends Record<string, unknown> = Record<string, unknown>, TRequestContext extends Record<string, unknown> = Record<string, unknown>> = {
30
+ systemPrompt: string | ((context: PromptContext<TSession, TRequestContext>) => string | Promise<string>);
31
31
  tools?: ToolSet;
32
32
  backendFetch?: {
33
33
  baseUrl: string;
@@ -38,8 +38,8 @@ export type CortexAgentDefinition = {
38
38
  }) => Promise<Record<string, unknown>>;
39
39
  interceptor?: RequestInterceptorOptions;
40
40
  };
41
- loadSessionData?: (token: string) => Promise<Record<string, unknown>>;
42
- resolveRequestContext?: (request: Request) => Record<string, unknown> | Promise<Record<string, unknown>>;
41
+ loadSessionData?: (token: string) => Promise<TSession>;
42
+ resolveRequestContext?: (request: Request) => TRequestContext | Promise<TRequestContext>;
43
43
  onToolCall?: (toolCall: {
44
44
  toolName: string;
45
45
  toolCallId: string;
@@ -157,3 +157,9 @@ export type CortexConfig = {
157
157
  knowledge?: KnowledgeConfig;
158
158
  agents: Record<string, CortexAgentDefinition>;
159
159
  };
160
+ /**
161
+ * Helper to define an agent with full type inference for `systemPrompt` context.
162
+ * The `context.session` type is inferred from `loadSessionData`'s return type,
163
+ * and `context.requestContext` is inferred from `resolveRequestContext`'s return type.
164
+ */
165
+ export declare function defineAgent<TSession extends Record<string, unknown> = Record<string, unknown>, TRequestContext extends Record<string, unknown> = Record<string, unknown>>(config: CortexAgentDefinition<TSession, TRequestContext>): CortexAgentDefinition;
@@ -1,4 +1,5 @@
1
1
  export type { CortexConfig, CortexAgentDefinition, KnowledgeConfig, DatabaseConfig, StorageConfig, PromptContext, } from "./config";
2
+ export { defineAgent } from "./config";
2
3
  export type { ContextConfig, ThreadContextMeta } from "./ai/context/types";
3
4
  export type { Thread, AppEnv } from "./types";
4
5
  export type { CortexInstance } from "./factory";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@m6d/cortex-server",
3
- "version": "1.6.0",
3
+ "version": "1.7.0",
4
4
  "description": "Reusable AI agent chat server library for Hono + Bun",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/config.ts CHANGED
@@ -10,9 +10,12 @@ export type KnowledgeConfig = {
10
10
  domains?: Record<string, DomainDef>;
11
11
  };
12
12
 
13
- export type PromptContext = {
14
- session: Record<string, unknown> | null;
15
- requestContext: Record<string, unknown>;
13
+ export type PromptContext<
14
+ TSession extends Record<string, unknown> = Record<string, unknown>,
15
+ TRequestContext extends Record<string, unknown> = Record<string, unknown>,
16
+ > = {
17
+ session: TSession | null;
18
+ requestContext: TRequestContext;
16
19
  };
17
20
 
18
21
  export type DatabaseConfig = {
@@ -29,8 +32,13 @@ export type StorageConfig = {
29
32
  bucketName?: string;
30
33
  };
31
34
 
32
- export type CortexAgentDefinition = {
33
- systemPrompt: string | ((context: PromptContext) => string | Promise<string>);
35
+ export type CortexAgentDefinition<
36
+ TSession extends Record<string, unknown> = Record<string, unknown>,
37
+ TRequestContext extends Record<string, unknown> = Record<string, unknown>,
38
+ > = {
39
+ systemPrompt:
40
+ | string
41
+ | ((context: PromptContext<TSession, TRequestContext>) => string | Promise<string>);
34
42
  tools?: ToolSet;
35
43
  backendFetch?: {
36
44
  baseUrl: string;
@@ -42,10 +50,8 @@ export type CortexAgentDefinition = {
42
50
  ) => Promise<Record<string, unknown>>;
43
51
  interceptor?: RequestInterceptorOptions;
44
52
  };
45
- loadSessionData?: (token: string) => Promise<Record<string, unknown>>;
46
- resolveRequestContext?: (
47
- request: Request,
48
- ) => Record<string, unknown> | Promise<Record<string, unknown>>;
53
+ loadSessionData?: (token: string) => Promise<TSession>;
54
+ resolveRequestContext?: (request: Request) => TRequestContext | Promise<TRequestContext>;
49
55
  onToolCall?: (toolCall: {
50
56
  toolName: string;
51
57
  toolCallId: string;
@@ -162,3 +168,15 @@ export type CortexConfig = {
162
168
  knowledge?: KnowledgeConfig;
163
169
  agents: Record<string, CortexAgentDefinition>;
164
170
  };
171
+
172
+ /**
173
+ * Helper to define an agent with full type inference for `systemPrompt` context.
174
+ * The `context.session` type is inferred from `loadSessionData`'s return type,
175
+ * and `context.requestContext` is inferred from `resolveRequestContext`'s return type.
176
+ */
177
+ export function defineAgent<
178
+ TSession extends Record<string, unknown> = Record<string, unknown>,
179
+ TRequestContext extends Record<string, unknown> = Record<string, unknown>,
180
+ >(config: CortexAgentDefinition<TSession, TRequestContext>): CortexAgentDefinition {
181
+ return config as CortexAgentDefinition;
182
+ }
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ export type {
7
7
  StorageConfig,
8
8
  PromptContext,
9
9
  } from "./config";
10
+ export { defineAgent } from "./config";
10
11
 
11
12
  export type { ContextConfig, ThreadContextMeta } from "./ai/context/types";
12
13