@ekairos/story 1.6.3 → 1.8.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/dist/events.js CHANGED
@@ -1,204 +1,204 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.EMAIL_CHANNEL = exports.AGENT_CHANNEL = exports.WEB_CHANNEL = exports.SYSTEM_MESSAGE_TYPE = exports.ASSISTANT_MESSAGE_TYPE = exports.USER_MESSAGE_TYPE = void 0;
4
- exports.createUserEventFromUIMessages = createUserEventFromUIMessages;
5
- exports.createAssistantEventFromUIMessages = createAssistantEventFromUIMessages;
6
- exports.convertToUIMessage = convertToUIMessage;
7
- exports.convertEventsToModelMessages = convertEventsToModelMessages;
8
- exports.convertEventToModelMessages = convertEventToModelMessages;
9
- exports.convertModelMessageToEvent = convertModelMessageToEvent;
10
- const admin_1 = require("@instantdb/admin");
11
- const ai_1 = require("ai");
12
- const document_parser_1 = require("./document-parser");
13
- const db = (0, admin_1.init)({
14
- appId: process.env.NEXT_PUBLIC_INSTANT_APP_ID,
15
- adminToken: process.env.INSTANT_APP_ADMIN_TOKEN,
16
- });
17
- exports.USER_MESSAGE_TYPE = "user.message";
18
- exports.ASSISTANT_MESSAGE_TYPE = "assistant.message";
19
- exports.SYSTEM_MESSAGE_TYPE = "system.message";
20
- exports.WEB_CHANNEL = "web";
21
- exports.AGENT_CHANNEL = "whatsapp";
22
- exports.EMAIL_CHANNEL = "email";
23
- function createUserEventFromUIMessages(messages) {
24
- if (!Array.isArray(messages) || messages.length === 0) {
25
- throw new Error("Missing messages to create event");
26
- }
27
- const lastMessage = messages[messages.length - 1];
28
- return {
29
- id: lastMessage.id,
30
- type: exports.USER_MESSAGE_TYPE,
31
- channel: exports.WEB_CHANNEL,
32
- content: {
33
- parts: lastMessage.parts,
34
- },
35
- createdAt: new Date().toISOString(),
36
- };
37
- }
38
- function createAssistantEventFromUIMessages(eventId, messages) {
39
- if (!Array.isArray(messages) || messages.length === 0) {
40
- throw new Error("Missing messages to create event");
41
- }
42
- const lastMessage = messages[messages.length - 1];
43
- return {
44
- id: eventId,
45
- type: exports.ASSISTANT_MESSAGE_TYPE,
46
- channel: exports.WEB_CHANNEL,
47
- content: {
48
- parts: lastMessage.parts,
49
- },
50
- createdAt: new Date().toISOString(),
51
- };
52
- }
53
- function convertToUIMessage(event) {
54
- let role;
55
- if (event.type === exports.USER_MESSAGE_TYPE) {
56
- role = "user";
57
- }
58
- else {
59
- role = "assistant";
60
- }
61
- return {
62
- id: event.id,
63
- role: role,
64
- parts: event.content.parts,
65
- metadata: {
66
- channel: event.channel,
67
- type: event.type,
68
- createdAt: event.createdAt,
69
- }
70
- };
71
- }
72
- async function convertEventsToModelMessages(events) {
73
- const results = [];
74
- for (const event of events) {
75
- const messages = await convertEventToModelMessages(event);
76
- results.push(messages);
77
- }
78
- return results.flat();
79
- }
80
- async function convertEventToModelMessages(event) {
81
- // convert files in message
82
- // const files = event.content.parts.filter(part => part.type === "file")
83
- // 1. copy event to new . we will manipulate the parts
84
- // 2. each file part will be converted using convertFilePart
85
- const convertedParts = await Promise.all((event.content?.parts || []).map(async (part) => {
86
- if (part?.type === "file") {
87
- return await convertFilePart(part);
88
- }
89
- return [part];
90
- }));
91
- const newEvent = {
92
- ...event,
93
- content: {
94
- ...event.content,
95
- parts: convertedParts.flat(),
96
- },
97
- };
98
- // convert event to convertToModelMessages compatible
99
- let message = convertToUIMessage(newEvent);
100
- // use ai sdk helper
101
- return (0, ai_1.convertToModelMessages)([message]);
102
- }
103
- function convertModelMessageToEvent(eventId, message) {
104
- let type;
105
- switch (message.message.role) {
106
- case "user":
107
- type = exports.USER_MESSAGE_TYPE;
108
- break;
109
- case "assistant":
110
- type = exports.ASSISTANT_MESSAGE_TYPE;
111
- break;
112
- case "system":
113
- type = exports.SYSTEM_MESSAGE_TYPE;
114
- break;
115
- }
116
- return {
117
- id: eventId,
118
- type: type,
119
- channel: exports.WEB_CHANNEL,
120
- content: {
121
- parts: message.message.content,
122
- },
123
- createdAt: message.timestamp.toISOString(),
124
- };
125
- }
126
- async function convertFilePart(part) {
127
- // file part has data:fileId=xxxx in its url
128
- // we will extract that file id and call instantdb to get the file url
129
- if (!part?.url || typeof part.url !== "string") {
130
- return [part];
131
- }
132
- // Look for 'data:fileId=' pattern and extract the fileId
133
- const fileIdMatch = part.url.match(/data:fileId=([A-Za-z0-9_\-]+)/);
134
- const fileId = fileIdMatch ? fileIdMatch[1] : null;
135
- if (!fileId) {
136
- return [part];
137
- }
138
- try {
139
- const fileQuery = await db.query({
140
- $files: {
141
- $: {
142
- where: {
143
- id: fileId,
144
- },
145
- limit: 1,
146
- },
147
- document: {},
148
- },
149
- });
150
- const fileRecord = Array.isArray(fileQuery.$files) ? fileQuery.$files[0] : undefined;
151
- if (!fileRecord) {
152
- return [part];
153
- }
154
- let documentRecord = fileRecord.document;
155
- if (!documentRecord || (Array.isArray(documentRecord) && documentRecord.length === 0)) {
156
- const fileResponse = await fetch(fileRecord.url);
157
- if (!fileResponse.ok) {
158
- return [part];
159
- }
160
- const buffer = await fileResponse.arrayBuffer();
161
- const documentId = await (0, document_parser_1.parseAndStoreDocument)(db, Buffer.from(buffer), fileRecord.path, fileRecord.path, fileRecord.id);
162
- const documentQuery = await db.query({
163
- documents: {
164
- $: {
165
- where: {
166
- id: documentId,
167
- },
168
- limit: 1,
169
- },
170
- },
171
- });
172
- documentRecord = Array.isArray(documentQuery.documents) ? documentQuery.documents[0] : undefined;
173
- }
174
- const parts = [];
175
- const fileName = documentRecord && typeof documentRecord === "object" && "fileName" in documentRecord
176
- ? String(documentRecord.fileName)
177
- : String(fileRecord.path || "Unknown");
178
- parts.push({
179
- type: "text",
180
- text: `User attached a file.\nFile ID: ${fileRecord.id}\nFile Name: "${fileName}"\nMedia Type: ${part.mediaType || "unknown"}`,
181
- });
182
- if (documentRecord?.content && Array.isArray(documentRecord.content.pages)) {
183
- const pages = documentRecord.content.pages;
184
- const pageTexts = pages
185
- .map((page, index) => {
186
- const text = typeof page.text === "string" ? page.text : "";
187
- return `\n\n--- Page ${index + 1} ---\n\n${text}`;
188
- })
189
- .join("");
190
- if (pageTexts.length > 0) {
191
- parts.push({
192
- type: "text",
193
- text: `Document transcription for File ID ${fileRecord.id}:${pageTexts}`,
194
- });
195
- }
196
- }
197
- return parts;
198
- }
199
- catch (error) {
200
- console.error("convertFilePart error", error);
201
- return [part];
202
- }
203
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.EMAIL_CHANNEL = exports.AGENT_CHANNEL = exports.WEB_CHANNEL = exports.SYSTEM_MESSAGE_TYPE = exports.ASSISTANT_MESSAGE_TYPE = exports.USER_MESSAGE_TYPE = void 0;
4
+ exports.createUserEventFromUIMessages = createUserEventFromUIMessages;
5
+ exports.createAssistantEventFromUIMessages = createAssistantEventFromUIMessages;
6
+ exports.convertToUIMessage = convertToUIMessage;
7
+ exports.convertEventsToModelMessages = convertEventsToModelMessages;
8
+ exports.convertEventToModelMessages = convertEventToModelMessages;
9
+ exports.convertModelMessageToEvent = convertModelMessageToEvent;
10
+ const admin_1 = require("@instantdb/admin");
11
+ const ai_1 = require("ai");
12
+ const document_parser_1 = require("./document-parser");
13
+ const db = (0, admin_1.init)({
14
+ appId: process.env.NEXT_PUBLIC_INSTANT_APP_ID,
15
+ adminToken: process.env.INSTANT_APP_ADMIN_TOKEN,
16
+ });
17
+ exports.USER_MESSAGE_TYPE = "user.message";
18
+ exports.ASSISTANT_MESSAGE_TYPE = "assistant.message";
19
+ exports.SYSTEM_MESSAGE_TYPE = "system.message";
20
+ exports.WEB_CHANNEL = "web";
21
+ exports.AGENT_CHANNEL = "whatsapp";
22
+ exports.EMAIL_CHANNEL = "email";
23
+ function createUserEventFromUIMessages(messages) {
24
+ if (!Array.isArray(messages) || messages.length === 0) {
25
+ throw new Error("Missing messages to create event");
26
+ }
27
+ const lastMessage = messages[messages.length - 1];
28
+ return {
29
+ id: lastMessage.id,
30
+ type: exports.USER_MESSAGE_TYPE,
31
+ channel: exports.WEB_CHANNEL,
32
+ content: {
33
+ parts: lastMessage.parts,
34
+ },
35
+ createdAt: new Date().toISOString(),
36
+ };
37
+ }
38
+ function createAssistantEventFromUIMessages(eventId, messages) {
39
+ if (!Array.isArray(messages) || messages.length === 0) {
40
+ throw new Error("Missing messages to create event");
41
+ }
42
+ const lastMessage = messages[messages.length - 1];
43
+ return {
44
+ id: eventId,
45
+ type: exports.ASSISTANT_MESSAGE_TYPE,
46
+ channel: exports.WEB_CHANNEL,
47
+ content: {
48
+ parts: lastMessage.parts,
49
+ },
50
+ createdAt: new Date().toISOString(),
51
+ };
52
+ }
53
+ function convertToUIMessage(event) {
54
+ let role;
55
+ if (event.type === exports.USER_MESSAGE_TYPE) {
56
+ role = "user";
57
+ }
58
+ else {
59
+ role = "assistant";
60
+ }
61
+ return {
62
+ id: event.id,
63
+ role: role,
64
+ parts: event.content.parts,
65
+ metadata: {
66
+ channel: event.channel,
67
+ type: event.type,
68
+ createdAt: event.createdAt,
69
+ }
70
+ };
71
+ }
72
+ async function convertEventsToModelMessages(events) {
73
+ const results = [];
74
+ for (const event of events) {
75
+ const messages = await convertEventToModelMessages(event);
76
+ results.push(messages);
77
+ }
78
+ return results.flat();
79
+ }
80
+ async function convertEventToModelMessages(event) {
81
+ // convert files in message
82
+ // const files = event.content.parts.filter(part => part.type === "file")
83
+ // 1. copy event to new . we will manipulate the parts
84
+ // 2. each file part will be converted using convertFilePart
85
+ const convertedParts = await Promise.all((event.content?.parts || []).map(async (part) => {
86
+ if (part?.type === "file") {
87
+ return await convertFilePart(part);
88
+ }
89
+ return [part];
90
+ }));
91
+ const newEvent = {
92
+ ...event,
93
+ content: {
94
+ ...event.content,
95
+ parts: convertedParts.flat(),
96
+ },
97
+ };
98
+ // convert event to convertToModelMessages compatible
99
+ let message = convertToUIMessage(newEvent);
100
+ // use ai sdk helper
101
+ return (0, ai_1.convertToModelMessages)([message]);
102
+ }
103
+ function convertModelMessageToEvent(eventId, message) {
104
+ let type;
105
+ switch (message.message.role) {
106
+ case "user":
107
+ type = exports.USER_MESSAGE_TYPE;
108
+ break;
109
+ case "assistant":
110
+ type = exports.ASSISTANT_MESSAGE_TYPE;
111
+ break;
112
+ case "system":
113
+ type = exports.SYSTEM_MESSAGE_TYPE;
114
+ break;
115
+ }
116
+ return {
117
+ id: eventId,
118
+ type: type,
119
+ channel: exports.WEB_CHANNEL,
120
+ content: {
121
+ parts: message.message.content,
122
+ },
123
+ createdAt: message.timestamp.toISOString(),
124
+ };
125
+ }
126
+ async function convertFilePart(part) {
127
+ // file part has data:fileId=xxxx in its url
128
+ // we will extract that file id and call instantdb to get the file url
129
+ if (!part?.url || typeof part.url !== "string") {
130
+ return [part];
131
+ }
132
+ // Look for 'data:fileId=' pattern and extract the fileId
133
+ const fileIdMatch = part.url.match(/data:fileId=([A-Za-z0-9_\-]+)/);
134
+ const fileId = fileIdMatch ? fileIdMatch[1] : null;
135
+ if (!fileId) {
136
+ return [part];
137
+ }
138
+ try {
139
+ const fileQuery = await db.query({
140
+ $files: {
141
+ $: {
142
+ where: {
143
+ id: fileId,
144
+ },
145
+ limit: 1,
146
+ },
147
+ document: {},
148
+ },
149
+ });
150
+ const fileRecord = Array.isArray(fileQuery.$files) ? fileQuery.$files[0] : undefined;
151
+ if (!fileRecord) {
152
+ return [part];
153
+ }
154
+ let documentRecord = fileRecord.document;
155
+ if (!documentRecord || (Array.isArray(documentRecord) && documentRecord.length === 0)) {
156
+ const fileResponse = await fetch(fileRecord.url);
157
+ if (!fileResponse.ok) {
158
+ return [part];
159
+ }
160
+ const buffer = await fileResponse.arrayBuffer();
161
+ const documentId = await (0, document_parser_1.parseAndStoreDocument)(db, Buffer.from(buffer), fileRecord.path, fileRecord.path, fileRecord.id);
162
+ const documentQuery = await db.query({
163
+ documents: {
164
+ $: {
165
+ where: {
166
+ id: documentId,
167
+ },
168
+ limit: 1,
169
+ },
170
+ },
171
+ });
172
+ documentRecord = Array.isArray(documentQuery.documents) ? documentQuery.documents[0] : undefined;
173
+ }
174
+ const parts = [];
175
+ const fileName = documentRecord && typeof documentRecord === "object" && "fileName" in documentRecord
176
+ ? String(documentRecord.fileName)
177
+ : String(fileRecord.path || "Unknown");
178
+ parts.push({
179
+ type: "text",
180
+ text: `User attached a file.\nFile ID: ${fileRecord.id}\nFile Name: "${fileName}"\nMedia Type: ${part.mediaType || "unknown"}`,
181
+ });
182
+ if (documentRecord?.content && Array.isArray(documentRecord.content.pages)) {
183
+ const pages = documentRecord.content.pages;
184
+ const pageTexts = pages
185
+ .map((page, index) => {
186
+ const text = typeof page.text === "string" ? page.text : "";
187
+ return `\n\n--- Page ${index + 1} ---\n\n${text}`;
188
+ })
189
+ .join("");
190
+ if (pageTexts.length > 0) {
191
+ parts.push({
192
+ type: "text",
193
+ text: `Document transcription for File ID ${fileRecord.id}:${pageTexts}`,
194
+ });
195
+ }
196
+ }
197
+ return parts;
198
+ }
199
+ catch (error) {
200
+ console.error("convertFilePart error", error);
201
+ return [part];
202
+ }
203
+ }
204
204
  //# sourceMappingURL=events.js.map
package/dist/index.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- export * from "./story";
2
- export * from "./storyEngine";
3
- export * from "./storyRunner";
4
- export * from "./engine";
5
- export * from "./schema";
6
- export * from "./service";
7
- export * from "./steps-context";
8
- export * from "./agent";
9
- export * from "./events";
10
- export * from "./document-parser";
11
- export * as Steps from "./steps";
1
+ export * from "./story";
2
+ export * from "./storyEngine";
3
+ export * from "./storyRunner";
4
+ export * from "./engine";
5
+ export * from "./schema";
6
+ export * from "./service";
7
+ export * from "./steps-context";
8
+ export * from "./agent";
9
+ export * from "./events";
10
+ export * from "./document-parser";
11
+ export * as Steps from "./steps";
12
12
  //# sourceMappingURL=index.d.ts.map
package/dist/index.js CHANGED
@@ -1,51 +1,51 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
- };
21
- var __importStar = (this && this.__importStar) || (function () {
22
- var ownKeys = function(o) {
23
- ownKeys = Object.getOwnPropertyNames || function (o) {
24
- var ar = [];
25
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
- return ar;
27
- };
28
- return ownKeys(o);
29
- };
30
- return function (mod) {
31
- if (mod && mod.__esModule) return mod;
32
- var result = {};
33
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
- __setModuleDefault(result, mod);
35
- return result;
36
- };
37
- })();
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- exports.Steps = void 0;
40
- __exportStar(require("./story"), exports);
41
- __exportStar(require("./storyEngine"), exports);
42
- __exportStar(require("./storyRunner"), exports);
43
- __exportStar(require("./engine"), exports);
44
- __exportStar(require("./schema"), exports);
45
- __exportStar(require("./service"), exports);
46
- __exportStar(require("./steps-context"), exports);
47
- __exportStar(require("./agent"), exports);
48
- __exportStar(require("./events"), exports);
49
- __exportStar(require("./document-parser"), exports);
50
- exports.Steps = __importStar(require("./steps"));
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
21
+ var __importStar = (this && this.__importStar) || (function () {
22
+ var ownKeys = function(o) {
23
+ ownKeys = Object.getOwnPropertyNames || function (o) {
24
+ var ar = [];
25
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
+ return ar;
27
+ };
28
+ return ownKeys(o);
29
+ };
30
+ return function (mod) {
31
+ if (mod && mod.__esModule) return mod;
32
+ var result = {};
33
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
+ __setModuleDefault(result, mod);
35
+ return result;
36
+ };
37
+ })();
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.Steps = void 0;
40
+ __exportStar(require("./story"), exports);
41
+ __exportStar(require("./storyEngine"), exports);
42
+ __exportStar(require("./storyRunner"), exports);
43
+ __exportStar(require("./engine"), exports);
44
+ __exportStar(require("./schema"), exports);
45
+ __exportStar(require("./service"), exports);
46
+ __exportStar(require("./steps-context"), exports);
47
+ __exportStar(require("./agent"), exports);
48
+ __exportStar(require("./events"), exports);
49
+ __exportStar(require("./document-parser"), exports);
50
+ exports.Steps = __importStar(require("./steps"));
51
51
  //# sourceMappingURL=index.js.map