agentlang 0.0.2
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 +83 -0
- package/README.md +120 -0
- package/bin/cli.js +4 -0
- package/out/api/http.d.ts +3 -0
- package/out/api/http.d.ts.map +1 -0
- package/out/api/http.js +290 -0
- package/out/api/http.js.map +1 -0
- package/out/cli/cli-util.d.ts +7 -0
- package/out/cli/cli-util.d.ts.map +1 -0
- package/out/cli/cli-util.js +9 -0
- package/out/cli/cli-util.js.map +1 -0
- package/out/cli/docs.d.ts +2 -0
- package/out/cli/docs.d.ts.map +1 -0
- package/out/cli/docs.js +236 -0
- package/out/cli/docs.js.map +1 -0
- package/out/cli/main.d.ts +288 -0
- package/out/cli/main.d.ts.map +1 -0
- package/out/cli/main.js +119 -0
- package/out/cli/main.js.map +1 -0
- package/out/cli/openapi-docs.yml +695 -0
- package/out/extension/main.cjs +18093 -0
- package/out/extension/main.cjs.map +7 -0
- package/out/extension/main.d.ts +4 -0
- package/out/extension/main.d.ts.map +1 -0
- package/out/extension/main.js +42 -0
- package/out/extension/main.js.map +1 -0
- package/out/language/agentlang-module.d.ts +42 -0
- package/out/language/agentlang-module.d.ts.map +1 -0
- package/out/language/agentlang-module.js +42 -0
- package/out/language/agentlang-module.js.map +1 -0
- package/out/language/agentlang-validator.d.ts +15 -0
- package/out/language/agentlang-validator.d.ts.map +1 -0
- package/out/language/agentlang-validator.js +50 -0
- package/out/language/agentlang-validator.js.map +1 -0
- package/out/language/generated/ast.d.ts +491 -0
- package/out/language/generated/ast.d.ts.map +1 -0
- package/out/language/generated/ast.js +934 -0
- package/out/language/generated/ast.js.map +1 -0
- package/out/language/generated/grammar.d.ts +7 -0
- package/out/language/generated/grammar.d.ts.map +1 -0
- package/out/language/generated/grammar.js +4475 -0
- package/out/language/generated/grammar.js.map +1 -0
- package/out/language/generated/module.d.ts +14 -0
- package/out/language/generated/module.d.ts.map +1 -0
- package/out/language/generated/module.js +21 -0
- package/out/language/generated/module.js.map +1 -0
- package/out/language/main-browser.d.ts +2 -0
- package/out/language/main-browser.d.ts.map +1 -0
- package/out/language/main-browser.js +10 -0
- package/out/language/main-browser.js.map +1 -0
- package/out/language/main.cjs +36229 -0
- package/out/language/main.cjs.map +7 -0
- package/out/language/main.d.ts +2 -0
- package/out/language/main.d.ts.map +1 -0
- package/out/language/main.js +11 -0
- package/out/language/main.js.map +1 -0
- package/out/language/parser.d.ts +9 -0
- package/out/language/parser.d.ts.map +1 -0
- package/out/language/parser.js +273 -0
- package/out/language/parser.js.map +1 -0
- package/out/language/syntax.d.ts +155 -0
- package/out/language/syntax.d.ts.map +1 -0
- package/out/language/syntax.js +527 -0
- package/out/language/syntax.js.map +1 -0
- package/out/runtime/agents/common.d.ts +2 -0
- package/out/runtime/agents/common.d.ts.map +1 -0
- package/out/runtime/agents/common.js +178 -0
- package/out/runtime/agents/common.js.map +1 -0
- package/out/runtime/agents/impl/openai.d.ts +8 -0
- package/out/runtime/agents/impl/openai.d.ts.map +1 -0
- package/out/runtime/agents/impl/openai.js +15 -0
- package/out/runtime/agents/impl/openai.js.map +1 -0
- package/out/runtime/agents/provider.d.ts +21 -0
- package/out/runtime/agents/provider.d.ts.map +1 -0
- package/out/runtime/agents/provider.js +32 -0
- package/out/runtime/agents/provider.js.map +1 -0
- package/out/runtime/agents/registry.d.ts +2 -0
- package/out/runtime/agents/registry.d.ts.map +1 -0
- package/out/runtime/agents/registry.js +10 -0
- package/out/runtime/agents/registry.js.map +1 -0
- package/out/runtime/auth/cognito.d.ts +16 -0
- package/out/runtime/auth/cognito.d.ts.map +1 -0
- package/out/runtime/auth/cognito.js +186 -0
- package/out/runtime/auth/cognito.js.map +1 -0
- package/out/runtime/auth/defs.d.ts +11 -0
- package/out/runtime/auth/defs.d.ts.map +1 -0
- package/out/runtime/auth/defs.js +24 -0
- package/out/runtime/auth/defs.js.map +1 -0
- package/out/runtime/auth/interface.d.ts +22 -0
- package/out/runtime/auth/interface.d.ts.map +1 -0
- package/out/runtime/auth/interface.js +2 -0
- package/out/runtime/auth/interface.js.map +1 -0
- package/out/runtime/defs.js +24 -0
- package/out/runtime/defs.js.map +1 -0
- package/out/runtime/interpreter.d.ts +69 -0
- package/out/runtime/interpreter.d.ts.map +1 -0
- package/out/runtime/interpreter.js +1163 -0
- package/out/runtime/interpreter.js.map +1 -0
- package/out/runtime/loader.d.ts +25 -0
- package/out/runtime/loader.d.ts.map +1 -0
- package/out/runtime/loader.js +346 -0
- package/out/runtime/loader.js.map +1 -0
- package/out/runtime/logger.d.ts +2 -0
- package/out/runtime/logger.d.ts.map +1 -0
- package/out/runtime/logger.js +44 -0
- package/out/runtime/logger.js.map +1 -0
- package/out/runtime/module.d.ts +273 -0
- package/out/runtime/module.d.ts.map +1 -0
- package/out/runtime/module.js +1786 -0
- package/out/runtime/module.js.map +1 -0
- package/out/runtime/modules/ai.d.ts +26 -0
- package/out/runtime/modules/ai.d.ts.map +1 -0
- package/out/runtime/modules/ai.js +211 -0
- package/out/runtime/modules/ai.js.map +1 -0
- package/out/runtime/modules/auth.d.ts +39 -0
- package/out/runtime/modules/auth.d.ts.map +1 -0
- package/out/runtime/modules/auth.js +359 -0
- package/out/runtime/modules/auth.js.map +1 -0
- package/out/runtime/modules/core.d.ts +2 -0
- package/out/runtime/modules/core.d.ts.map +1 -0
- package/out/runtime/modules/core.js +67 -0
- package/out/runtime/modules/core.js.map +1 -0
- package/out/runtime/relgraph.d.ts +21 -0
- package/out/runtime/relgraph.d.ts.map +1 -0
- package/out/runtime/relgraph.js +156 -0
- package/out/runtime/relgraph.js.map +1 -0
- package/out/runtime/resolvers/interface.d.ts +59 -0
- package/out/runtime/resolvers/interface.d.ts.map +1 -0
- package/out/runtime/resolvers/interface.js +111 -0
- package/out/runtime/resolvers/interface.js.map +1 -0
- package/out/runtime/resolvers/registry.d.ts +8 -0
- package/out/runtime/resolvers/registry.d.ts.map +1 -0
- package/out/runtime/resolvers/registry.js +26 -0
- package/out/runtime/resolvers/registry.js.map +1 -0
- package/out/runtime/resolvers/sqldb/database.d.ts +50 -0
- package/out/runtime/resolvers/sqldb/database.d.ts.map +1 -0
- package/out/runtime/resolvers/sqldb/database.js +618 -0
- package/out/runtime/resolvers/sqldb/database.js.map +1 -0
- package/out/runtime/resolvers/sqldb/dbutil.d.ts +18 -0
- package/out/runtime/resolvers/sqldb/dbutil.d.ts.map +1 -0
- package/out/runtime/resolvers/sqldb/dbutil.js +221 -0
- package/out/runtime/resolvers/sqldb/dbutil.js.map +1 -0
- package/out/runtime/resolvers/sqldb/impl.d.ts +26 -0
- package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -0
- package/out/runtime/resolvers/sqldb/impl.js +300 -0
- package/out/runtime/resolvers/sqldb/impl.js.map +1 -0
- package/out/runtime/state.js +83 -0
- package/out/runtime/state.js.map +1 -0
- package/out/runtime/util.d.ts +43 -0
- package/out/runtime/util.d.ts.map +1 -0
- package/out/runtime/util.js +447 -0
- package/out/runtime/util.js.map +1 -0
- package/out/setupClassic.d.ts +98 -0
- package/out/setupClassic.d.ts.map +1 -0
- package/out/setupClassic.js +38 -0
- package/out/setupClassic.js.map +1 -0
- package/out/setupCommon.d.ts +2 -0
- package/out/setupCommon.d.ts.map +1 -0
- package/out/setupCommon.js +33 -0
- package/out/setupCommon.js.map +1 -0
- package/out/setupExtended.d.ts +40 -0
- package/out/setupExtended.d.ts.map +1 -0
- package/out/setupExtended.js +67 -0
- package/out/setupExtended.js.map +1 -0
- package/out/syntaxes/agentlang.monarch.d.ts +77 -0
- package/out/syntaxes/agentlang.monarch.d.ts.map +1 -0
- package/out/syntaxes/agentlang.monarch.js +31 -0
- package/out/syntaxes/agentlang.monarch.js.map +1 -0
- package/out/utils/fs/index.d.ts +14 -0
- package/out/utils/fs/index.d.ts.map +1 -0
- package/out/utils/fs/index.js +26 -0
- package/out/utils/fs/index.js.map +1 -0
- package/out/utils/fs/interfaces.d.ts +105 -0
- package/out/utils/fs/interfaces.d.ts.map +1 -0
- package/out/utils/fs/interfaces.js +5 -0
- package/out/utils/fs/interfaces.js.map +1 -0
- package/out/utils/fs/lightning-fs.d.ts +116 -0
- package/out/utils/fs/lightning-fs.d.ts.map +1 -0
- package/out/utils/fs/lightning-fs.js +243 -0
- package/out/utils/fs/lightning-fs.js.map +1 -0
- package/out/utils/fs/node-fs.d.ts +93 -0
- package/out/utils/fs/node-fs.d.ts.map +1 -0
- package/out/utils/fs/node-fs.js +169 -0
- package/out/utils/fs/node-fs.js.map +1 -0
- package/out/utils/fs-utils.d.ts +153 -0
- package/out/utils/fs-utils.d.ts.map +1 -0
- package/out/utils/fs-utils.js +271 -0
- package/out/utils/fs-utils.js.map +1 -0
- package/out/utils/runtime.d.ts +36 -0
- package/out/utils/runtime.d.ts.map +1 -0
- package/out/utils/runtime.js +39 -0
- package/out/utils/runtime.js.map +1 -0
- package/package.json +155 -0
- package/src/api/http.ts +361 -0
- package/src/cli/cli-util.ts +18 -0
- package/src/cli/main.ts +146 -0
- package/src/extension/main.ts +51 -0
- package/src/language/agentlang-module.ts +75 -0
- package/src/language/agentlang-validator.ts +60 -0
- package/src/language/agentlang.langium +178 -0
- package/src/language/generated/ast.ts +1698 -0
- package/src/language/generated/grammar.ts +4477 -0
- package/src/language/generated/module.ts +25 -0
- package/src/language/main-browser.ts +19 -0
- package/src/language/main.ts +13 -0
- package/src/language/parser.ts +329 -0
- package/src/language/syntax.ts +646 -0
- package/src/runtime/agents/common.ts +177 -0
- package/src/runtime/agents/impl/openai.ts +19 -0
- package/src/runtime/agents/provider.ts +58 -0
- package/src/runtime/agents/registry.ts +9 -0
- package/src/runtime/auth/cognito.ts +225 -0
- package/src/runtime/auth/defs.ts +33 -0
- package/src/runtime/auth/interface.ts +31 -0
- package/src/runtime/defs.ts +33 -0
- package/src/runtime/interpreter.ts +1352 -0
- package/src/runtime/loader.ts +450 -0
- package/src/runtime/logger.ts +51 -0
- package/src/runtime/module.ts +2188 -0
- package/src/runtime/modules/ai.ts +257 -0
- package/src/runtime/modules/auth.ts +489 -0
- package/src/runtime/modules/core.ts +95 -0
- package/src/runtime/relgraph.ts +195 -0
- package/src/runtime/resolvers/interface.ts +160 -0
- package/src/runtime/resolvers/registry.ts +30 -0
- package/src/runtime/resolvers/sqldb/database.ts +823 -0
- package/src/runtime/resolvers/sqldb/dbutil.ts +257 -0
- package/src/runtime/resolvers/sqldb/impl.ts +471 -0
- package/src/runtime/state.ts +87 -0
- package/src/runtime/util.ts +513 -0
- package/src/setupClassic.ts +43 -0
- package/src/setupCommon.ts +33 -0
- package/src/setupExtended.ts +79 -0
- package/src/syntaxes/agentlang.monarch.ts +31 -0
- package/src/utils/fs/index.ts +28 -0
- package/src/utils/fs/interfaces.ts +118 -0
- package/src/utils/fs/lightning-fs.ts +284 -0
- package/src/utils/fs/node-fs.ts +185 -0
- package/src/utils/fs-utils.ts +304 -0
- package/src/utils/runtime.ts +43 -0
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import { makeCoreModuleName, makeFqName } from '../util.js';
|
|
2
|
+
import { Environment, makeEventEvaluator, parseAndEvaluateStatement } from '../interpreter.js';
|
|
3
|
+
import { fetchModule, Instance, instanceToObject, isModule } from '../module.js';
|
|
4
|
+
import { provider } from '../agents/registry.js';
|
|
5
|
+
import {
|
|
6
|
+
AgentServiceProvider,
|
|
7
|
+
AIResponse,
|
|
8
|
+
assistantMessage,
|
|
9
|
+
humanMessage,
|
|
10
|
+
systemMessage,
|
|
11
|
+
} from '../agents/provider.js';
|
|
12
|
+
import { AIMessage, BaseMessage, HumanMessage } from '@langchain/core/messages';
|
|
13
|
+
import { PlannerInstructions } from '../agents/common.js';
|
|
14
|
+
import { PathAttributeNameQuery } from '../defs.js';
|
|
15
|
+
|
|
16
|
+
export const CoreAIModuleName = makeCoreModuleName('ai');
|
|
17
|
+
|
|
18
|
+
export default `module ${CoreAIModuleName}
|
|
19
|
+
|
|
20
|
+
entity llm {
|
|
21
|
+
name String @id,
|
|
22
|
+
service String @default("openai"),
|
|
23
|
+
config Map @optional
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
entity agent {
|
|
27
|
+
name String @id,
|
|
28
|
+
type @enum("chat", "planner") @default("chat"),
|
|
29
|
+
instruction String @optional,
|
|
30
|
+
tools String[] @optional,
|
|
31
|
+
documents String[] @optional,
|
|
32
|
+
llm String
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
entity agentChatSession {
|
|
36
|
+
id String @id,
|
|
37
|
+
messages String
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
workflow findAgentChatSession {
|
|
41
|
+
{agentChatSession {id? findAgentChatSession.id}} as [sess];
|
|
42
|
+
sess
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
workflow saveAgentChatSession {
|
|
46
|
+
upsert {agentChatSession {id saveAgentChatSession.id, messages saveAgentChatSession.messages}}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
entity document {
|
|
50
|
+
title String @id,
|
|
51
|
+
content String,
|
|
52
|
+
@meta {"fullTextSearch": "*"}
|
|
53
|
+
}
|
|
54
|
+
`;
|
|
55
|
+
|
|
56
|
+
export const AgentFqName = makeFqName(CoreAIModuleName, 'agent');
|
|
57
|
+
|
|
58
|
+
const ProviderDb = new Map<string, AgentServiceProvider>();
|
|
59
|
+
|
|
60
|
+
export class Agent {
|
|
61
|
+
llm: string = '';
|
|
62
|
+
name: string = '';
|
|
63
|
+
chatId: string | undefined;
|
|
64
|
+
instruction: string = '';
|
|
65
|
+
type: string = 'chat';
|
|
66
|
+
tools: string[] | undefined;
|
|
67
|
+
documents: string[] | undefined;
|
|
68
|
+
|
|
69
|
+
private constructor() {}
|
|
70
|
+
|
|
71
|
+
static FromInstance(agentInstance: Instance): Agent {
|
|
72
|
+
return instanceToObject<Agent>(agentInstance, new Agent());
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
isPlanner(): boolean {
|
|
76
|
+
return (this.tools && this.tools.length > 0) || this.type == 'planner';
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async invoke(message: string, env: Environment) {
|
|
80
|
+
const p = await findProviderForLLM(this.llm, env);
|
|
81
|
+
const agentName = this.name;
|
|
82
|
+
const chatId = this.chatId || agentName;
|
|
83
|
+
const sess: Instance | null = await findAgentChatSession(chatId, env);
|
|
84
|
+
let msgs: BaseMessage[] | undefined;
|
|
85
|
+
const isplnr = this.isPlanner();
|
|
86
|
+
if (sess) {
|
|
87
|
+
msgs = sess.lookup('messages');
|
|
88
|
+
} else {
|
|
89
|
+
msgs = [systemMessage(this.instruction)];
|
|
90
|
+
}
|
|
91
|
+
if (msgs) {
|
|
92
|
+
const sysMsg = msgs[0];
|
|
93
|
+
if (isplnr) {
|
|
94
|
+
const newSysMsg = systemMessage(
|
|
95
|
+
`${PlannerInstructions}\n${this.toolsAsString()}\n${this.instruction}`
|
|
96
|
+
);
|
|
97
|
+
msgs[0] = newSysMsg;
|
|
98
|
+
}
|
|
99
|
+
msgs.push(humanMessage(await this.maybeAddRelevantDocuments(message, env)));
|
|
100
|
+
const response: AIResponse = await p.invoke(msgs);
|
|
101
|
+
msgs.push(assistantMessage(response.content));
|
|
102
|
+
if (isplnr) {
|
|
103
|
+
msgs[0] = sysMsg;
|
|
104
|
+
}
|
|
105
|
+
await saveAgentChatSession(chatId, msgs, env);
|
|
106
|
+
env.setLastResult(response.content);
|
|
107
|
+
} else {
|
|
108
|
+
throw new Error(`failed to initialize messages for agent ${agentName}`);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
private async maybeAddRelevantDocuments(message: string, env: Environment): Promise<string> {
|
|
113
|
+
if (this.documents && this.documents.length > 0) {
|
|
114
|
+
const s = `${message}. Relevant documents are: ${this.documents.join(',')}`;
|
|
115
|
+
const result: any[] = await parseHelper(`{agentlang_ai/document? "${s}"}`, env);
|
|
116
|
+
if (result && result.length > 0) {
|
|
117
|
+
const docs: Instance[] = [];
|
|
118
|
+
for (let i = 0; i < result.length; ++i) {
|
|
119
|
+
const v: any = result[i];
|
|
120
|
+
const r: Instance[] = await parseHelper(
|
|
121
|
+
`{agentlang_ai/document {${PathAttributeNameQuery} "${v.id}"}}`,
|
|
122
|
+
env
|
|
123
|
+
);
|
|
124
|
+
if (r && r.length > 0) {
|
|
125
|
+
docs.push(r[0]);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
if (docs.length > 0) {
|
|
129
|
+
message = message.concat('\nUse the additional information given below:\n').concat(
|
|
130
|
+
docs
|
|
131
|
+
.map((v: Instance) => {
|
|
132
|
+
return v.lookup('content');
|
|
133
|
+
})
|
|
134
|
+
.join('\n')
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
return message;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private toolsAsString(): string {
|
|
143
|
+
if (this.tools) {
|
|
144
|
+
return this.tools
|
|
145
|
+
.filter((s: string) => {
|
|
146
|
+
return isModule(s);
|
|
147
|
+
})
|
|
148
|
+
.map((moduleName: string) => {
|
|
149
|
+
return fetchModule(moduleName).toString();
|
|
150
|
+
})
|
|
151
|
+
.join('\n');
|
|
152
|
+
} else {
|
|
153
|
+
return '';
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function parseHelper(stmt: string, env: Environment): Promise<any> {
|
|
159
|
+
await parseAndEvaluateStatement(stmt, undefined, env);
|
|
160
|
+
return env.getLastResult();
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export async function findAgentByName(name: string, env: Environment): Promise<Agent> {
|
|
164
|
+
const result = await parseHelper(`{agentlang_ai/agent {name? "${name}"}}`, env);
|
|
165
|
+
if (result instanceof Array && result.length > 0) {
|
|
166
|
+
const agentInstance: Instance = result[0];
|
|
167
|
+
return Agent.FromInstance(agentInstance);
|
|
168
|
+
} else {
|
|
169
|
+
throw new Error(`Failed to fine agent ${name}`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export async function findProviderForLLM(
|
|
174
|
+
llmName: string,
|
|
175
|
+
env: Environment
|
|
176
|
+
): Promise<AgentServiceProvider> {
|
|
177
|
+
let p: AgentServiceProvider | undefined = ProviderDb.get(llmName);
|
|
178
|
+
if (p == undefined) {
|
|
179
|
+
const result: Instance[] = await parseAndEvaluateStatement(
|
|
180
|
+
`{${CoreAIModuleName}/llm {name? "${llmName}"}}`,
|
|
181
|
+
undefined,
|
|
182
|
+
env
|
|
183
|
+
);
|
|
184
|
+
if (result.length > 0) {
|
|
185
|
+
const llm: Instance = result[0];
|
|
186
|
+
const service = llm.lookup('service');
|
|
187
|
+
const pclass = provider(service);
|
|
188
|
+
const providerConfig: Map<string, any> =
|
|
189
|
+
llm.lookup('config') || new Map().set('service', service);
|
|
190
|
+
p = new pclass(providerConfig);
|
|
191
|
+
if (p) ProviderDb.set(llmName, p);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
if (p) {
|
|
195
|
+
return p;
|
|
196
|
+
} else {
|
|
197
|
+
throw new Error(`Failed to load provider for ${llmName}`);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const evalEvent = makeEventEvaluator(CoreAIModuleName);
|
|
202
|
+
|
|
203
|
+
type GenericMessage = {
|
|
204
|
+
role: 'system' | 'user' | 'assistant';
|
|
205
|
+
content: string;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
function asBaseMessages(gms: GenericMessage[]): BaseMessage[] {
|
|
209
|
+
return gms.map((gm: GenericMessage): BaseMessage => {
|
|
210
|
+
switch (gm.role) {
|
|
211
|
+
case 'user': {
|
|
212
|
+
return humanMessage(gm.content);
|
|
213
|
+
}
|
|
214
|
+
case 'assistant': {
|
|
215
|
+
return assistantMessage(gm.content);
|
|
216
|
+
}
|
|
217
|
+
default: {
|
|
218
|
+
return systemMessage(gm.content);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function asGenericMessages(bms: BaseMessage[]): GenericMessage[] {
|
|
225
|
+
return bms.map((bm: BaseMessage): GenericMessage => {
|
|
226
|
+
if (bm instanceof HumanMessage) {
|
|
227
|
+
return { role: 'user', content: bm.text };
|
|
228
|
+
} else if (bm instanceof AIMessage) {
|
|
229
|
+
return { role: 'assistant', content: bm.text };
|
|
230
|
+
} else {
|
|
231
|
+
return { role: 'system', content: bm.text };
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
export async function findAgentChatSession(
|
|
237
|
+
chatId: string,
|
|
238
|
+
env: Environment
|
|
239
|
+
): Promise<Instance | null> {
|
|
240
|
+
const result: Instance | null = await evalEvent('findAgentChatSession', { id: chatId }, env);
|
|
241
|
+
if (result) {
|
|
242
|
+
result.attributes.set('messages', asBaseMessages(JSON.parse(result.lookup('messages'))));
|
|
243
|
+
}
|
|
244
|
+
return result;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
export async function saveAgentChatSession(chatId: string, messages: any[], env: Environment) {
|
|
248
|
+
await evalEvent(
|
|
249
|
+
'saveAgentChatSession',
|
|
250
|
+
{ id: chatId, messages: JSON.stringify(asGenericMessages(messages)) },
|
|
251
|
+
env
|
|
252
|
+
);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
export function agentName(agentInstance: Instance): string {
|
|
256
|
+
return agentInstance.lookup('name');
|
|
257
|
+
}
|