Repository navigation
Expand file tree
/
Copy pathprompts.ts
More file actions
424 lines (370 loc) · 15.6 KB
/
Copy pathprompts.ts
File metadata and controls
424 lines (370 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
import { randomInspiration } from './word-lists';
import { invoke, invokeStream } from './model';
import yaml from 'js-yaml';
// Generates a random item that might be found in a scrapyard.
export const generateItems = async function (itemCount: number): Promise<any> {
const randomItems: string[] = [];
const brokenessLevels: string[] = [];
for (var itemNumber = 0; itemNumber < itemCount; itemNumber++) {
const item = randomInspiration();
if (item) {
randomItems.push(item);
const brokeness = Math.floor(Math.random() * 100);
brokenessLevels.push(`${brokeness}%`);
}
}
const prompt = {
system: [
{
"text": `
You are running a dispenser that taps into a dimension full of discarded items.
Your responses must be in YAML format between two <RESULT> tags, with the following fields:
story: A tiny story about the item flying out of the dispenser
items: An array (length 1) with one object describing the item, including:
name: a descriptive name for the item, with fake brand names and model numbers where appropriate
weight: Include unit (e.g., "2.5 kg")
value: A positive integer, representing its in-world value.
description: 2–3 flavorful sentences about the item's current state and past usage
color: human readable
icon: short description of item appearance
materials: array of material types (e.g., ["Ceramic", "Metal"])
damage: A short description of damaged or missing parts
skills: array of length 1 to 3 depending on item usefulness, each containing:
name: Verb-like action performed by this item on another item, capitalized (e.g., "Absorb", "Deploy", "Smash")
description: Corny, adventurous, describes how the verb is performed on it's target
targets: Number of targets. Either 0 (targets self), 1 (targets one other item), or 2 (joins two other items)
`
},
{
"cachePoint": {
"type": "default"
}
}
],
messages: [
{
role: 'user',
content: [
{
text: `Generate a single item that might come out of the dispenser, drawing random inspiration from the following items: ${randomItems.map((item, i) => `${item} (${brokenessLevels[i]} damaged)`).join(', ')}`
}
]
}
]
};
const result = await invoke(prompt);
if (!result) return null;
const resultMatch = result.match(/<RESULT>([\s\S]*?)<\/RESULT>/);
const resultContent = resultMatch ? resultMatch[1].trim() : result;
return yaml.load(resultContent);
};
// The only properties that the LLM should be seeing.
function filterItemProperties(item) {
return {
id: item.id,
name: item.name,
weight: item.weight,
value: item.value,
description: item.description,
color: item.color,
icon: item.icon,
materials: item.materials,
damage: item.damage,
skills: item.skills
}
}
// Maps original IDs to short letter IDs (a, b, c, etc.)
// This is an optimization to save token in the LLM. No need to send
// full GUID's through the LLM.
function createIdMapping(toolItem: any, targetItems: any[]): { idToShortId: Record<string, string>, shortIdToId: Record<string, string> } {
const idToShortId: Record<string, string> = {};
const shortIdToId: Record<string, string> = {};
// Start with lowercase letters
const letters = 'abcdefghijklmnopqrstuvwxyz';
let letterIndex = 0;
// Map tool item ID first
if (toolItem && toolItem.id) {
const shortId = letters[letterIndex++];
idToShortId[toolItem.id] = shortId;
shortIdToId[shortId] = toolItem.id;
}
// Map target item IDs
if (targetItems && targetItems.length) {
for (const item of targetItems) {
if (item && item.id && !idToShortId[item.id]) {
const shortId = letters[letterIndex++];
idToShortId[item.id] = shortId;
shortIdToId[shortId] = item.id;
}
}
}
return { idToShortId, shortIdToId };
}
// Replace original IDs with short IDs in an item
function replaceWithShortIds(item: any, idToShortId: Record<string, string>): any {
if (!item) return item;
const newItem = { ...item };
if (newItem.id && idToShortId[newItem.id]) {
newItem.id = idToShortId[newItem.id];
}
return newItem;
}
// Replace short IDs with original IDs in an item
function replaceWithOriginalIds(item: any, shortIdToId: Record<string, string>): any {
if (!item) return item;
const newItem = { ...item };
if (newItem.id && shortIdToId[newItem.id]) {
newItem.id = shortIdToId[newItem.id];
}
return newItem;
}
// Appraise an item to determine its market value
export const appraiseItem = async function (item: any): Promise<any> {
// Filter item properties to only include what's needed for appraisal
const itemToAppraise = filterItemProperties(item);
const prompt = {
system: [
{
"text": `
You are an experienced appraiser in a fantasy crafting game.
Your job is to evaluate items and determine their market value.
You are very sensitive to the condition of items, and you treat
the "value" field on the item as the maximum you would be willing
to pay for the item if it was in perfect condition.
You will be disappointed, and reduce your appraisal based
on the following factors:
- Material quality and rarity
- Condition (damage level)
- Utility (practicality of skills and functionality)
- Some wiggle room for uniqueness and collectability
Your responses must be in YAML format between two <RESULT> tags, with the following fields:
appraisal:
analysis: A brief, colorful analysis of the item (2-3 sentences)
saleAmount: The amount of gold the appraiser is willing to pay
happiness: A number between -100 and 100 indicating how happy/unhappy the appraiser is with the item
# -100: Extremely disappointed, item is broken, worthless, or offensive
# 0: Neutral, item is average
# 100: Extremely excited, item is exceptional
`
},
{
"cachePoint": {
"type": "default"
}
}
],
messages: [
{
role: 'user',
content: [
{
text: `Please appraise this item: ${JSON.stringify(itemToAppraise)}`
}
]
}
]
};
const result = await invoke(prompt);
if (!result) return null;
const resultMatch = result.match(/<RESULT>([\s\S]*?)<\/RESULT>/);
const resultContent = resultMatch ? resultMatch[1].trim() : result;
return yaml.load(resultContent);
};
// Interface for streaming skill use callbacks
export interface SkillStreamCallbacks {
onStory?: (story: string) => void;
onTool?: (tool: any) => void;
onOutputItem?: (item: any) => void;
onRemovedItemId?: (id: string) => void;
onComplete?: (result: any) => void;
}
// Use one item's skill on one or more other items with streaming response
export const useSkillStream = async function (
toolItem: any,
skillIndex: any,
targetItems: any,
callbacks: SkillStreamCallbacks
): Promise<void> {
// Create ID mappings for tool item and target items
const { idToShortId, shortIdToId } = createIdMapping(toolItem, targetItems);
// Create copies with short IDs
const shortIdToolItem = replaceWithShortIds(filterItemProperties(toolItem), idToShortId);
const shortIdTargetItems = targetItems.map(item => replaceWithShortIds(filterItemProperties(item), idToShortId));
const prompt = {
system: [
{
"text": `
You are the reasonable DM of a casual dungeons and dragons game among friends.
You are simulating the results of using a crafting skill.
The tool item is the source of the skill. Consider the skill and the tool item's
condition. Then think about what type of changes the skill could produce.
The skill may produce changes to the tool item (such as durability loss)
as well as changes to any given target items.
Skills may be destructive or deconstructive. In this case they may destroy tools
or target items to create new items, for example a destructive "Smash" skill may
break an item into new pieces that represent broken parts of the item. A deconstructive
skill like "Unscrew" might cleanly detach a component. A deconstructive
skill like "Cut" might cut off part of an item. Prioritize removing entire named
components of the target item, for example if smashing a "clock" it might produce
"bent hands", "clock springs", and "clock face".
Skills may join one item to another item, for example a "Glue" skill or "Screw"
skill may attach two items to each other, yielding a single new item. Attempt
to produce a coherent new item. For example attaching a "spring" to a "board"
that might produce a "mouse trap".
Skills may change the properties of tools or target items, for example "Drill"
might add a hole to an item, "Paint" might change its color,
"Enchant" might infused it with magic.
Skills may reveal new items, for example an "Open" skill might reveal a new item
that was inside of an existing item, or a "Find" or "Identify" skill might
discover some new aspect of a target object.
You must structure your response using specific XML tags for each part:
<STORY>A three to four sentence, first person perspective story about the skill being used on any targets, and the outcome</STORY>
<ITEM>
# YAML representation of a resulting item.
# Use one ITEM tag per item. This includes
# tools, target items, and any new items that result.
# Include all required item properties
</ITEM>
<REMOVED_ITEM>itemId</REMOVED_ITEM> # An item that is removed must not have an ITEM tag.
Items must have the following format:
id: You may change the values of any item property (except ID) if that seems realistic,
but ID's are immutable and may not be reused for
new items. New items get their own special ID "new-item".
name: a descriptive name for the item, with fake brand names and model numbers where appropriate
weight: Include unit (e.g., "2.5 kg")
value: A positive integer. Successful crafting interactions increase value
description: 2–3 flavorful sentences about the item's current state and past usage
color: human readable
icon: short description of item appearance
materials: array of material types (e.g., ["Ceramic", "Metal"])
damage: A short description of damaged or missing parts
skills[] - length 1 to 3 depending on item state. Add skills to items if they have none, but do not exceed 5 skills.
name: Verb-like action performed by this item on another item, capitalized (e.g., "Absorb", "Deploy", "Smash")
description: Corny, adventurous, describes how the verb is performed on it's target
targets: Number of targets. Either 0 (targets self), 1 (targets one other item), or 2 (joins two other items)
`
},
{
"cachePoint": {
"type": "default"
}
}
],
messages: [
{
role: 'user',
content: [
{
text: `Tool Item: ${JSON.stringify(shortIdToolItem)}
Tool Skill Index: ${skillIndex}
Target Item(s): ${JSON.stringify(shortIdTargetItems)} `
}
]
}
]
};
// Buffers to accumulate partial XML tags
let buffer = '';
let storyContent = '';
let toolContent = '';
const outputItems: any[] = [];
const removedItemIds: string[] = [];
// Process each chunk as it arrives
await invokeStream(prompt, (chunk) => {
// Add the new chunk to our buffer
buffer += chunk;
// Process any complete XML tags in the buffer
processBuffer();
}, (fullResponse) => {
// Process any remaining content in the buffer
buffer += ' '; // Add a space to help with regex matching
processBuffer(true);
// Construct the final result object
let result;
try {
result = {
story: storyContent,
tool: toolContent ? yaml.load(toolContent) : null,
outputItems: outputItems.map(item => yaml.load(item)),
removedItemIds: removedItemIds
};
} catch (e) {
console.error('Error parsing tool YAML:', e);
if (callbacks.onComplete) {
callbacks.onComplete({});
}
return;
}
// Convert short IDs back to original IDs
if (result.tool) {
result.tool = replaceWithOriginalIds(result.tool, shortIdToId);
}
if (result.outputItems && Array.isArray(result.outputItems)) {
result.outputItems = result.outputItems.map(item =>
replaceWithOriginalIds(item, shortIdToId)
);
}
if (result.removedItemIds && Array.isArray(result.removedItemIds)) {
result.removedItemIds = result.removedItemIds.map(id =>
shortIdToId[id] || id
);
}
// Call the completion callback with the final result
if (callbacks.onComplete) {
callbacks.onComplete(result);
}
});
// Helper function to process the buffer for complete XML tags
function processBuffer(isFinal = false) {
// Process <STORY> tags
let storyMatch;
const storyRegex = /<STORY>([\s\S]*?)<\/STORY>/g;
while ((storyMatch = storyRegex.exec(buffer)) !== null) {
const story = storyMatch[1].trim();
storyContent = story; // Store the story content
if (callbacks.onStory) {
callbacks.onStory(story);
}
// Remove the processed tag from the buffer
buffer = buffer.replace(storyMatch[0], '');
}
// Process <ITEM> tags
let itemMatch;
const itemRegex = /<ITEM>([\s\S]*?)<\/ITEM>/g;
while ((itemMatch = itemRegex.exec(buffer)) !== null) {
const itemYaml = itemMatch[1].trim();
outputItems.push(itemYaml); // Store the output item YAML
if (callbacks.onOutputItem) {
try {
const item = yaml.load(itemYaml);
// Convert short IDs back to original IDs before callback
const itemWithOriginalIds = replaceWithOriginalIds(item, shortIdToId);
callbacks.onOutputItem(itemWithOriginalIds);
} catch (e) {
console.error('Error parsing output item YAML:', e);
}
}
// Remove the processed tag from the buffer
buffer = buffer.replace(itemMatch[0], '');
}
// Process <REMOVED_ITEM> tags
let removedMatch;
const removedRegex = /<REMOVED_ITEM>(.*?)<\/REMOVED_ITEM>/g;
while ((removedMatch = removedRegex.exec(buffer)) !== null) {
const itemId = removedMatch[1].trim();
removedItemIds.push(itemId); // Store the removed item ID
if (callbacks.onRemovedItemId) {
// Convert short ID back to original ID before callback
const originalId = shortIdToId[itemId] || itemId;
callbacks.onRemovedItemId(originalId);
}
// Remove the processed tag from the buffer
buffer = buffer.replace(removedMatch[0], '');
}
// If this is the final processing and we still have content in the buffer,
// try to extract any partial tags or remaining content
if (isFinal && buffer.trim()) {
console.log('Remaining buffer content:', buffer);
}
}
};