Skip to content

Commit 783dc77

Browse files
committed
Pass log history to AI modification of spell
1 parent 7c7d56f commit 783dc77

File tree

2 files changed

+68
-39
lines changed

2 files changed

+68
-39
lines changed

typescript/packages/lookslike-high-level/src/spells/19_process_manager.tsx

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { Ref, UiFragment } from "../sugar/zod.js";
1313
import { tsToExports } from "../localBuild.js";
1414
import { sendMessage } from "./stickers/chat.jsx";
1515
import { llm, RESPONSE } from "../effects/fetch.jsx";
16-
import { log } from "../sugar/activity.js";
16+
import { log, logEntries } from "../sugar/activity.js";
1717

1818
const adjectives = [
1919
"indigo",
@@ -176,6 +176,14 @@ export const charmViewer = typedService(CharmWithSpell, {
176176
}),
177177
});
178178

179+
const logEntries = select({
180+
self: $.self,
181+
log: [{ self: $.log, message: $.message, modified: $.modified }],
182+
})
183+
.match($.self, "common/activity", $.log)
184+
.match($.log, "message", $.message)
185+
.match($.log, "modified", $.modified);
186+
179187
const spellEditor = typedBehavior(Spell, {
180188
render: ({ self, name, sourceCode, notes }) => (
181189
<div entity={self}>
@@ -204,15 +212,31 @@ const spellEditor = typedBehavior(Spell, {
204212
cmd.add(...tagWithSchema(self, Spell));
205213
}),
206214

215+
// listLogEntries: select({
216+
// self: $.self,
217+
// log: [{ self: $.log, message: $.message, modified: $.modified }],
218+
// })
219+
// .match($.self, "common/activity", $.log)
220+
// .match($.log, "message", $.message)
221+
// .match($.log, "modified", $.modified)
222+
// .transact(({ self, log }, cmd) => {
223+
// debugger;
224+
// }),
225+
207226
onModifyWithAI: event("~/on/modify-with-ai")
208227
.with(resolve(Spell.pick({ sourceCode: true, notes: true })))
209-
.transact(({ self, event, sourceCode, notes }, cmd) => {
228+
.with(logEntries)
229+
.transact(({ self, event, sourceCode, notes, log: logEntries }, cmd) => {
210230
const ev =
211231
Session.resolve<
212232
SubmitEvent<z.infer<typeof SourceModificationPrompt>>
213233
>(event);
234+
235+
cmd.add(...log(self, "AI modification: " + ev.detail.value.prompt));
236+
214237
const message = `Modify the attached source code based on the following prompt:
215238
<context>${notes}</context>
239+
<change-history>${JSON.stringify(logEntries)}</change-history>
216240
<modification>${ev.detail.value.prompt}</modification>
217241
218242
\`\`\`js\n${sourceCode}\n\`\`\``;
@@ -968,31 +992,28 @@ export const spellManager = typedBehavior(
968992
self={focusedCharm}
969993
spell={charmViewer as any}
970994
/>
971-
{editingSpell && (
972-
<details style={detailsStyle} open>
973-
<summary style={summaryStyle}>
974-
Edit Spell
975-
<button
976-
style={buttonStyle}
977-
onclick="~/on/close-spell-editor"
978-
>
979-
Close
980-
</button>
981-
</summary>
982-
<div style={formContainerStyle}>
983-
<CharmComponent
984-
self={editingSpell}
985-
spell={spellEditor as any}
986-
/>
987-
</div>
988-
</details>
989-
)}
990995
</div>
991996
) : (
992-
<div style="display: flex; align-items: center; justify-content: center; height: 100%; color: #666;">
997+
<div style="display: flex; align-items: center; justify-content: center; height: 33%; color: #666;">
993998
<h2>Select a charm to focus</h2>
994999
</div>
9951000
)}
1001+
{editingSpell && (
1002+
<details style={detailsStyle} open>
1003+
<summary style={summaryStyle}>
1004+
Edit Spell
1005+
<button style={buttonStyle} onclick="~/on/close-spell-editor">
1006+
Close
1007+
</button>
1008+
</summary>
1009+
<div style={formContainerStyle}>
1010+
<CharmComponent
1011+
self={editingSpell}
1012+
spell={spellEditor as any}
1013+
/>
1014+
</div>
1015+
</details>
1016+
)}
9961017
</div>
9971018
</div>
9981019
);
Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,38 @@
11
import { refer, Reference } from "merkle-reference";
2-
import z from 'zod'
2+
import z from "zod";
33
import { Ref } from "./zod.js";
4-
import { importEntity } from "./sugar.jsx";
5-
import { Instruction } from "@commontools/common-system";
4+
import { importEntity, list } from "./sugar.jsx";
5+
import { $, Instruction } from "@commontools/common-system";
66

7-
export const activityRef = refer({ activity: 1 })
7+
export const activityRef = refer({ activity: 1 });
88

9-
export const LogEntry = z.object({
10-
modified: z.string(),
11-
message: z.string(),
12-
target: Ref
13-
}).describe('LogEntry')
9+
export const LogEntry = z
10+
.object({
11+
modified: z.string(),
12+
message: z.string(),
13+
target: Ref,
14+
})
15+
.describe("LogEntry");
16+
17+
export const logEntries = list(LogEntry, LogEntry.omit({ target: true })).match(
18+
$.self,
19+
"common/activity",
20+
$.listItem,
21+
);
1422

1523
export function log(entity: Reference, message: string): Instruction[] {
16-
console.log('ACTIVITY', entity, message)
24+
console.log("ACTIVITY", entity, message);
1725
const entry = {
18-
modified: (new Date()).toString(),
26+
modified: new Date().toString(),
1927
message,
20-
target: entity
21-
}
28+
target: entity,
29+
};
2230

23-
const { self, instructions } = importEntity(entry, LogEntry)
31+
const { self, instructions } = importEntity(entry, LogEntry);
2432

2533
return [
2634
...instructions,
27-
{ Assert: [activityRef, 'log', self] },
28-
{ Assert: [entity, 'common/activity', self] }
29-
]
35+
{ Assert: [activityRef, "log", self] },
36+
{ Assert: [entity, "common/activity", self] },
37+
];
3038
}

0 commit comments

Comments
 (0)