Skip to content

Commit 4d8707a

Browse files
Implement translate tool for note.tsx (#1994)
* Implement `translate` tool for `note.tsx` And fix link handling in tool results * Remove unused `ifElse` * Prefer `null` check Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> --------- Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com>
1 parent af9942d commit 4d8707a

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ local-dev-toolshed.log
2424
local-dev-shell.log
2525
tools/ralph/logs/
2626
tools/ralph/smoketest/
27+
.worktrees

packages/patterns/note.tsx

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ import {
55
Default,
66
derive,
77
handler,
8+
llm,
89
NAME,
910
navigateTo,
1011
Opaque,
1112
OpaqueRef,
1213
patternTool,
1314
recipe,
15+
str,
1416
UI,
1517
wish,
1618
} from "commontools";
@@ -27,6 +29,7 @@ type Output = {
2729
/** The content of the note */
2830
content: Default<string, "">;
2931
grep: OpaqueRef<{ query: string }>;
32+
translate: OpaqueRef<{ language: string }>;
3033
editContent: OpaqueRef<{ detail: { value: string } }>;
3134
};
3235

@@ -164,6 +167,31 @@ const Note = recipe<Input, Output>(
164167
},
165168
{ content },
166169
),
170+
translate: patternTool(
171+
(
172+
{ language, content }: {
173+
language: string;
174+
content: string;
175+
},
176+
) => {
177+
const result = llm({
178+
system: str`Translate the content to ${language}.`,
179+
messages: derive(content, (c) => [
180+
{
181+
role: "user",
182+
content: c,
183+
},
184+
]),
185+
});
186+
187+
return derive(result, ({ pending, result }) => {
188+
if (pending) return undefined;
189+
if (result == null) return "Error occured";
190+
return result;
191+
});
192+
},
193+
{ content },
194+
),
167195
editContent: handleEditContent({ content }),
168196
};
169197
},

packages/runner/src/builtins/llm-dialog.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ async function invokeToolCall(
832832
const cancel = result.sink((r) => {
833833
r !== undefined && resolve(r);
834834
});
835-
const resultValue = await promise;
835+
let resultValue = await promise;
836836
cancel();
837837

838838
if (pattern) {
@@ -842,7 +842,10 @@ async function invokeToolCall(
842842
await runtime.idle(); // maybe pointless
843843
}
844844

845-
return { type: "json", value: resultValue ?? "OK" }; // if there was no return value, just tell the LLM it worked
845+
// Prevent links being returned
846+
resultValue = JSON.parse(JSON.stringify(resultValue ?? "OK"));
847+
848+
return { type: "json", value: resultValue };
846849
}
847850

848851
/**

0 commit comments

Comments
 (0)