u
This commit is contained in:
2
API.md
2
API.md
@@ -300,7 +300,7 @@ The store is exposed as a `LibDashStore` instance (see `src/index.js`). Each met
|
|||||||
- [modelconversation](API.modelconversation.md)
|
- [modelconversation](API.modelconversation.md)
|
||||||
- `create(googleAuthId: number, input: {provider: string, model: string, started_by?: number|null}, tx?: object): Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null}>`
|
- `create(googleAuthId: number, input: {provider: string, model: string, started_by?: number|null}, tx?: object): Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null}>`
|
||||||
- `delete(googleAuthId: number, conversationId: number, tx?: object): Promise<void>`
|
- `delete(googleAuthId: number, conversationId: number, tx?: object): Promise<void>`
|
||||||
- `get(googleAuthId: number, conversationId: number, tx?: object): Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null}>`
|
- `get(googleAuthId: number, conversationId: number, tx?: object): Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null, first_user_message: string|null}>`
|
||||||
- `list(googleAuthId: number, tx?: object): Promise<Array<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null, first_user_message: string|null}>>`
|
- `list(googleAuthId: number, tx?: object): Promise<Array<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null, first_user_message: string|null}>>`
|
||||||
- `setState(googleAuthId: number, conversationId: number, state: string, tx?: object): Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null}>`
|
- `setState(googleAuthId: number, conversationId: number, state: string, tx?: object): Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null}>`
|
||||||
- [modelmessage](API.modelmessage.md)
|
- [modelmessage](API.modelmessage.md)
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ Gets a single model conversation by ID for the user in the selected company.
|
|||||||
|
|
||||||
**Returns**
|
**Returns**
|
||||||
|
|
||||||
`Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null}>`
|
`Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null, first_user_message: string|null}>` — `first_user_message` is the stored `message` payload of the first `user` message (null if none yet).
|
||||||
|
|
||||||
**Throws**
|
**Throws**
|
||||||
|
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ async function run() {
|
|||||||
assert.deepEqual(msgs.map((m) => m.message), ['hello', 'echo: hello', 'second']);
|
assert.deepEqual(msgs.map((m) => m.message), ['hello', 'echo: hello', 'second']);
|
||||||
assert.deepEqual(msgs.map((m) => m.role), ['user', 'assistant', 'user']);
|
assert.deepEqual(msgs.map((m) => m.role), ['user', 'assistant', 'user']);
|
||||||
assert.equal((await store.modelconversation.list(userA)).find((c) => c.id === conv.id).first_user_message, 'hello');
|
assert.equal((await store.modelconversation.list(userA)).find((c) => c.id === conv.id).first_user_message, 'hello');
|
||||||
|
assert.equal((await store.modelconversation.get(userA, conv.id)).first_user_message, 'hello');
|
||||||
|
|
||||||
// Other user cannot see or write
|
// Other user cannot see or write
|
||||||
const userB = await store.google_auth.add('mc-b@example.com');
|
const userB = await store.google_auth.add('mc-b@example.com');
|
||||||
|
|||||||
@@ -9,9 +9,8 @@ export const MODELCONVERSATION_COLUMNS = `
|
|||||||
started_by
|
started_by
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const LIST_BY_COMPANYUSER = `
|
/** Raw stored payload of the conversation's first `user` message; `c` is the conversation alias. */
|
||||||
SELECT
|
const FIRST_USER_MESSAGE_COLUMN = `
|
||||||
${MODELCONVERSATION_COLUMNS},
|
|
||||||
(
|
(
|
||||||
SELECT m.message
|
SELECT m.message
|
||||||
FROM modelmessage m
|
FROM modelmessage m
|
||||||
@@ -21,6 +20,12 @@ ${MODELCONVERSATION_COLUMNS},
|
|||||||
WHERE f.conversation_id = c.id AND f.role = 'user'
|
WHERE f.conversation_id = c.id AND f.role = 'user'
|
||||||
)
|
)
|
||||||
) AS first_user_message
|
) AS first_user_message
|
||||||
|
`;
|
||||||
|
|
||||||
|
export const LIST_BY_COMPANYUSER = `
|
||||||
|
SELECT
|
||||||
|
${MODELCONVERSATION_COLUMNS},
|
||||||
|
${FIRST_USER_MESSAGE_COLUMN}
|
||||||
FROM modelconversation c
|
FROM modelconversation c
|
||||||
WHERE c.companyuser_id = @companyuserId
|
WHERE c.companyuser_id = @companyuserId
|
||||||
ORDER BY c.updated_at DESC, c.id DESC
|
ORDER BY c.updated_at DESC, c.id DESC
|
||||||
@@ -28,9 +33,10 @@ ORDER BY c.updated_at DESC, c.id DESC
|
|||||||
|
|
||||||
export const GET_BY_COMPANYUSER_AND_ID = `
|
export const GET_BY_COMPANYUSER_AND_ID = `
|
||||||
SELECT
|
SELECT
|
||||||
${MODELCONVERSATION_COLUMNS}
|
${MODELCONVERSATION_COLUMNS},
|
||||||
FROM modelconversation
|
${FIRST_USER_MESSAGE_COLUMN}
|
||||||
WHERE id = @id AND companyuser_id = @companyuserId
|
FROM modelconversation c
|
||||||
|
WHERE c.id = @id AND c.companyuser_id = @companyuserId
|
||||||
`;
|
`;
|
||||||
|
|
||||||
export const CREATE = `
|
export const CREATE = `
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export default function get(provider) {
|
|||||||
* @param {number} googleAuthId - The id of the Google auth user.
|
* @param {number} googleAuthId - The id of the Google auth user.
|
||||||
* @param {number} conversationId - The conversation id.
|
* @param {number} conversationId - The conversation id.
|
||||||
* @param {object} [tx] - Optional transaction to run within.
|
* @param {object} [tx] - Optional transaction to run within.
|
||||||
* @returns {Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null}>}
|
* @returns {Promise<{id: number, companyuser_id: number, provider: string, model: string, created_at: string, updated_at: string, state: string, started_by: number|null, first_user_message: string|null}>} `first_user_message` is the stored `message` payload of the first `user` message (null if none yet).
|
||||||
* @throws {Error} 'Conversation not found'.
|
* @throws {Error} 'Conversation not found'.
|
||||||
*/
|
*/
|
||||||
return async (googleAuthId, conversationId, tx) => {
|
return async (googleAuthId, conversationId, tx) => {
|
||||||
@@ -28,6 +28,6 @@ export default function get(provider) {
|
|||||||
|
|
||||||
const result = await runner.query(sql, { id, companyuserId });
|
const result = await runner.query(sql, { id, companyuserId });
|
||||||
if (!result.rows[0]) throw new Error('Conversation not found');
|
if (!result.rows[0]) throw new Error('Conversation not found');
|
||||||
return shape(result.rows[0]);
|
return { ...shape(result.rows[0]), first_user_message: result.rows[0].first_user_message ?? null };
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user