@ekairos/story 1.6.2 → 1.6.3

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 (42) hide show
  1. package/dist/agent.d.ts +72 -72
  2. package/dist/agent.js +478 -478
  3. package/dist/document-parser.d.ts +15 -15
  4. package/dist/document-parser.js +156 -156
  5. package/dist/engine.d.ts +21 -21
  6. package/dist/engine.js +35 -35
  7. package/dist/events.d.ts +27 -27
  8. package/dist/events.js +203 -203
  9. package/dist/index.d.ts +11 -13
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +50 -52
  12. package/dist/index.js.map +1 -1
  13. package/dist/schema.d.ts +107 -107
  14. package/dist/schema.js +63 -63
  15. package/dist/service.d.ts +44 -44
  16. package/dist/service.js +202 -202
  17. package/dist/steps/ai.d.ts +42 -42
  18. package/dist/steps/ai.js +135 -135
  19. package/dist/steps/base.d.ts +13 -13
  20. package/dist/steps/base.js +36 -36
  21. package/dist/steps/index.d.ts +3 -4
  22. package/dist/steps/index.d.ts.map +1 -1
  23. package/dist/steps/index.js +19 -20
  24. package/dist/steps/index.js.map +1 -1
  25. package/dist/steps/registry.d.ts +4 -4
  26. package/dist/steps/registry.js +28 -28
  27. package/dist/steps/sampleStep.d.ts.map +1 -1
  28. package/dist/steps/sampleStep.js +1 -1
  29. package/dist/steps/sampleStep.js.map +1 -1
  30. package/dist/steps-context.d.ts +11 -11
  31. package/dist/steps-context.js +19 -19
  32. package/dist/story.d.ts +49 -49
  33. package/dist/story.js +54 -54
  34. package/dist/storyEngine.d.ts +54 -54
  35. package/dist/storyEngine.js +50 -50
  36. package/dist/storyRunner.d.ts +7 -7
  37. package/dist/storyRunner.js +55 -55
  38. package/dist/workflows/sampleWorkflow.d.ts +5 -1
  39. package/dist/workflows/sampleWorkflow.d.ts.map +1 -1
  40. package/dist/workflows/sampleWorkflow.js +6 -2
  41. package/dist/workflows/sampleWorkflow.js.map +1 -1
  42. package/package.json +2 -2
package/dist/service.js CHANGED
@@ -1,203 +1,203 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AgentService = void 0;
4
- const admin_1 = require("@instantdb/admin");
5
- const schema_1 = require("./schema");
6
- class AgentService {
7
- constructor() {
8
- this.db = (0, admin_1.init)({
9
- appId: process.env.NEXT_PUBLIC_INSTANT_APP_ID,
10
- adminToken: process.env.INSTANT_APP_ADMIN_TOKEN,
11
- schema: schema_1.storyDomain.schema()
12
- });
13
- }
14
- async getOrCreateContext(contextIdentifier) {
15
- if (!contextIdentifier) {
16
- return this.createContext();
17
- }
18
- let context = await this.getContext(contextIdentifier);
19
- if (!context) {
20
- return this.createContext(contextIdentifier.key ? { key: contextIdentifier.key } : null, contextIdentifier.id);
21
- }
22
- else {
23
- return context;
24
- }
25
- }
26
- async createContext(contextKey, contextId) {
27
- let contextData = {
28
- createdAt: new Date(),
29
- content: {},
30
- key: null
31
- };
32
- const newContextId = contextId ?? (0, admin_1.id)();
33
- if (contextKey?.key) {
34
- contextData = {
35
- ...contextData,
36
- key: contextKey.key
37
- };
38
- }
39
- await this.db.transact([
40
- this.db.tx.story_contexts[newContextId].create(contextData)
41
- ]);
42
- return this.getContext({ id: newContextId });
43
- }
44
- async getContext(contextIdentifier) {
45
- let context;
46
- try {
47
- if (contextIdentifier.id) {
48
- const tRes = await this.db.query({
49
- story_contexts: {
50
- $: { where: { id: contextIdentifier.id }, limit: 1 }
51
- }
52
- });
53
- context = tRes.story_contexts?.[0];
54
- }
55
- if (contextIdentifier.key) {
56
- const tRes = await this.db.query({
57
- story_contexts: {
58
- $: { where: { key: contextIdentifier.key } }
59
- }
60
- });
61
- context = tRes.story_contexts?.[0];
62
- }
63
- return context;
64
- }
65
- catch (error) {
66
- console.error("Error getting context", error);
67
- throw new Error("Error getting context: " + error.message);
68
- }
69
- }
70
- async updateContextContent(contextIdentifier, content) {
71
- const contextDBIdentifier = contextIdentifier.id ?? (0, admin_1.lookup)("key", contextIdentifier.key);
72
- await this.db.transact([
73
- this.db.tx.story_contexts[contextDBIdentifier].update({
74
- content: content,
75
- updatedAt: new Date()
76
- })
77
- ]);
78
- return this.getContext(contextIdentifier);
79
- }
80
- async saveEvent(contextIdentifier, event) {
81
- const txs = [
82
- this.db.tx.story_events[event.id].create({
83
- ...event,
84
- status: "stored"
85
- })
86
- ];
87
- if (contextIdentifier.id) {
88
- txs.push(this.db.tx.story_events[event.id].link({ context: contextIdentifier.id }));
89
- }
90
- else {
91
- txs.push(this.db.tx.story_events[event.id].link({ context: (0, admin_1.lookup)("key", contextIdentifier.key) }));
92
- }
93
- await this.db.transact(txs);
94
- return await this.getEvent(event.id);
95
- }
96
- async createExecution(contextIdentifier, triggerEventId, reactionEventId) {
97
- const executionId = (0, admin_1.id)();
98
- const execCreate = this.db.tx.story_executions[executionId].create({
99
- createdAt: new Date(),
100
- status: "executing",
101
- });
102
- const txs = [execCreate];
103
- if (contextIdentifier.id) {
104
- txs.push(this.db.tx.story_executions[executionId].link({ context: contextIdentifier.id }));
105
- txs.push(this.db.tx.story_contexts[contextIdentifier.id].update({ status: "executing" }));
106
- txs.push(this.db.tx.story_contexts[contextIdentifier.id].link({ currentExecution: executionId }));
107
- }
108
- else {
109
- const ctxLookup = (0, admin_1.lookup)("key", contextIdentifier.key);
110
- txs.push(this.db.tx.story_executions[executionId].link({ context: ctxLookup }));
111
- txs.push(this.db.tx.story_contexts[ctxLookup].update({ status: "executing" }));
112
- txs.push(this.db.tx.story_contexts[ctxLookup].link({ currentExecution: executionId }));
113
- }
114
- txs.push(this.db.tx.story_executions[executionId].link({ trigger: triggerEventId }));
115
- txs.push(this.db.tx.story_executions[executionId].link({ reaction: reactionEventId }));
116
- await this.db.transact(txs);
117
- return { id: executionId };
118
- }
119
- async completeExecution(contextIdentifier, executionId, status) {
120
- const txs = [];
121
- txs.push(this.db.tx.story_executions[executionId].update({ status, updatedAt: new Date() }));
122
- if (contextIdentifier.id) {
123
- txs.push(this.db.tx.story_contexts[contextIdentifier.id].update({ status: "open" }));
124
- // optionally unlink currentExecution if desired
125
- }
126
- else {
127
- txs.push(this.db.tx.story_contexts[(0, admin_1.lookup)("key", contextIdentifier.key)].update({ status: "open" }));
128
- }
129
- await this.db.transact(txs);
130
- }
131
- async updateEvent(eventId, event) {
132
- await this.db.transact([
133
- this.db.tx.story_events[eventId].update(event)
134
- ]);
135
- return await this.getEvent(eventId);
136
- }
137
- async getEvent(eventId) {
138
- const event = await this.db.query({
139
- story_events: {
140
- $: { where: { id: eventId } }
141
- }
142
- });
143
- return event.story_events?.[0];
144
- }
145
- async getEvents(contextIdentifier) {
146
- let contextWhere;
147
- if (contextIdentifier.id) {
148
- contextWhere = { context: contextIdentifier.id };
149
- }
150
- else {
151
- contextWhere = { context: (0, admin_1.lookup)("key", contextIdentifier.key) };
152
- }
153
- const events = await this.db.query({
154
- story_events: {
155
- $: {
156
- where: contextWhere,
157
- limit: 30,
158
- order: {
159
- createdAt: 'desc',
160
- },
161
- }
162
- }
163
- });
164
- return events.story_events;
165
- }
166
- async readEventStream(stream) {
167
- const reader = stream.getReader();
168
- const chunks = [];
169
- let firstChunk;
170
- while (true) {
171
- const { value, done } = await reader.read();
172
- if (done) {
173
- break;
174
- }
175
- const currentChunk = value;
176
- if (!firstChunk) {
177
- firstChunk = currentChunk;
178
- }
179
- chunks.push(currentChunk);
180
- }
181
- if (!firstChunk) {
182
- throw new Error("No chunks received from stream");
183
- }
184
- const eventId = firstChunk.messageId;
185
- const query = await this.db.query({
186
- story_events: {
187
- $: {
188
- where: { id: eventId },
189
- limit: 1,
190
- fields: ["id", "channel", "type", "status", "createdAt", "content"],
191
- },
192
- },
193
- });
194
- const persistedEvent = Array.isArray(query.story_events) ? query.story_events[0] : undefined;
195
- return {
196
- eventId,
197
- chunks,
198
- persistedEvent,
199
- };
200
- }
201
- }
202
- exports.AgentService = AgentService;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AgentService = void 0;
4
+ const admin_1 = require("@instantdb/admin");
5
+ const schema_1 = require("./schema");
6
+ class AgentService {
7
+ constructor() {
8
+ this.db = (0, admin_1.init)({
9
+ appId: process.env.NEXT_PUBLIC_INSTANT_APP_ID,
10
+ adminToken: process.env.INSTANT_APP_ADMIN_TOKEN,
11
+ schema: schema_1.storyDomain.schema()
12
+ });
13
+ }
14
+ async getOrCreateContext(contextIdentifier) {
15
+ if (!contextIdentifier) {
16
+ return this.createContext();
17
+ }
18
+ let context = await this.getContext(contextIdentifier);
19
+ if (!context) {
20
+ return this.createContext(contextIdentifier.key ? { key: contextIdentifier.key } : null, contextIdentifier.id);
21
+ }
22
+ else {
23
+ return context;
24
+ }
25
+ }
26
+ async createContext(contextKey, contextId) {
27
+ let contextData = {
28
+ createdAt: new Date(),
29
+ content: {},
30
+ key: null
31
+ };
32
+ const newContextId = contextId ?? (0, admin_1.id)();
33
+ if (contextKey?.key) {
34
+ contextData = {
35
+ ...contextData,
36
+ key: contextKey.key
37
+ };
38
+ }
39
+ await this.db.transact([
40
+ this.db.tx.story_contexts[newContextId].create(contextData)
41
+ ]);
42
+ return this.getContext({ id: newContextId });
43
+ }
44
+ async getContext(contextIdentifier) {
45
+ let context;
46
+ try {
47
+ if (contextIdentifier.id) {
48
+ const tRes = await this.db.query({
49
+ story_contexts: {
50
+ $: { where: { id: contextIdentifier.id }, limit: 1 }
51
+ }
52
+ });
53
+ context = tRes.story_contexts?.[0];
54
+ }
55
+ if (contextIdentifier.key) {
56
+ const tRes = await this.db.query({
57
+ story_contexts: {
58
+ $: { where: { key: contextIdentifier.key } }
59
+ }
60
+ });
61
+ context = tRes.story_contexts?.[0];
62
+ }
63
+ return context;
64
+ }
65
+ catch (error) {
66
+ console.error("Error getting context", error);
67
+ throw new Error("Error getting context: " + error.message);
68
+ }
69
+ }
70
+ async updateContextContent(contextIdentifier, content) {
71
+ const contextDBIdentifier = contextIdentifier.id ?? (0, admin_1.lookup)("key", contextIdentifier.key);
72
+ await this.db.transact([
73
+ this.db.tx.story_contexts[contextDBIdentifier].update({
74
+ content: content,
75
+ updatedAt: new Date()
76
+ })
77
+ ]);
78
+ return this.getContext(contextIdentifier);
79
+ }
80
+ async saveEvent(contextIdentifier, event) {
81
+ const txs = [
82
+ this.db.tx.story_events[event.id].create({
83
+ ...event,
84
+ status: "stored"
85
+ })
86
+ ];
87
+ if (contextIdentifier.id) {
88
+ txs.push(this.db.tx.story_events[event.id].link({ context: contextIdentifier.id }));
89
+ }
90
+ else {
91
+ txs.push(this.db.tx.story_events[event.id].link({ context: (0, admin_1.lookup)("key", contextIdentifier.key) }));
92
+ }
93
+ await this.db.transact(txs);
94
+ return await this.getEvent(event.id);
95
+ }
96
+ async createExecution(contextIdentifier, triggerEventId, reactionEventId) {
97
+ const executionId = (0, admin_1.id)();
98
+ const execCreate = this.db.tx.story_executions[executionId].create({
99
+ createdAt: new Date(),
100
+ status: "executing",
101
+ });
102
+ const txs = [execCreate];
103
+ if (contextIdentifier.id) {
104
+ txs.push(this.db.tx.story_executions[executionId].link({ context: contextIdentifier.id }));
105
+ txs.push(this.db.tx.story_contexts[contextIdentifier.id].update({ status: "executing" }));
106
+ txs.push(this.db.tx.story_contexts[contextIdentifier.id].link({ currentExecution: executionId }));
107
+ }
108
+ else {
109
+ const ctxLookup = (0, admin_1.lookup)("key", contextIdentifier.key);
110
+ txs.push(this.db.tx.story_executions[executionId].link({ context: ctxLookup }));
111
+ txs.push(this.db.tx.story_contexts[ctxLookup].update({ status: "executing" }));
112
+ txs.push(this.db.tx.story_contexts[ctxLookup].link({ currentExecution: executionId }));
113
+ }
114
+ txs.push(this.db.tx.story_executions[executionId].link({ trigger: triggerEventId }));
115
+ txs.push(this.db.tx.story_executions[executionId].link({ reaction: reactionEventId }));
116
+ await this.db.transact(txs);
117
+ return { id: executionId };
118
+ }
119
+ async completeExecution(contextIdentifier, executionId, status) {
120
+ const txs = [];
121
+ txs.push(this.db.tx.story_executions[executionId].update({ status, updatedAt: new Date() }));
122
+ if (contextIdentifier.id) {
123
+ txs.push(this.db.tx.story_contexts[contextIdentifier.id].update({ status: "open" }));
124
+ // optionally unlink currentExecution if desired
125
+ }
126
+ else {
127
+ txs.push(this.db.tx.story_contexts[(0, admin_1.lookup)("key", contextIdentifier.key)].update({ status: "open" }));
128
+ }
129
+ await this.db.transact(txs);
130
+ }
131
+ async updateEvent(eventId, event) {
132
+ await this.db.transact([
133
+ this.db.tx.story_events[eventId].update(event)
134
+ ]);
135
+ return await this.getEvent(eventId);
136
+ }
137
+ async getEvent(eventId) {
138
+ const event = await this.db.query({
139
+ story_events: {
140
+ $: { where: { id: eventId } }
141
+ }
142
+ });
143
+ return event.story_events?.[0];
144
+ }
145
+ async getEvents(contextIdentifier) {
146
+ let contextWhere;
147
+ if (contextIdentifier.id) {
148
+ contextWhere = { context: contextIdentifier.id };
149
+ }
150
+ else {
151
+ contextWhere = { context: (0, admin_1.lookup)("key", contextIdentifier.key) };
152
+ }
153
+ const events = await this.db.query({
154
+ story_events: {
155
+ $: {
156
+ where: contextWhere,
157
+ limit: 30,
158
+ order: {
159
+ createdAt: 'desc',
160
+ },
161
+ }
162
+ }
163
+ });
164
+ return events.story_events;
165
+ }
166
+ async readEventStream(stream) {
167
+ const reader = stream.getReader();
168
+ const chunks = [];
169
+ let firstChunk;
170
+ while (true) {
171
+ const { value, done } = await reader.read();
172
+ if (done) {
173
+ break;
174
+ }
175
+ const currentChunk = value;
176
+ if (!firstChunk) {
177
+ firstChunk = currentChunk;
178
+ }
179
+ chunks.push(currentChunk);
180
+ }
181
+ if (!firstChunk) {
182
+ throw new Error("No chunks received from stream");
183
+ }
184
+ const eventId = firstChunk.messageId;
185
+ const query = await this.db.query({
186
+ story_events: {
187
+ $: {
188
+ where: { id: eventId },
189
+ limit: 1,
190
+ fields: ["id", "channel", "type", "status", "createdAt", "content"],
191
+ },
192
+ },
193
+ });
194
+ const persistedEvent = Array.isArray(query.story_events) ? query.story_events[0] : undefined;
195
+ return {
196
+ eventId,
197
+ chunks,
198
+ persistedEvent,
199
+ };
200
+ }
201
+ }
202
+ exports.AgentService = AgentService;
203
203
  //# sourceMappingURL=service.js.map
@@ -1,43 +1,43 @@
1
- type PrimitiveType = "string" | "number" | "boolean" | "object" | "array";
2
- type FieldSchema = {
3
- type: PrimitiveType;
4
- description?: string;
5
- required?: boolean;
6
- properties?: Record<string, FieldSchema>;
7
- items?: FieldSchema;
8
- };
9
- type StepInputSchema = {
10
- type: "object";
11
- description?: string;
12
- properties?: Record<string, FieldSchema>;
13
- };
14
- type StoryActionSpec = {
15
- name: string;
16
- description: string;
17
- implementationKey: string;
18
- inputSchema?: StepInputSchema;
19
- finalize?: boolean;
20
- };
21
- type StoryOptions = {
22
- reasoningEffort?: "low" | "medium" | "high";
23
- webSearch?: boolean;
24
- includeBaseTools?: {
25
- createMessage?: boolean;
26
- requestDirection?: boolean;
27
- end?: boolean;
28
- };
29
- };
30
- export declare function runReasoningOnceStep(params: {
31
- contextId: string;
32
- systemPrompt: string;
33
- actions: StoryActionSpec[];
34
- options: StoryOptions;
35
- }): Promise<{
36
- toolCalls: Array<{
37
- toolCallId: string;
38
- toolName: string;
39
- args: any;
40
- }>;
41
- }>;
42
- export {};
1
+ type PrimitiveType = "string" | "number" | "boolean" | "object" | "array";
2
+ type FieldSchema = {
3
+ type: PrimitiveType;
4
+ description?: string;
5
+ required?: boolean;
6
+ properties?: Record<string, FieldSchema>;
7
+ items?: FieldSchema;
8
+ };
9
+ type StepInputSchema = {
10
+ type: "object";
11
+ description?: string;
12
+ properties?: Record<string, FieldSchema>;
13
+ };
14
+ type StoryActionSpec = {
15
+ name: string;
16
+ description: string;
17
+ implementationKey: string;
18
+ inputSchema?: StepInputSchema;
19
+ finalize?: boolean;
20
+ };
21
+ type StoryOptions = {
22
+ reasoningEffort?: "low" | "medium" | "high";
23
+ webSearch?: boolean;
24
+ includeBaseTools?: {
25
+ createMessage?: boolean;
26
+ requestDirection?: boolean;
27
+ end?: boolean;
28
+ };
29
+ };
30
+ export declare function runReasoningOnceStep(params: {
31
+ contextId: string;
32
+ systemPrompt: string;
33
+ actions: StoryActionSpec[];
34
+ options: StoryOptions;
35
+ }): Promise<{
36
+ toolCalls: Array<{
37
+ toolCallId: string;
38
+ toolName: string;
39
+ args: any;
40
+ }>;
41
+ }>;
42
+ export {};
43
43
  //# sourceMappingURL=ai.d.ts.map