Skip to content

Commit f4a3eff

Browse files
committed
fix storybuilding
1 parent 10eb64f commit f4a3eff

File tree

13 files changed

+447
-123
lines changed

13 files changed

+447
-123
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
"dependencies": {
3939
"@aitube/broadway": "0.1.3-1",
4040
"@aitube/clap": "0.1.2",
41-
"@aitube/clapper-services": "0.1.5-1",
41+
"@aitube/clapper-services": "0.1.5-2",
4242
"@aitube/engine": "0.1.2",
4343
"@aitube/timeline": "0.1.2-4",
4444
"@fal-ai/serverless-client": "^0.13.0",

src/app/api/assistant/askAnyAssistant.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
AssistantMessage,
2828
AssistantRequest,
2929
AssistantSceneSegment,
30-
AssistantStorySentence,
30+
AssistantStoryBlock,
3131
ComputeProvider,
3232
ChatEventVisibility,
3333
} from '@aitube/clapper-services'
@@ -124,17 +124,17 @@ export async function askAnyAssistant({
124124
['human', humanTemplate],
125125
])
126126

127-
//const storySentences: AssistantStorySentence[] = fullScene.split(/(?:. |\n)/).map(storySentence => {
127+
//const storyBlocks: AssistantStorySentence[] = fullScene.split(/(?:. |\n)/).map(storySentence => {
128128
//})
129129

130-
const storySentences: AssistantStorySentence[] = [
130+
const storyBlocks: AssistantStoryBlock[] = [
131131
{
132-
sentenceId: 0,
133-
sentence: fullScene,
132+
blockId: 0,
133+
block: fullScene,
134134
},
135135
{
136-
sentenceId: 1,
137-
sentence: actionLine,
136+
blockId: 1,
137+
block: actionLine,
138138
},
139139
]
140140

@@ -151,7 +151,7 @@ export async function askAnyAssistant({
151151
// TODO put this into a type
152152
const inputData: AssistantInput = {
153153
directorRequest: prompt,
154-
storySentences,
154+
storyBlocks,
155155
sceneSegments,
156156
}
157157

@@ -162,7 +162,7 @@ export async function askAnyAssistant({
162162
let assistantMessage: AssistantMessage = {
163163
comment: '',
164164
action: AssistantAction.NONE,
165-
updatedStorySentences: [],
165+
updatedStoryBlocks: [],
166166
updatedSceneSegments: [],
167167
}
168168
try {
@@ -199,11 +199,14 @@ export async function askAnyAssistant({
199199
}
200200
),
201201
})
202+
// console.log('Lanchain success on the first time! rawResponse:', rawResponse)
202203

203204
assistantMessage = parseLangChainResponse(rawResponse)
205+
// console.log('assistantMessage:', assistantMessage)
204206
} catch (err) {
205207
// LangChain failure (this happens quite often, actually)
206208

209+
// console.log(`Langchain error:\n${err}`)
207210
let errorPlainText = `${err}`
208211

209212
// Markdown formatting failure
@@ -229,7 +232,7 @@ export async function askAnyAssistant({
229232
assistantMessage.comment = errorPlainText || ''
230233
assistantMessage.action = AssistantAction.NONE
231234
assistantMessage.updatedSceneSegments = []
232-
assistantMessage.updatedStorySentences = []
235+
assistantMessage.updatedStoryBlocks = []
233236
if (!errorPlainText) {
234237
throw new Error(
235238
`failed to repair the output from LangChain (empty string)`

src/app/api/assistant/parseLangChainResponse.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ export function parseLangChainResponse(
88
const assistantMessage: AssistantMessage = {
99
comment: '',
1010
action: AssistantAction.NONE,
11-
updatedStorySentences: [],
11+
updatedStoryBlocks: [],
1212
updatedSceneSegments: [],
1313
}
1414
/*
@@ -74,6 +74,22 @@ export function parseLangChainResponse(
7474
})
7575
}
7676
}
77+
78+
i = 0
79+
for (const block of langChainResponse.updatedStoryBlocks || []) {
80+
i++
81+
const blockId = isValidNumber(block.blockId) ? block.blockId! : i
82+
83+
// TODO: rename block.block to block.text or block.content it would be better
84+
const textBlock = `${block.block || ''}`
85+
// we assume no prompt is an error
86+
if (textBlock) {
87+
assistantMessage.updatedStoryBlocks.push({
88+
blockId,
89+
block: textBlock,
90+
})
91+
}
92+
}
7793
}
7894

7995
return assistantMessage

src/app/api/assistant/parser.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,22 @@ export const zAssistantSceneSegment = z.object({
4545
})
4646

4747
export const zAssistantStorySentence = z.object({
48-
sentenceId: z.number().describe('unique identifier'),
49-
sentence: z
48+
blockId: z.number().describe('unique identifier for the story block'),
49+
block: z
5050
.string()
51-
.describe('A sentence extracted from the story plain-text.'),
51+
.describe(
52+
'A text block extracted from the story plain text. Can be about the general story, a specific scene like the current one.'
53+
),
5254
})
5355

5456
export const zAssistantMessage = z.object({
5557
comment: z
5658
.string()
5759
.describe(
58-
'A free-form comment and chat message, allowing you to answer to the user directly.'
60+
'A free-form comment and chat message, allowing the assistant (aka you) to directly address, answer or question the director (aka user).'
5961
),
6062
action: zAssistantAction,
61-
updatedStorySentences: z.array(zAssistantStorySentence),
63+
updatedStoryBlocks: z.array(zAssistantStorySentence),
6264
updatedSceneSegments: z.array(zAssistantSceneSegment),
6365
})
6466

src/app/api/assistant/route.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { NextResponse, NextRequest } from 'next/server'
22

3-
import { AssistantRequest, AssistantMessage } from '@aitube/clapper-services'
3+
import { AssistantRequest } from '@aitube/clapper-services'
44
import { askAnyAssistant } from './askAnyAssistant'
55

66
export async function POST(req: NextRequest) {

0 commit comments

Comments
 (0)