@ekairos/story 1.6.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.
Files changed (73) hide show
  1. package/dist/agent.d.ts +73 -0
  2. package/dist/agent.d.ts.map +1 -0
  3. package/dist/agent.js +479 -0
  4. package/dist/agent.js.map +1 -0
  5. package/dist/document-parser.d.ts +16 -0
  6. package/dist/document-parser.d.ts.map +1 -0
  7. package/dist/document-parser.js +157 -0
  8. package/dist/document-parser.js.map +1 -0
  9. package/dist/engine.d.ts +22 -0
  10. package/dist/engine.d.ts.map +1 -0
  11. package/dist/engine.js +36 -0
  12. package/dist/engine.js.map +1 -0
  13. package/dist/events.d.ts +28 -0
  14. package/dist/events.d.ts.map +1 -0
  15. package/dist/events.js +204 -0
  16. package/dist/events.js.map +1 -0
  17. package/dist/index.d.ts +14 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +53 -0
  20. package/dist/index.js.map +1 -0
  21. package/dist/schema.d.ts +108 -0
  22. package/dist/schema.d.ts.map +1 -0
  23. package/dist/schema.js +64 -0
  24. package/dist/schema.js.map +1 -0
  25. package/dist/service.d.ts +45 -0
  26. package/dist/service.d.ts.map +1 -0
  27. package/dist/service.js +203 -0
  28. package/dist/service.js.map +1 -0
  29. package/dist/steps/ai.d.ts +43 -0
  30. package/dist/steps/ai.d.ts.map +1 -0
  31. package/dist/steps/ai.js +136 -0
  32. package/dist/steps/ai.js.map +1 -0
  33. package/dist/steps/base.d.ts +14 -0
  34. package/dist/steps/base.d.ts.map +1 -0
  35. package/dist/steps/base.js +37 -0
  36. package/dist/steps/base.js.map +1 -0
  37. package/dist/steps/index.d.ts +5 -0
  38. package/dist/steps/index.d.ts.map +1 -0
  39. package/dist/steps/index.js +21 -0
  40. package/dist/steps/index.js.map +1 -0
  41. package/dist/steps/registry.d.ts +5 -0
  42. package/dist/steps/registry.d.ts.map +1 -0
  43. package/dist/steps/registry.js +29 -0
  44. package/dist/steps/registry.js.map +1 -0
  45. package/dist/steps/sampleStep.d.ts +2 -0
  46. package/dist/steps/sampleStep.d.ts.map +1 -0
  47. package/dist/steps/sampleStep.js +16 -0
  48. package/dist/steps/sampleStep.js.map +1 -0
  49. package/dist/steps-context.d.ts +12 -0
  50. package/dist/steps-context.d.ts.map +1 -0
  51. package/dist/steps-context.js +20 -0
  52. package/dist/steps-context.js.map +1 -0
  53. package/dist/story.d.ts +50 -0
  54. package/dist/story.d.ts.map +1 -0
  55. package/dist/story.js +55 -0
  56. package/dist/story.js.map +1 -0
  57. package/dist/storyEngine.d.ts +55 -0
  58. package/dist/storyEngine.d.ts.map +1 -0
  59. package/dist/storyEngine.js +51 -0
  60. package/dist/storyEngine.js.map +1 -0
  61. package/dist/storyRunner.d.ts +8 -0
  62. package/dist/storyRunner.d.ts.map +1 -0
  63. package/dist/storyRunner.js +56 -0
  64. package/dist/storyRunner.js.map +1 -0
  65. package/dist/workflows/index.d.ts +2 -0
  66. package/dist/workflows/index.d.ts.map +1 -0
  67. package/dist/workflows/index.js +18 -0
  68. package/dist/workflows/index.js.map +1 -0
  69. package/dist/workflows/sampleWorkflow.d.ts +8 -0
  70. package/dist/workflows/sampleWorkflow.d.ts.map +1 -0
  71. package/dist/workflows/sampleWorkflow.js +19 -0
  72. package/dist/workflows/sampleWorkflow.js.map +1 -0
  73. package/package.json +52 -0
@@ -0,0 +1,108 @@
1
+ export declare const storyDomain: import("@ekairos/domain").DomainInstance<{
2
+ readonly story_contexts: import("@instantdb/core").EntityDef<{
3
+ createdAt: import("@instantdb/core").DataAttrDef<Date, true, false>;
4
+ updatedAt: import("@instantdb/core").DataAttrDef<Date, false, false>;
5
+ type: import("@instantdb/core").DataAttrDef<string, false, false>;
6
+ key: import("@instantdb/core").DataAttrDef<string, false, true>;
7
+ status: import("@instantdb/core").DataAttrDef<string, false, true>;
8
+ content: import("@instantdb/core").DataAttrDef<any, false, false>;
9
+ }, {}, void>;
10
+ readonly story_events: import("@instantdb/core").EntityDef<{
11
+ channel: import("@instantdb/core").DataAttrDef<string, true, true>;
12
+ createdAt: import("@instantdb/core").DataAttrDef<Date, true, true>;
13
+ type: import("@instantdb/core").DataAttrDef<string, false, true>;
14
+ content: import("@instantdb/core").DataAttrDef<any, false, false>;
15
+ status: import("@instantdb/core").DataAttrDef<string, false, true>;
16
+ }, {}, void>;
17
+ readonly story_executions: import("@instantdb/core").EntityDef<{
18
+ createdAt: import("@instantdb/core").DataAttrDef<Date, true, false>;
19
+ updatedAt: import("@instantdb/core").DataAttrDef<Date, false, false>;
20
+ status: import("@instantdb/core").DataAttrDef<string, false, true>;
21
+ }, {}, void>;
22
+ }, {
23
+ readonly storyContextsOrganization: {
24
+ readonly forward: {
25
+ readonly on: "story_contexts";
26
+ readonly has: "one";
27
+ readonly label: "organization";
28
+ };
29
+ readonly reverse: {
30
+ readonly on: "organizations";
31
+ readonly has: "many";
32
+ readonly label: "story_contexts";
33
+ };
34
+ };
35
+ readonly storyEventsOrganization: {
36
+ readonly forward: {
37
+ readonly on: "story_events";
38
+ readonly has: "one";
39
+ readonly label: "organization";
40
+ };
41
+ readonly reverse: {
42
+ readonly on: "organizations";
43
+ readonly has: "many";
44
+ readonly label: "story_events";
45
+ };
46
+ };
47
+ readonly storyEventsContext: {
48
+ readonly forward: {
49
+ readonly on: "story_events";
50
+ readonly has: "one";
51
+ readonly label: "context";
52
+ };
53
+ readonly reverse: {
54
+ readonly on: "story_contexts";
55
+ readonly has: "many";
56
+ readonly label: "events";
57
+ };
58
+ };
59
+ readonly storyExecutionsContext: {
60
+ readonly forward: {
61
+ readonly on: "story_executions";
62
+ readonly has: "one";
63
+ readonly label: "context";
64
+ };
65
+ readonly reverse: {
66
+ readonly on: "story_contexts";
67
+ readonly has: "many";
68
+ readonly label: "executions";
69
+ };
70
+ };
71
+ readonly storyContextsCurrentExecution: {
72
+ readonly forward: {
73
+ readonly on: "story_contexts";
74
+ readonly has: "one";
75
+ readonly label: "currentExecution";
76
+ };
77
+ readonly reverse: {
78
+ readonly on: "story_executions";
79
+ readonly has: "one";
80
+ readonly label: "currentOf";
81
+ };
82
+ };
83
+ readonly storyExecutionsTrigger: {
84
+ readonly forward: {
85
+ readonly on: "story_executions";
86
+ readonly has: "one";
87
+ readonly label: "trigger";
88
+ };
89
+ readonly reverse: {
90
+ readonly on: "story_events";
91
+ readonly has: "many";
92
+ readonly label: "executionsAsTrigger";
93
+ };
94
+ };
95
+ readonly storyExecutionsReaction: {
96
+ readonly forward: {
97
+ readonly on: "story_executions";
98
+ readonly has: "one";
99
+ readonly label: "reaction";
100
+ };
101
+ readonly reverse: {
102
+ readonly on: "story_events";
103
+ readonly has: "many";
104
+ readonly label: "executionsAsReaction";
105
+ };
106
+ };
107
+ }, {}>;
108
+ //# sourceMappingURL=schema.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AA+DA,eAAO,MAAM,WAAW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;MAAqC,CAAC"}
package/dist/schema.js ADDED
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.storyDomain = void 0;
4
+ const core_1 = require("@instantdb/core");
5
+ const domain_1 = require("@ekairos/domain");
6
+ const entities = {
7
+ story_contexts: core_1.i.entity({
8
+ createdAt: core_1.i.date(),
9
+ updatedAt: core_1.i.date().optional(),
10
+ type: core_1.i.string().optional(),
11
+ key: core_1.i.string().optional().indexed().unique(),
12
+ status: core_1.i.string().optional().indexed(), // open | executing
13
+ content: core_1.i.any().optional(),
14
+ }),
15
+ story_events: core_1.i.entity({
16
+ channel: core_1.i.string().indexed(),
17
+ createdAt: core_1.i.date().indexed(),
18
+ type: core_1.i.string().optional().indexed(),
19
+ content: core_1.i.any().optional(),
20
+ status: core_1.i.string().optional().indexed(),
21
+ }),
22
+ story_executions: core_1.i.entity({
23
+ createdAt: core_1.i.date(),
24
+ updatedAt: core_1.i.date().optional(),
25
+ status: core_1.i.string().optional().indexed(), // executing | completed | failed
26
+ }),
27
+ };
28
+ const links = {
29
+ storyContextsOrganization: {
30
+ forward: { on: "story_contexts", has: "one", label: "organization" },
31
+ reverse: { on: "organizations", has: "many", label: "story_contexts" },
32
+ },
33
+ storyEventsOrganization: {
34
+ forward: { on: "story_events", has: "one", label: "organization" },
35
+ reverse: { on: "organizations", has: "many", label: "story_events" },
36
+ },
37
+ storyEventsContext: {
38
+ forward: { on: "story_events", has: "one", label: "context" },
39
+ reverse: { on: "story_contexts", has: "many", label: "events" },
40
+ },
41
+ // Executions belong to a context
42
+ storyExecutionsContext: {
43
+ forward: { on: "story_executions", has: "one", label: "context" },
44
+ reverse: { on: "story_contexts", has: "many", label: "executions" },
45
+ },
46
+ // Current execution pointer on a context
47
+ storyContextsCurrentExecution: {
48
+ forward: { on: "story_contexts", has: "one", label: "currentExecution" },
49
+ reverse: { on: "story_executions", has: "one", label: "currentOf" },
50
+ },
51
+ // Link execution to its trigger event
52
+ storyExecutionsTrigger: {
53
+ forward: { on: "story_executions", has: "one", label: "trigger" },
54
+ reverse: { on: "story_events", has: "many", label: "executionsAsTrigger" },
55
+ },
56
+ // Link execution to its reaction event
57
+ storyExecutionsReaction: {
58
+ forward: { on: "story_executions", has: "one", label: "reaction" },
59
+ reverse: { on: "story_events", has: "many", label: "executionsAsReaction" },
60
+ },
61
+ };
62
+ const rooms = {};
63
+ exports.storyDomain = (0, domain_1.domain)({ entities, links, rooms });
64
+ //# sourceMappingURL=schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":";;;AAAA,0CAAoC;AACpC,4CAAyC;AAEzC,MAAM,QAAQ,GAAG;IACf,cAAc,EAAE,QAAC,CAAC,MAAM,CAAC;QACvB,SA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K,EAAE,qBAAqB,EAAE;KAC3E;IACD,uCAAuC;IACvC,uBAAuB,EAAE;QACvB,OAAO,EAAE,EAAE,EAAE,EAAE,kBAAkB,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE;QAClE,OAAO,EAAE,EAAE,EAAE,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,sBAAsB,EAAE;KAC5E;CACO,CAAC;AAEX,MAAM,KAAK,GAAG,EAAW,CAAC;AAEb,QAAA,WAAW,GAAG,IAAA,eAAM,EAAC,EAAE,QAAQ,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC"}
@@ -0,0 +1,45 @@
1
+ import { InstaQLEntity } from "@instantdb/admin";
2
+ import { storyDomain } from "./schema";
3
+ export type StoredContext<Context> = Omit<InstaQLEntity<typeof storyDomain, 'story_contexts'>, 'content'> & {
4
+ content: Context;
5
+ };
6
+ export type ContextIdentifier = {
7
+ id: string;
8
+ key?: never;
9
+ } | {
10
+ key: string;
11
+ id?: never;
12
+ };
13
+ export type ContextEvent = InstaQLEntity<typeof storyDomain, 'story_events'> & {
14
+ content: any;
15
+ };
16
+ export type StreamChunk = {
17
+ type: string;
18
+ messageId?: string;
19
+ content?: string;
20
+ [key: string]: unknown;
21
+ };
22
+ export declare class AgentService {
23
+ private db;
24
+ constructor();
25
+ getOrCreateContext<C>(contextIdentifier: ContextIdentifier | null): Promise<StoredContext<C>>;
26
+ createContext<C>(contextKey?: {
27
+ key: string;
28
+ } | null, contextId?: string): Promise<StoredContext<C>>;
29
+ getContext<C>(contextIdentifier: ContextIdentifier): Promise<StoredContext<C>>;
30
+ updateContextContent<C>(contextIdentifier: ContextIdentifier, content: C): Promise<StoredContext<C>>;
31
+ saveEvent(contextIdentifier: ContextIdentifier, event: ContextEvent): Promise<ContextEvent>;
32
+ createExecution(contextIdentifier: ContextIdentifier, triggerEventId: string, reactionEventId: string): Promise<{
33
+ id: string;
34
+ }>;
35
+ completeExecution(contextIdentifier: ContextIdentifier, executionId: string, status: "completed" | "failed"): Promise<void>;
36
+ updateEvent(eventId: string, event: ContextEvent): Promise<ContextEvent>;
37
+ getEvent(eventId: string): Promise<ContextEvent>;
38
+ getEvents(contextIdentifier: ContextIdentifier): Promise<ContextEvent[]>;
39
+ readEventStream(stream: ReadableStream): Promise<{
40
+ eventId: string | undefined;
41
+ chunks: StreamChunk[];
42
+ persistedEvent: any;
43
+ }>;
44
+ }
45
+ //# sourceMappingURL=service.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,aAAa,EAAU,MAAM,kBAAkB,CAAA;AAClE,OAAO,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAEtC,MAAM,MAAM,aAAa,CAAC,OAAO,IAAI,IAAI,CAAC,aAAa,CAAC,OAAO,WAAW,EAAE,gBAAgB,CAAC,EAAE,SAAS,CAAC,GAAG;IAAE,OAAO,EAAE,OAAO,CAAA;CAAE,CAAA;AAChI,MAAM,MAAM,iBAAiB,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,KAAK,CAAA;CAAE,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,EAAE,CAAC,EAAE,KAAK,CAAA;CAAE,CAAA;AAEzF,MAAM,MAAM,YAAY,GAAG,aAAa,CAAC,OAAO,WAAW,EAAE,cAAc,CAAC,GAAG;IAAE,OAAO,EAAE,GAAG,CAAA;CAAE,CAAA;AAE/F,MAAM,MAAM,WAAW,GAAG;IACtB,IAAI,EAAE,MAAM,CAAA;IACZ,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAA;CACzB,CAAA;AAED,qBAAa,YAAY;IAErB,OAAO,CAAC,EAAE,CAAyB;;IAUtB,kBAA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AA;QAC3B,MAAM,EAAE,WAAW,EAAE,CAAA;QACrB,cAAc,EAAE,GAAG,CAAA;KACtB,CAAC;CAyCL"}
@@ -0,0 +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;
203
+ //# sourceMappingURL=service.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":";;;AAAA,4CAAkE;AAClE,qCAAsC;AActC,MAAa,YAAY;IAIrB;QACI,IAAI,CAAC,EAAE,GAAG,IAAA,YAAI,EAAC;YACX,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,0BAAoC;YACvD,UAAU,EAAE,OAAO,CAAC,GAAG,CAAC,uBAAiC;YACzD,MAAM,EAAE,oBAAW,CAAC,MAAM,EAAE;SAC/B,CAAC,CAAA;IACN,CAAC;IAEM,KAAK,CAAC,kBAAkB,CAAI,iBAA2C;QAC1E,IAAI,CAAC,iBAAiB,EAAE,CAAC;YACrB,OAAO,IAAI,CAAC,aAAa,EAAK,CAAA;QAClC,CAAC;QAED,IAAI,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAI,iBAAiB,CAAC,CAAA;QACzD,IAAI,CAAC,OAAO,EAAE,CAAC;YACX,OAAO,IAAI,CAAC,aAAa,CAAI,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,iBAAiB,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,iBAAiB,CAAC,EAAE,CAAC,CAAA;QACrH,CAAC;aAAM,CAAC;YACJ,OAAO,OAAO,CAAA;QAClB,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,aAAa,CAAI,UAAmC,EAAE,SAAkB;QACjF,IAAI,WAAW,GAA8E;YACzF,SAAS,EAAE,IAAI,IAAI,EAAE;YACr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AC;QAED,MAAM,OAAO,GAAG,UAAU,CAAC,SAAS,CAAA;QAEpC,MAAM,KAAK,GAAG,MAAM,IAAI,CAAC,EAAE,CAAC,KAAK,CAAC;YAC9B,YAAY,EAAE;gBACV,CAAC,EAAE;oBACC,KAAK,EAAE,EAAE,EAAE,EAAE,OAAO,EAAE;oBACtB,KAAK,EAAE,CAAC;oBACR,MAAM,EAAE,CAAC,IAAI,EAAE,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,WAAW,EAAE,SAAS,CAAC;iBACtE;aACJ;SACJ,CAAC,CAAA;QAEF,MAAM,cAAc,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,SAAS,CAAA;QAE5F,OAAO;YACH,OAAO;YACP,MAAM;YACN,cAAc;SACjB,CAAA;IACL,CAAC;CACJ;AAzOD,oCAyOC"}
@@ -0,0 +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 {};
43
+ //# sourceMappingURL=ai.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai.d.ts","sourceRoot":"","sources":["../../src/steps/ai.ts"],"names":[],"mappings":"AAMA,KAAK,aAAa,GAAG,QAAQ,GAAG,QAAQ,GAAG,SAAS,GAAG,QAAQ,GAAG,OAAO,CAAA;AAEzE,KAAK,WAAW,GAAG;IACjB,IAAI,EAAE,aAAa,CAAA;IACnB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;IACxC,KAAK,CAAC,EAAE,WAAW,CAAA;CACpB,CAAA;AAED,KAAK,eAAe,GAAG;IACrB,IAAI,EAAE,QAAQ,CAAA;IACd,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAA;CACzC,CAAA;AAED,KAAK,eAAe,GAAG;IACrB,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,MAAM,CAAA;IACnB,iBAAiB,EAAE,MAAM,CAAA;IACzB,WAAW,CAAC,EAAE,eAAe,CAAA;IAC7B,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED,KAAK,YAAY,GAAG;IAClB,eAAe,CAAC,EAAE,KAAK,GAAG,QAAQ,GAAG,MAAM,CAAA;IAC3C,SAAS,CAAC,EAAE,OAAO,CAAA;IACnB,gBAAgB,CAAC,EAAE;QAAE,aAAa,CAAC,EAAE,OAAO,CAAC;QAAC,gBAAgB,CAAC,EAAE,OAAO,CAAC;QAAC,GAAG,CAAC,EAAE,OAAO,CAAA;KAAE,CAAA;CAC1F,CAAA;AAgCD,wBAAsB,oBAAoB,CAAC,MAAM,EAAE;IACjD,SAAS,EAAE,MAAM,CAAA;IACjB,YAAY,EAAE,MAAM,CAAA;IACpB,OAAO,EAAE,eAAe,EAAE,CAAA;IAC1B,OAAO,EAAE,YAAY,CAAA;CACtB,GAAG,OAAO,CAAC;IAAE,SAAS,EAAE,KAAK,CAAC;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,GAAG,CAAA;KAAE,CAAC,CAAA;CAAE,CAAC,CAyGrF"}
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.runReasoningOnceStep = runReasoningOnceStep;
4
+ const ai_1 = require("ai");
5
+ const openai_1 = require("@ai-sdk/openai");
6
+ const zod_1 = require("zod");
7
+ const admin_1 = require("@instantdb/admin");
8
+ const events_1 = require("../events");
9
+ function zodFromField(field) {
10
+ switch (field.type) {
11
+ case "string":
12
+ return zod_1.z.string().describe(field.description ?? "");
13
+ case "number":
14
+ return zod_1.z.number().describe(field.description ?? "");
15
+ case "boolean":
16
+ return zod_1.z.boolean().describe(field.description ?? "");
17
+ case "array":
18
+ if (!field.items)
19
+ return zod_1.z.array(zod_1.z.any()).describe(field.description ?? "");
20
+ return zod_1.z.array(zodFromField(field.items)).describe(field.description ?? "");
21
+ case "object":
22
+ return zod_1.z.object(Object.fromEntries(Object.entries(field.properties ?? {}).map(([k, v]) => [k, zodFromField(v)])))
23
+ .describe(field.description ?? "");
24
+ default:
25
+ return zod_1.z.any();
26
+ }
27
+ }
28
+ function zodFromSchema(schema) {
29
+ if (!schema || schema.type !== "object")
30
+ return zod_1.z.object({}).strict();
31
+ const shape = {};
32
+ for (const [name, field] of Object.entries(schema.properties ?? {})) {
33
+ const base = zodFromField(field);
34
+ shape[name] = field.required ? base : base.optional();
35
+ }
36
+ const obj = zod_1.z.object(shape);
37
+ return obj;
38
+ }
39
+ async function runReasoningOnceStep(params) {
40
+ "use step";
41
+ // Construir tools para el modelo sin ejecutar (sin execute)
42
+ const tools = {};
43
+ const includeBase = params.options?.includeBaseTools || { createMessage: true, requestDirection: true, end: true };
44
+ for (const action of params.actions) {
45
+ const inputSchema = zodFromSchema(action.inputSchema);
46
+ tools[action.name] = (0, ai_1.tool)({
47
+ description: action.description,
48
+ inputSchema: inputSchema,
49
+ });
50
+ }
51
+ if (includeBase.createMessage) {
52
+ tools.createMessage = (0, ai_1.tool)({
53
+ description: "Send a message to the user. Use for final confirmations or information.",
54
+ inputSchema: zod_1.z.object({ message: zod_1.z.string().describe("Markdown content") }),
55
+ });
56
+ }
57
+ if (includeBase.requestDirection) {
58
+ tools.requestDirection = (0, ai_1.tool)({
59
+ description: "Ask a human for guidance when blocked or unsure.",
60
+ inputSchema: zod_1.z.object({ issue: zod_1.z.string(), context: zod_1.z.string(), suggestedActions: zod_1.z.array(zod_1.z.string()).optional(), urgency: zod_1.z.enum(["low", "medium", "high"]).default("medium") }),
61
+ });
62
+ }
63
+ if (includeBase.end) {
64
+ tools.end = (0, ai_1.tool)({
65
+ description: "End the current interaction loop.",
66
+ inputSchema: zod_1.z.object({}).strict(),
67
+ });
68
+ }
69
+ const providerOptions = {};
70
+ if (params.options?.reasoningEffort) {
71
+ providerOptions.openai = {
72
+ reasoningEffort: params.options.reasoningEffort,
73
+ reasoningSummary: "detailed",
74
+ };
75
+ }
76
+ const result = (0, ai_1.streamText)({
77
+ model: (0, openai_1.openai)("gpt-4o-mini"),
78
+ system: params.systemPrompt,
79
+ messages: [],
80
+ tools,
81
+ toolChoice: "required",
82
+ stopWhen: (0, ai_1.stepCountIs)(1),
83
+ ...(Object.keys(providerOptions).length > 0 && { providerOptions }),
84
+ });
85
+ result.consumeStream();
86
+ let resolveFinish;
87
+ let rejectFinish;
88
+ const finishPromise = new Promise((resolve, reject) => {
89
+ resolveFinish = resolve;
90
+ rejectFinish = reject;
91
+ });
92
+ const eventId = (0, admin_1.id)();
93
+ result
94
+ .toUIMessageStream({
95
+ sendStart: false,
96
+ generateMessageId: () => eventId,
97
+ messageMetadata() {
98
+ return { eventId };
99
+ },
100
+ onFinish: ({ messages }) => {
101
+ const lastEvent = (0, events_1.createAssistantEventFromUIMessages)(eventId, messages);
102
+ resolveFinish(lastEvent);
103
+ },
104
+ onError: (e) => {
105
+ rejectFinish(e);
106
+ const message = e instanceof Error ? e.message : String(e);
107
+ return message;
108
+ },
109
+ })
110
+ .pipeThrough(new TransformStream({
111
+ transform(chunk, controller) {
112
+ if (chunk.type === "start")
113
+ return;
114
+ if (chunk.type === "finish-step")
115
+ return;
116
+ if (chunk.type === "start-step")
117
+ return;
118
+ if (chunk.type === "finish")
119
+ return;
120
+ controller.enqueue(chunk);
121
+ },
122
+ }));
123
+ const lastEvent = await finishPromise;
124
+ const toolCalls = [];
125
+ try {
126
+ for (const p of lastEvent.content.parts || []) {
127
+ if (typeof p.type === "string" && p.type.startsWith("tool-")) {
128
+ const toolName = p.type.split("-")[1];
129
+ toolCalls.push({ toolCallId: p.toolCallId, toolName, args: p.input });
130
+ }
131
+ }
132
+ }
133
+ catch { }
134
+ return { toolCalls };
135
+ }
136
+ //# sourceMappingURL=ai.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ai.js","sourceRoot":"","sources":["../../src/steps/ai.ts"],"names":[],"mappings":";;AAkEA,oDA8GC;AAhLD,2BAA6D;AAC7D,2CAAuC;AACvC,6BAAuB;AACvB,4CAAqC;AACrC,sCAA8D;AAgC9D,SAAS,YAAY,CAAC,KAAkB;IACtC,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACnB,KAAK,QAAQ;YACX,OAAO,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,WAAW,IAAI,EAAE,CAAC,CAAA;QACrD,KAAK,QAAQ;YACX,OAAO,OAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,WAAW,IAAI,EAAE,CAAC,CAAA;QACrD,KAAK,SAAS;YACZ,OAAO,OAAC,CAAC,OAAO,EA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,MAAM,CAAC,IAAI,SAAS,CAAC,OAAO,CAAC,KAAK,IAAI,EAAE,EAAE,CAAC;YAC9C,IAAI,OAAO,CAAC,CAAC,IAAI,KAAK,QAAQ,IAAI,CAAC,CAAC,IAAI,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC7D,MAAM,QAAQ,GAAG,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;gBACrC,SAAS,CAAC,IAAI,CAAC,EAAE,UAAU,EAAE,CAAC,CAAC,UAAU,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAC,KAAK,EAAE,CAAC,CAAA;YACvE,CAAC;QACH,CAAC;IACH,CAAC;IAAC,MAAM,CAAC,CAAC,CAAC;IAEX,OAAO,EAAE,SAAS,EAAE,CAAA;AACtB,CAAC"}
@@ -0,0 +1,14 @@
1
+ export declare function executeRegisteredStep(params: {
2
+ implementationKey: string;
3
+ contextId: string;
4
+ args: any;
5
+ }): Promise<{
6
+ success: boolean;
7
+ result: any;
8
+ message?: undefined;
9
+ } | {
10
+ success: boolean;
11
+ message: any;
12
+ result?: undefined;
13
+ }>;
14
+ //# sourceMappingURL=base.d.ts.map