koishi-plugin-chatluna 1.3.34 → 1.3.36

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 (40) hide show
  1. package/lib/chains/chain.d.ts +1 -1
  2. package/lib/commands/conversation.d.ts +33 -0
  3. package/lib/index.cjs +11 -8
  4. package/lib/index.mjs +12 -9
  5. package/lib/legacy/types.d.ts +29 -0
  6. package/lib/llm-core/agent/agent.d.ts +65 -0
  7. package/lib/llm-core/agent/creator.d.ts +9 -6
  8. package/lib/llm-core/agent/executor.d.ts +15 -40
  9. package/lib/llm-core/agent/index.cjs +1710 -501
  10. package/lib/llm-core/agent/index.d.ts +3 -0
  11. package/lib/llm-core/agent/index.mjs +1594 -390
  12. package/lib/llm-core/agent/legacy-executor.d.ts +49 -0
  13. package/lib/llm-core/agent/service.d.ts +31 -0
  14. package/lib/llm-core/agent/sub-agent.d.ts +95 -0
  15. package/lib/llm-core/agent/types.d.ts +4 -0
  16. package/lib/llm-core/chain/plugin_chat_chain.d.ts +3 -3
  17. package/lib/llm-core/platform/types.d.ts +21 -1
  18. package/lib/middlewares/conversation/request_conversation.d.ts +15 -0
  19. package/lib/middlewares/conversation/resolve_conversation.d.ts +14 -0
  20. package/lib/middlewares/model/request_conversation.d.ts +15 -0
  21. package/lib/middlewares/system/conversation_manage.d.ts +27 -0
  22. package/lib/migration/legacy_tables.d.ts +16 -0
  23. package/lib/migration/room_to_conversation.d.ts +5 -0
  24. package/lib/migration/validators.d.ts +75 -0
  25. package/lib/services/chat.cjs +38 -1
  26. package/lib/services/chat.d.ts +2 -1
  27. package/lib/services/chat.mjs +46 -2
  28. package/lib/services/conversation.d.ts +101 -0
  29. package/lib/services/conversation_runtime.d.ts +61 -0
  30. package/lib/services/conversation_types.d.ts +149 -0
  31. package/lib/services/types.d.ts +1 -0
  32. package/lib/utils/archive.d.ts +5 -0
  33. package/lib/utils/chat_request.d.ts +4 -0
  34. package/lib/utils/chatluna.cjs +55 -0
  35. package/lib/utils/chatluna.d.ts +12 -0
  36. package/lib/utils/chatluna.mjs +32 -0
  37. package/lib/utils/compression.d.ts +6 -0
  38. package/lib/utils/message_content.d.ts +8 -0
  39. package/lib/utils/model.d.ts +1 -0
  40. package/package.json +6 -1
@@ -46,7 +46,7 @@ export declare class ChainMiddleware {
46
46
  constructor(name: string, execute: ChainMiddlewareFunction, graph: ChatChainDependencyGraph);
47
47
  before<T extends keyof ChainMiddlewareName>(name: T): this;
48
48
  after<T extends keyof ChainMiddlewareName>(name: T): this;
49
- run(session: Session, options: ChainMiddlewareContext): Promise<string | h[] | h[][] | ChainMiddlewareRunStatus>;
49
+ run(session: Session, options: ChainMiddlewareContext): Promise<string | h[] | ChainMiddlewareRunStatus | h[][]>;
50
50
  }
51
51
  export interface ChainMiddlewareContext {
52
52
  config: Config;
@@ -0,0 +1,33 @@
1
+ import { Context } from 'koishi';
2
+ import { Config } from '../config';
3
+ import { ChatChain } from '../chains/chain';
4
+ export declare function apply(ctx: Context, config: Config, chain: ChatChain): void;
5
+ declare module '../chains/chain' {
6
+ interface ChainMiddlewareContextOptions {
7
+ conversation_create?: {
8
+ title?: string;
9
+ preset?: string;
10
+ model?: string;
11
+ chatMode?: string;
12
+ };
13
+ conversation_manage?: {
14
+ targetConversation?: string;
15
+ presetLane?: string;
16
+ includeArchived?: boolean;
17
+ title?: string;
18
+ };
19
+ conversation_use?: {
20
+ model?: string;
21
+ preset?: string;
22
+ chatMode?: string;
23
+ };
24
+ conversation_rule?: {
25
+ model?: string;
26
+ preset?: string;
27
+ chatMode?: string;
28
+ share?: string;
29
+ lock?: string;
30
+ };
31
+ i18n_base?: string;
32
+ }
33
+ }
package/lib/index.cjs CHANGED
@@ -1124,7 +1124,7 @@ var ChatLunaPluginChain = class _ChatLunaPluginChain extends import_base2.ChatLu
1124
1124
  static {
1125
1125
  __name(this, "ChatLunaPluginChain");
1126
1126
  }
1127
- executor;
1127
+ runner;
1128
1128
  historyMemory;
1129
1129
  systemPrompts;
1130
1130
  llm;
@@ -1163,7 +1163,7 @@ var ChatLunaPluginChain = class _ChatLunaPluginChain extends import_base2.ChatLu
1163
1163
  embeddings: this.embeddings,
1164
1164
  toolMask: this.toolMask
1165
1165
  });
1166
- this.executor = this._createExecutor();
1166
+ this.runner = this._createRunner();
1167
1167
  }
1168
1168
  static fromLLMAndTools(llm, tools, {
1169
1169
  historyMemory,
@@ -1194,8 +1194,8 @@ var ChatLunaPluginChain = class _ChatLunaPluginChain extends import_base2.ChatLu
1194
1194
  toolMask
1195
1195
  });
1196
1196
  }
1197
- _createExecutor() {
1198
- return (0, import_agent.createAgentExecutor)({
1197
+ _createRunner() {
1198
+ return (0, import_agent.createAgentRunner)({
1199
1199
  llm: (0, import_reactivity.computed)(() => this.llm),
1200
1200
  tools: this._toolsRef.tools,
1201
1201
  prompt: this.prompt,
@@ -1253,12 +1253,17 @@ var ChatLunaPluginChain = class _ChatLunaPluginChain extends import_base2.ChatLu
1253
1253
  };
1254
1254
  this._toolsRef.update(session, messages.concat(message), toolMask);
1255
1255
  const preset = this.preset.value;
1256
- const executor = this.executor.value;
1256
+ const runner = this.runner.value.withConfig({
1257
+ configurable: {
1258
+ messageQueue,
1259
+ onAgentEvent
1260
+ }
1261
+ });
1257
1262
  let usedToken = 0;
1258
1263
  let response;
1259
1264
  let error;
1260
1265
  const request2 = /* @__PURE__ */ __name(() => {
1261
- return executor.invoke(
1266
+ return runner.invoke(
1262
1267
  {
1263
1268
  ...requests,
1264
1269
  maxTokens: maxToken
@@ -1301,8 +1306,6 @@ var ChatLunaPluginChain = class _ChatLunaPluginChain extends import_base2.ChatLu
1301
1306
  preset: preset.triggerKeyword[0],
1302
1307
  userId: session.userId,
1303
1308
  toolMask,
1304
- messageQueue,
1305
- onAgentEvent,
1306
1309
  subagentContext
1307
1310
  }
1308
1311
  }
package/lib/index.mjs CHANGED
@@ -1092,7 +1092,7 @@ import {
1092
1092
  ChatLunaLLMChainWrapper as ChatLunaLLMChainWrapper2
1093
1093
  } from "koishi-plugin-chatluna/llm-core/chain/base";
1094
1094
  import {
1095
- createAgentExecutor,
1095
+ createAgentRunner,
1096
1096
  createToolsRef
1097
1097
  } from "koishi-plugin-chatluna/llm-core/agent";
1098
1098
  import { logger } from "koishi-plugin-chatluna";
@@ -1110,7 +1110,7 @@ var ChatLunaPluginChain = class _ChatLunaPluginChain extends ChatLunaLLMChainWra
1110
1110
  static {
1111
1111
  __name(this, "ChatLunaPluginChain");
1112
1112
  }
1113
- executor;
1113
+ runner;
1114
1114
  historyMemory;
1115
1115
  systemPrompts;
1116
1116
  llm;
@@ -1149,7 +1149,7 @@ var ChatLunaPluginChain = class _ChatLunaPluginChain extends ChatLunaLLMChainWra
1149
1149
  embeddings: this.embeddings,
1150
1150
  toolMask: this.toolMask
1151
1151
  });
1152
- this.executor = this._createExecutor();
1152
+ this.runner = this._createRunner();
1153
1153
  }
1154
1154
  static fromLLMAndTools(llm, tools, {
1155
1155
  historyMemory,
@@ -1180,8 +1180,8 @@ var ChatLunaPluginChain = class _ChatLunaPluginChain extends ChatLunaLLMChainWra
1180
1180
  toolMask
1181
1181
  });
1182
1182
  }
1183
- _createExecutor() {
1184
- return createAgentExecutor({
1183
+ _createRunner() {
1184
+ return createAgentRunner({
1185
1185
  llm: computed(() => this.llm),
1186
1186
  tools: this._toolsRef.tools,
1187
1187
  prompt: this.prompt,
@@ -1239,12 +1239,17 @@ var ChatLunaPluginChain = class _ChatLunaPluginChain extends ChatLunaLLMChainWra
1239
1239
  };
1240
1240
  this._toolsRef.update(session, messages.concat(message), toolMask);
1241
1241
  const preset = this.preset.value;
1242
- const executor = this.executor.value;
1242
+ const runner = this.runner.value.withConfig({
1243
+ configurable: {
1244
+ messageQueue,
1245
+ onAgentEvent
1246
+ }
1247
+ });
1243
1248
  let usedToken = 0;
1244
1249
  let response;
1245
1250
  let error;
1246
1251
  const request2 = /* @__PURE__ */ __name(() => {
1247
- return executor.invoke(
1252
+ return runner.invoke(
1248
1253
  {
1249
1254
  ...requests,
1250
1255
  maxTokens: maxToken
@@ -1287,8 +1292,6 @@ var ChatLunaPluginChain = class _ChatLunaPluginChain extends ChatLunaLLMChainWra
1287
1292
  preset: preset.triggerKeyword[0],
1288
1293
  userId: session.userId,
1289
1294
  toolMask,
1290
- messageQueue,
1291
- onAgentEvent,
1292
1295
  subagentContext
1293
1296
  }
1294
1297
  }
@@ -0,0 +1,29 @@
1
+ export interface ConversationRoom {
2
+ visibility: 'public' | 'private' | 'template_clone';
3
+ roomMasterId: string;
4
+ roomName: string;
5
+ roomId: number;
6
+ conversationId?: string;
7
+ preset: string;
8
+ model: string;
9
+ chatMode: string;
10
+ password?: string;
11
+ autoUpdate?: boolean;
12
+ updatedTime: Date;
13
+ }
14
+ export interface ConversationRoomMemberInfo {
15
+ userId: string;
16
+ roomId: number;
17
+ mute?: boolean;
18
+ roomPermission: 'owner' | 'admin' | 'member';
19
+ }
20
+ export interface ConversationRoomGroupInfo {
21
+ groupId: string;
22
+ roomId: number;
23
+ roomVisibility: 'public' | 'private' | 'template_clone';
24
+ }
25
+ export interface ConversationRoomUserInfo {
26
+ groupId?: string;
27
+ defaultRoomId: number;
28
+ userId: string;
29
+ }
@@ -0,0 +1,65 @@
1
+ import { BaseMessage, BaseMessageChunk, HumanMessage } from '@langchain/core/messages';
2
+ import { StructuredTool } from '@langchain/core/tools';
3
+ import { type ComputedRef } from '@vue/reactivity';
4
+ import { type Session } from 'koishi';
5
+ import { z } from 'zod';
6
+ import type { ChatLunaChatPrompt } from '../chain/prompt';
7
+ import type { ChatLunaBaseEmbeddings, ChatLunaChatModel } from 'koishi-plugin-chatluna/llm-core/platform/model';
8
+ import type { ChatLunaTool } from 'koishi-plugin-chatluna/llm-core/platform/types';
9
+ import type { PresetTemplate } from '../prompt';
10
+ import type { AgentRunnerOutput } from './executor';
11
+ import type { AgentEvent, MessageQueue, SubagentContext, ToolMask } from './types';
12
+ export interface CreateAgentOptions {
13
+ id?: string;
14
+ name?: string;
15
+ description?: string;
16
+ llm: ComputedRef<ChatLunaChatModel>;
17
+ embeddings: ChatLunaBaseEmbeddings;
18
+ tools: ComputedRef<ChatLunaTool[]>;
19
+ prompt: ChatLunaChatPrompt;
20
+ mode?: 'tool-calling' | 'react';
21
+ maxSteps?: number;
22
+ handleParsingErrors?: boolean | string | ((e: Error) => string);
23
+ instructions?: ComputedRef<string | undefined>;
24
+ returnIntermediateSteps?: boolean;
25
+ toolMask?: ToolMask;
26
+ }
27
+ export interface AgentGenerateOptions {
28
+ prompt: string | HumanMessage;
29
+ session?: Session;
30
+ conversationId?: string;
31
+ history?: BaseMessage[];
32
+ variables?: Record<string, any>;
33
+ signal?: AbortSignal;
34
+ maxToken?: number;
35
+ messageQueue?: MessageQueue;
36
+ toolMask?: ToolMask;
37
+ subagentContext?: SubagentContext;
38
+ source?: 'chatluna' | 'character';
39
+ onStep?: (event: AgentEvent) => Promise<void> | void;
40
+ onToken?: (token: string) => Promise<void> | void;
41
+ onChunk?: (chunk: BaseMessageChunk) => Promise<void> | void;
42
+ }
43
+ export interface AgentStream {
44
+ text: AsyncIterable<string>;
45
+ steps: AsyncIterable<AgentEvent>;
46
+ result: Promise<AgentRunnerOutput>;
47
+ }
48
+ export interface AgentToolOptions {
49
+ name?: string;
50
+ description?: string;
51
+ }
52
+ export interface ChatLunaAgent {
53
+ id: string;
54
+ name: string;
55
+ description: string;
56
+ generate(input: AgentGenerateOptions): Promise<AgentRunnerOutput>;
57
+ stream(input: AgentGenerateOptions): Promise<AgentStream>;
58
+ asTool(options?: AgentToolOptions): StructuredTool;
59
+ }
60
+ export interface CreateAgentToolOptions extends AgentToolOptions {
61
+ schema?: z.ZodTypeAny;
62
+ }
63
+ export declare function createAgent(options: CreateAgentOptions): ChatLunaAgent;
64
+ export declare function createAgentTool(agent: ChatLunaAgent, options?: CreateAgentToolOptions): StructuredTool;
65
+ export declare function createPromptPreset(name: string, system?: string, preset?: ComputedRef<PresetTemplate>): ComputedRef<PresetTemplate>;
@@ -6,31 +6,34 @@ import { ChatLunaTool } from '../platform/types';
6
6
  import { BaseMessage } from '@langchain/core/messages';
7
7
  import { Session } from 'koishi';
8
8
  import type { Runnable } from '@langchain/core/runnables';
9
- import { AgentExecutor } from './executor';
9
+ import { AgentRunner } from './executor';
10
10
  import { ToolMask } from './types';
11
11
  export interface CreateAgentConfigOptions {
12
12
  llm: ComputedRef<ChatLunaChatModel>;
13
13
  tools: ComputedRef<StructuredTool[]>;
14
14
  prompt: ChatLunaChatPrompt;
15
15
  agentMode: 'react' | 'tool-calling';
16
- instructions?: ComputedRef<string>;
16
+ instructions?: ComputedRef<string | undefined>;
17
17
  }
18
18
  export interface AgentConfig {
19
19
  agent: Runnable;
20
20
  tools: StructuredTool[];
21
21
  agentMode: 'react' | 'tool-calling';
22
22
  }
23
- export interface CreateAgentExecutorOptions {
23
+ export interface CreateAgentRunnerOptions {
24
24
  llm: ComputedRef<ChatLunaChatModel>;
25
25
  tools: ComputedRef<StructuredTool[]>;
26
26
  prompt: ChatLunaChatPrompt;
27
27
  agentMode: 'react' | 'tool-calling';
28
+ maxIterations?: number;
28
29
  returnIntermediateSteps?: boolean;
29
- handleParsingErrors?: boolean;
30
- instructions?: ComputedRef<string>;
30
+ handleParsingErrors?: boolean | string | ((e: Error) => string);
31
+ instructions?: ComputedRef<string | undefined>;
31
32
  }
33
+ export type CreateAgentExecutorOptions = CreateAgentRunnerOptions;
32
34
  export declare function createAgentConfig(options: CreateAgentConfigOptions): ComputedRef<AgentConfig>;
33
- export declare function createAgentExecutor(options: CreateAgentExecutorOptions): ComputedRef<AgentExecutor>;
35
+ export declare function createAgentRunner(options: CreateAgentRunnerOptions): ComputedRef<AgentRunner>;
36
+ export declare const createAgentExecutor: typeof createAgentRunner;
34
37
  export interface CreateToolsRefOptions {
35
38
  tools: ComputedRef<ChatLunaTool[]>;
36
39
  embeddings: ChatLunaBaseEmbeddings;
@@ -1,49 +1,24 @@
1
- import { CallbackManagerForChainRun } from '@langchain/core/callbacks/manager';
2
- import { AIMessage } from '@langchain/core/messages';
3
- import { Runnable, type RunnableConfig } from '@langchain/core/runnables';
4
- import { StructuredTool, ToolInputParsingException } from '@langchain/core/tools';
1
+ import { Runnable, RunnableConfig } from '@langchain/core/runnables';
2
+ import type { StructuredTool } from '@langchain/core/tools';
5
3
  import type { ChainValues } from '@langchain/core/utils/types';
6
- import { BaseChain, ChainInputs } from 'koishi-plugin-chatluna/llm-core/chain/base';
7
- import { AgentEvent, AgentObservation, AgentStep, MessageQueue } from './types';
8
- export declare function runAgent(options: RunAgentOptions): AsyncGenerator<AgentEvent>;
9
- export declare class AgentExecutor extends BaseChain<ChainValues, AgentExecutorOutput> {
4
+ import type { AgentExecutorInput, AgentExecutorOutput } from './legacy-executor';
5
+ export { coerceToAgentObservation, LegacyAgentExecutor, runAgent, toToolInputErrorObservation, type AgentExecutorInput, type AgentExecutorOutput, type RunAgentOptions } from './legacy-executor';
6
+ export declare class AgentRunner extends Runnable<ChainValues, AgentRunnerOutput> {
10
7
  lc_serializable: boolean;
8
+ lc_namespace: string[];
11
9
  agent: Runnable;
12
10
  tools: StructuredTool[];
13
11
  returnIntermediateSteps: boolean;
14
12
  maxIterations?: number;
15
13
  handleParsingErrors?: boolean | string | ((e: Error) => string);
16
14
  handleToolRuntimeErrors?: (e: Error) => string;
17
- constructor(fields: AgentExecutorInput);
18
- get inputKeys(): string[];
19
- get outputKeys(): string[];
20
- static fromAgentAndTools(fields: AgentExecutorInput): AgentExecutor;
21
- _call(inputs: ChainValues, runManager?: CallbackManagerForChainRun, config?: RunnableConfig): Promise<AgentExecutorOutput>;
22
- _chainType(): "agent_executor";
15
+ constructor(fields: AgentRunnerInput);
16
+ static fromAgentAndTools(fields: AgentRunnerInput): AgentRunner;
17
+ private _run;
18
+ invoke(input: ChainValues, options?: RunnableConfig): Promise<AgentRunnerOutput>;
19
+ _streamIterator(input: ChainValues, options?: RunnableConfig): AsyncGenerator<AgentRunnerOutput>;
23
20
  }
24
- export interface RunAgentOptions {
25
- agent: Runnable;
26
- tools: StructuredTool[];
27
- input: ChainValues;
28
- messageQueue?: MessageQueue;
29
- signal?: AbortSignal;
30
- maxIterations?: number;
31
- handleParsingErrors?: boolean | string | ((e: Error) => string);
32
- handleToolRuntimeErrors?: (e: Error) => string;
33
- config?: RunnableConfig;
34
- }
35
- export interface AgentExecutorInput extends ChainInputs {
36
- agent: Runnable;
37
- tools: StructuredTool[];
38
- returnIntermediateSteps?: boolean;
39
- maxIterations?: number;
40
- handleParsingErrors?: boolean | string | ((e: Error) => string);
41
- handleToolRuntimeErrors?: (e: Error) => string;
42
- }
43
- export interface AgentExecutorOutput extends ChainValues {
44
- output: string;
45
- intermediateSteps?: AgentStep[];
46
- message: AIMessage;
47
- }
48
- export declare function coerceToAgentObservation(observation: unknown, toolName?: string): AgentObservation;
49
- export declare function toToolInputErrorObservation(handleParsingErrors: boolean | string | ((e: Error) => string), error: ToolInputParsingException): AgentObservation;
21
+ export declare const AgentExecutor: typeof AgentRunner;
22
+ export type AgentExecutor = AgentRunner;
23
+ export type AgentRunnerInput = AgentExecutorInput;
24
+ export type AgentRunnerOutput = AgentExecutorOutput;